def analyse_dir(args): """Analyse all images in an input directory.""" for fname in os.listdir(args.input_dir): if not fname.lower().endswith(".nd2"): continue logger.info("Analysing image: {}".format(fname)) def get_dir_name(fname): no_suffix_list = fname.split(".")[0:-1] return ".".join(no_suffix_list) dir_name = get_dir_name(fname) specific_out_dir = os.path.join(args.output_dir, dir_name) # Skip analysis of image if output directory exists. if os.path.isdir(specific_out_dir): logger.info("Directory exists: {}".format(specific_out_dir)) logger.info("Skipping: {}".format(fname)) continue os.mkdir(specific_out_dir) fpath = os.path.join(args.input_dir, fname) manifest_path = convert_and_split(fpath, 'nikon_backend') microscopy_collection = MicroscopyCollection(manifest_path) analyse_all(microscopy_collection, specific_out_dir, args.threshold, args.min_voxel, args.max_voxel)
def load(self, fpath): """Load a microscopy file. :param fpath: path to microscopy file """ def is_microscopy_item(fpath): """Return True if the fpath is likely to be microscopy data. :param fpath: file path to image :returns: :class:`bool` """ l = fpath.split('.') ext = l[-1] pre_ext = l[-2] if ((ext == 'tif' or ext == 'tiff') and pre_ext != 'ome'): return False return True if not self.convert.already_converted(fpath): path_to_manifest = self.convert(fpath) else: path_to_manifest = os.path.join(self.backend.directory, _md5_hexdigest_from_file(fpath), 'manifest.json') collection = None if is_microscopy_item(fpath): collection = MicroscopyCollection(path_to_manifest) else: collection = ImageCollection(path_to_manifest) self.append(collection) return collection
def test_zstack_proxy_iterator_is_iterable(self): import collections from jicbioimage.core.image import MicroscopyCollection microscopy_collection = MicroscopyCollection() self.assertTrue( isinstance(microscopy_collection.zstack_proxy_iterator(), collections.Iterable)) self.assertTrue( isinstance(microscopy_collection.zstack_proxy_iterator(), collections.Iterable))
def get_microscopy_collection_from_tiff(input_file): """Return microscopy collection from tiff file.""" data_manager, backend_dir = get_data_manager() data_manager.load(input_file) md5_hex = _md5_hexdigest_from_file(input_file) manifest_path = os.path.join(backend_dir, md5_hex, "manifest.json") microscopy_collection = MicroscopyCollection() microscopy_collection.parse_manifest(manifest_path) return microscopy_collection
def test_proxy_image(self): from jicbioimage.core.image import MicroscopyCollection, MicroscopyImage microscopy_collection = MicroscopyCollection() microscopy_collection.append( MicroscopyImage('test0.tif', dict(series=0, channel=0, zslice=0, timepoint=0))) microscopy_collection.append( MicroscopyImage('test1.tif', dict(series=1, channel=1, zslice=1, timepoint=1))) proxy_image = microscopy_collection.proxy_image() self.assertEqual(proxy_image.fpath, 'test0.tif') proxy_image = microscopy_collection.proxy_image(s=1, c=1, z=1, t=1) self.assertEqual(proxy_image.fpath, 'test1.tif')
def test_manifest_data(self): from jicbioimage.core.image import MicroscopyCollection, MicroscopyImage microscopy_collection = MicroscopyCollection() microscopy_collection.append( MicroscopyImage('test0.tif', dict(series=0, channel=0, zslice=0, timepoint=0))) microscopy_collection.append( MicroscopyImage('test0.tif', dict(series=0, channel=1, zslice=2, timepoint=3))) microscopy_collection.append( MicroscopyImage('test1.tif', dict(series=1, channel=2, zslice=3, timepoint=4))) self.assertEqual(microscopy_collection.series, [0, 1]) self.assertEqual(microscopy_collection.channels(), [0, 1]) self.assertEqual(microscopy_collection.channels(s=1), [2]) self.assertEqual(microscopy_collection.zslices(), [0, 2]) self.assertEqual(microscopy_collection.zslices(s=1), [3]) self.assertEqual(microscopy_collection.timepoints(), [0, 3]) self.assertEqual(microscopy_collection.timepoints(s=1), [4])
def test_repr_html(self): from jicbioimage.core.image import MicroscopyCollection, MicroscopyImage, Image microscopy_collection = MicroscopyCollection() image = Image((50, 50)) image.png = MagicMock( return_value=bytearray('image', encoding='utf-8')) with patch('jicbioimage.core.image.Image.from_file', return_value=image) as patched_image: microscopy_collection.append( MicroscopyImage( 'test0.tif', dict(series=1, channel=2, zslice=3, timepoint=4))) html = microscopy_collection._repr_html_() self.assertEqual( html.strip().replace(' ', '').replace('\n', ''), ''' <div style="float: left; padding: 2px;" > <p> <table> <tr> <th>Index</th> <th>Series</th> <th>Channel</th> <th>Z-slice</th> <th>Time point</th> </tr> <tr> <td>0</td> <td>1</td> <td>2</td> <td>3</td> <td>4</td> </tr> </table> </p> <img style="margin-left: auto; margin-right: auto;" src="data:image/png;base64,aW1hZ2U=" /> </div> '''.strip().replace(' ', '').replace('\n', ''))
def test_image(self): from jicbioimage.core.image import MicroscopyCollection microscopy_collection = MicroscopyCollection() self.assertTrue(callable(microscopy_collection.image))
def test_zstack_array(self): from jicbioimage.core.image import MicroscopyCollection microscopy_collection = MicroscopyCollection() self.assertTrue(callable(microscopy_collection.zstack_array))
def test_zstack_proxy_iterator(self): from jicbioimage.core.image import MicroscopyCollection microscopy_collection = MicroscopyCollection() self.assertTrue(callable(microscopy_collection.zstack_proxy_iterator))
def test_len(self): from jicbioimage.core.image import MicroscopyCollection microscopy_collection = MicroscopyCollection() self.assertEqual(len(microscopy_collection), 0)