dacapo.utils.view

Classes

BestScore

Represents the best score achieved during a run.

NeuroglancerRunViewer

A class for viewing neuroglancer runs.

Functions

get_viewer(→ neuroglancer.Viewer | IPython.display.IFrame)

Creates a neuroglancer viewer to visualize arrays.

add_seg_layer(state, name, data, voxel_size[, meshes])

Add a segmentation layer to the Neuroglancer state.

add_scalar_layer(state, name, data, voxel_size)

Add a scalar layer to the state.

Module Contents

dacapo.utils.view.get_viewer(arrays: dict, width: int = 1500, height: int = 600, headless: bool = True, bind_address: str = '0.0.0.0', bind_port: int = 0) neuroglancer.Viewer | IPython.display.IFrame

Creates a neuroglancer viewer to visualize arrays.

Parameters:
  • arrays (dict) – A dictionary containing arrays to be visualized.

  • width (int, optional) – The width of the viewer window in pixels. Defaults to 1500.

  • height (int, optional) – The height of the viewer window in pixels. Defaults to 600.

  • headless (bool, optional) – If True, returns the viewer object. If False, returns an IFrame object embedding the viewer. Defaults to True.

  • bind_address (str, optional) – Bind address for Neuroglancer webserver.

  • bind_port (int, optional) – Bind port for Neuroglancer webserver.

Returns:

The neuroglancer viewer object or an IFrame object embedding the viewer.

Return type:

neuroglancer.Viewer | IFrame

Raises:

ValueError – If the array is not a numpy array or a neuroglancer.LocalVolume object.

Examples

>>> import numpy as np
>>> import neuroglancer
>>> from dacapo.utils.view import get_viewer
>>> arrays = {
...     "raw": {
...         "array": np.random.rand(100, 100, 100)
...     },
...     "seg": {
...         "array": np.random.randint(0, 10, (100, 100, 100)),
...         "is_seg": True
...     }
... }
>>> viewer = get_viewer(arrays)
>>> viewer
dacapo.utils.view.add_seg_layer(state, name, data, voxel_size, meshes=False)

Add a segmentation layer to the Neuroglancer state.

Parameters:
  • state (neuroglancer.ViewerState) – The Neuroglancer viewer state.

  • name (str) – The name of the segmentation layer.

  • data (ndarray) – The segmentation data.

  • voxel_size (list) – The voxel size in nm.

  • meshes (bool, optional) – Whether to generate meshes for the segments. Defaults to False.

Raises:

ValueError – If the data is not a numpy array.

Examples

>>> import numpy as np
>>> import neuroglancer
>>> from dacapo.utils.view import add_seg_layer
>>> state = neuroglancer.ViewerState()
>>> data = np.random.randint(0, 10, (100, 100, 100))
>>> voxel_size = [1, 1, 1]
>>> add_seg_layer(state, "seg", data, voxel_size)
dacapo.utils.view.add_scalar_layer(state, name, data, voxel_size)

Add a scalar layer to the state.

Parameters:
  • state (neuroglancer.ViewerState) – The viewer state to add the layer to.

  • name (str) – The name of the layer.

  • data (ndarray) – The scalar data to be displayed.

  • voxel_size (list) – The voxel size in nm.

Raises:

ValueError – If the data is not a numpy array.

Examples

>>> import numpy as np
>>> import neuroglancer
>>> from dacapo.utils.view import add_scalar_layer
>>> state = neuroglancer.ViewerState()
>>> data = np.random.rand(100, 100, 100)
>>> voxel_size = [1, 1, 1]
>>> add_scalar_layer(state, "raw", data, voxel_size)
class dacapo.utils.view.BestScore(run: dacapo.experiments.run.Run)

Represents the best score achieved during a run.

run

The run object associated with the best score.

Type:

Run

score

The best score achieved.

Type:

float

iteration

The iteration number at which the best score was achieved.

Type:

int

parameter

The parameter associated with the best score.

Type:

Optional[str]

validation_parameters

The validation parameters used during the run.

array_store

The array store object used to store prediction arrays.

stats_store

The stats store object used to store iteration scores.

ds

The dataset object associated with the best score.

get_ds(iteration, validation_dataset)

