def test_wsp_export_simple_poly50(self): wsp_path = "examples/data/simple_line_example/simple_poly_and_rect_v2_poly50.wsp" fcs_path = "examples/data/simple_line_example/data_set_simple_line_100.fcs" sample_group = 'my_group' sample_id = 'data_set_simple_line_100.fcs' fks = Session(fcs_path) fks.import_flowjo_workspace(wsp_path) with BytesIO() as fh_out: fks.export_wsp(fh_out, sample_group) fh_out.seek(0) fks2 = Session(fcs_path) fks2.import_flowjo_workspace(fh_out) fks.analyze_samples(sample_group) fks_results = fks.get_gating_results(sample_group, sample_id) fks2.analyze_samples(sample_group) fks2_results = fks2.get_gating_results(sample_group, sample_id) gate_refs = fks.get_gate_ids(sample_group) self.assertEqual(len(gate_refs), 2) fks_rect1_count = fks_results.get_gate_count('rect1') fks2_rect1_count = fks2_results.get_gate_count('rect1') fks_poly1_count = fks_results.get_gate_count('poly1') fks2_poly1_count = fks2_results.get_gate_count('poly1') self.assertEqual(fks_rect1_count, 0) self.assertEqual(fks2_rect1_count, 0) self.assertEqual(fks_poly1_count, 50) self.assertEqual(fks2_poly1_count, 50)
def test_all_gates(): gml_path = 'examples/data/gate_ref/gml/gml_all_gates.xml' truth_pattern = 'examples/data/gate_ref/truth/Results*.txt' res_files = glob.glob(truth_pattern) truth_dict = {} for res_path in res_files: match = re.search("Results_(.+)\\.txt$", res_path) if match is not None: g_id = match.group(1) truth = pd.read_csv(res_path, header=None, squeeze=True, dtype='bool').values truth_dict[g_id] = truth s = Session() group_name = 'gml' s.add_sample_group(group_name, gating_strategy=gml_path) s.add_samples(data1_sample) s.assign_samples(data1_sample.original_filename, group_name) s.analyze_samples(group_name=group_name) results = s.get_gating_results(group_name, data1_sample.original_filename) for row in results.report.itertuples(): np.testing.assert_array_equal( truth_dict[row.gate_name], results.get_gate_membership(row.gate_name))
def test_load_wsp_single_quad(self): wsp_path = "examples/data/simple_diamond_example/simple_diamond_example_quad_gate.wsp" fcs_path = "examples/data/simple_diamond_example/test_data_diamond_01.fcs" fks = Session(fcs_samples=fcs_path) fks.import_flowjo_workspace(wsp_path) # FlowJo quadrant gates are not true quadrant gates, rather a collection of rectangle gates self.assertIsInstance( fks.get_gate('All Samples', 'Q1: channel_A- , channel_B+', sample_id='test_data_diamond_01.fcs'), gates.RectangleGate) fks.analyze_samples(group_name='All Samples') results = fks.get_gating_results('All Samples', 'test_data_diamond_01.fcs') gate_count_q1 = results.get_gate_count('Q1: channel_A- , channel_B+') gate_count_q2 = results.get_gate_count('Q2: channel_A+ , channel_B+') gate_count_q3 = results.get_gate_count('Q3: channel_A+ , channel_B-') gate_count_q4 = results.get_gate_count('Q4: channel_A- , channel_B-') self.assertEqual(gate_count_q1, 49671) self.assertEqual(gate_count_q2, 50596) self.assertEqual(gate_count_q3, 50330) self.assertEqual(gate_count_q4, 49403)
def test_quadrant2_gate(): gml_path = 'examples/gate_ref/gml/gml_quadrant2_gate.xml' res1_path = 'examples/gate_ref/truth/Results_FSCN-SSCN.txt' res2_path = 'examples/gate_ref/truth/Results_FSCD-SSCN-FL1N.txt' res3_path = 'examples/gate_ref/truth/Results_FSCP-SSCN-FL1N.txt' res4_path = 'examples/gate_ref/truth/Results_FSCD-FL1P.txt' res5_path = 'examples/gate_ref/truth/Results_FSCN-SSCP-FL1P.txt' s = Session() group_name = 'gml' s.add_sample_group(group_name, gating_strategy=gml_path) s.add_samples(data1_sample) s.assign_sample(data1_sample.original_filename, group_name) s.analyze_samples(sample_group=group_name) truth1 = pd.read_csv(res1_path, header=None, squeeze=True, dtype='bool').values truth2 = pd.read_csv(res2_path, header=None, squeeze=True, dtype='bool').values truth3 = pd.read_csv(res3_path, header=None, squeeze=True, dtype='bool').values truth4 = pd.read_csv(res4_path, header=None, squeeze=True, dtype='bool').values truth5 = pd.read_csv(res5_path, header=None, squeeze=True, dtype='bool').values result = s.get_gating_results(group_name, data1_sample.original_filename) np.testing.assert_array_equal(truth1, result.get_gate_indices('FSCN-SSCN')) np.testing.assert_array_equal(truth2, result.get_gate_indices('FSCD-SSCN-FL1N')) np.testing.assert_array_equal(truth3, result.get_gate_indices('FSCP-SSCN-FL1N')) np.testing.assert_array_equal(truth4, result.get_gate_indices('FSCD-FL1P')) np.testing.assert_array_equal(truth5, result.get_gate_indices('FSCN-SSCP-FL1P'))
def test_quadrant1_gate(): gml_path = 'examples/gate_ref/gml/gml_quadrant1_gate.xml' res1_path = 'examples/gate_ref/truth/Results_FL2N-FL4N.txt' res2_path = 'examples/gate_ref/truth/Results_FL2N-FL4P.txt' res3_path = 'examples/gate_ref/truth/Results_FL2P-FL4N.txt' res4_path = 'examples/gate_ref/truth/Results_FL2P-FL4P.txt' s = Session() group_name = 'gml' s.add_sample_group(group_name, gating_strategy=gml_path) s.add_samples(data1_sample) s.assign_sample(data1_sample.original_filename, group_name) truth1 = pd.read_csv(res1_path, header=None, squeeze=True, dtype='bool').values truth2 = pd.read_csv(res2_path, header=None, squeeze=True, dtype='bool').values truth3 = pd.read_csv(res3_path, header=None, squeeze=True, dtype='bool').values truth4 = pd.read_csv(res4_path, header=None, squeeze=True, dtype='bool').values s.analyze_samples(group_name) results = s.get_gating_results(group_name, data1_sample.original_filename) result1 = results.get_gate_indices('FL2N-FL4N') result2 = results.get_gate_indices('FL2N-FL4P') result3 = results.get_gate_indices('FL2P-FL4N') result4 = results.get_gate_indices('FL2P-FL4P') np.testing.assert_array_equal(truth1, result1) np.testing.assert_array_equal(truth2, result2) np.testing.assert_array_equal(truth3, result3) np.testing.assert_array_equal(truth4, result4)
def test_add_poly1_gate(): fks = Session(fcs_samples=data1_sample) fks.add_gate(poly1_gate) fks.analyze_samples() result = fks.get_gating_results('default', data1_sample.original_filename) res_path = 'examples/gate_ref/truth/Results_Polygon1.txt' truth = pd.read_csv(res_path, header=None, squeeze=True, dtype='bool').values np.testing.assert_array_equal(truth, result.get_gate_indices('Polygon1'))
def test_load_wsp_single_ellipse(self): wsp_path = "examples/simple_line_example/single_ellipse_51_events.wsp" fcs_path = "examples/simple_line_example/data_set_simple_line_100.fcs" fks = Session(fcs_samples=fcs_path) fks.import_flowjo_workspace(wsp_path) self.assertIsInstance( fks.get_gate('All Samples', 'data_set_simple_line_100.fcs', 'ellipse1'), gates.EllipsoidGate) fks.analyze_samples(sample_group='All Samples') results = fks.get_gating_results('All Samples', 'data_set_simple_line_100.fcs') gate_count = results.get_gate_count('ellipse1') self.assertEqual(gate_count, 48)
def test_wsp_fasinh_transform(self): wsp_path = "examples/data/simple_diamond_example/test_data_diamond_asinh_rect.wsp" fcs_path = "examples/data/simple_diamond_example/test_data_diamond_01.fcs" fks = Session(fcs_samples=fcs_path) fks.import_flowjo_workspace(wsp_path) self.assertIsInstance( fks.get_gate('All Samples', 'upper_right', sample_id='test_data_diamond_01.fcs'), gates.RectangleGate) fks.analyze_samples(group_name='All Samples') results = fks.get_gating_results('All Samples', 'test_data_diamond_01.fcs') gate_count = results.get_gate_count('upper_right') self.assertEqual(gate_count, 50559)
def test_quadrant_gate_relative_percent(self): gml_path = 'examples/gate_ref/gml/gml_quadrant1_gate.xml' s = Session() group_name = 'gml' s.add_sample_group(group_name, gating_strategy=gml_path) s.add_samples(data1_sample) s.assign_sample(data1_sample.original_filename, group_name) s.analyze_samples(sample_group=group_name) result = s.get_gating_results(group_name, data1_sample.original_filename) total_percent = result.get_gate_relative_percent('FL2N-FL4N') + \ result.get_gate_relative_percent('FL2N-FL4P') + \ result.get_gate_relative_percent('FL2P-FL4N') + \ result.get_gate_relative_percent('FL2P-FL4P') self.assertEqual(100.0, total_percent)
def test_add_matrix_poly4_gate(): fks = Session(fcs_samples=data1_sample) fks.add_comp_matrix(comp_matrix_01) dim1 = Dimension('PE', compensation_ref='MySpill') dim2 = Dimension('PerCP', compensation_ref='MySpill') dims = [dim1, dim2] poly_gate = gates.PolygonGate('Polygon4', None, dims, poly1_vertices) fks.add_gate(poly_gate) res_path = 'examples/gate_ref/truth/Results_Polygon4.txt' truth = pd.read_csv(res_path, header=None, squeeze=True, dtype='bool').values fks.analyze_samples() result = fks.get_gating_results('default', data1_sample.original_filename) np.testing.assert_array_equal(truth, result.get_gate_indices('Polygon4'))
def test_add_transform_asinh_range1_gate(): fks = Session(fcs_samples=data1_sample) fks.add_transform(asinh_xform1) dim1 = Dimension('FL1-H', 'uncompensated', 'AsinH_10000_4_1', range_min=0.37, range_max=0.63) dims = [dim1] rect_gate = gates.RectangleGate('ScaleRange1', None, dims) fks.add_gate(rect_gate) res_path = 'examples/gate_ref/truth/Results_ScaleRange1.txt' truth = pd.read_csv(res_path, header=None, squeeze=True, dtype='bool').values fks.analyze_samples() result = fks.get_gating_results('default', data1_sample.original_filename) np.testing.assert_array_equal(truth, result.get_gate_indices('ScaleRange1'))
""" Tests for GatingResults class """ import copy import unittest from flowkit import Session from .session_tests import test_samples_8c_full_set wsp_path = "examples/data/8_color_data_set/reused_quad_gate_with_child.wsp" group_name = 'All Samples' sample_id = '101_DEN084Y5_15_E01_008_clean.fcs' fks = Session(copy.deepcopy(test_samples_8c_full_set)) fks.import_flowjo_workspace(wsp_path, ignore_missing_files=True) fks.analyze_samples(group_name=group_name, sample_id=sample_id) results_8c_sample_008 = fks.get_gating_results(group_name=group_name, sample_id=sample_id) class GatingResultsTestCase(unittest.TestCase): def test_get_gate_count_ambiguous_raises_value_error(self): gate_name = 'some_child_gate' self.assertRaises(ValueError, results_8c_sample_008.get_gate_count, gate_name) def test_get_gate_count_with_gate_path(self): gate_name = 'some_child_gate' gate_path_1 = ('root', 'good cells', 'cd4+', 'Q2: CD107a+, IL2+') gate_count = results_8c_sample_008.get_gate_count( gate_name=gate_name, gate_path=gate_path_1)