-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio.rs
387 lines (345 loc) · 15.4 KB
/
io.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
use std::collections::VecDeque;
use crate::complex_opt::{OptComplex, WeightedOptComplex};
use ndarray::{Array2, ArrayView2};
use num_traits::AsPrimitive;
use polars::datatypes::{
DataType, Float32Type, Float64Type, Int32Type, Int64Type, UInt16Type, UInt32Type, UInt64Type,
};
use polars::prelude::*;
use polars_arrow::array::{Array, PrimitiveArray};
/// Convert simplex lists to 2D usize arrays
fn build_usize_ndarray(
simplex_series: &Vec<&ChunkedArray<ListType>>,
i: usize,
row: usize,
dtype: &DataType,
) -> PolarsResult<Array2<usize>> {
let row = simplex_series[i].get_as_series(row).unwrap();
match dtype {
DataType::UInt16 => Ok(row.list()?.to_ndarray::<UInt16Type>()?.mapv(|x| x as usize)),
DataType::UInt32 => Ok(row.list()?.to_ndarray::<UInt32Type>()?.mapv(|x| x as usize)),
DataType::UInt64 => Ok(row.list()?.to_ndarray::<UInt64Type>()?.mapv(|x| x as usize)),
DataType::Int64 => Ok(row.list()?.to_ndarray::<Int64Type>()?.mapv(|x| x as usize)),
DataType::Int32 => Ok(row.list()?.to_ndarray::<Int32Type>()?.mapv(|x| x as usize)),
_ => polars_bail!(InvalidOperation:format!(
"dtype {dtype} not supported for iterating a weighted complex, expected UInt32, UInt64, UInt16, Int64, Int32"
)),
}
}
// Define iterators over vertices and simplices.
// The macro to call is iter_vert_simp!. The logic is seen
// in impl_iter_vert_simp, with generic extensions by the other macros
macro_rules! impl_iter_vert_simp {
($func_name:ident, $ty:ty, $float_dtype:ty, $data_type:expr) => {
pub fn $func_name(
vertices_s: &Series,
simplices_s: &Series,
mut clfn: impl FnMut(&ArrayView2<$ty>, &ArrayView2<usize>) -> Vec<$ty>,
) -> PolarsResult<Series> {
let vertices: &ChunkedArray<ListType> = vertices_s.list()?;
let simplices: &ChunkedArray<ListType> = simplices_s.list()?;
let out: ChunkedArray<ListType> = vertices
.amortized_iter()
.zip(simplices.amortized_iter())
.map(|(v, s)| -> Option<Box<dyn Array>> {
let vert_array = v
.unwrap()
.as_ref()
.list()
.unwrap()
.to_ndarray::<$float_dtype>()
.unwrap();
let simplex_array = s
.unwrap()
.as_ref()
.list()
.unwrap()
.to_ndarray::<UInt32Type>()
.unwrap()
.mapv(|x| x as usize);
let out = clfn(&vert_array.view(), &simplex_array.view());
let prim = Box::new(PrimitiveArray::<$ty>::from_vec(out));
Some(prim as Box<dyn Array>)
})
.collect_ca_with_dtype("".into(), DataType::List(Box::new($data_type)));
Ok(out.into_series())
}
};
}
impl_iter_vert_simp!(iter_vert_simp_f32, f32, Float32Type, DataType::Float32);
impl_iter_vert_simp!(iter_vert_simp_f64, f64, Float64Type, DataType::Float64);
#[macro_export]
macro_rules! iter_vert_simp {
($vertices_s:expr, $simplices_s:expr, |$vertices:ident, $simplices:ident| $closure_body:block) => {{
let vdtype = $vertices_s.dtype().leaf_dtype();
// let sdtype = $simplices_s.dtype().leaf_dtype();
match vdtype {
DataType::Float32 => {
let mut closure = |$vertices: &ArrayView2<f32>, $simplices: &ArrayView2<usize>| -> Vec<f32> {
$closure_body
};
iter_vert_simp_f32($vertices_s, $simplices_s, &mut closure)
},
DataType::Float64 => {
let mut closure = |$vertices: &ArrayView2<f64>, $simplices: &ArrayView2<usize>| -> Vec<f64> {
$closure_body
};
iter_vert_simp_f64($vertices_s, $simplices_s, &mut closure)
},
_ => polars_bail!(ComputeError: "Unsupported data type: {:?}", vdtype),
}
}};
}
macro_rules! impl_iter_vert_simp_weight {
($func_name:ident, $ty:ty, $float_dtype:ty, $ca_method:ident, $data_type:expr) => {
pub fn $func_name(
vertices_s: &Series,
simplices_s: &Series,
weights_s: &Series,
mut clfn: impl FnMut(&ArrayView2<$ty>, &ArrayView2<usize>, &Vec<$ty>) -> Vec<$ty>,
) -> PolarsResult<Series> {
let vertices: &ChunkedArray<ListType> = vertices_s.list()?;
let simplices: &ChunkedArray<ListType> = simplices_s.list()?;
let weights: &ChunkedArray<ListType> = weights_s.list()?;
let out: ChunkedArray<ListType> = vertices
.amortized_iter()
.zip(simplices.amortized_iter())
.zip(weights.amortized_iter())
.map(|((v, s), w)| -> Option<Box<dyn Array>> {
let chunked_weights: &ChunkedArray<$float_dtype> =
w.as_ref().unwrap().as_ref().$ca_method().unwrap();
let w_vec = chunked_weights.to_vec_null_aware().left().unwrap();
let vert_array = v
.unwrap()
.as_ref()
.list()
.unwrap()
.to_ndarray::<$float_dtype>()
.unwrap();
let simplex_array = s
.unwrap()
.as_ref()
.list()
.unwrap()
.to_ndarray::<UInt32Type>()
.unwrap()
.mapv(|x| x as usize);
let out = clfn(&vert_array.view(), &simplex_array.view(), &w_vec);
let prim = Box::new(PrimitiveArray::<$ty>::from_vec(out));
Some(prim as Box<dyn Array>)
})
.collect_ca_with_dtype("".into(), DataType::List(Box::new($data_type)));
Ok(out.into_series())
}
};
}
impl_iter_vert_simp_weight!(
iter_vert_simp_weight_f32,
f32,
Float32Type,
f32,
DataType::Float32
);
impl_iter_vert_simp_weight!(
iter_vert_simp_weight_f64,
f64,
Float64Type,
f64,
DataType::Float64
);
#[macro_export]
macro_rules! iter_vert_simp_weight {
($vertices_s:expr, $simplices_s:expr, $weights_s:expr, |$vertices:ident, $simplices:ident, $weights:ident| $closure_body:block) => {{
let vdtype = $vertices_s.dtype().leaf_dtype();
// let sdtype = $simplices_s.dtype().leaf_dtype();
let wdtype = $weights_s.dtype().leaf_dtype();
if vdtype != wdtype {
polars_bail!(InvalidOperation:format!(
"dtype mismatch between vertices and weights, got {vdtype} and {wdtype}"
));
}
match vdtype {
DataType::Float32 => {
let mut closure = |$vertices: &ArrayView2<f32>, $simplices: &ArrayView2<usize>, $weights: &Vec<f32>| -> Vec<f32> {
$closure_body
};
iter_vert_simp_weight_f32($vertices_s, $simplices_s, $weights_s, &mut closure)
},
DataType::Float64 => {
let mut closure = |$vertices: &ArrayView2<f64>, $simplices: &ArrayView2<usize>, $weights: &Vec<f64>| -> Vec<f64> {
$closure_body
};
iter_vert_simp_weight_f64($vertices_s, $simplices_s, $weights_s, &mut closure)
},
_ => polars_bail!(ComputeError: "Unsupported data type: {:?}", vdtype),
}
}};
}
macro_rules! impl_iter_complex {
($func_name:ident, $ty:ty, $float_dtype:ty, $data_type:expr) => {
pub fn $func_name(
simplices_s: &Series,
mut complex_fn: impl FnMut(&mut OptComplex<$ty>) -> Vec<$ty>,
) -> PolarsResult<Series> {
let simplex_fields: Vec<Series> = simplices_s.struct_()?.fields_as_series();
let simplex_series: Vec<&ChunkedArray<ListType>> =
simplex_fields.iter().map(|x| x.list().unwrap()).collect();
// let vdtype = simplex_series[0].dtype().leaf_dtype();
let sdtype = simplex_series[1].dtype().leaf_dtype();
let out: ChunkedArray<ListType> = simplex_series[0]
.amortized_iter()
.enumerate()
.map(|(j, v)| -> Option<Box<dyn Array>> {
let vertices_array: &ListChunked = v.as_ref().unwrap().as_ref().list().unwrap();
let vertices = vertices_array.to_ndarray::<$float_dtype>().unwrap();
let simplices: VecDeque<Array2<usize>> = (1..simplex_series.len())
.map(|i| -> PolarsResult<Array2<usize>> {
build_usize_ndarray(&simplex_series, i, j, &sdtype)
})
.collect::<PolarsResult<VecDeque<Array2<usize>>>>()
.unwrap();
let psimps: Vec<usize> = simplices.iter().map(|x| x.shape()[1] - 1).collect();
let mut complex = OptComplex::from_provided(vertices, simplices, &psimps);
let out = complex_fn(&mut complex);
let prim = Box::new(PrimitiveArray::<$ty>::from_vec(out));
Some(prim as Box<dyn Array>)
})
.collect_ca_with_dtype("".into(), DataType::List(Box::new($data_type)));
Ok(out.into_series())
}
};
}
impl_iter_complex!(iter_complex_f32, f32, Float32Type, DataType::Float32);
impl_iter_complex!(iter_complex_f64, f64, Float64Type, DataType::Float64);
#[macro_export]
macro_rules! iter_complex {
($simplices_s:expr, |$complex:ident| $closure_body:block) => {{
let simplex_fields: Vec<Series> = $simplices_s.struct_()?.fields_as_series();
let vdtype = simplex_fields[0].dtype().leaf_dtype();
match vdtype {
DataType::Float32 => {
let mut closure = |$complex: &mut OptComplex<f32>| -> Vec<f32> {
$closure_body
};
iter_complex_f32($simplices_s, &mut closure)
},
DataType::Float64 => {
let mut closure = |$complex: &mut OptComplex<f64>| -> Vec<f64> {
$closure_body
};
iter_complex_f64($simplices_s, &mut closure)
},
_ => polars_bail!(ComputeError: "Unsupported vertex data type: {:?}", vdtype),
}
}};
}
// Iterate over a weighted complex.
// The macro to call is iter_weighted_complex!
macro_rules! impl_iter_weighted_complex {
($func_name:ident, $ty:ty, $float_dtype:ty, $ca_method:ident, $data_type:expr) => {
pub fn $func_name(
simplices_s: &Series,
weights_s: &Series,
pweights: Vec<usize>,
mut complex_fn: impl FnMut(&mut WeightedOptComplex<$ty, $ty>) -> Vec<$ty>,
) -> PolarsResult<Series> {
let simplex_fields: Vec<Series> = simplices_s.struct_()?.fields_as_series();
let simplex_series: Vec<&ChunkedArray<ListType>> =
simplex_fields.iter().map(|x| x.list().unwrap()).collect();
let weight_fields: Vec<Series> = weights_s.struct_()?.fields_as_series();
let weight_series: Vec<&ChunkedArray<ListType>> =
weight_fields.iter().map(|x| x.list().unwrap()).collect();
let vdtype = simplex_series[0].dtype().leaf_dtype();
let sdtype = simplex_series[1].dtype().leaf_dtype();
let wdtype = weight_series[0].dtype().leaf_dtype();
if vdtype != wdtype {
polars_bail!(InvalidOperation:format!(
"dtype mismatch between vertices and weights, got {vdtype} and {wdtype}"
));
}
let out: ChunkedArray<ListType> = simplex_series[0]
.amortized_iter()
.enumerate()
.map(|(j, v)| -> Option<Box<dyn Array>> {
let simplices: VecDeque<Array2<usize>> = (1..simplex_series.len())
.map(|i| -> PolarsResult<Array2<usize>> {
build_usize_ndarray(&simplex_series, i, j, &sdtype)
})
.collect::<PolarsResult<VecDeque<Array2<usize>>>>()
.unwrap();
let psimps: Vec<usize> =
simplices.iter().map(|x| x.shape()[1] - 1).collect();
let vertices_array: &ListChunked =
v.as_ref().unwrap().as_ref().list().unwrap();
let vertices = vertices_array.to_ndarray::<$float_dtype>().unwrap();
let weights: VecDeque<Vec<$ty>> = weight_series
.iter()
.map(|x| {
x.get_as_series(j)
.unwrap()
.$ca_method()
.unwrap()
.to_vec_null_aware()
.left()
.unwrap()
})
.collect();
let mut complex = WeightedOptComplex::from_provided(
vertices,
simplices,
weights,
&psimps,
&pweights,
);
let out = complex_fn(&mut complex);
let prim = Box::new(PrimitiveArray::<$ty>::from_vec(out));
Some(prim as Box<dyn Array>)
})
.collect_ca_with_dtype("".into(), DataType::List(Box::new($data_type)));
Ok(out.into_series())
}
};
}
impl_iter_weighted_complex!(
iter_weighted_complex_f32,
f32,
Float32Type,
f32,
DataType::Float32
);
impl_iter_weighted_complex!(
iter_weighted_complex_f64,
f64,
Float64Type,
f64,
DataType::Float64
);
#[macro_export]
macro_rules! iter_weighted_complex {
($simplices_s:expr, $weights_s:expr, $pweights:expr, |$complex:ident| $closure_body:block) => {{
let simplex_fields: Vec<Series> = $simplices_s.struct_()?.fields_as_series();
let weight_fields: Vec<Series> = $weights_s.struct_()?.fields_as_series();
let vdtype = simplex_fields[0].dtype().leaf_dtype();
let wdtype = weight_fields[0].dtype().leaf_dtype();
if vdtype != wdtype {
polars_bail!(InvalidOperation:format!(
"dtype mismatch between vertices and weights, got {vdtype} and {wdtype}"
));
}
match vdtype {
DataType::Float32 => {
let mut closure = |$complex: &mut WeightedOptComplex<f32, f32>| -> Vec<f32> {
$closure_body
};
iter_weighted_complex_f32($simplices_s, $weights_s, $pweights, &mut closure)
},
DataType::Float64 => {
let mut closure = |$complex: &mut WeightedOptComplex<f64, f64>| -> Vec<f64> {
$closure_body
};
iter_weighted_complex_f64($simplices_s, $weights_s, $pweights, &mut closure)
},
_ => polars_bail!(ComputeError: "Unsupported data type: {:?}", vdtype),
}
}};
}