def test_getFiles(self): # create setofImages dbFn = self.getOutputPath('multistack_set.sqlite') # dbFn = ':memory:' n = 10 m = 3 img = emobj.Particle() imgSet = emobj.SetOfParticles(filename=dbFn) imgSet.setSamplingRate(1.0) goldStacks = set() for j in range(1, m + 1): stackName = 'stack%02d.stk' % j goldStacks.add(stackName) for i in range(1, n + 1): img.setLocation(i, stackName) imgSet.append(img) img.cleanObjId() self.assertEquals(goldStacks, imgSet.getFiles()) imgSet.close() # It should automatically call load # before accessing items db imgSet.getFirstItem()
def test_hugeSet(self): """ Create a set of a big number of particles to measure creation time with sqlite operations. """ # Allow what huge means to be defined with environment var n = int(os.environ.get('SCIPION_TEST_HUGE', 10000)) print(">>>> Creating a set of %d particles." % n) print(" (set SCIPION_TEST_HUGE environment var to other value)") dbFn = self.getOutputPath('huge_set.sqlite') # dbFn = ':memory:' img = emobj.Particle() imgSet = emobj.SetOfParticles(filename=dbFn) imgSet.setSamplingRate(1.0) for i in range(1, n + 1): # Creating object inside the loop significantly # decrease performance # img = Particle() img.setLocation(i, "images.stk") imgSet.append(img) img.cleanObjId() # img.setObjId(None) imgSet.write()
def test_clone(self): """ Check that cloning the Transform will also copy the values of underlying numpy matrix. """ t = emobj.Transform() m = t.getMatrix() m[0, 3] = 2 m[1, 3] = 4 t2 = t.clone() m2 = t2.getMatrix() self.assertTrue(np.allclose(m, m2, rtol=1e-2)) p = emobj.Particle() p.setTransform(t) p2 = p.clone() m3 = p2.getTransform().getMatrix() self.assertTrue(np.allclose(m, m3, rtol=1e-2))
def test_orderBy(self): # create setofProjections sorted imgSet1 = emobj.SetOfParticles(filename=':memory:', prefix='set1') imgSet2 = emobj.SetOfParticles(filename=':memory:', prefix='set2') imgSet1.setSamplingRate(1.5) imgSet2.setSamplingRate(1.5) img = emobj.Particle() for i in range(1, 10): img.setLocation(i, 'mystack.stk') img.setMicId(10 - i) img.setClassId(i % 5) imgSet1.append(img) img.setMicId(i) imgSet2.append(img) img.cleanObjId() # orderby for item1, item2 in zip( imgSet1.iterItems(orderBy='_micId', direction='ASC'), imgSet2.iterItems(orderBy='_micId', direction='ASC')): self.assertEquals(item1.getMicId(), item2.getMicId())
def test_mapper(self): """ test that indexes are created when a setOfParticles is created """ PARTNUMBER = 10 indexesNames = ['_classId', '_micId'] prot = createDummyProtocol("dummy_protocol") # create set of particles partSet = prot._createSetOfParticles() part = emobj.Particle() for i in range(PARTNUMBER + 1): part.setLocation(i, "stack.vol") partSet.append(part) part.cleanObjId() partSet.write() # check defined indexes setOfPArticleFileName = prot._getPath("particles.sqlite") # print os.path.abspath(setOfPArticleFileName) indexes = sorted( [index[1] for index in getIndex(setOfPArticleFileName)]) for index, indexName in zip(indexes, indexesNames): self.assertEqual(index, 'index_' + indexName)
def _checkNewItems(self, objSet): """ Check for already computed micrograph/movie and update the output set. """ objDict = {} newObj = False for obj in objSet: objDict[obj.getFileName()] = True if objDict: if objSet.getSize(): objSet.enableAppend() objSet.loadAllProperties() else: objSet.setStreamState(objSet.STREAM_OPEN) acquisition = emobj.Acquisition() if self.setof == SET_OF_MICROGRAPHS: acquisition.setMagnification( self.inputMics.get().getAcquisition().getMagnification()) acquisition.setVoltage( self.inputMics.get().getAcquisition().getVoltage()) acquisition.setSphericalAberration( self.inputMics.get().getAcquisition().getSphericalAberration()) acquisition.setAmplitudeContrast( self.inputMics.get().getAcquisition().getAmplitudeContrast()) objSet.setAcquisition(acquisition) objSet.setSamplingRate(self.inputMics.get().getSamplingRate()) elif self.setof == SET_OF_MOVIES: acquisition.setMagnification( self.inputMovies.get().getAcquisition().getMagnification()) acquisition.setVoltage( self.inputMovies.get().getAcquisition().getVoltage()) acquisition.setSphericalAberration( self.inputMovies.get().getAcquisition().getSphericalAberration()) acquisition.setAmplitudeContrast( self.inputMovies.get().getAcquisition().getAmplitudeContrast()) objSet.setAcquisition(acquisition) objSet.setSamplingRate( self.inputMovies.get().getSamplingRate()) else: acquisition.setMagnification(self._magnification) acquisition.setVoltage(self._voltage) acquisition.setSphericalAberration(self._sphericalAberration) acquisition.setAmplitudeContrast(self._amplitudeContrast) objSet.setAcquisition(acquisition) if self.setof == SET_OF_PARTICLES: objSet.setSamplingRate( self.inputParticles.get().getSamplingRate()) else: objSet.setSamplingRate(self.samplingRate.get()) if self.setof == SET_OF_MOVIES: obj = emobj.Movie() elif self.setof == SET_OF_MICROGRAPHS: obj = emobj.Micrograph() elif self.setof == SET_OF_RANDOM_MICROGRAPHS: obj = emobj.Micrograph() elif self.setof == SET_OF_PARTICLES: obj = emobj.Particle() else: raise Exception('Unknown data type') for k, v in self.dictObj.items(): if k not in objDict: self.counter += 1 obj.setFileName(k) if self.setof != SET_OF_PARTICLES: obj.setMicName(basename(k)) obj.setObjId(self.counter) objSet.append(obj) newObj = True return objSet, newObj # why a dictionary, a boolean may be enough