Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 302 derived curve #305

Merged
merged 2 commits into from
May 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 52 additions & 22 deletions metcalcpy/agg_stat.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,22 +302,38 @@ def _calc_stats_derived(self, values_both_arrays):
# get values for the 2nd array
values_2 = values_both_arrays[:, int(num_of_columns / 2):num_of_columns]

func_name = f'calculate_{self.statistic}'
try:
# find the index of the stat column
stat_column_index = np.where(self.column_names == 'stat_name')[0][0]
# find the actual statistic and corresponding functions for both curves
stat_1 = values_1[0, stat_column_index].lower()
stat_2 = values_2[0, stat_column_index].lower()
func_name_1 = f'calculate_{stat_1}'
func_name_2 = f'calculate_{stat_2}'
except ValueError:
func_name_1 = f'calculate_{self.statistic}'
func_name_2 = f'calculate_{self.statistic}'



# some functions have an extra 3rd parameter that represents
# if some data preliminary data aggregation was done
# if this parameter is present we need to add it
num_parameters = len(signature(globals()[func_name]).parameters)
num_parameters_1 = len(signature(globals()[func_name_1]).parameters)
num_parameters_2 = len(signature(globals()[func_name_2]).parameters)

if num_parameters == 2:
# calculate stat for the 1st array
stat_values_1 = [globals()[func_name](values_1, self.column_names)]
# calculate stat for the 2nd array
stat_values_2 = [globals()[func_name](values_2, self.column_names)]

# calculate stat for the 1st array
if num_parameters_1 == 2:
stat_values_1 = [globals()[func_name_1](values_1, self.column_names)]
else:
# calculate stat for the 1st array
stat_values_1 = [globals()[func_name](values_1, self.column_names, True)]
# calculate stat for the 2nd array
stat_values_2 = [globals()[func_name](values_2, self.column_names, True)]
stat_values_1 = [globals()[func_name_1](values_1, self.column_names, True)]

# calculate stat for the 2nd array
if num_parameters_2 == 2:
stat_values_2 = [globals()[func_name_2](values_2, self.column_names)]
else:
stat_values_2 = [globals()[func_name_2](values_2, self.column_names, True)]

