# how to pickle SimpleITK transforms?

**URL:** <https://discourse.itk.org/t/how-to-pickle-simpleitk-transforms/901>\
**Category:** Uncategorized\
**Tags:** simpleitk, python\
**Created:** [May 6, 2018, 2:53pm UTC](https://discourse.itk.org/t/how-to-pickle-simpleitk-transforms/901 "2018-05-06T14:53:11Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adi\_Zholkover](https://discourse.itk.org/letter_avatar_proxy/v4/letter/a/839c29/32.png) [@Adi\_Zholkover](https://discourse.itk.org/u/Adi_Zholkover)\
**Post date:** [May 6, 2018, 2:53pm UTC](https://discourse.itk.org/t/how-to-pickle-simpleitk-transforms/901/1 "2018-05-06T14:53:12Z")

</div>

I’m trying to use python’s multiprocessing Pool.map\_async() with a function that returns a SimpleITK.Transform object, i.e.:

```
    res = []
    for ref_im in ref_imgs:
        res.append(self.pool.apply_async(register_src_ref,
                                         kwds={'moving_img': ref_im, 'fixed_img': src_im, 'scale': scale},
                                         error_callback= self.log_error))
    registration_results = [r.get() for r in res]

```

But I get an error saying:

> SimpleITK.SimpleITK.Transform; proxy of \<Swig Object of type ‘itk::simple::Transform \*’ at 0x000001FB02EC84E0\> \>)'. Reason: ‘TypeError(“can’t pickle SwigPyObject objects”,)’

Any idea how to do this?

Thanks

---

<div class="post-metadata">

**Author:** ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)\
**Post date:** [May 6, 2018, 5:18pm UTC](https://discourse.itk.org/t/how-to-pickle-simpleitk-transforms/901/2 "2018-05-06T17:18:38Z")

</div>

Hello Adi,

Currently SimpleITK does not support pickling of transformations and images, so this will not work when using a process pool. If you change to thread pool (`from multiprocessing.pool import ThreadPool` instead of `from multiprocessing.pool import Pool`) it will work.

As you are performing registration, using the process pool is likely more appropriate than a thread pool. To achieve this, you should have your registration function fully encapsulated (input: image file names, output: transformation written to disk, steps: read images -\> register -\> write resulting transform). This way you will only pass it the file names which are pickled. Even if you read the fixed image multiple times instead of only once, this is a small constant overhead as compared to the time spent on registration.

Hope this helps  
Ziv  
p.s. Please update the thread to let us know what worked for you (or if you have more questions).

---

<div class="post-metadata">

**Author:** ![Adi\_Zholkover](https://discourse.itk.org/letter_avatar_proxy/v4/letter/a/839c29/32.png) [@Adi\_Zholkover](https://discourse.itk.org/u/Adi_Zholkover)\
**Post date:** [May 7, 2018, 8:26am UTC](https://discourse.itk.org/t/how-to-pickle-simpleitk-transforms/901/3 "2018-05-07T08:26:33Z")

</div>

Thanks Ziv,  
I will try your second suggestion.

---

<div class="post-metadata">

**Author:** ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)\
**Post date:** [May 7, 2018, 2:02pm UTC](https://discourse.itk.org/t/how-to-pickle-simpleitk-transforms/901/4 "2018-05-07T14:02:17Z")

</div>

Adding pickling would be great to add to SimpleITK! Could you please create a feature request on Github?

The pickling should be fairly easy for most transforms. A transform is usually just defined by the transform name, the dimensions ( some transforms only work with one dimension size ), the fixed parameters, and the parameters ( the ones which are optimized ). It should be fairly easy to get code working for this. There may need to be some special handling for BSpline transforms and displacement fields, most of the effort would likely need to be put into good testing.

---

<div class="post-metadata">

**Author:** ![Adi\_Zholkover](https://discourse.itk.org/letter_avatar_proxy/v4/letter/a/839c29/32.png) [@Adi\_Zholkover](https://discourse.itk.org/u/Adi_Zholkover)\
**Post date:** [May 7, 2018, 2:07pm UTC](https://discourse.itk.org/t/how-to-pickle-simpleitk-transforms/901/5 "2018-05-07T14:07:09Z")

</div>

github issue: [https://github.com/SimpleITK/SimpleITK/issues/460](https://github.com/SimpleITK/SimpleITK/issues/460)

---

<div class="post-metadata">

**Author:** ![kayarre](https://discourse.itk.org/user_avatar/discourse.itk.org/kayarre/32/669_2.png) [@kayarre](https://discourse.itk.org/u/kayarre)\
**Post date:** [November 26, 2019, 4:36pm UTC](https://discourse.itk.org/t/how-to-pickle-simpleitk-transforms/901/6 "2019-11-26T16:36:13Z")

</div>

It looks like pickable images has been added with [Add support to Python for “pickling” an Image object](https://github.com/SimpleITK/SimpleITK/pull/857), but do transforms also fall into this bucket?  
or do they have to written to file?

I am about to test this, but the process is long running.

---

<div class="post-metadata">

**Author:** ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)\
**Post date:** [November 26, 2019, 5:29pm UTC](https://discourse.itk.org/t/how-to-pickle-simpleitk-transforms/901/7 "2019-11-26T17:29:28Z")

</div>

Hello,

Just support for pickling images was added, for now…

Please let us know how your testing going.

---

<div class="post-metadata">

**Author:** ![kayarre](https://discourse.itk.org/user_avatar/discourse.itk.org/kayarre/32/669_2.png) [@kayarre](https://discourse.itk.org/u/kayarre)\
**Post date:** [November 26, 2019, 6:17pm UTC](https://discourse.itk.org/t/how-to-pickle-simpleitk-transforms/901/8 "2019-11-26T18:17:32Z")

</div>

as I expected from your reply:

```python
Traceback (most recent call last):
  File "process_case.py", line 198, in <module>
    main()
  File "process_case.py", line 169, in main
    nx.write_gpickle(G, pickle_path)
  File "</home/krs/anaconda3/envs/reg/lib/python3.7/site-packages/decorator.py:decorator-gen-620>", line 2, in write_gpickle
  File "/home/krs/anaconda3/envs/reg/lib/python3.7/site-packages/networkx/utils/decorators.py", line 240, in _open_file
    result = func_to_be_decorated(*new_args, **kwargs)
  File "/home/krs/anaconda3/envs/reg/lib/python3.7/site-packages/networkx/readwrite/gpickle.py", line 70, in write_gpickle
    pickle.dump(G, path, protocol)
TypeError: can't pickle SwigPyObject objects

```

---

<div class="post-metadata">

**Author:** ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)\
**Post date:** [November 26, 2019, 6:23pm UTC](https://discourse.itk.org/t/how-to-pickle-simpleitk-transforms/901/9 "2019-11-26T18:23:41Z")

</div>

Is pickling of images working?

---

<div class="post-metadata">

**Author:** ![kayarre](https://discourse.itk.org/user_avatar/discourse.itk.org/kayarre/32/669_2.png) [@kayarre](https://discourse.itk.org/u/kayarre)\
**Post date:** [November 30, 2019, 5:09pm UTC](https://discourse.itk.org/t/how-to-pickle-simpleitk-transforms/901/10 "2019-11-30T17:09:46Z")

</div>

I need to try it still.

---

<div class="post-metadata">

**Author:** ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)\
**Post date:** [November 30, 2019, 8:13pm UTC](https://discourse.itk.org/t/how-to-pickle-simpleitk-transforms/901/11 "2019-11-30T20:13:06Z")

</div>

You may want to try this PR while you are at it. I still want to add more test but it appears to be working.

> <https://github.com/SimpleITK/SimpleITK/pull/893>

---

<div class="post-metadata">

**Author:** ![reox](https://discourse.itk.org/user_avatar/discourse.itk.org/reox/32/4490_2.png) [@reox](https://discourse.itk.org/u/reox)\
**Post date:** [May 18, 2020, 1:42pm UTC](https://discourse.itk.org/t/how-to-pickle-simpleitk-transforms/901/12 "2020-05-18T13:42:43Z")

</div>

Is this PR contained in 2.0.0rc1? I tried it today but pickling Transformations still fails for me:

```
>>> import SimpleITK as sitk
>>> import pickle
>>> pickle.loads(pickle.dumps(sitk.Transform()))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/reox/.local/lib/python3.7/site-packages/SimpleITK/SimpleITK.py", line 4562, in __init__
    _SimpleITK.Transform_swiginit(self, _SimpleITK.new_Transform(*args))
TypeError: Wrong number or type of arguments for overloaded function 'new_Transform'.
  Possible C/C++ prototypes are:
    itk::simple::Transform::Transform()
    itk::simple::Transform::Transform(itk::TransformBase *)
    itk::simple::Transform::Transform(unsigned int,itk::simple::TransformEnum)
    itk::simple::Transform::Transform(itk::simple::Image &,itk::simple::TransformEnum)
    itk::simple::Transform::Transform(itk::simple::Transform const &)

```

I installed SimpleITK via

```auto
pip install --upgrade --pre SimpleITK --find-links https://github.com/SimpleITK/SimpleITK/releases/tag/latest

```

If pickling does not work, is there a workaround? I think one method would be to write the transformation into a file, read that, pickle it and do the same thing on the other end. But that is utterly complicated.

---

<div class="post-metadata">

**Author:** ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)\
**Post date:** [May 18, 2020, 2:15pm UTC](https://discourse.itk.org/t/how-to-pickle-simpleitk-transforms/901/13 "2020-05-18T14:15:35Z")

</div>

Hello,

Thank you for testing the SimpleITK release candidate.

I was able to reproduce the bug you have encountered, with the base sitk::Transform class. However, pickling works with the derived Transform, classes, the ones used likes Affine, BSpline etc.:

```auto
import SimpleITK as sitk
import pickle
print(pickle.loads(pickle.dumps(sitk.AffineTransform(3))))

```

Can you please create a Github issue for this bug report:

> **[SimpleITK/SimpleITK](https://github.com/SimpleITK/SimpleITK/issues)**
>
> SimpleITK: a layer built on top of the Insight Toolkit (ITK), intended to simplify and facilitate ITK's use in rapid prototyping, education and interpreted languages. - SimpleITK/SimpleITK

Thanks!

---

<div class="post-metadata">

**Author:** ![reox](https://discourse.itk.org/user_avatar/discourse.itk.org/reox/32/4490_2.png) [@reox](https://discourse.itk.org/u/reox)\
**Post date:** [May 18, 2020, 2:22pm UTC](https://discourse.itk.org/t/how-to-pickle-simpleitk-transforms/901/14 "2020-05-18T14:22:48Z")

</div>

Ahh I see! Okay, the problem was I was sending composite transformation into pickle…  
But I also found a dirty workaround, I was simply sending FixedParameters and Parameters and re-creating the transformations afterwards.

Bug is created here: [https://github.com/SimpleITK/SimpleITK/issues/1112](https://github.com/SimpleITK/SimpleITK/issues/1112)
