spacr.plot¶
Module Contents¶
- spacr.plot.plot_image_mask_overlay(file, channels, cell_channel, nucleus_channel, pathogen_channel, figuresize=10, percentiles=(2, 98), thickness=3, save_pdf=True, mode='outlines', export_tiffs=False, all_on_all=False, all_outlines=False, filter_dict=None)[source]¶
Plot image and mask overlays.
- spacr.plot.plot_masks(batch, masks, flows, cmap='inferno', figuresize=10, nr=1, file_type='.npz', print_object_number=True)[source]¶
Plot the masks and flows for a given batch of images.
- Parameters:
batch (numpy.ndarray) – The batch of images.
masks (list or numpy.ndarray) – The masks corresponding to the images.
flows (list or numpy.ndarray) – The flows corresponding to the images.
cmap (str, optional) – The colormap to use for displaying the images. Defaults to ‘inferno’.
figuresize (int, optional) – The size of the figure. Defaults to 20.
nr (int, optional) – The maximum number of images to plot. Defaults to 1.
file_type (str, optional) – The file type of the flows. Defaults to ‘.npz’.
print_object_number (bool, optional) – Whether to print the object number on the mask. Defaults to True.
- Returns:
None
- spacr.plot.generate_mask_random_cmap(mask)[source]¶
Generate a random colormap based on the unique labels in the given mask.
Parameters: mask (numpy.ndarray): The input mask array.
Returns: matplotlib.colors.ListedColormap: The random colormap.
- spacr.plot.random_cmap(num_objects=100)[source]¶
Generate a random colormap.
Parameters: num_objects (int): The number of objects to generate colors for. Default is 100.
Returns: random_cmap (matplotlib.colors.ListedColormap): A random colormap.
- spacr.plot.plot_images_and_arrays(folders, lower_percentile=1, upper_percentile=99, threshold=1000, extensions=['.npy', '.tif', '.tiff', '.png'], overlay=False, max_nr=None, randomize=True)[source]¶
Plot images and arrays from the given folders.
- Parameters:
folders (list) – A list of folder paths containing the images and arrays.
lower_percentile (int, optional) – The lower percentile for image normalization. Defaults to 1.
upper_percentile (int, optional) – The upper percentile for image normalization. Defaults to 99.
threshold (int, optional) – The threshold for determining whether to display an image as a mask or normalize it. Defaults to 1000.
extensions (list, optional) – A list of file extensions to consider. Defaults to [‘.npy’, ‘.tif’, ‘.tiff’, ‘.png’].
overlay (bool, optional) – If True, overlay the outlines of the objects on the image. Defaults to False.
- spacr.plot.plot_arrays(src, figuresize=10, cmap='inferno', nr=1, normalize=True, q1=1, q2=99)[source]¶
Plot randomly selected arrays from a given directory.
Parameters: - src (str): The directory path containing the arrays. - figuresize (int): The size of the figure (default: 50). - cmap (str): The colormap to use for displaying the arrays (default: ‘inferno’). - nr (int): The number of arrays to plot (default: 1). - normalize (bool): Whether to normalize the arrays (default: True). - q1 (int): The lower percentile for normalization (default: 1). - q2 (int): The upper percentile for normalization (default: 99).
Returns: None
- spacr.plot.plot_merged(src, settings)[source]¶
Plot the merged images after applying various filters and modifications.
- Parameters:
src (path) – Path to folder with images.
settings (dict) – The settings for the plot.
- Returns:
None
- spacr.plot.generate_plate_heatmap(df, plate_number, variable, grouping, min_max, min_count)[source]¶
- spacr.plot.plot_plates(df, variable, grouping, min_max, cmap, min_count=0, verbose=True, dst=None)[source]¶
- spacr.plot.print_mask_and_flows(stack, mask, flows, overlay=True, max_size=1000, thickness=2)[source]¶
Display the original image, mask with outlines, and flow images.
- Parameters:
stack (np.array) – Original image or stack.
mask (np.array) – Mask image.
flows (list) – List of flow images.
overlay (bool) – Whether to overlay the mask outlines on the original image.
max_size (int) – Maximum allowed size for any dimension of the images.
thickness (int) – Thickness of the contour outlines.
- spacr.plot.normalize_and_visualize(image, normalized_image, title='')[source]¶
Utility function for visualization
- spacr.plot.visualize_cellpose_masks(masks, titles=None, filename=None, save=False, src=None)[source]¶
Visualize multiple masks with optional titles.
- Parameters:
masks (list of np.ndarray) – A list of masks to visualize.
titles (list of str, optional) – A list of titles for the masks. If None, default titles will be used.
comparison_title (str) – Title for the entire figure.
- spacr.plot.plot_object_outlines(src, objects=['nucleus', 'cell', 'pathogen'], channels=[0, 1, 2], max_nr=10)[source]¶
- spacr.plot.plot_lorenz_curves(csv_files, name_column='grna_name', value_column='count', remove_keys=None, x_lim=[0.0, 1], y_lim=[0, 1], remove_outliers=False, save=True)[source]¶
- spacr.plot.read_and_plot__vision_results(base_dir, y_axis='accuracy', name_split='_time', y_lim=[0.8, 0.9])[source]¶
- spacr.plot.jitterplot_by_annotation(src, x_column, y_column, plot_title='Jitter Plot', output_path=None, filter_column=None, filter_values=None)[source]¶
Reads a CSV file and creates a jitter plot of one column grouped by another column.
Args: src (str): Path to the source data. x_column (str): Name of the column to be used for the x-axis. y_column (str): Name of the column to be used for the y-axis. plot_title (str): Title of the plot. Default is ‘Jitter Plot’. output_path (str): Path to save the plot image. If None, the plot will be displayed. Default is None.
Returns: pd.DataFrame: The filtered and balanced DataFrame.
- spacr.plot.create_grouped_plot(df, grouping_column, data_column, graph_type='bar', summary_func='mean', order=None, colors=None, output_dir='./output', save=False, y_lim=None, error_bar_type='std')[source]¶
Create a grouped plot, perform statistical tests, and optionally export the results along with the plot.
Parameters: - df: DataFrame containing the data. - grouping_column: Column name for the categorical grouping. - data_column: Column name for the data to be grouped and plotted. - graph_type: Type of plot (‘bar’, ‘violin’, ‘jitter’, ‘box’, ‘jitter_box’). - summary_func: Summary function to apply to each group (‘mean’, ‘median’, etc.). - order: List specifying the order of the groups. If None, groups will be ordered alphabetically. - colors: List of colors for each group. - output_dir: Directory where the figure and test results will be saved if save=True. - save: Boolean flag indicating whether to save the plot and results to files. - y_lim: Optional y-axis min and max. - error_bar_type: Type of error bars to plot, either ‘std’ for standard deviation or ‘sem’ for standard error of the mean.
Outputs: - Figure of the plot. - DataFrame with full statistical test results, including normality tests.
- class spacr.plot.spacrGraph(df, grouping_column, data_column, graph_type='bar', summary_func='mean', order=None, colors=None, output_dir='./output', save=False, y_lim=None, log_y=False, log_x=False, error_bar_type='std', remove_outliers=False, theme='pastel', representation='object', paired=False, all_to_all=True, compare_group=None, graph_name=None)[source]¶
-
- preprocess_data()[source]¶
Preprocess the data: remove NaNs, optionally ensure ‘plateID’ column is created, then group by either ‘prc’, ‘plateID’, or do no grouping at all if representation == ‘object’.
- perform_statistical_tests(unique_groups, is_normal)[source]¶
Perform statistical tests separately for each data column.
- spacr.plot.plot_image_grid(image_paths, percentiles)[source]¶
Plots a square grid of images from a list of image paths. Unused subplots are filled with black, and padding is minimized.
Parameters: - image_paths: List of paths to images to be displayed.
Returns: - fig: The generated matplotlib figure.
- spacr.plot.overlay_masks_on_images(img_folder, normalize=True, resize=True, save=False, plot=False, thickness=2)[source]¶
Load images and masks from folders, overlay mask contours on images, and optionally normalize, resize, and save.
- Parameters:
img_folder (str) – Path to the folder containing images.
mask_folder (str) – Path to the folder containing masks.
normalize (bool) – If True, normalize images to the 1st and 99th percentiles.
resize (bool) – If True, resize the final overlay to 500x500.
save (bool) – If True, save the final overlay in an ‘overlay’ folder within the image folder.
thickness (int) – Thickness of the contour lines.
- spacr.plot.plot_proportion_stacked_bars(settings, df, group_column, bin_column, prc_column='prc', level='object', cmap='viridis')[source]¶
Generate a stacked bar plot for proportions and perform chi-squared and pairwise tests.
Parameters: - settings (dict): Analysis settings. - df (DataFrame): Input data. - group_column (str): Column indicating the groups. - bin_column (str): Column indicating the categories. - prc_column (str): Optional; column for additional stratification. - level (str): Level of aggregation (‘well’ or ‘object’).
Returns: - chi2 (float): Chi-squared statistic for the overall test. - p (float): p-value for the overall chi-squared test. - dof (int): Degrees of freedom for the overall chi-squared test. - expected (ndarray): Expected frequencies for the overall chi-squared test. - raw_counts (DataFrame): Contingency table of observed counts. - fig (Figure): The generated plot. - pairwise_results (list): Pairwise test results from chi_pairwise.
- spacr.plot.create_venn_diagram(file1, file2, gene_column='gene', filter_coeff=0.1, save=True, save_path=None)[source]¶
Reads two CSV files, extracts the gene column, and creates a Venn diagram to show overlapping and non-overlapping genes.
- Parameters:
file1 (str) – Path to the first CSV file.
file2 (str) – Path to the second CSV file.
gene_column (str) – Name of the column containing gene data (default: “gene”).
filter_coeff (float) – Coefficient threshold for filtering genes.
save (bool) – Whether to save the plot.
save_path (str) – Path to save the Venn diagram figure.
- Returns:
Overlapping and non-overlapping genes.
- Return type:
dict