Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Selection::points throws a runtime error as it only accepts 2D array and then shows runtime error ndim (1) != array ndim (2) #30

Open
robinpaul85 opened this issue Oct 18, 2024 · 3 comments

Comments

@robinpaul85
Copy link

robinpaul85 commented Oct 18, 2024

I am trying to query non-contiguous parts of a matrix in a single HDF5 query. For this I am try to use Selection::points. It seems Selection:points needs a 2D array other wise it throws a compilation error. However when I put in a 2D array (points2) it gives a runtime error Error: Selection ndim (1) != array ndim (2)

In general, I feel better documentation and examples should be provided regarding how to use this functions.

use hdf5::Selection;
use hdf5::{File, Result};
use ndarray::array;
use ndarray::Array1;
use ndarray::Array2;

fn main() -> Result<()> {
    // Step 3: Create an HDF5 file and dataset
    let file = File::create("example.h5")?;
    let dataset = file
        .new_dataset::<f64>()
        .shape((10, 10))
        .create("dataset")?;

    // Initialize the dataset with some data
    let data: Array2<f64> = Array2::from_shape_fn((10, 10), |(i, j)| (i * 10 + j + 1) as f64);

    println!("data:{:?}", data);
    dataset.write(&data)?;

    // Step 4: Define point selection to read non-contiguous parts of a matrix

    // Define the points to select (row, column)
    let points2: Array2<usize> = Array2::from_shape_vec((3, 2), vec![0, 2, 3, 5, 7, 9]).unwrap();
    let points: Array1<usize> = array![0, 3, 7, 9];
    println!("points ndim:{:?}", points.ndim());
    println!("points ndim:{:?}", points2.ndim());
    println!("points:{}", points);
    println!("points2:{}", points2);
    // Read the selected points from the dataset
    //let array2: Array2<f64> = dataset.read_slice_2d(Selection::Points(points2))?; // Gives a compilation error asking add 2D array.
    let array2: Array2<f64> = dataset.read_slice_2d(Selection::Points(points2))?;  // This compiles, but gives a runtime error: Error: Selection ndim (1) != array ndim (2) 
    println!("Read non-contiguous matrix points: {:?}", array2);
    Ok(())
}```
@magnusuMET
Copy link
Collaborator

I think it is working as expected as points will always be output as a 1D vector, and you are requesting a 2D output (read_slice_2d). You can modify your script as below to read the specified points.

let array: Array1<f64> = dataset.read_slice(Selection::Points(points2))?;

@robinpaul85
Copy link
Author

Is it possible to implement Lines within selection ? For querying a row, I will then have to list all the individual cells in that row.

@magnusuMET
Copy link
Collaborator

If you want a hyperslab you can use items such as

let subselector = (4, 3..=8, ..);
let all_selector = ..;
let ndarray_selector = ndarray::s![/* items here */];

Or if you want strides and blocks: a Vec of SliceOrIndex

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants