Skip to content

Commit

Permalink
Issue #383 modifications to support multiple plot types, list_stat_1 …
Browse files Browse the repository at this point in the history
…values
  • Loading branch information
bikegeek committed Feb 17, 2024
1 parent 16ac0f8 commit 115f803
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 29 deletions.
36 changes: 23 additions & 13 deletions metplotpy/plots/tcmpr_plots/line/mean/tcmpr_line_mean.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,32 @@


class TcmprLineMean(TcmprLine):
def __init__(self, config_obj, column_info, col, case_data, input_df, baseline_data):
super().__init__(config_obj, column_info, col, case_data, input_df, None)
def __init__(self, config_obj, column_info, col, case_data, input_df, baseline_data, stat_name):
super().__init__(config_obj, column_info, col, case_data, input_df, None, stat_name)
print("--------------------------------------------------------")
print(f"Plotting MEAN time series by {self.config_obj.series_val_names[0]}")

self._adjust_titles()
self.series_list = self._create_series(self.input_df)
self._adjust_titles(stat_name)
self.series_list = self._create_series(self.input_df, stat_name)
self.case_data = None
self.cur_baseline = baseline_data['cur_baseline']
self.cur_baseline_data = baseline_data['cur_baseline_data']
self.baseline_lead_time = 'ind'
self._init_hfip_baseline_for_plot()
if self.config_obj.prefix is None or len(self.config_obj.prefix) == 0:
self.plot_filename = f"{self.config_obj.plot_dir}{os.path.sep}{self.config_obj.list_stat_1[0]}_mean.png"
# self.plot_filename = f"{self.config_obj.plot_dir}{os.path.sep}{self.config_obj.list_stat_1[0]}_{stat_name}_mean.png"
self.plot_filename = f"{self.config_obj.plot_dir}{os.path.sep}{stat_name}_mean.png"
else:
self.plot_filename = f"{self.config_obj.plot_dir}{os.path.sep}{self.config_obj.prefix}.png"
self.plot_filename = f"{self.config_obj.plot_dir}{os.path.sep}{self.config_obj.prefix}_{stat_name}_mean.png"
# remove the old file if it exist
if os.path.exists(self.plot_filename):
os.remove(self.plot_filename)
self._create_figure()

def _adjust_titles(self):
def _adjust_titles(self, stat_name):
if self.yaxis_1 is None or len(self.yaxis_1) == 0:
self.yaxis_1 = self.config_obj.list_stat_1[0] + '(' + self.col['units'] + ')'
# self.yaxis_1 = self.config_obj.list_stat_1[0] + '(' + self.col['units'] + ')'
self.yaxis_1 = stat_name + '(' + self.col['units'] + ')'

if self.title is None or len(self.title) == 0:
self.title = 'Mean of ' + self.col['desc'] + ' by ' \
Expand All @@ -42,17 +44,19 @@ def _init_hfip_baseline_for_plot(self):
self.cur_baseline_data = self.cur_baseline_data[(self.cur_baseline_data['TYPE'] == 'CONS')]
print('Plot HFIP Baseline:' + self.cur_baseline.replace('Error ', ''))

def _create_series(self, input_data):
def _create_series(self, input_data, stat_name):
"""
Generate all the series objects that are to be displayed as specified by the plot_disp
setting in the config file. The points are all ordered by datetime. Each series object
is represented by a box in the diagram, so they also contain information
is represented by a line in the diagram, so they also contain information
for plot-related/appearance-related settings (which were defined in the config file).
A series object is defined by the series_val_1 setting in the config file.
Args:
input_data: The input data in the form of a Pandas dataframe.
This data will be subset to reflect the series data of interest.
stat_name: Corresponds to the current list_stat_1 value (e.g. TK_ERR, ABS(AMAX_WIND-BMAX_WIND), etc.)
in the config file.
Returns:
a list of series objects that are to be displayed
Expand All @@ -61,10 +65,16 @@ def _create_series(self, input_data):
series_list = []

# add series for y1 axis
num_series_y1 = len(self.config_obj.get_series_y(1))
for i, name in enumerate(self.config_obj.get_series_y(1)):
all_series = self.config_obj.get_series_y(1)

# Limit the series to only the current statistic, list_stat_1 in config file
series_by_stat = [cur for cur in all_series if stat_name in cur]

num_series_y1 = len(series_by_stat)
for i, name in enumerate(series_by_stat):
if not isinstance(name, list):
name = [name]

series_obj = TcmprSeriesLineMean(self.config_obj, i, input_data, series_list, name)
series_list.append(series_obj)

Expand Down
2 changes: 1 addition & 1 deletion metplotpy/plots/tcmpr_plots/line/mean/tcmpr_series_line_mean.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

class TcmprSeriesLineMean(TcmprSeries):
"""
Represents a Box plot series object
Represents a TCMPR mean series object
of data points and their plotting style
elements (line colors, etc.)
Expand Down
24 changes: 15 additions & 9 deletions metplotpy/plots/tcmpr_plots/line/median/tcmpr_line_median.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@


class TcmprLineMedian(TcmprLine):
def __init__(self, config_obj, column_info, col, case_data, input_df):
super().__init__(config_obj, column_info, col, case_data, input_df, None)
def __init__(self, config_obj, column_info, col, case_data, input_df, stat_name):
super().__init__(config_obj, column_info, col, case_data, input_df, None, stat_name)
print("--------------------------------------------------------")
print(f"Plotting MEDIAN time series by {self.config_obj.series_val_names[0]}")

