Skip to content
Snippets Groups Projects
3-prediction.py 2.26 KiB
Newer Older
  • Learn to ignore specific revisions
  • 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 *
    
    #M9, M10, M11 = st.columns([2,2,2])
    # Prediction module - TO BE DONE !!!!!
    with st.container():
        st.header("Predictions making")
        st.write("---")
        st.write("Predict chemical values from NIRS")
        model_column, space, file_column= st.columns((2, 1, 1))
        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_'
        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)
    
            # 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:
                    model_column.success("The model has been loaded successfully", icon="")
        result = ''
    
        if st.button("Predict"):
            # use prediction function from application_functions.py to predict chemical values
            result = prediction(NIRS_csv, qsep, qhdr, model_loaded)
            st.write('Predicted values are: ')
            st.dataframe(result.T)
            pd.DataFrame(result).to_csv(export_folder + export_name + '.csv')
            # 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