MCPcopy Index your code
hub / github.com/SingleRust/SingleRust / get_group_indices

Function get_group_indices

src/memory/processing/diffexp/mod.rs:890–961  ·  view source on GitHub ↗

Get row indices for cells belonging to a specific group. Searches through the groupby column and returns the indices of all cells that match the specified group label. Handles different column data types including strings, integers, and categorical data. # Arguments `adata` - The AnnData object `groupby` - Column name in `adata.obs` containing group labels `group` - Group label to search for #

(adata: &IMAnnData, groupby: &str, group: &str)

Source from the content-addressed store, hash-verified

888/// - No cells found for the specified group
889/// - Data type conversion fails
890fn get_group_indices(adata: &IMAnnData, groupby: &str, group: &str) -> anyhow::Result<Vec<usize>> {
891 let group_col = adata.obs().get_column_from_df(groupby)?;
892
893 let indices = match group_col.dtype() {
894 DataType::String => {
895 let string_col = group_col.str()?;
896 let mut indices = Vec::new();
897
898 for i in 0..string_col.len() {
899 if let Some(value) = string_col.get(i) {
900 if value == group {
901 indices.push(i);
902 }
903 }
904 }
905 indices
906 }
907 DataType::Int8
908 | DataType::Int16
909 | DataType::Int32
910 | DataType::Int64
911 | DataType::UInt8
912 | DataType::UInt16
913 | DataType::UInt32
914 | DataType::UInt64 => {
915 let target = group.parse::<i64>().map_err(|_| {
916 anyhow::anyhow!(
917 "Failed to parse group '{}' as integer for numeric column",
918 group
919 )
920 })?;
921
922 let mut indices = Vec::new();
923 let int_col = group_col.i64()?;
924 for i in 0..int_col.len() {
925 if let Some(value) = int_col.get(i) {
926 if value == target {
927 indices.push(i);
928 }
929 }
930 }
931 indices
932 }
933 DataType::Categorical(_, _) => {
934 let string_col = group_col.cast(&DataType::String)?;
935 let string_col = string_col.str()?;
936 let mut indices = Vec::new();
937
938 for i in 0..string_col.len() {
939 if let Some(value) = string_col.get(i) {
940 if value == group {
941 indices.push(i);
942 }
943 }
944 }
945
946 indices
947 }

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected