def test_init_empty_file(self): with ArtificialND2('test_data/empty.nd2', skip_blocks=['label_map_marker']): with self.assertRaises(EmptyFileError) as exception: with ND2Reader('test_data/empty.nd2'): pass self.assertEqual(str(exception.exception), "No axes were found for this .nd2 file.")
def test_read_chunk_fail_bad_header(self): with ArtificialND2(self.test_file) as artificial: fh = artificial.file_handle chunk_location = artificial.locations['image_attributes'][0] with self.assertRaises(ValueError) as context: read_chunk(fh, chunk_location + 1) self.assertEquals(str(context.exception), "The ND2 file seems to be corrupted.")
def test_get_frame_2D(self): # Best test we can do for now: # test everything up to the actual unpacking of the frame data with ArtificialND2('test_data/test_nd2_reader.nd2') as _: with ND2Reader('test_data/test_nd2_reader.nd2') as reader: with self.assertRaises(struct.error) as exception: frame = reader.get_frame_2D(c=0, t=0, z=0, x=0, y=0, v=0) self.assertIn('unpack', str(exception.exception))
def test_init_and_init_axes(self): with ArtificialND2('test_data/test_nd2_reader.nd2') as artificial: with ND2Reader('test_data/test_nd2_reader.nd2') as reader: attributes = artificial.data['image_attributes']['SLxImageAttributes'] self.assertEqual(reader.metadata['width'], attributes['uiWidth']) self.assertEqual(reader.metadata['height'], attributes['uiHeight']) self.assertEqual(reader.metadata['width'], reader.sizes['x']) self.assertEqual(reader.metadata['height'], reader.sizes['y']) self.assertEqual(reader.pixel_type, np.float64) self.assertEqual(reader.iter_axes, ['t'])
def test_init(self): with ArtificialND2('test_data/legacy.nd2'): with warnings.catch_warnings(record=True) as w: # Cause all warnings to always be triggered. warnings.simplefilter("always") with Nd2('test_data/legacy.nd2') as reader: self.assertIsInstance(reader.reader, ND2Reader) self.assertTrue(issubclass(w[0].category, DeprecationWarning)) self.assertEquals( str(w[0].message), "The 'Nd2' class is deprecated, please consider using the new" + " ND2Reader interface which uses pims.")
def test_misc(self): with ArtificialND2('test_data/legacy.nd2'): with Nd2('test_data/legacy.nd2') as reader: representation = "\n".join([ "<Deprecated ND2 %s>" % reader.reader.filename, "Created: Unknown", "Image size: %sx%s (HxW)" % (reader.height, reader.width), "Frames: %s" % len(reader.frames), "Channels: %s" % ", ".join( ["%s" % str(channel) for channel in reader.channels]), "Fields of View: %s" % len(reader.fields_of_view), "Z-Levels: %s" % len(reader.z_levels) ]) self.assertEquals(representation, str(reader)) # not implemented yet self.assertEquals(reader.pixel_microns, None) self.assertEquals(len(reader), 1)
def test_read_chunk(self): with ArtificialND2(self.test_file) as artificial: fh = artificial.file_handle chunk_location = artificial.locations['image_attributes'][0] chunk_read = read_chunk(fh, chunk_location) real_data = six.BytesIO(artificial.raw_text) real_data.seek(chunk_location) # The chunk metadata is always 16 bytes long chunk_metadata = real_data.read(16) header, relative_offset, data_length = struct.unpack("IIQ", chunk_metadata) self.assertEquals(header, 0xabeceda) # We start at the location of the chunk metadata, skip over the metadata, and then proceed to the # start of the actual data field, which is at some arbitrary place after the metadata. real_data.seek(chunk_location + 16 + relative_offset) real_chunk = real_data.read(data_length) self.assertEqual(real_chunk, chunk_read)
def setUp(self): self.nd2 = ArtificialND2('test_data/test_nd2_raw_metadata001.nd2') self.raw_text, self.locations, self.file_data = self.nd2.raw_text, self.nd2.locations, self.nd2.data self.label_map = LabelMap(self.raw_text) self.metadata = RawMetadata(self.nd2.file_handle, self.label_map)
def create_test_nd2(self): with ArtificialND2(self.test_file) as artificial: self.assertIsNotNone(artificial.file_handle) artificial.close()
def test_get_timesteps(self): with ArtificialND2('test_data/test_nd2_reader.nd2') as _: with ND2Reader('test_data/test_nd2_reader.nd2') as reader: timesteps = reader.timesteps self.assertEquals(len(timesteps), 0)
def test_get_parser(self): with ArtificialND2('test_data/test_nd2_reader.nd2') as _: with ND2Reader('test_data/test_nd2_reader.nd2') as reader: self.assertIsInstance(reader.parser, Parser)
def test_init_from_handler(self): with ArtificialND2('test_data/test_nd2_reader.nd2') as artificial: with open('test_data/test_nd2_reader.nd2', "rb") as FH: with ND2Reader(FH) as reader: self.cmp_two_readers(artificial, reader)
def test_init_and_init_axes(self): with ArtificialND2('test_data/test_nd2_reader.nd2') as artificial: with ND2Reader('test_data/test_nd2_reader.nd2') as reader: self.cmp_two_readers(artificial, reader)
def create_test_nd2(self): with ArtificialND2(self.test_file) as artificial: artificial.close()
def setUp(self): self.nd2 = ArtificialND2('test_data/test_nd2_label_map001.nd2') self.raw_text, self.locations = self.nd2.raw_text, self.nd2.locations self.label_map = LabelMap(self.raw_text)