From a3ecf5994018c4ab9e9f1f2a1b70f93bb6ee892c Mon Sep 17 00:00:00 2001 From: belthlemar Date: Wed, 8 Jan 2025 15:54:31 +0100 Subject: [PATCH] fix(matrix): return default matrix when it's empty --- .../filesystem/matrix/input_series_matrix.py | 4 +++ .../model/filesystem/matrix/matrix.py | 26 ++++++++++++++++--- .../filesystem/matrix/test_matrix_node.py | 5 ++++ 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/antarest/study/storage/rawstudy/model/filesystem/matrix/input_series_matrix.py b/antarest/study/storage/rawstudy/model/filesystem/matrix/input_series_matrix.py index d2ac83c19c..e23cb9842a 100644 --- a/antarest/study/storage/rawstudy/model/filesystem/matrix/input_series_matrix.py +++ b/antarest/study/storage/rawstudy/model/filesystem/matrix/input_series_matrix.py @@ -150,3 +150,7 @@ def get_file_content(self) -> OriginalFile: else: content = self.config.path.read_bytes() return OriginalFile(content=content, suffix=suffix, filename=filename) + + @override + def get_default_empty_matrix(self) -> t.Optional[npt.NDArray[np.float64]]: + return self.default_empty diff --git a/antarest/study/storage/rawstudy/model/filesystem/matrix/matrix.py b/antarest/study/storage/rawstudy/model/filesystem/matrix/matrix.py index 7897715f92..3b28f36600 100644 --- a/antarest/study/storage/rawstudy/model/filesystem/matrix/matrix.py +++ b/antarest/study/storage/rawstudy/model/filesystem/matrix/matrix.py @@ -16,7 +16,9 @@ from pathlib import Path from typing import List, Optional, Union, cast +import numpy as np import pandas as pd +from numpy import typing as npt from typing_extensions import override from antarest.core.model import JSON @@ -127,16 +129,25 @@ def load( formatted: bool = True, ) -> Union[bytes, JSON]: file_path, tmp_dir = self._get_real_file_path() - if not formatted: - if file_path.exists(): - return file_path.read_bytes() + if formatted: + return self.parse_as_json(file_path) + + if not file_path.exists(): logger.warning(f"Missing file {self.config.path}") if tmp_dir: tmp_dir.cleanup() return b"" - return self.parse_as_json(file_path) + file_content = file_path.read_bytes() + if file_content != b"": + return file_content + + # here we should return the default matrix + default_matrix = self.get_default_empty_matrix() + if not default_matrix: + return b"" + return default_matrix.tobytes() @abstractmethod def parse_as_json(self, file_path: Optional[Path] = None) -> JSON: @@ -145,6 +156,13 @@ def parse_as_json(self, file_path: Optional[Path] = None) -> JSON: """ raise NotImplementedError() + @abstractmethod + def get_default_empty_matrix(self) -> Optional[npt.NDArray[np.float64]]: + """ + Returns the default matrix to return when the existing one is empty + """ + raise NotImplementedError() + @override def dump( self, diff --git a/tests/storage/repository/filesystem/matrix/test_matrix_node.py b/tests/storage/repository/filesystem/matrix/test_matrix_node.py index 38dce45486..936a54b6ea 100644 --- a/tests/storage/repository/filesystem/matrix/test_matrix_node.py +++ b/tests/storage/repository/filesystem/matrix/test_matrix_node.py @@ -14,7 +14,9 @@ from typing import List, Optional from unittest.mock import Mock +import numpy as np import pandas as pd # type: ignore +from numpy import typing as npt from antarest.core.model import JSON from antarest.study.storage.rawstudy.model.filesystem.config.model import FileStudyTreeConfig @@ -41,6 +43,9 @@ def __init__(self, context: ContextServer, config: FileStudyTreeConfig) -> None: def parse_as_json(self, file_path: Optional[Path] = None) -> JSON: return MOCK_MATRIX_JSON + def get_default_empty_matrix(self) -> Optional[npt.NDArray[np.float64]]: + pass + def check_errors(self, data: str, url: Optional[List[str]] = None, raising: bool = False) -> List[str]: pass # not used