# calculate derived stat
stat_values = calc_derived_curve_value(
Expand All @@ -337,22 +353,36 @@ def _calc_stats_derived(self, values_both_arrays):
# get values for the 2nd array
values_2 = row[:, int(num_of_columns / 2):num_of_columns]

func_name = f'calculate_{self.statistic}'
try:
# find the index of the stat column
stat_column_index = np.where(self.column_names == 'stat_name')[0][0]
# find the actual statistic and corresponding functions for both curves
stat_1 = values_1[0, stat_column_index].lower()
stat_2 = values_2[0, stat_column_index].lower()
func_name_1 = f'calculate_{stat_1}'
func_name_2 = f'calculate_{stat_2}'
except ValueError:
func_name_1 = f'calculate_{self.statistic}'
func_name_2 = f'calculate_{self.statistic}'

# some functions have an extra 3rd parameter that represents
# if some data preliminary data aggregation was done
# if this parameter is present we need to add it
num_parameters = len(signature(globals()[func_name]).parameters)
num_parameters_1 = len(signature(globals()[func_name_1]).parameters)
num_parameters_2 = len(signature(globals()[func_name_2]).parameters)

if num_parameters == 2:
# calculate stat for the 1st array
stat_values_1 = [globals()[func_name](values_1, self.column_names)]
# calculate stat for the 2nd array
stat_values_2 = [globals()[func_name](values_2, self.column_names)]
# calculate stat for the 1st array
if num_parameters_1 == 2:
stat_values_1 = [globals()[func_name_1](values_1, self.column_names)]
else:
# calculate stat for the 1st array
stat_values_1 = [globals()[func_name](values_1, self.column_names, True)]
# calculate stat for the 2nd array
stat_values_2 = [globals()[func_name](values_2, self.column_names, True)]
stat_values_1 = [globals()[func_name_1](values_1, self.column_names, True)]

# calculate stat for the 2nd array
if num_parameters_2 == 2:
stat_values_2 = [globals()[func_name_2](values_2, self.column_names)]
else:
stat_values_2 = [globals()[func_name_2](values_2, self.column_names, True)]


# calculate derived stat
stat_value = calc_derived_curve_value(
Expand Down
27 changes: 27 additions & 0 deletions test/data/agg_ratio.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
model fcst_init_beg fcst_valid_beg fcst_lead interp_mthd vx_mask obtype fcst_var stat_name stat_value total me rmse crps crpss ign spread me_oerr rmse_oerr spread_oerr spread_plus_oerr crpscl crps_emp crpscl_emp crpss_emp crps_emp_fair spread_md mae mae_oerr n_ge_obs me_ge_obs n_lt_obs me_lt_obs
RRFSE_CONUS_ICperts_nostoch.rrfs_conuscompact_3km 2022-04-30 00:00:00 2022-04-30 03:00:00 30000 NEAREST CONUS CCPA APCP_03 ECNT_RMSE NA 320109 0.36622 2.8066 0.84772 NA 4.70273 4.07195 0.36636 2.80635 4.07613 4.07611 NA 0.7341 NA NA NA NA NA NA NA NA NA NA
RRFSE_CONUS_ICperts_nostoch.rrfs_conuscompact_3km 2022-05-01 00:00:00 2022-05-01 03:00:00 30000 NEAREST CONUS CCPA APCP_03 ECNT_RMSE NA 353742 0.38014 3.70388 0.9718 NA 8.25648 3.59291 0.38019 3.70433 3.59655 3.59648 NA 0.87711 NA NA NA NA NA NA NA NA NA NA
RRFSE_CONUS_ICperts_nostoch.rrfs_conuscompact_3km 2022-05-02 00:00:00 2022-05-02 03:00:00 30000 NEAREST CONUS CCPA APCP_03 ECNT_RMSE NA 374244 0.17353 3.14131 0.73601 NA 11.44134 3.14609 0.17361 3.14115 3.14875 3.14925 NA 0.62567 NA NA NA NA NA NA NA NA NA NA
RRFSE_CONUS_ICperts_nostoch.rrfs_conuscompact_3km 2022-05-03 00:00:00 2022-05-03 03:00:00 30000 NEAREST CONUS CCPA APCP_03 ECNT_RMSE NA 444554 0.4117 3.3865 0.78904 NA 5.13217 3.52163 0.41178 3.38729 3.52554 3.52557 NA 0.69105 NA NA NA NA NA NA NA NA NA NA
RRFSE_CONUS_ICperts_nostoch.rrfs_conuscompact_3km 2022-05-04 00:00:00 2022-05-04 03:00:00 30000 NEAREST CONUS CCPA APCP_03 ECNT_RMSE NA 407342 0.23007 2.55042 0.56938 NA 6.05901 2.47051 0.23013 2.55062 2.47344 2.47319 NA 0.48719 NA NA NA NA NA NA NA NA NA NA
RRFSE_CONUS_ICperts_nostoch.rrfs_conuscompact_3km 2022-05-05 00:00:00 2022-05-05 03:00:00 30000 NEAREST CONUS CCPA APCP_03 ECNT_RMSE NA 346222 0.3018 4.70622 1.06257 NA 6.0142 4.14158 0.30189 4.7058 4.14877 4.14902 NA 0.9488 NA NA NA NA NA NA NA NA NA NA
RRFSE_CONUS_ICperts_nostoch.rrfs_conuscompact_3km 2022-05-06 00:00:00 2022-05-06 03:00:00 30000 NEAREST CONUS CCPA APCP_03 ECNT_RMSE NA 382289 0.6357 3.92808 1.12617 NA 5.98337 4.09194 0.63566 3.92848 4.09559 4.09582 NA 1.01874 NA NA NA NA NA NA NA NA NA NA
RRFSE_CONUS_ICperts_nostoch.rrfs_conuscompact_3km 2022-05-07 00:00:00 2022-05-07 03:00:00 30000 NEAREST CONUS CCPA APCP_03 ECNT_RMSE NA 337693 0.56191 2.91899 0.98897 NA 2.16174 3.8834 0.56188 2.91963 3.88686 3.88663 NA 0.86967 NA NA NA NA NA NA NA NA NA NA
RRFSE_CONUS_ICperts_nostoch.rrfs_conuscompact_3km 2022-05-08 00:00:00 2022-05-08 03:00:00 30000 NEAREST CONUS CCPA APCP_03 ECNT_RMSE NA 291766 0.24127 2.284 0.60617 NA 2.93861 2.94593 0.24134 2.28444 2.94967 2.94942 NA 0.54989 NA NA NA NA NA NA NA NA NA NA
RRFSE_CONUS_ICperts_nostoch.rrfs_conuscompact_3km 2022-05-09 00:00:00 2022-05-09 03:00:00 30000 NEAREST CONUS CCPA APCP_03 ECNT_RMSE NA 350770 0.17212 0.96662 0.33304 NA 2.16917 1.421 0.17214 0.96648 1.42225 1.42233 NA 0.26203 NA NA NA NA NA NA NA NA NA NA
RRFSE_CONUS_ICperts_nostoch.rrfs_conuscompact_3km 2022-05-10 00:00:00 2022-05-10 03:00:00 30000 NEAREST CONUS CCPA APCP_03 ECNT_RMSE NA 249546 0.38799 2.30182 0.50836 NA 1.59822 3.45128 0.38799 2.3023 3.4529 3.45303 NA 0.39968 NA NA NA NA NA NA NA NA NA NA
RRFSE_CONUS_ICperts_nostoch.rrfs_conuscompact_3km 2022-05-11 00:00:00 2022-05-11 03:00:00 30000 NEAREST CONUS CCPA APCP_03 ECNT_RMSE NA 213870 0.61377 3.25007 0.93871 NA 5.60527 5.00227 0.61376 3.25021 5.00494 5.00456 NA 0.65272 NA NA NA NA NA NA NA NA NA NA
RRFSE_CONUS_ICperts_nostoch.rrfs_conuscompact_3km 2022-05-12 00:00:00 2022-05-12 03:00:00 30000 NEAREST CONUS CCPA APCP_03 ECNT_RMSE NA 252252 0.029757 3.08964 0.76245 NA 9.38456 3.4923 0.029802 3.09007 3.4976 3.49786 NA 0.64832 NA NA NA NA NA NA NA NA NA NA
RRFSE_CONUS_ICperts_nostoch.rrfs_conuscompact_3km 2022-04-30 00:00:00 2022-04-30 03:00:00 30000 NEAREST CONUS CCPA APCP_03 ECNT_SPREAD NA 320109 0.36622 2.8066 0.84772 NA 4.70273 4.07195 0.36636 2.80635 4.07613 4.07611 NA 0.7341 NA NA NA NA NA NA NA NA NA NA
RRFSE_CONUS_ICperts_nostoch.rrfs_conuscompact_3km 2022-05-01 00:00:00 2022-05-01 03:00:00 30000 NEAREST CONUS CCPA APCP_03 ECNT_SPREAD NA 353742 0.38014 3.70388 0.9718 NA 8.25648 3.59291 0.38019 3.70433 3.59655 3.59648 NA 0.87711 NA NA NA NA NA NA NA NA NA NA
RRFSE_CONUS_ICperts_nostoch.rrfs_conuscompact_3km 2022-05-02 00:00:00 2022-05-02 03:00:00 30000 NEAREST CONUS CCPA APCP_03 ECNT_SPREAD NA 374244 0.17353 3.14131 0.73601 NA 11.44134 3.14609 0.17361 3.14115 3.14875 3.14925 NA 0.62567 NA NA NA NA NA NA NA NA NA NA
RRFSE_CONUS_ICperts_nostoch.rrfs_conuscompact_3km 2022-05-03 00:00:00 2022-05-03 03:00:00 30000 NEAREST CONUS CCPA APCP_03 ECNT_SPREAD NA 444554 0.4117 3.3865 0.78904 NA 5.13217 3.52163 0.41178 3.38729 3.52554 3.52557 NA 0.69105 NA NA NA NA NA NA NA NA NA NA
RRFSE_CONUS_ICperts_nostoch.rrfs_conuscompact_3km 2022-05-04 00:00:00 2022-05-04 03:00:00 30000 NEAREST CONUS CCPA APCP_03 ECNT_SPREAD NA 407342 0.23007 2.55042 0.56938 NA 6.05901 2.47051 0.23013 2.55062 2.47344 2.47319 NA 0.48719 NA NA NA NA NA NA NA NA NA NA
RRFSE_CONUS_ICperts_nostoch.rrfs_conuscompact_3km 2022-05-05 00:00:00 2022-05-05 03:00:00 30000 NEAREST CONUS CCPA APCP_03 ECNT_SPREAD NA 346222 0.3018 4.70622 1.06257 NA 6.0142 4.14158 0.30189 4.7058 4.14877 4.14902 NA 0.9488 NA NA NA NA NA NA NA NA NA NA
RRFSE_CONUS_ICperts_nostoch.rrfs_conuscompact_3km 2022-05-06 00:00:00 2022-05-06 03:00:00 30000 NEAREST CONUS CCPA APCP_03 ECNT_SPREAD NA 382289 0.6357 3.92808 1.12617 NA 5.98337 4.09194 0.63566 3.92848 4.09559 4.09582 NA 1.01874 NA NA NA NA NA NA NA NA NA NA
RRFSE_CONUS_ICperts_nostoch.rrfs_conuscompact_3km 2022-05-07 00:00:00 2022-05-07 03:00:00 30000 NEAREST CONUS CCPA APCP_03 ECNT_SPREAD NA 337693 0.56191 2.91899 0.98897 NA 2.16174 3.8834 0.56188 2.91963 3.88686 3.88663 NA 0.86967 NA NA NA NA NA NA NA NA NA NA
RRFSE_CONUS_ICperts_nostoch.rrfs_conuscompact_3km 2022-05-08 00:00:00 2022-05-08 03:00:00 30000 NEAREST CONUS CCPA APCP_03 ECNT_SPREAD NA 291766 0.24127 2.284 0.60617 NA 2.93861 2.94593 0.24134 2.28444 2.94967 2.94942 NA 0.54989 NA NA NA NA NA NA NA NA NA NA
RRFSE_CONUS_ICperts_nostoch.rrfs_conuscompact_3km 2022-05-09 00:00:00 2022-05-09 03:00:00 30000 NEAREST CONUS CCPA APCP_03 ECNT_SPREAD NA 350770 0.17212 0.96662 0.33304 NA 2.16917 1.421 0.17214 0.96648 1.42225 1.42233 NA 0.26203 NA NA NA NA NA NA NA NA NA NA
RRFSE_CONUS_ICperts_nostoch.rrfs_conuscompact_3km 2022-05-10 00:00:00 2022-05-10 03:00:00 30000 NEAREST CONUS CCPA APCP_03 ECNT_SPREAD NA 249546 0.38799 2.30182 0.50836 NA 1.59822 3.45128 0.38799 2.3023 3.4529 3.45303 NA 0.39968 NA NA NA NA NA NA NA NA NA NA
RRFSE_CONUS_ICperts_nostoch.rrfs_conuscompact_3km 2022-05-11 00:00:00 2022-05-11 03:00:00 30000 NEAREST CONUS CCPA APCP_03 ECNT_SPREAD NA 213870 0.61377 3.25007 0.93871 NA 5.60527 5.00227 0.61376 3.25021 5.00494 5.00456 NA 0.65272 NA NA NA NA NA NA NA NA NA NA
RRFSE_CONUS_ICperts_nostoch.rrfs_conuscompact_3km 2022-05-12 00:00:00 2022-05-12 03:00:00 30000 NEAREST CONUS CCPA APCP_03 ECNT_SPREAD NA 252252 0.029757 3.08964 0.76245 NA 9.38456 3.4923 0.029802 3.09007 3.4976 3.49786 NA 0.64832 NA NA NA NA NA NA NA NA NA NA
73 changes: 73 additions & 0 deletions test/test_agg_ratio.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import numpy as np
import pytest

from metcalcpy.agg_stat import AggStat, pd


def test_agg_ratio(settings):
agg_stat = settings['agg_stat']
agg_stat.calculate_stats_and_ci()
result_frame = pd.read_csv(
agg_stat.params['agg_stat_output'],
header=[0],
sep='\t'
)
assert result_frame.size == 24
assert result_frame.shape == (3, 8)
assert np.allclose(result_frame['stat_value'][0], 3.15696)
assert np.allclose(result_frame['stat_value'][1], 3.52230)
assert np.allclose(result_frame['stat_value'][2], 0.89628)



@pytest.fixture
def settings():
"""Initialise values for testing.

Returns:
dictionary with values of different type
"""
params = {'random_seed': 1, 'indy_var': 'fcst_lead',
'method': 'perc',
'num_iterations': 10, 'event_equal': 'True',
'agg_stat_input': 'data/agg_ratio.data',
'agg_stat_output': 'data/agg_ratio_data_output.data',
'fixed_vars_vals_input': {
'obtype': {
'obtype_0' : ['CCPA']
},
'interp_mthd': {
'interp_mthd_3': ['NEAREST']
},
'vx_mask': {
'vx_mask_1': ['CONUS']
},
'fcst_init_beg': {
'fcst_init_beg_2': ['2022-04-30 00:00:00', '2022-05-01 00:00:00', '2022-05-02 00:00:00','2022-05-03 00:00:00', '2022-05-04 00:00:00',
'2022-05-05 00:00:00', '2022-05-06 00:00:00', '2022-05-07 00:00:00', '2022-05-08 00:00:00', '2022-05-09 00:00:00',
'2022-05-10 00:00:00','2022-05-11 00:00:00', '2022-05-12 00:00:00' ]
}
},
'series_val_1': {'model': ['RRFSE_CONUS_ICperts_nostoch.rrfs_conuscompact_3km']},
'series_val_2': {},
'alpha': 0.05, 'line_type': 'ecnt',
'num_threads': -1,
'indy_vals': ['30000'],
'circular_block_bootstrap': True,
'equalize_by_indep': 'True',
'cl_step': 0.05,
'derived_series_1':[
['RRFSE_CONUS_ICperts_nostoch.rrfs_conuscompact_3km APCP_03 ECNT_RMSE',
'RRFSE_CONUS_ICperts_nostoch.rrfs_conuscompact_3km APCP_03 ECNT_SPREAD',
'RATIO']
],
'fcst_var_val_1':{
'APCP_03': ['ECNT_RMSE','ECNT_SPREAD']
},
'list_stat_1':['ECNT_RMSE', 'ECNT_SPREAD'],
'list_stat_2':[]
}
agg_stat = AggStat(params)
settings_dict = dict()
settings_dict['agg_stat'] = agg_stat
return settings_dict