-
Notifications
You must be signed in to change notification settings - Fork 75
Multi-version plotting & SMOKE_TEST #111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
a6bb56a
Add util for plotting examples
AVHopp 2c7da0d
Add settings for plotting in examples
AVHopp bbec5e6
Add SMOKE_TEST check
AVHopp 62ba0e9
Use create_plots utility for creating plots
AVHopp 032f7d6
Activate SMOKE_SCREEN in relevant tox environments
Scienfitz 7d98dbe
Include stubs for seaborn
AVHopp 4552359
Use paths and base_name for determining plot output location
AVHopp 6a8b95c
Shorten comments and descriptions
AVHopp 66e009d
Improve search for plotting_themes and include fallback scheme
AVHopp 8f53931
Exlcude utils/plotting.py from coverage
AVHopp fd2a463
Remove multiversion environment variable
AVHopp 50f0d9d
Rename function to create_example_plots
AVHopp 1327d90
Full rework of plotting utility
AVHopp 15343aa
Adjust to new signature of plotting utility
AVHopp 8fbc639
Move plotting_themes.json into examples folder
AVHopp 732f822
Enable plotting of dark, light and default plots in examples
AVHopp 467b544
Update CHANGELOG.md
AVHopp 7170a9f
Add static svg files
AVHopp 176e8f9
Add type hints to loaded json fields
AVHopp 5de7c42
Fix mypy errors
AVHopp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| [run] | ||
| omit = baybe/utils/plotting.py |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| """Plotting utilities.""" | ||
|
|
||
| import json | ||
| import os | ||
| import sys | ||
| import warnings | ||
| from pathlib import Path | ||
| from typing import Any, Dict, Tuple | ||
|
|
||
| import matplotlib.pyplot as plt | ||
| from matplotlib.axes import Axes | ||
| from matplotlib.figure import Figure | ||
|
|
||
|
|
||
| def create_example_plots( | ||
| ax: Axes, | ||
| path: Path, | ||
| base_name: str, | ||
| ) -> None: | ||
| """Create plots from an Axes object and save them as a svg file. | ||
|
|
||
|
AVHopp marked this conversation as resolved.
|
||
| The plots will be saved in the location specified by ``path``. | ||
| The attribute ``base_name`` is used to define the name of the outputs. | ||
|
|
||
| If the ``SMOKE_TEST`` variable is set, no plots are being created and this method | ||
| immediately returns. | ||
|
|
||
| The function attempts to read the predefined themes from ``plotting_themes.json``. | ||
| For each theme it finds, a file ``{base_name}_{theme}.svg`` is being created. | ||
| If the file cannot be found, if the JSON cannot be loaded or if the JSON is not well | ||
| configured, a fallback theme is used. | ||
|
|
||
| Args: | ||
| ax: The Axes object containing the figure that should be plotted. | ||
| path: The path to the directory in which the plots should be saved. | ||
| base_name: The base name that is used for naming the output files. | ||
| """ | ||
| # Check whether we immediately return due to just running a SMOKE_TEST | ||
| if "SMOKE_TEST" in os.environ: | ||
| return | ||
|
|
||
| # Define a fallback theme in case no configuration is found | ||
| fallback: Dict[str, Any] = { | ||
| "color": "black", | ||
| "figsize": (24, 8), | ||
| "fontsize": 22, | ||
| "framealpha": 0.3, | ||
| } | ||
|
|
||
| # Try to find the plotting themes by backtracking | ||
| # Get the absolute path of the current script | ||
| script_path = Path(sys.path[0]).resolve() | ||
| while ( | ||
| not Path(script_path / "plotting_themes.json").is_file() | ||
| and script_path != script_path.parent | ||
| ): | ||
| script_path = script_path.parent | ||
| if script_path == script_path.parent: | ||
| warnings.warn("No themes for plotting found. A fallback theme is used.") | ||
| themes = {"fallback": fallback} | ||
| else: | ||
| # Open the file containing all the themes | ||
| # If we reach this point, we know that the file exists, so we try to load it. | ||
| # If the file is no proper json, the fallback theme is used. | ||
| try: | ||
| themes = json.load(open(script_path / "plotting_themes.json")) | ||
| except json.JSONDecodeError: | ||
| warnings.warn( | ||
| "The JSON containing the themes could not be loaded." | ||
| "A fallback theme is used.", | ||
| UserWarning, | ||
| ) | ||
| themes = {"fallback": fallback} | ||
|
|
||
| for theme_name in themes: | ||
| # Get all of the values from the themes | ||
| # TODO This can probably be generalized and improved later on such that the | ||
| # keys fit the rc_params of matplotlib | ||
| # TODO We might want to add a generalization here | ||
| necessary_keys = ("color", "figsize", "fontsize", "framealpha") | ||
| if not all(key in themes[theme_name] for key in necessary_keys): | ||
| warnings.warn( | ||
| "Provided theme does not contain the necessary keys." | ||
| "Using a fallback theme instead.", | ||
| UserWarning, | ||
| ) | ||
| current_theme = fallback | ||
| else: | ||
| current_theme = themes[theme_name] | ||
| color: str = current_theme["color"] | ||
| figsize: Tuple[int, int] = current_theme["figsize"] | ||
| fontsize: int = current_theme["fontsize"] | ||
| framealpha: float = current_theme["framealpha"] | ||
|
|
||
| # Adjust the axes of the plot | ||
| for key in ax.spines.keys(): | ||
| ax.spines[key].set_color(color) | ||
| ax.xaxis.label.set_color(color) | ||
| ax.xaxis.label.set_fontsize(fontsize) | ||
| ax.yaxis.label.set_color(color) | ||
| ax.yaxis.label.set_fontsize(fontsize) | ||
|
|
||
| # Adjust the size of the ax | ||
| # mypy thinks that ax.figure might become None, hence the explicit ignore | ||
| if isinstance(ax.figure, Figure): | ||
| ax.figure.set_size_inches(*figsize) | ||
| else: | ||
| warnings.warn("Could not adjust size of plot due to it not being a Figure.") | ||
|
|
||
| # Adjust the labels | ||
| for label in ax.get_xticklabels() + ax.get_yticklabels(): | ||
| label.set_color(color) | ||
| label.set_fontsize(fontsize) | ||
|
|
||
| # Adjust the legend | ||
| legend = ax.get_legend() | ||
| legend.get_frame().set_alpha(framealpha) | ||
| legend.get_title().set_color(color) | ||
| legend.get_title().set_fontsize(fontsize) | ||
| for text in legend.get_texts(): | ||
| text.set_fontsize(fontsize) | ||
| text.set_color(color) | ||
|
|
||
| output_path = Path(path, f"{base_name}_{theme_name}.svg") | ||
| # mypy thinks that ax.figure might become None, hence the explicit ignore | ||
| if isinstance(ax.figure, Figure): | ||
| ax.figure.savefig( | ||
| output_path, | ||
| format="svg", | ||
| transparent=True, | ||
| ) | ||
| else: | ||
| warnings.warn("Plots could not be saved.") | ||
| plt.close() | ||
|
AVHopp marked this conversation as resolved.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.