jtlib package

Module contents

Submodules

jtlib.features module

class jtlib.features.Features(label_image, intensity_image=None)

Bases: object

Abstract base class for the extraction of features from images.

Parameters:

label_image: numpy.ndarray[numpy.int32]

labeled image encoding objects (connected pixel components) for which features should be extracted

intensity_image: numpy.ndarray[numpy.uint16 or numpy.uint8]

grayscale image from which texture features should be extracted (default: None)

Raises:

TypeError

when intensity_image doesn’t have unsigned integer type

ValueError

when intensity_image and label_image don’t have identical shape or when label_image is not 2D

check_assignment(ref_label_image, aggregate)

Determines whether aggregation of features is required in order to assign the values to objects in ref_label_image.

Parameters:

ref_label_image: numpy.ndarray[numpy.int32]

reference label image to which feature values should be assigned

aggregate: bool

whether feature values should be aggregated

Raises:

ValueError

when assignment of feature values to objects is not possible

extract()

Extracts features for segmented objects.

Returns:

pandas.DataFrame[float]

extracted feature values for each object in label_image; n*x*p dataframe, where n is the number of objects and p is the number of features

extract_aggregate(ref_label_image)

Extracts aggregate features for segmented objects.

Parameters:

ref_label_image: numpy.ndarray[numpy.int32]

reference label image encoding objects to which computed aggregate features should be assigned

Returns:

pandas.DataFrame[float]

extracted feature values for each object in ref_label_image n*x*p dataframe, where n is the number of objects and p is the number of features times the number of aggregate statistics computed for each feature

get_object_intensity_image(object_id)

Extracts the bounding box for a given object from intensity_image.

Returns:

numpy.ndarray[numpy.uint16 or numpy.uint8]

intensity image for given object; the size of the image is determined by the bounding box of the object

get_object_mask_image(object_id)

Extracts the bounding box for a given object from label_image.

Returns:

numpy.ndarray[bool]

mask image for given object

n_objects

int: number of objects in label_image.

names

List[str]: names of features that should be extracted from label_image

object_ids

numpy.array[numpy.int]: label of each object in label_image.

object_properties

Dict[int, skimage.measure._regionprops._RegionProperties]: mapping of object id to properties for each object in label_image.

plot()
class jtlib.features.Intensity(label_image, intensity_image)

Bases: jtlib.features.Features

Class for calculating intensity statistics, such as mean and variation of pixel values within segmented objects.

Parameters:

label_image: numpy.ndarray[numpy.int32]

labeled image encoding objects (connected pixel components) for which features should be extracted

intensity_image: numpy.ndarray[numpy.uint16 or numpy.uint8]

grayscale image from which texture features should be extracted

extract()

Extracts intensity features by measuring maximum, minimum, sum, mean and the standard deviation of pixel values within each object region in the intensity image.

Returns:

pandas.DataFrame

extracted feature values for each object in label_image

class jtlib.features.Morphology(label_image, compute_zernike=False)

Bases: jtlib.features.Features

Class for extracting morphology features.

Parameters:

label_image: numpy.ndarray[numpy.int32]

labeled image encoding objects (connected pixel components) for which features should be extracted

compute_zernike: bool, optional

whether Zernike moments should be computed (default: False)

extract()

Extracts morphology features to measure the size and shape of objects.

Returns:

pandas.DataFrame

extracted feature values for each object in label_image

class jtlib.features.PointPattern(label_image, parent_label_image)

Bases: jtlib.features.Features

Class for performing a point pattern analysis.

Parameters:

label_image: numpy.ndarray[numpy.int32]

labeled image encoding objects (connected pixel components) for which point pattern should be assessed

parent_label_image: numpy.ndarray[numpy.int32]

labeled image encoding parent objects (connected pixel components) relative to which point patterns should be assessed

extract()

Extracts point pattern features.

Returns:

pandas.DataFrame

extracted feature values for each object in label_image

get_parent_object_mask_image(parent_object_id)

Extracts the bounding box for a given parent object from parent_label_image.

Returns:

numpy.ndarray[bool]

mask image for given object

get_points_object_label_image(parent_object_id)

Extracts the bounding box for a given parent object from label_image.

Parameters:

parant_object_id: int

ID of object in parent_label_image

Returns:

numpy.ndarray[numpy.int32]

label image for all objects falling within the bounding box of the given parent object

parent_object_ids

List[int]: IDs of objects in parent_label_image

class jtlib.features.Texture(label_image, intensity_image, theta_range=4, frequencies=set([1, 10, 5]), radius=set([1, 10, 5]), threshold=None, compute_haralick=False)

Bases: jtlib.features.Features

Class for calculating texture features based on Haralick [R1], Gabor [2], Hu [R3], Threshold Adjancency Statistics (TAS) [R4] and Local Binary Patterns [R5].

References

[R1](1, 2) Haralick R.M. (1979). “Statistical and structural approaches to texture”. Proceedings of the IEEE
[R2]Fogel I. et al. (1989). “Gabor filters as texture discriminator”. Biological Cybernetics
[R3](1, 2) Hu M.K. (1962). “Visual Pattern Recognition by Moment Invariants”, IRE Trans. Info. Theory
[R4](1, 2) Hamilton N.A. et al. (2007). “Fast automated cell phenotype image classification”. BMC Bioinformatics
[R5](1, 2) Ojala T. et al. (2000). “Gray Scale and Rotation Invariant Texture Classification with Local Binary Patterns”. Lecture Notes in Computer Science
Parameters:

label_image: numpy.ndarray[numpy.int32]

labeled image encoding objects (connected pixel components) for which features should be extracted

intensity_image: numpy.ndarray[numpy.uint16 or numpy.uint8]

grayscale image from which texture features should be extracted

theta_range: int, optional

number of angles to define the orientations of the Gabor filters (default: 4)

frequencies: Set[int], optional

frequencies of the Gabor filters (default: {1, 5, 10})

threshold: int, optional

threshold value for Threshold Adjacency Statistics (TAS) (defaults to value computed by Otsu’s method)

radius: Set[int], optional

radius for defining pixel neighbourhood for Local Binary Patterns (LBP) (default: {1, 5, 10})

compute_haralick: bool, optional

whether Haralick features should be computed (the computation is computationally expensive) (default: False)

extract()

Extracts Gabor texture features by filtering the intensity image with Gabor kernels for a defined range of frequency and theta values and then calculating a score for each object.

Returns:

pandas.DataFrame

extracted feature values for each object in label_image

jtlib.features.create_feature_image(feature_values, label_image)

Creates an image, where pixels belonging to an object (connected component) encode a given feature value.

Parameters:

feature_values: numpy.ndarray[numpy.float64]

vector of feature values of length n, where n is the number of labeled connected component in label_image

label_image: numpy.ndarray[numpy.int32]

labeled image with n unique label values

Returns:

numpy.ndarray[numpy.float64]

feature image

jtlib.plotting module

jtlib.plotting.COLORBAR_POSITION_MAPPING = {'ul': (0.5700000000000001, 0.43), 'll': (0, 0.43), 'lr': (0, 1), 'ur': (0.5700000000000001, 1)}

Mapping for the position of the colorbar relative to the figure.

jtlib.plotting.FIGURE_HEIGHT = 800

Absolute height of the figure in pixels.

jtlib.plotting.FIGURE_WIDTH = 800

Absolute width of the figure in pixels.

jtlib.plotting.IMAGE_RESIZE_FACTOR = 4

The factor by which the size of an image should be reduced for plotting. This helps reducing the amount of data that has to be send to the client.

jtlib.plotting.OBJECT_COLOR = 'rgb(255, 191, 0)'

Color used to represented segmented objects in binary mask images (on black background).

jtlib.plotting.PLOT_HEIGHT = 0.43

Relative height of each plot within the figure.

jtlib.plotting.PLOT_POSITION_MAPPING = {'ul': ('y1', 'x1'), 'll': ('y3', 'x3'), 'lr': ('y4', 'x4'), 'ur': ('y2', 'x2')}

Mapping for the position of the plot relative to the figure.

jtlib.plotting.PLOT_WIDTH = 0.43

Relative width of each plot within the figure.

jtlib.plotting.create_colorscale(name, n=256, permute=False, add_background=False, background_color='black')

Creates a color palette in the format required by plotly based on a matplotlib colormap. Please refer to plotly docs for more information on colorscale format.

Parameters:

name: str

name of a matplotlib colormap, e.g. "Greys"

n: int

number of colors (default: 256)

permute: bool, optional

whether colors should be randomly permuted (default: False)

add_background: bool, optional

whether a value for background should be prepented to the colorscale (default: False)

background_color: str, optional

whether “black” or “white” background should be used (default: "black")

Returns:

List[List[float, str]]

RGB color palette

Notes

You can invert a colorscale in a plotly graph using the reversescale argument.

Examples

>>> create_colorscale('Greys', 5)
[[0.0,  'rgb(255,255,255)'],
 [0.25, 'rgb(216,216,216)'],
 [0.5,  'rgb(149,149,149)'],
 [0.75, 'rgb(82,82,82)'],
 [1.0,  'rgb(0,0,0)']]
