Loading pickled data

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?

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

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)?

1 Like

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.

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 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.

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.

1 Like