From c7d8028a8b756bc34ed0121ee1eac1917ee1a926 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sim=C3=A3o=20Rodrigues?= Date: Mon, 13 Jan 2025 18:29:13 +0100 Subject: [PATCH] fix ruff errors, add pre-commit-config.yaml --- .pre-commit-config.yaml | 55 +++++++++++++++++++ .../models/autodifferentiated_discipline.py | 14 ++--- .../models/differentiable_model.py | 3 +- .../tools/discipline_tester.py | 5 +- 4 files changed, 66 insertions(+), 11 deletions(-) create mode 100644 .pre-commit-config.yaml diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..95cc460 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,55 @@ +repos: + - repo: https://github.com/b4pm-devops/sostrades-pre-commit.git + rev: v1.1.2 + hooks: + - id: update-headers + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.5.0 + hooks: + - id: ruff + args: [ + --fix, + --preview, + --exit-non-zero-on-fix, + --config=ruff.toml, + ] + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.5.0 + hooks: + - id: trailing-whitespace + - id: end-of-file-fixer + exclude: LICENSES/headers + - id: check-yaml + # !reference is specific to gitlab + # !! prefix is specific to mkdocs + exclude: \.gitlab-ci.yml|mkdocs.yml + - id: check-added-large-files + - id: check-json + - id: pretty-format-json + args: [ + --autofix, + --no-sort-keys, + ] + exclude: \.ipynb + - id: check-toml + - id: destroyed-symlinks + - id: check-symlinks + - repo: https://github.com/pre-commit/pygrep-hooks + rev: v1.10.0 + hooks: + - id: rst-backticks + - id: rst-directive-colons + - id: rst-inline-touching-normal + - repo: https://github.com/kynan/nbstripout + rev: 0.7.1 + hooks: + - id: nbstripout + - repo: https://github.com/igorshubovych/markdownlint-cli + rev: v0.39.0 + hooks: + - id: markdownlint + args: [ + --fix, + --disable, + MD024, + ] diff --git a/sostrades_optimization_plugins/models/autodifferentiated_discipline.py b/sostrades_optimization_plugins/models/autodifferentiated_discipline.py index a2d505d..8d7690f 100644 --- a/sostrades_optimization_plugins/models/autodifferentiated_discipline.py +++ b/sostrades_optimization_plugins/models/autodifferentiated_discipline.py @@ -13,14 +13,16 @@ See the License for the specific language governing permissions and limitations under the License. ''' -import numpy as np import logging from typing import TYPE_CHECKING, Union +import numpy as np from sostrades_core.execution_engine.sos_wrapp import SoSWrapp if TYPE_CHECKING: - from sostrades_optimization_plugins.models.differentiable_model import DifferentiableModel + from sostrades_optimization_plugins.models.differentiable_model import ( + DifferentiableModel, + ) class AutodifferentiedDisc(SoSWrapp): @@ -48,7 +50,6 @@ def get_non_numerical_inputs(self): inputs = self.get_sosdisc_inputs() return {key: value for key, value in inputs.items() if value is not None} - def compute_sos_jacobian(self): """ Compute jacobian for each coupling variable @@ -68,13 +69,12 @@ def compute_sos_jacobian(self): for c_i_df in coupling_dataframe_input: all_inputs_model_path.extend(self.model.get_df_input_dotpaths(df_inputname=c_i_df)) - all_inputs_model_path = list(filter(lambda x: not x.endswith(f":years"), all_inputs_model_path)) + all_inputs_model_path = list(filter(lambda x: not x.endswith(":years"), all_inputs_model_path)) all_outputs_model_path = other_coupling_outputs for c_o_df in coupling_dataframe_output: all_outputs_model_path.extend(self.model.get_df_output_dotpaths(df_outputname=c_o_df)) - all_outputs_model_path = list(filter(lambda x: not x.endswith(f":years"), all_outputs_model_path)) - + all_outputs_model_path = list(filter(lambda x: not x.endswith(":years"), all_outputs_model_path)) def handle_gradients_wrt_inputs(output_path: str, gradients: dict): arg_output = (output_path,) @@ -89,12 +89,10 @@ def handle_gradients_wrt_inputs(output_path: str, gradients: dict): grad_input_value = np.array([[grad_input_value]]) self.set_partial_derivative_for_other_types(arg_output, arg_input, grad_input_value) - for output_path in all_outputs_model_path: gradients = self.model.compute_partial(output_name=output_path, input_names=all_inputs_model_path) handle_gradients_wrt_inputs(output_path=output_path, gradients=gradients) - def _auto_configure_jacobian_variables(self): self.coupling_inputs = [] all_inputs_dict = {**self.DESC_IN, **self.inst_desc_in} diff --git a/sostrades_optimization_plugins/models/differentiable_model.py b/sostrades_optimization_plugins/models/differentiable_model.py index 558a79c..803d267 100644 --- a/sostrades_optimization_plugins/models/differentiable_model.py +++ b/sostrades_optimization_plugins/models/differentiable_model.py @@ -17,7 +17,7 @@ from collections import defaultdict from copy import deepcopy -from typing import Callable, Union, Any +from typing import Any, Callable, Union try: import jax @@ -32,7 +32,6 @@ import numpy as np import numpy.typing as npt import pandas as pd -from climateeconomics.glossarycore import GlossaryCore ArrayLike = Union[list[float], npt.NDArray[np.float64]] InputType = Union[float, int, ArrayLike, pd.DataFrame] diff --git a/sostrades_optimization_plugins/tools/discipline_tester.py b/sostrades_optimization_plugins/tools/discipline_tester.py index 1d6b740..8529715 100644 --- a/sostrades_optimization_plugins/tools/discipline_tester.py +++ b/sostrades_optimization_plugins/tools/discipline_tester.py @@ -20,7 +20,10 @@ from sostrades_core.tests.core.abstract_jacobian_unit_test import ( AbstractJacobianUnittest, ) -from sostrades_optimization_plugins.models.autodifferentiated_discipline import AutodifferentiedDisc + +from sostrades_optimization_plugins.models.autodifferentiated_discipline import ( + AutodifferentiedDisc, +) def discipline_test_function(module_path: str, name: str, model_name: str,