-
Notifications
You must be signed in to change notification settings - Fork 611
Expand file tree
/
Copy pathmock.py
More file actions
133 lines (111 loc) · 4.7 KB
/
mock.py
File metadata and controls
133 lines (111 loc) · 4.7 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
# SPDX-FileCopyrightText: Copyright (c) 2023 - 2026 NVIDIA CORPORATION & AFFILIATES.
# SPDX-FileCopyrightText: All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Any, Literal
import numpy as np
from .dataset import StormCastDataset
class _MockDataset(StormCastDataset):
"""A minimal mock dataset implementation for testing without real data.
Args:
num_state_channels: Number of channels in the state data
num_background_channels: Number of channels in the background data
image_size: Tuple of (height, width) for the images
num_samples: Number of samples in the dataset (default: 100)
"""
def __init__(
self,
num_state_channels: int = 3,
num_background_channels: int = 4,
num_invariant_channels: int = 2,
num_scalar_cond_channels: int = 2,
image_size: tuple[int, int] = (256, 128),
num_samples: int = 100,
train: bool = True,
model_type: Literal[
"hybrid", "nowcasting", "downscaling", "unconditional"
] = "hybrid",
):
self._num_state_channels = num_state_channels
self._num_background_channels = num_background_channels
self._num_invariant_channels = num_invariant_channels
self._num_scalar_cond_channels = num_scalar_cond_channels
self._image_size = image_size
self._num_samples = num_samples
self._model_type = model_type
def __len__(self) -> int:
return self._num_samples
def __getitem__(self, idx: int) -> dict[str, Any]:
"""Return a sample with random data."""
rng = np.random.default_rng(seed=idx) # Use idx as seed for reproducibility
height, width = self._image_size
# Generate random background data
background = rng.normal(
size=(self._num_background_channels, height, width)
).astype(np.float32)
# Generate random state data (input and target)
state_input = rng.normal(size=(self._num_state_channels, height, width)).astype(
np.float32
)
state_target = rng.normal(
size=(self._num_state_channels, height, width)
).astype(np.float32)
if self._model_type == "hybrid":
item = {
"background": background,
"state": [state_input, state_target],
}
elif self._model_type == "nowcasting":
item = {"state": [state_input, state_target]}
elif self._model_type == "downscaling":
item = {
"background": background,
"state": state_target,
}
elif self._model_type == "unconditional":
item = {"state": state_target}
# Generate scalar conditions
if self._num_scalar_cond_channels:
item["scalar_conditions"] = rng.normal(
size=(self._num_scalar_cond_channels,)
).astype(np.float32)
return item
def background_channels(self) -> list[str]:
"""Return metadata for background channels."""
return [f"background_{i}" for i in range(self._num_background_channels)]
def state_channels(self) -> list[str]:
"""Return metadata for state channels."""
return [f"state_{i}" for i in range(self._num_state_channels)]
def scalar_condition_channels(self) -> list[str]:
"""Return metadata for state channels."""
return [f"scalar_cond_{i}" for i in range(self._num_scalar_cond_channels)]
def image_shape(self) -> tuple[int, int]:
"""Return the (height, width) of the data."""
return self._image_size
def get_invariants(self) -> np.ndarray | None:
"""Return invariants used for training."""
if self._num_invariant_channels > 0:
rng = np.random.default_rng(seed=42)
return rng.normal(
size=(
self._num_invariant_channels,
self._image_size[0],
self._image_size[1],
)
).astype(np.float32)
else:
return None
class MockDataset(_MockDataset):
def __init__(self, params, train):
super().__init__(train=train, **params)