API details.

Preparing xarray DataArray of images from file path globs

clean_img_names[source]

clean_img_names(img_path_glob:str, img_name_regex:str)

clean_img_names takes a "globbed" string pattern, searches for all files that match the pattern and extracts image names from each file using a regular expression.

Args: img_path_glob (str): A globbed string pattern e.g. "C1/*.tif" img_name_regex (str): A regex pattern used to parse out image names from filenames e.g. r"\w\d\w\d\d\p\d"

Returns: list: Parsed image filenames stored in a list.

Examples:

clean_img_names("docs/fe*", r"feed")
['feed']
assert clean_img_names("docs/fe*", r"feed") == ["feed"]

check_lists_identical[source]

check_lists_identical(list_of_lists:List[List[T]])

Checks if all lists within a list are identical. Raises a ValueError exception if not.

Args: list_of_lists (list[list]): List of lists. Can contain anything e.g. strings, numbers.

Raises: ValueError: Exception.

Example where lists are identical:

check_lists_identical([["hello", "goodbye"], ["hello", "goodbye"]])

Example where lists are not identical and an ValueError exception would be thrown:

with ExceptionExpected():
    check_lists_identical([["hello", "goodbye", "morning"], ["hello", "goodbye"]])

img_path_to_xarr[source]

img_path_to_xarr(img_name_regex:str, pixel_size:float=0.275, ch_name_for_first_dim:str='images', **channel_path_globs)

Takes channel path globs and creates a dask backed xarray dataarray"

Args: img_name_regex (str): A regex pattern used to parse out image names from filenames e.g. r"\w\d\w\d\d\p\d" pixel_size (float, optional): Defaults to 0.275. ch_name_for_first_dim (str, optional): Defaults to "images". *channel_path_globs: required e.g. C0="data/MARCM_experiment/images/C0/.tif"

Returns: xarray dataarray: dask backed xarray dataarray of images

xarr = img_path_to_xarr(
    img_name_regex=r"a\dg\d\dp\d",
    C0="data/MARCM_experiment/images/C0/*.tif",
    C1="data/MARCM_experiment/images/C1/*.tif",
    C2="data/MARCM_experiment/images/C2/*.tif",
    C3="data/MARCM_experiment/images/C3/*.tif",
)
xarr
<xarray.DataArray 'stack-4a9a7b658cf09be0542e46e591c217bf' (images: 4, img_name: 67, y: 810, x: 810)>
dask.array<stack, shape=(4, 67, 810, 810), dtype=uint16, chunksize=(1, 1, 810, 810), chunktype=numpy.ndarray>
Coordinates:
  * images    (images) <U2 'C0' 'C1' 'C2' 'C3'
  * img_name  (img_name) <U7 'a1g01p1' 'a1g01p2' ... 'a2g13p2' 'a2g13p3'
  * y         (y) float64 0.0 0.275 0.55 0.825 1.1 ... 221.7 221.9 222.2 222.5
  * x         (x) float64 0.0 0.275 0.55 0.825 1.1 ... 221.7 221.9 222.2 222.5
assert xarr.shape == (4, 67, 810, 810)

last2dims[source]

last2dims(f:Callable)

Decorator function for operating on the last two dimensions of an array. Useful with dask map blocks.

Args: f (Callable): Function

Example:

@last2dims
def do_nothing(arr):
    return arr
do_nothing(np.ones((100, 100, 5))), do_nothing(np.ones((100, 100, 5))).shape
(array([[[1., 1., 1., 1., 1.]]]), (1, 1, 5))

Helper functions for regionprops

check_channels_input_suitable_and_return_channels[source]

check_channels_input_suitable_and_return_channels(channels:List[T], available_channels:List[T])

Checks if inputted channels are within a available channels list. If so, return inputted channels. If no channels given, returns all available channels.

Args: channels (List): List of desired channels. available_channels (List): List of available channels.

Raises: ValueError: channels are not in available channels. TypeError: channels and availables channels must be of type list

Returns: List: channels to be used.

Example:

