Skip to content

Commit 4ccbe74

Browse files
committed
updated versions, adjusted fields to match new requirements, anndata bumped to 0.6.1
1 parent a69cdb0 commit 4ccbe74

File tree

5 files changed

+41
-43
lines changed

5 files changed

+41
-43
lines changed

src/ad/helpers.rs

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,20 @@ use std::{
22
collections::HashMap,
33
fmt,
44
ops::{Deref, DerefMut},
5-
sync::Arc,
65
};
76

87
use anndata::{
98
backend::DataType,
109
container::{Axis, Dim},
11-
data::{DataFrameIndex, DynArray, DynCscMatrix, DynCsrMatrix, SelectInfoElem, Shape},
12-
ArrayData, ArrayOp, Data, HasShape, WriteData,
10+
data::{DataFrameIndex, DynArray, DynCscMatrix, DynCsrMatrix, Element, SelectInfoElem, Shape},
11+
ArrayData, Data, HasShape, Selectable,
1312
};
14-
use anyhow::{anyhow, bail};
13+
use anyhow::bail;
1514

1615
use ndarray::Array2;
1716
use polars::{
1817
frame::DataFrame,
19-
prelude::{IdxCa, NamedFrom},
18+
prelude::{Column, IdxCa, NamedFrom},
2019
series::Series,
2120
};
2221

