|
1 | 1 | extern crate wkwrap; |
| 2 | +extern crate zarrs; |
2 | 3 |
|
3 | 4 | #[macro_use] |
4 | 5 | extern crate wkw_mex; |
5 | 6 | use wkw_mex::*; |
| 7 | +use wkwrap::{Box3, Mat, Vec3, VoxelType}; |
| 8 | +use zarrs::array::data_type::DataType; |
| 9 | +use zarrs::array::Array; |
| 10 | +use zarrs::array_subset::ArraySubset; |
6 | 11 |
|
| 12 | +use std::path::PathBuf; |
7 | 13 | use std::slice; |
8 | | -use std::path::Path; |
| 14 | +use std::sync::Arc; |
| 15 | + |
| 16 | +fn zarrs_result_to_str_error<T, E: std::error::Error>( |
| 17 | + result: std::result::Result<T, E>, |
| 18 | +) -> Result<T> { |
| 19 | + match result { |
| 20 | + Ok(ok) => Ok(ok), |
| 21 | + Err(err) => Err(err.to_string()), |
| 22 | + } |
| 23 | +} |
9 | 24 |
|
10 | 25 | mex_function!(nlhs, lhs, nrhs, rhs, { |
11 | 26 | let rhs = match nrhs == 2 { |
12 | 27 | true => slice::from_raw_parts(rhs, nrhs as usize), |
13 | | - false => return Err("Invalid number of input arguments".to_string()) |
| 28 | + false => return Err("Invalid number of input arguments".to_string()), |
14 | 29 | }; |
15 | 30 |
|
16 | 31 | let lhs = match nlhs == 1 { |
17 | 32 | true => slice::from_raw_parts_mut(lhs, nlhs as usize), |
18 | | - false => return Err("Invalid number of output arguments".to_string()) |
| 33 | + false => return Err("Invalid number of output arguments".to_string()), |
19 | 34 | }; |
20 | 35 |
|
21 | | - let wkw_path = mx_array_to_str(rhs[0])?; |
22 | | - let dataset_path = Path::new(wkw_path); |
23 | | - let dataset = wkwrap::Dataset::new(dataset_path)?; |
| 36 | + let store_path: PathBuf = mx_array_to_str(rhs[0])?.into(); |
| 37 | + |
| 38 | + let store: zarrs::storage::ReadableWritableListableStorage = Arc::new( |
| 39 | + zarrs_result_to_str_error(zarrs::filesystem::FilesystemStore::new(&store_path))?, |
| 40 | + ); |
| 41 | + let array = zarrs_result_to_str_error(Array::open(store.clone(), "/"))?; |
24 | 42 |
|
25 | | - let num_channels = dataset.header().num_channels(); |
| 43 | + let num_channels = array.shape()[0] as usize; |
26 | 44 | let is_multi_channel = num_channels > 1; |
27 | 45 |
|
28 | 46 | // build shape |
29 | 47 | let bbox = mx_array_to_wkwrap_box(rhs[1])?; |
30 | | - let shape = bbox.width(); |
| 48 | + let subset = zarrs_result_to_str_error(ArraySubset::new_with_start_shape( |
| 49 | + vec![ |
| 50 | + 0, |
| 51 | + bbox.min().x as u64, |
| 52 | + bbox.min().y as u64, |
| 53 | + bbox.min().z as u64, |
| 54 | + ], |
| 55 | + vec![ |
| 56 | + 1, |
| 57 | + bbox.width().x as u64, |
| 58 | + bbox.width().y as u64, |
| 59 | + bbox.width().z as u64, |
| 60 | + ], |
| 61 | + ))?; |
31 | 62 |
|
32 | | - let shape_arr = [num_channels, shape.x as usize, shape.y as usize, shape.z as usize]; |
33 | | - let shape_slice = if is_multi_channel { &shape_arr[0..] } else { &shape_arr[1..] }; |
| 63 | + let shape_arr = [ |
| 64 | + num_channels, |
| 65 | + bbox.width().x as usize, |
| 66 | + bbox.width().y as usize, |
| 67 | + bbox.width().z as usize, |
| 68 | + ]; |
| 69 | + let shape_slice = if is_multi_channel { |
| 70 | + &shape_arr[0..] |
| 71 | + } else { |
| 72 | + &shape_arr[1..] |
| 73 | + }; |
34 | 74 |
|
35 | 75 | // prepare allocation |
36 | | - let voxel_type = dataset.header().voxel_type; |
37 | | - let class = voxel_type_to_mx_class_id(voxel_type); |
| 76 | + let voxel_type = match array.data_type() { |
| 77 | + DataType::UInt8 => VoxelType::U8, |
| 78 | + DataType::UInt16 => VoxelType::U16, |
| 79 | + DataType::UInt32 => VoxelType::U32, |
| 80 | + DataType::UInt64 => VoxelType::U64, |
| 81 | + DataType::Float32 => VoxelType::F32, |
| 82 | + DataType::Float64 => VoxelType::F64, |
| 83 | + DataType::Int8 => VoxelType::I8, |
| 84 | + DataType::Int16 => VoxelType::I16, |
| 85 | + DataType::Int32 => VoxelType::I32, |
| 86 | + DataType::Int64 => VoxelType::I64, |
| 87 | + _ => { |
| 88 | + return Err("Unsupported data type".to_string()); |
| 89 | + } |
| 90 | + }; |
| 91 | + let class = match array.data_type() { |
| 92 | + DataType::UInt8 => MxClassId::Uint8, |
| 93 | + DataType::UInt16 => MxClassId::Uint16, |
| 94 | + DataType::UInt32 => MxClassId::Uint32, |
| 95 | + DataType::UInt64 => MxClassId::Uint64, |
| 96 | + DataType::Float32 => MxClassId::Single, |
| 97 | + DataType::Float64 => MxClassId::Double, |
| 98 | + DataType::Int8 => MxClassId::Int8, |
| 99 | + DataType::Int16 => MxClassId::Int16, |
| 100 | + DataType::Int32 => MxClassId::Int32, |
| 101 | + DataType::Int64 => MxClassId::Int64, |
| 102 | + _ => { |
| 103 | + return Err("Unsupported data type".to_string()); |
| 104 | + } |
| 105 | + }; |
38 | 106 |
|
39 | 107 | // read data |
| 108 | + let data_all = zarrs_result_to_str_error(array.retrieve_array_subset(&subset))?; |
| 109 | + let mut buf = zarrs_result_to_str_error(data_all.into_fixed())?.into_owned(); // in c-order |
| 110 | + let src_mat = Mat::new( |
| 111 | + &mut buf, |
| 112 | + bbox.width(), |
| 113 | + voxel_type.size() * num_channels, |
| 114 | + voxel_type, |
| 115 | + true, |
| 116 | + )?; |
40 | 117 | let arr = create_numeric_array(shape_slice, class, MxComplexity::Real)?; |
41 | 118 | let mut mat = mx_array_mut_to_wkwrap_mat(is_multi_channel, arr)?; |
42 | | - dataset.read_mat(bbox.min(), &mut mat)?; |
| 119 | + |
| 120 | + src_mat.copy_as_fortran_order( |
| 121 | + &mut mat, |
| 122 | + Box3::new(Vec3 { x: 0, y: 0, z: 0 }, bbox.width())?, |
| 123 | + )?; |
43 | 124 |
|
44 | 125 | // set output |
45 | 126 | lhs[0] = arr; |
|
0 commit comments