Skip to content

Commit

Permalink
fix(matrix): return default matrix when it's empty
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinBelthle committed Jan 8, 2025
1 parent 345b0a7 commit a3ecf59
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
26 changes: 22 additions & 4 deletions antarest/study/storage/rawstudy/model/filesystem/matrix/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down

0 comments on commit a3ecf59

Please sign in to comment.