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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416 | class ReadData(SimInfo):
"""
Class to read different hdf5 data files.
Assumes underlying file structures used in MeasureIA and MeasureSnapshotVariables classes.
Attributes
----------
catalogue : str
Catalogue name that contains the data.
sub_group : str, optional
Name of group(s)/structure within snap_group where dataset is found. Default is empty str.
output_file_name : str, optional
Name where output should be stored.
data_path : str, optional
The path to where the data is saved. Default='./data/raw/
Methods
-------
read_cat()
Reads the data from the specified catalogue.
read_subhalo()
Read the data from the subhalo files.
read_snapshot()
Read the data from the snapshot files and optionally write to output file.
read_snapshot_multiple()
Read multiple datasets from the snapshot files for a specified shapshot number.
Notes
-----
Inherits attributes from 'SimInfo', where 'snap_group', 'snap_folder' and 'fof_folder' are used in this class.
"""
def __init__(
self, simulation, catalogue, snapshot, sub_group="", output_file_name=None, data_path="./data/raw/"
):
"""
The __init__ method of the ReadData class.
Parameters
----------
simulation : str
Identifier of the simulation, allowing for correct information to be obtained.
Choose from [TNG100, TNG100_2, TNG300, EAGLE, HorizonAGN, FLAMINGO_L1, FLAMINGO_L2p8, COLIBRE_L400,
COLIBRE_L200].
catalogue : str
Catalogue name that contains the data. If groupcat file: 'Subhalo' (then use read_subhalo).
If snapshot file: enter 'PartTypeX' where X is the particle type number (then use read_snapshot).
snapshot : int or str or NoneType
Number of the snapshot.
sub_group : str, optional
Name of group(s)/structure within snap_group where dataset is found. Default is empty str.
output_file_name : str, optional
Name where output should be stored.
data_path : str, optional
The path to where the data is saved. Default='./data/raw/
"""
SimInfo.__init__(self, simulation, snapshot, boxsize=None, file_info=True)
self.catalogue = catalogue
self.sub_group = sub_group + "/"
self.data_path = data_path + "/"
self.output_file_name = output_file_name
self.r = None
self.rp = None
self.w_gg = None
self.w_gp = None
self.multipoles_gg = None
self.multipoles_gp = None
self.cov_multipoles_gg = None
self.errors_multipoles_gg = None
self.cov_multipoles_gp = None
self.errors_multipoles_gp = None
self.cov_w_gg = None
self.errors_w_gg = None
self.cov_w_gp = None
self.errors_w_gp = None
return
def read_cat(self, dataset_name, cut=None, indices=None):
"""Reads the data from the specified catalogue.
Parameters
----------
dataset_name :
the dataset name for the requested data
cut : iterable with 2 or more entries
If 2 entries: Read dataset slice [cut[0]:cut[1]]. If more: Read dataset slice [cut]. Default value = None
Returns
-------
ndarray
The requested dataset (slice)
Raises
------
KeyError
If catalogue=Subhalo or Snapshot.
"""
if self.catalogue == "Subhalo":
raise KeyError("Use read_subhalo method")
elif self.catalogue == "Snapshot":
raise KeyError("Use read_snapshot method")
catalogue_path = f"{self.data_path}{self.catalogue}.hdf5"
if not os.path.exists(catalogue_path):
raise FileNotFoundError(f"Data file not found: {catalogue_path}")
file = h5py.File(catalogue_path, "r")
if cut is None and indices is None:
data = file[self.snap_group + self.sub_group + dataset_name][:]
elif cut is not None:
data = file[self.snap_group + self.sub_group + dataset_name][cut[0]: cut[1]]
else:
data = file[self.snap_group + self.sub_group + dataset_name][indices]
file.close()
return data
def read_subhalo(self, dataset_name, Nfiles=0):
"""Read the data from the subhalo files.
Parameters
----------
dataset_name :
The dataset name for the requested data
Nfiles : int, optional
Number of files to read from. Default=0, in which case the number from SimInfo object is used.
Returns
-------
ndarray
The requested dataset
"""
subhalo_file = h5py.File(f"{self.data_path}{self.fof_folder}.0.hdf5", "r")
Subhalo = subhalo_file[self.catalogue]
try:
data = Subhalo[dataset_name][:]
except KeyError:
raise KeyError(f"Variable '{dataset_name}' not found in Subhalo files. "
f"Choose from {list(Subhalo.keys())}") from None
if len(np.shape(data)) > 1:
stack = True
else:
stack = False
subhalo_file.close()
if Nfiles == 0:
Nfiles = self.N_files
for n in np.arange(1, Nfiles):
try:
subhalo_file = h5py.File(f"{self.data_path}{self.fof_folder}.{n}.hdf5", "r")
except OSError as e:
raise OSError(f"Could not open file {n} ({self.data_path}{self.fof_folder}.{n}.hdf5).") from e
try:
Subhalo = subhalo_file[self.catalogue]
data_n = Subhalo[dataset_name][:] # get data single file
except KeyError:
print("problem at file ", n)
subhalo_file.close()
continue
if stack:
data = np.vstack((data, data_n))
else:
data = np.append(data, data_n)
subhalo_file.close()
return data
def read_snapshot(self, dataset_name):
"""Read the data from the snapshot files and optionally write to output file.
Parameters
----------
dataset_name :
The dataset name for the requested data
Returns
-------
ndarray
The requested dataset or nothing if output_file_name is specified
"""
if self.output_file_name != None:
output_file = h5py.File(self.output_file_name, "a")
group_out = create_group_hdf5(output_file, self.snap_group)
write_output = True
else:
write_output = False
print(dataset_name)
snap_file = h5py.File(f"{self.data_path}{self.snap_folder}.0.hdf5", "r")
Snap_data = snap_file[self.catalogue]
try:
data = Snap_data[dataset_name][:]
except KeyError:
raise KeyError(f"Variable '{dataset_name}' not found in Snapshot files. "
f"Choose from {list(Snap_data.keys())}") from None
if len(np.shape(data)) > 1:
stack = True
else:
stack = False
if write_output:
if dataset_name in group_out:
del group_out[dataset_name]
if stack:
group_out.create_dataset(dataset_name, data=data, maxshape=(None, np.shape(data)[1]), chunks=True)
else:
group_out.create_dataset(dataset_name, data=data, maxshape=(None,), chunks=True)
snap_file.close()
for n in np.arange(1, self.N_files):
snap_file = h5py.File(f"{self.data_path}{self.snap_folder}.{n}.hdf5", "r")
try:
Snap_data = snap_file[self.catalogue]
data_n = Snap_data[dataset_name][:] # get data single file
except KeyError:
print("problem at file ", n)
snap_file.close()
continue
if write_output:
group_out[dataset_name].resize((group_out[dataset_name].shape[0] + data_n.shape[0]), axis=0)
group_out[dataset_name][-data_n.shape[0]:] = data_n
else:
if stack:
data = np.vstack((data, data_n))
else:
data = np.append(data, data_n)
snap_file.close()
if write_output:
output_file.close()
return
else:
return data
def read_snapshot_multiple(self, dataset_name):
"""Read multiple datasets from the snapshot files for a specified shapshot number.
Parameters
----------
dataset_name : list or str
The dataset names for the requested data
Returns
-------
ndarray
The requested datasets or nothing if output_file_name is specified
"""
if self.output_file_name != None:
output_file = h5py.File(self.output_file_name, "a")
group_out = create_group_hdf5(output_file, self.snap_group)
write_output = True
else:
write_output = False
snap_file = h5py.File(f"{self.data_path}{self.snap_folder}.0.hdf5", "r")
Snap_data = snap_file[self.catalogue]
stack = []
for i, variable in enumerate(dataset_name):
try:
data = Snap_data[dataset_name[i]][:]
except KeyError:
raise KeyError(f"Variable '{variable}' not found in Snapshot files. "
f"Choose from {list(Snap_data.keys())}") from None
if len(np.shape(data)) > 1:
stack.append(True)
else:
stack.append(False)
if write_output:
if variable in group_out:
del group_out[variable]
if stack[i]:
group_out.create_dataset(variable, data=data, maxshape=(None, np.shape(data)[1]), chunks=True)
else:
group_out.create_dataset(variable, data=data, maxshape=(None,), chunks=True)
snap_file.close()
for n in np.arange(1, self.N_files):
snap_file = h5py.File(f"{self.data_path}{self.snap_folder}.{n}.hdf5", "r")
for i, variable in enumerate(dataset_name):
try:
Snap_data = snap_file[self.catalogue]
data_n = Snap_data[variable][:] # get data single file
except KeyError:
print("problem at file ", n)
snap_file.close()
continue
if write_output:
group_out[variable].resize((group_out[variable].shape[0] + data_n.shape[0]), axis=0)
group_out[variable][-data_n.shape[0]:] = data_n
else:
if stack[i]:
data = np.vstack((data, data_n))
else:
data = np.append(data, data_n)
snap_file.close()
if write_output:
output_file.close()
return
else:
return data
def read_MeasureIA_output(self, dataset_name, num_jk):
"""
Fills in the available w_gg, w_gp, multipoles_gg, multipoles_gp, r, rp, and associated cov and errors attributes
for a given dataset and num_jk from the output file of MeasureIA.
Parameters
----------
dataset_name: str
Name of the dataset in the output file of MeasureIA.
num_jk: int or str or NoneType
Number of jackknife patches to be generated internally. If None, the covariance will not be read.
"""
# reset parameters (if same object is used for multiple datasets)
self.r = None
self.rp = None
self.w_gg = None
self.w_gp = None
self.multipoles_gg = None
self.multipoles_gp = None
self.cov_multipoles_gg = None
self.errors_multipoles_gg = None
self.cov_multipoles_gp = None
self.errors_multipoles_gp = None
self.cov_w_gg = None
self.errors_w_gg = None
self.cov_w_gp = None
self.errors_w_gp = None
file = h5py.File(f"{self.data_path}{self.catalogue}.hdf5", "r")
if self.snap_group != "":
data_group = file[self.snap_group]
else:
data_group = file
try:
self.multipoles_gg = data_group[f"multipoles_gg/{dataset_name}"][:]
self.r = data_group[f"multipoles_gg/{dataset_name}_r"][:]
if num_jk != None:
self.cov_multipoles_gg = data_group[f"multipoles_gg/{dataset_name}_jackknife_cov_{num_jk}"][:]
self.errors_multipoles_gg = data_group[f"multipoles_gg/{dataset_name}_jackknife_{num_jk}"][:]
except KeyError:
pass
try:
self.multipoles_gp = data_group[f"multipoles_g_plus/{dataset_name}"][:]
self.r = data_group[f"multipoles_g_plus/{dataset_name}_r"][:]
if num_jk != None:
self.cov_multipoles_gp = data_group[f"multipoles_g_plus/{dataset_name}_jackknife_cov_{num_jk}"][:]
self.errors_multipoles_gp = data_group[f"multipoles_g_plus/{dataset_name}_jackknife_{num_jk}"][:]
except KeyError:
pass
try:
self.w_gg = data_group[f"w_gg/{dataset_name}"][:]
self.rp = data_group[f"w_gg/{dataset_name}_rp"][:]
if num_jk != None:
self.cov_w_gg = data_group[f"w_gg/{dataset_name}_jackknife_cov_{num_jk}"][:]
self.errors_w_gg = data_group[f"w_gg/{dataset_name}_jackknife_{num_jk}"][:]
except KeyError:
pass
try:
self.w_gp = data_group[f"w_g_plus/{dataset_name}"][:]
self.rp = data_group[f"w_g_plus/{dataset_name}_rp"][:]
if num_jk != None:
self.cov_w_gp = data_group[f"w_g_plus/{dataset_name}_jackknife_cov_{num_jk}"][:]
self.errors_w_gp = data_group[f"w_g_plus/{dataset_name}_jackknife_{num_jk}"][:]
except KeyError:
pass
file.close()
return
def read_modelling_outputs(self, catalogue):
"""Reads fitted IA/bias modelling parameters from a results HDF5 file and stores them on
the object.
Reads the ``A_IA``/``b_g`` amplitudes and their errors, written as HDF5 attributes on the
``w`` and/or ``multipoles`` groups (of ``self.snap_group`` when set), into
``self.{w,multipoles}_{A_IA,A_IA_err,b_g,b_g_err}``. A group that is absent is skipped.
``self.z`` is read from the snapshot-group attributes when a snapshot group is used.
Parameters
----------
catalogue : str
Base name (without extension) of the ``.hdf5`` results file under ``self.data_path``.
"""
file = h5py.File(f"{self.data_path}{catalogue}.hdf5", "r")
if self.snap_group != "":
data_group = file[self.snap_group]
self.z = data_group.attrs["z"]
else:
data_group = file
try:
self.w_A_IA = data_group[f"w"].attrs["A_IA"]
self.w_A_IA_err = data_group[f"w"].attrs["A_IA_err"]
self.w_b_g = data_group[f"w"].attrs["b_g"]
self.w_b_g_err = data_group[f"w"].attrs["b_g_err"]
except KeyError:
pass
try:
self.multipoles_A_IA = data_group[f"multipoles"].attrs["A_IA"]
self.multipoles_A_IA_err = data_group[f"multipoles"].attrs["A_IA_err"]
self.multipoles_b_g = data_group[f"multipoles"].attrs["b_g"]
self.multipoles_b_g_err = data_group[f"multipoles"].attrs["b_g_err"]
except KeyError:
pass
file.close()
return
|