def test_repeated_delimiter_text_segment(self): parser = FCSParser() raw_text = '/flow_speed/3 m//s/x/a///y/b/////' text = parser._extract_text_dict(raw_text) self.assertDictEqual(text, { 'flow_speed': '3 m/s', 'x': 'a/', 'y': 'b//' })
def test_cloning_an_fcs_file_with_a_subset_of_data(self): """ Reading a FCS file, modifying and writing to disk """ fname = file_formats['IntelliCyt iQue'] fcs_parser = FCSParser(fname) # Only save a subset of the data points new_fcs_parser = fcs_parser.clone(data=fcs_parser.data[:1000]) with tempfile.NamedTemporaryFile(suffix='.fcs') as written_fcs: new_fcs_parser.write_to_file(written_fcs.name) new_fcs_parser = FCSParser(written_fcs.name) self.assertTrue(new_fcs_parser.data.shape == fcs_parser.data[:1000].shape)
def test_reading_and_writing_fcs_file(self): """ Reading a FCS file and writing to disk the same """ fname = file_formats['IntelliCyt iQue'] fcs_parser = FCSParser(fname) with tempfile.NamedTemporaryFile(suffix='.fcs') as written_fcs: fcs_parser.write_to_file(written_fcs.name) # use filecmp to compare the files self.assertTrue(filecmp.cmp(fname, written_fcs.name)) # read the written file and assert that the annotations, and data are equal new_fcs_parser = FCSParser(written_fcs.name) self.assertDictEqual(fcs_parser.annotation, new_fcs_parser.annotation) self.assertTrue(numpy.array_equal(fcs_parser.data, new_fcs_parser.data)) # compare the md5 checksum for the original and written file self._compare_file_md5(fname, written_fcs.name)
def test_reading_in_memory_fcs_file(self): """Find data segment of an in-memory FCS file.""" values = array( [[ 4.99655876159667968750e+01, -1.78884857177734375000e+02, 3.53545867919921875000e+02, -6.63189687500000000000e+04, 1.53373974609375000000e+03, 1.71934411621093750000e+03, 1.16922687500000000000e+05, -1.85308218002319335938e+00, 1.55241485595703125000e+02, -1.56457653808593750000e+03, 7.68178787231445312500e+01, 1.61987319946289062500e+02, 6.21571679687500000000e+04, 3.74284629821777343750e+01, 1.23476585388183593750e+02, 3.87178945312500000000e+04 ], [ 7.65274276733398437500e+01, 8.52657958984375000000e+02, 8.35975280761718750000e+02, 1.33687671875000000000e+05, 2.55060937500000000000e+03, 2.26837988281250000000e+03, 1.47379843750000000000e+05, 4.53825866699218750000e+02, 4.73883544921875000000e+02, 1.25524226562500000000e+05, -8.64693832397460937500e+00, 9.53993377685546875000e+01, -1.18802871093750000000e+04, 8.17031021118164062500e+01, 2.03511352539062500000e+02, 5.58651601562500000000e+04 ], [ 8.48738250732421875000e+01, 1.49076705932617187500e+02, 2.49545867919921875000e+02, 7.83013671875000000000e+04, 6.49180541992187500000e+02, 5.83344177246093750000e+02, 1.45864812500000000000e+05, 1.76183197021484375000e+02, 2.59241485595703125000e+02, 8.90778906250000000000e+04, 1.03054801940917968750e+02, 1.69987319946289062500e+02, 7.94623906250000000000e+04, -6.35836944580078125000e+01, 1.13396583557128906250e+02, -6.63863593750000000000e+04 ], [ 1.02074172973632812500e+02, 1.37832305908203125000e+02, 3.85545867919921875000e+02, 4.68581250000000000000e+04, 1.88981237792968750000e+03, 1.81534411621093750000e+03, 1.36448781250000000000e+05, 3.93574859619140625000e+02, 4.83241485595703125000e+02, 1.06751273437500000000e+05, 6.74475479125976562500e+01, 1.77987319946289062500e+02, 4.96691835937500000000e+04, -3.04502067565917968750e+01, 2.20916580200195312500e+02, -1.28346718750000000000e+04 ]]) fname = FILE_IDENTIFIER_TO_PATH['mq fcs 3.0'] with open(fname, 'rb') as f: data = f.read() matrix = FCSParser.from_data(data).data diff = numpy.abs(values - matrix[0:4, :]) self.assertTrue(numpy.all(diff < 10**-8))
def __init__( self, initdata: Union["URLPath", "FCSData", tuple], channels: list = None,): """Create a new FCS object. Args: initdata: Either tuple of meta and data from fcsparser, string filepath or another FCSData object. meta: Dict of fcsmeta named tuples. Returns: FCSData object. """ if isinstance(initdata, self.__class__): self.data = initdata.data.copy() self.mask = initdata.mask.copy() self.channels = initdata.channels.copy() elif isinstance(initdata, (URLPath, str)): parser = FCSParser(str(initdata), data_set=DEFAULT_DATASET, encoding=DEFAULT_ENCODING) self.data = parser.data self.mask = np.ones(self.data.shape) if parser.channel_names_s: self.channels = create_meta_from_fcs(parser.annotation, parser.channel_names_s) else: self.channels = create_meta_from_fcs(parser.annotation, parser.channel_names_n) elif isinstance(initdata, tuple): self.data, self.mask = initdata if channels is None: raise ValueError("Channels needed when initializing from np data") self.channels = create_meta_from_data(self.data, channels) else: raise RuntimeError( "Invalid data for FCS. Either Path, similar object or tuple of data and metadata needed.") self.data = self.data.astype("float32", copy=False) self.channels = [Marker.convert(c) for c in self.channels]