Data API¶
Data (General Container)¶
cobrabox.data.Data ¶
Data(
data: DataArray,
sampling_rate: float | None = None,
subjectID: str | None = None,
groupID: str | None = None,
condition: str | None = None,
history: list[str] | None = None,
extra: dict[str, Any] | None = None,
)
Container for labelled multidimensional data.
Immutable container wrapping xarray.DataArray with metadata attributes. This is the most general data container with no assumptions about dimensions. For time-series data, use SignalData subclass instead.
Optional dimensions (examples): - time: Temporal dimension - space: Spatial dimension (electrode/voxel) - spaceX, spaceY, spaceZ: Additional spatial dimensions - run_index: Run/block index - window_index: Window index (e.g., from sliding window) - band_index: Frequency band index
Data type handling
- Integer and float inputs are cast to float64
- Complex inputs (e.g., from Hilbert transform) are preserved as complex128
Note
This class is immutable. To create modified versions, use methods that return new Data instances (e.g., features create new Data objects).
Initialize Data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataArray
|
xarray DataArray with arbitrary dimensions |
required |
sampling_rate
|
float | None
|
Sampling rate in Hz (only meaningful for time-series data) |
None
|
subjectID
|
str | None
|
Subject identifier |
None
|
groupID
|
str | None
|
Group identifier |
None
|
condition
|
str | None
|
Experimental condition |
None
|
history
|
list[str] | None
|
List of operation names applied (default: empty list) |
None
|
extra
|
dict[str, Any] | None
|
Optional dict for additional fields and arrays (e.g. xr.DataArray, scalars) |
None
|
Source code in src/cobrabox/data.py
dims
property
¶
Dimension names, in the order they appear in .shape.
Shortcut for .data.dims. See GH #119.
Example
import numpy as np import cobrabox as cb d = cb.Data.from_numpy(np.zeros((4, 200)), dims=["space", "time"]) d.dims ('space', 'time')
extra
property
¶
User-defined additional fields and arrays.
Returns a copy of the extra dict. To add fields, create a new Data object with updated extra dict.
numpy
property
¶
Access underlying numpy array directly, without copying.
Equivalent to .xarr.data / .data.data, provided as a transparent
shortcut. See GH #112.
sampling_rate
property
¶
Sampling rate in Hz.
Returns None if data has no time dimension or if sampling rate was not provided and could not be inferred.
shape
property
¶
Length of each dimension, in dimension order, as numpy has it.
Shortcut for .data.shape, so you don't need to reach into xarray for
the most common question asked of a container. Pair with .dims to
learn which name goes with which length, or use .sizes to get both
at once. See GH #119.
Example
import numpy as np import cobrabox as cb d = cb.Data.from_numpy(np.zeros((4, 200)), dims=["space", "time"]) d.shape (4, 200)
size
property
¶
Total number of elements, as numpy has it.
Shortcut for .data.size. Note this is the element count — for the
per-dimension lengths use .shape or .sizes. See GH #119.
Example
import numpy as np import cobrabox as cb d = cb.Data.from_numpy(np.zeros((4, 200)), dims=["space", "time"]) d.size 800
sizes
property
¶
Mapping of dimension name to its length.
Shortcut for dict(.data.sizes) — the xarray view of the shape, where
each length is labelled. Returns a plain dict, so mutating it has no
effect on this (immutable) container. See GH #119.
Example
import numpy as np import cobrabox as cb d = cb.Data.from_numpy(np.zeros((4, 200)), dims=["space", "time"]) d.sizes {'space': 4, 'time': 200}
xarr
property
¶
Access underlying xarray DataArray (alias for .data).
Prefer this over .data when the reader may not know whether they're
holding a Data object or an xarray.DataArray — .data.data for the
underlying numpy array reads as a typo, while .xarr.data (or .numpy)
makes the intent explicit. See GH #112.
__deepcopy__ ¶
__deepcopy__(memo: dict[int, Any]) -> Data
Deep copy — produces a new instance with independent copies of all data.
__setattr__ ¶
Prevent modification of attributes after initialization.
Source code in src/cobrabox/data.py
from_numpy
classmethod
¶
from_numpy(
arr: ndarray,
dims: list[Hashable],
*,
sampling_rate: float | None = None,
subjectID: str | None = None,
groupID: str | None = None,
condition: str | None = None,
extra: dict[str, Any] | None = None,
) -> Data
Create a Data object from a numpy array.
Creates a general Data container. For time-series data, use SignalData.from_numpy() which provides additional validation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arr
|
ndarray
|
Numpy array with arbitrary shape |
required |
dims
|
list[Hashable]
|
Dimension names matching array shape |
required |
sampling_rate
|
float | None
|
Sampling rate in Hz (only used if 'time' in dims) |
None
|
subjectID
|
str | None
|
Subject identifier |
None
|
groupID
|
str | None
|
Group identifier |
None
|
condition
|
str | None
|
Experimental condition |
None
|
extra
|
dict[str, Any] | None
|
Optional extra dict |
None
|
Returns:
| Type | Description |
|---|---|
Data
|
Data instance |
Raises:
| Type | Description |
|---|---|
ValueError
|
If dims length doesn't match array ndim |
Source code in src/cobrabox/data.py
from_xarray
classmethod
¶
from_xarray(
ar: DataArray,
*,
sampling_rate: float | None = None,
subjectID: str | None = None,
groupID: str | None = None,
condition: str | None = None,
history: list[str] | None = None,
extra: dict[str, Any] | None = None,
) -> Data
Create a Data object from an xarray DataArray.
Creates a general Data container with arbitrary dimensions. For time-series data with validation, use SignalData.from_xarray().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ar
|
DataArray
|
xarray DataArray with dims (time, space) and optional coords. |
required |
sampling_rate
|
float | None
|
Sampling rate in Hz. If not provided, inferred from time coordinates when they represent time in seconds. |
None
|
subjectID
|
str | None
|
Subject identifier. |
None
|
groupID
|
str | None
|
Group identifier. |
None
|
condition
|
str | None
|
Experimental condition. |
None
|
history
|
list[str] | None
|
List of operation names applied. |
None
|
extra
|
dict[str, Any] | None
|
Optional extra dict. |
None
|
Returns:
| Type | Description |
|---|---|
Data
|
Data instance |
Example
ar = xr.DataArray(...) ds = cb.from_xarray(ar)
Source code in src/cobrabox/data.py
to_numpy ¶
Convert data to numpy arrays.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
style
|
str
|
Output style. |
'default'
|
Returns:
| Type | Description |
|---|---|
ndarray | tuple[ndarray, ndarray, ndarray]
|
Either the raw data array, or |
Raises:
| Type | Description |
|---|---|
ValueError
|
If style is unknown or if "gorkastyle" is used without required dimensions. |
Source code in src/cobrabox/data.py
to_pandas ¶
Convert to pandas DataFrame.
Returns:
| Type | Description |
|---|---|
DataFrame
|
pandas DataFrame with MultiIndex from dimensions |
SignalData (Time-Series Container)¶
cobrabox.data.SignalData ¶
SignalData(
data: DataArray,
sampling_rate: float | None = None,
subjectID: str | None = None,
groupID: str | None = None,
condition: str | None = None,
history: list[str] | None = None,
extra: dict[str, Any] | None = None,
)
Bases: Data
Container for time-series data with mandatory time dimension.
SignalData extends Data with the requirement that data must have a 'time' dimension. This is appropriate for EEG, fMRI, and other time-series data where temporal ordering matters.
Mandatory dimensions
- time: Temporal dimension
Optional dimensions (examples): - space: Spatial dimension (electrode/voxel) - spaceX, spaceY, spaceZ: Additional spatial dimensions - run_index: Run/block index - window_index: Window index (e.g., from sliding window) - band_index: Frequency band index
Note
This class is immutable. To create modified versions, use methods that return new Data instances (e.g., features create new Data objects).
Initialize SignalData.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataArray
|
xarray DataArray with at least 'time' dimension |
required |
sampling_rate
|
float | None
|
Sampling rate in Hz. If not provided, inferred from time coordinates when they represent time in seconds. |
None
|
subjectID
|
str | None
|
Subject identifier |
None
|
groupID
|
str | None
|
Group identifier |
None
|
condition
|
str | None
|
Experimental condition |
None
|
history
|
list[str] | None
|
List of operation names applied (default: empty list) |
None
|
extra
|
dict[str, Any] | None
|
Optional dict for additional fields and arrays |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If data does not have 'time' dimension |
Source code in src/cobrabox/data.py
from_numpy
classmethod
¶
from_numpy(
arr: ndarray,
dims: list[Hashable],
*,
sampling_rate: float | None = None,
subjectID: str | None = None,
groupID: str | None = None,
condition: str | None = None,
extra: dict[str, Any] | None = None,
) -> SignalData
Create a SignalData object from a numpy array.
Requires at least 1 dimension and that resulting dims include 'time'.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arr
|
ndarray
|
Numpy array |
required |
dims
|
list[Hashable]
|
Dimension names matching array shape, must include 'time' |
required |
sampling_rate
|
float | None
|
Sampling rate in Hz |
None
|
subjectID
|
str | None
|
Subject identifier |
None
|
groupID
|
str | None
|
Group identifier |
None
|
condition
|
str | None
|
Experimental condition |
None
|
extra
|
dict[str, Any] | None
|
Optional extra dict |
None
|
Returns:
| Type | Description |
|---|---|
SignalData
|
SignalData instance |
Raises:
| Type | Description |
|---|---|
ValueError
|
If dims doesn't include 'time' or dims length mismatch |
Source code in src/cobrabox/data.py
from_xarray
classmethod
¶
from_xarray(
ar: DataArray,
*,
sampling_rate: float | None = None,
subjectID: str | None = None,
groupID: str | None = None,
condition: str | None = None,
history: list[str] | None = None,
extra: dict[str, Any] | None = None,
) -> SignalData
Create a SignalData object from an xarray DataArray.
The DataArray must have 'time' dimension. Time coordinates should be in seconds if you want sampling_rate to be inferred.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ar
|
DataArray
|
xarray DataArray with 'time' dimension |
required |
sampling_rate
|
float | None
|
Sampling rate in Hz. If not provided, inferred from time coordinates when they represent time in seconds. |
None
|
subjectID
|
str | None
|
Subject identifier |
None
|
groupID
|
str | None
|
Group identifier |
None
|
condition
|
str | None
|
Experimental condition |
None
|
history
|
list[str] | None
|
List of operation names applied |
None
|
extra
|
dict[str, Any] | None
|
Optional extra dict |
None
|
Returns:
| Type | Description |
|---|---|
SignalData
|
SignalData instance |
Raises:
| Type | Description |
|---|---|
ValueError
|
If DataArray doesn't have 'time' dimension |
Example
ar = xr.DataArray(...) ds = SignalData.from_xarray(ar)
Source code in src/cobrabox/data.py
EEG (Type Marker)¶
cobrabox.data.EEG ¶
EEG(
data: DataArray,
sampling_rate: float | None = None,
subjectID: str | None = None,
groupID: str | None = None,
condition: str | None = None,
history: list[str] | None = None,
extra: dict[str, Any] | None = None,
ref_channel: str | None = None,
)
Bases: SignalData
EEG data container.
EEG is a time-series signal data type with mandatory time dimension. Typically also includes 'space' dimension representing electrodes/channels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ref_channel
|
str | None
|
The reference used for this recording. One of:
|
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/cobrabox/data.py
FMRI (Type Marker)¶
cobrabox.data.FMRI ¶
FMRI(
data: DataArray,
sampling_rate: float | None = None,
subjectID: str | None = None,
groupID: str | None = None,
condition: str | None = None,
history: list[str] | None = None,
extra: dict[str, Any] | None = None,
)
Bases: SignalData
fMRI data container.
FMRI is a time-series signal data type with mandatory time dimension. Typically also includes spatial dimensions representing voxels.