Skip to content

Commit 035dafa

Browse files
authored
Merge pull request #92 from SWIFTSIM/multiple-registration-files
Added support for multiple registration files.
2 parents 6489257 + 973acb7 commit 035dafa

File tree

3 files changed

+30
-24
lines changed

3 files changed

+30
-24
lines changed

velociraptor/__init__.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@
3030
from velociraptor.catalogue.catalogue import VelociraptorCatalogue
3131
from velociraptor.__version__ import __version__
3232

33-
from typing import Union
33+
from typing import Union, List
3434

3535

3636
def load(
3737
filename: str,
3838
disregard_units: bool = False,
39-
registration_file_path: Union[str, None] = None,
39+
registration_file_path: Union[List[str], str, None] = None,
4040
mask: slice = Ellipsis,
4141
) -> VelociraptorCatalogue:
4242
"""
@@ -59,8 +59,8 @@ def load(
5959
star formation rate units are presented in non-internal
6060
units.
6161
62-
registration_file_path: str, optional
63-
The filename of the derived quantities script to register
62+
registration_file_path: Union[List[str], str], optional
63+
The filename of the derived quantities script(s) to register
6464
additional properties with the catalogue. This is an
6565
advanced feature. See the documentation for more details.
6666

velociraptor/catalogue/catalogue.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,9 @@ def __create_sub_catalogues(self):
410410

411411
return
412412

413-
def register_derived_quantities(self, registration_file_path: str) -> None:
413+
def register_derived_quantities(
414+
self, registration_file_path: Union[List[str], str]
415+
) -> None:
414416
"""
415417
Register any required derived quantities. These will
416418
be available through `catalogue.derived_quantities.{your_names}`.
@@ -421,8 +423,8 @@ def register_derived_quantities(self, registration_file_path: str) -> None:
421423
Parameters
422424
----------
423425
424-
registration_file_path: str
425-
Path to the python source file that contains the code to
426+
registration_file_path: Union[List[str], str]
427+
Path to the python source file(s) that contain(s) the code to
426428
register the additional derived quantities.
427429
"""
428430

velociraptor/catalogue/derived.py

+21-17
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,21 @@
55

66
import numpy as np
77
import unyt
8+
from typing import Union, List
89

910

1011
class DerivedQuantities(object):
1112
"""
12-
Derived quantities class. Contains methods to open a python
13-
source file that creates derived quantities as follows:
13+
Derived quantities class. Contains methods to open (a) python
14+
source file(s) that create(s) derived quantities as follows:
1415
1516
The source file will have access to:
1617
1718
+ numpy (imported as np)
1819
+ unyt (imported as unyt)
1920
2021
You should write your derived quantities as follows:
21-
22+
2223
.. code-block:: python
2324
2425
self.derived_quantitiy_name = catalogue.type.field_name * 0.5
@@ -49,9 +50,9 @@ class DerivedQuantities(object):
4950
setattr(
5051
self, f"specific_sfr_gas_{aperture_size}_kpc", ssfr
5152
)
52-
5353
54-
The path to this file should be passed to the __init__ method
54+
55+
The path to this file(s) should be passed to the __init__ method
5556
of this class. Note that you should only register quantities that
5657
you do in fact intend to use, as these are not lazily loaded
5758
in the same way as the properties that are built into catalogues.
@@ -61,36 +62,39 @@ class DerivedQuantities(object):
6162
6263
"""
6364

64-
def __init__(self, registration_file_path: str, catalogue):
65+
def __init__(self, registration_file_path: Union[List[str], str], catalogue):
6566
"""
66-
Registers additional (derived) quantities from the
67+
Registers additional (derived) quantities from the
6768
VelociraptorCatalogue to itself, using a python
6869
source file that does this registration inside
6970
the private _register_quantities() method.
7071
7172
Parameters
7273
----------
7374
74-
registration_file_path: str
75-
Path to the python source file. For more information
76-
on the contents of this file, check out the information
75+
registration_file_path: Union[List[str], str]
76+
Path to the python source file(s). For more information
77+
on the contents of this file/these files, check out the information
7778
of this object.
7879
7980
catalogue: VelociraptorCatalogue
8081
The catalogue to derive the quantities from.
8182
82-
83+
8384
Returns
8485
-------
85-
86+
8687
DerivedQuantities
8788
An instance of the DerivedQuantities class with
8889
the properties defined in registration_file_path
8990
available as attributes.
90-
91+
9192
"""
9293

93-
self.registration_file_path = registration_file_path
94+
if isinstance(registration_file_path, list):
95+
self.registration_file_paths = list(registration_file_path)
96+
else:
97+
self.registration_file_paths = [registration_file_path]
9498
self.catalogue = catalogue
9599

96100
self._register_quantities()
@@ -105,8 +109,8 @@ def _register_quantities(self):
105109
"""
106110

107111
catalogue = self.catalogue
108-
109-
with open(self.registration_file_path, "r") as handle:
110-
exec(handle.read())
112+
for file_path in self.registration_file_paths:
113+
with open(file_path, "r") as handle:
114+
exec(handle.read())
111115

112116
return

0 commit comments

Comments
 (0)