1import numpy as np
2import os
3import nibabel as nib
4from typing import cast, Optional, List
6SOURCE_FILE = "fmri/__init__.py"
9def _load_long_description():
10 current_dir = os.path.dirname(os.path.abspath(__file__))
11 md_path = os.path.join(current_dir, "fmri.md")
12 with open(md_path, "r", encoding="utf-8") as f:
13 return f.read()
16LONG_DESCRIPTION = _load_long_description()
18tags = ["real", "fmri", "timeseries", "1d", "integer", "bold", "continuous"]
21def _load_bold_data(*, slice_indices: Optional[List[int]] = None) -> np.ndarray:
22 """Load BOLD fMRI data from OpenNeuro dataset ds005880.
24 Args:
25 slice_indices: Optional list of slice indices to load. If None, loads full volume.
26 Use range(15, 30) for middle 15 slices.
28 Returns:
29 Array of shape (X, Y, Z, T) where X,Y,Z are spatial dimensions and T is time points.
30 Z dimension will be length 1 if a single slice index is provided.
31 """
32 url = "https://s3.amazonaws.com/openneuro.org/ds005880/sub-01/func/sub-01_task-rest_run-01_bold.nii.gz?versionId=0z5_YvqoLC4pXDVUVDt9Y1nrJBRxMqXb"
33 # Download and load the data
34 import requests
35 from pathlib import Path
37 cache_dir = Path(os.path.expanduser("~/.cache/benchcompress/fmri"))
38 cache_dir.mkdir(parents=True, exist_ok=True)
39 local_file = cache_dir / "sub-01_task-rest_run-01_bold.nii.gz"
41 if not local_file.exists():
42 response = requests.get(url)
43 response.raise_for_status()
44 local_file.write_bytes(response.content)
46 img = nib.load(str(local_file)) # type: ignore
47 data = img.get_fdata() # type: ignore
49 # Convert to int16
50 data = data.astype(np.int16)
52 if slice_indices is not None:
53 data = data[:, :, slice_indices, :]
55 # Now we are going to convert to 1d array, and we make sure that the time dimension varies the fastest
56 data = data.ravel()
58 return data
61datasets = [
62 {
63 "name": "fmri-ds005880",
64 "version": "2",
65 "description": "Middle 15 slices from BOLD fMRI recording from ds005880 OpenNeuro dataset.",
66 "create": lambda: _load_bold_data(slice_indices=list(range(15, 30))),
67 "tags": tags,
68 "source_file": SOURCE_FILE,
69 "long_description": LONG_DESCRIPTION,
70 }
71]