nulogo

Bleedthrough Module

flyqma.bleedthrough provides methods for correcting for spectral bleedthrough between fluorescent reporter channels.

Background Extraction

Tools for extracting the background pixels from a segmented image.

class flyqma.bleedthrough.background.BackgroundExtraction(layer, niters=10)[source]

Object for extracting image background pixels.

Attributes:

layer (Layer) - layer RGB image

bg_mask (np.ndarray[bool]) - background mask, True where background

build_background_mask(niters=0)[source]

Construct background mask by dilating foregound.

Args:

niters (int) - number of binary dilations

Returns:

bg_mask (np.ndarray[bool]) - background mask, True where background

static dilate_foreground(foreground, niters=5)[source]

Apply binary dilation to foreground mask.

extract_pixels(channel)[source]

Extract pixels from image background.

Args:

channel (int) - color channel to be extracted

Returns:

px (np.ndarray[float]) - 1D array of background pixel intensities

isolate_pixels(channel)[source]

Isolate pixels in image background.

Args:

channel (int) - color channel to be extracted

Returns:

px (np.ma.masked_array) - background masked pixel intensities

plot_foreground_mask(invert=False, ax=None, figsize=(3, 3))[source]

Plot foreground mask.

Args:

invert (bool) - if True, mask background rather than foreground

ax (matplotlib.axes.AxesSubplot) - if None, create figure

figsize (tuple) - figure size

Returns:

figure

Resampling

Tools for resampling a set of pixel intensities.

flyqma.bleedthrough.resampling.resample_uniformly(x, y, size=None, cutoff=None)[source]

Resample X and Y uniformly in X.

Args:

x, y (np.ndarray[float]) - original samples

size (int) - number of uniform samples

cutoff (int) - upper bound for samples (quantile, 0 to 100)

Returns:

x, y (np.ndarray[float]) - resampled s.t. x is uniformly distributed

Bleedthrough Coefficient Characterization

Tools for characterizing bleedthrough coefficients.

class flyqma.bleedthrough.models.GLM(x, y, **fit_kw)[source]

Generalized linear model with gamma distributed residuals and an identity link function fit to X and Y data.

Attributes:

model (sm.genmod.GLM) - generalized linear model

domain (np.ndarray[float]) - regularly spaced x-domain

Inherited attributes:

x, y (array like) - data

fit(N=100000, maxiter=500, shift=0)[source]

Fit Gamma GLM with identity link function.

Args:

N (int) - number of samples used

maxiter (int) - maximum number of iterations for optimization

shift (float) - offset used to keep values positive

class flyqma.bleedthrough.models.OLS(x, y, **fit_kw)[source]

Ordinary least squares model fit to X and Y data.

Attributes:

x, y (array like) - data

model (sm.OLS) - fitted OLS model

detrend(x, y)[source]

Remove linear trend from X and Y data.

fit(**kw)[source]

Fit model to X and Y data.

predict(x)[source]

Make model prediction.

Bleedthrough Correction

Tools for correcting measurements subject to bleedthrough.

class flyqma.bleedthrough.correction.Correction(xt, yt, bg_x, bg_y, remove_zeros=False, resample=True, resample_size=None, resample_cutoff=None, store_pixels=True, **fit_kw)[source]

Linear correction for background correlation between fluorescence channels within an individual layer.

Attributes:

xt, yt (np.ndarray[float]) - foreground measurements

xraw, yraw (np.ndarray[float]) - raw background pixel intensities

x, y (np.ndarray[float]) - resampled background pixel intensities

correct_measurements()[source]

Apply correction to measurements.

class flyqma.bleedthrough.correction.LayerCorrection(layer, xvar, yvar, niters=50, remove_zeros=False, resample=True, resample_size=None, resample_cutoff=None, store_pixels=False, **fit_kw)[source]

Linear correction for background correlation between fluorescence channels within an individual layer.

Attributes:

layer (Layer) - layer RGB image

Inherited attributes:

xt, yt (np.ndarray[float]) - foreground measurements

xraw, yraw (np.ndarray[float]) - raw background pixel intensities

x, y (np.ndarray[float]) - resampled background pixel intensities

Parameters:

xvar (str) - name of independent variable attribute in measurement data

yvar (str) - name of dependent variable attribute in measurement data

niters (int) - number of binary dilations applied to foreground mask

remove_zeros (bool) - if True, remove all zero-valued pixels.

resample (bool) - if True, uniformly resample pixels in X

resample_size (int) - number of uniform samples

resample_cutoff (int) - upper bound for samples (quantile, 0 to 100)

extract_background()[source]

Returns raw background pixels.

classmethod load(layer)[source]

Load linear model from file.

Args:

path (str) - path to correction directory

Returns:

correction (LayerCorrection)

save(images=True)[source]

Save linear model and corrected levels.

Args:

images (bool) - if True, save model fit and corrected measurement figs

save_figs(dpi=100, fmt='png')[source]

Save all figures.

Args:

dpi (int) - resolution

fmt (str) - image format

property xkey

DataFrame key for independent channel.

property ykey

DataFrame key for dependent channel.

Visualization

Tools for visualizing the bleedthrough correction procedure.

class flyqma.bleedthrough.visualization.CorrectionVisualization[source]

Methods for visualizing correction procedure.

show_correction(figsize=(6, 2), selected_only=False)[source]

Show cell measurements before and after correction.

Args:

figsize (tuple) - figure size

selected_only (bool) - if True, exclude cells outside selection bounds

show_fit(mode='box', bin_size=0.05, figsize=(3, 2))[source]

Plot fit to background pixels using sns.boxplot .

show_resampling(xbins=None, ybins=None, **kwargs)[source]

Visualize resampling procedure.

Returns:

figures (tuple)

class flyqma.bleedthrough.visualization.LayerCorrectionVisualization[source]

Methods for visualizing layer correction procedure.

show_background_extraction(**kwargs)[source]

Visualize background extraction procedure.

Keyword arguments:

invert (bool) - if True, mask background rather than foreground

Returns:

figure