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

Parameters:
  • im (ImArray) –

  • bins (int) –

  • log (bool) –

  • nf (bool) –

nima.nima.read_tiff(fp, channels)#

Read multichannel tif timelapse image.

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

  • channels (list of string) – 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) – 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.

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

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

  • kws (Any) –

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 of images) –

Returns:

d_im – preserve dtype of input

Return type:

dict of images

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, TypeVar(ImArray, ndarray[Any, dtype[int64]], ndarray[Any, dtype[float64]], ndarray[Any, dtype[bool_]])]) – Dictionary of images.

  • dark (2D image or (2D) d_im) – Dark image.

  • flat (2D image or (2D) d_im) – Flat image.

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

Returns:

Corrected d_im.

Return type:

d_im

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) – Method {‘arcsinh’, ‘entropy’, ‘adaptive’, ‘li_adaptive’, ‘li_li’} used for the segmentation.

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

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

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

  • arcsinh_perc (int, 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 (list ?) – Values of all bg masked pixels.

  • figs ({[f1], [f1, f2]}) – List of fig(s). Only entropy and arcsinh methods have 2 elements.

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

Bg segmentation for d_im.

Parameters:
  • d_im (d_im) – desc

  • downscale ({None, tupla}) – Tupla, x, y are downscale factors for rows, cols.

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

  • clip (bool) – 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 (d_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 (list) – List of (list ?) of figures.

  • d_bg_values (dict) – 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):

  • 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 (d_im) – desc

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

  • channels (list of string) – List a name for each channel.

  • threshold_method ({'yen', 'li'}) – Method for thresholding (skimage) the geometric average plane-by-plane.

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

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

  • clear_border (bool, optional) – Boolean (default=False) for removing objects that are touching the image (2D) border.

  • randomwalk (bool, optional) – Boolean (default=False) for using random_walker in place of watershed (skimage) algorithm after ndimage.distance_transform_edt() calculation.

Return type:

None

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 (d_im) – desc

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

  • channels (list of string) – Names (default=[‘C’, ‘R’]) for the two channels [Numerator, Denominator].

  • radii (tupla of int, optional) – Each element contain a radius value for a median filter cycle.

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 (d_im) – desc

  • channels (list of string) – All d_im channels (default=[‘C’, ‘G’, ‘R’]).

  • channels_cl (tuple of string) – Names (default=(‘C’, ‘R’)) of the numerator and denominator channels for cl ratio.

  • channels_ph (tuple of string) – Names (default=(‘G’, ‘C’)) of the numerator and denominator channels for pH ratio.

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

  • radii ((int, int), Optional) – Radii of the optional median average performed on ratio images.

Return type:

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

Returns:

  • meas (dict of 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 of list of list) – 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 of pd.DataFrame) – meas object returned from d_meas_props().

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

Returns:

fig – 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 (Optional[str]) – Title of the figure.

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

  • vmin (float, optional) – Minimum value.

  • vmax (float, optional) – Maximum value.

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 (Optional[str]) – Title of the figure.

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 | list(int)) – y-coordinate(s).

  • x (int | list(int)) – x-coordinate(s).

Return type:

None