# Loading pickled data

**URL:** https://discourse.slicer.org/t/loading-pickled-data/21087
**Category:** Development
**Tags:** scripted, pickle, data-loading
**Created:** [December 16, 2021, 10:21am UTC](https://discourse.slicer.org/t/loading-pickled-data/21087 "2021-12-16T10:21:48Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![koeglfryderyk](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/koeglfryderyk/32/14638_2.png) [@koeglfryderyk](https://discourse.slicer.org/u/koeglfryderyk)
#### Post date: [December 16, 2021, 10:21am UTC](https://discourse.slicer.org/t/loading-pickled-data/21087/1 "2021-12-16T10:21:48Z")

</div>

I want to load pickled data from the GUI in my custom extension.

When using the ‘Add data’ dialog to try to load such data, Slicer throws an error (Error - load failed).

As a workaround, I thought I could use the ‘Add data’ dialog to get the path to the required file and then load it in my scripted module.

However, I could not find out how to access this path. I only managed to open the dialog with `slicer.util.openAddVolumeDialog()`.

How can I access this path?

---

<div class="post-metadata">

### Author: ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)
#### Post date: [December 16, 2021, 5:32pm UTC](https://discourse.slicer.org/t/loading-pickled-data/21087/2 "2021-12-16T17:32:26Z")

</div>

You can add a custom reader for any file format using a short Python script. See example here:

> <https://github.com/Slicer/Slicer/blob/8381ef3e6d73afc9f15ac0db5ddb7de753c90641/Applications/SlicerApp/Testing/Python/SlicerScriptedFileReaderWriterTest.py>

That said, pickling is intended for temporary storage, not for archival or sharing. So, loading pickled data via “Add data” does not sound like a good idea. Using any of the standard file formats, and maybe using a simple human-readable container file format, such as json for custom metadata would be more appropriate.

Can you tell a bit more about your use case? What generates the pickled data? What kind of data you pickle? Have you considered using a standard file format instead (nrrd for images, ply for meshes, etc)?

---

<div class="post-metadata">

### Author: ![koeglfryderyk](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/koeglfryderyk/32/14638_2.png) [@koeglfryderyk](https://discourse.slicer.org/u/koeglfryderyk)
#### Post date: [December 17, 2021, 3:38pm UTC](https://discourse.slicer.org/t/loading-pickled-data/21087/3 "2021-12-17T15:38:22Z")

</div>

Thank you!

Just a follow-up regarding the custom reader - do I add this class to the same file where all my custom extension code is?

A colleague of mine creates landmarks in US and MR images in his custom software, and from there he saves the landmarks. The landmarks are stored in a numpy array which he then pickles.

I understand that pickles are not created for such a use case, but I’m not sure I’ll be able to change his mind to adapt his workflow.

---

<div class="post-metadata">

### Author: ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)
#### Post date: [December 17, 2021, 4:02pm UTC](https://discourse.slicer.org/t/loading-pickled-data/21087/4 "2021-12-17T16:02:25Z")

</div>

Saving the data in json should be almost as simple as pickling and would have many advantages (I don’t even want to get started on that). You could even use Slicer’s [markup json file format](https://slicer.readthedocs.io/en/latest/developer_guide/modules/markups.html#markups-json-file-format-mrk-json) and then you would not need an importer at all, you could just drag-and-drop the files into Slicer to load them and could directly save them from Slicer.

> [@koeglfryderyk](#):
>
> do I add this class to the same file where all my custom extension code is?

Yes, you put the `MyModuleFileReader` and `MyModuleFileWriter` classes into any scripted module in your extension and Slicer automatically discovers them when the module is loaded. `MyModule` is the name of the main module class.
