-
Notifications
You must be signed in to change notification settings - Fork 0
/
bin_intervention-experiment.py
183 lines (155 loc) · 5.94 KB
/
bin_intervention-experiment.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
"""
Bin a set of forward-dream malaria control intervention
experiments
JHendry, 2021/02/04
This is necessary to compute average trajectories of
statistics across simulations, as forward-dream operates
in continuous time.
Usage:
python bin_intervention-experiment.py \
-e <expt_name> \
-p <params/param_file.ini>
The script will output to `analysis/<expt_name>`.
"""
import os
import sys
import getopt
import configparser
import json
from datetime import datetime
from lib.temporal_binning import *
print("=" * 80)
print("Bin and average results from forward-dream simulation")
print("-" * 80)
print("Command: %s" % " ".join(sys.argv))
print("Run on host: %s" % os.uname().nodename)
print("Operating system: %s" % os.uname().sysname)
print("Machine: %s" % os.uname().machine)
print("Started at: %s" % datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
print("=" * 80)
# PARSE COMMAND-LINE
print("Parsing Command Line Inputs...")
try:
opts, args = getopt.getopt(sys.argv[1:], ":e:p:")
except getopt.GetoptError:
print("Option Error. Please conform to:")
print("-e <str> -p <str>")
for opt, value in opts:
if opt == "-e":
expt_name = value
expt_path = os.path.join("results", expt_name)
output_path = os.path.join("analysis", expt_name)
print("Preparing to bin data for", expt_name)
print(" Output path:", output_path)
if not os.path.isdir(output_path):
os.mkdir(output_path)
elif opt == "-p":
param_path = value
print(" Parameter path:", param_path)
else:
print("Parameter %s not recognized." % opt)
sys.exit(2)
print("Done.")
print("")
# SEARCH FOR SIMULATIONS
print("Searching for all experiment simulations...")
dirs = [os.path.join(expt_path, d) for d in os.listdir(expt_path)]
dirs = [d for d in dirs if os.path.isdir(d)]
complete_dirs = []
for d in dirs:
contents = os.listdir(d)
try:
run_diagnostics = json.load(open(os.path.join(d, "run_diagnostics.json"), "r"))
if not run_diagnostics["extinct"]:
complete_dirs.append(d)
else:
print(" %s experienced extinction." % d)
except FileNotFoundError:
print(" %s is not completed." % d)
print(" Found %d total simulations." % len(dirs))
print(" No. incomplete/extinct: %d" % (len(dirs) - len(complete_dirs)))
print("Done.")
print("")
# Load Epochs
print("Loading Epoch data frame and parameter file...")
epoch_df = pd.read_csv(os.path.join(complete_dirs[0], "epoch_df.csv")) # assumes epochs are same for all replicates
epoch_df.to_csv(os.path.join(output_path, "epoch_df.csv"), index=False) # drop this here for future use
print(epoch_df)
epoch_df.index = epoch_df.name
# Load Parameters
config = configparser.ConfigParser()
config.read(param_path)
print("Done.")
print("")
# BIN DATA
# Preferences
print("Setting binning preferences...")
obs_per_bin = 5 # the number of observations to include in each bin, on average
print(" Observations per bin: %d" % obs_per_bin)
print("Done.")
print("")
# GENETIC DIVERSITY DATA
print("Binning genetic diversity data...")
# Create boundaries for bins
print(" Creating bin boundaries...")
og_bounds = create_bin_boundaries(config=config,
epoch_df=epoch_df,
data_type="div",
obs_per_bin=obs_per_bin)
# Load all the diversity simulations
print(" Loading data frames...")
ogs = load_simulations(dirs=complete_dirs,
file_name="og.csv")
# Bin them in time
print(" Binning data frames...")
keep_cols = [c for c in ogs[0].columns if c != "t0"]
ogs_binned = bin_simulations(simulations=ogs,
bins=og_bounds,
keep_cols=keep_cols)
# Compute means, standard devation, standard error across simulations
print(" Computing summary statistics...")
ogs_array, ogs_mean, ogs_std, ogs_se = average_simulations(simulations_binned=ogs_binned,
keep_cols=keep_cols,
bin_midpoints=(og_bounds[1:] + og_bounds[:-1])/2)
# Write outputs
print(" Writing...")
np.save(output_path + "/ogs_array.npy", ogs_array)
ogs_mean.to_csv(output_path + "/ogs_mean.csv", index=False)
ogs_std.to_csv(output_path + "/ogs_stds.csv", index=False)
ogs_se.to_csv(output_path + "/ogs_se.csv", index=False)
print("Done.")
print("")
# PREVALENCE DATA
print("Binning prevalence data...")
# Create boundaries for bins
print(" Creating bin boundaries...")
op_bounds = create_bin_boundaries(config=config,
epoch_df=epoch_df,
data_type="prev",
obs_per_bin=obs_per_bin)
# Load all the diversity simulations
print(" Loading data frames...")
ops = load_simulations(dirs=complete_dirs,
file_name="op.csv")
# Bin them in time
print(" Binning data frames...")
keep_cols = [c for c in ops[0].columns if c != "t0"]
ops_binned = bin_simulations(simulations=ops,
bins=op_bounds,
keep_cols=keep_cols)
# Compute means, standard devation, standard error across simulations
print(" Computing summary statistics...")
ops_array, ops_mean, ops_std, ops_se = average_simulations(simulations_binned=ops_binned,
keep_cols=keep_cols,
bin_midpoints=(op_bounds[1:] + op_bounds[:-1])/2)
# Write prevalence
print(" Writing...")
np.save(output_path + "/ops_array.npy", ops_array)
ops_mean.to_csv(output_path + "/ops_mean.csv", index=False)
ops_std.to_csv(output_path + "/ops_stds.csv", index=False)
ops_se.to_csv(output_path + "/ops_se.csv", index=False)
print("Done.")
print("")
print("-" * 80)
print("Finished at: %s" % datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
print("=" * 80)