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 centroid transforms #442

Merged
merged 3 commits into from
Nov 4, 2024
Merged

add centroid transforms #442

merged 3 commits into from
Nov 4, 2024

Conversation

benjijamorris
Copy link
Contributor

What does this PR do?

Add transforms for extracting crops based on centroid locations. Includes options for calculating centroids on the fly based on a segmentation image and passing centroids in the data dictionary.

Before submitting

  • Did you make sure title is self-explanatory and the description concisely explains the PR?
  • Did you make sure your PR does only one thing, instead of bundling different changes together?
  • Did you list all the breaking changes introduced by this pull request?
  • Did you test your PR locally with pytest command?
  • Did you run pre-commit hooks with pre-commit run -a command?

Did you have fun?

Make sure you had fun coding 🙃

vianamp
vianamp previously approved these changes Nov 2, 2024
slices.append(slice(start, end))
return tuple(slices)

def _filter_edge(self, centroids, shape, labels=None):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a slight more compact version:

def _filter_edge(self, centroids, shape, labels=None):
    centroids = np.array(centroids)
    crop_half_size = np.array(self.crop_size) // 2

    valid_mask = np.all(
        (centroids >= crop_half_size) & (centroids <= (np.array(shape) - crop_half_size)),
        axis=1
    )

    valid_centroids = centroids[valid_mask]
    if labels is None:
        return valid_centroids, None
    return valid_centroids, np.array(labels)[valid_mask]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! This prompted some other changes to simplify the code.

return crops


class CentroidCropd(CentroidCrop, Transform):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe CentroidCropDict is a better name?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding the d at the end instead of dict is just to be consistent with monai naming conventions

@benjijamorris benjijamorris merged commit 8a88689 into main Nov 4, 2024
4 of 6 checks passed
@benjijamorris benjijamorris deleted the feature/centroid_datasets branch November 4, 2024 18:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants