fishdist.fish_analysis_pipeline
apply_affine_trafo_to_image(image, affine_trafo_matrix)
Apply a 3D affine transformation to an image using the provided affine transformation matrix.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Example
>>> import numpy as np
>>> image = np.random.rand(10, 100, 100)
>>> affine = np.eye(4)
>>> transformed = apply_affine_trafo_to_image(image, affine)
>>> transformed.shape
(10, 100, 100)
apply_affine_transform_to_all_images(paths, root_folder_path_to_affine_trafo_matrix, table_name_prefix='points_n_distances3D', ch1=1, ch2=2, atol=1e-06)
Apply a precomputed affine transformation to all images and recompute corrected distances.
This function
- Loads the affine transformation matrix and voxel size
- Verifies voxel size matches the images
- Applies the affine transformation to the first set of coordinates in a table
- Computes distances between transformed and original second coordinates
- Saves the corrected coordinates and distances to a new table
- Plots the distance distribution after correction
| Parameters: |
|
|---|
| Returns: |
|
|---|
Example
paths = ['sample1.tif', 'sample2.tif']
root_folder = 'correction_results'
apply_affine_transform_to_all_images(paths, root_folder, ch1=1, ch2=2)
compute_affine_transform_for_images(paths, correction_matrix_save_path, ch1=1, ch2=2, db_to_read='points_n_distances3D', USE_AFFINE_TRAFO_WITH_SHEAR=True, use_median_n_IQR_filtering_of_coords=True)
Compute and save a global affine transformation matrix for multiple images based on spot coordinates.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Example
paths = ['sample1.tif', 'sample2.tif']
correction_path = 'correction_results'
compute_affine_transform_for_images(paths, correction_path, ch1=1, ch2=2)
detect_spots(detected_spot_binary_image, original_image, validity_mask=None, area_threshold=None, minimal_diameter=None, smart_detect_spots=False, CUBOID_RADIUS=None, gaussian_fit_3D=False, voxel_size=None, real_spot_size=None, gaussian_fit_mode='all')
Detect spot centroids in an image with optional filtering, ROI expansion, and sub-pixel Gaussian refinement.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Example
detect_spots_and_nuclei(paths, ch_nuclei=0, first_spot_channel=1, second_spot_channel=-1, area_threshold=None, channels_to_blur=None, blur_mode='recursive2D', threshold_nuclei=0.5, threshold_spot=0.5)
Detect 3D nuclei and spot coordinates from deep-learning probability maps.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Example
paths = ["img1.tif", "img2.tif"]
detect_spots_and_nuclei(
paths,
ch_nuclei=0,
spot_channels=[1, 2, 3, 4],
area_threshold=10,
channels_to_blur=[0, 1],
blur_mode="recursive2D",
threshold_nuclei=0.5,
threshold_spot=0.6
)
do_controls_for_easy_check(paths, ch1=1, ch2=2, ch_nuclei=0)
Generate quick visual control images for multi-channel datasets.
For each dataset path, the function loads specified channels, processes them (optional inversion, normalization, dilation), computes merged 3D stacks and maximum projections, and saves reduced-size versions for visual inspection.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Example
paths = ["sample_01.tif", "sample_02.tif"]
do_controls_for_easy_check(paths, ch1=1, ch2=2, ch_nuclei=0)
extend_ROIS_to_specified_diameter(regions, EXTEND_PLANAR_OR_POINT_TO_DIAMETER, bounds)
Extend regions of interest (ROIs) to ensure that each dimension of the region's bounding box has at least a specified diameter.
This is useful when single points or very small planar ROIs need to be expanded to a minimum size (e.g., for morphological consistency or for intensity measurements in 3D volumes).
| Parameters: |
|
|---|
| Returns: |
|
|---|
Example
>>> regions = [np.array([[2, 2, 2]])]
>>> bounds = np.array([[0, 10], [0, 10], [0, 10]])
>>> extend_ROIS_to_specified_diameter(regions, 3, bounds)
[array([[1, 1, 1],
[1, 1, 2],
[1, 1, 3],
[1, 2, 1],
[1, 2, 2],
[1, 2, 3],
[1, 3, 1],
[1, 3, 2],
[1, 3, 3],
[2, 1, 1],
[2, 1, 2],
[2, 1, 3],
[2, 2, 1],
[2, 2, 2],
[2, 2, 3],
[2, 3, 1],
[2, 3, 2],
[2, 3, 3],
[3, 1, 1],
[3, 1, 2],
[3, 1, 3],
[3, 2, 1],
[3, 2, 2],
[3, 2, 3],
[3, 3, 1],
[3, 3, 2],
[3, 3, 3]])]
finalize_quantifications_n_pairing(path_to_db, first_spot_channel=1, second_spot_channels=-1, PAIRING_THRESHOLD=250, voxel_conversion_factor=None, ONLY_DETECT_SPOTS_IN_NUCLEI=True)
Compute spot pairing across channels and save distance statistics to a database.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Example
path_to_db = "experiment/FISH.db"
finalize_quantifications_n_pairing(
path_to_db,
first_spot_channel=1,
second_spot_channels=[2, 3, 4],
PAIRING_THRESHOLD=200,
voxel_conversion_factor=(0.3, 0.1, 0.1),
ONLY_DETECT_SPOTS_IN_NUCLEI=True
)
find_spots_in_nuclei(spot_ROIs, nuclear_mask)
Compute the fraction of each spot ROI that overlaps with a nuclear mask.
Each spot is represented either as a RegionProperties object or as an array of coordinates. The nuclear mask is a binarized image where non-zero pixels indicate nuclear regions.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Example
>>> spot_ROIs = [np.array([[0,0],[0,1]]), np.array([[5,5]])]
>>> nuclear_mask = np.zeros((10,10), dtype=int)
>>> nuclear_mask[0,0] = 1
>>> find_spots_in_nuclei(spot_ROIs, nuclear_mask)
[0.5, 0.0]
get_bounds_for_nd_coords(nd_coords)
Compute per-dimension minimum and maximum bounds for an array of N-dimensional coordinates.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Example
>>> import numpy as np
>>> coords = np.array([[0, 1, 2],
... [5, -1, 3],
... [2, 4, 0]])
>>> get_bounds_for_nd_coords(coords)
array([[ 0, 5],
[-1, 4],
[ 0, 3]])
get_nb_channels(db_path)
Retrieves the number of channels from an SQLite database file.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Examples:
If the 'nb_channels' table in the database contains the value 3, then:
get_nb_channels('example.db')
3
If the 'nb_channels' table does not exist in the database file:
get_nb_channels('example.db')
None
If the database file does not exist:
get_nb_channels('nonexistent.db')
None
get_spot_channels(image_shape, ch_nuclei, first_spot_channel, second_spot_channel=None)
Returns a list of spot channel indices to use for image analysis.
If second_spot_channel is None, all channels except ch_nuclei and first_spot_channel are included.
If second_spot_channel is an integer, only that channel is included.
If second_spot_channel is a list, that list is returned directly.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Example
image_shape = (10, 256, 256, 5) # (z, h, w, c) ch_nuclei = 0 first_spot_channel = 1 second_spot_channel = None get_spot_channels(image_shape, ch_nuclei, first_spot_channel, second_spot_channel) [2, 3, 4]
second_spot_channel = 3 get_spot_channels(image_shape, ch_nuclei, first_spot_channel, second_spot_channel) [3]
second_spot_channel = [2, 4] get_spot_channels(image_shape, ch_nuclei, first_spot_channel, second_spot_channel) [2, 4]
get_sub_pixel_coordinates_through_gaussian_fit(coords, original_image, voxel_size=1, spot_radius=1)
Refine spot coordinates to sub-pixel accuracy using Gaussian fitting.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Example
>>> coords = np.array([[10, 15], [20, 25]])
>>> image = np.random.random((50, 50))
>>> refined = get_sub_pixel_coordinates_through_gaussian_fit(coords, image)
>>> refined.shape
(2, 2)
merge_coords(*regions, remove_dupes=True)
Merge multiple sets of coordinates into a single set.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Example
>>> import numpy as np
>>> coords1 = np.array([[1, 2], [3, 4]])
>>> coords2 = np.array([[3, 4], [5, 6]])
>>> result = merge_coords(coords1, coords2)
>>> print(result)
[[1 2]
[3 4]
[5 6]]
negative_to_positive_index(arr_shape, index)
Converts a negative index to a positive index for a NumPy ndarray.
| Parameters: |
|
|---|
| Returns: |
|
|---|
| Raises: |
|
|---|
Example
arr_shape = (10, 256, 256, 5) negative_to_positive_index(arr_shape, -1) 4 negative_to_positive_index(arr_shape, 2) 2
pair_spots(paths, PAIRING_THRESHOLD=250, first_spot_channel=1, second_spot_channels=-1)
Align and pair detected spots across channels for multiple datasets, both with and without restricting detection to nuclear regions.
For each dataset path, the pairing pipeline is executed twice: once considering all detected spots, and once considering only spots located inside nuclei.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Example
paths = ["sample_01.tif", "sample_02.tif"]
pair_spots(
paths,
PAIRING_THRESHOLD=200,
first_spot_channel=1,
second_spot_channels=[2, 3, 4]
)
plot_all_paired_spots(paths, ch1=1, ch2=2)
Render paired spot coordinates as 3D lines directly on the original image for all available tables that start with 'points_n_distances3D'.
| Parameters: |
|
|---|
| Returns: |
|
|---|
plot_analysis(paths, distance_cut_off=2.5, group_files_by_name_similarity=False, output_file_name=None, table_names=['points_n_distances3D_only_in_nuclei_acc', 'points_n_distances3D_only_in_nuclei_lcc'])
Generate violin plots of distances from FISH analysis datasets with optional grouping.
This function loads distance data from multiple FISH.db files, filters by a cutoff distance, optionally groups datasets by filename similarity, and generates violin plots with median annotations.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Example
paths = ['dataset1', 'dataset2']
plot_analysis(paths, distance_cut_off=3.0, group_files_by_name_similarity=True,
output_file_name='violin_plot.pdf')
plot_distance_distribution(distances, outputfile_name)
Compute distribution statistics for a list/array of distances, print them, and save a histogram plot to a file.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Example
distances = [1.2, 2.5, 3.0, 4.1, 2.9]
outputfile = 'distance_histogram.png'
plot_distance_distribution(distances, outputfile)
plot_histo(distances, median=None, q1_q3=None, title=None)
Plot a histogram of distance values and annotate it with the median and interquartile range.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Example
distances = [1.2, 1.5, 1.7, 2.0, 2.1]
plot_histo(distances, title="Distance distribution")
plot_violin_with_median(data, **kwargs)
Create a violin plot for the 'distance' column of a DataFrame and annotate it with the median.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Example
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
df = pd.DataFrame({'distance': [1.2, 2.5, 1.8, 3.0, 2.2]})
plot_violin_with_median(df, palette='muted')
plt.show()
run_analysis(paths, correction_matrix_save_path, PAIRING_THRESHOLD=60, ch_nuclei=0, first_spot_channel=1, second_spot_channel=-1, area_threshold=None, channels_to_blur=None, blur_mode='recursive2D', deep_learning='rapid', threshold_spot_ch1=0.5, threshold_nuclei=0.5, threshold_spot_ch2=0.5, RUN_SEG=True, RUN_REG=False, RUN_DISTANCE_MEASUREMENTS=True, RUN_CTRLS=True, RUN_GAUSSIAN_FIT=True, nuclear_model_to_use='nuclear_model_0', spot_model_to_use='spot_model_0', list_pairs_for_reg=None)
Execute a full pipeline for FISH spot and nuclei analysis including segmentation, spot detection, pairing, registration, distance measurements, controls, and plotting.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Example
paths = ["dataset1.tif", "dataset2.tif"]
correction_matrix_save_path = "corrections/"
run_analysis(
paths,
correction_matrix_save_path,
PAIRING_THRESHOLD=50,
ch_nuclei=0,
first_spot_channel=1,
second_spot_channel=2,
RUN_SEG=True,
RUN_REG=True,
RUN_DISTANCE_MEASUREMENTS=True,
RUN_CTRLS=True
)
segment_spots_and_nuclei(paths, ch_nuclei=0, first_spot_channel=1, second_spot_channel=-1, __autoskip=True, channels_to_blur=None, blur_mode='recursive2D', deep_learning='rapid', nuclear_model_to_use='nuclear_model_0', spot_model_to_use='spot_model_0')
Segment nuclei and spot channels in microscopy images using deep-learning models.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Example
paths = ["img1.tif", "img2.tif"]
segment_spots_and_nuclei(
paths,
ch_nuclei=0,
first_spot_channel=1,
second_spot_channel=2,
channels_to_blur=[0, 1],
blur_mode="recursive2D"
)
set_nb_channels(db_path, nb_channels)
Inserts the number of channels of an image into an SQLite database table named 'nb_channels' with a single column 'nb'.
| Parameters: |
|
|---|
| Returns: |
|
|---|