Extract unique group labels from a categorical or string column in the observation metadata. This function handles different data types commonly used for group labels: - String columns - Integer columns (converted to strings) - Categorical columns (with proper ordering preservation) # Arguments `adata` - The AnnData object `groupby` - Column name in `adata.obs` containing group labels # Returns
(adata: &IMAnnData, groupby: &str)
| 737 | /// # Returns |
| 738 | /// Sorted vector of unique group names as strings |
| 739 | fn get_unique_groups(adata: &IMAnnData, groupby: &str) -> anyhow::Result<Vec<String>> { |
| 740 | let group_col = adata.obs().get_column_from_df(groupby)?; |
| 741 | |
| 742 | let mut all_groups: Vec<String> = match group_col.dtype() { |
| 743 | DataType::String => { |
| 744 | let string_col = group_col.str()?; |
| 745 | let mut unique_groups = std::collections::HashSet::new(); |
| 746 | |
| 747 | for i in 0..string_col.len() { |
| 748 | if let Some(value) = string_col.get(i) { |
| 749 | unique_groups.insert(value.to_string()); |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | unique_groups.into_iter().collect() |
| 754 | } |
| 755 | DataType::Int8 |
| 756 | | DataType::Int16 |
| 757 | | DataType::Int32 |
| 758 | | DataType::Int64 |
| 759 | | DataType::UInt8 |
| 760 | | DataType::UInt16 |
| 761 | | DataType::UInt32 |
| 762 | | DataType::UInt64 => { |
| 763 | let int_col = group_col.i64()?; |
| 764 | let mut unique_groups = std::collections::HashSet::new(); |
| 765 | |
| 766 | for i in 0..int_col.len() { |
| 767 | if let Some(value) = int_col.get(i) { |
| 768 | unique_groups.insert(value.to_string()); |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | unique_groups.into_iter().collect() |
| 773 | } |
| 774 | DataType::Categorical(_, _) => { |
| 775 | let string_col = group_col.cast(&DataType::String)?; |
| 776 | let string_col = string_col.str()?; |
| 777 | let mut unique_groups = std::collections::HashSet::new(); |
| 778 | |
| 779 | for i in 0..string_col.len() { |
| 780 | if let Some(value) = string_col.get(i) { |
| 781 | unique_groups.insert(value.to_string()); |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | unique_groups.into_iter().collect() |
| 786 | } |
| 787 | other => { |
| 788 | return Err(anyhow::anyhow!( |
| 789 | "Unsupported data type for groupby column: {:?}", |
| 790 | other |
| 791 | )); |
| 792 | } |
| 793 | }; |
| 794 | |
| 795 | all_groups.sort(); |
| 796 | Ok(all_groups) |
no outgoing calls
no test coverage detected