nima.nima#

Main library module.

Contains functions for the analysis of multichannel timelapse images. It can be used to apply dark, flat correction; segment cells from bg; label cells; obtain statistics for each label; compute ratio and ratio images between channels.

Functions:

myhist(im[, bins, log, nf])

Plot image intensity as histogram.

read_tiff(fp, channels)

Read multichannel tif timelapse image.

d_show(d_im, **kws)

Imshow for dictionary of image (d_im).

d_median(d_im)

Median filter on dictionary of image (d_im).

d_shading(d_im, dark, flat[, clip])

Shading correction on d_im.

bg(im[, kind, perc, radius, ...])

Bg segmentation.

d_bg(d_im[, downscale, kind, clip])

Bg segmentation for d_im.

d_mask_label(d_im[, min_size, channels, ...])

Label cells in d_im.

d_ratio(d_im[, name, channels, radii])

Ratio image between 2 channels in d_im.

d_meas_props(d_im[, channels, channels_cl, ...])

Calculate pH and cl ratios and labelprops.

d_plot_meas(bgs, meas, channels)

Plot meas object.

plt_img_profile(img[, title, hpix, vmin, vmax])

Summary graphics for Flat-Bias images.

plt_img_profile_2(img[, title])

Summary graphics for Flat-Bias images.

hotpixels(bias[, n_sd])

Identify hot pixels in a bias-dark frame.

correct_hotpixel(img, y, x)

Correct hot pixels in a frame.

nima.nima.myhist(im, bins=60, log=False, nf=False)#

Plot image intensity as histogram.

..note:: Consider deprecation.

Return type:

None

nima.nima.read_tiff(fp, channels)#

Read multichannel tif timelapse image.

Parameters:
  • fp (Path) – File (TIF format) to be opened.

  • channels (Sequence[str]) – List a name for each channel.

Return type:

tuple[dict[str, TypeVar(ImArray, ndarray[Any, dtype[int64]], ndarray[Any, dtype[float64]], ndarray[Any, dtype[bool_]])], int, int]

Returns:

  • d_im (dict[str, ImArray]) – Dictionary of images. Each keyword represents a channel, named according to channels string list.

  • n_channels (int) – Number of channels.

  • n_times (int) – Number of timepoints.

Raises:

Exception – When number of channels and total length of tif sequence does not match.

Examples

>>> d_im, n_channels, n_times = read_tiff('tests/data/1b_c16_15.tif',             channels=['G', 'R', 'C'])
>>> n_channels, n_times
(3, 4)
nima.nima.d_show(d_im, **kws)#

Imshow for dictionary of image (d_im). Support plt.imshow kws.

Return type:

Figure

nima.nima.d_median(d_im)#

Median filter on dictionary of image (d_im).

Same to skimage.morphology.disk(1) and to median filter of Fiji/ImageJ with radius=0.5.

Parameters:

d_im (dict[str, ImArray]) – dict of images

Returns:

d_im – dict of images preserve dtype of input

Return type:

dict[str, ImArray]

Raises:

Exception – When ImArray is neither a single image nor a stack.

nima.nima.d_shading(d_im, dark, flat, clip=True)#

Shading correction on d_im.

Subtract dark; then divide by flat.

Works either with flat or d_flat Need also dark for each channel because it can be different when using different acquisition times.

Parameters:
  • d_im (dict[str, ImArray]) – Dictionary of images.

  • dark (dict[str, ImArray] | NDArray[np.float_]) – Dark image (either a 2D image or 2D d_im).

  • flat (dict[str, ImArray] | NDArray[np.float_]) – Flat image (either a 2D image or 2D d_im).

  • clip (bool) – Boolean for clipping values >=0.

Returns:

Corrected d_im.

Return type:

dict[str, ImArray]

nima.nima.bg(im, kind='arcsinh', perc=10.0, radius=10, adaptive_radius=None, arcsinh_perc=80)#

Bg segmentation.

Return median, whole vector, figures (in a [list])

