diff --git a/CHANGELOG.md b/CHANGELOG.md index 461c02592..f515c1089 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 ### Added diff --git a/dessia_common/core.py b/dessia_common/core.py index 97bd6ab6d..53ef7a6dc 100644 --- a/dessia_common/core.py +++ b/dessia_common/core.py @@ -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 @@ -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_data_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_data_view' decorator.") + for method in methods: + data = method(self) + if hasattr(data, 'mpl_plot'): + ax = data.mpl_plot() + axs.append(ax) return axs @classmethod diff --git a/dessia_common/decorators.py b/dessia_common/decorators.py index c4a4901f0..a8d735946 100644 --- a/dessia_common/decorators.py +++ b/dessia_common/decorators.py @@ -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 @@ -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.