Skip to content

skais_mapper.illustris.util

Illustris simulation snapshot utilities.

Adapted from: https://github.com/illustristng/illustris_python

Classes:

Name Description
IllustrisH5File

Represents an Illustris HDF5 file. Wrapper for the class.

Functions:

Name Description
pidx_from_ptype

Mapping common names to indices of particle types.

ptype_from_pidx

Mapping indices to names of particle types.

IllustrisH5File

IllustrisH5File(
    base_path: str | Path,
    snapshot: int,
    partition: int = 0,
    path_func: Callable | None = None,
    mode: str = "r",
    driver: str | None = None,
    cache_size: int | float | str = "2G",
    **kwargs,
)

Bases: h5py.File

Represents an Illustris HDF5 file. Wrapper for the class.

Initialize a IllustrisH5File instance.

Parameters:

Name Type Description Default
base_path str | Path

Base path to the Illustris(TNG) snapshots.

required
snapshot int

Snapshot ID {0-99}.

required
partition int

Subfile partition ID {0-600+}.

0
path_func Callable | None

A function fetching the filename of the HDF5 file. The function should accept , , as arguments.

None
mode str

'r' Readonly, file must exist (default) 'r+' Read/write, file must exist 'w' Create file, truncate if exists 'w-' or 'x' Create file, fail if exists 'a' Read/write if exists, create otherwise.

'r'
driver str | None

Name of the driver to use; valid values are None (default), 'core', 'sec2', 'direct', 'stdio', 'mpio', 'ros3'.

None
cache_size int | float | str

Chunk cache size in bytes or passed as string.

'2G'
**kwargs

More keyword arguments

{}
Source code in skais_mapper/illustris/util.py
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
def __init__(
    self,
    base_path: str | Path,
    snapshot: int,
    partition: int = 0,
    path_func: Callable | None = None,
    mode: str = "r",
    driver: str | None = None,
    cache_size: int | float | str = "2G",
    **kwargs,
):
    """Initialize a IllustrisH5File instance.

    Args:
        base_path: Base path to the Illustris(TNG) snapshots.
        snapshot: Snapshot ID {0-99}.
        partition: Subfile partition ID {0-600+}.
        path_func:
            A function fetching the filename of the HDF5 file. The function
            should accept <base_path>, <snapshot>, <partition> as arguments.
        mode:
            'r'          Readonly, file must exist (default)
            'r+'         Read/write, file must exist
            'w'          Create file, truncate if exists
            'w-' or 'x'  Create file, fail if exists
            'a'          Read/write if exists, create otherwise.
        driver: Name of the driver to use; valid values are
            None (default), 'core', 'sec2', 'direct', 'stdio', 'mpio', 'ros3'.
        cache_size: Chunk cache size in bytes or passed as string.
        **kwargs: More keyword arguments
    """
    self.exists = True
    self.chunk_cache_size = nbytes(cache_size)
    kwargs.setdefault("rdcc_nbytes", int(self.chunk_cache_size))
    kwargs.setdefault("rdcc_w0", 1.0)
    if os.path.exists(self.filename):
        super().__init__(self.filename, mode=mode, driver=driver, **kwargs)
    else:
        self.exists = False

pidx_from_ptype

pidx_from_ptype(ptype: int | str) -> int

Mapping common names to indices of particle types.

Parameters:

Name Type Description Default
ptype int | str

particle type description string

required

Returns:

Type Description
int

(int) particle type index 0 -> gas particles 1 -> dark-matter particles 2 -> lowres dark-matter / stellar disc particles (in zoom simulations) 3 -> tracer / stellar buldge particles 4 -> star / wind / stellar particles 5 -> blackhole / sink particles

Raises:

Type Description
ValueError

If the name doesn't match any particle type.

Source code in skais_mapper/illustris/util.py
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
def pidx_from_ptype(ptype: int | str) -> int:
    """Mapping common names to indices of particle types.

    Args:
        ptype: particle type description string

    Returns:
        (int) particle type index
          0 -> gas particles
          1 -> dark-matter particles
          2 -> lowres dark-matter / stellar disc particles (in zoom simulations)
          3 -> tracer / stellar buldge particles
          4 -> star / wind / stellar particles
          5 -> blackhole / sink particles

    Raises:
        (ValueError): If the name doesn't match any particle type.
    """
    if str(ptype).isdigit():
        return int(ptype)
    if str(ptype).lower() in ["gas", "cells"]:
        return 0
    if str(ptype).lower() in ["dm", "darkmatter"]:
        return 1
    if str(ptype).lower() in ["dmlowres"]:
        return 2  # only zoom simulations, not present in full periodic boxes
    if str(ptype).lower() in ["tracer", "tracers", "tracermc", "trmc"]:
        return 3
    if str(ptype).lower() in ["star", "stars", "stellar"]:
        return 4  # only those with GFM_StellarFormationTime > 0
    if str(ptype).lower() in ["wind"]:
        return 4  # only those with GFM_StellarFormationTime < 0
    if str(ptype).lower() in ["bh", "bhs", "blackhole", "blackholes"]:
        return 5
    raise ValueError(f"Unknown particle type name {ptype}.")

ptype_from_pidx

ptype_from_pidx(pidx: int | str) -> str

Mapping indices to names of particle types.

Parameters:

Name Type Description Default
pidx int | str

particle type index

required

Returns:

Type Description
str

(int) particle type index gas particles -> 0 dark-matter particles -> 1 lowres dark-matter / stellar disc particles (in zoom simulations) -> 2 tracer / stellar buldge particles -> 3 star / wind / stellar particles -> 4 blackhole / sink particles -> 5

Raises:

Type Description
ValueError

If the index is not

Source code in skais_mapper/illustris/util.py
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
def ptype_from_pidx(pidx: int | str) -> str:
    """Mapping indices to names of particle types.

    Args:
        pidx: particle type index

    Returns:
        (int) particle type index
          gas particles -> 0
          dark-matter particles -> 1
          lowres dark-matter / stellar disc particles (in zoom simulations) -> 2
          tracer / stellar buldge particles -> 3
          star / wind / stellar particles -> 4
          blackhole / sink particles -> 5

    Raises:
        (ValueError): If the index is not
    """
    if isinstance(pidx, str):
        return pidx
    if pidx == 0:
        return "gas"
    if pidx == 1:
        return "dm"
    if pidx == 2:
        return "dmlowres"  # only zoom simulations, not present in full periodic boxes
    if pidx == 3:
        return "tracer"
    if pidx == 4:
        return "star"  # only those with GFM_StellarFormationTime > 0
    if pidx == 5:
        return "bh"
    raise ValueError(f"Unknown particle type name {pidx}.")