Retrieves the dataset object associated with the best score.

does_new_best_exist()

Checks if a new best score exists.

run: dacapo.experiments.run.Run
score: float
iteration: int = 0
parameter: str | None = None
validation_parameters
array_store
stats_store
get_ds(iteration, validation_dataset)

Retrieves the dataset object associated with the best score.

Parameters:
  • iteration (int) – The iteration number.

  • validation_dataset – The validation dataset object.

Raises:

FileNotFoundError – If the dataset object does not exist.

Examples

>>> from dacapo.experiments.run import Run
>>> from dacapo.utils.view import BestScore
>>> run = Run()
>>> best_score = BestScore(run)
>>> iteration = 0
>>> validation_dataset = run.datasplit.validate[0]
>>> best_score.get_ds(iteration, validation_dataset)
does_new_best_exist()

Checks if a new best score exists.

Returns:

True if a new best score exists, False otherwise.

Return type:

bool

Raises:

FileNotFoundError – If the dataset object does not exist.

Examples

>>> from dacapo.experiments.run import Run
>>> from dacapo.utils.view import BestScore
>>> run = Run()
>>> best_score = BestScore(run)
>>> new_best_exists = best_score.does_new_best_exist()
class dacapo.utils.view.NeuroglancerRunViewer(run: dacapo.experiments.run.Run, embedded=False)

A class for viewing neuroglancer runs.

run

The run object.

Type:

Run

best_score

The best score object.

Type:

BestScore

embedded

Whether the viewer is embedded.

Type:

bool

viewer

The neuroglancer viewer.

raw

The raw dataset.

gt

The ground truth dataset.

segmentation

The segmentation dataset.

most_recent_iteration

The most recent iteration.

run_thread

The run thread.

array_store

The array store object.

updated_neuroglancer_layer(layer_name, ds)

Update the neuroglancer layer with the given name and data source.

deprecated_start_neuroglancer()

Deprecated method to start the neuroglancer viewer.

start_neuroglancer()

Start the neuroglancer viewer.

start()

Start the viewer.

open_from_array_identitifier(array_identifier)

Open the array from the given identifier.

get_datasets()

Get the datasets for validation.

update_best_info()

Update the best info.

update_neuroglancer()

Update the neuroglancer viewer.

update_best_layer()

Update the best layer.

new_validation_checker()

Start a new validation checker thread.

update_with_new_validation_if_possible()

Update with new validation if possible.

stop()

Stop the viewer.

run: dacapo.experiments.run.Run
best_score
embedded
updated_neuroglancer_layer(layer_name, ds)

Update the neuroglancer layer with the given name and data source.

Parameters:
  • layer_name (str) – The name of the layer.

  • ds – The data source.

Raises:

FileNotFoundError – If the dataset object does not exist.

Examples

>>> from dacapo.experiments.run import Run
>>> from dacapo.utils.view import NeuroglancerRunViewer
>>> run = Run()
>>> viewer = NeuroglancerRunViewer(run)
>>> layer_name = "prediction"
>>> ds = viewer.run.datasplit.validate[0].raw._source_array
>>> viewer.updated_neuroglancer_layer(layer_name, ds)
deprecated_start_neuroglancer()

Deprecated method to start the neuroglancer viewer.

Returns:

The embedded viewer.

Return type:

IFrame

Raises:

FileNotFoundError – If the dataset object does not exist.

Examples

>>> from dacapo.experiments.run import Run
>>> from dacapo.utils.view import NeuroglancerRunViewer
>>> run = Run()
>>> viewer = NeuroglancerRunViewer(run)
>>> viewer.deprecated_start_neuroglancer()
start_neuroglancer(bind_address='0.0.0.0', bind_port=None)

Start the neuroglancer viewer.

Parameters:
  • bind_address (str, optional) – Bind address for Neuroglancer webserver.

  • bind_port (int, optional) – Bind port for Neuroglancer webserver.

Returns:

The embedded viewer.

Return type:

IFrame

Raises:

FileNotFoundError – If the dataset object does not exist.

Examples

>>> from dacapo.experiments.run import Run
>>> from dacapo.utils.view import NeuroglancerRunViewer
>>> run = Run()
>>> viewer = NeuroglancerRunViewer(run)
>>> viewer.start_neuroglancer()
start()