jtlib.plotting.create_figure(plots, plot_positions=['ul', 'ur', 'll', 'lr'], plot_is_image=[True, True, True, True], title='')

Creates a figure based on one or more subplots. Plots will be arranged as a 2x2 grid.

Parameters:

plots: List[plotly.graph_objs.Plot or List[plotly.graph_objs.Plot]]

subplots that should be used in the figure; subplots can be further nested, i.e. grouped together in case they should be combined at the same figure position

plot_positions: List[str], optional

relative position of each plot in the figure; "ul" -> upper left, "ur" -> upper right, "ll" lower left, "lr" -> lower right (default: ["ul", "ur", "ll", "lr"])

plot_is_image: List[bool], optional

whether a plot represents an image; for images the y axis is inverted accounting for the fact that the 0,0 coordinate is located in the upper left corner of the plot rather than in the default lower left corner (default: [True, True, True, True])

title: str, optional

title for the figure

Returns:

str

JSON string representation of the figure

Raises:

TypeError

when plot, plot_positions, or plot_is_image don’t have type list

ValueError

when length of plot, plot_positions, or plot_is_image is larger than 4

jtlib.plotting.create_float_image_plot(image, position, clip=True, clip_value=None)

Creates a heatmap plot for a floating point image.

Parameters:

image: numpy.ndarray[numpy.float]

2D floating point image

position: str

coordinate that defines the relative position of the plot within the figure; "ul" -> upper left, "ur" -> upper right, "ll" lower left, "lr" -> lower right

clip: bool, optional

whether intensity values should be clipped (default: True)

clip_value: int, optional

value above which intensity values should be clipped; the 99th percentile of intensity values will be used in case no value is provided; will only be considered when clip is True (default: None)

Returns:

plotly.graph_objs.graph_objs.Heatmap

jtlib.plotting.create_histogram_plot(data, position, color='grey')

Creates a histogram plot.

Parameters:

data: list or numpy.array

data that should be plotted

position: str

coordinate that defines the relative position of the plot within the figure; "ul" -> upper left, "ur" -> upper right, "ll" lower left, "lr" -> lower right

color: str, optional

color that should be used for the marker

Returns:

plotly.graph_objs.graph_objs.Histogram

jtlib.plotting.create_intensity_image_plot(image, position, clip=True, clip_value=None)

Creates a heatmap plot for an intensity image. Intensity values will be encode with greyscale colors.

Parameters:

image: numpy.ndarray[numpy.uint8 or numpy.uint16]

2D intensity image

position: str

coordinate that defines the relative position of the plot within the figure; "ul" -> upper left, "ur" -> upper right, "ll" lower left, "lr" -> lower right

clip: bool, optional

whether intensity values should be clipped (default: True)

clip_value: int, optional

value above which intensity values should be clipped; the 99th percentile of intensity values will be used in case no value is provided; will only be considered when clip is True (default: None)

Returns:

plotly.graph_objs.graph_objs.Heatmap

jtlib.plotting.create_intensity_overlay_image_plot(image, mask, position, clip=True, clip_value=None, color=None)

Creates an intensity image plot and overlays colorized mask outlines on top of the greyscale image.

Parameters:

image: numpy.ndarray[numpy.uint8 or numpy.uint16]

2D intensity image

position: str

coordinate that defines the relative position of the plot within the figure; "ul" -> upper left, "ur" -> upper right, "ll" lower left, "lr" -> lower right

clip: bool, optional

whether intensity values should be clipped (default: True)

clip_value: int, optional

value above which intensity values should be clipped; the 99th percentile of intensity values will be used in case no value is provided; will only be considered when clip is True (default: None)

color: str, optional

color of the mask outline; defaults to attribute:jtlib.plotting.OBJECT_COLOR in case no color is specified (default: None)

Returns:

plotly.graph_objs.graph_objs.Heatmap

jtlib.plotting.create_line_plot(y_data, x_data, position, color='grey', line_width=4)

Creates a line plot.

Parameters:

y_data: list or numpy.array

data that should be plotted along the y-axis

x_data: list or numpy.array

data that should be plotted along the x-axis

position: str

coordinate that defines the relative position of the plot within the figure; "ul" -> upper left, "ur" -> upper right, "ll" lower left, "lr" -> lower right

color: str, optional

color of the line (default: "grey")

line_width: int, optional

width of the line (default: 4)

Returns:

plotly.graph_objs.graph_objs.Scatter

jtlib.plotting.create_mask_image_plot(mask, position, colorscale=None)

Creates a heatmap plot for a mask image. Unique object labels will be encoded with RGB colors.

Parameters:

mask: numpy.ndarray[numpy.bool or numpy.int32]

