Skip to content

Commit

Permalink
Added some caching
Browse files Browse the repository at this point in the history
  • Loading branch information
LorenaH84 committed Jun 14, 2024
1 parent 81aa567 commit d337428
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 43 deletions.
3 changes: 2 additions & 1 deletion streamlit/app_scripts/app_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,13 @@ def get_all_parameter_sets_meta_data_files_path():
## CONNECTIONS ##
#################


@st.cache_resource
def get_sqlite_con_and_cur():
database = get_path_to_database()
con = sqlite3.connect(database, check_same_thread=False)
return con, con.cursor()

@st.cache_resource
def get_sqlite_con_and_cur_thread_safe():
database = get_path_to_database()
con = sqlite3.connect(database, check_same_thread=False)
Expand Down
2 changes: 1 addition & 1 deletion streamlit/app_scripts/app_calculations.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
##############################
from app_scripts import app_access

@st.cache_data

def validate_mass_fraction(mf_sum,category_display_name,_tab):
mf_summing = 0
for id, value in mf_sum.items():
Expand Down
80 changes: 61 additions & 19 deletions streamlit/app_scripts/app_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import ast
import pandas as pd
import random
import re
import math


sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
Expand Down Expand Up @@ -331,21 +333,21 @@ def __init__(self):
# "bkb": "https://w3id.org/emmo/domain/battery_knowledge_base#",
# "qudt": "http://qudt.org/vocab/unit/",
}

def setup_linked_data_dict(self, model_id, model_name):
@st.cache_data
def setup_linked_data_dict(_self, model_id, model_name):

model_label = "{} model".format(model_name)
id = ""
model_type = "battery:{}Model".format(model_name)

dict = {
"@context": self.context,
"@context": _self.context,

self.universe_label:{
self.hasModel:{
_self.universe_label:{
_self.hasModel:{
"label": model_label,
"@type": model_type,
self.hasQuantitativeProperty: db_helper.get_model_parameters_as_dict(model_name)
_self.hasQuantitativeProperty: db_helper.get_model_parameters_as_dict(model_name)
}
}
}
Expand All @@ -367,19 +369,20 @@ def setup_linked_data_dict(self, model_id, model_name):

return dict

def fill_sub_dict(self,dict,relation_dict_1, parameters,existence,relation_dict_2 = None,relation_par=None):
@st.cache_data
def fill_sub_dict(_self,dict,relation_dict_1, parameters,existence,relation_dict_2 = None,relation_par=None):
parameters = parameters.copy()
if self.universe_label in dict:
if _self.universe_label in dict:
if relation_par:
if existence == "new":
dict[self.universe_label][relation_dict_1] = parameters[relation_par]
dict[_self.universe_label][relation_dict_1] = parameters[relation_par]
elif existence == "existing":
dict[self.universe_label][relation_dict_1] += parameters[relation_par]
dict[_self.universe_label][relation_dict_1] += parameters[relation_par]
else:
if existence == "new":
dict[self.universe_label][relation_dict_1] = parameters
dict[_self.universe_label][relation_dict_1] = parameters
elif existence == "existing":
dict[self.universe_label][relation_dict_1] += parameters
dict[_self.universe_label][relation_dict_1] += parameters
else:
if relation_par:
if existence == "new":
Expand Down Expand Up @@ -425,11 +428,12 @@ def setup_sub_dict(_self,dict=None,display_name=None, context_type=None, type=No

return dict

def fill_linked_data_dict(self, user_input, content):
user_input[self.universe_label][self.hasCell] = content
@st.cache_data
def fill_linked_data_dict(_self, user_input, content):
user_input[_self.universe_label][_self.hasCell] = content

return user_input

def setup_parameter_struct(self, parameter,component_parameters=None, value = None):
if component_parameters is None:
component_parameters = []
Expand Down Expand Up @@ -581,9 +585,10 @@ def fill_component_dict(_self,component_parameters,existence, dict = None, relat

return dict

def change_numerical_value(self,dict, index, value):
@st.cache_data
def change_numerical_value(_self,dict, index, value):
try:
dict[index]["value"][self.hasNumericalData]=value
dict[index]["value"][_self.hasNumericalData]=value
except:
dict[index]["value"]=value

Expand Down Expand Up @@ -1045,7 +1050,7 @@ def calc_indicators(_self,user_input):
def fill_category(self, category_id, category_display_name,category_name, emmo_relation, default_template_id, tab, category_parameters,mass_loadings,uploaded_input = None, selected_am_value_id=None):

density_mix = None

# get components associated with material parameter sets
if category_name == "boundary_conditions":
material_components = None
Expand Down Expand Up @@ -1124,6 +1129,7 @@ def fill_category(self, category_id, category_display_name,category_name, emmo_r
component_parameters = {}
parameter, user_input, component_parameters_, emmo_relation, mass_fraction_id_dict = self.fill_mass_fraction_column(mass_fraction_col,category_id,material_comp_default_template_id,material_component_id,component_parameters_,mass_fraction_id_dict)


if parameter:
component_parameters_ = self.LD.fill_component_dict(component_parameters_, "new")
component_parameters = self.LD.setup_sub_dict(dict=component_parameters,display_name=material_comp_display_name,context_type=material_comp_context_type)
Expand All @@ -1136,6 +1142,9 @@ def fill_category(self, category_id, category_display_name,category_name, emmo_r
mass_fraction_id_dict = None
density = None

if mass_fraction_id_dict:
self.validate_mass_fraction(mass_fraction_id_dict, category_display_name,tab)

non_material_component = db_helper.get_non_material_components_from_category_id(category_id)

non_material_component_id, non_material_component_name, _,_,_,_,non_material_comp_display_name,_,_,_,non_material_comp_default_template_id,non_material_comp_context_type,non_material_comp_context_type_iri,_ = non_material_component
Expand Down Expand Up @@ -1647,7 +1656,6 @@ def fill_non_material_components(self,density,category_display_name,category_par
ac += 1

if mass_fraction_id_dict:
self.validate_mass_fraction(mass_fraction_id_dict, category_display_name,tab)

density_mix = self.calc_density_mix(mass_fraction_id_dict, density)
density_eff = self.calc_density_eff(density_mix, porosity)
Expand Down Expand Up @@ -5705,6 +5713,40 @@ def set_material_description(self):
else:
st.latex(sp.latex(sp.sympify(string_py)))

# updated_string = string_py.replace("c/cmax", "x")
# def parsed_function(x):
# # Define a dictionary to store allowed functions and their mappings
# allowed_functions = {
# "exp": math.exp,
# "tanh": math.tanh,
# # Add more functions as needed
# }

# # Regular expression pattern to find function calls
# pattern = r"([a-zA-Z_][a-zA-Z0-9_]*)\(" # Matches function names

# # Replace function calls with their respective mappings
# safe_equation_string = re.sub(pattern, lambda match: f"allowed_functions['{match.group(1)}'](", updated_string)

# # Evaluate the modified equation string with x
# return eval(safe_equation_string, {"allowed_functions": allowed_functions, "x": x})

# x = np.linspace(0,1,100)
# y = np.zeros(len(x))
# for i in range(len(x)):
# y[i] = parsed_function(x[i])

# fig = go.Figure(data=go.Scatter(x=x, y=y, mode='lines', name='Sample Line'))

# # Add titles and labels
# fig.update_layout(
# title='Simple Line Graph',
# xaxis_title='X Axis',
# yaxis_title='Y Axis'
# )

# st.plotly_chart(fig, use_container_width=True)

else:
st.markdown('''```<Julia>
{}'''.format(value_dict["functionname"]))
Expand Down
6 changes: 3 additions & 3 deletions streamlit/database/db_BaseHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ def thread_safe_db_access(_self,query, params = None, fetch = None):
raise

finally:
if cur:
cur.close()
# if cur:
# cur.close()
if con:
con.commit()
con.close()




Expand Down
8 changes: 4 additions & 4 deletions streamlit/input_files/battmo_formatted_input.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"Coating": {
"thickness": 8.499999999999999e-05,
"N": 20,
"effectiveDensity": 1770.4,
"effectiveDensity": 1726.14,
"bruggemanCoefficient": 1.5,
"ActiveMaterial": {
"massFraction": 0.9,
Expand Down Expand Up @@ -36,9 +36,9 @@
},
"diffusionModelType": "full",
"SolidDiffusion": {
"activationEnergyOfDiffusion": 80000.0,
"activationEnergyOfDiffusion": 70000.0,
"referenceDiffusionCoefficient": 3.9e-14,
"particleRadius": 1e-06,
"particleRadius": 5e-07,
"N": 10
}
},
Expand Down Expand Up @@ -170,7 +170,7 @@
"Control": {
"controlPolicy": "CCCV",
"initialControl": "charging",
"numberOfCycles": 1,
"numberOfCycles": 3,
"CRate": 1.0,
"DRate": 1.0,
"lowerCutoffVoltage": 2.4,
Expand Down
16 changes: 8 additions & 8 deletions streamlit/input_files/linked_data_input.json
Original file line number Diff line number Diff line change
Expand Up @@ -1929,7 +1929,7 @@
"@type": "emmo:Energy",
"value": {
"@type": "emmo:Numerical",
"hasNumericalData": 80000.0
"hasNumericalData": 70000.0
},
"unit": {
"label": "JoulePerMole",
Expand All @@ -1955,7 +1955,7 @@
"@type": "emmo:Radius",
"value": {
"@type": "emmo:Numerical",
"hasNumericalData": 1e-06
"hasNumericalData": 5e-07
},
"unit": {
"label": "Metre",
Expand Down Expand Up @@ -2337,7 +2337,7 @@
"@type": "emmo:Porosity",
"value": {
"@type": "emmo:Numerical",
"hasNumericalData": 0.2
"hasNumericalData": 0.22000000000000003
},
"unit": {
"label": "UnitOne",
Expand All @@ -2350,7 +2350,7 @@
"@type": "emmo:MassLoading",
"value": {
"@type": "emmo:Numerical",
"hasNumericalData": 15.0484
"hasNumericalData": 14.67219
},
"unit": {
"label": "MiliGramPerCubicCentiMeter",
Expand Down Expand Up @@ -2402,7 +2402,7 @@
"@type": "emmo:Capacity",
"value": {
"@type": "emmo:Numerical",
"hasNumericalData": 1120.5836528712066
"hasNumericalData": 1092.5690615494264
},
"unit": {
"label": "MiliAmpereHour",
Expand Down Expand Up @@ -6233,7 +6233,7 @@
"@type": "emmo:N/PRatio",
"value": {
"@type": "emmo:Numerical",
"hasNumericalData": 1.07
"hasNumericalData": 1.04
},
"unit": {
"label": "UnitOne",
Expand All @@ -6246,7 +6246,7 @@
"@type": "emmo:CellMass",
"value": {
"@type": "emmo:Numerical",
"hasNumericalData": 31.7796972032
"hasNumericalData": 31.6742860172
},
"unit": {
"label": "Gram",
Expand Down Expand Up @@ -6357,7 +6357,7 @@
"@type": "emmo:NumberOfEntities",
"value": {
"@type": "emmo:Numerical",
"hasNumericalData": 1
"hasNumericalData": 3
},
"unit": {
"label": "UnitOne",
Expand Down
Binary file modified streamlit/output_files/battmo_results.hdf5
Binary file not shown.
4 changes: 2 additions & 2 deletions streamlit/output_files/calculated_values.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"calculatedParameters": {
"effective_density": {
"negative_electrode": 1770.4,
"negative_electrode": 1726.14,
"positive_electrode": 3151.12
},
"mass_loadings": {
"negative_electrode": 15.0484,
"negative_electrode": 14.67219,
"positive_electrode": 23.948511999999994
}
}
Expand Down
10 changes: 5 additions & 5 deletions streamlit/output_files/indicator_values.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"Cell": {
"cellMass": {
"value": 31.7796972032,
"value": 31.6742860172,
"unit": "g"
},
"roundTripEfficiency": {
Expand All @@ -12,25 +12,25 @@
"unit": "mAh"
},
"NPRatio": {
"value": 1.07,
"value": 1.04,
"unit": "1"
}
},
"NegativeElectrode": {
"massLoading": {
"value": 15.0484,
"value": 14.67219,
"unit": "mg\u00b7cm\u207b\u00b2"
},
"thickness": {
"value": 85.0,
"unit": "\u03bcm"
},
"porosity": {
"value": 0.2,
"value": 0.22000000000000003,
"unit": "1"
},
"specificCapacity": {
"value": 1120.5836528712066,
"value": 1092.5690615494264,
"unit": "mAh"
},
"ActiveMaterial": {
Expand Down

0 comments on commit d337428

Please sign in to comment.