Core API¶
Core APIs define the data container and transform contracts used throughout XDFlow.
For extension-oriented examples, see Writing Custom Transforms and Writing Custom Cross-Validators.
Top-Level Exports¶
The root package exposes the main workflow primitives:
from xdflow import DataContainer, Transform, Predictor, Pipeline, CrossValidator
Tuner is conditionally exported when the tuning extra is installed.
Data Containers¶
DataContainer ¶
DataContainer(data: DataArray, required_coords: list[str] | None = None)
Thin framework wrapper around an xarray.DataArray.
XDFlow's data model is xarray. DataContainer is not a parallel array
abstraction; it is the object passed between transforms, predictors, and
cross-validation utilities so the framework has a consistent boundary. The
wrapped xarray.DataArray remains the source of truth for values,
dimensions, coordinates, and attrs.
The wrapper initializes the data_history attribute used to track pipeline
operations and rewraps common xarray operations so chained calls stay inside
the XDFlow transform contract.
Most xarray methods can be called directly on the container. Methods that
return a new xarray.DataArray are rewrapped as a new DataContainer, so
calls such as container.sel(...) or container.mean(...) remain inside
the XDFlow container contract.
The wrapped array is shallow-copied on construction. Transforms should still treat containers as immutable and return new containers instead of mutating their inputs.
Initialize a container from an xarray data array.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataArray
|
Array with labeled dimensions and coordinates. |
required |
required_coords
|
list[str] | None
|
Optional coordinate names to check for. Missing coordinates emit warnings rather than raising, which lets callers decide how strict to be for a given pipeline. |
None
|
Notes
The constructor ensures data.attrs["data_history"] exists. It does
not validate dimension names or coordinate schemas beyond
required_coords.
Source code in xdflow/core/data_container.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | |
data
property
¶
data: DataArray
Public accessor for the wrapped DataArray. Used in order to ensure immutability.
time_units
property
¶
time_units: str | None
Return declared time units for the time coordinate if present.
Returns:
| Type | Description |
|---|---|
str | None
|
The value of |
__getstate__ ¶
__getstate__()
Return the state to be pickled.
Source code in xdflow/core/data_container.py
67 68 69 | |
__setstate__ ¶
__setstate__(state)
Restore the state from the unpickled state.
Source code in xdflow/core/data_container.py
71 72 73 | |
__getitem__ ¶
__getitem__(key)
Enable slice indexing on DataContainer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Index, slice, or tuple of indices/slices |
required |
Returns:
| Name | Type | Description |
|---|---|---|
DataContainer |
New DataContainer with indexed data |
Source code in xdflow/core/data_container.py
91 92 93 94 95 96 97 98 99 100 101 102 103 104 | |
__getattr__ ¶
__getattr__(name: str)
Delegate attribute access to the underlying xarray.DataArray.
If the attribute is a method that returns a new DataArray, it is wrapped to return a new DataContainer instance. This preserves the wrapper's validation and immutability for chained operations.
Source code in xdflow/core/data_container.py
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | |
TransformError ¶
Bases: Exception
Error raised when a transform or pipeline step fails.
Transform Contracts¶
Transform ¶
Transform(sel: dict[str, Any] | None = None, drop_sel: dict[str, Any] | None = None, transform_sel: dict | None = None, transform_drop_sel: dict | None = None, **kwargs)
Bases: ABC
Base class for XDFlow processing steps.
A transform accepts a DataContainer and returns a new DataContainer.
Concrete subclasses implement _transform; stateful subclasses also
implement _fit. The public fit, transform, and fit_transform methods
provide common selection handling, optional timing output, history logging,
and the stateless/stateful execution contract used by Pipeline and
CrossValidator.
Implementations should prefer named dimensions over positional axes. For
example, use data.mean(dim="time") instead of assuming the time axis is at
a fixed integer position. Transforms should not mutate their input container;
return a new container or an immutable view consistent with xarray behavior.
Class attributes
is_stateful: Whether the transform learns state from fit.
input_dims: Required input dimensions. An empty tuple means the transform
accepts dynamic input dimensions.
output_dims: Output dimensions when known statically. An empty tuple
means subclasses must infer them with get_expected_output_dims.
Initialize common transform selection options.
sel and drop_sel subset the whole input before the transform runs, so
the output contains only the selected data. transform_sel and
transform_drop_sel select only the portion to fit or transform, then
write that transformed portion back into the original array. Partial
write-back is only allowed for transforms that preserve dims, sizes, and
coordinates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sel
|
dict[str, Any] | None
|
Label selection passed to xarray |
None
|
drop_sel
|
dict[str, Any] | None
|
Label selection passed to xarray |
None
|
transform_sel
|
dict | None
|
Label selection used only for the transformed portion. |
None
|
transform_drop_sel
|
dict | None
|
Labels to exclude from the transformed portion. |
None
|
Source code in xdflow/core/base.py
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | |
supports_transform_sel
property
¶
supports_transform_sel: bool
Whether this transform supports transform_sel semantics.
Defaults to the class attribute _supports_transform_sel but allows subclasses
to compute support dynamically via an override.
get_expected_output_dims ¶
get_expected_output_dims(input_dims: tuple[str, ...]) -> tuple[str, ...]
Determines expected output dims based on manually inputed input_dims
Source code in xdflow/core/base.py
192 193 194 195 196 197 | |
transform ¶
transform(container: DataContainer, **kwargs) -> DataContainer
Applies the transformation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
container
|
DataContainer
|
DataContainer to transform |
required |
**kwargs
|
Additional context/parameters passed through the pipeline |
{}
|
Returns:
| Type | Description |
|---|---|
DataContainer
|
New DataContainer with transformation applied |
Source code in xdflow/core/base.py
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | |
fit ¶
fit(container: DataContainer, **kwargs) -> Transform
Fits the transform to the data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
container
|
DataContainer
|
DataContainer to fit on |
required |
**kwargs
|
Additional context/parameters passed through the pipeline |
{}
|
Returns:
| Type | Description |
|---|---|
Transform
|
Self (fitted transform) |
Source code in xdflow/core/base.py
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 | |
fit_transform ¶
fit_transform(container: DataContainer, **kwargs) -> DataContainer
Fit then transform in a single pass. Note that predictors have their own fit_transform.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
container
|
DataContainer
|
DataContainer to fit and transform |
required |
**kwargs
|
Additional context/parameters passed through the pipeline |
{}
|
Returns:
| Type | Description |
|---|---|
DataContainer
|
DataContainer with the fit and transform applied |
Source code in xdflow/core/base.py
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 | |
get_params ¶
get_params(deep: bool = True) -> dict[str, Any]
Get parameters for this transform.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
deep
|
bool
|
If True, will return the parameters for this transform and contained sub-objects that are themselves transforms. |
True
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
dict[str, Any]: Parameter names mapped to their values. |
Source code in xdflow/core/base.py
374 375 376 377 378 379 380 381 382 383 384 385 386 | |
clone ¶
clone() -> Self
Return a fresh instance with the same constructor parameters.
Subclasses that need to preserve constructor kwargs not surfaced by
get_params should override _get_clone_kwargs() instead of
overriding this method.
Source code in xdflow/core/base.py
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 | |
set_params ¶
set_params(**params: Any) -> Transform
Set the parameters of this transform.
Supports nested parameter setting for dict/object attributes using '__' delimiter. For example, 'weight_map__stim_A' will set the "stim_A" key in the weight_map dict. Keys are type-inferred from existing dict keys when possible (e.g., "False" -> False).
Returns:
| Name | Type | Description |
|---|---|---|
self |
Transform
|
The transform instance. |
Source code in xdflow/core/base.py
420 421 422 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 | |
Predictor ¶
Predictor(sample_dim: str, target_coord: str | list[str], is_classifier: bool, encoder: LabelEncoder | None = None, proba: bool = False, is_multilabel: bool = False, sel: dict | None = None, drop_sel: dict | None = None, transform_sel: dict | None = None, transform_drop_sel: dict | None = None, calibrated_thresholds: ndarray | list[float] | None = None, **kwargs)
Bases: Transform, ABC
Base class for transforms that learn targets and produce predictions.
Predictors are stateful transforms. During fitting, single-label classifier
targets are encoded with a LabelEncoder; regressors and multilabel
classifiers use their target coordinates directly. Subclasses implement the
estimator-specific _predict method and optionally _predict_proba.
Public prediction methods return DataContainer objects whose sample
coordinate is preserved from sample_dim. Classifier outputs are decoded
back to original labels when possible, while probability outputs are aligned
to the fitted global class order.
Initialize common prediction metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sample_dim
|
str
|
Dimension whose entries are independent samples. |
required |
target_coord
|
str | list[str]
|
Target coordinate name, list of target coordinate names, or a pattern resolved by subclasses during fit. |
required |
is_classifier
|
bool
|
Whether predictions are categorical labels instead of continuous values. |
required |
encoder
|
LabelEncoder | None
|
Optional label encoder for single-label classifiers. If omitted, a new encoder is created for classifier predictors. |
None
|
proba
|
bool
|
Whether |
False
|
is_multilabel
|
bool
|
Whether classification targets are multiple binary
target coordinates. Multilabel classifiers do not use a
|
False
|
sel
|
dict | None
|
Label selection applied before fitting or transforming. |
None
|
drop_sel
|
dict | None
|
Label selection dropped before fitting or transforming. |
None
|
transform_sel
|
dict | None
|
Label selection used only for partial transformation. |
None
|
transform_drop_sel
|
dict | None
|
Labels excluded from partial transformation. |
None
|
calibrated_thresholds
|
ndarray | list[float] | None
|
Optional multilabel decision thresholds, one per output. |
None
|
Source code in xdflow/core/base.py
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 | |
is_regressor
property
¶
is_regressor: bool
Whether this is a regression task (inverse of is_classifier).
get_labels ¶
get_labels() -> list[Any]
Return the learned label ordering for classifiers.
Requires the predictor to be configured as a classifier with a fitted encoder.
Source code in xdflow/core/base.py
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 | |
set_encoder ¶
set_encoder(encoder: LabelEncoder)
Sets the encoder for the predictor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
encoder
|
LabelEncoder
|
The encoder to set |
required |
Source code in xdflow/core/base.py
631 632 633 634 635 636 637 638 639 640 641 642 643 644 | |
fit_and_set_encoder ¶
fit_and_set_encoder(data: DataArray) -> None
Fits the encoder and sets it for the predictor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataArray
|
The data to fit the encoder on |
required |
Source code in xdflow/core/base.py
646 647 648 649 650 651 652 653 654 655 656 | |
fit ¶
fit(container: DataContainer, **kwargs) -> Transform
Fits the transform to the data. Handles encoding of the target coordinate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
container
|
DataContainer
|
DataContainer to fit on |
required |
**kwargs
|
Additional context/parameters passed through the pipeline |
{}
|
Returns:
| Type | Description |
|---|---|
Transform
|
Self (fitted transform) |
Source code in xdflow/core/base.py
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 | |
fit_transform ¶
fit_transform(container: DataContainer, **kwargs) -> DataContainer
Predictor-specific fit/transform to avoid double selection and ensure encoded y during fit.
Applies selection once, fits on an encoded view (for classifiers), then transforms
the unencoded selected view directly via the protected _transform path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
container
|
DataContainer
|
DataContainer to fit and transform |
required |
**kwargs
|
Additional context/parameters passed through the pipeline |
{}
|
Returns:
| Type | Description |
|---|---|
DataContainer
|
DataContainer with the fit and transform applied |
Source code in xdflow/core/base.py
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 | |
predict ¶
predict(container: DataContainer, **kwargs) -> DataContainer
Predicts labels, handling data selection and output structuring.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
container
|
DataContainer
|
DataContainer to predict on |
required |
**kwargs
|
Additional context/parameters passed through the pipeline |
{}
|
Returns:
| Type | Description |
|---|---|
DataContainer
|
DataContainer with predictions, data is 1D with shape (n_trials,) |
Source code in xdflow/core/base.py
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 | |
predict_proba ¶
predict_proba(container: DataContainer, **kwargs) -> DataContainer
Predicts probabilities, handling data selection and output structuring. DataContainer has data with shape (sample_dim, class) (e.g. n_trials, n_stimuli)
Source code in xdflow/core/base.py
1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 | |
SampleWeightMixin ¶
Mixin providing generic coordinate-to-array extraction for sample weights.
This mixin decouples weight reading/alignment (generic across frameworks) from signature inspection and kwargs building (framework-specific).
Any transform that wants to support sample weights can inherit this mixin to gain:
- sample_weight_coord attribute for specifying the weight coordinate name
- _extract_sample_weights() method for reading and aligning weights from a DataArray
The transform is then responsible for: - Checking if its underlying estimator/model supports sample weights - Passing the weights to the appropriate fit/train method
Example
class MyPredictor(Transform, SampleWeightMixin): def init(self, sample_weight_coord=None, kwargs): super().init(kwargs) self.sample_weight_coord = sample_weight_coord
def _fit(self, container: DataContainer, **kwargs):
X, sample_index = self._prepare_data(container.data)
weights = self._extract_sample_weights(container.data, sample_index)
if weights is not None:
self.model.fit(X, sample_weight=weights)
else:
self.model.fit(X)