yt_napari package

Submodules

yt_napari.viewer module

class yt_napari.viewer.Scene(reference_layer: ReferenceLayer | None = None)

Bases: object

add_covering_grid(viewer: Viewer, ds, field: Tuple[str, str], left_edge: unyt_array | None = None, right_edge: unyt_array | None = None, level: int | None = 0, num_ghost_zones: int | None = 0, take_log: bool | None = None, colormap: str | None = None, link_to: str | Layer | None = None, rescale: bool | None = False, **kwargs)

uniformly sample a region from a yt dataset using a covering grid and add it to a viewer

Parameters:
  • viewer (napari.Viewer) – the active napari viewer

  • ds – the yt dataset to sample

  • field (Tuple[str, str]) – the field tuple to sample e.g., (‘enzo’, ‘Density’)

  • left_edge (unyt_array) – the left edge of the bounding box

  • right_edge (unyt_array) – the right edge of the bounding box

  • level (int) – the level to sample at (default 0)

  • num_ghost_zones (int) – number of ghost zones to inclue (default 0)

  • take_log (Optional[bool]) – if True, will take the log of the extracted data. Defaults to the default behavior for the field set by ds.

  • colormap (Optional[str]) – the color map to use, default is “viridis”

  • link_to (Optional[Union[str, Layer]]) – specify a layer to which the new layer should link

  • **kwargs – any keyword argument accepted by Viewer.add_image()

Examples

>>> import napari
>>> import yt
>>> from yt_napari.viewer import Scene
>>> viewer = napari.Viewer()
>>> ds = yt.load_sample("IsolatedGalaxy")
>>> yt_scene = Scene()
>>> yt_scene.add_region(viewer, ds, ("enzo", "Temperature"))
add_region(viewer: Viewer, ds, field: Tuple[str, str], resolution: Tuple[int, int, int] | None = None, left_edge: unyt_array | None = None, right_edge: unyt_array | None = None, take_log: bool | None = None, colormap: str | None = None, link_to: str | Layer | None = None, rescale: bool | None = False, **kwargs)

uniformly sample a region from a yt dataset and add it to a viewer

Parameters:
  • viewer (napari.Viewer) – the active napari viewer

  • ds – the yt dataset to sample

  • field (Tuple[str, str]) – the field tuple to sample e.g., (‘enzo’, ‘Density’)

  • left_edge (unyt_array) – the left edge of the bounding box

  • right_edge (unyt_array) – the right edge of the bounding box

  • resolution (Tuple[int, int, int]) – the sampling resolution in each dimension, e.g., (400, 400, 400)

  • take_log (Optional[bool]) – if True, will take the log of the extracted data. Defaults to the default behavior for the field set by ds.

  • colormap (Optional[str]) – the color map to use, default is “viridis”

  • link_to (Optional[Union[str, Layer]]) – specify a layer to which the new layer should link

  • **kwargs – any keyword argument accepted by Viewer.add_image()

Examples

>>> import napari
>>> import yt
>>> from yt_napari.viewer import Scene
>>> viewer = napari.Viewer()
>>> ds = yt.load_sample("IsolatedGalaxy")
>>> yt_scene = Scene()
>>> yt_scene.add_region(viewer, ds, ("enzo", "Temperature"))
add_slice(viewer: Viewer, ds, normal: str | int, field: Tuple[str, str], center: unyt_array | None = None, resolution: Tuple[int, int] | None = (400, 400), width: unyt_quantity | None = None, height: unyt_quantity | None = None, take_log: bool | None = None, periodic: bool | None = False, colormap: str | None = None, link_to: str | Layer | None = None, rescale: bool | None = False, **kwargs)

sample an orthogonal slice from a yt dataset and add it to a viewer

Parameters:
  • viewer (napari.Viewer) – the active napari viewer

  • ds – the yt dataset to sample

  • normal (str, int) – the normal axis of the slice, either an axis name or number

  • field (Tuple[str, str]) – the field tuple to sample e.g., (‘enzo’, ‘Density’)

  • center (unyt_array) – the center of the slice (3D)

  • width (unyt_quantity) – the width of the slice

  • height (unyt_quantity) – the height of the slice

  • resolution (Tuple[int, int]) – the sampling resolution in each dimension, e.g., (400, 400)

  • take_log (Optional[bool]) – if True, will take the log of the extracted data. Defaults to the default behavior for the field set by ds.

  • periodic (Optional[bool]) – use periodic bounds for the slice, default False

  • colormap (Optional[str]) – the color map to use, default is “viridis”

  • link_to (Optional[Union[str, Layer]]) – specify a layer to which the new layer should link

  • **kwargs – any keyword argument accepted by Viewer.add_image()

Examples

