Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function in monai.transforms.utils.py #7712

Merged
merged 30 commits into from
May 21, 2024
Merged
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
a78c5a6
Fixes #6704
ytl0623 Apr 25, 2024
4797043
Merge branch 'Project-MONAI:dev' into fix-issue-6704
ytl0623 Apr 27, 2024
f04a29e
Update monai/transforms/utils.py
ytl0623 Apr 27, 2024
7a31661
Add a unittest and modify few codes.
ytl0623 Apr 27, 2024
e66ae71
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 27, 2024
df0f0d0
Merge branch 'Project-MONAI:dev' into fix-issue-6704
ytl0623 May 14, 2024
a06511b
Apply suggestions from code review
ytl0623 May 14, 2024
30d4876
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 14, 2024
535675d
Update monai/transforms/utils.py
ytl0623 May 14, 2024
656f56a
import copy
ytl0623 May 14, 2024
32c888d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 14, 2024
9602d3a
import copy
ytl0623 May 14, 2024
12e5881
remove backup file :")
ytl0623 May 14, 2024
e2641d4
problem solved: Imports are incorrectly sorted and/or formatted.
ytl0623 May 14, 2024
639022c
Reformat utils.py
ytl0623 May 14, 2024
eeae642
solve utils.py line: 388 too long.
ytl0623 May 14, 2024
8ba5c7e
Solved import error.
ytl0623 May 14, 2024
d3ba184
Add function in _init_.py
ytl0623 May 14, 2024
f1f9a80
Update monai/transforms/utils.py
ytl0623 May 15, 2024
4ec2b64
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 15, 2024
dbc86e4
Reformatted utils.py
ytl0623 May 15, 2024
7a8c7cd
Merge branch 'dev' into fix-issue-6704
ytl0623 May 16, 2024
f599cbe
20240516
ytl0623 May 16, 2024
2259678
Merge branch 'dev' into fix-issue-6704
ytl0623 May 17, 2024
e394797
Merge branch 'dev' into fix-issue-6704
ytl0623 May 17, 2024
943708e
Update utils.py
ytl0623 May 17, 2024
58ea019
Update utils.py
ytl0623 May 17, 2024
317fc06
Fix long line
ytl0623 May 17, 2024
6ba0a04
Merge branch 'dev' into fix-issue-6704
ytl0623 May 17, 2024
53f472b
Merge branch 'dev' into fix-issue-6704
KumoLiu May 21, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions monai/transforms/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
"in_bounds",
"is_empty",
"is_positive",
"map_and_generate_sampling_centers",
"map_binary_to_indices",
"map_classes_to_indices",
"map_spatial_axes",
Expand Down Expand Up @@ -368,6 +369,64 @@ def check_non_lazy_pending_ops(
warnings.warn(msg)


def map_and_generate_sampling_centers(
ytl0623 marked this conversation as resolved.
Show resolved Hide resolved
label: NdarrayOrTensor,
spatial_size: Sequence[int] | int,
num_samples: int,
label_spatial_shape: Sequence[int],
ytl0623 marked this conversation as resolved.
Show resolved Hide resolved
indices: Sequence[NdarrayOrTensor],
ytl0623 marked this conversation as resolved.
Show resolved Hide resolved
ytl0623 marked this conversation as resolved.
Show resolved Hide resolved
num_classes: int | None = None,
image: NdarrayOrTensor | None = None,
image_threshold: float = 0.0,
max_samples_per_class: int | None = None,
ratios: list[float | int] | None = None,
rand_state: np.random.RandomState | None = None,
allow_smaller: bool = False,
warn: bool = True,
) -> tuple[tuple]:
"""
Combine "map_classes_to_indices" and "generate_label_classes_crop_centers" functions, return crop center coordinates.
ytl0623 marked this conversation as resolved.
Show resolved Hide resolved

Args:
label: use the label data to get the indices of every class.
spatial_size: spatial size of the ROIs to be sampled.
num_samples: total sample centers to be generated.
label_spatial_shape: spatial shape of the original label data to unravel selected centers.
indices: sequence of pre-computed foreground indices of every class in 1 dimension.
num_classes: number of classes for argmax label, not necessary for One-Hot label.
image: if image is not None, only return the indices of every class that are within the valid
region of the image (``image > image_threshold``).
image_threshold: if enabled `image`, use ``image > image_threshold`` to
determine the valid image content area and select class indices only in this area.
max_samples_per_class: maximum length of indices in each class to reduce memory consumption.
Default is None, no subsampling.
ratios: ratios of every class in the label to generate crop centers, including background class.
if None, every class will have the same ratio to generate crop centers.
rand_state: numpy randomState object to align with other modules.
allow_smaller: if `False`, an exception will be raised if the image is smaller than
the requested ROI in any dimension. If `True`, any smaller dimensions will be set to
match the cropped size (i.e., no cropping in that dimension).
warn: if `True` prints a warning if a class is not present in the label.

ytl0623 marked this conversation as resolved.
Show resolved Hide resolved
"""
indices_ = indices if indices is None else indices
ytl0623 marked this conversation as resolved.
Show resolved Hide resolved
if indices_ is None:
if label is None:
ytl0623 marked this conversation as resolved.
Show resolved Hide resolved
raise ValueError("label must not be None.")
ytl0623 marked this conversation as resolved.
Show resolved Hide resolved
indices_ = map_classes_to_indices(label, num_classes, image, image_threshold, max_samples_per_class)
_shape = None
if label is not None:
_shape = label.peek_pending_shape() if isinstance(label, monai.data.MetaTensor) else label.shape[1:]
elif image is not None:
_shape = image.peek_pending_shape() if isinstance(image, monai.data.MetaTensor) else image.shape[1:]
if _shape is None:
raise ValueError("label or image must be provided to infer the output spatial shape.")
centers = generate_label_classes_crop_centers(
spatial_size, num_samples, _shape, indices_, ratios, rand_state, allow_smaller, warn
)
return ensure_tuple(centers)


def map_binary_to_indices(
label: NdarrayOrTensor, image: NdarrayOrTensor | None = None, image_threshold: float = 0.0
) -> tuple[NdarrayOrTensor, NdarrayOrTensor]:
Expand Down
Loading