def integrate(json: str, params: Calibration, f: Callable[[ndarray], ndarray], plot_calibrant: bool = False, save: bool = False, n: int = 10000, lst_mask: ndarray = None, lst_flat: ndarray = None, to_use: bool = False) -> None: """Integrate a file with a json calibration file""" gonio = pyFAI.goniometer.Goniometer.sload(json) with File(params.filename, mode='r') as h5file: images = [] deltas = [] for frame in gen_metadata_idx(h5file, params, to_use=to_use): images.append(f(frame.image)) deltas.append((frame.delta, )) mai = gonio.get_mg(deltas) res = mai.integrate1d(images, n, lst_mask=lst_mask, lst_flat=lst_flat) if save is True: try: os.makedirs(params.basedir) except os.error: pass numpy.savetxt( os.path.join(params.basedir, os.path.basename(params.filename) + '.txt'), numpy.vstack([res.radial, res.intensity]).T) if plot_calibrant: calibrant = get_calibrant(params.calibrant, params.wavelength) jupyter.plot1d(res, calibrant) else: jupyter.plot1d(res) return res
def integrate_mars_tx_tz(json: str, paramss: List[CalibrationMarsTxTz], f: Callable[[ndarray], ndarray], plot_calibrant: bool = False, save: bool = False, n: int = 10000, lst_mask: ndarray = None, lst_flat: ndarray = None, to_use: bool = False, no_plot: bool = False) -> None: """Integrate a file with a json calibration file""" gonio = pyFAI.goniometer.Goniometer.sload(json) images = [] positions = [] for params in paramss: with File(params.filename, mode='r') as h5file: for frame in gen_metadata_idx_mars_tx_tz(h5file, params, to_use=to_use): images.append(f(frame.image)) positions.append((frame.tx, frame.tz)) mai = gonio.get_mg(positions) res = mai.integrate1d(images, n, lst_mask=lst_mask, lst_flat=lst_flat) if save is True: try: os.makedirs(params.basedir) except os.error: pass ofilename = os.path.join(params.basedir, os.path.basename(params.filename) + '.txt') numpy.savetxt(ofilename, numpy.vstack([res.radial, res.intensity]).T) print("Saves as: {}".format(ofilename)) if no_plot is False: if plot_calibrant: calibrant = get_calibrant(params.calibrant, params.wavelength) jupyter.plot1d(res, calibrant) else: jupyter.plot1d(res) return res
def add_module(name, zero_pos, frame_start, frame_stop): ds = data[name] param = {"dist": 0.72, "poni1": 640*50e-6, "poni2": 4e-3, "rot1": 0, "offset": -get_position(zero_pos), "scale": 1, "nrj": nrj} # Lock enegy for now and a couple of other parameters bounds = {"nrj": (nrj, nrj), "dist": (0.7, 0.8), "poni2": (4e-3, 4e-3), "rot1": (0, 0), "scale": (1, 1)} gonioref = GoniometerRefinement(param, get_position, trans, detector=modules[name], wavelength=wl, bounds=bounds) goniometers[name] = gonioref for i in range(frame_start, frame_stop): cp = ControlPoints(calibrant=LaB6, wavelength=wl) peak = peak_picking(name, i) if len(peak) != 1: continue cp.append([peak[0]], ring=0) img = (ds[i]).reshape((-1, 1)) sg = gonioref.new_geometry("%s_%04i" % (name, i), image=img, metadata=i, control_points=cp, calibrant=LaB6) sg.geometry_refinement.data = numpy.array(cp.getList()) print(gonioref.chi2()) gonioref.refine2() tths = LaB6.get_2th() # print(tths) for i in range(frame_stop, ds.shape[0]): frame_name = "%s_%04i" % (name, i) if frame_name in gonioref.single_geometries: continue peak = peak_picking(name, i) ai = gonioref.get_ai(get_position(i)) tth = ai.array_from_unit(unit="2th_rad", scale=False) tth_low = tth[20] tth_hi = tth[-20] ttmin, ttmax = min(tth_low, tth_hi), max(tth_low, tth_hi) valid_peaks = numpy.logical_and(ttmin <= tths, tths < ttmax) cnt = valid_peaks.sum() if (len(peak) == cnt) and cnt > 0: cp = ControlPoints(calibrant=LaB6, wavelength=wl) # revert the order of assignment if needed !! if tth_hi < tth_low: peak = peak[-1::-1] for p, r in zip(peak, numpy.where(valid_peaks)[0]): cp.append([p], ring=r) img = (ds[i]).reshape((-1, 1)) sg = gonioref.new_geometry(frame_name, image=img, metadata=i, control_points=cp, calibrant=LaB6) sg.geometry_refinement.data = numpy.array(cp.getList()) # print(frame_name, len(sg.geometry_refinement.data)) print(" Number of peaks found and used for refinement") print(sum([len(sg.geometry_refinement.data) for sg in gonioref.single_geometries.values()])) gonioref.refine2() gonioref.set_bounds("poni1", -1, 1) gonioref.set_bounds("poni2", -1, 1) gonioref.set_bounds("rot1", -1, 1) gonioref.set_bounds("scale", 0.9, 1.1) gonioref.refine2() mg = gonioref.get_mg(position) mg.radial_range = (0, 95) images = [i.reshape(-1, 1) for i in ds] res_mg = mg.integrate1d(images, 50000) results[name] = res_mg LaB6_new = get_calibrant("LaB6") LaB6_new.wavelength = hc/gonioref.param[-1]*1e-10 p = jupyter.plot1d(res_mg, calibrant=LaB6_new) p.figure.show()
multigonio.refine2() # In[65]: LaB6_new = get_calibrant("LaB6") LaB6_new.wavelength = 1e-10 * hc / multigonio.param[-1] print(LaB6, "\n", LaB6_new) # In[66]: res = multigonio.integrate([data[ds_names[i]] for i in range(9)]) # In[67]: ax = jupyter.plot1d(res, calibrant=LaB6_new) ax.figure.show() ax.figure.savefig("toto.svg") # In[68]: # fig, ax = plt.subplots() # ax.plot(*calc_fwhm(res, LaB6_new, 10, 95), "o", label="FWHM") # ax.plot(*calc_peak_error(res, LaB6_new, 10, 95), "o", label="error") # ax.set_title("Peak shape & error as function of the angle") # ax.set_xlabel(res.unit.label) # ax.legend() # fig.show() # In[69]: print("total run time: ", time.time() - start_time)
mg = gonioref0.get_mg(position) mg.radial_range = (0, 95) images = [i.reshape(-1, 1) for i in ds] res_mg = mg.integrate1d(images, 50000) results = {name: res_mg} print(results) # In[20]: # Plot the integrated pattern vs expected peak positions: LaB6_new = get_calibrant("LaB6") LaB6_new.wavelength = hc/gonioref0.param[-1]*1e-10 p = jupyter.plot1d(res_mg, calibrant=LaB6_new) p.figure.show() # In[21]: # Peak profile function based on a bilinear interpolations: def calc_fwhm(integrate_result, calibrant, tth_min=None, tth_max=None): "calculate the tth position and FWHM for each peak" delta = integrate_result.intensity[1:] - integrate_result.intensity[:-1] maxima = numpy.where(numpy.logical_and(delta[:-1] > 0, delta[1:] < 0))[0] minima = numpy.where(numpy.logical_and(delta[:-1] < 0, delta[1:] > 0))[0] maxima += 1 minima += 1 tth = []