Example #1
0
    def test_manual_addition_of_ImageCollection_to_DataManager(self):
        # We start off by creating a :class:`jicbioimage.core.io.DataManager`.
        # This takes a backend argument. The backend provides a means to store
        # unpacked image files.
        from jicbioimage.core.io import DataManager, FileBackend
        backend = FileBackend(directory=TMP_DIR)
        data_manager = DataManager(backend=backend)

        # The :func:`jicbioimage.core.image.DataManager.conver` function is be
        # default an instance of the callable
        # :class:`jicbioimage.core.io.BFConvertWrapper` class.
        from jicbioimage.core.io import BFConvertWrapper, _md5_hexdigest_from_file
        self.assertTrue(isinstance(data_manager.convert, BFConvertWrapper))

        # We also need to import an ImageCollection
        from jicbioimage.core.image import ImageCollection

        # If the input file has not already been converted with do so.
        fpath = os.path.join(DATA_DIR, 'z-series.ome.tif')
        self.assertFalse(data_manager.convert.already_converted(fpath))
        if not data_manager.convert.already_converted(fpath):
            path_to_manifest = data_manager.convert(fpath) # unpacks and creates manifests
            self.assertEqual(path_to_manifest, os.path.join(TMP_DIR,
                                                            _md5_hexdigest_from_file(fpath),
                                                            'manifest.json'))
            image_collection = ImageCollection()
            image_collection.parse_manifest(path_to_manifest)
            self.assertEqual(len(image_collection), 5)
            data_manager.append(image_collection)
            self.assertEqual(len(data_manager), 1)
        self.assertTrue(data_manager.convert.already_converted(fpath))
    def test_parse_manifest_raises_runtime_error_if_no_filename(self):
        # Create manifest.json file without fpath.
        manifest_fp = os.path.join(TMP_DIR, 'manifest.json')
        with open(manifest_fp, 'w') as fh:
            json.dump({"nofpath": "/tmp"}, fh)

        from jicbioimage.core.image import ImageCollection
        image_collection = ImageCollection()
        with self.assertRaises(RuntimeError):
            image_collection.parse_manifest(manifest_fp)
Example #3
0
    def test_parse_manifest_raises_runtime_error_if_no_filename(self):
        # Create manifest.json file without fpath.
        manifest_fp = os.path.join(TMP_DIR, 'manifest.json')
        with open(manifest_fp, 'w') as fh:
            json.dump({"nofpath": "/tmp"}, fh)

        from jicbioimage.core.image import ImageCollection
        image_collection = ImageCollection()
        with self.assertRaises(RuntimeError):
            image_collection.parse_manifest(manifest_fp)
Example #4
0
    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_parse_manifest_raises_backwards_compatible_with_abs_paths(self):
        # Create manifest.json file without fpath.
        manifest_fp = os.path.join(TMP_DIR, 'manifest.json')
        shutil.copy(os.path.join(DATA_DIR, "tjelvar.png"), TMP_DIR)
        abs_im_fpath = os.path.join(TMP_DIR, 'tjelvar.png')
        entry = dict(filename=abs_im_fpath, series=0, channel=0, zslice=0,
                     timepoint=0)
        with open(manifest_fp, 'w') as fh:
            json.dump([entry], fh)

        from jicbioimage.core.image import ImageCollection, Image
        image_collection = ImageCollection()
        image_collection.parse_manifest(manifest_fp)
        im = image_collection[0].image
        expected_im = Image.from_file(abs_im_fpath)
        import numpy as np
        self.assertTrue(np.array_equal(im, expected_im))
    def test_repr_html(self):
        from jicbioimage.core.image import ImageCollection, ProxyImage, Image
        image_collection = ImageCollection()
        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:
            image_collection.append(ProxyImage('test0.tif'))
            html = image_collection._repr_html_()
            self.assertEqual(html.strip().replace(' ', '').replace('\n', ''),
'''
<div style="float: left; padding: 2px;" >
    <p>
        <table><tr><th>Index</th><td>0</td></tr></table>
    </p>
    <img style="margin-left: auto; margin-right: auto;" src="data:image/png;base64,aW1hZ2U=" />
</div>
'''.strip().replace(' ', '').replace('\n', ''))

        image.png.assert_called_once_with(width=300)
Example #7
0
    def test_parse_manifest_raises_backwards_compatible_with_abs_paths(self):
        # Create manifest.json file without fpath.
        manifest_fp = os.path.join(TMP_DIR, 'manifest.json')
        shutil.copy(os.path.join(DATA_DIR, "tjelvar.png"), TMP_DIR)
        abs_im_fpath = os.path.join(TMP_DIR, 'tjelvar.png')
        entry = dict(filename=abs_im_fpath,
                     series=0,
                     channel=0,
                     zslice=0,
                     timepoint=0)
        with open(manifest_fp, 'w') as fh:
            json.dump([entry], fh)

        from jicbioimage.core.image import ImageCollection, Image
        image_collection = ImageCollection()
        image_collection.parse_manifest(manifest_fp)
        im = image_collection[0].image
        expected_im = Image.from_file(abs_im_fpath)
        import numpy as np
        self.assertTrue(np.array_equal(im, expected_im))
    def test_proxy_image(self):
        from jicbioimage.core.image import ImageCollection, ProxyImage
        image_collection = ImageCollection()
        image_collection.append(ProxyImage('test0.tif'))
        image_collection.append(ProxyImage('test1.tif'))

        proxy_image = image_collection.proxy_image()
        self.assertEqual(proxy_image.fpath, 'test0.tif')

        proxy_image = image_collection.proxy_image(index=1)
        self.assertEqual(proxy_image.fpath, 'test1.tif')
 def test_has_proxy_image(self):
     from jicbioimage.core.image import ImageCollection
     image_collection = ImageCollection()
     self.assertTrue(callable(image_collection.proxy_image))
 def test_len(self):
     from jicbioimage.core.image import ImageCollection
     image_collection = ImageCollection()
     self.assertEqual(len(image_collection), 0)