Repository navigation
feat(transforms): add ultrasound speckle noise and depth-gain augmentation (#8172) #9319
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
base: dev
Are you sure you want to change the base?
Changes from 2 commits
26c120a
3fb09e3
3cfe834
342169d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -52,7 +52,9 @@ | |
| RandScaleIntensity, | ||
| RandScaleIntensityFixedMean, | ||
| RandShiftIntensity, | ||
| RandSpeckleNoise, | ||
| RandStdShiftIntensity, | ||
| RandUltrasoundDepthGain, | ||
| SavitzkyGolaySmooth, | ||
| ScaleIntensity, | ||
| ScaleIntensityRange, | ||
|
|
@@ -70,6 +72,8 @@ | |
| __all__ = [ | ||
| "RandGaussianNoised", | ||
| "RandRicianNoised", | ||
| "RandSpeckleNoised", | ||
| "RandUltrasoundDepthGaind", | ||
| "ShiftIntensityd", | ||
| "RandShiftIntensityd", | ||
| "ScaleIntensityd", | ||
|
|
@@ -305,6 +309,114 @@ def __call__(self, data: Mapping[Hashable, NdarrayOrTensor]) -> dict[Hashable, N | |
| return d | ||
|
|
||
|
|
||
| class RandSpeckleNoised(RandomizableTransform, MapTransform): | ||
| """ | ||
| Dictionary-based version :py:class:`monai.transforms.RandSpeckleNoise`. | ||
| Add multiplicative speckle noise to image. This transform assumes all the expected fields have same shape, | ||
| if you want to add different noise for every field, please use this transform separately. | ||
|
|
||
| Args: | ||
| keys: keys of the corresponding items to be transformed. | ||
| See also: :py:class:`monai.transforms.compose.MapTransform` | ||
| prob: Probability to add speckle noise to the dictionary. | ||
| std: relative standard deviation of the multiplicative speckle factor. | ||
| Must be non-negative. A larger value produces stronger speckle. | ||
| sample_std: If True, sample the relative standard deviation uniformly | ||
| from 0 to `std` on each call. | ||
| dtype: output data type, if None, same as input image. defaults to float32. | ||
| allow_missing_keys: don't raise exception if key is missing. | ||
| """ | ||
|
|
||
| backend = RandSpeckleNoise.backend | ||
|
|
||
| def __init__( | ||
| self, | ||
| keys: KeysCollection, | ||
| prob: float = 0.1, | ||
| std: float = 0.5, | ||
| sample_std: bool = True, | ||
| dtype: DtypeLike = np.float32, | ||
| allow_missing_keys: bool = False, | ||
| ) -> None: | ||
| MapTransform.__init__(self, keys, allow_missing_keys) | ||
| RandomizableTransform.__init__(self, prob) | ||
| self.rand_speckle_noise = RandSpeckleNoise(prob=1.0, std=std, sample_std=sample_std, dtype=dtype) | ||
|
|
||
| def set_random_state( | ||
| self, seed: int | None = None, state: np.random.RandomState | None = None | ||
| ) -> RandSpeckleNoised: | ||
| super().set_random_state(seed, state) | ||
| self.rand_speckle_noise.set_random_state(seed, state) | ||
| return self | ||
|
|
||
| def __call__(self, data: Mapping[Hashable, NdarrayOrTensor]) -> dict[Hashable, NdarrayOrTensor]: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Document the dictionary method contracts. Both wrappers lack method docstrings for As per path instructions, “Docstrings should be present for all definition” with variables, returns and exceptions in Google-style sections. Also applies to: 407-407
|
||
| d = dict(data) | ||
| self.randomize(None) | ||
| if not self._do_transform: | ||
| for key in self.key_iterator(d): | ||
| d[key] = convert_to_tensor(d[key], track_meta=get_track_meta()) | ||
| return d | ||
|
|
||
| for key in self.key_iterator(d): | ||
| d[key] = self.rand_speckle_noise(d[key], randomize=True) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reuse one speckle factor across the configured fields. The class docstring promises the same noise across same-shaped fields. With two identical images, this call randomizes the child twice and applies separate speckle factors. The dictionary test currently expects that conflicting behavior. Randomize once using the first present field. Then apply the child with
|
||
| return d | ||
|
|
||
|
|
||
| class RandUltrasoundDepthGaind(RandomizableTransform, MapTransform): | ||
| """ | ||
| Dictionary-based version :py:class:`monai.transforms.RandUltrasoundDepthGain`. | ||
| Apply a smooth depth-dependent gain to a B-mode ultrasound image, simulating | ||
| time-gain compensation (TGC) adjustments and depth attenuation. | ||
|
|
||
| Args: | ||
| keys: keys of the corresponding items to be transformed. | ||
| See also: :py:class:`monai.transforms.compose.MapTransform` | ||
| prob: Probability to apply the depth gain to the dictionary. | ||
| gain_range: (minimum, maximum) log-gain slope sampled uniformly. | ||
| A slope of 0 leaves the image unchanged. | ||
| depth_axis: axis of the image along which depth increases. Defaults to 1, | ||
| the first spatial axis for channel-first images. | ||
| dtype: output data type, if None, same as input image. defaults to float32. | ||
| allow_missing_keys: don't raise exception if key is missing. | ||
| """ | ||
|
|
||
| backend = RandUltrasoundDepthGain.backend | ||
|
|
||
| def __init__( | ||
| self, | ||
| keys: KeysCollection, | ||
| prob: float = 0.1, | ||
| gain_range: tuple[float, float] = (-1.0, 1.0), | ||
| depth_axis: int = 1, | ||
| dtype: DtypeLike = np.float32, | ||
| allow_missing_keys: bool = False, | ||
| ) -> None: | ||
| MapTransform.__init__(self, keys, allow_missing_keys) | ||
| RandomizableTransform.__init__(self, prob) | ||
| self.rand_ultrasound_depth_gain = RandUltrasoundDepthGain( | ||
| prob=1.0, gain_range=gain_range, depth_axis=depth_axis, dtype=dtype | ||
| ) | ||
|
|
||
| def set_random_state( | ||
| self, seed: int | None = None, state: np.random.RandomState | None = None | ||
| ) -> RandUltrasoundDepthGaind: | ||
| super().set_random_state(seed, state) | ||
| self.rand_ultrasound_depth_gain.set_random_state(seed, state) | ||
| return self | ||
|
|
||
| def __call__(self, data: Mapping[Hashable, NdarrayOrTensor]) -> dict[Hashable, NdarrayOrTensor]: | ||
| d = dict(data) | ||
| self.randomize(None) | ||
| if not self._do_transform: | ||
| for key in self.key_iterator(d): | ||
| d[key] = convert_to_tensor(d[key], track_meta=get_track_meta()) | ||
| return d | ||
|
|
||
| for key in self.key_iterator(d): | ||
| d[key] = self.rand_ultrasound_depth_gain(d[key], randomize=True) | ||
| return d | ||
|
|
||
|
|
||
| class ShiftIntensityd(MapTransform): | ||
| """ | ||
| Dictionary-based wrapper of :py:class:`monai.transforms.ShiftIntensity`. | ||
|
|
@@ -1987,6 +2099,8 @@ def __call__(self, data: Mapping[Hashable, NdarrayOrTensor]) -> dict[Hashable, N | |
|
|
||
| RandGaussianNoiseD = RandGaussianNoiseDict = RandGaussianNoised | ||
| RandRicianNoiseD = RandRicianNoiseDict = RandRicianNoised | ||
| RandSpeckleNoiseD = RandSpeckleNoiseDict = RandSpeckleNoised | ||
| RandUltrasoundDepthGainD = RandUltrasoundDepthGainDict = RandUltrasoundDepthGaind | ||
| ShiftIntensityD = ShiftIntensityDict = ShiftIntensityd | ||
| RandShiftIntensityD = RandShiftIntensityDict = RandShiftIntensityd | ||
| StdShiftIntensityD = StdShiftIntensityDict = StdShiftIntensityd | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add Google-style docstrings to the new transform methods.
RandSpeckleNoise.__init__(line 262),RandSpeckleNoise.randomize(line 273),RandUltrasoundDepthGain.__init__(line 330), andRandUltrasoundDepthGain.randomize(line 345) have no docstrings. The__call__docstrings at lines 284-287 and 351-354 state only "Apply the transform toimg."The Python path rule requires docstrings for all definitions. Each docstring must document its arguments, return value, and raised exceptions in Google-style sections.
__call__: documentimg,randomize, and the returned tensor. DocumentRuntimeErrorforrandomize=Falsewithout prior randomization. ForRandUltrasoundDepthGain, also documentValueErrorfor an out-of-rangedepth_axis.randomize: document the stored state (speckleorslope). Note thatrandomize=Falsereuses this state and requires a prior call.__init__: document each argument. ForRandUltrasoundDepthGain, document theValueErrorraised whengain_rangedoes not contain exactly two values.