check_channels_input_suitable_and_return_channels(
    ["C0", "C1", "C2"], ["C0", "C1", "C2", "C3"]
)
['C0', 'C1', 'C2']
check_channels_input_suitable_and_return_channels(None, ["C0", "C1", "C2", "C3"])
['C0', 'C1', 'C2', 'C3']
assert check_channels_input_suitable_and_return_channels(
    ["C0", "C1", "C2"], ["C0", "C1", "C2", "C3"]
) == ["C0", "C1", "C2"]
with ExceptionExpected():
    check_channels_input_suitable_and_return_channels(
        ["C0", "C1", "C4", "C3"], ["C0", "C1", "C2", "C3"]
    )

extend_region_properties_list[source]

extend_region_properties_list(extra_properties:List[T]=None)

Adds more properties to scikit-image regionprops function. Defaults are ["label", "area", "mean_intensity", "centroid"].

Args: extra_properties (List, optional): See scikit-image regionprops for possible extra properties. Defaults to None.

Raises: TypeError: extra_properties must be a list. e: remaining exceptions.

Returns: List: extra properties appended to default properties.

Example:

extend_region_properties_list()
['label', 'area', 'mean_intensity', 'centroid']
extend_region_properties_list(["median_intensity"])
['label', 'area', 'mean_intensity', 'centroid', 'median_intensity']
assert extend_region_properties_list(["median_intensity"]) == [
    "label",
    "area",
    "mean_intensity",
    "centroid",
    "median_intensity",
]
with ExceptionExpected():
    extend_region_properties_list(5)

add_scale_regionprops_table_area_measurements[source]

add_scale_regionprops_table_area_measurements(df:DataFrame, pixel_size:int)

Extra column(s) in regionprops for areas in um2.

Args: df (pd.DataFrame): Dataframe generated by scikit-image regionprops_table pixel_size (int): pixel size in um2.

Returns: pd.DataFrame: Regionprops dataframe with extra area in um2 columns.

df = pd.DataFrame(
    {"area": np.array([1, 2, 3, 4, 5]), "mean_intensity": np.array([4, 5, 6, 7, 7])}
)
df
area mean_intensity
0 1 4
1 2 5
2 3 6
3 4 7
4 5 7
add_scale_regionprops_table_area_measurements(df, 0.5)
area mean_intensity area_um2
0 1 4 0.25
1 2 5 0.50
2 3 6 0.75
3 4 7 1.00
4 5 7 1.25
assert add_scale_regionprops_table_area_measurements(df, 0.5)["area_um2"].tolist() == [
    0.25,
    0.50,
    0.75,
    1.00,
    1.25,
]

Delayed('lazy_props')[source]

Delayed('lazy_props')()

reorder_df_to_put_ch_info_first[source]

reorder_df_to_put_ch_info_first(df:DataFrame)

reorders a pandas dataframe to channel columns first e.g. "int_img_ch"

Args: df (pd.DataFrame): pandas dataframe containing channel columns.

Returns: pd.DataFrame: pandas dataframe with the channel columns first

Visualization helper functions

is_label_image[source]

is_label_image(img:array, unique_value_thresh:int=2000)

Tests whether supplied image is a label image based on the number of unique values in the image.

Args: img (np.array): image unique_value_thresh (int, optional): Defaults to 2000.

Returns: bool: is the supplied image a label image.

generate_random_cmap[source]

generate_random_cmap(num_of_colors:int=2000)

Uses matplotlib to generate a random cmap.

Args: num_of_colors (int, optional): Defaults to 2000.

Returns: matplotlib.colors.ListedColormap: colormap that can be use when plotting e.g. cmap=custom_cmap

what_cmap[source]

what_cmap(img:array, img_cmap:ListedColormap, label_cmap:ListedColormap)

Determines whether to use a image cmap (e.g. gray) or a label cmap (e.g. random)

Args: img (np.array) img_cmap (matplotlib.colors.ListedColormap) label_cmap (matplotlib.colors.ListedColormap)

Returns: matplotlib.colors.ListedColormap: image cmap (e.g. gray) or a label cmap (e.g. random).

figure_rows_columns[source]

figure_rows_columns(total_fig_axes:int, rows:int=3)

Calculates the sensible default number of columns and rows for a figure.

Args: total_fig_axes (int): Total number figure axes e.g. for 3x3 grid, would be 9. rows (int, optional): How many rows. Defaults to 3.

Returns: Tuple: Number of columns and rows for a figure

auto_figure_size[source]

auto_figure_size(figure_shape:Tuple, scaling_factor:int=4)

Scales figure shape to generate figure dimensions.

