Newer
Older
from Packages import *
# local CSS
## load the custom CSS in the style folder

BARTHES Nicolas
committed
@st.cache_data
def local_css(file_name):
with open(file_name) as f:
st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
local_css("style/style.css")
# predict module
def prediction(NIRS_csv, qsep, qhdr, model):
# hdr var correspond to column header True or False in the CSV
if qhdr == 'yes':
col = 0
else:
col = False
X_test = pd.read_csv(NIRS_csv, sep=qsep, index_col=col)
Y_preds = model.predict(X_test)
# Y_preds = X_test
return Y_preds

BARTHES Nicolas
committed
@st.cache_data
def reg_plot( meas, pred):
fig, ax = plt.subplots(figsize = (12,4))
sns.regplot(x = meas[0] , y = pred[0], color='blue', label = 'Calib')
sns.regplot(x = meas[1], y = pred[1], color='red', label = 'CV')
sns.regplot(x = meas[2], y = pred[2], color='green', label = 'Test')
plt.plot([np.min(meas[0])-0.05, np.max([meas[0]])+0.05], [np.min(meas[0])-0.05, np.max([meas[0]])+0.05], color = 'black')
ax.set_ylabel('Predicted values')
ax.set_xlabel('Measured values')
plt.legend()
plt.margins(0)

BARTHES Nicolas
committed
@st.cache_data
def resid_plot( meas, pred):
fig, ax = plt.subplots(figsize = (12,4))
sns.residplot(x = meas[0], y = pred[0], color='blue', label = 'Calib')
sns.residplot(x = meas[1], y = pred[1], color='red', label = 'CV')
sns.residplot(x = meas[2], y = pred[2], color='green', label = 'Test')
ax.set_ylabel('Residuals')
ax.set_xlabel('Measured values')

BARTHES Nicolas
committed
plt.legend()
# function that create a download button - needs the data to save and the file name to store to
def download_results(data, export_name):
with open(data) as f:
st.download_button('Download Results', f, export_name)
@st.cache_resource
def plot_spectra(df):
if isinstance(df.columns[0], str):
m = 0
else:
m = np.min(df.columns)
fig, ax = plt.subplots(figsize = (30,7))
df.T.plot(legend=False, ax = ax, color = 'blue')
ax.set_xlabel('Wavelength/Wavenumber', fontsize=18)
ax.set_ylabel('Signal intensity', fontsize=18)
plt.margins(x = 0)
plt.annotate(text = f'The total number of spectra is {df.shape[0]}', xy =(m, np.max(df)), size=20, color = 'black', backgroundcolor='red')
return fig