harpy.tb.bin_counts#
- harpy.tb.bin_counts(sdata, table_name, labels_name, output_table_name, to_coordinate_system='global', chunks=10000, append=True, region_key='fov_labels', instance_key='cell_ID', spatial_key='spatial', cell_index_name='cells', overwrite=False)#
Bins gene counts from barcodes to cells or regions defined in
labels_nameand returns an updated SpatialData object with a table element (sdata.tables[output_table_name]) holding an AnnData object with the binned counts per cell or region.- Parameters:
sdata (
SpatialData) – The SpatialData object.table_name (
str) – The table element holding the counts. E.g. obtained usingharpy.io.visium_hd(). We assume thatsdata[table_name].obsm[spatial_key]contains a numpy array holding the barcode coordinates (‘x’, ‘y’). The relation ofsdata[table_name].obsm[spatial_key]toto_coordinate_systemshould be an identity transformation.labels_name (
str) – The labels element (e.g., segmentation mask, or a grid generated byharpy.im.add_grid_labels()) insdataused to bin barcodes (as specified viatable_name) into cells or regions.output_table_name (
str) – The table element insdatain which to save the AnnData object with the binned counts per cell or region defined bylabels_name.to_coordinate_system (
str(default:'global')) – The coordinate system that holdslabels_name.chunks (
str|tuple[int,...] |int|None(default:10000)) – Chunk sizes for processing. Can be a string, integer, or tuple of integers. Consider setting the chunks to a relatively high value to speed up processing, taking into account the available memory of your system.append (
bool(default:True)) – If set toTrue, and thelabels_namedoes not yet exist as aregion_keyinsdata.tables[output_table_name].obs, the binned counts obtained during the current function call will be appended (along axis=0) tooutput_table_name. IfFalse, andoverwriteis set toTrue, any existing data insdata.tables[output_table_name]will be overwritten by the newly binned counts.instance_key (
str(default:'cell_ID')) – Instance key. The name of the column inAnnDatatable.obsthat will hold the instance ids.region_key (
str(default:'fov_labels')) – Region key. The name of the column inAnnDatatable.obsthat will hold the name of the elements that is annotated by the resulting table.spatial_key (
str(default:'spatial')) – The key in theAnnDatatable.obsmthat will hold thexandycenter of the instances. This center is calculated taking the average x,y coordinate of the assigned spots per bin/cell.cell_index_name (
str(default:'cells')) – The name of the index of the resultingAnnDatatable.overwrite (
bool(default:False)) – IfTrue, overwrites theoutput_table_nameif it already exists insdata.
- Return type:
- Returns:
: An updated SpatialData object with an AnnData table added to
sdata.tablesat slotoutput_table_name.
Example
import harpy as hp sdata_bin = hp.datasets.visium_hd_example_custom_binning() table_name_bins = "square_002um" labels_name = ( "square_labels_32" # custom grid to bin the counts of table_name_bins; can be any segmentation mask ) table_name = "table_custom_bin_32" output_table_name = f"{table_name}_reproduce" # Check that barcodes are unique in table_name_bins of sdata_bin assert sdata_bin.tables[table_name_bins].obs.index.is_unique sdata_bin = hp.tb.bin_counts( sdata_bin, table_name=table_name_bins, labels_name=labels_name, output_table_name=output_table_name, overwrite=True, append=False, )