-
Notifications
You must be signed in to change notification settings - Fork 2
/
agg_stat_event_equalize.py
163 lines (132 loc) · 6.58 KB
/
agg_stat_event_equalize.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
# ============================*
# ** Copyright UCAR (c) 2020
# ** University Corporation for Atmospheric Research (UCAR)
# ** National Center for Atmospheric Research (NCAR)
# ** Research Applications Lab (RAL)
# ** P.O.Box 3000, Boulder, Colorado, 80307-3000, USA
# ============================*
"""
Program Name: agg_stat_event_equalize.py
How to use:
- Call from other Python function
AGG_STAT_EVENT_EQUALIZE = AggStatEventEqualize(PARAMS)
AGG_STAT_EVENT_EQUALIZE.calculate_values()
where PARAMS – a dictionary with data description parameters including
location of input and output data.
The structure is similar to Rscript template
- Run as a stand-alone script
python agg_stat_event_equalize.py <parameters_file>
where - <parameters_file> is YAML file with parameters
and environment variable should be set to PYTHONPATH=<path_to_METcalcpy>
- Run from Java
proc = Runtime.getRuntime().exec(
“python agg_stat_event_equalize.eqz.py <parameters_file>”,
new String[]{”PYTHONPATH=<path_to_METcalcpy>”},
new File(System.getProperty("user.home")));
"""
import argparse
import sys
import itertools
import pandas as pd
import yaml
import numpy as np
import warnings
from metcalcpy import GROUP_SEPARATOR
from metcalcpy.event_equalize import event_equalize
class AggStatEventEqualize:
"""A class that performs event equalisation logic on input data
with MODE and MTD attribute statistics
All parameters including data description and location is in the parameters dictionary
Usage:
initialise this call with the parameters dictionary and then
calls perform_ee method
This method will execute EE and save the result to the file
AGG_STAT_EVENT_EQUALIZE = AggStatEventEqualize(PARAMS)
AGG_STAT_EVENT_EQUALIZE.calculate_values()
"""
def __init__(self, in_params):
self.params = in_params
self.input_data = pd.read_csv(
self.params['agg_stat_input'],
header=[0],
sep='\t'
)
self.column_names = self.input_data.columns.values
self.series_data = None
def calculate_values(self):
"""Performs event equalisation if needed and saves equalized data to the file.
"""
if not self.input_data.empty:
# list all fixed variables
if 'fixed_vars_vals_input' in self.params:
fix_vals_permuted_list = []
for key in self.params['fixed_vars_vals_input']:
vals_permuted = list(itertools.product(*self.params['fixed_vars_vals_input'][key].values()))
fix_vals_permuted_list.append(vals_permuted)
fix_vals_permuted = [item for sublist in fix_vals_permuted_list for item in sublist]
else:
fix_vals_permuted = []
# perform EE for each forecast variable on y1 axis
output_ee_data = self.run_ee_on_axis(fix_vals_permuted, '1')
# if the second Y axis is present - run event equalizer on Y1
# and then run event equalizer on Y1 and Y2 equalized data
if self.params['series_val_2']:
output_ee_data_2 = self.run_ee_on_axis(fix_vals_permuted, '2')
output_ee_data = output_ee_data.drop('equalize', axis=1)
output_ee_data_2 = output_ee_data_2.drop('equalize', axis=1)
warnings.simplefilter(action='error', category=FutureWarning)
all_ee_records = pd.concat([output_ee_data, output_ee_data_2]).reindex()
all_series_vars = {}
for key in self.params['series_val_2']:
all_series_vars[key] = np.unique(self.params['series_val_2'][key]
+ self.params['series_val_2'][key])
output_ee_data = event_equalize(all_ee_records, self.params['indy_var'],
all_series_vars,
list(self.params['fixed_vars_vals_input'].keys()),
fix_vals_permuted, True,
False)
else:
output_ee_data = pd.DataFrame()
header = True
mode = 'w'
output_ee_data.to_csv(self.params['agg_stat_output'],
index=None, header=header, mode=mode,
sep="\t", na_rep="NA")
def run_ee_on_axis(self, fix_vals_permuted, axis='1'):
"""Performs event equalisation against previously calculated cases for the selected axis
Returns:
A data frame that contains equalized records
"""
output_ee_data = pd.DataFrame()
for series_var, series_var_vals in self.params['series_val_' + axis].items():
# ungroup series value
series_var_vals_no_group = []
for val in series_var_vals:
split_val = val.split(GROUP_SEPARATOR)
series_var_vals_no_group.extend(split_val)
# filter input data based on fcst_var, statistic and all series variables values
series_data_for_ee = self.input_data[
self.input_data[series_var].isin(series_var_vals_no_group)
]
# perform EE on filtered data
series_data_after_ee = \
event_equalize(series_data_for_ee, self.params['indy_var'],
self.params['series_val_' + axis],
list(self.params['fixed_vars_vals_input'].keys()),
fix_vals_permuted, True, False)
# append EE data to result
if output_ee_data.empty:
output_ee_data = series_data_after_ee
else:
warnings.simplefilter(action="error", category=FutureWarning)
output_ee_data = pd.concat([output_ee_data, series_data_after_ee])
return output_ee_data
if __name__ == "__main__":
PARSER = argparse.ArgumentParser(description='List of agg_stat_event_equalize arguments')
PARSER.add_argument("parameters_file", help="Path to YAML parameters file",
type=argparse.FileType('r'),
default=sys.stdin)
ARGS = PARSER.parse_args()
PARAMS = yaml.load(ARGS.parameters_file, Loader=yaml.FullLoader)
AGG_STAT_EVENT_EQUALIZE = AggStatEventEqualize(PARAMS)
AGG_STAT_EVENT_EQUALIZE.calculate_values()