-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvgpa_main.py
More file actions
executable file
·179 lines (133 loc) · 4.56 KB
/
vgpa_main.py
File metadata and controls
executable file
·179 lines (133 loc) · 4.56 KB
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
#!/usr/bin/env python3
import sys
import json
import pandas as pd
from pathlib import Path
from src.var_bayes.simulation import Simulation
# INFO:
__author__ = "Michail Vrettas, PhD"
__email__ = "michail.vrettas@gmail.com"
def validate_input_parameters_file(filename):
"""
Validates an input (json) file to check if
it contains the required keys. It does not
validate the values of the keys.
:param filename: Is a "Path" object that
contains the input model parameters for the
simulation.
:return: A dictionary loaded from the input
file.
:raises ValueError: if a keyword is missing
from the file.
"""
# Open the file in "Read Only" mode.
with open(filename, 'r') as input_file:
# Load the model parameters.
model_params = json.load(input_file)
# Required keys in the json file.
required_keys = {"Output_Name", "Model", "Ode-method",
"Time-window", "Noise", "Observations",
"Drift", "Prior", "Random-Seed"}
# Check the keywords for membership.
for k in required_keys:
# The order in here doesn't matter.
if k not in model_params:
raise ValueError(f" Key: {k}, is not given.")
# _end_if_
# _end_for_
# Show message.
print(" Model parameters are given correctly.")
# _end_with_
# Dictionary will contain all the input parameters.
return model_params
# _end_def_
# Main function.
def main(params_file=None, data_file=None):
"""
As the name suggests, this is the main function
that is called to initiate the simulation run.
:param params_file: (string) that points to the
input file for the parameters.
:param data_file: (string) that points to the
input file for the observations (optional).
:return: None.
"""
# Check if we got model parameters.
if params_file is not None:
try:
# Make sure params_file is a Path object.
params_file = Path(params_file)
# Check if everything is ok.
params = validate_input_parameters_file(params_file)
except ValueError as e0:
# Show the error message.
print(e0)
# Exit the program.
sys.exit(1)
# _end_try_
else:
print(" The simulation can't run without input parameters.")
# Exit the program.
sys.exit(1)
# _end_if_
# Set this to None.
obs_data = None
# Check if we have given data separately.
if data_file is not None:
# Make sure it's a Path object.
data_file = Path(data_file)
# Display where we got the observational data.
print(f" Simulation observational data file: {data_file}")
# Open the data in "Read Only" mode.
with open(data_file, 'r') as input_file:
# The file should have two columns.
obs_data = pd.read_csv(input_file,
names=["t", "Yt"])
# _end_with_
# _end_if_
# Get the output name from the file.
output_name = params["Output_Name"]
# If nothing has been given set a
# default name.
if output_name is None:
output_name = "Sim_00"
# _end_if_
try:
# Create a simulation object.
sim_vgpa = Simulation(output_name)
# Setup parameters (initialization).
sim_vgpa.setup(params, obs_data)
# Run the simulation (smoothing).
sim_vgpa.run()
# Save the results.
sim_vgpa.save()
except Exception as e1:
print(e1)
# Exit the program.
sys.exit(1)
# _end_try_
# _end_main_
# Run the script.
if __name__ == "__main__":
# Check if we have given input parameters.
if len(sys.argv) > 1:
# Local import.
import argparse
# Create a parser object
parser = argparse.ArgumentParser(description=" VGPA (1.0) ")
# Input file with simulation parameters.
parser.add_argument("--params",
help=" Input file (.json) with simulation parameters.")
# Input file with simulation data.
parser.add_argument("--data",
help=" Input file (.csv) with observational data.")
# Parse the arguments.
args = parser.parse_args()
# Call the main function.
main(args.params, args.data)
# Display final info.
print(' Simulation completed.')
else:
sys.exit('Error: Not enough input parameters.')
# _end_if_
# _end_program_