serial
canari_ml.data.loaders.serial
¶
canari_ml.data.loaders.serial.logger = logging.getLogger(__name__)
module-attribute
¶
canari_ml.data.loaders.serial.DATE_FORMAT = '%Y-%m-%d'
module-attribute
¶
canari_ml.data.loaders.serial.SerialLoader(*args, plot=False, **kwargs)
¶
Bases: CanariMLBaseDataLoader
A loader that generates and loads data serially.
This class extends CanariMLBaseDataLoader to provide functionality for
generating and loading data sequentially. It supports generating data for
multiple datasets (e.g., 'train', 'val', 'test') and can optionally produce
plots for each sample. The generation process is configurable with various
arguments, including batch size, number of workers, dry mode, and plot output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
Unpack
|
Variable length argument list. |
()
|
plot
|
optional
|
Whether to also output plots for each sample. Defaults to False. |
False
|
**kwargs
|
Unpack
|
Arbitrary keyword arguments. |
{}
|
Attributes:
| Name | Type | Description |
|---|---|---|
_masks |
dict[str, DataArray]
|
Dictionary of masks for each variable, loaded from configuration. |
_plot |
bool
|
Flag indicating whether to produce plots during data generation. |
Source code in src/canari_ml/data/loaders/serial.py
canari_ml.data.loaders.serial.SerialLoader.generate()
¶
canari_ml.data.loaders.serial.SerialLoader.client_generate(dates_override=None, pickup=False, client=None)
¶
Generate data for multiple datasets sequentially.
This method generates data for 'train', 'val', and 'test' datasets
in sequence. It supports overriding dates using dates_override argument
and can pick up an existing generation process using pickup.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dates_override
|
optional
|
Dates to override for each split. Should be a dictionary with keys 'train', 'val', and 'test', where the values are lists of dates. Defaults to None. |
None
|
pickup
|
optional
|
Whether to pick up an existing generation process. Defaults to False. |
False
|
client
|
object | None
|
Client object. Defaults to None. |
None
|
Returns:
| Type | Description |
|---|---|
None
|
None |
Source code in src/canari_ml/data/loaders/serial.py
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | |
canari_ml.data.loaders.serial.SerialLoader.generate_sample(date, prediction=False, parallel=True)
¶
Generate a sample for the given date.
This method generates a single data sample for the provided date using the configured variables and masks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
date
|
date
|
The date to generate a sample for. |
required |
prediction
|
optional
|
Whether requesting a sample for predictions instead of targets. Defaults to False. |
False
|
parallel
|
optional
|
Whether to read the data from multiple |
True
|
Returns:
| Type | Description |
|---|---|
tuple[array, array, array]
|
A tuple containing the input features, output target, and sample weights for the generated sample. |
Source code in src/canari_ml/data/loaders/serial.py
canari_ml.data.loaders.serial.plot_samples_grid(data_array, title_prefix, fname, titles=None, vmin=0, vmax=1, cmap='RdBu_r')
¶
Plot samples in a grid.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_array
|
DataArray
|
3D array (N, H, W), where N is the number of channels |
required |
title_prefix
|
str
|
Prefix for figure title |
required |
fname
|
str
|
Output file path (.jpg) |
required |
titles
|
optional
|
List of strings to title each subplot |
None
|
vmin
|
optional
|
Minimum value for colourbar. Defaults to 0. |
0
|
vmax
|
optional
|
Maximum value for colourbar. Defaults to 1. |
1
|
cmap
|
optional
|
Matplotlib colormap |
'RdBu_r'
|
Source code in src/canari_ml/data/loaders/serial.py
canari_ml.data.loaders.serial.process_date(idx, date, n_forecast_steps, var_ds, var_files, trend_ds, channels, meta_channels, trend_steps, frequency_attr, dry, plot, plot_dir, args)
¶
Process a single date to generate samples and write them to the Zarr store.
This function generates a sample for the given date using the provided datasets and configuration arguments. It writes the generated sample to the specified Zarr store if not running in dry mode. Optionally, it outputs plots of inputs, outputs, and sample weights for visualisation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
idx
|
int
|
Index of the current date. |
required |
date
|
date
|
Date to generate samples for. |
required |
n_forecast_steps
|
int
|
Number of |
required |
var_ds
|
Dataset
|
Dataset containing variable data. |
required |
var_files
|
dict[str, str]
|
Dictionary of variable files with their corresponding paths. |
required |
trend_ds
|
Dataset
|
Dataset containing linear trend data (if any). |
required |
channels
|
dict[str, int]
|
Dictionary mapping variable names to the number of channels. |
required |
meta_channels
|
list[str]
|
List of metadata channel names. |
required |
trend_steps
|
list[int] | int
|
Trend steps for linear trends (if applicable). |
required |
frequency_attr
|
str
|
Attribute indicating the time frequency (e.g., "months" or "days"). |
required |
dry
|
bool
|
Whether to run in dry mode. Default is False. |
required |
plot
|
bool
|
Whether to output plots for each sample. Default is False. |
required |
plot_dir
|
str
|
Directory path for saving plots. |
required |
args
|
tuple
|
Additional arguments required for generating samples. |
required |
Returns:
| Type | Description |
|---|---|
tuple[array, array, array, float]
|
A tuple of: * x: inputs, * y: target, * sample_weights: sample weights * and the time taken to process the date in seconds. |
Source code in src/canari_ml/data/loaders/serial.py
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 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 | |
canari_ml.data.loaders.serial.generate_and_write(path, var_files, dates, args, batch_size=32, workers=4, dry=False, plot=False)
¶
Generate and write Zarr dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Path to the output Zarr dataset. |
required |
var_files
|
dict[str, str]
|
Dictionary of variable files with their corresponding paths. |
required |
dates
|
list[date]
|
List of dates to generate samples for. |
required |
args
|
tuple
|
Method arguments. |
required |
batch_size
|
optional
|
Batch size for processing. Defaults to 32. |
32
|
workers
|
optional
|
Number of worker processes for parallel processing. Defaults to 4. |
4
|
dry
|
optional
|
Whether to run in dry mode. Defaults to False. |
False
|
plot
|
optional
|
Whether to also output plots for each sample. Defaults to False. |
False
|
Returns:
| Type | Description |
|---|---|
tuple[str, int, list[float]]
|
Paths to the output Zarr dataset, the count of processed dates, and a list of time taken for each date. |
Source code in src/canari_ml/data/loaders/serial.py
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 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 | |
canari_ml.data.loaders.serial.get_date_indices(forecast_date, var_ds, n_forecast_steps, relative_attr)
¶
Compute the indices and dates need as inputs and outputs to the forecast model.
Given a forecast initialisation date, the input dataset, the number of steps to forecast for, and a relative time attribute (e.g., 'months', 'days').
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
forecast_date
|
datetime
|
The initialisation date for the forecast. |
required |
var_ds
|
Dataset
|
xarray Dataset. |
required |
n_forecast_steps
|
int
|
Number of forecast steps (lead times) to generate. |
required |
relative_attr
|
str
|
The time attribute for stepping forward (e.g., "months", "days"). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
forecast_base_idx |
(int, list[int], Generator)
|
The index of the forecast init date in |
forecast_idxs |
(int, list[int], Generator)
|
List of indices for each forecast step in |
forecast_steps |
(int, list[int], Generator)
|
Generator yielding the dates for each forecast step. |
Source code in src/canari_ml/data/loaders/serial.py
canari_ml.data.loaders.serial.get_channel_idxs(var_name, forecast_base_idx, num_channels, trend_steps)
¶
Compute the time indices for input channels for a given variable.
Determine which time indices to use for a variable's input channels, depending on whether the variable is a linear trend or a lagged variable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
var_name
|
str
|
Name of the variable. If it ends with "linear_trend", trend logic is used. |
required |
forecast_base_idx
|
int
|
Index of the forecast initialisation date in the time dimension. |
required |
num_channels
|
int
|
Number of channels to generate for this variable. |
required |
trend_steps
|
int | list[int]
|
Steps to use for trend channels.
If list: use these as offsets from the base index.
If int: use a range from 0 to |
required |
Returns:
| Type | Description |
|---|---|
list[int]
|
List of indices corresponding to the time dimension for each channel. |
Source code in src/canari_ml/data/loaders/serial.py
canari_ml.data.loaders.serial.generate_sample(forecast_date, var_ds, var_files, trend_ds, channels, dtype, loss_weight_days, meta_channels, missing_dates, n_forecast_steps, num_channels, shape, trend_steps, frequency_attr, masks, prediction=False)
¶
Generate a sample for train/val/prediction.
This function creates input features (x), targets (y), and sample weights based on the given parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
forecast_date
|
object
|
The forecast initialisation date. |
required |
var_ds
|
object
|
The input xarray dataset containing variables like ua700_abs, siconca, etc. |
required |
var_files
|
dict
|
Map of meta variable names to their corresponding file paths. |
required |
trend_ds
|
object
|
The xarray dataset containing linear trends. |
required |
channels
|
dict
|
Map of variable name to number of channels(excluding meta). |
required |
dtype
|
object
|
The data type used for the input features, targets, and sample weights. |
required |
loss_weight_days
|
bool
|
If True, apply temporal weighting for loss calculation. |
required |
meta_channels
|
list
|
Meta channel names to include in the input features. |
required |
missing_dates
|
list
|
Dates with missing data. |
required |
n_forecast_steps
|
int
|
The number of forecast steps in target (target leadtime). |
required |
num_channels
|
int
|
The total number of channels (input features). |
required |
shape
|
object
|
The spatial shape of the dataset. |
required |
trend_steps
|
object
|
The step(s) for linear trends. Can be a single integer or a list of integers. |
required |
frequency_attr
|
str
|
The time frequency attribute, e.g., 'DAY' for daily data. |
required |
masks
|
object
|
Map of mask names and their corresponding DataArrays. |
required |
prediction
|
optional
|
If True, generate a sample for prediction; otherwise, generate a training sample. Defaults to False. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
x |
tuple[DataArray, DataArray, DataArray] | tuple[DataArray, DataArray, DataArray, DataArray]
|
Input features with shape (num_channels, *shape). |
y |
tuple[DataArray, DataArray, DataArray] | tuple[DataArray, DataArray, DataArray, DataArray]
|
Targets with shape (1, *shape, n_forecast_steps). |
sample_weights |
tuple[DataArray, DataArray, DataArray] | tuple[DataArray, DataArray, DataArray, DataArray]
|
Sample weights with shape (1, *shape, n_forecast_steps). |
Source code in src/canari_ml/data/loaders/serial.py
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 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 808 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 | |