particle_stack
Particle stack Pydantic model for dealing with extracted particle data.
ParticleStack
Bases: BaseModel2DTM
Pydantic model for dealing with particle stack data.
Attributes:
Name | Type | Description |
---|---|---|
df_path |
str
|
Path to the DataFrame containing the particle data. The DataFrame must have the following columns (see the documentation for further information):
|
extracted_box_size |
tuple[int, int]
|
The size of the extracted particle boxes in pixels in units of pixels. |
original_template_size |
tuple[int, int]
|
The original size of the template used during the matching process. Should be smaller than the extracted box size. |
image_stack |
ExcludedTensor
|
The stack of images extracted from the micrographs. Is effectively a pytorch Tensor with shape (N, H, W) where N is the number of particles and (H, W) is the extracted box size. |
Source code in src/leopard_em/pydantic_models/data_structures/particle_stack.py
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 |
|
df_columns
property
Get the columns of the DataFrame.
num_particles
property
Get the number of particles in the stack.
__getitem__(key)
Get an item from the DataFrame.
Source code in src/leopard_em/pydantic_models/data_structures/particle_stack.py
805 806 807 808 809 810 |
|
__init__(skip_df_load=False, **data)
Initialize the ParticleStack object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
skip_df_load
|
bool
|
Whether to skip loading the DataFrame, by default False and the dataframe is loaded automatically. |
False
|
data
|
dict[str, Any]
|
The data to initialize the object with. |
{}
|
Source code in src/leopard_em/pydantic_models/data_structures/particle_stack.py
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 |
|
construct_cropped_statistic_stack(stat, handle_bounds='pad', padding_mode='constant', padding_value=0.0)
Return a tensor of the specified statistic for each cropped image.
NOTE: This function is very similar to construct_image_stack
but returns the
statistic in one of the result maps. Shape here is (N, H - h + 1, W - w + 1).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
stat
|
Literal["mip", "scaled_mip", "correlation_average",
|
"correlation_variance", "defocus", "psi", "theta", "phi"] The statistic to extract from the DataFrame. |
required |
handle_bounds
|
Literal['pad', 'clip', 'error']
|
How to handle the bounds of the image, by default "pad". If "pad", the image will be padded with the padding value based on the padding mode. If "error", an error will be raised if any region exceeds the image bounds. NOTE: clipping is not supported since returned stack may have inhomogeneous sizes. |
'pad'
|
padding_mode
|
Literal['constant', 'reflect', 'replicate']
|
The padding mode to use when padding the image, by default "constant".
"constant" pads with the value |
'constant'
|
padding_value
|
float
|
The value to use for padding when |
0.0
|
Returns:
Type | Description |
---|---|
Tensor
|
The stack of statistics with shape (N, H - h + 1, W - w + 1) where N is the number of particles and (H, W) is the extracted box size with (h, w) being the original template size. |
Source code in src/leopard_em/pydantic_models/data_structures/particle_stack.py
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 |
|
construct_filter_stack(preprocess_filters, output_shape)
Get stack of Fourier filters from filter config and reference micrographs.
Note that here the filters are assumed to be applied globally (i.e. no local whitening, etc. is being done). Whitening filters are calculated with reference to each original micrograph in the DataFrame.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
preprocess_filters
|
PreprocessingFilters
|
Configuration object of filters to apply. |
required |
output_shape
|
tuple[int, int]
|
What shape along the last two dimensions the filters should be. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
The stack of filters with shape (N, h, w) where N is the number of particles and (h, w) is the output shape. |
Source code in src/leopard_em/pydantic_models/data_structures/particle_stack.py
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 |
|
construct_image_stack(pos_reference='top-left', handle_bounds='pad', padding_mode='constant', padding_value=0.0)
Construct stack of images from the DataFrame (updates image_stack in-place).
This method preferentially selects refined position columns by default (refined_pos_x, refined_pos_y) if they are present in the DataFrame, falling back to unrefined positions (pos_x, pos_y) otherwise.
This method uses columns pos_x and pos_y (or refined_pos_x and refined_pos_y if available) to extract the boxes from the images. When using top-left reference position, the boxes are extracted as follows, where the dots represent the actual particle in the image
Example: : +----------------------------------+ : | | : | | : | (x, y) *=== box_w ===+ | : | | | | : | | .... box_h | : img_height | ...... | | : | | .... | | : | | | | : | +=============+ | : | | : +------------ img_width -----------+
When center reference is used, then the position columns in the DataFrame are interpreted as the center of the particle, and the boxes are extracted around this x and y position as follows:
Example: : +----------------------------------+ : | | : | | : | +=== box_w ===+ | : | | | | : | | .... | | : img_height |(x, y).*.. box_h | : | | .... | | : | | | | : | +=============+ | : | | : +------------ img_width -----------+
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pos_reference
|
Literal['center', 'top-left']
|
The reference point for the positions, by default "top-left". If "center", the boxes extracted will be image[y - box_size // 2 : y + box_size // 2, ...]. Columns in the dataframe which are used as position references are always pos_x and pos_y, or refined_pos_x and refined_pos_y if available. If "top-left", the boxes will be image[y : y + box_size, ...]. Leopard-EM uses the "top-left" reference position, and unless you know data was processed in a different way you should not change this value. |
'top-left'
|
handle_bounds
|
Literal['pad', 'clip', 'error']
|
How to handle the bounds of the image, by default "pad". If "pad", the image will be padded with the padding value based on the padding mode. If "error", an error will be raised if any region exceeds the image bounds. NOTE: clipping is not supported since returned stack may have inhomogeneous sizes. |
'pad'
|
padding_mode
|
Literal['constant', 'reflect', 'replicate']
|
The padding mode to use when padding the image, by default "constant".
"constant" pads with the value |
'constant'
|
padding_value
|
float
|
The value to use for padding when |
0.0
|
Returns:
Type | Description |
---|---|
Tensor
|
The stack of images, this is the internal 'image_stack' attribute. |
Source code in src/leopard_em/pydantic_models/data_structures/particle_stack.py
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 |
|
get_absolute_defocus(prefer_refined_defocus=True)
Get the absolute defocus values for each particle.
NOTE: If the refined defocus values are requested but not present in the DataFrame (either no column or any NaN values), a user warning is raised and the original defocus values are returned instead.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prefer_refined_defocus
|
bool
|
Whether to use the refined defocus values (columns prefixed with 'refined_') or not, by default True. |
True
|
Returns:
Type | Description |
---|---|
tuple[Tensor, Tensor]
|
A tuple of two tensors containing the absolute defocus values along the major (defocus_u) and minor axes (defocus_v), respectively in units of Angstroms. |
Source code in src/leopard_em/pydantic_models/data_structures/particle_stack.py
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 |
|
get_dataframe_copy()
Return a copy of the underlying DataFrame.
Returns:
Type | Description |
---|---|
DataFrame
|
|
A copy of the underlying DataFrame
|
|
Source code in src/leopard_em/pydantic_models/data_structures/particle_stack.py
824 825 826 827 828 829 830 831 832 |
|
get_euler_angles(prefer_refined_angles=True)
Return the Euler angles (phi, theta, psi) of all particles as a tensor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prefer_refined_angles
|
bool
|
When true, the refined Euler angles are used (columns prefixed with 'refined_'), otherwise the original angles are used, by default True. |
True
|
Returns:
Type | Description |
---|---|
Tensor
|
A tensor of shape (N, 3) where N is the number of particles and the columns correspond to (phi, theta, psi) in ZYZ format. |
Source code in src/leopard_em/pydantic_models/data_structures/particle_stack.py
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 |
|
get_pixel_size(prefer_refined_pixel_size=True)
Get the relative pixel size values for each particle.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prefer_refined_pixel_size
|
bool
|
Whether to use the refined pixel size values (columns prefixed with 'refined_') or not, by default True. |
True
|
Returns:
Type | Description |
---|---|
Tensor
|
The relative pixel size values for each particle. |
Warnings
Warns if NaN values or no column present for either 'refined_pixel_size'
or 'pixel_size'. Falls back to the unrefined values.
Source code in src/leopard_em/pydantic_models/data_structures/particle_stack.py
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 |
|
get_relative_defocus(prefer_refined_defocus=True)
Get the relative defocus values for each particle.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prefer_refined_defocus
|
bool
|
Whether to use the refined defocus values (columns prefixed with 'refined_') or not, by default True. |
True
|
Returns:
Type | Description |
---|---|
Tensor
|
The relative defocus values for each particle. |
Warnings
Warns if NaN values or no column present for either
'refined_relative_defocus' or 'relative_defocus'.
Falls back to the unrefined values.
Source code in src/leopard_em/pydantic_models/data_structures/particle_stack.py
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 |
|
load_df()
Load the DataFrame from the specified path.
Raises:
Type | Description |
---|---|
ValueError
|
If the DataFrame is missing required columns. |
Source code in src/leopard_em/pydantic_models/data_structures/particle_stack.py
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 |
|
set_column(column_name, value)
Set a column in the underlying DataFrame.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
column_name
|
str
|
The name of the column to set |
required |
value
|
Any
|
The value to set the column to |
required |
Source code in src/leopard_em/pydantic_models/data_structures/particle_stack.py
812 813 814 815 816 817 818 819 820 821 822 |
|
get_cropped_image_regions(image, pos_y, pos_x, box_size, pos_reference='top-left', handle_bounds='pad', padding_mode='constant', padding_value=0.0)
Extracts regions from an image into a stack of cropped images.
The pos_reference
argument determines how the (y, x) coordinates are interpreted
when extracting boxes:
-
If
pos_reference="center"
: The (y, x) coordinate refers to the center of the box. The box extends from (y - height // 2, x - width // 2) to (y + height // 2, x + width // 2).Example: : +------------------+ : | | : height * (y, x) | : | | : +------ width -----+
-
If
pos_reference="top-left"
: The (y, x) coordinate refers to the top-left corner of the box. The box extends from (y, x) to (y + height, x + width).Example: : (y, x) *------ width -----+ : | | : | height : | | : +------------------+
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image
|
Tensor | ndarray
|
The input image from which to extract the regions. |
required |
pos_y
|
Tensor | ndarray
|
The y positions of the regions to extract. Type must mach |
required |
pos_x
|
Tensor | ndarray
|
The x positions of the regions to extract. Type must mach |
required |
box_size
|
int | tuple[int, int]
|
The size of the box to extract. If an integer is passed, the box will be square. |
required |
pos_reference
|
Literal['center', 'top-left']
|
The reference point for the positions, by default "center". If "center", the boxes extracted will be image[y - box_size // 2 : y + box_size // 2, ...]. If "top-left", the boxes will be image[y : y + box_size, ...]. |
'top-left'
|
handle_bounds
|
Literal['pad', 'clip', 'error']
|
How to handle the bounds of the image, by default "pad". If "pad", the image will be padded with the padding value based on the padding mode. If "error", an error will be raised if any region exceeds the image bounds. Note clipping is not supported since returned stack may have inhomogeneous sizes. |
'pad'
|
padding_mode
|
Literal['constant', 'reflect', 'replicate']
|
The padding mode to use when padding the image, by default "constant".
"constant" pads with the value |
'constant'
|
padding_value
|
float
|
The value to use for padding when |
0.0
|
Returns:
Type | Description |
---|---|
Tensor | ndarray
|
The stack of cropped images extracted from the input image. Type will match the input image type. |
Raises:
Type | Description |
---|---|
ValueError
|
If |
Source code in src/leopard_em/pydantic_models/data_structures/particle_stack.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 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 |
|