diff --git a/src/pages/3-prediction.py b/src/pages/3-prediction.py index 01ef0be3d3dfbc02dc08031afb83f18ab631d0f8..b6affcc96ab118c812ea8c626ce5c1e8fd370555 100644 --- a/src/pages/3-prediction.py +++ b/src/pages/3-prediction.py @@ -2,88 +2,44 @@ from common import * st.set_page_config(page_title="NIRS Utils", page_icon=":goat:", layout="wide") +# layout +UiComponents(pagespath=pages_folder, csspath=css_file, imgpath=image_path, + header=True, sidebar=True, bgimg=False, colborders=True) +st_var(variable='predict', initialize=True, update=False, type='boolean') -# layout -UiComponents(pagespath = pages_folder, csspath= css_file,imgpath=image_path , - header=True, sidebar= True, bgimg=False, colborders=True) -# HTML pour le bandeau "CEFE - CNRS" -# bandeau_html = """ -# <div style="width: 100%; background-color: #4682B4; padding: 10px; margin-bottom: 10px;"> -# <h1 style="text-align: center; color: white;">CEFE - CNRS</h1> -# </div> -# """ -# # Injecter le code HTML du bandeau -# st.markdown(bandeau_html, unsafe_allow_html=True) -# PageStyle(pages_folder) - -# local_css(css_file / "style_model.css") -hash_ = '' -# def p_hash(add): -# global hash_ -# hash_ = hash_data(hash_+str(add)) -# return hash_ - +# ############### clean the results dir ############# dirpath = Path('report/results/model') if dirpath.exists() and dirpath.is_dir(): rmtree(dirpath) - -if 'Predict' not in st.session_state: - st.session_state['Predict'] = False - -#################################### Methods ############################################## +# ############### page preamble ############### st.header("Prediction Making") st.markdown("Predict future values using previously developed calibration.") -c1, c2 = st.columns([2, 1]) -c1.image("./images/prediction making.png", use_column_width=True) -pred_data = DataFrame - +c0, c1 = st.columns([1, .4]) +c0.image("./images/prediction making.png", use_column_width=True) +# ###### Begin : I- Data loading and preparation ###### +hash_ = '' +pred_data = DataFrame() - - -def preparespecdf(df): - other = df.select_dtypes(exclude = 'float') - spec = df.select_dtypes(include='float') - if other.shape[1] > 0: - rownames = other.iloc[:,0] - spec.index = rownames - else: - rownames = [str(i) for i in range(df.shape[0])] - if spec.shape[1]<60: - spec = DataFrame - return spec, other, rownames - -def check_exist(var): - out = var in globals() - return out - -with c2: - zip = st.file_uploader("Load your zip file:", type = ['.zip'], help=" :mushroom: select a csv matrix with samples as rows and lambdas as columns") - if not zip: - st.info('Info: Insert your zip file above!') - - disable1 = False if zip else True - new_data = st.file_uploader("Load NIRS Data for prediction making:", type = ['csv', 'dx'], help=" :mushroom: select a csv matrix with samples as rows and lambdas as columns", disabled=disable1) - if not disable1 : - info1 = st.info('Info: Insert your NIRS data file above!') - +with c1: + zip = st.file_uploader("Load your zip file:", type=[ + '.zip'], help=" :mushroom: select a csv matrix with samples as rows and lambdas as columns") if zip: - @st.cache_data def tempdir(prefix, dir): from tempfile import TemporaryDirectory - with TemporaryDirectory( prefix= prefix, dir= dir ) as temp_dir:# create a temp directory + # create a temp directory + with TemporaryDirectory(prefix=prefix, dir=dir) as temp_dir: tempdirname = os.path.split(temp_dir)[1] return tempdirname - - temp_dir = tempdir(prefix = "pred_temp", dir = "./temp") + + temp_dir = tempdir(prefix="pred_temp", dir="./temp") # Open and extract the zip file from zipfile import ZipFile with ZipFile(zip, 'r') as zip_ref: zip_ref.extractall(temp_dir) - - + def find_pkl_files(root_dir): # List to store the paths of .pkl files pkl_files = [] @@ -105,284 +61,330 @@ with c2: from joblib import load system_data = load(fi) -if new_data: - info1.empty() + st.success('The file have been loaded successfully') + else: + st.info('Info: Insert your zip file above!') -with c2: - if new_data: - hash_ = ObjectHash(current = hash_,add = new_data.name) - test = new_data.name.split('.')[-1] + disable1 = False if zip else True + # new_data + predfile = st.file_uploader("Load NIRS Data for prediction making:", type=[ + 'csv', 'dx'], help=" :mushroom: select a csv matrix with samples as rows and lambdas as columns", disabled=disable1) + if not predfile: + st.info('Info: Insert your NIRS data file above!') + + +with c1: + if predfile: + hash_ = ObjectHash(current=hash_, add=predfile.name) + filetype = predfile.name.split('.')[-1] export_name = 'Pred_of' - export_name += new_data.name[:new_data.name.find('.')] + export_name += predfile.name[:predfile.name.find('.')] - match test: + match filetype: case 'csv': c1_1, c2_2 = st.columns([.5, .5]) with c1_1: - qdec = st.radio('decimal(x):', options= [".", ","], horizontal = True) - qsep = st.radio("separator(x):", options = [";", ","], horizontal = True) + dec = st.radio('decimal(x):', options=[ + ".", ","], horizontal=True) + sep = st.radio("separator(x):", options=[ + ";", ","], horizontal=True) with c2_2: - qhdr = st.radio("header(x): ", options = ["yes", "no"], horizontal = True) - qnames = st.radio("samples name(x):", options = ["yes", "no"], horizontal = True) - - qhdr = 0 if qhdr =="yes" else None - qnames = 0 if qnames =="yes" else None - hash_ = ObjectHash(current = hash_,add = [qsep, qhdr, qnames, qdec]) - - - - def read_csv(file = None, change = None, dec = None, sep= None, names = None, hdr = None): - HandleItems.delete_files(keep = ['.py', '.pyc','.bib']) - from utils.data_parsing import CsvParser - if file is not None: - par = CsvParser(file= file) - par.parse(decimal = dec, separator = sep, index_col = names, header = hdr) - return par.float, par.meta_data, par.meta_data_st_, par.df + hdr = st.radio("header(x): ", options=[ + "yes", "no"], horizontal=True) + names = st.radio("samples name(x):", options=[ + "yes", "no"], horizontal=True) + hdr = 0 if hdr == "yes" else None + names = 0 if names == "yes" else None + hash_ = ObjectHash(current=hash_, add=[ + sep, hdr, names, dec]) try: - pred_data, _, _, df = read_csv(file= new_data, change = hash_, dec = qdec, sep = qsep, - names =qnames, hdr = qhdr) - rownames = pred_data.index - st.success('file has been loaded successfully') + hash_ = ObjectHash(current=hash_, add=[ + predfile.getvalue(), dec, sep, hdr, names]) + x_block, meta_data = csv_parser( + path=predfile, decimal=dec, separator=sep, index_col=names, header=hdr, change=None) + if x_block.shape[1] > 20: + st.success( + "The data have been loaded successfully and spectral data was successfully detected, you might need to tune dialect.", icon="✅") + else: + st.warning( + "The data have been loaded successfully but spectral data was not detected.") except: - df = read_csv(new_data, sep=qsep, header= col, decimal=".") - pred_data, cat, rownames = preparespecdf(df) + st.error( + 'Error: The xfile has not been loaded successfully, please consider tuning the dialect settings!') case "dx": - with NamedTemporaryFile(delete=False, suffix=".dx") as tmp: - tmp.write(new_data.read()) - tmp_path = tmp.name - with open(tmp.name, 'r') as dd: - dxdata = new_data.read() - hash_ = ObjectHash(current= hash_, add = str(dxdata)+str(new_data.name)) - - ## load and parse the temp dx file - @st.cache_data - def dx_loader(change): - from utils.data_parsing import jcamp_parser - M = jcamp_parser(path = tmp_path) - M.parse() - return M.chem_data, M.specs_df_, M.meta_data, M.meta_data_st_ - - chem_data, spectra, meta_data, _ = dx_loader(change = hash_) - st.success("The data have been loaded successfully", icon="✅") - if chem_data.to_numpy().shape[1]>0: - # yname = st.selectbox('Select target', options=chem_data.columns, index=chem_data.columns.to_list().index(system_data['data']['target'].name)) - yname = system_data['data']['target'].name - st.info("Loaded model to predict " + yname) - measured = chem_data.loc[:,yname] == 0 - y = chem_data.loc[:,yname].loc[measured] - pred_data = spectra.loc[measured] - - else: - pred_data = spectra - os.unlink(tmp_path) + try: + hash_ = ObjectHash(current=predfile.getvalue()) + # creating the temp file + with NamedTemporaryFile(delete=False, suffix=".dx") as tmp: + tmp.write(predfile.read()) + tmp_path = tmp.name + + from utils.data_parsing import jcamp_parser + x_block, y_block, meta_data = jcamp_parser( + path=tmp_path, include='all', change=hash_) + st.success( + "Info: The data have been loaded successfully", icon="✅") + except: + st.error( + 'Error: The input file has not been loaded successfully, please consider tuning the dialect settings!') + + if y_block.to_numpy().shape[1] > 0: + yname = system_data['data']['target'].name + st.info("Loaded model to predict " + yname) + y = y_block.loc[:, yname] + pred_data = x_block + pred_data.columns = pred_data.columns.astype(str) # Load parameters st.subheader("I - Spectral data preprocessing & visualization", divider='blue') -# try: -if not pred_data.empty:# Load the model with joblib - @st.cache_data - def preprocess_spectra(data, change): - from scipy.signal import savgol_filter - - # M4.write(ProcessLookupError) - - if system_data['spec-preprocessing']['normalization'] == 'Snv': - x1 = Snv(data) - norm = 'Standard Normal Variate' - else: - norm = 'No Normalization was applied' - x1 = data - x2 = savgol_filter(x1, - window_length = int(system_data['spec-preprocessing']['SavGol(polyorder,window_length,deriv)'][1]), - polyorder = int(system_data['spec-preprocessing']['SavGol(polyorder,window_length,deriv)'][0]), - deriv = int(system_data['spec-preprocessing']['SavGol(polyorder,window_length,deriv)'][2]), - delta=1.0, axis=-1, mode="interp", cval=0.0) - preprocessed = DataFrame(x2, index = data.index, columns = data.columns) - return norm, preprocessed - norm, preprocessed = preprocess_spectra(pred_data, change= hash_) - - # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # - # @st.cache_data - # def specplot_raw(change): - # fig2 = plot_spectra(pred_data, xunits = 'lab', yunits = "meta_data.loc[:,'yunits'][0]") - # return fig2 - # rawspectraplot = specplot_raw(change = hash_) - rawspectraplot = plot_spectra(pred_data, xunits = 'Wavelength/Wavenumber', yunits = "Signal intensity") - - c3, c4 = st.columns([2, 1]) - with c3: +if not pred_data.empty: # Load the model with joblib + try: + # @st.cache_data + def preprocess_spectra(data, change): + from scipy.signal import savgol_filter + if system_data['spec-preprocessing']['normalization'] == 'Snv': + x1 = Snv(data) + norm = 'Standard Normal Variate' + else: + norm = 'No Normalization was applied' + x1 = data + x2 = savgol_filter(x1, + window_length=int( + system_data['spec-preprocessing']['SavGol(polyorder,window_length,deriv)'][1]), + polyorder=int( + system_data['spec-preprocessing']['SavGol(polyorder,window_length,deriv)'][0]), + deriv=int( + system_data['spec-preprocessing']['SavGol(polyorder,window_length,deriv)'][2]), + delta=1.0, axis=-1, mode="interp", cval=0.0) + preprocessed = DataFrame( + x2, index=data.index, columns=data.columns) + return norm, preprocessed + norm, preprocessed = preprocess_spectra( + pred_data.loc[:, system_data['predictors_']], change=hash_) + except: + st.write("Error: Spectral preprocessing was not performed successfully.") + +if not preprocessed.empty: + c2, c3 = st.columns([2, 1]) + with c2: + rawspectraplot = plot_spectra( + pred_data, color=None, cmap=None, xunits='Wavelength/Wavenumber', yunits="Signal intensity") + prepspectraplot = plot_spectra( + preprocessed, color=None, cmap=None, xunits='Wavelength/Wavenumber', yunits="Signal intensity") + st.write('Raw spectra') st.pyplot(rawspectraplot) + st.write('Preprocessed spectra') + st.pyplot(prepspectraplot) - ## plot preprocessed spectra - if check_exist("preprocessed"): - # def specplot_prep(change): - # fig2 = plot_spectra(preprocessed, xunits = 'lab', yunits = "meta_data.loc[:,'yunits'][0]") - # return fig2 - # prepspectraplot = specplot_prep(change = hash_) - prepspectraplot = plot_spectra(preprocessed, xunits = 'Wavelength/Wavenumber', yunits = "Signal intensity") - st.write('Preprocessed spectra') - st.pyplot(prepspectraplot) - with c4: + # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # + with c3: @st.cache_data def prep_info(change): - SG = f'- Savitzky-Golay derivative parameters \n:(Window_length:{system_data['spec-preprocessing']['SavGol(polyorder,window_length,deriv)'][1]}; polynomial order: {system_data['spec-preprocessing']['SavGol(polyorder,window_length,deriv)'][0]}; Derivative order : {system_data['spec-preprocessing']['SavGol(polyorder,window_length,deriv)'][2]})' - Norm = f'- Spectral Normalization \n: {system_data['spec-preprocessing']['normalization']}' + pr = system_data['spec-preprocessing']['SavGol(polyorder,window_length,deriv)'] + aa, bb, cc = pr[1], pr[0], pr[2] + SG = '- Savitzky-Golay derivative : \n(Window_length:' + str( + aa)+'; polynomial order:' + str(bb)+'; Derivative order :'+str(cc)+')' + Norm = '- Spectral Normalization \n:' + \ + system_data['spec-preprocessing']['normalization'] return SG, Norm - SG, Norm = prep_info(change = hash_) - st.info('The spectra were preprocessed using:\n'+SG+"\n"+Norm) + SG, Norm = prep_info(change=hash_) + st.info('The spectra were preprocessed using:\n'+SG+"\n" + Norm) - ################### Predictions making ########################## + with c3: + import cProfile + columns = system_data['predictors_'] + + def calpred_pca(cal, val, pred): + from sklearn.decomposition import PCA + pca = PCA(n_components=3) + pca.fit(cal) + pc = pca.transform(cal) + pv = pca.transform(val) + pt = pca.transform(pred) + import plotly.graph_objects as go + fig = go.Figure() + fig.add_trace(go.Scatter3d( + x=pt[:, 0], y=pt[:, 1], z=pt[:, 2], marker=dict(color='black'), mode='markers', name='All/prediction')) + fig.add_trace(go.Scatter3d( + x=pc[:, 0], y=pc[:, 1], z=pc[:, 2], marker=dict(color='blue'), mode='markers', name='Cal')) + fig.add_trace(go.Scatter3d( + x=pv[:, 0], y=pv[:, 1], z=pv[:, 2], marker=dict(color='red'), mode='markers', name='Val')) + return fig + + pca = calpred_pca(cal=system_data['data']['raw-spectra'].iloc[system_data['data']["training_data_idx"], :].loc[:, columns], + val=system_data['data']['raw-spectra'].iloc[system_data['data']["testing_data_idx"], :].loc[:, columns], pred=pred_data) + + st.write('-----------------') + st.write('3D PCA on raw data for data distribution analysis') + st.plotly_chart(pca) + + +# ################### Predictions making ########################## st.subheader("II - Prediction making", divider='blue') - - disable2 = False if check_exist("pred_data") else True - pred_button = st.button('Predict', type='primary', disabled= disable2, use_container_width=False) - if pred_button:st.session_state['Predict'] = True - - if st.session_state['Predict']: - if check_exist("pred_data"):# Load the model with joblib - c5, c6 = st.columns([2, 1]) - with c6: + + disable2 = False if not pred_data.empty else True + pred_button = st.button('Predict', type='primary', + disabled=disable2, use_container_width=False) + if pred_button: + st.session_state['predict'] = True + + if st.session_state['predict']: + if not preprocessed.empty: + c4, c5 = st.columns([2, 1]) + with c5: model = system_data['model_'] - if system_data['model_type'] in ['PLS','TPE-iPLS']: + if system_data['model_type'] in ['PLS', 'TPE-iPLS']: nvar = system_data['model_'].n_features_in_ - elif system_data['model_type'] =='LW-PLS': + elif system_data['model_type'] == 'LW-PLS': nvar = system_data['data']['raw-spectra'].shape[1] - - if check_exist('preprocessed'): - if isinstance(system_data['selected-wls']['idx'], DataFrame): - idx = np.concatenate([np.arange(system_data['selected-wls']['idx'].values.reshape((-1,))[2*i],system_data['selected-wls']['idx'].values.reshape((-1,))[2*i+1]+1) for i in range(system_data['selected-wls']['idx'].shape[0])]) - else: - idx = np.arange(nvar) - - if np.max(idx) <= preprocessed.shape[1]: - preprocesseddf = preprocessed.iloc[:,idx] ### get predictors - else: - st.error("Error: The number of columns in your data does not match the number of columns used to train the model. Please ensure they are the same.") - - - if check_exist("preprocesseddf"): - if st.session_state['Predict'] and nvar == preprocesseddf.shape[1]: - # if nvar == preprocesseddf.shape[1]: - match system_data['model_type']: - case 'PLS'|'TPE-iPLS': - try: - result = DataFrame(system_data['model_'].predict(preprocesseddf), index = rownames, columns = ['Results']) - except: - st.error(f'''Error: Length mismatch: the number of samples indices is {len(rownames)}, while the model produced - {len(model.predict(preprocesseddf))} values. correct the "indexes column in csv?" parameter''') - case 'LW-PLS': - try: - temp_path = Path('temp/') - # export data to csv for Julia train/pred - # with pretreatments - spectra = preprocess_spectra(system_data['data']['raw-spectra'], change= hash_) - x_pred = preprocessed - rownames = x_pred.index.to_list() - y = system_data['data']['target'] - data_to_work_with = ['spectra_np', 'y_np', 'x_pred_np'] - spectra_np, y_np, x_pred_np = spectra[1].to_numpy(), y.to_numpy(), x_pred.to_numpy() - # export spectra, y, x_pred to temp folder as csv files - for i in data_to_work_with: - j = globals()[i] - np.savetxt(temp_path / str(i + ".csv"), j, delimiter=",") - # export best LWPLSR params - with open(temp_path / "lwplsr_best_params.json", "w+") as outfile: - json.dump(system_data['lwpls_params'], outfile) - # create empty file to specify LWPLSR_Call.py that we want predictions - open(temp_path / 'predict', 'w').close() - # # run Julia Jchemo as subprocess - import subprocess - subprocess_path = Path("utils/") - subprocess.run([f"{sys.executable}", subprocess_path / "LWPLSR_Call.py"]) - # retrieve json results from Julia JChemo - try: - with open(temp_path / "lwplsr_outputs.json", "r") as outfile: - Reg_json = json.load(outfile) - # delete csv files - for i in data_to_work_with: os.unlink(temp_path / str(i + ".csv")) - os.unlink(temp_path / 'predict') - # delete json file after import - os.unlink(temp_path / "lwplsr_outputs.json") - os.unlink(temp_path / "lwplsr_best_params.json") - # format result data into Reg object - result = DataFrame(Reg_json['y_pred']) ### keys of the json dict - result.index = rownames - result.columns = ['Results'] - except FileNotFoundError as e: - Reg = None - for i in data_to_work_with: os.unlink(temp_path / str(i + ".csv")) - os.unlink(temp_path / 'predict') - except: - st.error('Error during LWPLSR predictions') - - ################################### results display ################################### - if check_exist("preprocesseddf"): - if preprocesseddf.shape[1]>1 and check_exist('result'): - hist = pred_hist(pred=result) - with c5: - st.write('Predicted values distribution') - st.pyplot(hist) - st.write('Predicted values table') - resultT = result.reset_index() - st.dataframe(resultT.T) - with c6: - from utils.miscellaneous import desc_stats - st.info('descriptive statistics for the model output') - st.write(DataFrame(desc_stats(result))) - - elif pred_button and nvar != preprocesseddf.shape[1]: - with c6: - st.error(f'Error: The model was trained on {nvar} wavelengths, but you provided {preprocessed.shape[1]} wavelengths for prediction. Please ensure they match!') - - ################################# Download results ################################# - if check_exist('result'): - @st.cache_data(show_spinner =False) - def preparing_results_for_downloading(change): - match test: - # load csv file - case 'csv': - df.to_csv('report/results/dataset/'+ new_data.name, sep = ';', encoding = 'utf-8', mode = 'a') - case 'dx': - with open('report/results/dataset/'+new_data.name, 'w') as dd: - dd.write(dxdata) - - prepspectraplot.savefig('./report/results/figures/raw_spectra.png') - rawspectraplot.savefig('./report/results/figures/preprocessed_spectra.png') - hist.savefig('./report/results/figures/histogram.png') - result.round(4).to_csv('./report/results/The_analysis_result.csv', sep = ";") - return change - - preparing_results_for_downloading(change = hash_) - - @st.cache_data(show_spinner =False) - def tempdir(change): - from tempfile import TemporaryDirectory - with TemporaryDirectory( prefix="results", dir="./report") as temp_dir:# create a temp directory - tempdirname = os.path.split(temp_dir)[1] - if len(os.listdir('./report/results/figures/'))==3: - make_archive(base_name="./report/Results", format="zip", base_dir="out", root_dir = "./report")# create a zip file - move("./report/Results.zip", f"./report/{tempdirname}/Results.zip")# put the inside the temp dir - with open(f"./report/{tempdirname}/Results.zip", "rb") as f: - zip_data = f.read() - return tempdirname, zip_data - - date_time = datetime.now().strftime('%y%m%d%H%M') - try : - tempdirname, zip_data = tempdir(change = hash_) - st.download_button(label = 'Download', data = zip_data, file_name = f'Nirs_Workflow_{date_time}_Pred_.zip', mime ="application/zip", - args = None, kwargs = None,type = "primary",use_container_width = True) - except: - st.write('-') - # except: - # c2.error('''Error: Data loading failed. Please check your file. Consider fine-tuning the dialect settings or ensure the file isn't corrupted.''') - - -else: - with c2: - if new_data: - st.error("Error!:The The data you provided for making predictions doesn't appear to be multivariable.!") + if system_data['selected-wls'] is None: + preprocesseddf = preprocessed + else: + preprocesseddf = preprocessed.iloc[:, system_data['selected-wls']] + +# if check_exist("preprocesseddf"): +# if st.session_state['predict'] and nvar == preprocesseddf.shape[1]: +# # if nvar == preprocesseddf.shape[1]: +# match system_data['model_type']: +# case 'PLS' | 'TPE-iPLS': +# try: +# result = DataFrame(system_data['model_'].predict( +# preprocesseddf), index=rownames, columns=['Results']) +# except: +# st.error(f'''Error: Length mismatch: the number of samples indices is {len(rownames)}, while the model produced +# {len(model.predict(preprocesseddf))} values. correct the "indexes column in csv?" parameter''') +# case 'LW-PLS': +# try: +# temp_path = Path('temp/') +# # export data to csv for Julia train/pred +# # with pretreatments +# spectra = preprocess_spectra( +# system_data['data']['raw-spectra'], change=hash_) +# x_pred = preprocessed +# rownames = x_pred.index.to_list() +# y = system_data['data']['target'] +# data_to_work_with = [ +# 'spectra_np', 'y_np', 'x_pred_np'] +# spectra_np, y_np, x_pred_np = spectra[1].to_numpy( +# ), y.to_numpy(), x_pred.to_numpy() +# # export spectra, y, x_pred to temp folder as csv files +# for i in data_to_work_with: +# j = globals()[i] +# np.savetxt( +# temp_path / str(i + ".csv"), j, delimiter=",") +# # export best LWPLSR params +# with open(temp_path / "lwplsr_best_params.json", "w+") as outfile: +# json.dump(system_data['lwpls_params'], outfile) +# # create empty file to specify LWPLSR_Call.py that we want predictions +# open(temp_path / 'predict', 'w').close() +# # # run Julia Jchemo as subprocess +# import subprocess +# subprocess_path = Path("utils/") +# subprocess.run( +# [f"{sys.executable}", subprocess_path / "LWPLSR_Call.py"]) +# # retrieve json results from Julia JChemo +# try: +# with open(temp_path / "lwplsr_outputs.json", "r") as outfile: +# Reg_json = json.load(outfile) +# # delete csv files +# for i in data_to_work_with: +# os.unlink(temp_path / str(i + ".csv")) +# os.unlink(temp_path / 'predict') +# # delete json file after import +# os.unlink(temp_path / "lwplsr_outputs.json") +# os.unlink( +# temp_path / "lwplsr_best_params.json") +# # format result data into Reg object +# # keys of the json dict +# result = DataFrame(Reg_json['y_pred']) +# result.index = rownames +# result.columns = ['Results'] +# except FileNotFoundError as e: +# Reg = None +# for i in data_to_work_with: +# os.unlink(temp_path / str(i + ".csv")) +# os.unlink(temp_path / 'predict') +# except: +# st.error('Error during LWPLSR predictions') + +# ################################### results display ################################### +# if check_exist("preprocesseddf"): +# if preprocesseddf.shape[1] > 1 and check_exist('result'): +# hist = pred_hist(pred=result) +# with c5: +# st.write('Predicted values distribution') +# st.pyplot(hist) +# st.write('Predicted values table') +# resultT = result.reset_index() +# st.dataframe(resultT.T) +# with c6: +# from utils.miscellaneous import desc_stats +# st.info('descriptive statistics for the model output') +# st.write(DataFrame(desc_stats(result))) + +# elif pred_button and nvar != preprocesseddf.shape[1]: +# with c6: +# st.error(f'Error: The model was trained on {nvar} wavelengths, but you provided { +# preprocessed.shape[1]} wavelengths for prediction. Please ensure they match!') + +# ################################# Download results ################################# +# if check_exist('result'): +# @st.cache_data(show_spinner=False) +# def preparing_results_for_downloading(change): +# match test: +# # load csv file +# case 'csv': +# df.to_csv('report/results/dataset/' + predfile.name, +# sep=';', encoding='utf-8', mode='a') +# case 'dx': +# with open('report/results/dataset/'+predfile.name, 'w') as dd: +# dd.write(dxdata) + +# prepspectraplot.savefig( +# './report/results/figures/raw_spectra.png') +# rawspectraplot.savefig( +# './report/results/figures/preprocessed_spectra.png') +# hist.savefig('./report/results/figures/histogram.png') +# result.round(4).to_csv( +# './report/results/The_analysis_result.csv', sep=";") +# return change + +# preparing_results_for_downloading(change=hash_) + +# @st.cache_data(show_spinner=False) +# def tempdir(change): +# from tempfile import TemporaryDirectory +# # create a temp directory +# with TemporaryDirectory(prefix="results", dir="./report") as temp_dir: +# tempdirname = os.path.split(temp_dir)[1] +# if len(os.listdir('./report/results/figures/')) == 3: +# make_archive(base_name="./report/Results", format="zip", +# base_dir="out", root_dir="./report") # create a zip file +# # put the inside the temp dir +# move("./report/Results.zip", +# f"./report/{tempdirname}/Results.zip") +# with open(f"./report/{tempdirname}/Results.zip", "rb") as f: +# zip_data = f.read() +# return tempdirname, zip_data + +# date_time = datetime.now().strftime('%y%m%d%H%M') +# try: +# tempdirname, zip_data = tempdir(change=hash_) +# st.download_button(label='Download', data=zip_data, file_name=f'Nirs_Workflow_{date_time}_Pred_.zip', mime="application/zip", +# args=None, kwargs=None, type="primary", use_container_width=True) +# except: +# st.write('-') +# # except: +# # c2.error('''Error: Data loading failed. Please check your file. Consider fine-tuning the dialect settings or ensure the file isn't corrupted.''') + + +# else: +# with c1: +# if predfile: +# st.error( +# "Error!:The The data you provided for making predictions doesn't appear to be multivariable.!")