>>> import napari
>>> import yt
>>> from yt_napari.viewer import Scene
>>> viewer = napari.Viewer()
>>> ds = yt.load_sample("IsolatedGalaxy")
>>> yt_scene = Scene()
>>> yt_scene.add_slice(viewer, ds, "x", ("enzo", "Temperature"))
add_to_viewer(viewer: Viewer, ds, field: Tuple[str, str], resolution: Tuple[int, int, int] | None = None, left_edge: unyt_array | None = None, right_edge: unyt_array | None = None, take_log: bool | None = None, colormap: str | None = None, link_to: str | Layer | None = None, **kwargs)

deprecated, will be removed in v>0.1.0. use add_region

get_data_range(layers: List[str | Layer], layer_list: LayerList | None = None, check_linked: bool | None = True) Tuple[float, float]

retrieve the extrema across layers

Parameters:
  • layers (List[Union[str, Tuple]]) – a list of the layers to normalize across

  • layer_list (Optional[LayerList]) – the LayerList from an active napari Viewer. Required if you are providing layers by layer name only

  • check_linked (Optional[bool]) – if True (default), will also check for linked layers even if they are not explicitly included in layers.

Returns:

length-2 tuple with the min and max values across layers.

Return type:

Tuple(float, float)

Examples

>>> import napari
>>> import yt
>>> from yt_napari.viewer import Scene
>>> viewer = napari.Viewer()
>>> ds = yt.load_sample("IsolatedGalaxy")
>>> yt_scene = Scene()
>>> yt_scene.add_region(viewer, ds, ("enzo", "Temperature"))
>>> yt_scene.get_data_range(viewer.layers)
(3.2446250040130398, 5.003147905498429)
normalize_color_limits(layers: List[str | Layer], layer_list: LayerList | None = None, check_linked: bool | None = True)

normalize the color limits (the contrast_limits) across layers.

Parameters:
  • layers (List[Union[str, Tuple]]) – a list of the layers to normalize across

  • layer_list (Optional[LayerList]) – the LayerList from an active napari Viewer. Required if you are providing layers by layer name only

  • check_linked (Optional[bool]) – if True (default), will also check for linked layers even if they are not explicitly included in layers.

Notes

This method does not affect the linked state of any layers.

Examples

The following adds two layers and then normalizes the color scale between the two:

>>> import napari
>>> import yt
>>> from yt_napari.viewer import Scene
>>> viewer = napari.Viewer()
>>> ds = yt.load_sample("IsolatedGalaxy")
>>> yt_scene = Scene()
>>> le = ds.domain_center - ds.arr([10, 10, 10], 'kpc')
>>> re = ds.domain_center + ds.arr([10, 10, 10], 'kpc')
>>> yt_scene.add_region(viewer,
>>>                        ds,
>>>                        ("enzo", "Density"),
>>>                        left_edge = le,
>>>                        right_edge = re,
>>>                        resolution=(600, 600, 600),
>>>                        name="Density_1")
>>> le = ds.domain_center + ds.arr([10, 10, 10], 'kpc')
>>> re = le + ds.arr([20, 20, 20], 'kpc')
>>> yt_scene.add_region(viewer,
>>>                        ds,
>>>                        ("enzo", "Density"),
>>>                        left_edge = le,
>>>                        right_edge = re,
>>>                        resolution=(300, 300, 300),
>>>                        name="Density_2")
>>> yt_scene.normalize_color_limits(["Density_2", "Density_1"], viewer.layers)
set_across_layers(layers: List[str | Layer], attribute: str, value: Any, layer_list: LayerList | None = None)

set the value of an attribute for all the provided layers

Parameters:
  • layers – a list of napari Layer objects or string names of layers.

  • attribute – the layer attribute to set

  • value – the value of the attribute to set

  • layer_list – the active napari LayerList. required if providing layers by name.

Notes

Any existing layers that are linked to the provided layers will also be updated.

yt_napari.timeseries module

class yt_napari.timeseries.CoveringGrid(field: Tuple[str, str], left_edge: unyt_array | Tuple[ndarray, str] | None = None, right_edge: unyt_array | Tuple[ndarray, str] | None = None, level: int | None = 0, num_ghost_zones: int | None = 0, take_log: bool | None = None)

Bases: _RegionBase

sample_ds(ds)

sample a yt dataset with the selection object

class yt_napari.timeseries.Region(field: Tuple[str, str], left_edge: unyt_array | Tuple[ndarray, str] | None = None, right_edge: unyt_array | Tuple[ndarray, str] | None = None, resolution: Tuple[int, int, int] | None = (400, 400, 400), take_log: bool | None = None)

Bases: _RegionBase

A 3D rectangular selection through a domain.

Parameters:
  • field ((str, str)) – a yt field present in all timeseries to load.

  • left_edge (unyt_array or (ndarray, str)) – (optional) a 3-element unyt_array defining the left edge of the region, defaults to the domain left_edge of each active timestep.

  • right_edge (unyt_array or (ndarray, str)) – (optional) a 3-element unyt_array defining the right edge of the region, defaults to the domain right_edge of each active timestep.

  • resolution ((int, int, int)) – (optional) 3-element tuple defining the resolution to sample at. Default is (400, 400, 400).

  • take_log (bool) – (optional) If True, take the log10 of the sampled field. Defaults to the default behavior for the field in the dataset.

nd: int = 3
sample_ds(ds)

return a fixed resolution sample of a field in a yt dataset.

Parameters:

ds (yt dataset) – the yt dataset to sample

Examples

>>> import yt
>>> import numpy as np
>>> from yt_napari.timeseries import Region
>>> ds = yt.load_sample("IsolatedGalaxy")
>>> le = np.array([0.4, 0.4, 0.4], 'Mpc')
>>> re = np.array([0.6, 0.6, 0.6], 'Mpc')
>>> reg = Region(("enzo", "Density"), left_edge=le, right_edge=re)
>>> reg_data = reg.sample_ds(ds)

Notes

This is equivalent to ds.r[…,…,..][field], but is a useful abstraction for applying the same selection to a series of datasets.

class yt_napari.timeseries.Slice(field: Tuple[str, str], normal: str | int, center: unyt_array | Tuple[ndarray, str] | None = None, width: unyt_quantity | Tuple[float, str] | None = None, height: unyt_quantity | Tuple[float, str] | None = None, resolution: Tuple[int, int] | None = (400, 400), periodic: bool | None = False, take_log: bool | None = None)

Bases: _Selection

A 2D axis-normal slice through a domain.

Parameters:
  • field ((str, str)) – a yt field present in all timeseries to load.

  • normal (int or str) – the normal axis for slicing

  • center (unyt_array) – (optional) a 3-element unyt_array defining the slice center, defaults to the domain center of each active timestep.

  • width (unyt_quantity or (value, unit)) – (optional) the slice width, defaults to the domain width of each active timestep.

  • height (unyt_quantity or (value, unit)) – (optional) the slice height, defaults to the domain height of each active timestep.

  • resolution ((int, int)) – (optional) 2-element tuple defining the resolution to sample at. Default is (400, 400).

  • periodic (bool) – (optional, default is False) If True, treat domain as periodic

  • take_log (bool) – (optional) If True, take the log10 of the sampled field. Defaults to the default behavior for the field in the dataset.

nd: int = 2
sample_ds(ds)

return a fixed resolution slice of a field in a yt dataset.

Parameters:

ds (yt dataset) – the yt dataset to sample

Examples

>>> import yt
>>> from unyt import unyt_quantity
>>> from yt_napari.timeseries import Slice
>>> ds = yt.load_sample("IsolatedGalaxy")
>>> w = unyt_quantity(0.2, 'Mpc')
>>> slc = Slice(("enzo", "Density"), "x", width=w, height=w)
>>> slc_data = slc.sample_ds(ds)

Notes

This is equivalent to ds.slice(…).to_frb()[field], but is a useful abstraction for applying the same selection to a series of datasets.

yt_napari.timeseries.add_to_viewer(viewer: Viewer, selection: Slice | Region, file_dir: str | None = None, file_pattern: str | None = None, file_list: List[str] | None = None, file_range: Tuple[int, int, int] | None = None, load_as_stack: bool | None = False, use_dask: bool | None = False, return_delayed: bool | None = True, stack_scaling: float | None = 1.0, **kwargs)

Sample a timeseries and add to a napari viewer

Parameters:
  • viewer (napari.Viewer) – a napari Viewer instance

  • selection (Slice or Region) – the selection to apply to each matched dataset

  • file_dir (str) – (optional) a file directory to prepend to either the file_pattern or file_list argument.

  • file_pattern (str) – (optional) a file pattern to match, not used if file_list is set. One of file_pattern or file_list must be set.

  • file_list (str) – (optional) a list of files to use. One of file_list or file_pattern must be set.

  • file_range ((int, int, int)) – (optional) A range to limit matched files in the form (start, stop, step).

  • load_as_stack (bool) – (optional, default False) If True, the timeseries will be stacked to a single image array

  • use_dask (bool) – (optional, default False) If True, use dask to assemble the image array

  • return_delayed (bool) – (optional, default True) If True and if use_dask=True, then the image array will be a delayed array, resulting in lazy loading in napari. If False and if use_dask=True, then dask will distribute sampling tasks and assemble a final in-memory array.

  • stack_scaling (float) – (optional, default 1.0) Applies a scaling to the effective image array in the stacked (time) dimension if load_as_stack is True. If scale is provided as a separate parameter, then stack_scaling is only used if the len(scale) matches the dimensionality of the spatial selection.

  • **kwargs – any additional keyword arguments are passed to napari.Viewer().add_image()

Examples

>>> import napari
>>> from yt_napari.timeseries import Slice, add_to_viewer
>>> viewer = napari.Viewer()
>>> slc = Slice(("enzo", "Density"), "x")
>>> enzo_files = "enzo_tiny_cosmology/DD????/DD????"
>>> add_to_viewer(viewer, slc, file_pattern=enzo_files, file_range=(0,47, 5),
>>>                load_as_stack=True)