Skip to content

Commit

Permalink
Added version checking to json ld input, and some debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
LorenaH84 committed Dec 16, 2024
1 parent 2e782af commit c0d3e69
Show file tree
Hide file tree
Showing 21 changed files with 271 additions and 250 deletions.
47 changes: 2 additions & 45 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ services:
ports:
- "8081:8081"
- "8080:8082"
networks:
- app-network
volumes:
- ./api/scripts:/api/scripts
- ./api/api.jl:/api/api.jl
Expand All @@ -37,51 +39,6 @@ services:
command: streamlit run app.py --global.disableWidgetStateDuplicationWarning true --server.port=8501


nginx:
build: ./nginx
container_name: nginx
restart: always
ports:
- "80:80"
networks:
- app-network
volumes:
- /etc/letsencrypt:/etc/letsencrypt
- /usr/share/nginx/html/.well-known:/usr/share/nginx/html/.well-known
command: nginx -g 'daemon off;'

# umami:
# image: docker.umami.is/umami-software/umami:postgresql-latest
# container_name: umami
# restart: always
# environment:
# DATABASE_URL: postgres://umami:password@umami-db/umami
# DATABASE_TYPE: postgres
# HASH_SALT: ${HASH_SALT} # Replace this with a secure, random hash salt
# ports:
# - "3000:3000"
# networks:
# - app-network
# depends_on:
# - umami-db

# umami-db:
# image: postgres:13
# container_name: umami-db
# restart: always
# environment:
# POSTGRES_USER: umami
# POSTGRES_PASSWORD: ${POSTGRES_PASSWORD} # Replace with a strong password
# POSTGRES_DB: umami
# networks:
# - app-network
# volumes:
# - umami_db_data:/var/lib/postgresql/data

# Define volumes at the root level, outside of services
# volumes:
# umami_db_data:

networks:
app-network:
driver: bridge
151 changes: 106 additions & 45 deletions gui/app_scripts/app_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -807,20 +807,36 @@ def set_file_input(self):
uploaded_file_dict = json.loads(uploaded_file)
# uploaded_file_str = str(uploaded_file_dict)

with open(app_access.get_path_to_uploaded_input(), "w") as outfile:
json.dump(uploaded_file_dict, outfile, indent=3)
if "@id" in uploaded_file_dict["@graph"]:
id = uploaded_file_dict["@graph"]["@id"]

uploaded_input_gui_dict = match_json_LD.get_gui_dict_from_linked_data(
uploaded_file_dict
)
if id != "BattMoApp-v0.2.0":
st.error(
"Your JSON LD input file doesn't contain a valid id. Setup your parameters they you want and download your JSON LD version in the sidebar."
)
self.set_sessions_state_clear_upload()
else:

with open(app_access.get_path_to_uploaded_input(), "w") as outfile:
json.dump(uploaded_file_dict, outfile, indent=3)

uploaded_input_gui_dict = match_json_LD.GuiDict(uploaded_input_gui_dict)
uploaded_input_gui_dict = match_json_LD.get_gui_dict_from_linked_data(
uploaded_file_dict
)

st.session_state.json_uploaded_input = uploaded_input_gui_dict
uploaded_input_gui_dict = match_json_LD.GuiDict(uploaded_input_gui_dict)

st.session_state.json_uploaded_input = uploaded_input_gui_dict

st.success(
"Your file is succesfully uploaded. Click on the 'CLEAR' button if you want to reset the parameters to the default values again."
)
else:
st.error(
"Your JSON LD input file doesn't contain a valid id. Setup your parameters the you want and download your JSON LD file in the sidebar."
)
self.set_sessions_state_clear_upload()

st.success(
"Your file is succesfully uploaded. Click on the 'CLEAR' button if you want to reset the parameters to the default values again."
)
else:
uploaded_input_gui_dict = None