@@ -68,14 +67,14 @@ impl IMArrayElement {
6867
pub fn convert_matrix_format(&self) -> anyhow::Result<()> {
6968
let mut write_guard = self.0.write_inner();
7069
let d = write_guard.deref_mut();
71-
70+
7271
// Create a placeholder that we can swap with - use an empty dense array as it's likely the smallest
7372
let ddata: Array2<f64> = Array2::zeros((0, 0));
7473
let placeholder = ArrayData::Array(DynArray::from(ddata));
75-
74+
7675
// Take ownership using replace
7776
let matrix_data = std::mem::replace(d, placeholder);
78-
77+
7978
let converted = match matrix_data {
8079
ArrayData::CsrMatrix(dyn_csr_matrix) => {
8180
let csc = match dyn_csr_matrix {
@@ -91,7 +90,6 @@ impl IMArrayElement {
9190
DynCsrMatrix::U8(m) => DynCscMatrix::U8(m.transpose_as_csc()),
9291
DynCsrMatrix::Bool(m) => DynCscMatrix::Bool(m.transpose_as_csc()),
9392
DynCsrMatrix::String(m) => DynCscMatrix::String(m.transpose_as_csc()),
94-
DynCsrMatrix::Usize(m) => DynCscMatrix::Usize(m.transpose_as_csc()),
9593
};
9694
ArrayData::CscMatrix(csc)
9795
}
@@ -109,7 +107,6 @@ impl IMArrayElement {
109107
DynCscMatrix::U8(m) => DynCsrMatrix::U8(m.transpose_as_csr()),
110108
DynCscMatrix::Bool(m) => DynCsrMatrix::Bool(m.transpose_as_csr()),
111109
DynCscMatrix::String(m) => DynCsrMatrix::String(m.transpose_as_csr()),
112-
DynCscMatrix::Usize(m) => DynCsrMatrix::Usize(m.transpose_as_csr()),
113110
};
114111
ArrayData::CsrMatrix(csr)
115112
}
@@ -119,7 +116,7 @@ impl IMArrayElement {
119116
bail!("This datatype is not supported, only CSC and CSR matrices are supported.")
120117
}
121118
};
122-
119+
123120
*d = converted;
124121
Ok(())
125122
}
@@ -182,7 +179,8 @@ impl IMDataFrameElement {
182179
pub fn new(df: DataFrame, index: DataFrameIndex) -> Self {
183180
if df.height() == 0 {
184181
let tmp_df =
185-
DataFrame::new(vec![Series::new("index", &index.clone().into_vec())]).unwrap();
182+
DataFrame::new(vec![Column::new("index".into(), &index.clone().into_vec())])
183+
.unwrap();
186184
return IMDataFrameElement(RwSlot::new(InnerIMDataFrame { df: tmp_df, index }));
187185
}
188186
if df.height() != index.len() {
@@ -282,12 +280,12 @@ impl IMDataFrameElement {
282280
}
283281
}
284282

285-
pub fn get_column_from_df(&self, column_name: &str) -> anyhow::Result<Series> {
283+
pub fn get_column_from_df(&self, column_name: &str) -> anyhow::Result<Column> {
286284
let read_guard = self.0.lock_read();
287285
let d = read_guard.as_ref();
288286
match d {
289287
Some(data) => match data.df.column(column_name) {
290-
Ok(series) => Ok(series.clone()),
288+
Ok(column) => Ok(column.clone()),
291289
Err(e) => Err(anyhow::anyhow!("Column not found: {}", e)),
292290
},
293291
None => Err(anyhow::anyhow!("DataFrame is not initialized")),
@@ -311,7 +309,7 @@ impl IMDataFrameElement {
311309
let d = read_guard.as_ref().unwrap();
312310
let indices = crate::utils::select_info_elem_to_indices(s, d.index.len())?;
313311
let indices_u32: Vec<u32> = indices.iter().map(|&i| i as u32).collect();
314-
let idx = IdxCa::new("idx", &indices_u32);
312+
let idx = IdxCa::new("idx".into(), &indices_u32);
315313
let ind = d.index.clone().into_vec();
316314
let ind_subset: Vec<String> = indices.iter().map(|&i| ind[i].clone()).collect();
317315
let df_subset = d.df.take(&idx)?;
@@ -324,7 +322,7 @@ impl IMDataFrameElement {
324322
let d = read_guard.as_ref().unwrap();
325323
let indices = crate::utils::select_info_elem_to_indices(s, d.index.len())?;
326324
let indices_u32: Vec<u32> = indices.iter().map(|&i| i as u32).collect();
327-
let idx = IdxCa::new("idx", &indices_u32);
325+
let idx = IdxCa::new("idx".into(), &indices_u32);
328326
let ind = d.index.clone().into_vec();
329327
let ind_subset: Vec<String> = indices.iter().map(|&i| ind[i].clone()).collect();
330328
let df_subset = d.df.take(&idx)?;
@@ -620,29 +618,29 @@ impl IMAxisArrays {
620618
}
621619
}
622620

623-
pub struct Element(pub RwSlot<Data>);
621+
pub struct IMElement(pub RwSlot<Data>);
624622

625623
impl DeepClone for Data {
626624
fn deep_clone(&self) -> Self {
627625
self.clone()
628626
}
629627
}
630628

631-
impl DeepClone for Element {
629+
impl DeepClone for IMElement {
632630
fn deep_clone(&self) -> Self {
633-
Element(self.0.deep_clone())
631+
IMElement(self.0.deep_clone())
634632
}
635633
}
636634

637-
impl Clone for Element {
635+
impl Clone for IMElement {
638636
fn clone(&self) -> Self {
639-
Element(self.0.clone())
637+
IMElement(self.0.clone())
640638
}
641639
}
642640

643-
impl Element {
641+
impl IMElement {
644642
pub fn new(data: Data) -> Self {
645-
Element(RwSlot::new(data))
643+
IMElement(RwSlot::new(data))
646644
}
647645

648646
pub fn get_data(&self) -> anyhow::Result<Data> {
@@ -657,7 +655,7 @@ impl Element {
657655
}
658656
}
659657

660-
pub struct IMElementCollection(pub RwSlot<HashMap<String, Element>>);
658+
pub struct IMElementCollection(pub RwSlot<HashMap<String, IMElement>>);
661659

662660
impl DeepClone for IMElementCollection {
663661
fn deep_clone(&self) -> Self {
@@ -682,7 +680,7 @@ impl IMElementCollection {
682680
IMElementCollection(RwSlot::new(HashMap::new()))
683681
}
684682

685-
pub fn add_data(&self, key: String, element: Element) -> anyhow::Result<()> {
683+
pub fn add_data(&self, key: String, element: IMElement) -> anyhow::Result<()> {
686684
let mut write_guard = self.0.write_inner();
687685
let collection = write_guard.deref_mut();
688686
if collection.contains_key(&key) {
@@ -692,22 +690,22 @@ impl IMElementCollection {
692690
Ok(())
693691
}
694692

695-
pub fn remove_data(&self, key: &str) -> anyhow::Result<Element> {
693+
pub fn remove_data(&self, key: &str) -> anyhow::Result<IMElement> {
696694
let mut write_guard = self.0.write_inner();
697695
write_guard
698696
.remove(key)
699697
.ok_or_else(|| anyhow::anyhow!("Key not found"))
700698
}
701699

702-
pub fn get_data(&self, key: &str) -> anyhow::Result<Element> {
700+
pub fn get_data(&self, key: &str) -> anyhow::Result<IMElement> {
703701
let read_guard = self.0.read_inner();
704702
read_guard
705703
.get(key)
706704
.cloned()
707705
.ok_or_else(|| anyhow::anyhow!("Key not found"))
708706
}
709707

710-
pub fn get_data_deep(&self, key: &str) -> anyhow::Result<Element> {
708+
pub fn get_data_deep(&self, key: &str) -> anyhow::Result<IMElement> {
711709
let read_guard = self.0.read_inner();
712710
read_guard
713711
.get(key)

src/ad/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use anndata::{
55
};
66
use helpers::IMAxisArrays;
77
use log::{log, Level};
8-
use polars::{frame::DataFrame, prelude::NamedFrom, series::Series};
8+
use polars::{frame::DataFrame, prelude::Column};
99

1010
use crate::{base::DeepClone, IMArrayElement, IMDataFrameElement, IMElementCollection};
1111

@@ -111,12 +111,12 @@ impl IMAnnData {
111111
}
112112

113113
// Create basic obs DataFrame and IMDataFrameElement
114-
let obs_df = DataFrame::new(vec![Series::new("index", &obs_names)])?;
114+
let obs_df = DataFrame::new(vec![Column::new("index".into(), &obs_names)])?;
115115
let obs_index: DataFrameIndex = obs_names.into();
116116
let obs = IMDataFrameElement::new(obs_df, obs_index);
117117

118118
// Create basic var DataFrame and IMDataFrameElement
119-
let var_df = DataFrame::new(vec![Series::new("index", &var_names)])?;
119+
let var_df = DataFrame::new(vec![Column::new("index".into(), &var_names)])?;
120120
let var_index: DataFrameIndex = var_names.into();
121121
let var = IMDataFrameElement::new(var_df, var_index);
122122

src/converter.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use std::ops::Deref;
22

3-
use anndata::{AnnData, AnnDataOp, ArrayData, ArrayElemOp, AxisArrays, Backend, Data, ElemCollection};
3+
use anndata::{AnnData, AnnDataOp, ArrayData, ArrayElemOp, AxisArrays, Backend, ElemCollection};
44
use anyhow::Ok;
55

6-
use crate::{ad::helpers::{Element, IMAxisArrays}, IMAnnData, IMArrayElement, IMElementCollection};
6+
use crate::{ad::helpers::{IMElement, IMAxisArrays}, IMAnnData, IMArrayElement, IMElementCollection};
77

88
pub fn convert_to_in_memory<B: Backend>(anndata: AnnData<B>) -> anyhow::Result<IMAnnData> {
99
let obs_df = anndata.read_obs()?;
@@ -44,8 +44,8 @@ fn convert_uns_to_mem<B: Backend>(elem_col: &ElemCollection<B>, reference_elemen
4444
let iax = x.deref();
4545
let data = iax.deref();
4646
for (k,v) in data.iter() {
47-
let data = v.inner().data::<Data>();
48-
let d = Element::new(data?);
47+
let data = v.inner().data();
48+
let d = IMElement::new(data?);
4949
reference_element.add_data(k.to_string(), d)?;
5050
}
5151
Ok(())

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ pub use ad::IMAnnData;
77
pub use ad::helpers::IMArrayElement;
88
pub use ad::helpers::IMDataFrameElement;
99
pub use ad::helpers::IMElementCollection;
10-
pub use ad::helpers::Element;
10+
pub use ad::helpers::IMElement;
1111
pub use ad::helpers::IMAxisArrays;
1212
pub use converter::convert_to_in_memory;
1313
pub use base::DeepClone;

tests/test_basic.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ fn test_convert_matrix_format() {
4141
// Convert to CSR format
4242
let csr = CsrMatrix::from(&coo);
4343
let array_data = ArrayData::CsrMatrix(DynCsrMatrix::F64(csr));
44-
let mut matrix = IMArrayElement::new(array_data);
44+
let matrix = IMArrayElement::new(array_data);
4545

4646
// Convert CSR to CSC
4747
matrix.convert_matrix_format().unwrap();
@@ -72,37 +72,37 @@ fn test_convert_matrix_format() {
7272
// Check specific values
7373
assert_eq!(
7474
m.triplet_iter()
75-
.find(|&(i, j, &v)| i == 0 && j == 0)
75+
.find(|&(i, j, &_v)| i == 0 && j == 0)
7676
.map(|(_, _, &v)| v),
7777
Some(1.0)
7878
);
7979
assert_eq!(
8080
m.triplet_iter()
81-
.find(|&(i, j, &v)| i == 1 && j == 1)
81+
.find(|&(i, j, &_v)| i == 1 && j == 1)
8282
.map(|(_, _, &v)| v),
8383
Some(2.0)
8484
);
8585
assert_eq!(
8686
m.triplet_iter()
87-
.find(|&(i, j, &v)| i == 1 && j == 2)
87+
.find(|&(i, j, &_v)| i == 1 && j == 2)
8888
.map(|(_, _, &v)| v),
8989
Some(3.0)
9090
);
9191
assert_eq!(
9292
m.triplet_iter()
93-
.find(|&(i, j, &v)| i == 2 && j == 3)
93+
.find(|&(i, j, &_v)| i == 2 && j == 3)
9494
.map(|(_, _, &v)| v),
9595
Some(4.0)
9696
);
9797
assert_eq!(
9898
m.triplet_iter()
99-
.find(|&(i, j, &v)| i == 3 && j == 1)
99+
.find(|&(i, j, &_v)| i == 3 && j == 1)
100100
.map(|(_, _, &v)| v),
101101
Some(5.0)
102102
);
103103
assert_eq!(
104104
m.triplet_iter()
105-
.find(|&(i, j, &v)| i == 4 && j == 3)
105+
.find(|&(i, j, &_v)| i == 4 && j == 3)
106106
.map(|(_, _, &v)| v),
107107
Some(6.0)
108108
);

0 commit comments

Comments
 (0)