Skip to content

Commit

Permalink
add new parameter quantities_names to the TimModel class
Browse files Browse the repository at this point in the history
  • Loading branch information
MAfarrag committed Jan 17, 2025
1 parent 3f21a2f commit 996661f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
41 changes: 38 additions & 3 deletions hydrolib/core/dflowfm/tim/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from pathlib import Path
from typing import Callable, Dict, List
from typing import Any, Callable, Dict, List, Optional

from pandas import DataFrame
from pydantic.v1 import Field
Expand Down Expand Up @@ -31,6 +31,8 @@ class TimModel(ParsableFileModel):
Header comments from the .tim file.
timeseries : List[TimRecord]
A list of TimRecord objects, each containing a time value and associated data.
quantities_names : Optional[List[str]]
List of names for the quantities in the timeseries.
Methods:
--------
Expand All @@ -44,6 +46,10 @@ class TimModel(ParsableFileModel):
Returns the parser callable for .tim files.
_validate_timeseries_values(cls, v: List[TimRecord]) -> List[TimRecord]
Validates the timeseries data.
as_dataframe(columns: List[Any] = None) -> DataFrame
Returns the timeseries as a pandas DataFrame.
_validate_quantities_names(cls, v, values) -> List[str]
Validates the quantities_names equals to the values or each record.
Args:
-----
Expand Down Expand Up @@ -106,6 +112,8 @@ class TimModel(ParsableFileModel):
timeseries: List[TimRecord] = Field(default_factory=list)
"""List[TimRecord]: A list containing the timeseries."""

quantities_names: Optional[List[str]] = Field(default=None)

@classmethod
def _ext(cls) -> str:
return ".tim"
Expand Down Expand Up @@ -171,9 +179,28 @@ def _raise_error_if_duplicate_time(timeseries: List[TimRecord]) -> None:
)
seen_times.add(timrecord.time)

def as_dataframe(self) -> DataFrame:
@validator("quantities_names")
def _validate_quantities_names(cls, v, values):
"""Validate if the amount of quantities_names match the amount of columns in the timeseries.
The validator compared the amount of quantities_names with the amount of columns in the first record of
the timeseries.
"""
if v is not None:
first_records_data = values["timeseries"][0].data
if len(v) != len(first_records_data):
raise ValueError(
f"The number of quantities_names ({len(v)}) must match the number of columns in the Tim file ({len(first_records_data)})."
)
return v

def as_dataframe(self, columns: List[Any] = None) -> DataFrame:
"""Return the timeseries as a pandas DataFrame.
Args:
columns (List[Any, str], optional, Defaults to None):
The column names for the DataFrame.
Returns:
DataFrame: The timeseries as a pandas DataFrame.
Expand All @@ -187,7 +214,15 @@ def as_dataframe(self) -> DataFrame:
10.0 1.232 2.343 3.454
20.0 4.565 5.676 6.787
30.0 1.500 2.600 3.700
To add column names to the DataFrame:
>>> df = tim_model.as_dataframe(columns=["Column1", "Column2", "Column3"])
>>> print(df)
Column1 Column2 Column3
10.0 1.232 2.343 3.454
20.0 4.565 5.676 6.787
30.0 1.500 2.600 3.700
"""
time_series = [record.data for record in self.timeseries]
index = [record.time for record in self.timeseries]
return DataFrame(time_series, index=index)
return DataFrame(time_series, index=index, columns=columns)
14 changes: 14 additions & 0 deletions tests/dflowfm/test_tim.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,20 @@ def test_as_dataframe(self):
]
assert df.loc[:, 0].to_list() == vals

df = model.as_dataframe(columns=["data"])
assert df.columns.to_list() == ["data"]

def test_with_quantities_names(self):
model = TimModel(
timeseries=self.single_data_for_timeseries_floats, quantities_names=["a"]
)
assert model.quantities_names == ["a"]
with pytest.raises(ValueError):
TimModel(
timeseries=self.single_data_for_timeseries_floats,
quantities_names=["a", "b"],
)

@pytest.mark.parametrize(
"input_data, reference_path",
[
Expand Down

0 comments on commit 996661f

Please sign in to comment.