def createImageModel(cls, path): """ Create an ImageModel reading path as an emc.Image. """ image = emc.Image() loc = emc.ImageLocation(path) image.read(loc) return models.ImageModel(data=np.array(image, copy=False), location=(loc.index, loc.path))
def test_readMrc(self): mrcIO = emc.ImageFile() testDataPath = os.environ.get("EM_TEST_DATA", None) if testDataPath: # 2D image (a big one): micrograph micName = "xmipp_tutorial/micrographs/BPV_1386.mrc" micDim = emc.ArrayDim(9216, 9441, 1, 1) # 2D stack of particles stackName = "emx/alignment/Test2/stack2D.mrc" stackDim = emc.ArrayDim(128, 128, 1, 100) # 3D volumes vol1Name = "resmap/t20s_proteasome_full.map" vol1Dim = emc.ArrayDim(300, 300, 300, 1) fileDims = { micName: (micDim, 87008256), stackName: (stackDim, 65536), # 128 * 128 * 4 vol1Name: (vol1Dim, 108000000) } for fn, (dim, size) in fileDims.items(): img = emc.Image() loc = emc.ImageLocation(os.path.join(testDataPath, fn), 1) print(">>> Reading image: ", loc.path) img.read(loc) dim.n = 1 self.assertEqual(img.getDim(), dim) self.assertEqual(img.getDataSize(), size) # Test that we can read also from string # that will be converted to ImageLocation img = emc.Image() try: img.read(emc.ImageLocation(os.path.join(testDataPath, micName))) import numpy as np a = np.array(img, copy=False) except Exception as ex: raise ex self.assertEqual(img.getDim(), micDim)
def getImage(self, imgSource, copy=False): """ Retrieve the image (from cache or from file) from the given imageSource. :param imgSource: Either ImageRef or path :param copy: If True, a copy of the image will be returned. If False, a reference to the internal buffer image will be returned. The internal image can be modified in further call to other methods from the ImageManager. """ imgRef = self.getRef(imgSource) imgId = self._getId(imgRef) imgOut = self._imageCache.get(imgId, None) if imgOut is None: imgRef, imgIO = self._openRO(imgSource) # Create a new image if a copy is requested # TODO: Review this when the cache is implemented imgOut = emc.Image() self._readCount += 1 imgIO.read(imgRef.index, imgOut) self._imageCache[imgId] = imgOut self._cacheSize += imgOut.getDataSize() if copy: imgOut = emc.Image(imgOut) return imgOut
def test_VolumeModel(self): volName = self.getDataPaths()[2] print("Checking %s" % volName) volModel = dv.models.VolumeModel( data=emv.utils.ImageManager().getData(volName)) minValue, maxValue = volModel.getMinMax() def _check(model): self.assertEqual(model.getDim(), (300, 300, 300)) self.assertAlmostEqual(minValue, -0.011036, places=5) self.assertAlmostEqual(maxValue, 0.027878, places=5) self.assertIsNotNone(model.getData()) _check(volModel) sliceModel = volModel.getSlicesModel(dv.models.AXIS_Z) _check(sliceModel) imageModel = volModel.getSliceImageModel(dv.models.AXIS_Z, 0) self.assertEqual(imageModel.getDim(), (300, 300)) volModel.setData(None) self.assertIsNone(volModel.getData()) self.assertIsNone(volModel.getDim()) return # 2D stack of particles stackName = "emx/alignment/Test2/stack2D.mrc" stackDim = emc.ArrayDim(128, 128, 1, 100) # 3D volumes vol1Name = "resmap/t20s_proteasome_full.map" vol1Dim = emc.ArrayDim(300, 300, 300, 1) fileDims = { volName: (micDim, 87008256), stackName: (stackDim, 65536), # 128 * 128 * 4 vol1Name: (vol1Dim, 108000000) } for fn, (dim, size) in fileDims.items(): img = emc.Image() loc = emc.ImageLocation(os.path.join(testDataPath, fn), 1) print(">>> Reading image: ", loc.path) img.read(loc) dim.n = 1 self.assertEqual(img.getDim(), dim) self.assertEqual(img.getDataSize(), size)
def __init__(self, maxCacheSize=100, maxOpenFiles=10): self._imgData = dict() # Internally convert from Mb to bytes (default 100 Mb) self._maxCacheSize = maxCacheSize * 1024 * 1024 self._maxOpenFiles = maxOpenFiles # FIXME: We can have many open ImageFile (until maxOpenFiles) # to prevent opening many times the same file self._imgIO = emc.ImageFile() self._img = emc.Image() self._lastOpenedFile = None # FIXME: Control the max size of the Cache (no limited now) # TODO: Maybe check LRUCache implementation here: # https://www.kunxi.org/2014/05/lru-cache-in-python/ self._imageCache = {} # Just for debugging purposes self._readCount = 0 self._cacheSize = 0
#!/usr/bin/env python from __future__ import print_function import emcore as emc import numpy as np import sys if __name__ == '__main__': if len(sys.argv) < 2: print("Provide input file(s). ") sys.exit(1) img = emc.Image() print("Computing stats with numpy: ") for fn in sys.argv[1:]: loc = emc.ImageLocation(fn) img.read(loc) data = np.array(img, copy=False) print("\nFile: ", fn, "\nmin:", np.amin(data), "max:", np.amax(data), "mean: %0.5f" % np.mean(data), "std: %0.5f" % np.std(data))
def test_basic(self): img = emc.Image() self.assertTrue(img.getType().isNull())