API Reference
SANSFitter
The main class for SANS data fitting.
sans_fitter.sans_fitter.SANSFitter
A flexible SANS model fitter that works with any SasModels model.
Features: - Loads data from various file formats (CSV, XML, HDF5) - Model-agnostic: works with any model from SasModels library - Supports multiple fitting engines (BUMPS, LMFit) - User-friendly parameter management
Example
fitter = SANSFitter() fitter.load_data('my_sans_data.csv') fitter.set_model('cylinder') fitter.set_param('radius', value=20, min=1, max=100) fitter.set_param('length', value=400, min=10, max=1000) result = fitter.fit(engine='bumps') fitter.plot_results()
Source code in src/sans_fitter/sans_fitter.py
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 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 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 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 | |
load_data(filename)
Load SANS data from a file.
Supports CSV, XML, and HDF5 formats through sasdata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Path to the data file |
required |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the file doesn't exist |
ValueError
|
If the data cannot be loaded or is invalid |
Source code in src/sans_fitter/sans_fitter.py
set_model(model_name, platform='cpu')
Set the SANS model to use for fitting.
This resets any active structure factor to ensure a clean state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_name
|
str
|
Name of the model from SasModels (e.g., 'cylinder', 'sphere') |
required |
platform
|
str
|
Computation platform ('cpu' or 'opencl') |
'cpu'
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the model name is not valid |
Source code in src/sans_fitter/sans_fitter.py
set_structure_factor(structure_factor_name, radius_effective_mode='unconstrained')
Apply a structure factor to the current model.
This creates a product model (form_factor * structure_factor) to account for inter-particle interactions in concentrated systems.
Supported structure factors: - 'hardsphere': Hard sphere structure factor (Percus-Yevick closure) - 'hayter_msa': Hayter-Penfold rescaled MSA for charged spheres - 'squarewell': Square well potential - 'stickyhardsphere': Sticky hard sphere (Baxter model)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
structure_factor_name
|
str
|
Name of the structure factor (e.g., 'hardsphere') |
required |
radius_effective_mode
|
str
|
How to handle the effective radius. - 'unconstrained': 'radius_effective' is a separate fitting parameter. - 'link_radius': 'radius_effective' is constrained to the form factor's 'radius'. |
'unconstrained'
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If no form factor model is set, or if the structure factor is invalid |
Source code in src/sans_fitter/sans_fitter.py
get_params()
set_param(name, value=None, min=None, max=None, vary=None)
Configure a model parameter for fitting.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Parameter name |
required |
value
|
Optional[float]
|
Initial value (optional) |
None
|
min
|
Optional[float]
|
Minimum bound (optional) |
None
|
max
|
Optional[float]
|
Maximum bound (optional) |
None
|
vary
|
Optional[bool]
|
Whether to vary during fit (optional) |
None
|
Raises:
| Type | Description |
|---|---|
KeyError
|
If parameter name doesn't exist for the current model |
Source code in src/sans_fitter/sans_fitter.py
fit(engine='bumps', method=None, **kwargs)
Perform the fit using the specified engine.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
engine
|
Literal['bumps', 'lmfit']
|
Fitting engine ('bumps' or 'lmfit') |
'bumps'
|
method
|
Optional[str]
|
Optimization method (engine-specific) - BUMPS: 'amoeba', 'lm', 'newton', 'de' (default: 'amoeba') - LMFit: 'leastsq', 'least_squares', 'differential_evolution', etc. |
None
|
**kwargs
|
Any
|
Additional arguments passed to the fitting engine |
{}
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with fit results including chi-squared and parameter values |
Raises:
| Type | Description |
|---|---|
ValueError
|
If data or model not loaded, or invalid engine |
Source code in src/sans_fitter/sans_fitter.py
plot_results(show_residuals=True, log_scale=True)
Plot experimental data and fitted model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
show_residuals
|
bool
|
If True, show residuals in a separate panel |
True
|
log_scale
|
bool
|
If True, use log scale for both axes |
True
|
Returns:
| Type | Description |
|---|---|
Figure
|
Plotly Figure object |
Source code in src/sans_fitter/sans_fitter.py
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 | |
save_results(filename)
Save fit results to a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Output file path (CSV format) |
required |
Source code in src/sans_fitter/sans_fitter.py
supports_polydispersity()
Check if current model has polydisperse parameters.
Returns:
| Type | Description |
|---|---|
bool
|
True if model supports polydispersity, False otherwise |
Source code in src/sans_fitter/sans_fitter.py
get_polydisperse_parameters()
Get list of polydisperse parameter names.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of parameter names that support polydispersity |
set_pd_param(param_name, pd_width=None, pd_n=None, pd_nsigma=None, pd_type=None, vary=None)
Configure polydispersity for a parameter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
param_name
|
str
|
Name of the base parameter (e.g., 'radius') |
required |
pd_width
|
Optional[float]
|
Polydispersity width (relative, 0.0 = monodisperse) |
None
|
pd_n
|
Optional[int]
|
Number of Gaussian quadrature points (default: 35) |
None
|
pd_nsigma
|
Optional[float]
|
Number of sigmas to include (default: 3.0) |
None
|
pd_type
|
Optional[str]
|
Distribution type ('gaussian', 'rectangle', 'lognormal', 'schulz', 'boltzmann') |
None
|
vary
|
Optional[bool]
|
Whether to vary the pd_width during fitting |
None
|
Raises:
| Type | Description |
|---|---|
KeyError
|
If param_name is not a polydisperse parameter |
ValueError
|
If pd_type is not a valid distribution type |
Source code in src/sans_fitter/sans_fitter.py
get_pd_param(param_name)
Get polydispersity configuration for a parameter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
param_name
|
str
|
Name of the base parameter (e.g., 'radius') |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with pd, pd_n, pd_nsigma, pd_type, vary, and active values. |
dict[str, Any]
|
'active' indicates whether polydispersity is active for this parameter (pd > 0). |
Raises:
| Type | Description |
|---|---|
KeyError
|
If param_name is not a polydisperse parameter |
Source code in src/sans_fitter/sans_fitter.py
enable_polydispersity(enabled=True)
Enable or disable polydispersity globally.
When disabled, polydispersity parameters are excluded from fitting but their values are preserved for when PD is re-enabled.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
enabled
|
bool
|
Whether to enable polydispersity (default: True) |
True
|
Source code in src/sans_fitter/sans_fitter.py
is_polydispersity_enabled()
Check if polydispersity is enabled.
Returns:
| Type | Description |
|---|---|
bool
|
True if polydispersity is globally enabled, False otherwise |
get_pd_params()
get_varying_pd_params()
Get list of polydispersity parameters that are set to vary.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of parameter names (e.g., ['radius_pd']) that will vary during fitting |
Source code in src/sans_fitter/sans_fitter.py
ParameterManager
Internal class for managing model parameters and polydispersity settings.
sans_fitter.parameter_manager.ParameterManager
Manages model parameters for SANS fitting.
Handles parameter initialization, validation, bounds management, special logic for structure factor parameter linking, and polydispersity support.
Attributes:
| Name | Type | Description |
|---|---|---|
params |
dict[str, dict[str, Any]]
|
Dictionary of parameter configurations |
model_name |
Optional[str]
|
Name of the current model |
structure_factor_name |
Optional[str]
|
Name of applied structure factor (if any) |
radius_effective_mode |
Optional[str]
|
Mode for handling radius_effective ('unconstrained' or 'link_radius') |
polydisperse_params |
dict[str, dict[str, Any]]
|
Dictionary of polydispersity parameters |
pd_enabled |
dict[str, dict[str, Any]]
|
Whether polydispersity is globally enabled |
Source code in src/sans_fitter/parameter_manager.py
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 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 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 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 | |
get_polydisperse_parameters()
Return list of parameter names that support polydispersity.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of parameter names that can have polydispersity applied |
Source code in src/sans_fitter/parameter_manager.py
has_polydisperse_parameters()
Check if the current model has any polydisperse parameters.
Returns:
| Type | Description |
|---|---|
bool
|
True if model has polydisperse parameters, False otherwise |
Source code in src/sans_fitter/parameter_manager.py
set_pd_param(base_param, pd_width=None, pd_n=None, pd_nsigma=None, pd_type=None, vary=None)
Configure polydispersity for a specific parameter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_param
|
str
|
Name of the base parameter (e.g., 'radius') |
required |
pd_width
|
Optional[float]
|
Polydispersity width (relative, 0.0 = monodisperse) |
None
|
pd_n
|
Optional[int]
|
Number of Gaussian quadrature points (default: 35) |
None
|
pd_nsigma
|
Optional[float]
|
Number of sigmas to include (default: 3.0) |
None
|
pd_type
|
Optional[str]
|
Distribution type ('gaussian', 'rectangle', 'lognormal', 'schulz', 'boltzmann') |
None
|
vary
|
Optional[bool]
|
Whether to vary the pd_width during fitting |
None
|
Raises:
| Type | Description |
|---|---|
KeyError
|
If base_param is not a polydisperse parameter |
ValueError
|
If pd_type is not a valid distribution type |
Source code in src/sans_fitter/parameter_manager.py
get_pd_param(base_param)
Get polydispersity configuration for a specific parameter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_param
|
str
|
Name of the base parameter (e.g., 'radius') |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with pd, pd_n, pd_nsigma, pd_type, vary, and active values. |
dict[str, Any]
|
'active' indicates whether polydispersity is active for this parameter (pd > 0). |
Raises:
| Type | Description |
|---|---|
KeyError
|
If base_param is not a polydisperse parameter |
Source code in src/sans_fitter/parameter_manager.py
toggle_pd_visibility(enabled)
Enable/disable polydispersity globally.
When disabled, polydispersity parameters are excluded from fitting but their values are preserved for when PD is re-enabled.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
enabled
|
bool
|
Whether polydispersity should be enabled |
required |
Source code in src/sans_fitter/parameter_manager.py
is_pd_enabled()
Check if polydispersity is globally enabled.
Returns:
| Type | Description |
|---|---|
bool
|
True if polydispersity is enabled, False otherwise |
get_pd_params_for_fitting()
Return polydispersity parameters to include in fitting.
Only returns PD parameters when pd_enabled is True. Returns parameters in the format expected by SasModels: - {param}_pd: polydispersity width - {param}_pd_n: number of quadrature points - {param}_pd_nsigma: number of sigmas - {param}_pd_type: distribution type
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary of PD parameters ready for fitting |
Source code in src/sans_fitter/parameter_manager.py
display_pd_params()
Display polydispersity parameter values and settings.
Source code in src/sans_fitter/parameter_manager.py
Polydispersity Constants
The following constants are available in sans_fitter.parameter_manager:
PD_DISTRIBUTION_TYPES
Supported polydispersity distribution types:
| Type | Description |
|---|---|
gaussian |
Gaussian/normal distribution (default) |
rectangle |
Uniform/rectangular distribution |
lognormal |
Log-normal distribution |
schulz |
Schulz distribution (common for polymers) |
boltzmann |
Boltzmann distribution |
Default Values
| Constant | Default Value | Description |
|---|---|---|
DEFAULT_PD_WIDTH |
0.0 |
Default polydispersity width (monodisperse) |
DEFAULT_PD_N |
35 |
Default number of quadrature points |
DEFAULT_PD_NSIGMA |
3.0 |
Default number of sigmas to include |
DEFAULT_PD_TYPE |
'gaussian' |
Default distribution type |