Skip to content
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

Update/perf: generic plot/mpl_plot #692

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 0.17.1

### Feature

- Plot_data : Update generic plot and mpl_plot


## 0.17.0

### Feature
Expand Down
42 changes: 17 additions & 25 deletions dessia_common/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
import dessia_common.utils.helpers as dch
import dessia_common.files as dcf
from dessia_common.document_generator import DocxWriter
from dessia_common.decorators import get_decorated_methods, DISPLAY_DECORATORS, EXPORT_DECORATORS
from dessia_common.decorators import get_decorated_methods, DISPLAY_DECORATORS, EXPORT_DECORATORS,\
get_method_from_selector_name
from dessia_common.excel_reader import ExcelReader


Expand Down Expand Up @@ -389,34 +390,25 @@ def plot_data(self, reference_path: str = "#", **kwargs):
"anymore. Please use Display Decorators, instead", DeprecationWarning)
return []

def plot(self, reference_path: str = "#", **kwargs):
def plot(self, selector_name: str = ''):
""" Generic plot getting plot_data function to plot. """
if hasattr(self, 'plot_data'):
import plot_data
for data in self.plot_data(reference_path, **kwargs):
plot_data.plot_canvas(plot_data_object=data,
canvas_id='canvas',
width=1400, height=900,
debug_mode=False)
else:
msg = f"Class '{self.__class__.__name__}' does not implement a plot_data method to define what to plot"
raise NotImplementedError(msg)
methods = get_method_from_selector_name(class_=self.__class__, selector_name=selector_name)
if not methods:
warnings.warn("There are no methods in this class that contain a 'plot_date_view' decorator.")
for method in methods:
method(self).plot()

def mpl_plot(self, **kwargs):
def mpl_plot(self, selector_name: str = ''):
""" Plot with matplotlib using plot_data function. """
axs = []
if hasattr(self, 'plot_data'):
try:
plot_datas = self.plot_data(**kwargs)
except TypeError as error:
raise TypeError(f'{self.__class__.__name__}.{error}') from error
for data in plot_datas:
if hasattr(data, 'mpl_plot'):
ax = data.mpl_plot()
axs.append(ax)
else:
msg = f"Class '{self.__class__.__name__}' does not implement a plot_data method to define what to plot"
raise NotImplementedError(msg)
methods = get_method_from_selector_name(class_=self.__class__, selector_name=selector_name)
if not methods:
warnings.warn("There are no methods in this class that contain a 'plot_date_view' decorator.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you wanna say 'plot_data_view' ?

Copy link
Contributor Author

@younesdessia younesdessia May 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, I correct that, thank's

for method in methods:
data = method(self)
if hasattr(data, 'mpl_plot'):
ax = data.mpl_plot()
axs.append(ax)
return axs

@classmethod
Expand Down
15 changes: 14 additions & 1 deletion dessia_common/decorators.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
""" Provides decorators that work as 'flags' for display settings. """

import warnings
from typing import Type, List, TypeVar
import inspect
import ast
Expand Down Expand Up @@ -40,6 +40,19 @@ def get_decorated_methods(class_: Type, decorator_name: str):
return [getattr(class_, n) for n in method_names]


def get_method_from_selector_name(class_, selector_name: str = ''):
""" Get decorated methods with a specific selector name from a class. """
methods = get_decorated_methods(class_=class_, decorator_name='plot_data_view')
if selector_name:
for method in methods:
selector = getattr(method, "selector", None)
if selector == selector_name:
return [method]
warnings.warn(f"The given selector name '{selector_name}' is not used in any method of your class.")
return methods
return methods


def plot_data_view(selector: str = None, load_by_default: bool = False):
"""
Decorator to plot data.
Expand Down