Droplet reconstruction from code

Droplet reconstruction from code#

In this tutorial, you will learn how to use the droplet reconstruction toolbox from code. The toolbox is part of the napari_stress package and can be imported as follows:

from napari_stress import sample_data, reconstruction
import napari
import numpy as np

We first create a napari viewer and add some sample data:

viewer = napari.Viewer(ndisplay=3)
WARNING: DirectWrite: CreateFontFaceFromHDC() failed (Indicates an error in an input file such as a font file.) for QFontDef(Family="", pointsize=12, pixelsize=16, styleHint=5, weight=50, stretch=100, hintingPreference=0) LOGFONT("MS Sans Serif", lfWidth=0, lfHeight=-16) dpi=192
Assistant skips harvesting pyclesperanto as it's not installed.
example_data = sample_data.get_droplet_4d()[0][0]
viewer.add_image(example_data, name='droplet')
<Image layer 'droplet' at 0x22899c861f0>

The reconstruction requires some arguments to be set. For more information about the arguments, see the interactive version of the toolbox.

arguments = {
    'voxelsize': np.array([2, 1, 1]),
    'target_voxelsize': 1,
    'resampling_length': 2.5,
    'interpolation_method'  : 'linear',
    'return_intermediate_results': True
}

You can also display all arguments and their meaning inside the notebook:

reconstruction.reconstruct_droplet?
Signature:
reconstruction.reconstruct_droplet(
    image: <function NewType.<locals>.new_type at 0x00000228FAD2D160>,
    voxelsize: numpy.ndarray = None,
    target_voxelsize: float = 1.0,
    smoothing_sigma: float = 1.0,
    n_smoothing_iterations: int = 10,
    n_points: int = 256,
    n_tracing_iterations: int = 1,
    resampling_length: float = 5,
    fit_type: str = 'fancy',
    edge_type: str = 'interior',
    trace_length: float = 10,
    remove_outliers: bool = True,
    outlier_tolerance: float = 1.5,
    sampling_distance: float = 0.5,
    interpolation_method: str = 'cubic',
    return_intermediate_results: bool = False,
) -> List[LayerDataTuple]
Docstring:
Reconstruct droplet surface from points layer.

Parameters
----------
image : ImageData
    Image data.
voxelsize : np.ndarray, optional
    Voxel size of image. The default is None.
target_voxelsize : float, optional
    Target voxel size for reconstruction. The default is 1.0.
smoothing_sigma : float, optional
    Sigma for gaussian smoothing. The default is 1.0.
n_smoothing_iterations : int, optional
    Number of smoothing iterations. The default is 10.
n_points : int, optional
    Number of points to be sampled. The default is 256.
n_tracing_iterations : int, optional
    Number of tracing iterations. The default is 1.
resampling_length : float, optional
    Distance between sampled point locations. The default is 5.
    Choose smaller values for more accurate reconstruction.
fit_type : str, optional
    Type of fit to be used. The default is "fancy". Can also be "quick".
edge_type : str, optional
    Type of edge to be used. The default is "interior". Can also be "surface".
trace_length : float, optional
    Length of traces along which to measure intensity. The default is 10.
remove_outliers : bool, optional
    Whether to remove outliers. The default is True.
outlier_tolerance : float, optional
    Tolerance for outlier removal. The default is 1.5.
sampling_distance : float, optional
    Distance between points sampled on traces. The default is 0.5.
interpolation_method : str, optional
    Interpolation method to be used. The default is "cubic". Can also be "linear".
    "cubic" is more accurate but slower.
use_dask: bool, optional
    Whether to use dask for parallelization. The default is False.
return_intermediate_results: bool, optional
    Whether to return intermediate results. The default is False.

Returns
-------
List[LayerDataTuple]
    List of napari layers:
        - rescaled image: image rescaled to target voxel size
        - label image: connected components of rescaled image
        - points first guess: points sampled on fibonacci grid
        - traced points: points after tracing
        - trace vectors: vectors along which intensity was measured
        - droplet center: center of droplet
File:      d:\johannes\github\napari-stress\src\napari_stress\_reconstruction\toolbox.py
Type:      function

Anyway - run the reconstruction with the following command. The result variable results_reconstruction contains all intermediate results:

results_reconstruction = reconstruction.reconstruct_droplet(example_data, **arguments)

You can add all the results layers to the viewer like this:

for result in results_reconstruction:
    layer = napari.layers.Layer.create(result[0], result[1], result[2])
    viewer.add_layer(layer)
WARNING: DirectWrite: CreateFontFaceFromHDC() failed (Indicates an error in an input file such as a font file.) for QFontDef(Family="", pointsize=12, pixelsize=16, styleHint=5, weight=50, stretch=100, hintingPreference=0) LOGFONT("MS Sans Serif", lfWidth=0, lfHeight=-16) dpi=192
napari.utils.nbscreenshot(viewer)