binary or labeled 2D mask image

position: str

coordinate that defines the relative position of the plot within the figure; "ul" -> upper left, "ur" -> upper right, "ll" lower left, "lr" -> lower right

colorscale: List[List[int, str]]

colors that should be used to visually highlight the objects in the mask image; a default color map will be used if not provided; see plotly docs for more information of colorscale format (default: None)

Returns:

plotly.graph_objs.graph_objs.Heatmap

jtlib.plotting.create_mask_overlay_image_plot(mask, mask2, position, color=None)

Creates an mask image plot and overlays colorized mask outlines on top of the binary image.

Parameters:

mask: numpy.ndarray[numpy.bool]

2D mask image

outlines: numpy.ndarray[numpy.bool]

2D mask image that should be overlayed

position: str

coordinate that defines the relative position of the plot within the figure; "ul" -> upper left, "ur" -> upper right, "ll" lower left, "lr" -> lower right

color: str, optional

color of the mask outline; defaults to attribute:jtlib.plotting.OBJECT_COLOR in case no color is specified (default: None)

Returns:

plotly.graph_objs.graph_objs.Heatmap

jtlib.plotting.create_scatter_plot(y_data, x_data, position, color='grey', marker_size=4)

Creates a scatter plot.

Parameters:

y_data: list or numpy.array

data that should be plotted along the y-axis

x_data: list or numpy.array

data that should be plotted along the x-axis

position: str

coordinate that defines the relative position of the plot within the figure; "ul" -> upper left, "ur" -> upper right, "ll" lower left, "lr" -> lower right

color: str, optional

color of the points (default: "grey")

marker_size: int, optional

size of the points (default: 4)

Returns:

plotly.graph_objs.graph_objs.Scatter

jtlib.utils module

jtlib.utils.downsample(im, bins)

Murphy et al. 2002 “Robust Numerical Features for Description and Classification of Subcellular Location Patterns in Fluorescence Microscope Images”

Parameters:

im: numpy.ndarray

grayscale image

bins: int

number of bins

Returns:

numpy.ndarray

downsampled image

jtlib.utils.extract_bbox(im, bbox, pad=0)

Extracts a subset of pixels from a 2D image defined by a given bounding box.

Parameters:

im: numpy.ndarray

image

bbox: List[int]

bounding box coordinates

pad: int, optional

pad extracted image with n lines of zero values along each dimension (default: 0)

Returns:

numpy.ndarray

extracted pixels

jtlib.utils.get_border_ids(im)

Determines the ids (labels) of objects at the border of an image.

Parameters:

im: numpy.ndarray[numpy.int32]

labeled image

Returns:

List[int]

object ids

jtlib.utils.label(im, n=4)

Labels connected components in an image.

Parameters:

im: numpy.ndarray[numpy.bool]

binary image that should be labeled

n: int, optional

neighbourhood (default: 4, choices: {4, 8})

Returns:

numpy.ndarray[int]

labeled image

Raises:

ValueError

when n is not 4 or 8

jtlib.utils.rescale_to_8bit(im, lower=0, upper=100)

Maps pixel values of an image from the range between a lower and an upper quantile to the range [0,255].

Parameters:

im: numpy.ndarray[numpy.uint16]

16-bit grayscale image

lower: float, optional

quantile to define lower bound of range (default: 0)

upper: float, optional

quantile to define upper bound of range (default: 100)

Returns:

numpy.ndarray[numpy.uint8]

rescaled 8-bit grayscale image

jtlib.utils.sort_coordinates_counter_clockwise(coordinates)

Sorts y, x coordinates values in counter clockwise order.

Parameters:

coordinates: numpy.ndarray[int]

nx2 array of coordinate values, where each row represents a point and the 1. column are the y values and the 2. column the x values

Returns:

numpy.ndarray[np.int64]

sorted array

clip module

combine_channels module

combine_masks module

expand module

fill module

filter module

filter.gaussian_2d(size, sigma)

Creates a 2D Gaussian convolution filter.

Parameters:

size: int

width and height of the filter kernel

sigma: float

standard deviation

Returns:

numpy.ndarray[numpy.float]

convolution kernel

filter.log_2d(size, sigma)

Creates a 2D Laplacian of Gaussian convolution filter.

Parameters:

size: int

width and height of the filter kernel

sigma: float

standard deviation of the Gaussian component

Returns:

numpy.ndarray[numpy.float]

convolution kernel

invert module

label module

measure_texture module

measure_intensity module

measure_morphology module

register_objects module

relabel module

segment_secondary module

separate_clumps module

smooth module

threshold_adaptive module

threshold_manual module

threshold_otsu module