from Packages import * st.set_page_config(page_title="NIRS Utils", page_icon=":goat:", layout="wide") from Modules import * from Class_Mod.DATA_HANDLING import * st.session_state["interface"] = st.session_state.get('interface') st.header("Predictions making", divider='blue') model_column, space, file_column= st.columns((2, 1, 1)) #M9, M10, M11 = st.columns([2,2,2]) NIRS_csv = file_column.file_uploader("Select NIRS Data to predict", type="csv", help=" :mushroom: select a csv matrix with samples as rows and lambdas as columns") export_folder = './data/predictions/' export_name = 'Predictions_of_' reg_algo = ["Interval-PLS"] if NIRS_csv: export_name += str(NIRS_csv.name[:-4]) qsep = file_column.selectbox("Select csv separator - _detected_: " + str(find_delimiter('data/'+NIRS_csv.name)), options=[";", ","], index=[";", ","].index(str(find_delimiter('data/'+NIRS_csv.name))), key=2) qhdr = file_column.selectbox("indexes column in csv? - _detected_: " + str(find_col_index('data/'+NIRS_csv.name)), options=["no", "yes"], index=["no", "yes"].index(str(find_col_index('data/'+NIRS_csv.name))), key=3) if qhdr == 'yes': col = 0 else: col = False pred_data = pd.read_csv(NIRS_csv, sep=qsep, index_col=col) # Load the model with joblib model_column.write("Load your saved predictive model") model_name_import = model_column.selectbox('Choose file:', options=os.listdir('data/models/'), key = 21) if model_name_import != ' ': export_name += '_with_' + str(model_name_import[:-4]) with open('data/models/'+ model_name_import,'rb') as f: model_loaded = joblib.load(f) if model_loaded: s = model_column.checkbox('the model is of ipls type?') model_column.success("The model has been loaded successfully", icon="✅") if s: index = model_column.file_uploader("select wavelengths index file", type="csv") if index: idx = pd.read_csv(index, sep=';', index_col=0).iloc[:,0].to_numpy() #result = '' if st.button("Predict"): if s: result = model_loaded.predict(pred_data.iloc[:,idx]) else: # use prediction function from application_functions.py to predict chemical values result = model_loaded.predict(pred_data) st.write('Predicted values are: ') st.dataframe(result.T) pd.DataFrame(result).to_csv(export_folder + export_name + '.csv', sep = ';') # export to local drive - Download download_results(export_folder + export_name + '.csv', export_name + '.csv') # create a report with information on the prediction ## see https://stackoverflow.com/a/59578663