Expand Down Expand Up @@ -863,33 +879,64 @@ def set_increment(_self, value):
Calculates increment from min and max values.
Increment is used to define the number input widget.
"""

if type(value) == float:
if isinstance(value, float):
if value == 0:
increment = 1e-17
# Dynamic minimum increment for zero
increment = 1e-14
return increment

# Calculate the order of magnitude
# Calculate order of magnitude
order_of_magnitude = math.floor(math.log10(abs(value)))

# Determine a base increment which is a power of 10
# Base increment as power of 10
base_increment = 10**order_of_magnitude

# Adjust the increment to be more user-friendly
# Adjust increment for user-friendliness
if value < 1:
increment = base_increment / 10
else:
increment = base_increment / 2

# Further refinement for very small values
if abs(increment) < 1e-10:
increment = 1e-10
increment = max(increment, 1e-14) # Set minimum threshold to 1e-14

return float(increment)
else:
increment = 1

return int(increment)
# Default increment for integers or non-float values
return 1

# def set_increment(_self, value):
# """
# Calculates increment from min and max values.
# Increment is used to define the number input widget.
# """

# if type(value) == float:
# if value == 0:
# increment = 1e-17
# return increment

# # Calculate the order of magnitude
# order_of_magnitude = math.floor(math.log10(abs(value)))

# # Determine a base increment which is a power of 10
# base_increment = 10**order_of_magnitude

# # Adjust the increment to be more user-friendly
# if value < 1:
# increment = base_increment / 10
# else:
# increment = base_increment / 2

# # Further refinement for very small values
# if abs(increment) < 1e-10:
# increment = 1e-10

# return float(increment)
# else:
# increment = 1

# return int(increment)

