def rowToAlignment(alignmentRow, alignType): """ is2D == True-> matrix is 2D (2D images alignment) otherwise matrix is 3D (3D volume alignment or projection) invTransform == True -> for xmipp implies projection """ if alignType == ALIGN_3D: raise Exception("3D alignment conversion for Relion not implemented.") is2D = alignType == ALIGN_2D inverseTransform = alignType == ALIGN_PROJ if alignmentRow.containsAny(ALIGNMENT_DICT): alignment = pwobj.Transform() angles = np.zeros(3) shifts = np.zeros(3) shifts[0] = alignmentRow.get(md.RLN_ORIENT_ORIGIN_X, 0.) shifts[1] = alignmentRow.get(md.RLN_ORIENT_ORIGIN_Y, 0.) if not is2D: angles[0] = alignmentRow.get(md.RLN_ORIENT_ROT, 0.) angles[1] = alignmentRow.get(md.RLN_ORIENT_TILT, 0.) angles[2] = alignmentRow.get(md.RLN_ORIENT_PSI, 0.) shifts[2] = alignmentRow.get(md.RLN_ORIENT_ORIGIN_Z, 0.) else: angles[2] = - alignmentRow.get(md.RLN_ORIENT_PSI, 0.) M = matrixFromGeometry(shifts, angles, inverseTransform) alignment.setMatrix(M) else: alignment = None return alignment
def _createSetOfParticles(self, setPartSqliteName, partFn, doCtf=False): # create a set of particles self.partSet = SetOfParticles(filename=setPartSqliteName) self.partSet.setAlignment(ALIGN_PROJ) self.partSet.setAcquisition(Acquisition(voltage=300, sphericalAberration=2, amplitudeContrast=0.1, magnification=60000)) self.partSet.setSamplingRate(samplingRate) if doCtf: self.partSet.setHasCTF(True) aList = [np.array(m) for m in mList] for i, a in enumerate(aList): p = Particle() if doCtf: defocusU = defocusList[i] defocusV = defocusList[i] ctf = CTFModel(defocusU=defocusU, defocusV=defocusV, defocusAngle=defocusAngle[i]) ctf.standardize() p.setCTF(ctf) p.setLocation(i + 1, partFn) p.setTransform(emobj.Transform(a)) self.partSet.append(p) self.partSet.write()
def createOutputStep(self): tilt_series = self.tiltSeries.get() out_tilt_series = self.getOutputSetOfTiltSeries(tilt_series) json_paths = self._getJsonPaths() for tilt_serie in tilt_series.iterItems(): out_tilt_serie = tomoObj.TiltSeries(tsId=tilt_serie.getTsId()) out_tilt_serie.copyInfo(tilt_serie) out_tilt_series.append(out_tilt_serie) item_basename = os.path.basename(os.path.dirname(tilt_serie.getFirstItem().getFileName())) +\ '-' + tilt_serie.getTsId() + '_info' json_path = [ path for path in json_paths if item_basename == pwutils.removeBaseExt(path) ] params = loadJson(json_path[0])['tlt_params'] for idx, tiltImage in enumerate(tilt_serie.iterItems()): out_tiltImage = tomoObj.TiltImage() out_tiltImage.copyInfo(tiltImage, copyId=True) out_tiltImage.setLocation(tiltImage.getLocation()) matrix = np.eye(3) matrix[0, 0] = matrix[1, 1] = np.cos(np.deg2rad(params[idx][2])) matrix[0, 1], matrix[1, 0] = np.sin(np.deg2rad(params[idx][2])), \ -np.sin(np.deg2rad(params[idx][2])) out_tiltImage.tiltAngleAxis = Float(params[idx][4]) out_tiltImage.setTiltAngle(params[idx][3]) matrix[0, 2], matrix[1, 2] = params[idx][0], params[idx][1] transform = data.Transform() transform.setMatrix(matrix) out_tiltImage.setTransform(transform) out_tilt_serie.append(out_tiltImage) self._defineOutputs(alignedTiltSeries=out_tilt_series) self._defineSourceRelation(self.tiltSeries, out_tilt_series)
def introduceRandomMisalignment(self, tsObjId): ts = self.inputSetOfTiltSeries.get()[tsObjId] tsId = ts.getTsId() outputMisalignedSetOfTiltSeries = self.getOutputMisalignedSetOfTiltSeries( ) missAliTs = tomoObj.TiltSeries(tsId=tsId) missAliTs.copyInfo(ts) outputMisalignedSetOfTiltSeries.append(missAliTs) for index, ti in enumerate(ts): missAliTi = tomoObj.TiltImage() missAliTi.copyInfo(ti, copyId=True) missAliTi.setLocation(ti.getLocation()) if ti.hasTransform(): transformMat = ti.getTransform().getMatrix() else: transformMat = np.identity(3) newTransformMat = self.modifyTransformMatrix( transformMat, index, ts.getSize(), tsId) newTransform = data.Transform() newTransform.setMatrix(newTransformMat) missAliTi.setTransform(newTransform) missAliTs.append(missAliTi) missAliTs.write() outputMisalignedSetOfTiltSeries.update(missAliTs) outputMisalignedSetOfTiltSeries.write() self._store()
def assignStep(self): # Create a Volume object self.inVol = self.inVolume.get() self.outVol = emobj.Volume() self.outVol.copy(self.inVol) # Set sampling Rate (or copy from input) if self.setSampling.get(): samplingRate = self.samplingRate else: samplingRate = self.inVol.getSamplingRate() self.outVol.setSamplingRate(samplingRate) # set Origin if self.setOrigCoord.get(): origin = emobj.Transform() origin.setShifts(self.x, self.y, self.z) else: origin = self.inVol.getOrigin() self.outVol.setOrigin(origin) # copy or link copyOrLink = self.getCopyOrLink() fileName = self.inVol.getFileName() fileName = fileName.replace(':mrc', '') imgBase = basename(fileName) imgDst = self._getExtraPath(imgBase) copyOrLink(os.path.abspath(fileName), imgDst) self.outVol.setLocation(imgDst) # save self._defineOutputs(outputVolume=self.outVol) self._defineSourceRelation(self.inVol, self.outVol)
def assignTransformationMatricesStep(self): self.getOutputAssignedTransformSetOfTiltSeries() inputSetOfTiltSeries = self.inputSetOfTiltSeries.get() for ts in inputSetOfTiltSeries: tsId = ts.getTsId() tsFileName = ts.getFirstItem().parseFileName(extension='') for tmFilePath, _ in self.iterFiles(): tmFileName = os.path.basename(os.path.splitext(tmFilePath)[0]) if tsFileName == tmFileName: alignmentMatrix = utils.formatTransformationMatrix( tmFilePath) newTs = tomoObj.TiltSeries(tsId=tsId) newTs.copyInfo(ts) self.outputAssignedTransformSetOfTiltSeries.append(newTs) for index, tiltImage in enumerate(ts): newTi = tomoObj.TiltImage() newTi.copyInfo(tiltImage, copyId=True) newTi.setLocation(tiltImage.getLocation()) transform = data.Transform() if tiltImage.hasTransform(): previousTransform = tiltImage.getTransform( ).getMatrix() newTransform = alignmentMatrix[:, :, index] previousTransformArray = np.array( previousTransform) newTransformArray = np.array(newTransform) outputTransformMatrix = np.matmul( previousTransformArray, newTransformArray) transform.setMatrix(outputTransformMatrix) newTi.setTransform(transform) else: transform.setMatrix(alignmentMatrix[:, :, index]) newTi.setTransform(transform) newTs.append(newTi) ih = ImageHandler() x, y, z, _ = ih.getDimensions( newTs.getFirstItem().getFileName()) newTs.setDim((x, y, z)) newTs.write(properties=False) self.outputAssignedTransformSetOfTiltSeries.update(newTs) self.outputAssignedTransformSetOfTiltSeries.updateDim() self.outputAssignedTransformSetOfTiltSeries.write() self._store()
def test_scaleShifts(self): """ Check Scale 2D shifts in transformation class """ t = emobj.Transform() m = t.getMatrix() m[0, 3] = 2 m[1, 3] = 4 m[2, 3] = 6 m[3, 3] = 5 t.scaleShifts(0.5) self.assertAlmostEqual(m[0, 3], 1) self.assertAlmostEqual(m[1, 3], 2) self.assertAlmostEqual(m[2, 3], 3) self.assertAlmostEqual(m[3, 3], 5)
def testOrigin(self): fn = self.mic1 mic = emobj.Micrograph() mic.setFileName(fn) mic.setSamplingRate(1.) referenceT = emobj.Transform() referenceT.setShifts(-4608., -4720., 1) mreferenceT = referenceT.getMatrix() t = mic.getOrigin(True) mt = t.getMatrix() for i in range(4): for j in range(4): self.assertAlmostEquals(mt[i][j], mreferenceT[i][j], delta=0.5)
def assignStep(self): # Create a Volume object self.inVol = self.inVolume.get() self.outVol = emobj.Volume() self.outVol.copy(self.inVol) # Set sampling Rate (or copy from input) if self.setSampling.get(): samplingRate = self.samplingRate.get() else: samplingRate = self.inVol.getSamplingRate() self.outVol.setSamplingRate(samplingRate) # set Origin if self.setOrigCoord.get(): origin = emobj.Transform() origin.setShifts(self.x.get(), self.y.get(), self.z.get()) else: origin = self.inVol.getOrigin() self.outVol.setOrigin(origin) # Files system stuff fileName = os.path.abspath(self.inVol.getFileName()) fileName = fileName.replace(':mrc', '') # copy or link if self.copyFiles: imgBase = pwutils.replaceBaseExt(fileName, "mrc") imgDst = self._getExtraPath(imgBase) Ccp4Header.fixFile(fileName, imgDst, origin.getShifts(), sampling=samplingRate) else: imgBase = basename(fileName) imgDst = self._getExtraPath(imgBase) pwutils.createAbsLink(fileName, imgDst) self.outVol.setLocation(imgDst) # save self._defineOutputs(outputVolume=self.outVol) self._defineSourceRelation(self.inVol, self.outVol)
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 importVolumesStep(self, pattern, samplingRate, setOrigCoord=False): """ Copy images matching the filename pattern Register other parameters. """ volSet = self._createSetOfVolumes() vol = emobj.Volume() if self.importFrom == self.IMPORT_FROM_FILES: self.info("Using pattern: '%s'" % pattern) # Create a Volume template object vol.setSamplingRate(samplingRate) imgh = emlib.image.ImageHandler() volSet.setSamplingRate(samplingRate) for fileName, fileId in self.iterFiles(): x, y, z, n = imgh.getDimensions(fileName) if fileName.endswith('.mrc') or fileName.endswith('.map'): fileName += ':mrc' if z == 1 and n != 1: zDim = n n = 1 else: zDim = z else: zDim = z origin = emobj.Transform() if setOrigCoord: origin.setShiftsTuple(self._getOrigCoord()) else: origin.setShifts(x / -2. * samplingRate, y / -2. * samplingRate, zDim / -2. * samplingRate) vol.setOrigin(origin) # read origin from form if self.copyFiles or setOrigCoord: newFileName = abspath( self._getVolumeFileName(fileName, "mrc")) emconv.Ccp4Header.fixFile(fileName, newFileName, origin.getShifts(), samplingRate, emconv.Ccp4Header.ORIGIN) if self.setHalfMaps.get(): newFileName1 = abspath( self._getVolumeFileName(self.half1map.get(), "mrc")) emconv.Ccp4Header.fixFile(self.half1map.get(), newFileName1, origin.getShifts(), samplingRate, emconv.Ccp4Header.ORIGIN) newFileName2 = abspath( self._getVolumeFileName(self.half2map.get(), "mrc")) emconv.Ccp4Header.fixFile(self.half2map.get(), newFileName2, origin.getShifts(), samplingRate, emconv.Ccp4Header.ORIGIN) vol.setHalfMaps( [relpath(newFileName1), relpath(newFileName2)]) else: newFileName = abspath(self._getVolumeFileName(fileName)) if fileName.endswith(':mrc'): fileName = fileName[:-4] pwutils.createAbsLink(fileName, newFileName) if self.setHalfMaps.get(): pwutils.createAbsLink( self.half1map.get(), abspath( self._getVolumeFileName(self.half1map.get()))) pwutils.createAbsLink( self.half2map.get(), abspath( self._getVolumeFileName(self.half2map.get()))) vol.setHalfMaps([ relpath( self._getVolumeFileName(self.half1map.get())), relpath( self._getVolumeFileName(self.half2map.get())) ]) # Make newFileName relative # https://github.com/I2PC/scipion/issues/1935 newFileName = relpath(newFileName) if n == 1: vol.cleanObjId() vol.setFileName(newFileName) volSet.append(vol) else: for index in range(1, n + 1): vol.cleanObjId() vol.setLocation(index, newFileName) volSet.append(vol) else: # import from EMDB self.info("Downloading map with ID = %s" % self.emdbId) try: localFileName, sampling, origin = \ fetch_emdb_map(self.emdbId, self._getExtraPath(), self._getTmpPath()) except Exception as e: print(e) return # open volume and fill sampling and origin vol.setSamplingRate(sampling) vol.setFileName(localFileName) from pwem.objects.data import Transform originMat = Transform() originMat.setShifts(origin[0], origin[1], origin[2]) vol.setOrigin(originMat) if volSet.getSize() > 1: self._defineOutputs(outputVolumes=volSet) else: self._defineOutputs(outputVolume=vol)
def assignStep(self): # Create a Volume object self.inVol = self.inVolume.get() self.outVol = emobj.Volume() self.outVol.copy(self.inVol) # Set sampling Rate (or copy from input) if self.setSampling.get(): samplingRate = self.samplingRate else: samplingRate = self.inVol.getSamplingRate() self.outVol.setSamplingRate(samplingRate) # set Origin # get location of the subparticles (px) # this is a string with 3 coor inputProtocol = self.inputProtocol.get() if inputProtocol.defineVector == CMM: cmmFn = inputProtocol.vectorFile.get() vector = " " else: cmmFn = "" vector = inputProtocol.vector.get() length = inputProtocol.length.get() pxSize = inputProtocol.inputParticles.get().getSamplingRate() subpartVectorList = load_vectors(cmmFn, vector, length, pxSize) vector = subpartVectorList[0] # coordinates with origin as defined in localrec # convert from string to array length = vector.get_length() x = vector.vector[0] * length y = vector.vector[1] * length z = vector.vector[2] * length # 0.0 107.3775 281.1066 pixel # get subvolume vol = self.inVolume.get() shifts = vol.getOrigin(force=True).getShifts() sampling = vol.getSamplingRate() # 100 100 100 x += shifts[0] / sampling y += shifts[1] / sampling z += shifts[2] / sampling self.origin = emobj.Transform() samplingRate = samplingRate.get() self.origin.setShifts(x * samplingRate, y * samplingRate, z * samplingRate) self.outVol.setOrigin(self.origin) # copy or link copyOrLink = self.getCopyOrLink() fileName = self.inVol.getFileName() fileName = fileName.replace(':mrc', '') imgBase = basename(fileName) imgDst = self._getExtraPath(imgBase) copyOrLink(os.path.abspath(fileName), imgDst) self.outVol.setLocation(imgDst) # save self._defineOutputs(outputVolume=self.outVol) self._defineSourceRelation(self.inVol, self.outVol)
def computeXcorrStep(self, tsObjId): """Compute transformation matrix for each tilt series""" ts = self.inputSetOfTiltSeries.get()[tsObjId] tsId = ts.getTsId() extraPrefix = self._getExtraPath(tsId) tmpPrefix = self._getTmpPath(tsId) paramsXcorr = { 'input': os.path.join(tmpPrefix, ts.getFirstItem().parseFileName()), 'output': os.path.join(extraPrefix, ts.getFirstItem().parseFileName(extension=".prexf")), 'tiltfile': os.path.join(tmpPrefix, ts.getFirstItem().parseFileName(extension=".tlt")), 'rotationAngle': self.rotationAngle.get(), 'filterSigma1': self.filterSigma1.get(), 'filterSigma2': self.filterSigma2.get(), 'filterRadius1': self.filterRadius1.get(), 'filterRadius2': self.filterRadius2.get() } argsXcorr = "-input %(input)s " \ "-output %(output)s " \ "-tiltfile %(tiltfile)s " \ "-RotationAngle %(rotationAngle)f " \ "-FilterSigma1 %(filterSigma1)f " \ "-FilterSigma2 %(filterSigma2)f " \ "-FilterRadius1 %(filterRadius1)f " \ "-FilterRadius2 %(filterRadius2)f " Plugin.runImod(self, 'tiltxcorr', argsXcorr % paramsXcorr) paramsXftoxg = { 'input': os.path.join(extraPrefix, ts.getFirstItem().parseFileName(extension=".prexf")), 'goutput': os.path.join(extraPrefix, ts.getFirstItem().parseFileName(extension=".prexg")), } argsXftoxg = "-input %(input)s " \ "-goutput %(goutput)s" Plugin.runImod(self, 'xftoxg', argsXftoxg % paramsXftoxg) """Generate output tilt series""" outputSetOfTiltSeries = self.getOutputSetOfTiltSeries() alignmentMatrix = utils.formatTransformationMatrix( os.path.join(extraPrefix, ts.getFirstItem().parseFileName(extension=".prexg"))) newTs = tomoObj.TiltSeries(tsId=tsId) newTs.copyInfo(ts) outputSetOfTiltSeries.append(newTs) for index, tiltImage in enumerate(ts): newTi = tomoObj.TiltImage() newTi.copyInfo(tiltImage, copyId=True) newTi.setLocation(tiltImage.getLocation()) transform = data.Transform() transform.setMatrix(alignmentMatrix[:, :, index]) newTi.setTransform(transform) newTs.append(newTi) newTs.write(properties=False) outputSetOfTiltSeries.update(newTs) outputSetOfTiltSeries.write() self._store()