def __init__(self, felixfile, location, plotIt=True, checkdir=True, verbose=True): attributes = { 'felixfile': felixfile, 'fname': felixfile.split(".")[0], 'baseline': None, 'data': None, 'undo_counter': 0, 'redo_counter': 0, 'removed_datas': np.array([[], [], []]), 'redo_datas': np.array([[], [], []]), 'removed_index': [], 'redo_index': [], 'felix_corrected': False, "baseline_corrected": False, 'plotIt':plotIt, "verbose": verbose } for keys, values in attributes.items(): setattr(self, keys, values) if felixfile.endswith("ofelix"): self.opo = True self.basefile = f'{self.fname}.obase' else: self.opo = False self.basefile = f'{self.fname}.base' self.powerfile = f'{self.fname}.pow' self.cfelix = f'{self.fname}.cfelix' folders = ["DATA", "EXPORT", "OUT"] if checkdir: back_dir = dirname(location) if set(folders).issubset(os.listdir(back_dir)): self.location = pt(back_dir) else: self.location = pt(location) os.chdir(self.location) else: self.location = location os.chdir(location) if verbose: print(f"Current location: {self.location}") for dirs in folders: if not isdir(dirs): os.mkdir(dirs) if isfile(self.felixfile): move(self.location, self.felixfile) if isfile(self.basefile): move(self.location, self.basefile) if isfile(self.powerfile): move(self.location, self.powerfile) self.checkInf() self.felix_read_file() self.PPS = 5 self.NUM_POINTS = 10 if isfile(f'./DATA/{self.basefile}'): if verbose: print(f"Basefile EXISTS: Opening existing basefile for baseline points") self.ReadBase() # Read baseline file if exist else guess it else: if verbose: print(f"Basefile doesn't EXISTS: Guessing baseline points") self.GuessBaseLine() self.line = Line2D(self.xs, self.ys) if plotIt: self.InteractivePlots() # Plot
def __init__(self, inp, outfile, verbose=None): self.inpdir = pt(inp[0]) try: assert inp[1] in ['image', 'audio', 'video'] except AssertionError as error: print('Wrong type given.', error) self.typus = inp[1].lower() self.outfile = pt(outfile) if verbose: RRL.verbosity = verbose
def export_file(self, fname, wn, inten, relative_depletion, energyPerPhoton, raw_intensity=None): with open('EXPORT/' + fname + '.dat', 'w+') as f: if raw_intensity is not None: f.write( "#NormalisedWavelength(cm-1)\t#NormalisedIntensity\t#RelativeDepletion(%)\t#IntensityPerPhoton\t#RawIntensity\n" ) for i in range(len(wn)): f.write( f"{wn[i]}\t{inten[i]}\t{relative_depletion[i]}\t{energyPerPhoton[i]}\t{raw_intensity[i]}\n" ) else: f.write( "#NormalisedWavelength(cm-1)\t#NormalisedIntensity\t#RelativeDepletion(%)\t#IntensityPerPhoton\n" ) for i in range(len(wn)): f.write( f"{wn[i]}\t{inten[i]}\t{relative_depletion[i]}\t{energyPerPhoton[i]}\n" ) expfitFile = pt(f"./EXPORT/{fname}.expfit") if not expfitFile.exists(): with open(expfitFile, 'w+') as f: f.write( f"#Frequency\t#Freq_err\t#Sigma\t#Sigma_err\t#FWHM\t#FWHM_err\t#Amplitude\t#Amplitude_err\n" )
def latexPlot(self): style_path = pt(__file__).parent / "matplolib_styles/styles/science.mplstyle" with plt.style.context([f"{style_path}"]): fig, ax0 = plt.subplots() fig2, ax1 = plt.subplots() ax0.set(xlabel="n*t*E (mJ)", ylabel="Counts", title="Res ON-OFF scan") ax1.set(xlabel="n*t*E (mJ)", ylabel="Relative abundace of active isomer", title="$D(t)=A*(1-e^{-K_{ON}*(ntE)})$") ax0.grid() ax1.grid() for index, fitY, i in zip(["resOn", "resOff"], [self.fitOn, self.fitOff], [0, 1]): ax0.errorbar(self.power[index], self.counts[index], yerr=self.error[index], fmt=f"C{i}.") ax0.plot(self.fitX, fitY, f"C{i}") ax1.errorbar(self.power["resOn"], self.depletion_exp, yerr=self.depletion_exp_err, fmt="k.") ax1.plot(self.fitX, self.depletion_fitted) ax1.plot(self.fitX, self.relative_abundance) ax0.legend(labels=["ResON", "ResOFF"], title=f"Mass: {self.mass[0]}u, Res: {self.t_res}V, B0: {self.t_b0}ms") ax1.legend(["Fitted", f"A: {self.uA:.3f}", "Experiment"]) save_name = f"{self.widget.name.get()}_timescan.png" save_name2 = f"{self.widget.name.get()}_depletion.png" save_file = self.location / save_name save_file2 = self.location / save_name2 fig.savefig(save_file, dpi=self.widget.dpi_value.get()*3) fig2.savefig(save_file2, dpi=self.widget.dpi_value.get()*3) showinfo("Saved", f"File saved: {save_name} and {save_name2} \nin {self.location}")
def org_junk(): for entry in os.scandir(): if entry.is_dir(): continue file_path = pt(entry) file_format = file_path.suffix.lower() if file_format in FILE_FORMATS: directory_path = pt(FILE_FORMATS[file_format]) directory_path.mkdir(exist_ok=True) file_path.rename(directory_path.joinpath(file_path)) for dir in os.scandir(): try: os.rmdir(dir) except: pass
def __init__(self, scanfile, tkplot=False): self.scanfile = scanfile = pt(scanfile) self.location = location = scanfile.parent os.chdir(location) if tkplot: self.widget = FELion_Tk(title=scanfile, location=scanfile.parent) self.fig, self.canvas = self.widget.Figure( default_save_widget=False) self.widget.save_fmt = self.widget.Entries("Entry", "png", 0.1, 0.05 * 9 + 0.02) self.widget.save_btn = self.widget.Buttons("Save", 0.5, 0.05 * 9, self.savefig_timescan) savename = scanfile.stem ax = self.widget.make_figure_layout( title=f"Timescan: {scanfile.name}", xaxis="Time (ms)", yaxis="Counts", yscale="linear", savename=savename) self.widget.lines = {} if tkplot: time, mean, error = self.read_timescan_file(ax=ax) self.widget.lines["SUM"] = ax.errorbar(time, mean.sum(axis=0), yerr=error.sum(axis=0), label="SUM", fmt="k.-") self.widget.plot_legend = ax.legend() self.widget.mainloop() else: m = {} time, mean, error = self.read_timescan_file(tkplot=False, m=m) m["SUM"] = { "x": list(time), "y": list(mean.sum(axis=0)), "name": f"SUM", "mode": 'lines+markers', "line": { "color": "black" }, "error_y": { "type": "data", "array": list(error.sum(axis=0)), "visible": True } } sendData(m) self.time, self.mean, self.error = time, mean, error
def sendData(dataToSend): with open(pt(__file__).parent / "data.json", 'w+') as f: data = json.dumps(dataToSend, sort_keys=True, indent=4, separators=(',', ': ')) f.write(data)
def __init__(self, inp, quali, ani_mix=False, verbose=None, **kwargs): if verbose: C2wCommon.verbosity = verbose super().__init__() self.inpath = pt(inp) self.set_quali(quali, ani_mix) self.recode_webp = kwargs.get('recode_webp') self.conv_ani = kwargs.get('conv_ani') self.handle_src = kwargs.get('handle_src')
def save_fig(): save_fname = f"{widget.name.get()}.{widget.save_fmt.get()}" print(f"Saving filename: {save_fname}") location = filenames[0].parent save_filename = location / save_fname if not widget.latex.get(): widget.save_fig() else: style_path = pt(__file__).parent / "matplolib_styles/styles/science.mplstyle" with plt.style.context([f"{style_path}"]): fig, ax = plt.subplots() fit_data = plot_thz(ax=ax, tkplot=True, save_dat=False, latex=True) # Setting fig ax properties ax.grid(widget.plotGrid.get()) legend = ax.legend(bbox_to_anchor=[1, 1], fontsize=widget.xlabelSz.get()/2, title=f"Intensity: {fit_data.max():.2f}\%") legend.set_visible(widget.plotLegend.get()) # Setting title ax.set_title(widget.plotTitle.get().replace("_", "\_"), fontsize=widget.titleSz.get()) # Setting X and Y label if widget.plotYscale.get(): scale = "log" else: scale = "linear" ax.set(yscale=scale) ax.set( ylabel=widget.plotYlabel.get().replace("%", "\%"), xlabel=widget.plotXlabel.get() ) # Xlabel and Ylabel fontsize ax.xaxis.label.set_size(widget.xlabelSz.get()) ax.yaxis.label.set_size(widget.ylabelSz.get()) ax.tick_params(axis='x', which='major', labelsize=widget.xlabelSz.get()) ax.tick_params(axis='y', which='major', labelsize=widget.ylabelSz.get()) try: fig.savefig(save_filename, dpi=widget.dpi_value.get()*2) print(f"File saved:\n{save_filename}") if askokcancel('Open savedfile?', f'File: {save_fname}\nsaved in directory: {location}'): print("Opening file: ", save_filename) os.system(f"{save_filename}") except: showerror("Error", traceback.format_exc(5))
def mp_worker(self, inp): """Convert method for images with multiprocessing capapility.""" mp_conv_f, img_state = inp dst_f = pt(mp_conv_f).with_suffix('.webp') try: if img_state == "stl": self.stl_converter(mp_conv_f, dst_f) elif img_state == "ani": self.ani_converter(mp_conv_f, dst_f) except OSError: self.inf(1, f"Image {mp_conv_f} could not be converted.") if self.handle_src: self.orgs_switch(mp_conv_f)
def savefig_latex(): style_path = pt( __file__).parent / "matplolib_styles/styles/science.mplstyle" with style.context([f"{style_path}"]): self.fig2, self.ax2 = plt.subplots() for i, line in enumerate(self.ax.lines): x = line.get_xdata() y = line.get_ydata() lg = line.get_label().replace("_", "\_") if lg.endswith("felix"): ls = f"C{i}." elif lg.startswith("Binned"): ls = "k." else: ls = f"C{i}-" if lg == "Averaged" or lg.startswith("Fitted"): self.ax2.plot(x, y, "k-", label=lg, zorder=100) else: self.ax2.plot(x, y, ls, ms=2, label=lg) self.ax2.grid() self.ax2.set(xlim=self.ax.get_xlim()) legend = self.ax2.legend(bbox_to_anchor=[1, 1], fontsize=self.xlabelSz.get()) legend.set_visible(self.plotLegend.get()) # Setting title self.ax2.set_title(self.plotTitle.get().replace("_", "\_"), fontsize=self.titleSz.get()) # Setting X and Y label if self.plotYscale.get(): scale = "log" else: scale = "linear" self.ax2.set(yscale=scale) self.ax2.set(ylabel=self.plotYlabel.get().replace("%", "\%"), xlabel=self.plotXlabel.get()) # Xlabel and Ylabel fontsize self.ax2.xaxis.label.set_size(self.xlabelSz.get()) self.ax2.yaxis.label.set_size(self.ylabelSz.get()) self.ax2.tick_params(axis='x', which='major', labelsize=self.xlabelSz.get()) self.ax2.tick_params(axis='y', which='major', labelsize=self.ylabelSz.get()) self.fig2.savefig(save_filename, dpi=self.dpi_value.get() * 6)
def __init__(self, location): location = st.text_input("Current Location", location) self.location = pt(location) self.initialise() self.fig = make_subplots(rows=1, cols=2) try: self.get_timescan_data() Koff, N = self.resOff_fit() Na0, Nn0, Kon = self.resOn_fit(Koff, N) self.make_slider(Koff, Kon, N, Na0, Nn0) Koff = self.koff_slider Kon = self.kon_slider N = self.n_slider Na0 = self.na_slider Nn0 = self.nn_slider self.runFit(Koff, Kon, N, Na0, Nn0) layout = go.Layout( xaxis={"title":self.xaxis_title}, yaxis={"title":"Counts"}, xaxis2={"title":self.xaxis_title}, yaxis2={"title":"Relative depletion of active isomer"} ) self.fig.update_layout(layout) st.plotly_chart(self.fig, height=700) pycode = st.text_area("pyCode") with stdoutIO() as result: exec(pycode) st.write(result.getvalue()) except Exception as error: st.title("Choose proper ResOn and ResOff file from the sidebar") st.subheader("Error details") st.write(error)
def __init__(self, location, resOnFile=None, resOffFile=None, power=None, nshots=10, massIndex=0, timeStart=1): self.location = pt(location) self.scanfiles = list(self.location.glob("*.scan")) self.resOnFile = resOnFile self.resOffFile = resOffFile self.power = {"resOn": power[0]/1000, "resOff": power[1]/1000} # mJ to J self.nshots = nshots self.massIndex = massIndex self.timeStart = timeStart self.widget = FELion_Tk(title="Depletion Plot", location=self.location) self.create_figure() self.startPlotting() self.widget.mainloop()
def dirwalker(self): """Searches a directory for images, filters and provides them as a list.""" img_list = list() for path, dirs, files in os.walk(self.inpath): if 'img_backup' in dirs: dirs.remove('img_backup') for fln in files: self.src_file = pt(path).joinpath(fln) m_type, f_type = self.get_mimetype() if self.skip_check(m_type, f_type): C2wMain.file_count['fle_skip'] += 1 continue # assert format support early try: Image.open(self.src_file) except Image.UnidentifiedImageError as err: self.inf( 1, f"{err}" "Format is not supported by Pillow. Skipped.") C2wMain.file_count['fle_skip'] += 1 continue except Image.DecompressionBombError as err: if not self.big_img_assert(err): continue if self.test_ani(f_type) is False: C2wMain.file_count['stl_f_found'] += 1 img_list.append((self.src_file, "stl")) else: if self.conv_ani is True: # placing counter in worker funcs doesn't work C2wMain.file_count['ani_f_found'] += 1 img_list.append((self.src_file, "ani")) else: C2wMain.file_count['fle_skip'] += 1 continue return img_list
def __init__(self, title="FELion GUI2", location=".", background="light grey", *args, **kwargs): Tk.__init__(self, *args, **kwargs) self.location = pt(location) os.chdir(self.location) Tk.wm_title(self, title) Tk.wm_geometry(self, "1000x600") self.canvas_frame = Frame(self, bg='white') self.canvas_frame.place(relx=0, rely=0, relwidth=0.8, relheight=1) self.widget_frame = Frame(self, bg=background) self.widget_frame.bind("<Button-1>", lambda event: self.focus()) self.widget_frame.place(relx=0.8, rely=0, relwidth=0.2, relheight=1)
def dir_lister(self): """This finds, filters and lists all elements in the given path.""" self.tmp_lst.append(f"\n\n# {self.typus.title()} section {'#' * 64}\n") unsup_count = 0 for path, dirs, files in os.walk(self.inpdir): dirs = humansorted(dirs) subdirs = f" with subdirectorys: {', '.join(dirs)}" # TODO: rrl should not write empty dirs in the outfile self.tmp_lst.append( f"# ### Current directory: {path}/{subdirs if dirs else None}") files = humansorted(files) cur_dir = pt(path).name for fn in files: fullpath = path.joinpath(fn) format_status = self.format_test(fullpath) if format_status is True: fn_base = fn.stem.lower() rel_path = fullpath.relative_to(self.inpdir) self.tmp_lst.append( self.typus_statement(cur_dir, rel_path, fn_base)) elif format_status is False: unsup_count += 1 else: continue if unsup_count > 0: self.inf( 1, f"The directory contains {unsup_count!s} {self.typus} file(s) of non-supported type." )
savename="felix_averaged") datfiles = [ dat_location / f"{filename.stem}.dat" for filename in felixfiles ] avgfile = dat_location / f"{output_filename}.dat" wn, inten = read_dat_file(avgfile, norm_method) ax.plot(wn, inten, "k.-", label="Averaged", alpha=0.7, zorder=100) print(f"felix dat files: {datfiles}\nAveraged file: {avgfile}") for felixfile, datfile in zip(felixfiles, datfiles): wn, inten = read_dat_file(datfile, norm_method) ax.plot(wn, inten, ".", label=f"{felixfile.name}") widget.plot_legend = ax.legend() widget.mainloop() if __name__ == "__main__": args = sys.argv[1:][0].split(",") filenames = args[0:-1] norm_method = args[-1] filenames = [pt(i) for i in filenames] location = filenames[0].parent if location.name is "DATA": location = location.parent main(filenames, location, norm_method)
self.baseline = self.line.get_data() b = np.asarray(self.baseline) basefile = self.location / f"DATA/{self.basefile}" print(f"Saving basefile in {basefile}") try: with open(basefile, 'w') as f: f.write(f'#Baseline generated for {self.felixfile} data file!\n') f.write("#BTYPE=cubic\n") for i in range(len(b[0])): f.write("{:8.3f}\t{:8.2f}\n".format(b[0][i], b[1][i])) print(f"Basefile written for {self.felixfile} as {self.basefile}") if isfile(basefile): print(f'{self.basefile} is SAVED') self.fig.savefig(f'{self.location}/OUT/{self.fname}.png') return showinfo('Info', f'{self.basefile} file is saved in /DATA directory') except Exception as error: return showerror("Error", f"Following error has occured while saving {self.basefile} file\n{error}") def get_data(self): return np.asarray([self.data[0], self.data[1]]), np.asarray([self.line.get_data()]) if __name__ == "__main__": args = sys.argv[1:][0].split(",") filepaths = [pt(files) for files in args] for files in filepaths: felixfile = files.name location = files.parent Create_Baseline(felixfile, location)
def __init__(self, received_files, delta): self.delta = delta received_files = [pt(files) for files in received_files] self.location = received_files[0].parent # Cheking if the folder contents are already created folders = ["DATA", "EXPORT", "OUT"] back_dir = self.location.parent if set(folders).issubset(os.listdir(back_dir)): os.chdir(back_dir) self.location = back_dir else: os.chdir(self.location) dataToSend = { "felix": {}, "base": {}, "average": {}, "SA": {}, "pow": {} } xs = np.array([], dtype=np.float) ys = np.array([], dtype=np.float) c = 0 group = 1 for filename in received_files: felixfile = filename.name fname = filename.stem basefile = f"{fname}.base" powerfile = f"{fname}.pow" self.filetypes = [felixfile, basefile, powerfile] for folder, filetype in zip(folders, self.filetypes): if not isdir(folder): os.mkdir(folder) if isfile(filetype): shutil.move( self.location.joinpath(filetype), self.location.joinpath("DATA", filetype), ) # Wavelength and intensity of individuals without binning wavelength, intensity, raw_intensity, relative_depletion = self.norm_line_felix( ) # collecting Wavelength and intensity to average spectrum with binning xs = np.append(xs, wavelength) ys = np.append(ys, intensity) # Wavelength and intensity of individuals with binning wavelength, intensity = self.felix_binning(wavelength, intensity) self.powerPlot(powerfile, wavelength) ################################## # Spectrum Analyser wn, sa = self.saCal.get_data() X = np.arange(wn.min(), wn.max(), 1) dataToSend["SA"][felixfile] = { "x": list(wn), "y": list(sa), "name": f"{filename.stem}_SA", "mode": "markers", "line": { "color": f"rgb{colors[c]}" }, "legendgroup": f'group{group}', } dataToSend["SA"][f"{felixfile}_fit"] = { "x": list(X), "y": list(self.saCal.sa_cm(X)), "name": f"{filename.stem}_fit", "type": "scatter", "line": { "color": "black" }, "showlegend": False, "legendgroup": f'group{group}', } ############################### dataToSend["felix"][f"{felixfile}_histo"] = { "x": list(wavelength), "y": list(intensity), "name": felixfile, "type": "bar", "marker": { "color": f"rgb{colors[c]}" }, "legendgroup": 'group', } dataToSend["felix"][felixfile] = { "x": list(wavelength), "y": list(intensity), "name": felixfile, "type": "scatter", "line": { "color": f"rgb{colors[c]}" }, #"legendgroup": 'group2' } dataToSend["average"][felixfile] = { "x": list(wavelength), "y": list(intensity), "name": felixfile, "mode": "markers", "line": { "color": f"rgb{colors[c]}" }, } self.export_file(fname, wavelength, intensity, raw_intensity, relative_depletion) basefile_data = np.array( Create_Baseline(felixfile, self.location, plotIt=False).get_data()) # Ascending order sort by wn base_line = basefile_data[1][0] base_line = np.take(base_line, base_line[0].argsort(), 1).tolist() base_felix = basefile_data[0] base_felix = np.take(base_felix, base_felix[0].argsort(), 1).tolist() dataToSend["base"][f"{felixfile}_base"] = { "x": list(base_felix[0]), "y": list(base_felix[1]), "name": felixfile, "mode": "lines", "line": { "color": f"rgb{colors[c]}" }, "legendgroup": f'group{group}' } dataToSend["base"][f"{felixfile}_line"] = { "x": list(base_line[0]), "y": list(base_line[1]), "name": f"{filename.stem}_base", "mode": "lines+markers", "marker": { "color": "black" }, "legendgroup": f'group{group}', "showlegend": False, } dataToSend["pow"][powerfile] = { "x": list(self.power_wn), "y": list(self.power_mj), "name": powerfile, "mode": "markers", "xaxis": "x2", "yaxis": "y2", "marker": { "color": f"rgb{colors[c]}" }, "legendgroup": f'group{group}', } group += 1 c += 2 binns, intens = self.felix_binning(xs, ys) dataToSend["average"]["average"] = { "x": list(binns), "y": list(intens), "name": "Averaged", "mode": "lines", "line": { "color": "black" }, } # print(f"Before JSON DATA: {dataToSend}") dataJson = json.dumps(dataToSend) print(dataJson)
def savefig_timescan(self): save_fname = f"{self.widget.name.get()}.{self.widget.save_fmt.get()}" print(f"Saving filename: {save_fname}") save_filename = self.location / save_fname if not self.widget.latex.get(): self.widget.save_fig() else: style_path = pt( __file__).parent / "matplolib_styles/styles/science.mplstyle" with plt.style.context([f"{style_path}"]): fig, ax = plt.subplots() time, mean, error = self.read_timescan_file(ax=ax) ax.errorbar(time, mean.sum(axis=0), yerr=error.sum(axis=0), label="SUM", fmt="k.-") ax.grid(self.widget.plotGrid.get()) legend = ax.legend(bbox_to_anchor=[1, 1], fontsize=self.widget.xlabelSz.get() / 2) legend.set_visible(self.widget.plotLegend.get()) # Setting title ax.set_title(self.widget.plotTitle.get().replace("_", "\_"), fontsize=self.widget.titleSz.get()) # Setting X and Y label if self.widget.plotYscale.get(): scale = "log" else: scale = "linear" ax.set(yscale=scale) ax.set(ylabel=self.widget.plotYlabel.get().replace("%", "\%"), xlabel=self.widget.plotXlabel.get()) # Xlabel and Ylabel fontsize ax.xaxis.label.set_size(self.widget.xlabelSz.get()) ax.yaxis.label.set_size(self.widget.ylabelSz.get()) ax.tick_params(axis='x', which='major', labelsize=self.widget.xlabelSz.get()) ax.tick_params(axis='y', which='major', labelsize=self.widget.ylabelSz.get()) try: fig.savefig(save_filename, dpi=self.widget.dpi_value.get() * 2) print(f"File saved:\n{save_filename}") if askokcancel( 'Open savedfile?', f'File: {save_fname}\nsaved in directory: {self.location}' ): print("Opening file: ", save_filename) os.system(f"{save_filename}") except: showerror("Error", traceback.format_exc(5))
if len(line) > 1: line = line.strip() if line == 'ALL:': #print(f'\n{line} found at line no. {skip+1}\n') return skip + 1 skip += 1 return f'ALL: is not found in the file' def get_iterations(scanfile, location): os.chdir(location) iterations = np.array([]) with open(scanfile, 'r') as f: for line in f: if line.startswith('#mass'): #print(line) iterations = np.append(iterations, line.split(':')[-1]).astype(np.int64) else: continue return iterations if __name__ == "__main__": args = sys.argv[1:][0].split(",") filepaths = [pt(i) for i in args] location = filepaths[0].parent timescanplot(filepaths[0], location)
def plot_thz(ax=None, data={}, tkplot=False, save_dat=True, latex=False): xs, ys = [], [] for i, filename in enumerate(filenames): filename = pt(filename) freq, depletion_counts, iteraton = thz_plot(filename) model = gauss_fit(freq, depletion_counts) fit_data, uline_freq, usigma, uamplitude, ufwhm = model.get_data() freq_fit = uline_freq.nominal_value freq_fit_err = uline_freq.std_dev*1e7 lg = f"{filename.name} [{iteraton}]" lg_fit = f"Fit: {freq_fit:.7f}({freq_fit_err:.0f}) [{ufwhm.nominal_value*1e6:.1f} KHz]" if latex: lg = lg.replace("_", "\_") ms = 2 else: ms = 7 if tkplot: ax.plot(freq, depletion_counts, f"C{i}.", label=lg, ms=ms) ax.plot(freq, fit_data, f"C{i}-", label=lg_fit, zorder=100) else: data[f"{filename.name}"] = {"x": list(freq), "y": list(depletion_counts), "name": lg, "mode":'markers', "line":{"color":f"rgb{colors[i*2]}"} } data[f"{filename.name}_fit"] = {"x": list(freq), "y": list(fit_data), "name": lg_fit, "mode": "lines", "line":{"color":f"rgb{colors[i*2]}"} } xs = np.append(xs, freq) ys = np.append(ys, depletion_counts) # Averaged binx, biny = binning(xs, ys, delta) model = gauss_fit(binx, biny) fit_data, uline_freq, usigma, uamplitude, ufwhm = model.get_data() sigma = usigma.nominal_value fwhm = ufwhm.nominal_value amplitude = uamplitude.nominal_value half_max = amplitude/2 line_freq_fit = uline_freq.nominal_value freq_fit_err = uline_freq.std_dev*1e7 if save_dat: with open(f"./averaged_thz.dat", "w") as f: f.write("#Frequency(in MHz)\t#Intensity\n") for freq, inten in zip(binx, fit_data): f.write(f"{freq*1e3}\t{inten}\n") label = f"Binned (delta={delta*1e9:.2f} Hz)" if tkplot: ax.plot(binx, biny, "k.", label=label, ms=ms) ax.plot(binx, fit_data, "k-", label=f"Fitted: {line_freq_fit:.7f}({freq_fit_err:.0f}) [{fwhm*1e6:.1f} KHz]", zorder=100) ax.vlines(x=line_freq_fit, ymin=0, ymax=amplitude, zorder=10) ax.hlines(y=half_max, xmin=line_freq_fit-fwhm/2, xmax=line_freq_fit+fwhm/2, zorder=10) xcord, ycord = line_freq_fit, fit_data.max() ax.annotate(f'{line_freq_fit:.7f}({freq_fit_err:.0f})', xy=(xcord, ycord), xycoords='data', xytext=(xcord, ycord+5), textcoords='data', arrowprops=dict(arrowstyle="->", connectionstyle="arc3"), zorder=200 ) ax.set(ylim=([-(fit_data.max()/2), fit_data.max()*1.5])) return fit_data else: data["Averaged_exp"] = { "x": list(binx), "y": list(biny), "name":label, "mode": "markers", "marker":{"color":"black"} } data["Averaged_fit"] = { "x": list(binx), "y": list(fit_data), "name": f"Fitted: {line_freq_fit:.7f} ({freq_fit_err:.0f}) [{fwhm*1e6:.1f} KHz]", "mode": "lines", "line":{"color":"black"} } data["text"] = { "x":[line_freq_fit-9e-5, line_freq_fit], "y":[half_max*.7, -2], "text":[f"{fwhm*1e6:.1f} KHz", f"{line_freq_fit:.7f} GHz"], "mode":"text", "showlegend":False } data["shapes"] = { "center": { "type":"line", "x0":line_freq_fit, "x1":line_freq_fit, "y0": 0, "y1":amplitude, }, "fwhm": { "type":"line", "x0":line_freq_fit-fwhm/2, "x1":line_freq_fit+fwhm/2, "y0": half_max, "y1":half_max } } return data
ax = widget.make_figure_layout(title="THz scan", xaxis="Frequency (GHz)", yaxis="Depletion (%)", savename=savename) fit_data = plot_thz(ax=ax, tkplot=True) widget.plot_legend = ax.legend(title=f"Intensity: {fit_data.max():.2f} %") widget.mainloop() else: data = plot_thz() sendData(data) if __name__ == "__main__": global filenames args = sys.argv[1:][0].split(",") filenames = [pt(i) for i in args[0:-3]] gamma = float(args[-1])*1e-3 tkplot = args[-2] if tkplot == "plot": tkplot = True else: tkplot = False delta = float(args[-3]) # in Hz delta = delta*1e-9 # in GHz (to compare with our data) if tkplot: print(f"Received arguments: {args}") print(f"Received files: {filenames}") print(f"Gamma: {gamma} {args[-1]}") print(f"tkplot: {tkplot} {args[-2]}")
def __init__(self, received_files, delta, output_filename="averaged"): self.delta = delta received_files = [pt(files) for files in received_files] location = received_files[0].parent back_dir = dirname(location) folders = ["DATA", "EXPORT", "OUT"] if set(folders).issubset(os.listdir(back_dir)): self.location = pt(back_dir) else: self.location = pt(location) os.chdir(self.location) dataToSend = { "felix": {}, "base": {}, "average": {}, "SA": {}, "pow": {}, "felix_rel": {}, "average_rel": {}, "felix_per_photon": {}, "average_per_photon": {} } # For Average binning (Norm. method: log) xs = np.array([], dtype=np.float) ys = np.array([], dtype=np.float) # For Average binning (Norm. method: rel) xs_r = np.array([], dtype=np.float) ys_r = np.array([], dtype=np.float) c = 0 group = 1 color_size = len(colors) for filename in received_files: res, b0, trap = var_find(filename) label = f"Res:{res}; B0: {b0}ms; trap: {trap}ms" felixfile = filename.name fname = filename.stem basefile = f"{fname}.base" powerfile = f"{fname}.pow" try: with open(f"./DATA/{powerfile}") as f: for line in f: if line[0] == "#": if line.find("Hz") > -1: felix_hz = int(line.split(" ")[1]) break self.felix_hz = int(felix_hz) except: self.felix_hz = 10 self.nshots = int((trap / 1000) * self.felix_hz) self.filetypes = [felixfile, basefile, powerfile] for folder, filetype in zip(folders, self.filetypes): if not isdir(folder): os.mkdir(folder) if isfile(filetype): shutil.move( self.location.joinpath(filetype), self.location.joinpath("DATA", filetype), ) # Wavelength and intensity of individuals without binning wavelength, intensity, raw_intensity, relative_depletion = self.norm_line_felix( ) wavelength_rel = np.copy(wavelength) # collecting Wavelength and intensity to average spectrum with binning xs = np.append(xs, wavelength) ys = np.append(ys, intensity) xs_r = np.append(xs_r, wavelength_rel) ys_r = np.append(ys_r, relative_depletion) # Wavelength and intensity of individuals with binning wavelength, intensity = self.felix_binning(wavelength, intensity) wavelength_rel, relative_depletion = self.felix_binning( wavelength_rel, relative_depletion) energyJ = self.inten_per_photon(wavelength, intensity) self.export_file(fname, wavelength, intensity, relative_depletion, energyJ, raw_intensity) ################### Spectrum Analyser ################################# wn, sa = self.saCal.get_data() X = np.arange(wn.min(), wn.max(), 1) dataToSend["SA"][felixfile] = { "x": list(wn), "y": list(sa), "name": f"{filename.stem}_SA", "mode": "markers", "line": { "color": f"rgb{colors[c]}" }, "legendgroup": f'group{group}', } dataToSend["SA"][f"{felixfile}_fit"] = { "x": list(X), "y": list(self.saCal.sa_cm(X)), "name": f"{filename.stem}_fit", "type": "scatter", "line": { "color": "black" }, "showlegend": False, "legendgroup": f'group{group}', } ################### Spectrum Analyser END ################################# ################### Averaged and Normalised Spectrum ################################# # Normalised Intensity dataToSend["average"][felixfile] = { "x": list(wavelength), "y": list(intensity), "name": felixfile, "fill": 'tozeroy', "mode": "lines+markers", "line": { "color": f"rgb{colors[c]}" }, "marker": { "size": 1 } } # Relative Depletion Intensity dataToSend["average_rel"][felixfile] = { "x": list(wavelength_rel), "y": list(relative_depletion), "name": felixfile, "fill": 'tozeroy', "mode": "lines+markers", "line": { "color": f"rgb{colors[c]}" }, "marker": { "size": 1 } } # Intensitz per photon dataToSend["average_per_photon"][felixfile] = { "x": list(wavelength), "y": list(energyJ), "name": felixfile, "fill": 'tozeroy', "mode": "lines+markers", "line": { "color": f"rgb{colors[c]}" }, "marker": { "size": 1 } } ################### Averaged Spectrum END ################################# basefile_data = np.array( Create_Baseline(felixfile, self.location, plotIt=False, checkdir=False, verbose=False).get_data()) # Ascending order sort by wn base_line = basefile_data[1][0] base_line = np.take(base_line, base_line[0].argsort(), 1).tolist() base_felix = basefile_data[0] base_felix = np.take(base_felix, base_felix[0].argsort(), 1).tolist() dataToSend["base"][f"{felixfile}_base"] = { "x": list(base_felix[0]), "y": list(base_felix[1]), "name": f"{felixfile}: {label}", "mode": "lines", "line": { "color": f"rgb{colors[c]}" }, "legendgroup": f'group{group}' } dataToSend["base"][f"{felixfile}_line"] = { "x": list(base_line[0]), "y": list(base_line[1]), "name": f"{filename.stem}_base", "mode": "lines+markers", "marker": { "color": "black" }, "legendgroup": f'group{group}', "showlegend": False, } dataToSend["pow"][powerfile] = { "x": list(wavelength), "y": list(self.total_power), "name": f"{powerfile}: [{self.nshots} - ({self.felix_hz}Hz)]", "mode": "markers", "xaxis": "x2", "yaxis": "y2", "marker": { "color": f"rgb{colors[c]}" }, "legendgroup": f'group{group}', "showlegend": True, } group += 1 c += 2 if c >= color_size: c = 1 # For Normalised Intensity binns, intens = self.felix_binning(xs, ys) dataToSend["average"]["average"] = { "x": list(binns), "y": list(intens), "name": "averaged", "mode": "lines+markers", "line": { "color": "black" }, "marker": { "size": 1 } } # For intensityPerPhoton energyJ_norm = self.inten_per_photon(binns, intens) dataToSend["average_per_photon"]["average"] = { "x": list(binns), "y": list(energyJ_norm), "name": "averaged", "mode": "lines+markers", "line": { "color": "black" }, "marker": { "size": 1 } } # For relative binns_r, intens_r = self.felix_binning(xs_r, ys_r) dataToSend["average_rel"]["average"] = { "x": list(binns_r), "y": list(intens_r), "name": "averaged", "mode": "lines+markers", "line": { "color": "black" }, "marker": { "size": 1 } } # Exporting averaged.dat file self.export_file(f"averaged", binns, intens, intens_r, energyJ_norm) # print(f"Before JSON DATA: {dataToSend}") # dataJson = json.dumps(dataToSend) # print(dataJson) sendData(dataToSend)
A = pop_depletion A_err = perr_depletion self.uA = uf(A, A_err) print(f"A: {self.uA:.3uP}") self.relative_abundance = self.Depletion(self.fitX, A) if plot: self.ax1.errorbar(self.power["resOn"], self.depletion_exp, yerr=self.depletion_exp_err, fmt="k.") self.fit_plot, = self.ax1.plot(self.fitX, self.depletion_fitted) self.relativeFit_plot, = self.ax1.plot(self.fitX, self.relative_abundance) if __name__ == "__main__": args = sys.argv[1:][0].split(",") location = args[0] resOnFile = pt(location)/args[1] resOffFile = pt(location)/args[2] power = np.asarray(args[3:5], dtype=np.float) nshots = int(args[5]) massIndex = int(args[6]) TimeIndex = int(args[7]) print(f'Location: {location}\nON: {resOnFile}\nOFF: {resOffFile}\npower: {power} {type(power)}\nshots: {nshots} {type(nshots)}') print(f"MassIndex: {massIndex}\nTimeIndex: {TimeIndex}") depletionplot(location, resOnFile, resOffFile, power, nshots, massIndex, TimeIndex)
if tkplot: widget.plot_legend = ax.legend() widget.mainloop() else: fit_data.append({"annotations":annotations}) fit_data.append(get_data) sendData(fit_data) if __name__ == "__main__": args = sys.argv[1:][0].split(",") filename = args[0] location = pt(args[1]) if location.name == "DATA": location = location.parent filename = location / f"EXPORT/{filename}.dat" norm_method = args[2] prominence = args[3] if prominence == "": prominence = None else: prominence = float(prominence) fitall = args[4] if fitall == "true": fitall = True else: fitall = False width = args[5] if width == "": width = None
import os, sys from pathlib import Path as pt def delete_last_line(filename): print(f"File received: {filename}") with open(filename, "r+", encoding = "utf-8") as file: file.seek(0, os.SEEK_END) pos = file.tell() - 2 while pos > 0 and file.read(1) != "\n": pos -= 1 file.seek(pos, os.SEEK_SET) if pos > 0: file.seek(pos, os.SEEK_SET) file.truncate() print(f"Last line deleted") if __name__ == "__main__": args = sys.argv[1:][0].split(",") filename = f"{args[0]}.expfit" location = pt(args[1]) if location.name is "DATA": datfile_location = location.parent/"EXPORT" else: datfile_location = location/"EXPORT" filename = datfile_location / filename delete_last_line(filename)
"x": list(wn), "y": list(relative_depletion), "name": label, "mode": "lines", "showlegend": True, "line": { "color": f"rgb{colors[c]}" } } c += 2 if c >= len(colors): c = 1 if not tkplot: sendData(data) else: widget.plot_legend = ax.legend() widget.mainloop() if __name__ == "__main__": args = sys.argv[1:][0].split(",") opofiles = [pt(i) for i in args[:-1]] tkplot = args[-1] if tkplot == "plot": tkplot = True else: tkplot = False # print(args) opoplot(opofiles, tkplot)
# The views and conclusions contained in the software and documentation are those # of the authors and should not be interpreted as representing official policies, # either expressed or implied, of the FreeBSD Project. ### Load libraries import argparse, os, sys import pickle as pk import numpy as np import torch from torch import nn from torch import Tensor from torch.utils.data import DataLoader from torch import autograd import pickle as pk from pathlib import Path as pt sys.path.insert(0, os.path.dirname(pt(__file__).absolute())) import deepqmri if __name__ == "__main__": ### Print help and parse arguments parser = argparse.ArgumentParser( description= 'This program synthesise MRI signals that can be used to train a qMRI-net for quantitative MRI parameter estimation. Author: Francesco Grussu, University College London (<*****@*****.**><*****@*****.**>). Code released under BSD Two-Clause license. Copyright (c) 2020 University College London. All rights reserved.' ) parser.add_argument( 'mri_model', help= 'string indicating the MRI model to fit (choose among: "pr_hybriddwi" for prostate hybrid diffusion-relaxometry imaging; "br_sirsmdt" for brain saturation recovery diffusion tensor on spherical mean signals; "twocompdwite" for a two-compartment diffusion-t2 relaxation model without anisotropy)). Tissue parameters will be: model "pr_hybriddwi", parameters vl, v s.t. ve=(1-vl)*v, Dl, De, Ds, t2l, t2e, t2s, s0, where l/e/stroma stands for lumen/epithelium/stroma; model "br_sirsmdt", parameters dpar, kperp s.t. dperp=kperp*dpar, t1, s0; model "twocompdwite", parameters v, Da, t2a, Db, Kb, t2b, s0' ) parser.add_argument(
def orgs_switch(self, src_f): """Handles the orginal files if option is given.""" if self.handle_src == 'backup': self.orgs_bup(src_f) elif self.handle_src == 'erase' and pt(src_f).suffix != 'webp': pt(src_f).unlink()