def set_tabs(self):
cell_parameters = {}
Expand Down Expand Up @@ -1917,9 +1964,14 @@ def ud_set_select(
if key_arg:
selected_parameter_set = st.session_state[key_select]
parameter_set_id = material.options.get(selected_parameter_set).parameter_set_id
st.session_state[key_user_input] = str(
parameter.options.get(parameter_set_id).value["function"]
)
if "function" in parameter.options.get(parameter_set_id).value:
st.session_state[key_user_input] = str(
parameter.options.get(parameter_set_id).value["function"]
)
else:
st.session_state[key_user_input] = str(
parameter.options.get(parameter_set_id).value["functionname"]
)
st.session_state[key_arg] = self.create_string_from_list(
parameter.options.get(parameter_set_id).value["argument_list"]
)
Expand Down Expand Up @@ -1970,6 +2022,7 @@ def fill_user_defined_expander(
parameter = material_formatted_parameters.get(parameter_id)

keys_to_include = list(parameter.options.keys())

sub_formatted_material = {
key: formatted_material.options[key]
for key in keys_to_include
Expand Down Expand Up @@ -3290,6 +3343,9 @@ def fill_material_components(

if excluded_template_parameter_ids:

parameter_set_ids = db_helper.get_all_material_parameter_sets_by_component_id(
material_component_id
)
expander_missing_parameters = tab.expander(
label="Define {} missing material parameters".format(material)
)
Expand Down Expand Up @@ -3321,12 +3377,13 @@ def fill_material_components(
) = template_parameter

raw_parameters = db_helper.get_parameter_from_template_parameter_id(
template_par_id
template_par_id, parameter_set_ids
)

material_display_names = []
material_values = []
sub_formatted_material = {}

for raw_parameter in raw_parameters:
id, name, material_parameter_set_id, _, value = raw_parameter

Expand All @@ -3348,18 +3405,18 @@ def fill_material_components(
material_name[0], self.model_name
)

if (
material_display_name
and material_display_name[0][0] != "User defined"
):
material_display_names.append(material_display_name[0][0])
material_values.append(value)
elif (
not material_display_name
and "User defined" not in material_display_names
):
material_display_names.append("User defined")
material_values.append(value)
# if (
# material_display_name
# and material_display_name[0][0] != "User defined"
# ):
material_display_names.append(material_display_name[0][0])
material_values.append(value)
# elif (
# not material_display_name
# and "User defined" not in material_display_names
# ):
# material_display_names.append("User defined")
# material_values.append(value)

st.write(
"[{}]({})".format(par_display_name, context_type_iri)
Expand Down Expand Up @@ -3436,6 +3493,9 @@ def fill_material_components(
if st.session_state[key_user_input] is None:
st.session_state[key_user_input] = material_value

step_value = self.set_increment(st.session_state[key_user_input])
format_value = self.set_format(st.session_state[key_user_input])

user_input = value_col.number_input(
label=par_display_name,
value=st.session_state[key_user_input],
Expand All @@ -3449,7 +3509,8 @@ def fill_material_components(
user_interaction,
key_select,
),
step=self.set_increment(st.session_state[key_user_input]),
step=step_value,
format=format_value,
label_visibility="collapsed",
)

Expand Down Expand Up @@ -4592,7 +4653,7 @@ def divergence_check_logging(self, number_of_states, log_messages):
if (
self.response == False
and st.session_state.success == False
and st.session_state.battmo_api_response != None
# and st.session_state.battmo_api_response != None
):
self.save_run.error(
"The data has not been retrieved succesfully, most probably due to an unsuccesful simulation"
Expand Down Expand Up @@ -4628,7 +4689,7 @@ def divergence_check_logging(self, number_of_states, log_messages):

self.success = True

if len(log_messages) > 1:
if log_messages and len(log_messages) > 1:
c = self.save_run.container()
c.warning(
"Simulation results retrieved, but Some errors/warnings were produced. See the logging."
Expand Down Expand Up @@ -4680,8 +4741,8 @@ def divergence_check_logging(self, number_of_states, log_messages):
# except:
# pass

elif st.session_state.battmo_api_response == None:
pass
# elif st.session_state.battmo_api_response == None:
# pass

st.session_state.response == None

Expand Down Expand Up @@ -4781,7 +4842,7 @@ def setup_gui_schema(self, headline, description, creator):

schema["@context"] = parameters["@context"]
schema["@graph"] = {}
schema["@graph"]["@id"] = parameters["@graph"]["@id"]
schema["@graph"]["@id"] = "BattMoApp-v0.2.0"
schema["@graph"]["@type"] = parameters["@graph"]["@type"]

if isBasedOn:
Expand Down Expand Up @@ -5159,11 +5220,11 @@ def render_buttons(self):
# st.markdown("###### " + "Schema headline")
# headline = st.text_input(label="headline", label_visibility="collapsed")
headline = None
st.markdown("###### " + "Schema description")
st.markdown("###### " + "Description")
description = st.text_input(label="description", label_visibility="collapsed")

cola, colb = st.columns(2)
st.markdown("###### " + "Schema creators")
st.markdown("###### " + "Creators")
col1, col2 = st.columns(2)
col1.markdown("Number of creators")
number = col2.number_input(
Expand Down
Binary file modified gui/database/BattMo_gui.db
Binary file not shown.
4 changes: 4 additions & 0 deletions gui/database/db_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ def get_id_from_name(self, name):
res = self.select_one(values="id", where="name='%s'" % name)
return res[0] if res else None

def get_all_material_by_component_id(self, component_id):
res = self.select(values="id", where="component_id=%d" % component_id)
return [res_[0] for res_ in res]


#####################################
# TEMPLATE
Expand Down
21 changes: 19 additions & 2 deletions gui/database/db_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,9 +398,12 @@ def get_parameter_id_from_template_parameters_and_parameter_set(


@st.cache_data
def get_parameter_from_template_parameter_id(template_parameter_id):
def get_parameter_from_template_parameter_id(template_parameter_id, parameter_set_ids):
ids_str = ",".join(map(str, parameter_set_ids))
res = sql_parameter().select(
values="*", where="template_parameter_id = '%d'" % int(template_parameter_id)
values="*",
where="template_parameter_id = '%d' AND parameter_set_id IN (%s)"
% (int(template_parameter_id), ids_str),
)
return res

Expand Down Expand Up @@ -698,6 +701,20 @@ def get_parameters_by_template_parameter_ids(template_parameter_ids, model_name)
return res


@st.cache_data
def get_parameters_by_template_parameter_ids_and_parameter_set_ids(
template_parameter_ids, parameter_set_ids, model_name
):
ids_str = ",".join(map(str, template_parameter_ids))
ids_sets_str = ",".join(map(str, parameter_set_ids))
res = sql_template_parameter().select(
values="*",
where="id IN (%s) AND parameter_set_id IN (%s) AND model_name = '%s'"
% (ids_str, ids_sets_str, model_name),
)
return res


@st.cache_data
def get_parameter_by_template_parameter_id(template_parameter_id, model_name):
res = sql_template_parameter().select(
Expand Down
Loading

0 comments on commit c0d3e69

Please sign in to comment.