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_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_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_extension(self): self.assertTrue('nd2' in ND2Reader.class_exts())
def test_invalid_file_extension(self): self.assertRaises(InvalidFileType, lambda: ND2Reader('test_data/invalid_extension_file.inv'))
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 Z_Stack_Images_Extractor(address, fields_of_view, z_answer): Image_Sequence = ND2Reader(address) Channel_list = Image_Sequence.metadata['channels'] # Check whether the data has one channel or two channels. If only one channel, put GUV_Channel and Protein_Channel = 0 if len(Channel_list) == 1: GUV_Channel = 0 Protein_Channel = 0 # Select correct channels for downstream import elif 'DsRed' in Channel_list[0] or '561' in Channel_list[0]: GUV_Channel = 0 Protein_Channel = 1 else: GUV_Channel = 1 Protein_Channel = 0 time_series = Image_Sequence.sizes['t'] if z_answer: z_stack = Image_Sequence.sizes['z'] Intensity_Slice = [] GUV_Slice = [] n = 0 # create progress bar pb = tqdm(range(time_series), bar_format="{l_bar}%s{bar}%s{r_bar}" % (Fore.RED, Fore.RESET)) for time in pb: pb.set_description("Converting images to numpy arrays") if z_answer: z_stack_images = [] z_stack_Intensity_images = [] for z_slice in range(z_stack): slice = Image_Sequence.get_frame_2D(c=GUV_Channel, t=time, z=z_slice, v=fields_of_view) Intensity_slice = Image_Sequence.get_frame_2D( c=Protein_Channel, t=time, z=z_slice, v=fields_of_view) z_stack_images.append(slice) z_stack_Intensity_images.append(Intensity_slice) z_stack_images = np.array(z_stack_images) z_stack_Intensity_images = np.array(z_stack_Intensity_images) else: z_stack_images = Image_Sequence.get_frame_2D(c=GUV_Channel, t=time, v=fields_of_view) z_stack_Intensity_images = Image_Sequence.get_frame_2D( c=Protein_Channel, t=time, v=fields_of_view) GUV_Slice.append(z_stack_images) Intensity_Slice.append(z_stack_Intensity_images) GUV_Slice = np.array(GUV_Slice) Intensity_Slice = np.array(Intensity_Slice) return (GUV_Slice, Intensity_Slice)
t=time, v=fields_of_view) z_stack_Intensity_images = Image_Sequence.get_frame_2D( c=Protein_Channel, t=time, v=fields_of_view) GUV_Slice.append(z_stack_images) Intensity_Slice.append(z_stack_Intensity_images) GUV_Slice = np.array(GUV_Slice) Intensity_Slice = np.array(Intensity_Slice) return (GUV_Slice, Intensity_Slice) Image_Sequence = ND2Reader(Image_Stack_Path) FOV_list = Image_Sequence.metadata['fields_of_view'] GUV_Image_list = [] Intensity_list = [] for fov in range(len(FOV_list)): GUV_Images, Image_Intensity = Z_Stack_Images_Extractor(Image_Stack_Path, fields_of_view=fov, z_answer=answer) GUV_Image_list.append(GUV_Images) Intensity_list.append(Image_Intensity) File_save_names = '.'.join(Image_Stack_Path.split(".")[:-1]) for n in range(len(FOV_list)):