Skip to content

Keep the phased flag through to_zarr and from_zarr - #441

Merged
andrewkern merged 2 commits into
mainfrom
fix/403-phased-zarr-round-trip
Nov 18, 2026
Merged

andrewkern merged 2 commits into
mainfrom
fix/403-phased-zarr-round-trip

Conversation

@andrewkern

Copy link
Copy Markdown
Member

Fixes #403.

The fault

A HaplotypeMatrix lost its phased flag through to_zarr and from_zarr. A simulation from from_ts (phased) that was saved to a store came back unphased, and the phase-dependent statistics warned on it.

The fix

The issue gave two options for the load rule. This PR uses a third one, which keeps the rule from #401 that a loader does not infer phase from the calls.

  • to_zarr records the matrix's phased flag as a store attribute, pg_gpu_phased, in both store layouts.
  • from_zarr now has phased=None as its default, which takes the recorded flag. This holds on the eager and the streaming path. An explicit True or False still wins.
  • A store with no record, such as one from bio2zarr, loads as unphased, as before. The loader reads no per-call data to decide.
  • A phased matrix also writes call_genotype_phased, the VCZ field for phase. Every call has the same value, so the array has a fill value and no stored chunk (under 600 bytes at any size). An unphased matrix writes no such field: its flag means that phase was not declared, not that each call is known to be unphased.
  • A matrix that carries genotype_phased among its fields keeps those per-call values in the store.
  • allel_zarr_to_vcz carries the record to the new store.

GenotypeMatrix.to_zarr writes no record.

One effect to know

A store written by this version loads as phased where it loaded as unphased before. Under estimator='auto', the LD statistics then take the haplotype estimator, so values from a saved store can differ from those of an earlier load with the old default. They now agree with the values from the matrix before it was saved.

Tests

tests/test_phasing.py::TestZarrKeepsPhase: the round trip for both flag values (VCZ eager, VCZ streaming, scikit-allel); a saved simulation gives no UnphasedDataWarning; the phase field for a phased store and its absence for an unphased one; a genotype_phased field among the matrix's fields; a store with the record removed; a genotype store; the layout conversion.

Full GPU suite: 3389 passed. Lint and the pipeline-map check pass.

Docs

phasing.rst, quickstart.rst, the HaplotypeMatrix class docstring, the changelog entry for the phased flag (which is itself unreleased, so there is no separate bug-fix entry), and the map note for from_zarr.

to_zarr records the matrix's phased flag as a store attribute, and
from_zarr (phased=None by default) gives it back, on the eager and the
streaming path and for both store layouts. A phased matrix also writes
call_genotype_phased, the VCZ field for phase. A store with no record,
such as one from bio2zarr, loads as unphased as before: the loader does
not infer phase from the calls. allel_zarr_to_vcz carries the record.

Fixes #403

@nspope nspope left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me

Comment thread pg_gpu/haplotype_matrix.py Outdated
)

if phased is None:
phased = stored_phase(path)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm ... this now opens the zarr store twice, right? (That is, _decide_streaming_mode below on line 844 will also read the store). I guess that doesn't really matter because it's cheap locally. If we ever supported remote access, it might become an issue. If so, I think we could likely determine phase via _decide_streaming_mode

from_zarr opened the store one more time to read the phase record. The
streaming builder now reads it from its source's handle and the eager
builder from the handle of its population step, so a load opens the
store no extra time.
@andrewkern
andrewkern merged commit 24e0419 into main Nov 18, 2026
2 checks passed
@andrewkern
andrewkern deleted the fix/403-phased-zarr-round-trip branch November 18, 2026 05:43
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.

to_zarr then from_zarr drops the phased flag, so a saved simulation comes back unphased

2 participants