Skip to content

ndv.data #

Sample data for testing and examples.

Functions:

  • astronaut

    Load RGB data (512, 512, 3), uint8.

  • cat

    Load RGB cat data (300, 451, 3), uint8.

  • cells3d

    Load cells3d from scikit-image (60, 2, 256, 256) uint16.

  • cosem_dataset

    Load a dataset from the COSEM/OpenOrganelle project.

  • nd_sine_wave

    5D dataset: (10, 3, 5, 512, 512), float64.

astronaut #

astronaut() -> ndarray

Load RGB data (512, 512, 3), uint8.

Requires imageio to be installed.

Source code in ndv/data.py
85
86
87
88
89
90
def astronaut() -> np.ndarray:
    """Load RGB data `(512, 512, 3)`, uint8.

    Requires [imageio](https://pypi.org/project/imageio/) to be installed.
    """
    return _imread("imageio:astronaut.png")

cat #

cat() -> ndarray

Load RGB cat data (300, 451, 3), uint8.

Requires imageio to be installed.

Source code in ndv/data.py
77
78
79
80
81
82
def cat() -> np.ndarray:
    """Load RGB cat data `(300, 451, 3)`, uint8.

    Requires [imageio](https://pypi.org/project/imageio/) to be installed.
    """
    return _imread("imageio:chelsea.png")

cells3d #

cells3d() -> ndarray

Load cells3d from scikit-image (60, 2, 256, 256) uint16.

Requires imageio and tifffile to be installed.

Source code in ndv/data.py
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
def cells3d() -> np.ndarray:
    """Load cells3d from scikit-image `(60, 2, 256, 256)` uint16.

    Requires `imageio and tifffile` to be installed.
    """
    try:
        from imageio.v2 import volread
    except ImportError as e:
        raise ImportError(
            "Please `pip install imageio[tifffile]` to load cells3d"
        ) from e

    url = "https://gitlab.com/scikit-image/data/-/raw/2cdc5ce89b334d28f06a58c9f0ca21aa6992a5ba/cells3d.tif"
    data = np.asarray(volread(url))

    # this data has been stretched to 16 bit, and lacks certain intensity values
    # add a small random integer to each pixel ... so the histogram is not silly
    data = (data + np.random.randint(-24, 24, data.shape)).clip(0, 65535)
    return data.astype(np.uint16)

cosem_dataset #

cosem_dataset(
    uri: str = "",
    dataset: str = "jrc_hela-3",
    label: str = "er-mem_pred",
    level: int = 4,
) -> Any

Load a dataset from the COSEM/OpenOrganelle project.

Search for available options at: https://openorganelle.janelia.org/datasets

Requires tensorstore to be installed.

Parameters:

  • uri #

    (str, default: '' ) –

    The URI of the dataset to load. If not provided, the default URI is f"{dataset}/{dataset}.n5/labels/{label}/s{level}/".

  • dataset #

    (str, default: 'jrc_hela-3' ) –

    The name of the dataset to load. Default is "jrc_hela-3".

  • label #

    (str, default: 'er-mem_pred' ) –

    The label to load. Default is "er-mem_pred".

  • level #

    (int, default: 4 ) –

    The pyramid level to load. Default is 4.

Source code in ndv/data.py
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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
def cosem_dataset(
    uri: str = "",
    dataset: str = "jrc_hela-3",
    label: str = "er-mem_pred",
    level: int = 4,
) -> Any:
    """Load a dataset from the COSEM/OpenOrganelle project.

    Search for available options at: <https://openorganelle.janelia.org/datasets>

    Requires [tensorstore](https://pypi.org/project/tensorstore/) to be installed.

    Parameters
    ----------
    uri : str, optional
        The URI of the dataset to load. If not provided, the default URI is
        `f"{dataset}/{dataset}.n5/labels/{label}/s{level}/"`.
    dataset : str, optional
        The name of the dataset to load. Default is "jrc_hela-3".
    label : str, optional
        The label to load. Default is "er-mem_pred".
    level : int, optional
        The pyramid level to load. Default is 4.
    """
    try:
        import tensorstore as ts
    except ImportError:
        raise ImportError("Please install tensorstore to fetch cosem data") from None

    if not uri:
        uri = f"{dataset}/{dataset}.n5/labels/{label}/s{level}/"

    ts_array = ts.open(
        {
            "driver": "n5",
            "kvstore": {
                "driver": "s3",
                "bucket": "janelia-cosem-datasets",
                "path": uri,
            },
            # 1GB cache... but i don't think it's working
            "cache_pool": {"total_bytes_limit": 1e9},
        },
    ).result()
    ts_array = ts_array[ts.d[:].label["z", "y", "x"]]
    return ts_array[ts.d[("y", "x", "z")].transpose[:]]

nd_sine_wave #

nd_sine_wave(
    shape: tuple[int, int, int, int, int] = (
        10,
        3,
        5,
        512,
        512,
    ),
    amplitude: float = 240,
    base_frequency: float = 5,
) -> ndarray

5D dataset: (10, 3, 5, 512, 512), float64.

Source code in ndv/data.py
12
13
14
15
16
17
18
19
20
21
22
23
24
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
def nd_sine_wave(
    shape: tuple[int, int, int, int, int] = (10, 3, 5, 512, 512),
    amplitude: float = 240,
    base_frequency: float = 5,
) -> np.ndarray:
    """5D dataset: `(10, 3, 5, 512, 512)`, float64."""
    # Unpack the dimensions
    if not len(shape) == 5:
        raise ValueError("Shape must have 5 dimensions")
    angle_dim, freq_dim, phase_dim, ny, nx = shape

    # Create an empty array to hold the data
    output = np.zeros(shape)

    # Define spatial coordinates for the last two dimensions
    half_per = base_frequency * np.pi
    x = np.linspace(-half_per, half_per, nx)
    y = np.linspace(-half_per, half_per, ny)
    y, x = np.meshgrid(y, x)

    # Iterate through each parameter in the higher dimensions
    for phase_idx in range(phase_dim):
        for freq_idx in range(freq_dim):
            for angle_idx in range(angle_dim):
                # Calculate phase and frequency
                phase = np.pi / phase_dim * phase_idx
                frequency = 1 + (freq_idx * 0.1)  # Increasing frequency with each step

                # Calculate angle
                angle = np.pi / angle_dim * angle_idx
                # Rotate x and y coordinates
                xr = np.cos(angle) * x - np.sin(angle) * y
                np.sin(angle) * x + np.cos(angle) * y

                # Compute the sine wave
                sine_wave = (amplitude * 0.5) * np.sin(frequency * xr + phase)
                sine_wave += amplitude * 0.5

                # Assign to the output array
                output[angle_idx, freq_idx, phase_idx] = sine_wave

    return output