-
Notifications
You must be signed in to change notification settings - Fork 129
Closed
Description
Not sure this is an issue! but didnt know where to ask the question.
I am very new to rust and I am trying to write a pyfunction which will take an input as a python list (PyList from pyo3) and each item is a PyArray2 (numpy array). I want ot send this item to another function as a reference. but PyList iter() returns PyAny. How do I convert that to PyArray?
if I directly call preprocess from python and provide a numpy array, it works.
but I cannot send a list of numpy arrays to pylist_implementation
I get error
mismatched types
expected struct `numpy::PyArray`, found reference
note: expected type `&mut numpy::PyArray<f64, ndarray::Dim<[usize; 2]>>`
found type `&mut &pyo3::types::PyAny`rustc(E0308)
lib.rs(89, 25): expected struct `numpy::PyArray`, found reference
Which I understand, that there is not way for the compiler to know what kind of data is in the list. but how to "cast it"?
Any help is appreciated, if this is a wrong place to ask the question apologies in advance.
#[pyfunction]
fn pylist_implementation(data: & mut PyList) -> PyResult<(&PyList)> {
// -> &PyArray2<f64>
for d in data.iter() {
preprocess_data(&mut d, 100);
}
Ok(data)
}
#[pyfunction]
fn preprocess_data(
data: &mut PyArray2<f64>,
index: i32,
) -> PyResult<(Vec<f64>, Vec<f64>, Vec<usize>)> {
let d = data.shape();
remove_baseline(data, index);
let mut peaks: Vec<f64> = Vec::new();
let mut peaks_index: Vec<usize> = Vec::new();
//println!("data in preprocess: {:?}", d);
for subdata in data.as_slice_mut().chunks_exact_mut(d[0]) {
let max = subdata.iter().max_by(|a, b| a.partial_cmp(b).unwrap());
peaks.push(*max.unwrap());
let index = subdata.iter().position(|&r| r == *max.unwrap()).unwrap();
//println!("INDEX {}", index);
peaks_index.push(index);
}
let not_outlier: Vec<bool> = mad_based_outlier(peaks.to_vec());
let mut i = 0;
peaks.retain(|_| (not_outlier[i], i += 1).0);
let mut j = 0;
peaks_index.retain(|_| (not_outlier[j], j += 1).0);
let mut mean_data: Vec<f64> = Vec::new();
for subdata in data.as_array_mut().genrows_mut() {
let mut j = 0;
subdata.to_vec().retain(|_| (not_outlier[j], j += 1).0);
let sum_col: f64 = subdata.iter().sum();
let no_signals = not_outlier.iter().filter(|&n| *n == true).count() as f64;
mean_data.push(sum_col / no_signals);
}
//let interpolated_signal: Vec<f64> = mean_data.interpolate_hermit;
Ok((mean_data, peaks, peaks_index))
}
Metadata
Metadata
Assignees
Labels
No labels