Skip to content

Commit d1e33ea

Browse files
authored
Merge branch 'main' into release/v1.0.0
2 parents 52fa23c + f2c90ab commit d1e33ea

File tree

2 files changed

+66
-7
lines changed

2 files changed

+66
-7
lines changed

src/infrasys/arrow_storage.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,38 @@ def _get_time_series_dispatch(
170170
msg = f"Bug: need to implement get_time_series for {type(metadata)}"
171171
raise NotImplementedError(msg)
172172

173-
@_get_time_series_dispatch.register(SingleTimeSeriesMetadata)
174-
def _(
173+
def remove_time_series(self, metadata: TimeSeriesMetadata, connection: Any = None) -> None:
174+
fpath = self._ts_directory.joinpath(f"{metadata.time_series_uuid}{EXTENSION}")
175+
if not fpath.exists():
176+
msg = f"No time series with {metadata.time_series_uuid} is stored"
177+
raise ISNotStored(msg)
178+
fpath.unlink()
179+
180+
def serialize(
181+
self, data: dict[str, Any], dst: Path | str, src: Path | str | None = None
182+
) -> None:
183+
# From the shutil documentation: the copying operation will continue if
184+
# it encounters existing directories, and files within the dst tree
185+
# will be overwritten by corresponding files from the src tree.
186+
if src is None:
187+
src = self._ts_directory
188+
src_path = Path(src)
189+
dst_path = Path(dst)
190+
# Note that src could be read-only. Don't copy it's permissions.
191+
for path in src_path.iterdir():
192+
if path.is_file():
193+
if path.suffix == ".arrow":
194+
shutil.copyfile(path, dst_path / path.name)
195+
else:
196+
shutil.copytree(src, dst_path / path.name, dirs_exist_ok=True)
197+
self.add_serialized_data(data)
198+
logger.info("Copied time series data to {}", dst)
199+
200+
@staticmethod
201+
def add_serialized_data(data: dict[str, Any]) -> None:
202+
data["time_series_storage_type"] = TimeSeriesStorageType.ARROW.value
203+
204+
def _get_single_time_series(
175205
self,
176206
metadata: SingleTimeSeriesMetadata,
177207
start_time: datetime | None = None,

tests/test_serialization.py

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from pydantic import WithJsonSchema
1414
from typing_extensions import Annotated
1515

16-
from infrasys import Location, NonSequentialTimeSeries, SingleTimeSeries
16+
from infrasys import Location, SingleTimeSeries, NonSequentialTimeSeries, System
1717
from infrasys.component import Component
1818
from infrasys.exceptions import ISInvalidParameter, ISOperationNotAllowed
1919
from infrasys.normalization import NormalizationMax
@@ -107,14 +107,42 @@ def test_serialize_single_time_series(tmp_path, time_series_storage_type):
107107

108108
variable_name = "active_power"
109109
length = 8784
110-
df = range(length)
110+
data = range(length)
111111
start = datetime(year=2020, month=1, day=1)
112112
resolution = timedelta(hours=1)
113-
ts = SingleTimeSeries.from_array(df, variable_name, start, resolution)
113+
ts = SingleTimeSeries.from_array(data, variable_name, start, resolution)
114114
system.add_time_series(ts, gen1, gen2, scenario="high", model_year="2030")
115115
filename = tmp_path / "system.json"
116116
system.to_json(filename)
117-
check_deserialize_with_read_write_time_series(filename)
117+
system2 = check_deserialize_with_read_write_time_series(filename)
118+
gen1b = system2.get_component(SimpleGenerator, gen1.name)
119+
gen2b = system2.get_component(SimpleGenerator, gen2.name)
120+
data2 = range(1, length + 1)
121+
ts2 = SingleTimeSeries.from_array(data2, variable_name, start, resolution)
122+
system2.add_time_series(ts2, gen1b, gen2b, scenario="low", model_year="2030")
123+
filename2 = tmp_path / "system2.json"
124+
system2.to_json(filename2)
125+
system3 = SimpleSystem.from_json(filename2)
126+
assert np.array_equal(
127+
system3.get_time_series(
128+
gen1b,
129+
time_series_type=SingleTimeSeries,
130+
variable_name=variable_name,
131+
scenario="low",
132+
model_year="2030",
133+
).data,
134+
data2,
135+
)
136+
assert np.array_equal(
137+
system3.get_time_series(
138+
gen2b,
139+
time_series_type=SingleTimeSeries,
140+
variable_name=variable_name,
141+
scenario="low",
142+
model_year="2030",
143+
).data,
144+
data2,
145+
)
118146
check_deserialize_with_read_only_time_series(
119147
filename, gen1.name, gen2.name, variable_name, ts.data
120148
)
@@ -175,13 +203,14 @@ def test_serialize_nonsequential_time_series(tmp_path, time_series_storage_type)
175203
)
176204

177205

178-
def check_deserialize_with_read_write_time_series(filename):
206+
def check_deserialize_with_read_write_time_series(filename) -> System:
179207
system3 = SimpleSystem.from_json(filename, time_series_read_only=False)
180208
assert system3.get_time_series_directory() != SimpleSystem._make_time_series_directory(
181209
filename
182210
)
183211
system3_ts_dir = system3.get_time_series_directory()
184212
assert system3_ts_dir is not None
213+
return system3
185214

186215

187216
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)