Skip to content

Commit

Permalink
Allow manual input
Browse files Browse the repository at this point in the history
  • Loading branch information
RobbinBouwmeester committed Jan 24, 2025
1 parent f7c3c7b commit 9f18fca
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 9 deletions.
160 changes: 154 additions & 6 deletions webinterface/pages/base_pages/quant.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from pages.pages_variables.Quant.lfq.ion.DDA.variables import VariablesDDAQuant
from streamlit_extras.let_it_rain import rain

from proteobench.io.params import ProteoBenchParameters
from proteobench.io.parsing.parse_settings import ParseSettingsBuilder
from proteobench.modules.quant.lfq.ion.DDA.quant_lfq_ion_DDA import (
DDAQuantIonModule as IonModule,
Expand Down Expand Up @@ -50,6 +51,10 @@ def __init__(
st.session_state[self.variables_quant.submit] = False
self.stop_duplicating = False

if self.variables_quant.params_file_dict not in st.session_state.keys():
input("stop")
st.session_state[self.variables_quant.params_file_dict] = dict()

def display_submission_form(self) -> None:
"""Creates the main submission form for the Streamlit UI."""
with st.form(key="main_form"):
Expand All @@ -61,19 +66,114 @@ def display_submission_form(self) -> None:
if submit_button:
self.process_submission_form()

def generate_input_widget(self, input_format: str, content: dict) -> Any:
def generate_input_widget(self, input_format: str, content: dict, key: str = "") -> Any:
"""Generates input fields in the Streamlit UI based on the specified format and content."""
field_type = content.get("type")
if field_type == "text_area":
return self.generate_text_area_widget(input_format, content)
return self.generate_text_area_widget(input_format, content, key)
elif field_type == "text_input":
return self._generate_text_input(input_format, content)
return self._generate_text_input(input_format, content, key)
elif field_type == "number_input":
return self._generate_number_input(content)
return self._generate_number_input(content, key)
elif field_type == "selectbox":
return self._generate_selectbox(input_format, content)
return self._generate_selectbox(input_format, content, key)
elif field_type == "checkbox":
return self._generate_checkbox(input_format, content)
return self._generate_checkbox(input_format, content, key)

def _generate_text_area(self, input_format: str, content: dict, key: str = "") -> Any:
"""Generates a text area input field."""
placeholder = content.get("placeholder")
if key in st.session_state[self.variables_quant.params_file_dict].keys():
value = st.session_state[self.variables_quant.params_file_dict].get(key) # Get parsed value if available
else:
value = content.get("value", {}).get(input_format)
height = content.get("height", 200) # Default height if not specified
return st.text_area(
content["label"],
placeholder=placeholder,
key=self.variables_quant.prefix_params + key,
value=value,
height=height,
on_change=self.update_parameters_submission_form(
key, st.session_state.get(self.variables_quant.prefix_params + key, 0)
),
)

# Function to update session state dictionary

def update_parameters_submission_form(self, field, value) -> None:
try:
st.session_state[self.variables_quant.params_json_dict][field] = value
except KeyError:
st.session_state[self.variables_quant.params_json_dict] = {}
st.session_state[self.variables_quant.params_json_dict][field] = value

def _generate_text_input(self, input_format: str, content: dict, key: str = "") -> Any:
"""Generates a text input field."""
placeholder = content.get("placeholder")
if key in st.session_state[self.variables_quant.params_file_dict].keys():
value = st.session_state[self.variables_quant.params_file_dict].get(key) # Get parsed value if available
else:
value = content.get("value", {}).get(input_format)

return st.text_input(
content["label"],
placeholder=placeholder,
key=self.variables_quant.prefix_params + key,
value=value,
on_change=self.update_parameters_submission_form(
key, st.session_state.get(self.variables_quant.prefix_params + key, 0)
),
)

def _generate_number_input(self, content: dict, key: str = "") -> Any:
"""Generates a number input field."""
if key in st.session_state[self.variables_quant.params_file_dict].keys():
value = st.session_state[self.variables_quant.params_file_dict].get(key) # Get parsed value if available
else:
value = content.get("value", {}).get("min_value")
return st.number_input(
content["label"],
value=value,
key=self.variables_quant.prefix_params + key,
format=content["format"],
min_value=content["min_value"],
max_value=content["max_value"],
on_change=self.update_parameters_submission_form(
key, st.session_state.get(self.variables_quant.prefix_params + key, 0)
),
)

def _generate_selectbox(self, input_format: str, content: dict, key: str = "") -> Any:
"""Generates a selectbox input field."""
options = content.get("options", [])
if key in st.session_state[self.variables_quant.params_file_dict].keys():
value = st.session_state[self.variables_quant.params_file_dict].get(key) # Get parsed value if available
else:
value = content.get("value", {}).get(input_format)
index = options.index(value) if value in options else 0

return st.selectbox(
content["label"],
options,
key=self.variables_quant.prefix_params + key,
index=index,
on_change=self.update_parameters_submission_form(
key, st.session_state.get(self.variables_quant.prefix_params + key, 0)
),
)

def _generate_checkbox(self, input_format: str, content: dict, key: str = "") -> Any:
"""Generates a checkbox input field."""
# value = content.get("value", {}).get(input_format, False)
return st.checkbox(
content["label"],
key=self.variables_quant.prefix_params + key,
value=False,
on_change=self.update_parameters_submission_form(
key, st.session_state.get(self.variables_quant.prefix_params + key, 0)
),
)

def initialize_main_slider(self) -> None:
if self.variables_quant.slider_id_uuid not in st.session_state.keys():
Expand Down Expand Up @@ -573,6 +673,10 @@ def load_user_parameters(self) -> Any:
params = self.ionmodule.load_params_file(
self.user_input[self.variables_quant.meta_data], self.user_input["input_format"]
)
st.session_state[self.variables_quant.params_json_dict] = (
params.__dict__ if hasattr(params, "__dict__") else params
)

st.text(f"Parsed and selected parameters:\n{pformat(params.__dict__)}")
except KeyError as e:
st.error("Parsing of meta parameters file for this software is not supported yet.", icon="🚨")
Expand All @@ -584,6 +688,31 @@ def load_user_parameters(self) -> Any:
)
return params

def generate_additional_parameters_fields_submission(self) -> None:
"""Creates the additional parameters section of the form and initializes the parameter fields."""
st.markdown(self.variables_quant.texts.ShortMessages.initial_parameters)

# Load JSON config
with open(self.variables_quant.additional_params_json) as file:
config = json.load(file)

# Check if parsed values exist in session state
parsed_params = st.session_state.get(self.variables_quant.params_json_dict, {})

st_col1, st_col2, st_col3 = st.columns(3)
input_param_len = int(len(config.items()) / 3)

for idx, (key, value) in enumerate(config.items()):
if idx < input_param_len:
with st_col1:
self.user_input[key] = self.generate_input_widget(self.user_input["input_format"], value, key)
elif idx < input_param_len * 2:
with st_col2:
self.user_input[key] = self.generate_input_widget(self.user_input["input_format"], value, key)
else:
with st_col3:
self.user_input[key] = self.generate_input_widget(self.user_input["input_format"], value, key)

def generate_sample_name(self) -> str:
"""Generates a unique sample name based on the input format, software version, and the current timestamp."""
time_stamp = datetime.now().strftime("%Y%m%d_%H%M%S")
Expand All @@ -596,16 +725,35 @@ def generate_sample_name(self) -> str:

return sample_name

def get_form_values(self) -> Dict[str, Any]:
"""Retrieves all user inputs from Streamlit session state and returns them as a dictionary."""
form_values = {}

# Load JSON config (same file used to create fields)
with open(self.variables_quant.additional_params_json, "r") as file:
config = json.load(file)

# Extract values from session state
for key in config.keys():
form_key = self.variables_quant.prefix_params + key # Ensure correct session key
form_values[key] = st.session_state.get(form_key, None) # Retrieve value, default to None if missing

return form_values

def display_public_submission_ui(self) -> None:
if self.variables_quant.first_new_plot:
self.generate_submission_ui_elements()

if self.user_input[self.variables_quant.meta_data]:
params = self.load_user_parameters()
st.session_state[self.variables_quant.params_file_dict] = params.__dict__
self.generate_additional_parameters_fields_submission()
else:
params = None

if st.session_state[self.variables_quant.check_submission] and params != None:
get_form_values = self.get_form_values()
params = ProteoBenchParameters(**get_form_values)
pr_url = self.submit_to_repository(params)
if self.submission_ready == False:
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class VariablesDDAQuant:
beta_warning: bool = True
github_link_pr: str = "github.com/Proteobot/Results_quant_ion_DDA.git"

additional_params_json: str = "../webinterface/configuration/dda_quant.json"
additional_params_json: str = "../proteobench/io/params/json/Quant/lfq/ion/DDA/fields.json"

description_module_md: str = "pages/markdown_files/Quant/lfq/ion/DDA/introduction_DDA_quan_ions.md"
description_files_md: str = "pages/markdown_files/Quant/lfq/ion/DDA/file_description.md"
Expand All @@ -63,3 +63,6 @@ class VariablesDDAQuant:
doc_url: str = "https://proteobench.readthedocs.io/en/latest/available-modules/2-quant-lfq-ion-dda/"

title: str = "DDA Ion quantification"
prefix_params: str = "lfq_ion_dda_quant_"
params_json_dict: str = "params_json_dict_lfq_ion_dda_quant"
params_file_dict: str = "params_file_dict_lfq_ion_dda_quant"
5 changes: 3 additions & 2 deletions webinterface/pages/texts/generic_texts.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ class ShortMessages:
"""

initial_parameters = """
Additionally, you can fill out some information on the paramters that were
used for this benchmark run bellow. These will be printed when hovering on your point.
Additionally, you can fill out parameters for your search manually. Please,
only fill out the parameters that are not already included in the input file.
Only make changes if you are sure about the parameters you are changing.
"""

run_instructions = """
Expand Down

0 comments on commit 9f18fca

Please sign in to comment.