print("Plot HFIP Baseline:" + self.cur_baseline)
self._adjust_titles()
self.series_list = self._create_series(self.input_df)
self.series_list = self._create_series(self.input_df, stat_name)
self.case_data = None
if self.config_obj.prefix is None or len(self.config_obj.prefix) == 0:
self.plot_filename = f"{self.config_obj.plot_dir}{os.path.sep}{self.config_obj.list_stat_1[0]}_median.png"
self.plot_filename = f"{self.config_obj.plot_dir}{os.path.sep}{stat_name}_median.png"
else:
self.plot_filename = f"{self.config_obj.plot_dir}{os.path.sep}{self.config_obj.prefix}.png"

# self.plot_filename = f"{self.config_obj.plot_dir}{os.path.sep}{self.config_obj.prefix}.png"
self.plot_filename = f"{self.config_obj.plot_dir}{os.path.sep}{self.config_obj.prefix}_{stat_name}_median.png"
# remove the old file if it exist
if os.path.exists(self.plot_filename):
os.remove(self.plot_filename)
Expand All @@ -33,7 +33,7 @@ def _adjust_titles(self):
+ self.column_info[self.column_info['COLUMN'] == self.config_obj.series_val_names[0]][
"DESCRIPTION"].tolist()[0]

def _create_series(self, input_data):
def _create_series(self, input_data, stat_name):
"""
Generate all the series objects that are to be displayed as specified by the plot_disp
setting in the config file. The points are all ordered by datetime. Each series object
Expand All @@ -52,10 +52,16 @@ def _create_series(self, input_data):
series_list = []

# add series for y1 axis
num_series_y1 = len(self.config_obj.get_series_y(1))
for i, name in enumerate(self.config_obj.get_series_y(1)):
all_series = self.config_obj.get_series_y(1)

# Limit the series to only the current statistic, list_stat_1 in config file
series_by_stat = [cur for cur in all_series if stat_name in cur]

num_series_y1 = len(series_by_stat)
for i, name in enumerate(series_by_stat):
if not isinstance(name, list):
name = [name]

series_obj = TcmprSeriesLineMedian(self.config_obj, i, input_data, series_list, name)
series_list.append(series_obj)

Expand Down
Empty file.
2 changes: 1 addition & 1 deletion metplotpy/plots/tcmpr_plots/line/tcmpr_line.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


class TcmprLine(Tcmpr):
def __init__(self, config_obj, column_info, col, case_data, input_df, baseline_data):
def __init__(self, config_obj, column_info, col, case_data, input_df, baseline_data, stat_name):
super().__init__(config_obj, column_info, col, case_data, input_df)

def _create_figure(self):
Expand Down
6 changes: 3 additions & 3 deletions metplotpy/plots/tcmpr_plots/tcmpr_config.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def __init__(self, parameters: dict) -> None:
super().__init__(parameters)

##############################################
self.plot_list = self._get_plot()
self.plot_type = self._get_plot()
self.tcst_files = self._get_tcst_files()
self.tcst_dir = self._get_tcst_dir()
self.rp_diff = self._get_rp_diff()
Expand Down Expand Up @@ -219,9 +219,9 @@ def _get_markers_size(self) -> list:
return self.create_list_by_series_ordering(markers_size)

def _get_plot(self) -> list:
plot_list = self.get_config_value('plot_list')
plot_type = self.get_config_value('plot_type')
# TODO validate plots BOXPLOT, POINT, MEAN, MEDIAN, RELPERF, RANK, SKILL_MN, SKILL_MD
return plot_list
return plot_type

def _get_tcst_files(self) -> list:
tcst_files = self.get_config_value('tcst_files')
Expand Down
3 changes: 2 additions & 1 deletion metplotpy/plots/tcmpr_plots/tcmpr_series.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ def __init__(self, config, idx: int, input_data, series_list: list,
self.series_list = series_list
self.series_name = series_name
self.rank_min_val = []
self.series_len = len(config.get_series_y(1)) + len(config.get_config_value('derived_series_1'))
# self.series_len = len(config.get_series_y(1)) + len(config.get_config_value('derived_series_1'))
self.series_len = len(self.series_list) + len(config.get_config_value('derived_series_1'))
self.skill_ref_data = skill_ref_data
if idx >= self.series_len:
super().__init__(config, 0, input_data, 1)
Expand Down
7 changes: 6 additions & 1 deletion metplotpy/plots/tcmpr_plots/tcmpr_util.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,18 @@ def init_hfip_baseline(config, baseline_file, input_df):
all_filters = [baseline['BASIN'].isin(input_df['BASIN']), baseline['VARIABLE'].isin([stat]),
baseline['LEAD_HR'].isin(input_df['LEAD_HR'])]
mask = np.array(all_filters).all(axis=0)
cur_baseline_data = baseline.loc[mask]
cur_baseline_data_filtered = baseline.loc[mask]
# work on a copy to prevent the SettingWithCopyWarning
cur_baseline_data = cur_baseline_data_filtered.copy()

# cur_baseline_data = cur_baseline_data_filtered.copy(deep=True)
if config.hfip_bsln == "0":
cur_baseline = "HFIP Baseline"
elif config.hfip_bsln == "5":
cur_baseline = 'Error Target for 20% HFIP Goal'
cur_baseline_data['VALUE'] = cur_baseline_data['VALUE'].apply(
lambda x: calc_util.round_half_up(x * 0.8, 1))

else: # config.hfip_bsln == "10":
cur_baseline = 'Error Target for 50% HFIP Goal'
cur_baseline_data['VALUE'] = cur_baseline_data['VALUE'].apply(
Expand Down

0 comments on commit 115f803

Please sign in to comment.