Skip to content

Support multi-dataset MTZ files in rs.read_mtz (multiple=True) - #367

Open
kmdalton wants to merge 2 commits into
mainfrom
read-mtz-multiple
Open

kmdalton wants to merge 2 commits into
mainfrom
read-mtz-multiple

Conversation

@kmdalton

Copy link
Copy Markdown
Member

Addresses #336. This is an alternative to #366.

Summary

New: rs.read_mtz(mtzfile, multiple=False)

  • multiple=False (the default) returns a single DataSet with all columns, as before.
  • multiple=True returns an iterator that yields one rs.DataSet per MTZ dataset, in header order. Each one has:
    • the H, K, L index plus the columns belonging to that dataset
    • that dataset's own wavelength
    • the file's global cell and spacegroup
    • its own merged/unmerged detection, using the same M/ISYM rules as before
  • Every dataset is yielded, including a placeholder HKL_base that holds only H/K/L. It comes back as a DataSet with no columns, so the output matches the file's structure.
  • A missing H, K or L column raises ValueError when read_mtz is called, not later during iteration.
for ds in rs.read_mtz("autoproc_staraniso.mtz", multiple=True):
    print(ds.wavelength, ds.columns.to_list())
# 9.999   []
# 1.05965 ['F', 'SIGF', 'IMEAN', ...]

Fix for the case reported in #336

@YehudiB reported that files from XDS → XDSCONV → CAD and from autoPROC/STARANISO put H/K/L in dataset 0 (HKL_base), which has a placeholder wavelength (0.0 or 9.999). The real data and wavelength are in dataset 1. rs.read_mtz used datasets[0].wavelength, so it returned the placeholder and warned about multiple wavelengths.

The default path now takes the wavelength only from datasets that own at least one non-H/K/L column. For these files that gives the correct wavelength with no warning. This avoids hard-coding 0.0 or 9.999 as values to skip, which is what #366 does. Files where real data datasets have different wavelengths (for example MAD) still warn, and the warning now points to multiple=True. If no dataset owns data columns, the old behavior is kept.

Backward compatibility

  • The default signature and return type are unchanged, and columns, index, dtypes and merged status are identical.
  • The only change in the default path is the wavelength for files whose first dataset holds only H/K/L. Those files used to get a placeholder value, so this fixes a bug. Single-dataset files, which covers every MTZ in the test data, behave exactly as before.
  • from_gemmi (also used by DataSet(gemmi.Mtz) and read_cif) keeps its signature.

Tests

New tests in tests/io/test_mtz.py build synthetic multi-dataset MTZs with gemmi that mirror the layout in #336:

  • multiple=False gives the same result as the default call
  • a single-dataset file with multiple=True yields one DataSet identical to the default read
  • HKL_base + data: the default read gets the right wavelength with no warning, and multiple=True yields both datasets with the correct columns and wavelengths
  • a MAD-style file (HKL_base + peak + remote): the default read warns, and each yielded dataset matches its slice of the combined read
  • the return value is a lazy iterator
  • an unmerged multi-dataset file is detected as unmerged
  • a missing L column raises ValueError right away

Full test suite: 22439 passed, 51 skipped, 53 xfailed.

Note: black and isort weren't available locally, so pre-commit.ci may push a formatting commit.

🤖 Generated with Claude Code

kmdalton and others added 2 commits November 20, 2026 14:23
Add a `multiple` flag to rs.read_mtz. When True, return an iterator
yielding one DataSet per MTZ dataset, each with H/K/L, that dataset's
columns, and its own wavelength. The default (multiple=False) still
returns a single DataSet with all columns.

Addresses GH#336: in the default path the wavelength now comes only from
datasets that own data columns, so placeholder HKL_base datasets
(wavelength 0.0 or 9.999 from XDSCONV/CAD or autoPROC/STARANISO) no
longer give the wrong wavelength or a spurious warning.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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

Successfully merging this pull request may close these issues.

1 participant