Skip to content

Commit

Permalink
Merge pull request #37 from MetaSys-LISBP/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
llegregam committed Jun 7, 2023
2 parents 2dd62d7 + 0eb2a55 commit 3e648b5
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 89 deletions.
10 changes: 0 additions & 10 deletions data/model_5_test_data.tsv

This file was deleted.

4 changes: 4 additions & 0 deletions physiofit/base/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,10 @@ def output_recap(self, export_path: str):
f"detected at indice {idx}"
)
final_df = concat(self.multiple_experiments)
final_df = final_df.reset_index()
final_df[["experiments", "parameter name"]] = final_df["index"].str.split(" ", expand=True)
final_df.set_index(["experiments", "parameter name"], inplace=True)
final_df.drop("index", axis=1, inplace=True)
final_df.to_csv(f"{str(Path(export_path))}/summary.csv")


Expand Down
17 changes: 17 additions & 0 deletions physiofit/data/test-data_single_exp.tsv
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
experiments time X Glc Ace
A 1.694722222 6.20E-02 NA NA
A 2.300277778 8.62E-02 NA NA
A 2.698333333 0.110376 NA NA
A 3.436111111 0.178416 NA NA
A 3.821111111 0.208656 NA NA
A 4.201111111 0.3024 NA NA
A 4.598055556 0.378 NA NA
A 4.995 0.545832 NA NA
A 5.375277778 0.609336 NA NA
A 1.35 NA 13.62768138 0.216958848
A 1.916666667 NA 13.57921333 0.371914222
A 2.866666667 NA 12.62004575 0.707537683
A 4 NA 11.24998175 1.464367755
A 4.8 NA 9.365603478 2.13574936
A 5.183333333 NA 8.200209151 2.546579626
A 5.583333333 NA 6.551724322 2.885181161
4 changes: 2 additions & 2 deletions physiofit/models/model_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ def get_params(self):

self.parameters_to_estimate = {
"X_0" : self.vini,
"mu" : self.vini,
"growth_rate" : self.vini,
"t_lag" : self.vini
}
self.bounds = Bounds(
X_0=(1e-3, 10),
mu=(1e-3, 3),
growth_rate=(1e-3, 3),
t_lag = (0, 0.5*self.time_vector.max())
)
for metabolite in self.metabolites:
Expand Down
4 changes: 2 additions & 2 deletions physiofit/models/model_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ def get_params(self):

self.parameters_to_estimate = {
"X_0" : self.vini,
"mu" : self.vini,
"growth_rate" : self.vini,
"t_lag" : self.vini
}
self.bounds = Bounds({
"X_0": (1e-3, 10),
"mu": (1e-3, 3),
"growth_rate": (1e-3, 3),
"t_lag": (0, 0.5 * self.time_vector.max()),
})
for metabolite in self.metabolites:
Expand Down
4 changes: 2 additions & 2 deletions physiofit/models/model_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ def get_params(self):

self.parameters_to_estimate = {
"X_O" : self.vini,
"mu" : self.vini
"growth_rate" : self.vini
}
self.fixed_parameters = {"Degradation": {
met: 0 for met in self.metabolites
}
}
self.bounds = Bounds({
"X_0": (1e-3, 10),
"mu": (1e-3, 3),
"growth_rate": (1e-3, 3),
})
for metabolite in self.metabolites:
self.parameters_to_estimate.update(
Expand Down
4 changes: 2 additions & 2 deletions physiofit/models/model_4.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ def get_params(self):

self.parameters_to_estimate = {
"X_0" : self.vini,
"mu" : self.vini
"growth_rate" : self.vini
}
self.bounds = Bounds({
"X_0": (1e-3, 10),
"mu": (1e-3, 3)
"growth_rate": (1e-3, 3)
})
for metabolite in self.metabolites:
self.parameters_to_estimate.update(
Expand Down
5 changes: 4 additions & 1 deletion physiofit/models/model_5.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ def get_params(self):
break

if len(self.parameters_to_estimate) != 7:
raise ValueError("This model expects 2 metabolites in the data file (1 substrate with name starting with 'S_' and 1 product with name starting with 'P_').")
raise ValueError(
"This model expects 2 metabolites in the data file (1 substrate with name starting with 'S_' and 1 "
"product with name starting with 'P_')."
)


@staticmethod
Expand Down
105 changes: 36 additions & 69 deletions physiofit/ui/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,12 @@ def _initialize_opt_menu(self):
f"files or json for configuration files. Detected file: "
f"{self.data_file.name}")

if "experiments" in self.io.data.columns:
to_sort = ["experiments", "time"]
else:
to_sort = "time"
if "experiments" not in self.io.data.columns:
raise ValueError(
"'experiments' column missing from dataset"
)
self.io.data = self.io.data.sort_values(
to_sort, ignore_index=True
["experiments", "time"], ignore_index=True
)

try:
Expand Down Expand Up @@ -150,93 +150,60 @@ def _initialize_opt_menu(self):
iterations = self.iterations
)

