Skip to content

Open dataset HDF5 files in read-only mode when loading - #1847

Open
MaxGhenis wants to merge 1 commit into
mainfrom
fix/hdfstore-read-only
Open

Open dataset HDF5 files in read-only mode when loading#1847
MaxGhenis wants to merge 1 commit into
mainfrom
fix/hdfstore-read-only

Conversation

@MaxGhenis

Copy link
Copy Markdown
Collaborator

Problem

UKSingleYearDataset and UKMultiYearDataset open dataset files with pd.HDFStore(file_path). pandas' default mode is append ("a"), which requires write access, so loading any dataset that lives in a read-only location fails before a single row is read:

PermissionError: file `.../enhanced_frs_2023_24.h5` exists but it can not be written

This bites Hugging Face cache snapshots (~/.cache/huggingface/hub/models--policyengine--policyengine-uk-data-private/snapshots/<rev>/enhanced_frs_2023_24.h5), datasets on read-only mounts, and any copy without the write bit. Found on 2026-09-03 while reproducing PolicyEngine/uk-trade-shock-study, where two separate sessions had to patch their venv copy of the loader to proceed.

Reproduction on main:

cp enhanced_frs_2023_24_tiny.h5 ro.h5 && chmod 444 ro.h5
python -c "from policyengine_uk import Microsimulation; Microsimulation(dataset='ro.h5')"

Fix

Pass mode="r" to the two HDFStore calls that only read (the __init__ loaders of both dataset classes). The two save() calls keep their default write mode. Loading never writes, so this changes nothing for writable files.

Tests

policyengine_uk/tests/test_read_only_dataset.py:

  • Two synthetic tests (no network): build single- and multi-year datasets, save them, chmod 444, reload, and assert the contents and the file's SHA-256 are unchanged. Both fail on main and pass with this change. They skip if the environment ignores permission bits (root).
  • One microsimulation-marked test: download enhanced_frs_2023_24_tiny.h5@1.57.2, copy it read-only, build Microsimulation(dataset=path), and compute household_net_income for 2025. Passes locally (1,000 households, all finite, file hash unchanged). Skips without HUGGING_FACE_TOKEN, like the other microsimulation tests.

make format clean.

Changelog

changelog.d/hdfstore-read-only.fixed.md (patch bump).

🤖 Generated with Claude Code

pandas opens HDFStore in append mode by default, so loading a dataset
from a read-only file (a Hugging Face cache snapshot, a read-only mount,
or a chmod 444 copy) failed with PermissionError before any data was
read. Pass mode="r" at both read sites in UKSingleYearDataset and
UKMultiYearDataset; the two save() sites keep write access.

Add regression tests that load synthetic single- and multi-year datasets
from read-only files without network access, plus a microsimulation test
that loads a read-only copy of enhanced_frs_2023_24_tiny.h5 and computes
household_net_income.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
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