Skip to content

Commit 3479f04

Browse files
committed
Improve the picklability of TraceableTransform
Signed-off-by: Eric Kerfoot <[email protected]>
1 parent 668ad99 commit 3479f04

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

monai/transforms/inverse.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ class TraceableTransform(Transform):
7272
"""
7373

7474
def _init_trace_threadlocal(self):
75+
"""Create a `_tracing` instance member to store the thread-local tracing state value."""
7576
# needed since this class is meant to be a trait with no constructor
7677
if not hasattr(self, "_tracing"):
7778
self._tracing = threading.local()
@@ -81,6 +82,12 @@ def _init_trace_threadlocal(self):
8182
if not hasattr(self._tracing, "value"):
8283
self._tracing.value = MONAIEnvVars.trace_transform() != "0"
8384

85+
def __getstate__(self):
86+
"""When pickling, delete the `_tracing` member first, if present, since it's not picklable."""
87+
if hasattr(self, "_tracing"):
88+
del self._tracing # this can always be re-created with the default value
89+
return super().__getstate__()
90+
8491
@property
8592
def tracing(self) -> bool:
8693
"""

0 commit comments

Comments
 (0)