-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathzebra_data_module.py
More file actions
162 lines (145 loc) · 5.49 KB
/
zebra_data_module.py
File metadata and controls
162 lines (145 loc) · 5.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import logging
from collections import defaultdict
from functools import cached_property
from pathlib import Path
from lightning import LightningDataModule
from omegaconf import DictConfig
from torch.utils.data import DataLoader
from ice_station_zebra.types import ArrayTCHW, DataloaderArgs, DataSpace
from .combined_dataset import CombinedDataset
from .zebra_dataset import ZebraDataset
logger = logging.getLogger(__name__)
class ZebraDataModule(LightningDataModule):
def __init__(self, config: DictConfig) -> None:
super().__init__()
# Load paths
self.base_path = Path(config["base_path"])
# Construct dataset groups
self.dataset_groups = defaultdict(list)
for dataset in config["datasets"].values():
self.dataset_groups[dataset["group_as"]].append(
(
self.base_path / "data" / "anemoi" / f"{dataset['name']}.zarr"
).resolve()
)
logger.info(f"Found {len(self.dataset_groups)} dataset_groups")
for dataset_group in self.dataset_groups.keys():
logger.debug(f"... {dataset_group}")
# Check prediction target
self.predict_target = config["predict"]["dataset_group"]
if self.predict_target not in self.dataset_groups:
raise ValueError(f"Could not find prediction target {self.predict_target}")
# Set periods for train, validation, and test
self.batch_size = int(config["split"]["batch_size"])
self.train_period = {
k: None if v == "None" else v for k, v in config["split"]["train"].items()
}
self.val_period = {
k: None if v == "None" else v
for k, v in config["split"]["validate"].items()
}
self.test_period = {
k: None if v == "None" else v for k, v in config["split"]["test"].items()
}
# Set history and forecast steps
self.n_forecast_steps = int(config["predict"].get("n_forecast_steps", 1))
self.n_history_steps = int(config["predict"].get("n_history_steps", 1))
# Set common arguments for the dataloader
self._common_dataloader_kwargs = DataloaderArgs(
batch_size=self.batch_size,
sampler=None,
batch_sampler=None,
drop_last=False,
worker_init_fn=None,
)
@cached_property
def input_spaces(self) -> list[DataSpace]:
"""Return the data space for each input"""
return [
ZebraDataset(name, paths).space
for name, paths in self.dataset_groups.items()
if name != self.predict_target
]
@cached_property
def output_space(self) -> DataSpace:
"""Return the data space of the desired output"""
return next(
ZebraDataset(name, paths).space
for name, paths in self.dataset_groups.items()
if name == self.predict_target
)
def train_dataloader(
self,
) -> DataLoader[dict[str, ArrayTCHW]]:
"""Construct train dataloader"""
dataset = CombinedDataset(
[
ZebraDataset(
name,
paths,
start=self.train_period["start"],
end=self.train_period["end"],
)
for name, paths in self.dataset_groups.items()
],
n_forecast_steps=self.n_forecast_steps,
n_history_steps=self.n_history_steps,
target=self.predict_target,
)
logger.info(
"Loaded training dataset with %d samples between %s and %s",
len(dataset),
dataset.start_date,
dataset.end_date,
)
return DataLoader(dataset, shuffle=True, **self._common_dataloader_kwargs)
def val_dataloader(
self,
) -> DataLoader[dict[str, ArrayTCHW]]:
"""Construct validation dataloader"""
dataset = CombinedDataset(
[
ZebraDataset(
name,
paths,
start=self.val_period["start"],
end=self.val_period["end"],
)
for name, paths in self.dataset_groups.items()
],
n_forecast_steps=self.n_forecast_steps,
n_history_steps=self.n_history_steps,
target=self.predict_target,
)
logger.info(
"Loaded validation dataset with %d samples between %s and %s",
len(dataset),
dataset.start_date,
dataset.end_date,
)
return DataLoader(dataset, shuffle=False, **self._common_dataloader_kwargs)
def test_dataloader(
self,
) -> DataLoader[dict[str, ArrayTCHW]]:
"""Construct test dataloader"""
dataset = CombinedDataset(
[
ZebraDataset(
name,
paths,
start=self.test_period["start"],
end=self.test_period["end"],
)
for name, paths in self.dataset_groups.items()
],
n_forecast_steps=self.n_forecast_steps,
n_history_steps=self.n_history_steps,
target=self.predict_target,
)
logger.info(
"Loaded test dataset with %d samples between %s and %s",
len(dataset),
dataset.start_date,
dataset.end_date,
)
return DataLoader(dataset, shuffle=False, **self._common_dataloader_kwargs)