unravel.region_stats.rstats module#

Use rstats from UNRAVEL to quantify cell or label densities for all regions in an atlas.

Prereqs:
  • reg_prep, reg, and seg_ilastik

Inputs:
  • rel_path/segmentation_image.nii.gz (can be glob pattern)

  • rel_path/native_atlas_split.nii.gz (use this -a if this exists from warp_to_native; otherwise, use -m to warp atlas to native space)

Outputs:
  • CSV files in ./sample??/regional_stats/ with cell counts or volumes of segmented voxels, region volumes, and cell or label densities for each region

  • For cell counts, a CSV with cell centroids is also saved

Note

  • Regarding –type, alternatively use ‘counts’ or ‘volumes’ for object counts or regional volumes

  • Default csv: UNRAVEL/unravel/core/csvs/CCFv3-2020__regionID_side_IDpath_region_abbr.csv

  • Columns: Region_ID, Side, ID_path, Region, Abbr

Next steps:
  • Use utils_agg_files to aggregate the CSVs from sample directories to the current directory

  • Use rstats_summary to summarize the results

Usage if the atlas is already in native space from warp_to_native:#

rstats -s rel_path/segmentation_image.nii.gz -a rel_path/native_atlas_split.nii.gz -c Saline –dirs sample14 sample36 [-t cell_densities] [-md parameters/metadata.txt] [-cc 6] [-ro reg_outputs] [-fri autofl_50um_masked_fixed_reg_input.nii.gz] [-r 50] [-csv CCFv3-2020__regionID_side_IDpath_region_abbr.csv] [-mi] [-d list of paths] [-p sample??] [-v]

Usage if the native atlas is not available; it is not saved (faster):#

rstats -s rel_path/segmentation_image.nii.gz -m path/atlas_split.nii.gz -c Saline –dirs sample14 sample36 [-t cell_densities] [-md parameters/metadata.txt] [-cc 6] [-ro reg_outputs] [-fri autofl_50um_masked_fixed_reg_input.nii.gz] [-r 50] [-csv CCFv3-2020__regionID_side_IDpath_region_abbr.csv] [-mi] [-d list of paths] [-p sample??] [-v]

unravel.region_stats.rstats.parse_args()[source]#
unravel.region_stats.rstats.get_atlas_region_at_coords(atlas, x, y, z)[source]#

“Get the ndarray atlas region intensity at the given coordinates

unravel.region_stats.rstats.count_cells_in_regions(sample_path, seg_img, atlas_img, connectivity, condition, region_info_df)[source]#

Count the number of cells in each region based on atlas region intensities

Parameters:#

  • sample_path (Path): Path to the sample directory.

  • seg_img (ndarray): 3D numpy array with the segmented image.

  • atlas_img (ndarray): 3D numpy array with the atlas image.

  • connectivity (int): Connectivity for connected components. Options: 6, 18, or 26.

  • condition (str): Name of the group.

  • region_info_df (DataFrame): DataFrame with region information (Region_ID, Side, ID_path, Region, Abbr).

Returns:#

  • region_counts_df (DataFrame): DataFrame with regional cell counts in the last column (Region_ID, Side, ID_path, Region, Abbr, <condition>_<sample_name>).

  • region_ids (list): List of region IDs in the atlas.

Output:#

  • Saves the regional cell counts as a CSV file in the sample directory (./sample??/regional_stats/)

unravel.region_stats.rstats.calculate_regional_volumes(sample_path, atlas, region_ids, xy_res, z_res, condition, region_info_df)[source]#

Calculate volumes for given regions in an atlas image.

Parameters:#

  • sample_path (Path): Path to the sample directory.

  • atlas (ndarray): 3D numpy array with the atlas image.

  • region_ids (list): List of region IDs to calculate volumes for.

  • xy_res (float): Resolution in the xy plane in microns.

  • z_res (float): Resolution in the z plane in microns.

  • condition (str): Name of the group.

  • region_info_df (DataFrame): DataFrame with region information (Region_ID, Side, ID_path, Region, Abbr).

Returns:#

  • regional_volumes_df (DataFrame): DataFrame with regional volumes in the last column (Region_ID, Side, ID_path, Region, Abbr, <condition>_<sample_name>).

Output:#

  • Saves the regional volumes as a CSV file in the sample directory (./sample??/regional_stats/)

unravel.region_stats.rstats.calculate_regional_densities(sample_path, regional_data_df, regional_volumes_df, condition, density_type='cell_densities')[source]#

Calculate cell or label densities for each region in the atlas.

Parameters:#

  • sample_path (Path): Path to the sample directory.

  • regional_data_df (DataFrame): DataFrame with regional data (object counts or label volumes).

  • regional_volumes_df (DataFrame): DataFrame with regional volumes.

  • condition (str): Name of the group.

  • density_type (str): Type of density to calculate. Options: ‘cell_densities’ or ‘label_densities’.

Output:#

  • Saves the regional densities as a CSV file in the sample directory (./sample??/regional_stats/)

unravel.region_stats.rstats.main()[source]#