|
| 1 | +"""PyBIDS 1.0 API specification""" |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | +from typing import Any, Dict, List, Optional, Protocol, TypeVar, Union |
| 5 | + |
| 6 | +from .utils import PaddedInt |
| 7 | + |
| 8 | +try: |
| 9 | + from typing import TypeAlias |
| 10 | +except ImportError: |
| 11 | + from typing_extensions import TypeAlias |
| 12 | + |
| 13 | + |
| 14 | +# Datasets should be parameterizable on some kind of schema object. |
| 15 | +# External API users should not depend on it, so this is bound to Any, |
| 16 | +# but once a Schema type is defined for an API implementation, type checkers |
| 17 | +# should be able to introspect it. |
| 18 | +SchemaT = TypeVar("SchemaT") |
| 19 | + |
| 20 | + |
| 21 | +Index: TypeAlias = PaddedInt |
| 22 | +Label: TypeAlias = str |
| 23 | + |
| 24 | + |
| 25 | +class File(Protocol[SchemaT]): |
| 26 | + """Generic file holder |
| 27 | +
|
| 28 | + This serves as a base class for :class:`BIDSFile` and can represent |
| 29 | + non-BIDS files. |
| 30 | + """ |
| 31 | + |
| 32 | + path: Path |
| 33 | + relative_path: Path |
| 34 | + dataset: Optional["BIDSDataset[SchemaT]"] |
| 35 | + |
| 36 | + def __fspath__(self) -> str: |
| 37 | + ... |
| 38 | + |
| 39 | + |
| 40 | +class BIDSFile(File[SchemaT], Protocol): |
| 41 | + """BIDS file |
| 42 | +
|
| 43 | + This provides access to BIDS concepts such as path components |
| 44 | + and sidecar metadata. |
| 45 | +
|
| 46 | + BIDS paths take the form:: |
| 47 | +
|
| 48 | + [sub-<label>/[ses-<label>/]<datatype>/]<entities>_<suffix><extension> |
| 49 | + """ |
| 50 | + |
| 51 | + entities: Dict[str, Union[Label, Index]] |
| 52 | + datatype: Optional[str] |
| 53 | + suffix: Optional[str] |
| 54 | + extension: Optional[str] |
| 55 | + |
| 56 | + metadata: Dict[str, Any] |
| 57 | + """Sidecar metadata aggregated according to inheritance principle""" |
| 58 | + |
| 59 | + |
| 60 | +class BIDSDataset(Protocol[SchemaT]): |
| 61 | + """Interface to a single BIDS dataset. |
| 62 | +
|
| 63 | + This structure does not consider the contents of sub-datasets |
| 64 | + such as `sourcedata/` or `derivatives/`. |
| 65 | + """ |
| 66 | + root: Path |
| 67 | + schema: SchemaT |
| 68 | + |
| 69 | + dataset_description: Dict[str, Any] |
| 70 | + """Contents of dataset_description.json""" |
| 71 | + |
| 72 | + ignored: List[File[SchemaT]] |
| 73 | + """Invalid files found in dataset""" |
| 74 | + |
| 75 | + files: List[BIDSFile[SchemaT]] |
| 76 | + """Valid files found in dataset""" |
| 77 | + |
| 78 | + datatypes: List[str] |
| 79 | + """Datatype directories found in dataset""" |
| 80 | + |
| 81 | + modalities: List[str] |
| 82 | + """BIDS "modalities" found in dataset""" |
| 83 | + |
| 84 | + subjects: List[str] |
| 85 | + """Subject/participant identifiers found in the dataset""" |
| 86 | + |
| 87 | + entities: List[str] |
| 88 | + """Entities (long names) found in any filename in the dataset""" |
| 89 | + |
| 90 | + def get(self, **filters) -> List[BIDSFile[SchemaT]]: |
| 91 | + """Query dataset for files""" |
| 92 | + |
| 93 | + def get_entities(self, entity: str, **filters) -> List[Label | Index]: |
| 94 | + """Query dataset for entity values""" |
| 95 | + |
| 96 | + def get_metadata(self, term: str, **filters) -> List[Any]: |
| 97 | + """Query dataset for metadata values""" |
| 98 | + |
| 99 | + |
| 100 | +class DatasetCollection(BIDSDataset[SchemaT], Protocol): |
| 101 | + """Interface to a collection of BIDS dataset. |
| 102 | +
|
| 103 | + This structure allows the user to construct a single view of |
| 104 | + multiple datasets, such as including source or derivative datasets. |
| 105 | + """ |
| 106 | + primary: BIDSDataset[SchemaT] |
| 107 | + datasets: List[BIDSDataset[SchemaT]] |
| 108 | + |
| 109 | + def add_dataset(self, dataset: BIDSDataset[SchemaT]) -> None: |
| 110 | + ... |
0 commit comments