Commit 294e9904 authored by Huste, Tobias (FWCC) - 111645's avatar Huste, Tobias (FWCC) - 111645 Committed by Erxleben, Fredo
Browse files

Resolve "Allow to configure the default plot style via configuration"

parent edf86573
Pipeline #113446 passed with stages
in 3 minutes and 20 seconds
......@@ -35,6 +35,12 @@ Group your changes into these categories:
[List of commits](https://gitlab.hzdr.de/hifis/overall/surveys/hifis-surveyval/-/compare/v1.4.0...main)
### Added
- Allow to configure the default plot style via configuration
([!157](https://gitlab.hzdr.de/hifis/overall/surveys/hifis-surveyval/-/merge_requests/157)
by [frust45](https://gitlab.hzdr.de/frust45)).
### Fixed
- Fix custom plot style template generation on init
......
......@@ -216,6 +216,7 @@ OUTPUT_FORMAT: SCREEN
PREPROCESSING_FILENAME: preprocess.py
SCRIPT_FOLDER: scripts
SCRIPT_NAMES: []
CUSTOM_PLOT_STYLE: "report_style" # Optional
```
> ##### Concepts that You Need to Know
......
......@@ -30,7 +30,7 @@ It provides:
import logging
from datetime import datetime
from pathlib import Path
from typing import Any, Dict, List, Set
from typing import Any, Dict, List, Optional, Set
import yaml
from pydantic import BaseSettings, validator
......@@ -204,6 +204,11 @@ class FileSettings(BaseSettings):
# and Question ID.
DATA_ID_SEPARATOR: str = "_"
# Specify a custom plot style globally for all scripts.
# A plot style which is explicitly specified in scripts
# take precedence over this option.
CUSTOM_PLOT_STYLE: Optional[str] = "" # Default value None gives an error
class Config:
"""
Subclass for specification.
......
......@@ -52,8 +52,7 @@ class HIFISSurveyval:
# register plotter
self.plotter: MatplotlibPlotter = MatplotlibPlotter(
output_format=self.settings.OUTPUT_FORMAT,
output_path=self.settings.ANALYSIS_OUTPUT_PATH,
settings=self.settings,
)
# register printer
......
......@@ -63,7 +63,7 @@ class MatplotlibPlotter(Plotter):
generation of a file name from the date of the run and the
module producing the image.
"""
if self.OUTPUT_FORMAT == SupportedOutputFormat.SCREEN:
if self.settings.OUTPUT_FORMAT == SupportedOutputFormat.SCREEN:
pyplot.show()
pyplot.close()
return
......@@ -83,10 +83,10 @@ class MatplotlibPlotter(Plotter):
output_file_stem: str = f"{calling_module_name}"
file_ending: str = self.OUTPUT_FORMAT.name.lower()
file_ending: str = self.settings.OUTPUT_FORMAT.name.lower()
file_name: str = f"{output_file_stem}.{file_ending}"
output_path: Path = self.ANALYSIS_OUTPUT_PATH / file_name
output_path: Path = self.settings.ANALYSIS_OUTPUT_PATH / file_name
if output_path.exists():
logging.warning(f"Overriding existing output file {output_path}")
......@@ -128,8 +128,7 @@ class MatplotlibPlotter(Plotter):
if figure_size and len(figure_size) == 2:
MatplotlibPlotter._set_figure_size(figure_size[0], figure_size[1])
@classmethod
def _set_custom_plot_style(cls, plot_style_name: str) -> None:
def _set_custom_plot_style(self, plot_style_name: str) -> None:
"""
Set Matplotlib custom plot style.
......@@ -141,6 +140,11 @@ class MatplotlibPlotter(Plotter):
custom_plot_styles_path: Path = Path('custom_plot_styles')
file_ending: str = '.mplstyle'
# Use the setting provided from the config file if not specified
# via kwargs for the custom plot style
if not plot_style_name:
plot_style_name = self.settings.CUSTOM_PLOT_STYLE
if len(plot_style_name) > 0:
custom_style_file_path: Path = \
custom_plot_styles_path / Path(plot_style_name + file_ending)
......@@ -241,8 +245,7 @@ class MatplotlibPlotter(Plotter):
Object to initialize the parameters to create the color map
for value labels of bars.
"""
MatplotlibPlotter._set_custom_plot_style(
kwargs.get("plot_style_name", ""))
self._set_custom_plot_style(kwargs.get("plot_style_name", ""))
rcParams.update({"figure.autolayout": True})
......@@ -585,8 +588,7 @@ class MatplotlibPlotter(Plotter):
box_face_color (str):
Name of face color of boxes. (Default: "wheat")
"""
MatplotlibPlotter._set_custom_plot_style(
kwargs.get("plot_style_name", ""))
self._set_custom_plot_style(kwargs.get("plot_style_name", ""))
rcParams.update({"figure.autolayout": True})
x_rotation: int = kwargs.get("x_label_rotation", 0)
......@@ -702,8 +704,7 @@ class MatplotlibPlotter(Plotter):
add_value_label_box (bool):
Flag specifies whether to add value label boxes.
"""
MatplotlibPlotter._set_custom_plot_style(
kwargs.get("plot_style_name", ""))
self._set_custom_plot_style(kwargs.get("plot_style_name", ""))
rcParams.update({"figure.autolayout": True})
......
......@@ -26,24 +26,24 @@ This module provides a framework for plotters.
"""
from abc import ABC
from pathlib import Path
from hifis_surveyval.plotting.supported_output_format import \
SupportedOutputFormat
from hifis_surveyval.core.settings import Settings
class Plotter(ABC):
"""Base class to derive plotters from."""
def __init__(
self, output_format: SupportedOutputFormat, output_path: Path
self,
settings: Settings,
) -> None:
"""
Initialize a plotter.
Args:
output_format (SupportedOutputFormat): Supported output format.
output_path (Path): Path to the output folder.
settings: (Settings):
A Settings container to store the setup configuration
in. It will be populated with the related settings during the
initialization of the HIFISSurveyval object.
"""
self.OUTPUT_FORMAT: SupportedOutputFormat = output_format
self.ANALYSIS_OUTPUT_PATH: Path = output_path
self.settings = settings
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment