def test_calculate_average(): # Independently test getting the average # of a data array based on method date_base = datetime.datetime(2019, 1, 1) date_array = np.array( [date_base + datetime.timedelta(days=i) for i in range(5)]) expected_stat_file_dates = [] for date in date_array: dt = date.time() expected_stat_file_dates.append(date.strftime('%Y%m%d_%H%M%S')) model_data_index = pd.MultiIndex.from_product( [['MODEL_TEST'], expected_stat_file_dates], names=['model_plot_name', 'dates']) model_data_array = np.array([[ 3600, 5525.75062, 5525.66493, 30615218.26089, 30615764.49722, 30614724.90979, 5.06746 ], [ 3600, 5519.11108, 5519.1014, 30549413.45946, 30549220.68868, 30549654.24048, 5.12344 ], [ 3600, 5516.80228, 5516.79513, 30522742.16484, 30522884.89927, 30522660.30975, 5.61752 ], [ 3600, 5516.93924, 5517.80544, 30525709.03932, 30520984.50965, 30530479.99675, 4.94325 ], [ 3600, 5514.52274, 5514.68224, 30495695.82208, 30494633.24046, 30496805.48259, 5.20369 ]]) model_data = pd.DataFrame( model_data_array, index=model_data_index, columns=['TOTAL', 'FBAR', 'OBAR', 'FOBAR', 'FFBAR', 'OOBAR', 'MAE']) stat_values_array = np.array([[[(5525.75062 - 5525.66493), (5519.11108 - 5519.1014), (5516.80228 - 5516.79513), (5516.93924 - 5517.80544), (5514.52274 - 5514.68224)]]]) # Test 1 average_method = 'MEAN' stat = 'bias' model_dataframe = model_data model_stat_values = stat_values_array[:, 0, :] expected_average_array = np.array([-0.184636]) test_average_array = plot_util.calculate_average(logger, average_method, stat, model_dataframe, model_stat_values) assert (len(test_average_array) == len(expected_average_array)) for l in range(len(test_average_array)): assert (round(test_average_array[l], 6) == expected_average_array[l]) # Test 2 average_method = 'MEDIAN' stat = 'bias' model_dataframe = model_data model_stat_values = stat_values_array[:, 0, :] expected_average_array = np.array([0.00715]) test_average_array = plot_util.calculate_average(logger, average_method, stat, model_dataframe, model_stat_values) assert (len(test_average_array) == len(expected_average_array)) for l in range(len(test_average_array)): assert (round(test_average_array[l], 6) == expected_average_array[l]) # Test 3 average_method = 'AGGREGATION' stat = 'bias' model_dataframe = model_data model_stat_values = stat_values_array[:, 0, :] expected_average_array = np.array([-0.184636]) test_average_array = plot_util.calculate_average(logger, average_method, stat, model_dataframe, model_stat_values) assert (len(test_average_array) == len(expected_average_array)) for l in range(len(test_average_array)): assert (round(test_average_array[l], 6) == expected_average_array[l]) # Test 4 stat_values_array = np.array( [[[5525.75062, 5519.11108, 5516.80228, 5516.93924, 5514.52274]], [[5525.66493, 5519.1014, 5516.79513, 5517.80544, 5514.68224]]]) average_method = 'MEAN' stat = 'fbar_obar' model_dataframe = model_data model_stat_values = stat_values_array[:, 0, :] expected_average_array = np.array([5518.625192, 5518.809828]) test_average_array = plot_util.calculate_average(logger, average_method, stat, model_dataframe, model_stat_values) assert (len(test_average_array) == len(expected_average_array)) for l in range(len(test_average_array)): assert (round(test_average_array[l], 6) == expected_average_array[l]) # Test 5 average_method = 'MEDIAN' stat = 'fbar_obar' model_dataframe = model_data model_stat_values = stat_values_array[:, 0, :] expected_average_array = np.array([5516.93924, 5517.80544]) test_average_array = plot_util.calculate_average(logger, average_method, stat, model_dataframe, model_stat_values) assert (len(test_average_array) == len(expected_average_array)) for l in range(len(test_average_array)): assert (round(test_average_array[l], 6) == expected_average_array[l])
'fcst_level': fcst_var_level, 'obs_level': obs_var_level, 'fcst_thresh': fcst_var_thresh, 'obs_thresh': obs_var_thresh, } model_stat_file = do_string_sub(model_stat_template, **string_sub_dict) lead_avg_file = get_lead_avg_file(stat, model_stat_file, fcst_lead, output_base_dir) logger.debug("Writing model " + str(model_num) + " " + model_name + " " + "with name on plot " + model_plot_name + " lead " + fcst_lead + " average to file: " + lead_avg_file) model_stat_average_array = plot_util.calculate_average( logger, average_method, stat, model_data.loc[[model_plot_name]], model_stat_values_array) with open(lead_avg_file, 'a') as file2write: file2write.write(fcst_lead) if fcst_var_units_plot_title != '': file2write.write(' ' + fcst_var_units_plot_title) else: file2write.write(' [NA]') if obs_var_units_plot_title != '': file2write.write(' ' + obs_var_units_plot_title) else: file2write.write(' [NA]') for l in range(len(model_stat_average_array)): file2write.write(' ' + str(model_stat_average_array[l])) file2write.write('\n') # Write confidence intervals to file, if requested,