-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdatasets.py
247 lines (219 loc) · 6.63 KB
/
datasets.py
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import tensorflow as tf
import time
import jax
import jax.numpy as np
import numpy as vnp
from pathlib import Path
import pandas as pd
from absl import logging
from models.graph_utils import get_rotated_box
EPS = 1e-7
def make_dataloader(x, conditioning, mask, batch_size, seed, shuffle=True):
n_train = len(x)
train_ds = tf.data.Dataset.from_tensor_slices((x, conditioning, mask))
train_ds = train_ds.cache()
train_ds = train_ds.repeat()
batch_dims = [jax.local_device_count(), batch_size // jax.device_count()]
for _batch_size in reversed(batch_dims):
train_ds = train_ds.batch(_batch_size, drop_remainder=False)
if shuffle:
train_ds = train_ds.shuffle(n_train, seed=seed)
return train_ds
def get_halo_data(
data_dir,
n_features,
n_particles,
split: str = "train",
simulation_set: str = "lhc",
conditioning_parameters: list = ["Omega_m", "sigma_8"],
):
if simulation_set == "lhc":
x = np.load(data_dir / f"{split}_halos.npy")
conditioning = pd.read_csv(data_dir / f"{split}_cosmology.csv")
elif simulation_set == "lhc+fiducial":
x = np.load(data_dir / f"{split}_halos_combined.npy")
conditioning = pd.read_csv(data_dir / f"{split}_cosmology_combined.csv")
elif simulation_set == "fiducial":
x = np.load(data_dir / f"{split}_halos_fiducial.npy")
conditioning = None
else:
raise NotImplementedError(
f"{simulation_set} does not exist as a simulation set"
)
if conditioning is not None:
conditioning = np.array(conditioning[conditioning_parameters].values)
if n_features == 7:
x = x.at[:, :, -1].set(np.log10(x[:, :, -1])) # Use log10(mass)
x = x[:, :n_particles, :n_features]
return x, conditioning
def get_nbody_data(
n_features,
n_particles,
split: str = "train",
simulation_set: str = "lhc",
conditioning_parameters: list = ["Omega_m", "sigma_8"],
):
DATA_DIR = Path("/n/holystore01/LABS/iaifi_lab/Lab/set-diffuser-data/")
x, conditioning = get_halo_data(
data_dir=DATA_DIR,
n_features=n_features,
n_particles=n_particles,
split=split,
simulation_set=simulation_set,
conditioning_parameters=conditioning_parameters,
)
if split == "train":
x_train = x
else:
x_train, _ = get_halo_data(
data_dir=DATA_DIR,
n_features=n_features,
n_particles=n_particles,
split="train",
simulation_set=simulation_set,
)
# Standardize per-feature (over datasets and particles)
x_mean = x_train.mean(axis=(0, 1))
x_std = x_train.std(axis=(0, 1))
norm_dict = {"mean": x_mean, "std": x_std}
# if conditioning is not None:
# conditioning = conditioning[:, [0, -1]] # Select only omega_m and sigma_8
mask = np.ones((x.shape[0], n_particles)) # No mask
x = (x - x_mean + EPS) / (x_std + EPS)
# Finalize
return x, mask, conditioning, norm_dict
def nbody_dataset(
n_features,
n_particles,
batch_size,
seed,
split: str = "train",
shuffle: bool = True,
simulation_set: str = "lhc",
conditioning_parameters: list = ["Omega_m", "sigma_8"],
):
x, mask, conditioning, norm_dict = get_nbody_data(
n_features,
n_particles,
split=split,
simulation_set=simulation_set,
conditioning_parameters=conditioning_parameters,
)
ds = make_dataloader(
x,
conditioning,
mask,
batch_size,
seed,
shuffle=shuffle,
)
return ds, norm_dict
def load_data(
dataset, n_features, n_particles, batch_size, seed, shuffle, split, **kwargs
):
if dataset == "nbody":
train_ds, norm_dict = nbody_dataset(
n_features,
n_particles,
batch_size,
seed,
shuffle=shuffle,
split=split,
**kwargs,
)
else:
raise ValueError("Unknown dataset: {}".format(dataset))
return train_ds, norm_dict
def augment_with_translations(
x,
conditioning,
mask,
rng,
norm_dict,
n_pos_dim=3,
box_size: float = 1000.0,
):
rng, _ = jax.random.split(rng)
x = x * norm_dict["std"] + norm_dict["mean"]
# Draw N random translations
translations = jax.random.uniform(
rng, minval=-box_size / 2, maxval=box_size / 2, shape=(*x.shape[:2], 3)
)
x = x.at[..., :n_pos_dim].set(
(x[..., :n_pos_dim] + translations[..., None, :]) % box_size
)
x = (x - norm_dict["mean"]) / norm_dict["std"]
return x, conditioning, mask
def random_symmetry_matrix(key):
# 8 possible sign combinations for reflections
signs = np.array(
[
[-1, -1, -1],
[-1, -1, 1],
[-1, 1, -1],
[-1, 1, 1],
[1, -1, -1],
[1, -1, 1],
[1, 1, -1],
[1, 1, 1],
]
)
# 6 permutations for axis swapping
perms = np.array([[0, 1, 2], [0, 2, 1], [1, 0, 2], [1, 2, 0], [2, 0, 1], [2, 1, 0]])
# Randomly select one sign combination and one permutation
sign = signs[jax.random.randint(key, (), 0, 8)]
perm = perms[jax.random.randint(key, (), 0, 6)]
# Combine them to form the random symmetry matrix
matrix = np.eye(3)[perm] * sign
return matrix
def augment_with_symmetries(
x,
conditioning,
mask,
rng,
norm_dict,
n_pos_dim=3,
box_size: float = 1000.0,
):
rng, _ = jax.random.split(rng)
# Rotations and reflections that respect boundary conditions
matrix = random_symmetry_matrix(rng)
x = x.at[..., :n_pos_dim].set(np.dot(x[..., :n_pos_dim], matrix.T))
if x.shape[-1] > n_pos_dim:
# Rotate velocities too
x = x.at[..., n_pos_dim : n_pos_dim + 3].set(
np.dot(x[..., n_pos_dim : n_pos_dim + 3], matrix.T)
)
return x, conditioning, mask
def augment_data(
x,
conditioning,
mask,
rng,
norm_dict,
rotations: bool = True,
translations: bool = True,
n_pos_dim=3,
box_size: float = 1000.0,
):
if rotations:
x, conditioning, mask = augment_with_symmetries(
x=x,
mask=mask,
conditioning=conditioning,
rng=rng,
norm_dict=norm_dict,
n_pos_dim=n_pos_dim,
box_size=box_size,
)
if translations:
x, conditioning, mask = augment_with_translations(
x=x,
mask=mask,
conditioning=conditioning,
rng=rng,
norm_dict=norm_dict,
n_pos_dim=n_pos_dim,
box_size=box_size,
)
return x, conditioning, mask