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

Fix old I03 data not processing #737

Merged
merged 8 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/737.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix case where old I03 nexus data (pre-2020) would fail to process.
ndevenish marked this conversation as resolved.
Show resolved Hide resolved
50 changes: 38 additions & 12 deletions src/dxtbx/format/FormatNXmxDLS.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from __future__ import annotations

import contextlib
import datetime
import os
from functools import cached_property
from pathlib import Path
from typing import Union

import h5py
import numpy as np
Expand All @@ -13,36 +12,63 @@
from dxtbx.format.FormatNXmx import FormatNXmx


def get_bit_depth_from_meta(meta_file_name):
def get_bit_depth_from_meta(meta_file_name: Path) -> int:
with h5py.File(meta_file_name) as f:
return int(f["/_dectris/bit_depth_image"][()])
with contextlib.suppress(KeyError):
return int(f["/_dectris/bit_depth_image"][()])

# This might be a very old meta file, that only had 'datatype'?
if "datatype" in f:
unique_vals = np.unique(f["datatype"])
ndevenish marked this conversation as resolved.
Show resolved Hide resolved
if not len(unique_vals) == 1:
raise RuntimeError(

Check warning on line 24 in src/dxtbx/format/FormatNXmxDLS.py

View check run for this annotation

Codecov / codecov/patch

src/dxtbx/format/FormatNXmxDLS.py#L24

Added line #L24 was not covered by tests
ndevenish marked this conversation as resolved.
Show resolved Hide resolved
f"Error: Could not determine bit depth from metafile {meta_file_name} (multiple bit depths)"
)
return np.dtype(unique_vals[0]).itemsize * 8

def find_meta_filename(master_like: Union[str, Path]) -> Union[str, Path]:
raise RuntimeError("Cannot determine metafile bit depth")

Check warning on line 29 in src/dxtbx/format/FormatNXmxDLS.py

View check run for this annotation

Codecov / codecov/patch

src/dxtbx/format/FormatNXmxDLS.py#L29

Added line #L29 was not covered by tests


def find_meta_filename(master_like: Path) -> Path:
ndevenish marked this conversation as resolved.
Show resolved Hide resolved
"""
Find the path to the '..._meta.h5' file in the same directory as the master file.
Args:
master_like: File path of the master HDF5 file.

Returns:
File path of the HDF5 metadata '..._meta.h5' file.

Raises:
RuntimeError: If no meta-file was found.
"""

def _local_visit(name):
def _local_visit(name) -> Path | None:
obj = f[name]
if not hasattr(obj, "keys"):
return None
for k in obj.keys():
kclass = obj.get(k, getlink=True, getclass=True)
if kclass is h5py._hl.group.ExternalLink:
kfile = obj.get(k, getlink=True).filename
if kfile.split(".")[0].endswith("meta"):
kfile = master_like.parent / obj.get(k, getlink=True).filename
if kfile.stem.endswith("meta"):
return kfile
return None

master_dir = os.path.split(master_like)[0]
master_like = Path(master_like)
with h5py.File(master_like) as f:
meta_filename = f.visit(_local_visit)

return os.path.join(master_dir, meta_filename)
if meta_filename is None:
# Try a fallback of looking for a file named the same but "meta.h5"
look_for = master_like.with_name(
master_like.stem.removesuffix("_master") + "_meta.h5"
ndevenish marked this conversation as resolved.
Show resolved Hide resolved
)
if look_for.is_file():
meta_filename = look_for
else:
raise RuntimeError(f"Could not find h5 meta file for {master_like}")

return meta_filename


class FormatNXmxDLS(FormatNXmx):
Expand Down Expand Up @@ -76,11 +102,11 @@
# See https://jira.diamond.ac.uk/browse/MXGDA-3674
try:
self._bit_depth_readout = get_bit_depth_from_meta(self._meta)
except Exception:
except RuntimeError:

Check warning on line 105 in src/dxtbx/format/FormatNXmxDLS.py

View check run for this annotation

Codecov / codecov/patch

src/dxtbx/format/FormatNXmxDLS.py#L105

Added line #L105 was not covered by tests
self._bit_depth_readout = 16

@cached_property
def _meta(self):
def _meta(self) -> Path:
return find_meta_filename(self._image_file)

def _get_nxmx(self, fh: h5py.File):
Expand Down
Loading