Skip to content

Commit d034fcf

Browse files
authored
Merge pull request #69 from kyleaoman/split_mask_funcs
Split spatial masking and masking by group ID into separate functions.
2 parents 84b5f53 + ecd6604 commit d034fcf

File tree

2 files changed

+92
-35
lines changed

2 files changed

+92
-35
lines changed

docs/source/particles_files/index.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ follows:
8686
sphviewer = SPHViewerWrapper(data.gas)
8787
sphviewer.quickview(xsize=1024,ysize=1024,r="infinity")
8888
89+
The spatial mask and extra mask can be obtained individually by using
90+
the functions :func:`velociraptor.swift.swift.generate_spatial_mask` and
91+
:func:`velociraptor.swift.swift.generate_bound_mask`, respectively.
8992

9093
To see these functions in action, you can check out the examples available in
9194
``examples/swift_integration*.py``` in the repository.

velociraptor/swift/swift.py

Lines changed: 89 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,44 +16,28 @@
1616
from collections import namedtuple
1717

1818

19-
def to_swiftsimio_dataset(
20-
particles: VelociraptorParticles,
21-
snapshot_filename,
22-
generate_extra_mask: bool = False,
23-
) -> Union[
24-
swiftsimio.reader.SWIFTDataset, Tuple[swiftsimio.reader.SWIFTDataset, namedtuple]
25-
]:
19+
def generate_spatial_mask(
20+
particles: VelociraptorParticles,
21+
snapshot_filename
22+
) -> swiftsimio.mask:
2623
"""
27-
Loads a VelociraptorParticles instance for one halo into a
28-
`swiftsimio` masked dataset.
24+
Determines the mask defining the spatial region containing
25+
the particles of interest.
2926
30-
Initially, this uses `r_max` to perform a spatial mask, and
31-
then returns the `swiftsimio` dataset and a secondary mask
32-
that may be used to extract only the particles that are
33-
part of the FoF group.
34-
35-
You will need to instantiate the VelociraptorParticles instance
27+
This uses `r_max` to define the extent of the region, so you
28+
will need to instantiate the VelociraptorParticles instance
3629
with an associated catalogue to use this feature, as it requires
3730
the knowledge of `r_max`.
38-
39-
Takes three arguments:
31+
32+
Takes two arguments:
4033
4134
+ particles, the VelociraptorParticles instance,
4235
+ snapshot_filename, the path to the associated SWIFT snapshot.
43-
+ generate_extra_mask, whether or not to generate the secondary
44-
mask object that allows for the extraction
45-
of particles that are present only in the
46-
FoF group.
4736
4837
It returns:
4938
50-
+ data, the swiftsimio dataset
51-
+ mask, an object containing for all available datasets in the
52-
swift dataset. The initial masking is performed on a
53-
spatial only basis, and this is required to only extract
54-
the particles in the FoF group as identified by
55-
velociraptor. This is only provided if generate_extra_mask
56-
has a truthy value.
39+
+ mask, an object containing masks for all available datasets in the
40+
swift dataset.
5741
"""
5842

5943
# First use the swiftsimio spatial masking to constrain our dataset
@@ -70,7 +54,8 @@ def to_swiftsimio_dataset(
7054
length_factor = 1.0
7155
except AttributeError:
7256
raise RuntimeError(
73-
"Please use a particles instance with an associated halo catalogue."
57+
"Please use a particles instance with an associated halo "
58+
"catalogue."
7459
)
7560

7661
spatial_mask = [
@@ -90,14 +75,27 @@ def to_swiftsimio_dataset(
9075

9176
swift_mask.constrain_spatial(spatial_mask)
9277

93-
# TODO: Make spatial masking work
94-
# swift_mask = None
78+
return swift_mask
9579

96-
data = swiftsimio.load(snapshot_filename, mask=swift_mask)
9780

98-
if not generate_extra_mask:
99-
return data
81+
def generate_bound_mask(
82+
data: swiftsimio.reader.SWIFTDataset,
83+
particles: VelociraptorParticles
84+
) -> namedtuple:
85+
"""
86+
Determines the mask defining the particles bound to the object
87+
of interest.
88+
89+
Takes two arguments:
90+
91+
+ data, a SWIFTDataset, which may be spatially masked,
92+
+ particles, the VelociraptorParticles instance.
10093
94+
It returns:
95+
96+
+ mask, an object containing masks for all available datasets in the
97+
swift dataset.
98+
"""
10199
# Now we must generate the secondary mask, for all available
102100
# particle types.
103101

@@ -112,7 +110,63 @@ def to_swiftsimio_dataset(
112110

113111
# Finally we generate a named tuple with the correct fields and
114112
# fill it with the contents of our dictionary
115-
MaskTuple = namedtuple("MaskCollection", data.metadata.present_particle_names)
113+
MaskTuple = namedtuple(
114+
"MaskCollection",
115+
data.metadata.present_particle_names
116+
)
116117
mask = MaskTuple(**particle_name_masks)
117118

119+
return mask
120+
121+
122+
def to_swiftsimio_dataset(
123+
particles: VelociraptorParticles,
124+
snapshot_filename,
125+
generate_extra_mask: bool = False,
126+
) -> Union[
127+
swiftsimio.reader.SWIFTDataset,
128+
Tuple[swiftsimio.reader.SWIFTDataset, namedtuple]
129+
]:
130+
"""
131+
Loads a VelociraptorParticles instance for one halo into a
132+
`swiftsimio` masked dataset.
133+
134+
Initially, this uses `r_max` to perform a spatial mask, and
135+
then returns the `swiftsimio` dataset and a secondary mask
136+
that may be used to extract only the particles that are
137+
part of the FoF group.
138+
139+
You will need to instantiate the VelociraptorParticles instance
140+
with an associated catalogue to use this feature, as it requires
141+
the knowledge of `r_max`.
142+
143+
Takes three arguments:
144+
145+
+ particles, the VelociraptorParticles instance,
146+
+ snapshot_filename, the path to the associated SWIFT snapshot.
147+
+ generate_extra_mask, whether or not to generate the secondary
148+
mask object that allows for the extraction
149+
of particles that are present only in the
150+
FoF group.
151+
152+
It returns:
153+
154+
+ data, the swiftsimio dataset
155+
+ mask, an object containing masks for all available datasets in the
156+
swift dataset. The initial masking is performed on a
157+
spatial only basis, and this is required to only extract
158+
the particles in the FoF group as identified by
159+
velociraptor. This is only provided if generate_extra_mask
160+
has a truthy value.
161+
"""
162+
163+
swift_mask = generate_spatial_mask(particles, snapshot_filename)
164+
165+
data = swiftsimio.load(snapshot_filename, mask=swift_mask)
166+
167+
if not generate_extra_mask:
168+
return data
169+
170+
mask = generate_bound_mask(data, particles)
171+
118172
return data, mask

0 commit comments

Comments
 (0)