Skip to content

Commit dc48fc0

Browse files
committed
Allow opening single tensor in hdf5 file without location hint
1 parent e2cda59 commit dc48fc0

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

palace-hdf5/src/lib.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,26 @@ pub(crate) fn decode_chunk<'a>(
165165

166166
pub fn open(
167167
path: PathBuf,
168-
volume_location: String,
168+
volume_location: Option<String>,
169169
) -> Result<EmbeddedTensorOperator<DDyn, DType>, Error> {
170+
let volume_location = if let Some(volume_location) = volume_location {
171+
volume_location
172+
} else {
173+
let file = hdf5::File::open(&path)?;
174+
let datasets = file.datasets()?;
175+
match datasets.len() {
176+
0 => return Err(format!("No tensors found in dataset").into()),
177+
1 => datasets[0].name(),
178+
n => {
179+
return Err(format!(
180+
"More than ({}) one tensor found and no dataset name given",
181+
n
182+
)
183+
.into())
184+
}
185+
}
186+
};
187+
170188
let state = Hdf5TensorSourceState::open(path, volume_location)?;
171189
Ok(state.operate())
172190
}
@@ -688,7 +706,7 @@ pub fn save_lod_tensor(
688706
let new_md = palace_core::operators::resample::coarser_lod_md(&current, steps.clone());
689707
std::mem::drop(current);
690708

691-
current = open(path.into(), current_location)?;
709+
current = open(path.into(), Some(current_location))?;
692710

693711
current =
694712
palace_core::operators::resample::smooth_downsample(current, new_md.clone()).into();

palace-io/src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,7 @@ pub fn open_single_level(
6565
let data = path.with_extension("img");
6666
palace_nifti::open_separate(path, data)?.into_dyn()
6767
}
68-
[.., "h5" | "hdf5"] => {
69-
palace_hdf5::open(path, hints.location.unwrap_or("/volume".to_owned()))?
70-
}
68+
[.., "h5" | "hdf5"] => palace_hdf5::open(path, hints.location)?,
7169
[.., "zarr"] | [.., "zarr", "zip"] => {
7270
palace_zarr::open(path, hints.location.unwrap_or("/array".to_owned()))?
7371
}

0 commit comments

Comments
 (0)