Skip to content

Commit

Permalink
add a special method to initialise custom lines configuration #140
Browse files Browse the repository at this point in the history
  • Loading branch information
TatianaBurek committed Feb 10, 2022
1 parent 9143218 commit 6c720bb
Showing 1 changed file with 55 additions and 12 deletions.
67 changes: 55 additions & 12 deletions metplotpy/plots/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ def __init__(self, parameters):
# Enclose legend labels in a box
self.draw_box = True



# These are the inner keys to the series_val setting, and
# they represent the series variables of
# interest. The keys correspond to the column names
Expand All @@ -110,7 +108,7 @@ def __init__(self, parameters):
self.series_val_names = self._get_series_val_names()
self.series_ordering = None
self.indy_plot_val = self.get_config_value('indy_plot_val')
self.lines = self.get_config_value('lines')
self.lines = self._get_lines()

def get_config_value(self, *args):
"""Gets the value of a configuration parameter.
Expand Down Expand Up @@ -174,10 +172,10 @@ def _get_legend_style(self):
legend_bbox_y = legend_inset['y']
legend_size = self.get_config_value('legend_size')
legend_settings = dict(bbox_x=legend_bbox_x,
bbox_y=legend_bbox_y,
legend_size=legend_size,
legend_ncol=legend_ncol,
legend_box=legend_box)
bbox_y=legend_bbox_y,
legend_size=legend_size,
legend_ncol=legend_ncol,
legend_box=legend_box)
else:
legend_settings = dict()

Expand Down Expand Up @@ -372,7 +370,6 @@ def _get_linestyles(self):
linestyle_list_ordered = self.create_list_by_series_ordering(list(linestyles))
return linestyle_list_ordered


def _get_user_legends(self, legend_label_type):
"""
Retrieve the text that is to be displayed in the legend at the bottom of the plot.
Expand All @@ -399,7 +396,6 @@ def _get_user_legends(self, legend_label_type):
# - CONUS
# The constructed legend label will be "NoahMPv3.5.1_d01 CONUS Performance"


# Check for empty list as setting in the config file
legends_list = []

Expand Down Expand Up @@ -433,7 +429,7 @@ def _get_user_legends(self, legend_label_type):
return [legend_label_type]

perms = utils.create_permutations(series_list)
for idx,ll in enumerate(legends_list):
for idx, ll in enumerate(legends_list):
if ll == ' ':
if len(series_list) > 1:
label_parts = [perms[idx][0], ' ', perms[idx][1], ' ', legend_label_type]
Expand Down Expand Up @@ -582,7 +578,6 @@ def create_list_by_plot_val_ordering(self, setting_to_order):

return ordered_settings_list


def calculate_plot_dimension(self, config_value, output_units):
'''
To calculate the width or height that defines the size of the plot.
Expand Down Expand Up @@ -655,4 +650,52 @@ def _get_indy_label(self):
return self.get_config_value('indy_label')
return self.indy_vals


def _get_lines(self) -> Union[list, None]:
"""
Initialises the custom lines properties and returns a validated list
Args:
Returns:
:return: list of lines properties or None
"""

# get property value from the parameters
lines = self.get_config_value('lines')

# if the property exists - proceed
if lines is not None:
# validate data and replace the values
for line in lines:

# validate line_type
line_type = line['type']
if line_type not in ('horiz_line', 'vert_line') :
print(f'WARNING: custom line type {line["type"]} is not supported')
line['type'] = None
else:
# convert position to float if line_type=horiz_line
if line['type'] == 'horiz_line':
try:
line['position'] = float(line['position'])
except ValueError:
print(f'WARNING: custom line position {line["position"]} is invalid')
line['type'] = None
else:
# convert position to string if line_type=vert_line
line['position'] = str(line['position'])

# convert line_style
line_style = line['line_style']
if line_style in constants.LINE_STYLE_TO_PLOTLY_DASH.keys():
line['line_style'] = constants.LINE_STYLE_TO_PLOTLY_DASH[line_style]
else:
line['line_style'] = None

# convert line_width to float
try:
line['line_width'] = float(line['line_width'])
except ValueError:
print(f'WARNING: custom line width {line["line_width"]} is invalid')
line['type'] = None

return lines

0 comments on commit 6c720bb

Please sign in to comment.