Skip to content

Commit 86000a6

Browse files
wyliKumoLiu
authored andcommitted
5414 size/size() for metatensor compatibility (#5415)
Signed-off-by: Wenqi Li <[email protected]> Fixes #5414 ### Types of changes <!--- Put an `x` in all the boxes that apply, and remove the not applicable items --> - [x] Non-breaking change (fix or new feature that would not break existing functionality). - [ ] Breaking change (fix or new feature that would cause existing functionality to change). - [x] New tests added to cover the changes. - [ ] Integration tests passed locally by running `./runtests.sh -f -u --net --coverage`. - [ ] Quick tests passed locally by running `./runtests.sh --quick --unittests --disttests`. - [ ] In-line docstrings updated. - [ ] Documentation updated, tested `make html` command in the `docs/` folder. Signed-off-by: Wenqi Li <[email protected]> Signed-off-by: KumoLiu <[email protected]>
1 parent b226da6 commit 86000a6

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

monai/transforms/utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -505,11 +505,11 @@ def generate_pos_neg_label_crop_centers(
505505
raise ValueError("No sampling location available.")
506506

507507
if len(fg_indices) == 0 or len(bg_indices) == 0:
508+
pos_ratio = 0 if len(fg_indices) == 0 else 1
508509
warnings.warn(
509-
f"N foreground {len(fg_indices)}, N background {len(bg_indices)},"
510-
"unable to generate class balanced samples."
510+
f"Num foregrounds {len(fg_indices)}, Num backgrounds {len(bg_indices)}, "
511+
f"unable to generate class balanced samples, setting `pos_ratio` to {pos_ratio}."
511512
)
512-
pos_ratio = 0 if fg_indices.size == 0 else 1
513513

514514
for _ in range(num_samples):
515515
indices_to_use = fg_indices if rand_state.rand() < pos_ratio else bg_indices

tests/test_generate_pos_neg_label_crop_centers.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,20 @@
3131
list,
3232
2,
3333
3,
34-
]
34+
],
35+
[
36+
{
37+
"spatial_size": [2, 2, 2],
38+
"num_samples": 2,
39+
"pos_ratio": 0.0,
40+
"label_spatial_shape": [3, 3, 3],
41+
"fg_indices": [],
42+
"bg_indices": [3, 12, 21],
43+
},
44+
list,
45+
2,
46+
3,
47+
],
3548
]
3649

3750

0 commit comments

Comments
 (0)