| import zipfile |
| from pathlib import Path |
| import numpy as np |
|
|
| EXPECTED_OOD_KEYS = { |
| "chaplin1", "chaplin2", |
| "mononoke1", "mononoke2", |
| "passepartout1", "passepartout2", |
| "planetearth1", "planetearth2", |
| "pulpfiction1", "pulpfiction2", |
| "wot1", "wot2", |
| } |
|
|
| EXPECTED_SUBJECTS = {"sub-01", "sub-02", "sub-03", "sub-05"} |
|
|
| def load_fmri_num_samples(datasets_root: Path, test_set_name: str) -> dict[str, dict[str, int]]: |
| """Load per-subject, per-episode fMRI sample counts for the test set.""" |
| if test_set_name == "friends-s7": |
| file_pattern = "friends-s7" |
| elif test_set_name == "ood": |
| file_pattern = "ood" |
| else: |
| raise ValueError(f"Unknown test set: {test_set_name}") |
|
|
| fmri_dir = datasets_root / "algonauts_2025.competitors" / "fmri" |
| sample_paths = sorted(fmri_dir.rglob(f"*_{file_pattern}_fmri_samples.npy")) |
|
|
| if not sample_paths: |
| raise FileNotFoundError( |
| f"No fmri_samples files found for {test_set_name} in {fmri_dir}. " |
| f"Expected pattern: *_{file_pattern}_fmri_samples.npy" |
| ) |
|
|
| fmri_num_samples = {} |
| for path in sample_paths: |
| sub = path.parents[1].name |
| fmri_num_samples[sub] = np.load(path, allow_pickle=True).item() |
|
|
| print(f" Loaded fmri_num_samples for {list(fmri_num_samples.keys())}") |
| return fmri_num_samples |
|
|
| def print_summary(predictions: dict[str, dict[str, np.ndarray]]): |
| """Print a summary of the generated predictions.""" |
| for subject, episodes_dict in predictions.items(): |
| print(f" {subject}: {len(episodes_dict)} episodes") |
| for episode, pred in episodes_dict.items(): |
| print(f" {episode}: {pred.shape} {pred.dtype}") |
|
|
| def save_predictions( |
| predictions: dict[str, dict[str, np.ndarray]], |
| test_set_name: str, |
| out_dir: Path, |
| ): |
| """Save predictions as .npy and .zip files.""" |
| file_name = f"fmri_predictions_{test_set_name.replace('-', '_')}" |
|
|
| npy_path = out_dir / f"{file_name}.npy" |
| np.save(npy_path, predictions) |
| print(f" Saved: {npy_path}") |
|
|
| zip_path = out_dir / f"{file_name}.zip" |
| with zipfile.ZipFile(zip_path, "w") as zipf: |
| zipf.write(npy_path, npy_path.name) |
| print(f" Saved: {zip_path}") |
|
|