Args: figure_shape (Tuple): Figure shape in determines of rows and columns. scaling_factor (int, optional): Defaults to 4.

Returns: Tuple: figure dimensions in inches for matplotlib.

crop_RGB_img_to_square[source]

crop_RGB_img_to_square(RGB_img:array)

Crops an RGB image to a square e.g useful after taking a napari screenshot.

Args: RGB_img (np.array): Image of form (y,x,c).

Returns: np.array: cropped image

plot_new_images[source]

plot_new_images(images:List[array], label_text:List[str], label_letter:str=None, figure_shape:tuple=None, figure_size:tuple=None, img_cmap:str='gray', label_cmap:str=None, colorbar:bool=False, colorbar_title:str='number of neighbours', **kwargs)

Plots a grid of images with labels.

Args: images (list[np.array]): List of numpy arrays. Can be RGB or 2D. label_text (list[str]): List of image labels. label_letter (str, optional): e.g. ABCDE Defaults to None. figure_shape (tuple, optional): e.g. four rows and columns (4,4) Defaults to None. figure_size (tuple, optional): e.g. a 12 by 12 image (12, 12) Defaults to None. img_cmap (str, optional): Defaults to "gray". label_cmap (str, optional): Defaults to None. colorbar (bool, optional): Defaults to False. colorbar_title (str, optional): Defaults to "number of neighbours".

RGB_image_from_CYX_img[source]

RGB_image_from_CYX_img(red:array=None, green:array=None, blue:array=None, ref_ch:int=2, clims:Tuple=(2, 98))

Takes individual, equal-sized 2D numby arrays and generates an RGB image of the form (Y,X,C).

Args: red (np.array, optional): Defaults to None. green (np.array, optional): Defaults to None. blue (np.array, optional): Defaults to None. ref_ch (int, optional): Channel used to define shape. Defaults to 2. clims (Tuple, optional): Adjust contrast limits. Defaults to (2, 98).

Returns: np.array: [description]

four_ch_CYX_img_to_three_ch_CYX_img[source]

four_ch_CYX_img_to_three_ch_CYX_img(img:array)

Converts a four channel CYX image into a three channel CYX image.

Args: img (np.array): Four channel CYX image.

Returns: np.array: Three channel CYX image.

Region overlap calculations

region_overlap[source]

region_overlap(label_no:int, label_img_outer:array, label_img_inner:array, overlap_thresh:int=0.5)

Determine which two regions overlap in two label images.

Args: label_no (int): Label number in the inner label image to look for overlap with. label_img_outer (np.array): Outer label image i.e. the one for which we are testing overlap against. label_img_inner (np.array): Inner label image i.e. the image contain the label_no provide in the first argument. overlap_thresh (int, optional): How much overlap between two labels to call an overlap. Defaults to 0.5.

Returns: int: Label in outer label image which overlaps with label_no.

calculate_overlap[source]

calculate_overlap(img:array, num_of_segs:int=4, preallocate_value:int=1000)

Calculates overlap between stack of label images.

Args: img (np.array): Label image with channel first. num_of_segs (int, optional): Defaults to 4. preallocate_value (int, optional): Defaults to 1000.

Returns: np.array: Array of overlaps

Defining cell types

calc_allfilt_from_thresholds[source]

calc_allfilt_from_thresholds(thresholds:list, df)

concat_list_of_thresholds_to_string[source]

concat_list_of_thresholds_to_string(thresholds)

Touching cell calculations

generate_touch_counting_image[source]

generate_touch_counting_image(g_img)

adjusted_cell_touch_images[source]

adjusted_cell_touch_images(total_neigh_counts, neg_neigh_neg, pos_neigh_pos, pos_binary_image)

Delayed('calc_neighbours')[source]

Delayed('calc_neighbours')()

get_all_labeled_clones_unmerged_and_merged[source]

get_all_labeled_clones_unmerged_and_merged(total_seg_labels, labels_to_keep:dict, calc_clones:bool)

Delayed('determine_labels_across_other_images_using_centroids')[source]

Delayed('determine_labels_across_other_images_using_centroids')()

calculate_corresponding_labels[source]

calculate_corresponding_labels(labels, centroids_list, first_output_dim, second_output_dim)

Editing xarray dims and coords

update_1st_coord_and_dim_of_xarr[source]

update_1st_coord_and_dim_of_xarr(xarr, new_coord:list, new_dim:str)