laplax.util.loader
Utilities for handling DataLoaders/Iterables instead of single batches.
DataLoaderMV ¶
Source code in laplax/util/loader.py
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 340 341 342 343 344 345 346 347 348 349 350 351 352 |
|
__init__ ¶
__init__(mv: Callable, loader: Iterable, transform: Callable = input_target_split, reduce: Callable = reduce_online_mean, *, verbose_logging: bool = False, **kwargs: Kwargs) -> None
Initialize the DataLoaderMV object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mv
|
Callable
|
A callable that processes a single batch of data. |
required |
loader
|
Iterable
|
An iterable yielding batches of data. |
required |
transform
|
Callable
|
A callable to transform each batch into the desired format
(default: |
input_target_split
|
reduce
|
Callable
|
A callable to reduce results across batches
(default: |
reduce_online_mean
|
verbose_logging
|
bool
|
Whether to log progress using tqdm (default: False). |
False
|
**kwargs
|
Kwargs
|
Additional keyword arguments (currently unused). |
{}
|
Source code in laplax/util/loader.py
__call__ ¶
Process the input vector using the data loader and return the result.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
vec
|
Array
|
The input vector to process. |
required |
Returns:
Type | Description |
---|---|
Array | PyTree
|
The processed result as an Array or PyTree. |
Source code in laplax/util/loader.py
lower_func ¶
Apply a function to the data loader and return the result.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func
|
Callable
|
A callable to apply to the data loader. |
required |
**kwargs
|
Kwargs
|
Additional keyword arguments for the function. |
{}
|
Returns:
Type | Description |
---|---|
Array
|
The result of applying the function to the data loader. |
Source code in laplax/util/loader.py
input_target_split ¶
Split a batch into input and target components.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
batch
|
tuple[Array, Array]
|
A tuple where the first element is the input data and the second element is the target data. |
required |
Returns:
Type | Description |
---|---|
Data
|
A dictionary containing:
|
Source code in laplax/util/loader.py
reduce_sum ¶
reduce_sum(res_new: Any, state: Any | None = None, *, keepdims: bool = True, axis: int = 0) -> tuple[Any, Any]
Perform a reduction by summing results across a specified axis.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
res_new
|
Any
|
The new result to add to the current state. |
required |
state
|
Any | None
|
The current accumulated state (default: None). |
None
|
keepdims
|
bool
|
Whether to keep reduced dimensions (default: True). |
True
|
axis
|
int
|
The axis along which to sum (default: 0). |
0
|
Returns:
Type | Description |
---|---|
tuple[Any, Any]
|
The updated state and the new accumulated sum. |
Source code in laplax/util/loader.py
reduce_add ¶
Perform a reduction by adding results.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
res_new
|
Any
|
The new result to add to the current state. |
required |
state
|
Any | None
|
The current accumulated state (default: None). |
None
|
Returns:
Type | Description |
---|---|
tuple[Any, Any]
|
The updated state and the new accumulated sum. |
Source code in laplax/util/loader.py
concat ¶
Concatenate two PyTrees along a specified axis.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tree1
|
PyTree
|
The first PyTree to concatenate. |
required |
tree2
|
PyTree
|
The second PyTree to concatenate. |
required |
axis
|
int
|
The axis along which to concatenate (default: 0). |
0
|
Returns:
Type | Description |
---|---|
PyTree
|
A PyTree resulting from concatenating |
Source code in laplax/util/loader.py
reduce_concat ¶
Perform a reduction by concatenating results.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
res_new
|
Any
|
The new result to concatenate with the current state. |
required |
state
|
Any | None
|
The current accumulated state (default: None). |
None
|
axis
|
int
|
The axis along which to concatenate (default: 0). |
0
|
Returns:
Type | Description |
---|---|
tuple[Any, Any]
|
The updated state and the concatenated result. |
Source code in laplax/util/loader.py
reduce_online_mean ¶
Compute the online mean of results, maintaining a running count and sum.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
res_new
|
Any
|
The new result to incorporate into the mean calculation. |
required |
state
|
tuple | None
|
A tuple containing the current count and running sum (default: None). |
None
|
Returns:
Type | Description |
---|---|
tuple[Any, tuple]
|
The updated mean and the new state (count, running sum). |
Source code in laplax/util/loader.py
process_batches ¶
process_batches(function: Callable, data_loader: Iterable, transform: Callable, reduce: Callable, *args: Any, verbose_logging: bool = False, **kwargs: Kwargs) -> Any
Process batches of data using a function, transformation, and reduction.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
function
|
Callable
|
A callable that processes a single batch of data. |
required |
data_loader
|
Iterable
|
An iterable yielding batches of data. |
required |
transform
|
Callable
|
A callable that transforms each batch into the desired format. |
required |
reduce
|
Callable
|
A callable that reduces results across batches. |
required |
*args
|
Any
|
Additional positional arguments for the processing function. |
()
|
verbose_logging
|
bool
|
Whether to log progress using tqdm (default: False). |
False
|
**kwargs
|
Kwargs
|
Additional keyword arguments for the processing function. |
{}
|
Returns:
Type | Description |
---|---|
Any
|
The final result after processing all batches. |
Raises:
Type | Description |
---|---|
ValueError
|
If the data loader is empty. |
Source code in laplax/util/loader.py
execute_with_data_loader ¶
execute_with_data_loader(function: Callable, data_loader: Iterable, transform: Callable = input_target_split, reduce: Callable = reduce_online_mean, *, jit: bool = False, **kwargs: Kwargs) -> Any
Execute batch processing with a data loader.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
function
|
Callable
|
A callable that processes a single batch of data. |
required |
data_loader
|
Iterable
|
An iterable yielding batches of data. |
required |
transform
|
Callable
|
A callable to transform each batch into the desired format
(default: |
input_target_split
|
reduce
|
Callable
|
A callable to reduce results across batches
(default: |
reduce_online_mean
|
jit
|
bool
|
Whether to JIT compile the processing function (default: False). |
False
|
**kwargs
|
Kwargs
|
Additional keyword arguments for the processing function. |
{}
|
Returns:
Type | Description |
---|---|
Any
|
The final result after processing all batches. |
Source code in laplax/util/loader.py
wrap_function_with_data_loader ¶
wrap_function_with_data_loader(function: Callable, data_loader: Iterable, transform: Callable = input_target_split, reduce: Callable = reduce_online_mean, *, jit: bool = False) -> Callable
Wrap a function to process batches with a data loader.
This wrapper generates a callable that processes all batches from the data loader using the specified function, transformation, and reduction.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
function
|
Callable
|
A callable that processes a single batch of data. |
required |
data_loader
|
Iterable
|
An iterable yielding batches of data. |
required |
transform
|
Callable
|
A callable to transform each batch into the desired format
(default: |
input_target_split
|
reduce
|
Callable
|
A callable to reduce results across batches
(default: |
reduce_online_mean
|
jit
|
bool
|
Whether to JIT compile the processing function (default: False). |
False
|
Returns:
Type | Description |
---|---|
Callable
|
A wrapped function for batch processing. |
Source code in laplax/util/loader.py
_ ¶
_(mv: DataLoaderMV, input_fn: Callable | None = None, output_fn: Callable | None = None, argnums: int = 0) -> Callable
Apply wrap_function to DataLoaderMV.
Returns:
Type | Description |
---|---|
Callable
|
A DataLoaderMV object representing the wrapped MV. |