Datasets API¶
Dataset[T]¶
cobrabox.dataset.Dataset ¶
Bases: Generic[T]
Immutable, typed collection of Data objects.
Behaves like a read-only sequence: supports indexing, iteration, and len(). All filtering and combination operations return new Dataset instances.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
Iterable[T]
|
Sequence of Data objects (list, tuple, or another Dataset). |
required |
Example
ds = cb.load_dataset("dummy_chain") ds[0] # first item ds.filter(groupID="A") # returns new Dataset ds.groupby("subjectID") # returns dict[str, Dataset] ds.describe() # prints summary
Source code in src/cobrabox/dataset.py
__len__ ¶
__getitem__ ¶
__getitem__(index: slice) -> Dataset[T]
__getitem__(index: int | slice) -> T | Dataset[T]
__iter__ ¶
__contains__ ¶
__add__ ¶
__repr__ ¶
__str__ ¶
Source code in src/cobrabox/dataset.py
describe ¶
filter ¶
filter(
*,
subjectID: str | None = None,
groupID: str | None = None,
condition: str | None = None,
) -> Dataset[T]
Return a new Dataset containing only items matching all given criteria.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
subjectID
|
str | None
|
Keep items where item.subjectID == this value. |
None
|
groupID
|
str | None
|
Keep items where item.groupID == this value. |
None
|
condition
|
str | None
|
Keep items where item.condition == this value. |
None
|
Returns:
| Type | Description |
|---|---|
Dataset[T]
|
New Dataset with matching items. Empty Dataset if none match. |
Example
ds.filter(groupID="control") ds.filter(subjectID="S01", condition="rest")
Source code in src/cobrabox/dataset.py
groupby ¶
groupby(
attr: Literal["subjectID", "groupID", "condition"],
) -> dict[str, Dataset[T]]
Group items by a metadata attribute.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
attr
|
Literal['subjectID', 'groupID', 'condition']
|
One of "subjectID", "groupID", or "condition". |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Dataset[T]]
|
Dict mapping attribute value (as string) to a Dataset of matching items. |
dict[str, Dataset[T]]
|
Items with None for the attribute are grouped under the key "None". |
Raises:
| Type | Description |
|---|---|
ValueError
|
If attr is not one of the valid metadata attributes. |
Example
by_group = ds.groupby("groupID") by_group["control"]
Source code in src/cobrabox/dataset.py
load_dataset()¶
cobrabox.datasets.load_dataset ¶
load_dataset(
identifier: str,
*,
subset: SubsetSpec | None = None,
accept: bool = False,
force: bool = False,
) -> Dataset[SignalData]
Load a dataset by identifier.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
identifier
|
str
|
Dataset name, e.g. |
required |
subset
|
SubsetSpec | None
|
Restrict which data is downloaded and loaded. Two forms:
Example::
|
None
|
accept
|
bool
|
If |
False
|
force
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
Dataset[SignalData]
|
class: |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
LargeLoadError
|
If |
DownloadCancelled
|
If |
Source code in src/cobrabox/datasets.py
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 | |
download_dataset()¶
cobrabox.datasets.download_dataset ¶
download_dataset(
identifier: str,
*,
subset: SubsetSpec | None = None,
accept: bool = False,
force: bool = False,
dry_run: bool = False,
) -> Path
Download a remote dataset without loading it into memory.
Useful for pre-fetching large datasets before analysis, or for downloading to a shared location. Interrupted downloads are resumed automatically.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
identifier
|
str
|
Remote dataset name, e.g. |
required |
subset
|
SubsetSpec | None
|
Restrict which files are downloaded. Two forms:
Example::
|
None
|
accept
|
bool
|
Skip the confirmation prompt. |
False
|
force
|
bool
|
Delete existing local files and re-download from scratch. |
False
|
dry_run
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to the local dataset directory (may not exist if |
Path
|
and nothing has been downloaded yet). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
DownloadCancelled
|
If |
Source code in src/cobrabox/datasets.py
list_datasets()¶
cobrabox.datasets.list_datasets ¶
Return all known dataset identifiers grouped by type.
Example::
cb.list_datasets()
# {
# 'local': ['dummy_chain', 'dummy_noise', ...],
# 'remote': ['bonn_eeg', 'chb_mit', ...],
# }
Returns:
| Type | Description |
|---|---|
dict[str, list[str]]
|
Dict with keys |
dict[str, list[str]]
|
each mapping to a sorted list of identifier strings. |
Source code in src/cobrabox/datasets.py
show_datasets()¶
cobrabox.datasets.show_datasets ¶
Print a summary table of all available datasets and return the data.
Example::
cb.show_datasets()
# Dataset Source Data type Cached Seizures Size Subsets License
# bonn_eeg remote ictal/interictal yes 100 ~10 MB 5 sets ...
# chb_mit remote ictal/interictal 3/24 200 ~30 GB 24 subj ...
# dummy_chain local — — — — — —
Returns:
| Type | Description |
|---|---|
list[dict[str, str | None]]
|
List of dicts with keys |
list[dict[str, str | None]]
|
|
list[dict[str, str | None]]
|
|
list[dict[str, str | None]]
|
|
list[dict[str, str | None]]
|
|
Source code in src/cobrabox/datasets.py
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 | |
dataset_info()¶
cobrabox.datasets.dataset_info ¶
Return metadata for a dataset, including available subset keys.
Example::
info = cb.dataset_info("swiss_eeg_short")
print(info)
# DatasetInfo: swiss_eeg_short
# description : Short-term scalp EEG ...
# subjects (18): ID1, ID2, ID4a, ...
# usage : cb.load_dataset("swiss_eeg_short", subset=["ID1", "ID2"])
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
identifier
|
str
|
Dataset name, e.g. |
required |
Returns:
| Type | Description |
|---|---|
DatasetInfo
|
class: |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the identifier is unknown. |
Source code in src/cobrabox/datasets.py
delete_dataset()¶
cobrabox.datasets.delete_dataset ¶
delete_dataset(
identifier: str,
*,
subset: list[str] | None = None,
confirm: bool = True,
) -> None
Delete locally cached files for a remote dataset.
Useful for freeing disk space after analysis is complete. Use
:func:dataset_info to inspect what subset keys are available, and
:func:show_datasets to see which datasets are currently cached.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
identifier
|
str
|
Remote dataset name, e.g. |
required |
subset
|
list[str] | None
|
If given, only delete files for the listed subset keys (e.g.
|
None
|
confirm
|
bool
|
If |
True
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
DownloadCancelled
|
If |
Example::
# Delete a full dataset
cb.delete_dataset("bonn_eeg")
# Delete specific subjects only
cb.delete_dataset("chb_mit", subset=["chb01", "chb02"])
# Skip confirmation
cb.delete_dataset("bonn_eeg", confirm=False)