Commit 098a443
Enable global coordinates in spatial crop transforms (#8794)
I ran into this while building an injury classification pipeline on MRI.
My annotations come in physical coordinates and I kept having to
manually convert them to voxel space whenever I changed the
preprocessing spacing. Found this issue and saw the design was already
agreed on, so I went ahead and implemented it.
Follows the approach proposed by @ericspod in #8206.
Resolves #8206
## Changes
- Adds `TransformPointsWorldToImaged` and `TransformPointsImageToWorldd`
— thin subclasses of `ApplyTransformToPointsd` that hardcode the
`invert_affine` direction
- `SpatialCropd` now accepts string dictionary keys for `roi_center`,
`roi_size`, `roi_start`, and `roi_end`. When a string is passed, the
actual values are read from the data dict at call time instead of at
init
- This means you can pipe world-space annotations straight through
coordinate conversion and into cropping without manual recalculation
```python
from monai.transforms import Compose, TransformPointsWorldToImaged, SpatialCropd
pipeline = Compose([
TransformPointsWorldToImaged(keys="roi_start", refer_keys="image"),
TransformPointsWorldToImaged(keys="roi_end", refer_keys="image"),
SpatialCropd(keys="image", roi_start="roi_start", roi_end="roi_end"),
])
```
## Design notes
- When no string keys are passed, `SpatialCropd` takes the original code
path — zero overhead for existing usage
- The string-key path recreates a `SpatialCrop` on each `__call__`
(stored on `self.cropper`), since slice computation must be deferred
until the data dict is available. `inverse()` is overridden for this
path to use `pop_transform(check=False)`, since the cropper identity
changes between calls — the actual crop info is read from the
MetaTensor's transform stack
- Tensors from `ApplyTransformToPoints` (shape `(C, N, dims)`) get
flattened and rounded to int via `torch.round` (banker's rounding) to
avoid systematic bias
## Tests
- 13 tests for `TransformPointsWorldToImaged` (correctness, equivalence
with base class, inverse, error cases)
- 7 tests for `TransformPointsImageToWorldd`
- 11 tests for `SpatialCropd` string-key support (start/end,
center/size, mixed params, tensor shapes, float rounding, missing keys,
`requires_current_data`, multi-key, inverse)
- 1 end-to-end integration test: `TransformPointsWorldToImaged` →
`SpatialCropd` with world-space ROI
- All 12 existing `SpatialCropd` tests pass unchanged
- All 21 existing `ApplyTransformToPointsd` tests pass unchanged
---------
Signed-off-by: Emanuilo Jovanovic <emanuilo.jovanovic@smartcat.io>
Signed-off-by: Emanuilo Jovanovic <emanuilo.jovanovic@hotmail.com>
Co-authored-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com>1 parent 65beb58 commit 098a443
6 files changed
Lines changed: 546 additions & 11 deletions
File tree
- monai/transforms
- croppad
- utility
- tests/transforms
- utility
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
676 | 676 | | |
677 | 677 | | |
678 | 678 | | |
| 679 | + | |
| 680 | + | |
| 681 | + | |
| 682 | + | |
| 683 | + | |
| 684 | + | |
679 | 685 | | |
680 | 686 | | |
681 | 687 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
22 | | - | |
| 22 | + | |
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
| |||
50 | 50 | | |
51 | 51 | | |
52 | 52 | | |
53 | | - | |
| 53 | + | |
54 | 54 | | |
55 | 55 | | |
56 | 56 | | |
| |||
431 | 431 | | |
432 | 432 | | |
433 | 433 | | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
434 | 450 | | |
435 | 451 | | |
436 | 452 | | |
437 | 453 | | |
438 | 454 | | |
439 | 455 | | |
440 | 456 | | |
441 | | - | |
442 | | - | |
443 | | - | |
444 | | - | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
445 | 461 | | |
446 | 462 | | |
447 | 463 | | |
| |||
450 | 466 | | |
451 | 467 | | |
452 | 468 | | |
453 | | - | |
| 469 | + | |
| 470 | + | |
454 | 471 | | |
455 | | - | |
456 | | - | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
457 | 475 | | |
458 | | - | |
| 476 | + | |
459 | 477 | | |
460 | 478 | | |
461 | 479 | | |
462 | 480 | | |
463 | | - | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
464 | 502 | | |
465 | 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 | + | |
466 | 597 | | |
467 | 598 | | |
468 | 599 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
192 | 192 | | |
193 | 193 | | |
194 | 194 | | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
195 | 201 | | |
196 | 202 | | |
197 | 203 | | |
| |||
1918 | 1924 | | |
1919 | 1925 | | |
1920 | 1926 | | |
| 1927 | + | |
| 1928 | + | |
| 1929 | + | |
| 1930 | + | |
| 1931 | + | |
| 1932 | + | |
| 1933 | + | |
| 1934 | + | |
| 1935 | + | |
| 1936 | + | |
| 1937 | + | |
| 1938 | + | |
| 1939 | + | |
| 1940 | + | |
| 1941 | + | |
| 1942 | + | |
| 1943 | + | |
| 1944 | + | |
| 1945 | + | |
| 1946 | + | |
| 1947 | + | |
| 1948 | + | |
| 1949 | + | |
| 1950 | + | |
| 1951 | + | |
| 1952 | + | |
| 1953 | + | |
| 1954 | + | |
| 1955 | + | |
| 1956 | + | |
| 1957 | + | |
| 1958 | + | |
| 1959 | + | |
| 1960 | + | |
| 1961 | + | |
| 1962 | + | |
| 1963 | + | |
| 1964 | + | |
| 1965 | + | |
| 1966 | + | |
| 1967 | + | |
| 1968 | + | |
| 1969 | + | |
| 1970 | + | |
| 1971 | + | |
| 1972 | + | |
| 1973 | + | |
| 1974 | + | |
| 1975 | + | |
| 1976 | + | |
| 1977 | + | |
| 1978 | + | |
| 1979 | + | |
| 1980 | + | |
| 1981 | + | |
| 1982 | + | |
| 1983 | + | |
| 1984 | + | |
| 1985 | + | |
| 1986 | + | |
| 1987 | + | |
| 1988 | + | |
| 1989 | + | |
| 1990 | + | |
| 1991 | + | |
| 1992 | + | |
| 1993 | + | |
| 1994 | + | |
| 1995 | + | |
| 1996 | + | |
| 1997 | + | |
| 1998 | + | |
| 1999 | + | |
| 2000 | + | |
| 2001 | + | |
| 2002 | + | |
| 2003 | + | |
| 2004 | + | |
| 2005 | + | |
| 2006 | + | |
1921 | 2007 | | |
1922 | 2008 | | |
1923 | 2009 | | |
| |||
1983 | 2069 | | |
1984 | 2070 | | |
1985 | 2071 | | |
| 2072 | + | |
| 2073 | + | |
1986 | 2074 | | |
0 commit comments