utils¶
Description of general helper functions implemented in the module bnn_for_14C_calibration.utils:
bnn_for_14C_calibration.utils
¶
ajoute_segment_horizontal(y, x_min, x_max, ax=None, color='blue', linestyle='-', linewidth=2, label=None, ticks=True, tick_size=0.1)
¶
Plot a horizontal segment from (x_min, y) to (x_max, y) with optional vertical ticks at ends.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
float
|
Y-coordinate of the segment. |
required |
x_min
|
float
|
Starting X-coordinate (left). |
required |
x_max
|
float
|
Ending X-coordinate (right). |
required |
ax
|
Axes
|
Axis to plot on. If None, uses current axis. |
None
|
color
|
str
|
Segment color (default 'blue'). |
'blue'
|
linestyle
|
str
|
Line style (default '-'). |
'-'
|
linewidth
|
float
|
Line width (default 2). |
2
|
label
|
str
|
Label for the segment. |
None
|
ticks
|
bool
|
If True, adds small vertical ticks at the segment ends (default True). |
True
|
tick_size
|
float
|
Length of vertical ticks (default 0.1). |
0.1
|
Returns:
| Type | Description |
|---|---|
Line2D
|
The main line object of the horizontal segment. |
Source code in src/bnn_for_14C_calibration/utils.py
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 | |
ajoute_segment_vertical(x, y_min, y_max, ax=None, color='red', linestyle='-', linewidth=2, label=None, ticks=True, tick_size=0.1)
¶
Plot a vertical segment from (x, y_min) to (x, y_max) with optional horizontal ticks at ends.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
X-coordinate of the segment. |
required |
y_min
|
float
|
Starting Y-coordinate (bottom). |
required |
y_max
|
float
|
Ending Y-coordinate (top). |
required |
ax
|
Axes
|
Axis to plot on. If None, uses current axis. |
None
|
color
|
str
|
Segment color (default 'red'). |
'red'
|
linestyle
|
str
|
Line style (default '-'). |
'-'
|
linewidth
|
float
|
Line width (default 2). |
2
|
label
|
str
|
Label for the segment. |
None
|
ticks
|
bool
|
If True, adds small horizontal ticks at the segment ends (default True). |
True
|
tick_size
|
float
|
Length of horizontal ticks (default 0.1). |
0.1
|
Returns:
| Type | Description |
|---|---|
Line2D
|
The main line object of the vertical segment. |
Source code in src/bnn_for_14C_calibration/utils.py
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 | |
bp_to_calendar(bp)
¶
Converts BP (Before Present, ref. 1949) to calendar year BCE/CE.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bp
|
int or ndarray
|
Year(s) in BP (Before Present, reference year 1949). |
required |
Returns:
| Type | Description |
|---|---|
Tuple[int, str] or ndarray
|
|
Notes
- BP > 1949 corresponds to BCE, else CE.
- Skips year 0: 1949 BP = 1 CE.
Source code in src/bnn_for_14C_calibration/utils.py
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 | |
c14_to_d14c(c14, teta)
¶
Convert radiocarbon age (c14) to d14c domain.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c14
|
float or ndarray
|
Radiocarbon age(s). |
required |
teta
|
float or ndarray
|
Calendar age(s), same shape as c14. |
required |
Returns:
| Type | Description |
|---|---|
float or ndarray
|
Corresponding d14c value(s). |
Notes
- Supports element-wise operations.
- This function is computed as the composition of
f14c_to_d14candc14_to_f14c: d14c = f14c_to_d14c(c14_to_f14c(c14),teta).
Source code in src/bnn_for_14C_calibration/utils.py
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 | |
c14_to_f14c(c14)
¶
Convert radiocarbon age (\(^{14}\)C) to f14c domain.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c14
|
float or ndarray
|
Radiocarbon age(s). |
required |
Returns:
| Type | Description |
|---|---|
float or ndarray
|
Corresponding f14c value(s). |
Notes
- Supports element-wise operations.
- As the inverse function of
f14c_to_c14, it uses its formula's inverse for transformation: f14c = exp(-c14/8033)
Source code in src/bnn_for_14C_calibration/utils.py
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 | |
c14sig_to_d14csig(c14, c14sig, teta)
¶
Convert \(^{14}\)C uncertainty to d14c uncertainty.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c14
|
float or ndarray
|
Radiocarbon age(s). |
required |
c14sig
|
float or ndarray
|
Uncertainty in c14. |
required |
teta
|
float or ndarray
|
Calendar age(s), same shape as c14. |
required |
Returns:
| Type | Description |
|---|---|
float or ndarray
|
Corresponding uncertainty in d14c. |
Notes
- Supports element-wise operations.
- This function is computed as the composition of
f14csig_to_d14csigandc14sig_to_f14csig: c14sig = f14csig_to_d14csig( c14sig_to_f14csig(c14,c14sig), teta ).
Source code in src/bnn_for_14C_calibration/utils.py
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 | |
c14sig_to_f14csig(c14, c14sig)
¶
Convert c14 uncertainty to f14c uncertainty using delta-method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c14
|
float or ndarray
|
Radiocarbon age(s). |
required |
c14sig
|
float or ndarray
|
Uncertainty in c14, same shape as c14. |
required |
Returns:
| Type | Description |
|---|---|
float or ndarray
|
Corresponding uncertainty in f14c. |
Notes
- Supports element-wise operations.
- As the inverse function of
f14csig_to_c14sig, it uses its formula's inverse for transformation: f14csig = c14sig * f14c/8033, where f14c is computed using the functionc14_to_f14c.
Source code in src/bnn_for_14C_calibration/utils.py
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 | |
calendar_to_bp(year, era)
¶
Converts calendar year(s) in BCE or CE to BP (Before Present, ref. 1949).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
year
|
int or ndarray
|
Positive calendar year(s) in BCE or CE. |
required |
era
|
str or ndarray
|
Era indicator(s): 'BCE' or 'CE'.
If a NumPy array is provided, it must have the same shape as |
required |
Returns:
| Type | Description |
|---|---|
int or ndarray
|
Corresponding year(s) in BP. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If any element in |
Notes
- Skips year 0: 1 CE = 1949 BP.
- Supports both scalar and vectorized inputs.
- When given a structured array as returned by
bp_to_calendar, it will internally extract the year and era columns before computation.
Examples:
>>> calendar_to_bp(2500, 'BCE')
4449
>>> calendar_to_bp(2020, 'CE')
-70
>>> arr = np.array([[2500, 'BCE'], [2020, 'CE']], dtype=object)
>>> calendar_to_bp(arr[:, 0].astype(int), arr[:, 1])
array([4449., -70.])
>>> calendar_to_bp(arr[:, 0], arr[:, 1])
array([4449., -70.])
Source code in src/bnn_for_14C_calibration/utils.py
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 844 845 | |
d14c_to_c14(d14c, teta)
¶
Convert d14c domain to radiocarbon age (\(^{14}\)C).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
d14c
|
float or ndarray
|
d14c value(s). |
required |
teta
|
float or ndarray
|
Calendar age(s), same shape as d14c. |
required |
Returns:
| Type | Description |
|---|---|
float or ndarray
|
Corresponding c14 value(s). |
Notes
- Supports element-wise operations.
- This function is computed as the composition of
f14c_to_c14andd14c_to_f14c: c14 = f14c_to_c14(d14c_to_f14c(d14c,teta)).
Source code in src/bnn_for_14C_calibration/utils.py
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 | |
d14c_to_f14c(d14c, teta)
¶
Convert d14c domain to f14c domain.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
d14c
|
float or ndarray
|
d14c value(s). |
required |
teta
|
float or ndarray
|
Calendar age(s), must match the shape of d14c. |
required |
Returns:
| Type | Description |
|---|---|
float or ndarray
|
Corresponding f14c value(s). |
Notes
- Supports element-wise operations.
- Uses transformation formula: f14c = (1 + d14c/1000) * exp(-teta/8267)
Source code in src/bnn_for_14C_calibration/utils.py
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 | |
d14csig_to_c14sig(d14c, d14csig, teta)
¶
Convert d14c uncertainty to \(^{14}\)C uncertainty.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
d14c
|
float or ndarray
|
d14c value(s). |
required |
d14csig
|
float or ndarray
|
Uncertainty in d14c. |
required |
teta
|
float or ndarray
|
Calendar age(s), same shape as d14c. |
required |
Returns:
| Type | Description |
|---|---|
float or ndarray
|
Corresponding uncertainty in \(^{14}\)C. |
Notes
- Supports element-wise operations.
- This function is computed as the composition of
f14csig_to_c14sig,d14c_to_f14candd14csig_to_f14csig: c14sig = f14csig_to_c14sig( d14c_to_f14c(d14c,teta), d14csig_to_f14csig(d14csig,teta) ).
Source code in src/bnn_for_14C_calibration/utils.py
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 | |
d14csig_to_f14csig(d14csig, teta)
¶
Convert d14c uncertainty to f14c uncertainty.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
d14csig
|
float or ndarray
|
Uncertainty in d14c. |
required |
teta
|
float or ndarray
|
Calendar age(s), same shape as d14csig. |
required |
Returns:
| Type | Description |
|---|---|
float or ndarray
|
Corresponding uncertainty in f14c. |
Notes
- Supports element-wise operations.
- Transformation formula can be derived directly or by using delta-method: f14csig = d14csig * exp(-teta/8267)/1000
Source code in src/bnn_for_14C_calibration/utils.py
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 | |
f14c_to_c14(f14c)
¶
Convert f14c domain to radiocarbon age (\(^{14}\)C).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f14c
|
float or ndarray
|
f14c value(s). |
required |
Returns:
| Type | Description |
|---|---|
float or ndarray
|
Radiocarbon age(s) \(^{14}\)C. |
Notes
- Supports element-wise operations.
- Uses transformation formula: c14 = -8033 * log(f14c)
Source code in src/bnn_for_14C_calibration/utils.py
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 | |
f14c_to_d14c(f14c, teta)
¶
Convert f14c to d14c.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f14c
|
float or ndarray
|
f14c value(s). |
required |
teta
|
float or ndarray
|
Calendar age(s), same shape as f14c. |
required |
Returns:
| Type | Description |
|---|---|
float or ndarray
|
Corresponding d14c value(s). |
Notes
- Supports element-wise operations.
- As the inverse function of
d14c_to_f14c, it uses its formula's inverse for transformation: d14c = 1000 * (-1 + f14c * exp(teta/8267))
Source code in src/bnn_for_14C_calibration/utils.py
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 | |
f14csig_to_c14sig(f14c, f14csig)
¶
Convert f14c uncertainty to \(^{14}\)C uncertainty using delta-method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f14c
|
float or ndarray
|
f14c value(s). |
required |
f14csig
|
float or ndarray
|
Uncertainty in f14c, same shape as f14c. |
required |
Returns:
| Type | Description |
|---|---|
float or ndarray
|
Corresponding uncertainty in \(^{14}\)C. |
Notes
- Supports element-wise operations.
- Transformation formula cannot be derived directly but by using delta-method: c14sig = f14csig * 8033/f14c
Source code in src/bnn_for_14C_calibration/utils.py
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 | |
f14csig_to_d14csig(f14csig, teta)
¶
Convert f14c uncertainty to d14c uncertainty.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f14csig
|
float or ndarray
|
Uncertainty in f14c. |
required |
teta
|
float or ndarray
|
Calendar age(s), same shape as f14csig. |
required |
Returns:
| Type | Description |
|---|---|
float or ndarray
|
Corresponding uncertainty in d14c. |
Notes
- Supports element-wise operations.
- As the inverse function of
d14csig_to_f14csig, it uses its formula's inverse for transformation: d14csig = 1000 * f14csig * exp(teta/8267)
Source code in src/bnn_for_14C_calibration/utils.py
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_lib_data_paths()
¶
Generate paths to embedded package data or local cache data.
Returns:
| Type | Description |
|---|---|
Dict[str, Path]
|
Dictionary containing paths to: - IntCal20 data - Exogenous variables - Bayesian neural network predictions and weights (from local cache if exists) |
Notes
- If the cache does not exist, it will automatically be created using
download_cache_lib_data.
Source code in src/bnn_for_14C_calibration/utils.py
30 31 32 33 34 35 36 37 38 39 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 66 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 | |
load_data(path, sep=';')
¶
Load a CSV file into a pandas DataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str or Path
|
Path to the CSV file. |
required |
sep
|
str
|
Separator used in the CSV file (default ';'). |
';'
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Loaded dataset. |
Source code in src/bnn_for_14C_calibration/utils.py
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | |
minimax_scaling(x, Max, Min)
¶
Apply manual min-max scaling to a single value or numpy array.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float or ndarray
|
Input value(s). |
required |
Max
|
float
|
Maximum of the original range. |
required |
Min
|
float
|
Minimum of the original range. |
required |
Returns:
| Type | Description |
|---|---|
float or ndarray
|
Scaled value(s) (in [0,1] if Min <= x <= Max). |
Notes
- If x is a numpy array, the operation is applied element-wise.
Source code in src/bnn_for_14C_calibration/utils.py
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | |
minimax_scaling_reciproque(x, Max, Min)
¶
Inverse min-max scaling for a value or numpy array.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float or ndarray
|
Scaled value(s). |
required |
Max
|
float
|
Maximum of the original range. |
required |
Min
|
float
|
Minimum of the original range. |
required |
Returns:
| Type | Description |
|---|---|
float or ndarray
|
Original value(s) before scaling (in [Min,Max] if 0 <= x <= 1). |
Notes
- If x is a numpy array, the operation is applied element-wise.
Source code in src/bnn_for_14C_calibration/utils.py
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | |
read_params_from_file(file_path)
¶
Read parameters from a text file and convert them to proper Python types.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str or Path
|
Path to the file containing parameters in 'key : value' format. |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dictionary mapping parameter names to their values, converted to int, float, bool, or str depending on content. |
Notes
- Boolean values are recognized as "True" or "False".
- Float values are recognized for keys ['alpha', 'beta', 'min_delta'].
- Other values are attempted to convert to int, otherwise stored as str.
Source code in src/bnn_for_14C_calibration/utils.py
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 | |