Parameters:
  • im (Im) – An image stack.

  • kind (str, optional) – Method {‘arcsinh’, ‘entropy’, ‘adaptive’, ‘li_adaptive’, ‘li_li’} used for the segmentation.

  • perc (float, optional) – Perc % of max-min (default=10) for thresholding entropy and arcsinh methods.

  • radius (int | None, optional) – Radius (default=10) used in entropy and arcsinh (percentile_filter) methods.

  • adaptive_radius (int | None, optional) – Size for the adaptive filter of skimage (default is im.shape[1]/2).

  • arcsinh_perc (int | None, optional) – Perc (default=80) used in the percentile_filter (scipy) within arcsinh method.

Return type:

tuple[float, ndarray[Any, dtype[int64]] | ndarray[Any, dtype[float64]], list[Figure]]

Returns:

  • median (float) – Median of the bg masked pixels.

  • pixel_values (NDArray[np.int_] | NDArray[np.float_]) – Values of all bg masked pixels.

  • figs (list[plt.Figure]) – List of fig(s). Only entropy and arcsinh methods have 2 elements.

Raises:

Exception – When % radius is out of bounds.

nima.nima.d_bg(d_im, downscale=None, kind='li_adaptive', clip=True)#

Bg segmentation for d_im.

Parameters:
  • d_im (dict[str, Im]) – desc

  • downscale (tuple[int, int] | None) – Tupla, x, y are downscale factors for rows, cols (default=None).

  • kind (str, optional) – Bg method among {‘li_adaptive’, ‘arcsinh’, ‘entropy’, ‘adaptive’, ‘li_li’}.

  • clip (bool, optional) – Boolean (default=True) for clipping values >=0.

Return type:

tuple[dict[str, TypeVar(Im, ndarray[Any, dtype[int64]], ndarray[Any, dtype[float64]])], DataFrame, dict[str, list[list[Figure]]], dict[str, list[ndarray[Any, dtype[int64]] | ndarray[Any, dtype[float64]]]]]

Returns:

  • d_cor (dict[str, Im]) – Dictionary of images subtracted for the estimated bg.

  • bgs (pd.DataFrame) – Median of the estimated bg; columns for channels and index for time points.

  • figs (dict[str, list[list[plt.Figure]]]) – List of (list ?) of figures.

  • d_bg_values (dict[str, list[NDArray[np.int_] | NDArray[np.float_]]]) – Background values keys are channels containing a list (for each time point) of list of values.

nima.nima.d_mask_label(d_im, min_size=640, channels=('C', 'G', 'R'), threshold_method='yen', wiener=False, watershed=False, clear_border=False, randomwalk=False)#

Label cells in d_im. Add two keys, mask and label.

Perform plane-by-plane (2D image): :rtype: None

  • geometric average of all channels;

  • optional wiener filter (3,3);

  • mask using threshold_method;

  • remove objects smaller than min_size;

  • binary closing;

  • optionally remove any object on borders;

  • label each ROI;

  • optionally perform watershed on labels.

Parameters:
  • d_im (dict[str, ImArray]) – desc

  • min_size (int | None, optional) – Objects smaller than min_size (default=640 pixels) are discarded from mask.

  • channels (Sequence[str], optional) – List a name for each channel.

  • threshold_method (str | None, optional) – Threshold method applied to the geometric average plane-by-plane (default=yen).

  • wiener (bool, optional) – Boolean for wiener filter (default=False).

  • watershed (bool, optional) – Boolean for watershed on labels (default=False).

  • clear_border (bool, optional) – Whether to filter out objects near the 2D image edge (default=False).

  • randomwalk (bool, optional) – Use random_walker instead of watershed post-ndimage-EDT (default=False).

Notes

Side effects:

Add a ‘label’ key to the d_im.

nima.nima.d_ratio(d_im, name='r_cl', channels=('C', 'R'), radii=(7, 3))#

Ratio image between 2 channels in d_im.

Add masked (bg=0; fg=ratio) median-filtered ratio for 2 channels. So, d_im must (already) contain keys for mask and the two channels.

After ratio computation any -inf, nan and inf values are replaced with 0. These values should be generated (upon ratio) only in the bg. You can check: r_cl[d_im[‘labels’]==4].min()

