API reference¶
This page provides an auto-generated summary of the APIs used for pharynx analysis.
Experiment¶
Pharynx IO¶
This module coordinates loading data from and saving data to disk.
Image Processing¶
This module contains the code for processing images, including segmentation, midline calculation and measurement, translation, binary morphological operations, etc.
-
pharedox.image_processing.
calculate_midline
(rot_seg_img, degree=4, pad=10)[source]¶ Calculate a the midline for a single image by fitting a polynomial to the segmented pharynx
- Parameters
rot_seg_img (Union[np.ndarray, xr.DataArray]) – The rotated masked pharynx image
degree (
int
) – the degree of the polynomialpad (
int
) – the number of pixels to “pad” the domain of the midline with respect to the boundaries of the segmentation mask
- Returns
the estimated midline
- Return type
Polynomial
Notes
Right now this only works for images that this have been centered and aligned with their anterior-posterior along the horizontal.
-
pharedox.image_processing.
calculate_midlines
(rot_seg_stack, degree=4)[source]¶ Calculate a midline for each animal in the given stack
- Parameters
rot_seg_stack (
DataArray
) – The rotated mask with which midlines should be calculated.degree (
int
) – The degree of the polynomial fit
- Returns
a DataArray containing the midline objects
- Return type
midlines
See also
-
pharedox.image_processing.
center_and_rotate_pharynxes
(fl_images, seg_images)[source]¶ Given a fluorescence stack and a pharyngeal mask stack, center and rotate each frame of both the FL and mask such that the pharynx is in the center of the image, with its anterior on the left.
- Parameters
fl_images (
DataArray
) – The fluorescence images to rotate and alignseg_images (
DataArray
) – The segmented images to rotate and align
- Returns
A 2-tuple where the first item is the rotated fluorescence stack and the second is the rotated mask stack
- Return type
(rotated_fl_stack, rotated_seg_stack)
-
pharedox.image_processing.
create_normed_rgb_ratio_stack
(r_imgs, seg_imgs, vmin=- 7, vmax=7, cmap='coolwarm', output_filename=None)[source]¶ Z-normalize the images (relative to the masks), then transform them into RGB with the given colormap
-
pharedox.image_processing.
extract_largest_binary_object
(bin_img)[source]¶ Extracts the largest binary object from the given binary image
- Parameters
bin_img (
Union
[DataArray
,ndarray
]) – The binary image to process- Returns
The binary image containing only the largest binary object from the input
- Return type
bin_img
-
pharedox.image_processing.
get_area_of_largest_object
(mask)[source]¶ Returns the area (px) of the largest object in a binary image
- Parameters
mask (np.ndarray) – the binary image
- Returns
the area of the largest object
- Return type
int
-
pharedox.image_processing.
get_lr_bounds
(rot_seg_stack, pad=0, ref_wvl='410', ref_pair=0)[source]¶ Get the left and right boundaries of the rotated pharynxes
- Parameters
rot_seg_stack (
DataArray
) – the rotated segmented pharynxespad (
int
) – the amount of padding on the left/right of the boundsref_wvl (
str
) – the wavelength to use for calculating boundsref_pair (
int
) – the pair to use for calculating bounds
- Returns
An (m, 2) array where m = number of animals, the first column is the left bound and the second column is the right bound
- Return type
bounds
-
pharedox.image_processing.
measure_under_labels
(imgs, masks, ref_wvl='410', ratio_numerator='410', ratio_denominator='470')[source]¶ Measure the intensities of each channel under the label image
-
pharedox.image_processing.
measure_under_midline
(fl, mid, n_points=100, thickness=0.0, order=1, norm_scale=1, flatten=True)[source]¶ Measure the intensity profile of the given image under the given midline at the given x-coordinates.
- Parameters
flatten –
norm_scale –
order – the interpolation order
fl (
DataArray
) – The fluorescence image to measuremid (
Polynomial
) – The midline under which to measuren_points (
int
) – The number of points to measure underthickness (
float
) – The thickness of the line to measure under.
Notes
Using thickness is slower, depending on the amount of thickness
- On my machine (2GHz Intel Core i5), as of 12/4/19:
- 0-thickness:
492 µs ± 16.6 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
- 2-thickness:
1.99 ms ± 65.8 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
- 10-thickness:
3.89 ms ± 92.1 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
- Returns
zs – The intensity profile of the image measured under the midline at the given x-coordinates.
- Return type
np.ndarray
-
pharedox.image_processing.
measure_under_midlines
(fl_stack, midlines, n_points=300, order=1, thickness=0)[source]¶ Measure under all midlines in stack
- Parameters
order –
fl_stack (
DataArray
) – The fluorescence stack under which to measuremidlines (dict) – A DataArray containing the midlines
n_points (int) – the number of points to sample under the midline
thickness (float) – the thickness of the midline to measure under
- Returns
profile_data – the intensity profiles for each image in the stack
- Return type
xr.DataArray
-
pharedox.image_processing.
normalize_images_by_wvl_pair
(fl_imgs, profiles, percent_to_clip=2.0)[source]¶ Normalize images by subtracting mean profile then min-max rescaling to [0, 1] :type fl_imgs:
DataArray
:param fl_imgs: the images to normalize :type profiles:DataArray
:param profiles: the intensity profiles corresponding to the images :type percent_to_clip:float
:param percent_to_clip: how much to clip the profile when calculating mean/min/max, expressed as a percentage of the length of the profile- Returns
the normalized images
- Return type
xr.DataArray
-
pharedox.image_processing.
normalize_images_single_wvl
(fl_imgs, profiles, percent_to_clip=2.0)[source]¶ Normalize single wavelength image stack by subtracting the mean of the corresponding intensity profile, then min-max rescaling to [0, 1]
- Parameters
fl_imgs (
Union
[ndarray
,DataArray
]) – an array-like structure of shape (frame, row, col)profiles (
Union
[ndarray
,DataArray
]) – an array-like structure of shape (frame, position_along_midline)percent_to_clip (
float
) – how much to clip the profile when calculating mean/min/max, expressed as a percentage of the length of the profile
- Returns
normalized images
- Return type
Union[np.ndarray, xr.DataArray]
-
pharedox.image_processing.
rotate
(img, tform, orientation, order=1, preserve_range=True)[source]¶ Rotate the given image with the given translation matrix and orientation angle
- Parameters
img (
Union
[ndarray
,DataArray
]) – the image to rotatetform – the translation matrix to apply
orientation – the angle of orientation (radians)
order – the order of the interpolation
preserve_range – preserve the input data range
- Returns
the translated and rotated image
- Return type
rotated
-
pharedox.image_processing.
segment_pharynx
(fl_img, target_area=450, area_range=100)[source]¶ Generate a mask for the given image containing a pharynx.
- Parameters
fl_img (xr.DataArray) – a fluorescent image containing a single pharynx
target_area (int, optional) – the presumptive area (in px) of a pharynx, by default 450
area_range (int, optional) – the acceptable range (in px) above/below the target_area, by default 100
- Returns
an image containing the segmented pharynx (dtype: np.uint8). Pixels of value=1 indicate the pharynx, pixels of value=0 indicate the background.
- Return type
xr.DataArray
-
pharedox.image_processing.
segment_pharynxes
(fl_stack, wvl='410', target_area=450, area_range=100)[source]¶ Segment a hyperstack of pharynxes
- Parameters
fl_stack (xr.DataArray) – the fluorescent images to segment
wvl (str, optional) – the wavelength to segment, by default “410”
target_area (int, optional) – the presumptive area of a pharynx, in pixels, by default 450
area_range (int, optional) – the acceptable range of pharyngeal areas, by default 100
- Returns
the masks for the specified wavelength
- Return type
xr.DataArray
-
pharedox.image_processing.
shift
(image, vector)[source]¶ Translate the image according to the given movement vector
- Parameters
image (
ndarray
) – the image to translatevector (
ndarray
) – translation parameters(dx, dy)
- Returns
img – the translated image
- Return type
np.ndarray
-
pharedox.image_processing.
subtract_medians
(data, image_data=None)[source]¶ Subtract medians from data, optionally calculating them from a separate piece of data.
- Parameters
data (
DataArray
) – the data to subtract the median from.image_data (
Optional
[DataArray
]) – the data to calculate the median with. Must include the dimensions x and y. If specified, all other dimensions must be identical to those in data. If not specified, the medians will be calculated with data.
- Return type
DataArray
-
pharedox.image_processing.
z_normalize_with_masks
(imgs, masks)[source]¶ Perform z-normalization [0] on the entire image (relative to the content within the masks).
That is to say, we center the pixels (within the mask) such that their mean is 0, and ensure their standard deviation is ~1.
This allows us to see spatial patterns within the masked region (even if pixels outside of the masked region fall very far above or below those inside) by setting the colormap center around 0.
[0] - https://jmotif.github.io/sax-vsm_site/morea/algorithm/znorm.html
Profile Processing¶
This module contains the code for processing the measured profiles, including functions for transforming ratios to OxD and E, trimming profiles, and registering profiles.
Plotting¶
This module contains plotting code