Start the viewer.

Returns:

The embedded viewer.

Return type:

IFrame

Raises:

FileNotFoundError – If the dataset object does not exist.

Examples

>>> from dacapo.experiments.run import Run
>>> from dacapo.utils.view import NeuroglancerRunViewer
>>> run = Run()
>>> viewer = NeuroglancerRunViewer(run)
>>> viewer.start()
open_from_array_identitifier(array_identifier)

Open the array from the given identifier.

Parameters:

array_identifier – The array identifier.

Returns:

The opened dataset or None if it doesn’t exist.

Raises:

FileNotFoundError – If the dataset object does not exist.

Examples

>>> from dacapo.experiments.run import Run
>>> from dacapo.utils.view import NeuroglancerRunViewer
>>> run = Run()
>>> viewer = NeuroglancerRunViewer(run)
>>> array_identifier = viewer.run.datasplit.validate[0].raw._source_array
>>> ds = viewer.open_from_array_identitifier(array_identifier)
get_datasets()

Get the datasets for validation.

Parameters:

run (Run) – The run object.

Returns:

The raw and ground truth datasets for validation.

Raises:

FileNotFoundError – If the dataset object does not exist.

Examples

>>> from dacapo.experiments.run import Run
>>> from dacapo.utils.view import NeuroglancerRunViewer
>>> run = Run()
>>> viewer = NeuroglancerRunViewer(run)
>>> viewer.get_datasets()
update_best_info()

Update the best info.

Parameters:

run (Run) – The run object.

Returns:

The embedded viewer.

Return type:

IFrame

Raises:

FileNotFoundError – If the dataset object does not exist.

Examples

>>> from dacapo.experiments.run import Run
>>> from dacapo.utils.view import NeuroglancerRunViewer
>>> run = Run()
>>> viewer = NeuroglancerRunViewer(run)
>>> viewer.update_best_info()
update_neuroglancer()

Update the neuroglancer viewer.

Parameters:

run (Run) – The run object.

Raises:

FileNotFoundError – If the dataset object does not exist.

Examples

>>> from dacapo.experiments.run import Run
>>> from dacapo.utils.view import NeuroglancerRunViewer
>>> run = Run()
>>> viewer = NeuroglancerRunViewer(run)
>>> viewer.update_neuroglancer()
update_best_layer()

Update the best layer.

Parameters:

run (Run) – The run object.

Returns:

The embedded viewer.

Return type:

IFrame

Raises:

FileNotFoundError – If the dataset object does not exist.

Examples

>>> from dacapo.experiments.run import Run
>>> from dacapo.utils.view import NeuroglancerRunViewer
>>> run = Run()
>>> viewer = NeuroglancerRunViewer(run)
>>> viewer.update_best_layer()
new_validation_checker()

Start a new validation checker thread.

Parameters:

run (Run) – The run object.

Returns:

The embedded viewer.

Return type:

IFrame

Raises:

FileNotFoundError – If the dataset object does not exist.

Examples

>>> from dacapo.experiments.run import Run
>>> from dacapo.utils.view import NeuroglancerRunViewer
>>> run = Run()
>>> viewer = NeuroglancerRunViewer(run)
>>> viewer.new_validation_checker()
update_with_new_validation_if_possible()

Update with new validation if possible.

Parameters:

run (Run) – The run object.

Returns:

The embedded viewer.

Return type:

IFrame

Raises:

FileNotFoundError – If the dataset object does not exist.

Examples

>>> from dacapo.experiments.run import Run
>>> from dacapo.utils.view import NeuroglancerRunViewer
>>> run = Run()
>>> viewer = NeuroglancerRunViewer(run)
>>> viewer.update_with_new_validation_if_possible()
stop()

Stop the viewer.

Parameters:

run (Run) – The run object.

Raises:

FileNotFoundError – If the dataset object does not exist.

Returns:

The embedded viewer.

Return type:

IFrame

Examples

>>> from dacapo.experiments.run import Run
>>> from dacapo.utils.view import NeuroglancerRunViewer
>>> run = Run()
>>> viewer = NeuroglancerRunViewer(run)
>>> viewer.stop()