Parameters:
  • d_im (dict[str, ImArray]) – desc

  • name (str, optional) – Name (default=’r_cl’) for the new key.

  • channels (tuple[str, str], optional) – Names for the two channels (Numerator, Denominator) (default=(‘C’, ‘R’)).

  • radii (tuple[int, int], optional) – Each element contain a radius value for a median filter cycle (default=(7, 3)).

Return type:

None

Notes

Add a key named “name” and containing the calculated ratio to d_im.

nima.nima.d_meas_props(d_im, channels=('C', 'G', 'R'), channels_cl=('C', 'R'), channels_ph=('G', 'C'), ratios_from_image=True, radii=None)#

Calculate pH and cl ratios and labelprops.

Parameters:
  • d_im (dict[str, Im]) – desc

  • channels (Sequence[str], optional) – All d_im channels (default=(‘C’, ‘G’, ‘R’)).

  • channels_cl (tuple[str, str], optional) – Numerator and denominator channels for cl ratio (default=(‘C’, ‘R’)).

  • channels_ph (tuple[str, str], optional) – Numerator and denominator channels for pH ratio (default=(‘G’, ‘C’)).

  • ratios_from_image (bool, optional) – Boolean for executing d_ratio i.e. compute ratio images (default=True).

  • radii (tuple[int, int] | None, optional) – Radii of the optional median average performed on ratio images (default=None).

Return type:

tuple[dict[int32, DataFrame], dict[str, list[list[Any]]]]

Returns:

  • meas (dict[np.int32, pd.DataFrame]) – For each label in labels: {‘label’: df}. DataFrame columns are: mean intensity of all channels, ‘equivalent_diameter’, ‘eccentricity’, ‘area’, ratios from the mean intensities and optionally ratios from ratio-image.

  • pr (dict[str, list[list[Any]]]) – For each channel: {‘channel’: [props]} i.e. {‘channel’: [time][label]}.

nima.nima.d_plot_meas(bgs, meas, channels)#

Plot meas object.

Plot r_pH, r_cl, mean intensity for each channel and estimated bg over timepoints for each label (color coded).

Parameters:
  • bgs (pd.DataFrame) – Estimated bg returned from d_bg()

  • meas (dict[np.int32, pd.DataFrame]) – meas object returned from d_meas_props().

  • channels (Sequence[str]) – All bgs and meas channels (default=[‘C’, ‘G’, ‘R’]).

Returns:

Figure.

Return type:

plt.Figure

nima.nima.plt_img_profile(img, title=None, hpix=None, vmin=None, vmax=None)#

Summary graphics for Flat-Bias images.

Parameters:
  • img (ImArray) – Image of Flat or Bias.

  • title (str | None, optional) – Title of the figure (default=None).

  • hpix (pd.DataFrame | None, optional) – Identified hot pixels (as empty or not empty df) (default=None).

  • vmin (float | None, optional) – Minimum value (default=None).

  • vmax (float | None, optional) – Maximum value (default=None).

Returns:

Profile plot.

Return type:

plt.Figure

nima.nima.plt_img_profile_2(img, title=None)#

Summary graphics for Flat-Bias images.

Parameters:
  • img (ImArray) – Image of Flat or Bias.

  • title (str | None, optional) – Title of the figure (default=None).

Returns:

Profile plot.

Return type:

plt.Figure

nima.nima.hotpixels(bias, n_sd=20)#

Identify hot pixels in a bias-dark frame.

After identification of first outliers recompute masked average and std until convergence.

Parameters:
  • bias (ImArray) – Usually the median over a stack of 100 frames.

  • n_sd (int) – Number of SD above mean (masked out of hot pixels) value.

Returns:

y, x positions and values of hot pixels.

Return type:

pd.DataFrame

nima.nima.correct_hotpixel(img, y, x)#

Correct hot pixels in a frame.

Substitute indicated position y, x with the median value of the 4 neighbor pixels.

Parameters:
  • img (ImArray) – Frame (2D) image.

  • y (int | NDArray[np.int_]) – y-coordinate(s).

  • x (int | NDArray[np.int_]) – x-coordinate(s).

Return type:

None