From 6f7a659f3f586d0da2886f9ee3bc9c0fbae23f6d Mon Sep 17 00:00:00 2001 From: younesdessia Date: Mon, 13 May 2024 11:20:24 +0200 Subject: [PATCH 1/4] Update/perf: generic plot --- dessia_common/core.py | 42 +++++++++++++++---------------------- dessia_common/decorators.py | 16 +++++++++++++- 2 files changed, 32 insertions(+), 26 deletions(-) diff --git a/dessia_common/core.py b/dessia_common/core.py index 97bd6ab6d..d25eb011b 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_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.") + 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..5f7b3d78f 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,20 @@ 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 + else: + return methods + + def plot_data_view(selector: str = None, load_by_default: bool = False): """ Decorator to plot data. From 5ffc5ce2c4a456634067d087c0a7cb3a3c7445d8 Mon Sep 17 00:00:00 2001 From: younesdessia Date: Mon, 13 May 2024 13:07:02 +0200 Subject: [PATCH 2/4] CI: remove else --- dessia_common/decorators.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/dessia_common/decorators.py b/dessia_common/decorators.py index 5f7b3d78f..a8d735946 100644 --- a/dessia_common/decorators.py +++ b/dessia_common/decorators.py @@ -50,8 +50,7 @@ def get_method_from_selector_name(class_, selector_name: str = ''): return [method] warnings.warn(f"The given selector name '{selector_name}' is not used in any method of your class.") return methods - else: - return methods + return methods def plot_data_view(selector: str = None, load_by_default: bool = False): From 038df2e396e30d0b89a98c03fe0a45bbab08f853 Mon Sep 17 00:00:00 2001 From: younesdessia Date: Mon, 13 May 2024 15:06:08 +0200 Subject: [PATCH 3/4] write changelog --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f3b72def..fbf88547f 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 ### Feature From 7b4b7eb9055a7ba9c60ca4b5ad4b2654c4a3b91a Mon Sep 17 00:00:00 2001 From: Younes BELABID <109952534+younesdessia@users.noreply.github.com> Date: Wed, 29 May 2024 09:27:22 +0200 Subject: [PATCH 4/4] Style: decorator name --- dessia_common/core.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dessia_common/core.py b/dessia_common/core.py index d25eb011b..53ef7a6dc 100644 --- a/dessia_common/core.py +++ b/dessia_common/core.py @@ -394,7 +394,7 @@ def plot(self, selector_name: str = ''): """ Generic plot getting plot_data function to plot. """ 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.") + warnings.warn("There are no methods in this class that contain a 'plot_data_view' decorator.") for method in methods: method(self).plot() @@ -403,7 +403,7 @@ def mpl_plot(self, selector_name: str = ''): axs = [] 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.") + 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'):