-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
131 lines (104 loc) · 6.09 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
"""
This file contains master functions for the different cryopeg brine scenarios.
Run the method that corresponds to the scenario you'd like to simulate.
All relevant parameters will be calculated for that scenario including:
- Maintenance energy high and low bounds
- Model output given the calculated maintenance energy
- Brine expansion factor
The main function of this file executes all scenarios and the sensitivity analysis.
Call run_all_analysis() with custom parameters to create your own scenario.
"""
import csv
import os
import pickle
from scenario import *
from plots import *
def log_results(analyses, cached_SA):
"""
Displays or saves the plots for each analysis done. Prints or save endpoints of model and maintenance energy
calculations. Saves plots and values to disk if savelog is true.
"""
csv_header = ["Analysis title", "Surrounding pOC", "Brine pOC", "Predicted brine pOC", "Surrounding dOC",
"Brine dOC", "Predicted brine dOC", "Present cell density", "Predicted end cell density",
"EEA Lower", "EEA Upper", "Real EEA", "EEA predicted timespan", "dOC/cell",
"Maintenance energy lower bounds", "Maintenance energy upper bound", "Simulation growth rate",
"Minimum growth rate", "Minimum doubling time", "Growth yield", "Brine expansion factor"]
csv_rows = []
for analysis in analyses:
# Endpoints and ME values
values = [analysis.title,
# POC
np.format_float_scientific(analysis.scenario.start_poc, precision=2),
np.format_float_scientific(analysis.scenario.end_poc, precision=2),
np.format_float_scientific(analysis.model_result.pOC[-1], precision=2),
# DOC
np.format_float_scientific(analysis.scenario.start_doc, precision=2),
np.format_float_scientific(analysis.scenario.end_doc, precision=2),
np.format_float_scientific(analysis.model_result.dOC[-1], precision=2),
# Cell density
np.format_float_scientific(analysis.scenario.observed_end_cell_density, precision=2),
np.format_float_scientific(analysis.model_result.cells[-1], precision=2),
# EEA
np.format_float_scientific(analysis.eea_estimation.eea_lower, precision=2),
np.format_float_scientific(analysis.eea_estimation.eea_upper, precision=2),
np.format_float_scientific(analysis.scenario._eea_rate, precision=2),
np.format_float_scientific(analysis.eea_estimation.predicted_timespan/365.25, precision=2),
# Cell carbon content
np.format_float_scientific(analysis.scenario.dissolved_organic_carbon_per_cell, precision=2),
# Maintenance energy
np.format_float_scientific(analysis.maintenance_energy_result.lower_bound_me, precision=4),
np.format_float_scientific(analysis.maintenance_energy_result.upper_bound_me, precision=4),
# Growth rate
np.format_float_scientific(analysis.scenario._growth_rate, precision=4),
np.format_float_scientific(analysis.maintenance_energy_result.minimum_growth_rate, precision=4),
np.format_float_scientific(analysis.maintenance_energy_result.minimum_doubling_time / 365.25, precision=2),
# Growth yield
np.format_float_scientific(analysis.growth_yield, precision=4),
# Brine expansion
np.format_float_scientific(analysis.expansion_result.ratio_dimensions, precision=2)]
csv_rows.append(values)
# Save plots and write values to CSV
# Make a plots folder is it doesn't exist
if not os.path.exists("Results/"):
os.mkdir('Results/')
if analysis.sensitivity_analysis_result:
sa_fig = plot_sensitivity(analysis)
sa_fig.savefig("Results/" + analysis.title.replace("$", "") + "_sa.tif", format="tif", dpi=300)
# Save SA values if not from cache
if not cached_SA:
with open("Results/SA_result_object", "wb") as sa_results_file:
pickle.dump(analysis.sensitivity_analysis_result, sa_results_file)
# Write the values to CSV if required
with open("Results/values.csv", "w+") as f:
write = csv.writer(f)
write.writerow(csv_header)
write.writerows(csv_rows)
# Plot for all scenarios all analyses
all_analyses_fig = plot_all_scenarios_all_analyses(analyses)
single_datatype_plot_cells = plot_one_result_type_all_analyses(analyses, "Cells")
all_analyses_fig.savefig("Results/all_model_outputs.tif", format="tif", dpi=300)
single_datatype_plot_cells.savefig("Results/all_model_outputs_just_cells.tif", format="tif", dpi=300)
# Example usage
interactive_plot = plot_one_result_type_all_analyses_interactive_plotly(all_analyses, "Cells")
interactive_plot.show()
return
if __name__ == "__main__": # Generates all figures and data points.
cached_results = False
if not cached_results:
# All sensitivity analyses should be the same.
scenarios = [cb1_scenario(), cb4_scenario(), cbiw_scenario()]
all_analyses = []
# On every scenario, try every analysis configuration.
for use_me_lower_bound in [True, False]:
for scenario in scenarios:
a = Analysis(scenario, use_minimum_growth_rate=False, use_me_lower_bound=use_me_lower_bound, use_eea_average=False)
a.run_analysis(do_sensitivity_analysis=False, cached_SA=False)
all_analyses.append(a)
# Save plots and values of all results.
with open("Results/all_analyses", "wb") as aa_results_file:
pickle.dump(all_analyses, aa_results_file)
log_results(all_analyses, False)
else: # Load results and create plots
with open("Results/all_analyses", "rb") as aa_results_file:
all_analyses = pickle.load(aa_results_file)
log_results(all_analyses, False)