-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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 Transform to Subtract Images #8301
base: dev
Are you sure you want to change the base?
Conversation
Signed-off-by: Ana Matoso <[email protected]>
Signed-off-by: Ana Matoso <[email protected]>
I, Ana Matoso <[email protected]>, hereby add my Signed-off-by to this commit: 0ff2d45, 6d47218, 670e39d, e7d307b, 7c41ce5, d1f0257, 259ed0f, 6b50562 Signed-off-by: Ana Matoso <[email protected]>
Signed-off-by: Ana Matoso <[email protected]>
Signed-off-by: Author Name <[email protected]>
…ly.github.com> I, Ana Matoso <[email protected]>, hereby add my Signed-off-by to this commit: e7d307b I, Ana Matoso <[email protected]>, hereby add my Signed-off-by to this commit: 7c41ce5 I, Ana Matoso <[email protected]>, hereby add my Signed-off-by to this commit: d1f0257 I, Ana Matoso <[email protected]>, hereby add my Signed-off-by to this commit: 259ed0f I, Ana Matoso <[email protected]>, hereby add my Signed-off-by to this commit: 6b50562 I, Ana Matoso <[email protected]>, hereby add my Signed-off-by to this commit: b837235 I, Ana Matoso <[email protected]>, hereby add my Signed-off-by to this commit: 75d4d15 I, Ana Matoso <[email protected]>, hereby add my Signed-off-by to this commit: 8695428 Signed-off-by: Ana Matoso <[email protected]>
Signed-off-by: Ana Matoso <[email protected]>
…ly.github.com> I, Ana Matoso <[email protected]>, hereby add my Signed-off-by to this commit: 6a6243c Signed-off-by: Ana Matoso <[email protected]>
Signed-off-by: Ana Matoso <[email protected]>
Signed-off-by: Ana Matoso <[email protected]>
…ly.github.com> I, Ana Matoso <[email protected]>, hereby add my Signed-off-by to this commit: 261c81d Signed-off-by: Ana Matoso <[email protected]>
Hi @anamatoso thanks for the contribution. This class is probably something very specific to your use case so I'm not sure about how general purpose it is. We don't have transforms for doing other arithmetic operations, so I wonder if it would be better defining a generic transform which collects selected keys together and applies some callable to those items. This could be used as a new base for |
Hi @ericspod. I guess it is indeed specific to my case as I used it to highlight longitudinal changes between images. I don't know whether other arithmetic functions would be useful, but I agree with you that a generic function that aggregates all these callables is perhaps the way to go. However, in the meantime, I won't have much time to implement that. So, if you agree, I would prefer to commit just this for now, as it does not break existing functionality. Then, later down the line, I could write the aggregator function that would replace this one and ConcatItemsd. What do you think? |
Hi @anamatoso, I agree that there's a specific use case for subtraction but much less so for other operations, I'm still concerned about not adding code we wouldn't expect to keep in the code base since we have to follow our deprecation process. This is minor code so not a large problem, if @KumoLiu or @Nic-Ma have any input I'd like to hear but otherwise I think this is good. For discussion, I would have suggested something like the following to add a lambda transform to apply to multiple members together: class LambdaItemsd(MapTransform):
"""
Apply a callable to specified items from data dictionary together. This will collect the items together into a list
and pass this as the single argument to the supplied callable. If an output name is given, this will key to the call
result in the produced dictionary, otherwise the result value is discarded.
"""
backend = [TransformBackends.TORCH, TransformBackends.NUMPY]
def __init__(
self,
keys: KeysCollection,
output_name: str | None,
func: Callable | None = None,
track_meta: bool = True,
allow_missing_keys: bool = False,
) -> None:
"""
Args:
keys: keys of the corresponding items to be passed to `func` as a single list.
See also: :py:class:`monai.transforms.compose.MapTransform`
output_name: the output_name corresponding to the key to store the result data.
func: function to apply to collected values.
track_meta: if true, the result of the function application will be converted to a MetaTensor.
allow_missing_keys: don't raise exception if key is missing.
"""
super().__init__(keys, allow_missing_keys)
self.output_name = output_name
self._lambd = Lambda(func=func, track_meta=track_meta)
def __call__(self, data: Mapping[Hashable, NdarrayOrTensor]) -> dict[Hashable, NdarrayOrTensor]:
""" """
d = dict(data)
inputs = [d[key] for key in self.key_iterator(d)]
result = self._lambd(inputs)
if self.output_name is not None:
d[self.output_name] = result
return d
test={"a":torch.rand(1,64,64),"b":torch.rand(1,64,64)}
t=LambdaItemsd(["a","b"],"out",lambda items:items[0]+items[1])
result=t(test)
print(result.keys()) |
Hello all,
I've created a transform to subtract two images.
I've created the tests and passed all integration tests.
Hope you find it useful.
Let me know if there is anything else still to do.
Description
Adds a transform for subtracting images.
Types of changes
./runtests.sh -f -u --net --coverage
../runtests.sh --quick --unittests --disttests
.make html
command in thedocs/
folder.