Automatic documentation generated from docstrings
This page is auto-generated from the docstrings of functions, methods, classes, and modules. Each lowest-level module in Mass2 (i.e., each python file) that you want documented and indexed for searching should be listed in this docstrings2.md file. The exception are the Core docstrings for the mass2.core docstrings.
Calibration
Energy to/from pulse heights
Objects to assist with calibration from pulse heights to absolute energies.
Created on May 16, 2011 Completely redesigned January 2025
Curvetypes
Bases: Enum
Enumerate the types of calibration curves supported by Mass2.
Source code in mass2/calibration/energy_calibration.py
24 25 26 27 28 29 30 31 32 | |
EnergyCalibration
dataclass
An energy calibration object that can convert pulse heights to (estimated) energies.
Subclasses implement the math of either exact or approximating calibration curves. Methods allow you to convert between pulse heights and energies, estimate energy uncertainties, and estimate pulse heights for lines whose names are know, or estimate the cal curve slope. Methods allow you to plot the calibration curve with its anchor points.
| Returns: |
|---|
| Raises: |
|
|---|
Source code in mass2/calibration/energy_calibration.py
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 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 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 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 844 | |
ismonotonic
property
Is the curve monotonic from 0 to 1.05 times the max anchor point's pulse height? Test at 1001 points, equally spaced in pulse height.
npts
property
Return the number of calibration anchor points.
__post_init__()
Fail for inputs of length zero.
Source code in mass2/calibration/energy_calibration.py
507 508 509 | |
__str__()
A full description of the calibration.
Source code in mass2/calibration/energy_calibration.py
628 629 630 631 632 633 | |
copy(**changes)
Make a copy of this object, optionally changing some attributes.
Source code in mass2/calibration/energy_calibration.py
511 512 513 | |
energy2dedph(energies)
Calculate the slope at the given energies.
Source code in mass2/calibration/energy_calibration.py
619 620 621 | |
energy2ph_exact(E)
An exact inversion of the calibration curve, converting energies E to pulse heights.
This is still in TO DO status, as it simply uses the spline in the forward direction.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in mass2/calibration/energy_calibration.py
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 | |
energy2uncertainty(energies)
Cal uncertainty in eV at the given energies.
Source code in mass2/calibration/energy_calibration.py
623 624 625 626 | |
load_from_hdf5(hdf5_group, name)
staticmethod
Load a calibration from an HDF5 group with the given name.
Source code in mass2/calibration/energy_calibration.py
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 | |
name2ph(name)
Convert a named energy feature to pulse height. name need not be a calibration point.
Source code in mass2/calibration/energy_calibration.py
614 615 616 617 | |
ph2dedph(ph)
Calculate the calibration curve's slope at pulse heights ph.
Source code in mass2/calibration/energy_calibration.py
659 660 661 662 663 664 665 666 667 668 669 670 671 | |
ph2energy(ph)
Apply the calibration, converting pulse heights ph to energies.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in mass2/calibration/energy_calibration.py
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 | |
plot(axis=None, color='blue', markercolor='red', plottype='linear', ph_rescale_power=0.0, removeslope=False, energy_x=False, showtext=True, showerrors=True, min_energy=None, max_energy=None)
Plot the calibration curve, with options.
Source code in mass2/calibration/energy_calibration.py
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 844 | |
plotgain(**kwargs)
Plot the calibration curve as gain (PH/eV) vs pulse height.
Source code in mass2/calibration/energy_calibration.py
721 722 723 724 | |
plotinvgain(**kwargs)
Plot the calibration curve as inverse gain (eV/PH) vs pulse height.
Source code in mass2/calibration/energy_calibration.py
726 727 728 729 | |
plotloggain(**kwargs)
Plot the calibration curve as log-gain log(PH/eV) vs pulse height.
Source code in mass2/calibration/energy_calibration.py
731 732 733 734 | |
save_to_hdf5(hdf5_group, name)
Save this calibration to an HDF5 group in a new subordinate group with the given name.
Source code in mass2/calibration/energy_calibration.py
690 691 692 693 694 695 696 697 698 699 700 701 702 | |
EnergyCalibrationMaker
dataclass
An object that can make energy calibration curves under various assumptions, but using a single set of calibration anchor points and uncertainties on them.
| Returns: |
|
|---|
| Raises: |
|
|---|
Source code in mass2/calibration/energy_calibration.py
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 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 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 204 205 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 251 252 253 254 255 256 257 258 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 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 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 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 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 | |
npts
property
The number of calibration anchor points.
__post_init__()
Check for inputs of unequal length. Check for monotone anchor points. Sort the input data by energy.
Source code in mass2/calibration/energy_calibration.py
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 | |
add_cal_point(ph, energy, name='', ph_error=None, e_error=None, replace=True)
Add a single energy calibration point.
Can call as .add_cal_point(ph, energy, name) or if the "energy" is a line name, then
.add_cal_point(ph, name) will find energy as energy=mass2.STANDARD_FEATURES[name].
Thus the following are equivalent:
cal = cal.add_cal_point(12345.6, 5898.801, "Mn Ka1")
cal = cal.add_cal_point(12456.6, "Mn Ka1")
ph must be in units of the self.ph_field and energy is in eV.
ph_error is the 1-sigma uncertainty on the pulse height. If None
(the default), then assign ph_error = ph/1000. e_error is the
1-sigma uncertainty on the energy itself. If None (the default), then
assign e_error=0.01 eV.
Careful! If you give a name that's already in the list, or you add an equivalent
energy but do NOT give a name, then this value replaces the previous one.
You can prevent overwriting (and instead raise an error) by setting replace=False.
Source code in mass2/calibration/energy_calibration.py
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 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 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | |
drop_one_errors(curvename=Curvetypes.LOGLOG, approximate=False, powerlaw=1.15)
For each calibration point, calculate the difference between the 'correct' energy and the energy predicted by creating a calibration without that point and using ph2energy to calculate the predicted energy
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in mass2/calibration/energy_calibration.py
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 | |
heuristic_samplepoints(anchors)
staticmethod
Given a set of calibration anchor points, return a few hundred sample points, reasonably spaced below, between, and above the anchor points.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in mass2/calibration/energy_calibration.py
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 | |
init(ph=None, energy=None, dph=None, de=None, names=None)
classmethod
Create an EnergyCalibrationMaker, filling in any missing requirements with empty arrays.
Source code in mass2/calibration/energy_calibration.py
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 | |
make_calibration(curvename=Curvetypes.LOGLOG, approximate=False, powerlaw=1.15, extra_info=None)
Create an energy calibration curve of the specified type.
| Parameters: |
|
|---|
| Returns: |
|
|---|
| Raises: |
|
|---|
Source code in mass2/calibration/energy_calibration.py
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 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 423 424 425 426 427 428 429 430 431 432 433 434 435 436 | |
make_calibration_gain(approximate=False, extra_info=None)
Create a calibration curve that is a spline in (pulse height/energy) vs pulse height.
Source code in mass2/calibration/energy_calibration.py
263 264 265 | |
make_calibration_invgain(approximate=False, extra_info=None)
Create a calibration curve that is a spline in (energy/pulse height) vs pulse height.
Source code in mass2/calibration/energy_calibration.py
267 268 269 | |
make_calibration_linear(approximate=False, addzero=False, extra_info=None)
Create a calibration curve that is a spline in energy vs pulse height. If addzero include a (0,0) anchor point.
Source code in mass2/calibration/energy_calibration.py
275 276 277 278 279 280 | |
make_calibration_loggain(approximate=False, extra_info=None)
Create a calibration curve that is a spline in log(pulse height/energy) vs pulse height.
Source code in mass2/calibration/energy_calibration.py
271 272 273 | |
make_calibration_loglog(approximate=False, powerlaw=1.15, extra_info=None)
Create a calibration curve that is a spline in log(energy) vs log(pulse height).
Source code in mass2/calibration/energy_calibration.py
257 258 259 260 261 | |
remove_cal_point_energy(energy, de)
Remove cal points at energies within ±de of energy. Return a new maker.
Source code in mass2/calibration/energy_calibration.py
143 144 145 146 147 148 149 | |
remove_cal_point_name(name)
Remove calibration point named name. Return a new maker.
Source code in mass2/calibration/energy_calibration.py
128 129 130 131 | |
remove_cal_point_prefix(prefix)
This removes all cal points whose name starts with prefix. Return a new maker.
Source code in mass2/calibration/energy_calibration.py
133 134 135 136 137 138 139 140 141 | |
Fluorescence line shapes
fluorescence_lines.py
Tools for fitting and simulating X-ray fluorescence lines.
AmplitudeType
Bases: Enum
AmplitudeType: which form of amplitude is used in the reference data.
Source code in mass2/calibration/fluorescence_lines.py
76 77 78 79 80 81 | |
LineshapeReference
dataclass
Description of our source of information on a line shape. Might be a reference to the literature, or notes on conversations. They are stored in a YAML file mass2/data/fluorescence_line_references.yaml
Source code in mass2/calibration/fluorescence_lines.py
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 423 424 425 426 427 428 429 430 431 432 433 | |
__str__()
The citation string for this reference.
Source code in mass2/calibration/fluorescence_lines.py
427 428 429 430 431 432 433 | |
load(filename=None)
classmethod
Load the reference comments from a YAML file. If filename is None, load the default file in mass2/data.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in mass2/calibration/fluorescence_lines.py
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 | |
SpectralLine
dataclass
An abstract base class for modeling spectral lines as a sum of Voigt profiles (i.e., Gaussian-convolved Lorentzians).
Call SpectralLine.addline(...) to create a new instance.
The API follows scipy.stats.stats.rv_continuous and is kind of like rv_frozen.
Calling this object with an argument evalutes the pdf at the argument, it does not
return an rv_frozen. So far, we ony define rvs and pdf.
Source code in mass2/calibration/fluorescence_lines.py
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 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 204 205 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 251 252 253 254 255 256 257 258 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 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 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 | |
cumulative_amplitudes
property
Cumulative sum of the Lorentzian integral intensities.
lorentz_amplitude
cached
property
Return (and cache) computed Lorentzian peak heights of the components.
lorentzian_integral_intensity
cached
property
Return (and cache) computed integrated intensities of the Lorentzian components.
normalized_lorentzian_integral_intensity
cached
property
Return (and cache) computed integrated intensities of the Lorentzian components, normalized so sum=1.
peak_energy
cached
property
Find the peak energy of the line shape assuming ideal instrument resolution.
reference
property
The full comment and/or citation for the reference data.
shortname
property
A short name for the line, suitable for use as a dictionary key.
__call__(x, instrument_gaussian_fwhm)
Make the class callable, returning the same value as the self.pdf method.
Source code in mass2/calibration/fluorescence_lines.py
152 153 154 | |
__repr__()
String representation of the SpectralLine.
Source code in mass2/calibration/fluorescence_lines.py
264 265 266 | |
addline(element, linetype, material, reference_short, reference_plot_instrument_gaussian_fwhm, nominal_peak_energy, energies, lorentzian_fwhm, reference_amplitude, reference_amplitude_type, ka12_energy_diff=None, position_uncertainty=np.nan, intrinsic_sigma=0, reference_measurement_type=None, is_default_material=True, allow_replacement=True)
classmethod
Add a new SpectralLine to the mass2.fluorescence_lines.spectra dictionary, and as a variable in this module.
Source code in mass2/calibration/fluorescence_lines.py
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 | |
components(x, instrument_gaussian_fwhm)
List of spectrum components as a function of
Source code in mass2/calibration/fluorescence_lines.py
166 167 168 169 170 171 172 173 | |
fitter()
Generate a GenericLineModel instance for fitting this SpectralLine.
Source code in mass2/calibration/fluorescence_lines.py
279 280 281 282 283 284 | |
minimum_fwhm(instrument_gaussian_fwhm)
for the narrowest lorentzian in the line model, calculate the combined fwhm including the lorentzian, intrinstic_sigma, and instrument_gaussian_fwhm
Source code in mass2/calibration/fluorescence_lines.py
286 287 288 289 290 | |
model(has_linear_background=True, has_tails=False, prefix='', qemodel=None)
Generate a LineModel instance from a SpectralLine
Source code in mass2/calibration/fluorescence_lines.py
268 269 270 271 272 273 274 275 276 277 | |
pdf(x, instrument_gaussian_fwhm)
Spectrum (units of fraction per eV) as a function of
Source code in mass2/calibration/fluorescence_lines.py
156 157 158 159 160 161 162 163 164 | |
plot(x=None, instrument_gaussian_fwhm=0, axis=None, components=True, label=None, setylim=True, color=None)
Plot the spectrum. x - np array of energy in eV to plot at (sensible default) axis - axis to plot on (default creates new figure) components - True plots each voigt component in addition to the spectrum label - a string to label the plot with (optional)
Source code in mass2/calibration/fluorescence_lines.py
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 204 205 206 207 208 209 210 211 | |
plot_like_reference(axis=None)
Plot the spectrum to match the instrument resolution used in the reference data publication, if known.
Source code in mass2/calibration/fluorescence_lines.py
213 214 215 216 217 218 219 220 | |
quick_monochromatic_line(name, energy, lorentzian_fwhm, intrinsic_sigma=0.0)
classmethod
Create a quick monochromatic line. Intended for use in calibration when we know a line energy, but not a lineshape model. Returns and instrance of SpectralLine with most fields having contents like "unknown: quick_line". The line will have a single lorentzian element with the given energy, fwhm, and intrinsic_sigma values.
Source code in mass2/calibration/fluorescence_lines.py
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 | |
rvs(size, instrument_gaussian_fwhm, rng=None)
The CDF and PPF (cumulative distribution and percentile point functions) are hard to compute. But it's easy enough to generate the random variates themselves, so we override that method.
Source code in mass2/calibration/fluorescence_lines.py
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 | |
LineEnergies()
A dictionary to know a lot of x-ray fluorescence line energies, based on Deslattes' database.
It is built on facts from mass2.calibration.nist_xray_database module.
It is a dictionary from peak name to energy, with several alternate names for the lines:
E = Energies() print E["MnKAlpha"] print E["MnKAlpha"], E["MnKA"], E["MnKA1"], E["MnKL3"]
Source code in mass2/calibration/fluorescence_lines.py
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 | |
plot_all_spectra(maxplots=10)
Makes plots showing the line shape and component parts for some lines. Intended to replicate plots in the literature giving spectral lineshapes.
Source code in mass2/calibration/fluorescence_lines.py
1632 1633 1634 1635 1636 1637 1638 | |
Implements MLEModel, CompositeMLEModel, GenericLineModel
CompositeMLEModel
Bases: MLEModel, CompositeModel
A version of lmfit.CompositeModel that uses Maximum Likelihood weights in place of chisq, as described in: doi:10.1007/s10909-014-1098-4 "Maximum-Likelihood Fits to Histograms for Improved Parameter Estimation"
Source code in mass2/calibration/line_models.py
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 | |
__add__(other)
Sum of two models
Source code in mass2/calibration/line_models.py
236 237 238 | |
__mul__(other)
Product of two models
Source code in mass2/calibration/line_models.py
244 245 246 | |
__sub__(other)
Difference of two models
Source code in mass2/calibration/line_models.py
240 241 242 | |
__truediv__(other)
Ratio of two models
Source code in mass2/calibration/line_models.py
248 249 250 | |
GenericLineModel
Bases: MLEModel
A generic line model for fitting spectral lines.
Source code in mass2/calibration/line_models.py
253 254 255 256 257 258 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 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 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 | |
__init__(spect, independent_vars=['bin_centers'], prefix='', nan_policy='raise', has_linear_background=True, has_tails=False, qemodel=None, **kwargs)
Initialize a GenericLineModel
| Parameters: |
|
|---|
| Returns: |
|
|---|
| Raises: |
|
|---|
Source code in mass2/calibration/line_models.py
256 257 258 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 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 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 | |
guess(data, bin_centers, dph_de, **kwargs)
Guess values for the peak_ph, integral, and background.
Source code in mass2/calibration/line_models.py
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 | |
LineModelResult
Bases: ModelResult
like lmfit.model.Model result, but with some convenient plotting functions for line spectra fits
Source code in mass2/calibration/line_models.py
412 413 414 415 416 417 418 419 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 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 | |
plotm(ax=None, title=None, xlabel=None, ylabel=None)
plot the data, the fit, and annotate the plot with the parameters
Source code in mass2/calibration/line_models.py
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 | |
set_label_hints(binsize, ds_shortname, attr_str, unit_str, cut_hint, states_hint='')
Set hints for axis labels and title for plotm().
Source code in mass2/calibration/line_models.py
458 459 460 461 462 463 464 465 466 467 468 | |
MLEModel
Bases: Model
A version of lmfit.Model that uses Maximum Likelihood weights in place of chisq, as described in: doi:10.1007/s10909-014-1098-4 "Maximum-Likelihood Fits to Histograms for Improved Parameter Estimation"
Source code in mass2/calibration/line_models.py
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 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 204 205 206 207 208 209 210 211 212 | |
__add__(other)
Sum of two models
Source code in mass2/calibration/line_models.py
156 157 158 | |
__mul__(other)
Product of two models
Source code in mass2/calibration/line_models.py
164 165 166 | |
__repr__()
Return representation of Model.
Source code in mass2/calibration/line_models.py
139 140 141 | |
__sub__(other)
Difference of two models
Source code in mass2/calibration/line_models.py
160 161 162 | |
__truediv__(other)
Ratio of two models
Source code in mass2/calibration/line_models.py
168 169 170 | |
fit(*args, minimum_bins_per_fwhm=3, **kwargs)
as lmfit.Model.fit except 1. the default method is "least_squares because it gives error bars more often at 1.5-2.0X speed penalty 2. supports "leastsq_refit" which uses "leastsq" to fit, but if there are no error bars, refits with "least_squares" call result.set_label_hints(...) then result.plotm() for a nice plot.
Source code in mass2/calibration/line_models.py
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | |
Calibration algorithms
This file is intended to include algorithms that could be generally useful for calibration. Mostly they are pulled out of the former mass.calibration.young module.
FailedToGetModelException
Bases: Exception
Exception raised when get_model() fails to find a model for a line
Source code in mass2/calibration/algorithms.py
176 177 178 179 | |
build_fit_ranges(line_names, excluded_line_names, approx_ecal, fit_width_ev)
Returns a list of (lo,hi) where lo and hi have units of energy of ranges to fit in for each energy in line_names.
Args: line_names (list[str or float]): list or line names or energies excluded_line_names (list[str or float]): list of line_names or energies to avoid when making fit ranges approx_ecal: an EnergyCalibration object containing an approximate calibration fit_width_ev (float): full size in eV of fit ranges
Source code in mass2/calibration/algorithms.py
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 | |
build_fit_ranges_ph(line_names, excluded_line_names, approx_ecal, fit_width_ev)
Call build_fit_ranges() to get (lo,hi) for fitranges in energy units, then convert to ph using approx_ecal
Source code in mass2/calibration/algorithms.py
122 123 124 125 126 127 128 129 130 131 132 133 134 | |
find_local_maxima(pulse_heights, gaussian_fwhm)
Smears each pulse by a gaussian of gaussian_fhwm and finds local maxima, returns a list of their locations in pulse_height units (sorted by number of pulses in peak) AND their peak values as: (peak_locations, peak_intensities)
Args: pulse_heights (np.array(dtype=float)): a list of pulse heights (eg p_filt_value) gaussian_fwhm = fwhm of a gaussian that each pulse is smeared with, in same units as pulse heights
Source code in mass2/calibration/algorithms.py
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 | |
find_opt_assignment(peak_positions, line_names, nextra=2, nincrement=3, nextramax=8, maxacc=0.015)
Tries to find an assignment of peaks to line names that is reasonably self consistent and smooth
Args: peak_positions (np.array(dtype=float)): a list of peak locations in arb units, e.g. p_filt_value units line_names (list[str or float)]): a list of calibration lines either as number (which is energies in eV), or name to be looked up in STANDARD_FEATURES nextra (int): the algorithm starts with the first len(line_names) + nextra peak_positions nincrement (int): each the algorithm fails to find a satisfactory peak assignment, it uses nincrement more lines nextramax (int): the algorithm stops incrementint nextra past this value, instead failing with a ValueError saying "no peak assignment succeeded" maxacc (float): an empirical number that determines if an assignment is good enough. The default number works reasonably well for tupac data
Source code in mass2/calibration/algorithms.py
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 | |
get_model(lineNameOrEnergy, has_linear_background=True, has_tails=False, prefix='')
Get a GenericLineModel for a line, given either a line name or energy in eV
| Parameters: |
|
|---|
| Returns: |
|
|---|
| Raises: |
|
|---|
Source code in mass2/calibration/algorithms.py
182 183 184 185 186 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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 | |
line_names_and_energies(line_names)
Given a list of line_names, return (names, energies) in eV.
Can also accept energies in eV directly and return (names, energies).
Source code in mass2/calibration/algorithms.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | |
multifit(ph, line_names, fit_lo_hi, binsize_ev, slopes_de_dph, hide_deprecation=False)
Args: ph (np.array(dtype=float)): list of pulse heights line_names: names of calibration lines fit_lo_hi (list[list[float]]): a list of (lo,hi) with units of ph, used as edges of histograms for fitting binsize_ev (list[float]): list of binsizes in eV for calibration lines slopes_de_dph (list[float]): - list of slopes de_dph (e in eV) hide_deprecation: whether to suppress deprecation warnings
Source code in mass2/calibration/algorithms.py
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 | |
singlefit(ph, name, lo, hi, binsize_ph, approx_dP_dE)
Performs a fit to a single line in pulse height units
| Parameters: |
|
|---|
| Returns: |
|
|---|
| Raises: |
|
|---|
Source code in mass2/calibration/algorithms.py
269 270 271 272 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 | |
Highly charged ions
hci_lines.py
Uses pickle file containing NIST ASD levels data to generate some commonly used HCI lines in mass. Meant to be a replacement for _highly_charged_ion_lines.py, which hard codes in line parameters.
The pickle file can be gzip-compressed, provided the compressed filename ends with ".gz".
February 2020 Paul Szypryt
NIST_ASD
Class for working with a pickled atomic spectra database
Source code in mass2/calibration/hci_lines.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 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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | |
__init__(pickleFilename=None)
Loads ASD pickle file (optionally gzipped)
| Parameters: |
|
|---|
Source code in mass2/calibration/hci_lines.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | |
getAvailableElements()
Returns a list of all available elements from the ASD pickle file
Source code in mass2/calibration/hci_lines.py
52 53 54 55 | |
getAvailableLevels(element, spectralCharge, requiredConf=None, requiredTerm=None, requiredJVal=None, maxLevels=None, units='eV', getUncertainty=True)
For a given element and spectral charge state, return a dict of all known levels from the ASD pickle file
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in mass2/calibration/hci_lines.py
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 | |
getAvailableSpectralCharges(element)
For a given element, returns a list of all available charge states from the ASD pickle file
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in mass2/calibration/hci_lines.py
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | |
getSingleLevel(element, spectralCharge, conf, term, JVal, units='eV', getUncertainty=True)
Return the level data for a fully defined element, charge state, conf, term, and JVal.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in mass2/calibration/hci_lines.py
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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | |
add_H_like_lines_from_asd(asd, element, maxLevels=None)
Add all known H-like lines for a given element from the ASD database
Source code in mass2/calibration/hci_lines.py
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 | |
add_He_like_lines_from_asd(asd, element, maxLevels=None)
Add all known He-like lines for a given element from the ASD database
Source code in mass2/calibration/hci_lines.py
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | |
add_hci_line(element, spectr_ch, line_identifier, energies, widths, ratios, nominal_peak_energy=None)
Add a single HCI line to the fluorescence_lines database
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in mass2/calibration/hci_lines.py
195 196 197 198 199 200 201 202 203 204 205 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 | |
hci_models.py
Some useful methods for initializing GenericLineModel and CompositeMLEModel objects applied to HCI lines.
June 2020 Paul Szypryt
add_bg_model(generic_model, vary_slope=False)
Adds a LinearBackgroundModel to a generic lmfit model
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in mass2/calibration/hci_models.py
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | |
initialize_HLike_2P_model(element, conf, has_linear_background=False, has_tails=False, vary_amp_ratio=False)
Initializes H-like 2P models consisting of J=1/2 and J=3/2 lines
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in mass2/calibration/hci_models.py
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 | |
initialize_HeLike_complex_model(element, has_linear_background=False, has_tails=False, additional_line_names=[])
Initializes 1s2s,2p He-like complexes for a given element.
By default, uses only the 1s.2s 3S J=1, 1s.2p 3P J=1, and 1s.2p 1P J=1 lines.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in mass2/calibration/hci_models.py
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 178 179 180 181 182 183 184 185 186 187 188 189 190 | |
initialize_hci_composite_model(composite_name, individual_models, has_linear_background=False, peak_component_name=None)
Initializes composite lmfit model from the sum of input models
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in mass2/calibration/hci_models.py
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 | |
initialize_hci_line_model(line_name, has_linear_background=False, has_tails=False)
Initializes a single lorentzian HCI line model. Reformats line_name to create a lmfit valid prefix.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in mass2/calibration/hci_models.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | |
models(has_linear_background=False, has_tails=False, vary_Hlike_amp_ratio=False, additional_Helike_complex_lines=[])
Generates some commonly used HCI line models that can be used for energy calibration, etc.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in mass2/calibration/hci_models.py
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 251 252 253 254 255 256 257 258 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 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 | |
Bookkeeping
import_asd.py
Tool for converting a NIST ASD levels sql dump into a pickle file
February 2020 Paul Szypryt
parseLine(energyLevelsDict, fieldNamesDict, formattedLine)
Parse a line from the ASD sql dump and add it to the energyLevelsDict
| Parameters: |
|
|---|
Source code in mass2/calibration/import_asd.py
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 | |
write_asd_pickle(inputFilename, outputFilename)
Write the levels from a NIST Atomic Spectra Database SQL dump to a pickle file
| Parameters: |
|
|---|
Source code in mass2/calibration/import_asd.py
18 19 20 21 22 23 24 25 26 27 28 29 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 | |
nist_xray_database
Download the NIST x-ray line database from the website, and parse the downloaded data into useable form.
For loading a file (locally, from disk) and plotting some information: * NISTXrayDBFile * plot_line_uncertainties
For updating the data files: * NISTXrayDBRetrieve * GetAllLines
Basic usage (assuming you put the x-ray files in ${MASS_HOME}/mass2/calibration/nist_xray_data.dat):
J. Fowler, NIST February 2014
NISTXrayDBFile
A NIST X-ray database file, loaded from disk.
Source code in mass2/calibration/nist_xray_database.py
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 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 204 205 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 | |
__getitem__(key)
Get a line by its full name, e.g., "Fe KL3", or by a nickname, e.g., "Fe Kalpha1".
| Parameters: |
|
|---|
| Returns: |
|
|---|
| Raises: |
|
|---|
Source code in mass2/calibration/nist_xray_database.py
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 | |
__init__(*filenames)
Initialize the database from 1 or more
Source code in mass2/calibration/nist_xray_database.py
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 | |
get_lines_by_type(linetype)
Return a tuple containing all lines of a certain type, e.g., "KL3". See self.LINE_NICKNAMES for some known line "nicknames".
Source code in mass2/calibration/nist_xray_database.py
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | |
NISTXrayLine
A single line from the NIST X-ray database.
Source code in mass2/calibration/nist_xray_database.py
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 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 286 287 288 | |
__init__(textline, column_defs=None)
Initialize a NISTXrayLine from a line of text found in the NIST x-ray database file.
| Parameters: |
|
|---|
Source code in mass2/calibration/nist_xray_database.py
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 | |
__repr__()
The code representation of the line
Source code in mass2/calibration/nist_xray_database.py
286 287 288 | |
__str__()
The user-friendly string representation of the line
Source code in mass2/calibration/nist_xray_database.py
282 283 284 | |
plot_line_energies()
Plot the energies of some common families of lines from the NIST X-ray database.
Source code in mass2/calibration/nist_xray_database.py
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 | |
plot_line_uncertainties()
Plot the uncertainties of some common families of lines from the NIST X-ray database.
Source code in mass2/calibration/nist_xray_database.py
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 | |
Materials transmission
Models for X-ray filter and detector efficiency.
Filter
dataclass
Represent a single material layer in a FilterStack
Source code in mass2/materials/efficiency_models.py
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 178 179 180 181 | |
__repr__()
Return a string representation of the Filter object.
Source code in mass2/materials/efficiency_models.py
133 134 135 136 137 138 139 140 | |
get_efficiency(xray_energies_eV, uncertain=False)
Return the efficiency of this Filter at the given x-ray energies.
Source code in mass2/materials/efficiency_models.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | |
newfilter(name, material, area_density_g_per_cm2=None, thickness_nm=None, density_g_per_cm3=None, fill_fraction=ufloat(1, 1e-08), absorber=False)
classmethod
Create a Filter from the given parameters, filling in defaults as needed.
Source code in mass2/materials/efficiency_models.py
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 178 179 180 181 | |
FilterStack
dataclass
Represent a sequence of named materials
Source code in mass2/materials/efficiency_models.py
26 27 28 29 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 101 | |
__call__(xray_energies_eV, uncertain=False)
Equivalent to get_efficiency.
Source code in mass2/materials/efficiency_models.py
72 73 74 | |
__repr__()
Return a string representation of the FilterStack object.
Source code in mass2/materials/efficiency_models.py
95 96 97 98 99 100 101 | |
add(film)
Add a Filter or FilterStack to this FilterStack.
Source code in mass2/materials/efficiency_models.py
33 34 35 | |
add_filter(name, material, area_density_g_per_cm2=None, thickness_nm=None, density_g_per_cm3=None, fill_fraction=ufloat(1, 1e-08), absorber=False)
Create and add a Filter layer to this FilterStack.
Source code in mass2/materials/efficiency_models.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | |
get_efficiency(xray_energies_eV, uncertain=False)
Return the overall efficiency of this FilterStack at the given x-ray energies.
Source code in mass2/materials/efficiency_models.py
60 61 62 63 64 65 66 67 68 69 70 | |
plot_efficiency(xray_energies_eV, ax=None)
Plot the efficiency of this FilterStack and its components.
Source code in mass2/materials/efficiency_models.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | |
AlFilmWithOxide(name, Al_thickness_nm, Al_density_g_per_cm3=None, num_oxidized_surfaces=2, oxide_density_g_per_cm3=None)
Create a Filter made of an alumninum film with oxides on one or both surfaces
Args: name: name given to filter object, e.g. '50K Filter'. Al_thickness_nm: thickness, in nm, of Al film Al_density_g_per_cm3: Al film density, in g/cm3, defaults to xraydb value num_oxidized_surfaces: Number of film surfaces that contain a native oxide, default 2 oxide_density_g_per_cm3: Al2O3 oxide density, in g/cm3, defaults to bulk xraydb value
Source code in mass2/materials/efficiency_models.py
184 185 186 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 214 215 216 217 218 219 | |
AlFilmWithPolymer(name, Al_thickness_nm, polymer_thickness_nm, Al_density_g_per_cm3=None, num_oxidized_surfaces=1, oxide_density_g_per_cm3=None, polymer_density_g_per_cm3=None)
Create a Filter made of an alumninum film with polymer backing
Args: name: name given to filter object, e.g. '50K Filter'. Al_thickness_nm: thickness, in nm, of Al film polymer_thickness_nm: thickness, in nm, of filter backside polymer Al_density_g_per_cm3: Al film density, in g/cm3, defaults to xraydb value num_oxidized_surfaces: Number of film surfaces that contain a native oxide, default 2 oxide_density_g_per_cm3: Al2O3 oxide density, in g/cm3, defaults to bulk xraydb value polymer_density_g_per_cm3: Polymer density, in g/cm3, defaults to Kapton
Source code in mass2/materials/efficiency_models.py
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 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 | |
LEX_HT(name)
Create an Al film with polymer and stainless steel backing.
Models the LEX-HT vacuum window.
Args: name: name given to filter object, e.g. '50K Filter'.
Source code in mass2/materials/efficiency_models.py
272 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 | |
get_filter_stacks_dict()
Create a dictionary with a few examples of FilterStack objects
| Returns: |
|
|---|
Source code in mass2/materials/efficiency_models.py
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 | |
Math and statistics functions
These live in mass2.mathstat.
Entropy
entropy.py
Estimates of the distribution entropy computed using kernel-density estimates of the distribution.
laplace_entropy(x, w=1.0)- Compute the entropy H(p) of data setxwhere the kernel used to estimate p from x is the Laplace kernel k(x) \propto exp(-abs(x-x0)/w).laplace_cross_entropy(x, y, w=1.0)- Compute the cross entropy of q from p, where q and p are the kernel-density estimates taken from data setyand data setx, and where the kernel is the Laplace kernel.KL_divergence(x, y, w=1.0)- Compute the Kullback-Leibler Divergence of data setyfromx, where the kernel is the Laplace kernel.
The K-L divergence of Q(x) from P(x) is defined as the integral over the full x domain of P(x) log[P(x)/Q(x)].
This equals the cross-entropy H(P,Q) - H(P). Note that cross-entropy and K-L divergence
are not symmetric with respect to reversal of x and y.
laplace_KL_divergence(x, y, w=1.0, approx_mode='size')
Compute the Kullback-Leibler divergence of data set y from data set x.
Use kernel-density estimation, where the kernel is the Laplace kernel k(x) \propto exp(-abs(x-x0)/w).
The approx_mode can be one of:
exact The exact integral is computed (can take ~0.25 sec per 10^6 values).
approx The integral is approximated by histogramming the data, smoothing
that, and using Simpson's rule on the PDF samples that result.
size Uses "approx" if len(x)+len(y)>200000, or "exact" otherwise.
Source code in mass2/mathstat/entropy.py
254 255 256 257 258 259 260 261 262 263 264 265 266 | |
laplace_cross_entropy(x, y, w=1.0, approx_mode='size')
laplace_cross_entropy(x, y, w: float = 1.0, approx_mode="size")
Compute the cross-entropy of data set x from data set y, where the
kernel for x is the Laplace kernel k(x) \propto exp(-abs(x-x0)/w).
The kernel for the y data is the piecewise-constant (top-hat) kernel. We choose this because a Laplace kernel for y led to possible divergences when the y-distribtion q is exceedingly small, but the x-distribution p nevertheless is non-zero because of a random x-value lying far from any random y-values. The constant kernel is given a non-zero floor value, so that q is never so small as to make any x-value impossible.
Args: x (array): One vector of data. y (array): The other vector of data. w (double): The width (exponential scale length) of the Laplace distribution to be used in kernel-density estimation. approx_mode (string): How to balance execution speed and accuracy (default "size").
The approx_mode can be one of:
exact The exact integral is computed (can take ~0.25 sec per 10^6 values).
approx The integral is approximated by histogramming the data, smoothing
that, and using Simpson's rule on the PDF samples that result.
size Uses "approx" if len(x)+len(y)>200000, or "exact" otherwise.
Source code in mass2/mathstat/entropy.py
269 270 271 272 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 | |
laplace_cross_entropy_approx(x, y, w=1.0)
Approximate the cross-entropy H(P, Q) between two empirical distributions P and Q,
where P is estimated from data x and Q from data y using Laplace kernel-density
estimation and binned histograms.
This method uses histograms and convolution with a Laplace kernel to estimate the probability distributions, then computes the cross-entropy using numerical integration.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Notes
- This is an approximate method, suitable for large data sets.
- The Laplace kernel is defined as k(x) ∝ exp(-abs(x-x0)/w).
- Uses Simpson's rule for numerical integration.
Source code in mass2/mathstat/entropy.py
413 414 415 416 417 418 419 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 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 | |
laplace_cross_entropy_arrays(x, y)
Compute the cross-entropy H(P, Q) between two empirical distributions P and Q,
where P is estimated from data x using a Laplace kernel, and Q is estimated
from data y using a piecewise-constant (top-hat) kernel.
This function assumes both x and y are sorted and scaled by the kernel width.
The cross-entropy is computed exactly by integrating over all points where the
estimated densities change due to the presence of data points in x or y.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Notes
- The Laplace kernel is defined as k(x) ∝ exp(-abs(x-x0)/w).
- The Q distribution uses a top-hat kernel with a nonzero floor to avoid divergences.
- This function is intended for internal use; see
laplace_cross_entropyfor the public API.
Source code in mass2/mathstat/entropy.py
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 | |
laplace_entropy(x_in, w=1.0, approx_mode='size')
Compute the entropy of data set x where the kernel is the Laplace kernel,
$k(x) \propto$ exp(-abs(x-x0)/w).
| Parameters: |
|
|---|
| Returns: |
|
|---|
| Raises: |
|
|---|
Source code in mass2/mathstat/entropy.py
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 | |
laplace_entropy_approx(x, w=1.0)
Approximage the entropy of data set x with a binned histogram and the Laplace-distribution
kernel-density estimator of the probability distribtion.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in mass2/mathstat/entropy.py
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 | |
laplace_entropy_array(x, w=1.0)
Compute the entropy of data set x where the kernel is the Laplace kernel,
$k(x) \propto$ exp(-abs(x-x0)/w).
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in mass2/mathstat/entropy.py
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 | |
Fitting
mass2.mathstat.fitting
Model-fitting utilities.
Joe Fowler, NIST
fit_kink_model(x, y, kbounds=None)
Find the linear least-squares solution for a kinked-linear model.
The model is f(x) = a+b(x-k) for x
Given k, the model is linear in the other parameters, which can thus be found exactly by linear algebra. The best value of k is found by use of the Bounded method of the sp.optimize.minimize_scalar() routine.
| Parameters: |
|
|---|
| Returns: |
|
|---|
| Raises: |
|
|---|
Examples:
x = np.arange(10, dtype=float) y = np.array(x) truek = 4.6 y[x>truek] = truek y += np.random.default_rng().standard_normal(len(x)).15 model, (kbest,a,b,c), X2 = fit_kink_model(x, y, kbounds=(3,6)) plt.clf() plt.plot(x, y, "or", label="Noisy data to be fit") xi = np.linspace(x[0], kbest, 200) xj = np.linspace(kbest, x[-1], 200) plt.plot(xi, a+b(xi-kbest), "--k", label="Best-fit kinked model") plt.plot(xj, a+c*(xj-kbest), "--k") plt.legend()
Source code in mass2/mathstat/fitting.py
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 | |
kink_model(k, x, y)
Compute a kinked-linear model on data {x,y} with kink at x=k.
The model is f(x) = a+b(x-k) for x
For a fixed k, the model is linear in the other parameters, whose linear least-squares values can thus be found exactly by linear algebra. This function computes them.
Returns (model_y, (a,b,c), X2) where: model_y is an array of the model y-values; (a,b,c) are the best-fit values of the linear parameters; X2 is the sum of square differences between y and model_y.
| Parameters: |
|
|---|
| Returns: |
|
|---|
| Raises: |
|
|---|
Source code in mass2/mathstat/fitting.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 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 | |
Interpolation
interpolate.py
Module mass2.mathstat.interpolate
Contains interpolations functions not readily available elsewhere.
CubicSpline - Perform an exact cubic spline through the data, with either specified slope at the end of the interval or 'natural boundary conditions' (y''=0 at ends).
GPRSpline - Create a smoothing spline based on the theory of Gaussian process regression.
Finds the curvature penalty by maximizing the Bayesian marginal likelihood.
Intended to supercede SmoothingSpline, but very similar. Differs in how the
curvature and data fidelity are balanced.
SmoothingSpline - Create a smoothing spline that does not exactly interpolate the data, but finds the cubic spline with lowest "curvature energy" among all splines that meet the maximum allowed value of chi-squared.
SmoothingSplineLog - Create a SmoothingSpline using the log of the x,y points.
NaturalBsplineBasis - A tool for expressing a spline basis using B-splines but also enforcing 'natural boundary conditions'.
Joe Fowler, NIST Created Feb 2014
CubicSpline
An exact cubic spline, with either a specified slope or 'natural boundary conditions' (y''=0) at ends of interval.
Note that the interface is similar to scipy.interpolate.InterpolatedUnivariateSpline, but the behavior is different. The scipy version will remove the 2nd and 2nd-to-last data points from the set of knots as a way of using the 2 extra degrees of freedom. This class instead sets the 1st or 2nd derivatives at the end of the interval to use the extra degrees of freedom.
This code is inspired by section 3.3. of Numerical Recipes, 3rd Edition.
Usage: x=np.linspace(4,12,20) y=(x-6)**2+np.random.standard_normal(20) cs = mass2.CubicSpline(x, y) plt.clf() plt.plot(x,y,'ok') xa = np.linspace(0,16,200) plt.plot(xa, cs(xa), 'b-')
Source code in mass2/mathstat/interpolate.py
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 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 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 | |
__call__(x, der=0)
Return the value of the cubic spline (or its derivative) at x.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in mass2/mathstat/interpolate.py
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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | |
__init__(x, y, yprime1=None, yprimeN=None)
Create an exact cubic spline representation for the function y(x).
| Parameters: |
|
|---|
Source code in mass2/mathstat/interpolate.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | |
variance(xtest)
Return a dummy estimate of the variance at points xtest.
Source code in mass2/mathstat/interpolate.py
201 202 203 | |
GPRSpline
Bases: CubicSpline
A callable object that performs a smoothing cubic spline operation
The smoothing spline is the cubic spline minimizing the "curvature energy" subject to a constraint that the maximum allowed chi-squared is equal to the number of data points. Here curvature energy is defined as the integral of the square of the second derivative from the lowest to the highest knots.
The value of sigmaf fixes the square root of the "function variance".
Small values of sigmaf correspond to large penalties on the curvature,
so they emphasize low curvature. Large sigmaf places emphasis on fidelity to
the data and will have relatively higher curvature (and a higher uncertainty on
the derived curve). Setting sigmaf=None (the default) will choose the value that
maximizes the Bayesian marginal likelihood of the data and is probably smart.
For further discussion, see Sections 2.2, 2.7, and 6.3 of Rasmussen, C. E., & Williams, K. I. (2006). Gaussian Processes for Machine Learning. Retrieved from http://www.gaussianprocess.org/gpml/chapters/
This object is very similar to SmoothingSpline in this module but is based on
Gaussian Process Regression theory. It improves on SmoothingSpline in that:
1. The curvature/data fidelity trade-off is chosen by more principaled, Bayesian means.
2. The uncertainty in the spline curve is estimated by GPR theory.
Source code in mass2/mathstat/interpolate.py
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 251 252 253 254 255 256 257 258 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 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 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 | |
__init__(x, y, dy, dx=None, sigmaf=None)
Set up the Gaussian Process Regression spline.
| Parameters: |
|
|---|
Source code in mass2/mathstat/interpolate.py
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 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 286 287 288 289 290 291 292 293 294 295 296 297 298 | |
best_sigmaf()
Return the sigmaf value that maximizes the marginal Bayesian likelihood.
Source code in mass2/mathstat/interpolate.py
300 301 302 303 304 305 306 307 | |
covariance(xtest)
Returns the covariance between function evaluations at the test points xtest.
Source code in mass2/mathstat/interpolate.py
357 358 359 360 361 362 363 364 365 366 367 368 369 | |
variance(xtest)
Returns the variance for function evaluations at the test points xtest.
This equals the diagonal of self.covariance(xtest), but for large test sets,
this method computes only the diagonal and should therefore be faster.
Source code in mass2/mathstat/interpolate.py
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 | |
NaturalBsplineBasis
Represent a cubic B-spline basis in 1D with natural boundary conditions.
That is, f''(x)=0 at the first and last knots. This constraint reduces the effective number of basis functions from (2+Nknots) to Nknots.
Usage: knots = [0,5,8,9,10,12] basis = NaturalBsplineBasis(knots) x = np.linspace(0, 12, 200) plt.clf() for id in range(len(knots)): plt.plot(x, basis(x, id))
Source code in mass2/mathstat/interpolate.py
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 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 452 453 454 455 456 | |
__call__(x, id, der=0)
Compute the Basis-spline at the points x for basis function id, or its derivative of degree der.
| Parameters: |
|
|---|
| Returns: |
|
|---|
| Raises: |
|
|---|
Source code in mass2/mathstat/interpolate.py
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 | |
__init__(knots)
Initialization requires only the list of knots.
Source code in mass2/mathstat/interpolate.py
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 | |
expand_coeff(beta)
Given coefficients of this length-Nk basis, return the coefficients needed by FITPACK, which are of length Nk+2.
Source code in mass2/mathstat/interpolate.py
451 452 453 454 455 456 | |
values_matrix(der=0)
Return matrix M where M_ij = value at knot i for basis function j. If der>0, then return the derivative of that order instead of the value.
Source code in mass2/mathstat/interpolate.py
445 446 447 448 449 | |
SmoothingSpline
A callable object that performs a smoothing cubic spline operation, using the NaturalBsplineBasis object for the basis representation of splines.
The smoothing spline is the cubic spline minimizing the "curvature energy" subject to a constraint that the maximum allowed chi-squared is equal to the number of data points. Here curvature energy is defined as the integral of the square of the second derivative from the lowest to the highest knots.
For a proof see Reinsch, C. H. (1967). "Smoothing by spline functions." Numerische Mathematik, 10(3), 177-183. http://doi.org/10.1007/BF02162161
Source code in mass2/mathstat/interpolate.py
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 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 | |
__call__(x, der=0)
Return the value of (the derth derivative of) the smoothing spline
at data points x.
Source code in mass2/mathstat/interpolate.py
608 609 610 611 | |
__eval(x, der=0, allow_extrapolate=True)
Return the value of (the derth derivative of) the smoothing spline
at data points x.
Source code in mass2/mathstat/interpolate.py
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 | |
__init__(x, y, dy, dx=None, maxchisq=None)
Smoothing spline for data {x,y} with errors {dy} on the y values and {dx} on the x values (or zero if not given).
If dx errors are given, a global quadratic fit is done to the data to estimate the local slope. If that's a poor choice, then you should combine your dx and dy errors to create a sensible single error list, and you should pass that in as dy.
maxchisq specifies the largest allowed value of chi-squared (the sum of the squares of the differences y_i-f(x_i), divided by the variance v_i). If not given, this defaults to the number of data values. When a (weighted) least squares fit of a line to the data meets the maxchisq constraint, then the actual chi-squared will be less than maxchisq.
Source code in mass2/mathstat/interpolate.py
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 | |
smooth(chisq=None)
Choose the value of the curve at the knots so as to achieve the smallest possible curvature subject to the constraint that the sum over all {x,y} pairs S = [(y-f(x))/dy]^2 <= chisq
Source code in mass2/mathstat/interpolate.py
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 | |
SmoothingSplineLog
A smoothing spline in log-log space.
Source code in mass2/mathstat/interpolate.py
614 615 616 617 618 619 620 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 | |
__call__(x, der=0)
Compute the log-log smoothing spline or its derivative at the points x.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in mass2/mathstat/interpolate.py
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 | |
__init__(x, y, dy, dx=None, maxchisq=None)
Set up a smoothing spline in log-log space.
| Parameters: |
|
|---|
| Raises: |
|
|---|
Source code in mass2/mathstat/interpolate.py
617 618 619 620 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 | |
k_spline(x, y)
Compute the spline covariance kernel, R&W eq 6.28.
Source code in mass2/mathstat/interpolate.py
206 207 208 209 | |
Power spectra
A class and functions to compute a power spectrum using some of the sophistications given in Numerical Recipes, including windowing and overlapping data segments.
Use the class PowerSpectrum in the case that you are compute-limited and PowerSpectrumOverlap in the case that you are data-limited. The latter uses k segments of data two segments at a time to make (k-1) estimates and makes fuller use of all data (except in the first and last segment).
Joe Fowler, NIST
October 13, 2010
Usage:
import power_spectrum as ps import pylab as plt N=1024 M=N/4 data=np.random.default_rng().standard_normal(N) spec = ps.PowerSpectrum(M, dt=1e-6) window = ps.hann(2M) for i in range(3): spec.addDataSegment(data[iM : (i+2)*M], window=window) plt.plot(spec.frequencies(), spec.spectrum())
Or you can use the convenience function that hides the class objects from you and simply returns a (frequency,spectrum) pair of arrays:
N=1024 data=np.random.default_rng().standard_normal(N) plt.clf() for i in (2,4,8,1): f,s = ps.computeSpectrum(data, segfactor=i, dt=1e-6, window=np.hanning) plt.plot(f, s)
Window choices are: bartlett - Triangle shape hann - Sine-squared hamming - 0.08 + 0.92*(sine-squared) welch - Parabolic None - Square (no windowing) *** - Any other vector of length 2m OR any callable accepting 2m as an argument and returning a sequence of that length.
Each window take an argument (n), the number of data points per segment. When using the PowerSpectrum or PowerSpectrumOverlap classes or the convenience function computeSpectrum, you have a choice. You can call the window and pass in the resulting vector, or you can pass in the callable function itself. It is allowed to use different windows on different data segments, though honestly that would be really weird.
PowerSpectrum
Object for accumulating power spectrum estimates from one or more data segments.
If you want to use multiple overlapping segments, use class PowerSpectrumOvelap.
Based on Num Rec 3rd Ed section 13.4
Source code in mass2/mathstat/power_spectrum.py
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 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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | |
__init__(m, dt=1.0)
Sets up to estimate PSD at m+1 frequencies (counting DC) given data segments of length 2m. Optional dt is the time step Delta
Source code in mass2/mathstat/power_spectrum.py
74 75 76 77 78 79 80 81 82 83 84 | |
addDataSegment(data, window=None)
Process a data segment of length 2m using the window function given. window can be None (square window), a callable taking the length and returning a sequence, or a sequence.
Source code in mass2/mathstat/power_spectrum.py
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 | |
addLongData(data, window=None)
Process a long vector of data as non-overlapping segments of length 2m.
Source code in mass2/mathstat/power_spectrum.py
125 126 127 128 129 130 131 132 | |
autocorrelation()
Return the autocorrelation (the DFT of this power spectrum)
Source code in mass2/mathstat/power_spectrum.py
147 148 149 | |
copy()
Return a copy of the object.
Handy when coding and you don't want to recompute everything, but you do want to update the method definitions.
Source code in mass2/mathstat/power_spectrum.py
86 87 88 89 90 91 92 93 | |
frequencies(nbins=None)
If
Source code in mass2/mathstat/power_spectrum.py
151 152 153 154 155 156 157 | |
plot(axis=None, arb_to_unit_scale_and_label=(1, 'arb'), sqrt_psd=True, **plotkwarg)
Plot the power spectrum (or its square root) on a log-log plot.
| Parameters: |
|
|---|
Source code in mass2/mathstat/power_spectrum.py
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | |
spectrum(nbins=None)
If
Source code in mass2/mathstat/power_spectrum.py
134 135 136 137 138 139 140 141 142 143 144 145 | |
PowerSpectrumOverlap
Bases: PowerSpectrum
Object for power spectral estimation using overlapping data segments.
User sends non-overlapping segments of length m, and they are processed in pairs of length 2m with overlap (except on the first and last segment).
Source code in mass2/mathstat/power_spectrum.py
194 195 196 197 198 199 200 201 202 203 204 205 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 | |
__init__(m, dt=1.0)
Sets up an object to accumulate a power spectrum estimate.
| Parameters: |
|
|---|
Source code in mass2/mathstat/power_spectrum.py
202 203 204 205 206 207 208 209 210 211 212 213 | |
addDataSegment(data, window=None)
Process a data segment of length m using window.
Source code in mass2/mathstat/power_spectrum.py
215 216 217 218 219 220 221 222 223 | |
addLongData(data, window=None)
Process a long vector of data as overlapping segments of length 2m.
Source code in mass2/mathstat/power_spectrum.py
225 226 227 228 229 230 231 232 233 234 235 236 | |
bartlett(n)
A Bartlett window (triangle shape) of length n
Source code in mass2/mathstat/power_spectrum.py
242 243 244 | |
computeSpectrum(data, segfactor=1, dt=None, window=None)
Convenience function to compute the power spectrum of a single data array.
Args:
Data for finding the spectrum
Returns: Either the PSD estimate as an array (non-negative frequencies only), OR the tuple (frequencies, PSD). The latter returns when
Source code in mass2/mathstat/power_spectrum.py
266 267 268 269 270 271 272 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 | |
demo(N=1024, window=np.hanning)
Plot a demonstration power spectrum with different segmentations.
| Parameters: |
|
|---|
Source code in mass2/mathstat/power_spectrum.py
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 | |
hamming(n)
A Hamming window (0.08 + 0.92*sine-squared) of length n
Source code in mass2/mathstat/power_spectrum.py
260 261 262 | |
hann(n)
A Hann window (sine-squared) of length n
Source code in mass2/mathstat/power_spectrum.py
252 253 254 255 256 257 | |
welch(n)
A Welch window (parabolic) of length n
Source code in mass2/mathstat/power_spectrum.py
247 248 249 | |
Robust statistics
mass2.mathstat.robust
Functions from the field of robust statistics.
Location estimators: bisquare_weighted_mean - Mean with weights given by the bisquare rho function. huber_weighted_mean - Mean with weights given by Huber's rho function. trimean - Tukey's trimean, the average of the median and the midhinge. shorth_range - Primarily a dispersion estimator, but location=True gives a (poor) location.
Dispersion estimators: median_abs_dev - Median absolute deviation from the median. shorth_range - Length of the shortest closed interval containing at least half the data. Qscale - Normalized Rousseeuw & Croux Q statistic, from the 25%ile of all 2-point distances.
Utility functions: high_median - Weighted median
Recommendations: For location, suggest the bisquare_weighted_mean with k=3.9*sigma, if you can make any reasonable guess as to the Gaussian-like width sigma. If not, trimean is a good second choice, though less efficient.
For dispersion, the Qscale is very efficient for nearly Gaussian data. The median_abs_dev is the most robust though less efficient. If Qscale doesn't work, then short_range is a good second choice.
Created on Feb 9, 2012 Rewritten with Numba Jan 23, 2025
@author: fowlerj
Qscale(x, sort_inplace=False)
Compute the robust estimator of scale Q of Rousseeuw and Croux using only O(n log n) memory and computations.
A naive implementation is O(n^2) in both memory and computations.
Args:
x: The data set, an unsorted sequence of values.
sort_inplace: Whether it is okay for the function to reorder the set
Q is defined as d_n * 2.2219 * {|xi-xj|; i<j}_k, where
{a}_k means the kth order-statistic of the set {a},
this set is that of the distances between all (n 2) possible pairs of data in {x}
n=# of observations in set {x},
k = (n choose 2)/4,
2.2219 makes Q consistent for sigma in normal distributions as n-->infinity,
and d_n is a correction factor to the 2.2219 when n is not large.
This function does apply the correction factors to make Q consistent with sigma for a Gaussian distribution.
Technique from C. Croux & P. Rousseeuw in Comp. Stat Vol 1 (1992) ed. Dodge & Whittaker, Heidelberg: Physica-Verlag pages 411-428. Available at ftp://ftp.win.ua.ac.be/pub/preprints/92/Timeff92.pdf
The estimator is further studied in Rousseeuw & Croux, J Am. Stat. Assoc 88 (1993), pp 1273-1283.
Source code in mass2/mathstat/robust.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 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 | |
bisquare_weighted_mean(x, k, center=None, tol=None)
The bisquare weighted mean of the data
Args: x (array): data values to be summarized k (number): give zero weight to values at least distance k from the weighted mean. center (number): an initial guess at the weighted mean. If None, then the data median will be used (default None). tol (number): tolerance on the estimator (see below; default None)
A sensible choice of
The answer is found iteratively, revised until it changes by less than
Data values a distance of more than
Source code in mass2/mathstat/robust.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 66 67 68 69 70 71 72 73 74 75 76 77 78 | |
high_median(x, weights=None, return_index=False)
Compute the weighted high median of data set x with weights
Returns: The smallest x[j] such that the sum of all weights for data with x[i] <= x[j] is strictly greater than half the total weight.
If return_index is True, then the chosen index is returned also as (highmed, index).
Source code in mass2/mathstat/robust.py
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | |
huber_weighted_mean(x, k, center=None, tol=None)
Huber's weighted mean of the data
Args: x (array): data values to be summarized k (number): give zero weight to values at least distance k from the weighted mean. center (number): an initial guess at the weighted mean. If None, then the data median will be used (default None). tol (number): tolerance on the estimator (see below; default None)
A sensible choice of
The answer is found iteratively, revised until it changes by less than
Data values a distance of more than
Source code in mass2/mathstat/robust.py
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 | |
median_abs_dev(x, normalize=False)
Median absolute deviation (from the median) of data vector.
Args: x (array): data to be summarized. normalize (bool): if True, then return MAD/0.675, which scaling makes the statistic consistent with the standard deviation for an asymptotically large sample of Gaussian deviates (default False).
Source code in mass2/mathstat/robust.py
134 135 136 137 138 139 140 141 142 143 144 145 146 147 | |
shorth_range(x, normalize=False, sort_inplace=False, location=False)
Returns the Shortest Half (shorth) Range, a robust estimator of dispersion.
The Shortest Half of a data set {x} means that closed interval [a,b] where (1) a and b are both elements of the data set, (2) at least half of the elements are in the closed interval, and (3) which minimizes the length of the closed interval (b-a). The shorth range is (b-a). See mass2.mathstat.robust.shorth_information for further explanation and references in the literature.
Args:
x (array): The data set under study. Must be a sequence of values.
normalize (bool): If False (default), then return the actual range b-a. If True, then the
range will be divided by 1.348960, which normalizes the range to be a consistent estimator
of the parameter sigma in the case of an exact Gaussian distribution. (A small correction
of order 1/N is applied, too, which mostly corrects for bias at modest values of the sample
size N.)
sort_inplace - Permit this function to reorder the data set
Returns:
shorth range if
In this, shorth mean is the mean of all samples in the closed range [a,b], and shorth center = (a+b)/2. Beware that both of these location estimators have the undesirable property that their asymptotic standard deviation improves only as N^(-1/3) rather than the more usual N^(-1/2). So it is not a very good idea to use them as location estimators. They are really only included here for testing just how useless they are.
Source code in mass2/mathstat/robust.py
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 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 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | |
trimean(x)
Return Tukey's trimean for a data set
If (q1,q2,q3) are the quartiles (i.e., the 25%ile, median, and 75 %ile), the trimean is (q1+q3)/4 + q2/2.
Source code in mass2/mathstat/robust.py
121 122 123 124 125 126 127 128 129 130 131 | |