Skip to content

Commit

Permalink
issue #383 modifications to support multiple plot types in a single c…
Browse files Browse the repository at this point in the history
…onfig file and logging to replace print statements
  • Loading branch information
bikegeek committed Mar 7, 2024
1 parent b9e588c commit 4a704cd
Showing 1 changed file with 44 additions and 17 deletions.
61 changes: 44 additions & 17 deletions metplotpy/plots/tcmpr_plots/relperf/tcmpr_relperf.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,44 +1,56 @@
import os

from typing import Union
from datetime import datetime
import numpy as np
from pandas import DataFrame
import plotly.graph_objects as go


from metcalcpy.util import utils
from metplotpy.plots.series import Series
from metplotpy.plots.tcmpr_plots.tcmpr import Tcmpr
from metplotpy.plots.tcmpr_plots.tcmpr_series import TcmprSeries
from metplotpy.plots.tcmpr_plots.tcmpr_util import get_case_data
import metplotpy.plots.util as util



class TcmprRelPerf(Tcmpr):
def __init__(self, config_obj, column_info, col, case_data, input_df):
super().__init__(config_obj, column_info, col, case_data, input_df)
print("--------------------------------------------------------")
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, stat_name)

# Set up Logging
self.relperf_logger = util.get_common_logger(self.config_obj.log_level, self.config_obj.log_filename)
self.relperf_logger.info(f"--------------------------------------------------------")

if not self.config_obj.use_ee:
self.relpef_logger.error(f"Plotting RELPERF time series by {self.config_obj.series_val_names[0]}")
raise Exception("ERROR: Cannot plot relative performance when event equalization is disabled.")
print(f"Plotting RELPERF time series by {self.config_obj.series_val_names[0]}")

print("Plot HFIP Baseline:" + self.cur_baseline)
self.relperf_logger.info(f"Plotting RELPERF time series by {stat_name}")

self.relperf_logger.info("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)
if self.case_data is None:
self.relperf_logger.info(f"Getting case data for {stat_name}")
self.case_data = get_case_data(self.input_df, self.config_obj.series_vals_1, self.config_obj.indy_vals,
self.config_obj.rp_diff, len(self.series_list))

for series in self.series_list:
series.create_relperf_points(self.case_data)

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]}_relperf.png"
self.plot_filename = f"{self.config_obj.plot_dir}{os.path.sep}{stat_name}_relperf.png"
else:
self.plot_filename = f"{self.config_obj.plot_dir}{os.path.sep}{self.config_obj.prefix}.png"
# remove the old file if it exist
self.plot_filename = f"{self.config_obj.plot_dir}{os.path.sep}{self.config_obj.prefix}_{stat_name}_relperf.png"
# remove the old file if it exists
if os.path.exists(self.plot_filename):
os.remove(self.plot_filename)
self.user_legends = self._create_user_legends()
self._create_figure()
self.user_legends = self._create_user_legends(stat_name)
self._create_figure(stat_name)

def _create_user_legends(self):
def _create_user_legends(self, stat_name):
all_user_legends = self.config_obj.get_config_value('user_legend')
legend_list = []
series = self.config_obj.get_series_y_relperf(1)
Expand All @@ -52,7 +64,8 @@ def _create_user_legends(self):
legend_list.append(all_user_legends[idx])

# add to legend list legends for y2-axis series
num_series_y1 = len(self.config_obj.get_series_y(1))
# num_series_y1 = len(self.config_obj.get_series_y(1))
num_series_y1 = len(stat_name)

for idx, ser_components in enumerate(self.get_config_value('derived_series_1')):
# index of the legend
Expand All @@ -66,9 +79,10 @@ def _create_user_legends(self):

return self.config_obj.create_list_by_series_ordering(legend_list)

def _create_figure(self):
def _create_figure(self, stat_name):
""" Create a box plot from default and custom parameters"""

start_time = datetime.now()
self.figure = self._create_layout()
self._add_xaxis()
self._add_yaxis()
Expand Down Expand Up @@ -98,10 +112,12 @@ def _create_figure(self):
yaxis_min, yaxis_max = self.find_min_max(series, yaxis_min, yaxis_max)
self._draw_series(series, x_points_index)

series = TcmprSeries(self.config_obj, len(self.series_list), self.input_df, [], ['TIE'])
series = TcmprSeries(self.config_obj, len(self.series_list), self.input_df, [], ['TIE'], stat_name)
# Reset some series values. Series should be grouped by the plot type and not by the series_val
# series = self._setup_series(self.config_obj, len(self.series_list), self.input_df, [], ['TIE'], stat_name)
series.create_relperf_points(self.case_data)
yaxis_min, yaxis_max = self.find_min_max(series, yaxis_min, yaxis_max)
print(f'Range of {self.config_obj.list_stat_1[0]}: {yaxis_min}, {yaxis_max}')
self.relperf_logger.info(f'Range of {stat_name}: {yaxis_min}, {yaxis_max}')
tie_conf = {
'line_color': '#808080',
'name': 'TIE',
Expand Down Expand Up @@ -147,13 +163,19 @@ def _create_figure(self):
# add x2 axis
self._add_x2axis(list(range(0, len(self.config_obj.indy_vals))))

end_time = datetime.now()
total_time = end_time - start_time
self.relperf_logger.info(f"Took {total_time} milliseconds to create the relative performance figure")

def _draw_series(self, series: TcmprSeries, x_points_index_adj: list, tie_conf=None) -> None:
"""
Draws the boxes on the plot
:param series: Line series object with data and parameters
"""

start_time = datetime.now()

if tie_conf is None:
color = self.config_obj.colors_list[series.idx]
width = self.config_obj.linewidth_list[series.idx]
Expand Down Expand Up @@ -219,6 +241,10 @@ def _draw_series(self, series: TcmprSeries, x_points_index_adj: list, tie_conf=N
secondary_y=series.y_axis != 1
)

end_time = datetime.now()
total_time = end_time - start_time
self.relperf_logger.info(f"Took {total_time} milliseconds to draw the series")

def _adjust_titles(self):
if self.yaxis_1 is None or len(self.yaxis_1) == 0:
self.yaxis_1 = 'Percent of Cases'
Expand All @@ -230,3 +256,4 @@ def _adjust_titles(self):
if len(np.unique(self.config_obj.rp_diff)) == 1:
self.title = f"{self.title} Difference {self.config_obj.rp_diff[0]}{self.col['units']}"
self.title = f'{self.title} by {self.column_info[self.column_info["COLUMN"] == self.config_obj.series_val_names[0]]["DESCRIPTION"].tolist()[0]}'

0 comments on commit 4a704cd

Please sign in to comment.