Skip to content

Commit

Permalink
added y calibration
Browse files Browse the repository at this point in the history
  • Loading branch information
evg-mar committed Aug 2, 2024
1 parent 5e8c48c commit 0db11cf
Show file tree
Hide file tree
Showing 4 changed files with 558 additions and 74 deletions.
23 changes: 22 additions & 1 deletion src/modules/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import streamlit as st
from streamlit.runtime.uploaded_file_manager import DeletedFile, UploadedFile


from pydantic import BaseModel
from typing import Literal

Expand All @@ -34,6 +35,15 @@ class SNIPBaselineArgs(BaseModel):
niter: int = 30


class StateSmooth(BaseModel):
use_smooth: bool = False
method: Literal['savgol', 'wiener', 'median',
'gauss', 'lowess', 'boxcar'] = 'savgol'

savgol_window_length: int = 5
savgol_polyorder: int = 3


class ALSBaselineArgs(BaseModel):
niter: int = 30
lam: float = 1e5
Expand All @@ -58,14 +68,25 @@ class StatePeakFind(BaseModel):
# value_sharpening: str | None = Neone # None | hhte
value_strategy: str | None = None # only string

# class StateSmooth()


class StateSpectrum(BaseModel):
crop: StateCrop
normalize: StateNormalize
normalize: StateNormalize | None = None
peak_find: StatePeakFind | None = None
baseline_corr: StateBaselineCorrection | None = None


class StateSpectrumSRMRef(BaseModel):

crop: StateCrop = StateCrop()
smooth: StateSmooth = StateSmooth()


default_state_srm_ref = StateSpectrumSRMRef()


default_peak_find_neon = StatePeakFind(value_sharpening=None,
value_strategy='topo')

Expand Down
62 changes: 59 additions & 3 deletions src/pages/apply_calibration.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

import matplotlib.patches as mpatches
import matplotlib.pyplot as plt

import pandas as pd

import streamlit as st
from front_end.htmlTemplates import css

from modules.navigation_bar import navbar
from ramanchada2.io.output.write_csv import write_csv as io_write_csv

from modules.models import default_state_target, StateNormalize, StateCrop
from modules.util import (
Expand Down Expand Up @@ -350,16 +350,65 @@ def load_tabs_target_spectrum():
"btn_press"
] = "apply_x_calib_btn"

apply_y_calib_btn = st.button("Apply Y-Calibration")

if apply_y_calib_btn:
if (
"target_spe_current"
not in st.session_state["cache_dicts"]["page01_load_spe"]
):
st.error("Target spectrum not loaded")

assert "ycalibration_model" in st.session_state["cache_dicts"]["y_calibration"]
st.session_state["cache_dicts"]["page03_apply_calib"][
"btn_press"
] = "apply_y_calib_btn"


btn_press = None
if "btn_press" in st.session_state["cache_dicts"]["page03_apply_calib"]:
btn_press = st.session_state["cache_dicts"]["page03_apply_calib"]["btn_press"]

# target_spe = st.session_state["cache_dicts"]["page01_load_spe"]["target_spe"]

if btn_press == "apply_x_calib_btn":
if btn_press == "apply_y_calib_btn":

spe_target = st.session_state["cache_dicts"]["page01_load_spe"][
"target_spe_current"]

ycalmodel = st.session_state["cache_dicts"]["y_calibration"]["ycalibration_model"]
spe_srm = st.session_state["cache_dicts"]["spectra_y_current"]["srm_ref"]

from matplotlib import pyplot as plt
# fig, ax = plt.subplots(1, 1, figsize=(15, 10))

# with st.form("derive_y_calibration"):
# col_srm, col1, col2 = st.columns([1, 1, 1])
# with col_srm:
# btn_download_srm = st.button("Download Y-calibrated spectrum")

ax = spe_srm.plot(label="SRM experimental", color="red")

spe_target.plot(ax=ax, label="Target spectrum", color="blue")

spe_ycalibrated = ycalmodel.process(spe_target)

spe_ycalibrated.plot(label="Y-calibrated", color="green", ax=ax.twinx())
fig = ax.get_figure()
st.pyplot(fig)

csv = io_write_csv(spe_ycalibrated.x, spe_ycalibrated.y)
str_csv = ''.join(csv)
st.download_button(
"Download Y-Calibrated spectrum (CSV)",
data=str_csv,
file_name="ycalibrated_spectrum.csv",
)

# if btn_download_srm:
# spe_ycalibrated.write_csv("ycalibrated_spectrum.csv")


if btn_press == "apply_x_calib_btn":

target_spe = st.session_state["cache_dicts"]["page01_load_spe"][
"target_spe_current"
Expand Down Expand Up @@ -409,6 +458,13 @@ def load_tabs_target_spectrum():

st.pyplot(fig)

csv = io_write_csv(target_calibrated.x, target_calibrated.y)
str_csv = ''.join(csv)
st.download_button(
"Download X-Calibrated spectrum (CSV)",
data=str_csv,
file_name="xcalibrated_spectrum.csv",
)

elif btn_press == "target_spe_btn":

Expand Down
Loading

0 comments on commit 0db11cf

Please sign in to comment.