of_script

OrientationField scripting module. Many functions in here refer to the exact term, that is “nematic field”.

orientationfield.of_script.get_help(widget)

Return tooltip of a widget. Same as widget.tooltip.

Parameters:

widget (str or FunctionGui) – The widget or widget name

orientationfield.of_script.proj_distance(p, l)

Distance between a point p and its projection on the extension of a line passing by two points, provided in a tuple l. Also known as the “perpendicular distance”.

Return type:

float

orientationfield.of_script.rdp(points_list, eps)

Internal implementation of RDP algorithm used by rdp_polygon.

orientationfield.of_script.rdp_polygon(polygon, eps)

Ramer-Douglas-Peucker algorithm for vertex reduction. Adapted to work on a polygon. Splits the polygon into two lines based on the “middle” vertex, and performs the classical algorithm as described in the wikipedia page.

Return type:

ndarray

Parameters:
  • polygon (list[np.ndarray]) – List of points used to construct the polygon.

  • eps (float) – Minimal distance between two vertices in the result.

orientationfield.of_script.compute_nematic_field(img: Image, sigma: float = 3.0, cutoff_ratio: float = 2.0, decolorize: bool = False, decolorize_axis: int = 2, normalize: bool = True, normalize_options: str = 'total')

Compute nematic field of an Image layer img, and stores the result in layer metadata as a img.data.shape+(2,2)-array keyword nematic_field.

Parameters:
  • img (Image) – the Image layer.

  • sigma (float) – Optional. Bandwidth of kernel. Defaults to 3.0.

  • decolorize (bool) – Whether image has an RGB (or RGBA) channel that needs to be collapsed. Defaults to True.

  • decolorize_axis (int) – The axis of the image that has the RGB (or RGBA) channel. Defaults to 2.

  • normalize (bool) – Whether to normalize pixel intensity between 0 and 1. Defaults to True.

  • normalize_options (str) – Type of normalization. 'total' normalization normalizes with respect to the whole image, all channels included (time included) 'per frame' normalizes each slice along the first axis individually. Defaults to 'total'.

Returns:

Whether or not the field was computed.

Return type:

bool

Example:

>>> viewer.add_image(img_array) # img_array is a numpy array
>>> img_layer = viewer.layers[-1] # get the corresponding Image layer
>>> of_script.compute_nematic_field(img_layer, sigma=3.0, cutoff_ratio=2.0, decolorize=False)
>>> nem_field = img_layer.metadata["nematic_field"]
orientationfield.of_script.preview_kernel(**kwargs)

Display and/or return kernels for a given bandwidth and cutoff ratio. Unless specified, uses values in compute_nematic_field GUI.

Parameters:
  • sigma (float) – Optional. Bandwidth of the kernel.

  • cutoff_ratio (float) – Optional. Ratio between size of kernel and bandwidth.

  • show (bool) – Optional. Whether to add the kernel image to the viewer or not. Defaults to True.

Returns:

Kernel (Kxx as first component and Kxy as second component).

Return type:

ndarray

orientationfield.of_script.extract_nematic_points_layer(img: Image, mask: Labels | None, folder: pathlib.Path, box_size: int = 8, invert_mask: bool = False, return_early: bool = False)

Generate a points layer for an Image layer, to save to a csv in a specified folder. Points will be positioned in the center of boxes as they would be drawn in draw_nematic_field. To manually set the box size (in case of exterior use of this function), use the box_size keyword argument.

For video/multichannel image layers, this function will generate a video of points layers (which can be split later for batch processing).

If nematic field for the image is not computed, it will be generated with default parameters.

Parameters:
  • img (Image) – Image layer.

  • mask (Labels) – Optional. Labels of mask used to filter which points to include.

  • folder (pathlib.Path) – Path to folder where the CSV will be saved.

  • box_size (int) – Size of the boxes whose centers will be the points of this layer.

  • invert_mask (bool) – Whether mask filters in or filters out parts of the image.

Return type:

Points or None

Example:

>>> viewer.add_image(img_array) # img_array is a (h,w) array
>>> img_layer = viewer.layers[-1] # get the corresponding Image layer
>>> of_script.compute_nematic_field(img_layer, sigma=3.0, cutoff_ratio=2.0, decolorize=False)
>>> points = of_script.extract_nematic_points_layer(img_layer, box_size=8, return_early=True)
orientationfield.of_script.draw_nematic_field_svg(img: Image, box_size: int = 8, thresh=1e-4, color: bool = True, lengths: bool = False, length_scale: float = 1.5, custom_kwargs: str = '', ordered: int = 0)

Napari-specific function for generating a Shapes layer with line representation of every nematic in the field.

Parameters:
  • img (Image) – Image layer.

  • box_size (int) – Optional. Size of boxes over which the field is averaged. Defaults to 8.

  • thresh (float) – Optional. Nematic norm threshold. Norms are generally very low so this needs to be really low as well. Defaults to 1e-4.

  • color (bool) – Optional. Whether to use the magma cmap for the nematic field or not. Defaults to True.

  • lengths (bool) – Optional. Whether to scale bars with nematic magnitude or not. Defaults to False.

  • length_scale (float) – Optional. Amount by which to scale the magnitude of the nematics for the scalable display. Defaults to 1.5.

  • custom_kwargs (str) – String of custom keyword arguments passed to viewer.add_shapes on layer generation. Keywords should be specified in the format arg:val seperated by -, without spaces. Ignores the color argument explained previously. Defaults to "".

Example:

>>> nemfield = of_script.draw_nematic_field_svg(img, box_size=8, custom_kwargs:'edge_color:angle-edge_colormap:hsv')
orientationfield.of_script.find_defects(img: Image, box_size: int = -1, thresh: float = -1, mode: str = 'simplified')

Finds defects in the given image.

Parameters:
  • img (Image) – The Image layer to find defects in.

  • box_size (int) – Optional. The box size to use for defect computation. Defaults to draw_nematic_field_svg’s default box size parameter.

  • thresh (float) – Optional. The magnitude threshold to use for defect computation. Defaults to draw_nematic_field_svg’s default threshold parameter.

  • mode (str) – Optional. One of ("squares", "simplified"). Whether to represent the clusters as squares in an Image layer. Defaults to "simplified".

Returns:

Tuple of shapes layers containing the potential defects.

Return type:

tuple[Shapes, Shapes]