Skip to content

Commit 882d196

Browse files
authored
Merge pull request #77 from kyleaoman/maskable
Add ability to mask catalogue
2 parents 1e14a55 + 56d7c8b commit 882d196

File tree

5 files changed

+55
-12
lines changed

5 files changed

+55
-12
lines changed

docs/source/basic_usage/index.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ based on the unit metadata in the file.
6262
There is also full unit information available in the ``data.units`` object, with
6363
an ``astropy`` cosmology object provided as ``data.units.cosmology``.
6464

65+
Subsets of the catalogue can be read by providing a mask, for instance to read only values for the 3rd object in the catalogue (indexed from 0):
66+
67+
.. code-block:: python
68+
masked_data = load("/path/to/catalogue.properties", mask=3)
69+
6570
Creating your first plot
6671
------------------------
6772

docs/source/particles_files/index.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ This returns two objects, ``particles``, and ``unbound_particles``,
3333
corresponding to both the bound and unbound component of your halo
3434
respectively. Each of these contains the information required to extract just
3535
those particles from a snapshot (this is made much easier by using the SWIFT
36-
integration, shown below).
36+
integration, shown below). Note that use of a masked ``catalogue`` is not
37+
supported.
3738

3839
The instances of
3940
:class:`velociraptor.particles.particles.VelociraptorParticles` have several

velociraptor/__init__.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def load(
3737
filename: str,
3838
disregard_units: bool = False,
3939
registration_file_path: Union[str, None] = None,
40+
mask: slice = Ellipsis
4041
) -> VelociraptorCatalogue:
4142
"""
4243
Loads a velociraptor catalogue, producing a VelociraptorCatalogue
@@ -63,6 +64,11 @@ def load(
6364
additional properties with the catalogue. This is an
6465
advanced feature. See the documentation for more details.
6566
67+
mask: Union[None, NDArray[bool], int], optional
68+
If a boolean array is provided, it is used to mask all
69+
catalogue arrays. If an int is provided, catalogue arrays
70+
are masked to the single corresponding element.
71+
6672
6773
Returns
6874
-------
@@ -72,7 +78,11 @@ def load(
7278
.properties file.
7379
"""
7480

75-
catalogue = VelociraptorCatalogue(filename, disregard_units=disregard_units)
81+
catalogue = VelociraptorCatalogue(
82+
filename,
83+
disregard_units=disregard_units,
84+
mask=mask
85+
)
7686

7787
if registration_file_path is not None:
7888
catalogue.register_derived_quantities(registration_file_path)

velociraptor/catalogue/catalogue.py

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,13 @@ def register_field_properties(self):
8888
return
8989

9090

91-
def generate_getter(filename, name: str, field: str, full_name: str, unit):
91+
def generate_getter(
92+
filename,
93+
name: str,
94+
field: str,
95+
full_name: str,
96+
unit
97+
):
9298
"""
9399
Generates a function that:
94100
@@ -115,7 +121,8 @@ def getter(self):
115121
else:
116122
with h5py.File(filename, "r") as handle:
117123
try:
118-
setattr(self, f"_{name}", unyt.unyt_array(handle[field][...], unit))
124+
mask = getattr(self, "mask")
125+
setattr(self, f"_{name}", unyt.unyt_array(handle[field][mask], unit))
119126
getattr(self, f"_{name}").name = full_name
120127
getattr(self, f"_{name}").file = filename
121128
except KeyError:
@@ -161,6 +168,7 @@ def generate_sub_catalogue(
161168
registration_function: Callable,
162169
units: VelociraptorUnits,
163170
field_metadata: List[VelociraptorFieldMetadata],
171+
mask: slice = Ellipsis
164172
):
165173
"""
166174
Generates a sub-catalogue object with the correct properties set.
@@ -190,7 +198,7 @@ def generate_sub_catalogue(
190198
metadata.snake_case,
191199
metadata.path,
192200
metadata.name,
193-
metadata.unit,
201+
metadata.unit
194202
),
195203
generate_setter(metadata.snake_case),
196204
generate_deleter(metadata.snake_case),
@@ -205,7 +213,7 @@ def generate_sub_catalogue(
205213
)
206214

207215
# Finally, we can actually create an instance of our new class.
208-
catalogue = ThisSubCatalogue(filename=filename)
216+
catalogue = ThisSubCatalogue(filename=filename, mask=mask)
209217
catalogue.valid_sub_paths = valid_sub_paths
210218

211219
return catalogue
@@ -224,8 +232,9 @@ class __VelociraptorSubCatalogue(object):
224232
# The valid paths contained within
225233
valid_sub_paths: List[str]
226234

227-
def __init__(self, filename):
235+
def __init__(self, filename, mask=Ellipsis):
228236
self.filename = filename
237+
self.mask = mask
229238

230239
return
231240

@@ -250,6 +259,7 @@ def __init__(
250259
filename: str,
251260
disregard_units: bool = False,
252261
extra_registration_functions: Union[None, Dict[str, Callable]] = None,
262+
mask: slice = Ellipsis,
253263
):
254264
"""
255265
Initialise the velociraptor catalogue with all of the available
@@ -276,10 +286,16 @@ def __init__(
276286
should be a dictionary of strings pointing to callables, which
277287
conform to the registration function API. This is an advanced
278288
feature.
289+
290+
mask: slice, optional
291+
If a boolean array is provided, it is used to mask all catalogue
292+
arrays. If an int is provided, catalogue arrays are masked to the
293+
single corresponding element. Default: Ellipsis (``...``).
279294
"""
280295
self.filename = filename
281296
self.disregard_units = disregard_units
282297
self.extra_registration_functions = extra_registration_functions
298+
self.mask = mask
283299

284300
self.get_units()
285301
self.extract_properties_from_units()
@@ -296,11 +312,18 @@ def __str__(self):
296312
the memory location.
297313
"""
298314

299-
return (
300-
f"Velociraptor catalogue at {self.filename}. "
301-
"Contains the following field collections: "
302-
f"{', '.join(self.valid_field_metadata.keys())}"
303-
)
315+
if self.mask is Ellipsis:
316+
return (
317+
f"Velociraptor catalogue at {self.filename}. "
318+
"Contains the following field collections: "
319+
f"{', '.join(self.valid_field_metadata.keys())}"
320+
)
321+
else:
322+
return (
323+
f"Masked velociraptor catalogue at {self.filename}. "
324+
"Contains the following field collections: "
325+
f"{', '.join(self.valid_field_metadata.keys())}"
326+
)
304327

305328
def __repr__(self):
306329
return str(self)
@@ -388,6 +411,7 @@ def __create_sub_catalogues(self):
388411
registration_function=self.registration_functions[attribute_name],
389412
units=self.units,
390413
field_metadata=field_metadata,
414+
mask=self.mask
391415
),
392416
)
393417

velociraptor/particles/particles.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ def __init__(self, filename, catalogue: Union[VelociraptorCatalogue, None]):
5757
halo.
5858
"""
5959

60+
if catalogue.mask is not Ellipsis:
61+
raise ValueError('VelociraptorGroups not compatible with masked catalogue.')
62+
6063
self.filename = filename
6164
self.catalogue = catalogue
6265

0 commit comments

Comments
 (0)