def save(self, baseFn=None): """save Mosaic size/pos/scale info in baseFn.txt save all images into baseFn_xx.mrc """ from Priithon.all import Mrc, U if baseFn is None: from .usefulX import FN baseFn = FN(1) if not baseFn: return a = N.concatenate((self.m_imgPosArr, self.m_imgSizeArr, N.array(self.m_imgRotArr,)[:,N.newaxis], self.m_imgScaleMM), 1) U.writeArray(a, baseFn + '.txt') n = len(self.m_imgSizeArr) for i in range( n ): # Mrc.save(self.m_imgArrL[i], "%s_%02d.mrc" % (baseFn, i)) #20070126 m = Mrc.Mrc("%s_%02d.mrc" % (baseFn, i), "w+", self.m_imgArrL[i] ) #20070126 m.calcMMM() #20070126 m.hdr('d')[:] = tuple(self.m_imgSizeArr[i] / N.array((self.m_imgArrL[i].shape))[::1]) + (1,) #20070126 m.hdr('zxy0')[:] = (0,) + tuple(self.m_imgPosArr[i]) #20070126 m.hdr('mmm1')[:] = tuple(self.m_imgScaleMM[i]) + (1,) #20070126 m.flush() #20070126 m.close() d = tuple(self.m_imgSizeArr[i] / N.array((self.m_imgArrL[i].shape), dtype=N.float32)[::1]) + (1,) zxy0 = (0,) + tuple(self.m_imgPosArr[i]) Mrc.save(self.m_imgArrL[i], "%s_%02d.mrc" % (baseFn, i), hdrEval='''hdr.d = d; hdr.zxy0 = zxy0''' )
def _cutoutForAlign2(fn, py, outFn=''): """ if not outFn, default with 'cut' extention resulting array is imgSequence=0 (t,w,z,y,x) return outFn """ h = imgManager.ImageManager(fn) slc, shiftZYX, ZYX = makeSlice(h, py) # input arr = N.empty((h.nt, h.nw, h.nz, h.ny, h.nx), h.dtype) for t in range(h.nt): for w in range(h.nw): arr[t, w] = h.get3DArr(w=w, t=t) canvas = N.squeeze(arr[slc].astype(arr.dtype.type)) newNum = (canvas.shape[-1], canvas.shape[-2], N.prod(canvas.shape[:-3])) if not outFn: outFn = '_'.join((h.filePath, EXT_CUTOUT)) #arr.Mrc.path, EXT_CUTOUT)) hdr = Mrc.makeHdrArray() Mrc.initHdrArrayFrom(hdr, h.hdr) #arr.Mrc.hdr) hdr.ImgSequence = 2 hdr.Num[:] = newNum mstart = [sl.start for sl in slc[::-1][:3] if isinstance(sl, slice)] hdr.mst[:len(mstart)] += mstart Mrc.save(canvas, outFn, ifExists='overwrite', hdr=hdr) return outFn
def OnSave(self, event=None): from Priithon.all import Mrc, U, Y fn = Y.FN(1)#, verbose=0) if not fn: return if fn[-4:] in [ ".mrc", ".dat" ]: Mrc.save(self.m_imgArr, fn) elif fn[-5:] in [ ".fits" ]: U.saveFits(self.m_imgArr, fn) else: U.saveImg8(self.m_imgArr, fn) Y.shellMessage("### section saved to '%s'\n"%fn)
def OnSave(self, event=None): from Priithon.all import Mrc, U, Y fn = Y.FN(1) #, verbose=0) if not fn: return if fn[-4:] in [".mrc", ".dat"]: Mrc.save(self.m_imgArr, fn) elif fn[-5:] in [".fits"]: U.saveFits(self.m_imgArr, fn) else: U.saveImg8(self.m_imgArr, fn) Y.shellMessage("### section saved to '%s'\n" % fn)
def __init__(self, fn): arr = Mrc.bindFile(fn) self.pxlsiz = arr.Mrc.hdr.d self.waves = arr.Mrc.hdr.wave[:arr.Mrc.hdr.NumWaves].copy() self.refwave = self.waves[arr.Mrc.hdr.n1] self.XYsize = arr.Mrc.hdr.Num[:2] self.nt = arr.Mrc.hdr.NumTimes self.nw = len(self.waves) self.t = 0 self.dtype = arr.dtype.type if 'SIR' in fn: self.mid = 512 else: self.mid = 256 if arr.Mrc.hdr.n2 == 1: parm = arr nentry = aligner.NUM_ENTRY self.map_str = 'None' else: parm = arr.Mrc.extFloats[:arr.Mrc.hdr.NumTimes * arr.Mrc.hdr.NumWaves, :arr.Mrc.hdr. NumFloats] nentry = arr.Mrc.hdr.NumFloats self.nz = arr.Mrc.hdr.Num[-1] / (arr.Mrc.hdr.NumTimes * arr.Mrc.hdr.NumWaves * 2) if self.nz == 1: self.map_str = 'Projection' else: self.map_str = 'Section-wise' parm = parm.reshape( (arr.Mrc.hdr.NumTimes, arr.Mrc.hdr.NumWaves, nentry)) self.alignParms = parm.copy() # writable del arr, parm
def OnMenuSaveND(self, ev=None): if self.data.dtype.type in (N.complex64, N.complex128): dat = self.dataCplx datX = abs(self.data) #CHECK else: dat = datX = self.data from Priithon.all import Mrc, U, FN, Y fn = FN(1,0) if not fn: return if fn[-4:] in [ ".mrc", ".dat" ]: Mrc.save(dat, fn) elif fn[-5:] in [ ".fits" ]: U.saveFits(dat, fn) else: # save as sequence of image files # if fn does contain something like '%0d' auto-insert '_%0NNd' # with NN to just fit the needed number of digits datX = datX.view() datX.shape = (-1,)+datX.shape[-2:] U.saveImg8_seq(datX, fn) Y.shellMessage("### Y.vd(%d) saved to '%s'\n"%(self.id, fn))
def OnMenuSaveND(self, ev=None): if self.data.dtype.type in (N.complex64, N.complex128): dat = self.dataCplx datX = abs(self.data) #CHECK else: dat = datX = self.data from Priithon.all import Mrc, U, FN, Y fn = FN(1, 0) if not fn: return if fn[-4:] in [".mrc", ".dat"]: Mrc.save(dat, fn) elif fn[-5:] in [".fits"]: U.saveFits(dat, fn) else: # save as sequence of image files # if fn does contain something like '%0d' auto-insert '_%0NNd' # with NN to just fit the needed number of digits datX = datX.view() datX.shape = (-1, ) + datX.shape[-2:] U.saveImg8_seq(datX, fn) Y.shellMessage("### Y.vd(%d) saved to '%s'\n" % (self.id, fn))
def makeFiles(fns, std=10, div_step=50, div_max=800): #1000): """ makes a series of images added Gaussain and Poisson noise std: standard deviation for Gaussian, and mean=std*10 for Poisson div_step: the step that the original image is divided while noise is added div_max: the maximum value that the original image is divided return output filenames """ outs = [] for fn in fns: a = Mrc.bindFile(fn) for ns in NOISE_STATS: hdr = mrcIO.makeHdr_like(a.Mrc.hdr) if ns == NOISE_STATS[0]: noise = F.noiseArr(a.shape, stddev=std, mean=0) else: noise = F.poissonArr(a.shape, mean=std * 10) steps = range(div_step, div_max + div_step, div_step) ag = [a / c + noise for c in steps] for i, arr in enumerate(ag): val = steps[i] if hasattr(arr, "Mrc"): del arr.Mrc if arr.dtype == N.float64: arr = arr.astype(N.float32) hdr.PixelType = 2 out = a.Mrc.path + ns + '%04d' % val Mrc.save(arr, out, ifExists='overwrite', hdr=hdr) outs.append(out) return outs
def setDimFromMrcHdr(self, hdr): from Priithon.all import Mrc self.wave = hdr.wave self.nw = hdr.NumWaves self.nt = hdr.NumTimes self.nx = hdr.Num[0] self.ny = hdr.Num[1] self.nz = hdr.Num[2] // (self.nt * self.nw) self.imgSequence = hdr.ImgSequence self.dtype = Mrc.MrcMode2dtype(hdr.PixelType) #if self.multipage: # self._setSecSize() #else: self._setSuffix()
def findOTF(fn, ctfdir=None): doesnotwork = """ tempfn = tempfile.mktemp(suffix='.txt', prefix='lensNum') # header does not work on command line! It stops before extended header com = os.system("header %s |sed -n -e '/Lens ID Number\.\.*/ s/Lens ID Number\.\.*//p' |awk '{print $1}' >> %s" % (fn, tempfn)) if com: raise RuntimeError, 'problem in reading header %s' % fn h = open(tempfn) lensNum = h.readline() h.close() print 'lensNum: ', lensNum""" a = Mrc.Mrc2(fn) lensNum = str(a.hdr.LensNum) a.close() return findOTFfromNum(lensNum, ctfdir)
def load(self, baseFn=None, n=None, n0=0, sparse=1): """load Mosaic size/pos/scale from in baseFn.txt load all images into baseFn_xx.mrc load only the first n images - n=None means all skip the first n0 of these if sparse>1 take only every 'sparse' pixel along x & y if baseFn end on '.txt' - that suffix gets ignored """ from Priithon.all import Mrc, U if baseFn is None: from usefulX import FN baseFn = FN() if not baseFn: return if baseFn[-4:] == '.txt': baseFn = baseFn[:-4] a = U.readArray(baseFn + '.txt') if len(a.shape) == 1: a.shape = (1, ) + a.shape nn = len(a) if n is not None and n < nn: nn = n n = nn apos = a[:, :2] asiz = a[:, 2:4] arot = a[:, 4] ascl = a[:, 5:7] #n = len( apos ) for i in range(n0, n): #20070126 aa = Mrc.bindFile("%s_%02d.mrc" % (baseFn, i)).copy()[0] aa = Mrc.load("%s_%02d.mrc" % (baseFn, i))[0] # self.appendMosaic( aa, self.appendNewImg(aa[::sparse, ::sparse], apos[i], asiz[i], ascl[i], holdBackUpdate=(i + 1 < n), rot=arot[i])
def load(self, baseFn=None, n=None, n0=0, sparse=1): """load Mosaic size/pos/scale from in baseFn.txt load all images into baseFn_xx.mrc load only the first n images - n=None means all skip the first n0 of these if sparse>1 take only every 'sparse' pixel along x & y if baseFn end on '.txt' - that suffix gets ignored """ from Priithon.all import Mrc, U if baseFn is None: from .usefulX import FN baseFn = FN() if not baseFn: return if baseFn[-4:] == '.txt': baseFn = baseFn[:-4] a = U.readArray(baseFn + '.txt') if len(a.shape) == 1: a.shape = (1,) + a.shape nn = len(a) if n is not None and n<nn: nn=n n=nn apos = a[:, :2] asiz = a[:, 2:4] arot = a[:, 4] ascl = a[:, 5:7] #n = len( apos ) for i in range(n0, n ): #20070126 aa = Mrc.bindFile("%s_%02d.mrc" % (baseFn, i)).copy()[0] aa = Mrc.load("%s_%02d.mrc" % (baseFn, i))[0] # self.appendMosaic( aa, self.appendNewImg( aa[::sparse,::sparse], apos[i], asiz[i], ascl[i], holdBackUpdate=(i+1<n), rot=arot[i] )