|
| 1 | +import numpy as np |
| 2 | +import h5py |
| 3 | +import pytest |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | + |
| 7 | +@pytest.fixture(scope="session", autouse=True) |
| 8 | +def generate_simu_hdf5(): |
| 9 | + """Generate a minimal simu.hdf5 if it doesn't exist (CI-friendly). |
| 10 | +
|
| 11 | + Tests that call easyInitialize("example:simulation_cpm") resolve to |
| 12 | + example_data/simu.hdf5. This fixture ensures that file exists before |
| 13 | + any test runs, so no real dataset needs to be committed to the repo. |
| 14 | + If the file already exists (e.g. locally), it is left untouched. |
| 15 | + """ |
| 16 | + example_data_dir = Path(__file__).parent.parent / "example_data" |
| 17 | + example_data_dir.mkdir(exist_ok=True) |
| 18 | + hdf5_path = example_data_dir / "simu.hdf5" |
| 19 | + |
| 20 | + if hdf5_path.exists(): |
| 21 | + yield hdf5_path |
| 22 | + return |
| 23 | + |
| 24 | + rng = np.random.default_rng(42) |
| 25 | + Nd, N_frames = 64, 20 |
| 26 | + |
| 27 | + ptychogram = rng.random((N_frames, Nd, Nd)).astype(np.float32) |
| 28 | + encoder = rng.uniform(-1e-3, 1e-3, (N_frames, 2)) |
| 29 | + |
| 30 | + with h5py.File(hdf5_path, "w") as hf: |
| 31 | + hf.create_dataset("ptychogram", data=ptychogram, dtype="f") |
| 32 | + hf.create_dataset("encoder", data=encoder, dtype="f") |
| 33 | + hf.create_dataset("dxd", data=np.array(75e-6)) |
| 34 | + hf.create_dataset("zo", data=np.array(0.1)) |
| 35 | + hf.create_dataset("wavelength", data=np.array(632e-9)) |
| 36 | + hf.create_dataset("entrancePupilDiameter", data=np.array(170e-6)) |
| 37 | + |
| 38 | + yield hdf5_path |
0 commit comments