if "experiments" in self.io.data.columns:
full_dataframe = self.io.data.copy()
results_path = copy(self.io.res_path)
experiments = list(self.io.data["experiments"].unique())
self.io.multiple_experiments = []
for experiment in experiments:
with st.spinner(f"Running optimization for {experiment}"):
final_table_dict = {}
self.model.data = full_dataframe[
full_dataframe["experiments"] == experiment
].drop("experiments", axis=1).copy()

self.io.res_path = results_path / experiment
if not self.io.res_path.is_dir():
self.io.res_path.mkdir()
# Initialize the fitter object
self.io.names = self.io.data.columns[1:].to_list()
kwargs = self._build_fitter_kwargs()
fitter = self.io.initialize_fitter(
self.model.data,
model=kwargs["model"],
mc=kwargs["mc"],
iterations=kwargs["iterations"],
sd=kwargs["sd"],
debug_mode=kwargs["debug_mode"]
)
# Do the work
fitter.optimize()
if self.mc:
fitter.monte_carlo_analysis()
fitter.khi2_test()
df = pd.DataFrame.from_dict(
fitter.parameter_stats,
orient="columns"
)
df.index = [
f"{experiment} {param}" for param in fitter.model.parameters_to_estimate.keys()
]
st.write(df)
self.io.multiple_experiments.append(df)

# Export results
self.io.output_report(fitter, self.io.res_path)
self.io.plot_data(fitter)
self.io.output_plots(fitter, self.io.res_path)
with st.expander(f"{experiment} plots"):
for fig in self.io.figures:
st.pyplot(fig[1])
self.io.output_pdf(fitter, self.io.res_path)
# Reset figures to free memory
self.io.figures = []
self.config_parser.export_config(self.io.res_path)
self.io.data = full_dataframe
self.io.res_path = results_path
self.io.output_recap(results_path)
else:
with st.spinner("Running flux calculation..."):
full_dataframe = self.io.data.copy()
results_path = copy(self.io.res_path)
experiments = list(self.io.data["experiments"].unique())
self.io.multiple_experiments = []
for experiment in experiments:
with st.spinner(f"Running optimization for {experiment}"):
# final_table_dict = {}
self.model.data = full_dataframe[
full_dataframe["experiments"] == experiment
].drop("experiments", axis=1).copy()

self.io.res_path = results_path / experiment
if not self.io.res_path.is_dir():
self.io.res_path.mkdir()
# Initialize the fitter object
self.io.names = self.io.data.columns[1:].to_list()
kwargs = self._build_fitter_kwargs()
fitter = self.io.initialize_fitter(
self.io.data,
self.model.data,
model=kwargs["model"],
mc=kwargs["mc"],
iterations=kwargs["iterations"],
sd=kwargs["sd"],
debug_mode=kwargs["debug_mode"]
)
# Do the work and export results
# Do the work
fitter.optimize()
if self.mc:
fitter.monte_carlo_analysis()
fitter.khi2_test()
df = pd.DataFrame.from_dict(
fitter.parameter_stats,
orient="columns"
)
df.index = [
f"{experiment} {param}" for param in fitter.model.parameters_to_estimate.keys()
]
st.write(df)
self.io.multiple_experiments.append(df)

# Export results
self.io.output_report(fitter, self.io.res_path)
self.io.plot_data(fitter)
self.io.output_plots(fitter, self.io.res_path)
with st.expander("Plots"):
with st.expander(f"{experiment} plots"):
for fig in self.io.figures:
st.pyplot(fig[1])
self.io.output_pdf(fitter, self.io.res_path)
# Reset figures to free memory
self.io.figures = []
self.config_parser.export_config(self.io.res_path)
st.success(
f"Run is finished. Check {self.io.res_path} for "
f"the results."
)
self.io.data = full_dataframe
self.io.res_path = results_path
self.io.output_recap(results_path)

def silent_sim(self):

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "physiofit"
version = "3.0.6"
version = "3.1.0"
description = "Calculate extracellular fluxes from metabolite concentrations and biomass data"
authors = ["llegregam <[email protected]>"]
license = "GNU General Public License (GPL)"
Expand Down

0 comments on commit 3e648b5

Please sign in to comment.