transforms
navis.transforms.AffineTransform #
Affine transformation of 3D spatial data.
| PARAMETER | DESCRIPTION |
|---|---|
matrix | TYPE: |
Examples:
A simple scaling transform
>>> from navis import transforms
>>> import numpy as np
>>> M = np.diag([1e3, 1e3, 1e3, 1])
>>> tr = transforms.affine.AffineTransform(M)
>>> points = np.array([[0, 0, 0], [1, 1, 1]])
>>> tr.xform(points)
array([[ 0., 0., 0.],
[1000., 1000., 1000.]])
Source code in navis/transforms/affine.py
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 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 | |
__init__ #
Initialize transform.
Source code in navis/transforms/affine.py
44 45 46 47 48 49 50 51 | |
copy #
Return copy of transform.
Source code in navis/transforms/affine.py
77 78 79 80 81 82 83 84 85 86 | |
xform #
Apply transform to points.
| PARAMETER | DESCRIPTION |
|---|---|
points | TYPE: |
invert | TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
pointsxf | The transformed points. |
Source code in navis/transforms/affine.py
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 | |
navis.transforms.AliasTransform #
Helper transform that simply passes points through.
Useful for defining aliases.
Source code in navis/transforms/base.py
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 | |
__init__ #
Initialize.
Source code in navis/transforms/base.py
140 141 142 | |
copy #
Return copy.
Source code in navis/transforms/base.py
154 155 156 157 158 | |
xform #
Pass through.
Note that the returned points are NOT a copy but the originals.
Source code in navis/transforms/base.py
160 161 162 163 164 165 | |
navis.transforms.CMTKtransform #
CMTK transforms of 3D spatial data.
Requires either CMTK to be installed, or navis-fastcore - the latter implements CMTK transforms in Rust and needs no external binaries. See the backend parameter.
| PARAMETER | DESCRIPTION |
|---|---|
regs | TYPE: |
directions | TYPE: |
threads | TYPE: |
backend | TYPE: |
Examples:
>>> from navis import transforms
>>> tr = transforms.cmtk.CMTKtransform('/path/to/CMTK_directory.list')
>>> tr.xform(points)
Notes
TODO (when navis-fastcore becomes a hard dependency and the "binary" backend goes away): switch affine_fallback to fastcore's fallback_to_affine="hop".
We currently ask fastcore for fallback_to_affine=True, i.e. "re-run a failed point affine-only through the whole chain, from where it started". That is what streamxform --affine-only does, and while we support both backends they have to agree - so it is the right choice for now.
But it is the cruder of the two. Where several registrations are merged into one transform (which happens on about half of all bridging paths), a point that clears the first few warps and then leaves the domain loses those warps too. fastcore's "hop" keeps them and swaps the affine in only for the hop that actually failed. On a 2-registration chain the two differ on ~19% of points, by a median of ~2 world units.
Once there is no binary to stay consistent with, "hop" is the better answer.
Source code in navis/transforms/cmtk.py
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 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 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 | |
regargs: list property #
Generate regargs.
append #
Add another transform.
| PARAMETER | DESCRIPTION |
|---|---|
transform | TYPE: |
direction | TYPE: |
Source code in navis/transforms/cmtk.py
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 | |
check_if_possible #
Check if this transform is possible.
Source code in navis/transforms/cmtk.py
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 | |
copy #
Return copy.
Source code in navis/transforms/cmtk.py
424 425 426 427 428 429 430 431 | |
from_file staticmethod #
Generate CMTKtransform from file.
| PARAMETER | DESCRIPTION |
|---|---|
filepath | TYPE: |
**kwargs | DEFAULT: |
| RETURNS | DESCRIPTION |
|---|---|
CMTKtransform | |
Source code in navis/transforms/cmtk.py
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 | |
make_args #
Generate arguments passed to subprocess.
Source code in navis/transforms/cmtk.py
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 | |
parse_cmtk_output #
Parse CMTK output.
Briefly, CMTK output will be a byte literal like this:
b'311 63 23 \n275 54 25 \n'
In case of failed transforms we will get something like this where the original coordinates are returned with a "FAILED" flag
b'343 72 23 \n-10 -10 -10 FAILED \n'
Parameter
output : tuple of (b'', None) Stdout of CMTK call. fail_value Value to use for points that failed to transform. By default we use np.nan.
| RETURNS | DESCRIPTION |
|---|---|
pointsxf | The parse transformed points. TYPE: |
Source code in navis/transforms/cmtk.py
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 | |
to_dfield #
Convert transform to dense deformation field.
Note this always requires CMTK: navis-fastcore implements point but not image transforms.
| PARAMETER | DESCRIPTION |
|---|---|
template | TYPE: |
out | TYPE: |
absolute | TYPE: |
verbose | TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
dfield | The dense deformation field as (3, Nx, Ny, Nz) numpy array with either absolute coordinates or offsets where the first dimension contains the x/y/z coordinates. Note that the coordinates stored in the field are in physical space (e.g. microns), not voxel space. |
header | The NRRD header associated with the deformation field. TYPE: |
See Also
CMTKtransform.to_grid_transform Method to directly convert to GridTransform. Please see that method's notes for details on how to handle the deformation field.
Source code in navis/transforms/cmtk.py
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 | |
to_grid_transform #
Convert to GridTransform via dense deformation field.
| PARAMETER | DESCRIPTION |
|---|---|
template | TYPE: |
absolute | TYPE: |
verbose | TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
transform | A few notes on using the resulting GridTransform: 1. The transform will expect input coordinates in voxel space. See the returned TYPE: |
voxel_size | The voxel size of the input space. This is important because the GridTransform will expect input coordinates in voxel space. TYPE: |
Source code in navis/transforms/cmtk.py
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 | |
xform #
Xform data.
| PARAMETER | DESCRIPTION |
|---|---|
points | TYPE: |
affine_only | TYPE: |
affine_fallback | TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
pointsxf | Transformed points. Points that failed to transform will be TYPE: |
Source code in navis/transforms/cmtk.py
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 | |
xform_image #
Transform an image using CMTK's reformatx.
Note this always requires CMTK: navis-fastcore implements point but not image transforms.
| PARAMETER | DESCRIPTION |
|---|---|
im | TYPE: |
target | TYPE: |
out | TYPE: |
interpolation | TYPE: |
verbose | TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
np.ndarray | None | If out is None, returns the transformed image as np.ndarray. Otherwise, None. |
Source code in navis/transforms/cmtk.py
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 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 | |
navis.transforms.ElastixTransform #
Elastix transforms of 3D spatial data.
Requires either Elastix to be installed, or navis-fastcore - the latter implements elastix transforms in Rust and needs no external binaries. See the backend parameter.
Based on code by Jasper Phelps (https://github.com/jasper-tms/pytransformix).
Note that elastix transforms can only be inverted on the "fastcore" backend: the transformix binary has no way to compute an inverse.
| PARAMETER | DESCRIPTION |
|---|---|
file | TYPE: |
copy_files | TYPE: |
backend | TYPE: |
Examples:
>>> from navis import transforms
>>> tr = transforms.ElastixTransform('/path/to/transform/transform')
>>> tr.xform(points)
Notes
Inverting an elastix transform is a Gauss-Newton solve per point: ~80x the cost of going forwards (and ~4x a dedicated reverse registration), it drops the ~0.04% of points that have no preimage, and where the warp folds ~4% of points come back somewhere other than they started - inherent to inverting a non-injective map, not a solver defect.
So a purpose-built reverse registration should always beat an on-the-fly inversion. It does: _pick_edge prefers a forward registration over the inverse of its counterpart unconditionally, and inverse_weight_factor (above) keeps an inverse hop from looking like a shortcut when routing.
navis.config.elastix_invertible nonetheless defaults to off, and should stay that way while the deprecated "binary" backend is still selectable: it cannot invert at all, so turning this on would let the two backends find different routes. Flip it in 3.0, when that backend goes away. It is safe to enable - on flybrains it changes nothing at all, because every elastix registration there already ships with a reverse - so the only thing it buys today is a route where somebody registered an elastix transform and no reverse for it.
One thing worth knowing before anyone tunes this: a dedicated reverse registration is not the inverse of its forward twin - they are independent fits. Ours agree to a median of ~0.2 world units, but the dedicated reverse only round-trips through the forward map to a median of 0.17 (p95 128), where the numerical inverse round-trips to ~1e-13. Preferring the dedicated registration is a choice about which map you want - the one its authors fitted, and the one the field uses - not a claim that it is more accurate.
Source code in navis/transforms/elastix.py
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 | |
can_invert: bool property #
Whether -transform works.
Needs the fastcore backend (transformix cannot invert) and a file that is actually invertible - a chain combined via "Add" is not. The latter is settled with fastcore's header-only probe, so this stays cheap enough to ask of every transform in the registry.
invertible: bool property #
Whether the bridging graph may traverse this transform backwards.
Deliberately narrower than can_invert: inverting works whenever we're on the fastcore backend with an invertible file, but we only let the registry route through an inverse if navis.config.elastix_invertible says so. Every elastix registration we know of ships with a purpose-built reverse registration, so the inverse buys no new connectivity - it just adds a cheaper-looking parallel edge that drags unrelated routes through it.
Note the config check comes first, so that in the default case this costs a single lookup and never touches the disk - it is evaluated for every registered transform each time the bridging graph is built.
check_if_possible #
Check if this transform is possible.
Source code in navis/transforms/elastix.py
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 | |
copy #
Return copy.
Source code in navis/transforms/elastix.py
317 318 319 320 321 322 323 324 325 326 327 328 | |
read_output_file #
Load output file.
Parameter
filepath : str Filepath to output file.
| RETURNS | DESCRIPTION |
|---|---|
pointsxf | The parse transformed points. TYPE: |
Source code in navis/transforms/elastix.py
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 | |
write_input_file #
Write a numpy array in format required by transformix.
Source code in navis/transforms/elastix.py
330 331 332 333 334 335 | |
xform #
Xform data.
| PARAMETER | DESCRIPTION |
|---|---|
points | TYPE: |
return_logs | TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
pointsxf | Transformed points. TYPE: |
Source code in navis/transforms/elastix.py
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 | |
navis.transforms.FunctionTransform #
Apply custom function as transform.
| PARAMETER | DESCRIPTION |
|---|---|
func | TYPE: |
Source code in navis/transforms/base.py
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 | |
__init__ #
Initialize.
Source code in navis/transforms/base.py
178 179 180 181 182 | |
copy #
Return copy.
Source code in navis/transforms/base.py
192 193 194 195 196 | |
xform #
Xform data.
| PARAMETER | DESCRIPTION |
|---|---|
points | TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
pointsxf | Transformed points. TYPE: |
Source code in navis/transforms/base.py
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 | |
navis.transforms.GridTransform #
Deformation or coordinate field transform of 3D spatial data.
This is effectively a simpler version of the H5transform class and only supports deformation fields stored as numpy arrays in memory.
| PARAMETER | DESCRIPTION |
|---|---|
field | TYPE: |
type | TYPE: |
spacing | TYPE: |
offset | TYPE: |
Source code in navis/transforms/grid.py
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 | |
affine: AffineTransform property #
Return affine part of the transform.
shape: tuple property #
Return shape of the deformation field.
__init__ #
Init class.
Source code in navis/transforms/grid.py
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | |
calculate_affine #
Calculate affine part of the transform.
Source code in navis/transforms/grid.py
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 | |
copy #
Return copy.
Source code in navis/transforms/grid.py
164 165 166 167 168 169 170 171 | |
from_file classmethod #
Create GridTransform from a file.
| PARAMETER | DESCRIPTION |
|---|---|
filepath | TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
GridTransform instance. | |
Source code in navis/transforms/grid.py
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 | |
from_warpfield classmethod #
Create GridTransform from a Warpfield deformation field.
| PARAMETER | DESCRIPTION |
|---|---|
warpfield | TYPE: |
Source code in navis/transforms/grid.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 | |
xform #
Xform data.
| PARAMETER | DESCRIPTION |
|---|---|
points | TYPE: |
affine_fallback | TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
pointsxf | Transformed points. Will contain TYPE: |
Source code in navis/transforms/grid.py
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 | |
navis.transforms.H5transform #
Hdf5 transform of 3D spatial data.
See here for specifications of the format.
| PARAMETER | DESCRIPTION |
|---|---|
f | TYPE: |
direction | TYPE: |
level | TYPE: |
cache | TYPE: |
full_ingest | TYPE: |
Source code in navis/transforms/h5reg.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 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 | |
quantization_multiplier property #
Quantization multiplier of the deformation field.
shape property writable #
Shape of the deformation field.
Note that the deformation field is likely to be (z, y, x, 3) where the last dimension contains the x/y/z offsets.
spacing property #
Voxel spacing of the deformation field (z, y, x).
use_cache property writable #
Whether to cache the deformation field.
__init__ #
Init class.
Source code in navis/transforms/h5reg.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 | |
copy #
Return copy.
Please note that by default we're carrying over the cache WITHOUT copying it! Currently, xform_brain turns transforms into a TransformSequence which makes copies of each transform. If we didn't carry over the cache, we would never actually use it.
Source code in navis/transforms/h5reg.py
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 | |
from_file staticmethod #
Generate H5transform from file.
| PARAMETER | DESCRIPTION |
|---|---|
filepath | TYPE: |
**kwargs | DEFAULT: |
| RETURNS | DESCRIPTION |
|---|---|
H5transform | |
Source code in navis/transforms/h5reg.py
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 | |
full_ingest #
Fully ingest the deformation field.
Source code in navis/transforms/h5reg.py
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | |
precache #
Cache deformation field for given bounding box.
| PARAMETER | DESCRIPTION |
|---|---|
bbox | TYPE: |
padding | TYPE: |
Source code in navis/transforms/h5reg.py
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 | |
xform #
Xform data.
| PARAMETER | DESCRIPTION |
|---|---|
points | TYPE: |
affine_fallback | TYPE: |
force_deform | TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
pointsxf | Transformed points. Out-of-bounds handling depends on TYPE: |
Source code in navis/transforms/h5reg.py
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 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 | |
xform_image #
Transform a 3D image using backward mapping.
If available, this method will use numba to accelerate the transformation. This is highly recommended as it can speed up the transformation by several orders of magnitude:
pip install numba
Note though that the numba-accelerated path only supports linear interpolation and constant-mode boundary handling (default).
| PARAMETER | DESCRIPTION |
|---|---|
image | TYPE: |
image_res | TYPE: |
out_res | TYPE: |
out_shape | TYPE: |
order | TYPE: |
mode | TYPE: |
cval | TYPE: |
cache | TYPE: |
progress | TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
transformed | Transformed image in target space. The shape is determined by TYPE: |
Notes
This method uses backward mapping: for each output chunk, we compute where the voxels came from in the source image using the transformation, then interpolate the source image at those locations. So if your transform goes from A -> B, this method actually uses the inverse transform under the hood to figure out where in A the voxels in B come from.
Source code in navis/transforms/h5reg.py
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 | |
navis.transforms.MovingLeastSquaresTransform #
Source code in navis/transforms/moving_least_squares.py
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 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 | |
matrix_affine property #
Return the affine transformation matrix.
Note that moving least squares is a locally weighted affine transform: every point effectively gets its own affine matrix. This property returns the global affine, i.e. the least-squares fit of the source onto the target landmarks. That is the transform the moving least squares converges to far away from the landmarks, where the distance weights even out.
transformer property #
The (lazily built) molesq transformer used by the "python" backend.
molesq is imported here rather than at module level so that import navis does not pay for it and, more to the point, so that an install without it can still run moving least squares on the default (fastcore) backend - which needs nothing but the landmarks.
__init__ #
Moving Least Squares transforms of 3D spatial data.
Runs on navis-fastcore's Rust implementation of the affine algorithm published in Schaefer et al. 2006. The deprecated "python" backend runs on molesq instead, which packages the implementation by Casey Schneider-Mizell of the same algorithm - see the backend parameter. molesq is imported on demand and is only needed for that backend.
Notes
At least in my hands, TPStransforms are significantly faster than MovingLeastSquaresTransforms. The results are similar but not identical, so make sure to use the one that works best for your use case. The two backends agree with each other to ~1e-13.
| PARAMETER | DESCRIPTION |
|---|---|
landmarks_source | Source landmarks as x/y/z coordinates. |
landmarks_target | Target landmarks as x/y/z coordinates. |
direction | 'forward' (default) or 'inverse' (treat the target as the source and vice versa) TYPE: |
batch_size | Batch size for transforming points. At one point during the transformation, molesq generates a (N, M) distance matrix, where N is the number of landmarks and M is the number of locations, which can get prohibitively expensive. We avoid the issue by batching the transformation by default. Note that the overhead from batching seems negligible. Ignored on the "fastcore" backend, which reduces over landmarks instead of materialising that matrix. TYPE: |
backend | Which implementation to use. TYPE: |
Examples:
>>> from navis import transforms
>>> import numpy as np
>>> # Generate some mock landmarks
>>> src = np.array([[0, 0, 0], [10, 10, 10], [100, 100, 100], [80, 10, 30]])
>>> trg = np.array([[1, 15, 5], [9, 18, 21], [80, 99, 120], [5, 10, 80]])
>>> tr = transforms.MovingLeastSquaresTransform(src, trg)
>>> points = np.array([[0, 0, 0], [50, 50, 50]])
>>> tr.xform(points)
array([[ 1. , 15. , 5. ],
[ 81.56361725, 155.32071504, 187.3147564 ]])
>>> # The global affine part of the transform
>>> tr.matrix_affine.shape
(4, 4)
Source code in navis/transforms/moving_least_squares.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 102 103 104 105 106 | |
copy #
Make a copy
Source code in navis/transforms/moving_least_squares.py
234 235 236 | |
xform #
Transform points.
| PARAMETER | DESCRIPTION |
|---|---|
points | TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
pointsxf | Transformed points. TYPE: |
Source code in navis/transforms/moving_least_squares.py
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 | |
navis.transforms.SimilarityTransform #
Calculate and apply a similarity transform.
Similarity transforms are a special case of affine transforms that consist of a rotation, a translation and a single uniform scaling factor but no shear and no reflection.
The transform is the least-squares optimal fit between the two sets of landmarks and is found in closed form via a singular value decomposition (see Umeyama, 1991). Because a similarity transform has only 7 degrees of freedom (3 rotation, 3 translation, 1 scale), it will generally not map the source landmarks onto the target landmarks exactly - unlike, say, a navis.transforms.thinplate.TPStransform.
| PARAMETER | DESCRIPTION |
|---|---|
landmarks_source | TYPE: |
landmarks_target | TYPE: |
weights | TYPE: |
scale | TYPE: |
Examples:
>>> from navis import transforms
>>> import numpy as np
>>> # Generate some mock landmarks
>>> src = np.array([[0, 0, 0], [10, 10, 10], [100, 100, 100], [80, 10, 30]])
>>> trg = np.array([[1, 15, 5], [9, 18, 21], [80, 99, 120], [5, 10, 80]])
>>> tr = transforms.similarity.SimilarityTransform(src, trg)
>>> tr.xform(np.array([[0, 0, 0], [50, 50, 50]]))
array([[-0.27589955, 10.98503005, 4.26630007],
[38.44936026, 53.04761397, 62.7346341 ]])
Note that - unlike a thin-plate spline transform - the landmarks are not mapped exactly because a similarity transform has only 7 degrees of freedom:
>>> round(tr.scale_factor, 4)
0.9443
Source code in navis/transforms/similarity.py
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 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 | |
n: int property #
Number of landmarks.
rotation: np.ndarray property #
The (3, 3) rotation matrix.
scale_factor: float property #
The uniform scaling factor.
translation: np.ndarray property #
The (3, ) translation vector.
copy #
Return copy of transform.
Source code in navis/transforms/similarity.py
216 217 218 219 220 | |
update #
(Re-)calculate the transform from the current landmarks.
Source code in navis/transforms/similarity.py
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 | |
xform #
Transform points.
| PARAMETER | DESCRIPTION |
|---|---|
points | TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
pointsxf | Transformed points. TYPE: |
Source code in navis/transforms/similarity.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 | |
navis.transforms.TPStransform #
Thin Plate Spline transforms of 3D spatial data.
Runs on navis-fastcore's Rust implementation; the deprecated "python" backend runs on morphops instead (see the backend parameter). The spline is always fitted with morphops - numpy's LAPACK-backed solve is faster than fastcore's here - so the backend only changes how points are transformed, which is where fastcore is ~10-15x faster. The two agree to ~1e-13.
Notes
At least in my hands, TPStransforms are significantly faster than MovingLeastSquaresTransforms. The results are similar but not identical, so make sure to use the one that works best for your use case.
| PARAMETER | DESCRIPTION |
|---|---|
landmarks_source | TYPE: |
landmarks_target | TYPE: |
batch_size | TYPE: |
backend | TYPE: |
Examples:
>>> from navis import transforms
>>> import numpy as np
>>> # Generate some mock landmarks
>>> src = np.array([[0, 0, 0], [10, 10, 10], [100, 100, 100], [80, 10, 30]])
>>> trg = np.array([[1, 15, 5], [9, 18, 21], [80, 99, 120], [5, 10, 80]])
>>> tr = transforms.thinplate.TPStransform(src, trg)
>>> points = np.array([[0, 0, 0], [50, 50, 50]])
>>> tr.xform(points)
array([[ 1. , 15. , 5. ],
[40.55555556, 54. , 65. ]])
Source code in navis/transforms/thinplate.py
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 | |
matrix_affine property #
Return the affine transformation matrix.
__init__ #
Initialize class.
Source code in navis/transforms/thinplate.py
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 | |
copy #
Make copy.
Source code in navis/transforms/thinplate.py
238 239 240 241 242 243 244 245 246 247 248 | |
xform #
Transform points.
| PARAMETER | DESCRIPTION |
|---|---|
points | TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
pointsxf | Transformed points. TYPE: |
Source code in navis/transforms/thinplate.py
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 | |
navis.transforms.clear_transform_cache #
Drop all cached (parsed) fastcore transforms.
Mostly useful if a registration changed on disk - we do not stat the files on every call.
Source code in navis/transforms/backends.py
200 201 202 203 204 205 206 207 208 209 | |
navis.transforms.set_transform_backend #
Set the backend used for point transforms.
Applies to CMTK and elastix transforms, and to the landmark transforms (navis.transforms.TPStransform and navis.transforms.MovingLeastSquaresTransform).
| PARAMETER | DESCRIPTION |
|---|---|
backend | TYPE: |
elastix_invertible | TYPE: |
Examples:
>>> import navis
>>> navis.transforms.set_transform_backend('fastcore')
>>> navis.transforms.set_transform_backend('auto')
Source code in navis/transforms/backends.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 | |