def reportFit(self, outpath='', file='', reglog=None, **kwargs): # Plot result, fit if (file): saveit = True f = dataio.derive_filename(file, ext='png', sfx='fit') outpng = dataio.get_output_file(f, path=outpath) # outpng = deriveFilename(outpath+file, 'png', 'fit') f = dataio.derive_filename(file, ext='png', sfx='model') outpngmodel = dataio.get_output_file(f, path=outpath) # outpngmodel = deriveFilename(outpath+file, 'png', 'model') else: saveit = False outpng = '' outpngmodel = '' gr.plotStrainFit(self, save=saveit, file=outpng) # Plot result, model gr.plotStrainModel(self, save=saveit, file=outpngmodel) # Save results self.saveResults(outpath, file, reglog=reglog) # Report averaged value if (self.avgrange is not None): fmt = 'Integral over x in [{:g}, {:g}]: {:g} +- {:g}\n' if (not _quiet): print( fmt.format(self.avgrange[0], self.avgrange[1], self.avg[0], self.avg[1]))
def reportFit(self, outpath='', file='', plotSampling=False, **kwargs): if (file): saveit = True f = dataio.derive_filename(file, ext='png', sfx='fit') outpng = dataio.get_output_file(f, path=outpath) # outpng = deriveFilename(outpath+file, 'png', 'fit') f = dataio.derive_filename(file, ext='png', sfx='model') outpngmodel = dataio.get_output_file(f, path=outpath) #outpngmodel = deriveFilename(outpath+file, 'png', 'model') f = dataio.derive_filename(file, ext='png', sfx='depth') outpngdepth = dataio.get_output_file(f, path=outpath) # outpngdepth = deriveFilename(outpath+file, 'png', 'depth') else: saveit = False outpng = '' outpngmodel = '' outpngdepth = '' gr.plotIntensityFit(self, save=saveit, file=outpng) # Plot result, model gr.plotIntensityModel(self, save=saveit, file=outpngmodel) # Save results self.saveResults(outpath, file) # Information depth and sampling width if plotSampling: self.calInfoDepth(self.data[:, 0]) gr.plotInfoDepth(self, save=saveit, file=outpngdepth) self.saveInfoDepth(outpath, file)
def saveResolution(self, outpath, fname, sfx='resol'): """Save table with spatial resolution parameters.""" if ((self.resolution is not None) and fname): f = dataio.derive_filename(fname, ext='dat', sfx=sfx) fn = str(dataio.get_output_file(f, path=outpath)) ss = self.formatResultResol() print('Resolution data saved in ' + fn) with open(fn, 'w') as f: f.write('# Resolution data: ' + fname + "\n") f.write(ss) f.close
def savePseudoStrain(self, outpath, fname, sfx='deps'): """Save pseudo-strain, intensity and information depth table.""" if ((self.infodepth is not None) and fname): f = dataio.derive_filename(fname, ext='dat', sfx=sfx) fn = str(dataio.get_output_file(f, path=outpath)) ss = self.formatResultDepth() print('Pseudo-strain data saved in ' + fn) with open(fn, 'w') as f: f.write('# Pseudo-strain data: ' + fname + "\n") f.write(ss) f.close
def saveResults(self, outpath, fname, reglog=None): hasData = (self.data is not None) hasFit = (self.fit is not None) if (hasData and hasFit and fname): f = dataio.derive_filename(fname, ext='dat', sfx='model') fn = str(dataio.get_output_file(f, path=outpath)) # fn = deriveFilename(outpath+fname, ext='dat', sfx='model') ss = self.formatResultModel() print('Model saved in ' + fn) with open(fn, 'w') as f: f.write('# Fitted data: ' + fname + "\n") f.write(ss) f.close # Save original data and fit f = dataio.derive_filename(fname, ext='dat', sfx='fit') fn = str(dataio.get_output_file(f, path=outpath)) #fn = deriveFilename(outpath+fname, ext='dat', sfx='fit') ss = self.formatResultFit() print('Fit saved in ' + fn) with open(fn, 'w') as f: f.write('# Fitted data: ' + fname + "\n") f.write(ss) f.close # Save log with parameters f = dataio.derive_filename(fname, ext='log', sfx='') fn = str(dataio.get_output_file(f, path=outpath)) # fn = deriveFilename(outpath+fname, ext='log', sfx='') ss = self.formatResultLog() if (reglog is not None): ss += '# Regularization log:\n' for ia in range(reglog.shape[0]): ss += '{:g}\t{:g}\t{:g}\n'.format(*reglog[ia, :]) print('Log saved in ' + fn) with open(fn, 'w') as f: f.write('# Fit result: ' + fname + "\n") f.write(ss) f.close
def report_resolution(scan_range, file, nev=3000, depths=False, cog=False, inline=True, plot=True, save=True): """Calculate, plot and save spatial resolution data. Parameters ---------- scan_range : [min, max, n] Scan range [mm] given as min, max and number of positions. Positions are relative to the scan centre provided in sample geometry. file : str Output file name (_depth.dat will be added). nev : int, optional Number of events to be used for convolution. cog : bool Plot also centre of gravity of the sampling. inline : bool Plot all in one row (else plot intensity below the strains) plot : bool Show plot save: bool Save figures and table with results. """ # Initialize model model = mc.Sfit(nev=nev, xdir=_geom.scandir) # choose randomly a subset of sampling events sam.shuffleEvents() # define strain distribution model x = np.linspace(scan_range[0], scan_range[1], num=scan_range[2]) y = np.zeros(len(x)) fx = len(x)*[1] fy = len(x)*[1] model.defDistribution(par=[x, y], vary=[fx, fy], ndim=100, scaled=True) model.calResolution(x) if plot: f = dataio.derive_filename(file, ext='png', sfx='dpos') filepng = dataio.get_output_file(f) #gr.plotInfoDepth(model, save=save, file=filepng) #gr.plotPseudoStrain(model, save=save, file=filepng2) gr.plot_resolution(model, depth=True, cog=cog, inline=inline, save=save, file=filepng) if file and save: model.saveResolution('', file, sfx='dpos')
def plot_scene(nev, scan=None, filename='', rang=[30, 30], proj=1, save=True): """Plot 2D scene with experiment geometry. Parameters ---------- nev : int Number of sampling points to plot. scan: dict Meta-data for the scan filename : str Output file name (*_scene.png will be added) If not defined, tries to derive irt from scan['epsfile'] rang : array(2) with, height of plotted scene in [mm] proj : int projection plane: 0: (z, y); 1: (x, z); 2: (x, y) """ # retrieve sampling for given number of events nevp = min(nev,10000) sampling = sam.getSampling(nevp) # format output filename if filename: f = dataio.derive_filename(filename, ext='png', sfx='scene') outpng = str(dataio.get_output_file(f)) elif scan is not None and 'epsfile' in scan: f = dataio.derive_filename(scan['epsfile'], ext='png', sfx='scene') outpng = str(dataio.get_output_file(f)) else: outpng = '' # define ki and kf vectors in laboratory frame take_off = sampling.src['tth']*_deg # Scattering angle ki = np.array([0., 0., 1.]) # default for SIMRES simulations kf = Geometry.rotate(ki, 1, take_off) # do plot gr.plotScene(rang, proj, sam.shape, ki, kf, _geom.scandir, sampling, save=save, file=outpng)
def report_pseudo_strains(scan_range, file, nev=3000, intensity=False, inline=True, plot=True, save=True, use_int=False): """Calculate, plot and save calculated pseuostrains and related data. Parameters ---------- scan_range : [min, max, n] Scan range [mm] given as min, max and number of positions. Positions are relative to the scan centre provided in sample geometry. file : str Output file name. Just base name is used, `_depth.dat` will be added. nev : int, optional Number of events to be used for convolution. intensity : bool Plot also intensity vs. position inline : bool Plot all in one row (else plot intensity below the strains) plot : bool Show plot save: bool Save figures and table with results. use_int : bool Use previously fitted intensity distribution in calculation of pseudo-strain. """ data, model = cal_pseudo_strains(scan_range, nev=nev, use_int=use_int) if not intensity and 'intensity' in data: del data['intensity'] if plot: f = dataio.derive_filename(file, ext='png', sfx='deps') filepng = dataio.get_output_file(f) gr.plot_collection(data, inline=inline, save=save, file=filepng) if file and save: model.savePseudoStrain('', file, sfx='deps') return data