def test_read(self): """ check we can read these images""" ref = edfimage() gzipped = edfimage() compressed = edfimage() refFile = "edfUncompressed_U16.edf" gzippedFile = "edfGzip_U16.edf" compressedFile = "edfCompressed_U16.edf" try: ref.read(os.path.join(self.im_dir, refFile)) except: raise RuntimeError("Cannot read image Uncompressed image %s" % refFile) try: gzipped.read(os.path.join(self.im_dir, gzippedFile)) except: raise RuntimeError("Cannot read image gzippedFile image %s" % gzippedFile) try: compressed.read(os.path.join(self.im_dir, compressedFile)) except: raise RuntimeError("Cannot read image compressedFile image %s" % compressedFile) self.assertEqual((ref.data - gzipped.data).max(), 0, "Gzipped data block is correct") self.assertEqual((ref.data - compressed.data).max(), 0, "Zlib compressed data block is correct")
def setUp(self): self.multiFrameFilename = UtilsTest.getimage("MultiFrame.edf.bz2")[:-4] self.Frame0Filename = UtilsTest.getimage( "MultiFrame-Frame0.edf.bz2")[:-4] self.Frame1Filename = UtilsTest.getimage( "MultiFrame-Frame1.edf.bz2")[:-4] self.ref = edfimage() self.frame0 = edfimage() self.frame1 = edfimage() try: self.ref.read(self.multiFrameFilename) except: raise RuntimeError( "Cannot read image multiFrameFilename image %s" % self.multiFrameFilename) try: self.frame0.read(self.Frame0Filename) except: raise RuntimeError("Cannot read image Frame0File image %s" % self.Frame0File) try: self.frame1.read(self.Frame1Filename) except: raise RuntimeError("Cannot read image Frame1File image %s" % self.Frame1File)
def save_as_edf(calibration: Calibration, basedir: str) -> None: """Save the multi calib images into edf files in order to do the first calibration """ with File(calibration.filename, mode='r') as h5file: for frame in gen_metadata_idx(h5file, calibration): base = os.path.basename(calibration.filename) output = os.path.join(basedir, base + "_{:02d}.edf".format(frame.idx)) # noqa print(output) edfimage(frame.image).write(output)
def write(self, stemname): """ save the dx, dy images """ im = edfimage.edfimage() im.data = self.dx im.write("%s_dx.edf" % (stemname), force_type=numpy.float32) im = edfimage.edfimage() im.data = self.dy im.write("%s_dy.edf" % (stemname), force_type=numpy.float32) numpy.save("%s_tth.npy" % (stemname), self.tthvals)
def findgisaxsarcs2(img, experiment): img = img.T.copy() cake, _, _ = integration.cake( img, experiment, mask=experiment.mask) # TODO: refactor these parameters and check .T maskcake, _, _ = integration.cake(experiment.mask.T, experiment) from fabio import edfimage fabimg = edfimage.edfimage(cake) filename = 'cake.edf' fabimg.write(filename) fabimg = edfimage.edfimage(maskcake) filename = 'cake_MASK.edf' fabimg.write(filename) img = inpaint(cake, maskcake) fabimg = edfimage.edfimage(img) filename = 'cake_LINEAR_INFILL.edf' fabimg.write(filename) maxchis, maxqs = findmaxs(img) out = [] for chi, q in zip(maxchis, maxqs): # roi=np.ones_like(img) #roi[chi - 10:chi + 10, q - 5:q + 5]=10 #roi=np.sum(roi,axis=1) slice = img[:, q - 5:q + 5] if np.max(slice) / np.min(slice) < 2: pass # continue chiprofile = np.sum(slice, axis=1) x = np.arange(np.size(chiprofile)) #plt.plot(chiprofile) params = fitarcgaussian(chiprofile, chi) if params['mu'] > chi + 5 or params['mu'] < chi - 5: continue params.add('q', value=q) out.append(params) #plt.show() # plt.imshow(np.log(img)) #plt.show() return out
def writeimage(image, path, mask=None, headers=None, suffix='',ext=None, dialog=False): if dialog: filename,ok = dialogs.savedatadialog(guesspath=path,caption="Save data to "+ext) if filename and ok: writeimage(image, filename, headers) if mask is not None: maskname = ''.join(os.path.splitext(filename)[:-1]) + "_mask" + os.path.splitext(filename)[-1] writeimage(mask, maskname, headers) if headers is None: headers = dict() if ext is None: ext = os.path.splitext(path)[-1] path = ''.join(os.path.splitext(path)[:-1]) + suffix + ext if notexitsoroverwrite(path): if ext.lower() == '.edf': fabimg = edfimage.edfimage(np.rot90(image), header=headers) fabimg.write(path) elif ext.lower() == '.tif': fabimg = tifimage.tifimage(np.rot90((image.astype(float)/image.max()*2**16).astype(np.int16)), header=headers) fabimg.write(path) elif ext.lower() == '.png': raise NotImplementedError elif ext.lower() == '.jpg': scipy.misc.imsave(path,np.rot90(image)) else: return False return True
def writeEDF(self, basename): """save the distortion matrices into a couple of files called basename-x.edf and basename-y.edf """ try: from fabio.edfimage import edfimage #from EdfFile import EdfFile as EDF except ImportError: print "You will need the Fabio library available from the Fable sourceforge" return self.spline2array() edfDispX = edfimage(data=self.xDispArray.astype("float32"), header={}) edfDispY = edfimage(data=self.yDispArray.astype("float32"), header={}) edfDispX.write(basename + "-x.edf", force_type="float32") edfDispY.write(basename + "-y.edf", force_type="float32")
def postProcess(self, _edObject=None): EDPluginExec.postProcess(self) self.DEBUG("EDPluginExecShiftImagev1_0.postProcess") # Create some output data xsDataResult = XSDataResultShiftImage() if self.strOutputType is None: xsDataResult.setOutputArray(EDUtilsArray.arrayToXSData(self.npaImage)) elif self.strOutputType == "file": image = edfimage( data=self.npaImage, header={"Offset_1": self.tOffset[0], "Offset_2": self.tOffset[1], "Max_Offset": self.MAX_OFFSET_VALUE}, ) image.write(self.strOutputImage, force_type=self.npaImage.dtype) xsdimg = XSDataImageExt(path=XSDataString(self.strOutputImage)) xsDataResult.outputImage = xsdimg elif self.strOutputType == "shared": EDShare[self.strOutputImage] = self.npaImage xsdimg = XSDataImageExt(shared=XSDataString(self.strOutputImage)) xsDataResult.outputImage = xsdimg elif self.strOutputType == "array": xsdimg = XSDataImageExt(array=EDUtilsArray.arrayToXSData(self.npaImage)) xsDataResult.outputImage = xsdimg self.setDataOutput(xsDataResult) self.npaImage = None
def test_getstats(self): """ test statistics""" obj = edfimage() obj.read(self.filename) self.assertEqual(obj.getmean(), 10) self.assertEqual(obj.getmin(), 0) self.assertEqual(obj.getmax(), 20)
def postProcess(self, _edObject=None): EDPluginExec.postProcess(self) self.DEBUG("EDPluginExecShiftImagev1_0.postProcess") # Create some output data xsDataResult = XSDataResultShiftImage() if self.strOutputType is None: xsDataResult.setOutputArray( EDUtilsArray.arrayToXSData(self.npaImage)) elif self.strOutputType == "file": image = edfimage(data=self.npaImage, header={ "Offset_1": self.tOffset[0], "Offset_2": self.tOffset[1], "Max_Offset": self.MAX_OFFSET_VALUE }) image.write(self.strOutputImage, force_type=self.npaImage.dtype) xsdimg = XSDataImageExt(path=XSDataString(self.strOutputImage)) xsDataResult.outputImage = xsdimg elif self.strOutputType == "shared": EDShare[self.strOutputImage] = self.npaImage xsdimg = XSDataImageExt(shared=XSDataString(self.strOutputImage)) xsDataResult.outputImage = xsdimg elif self.strOutputType == "array": xsdimg = XSDataImageExt( array=EDUtilsArray.arrayToXSData(self.npaImage)) xsDataResult.outputImage = xsdimg self.setDataOutput(xsDataResult) self.npaImage = None
def test_read(self): """ check we can read these images""" for line in TESTIMAGES.split("\n"): vals = line.split() name = vals[0] dim1, dim2 = [int(x) for x in vals[1:3]] mini, maxi, mean, stddev = [float(x) for x in vals[3:]] obj = edfimage() try: obj.read(os.path.join(self.im_dir, name)) except: print "Cannot read image", name raise self.assertAlmostEqual(mini, obj.getmin(), 2, "testedfs: %s getmin()" % name) self.assertAlmostEqual(maxi, obj.getmax(), 2, "testedfs: %s getmax" % name) logger.info("%s Mean: exp=%s, obt=%s" % (name, mean, obj.getmean())) self.assertAlmostEqual(mean, obj.getmean(), 2, "testedfs: %s getmean" % name) logger.info("%s StdDev: exp=%s, obt=%s" % (name, stddev, obj.getstddev())) self.assertAlmostEqual(stddev, obj.getstddev(), 2, "testedfs: %s getstddev" % name) self.assertEqual(dim1, obj.dim1, "testedfs: %s dim1" % name) self.assertEqual(dim2, obj.dim2, "testedfs: %s dim2" % name)
def write_edf(self, framenumber, frame, usegzip=False): e = edfimage.edfimage() e.data = frame edim2, edim1 = frame.shape e.header = {} e.header['origin'] = 'PolyXSim' e.header['Dim_1'] = edim1 e.header['Dim_2'] = edim2 e.header['col_end'] = edim1 - 1 e.header['row_end'] = edim2 - 1 e.header['DataType'] = 'UnsignedShort' e.header['Image'] = 1 e.header['ByteOrder'] = 'Low' e.header['time'] = time.asctime() e.header['Omega']= self.graindata.frameinfo[framenumber].omega +\ self.graindata.param['omega_step']/2.0 e.header['OmegaStep'] = self.graindata.param['omega_step'] e.header['grainfile']='%s/%s_%0.4dgrains.txt' \ %(self.graindata.param['direc'],self.graindata.param['stem'],self.graindata.param['no_grains']) fname = '%s%s' % (self.graindata.frameinfo[framenumber].name, '.edf') if usegzip: fobj = gzip.GzipFile(fname + ".gz", "wb") e.write(fobj) fobj.close() else: e.write(fname)
def checkFile(self, filename): """ check we can read EDF image with openimage""" obj = openimage(filename) obj2 = edfimage() obj2.read(filename) self.assertEqual(obj.data[10, 10], obj2.data[10, 10]) self.assertEqual(type(obj), type(obj2)) self.assertEqual(abs(obj.data.astype(int) - obj2.data.astype(int)).sum(), 0)
def test_rebin(self): """test the rebin of edfdata""" f = edfimage() f.read(os.path.join(self.im_dir, "F2K_Seb_Lyso0675.edf")) f.rebin(1024, 1024) self.assertEqual( abs(numpy.array([[1547, 1439], [1536, 1494]]) - f.data).max(), 0, "data are the same after rebin")
def testBzip2(self): self.filename = os.path.join(self.tmpdir, "merged.azim.gz") e = edfimage(data=self.data, header=self.header) e.write(self.filename) r = fabio.open(self.filename) self.assert_(r.header["toto"] == self.header["toto"], "header are OK") self.assert_(abs(r.data - self.data).max() == 0, "data are OK") self.assertEqual(int(r.header["EDF_HeaderSize"]), 512, "header size is one 512 block")
def exportimage(self): fabimg = edfimage.edfimage(np.rot90(self.imageitem.image)) dialog = QtGui.QFileDialog(parent=self.parentwindow.ui, caption='blah', directory=os.path.dirname(self.path), filter=u'EDF (*.edf)') dialog.selectFile(os.path.basename(self.path)) filename, _ = dialog.getSaveFileName() fabimg.write(filename)
def setUp(self): self.multiFrameFilename = UtilsTest.getimage("MultiFrame.edf.bz2")[:-4] self.Frame0Filename = UtilsTest.getimage("MultiFrame-Frame0.edf.bz2")[:-4] self.Frame1Filename = UtilsTest.getimage("MultiFrame-Frame1.edf.bz2")[:-4] self.ref = edfimage() self.frame0 = edfimage() self.frame1 = edfimage() try: self.ref.read(self.multiFrameFilename) except: raise RuntimeError("Cannot read image multiFrameFilename image %s" % self.multiFrameFilename) try: self.frame0.read(self.Frame0Filename) except: raise RuntimeError("Cannot read image Frame0File image %s" % self.Frame0File) try: self.frame1.read(self.Frame1Filename) except: raise RuntimeError("Cannot read image Frame1File image %s" % self.Frame1File)
def writeEDF(self, basename): """ save the distortion matrices into a couple of files called basename-x.edf and basename-y.edf :param basename: base of the name used to save the data :type basename: str """ try: from fabio.edfimage import edfimage except ImportError: logger.error("You will need the Fabio library available" " from the Fable sourceforge") return self.spline2array() edfDispX = edfimage(data=self.xDispArray.astype("float32"), header={}) edfDispY = edfimage(data=self.yDispArray.astype("float32"), header={}) edfDispX.write(basename + "-x.edf", force_type="float32") edfDispY.write(basename + "-y.edf", force_type="float32")
def writespot(filename, roi, box, hdr, flt, ispt): """ Filename - target filename roi - the roi in the original image box - the pixels hdr - original file header flt - peaksearch output in columnfile object spot3d_id - which peak this is """ # Make the header print("writing", filename) myheader = {"ByteOrder": hdr["ByteOrder"]} ks = [] for k in edfimage.MINIMUM_KEYS: ks.append(k) if k not in mappings: try: myheader[k] = edfimage.DEFAULT_VALUES[k] except: pass for k in flt.titles: myheader[k] = getattr(flt, k)[ispt] ks.append(k) ks.append("OriginalFile") myheader["OriginalFile"] = hdr['filename'] myheader["DataType"] = "FloatValue" for k, v in [("ROI_slow_start", roi[0].start), ("ROI_slow_stop", roi[0].stop), ("ROI_fast_start", roi[1].start), ("ROI_fast_stop", roi[1].stop)]: ks.append(k) myheader[k] = int(v) km = list(mappings.keys()) km.sort() for k in km: if k not in ks: ks.append(k) if k in km: v = mappings[k] if type(v) == type("String"): myheader[k] = v continue if type(v) == type(["List"]): # Funky try: args = [getattr(flt, a)[ispt] for a in list(v[0])] myheader[k] = v[1](*args) except: print(v[0]) raise #b = box.astype(np.float32) #print np.maximum.reduce(b), np.minimum.reduce(b) o = edfimage.edfimage(box.astype(np.float32), myheader) o.header_keys = ks o.header_keys.append("filename") o.write(filename, force_type=None)
def testsame(self): """test ADSC image match to EDF""" im1 = edfimage() im1.read(self.fn_edf) im2 = adscimage() im2.read(self.fn_adsc) diff = (im1.data.astype("float32") - im2.data.astype("float32")) logger.debug("type: %s %s shape %s %s " % (im1.data.dtype, im2.data.dtype, im1.data.shape, im2.data.shape)) logger.debug("im1 min %s %s max %s %s " % (im1.data.min(), im2.data.min(), im1.data.max(), im2.data.max())) logger.debug("delta min %s max %s mean %s" % (diff.min(), diff.max(), diff.mean())) self.assertEqual(abs(diff).max(), 0.0, "asdc data == edf data")
def test_read(self): """ check readable""" obj = edfimage() obj.read(self.filename) self.assertEqual(obj.dim1, 256, msg="dim1!=256 for file: %s" % self.filename) self.assertEqual(obj.dim2, 256, msg="dim2!=256 for file: %s" % self.filename) self.assertEqual(obj.bpp, 4, msg="bpp!=4 for file: %s" % self.filename) self.assertEqual(obj.bytecode, numpy.float32, msg="bytecode!=flot32 for file: %s" % self.filename) self.assertEqual(obj.data.shape, (256, 256), msg="shape!=(256,256) for file: %s" % self.filename) self.assertEqual(obj.header['History-1'], "something=something else")
def write(self, fname): e = edfimage.edfimage() e.data = self.total_image e.dim2, e.dim1 = self.total_image.shape e.header = self.header e.header['Dim_1'] = e.dim1 e.header['Dim_2'] = e.dim2 e.header['col_end'] = e.dim1 - 1 e.header['row_end'] = e.dim2 - 1 e.header['DataType'] = 'UnsignedShort' e.header['Image'] = 1 e.write(fname)
def postProcess(self, _edObject=None): EDPluginExec.postProcess(self) self.DEBUG("EDPluginExecStitchOffsetedImagev1_0.postProcess") # Create some output data xsDataResult = XSDataResultStitchOffsetedImage() if self._strOutFile is not None: edf = edfimage(data=self._ndaResult, header={"Dummy":str(self._fDummy), "Blending":self._strBlending, "Autoscale":str(self._bAutoscale)}) edf.write(self._strOutFile) xsDataResult.setOutputImage(XSDataImageExt(path=XSDataString(self._strOutFile))) else: xsDataResult.setOutputArray(EDUtilsArray.arrayToXSData(self._ndaResult)) self.setDataOutput(xsDataResult)
def save_as_edf(calibration: Calibration) -> None: """Save the multi calib images into edf files in order to do the first calibration and print the command line in order to do the calibration with pyFAI-calib """ cmds = [] with File(calibration.filename, mode='r') as h5file: for frame in gen_metadata_idx(h5file, calibration, calibration.idxs): base = os.path.basename(calibration.filename) output = base + "_{:02d}.edf".format(frame.idx) edfimage(frame.image).write( os.path.join(calibration.basedir, output)) # noqa # temporary until pyFAI-calib2 works wavelength = calibration.wavelength * 1e10 cmd = "cd {directory} && pyFAI-calib2 -w {wavelength} --calibrant {calibrant} -D {detector} {filename}".format( directory=calibration.basedir, # noqa wavelength=wavelength, # noqa calibrant=calibration.calibrant, # noqa detector=calibration.detector, # noqa filename=output) # noqa cmds.append(cmd) return cmds
def exportimage(self): fabimg = edfimage.edfimage( np.rot90(self.getCurrentTab().imageitem.image)) dialog = QtGui.QFileDialog( parent=None, caption=u"Export image as EDF", directory=unicode(os.path.dirname(self.getCurrentTab().paths[0])), filter=u"EDF (*.edf)") dialog.selectFile( unicode(os.path.dirname(self.getCurrentTab().paths[0]))) filename, ok = dialog.getSaveFileName() if ok and filename: fabimg.write(filename)
def postProcess(self, _edObject=None): EDPluginExec.postProcess(self) EDVerbose.DEBUG("EDPluginExecShiftImagev1_0.postProcess") # Create some output data xsDataResult = XSDataResultShiftImage() if self.strOutputImage is None: #ArrayOutput xsDataResult.setOutputArray(EDUtilsArray.arrayToXSData(self.npaImage)) else: image = edfimage(data=self.npaImage, header={"Offset_1":self.tOffset[0], "Offset_2":self.tOffset[1]}) image.write(self.strOutputImage, force_type=self.npaImage.dtype) xsdimg = XSDataImageExt(path=XSDataString(self.strOutputImage)) xsDataResult.setOutputImage(xsdimg) self.setDataOutput(xsDataResult)
def write(self, fname, header=None): if self.debug: print('write median image to', fname) e = edfimage.edfimage() e.data = N.clip(self.median, 0, 2**16 - 1) e.data = e.data.astype(N.uint16) #e.header=edfimage.DEFAULT_VALUES #e.header['DIM_1']=e.dim1 #e.header['DIM_2']=e.dim2 #e.header['col_end']=e.dim1-1 #e.header['row_end']=e.dim2-1 #e.header['DataType']='UnsignedShort' if header != None: for arg in header: e.header[arg] = header[arg] e.write(fname)
def postProcess(self, _edObject=None): EDPluginExec.postProcess(self) EDVerbose.DEBUG("EDPluginExecShiftImagev1_0.postProcess") # Create some output data xsDataResult = XSDataResultShiftImage() if self.strOutputImage is None: #ArrayOutput xsDataResult.setOutputArray( EDUtilsArray.arrayToXSData(self.npaImage)) else: image = edfimage(data=self.npaImage, header={ "Offset_1": self.tOffset[0], "Offset_2": self.tOffset[1] }) image.write(self.strOutputImage, force_type=self.npaImage.dtype) xsdimg = XSDataImageExt(path=XSDataString(self.strOutputImage)) xsDataResult.setOutputImage(xsdimg) self.setDataOutput(xsDataResult)
def postProcess(self, _edObject=None): EDPluginExec.postProcess(self) self.DEBUG("EDPluginExecStitchOffsetedImagev1_0.postProcess") # Create some output data xsDataResult = XSDataResultStitchOffsetedImage() if self._strOutFile is not None: edf = edfimage(data=self._ndaResult, header={ "Dummy": str(self._fDummy), "Blending": self._strBlending, "Autoscale": str(self._bAutoscale) }) edf.write(self._strOutFile) xsDataResult.setOutputImage( XSDataImageExt(path=XSDataString(self._strOutFile))) else: xsDataResult.setOutputArray( EDUtilsArray.arrayToXSData(self._ndaResult)) self.setDataOutput(xsDataResult)
def bug_27(self): """ import fabio obj = fabio.open("any.edf") obj.header["missing"]="blah" obj.write("any.edf") """ # create dummy image: shape = (32, 32) data = numpy.random.randint(0, 6500, size=shape[0] * shape[1]).astype("uint16").reshape(shape) fname = os.path.join(UtilsTest.tempdir, "bug27.edf") e = edfimage(data=data, header={"key1": "value1"}) e.write(fname) del e obj = fabio.open(fname) obj.header["missing"] = "blah" obj.write(fname) del obj
def write_edf(self, framenumber, frame): e = edfimage.edfimage() e.data = frame e.dim2, e.dim1 = frame.shape e.header = {} e.header['origin'] = 'ModelScanning3DXRD' e.header['Dim_1'] = e.dim1 e.header['Dim_2'] = e.dim2 e.header['col_end'] = e.dim1 - 1 e.header['row_end'] = e.dim2 - 1 e.header['DataType'] = 'UnsignedShort' e.header['Image'] = 1 e.header['ByteOrder'] = 'Low' e.header['time'] = time.asctime() e.header['Omega']= self.voxeldata.frameinfo[framenumber].omega +\ self.voxeldata.param['omega_step']/2.0 e.header['OmegaStep'] = self.voxeldata.param['omega_step'] e.header['voxelfile']='%s/%s_%0.4dvoxels.txt' \ %(self.voxeldata.param['direc'],self.voxeldata.param['stem'],self.voxeldata.param['no_voxels']) e.write('%s%s' % (self.voxeldata.frameinfo[framenumber].name, '.edf'))
def write_edf(slow,medium,omega,ang_val_s,ang_val_m,omega_val,frame,stem,format): e=edfimage.edfimage() e.data=frame e.dim2,e.dim1=frame.shape e.header = {} e.header['origin']='dfxrm' e.header['Dim_1']=e.dim1 e.header['Dim_2']=e.dim2 e.header['col_end']=e.dim1-1 e.header['row_end']=e.dim2-1 e.header['DataType']='UnsignedShort' e.header['Image']=1 e.header['ByteOrder']='Low' e.header['time']=time.asctime() e.header['Omega']= omega_val[omega] e.header['OmegaStep']=omega_val[1]-omega_val[0] e.header['phi_lower']= ang_val_m[medium] e.header['phi_lower_step']=ang_val_m[1]-ang_val_m[0] e.header['phi_upper']= ang_val_s[slow] e.header['phi_upper_step']=ang_val_s[1]-ang_val_s[0] e.write('%s_%0.4d_%0.4d_%0.4d.%s' %(stem,slow,medium,omega,format))
def writeimage(image, path, headers=None, suffix='', ext=None): if headers is None: headers = dict() if ext is None: ext = os.path.splitext(path)[-1] path = ''.join(os.path.splitext(path)[:-1]) + suffix + ext if notexitsoroverwrite(path): if ext.lower() == '.edf': fabimg = edfimage.edfimage(np.rot90(image), header=headers) fabimg.write(path) elif ext.lower() == '.tif': fabimg = tifimage.tifimage(np.rot90( (image.astype(float) / image.max() * 2**16).astype(np.int16)), header=headers) fabimg.write(path) elif ext.lower() == '.png': raise NotImplementedError elif ext.lower() == '.jpg': scipy.misc.imsave(path, np.rot90(image)) else: return False return True
def test_rebin(self): """test the rebin of edfdata""" f = edfimage() f.read(os.path.join(self.im_dir, "F2K_Seb_Lyso0675.edf")) f.rebin(1024, 1024) self.assertEqual(abs(numpy.array([[1547, 1439], [1536, 1494]]) - f.data).max(), 0, "data are the same after rebin")
def process(self, _edObject=None): EDPluginExec.process(self) EDVerbose.DEBUG("EDPluginExecMatrixWritev1_0.process") f = edfimage(self.matIn, {}) f.write(self.outputFile, force_type=self.matIn.dtype)
def main(): """ A CLI user interface """ import sys, time, os, logging start = time.time() root = logging.getLogger('') root.setLevel(logging.WARNING) try: from optparse import OptionParser parser = OptionParser() parser = get_options(parser) options, args = parser.parse_args() except SystemExit: raise except: parser.print_help() print("\nProblem with your options:") raise if options.mask is not None: fit2dmask = (1 - openimage(options.mask).data).ravel() else: fit2dmask = 1.0 first_image = True imagefiles = ImageD11_file_series.get_series_from_options(options, args) tthvals = numpy.load(options.lookup + "_tth.npy") try: for fim in imagefiles: dataim = fim.data print(fim.filename) if first_image: # allocate volume, compute k etc first_image = False dxim = openimage(options.lookup + "_dx.edf").data dyim = openimage(options.lookup + "_dy.edf").data outsum = numpy.ravel(numpy.zeros(dataim.shape, numpy.float32)) outnp = numpy.ravel(numpy.zeros(dataim.shape, numpy.float32)) e = edfimage() # C code from rsv_mapper (not intended to be obfuscated) o = blobcorrector.perfect() idealx, idealy = o.make_pixel_lut(dataim.shape) destx, desty = idealx + dxim, idealy + dyim assert destx.min() >= 0 assert destx.max() < dataim.shape[1] assert desty.min() >= 0 assert desty.max() < dataim.shape[1] imageshape = dataim.shape indices = numpy.ravel(destx).astype(numpy.intp) numpy.multiply(indices, dataim.shape[1], indices) numpy.add(indices, numpy.ravel(desty).astype(numpy.intp), indices) assert indices.min() >= 0 assert indices.max() < dataim.shape[0] * dataim.shape[1] on = numpy.ones(len(outnp), numpy.float32) if fit2dmask is not None: on = on * fit2dmask # Number of pixels and mask are constant cImageD11.put_incr(outnp, indices, on) mask = outnp < 0.1 scalar = (1.0 - mask) / (outnp + mask) flatshape = outsum.shape # arsorted = mask.copy() outmask = mask.copy() outmask = outmask * 1e6 outmask.shape = imageshape arsorted.shape = imageshape arsorted.sort(axis=1) minds = numpy.array([l.searchsorted(0.5) for l in arsorted]) # ENDIF firstimage start = time.time() numpy.multiply(outsum, 0, outsum) outsum.shape = flatshape dm = (dataim.ravel() * fit2dmask).astype(numpy.float32) cImageD11.put_incr(outsum, indices, dm) # outsum = outsum.reshape( dataim.shape ) # outnp = outnp.reshape( dataim.shape ).astype(numpy.int32) # Normalise numpy.multiply(outsum, scalar, outsum) print(dataim.max(), dataim.min(), end=' ') print(scalar.max(), scalar.min(), outsum.min(), outsum.max()) outsum.shape = imageshape # saving edf e.data = outsum e.write("r_" + fim.filename, force_type=numpy.float32) print(time.time() - start) except: raise
def process(self, _edObject=None): EDPluginExec.process(self) self.DEBUG("EDPluginExecStitchOffsetedImagev1_0.process") shiftedImage = [] distanceSq = lambda t2f: (t2f[0] * t2f[0] + t2f[1] * t2f[1]) ref = self._lOffsets[0] refOffset = distanceSq(ref) shape = self._lImages[0].shape if self._strMask: mask = (1 - fabio.open(self._strMask).data).astype("bool") else: mask = 0 for img, offset, dummy in zip(self._lImages, self._lOffsets, self._lDummies): shImg = scipy.ndimage.shift(img, offset, cval=dummy[0], order=0) if dummy[1] == 0: if self._strMask: shImgMasked = numpy.ma.MaskedArray(shImg, (shImg == dummy[0]) + scipy.ndimage.shift(mask, offset, cval=1, order=0)) else: shImgMasked = numpy.ma.MaskedArray(shImg, (shImg == dummy[0])) else: if self._strMask: shImgMasked = numpy.ma.MaskedArray(shImg, (abs(shImg - dummy[0]) <= dummy[1]) + scipy.ndimage.shift(mask, offset, cval=1, order=0)) else: shImgMasked = numpy.ma.MaskedArray(shImg, (abs(shImg - dummy[0]) <= dummy[1])) shiftedImage.append(shImgMasked) if (offset != ref) and (distanceSq(offset) < refOffset): ref = offset refOffset = distanceSq(offset) refIdx = self._lOffsets.index(ref) if EDVerbose.isVerboseDebug(): e = edfimage(data=shiftedImage[0].filled(self._fDummy), header={"Dummy":str(self._fDummy), "Offset_1":str(self._lOffsets[0][0]), "Offset_2":str(self._lOffsets[0][1])}) for img, offset in zip(shiftedImage[1:], self._lOffsets[1:]): e.appendFrame(data=img.filled(self._fDummy), header={"Dummy":str(self._fDummy), "Offset_1":str(offset[0]), "Offset_2":str(offset[1])}) e.write("stack.edf") ROI = False if self.tCenter is None: #the default center is the geometry center of the image ... self.tCenter = [ i // 2 for i in shape ] if self.tWidth is not None: d0min = max(0, self.tCenter[0] - (self.tWidth[0] // 2)) d0max = min(shape[0], d0min + self.tWidth[0]) d1min = max(0, self.tCenter[1] - (self.tWidth[1] // 2)) d1max = min(shape[1], d1min + self.tWidth[1]) shape = (d0max - d0min, d1max - d1min) else: d0min = 0 d0max = shape[0] d1min = 0 d1max = shape[1] refImg = shiftedImage[refIdx] stackMaskedImg = numpy.ma.zeros((len(self._lOffsets), refImg.shape[0], refImg.shape[1])) self.screen("ROI[%s:%s, %s:%s]\t Autoscale: %s\tBlending method: %s" % (d0min, d0max, d1min, d1max, self._bAutoscale, self._strBlending)) if self._bAutoscale: for idx, img in enumerate(shiftedImage): ratio = (refImg[d0min:d0max, d1min:d1max] / img[d0min:d0max, d1min:d1max]) stackMaskedImg[idx] = ratio.mean() * img else: for idx, img in enumerate(shiftedImage): stackMaskedImg[idx] = img if self._strBlending.lower().startswith("naive"): npaTempBool = numpy.cumsum((1 - stackMaskedImg.mask), axis=0) * (1 - stackMaskedImg.mask) == 1 result = numpy.ma.MaskedArray((npaTempBool * stackMaskedImg.data).sum(axis=0), stackMaskedImg.mask.prod(axis=0)) elif self._strBlending.lower().startswith("max"): result = stackMaskedImg.max(axis=0) elif self._strBlending.lower().startswith("min"): result = stackMaskedImg.min(axis=0) else: #self._strBlending.lower() == "mean": result = stackMaskedImg.mean(axis=0) self._ndaResult = result.filled(self._fDummy)