-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
112 lines (87 loc) · 4 KB
/
utils.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
"""
Some helper functions. Includes classes to encapsulate analysis results.
"""
from decimal import Decimal
import pandas
import numpy as np
def convert_micromolar_carbon_to_fgC_per_ml(micromolar):
"""Converts a micromolar of carbon value to femtograms of carbon per ml."""
return (micromolar * 12.011 * 10**-6 * 10**15)/1000
def convert_fgC_per_ml_to_micromolar_carbon(fgC_per_ml):
"""Converts a femtograms of carbon per ml value to micromolar of carbon."""
return (fgC_per_ml * 1000)/(12.011 * 10**-6 * 10**15)
class MaintenanceResult:
"""
Class that encapsulates the result of a maintenance energy estimation.
"""
lower_bound_me: float = None
upper_bound_me: float = None
minimum_growth_rate: float = None
minimum_doubling_time: float = None
class SensitivityResult:
"""
Class that encapsulates the results of a sensitivity analysis.
"""
total_sobol_indices: [float] = None
first_order_sobol_indices: [float] = None
total_conf_int: [[float]] = None
first_order_conf_int: [[float]] = None
variables: [str] = ["POC", "DOC", "DIC", "Cells", "Mean"]
def get_dataframe(self, scenario):
"""
Returns model results as a dataframe. Removes values that are exactly 0 (SA wasn't run).
"""
assert len(self.variables) == len(self.total_sobol_indices) + 1
p_names = scenario._parameter_names
ST = self.total_sobol_indices
S1 = self.first_order_sobol_indices
st_err = self.total_conf_int
s1_err = self.first_order_conf_int
df = []
for row in range(len(p_names[0:-1])):
if all(ST[i, row] == 0 for i in range(4)):
continue
for i, output in enumerate(self.variables):
if output == "Mean":
df.append([p_names[row], output, np.mean(ST[:, row]), np.mean(S1[:, row]), np.mean(st_err[:, row]), np.mean(s1_err[:, row])])
else:
df.append([p_names[row], output, ST[i, row], S1[i, row], st_err[i, row], s1_err[i, row]])
df = pandas.DataFrame(data=df, columns=["Parameter", "Output", "Total-effect", "First-order",
"Total Error", "First Error"])
return df.astype({"Parameter": str, "Output": str, "Total-effect": float, "First-order": float,
"Total Error": float, "First Error": float})
class ModelResult:
"""
Class that encapsulates the results of a model run.
"""
pOC: [float] = None
dOC: [float] = None
DIC: [float] = None
cells: [float] = None
t: [float] = None
def get_dataframe(self, scenario, variable_title):
"""
Returns model results as a dataframe. Removes duplicate values (e.g. discontinuities).
"""
data = [self.t/365.25, self.pOC, self.dOC, self.DIC, self.cells, [scenario]*len(self.t), [variable_title]*len(self.t)]
df = pandas.DataFrame(data=np.column_stack(data),
columns=["Years from start", "POC", "DOC", "DIC", "Cells", "Scenario", "Analysis type"])
df.drop_duplicates(subset=["Years from start", "Scenario", "Analysis type"], keep="last", inplace=True)
return df.astype({"Years from start": float, "POC": float, "DOC": float, "DIC": float, "Cells": float,
"Scenario": str, "Analysis type": str})
class ExpansionResult:
"""
Class that encapsulates the results of a brine expansion calculation.
"""
ratio_volume: Decimal = None # Ratio of expansion at 3D
ratio_area: Decimal = None # Ratio of expansion at 2D
ratio_dimensions: Decimal = None # Ratio of expansion at 1D
expansion_a: Decimal = None # Expansion along the A-axis of the prolate spheroid.
expansion_b: Decimal = None # Expansion along the B-axis of the prolate spheroid.
class EEAResult:
"""
Class that encapsulates a EEA rate calculation result.
"""
eea_upper: float = None
eea_lower: float = None
predicted_timespan: float = None