utils
This module provides utility functions for 4D-VarNet.
Utility functions include data preprocessing, optimization configuration, diagnostics, and evaluation metrics.
Functions:
Name | Description |
---|---|
pipe |
Apply a sequence of functions to an input. |
kwgetattr |
Get an attribute of an object by name. |
callmap |
Apply a list of functions to an input and return the results. |
half_lr_adam |
Configure an Adam optimizer with specific learning rates for model components. |
cosanneal_lr_adam |
Configure an Adam optimizer with cosine annealing learning rate scheduling. |
cosanneal_lr_lion |
Configure a Lion optimizer with cosine annealing learning rate scheduling. |
triang_lr_adam |
Configure an Adam optimizer with triangular cyclic learning rate scheduling. |
remove_nan |
Fill NaN values in a DataArray using Gauss-Seidel interpolation. |
get_constant_crop |
Generate a constant cropping mask for patches. |
get_cropped_hanning_mask |
Generate a cropped Hanning mask for patches. |
get_triang_time_wei |
Generate a triangular time weighting mask for patches. |
load_enatl |
Load ENATL dataset and preprocess it. |
load_altimetry_data |
Load altimetry data and preprocess it. |
load_dc_data |
Load DC data (currently a placeholder function). |
load_full_natl_data |
Load full NATL dataset and preprocess it. |
rmse_based_scores_from_ds |
Compute RMSE-based scores from a dataset. |
psd_based_scores_from_ds |
Compute PSD-based scores from a dataset. |
rmse_based_scores |
Compute RMSE-based scores for reconstruction evaluation. |
psd_based_scores |
Compute PSD-based scores for reconstruction evaluation. |
diagnostics |
Compute diagnostics for a given test domain. |
diagnostics_from_ds |
Compute diagnostics from a dataset. |
test_osse |
Perform OSSE testing and compute metrics. |
ensemble_metrics |
Compute ensemble metrics for multiple checkpoints. |
add_geo_attrs |
Add geographic attributes to a DataArray. |
vort |
Compute vorticity from a DataArray. |
geo_energy |
Compute geostrophic energy from a DataArray. |
best_ckpt |
Retrieve the best checkpoint from an experiment directory. |
load_cfg |
Load configuration files for an experiment. |
add_geo_attrs(da)
Add geographic attributes (longitude and latitude units) to a DataArray.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
da
|
DataArray
|
The input DataArray. |
required |
Returns:
Type | Description |
---|---|
xarray.DataArray: The DataArray with geographic attributes added. |
Source code in ocean4dvarnet/utils.py
635 636 637 638 639 640 641 642 643 644 645 646 647 |
|
best_ckpt(xp_dir)
Retrieve the best checkpoint from an experiment directory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
xp_dir
|
str
|
Path to the experiment directory. |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
Path to the best checkpoint file. |
Source code in ocean4dvarnet/utils.py
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 |
|
callmap(inp, fns)
Apply a list of functions to an input and return the results.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inp
|
The input to process. |
required | |
fns
|
list
|
A list of functions to apply. |
required |
Returns:
Name | Type | Description |
---|---|---|
list |
A list of results from applying each function. |
Source code in ocean4dvarnet/utils.py
85 86 87 88 89 90 91 92 93 94 95 96 |
|
cosanneal_lr_adam(lit_mod, lr, T_max=100, weight_decay=0.0)
Configure an Adam optimizer with cosine annealing learning rate scheduling.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lit_mod
|
The Lightning module containing the model. |
required | |
lr
|
float
|
The base learning rate. |
required |
T_max
|
int
|
Maximum number of iterations for the scheduler. |
100
|
weight_decay
|
float
|
Weight decay for the optimizer. |
0.0
|
Returns:
Name | Type | Description |
---|---|---|
dict |
A dictionary containing the optimizer and scheduler. |
Source code in ocean4dvarnet/utils.py
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 |
|
cosanneal_lr_lion(lit_mod, lr, T_max=100)
Configure a Lion optimizer with cosine annealing learning rate scheduling.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lit_mod
|
The Lightning module containing the model. |
required | |
lr
|
float
|
The base learning rate. |
required |
T_max
|
int
|
Maximum number of iterations for the scheduler. |
100
|
Returns:
Name | Type | Description |
---|---|---|
dict |
A dictionary containing the optimizer and scheduler. |
Source code in ocean4dvarnet/utils.py
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
|
diagnostics(lit_mod, test_domain)
Compute diagnostics for a given test domain.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lit_mod
|
The Lightning module containing the model. |
required | |
test_domain
|
dict
|
The test domain to evaluate. |
required |
Returns:
Type | Description |
---|---|
pandas.Series: A series containing diagnostic metrics. |
Source code in ocean4dvarnet/utils.py
507 508 509 510 511 512 513 514 515 516 517 518 519 |
|
diagnostics_from_ds(test_data, test_domain)
Compute diagnostics from a dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
test_data
|
Dataset
|
The test data. |
required |
test_domain
|
dict
|
The test domain to evaluate. |
required |
Returns:
Type | Description |
---|---|
pandas.Series: A series containing diagnostic metrics. |
Source code in ocean4dvarnet/utils.py
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 |
|
ensemble_metrics(trainer, lit_mod, ckpt_list, dm, save_path)
Compute ensemble metrics for multiple checkpoints.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
trainer
|
Trainer
|
The PyTorch Lightning trainer instance. |
required |
lit_mod
|
LightningModule
|
The Lightning module to test. |
required |
ckpt_list
|
list
|
List of checkpoint paths to evaluate. |
required |
dm
|
LightningDataModule
|
The datamodule for testing. |
required |
save_path
|
str
|
Path to save the metrics and ensemble outputs. |
required |
Returns:
Type | Description |
---|---|
None |
Source code in ocean4dvarnet/utils.py
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 |
|
geo_energy(da)
Compute the geostrophic energy from a DataArray.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
da
|
DataArray
|
The input DataArray. |
required |
Returns:
Type | Description |
---|---|
xarray.DataArray: The geostrophic energy computed from the input data. |
Source code in ocean4dvarnet/utils.py
667 668 669 670 671 672 673 674 675 676 677 |
|
get_constant_crop(patch_dims, crop, dim_order=['time', 'lat', 'lon'])
Generate a constant cropping mask for patches.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
patch_dims
|
dict
|
Dimensions of the patch. |
required |
crop
|
dict
|
Crop sizes for each dimension. |
required |
dim_order
|
list
|
Order of dimensions. |
['time', 'lat', 'lon']
|
Returns:
Type | Description |
---|---|
numpy.ndarray: A mask with cropped regions set to 0 and others to 1. |
Source code in ocean4dvarnet/utils.py
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
|
get_cropped_hanning_mask(patch_dims, crop, **kwargs)
Generate a cropped Hanning mask for patches.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
patch_dims
|
dict
|
Dimensions of the patch. |
required |
crop
|
dict
|
Crop sizes for each dimension. |
required |
Returns:
Type | Description |
---|---|
numpy.ndarray: The cropped Hanning mask. |
Source code in ocean4dvarnet/utils.py
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
|
get_triang_time_wei(patch_dims, offset=0, **crop_kw)
Generate a triangular time weighting mask for patches.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
patch_dims
|
dict
|
Dimensions of the patch. |
required |
offset
|
int
|
Offset for the triangular weighting. |
0
|
crop_kw
|
dict
|
Additional cropping parameters. |
{}
|
Returns:
Type | Description |
---|---|
numpy.ndarray: The triangular time weighting mask. |
Source code in ocean4dvarnet/utils.py
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
|
half_lr_adam(lit_mod, lr)
Configure an Adam optimizer with specific learning rates for model components.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lit_mod
|
The Lightning module containing the model. |
required | |
lr
|
float
|
The base learning rate. |
required |
Returns:
Type | Description |
---|---|
torch.optim.Adam: The configured optimizer. |
Source code in ocean4dvarnet/utils.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
|
kwgetattr(obj, name)
Get an attribute of an object by name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj
|
The object to query. |
required | |
name
|
str
|
The name of the attribute. |
required |
Returns:
Type | Description |
---|---|
The value of the attribute. |
Source code in ocean4dvarnet/utils.py
71 72 73 74 75 76 77 78 79 80 81 82 |
|
load_altimetry_data(path, obs_from_tgt=False)
Load and preprocess altimetry data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
Path to the altimetry dataset. |
required |
obs_from_tgt
|
bool
|
Whether to use target data as observations. |
False
|
Returns:
Type | Description |
---|---|
xarray.DataArray: The preprocessed altimetry dataset. |
Source code in ocean4dvarnet/utils.py
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 |
|
load_cfg(xp_dir)
Load configuration files for an experiment.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
xp_dir
|
str
|
Path to the experiment directory. |
required |
Returns:
Name | Type | Description |
---|---|---|
tuple |
A tuple containing the configuration and the experiment name. |
Source code in ocean4dvarnet/utils.py
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 |
|
load_dc_data(**kwargs)
Load DC data.
This is currently a placeholder function for loading DC data.
Returns:
Type | Description |
---|---|
None |
Source code in ocean4dvarnet/utils.py
346 347 348 349 350 351 352 353 354 355 356 357 358 359 |
|
load_enatl(*args, obs_from_tgt=True, **kwargs)
Load and preprocess the ENATL dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obs_from_tgt
|
bool
|
Whether to use target data as observations. |
True
|
Returns:
Type | Description |
---|---|
xarray.DataArray: The preprocessed ENATL dataset. |
Source code in ocean4dvarnet/utils.py
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 |
|
load_full_natl_data(path_obs='../sla-data-registry/CalData/cal_data_new_errs.nc', path_gt='../sla-data-registry/NATL60/NATL/ref_new/NATL60-CJM165_NATL_ssh_y2013.1y.nc', obs_var='five_nadirs', gt_var='ssh', **kwargs)
Load and preprocess the full NATL dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path_obs
|
str
|
Path to the observation dataset. |
'../sla-data-registry/CalData/cal_data_new_errs.nc'
|
path_gt
|
str
|
Path to the ground truth dataset. |
'../sla-data-registry/NATL60/NATL/ref_new/NATL60-CJM165_NATL_ssh_y2013.1y.nc'
|
obs_var
|
str
|
Observation variable name. |
'five_nadirs'
|
gt_var
|
str
|
Ground truth variable name. |
'ssh'
|
Returns:
Type | Description |
---|---|
xarray.DataArray: The preprocessed NATL dataset. |
Source code in ocean4dvarnet/utils.py
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 |
|
pipe(inp, fns)
Apply a sequence of functions to an input.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inp
|
The input to process. |
required | |
fns
|
list
|
A list of functions to apply. |
required |
Returns:
Type | Description |
---|---|
The processed input after applying all functions. |
Source code in ocean4dvarnet/utils.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
|
psd_based_scores(da_rec, da_ref)
Compute PSD-based scores for reconstruction evaluation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
da_rec
|
DataArray
|
The reconstructed data. |
required |
da_ref
|
DataArray
|
The reference data. |
required |
Returns:
Name | Type | Description |
---|---|---|
tuple |
A tuple containing PSD-based scores and resolved wavelengths. |
Source code in ocean4dvarnet/utils.py
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 |
|
psd_based_scores_from_ds(ds, ref_variable='tgt', study_variable='out')
Compute PSD-based scores from a dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ds
|
Dataset
|
The dataset containing the reference and study variables. |
required |
ref_variable
|
str
|
The name of the reference variable. |
'tgt'
|
study_variable
|
str
|
The name of the study variable. |
'out'
|
Returns:
Name | Type | Description |
---|---|---|
list |
A list containing PSD-based scores. |
Source code in ocean4dvarnet/utils.py
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 |
|
remove_nan(da)
Fill NaN values in a DataArray using Gauss-Seidel interpolation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
da
|
DataArray
|
The input DataArray. |
required |
Returns:
Type | Description |
---|---|
xarray.DataArray: The DataArray with NaN values filled. |
Source code in ocean4dvarnet/utils.py
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
|
rmse_based_scores(da_rec, da_ref)
Compute RMSE-based scores for reconstruction evaluation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
da_rec
|
DataArray
|
The reconstructed data. |
required |
da_ref
|
DataArray
|
The reference data. |
required |
Returns:
Name | Type | Description |
---|---|---|
tuple |
A tuple containing RMSE-based scores. |
Source code in ocean4dvarnet/utils.py
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 |
|
rmse_based_scores_from_ds(ds, ref_variable='tgt', study_variable='out')
Compute RMSE-based scores from a dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ds
|
Dataset
|
The dataset containing the reference and study variables. |
required |
ref_variable
|
str
|
The name of the reference variable. |
'tgt'
|
study_variable
|
str
|
The name of the study variable. |
'out'
|
Returns:
Name | Type | Description |
---|---|---|
list |
A list containing RMSE-based scores. |
Source code in ocean4dvarnet/utils.py
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 |
|
test_osse(trainer, lit_mod, osse_dm, osse_test_domain, ckpt, diag_data_dir=None)
Perform OSSE (Observing System Simulation Experiment) testing and compute metrics.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
trainer
|
Trainer
|
The PyTorch Lightning trainer instance. |
required |
lit_mod
|
LightningModule
|
The Lightning module to test. |
required |
osse_dm
|
LightningDataModule
|
The datamodule for OSSE testing. |
required |
osse_test_domain
|
dict
|
The test domain for evaluation. |
required |
ckpt
|
str
|
Path to the checkpoint to load. |
required |
diag_data_dir
|
Path
|
Directory to save diagnostic data. |
None
|
Returns:
Type | Description |
---|---|
pandas.Series: A series containing OSSE metrics. |
Source code in ocean4dvarnet/utils.py
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 |
|
triang_lr_adam(lit_mod, lr_min=5e-05, lr_max=0.003, nsteps=200)
Configure an Adam optimizer with triangular cyclic learning rate scheduling.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lit_mod
|
The Lightning module containing the model. |
required | |
lr_min
|
float
|
Minimum learning rate. |
5e-05
|
lr_max
|
float
|
Maximum learning rate. |
0.003
|
nsteps
|
int
|
Number of steps for the triangular cycle. |
200
|
Returns:
Name | Type | Description |
---|---|---|
dict |
A dictionary containing the optimizer and scheduler. |
Source code in ocean4dvarnet/utils.py
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
|
vort(da)
Compute the vorticity from a DataArray.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
da
|
DataArray
|
The input DataArray. |
required |
Returns:
Type | Description |
---|---|
xarray.DataArray: The vorticity computed from the input data. |
Source code in ocean4dvarnet/utils.py
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 |
|