How to download a file from datastore using python script?

( #1


I am trying to add testing part to my module using public images from datastore. How can I download the image with python script?


(James Butler) #2

I’m not familiar with the DataStore module, but there are several Slicer modules that use data from the SampleData module for testing purposes. See for an example.

(Andras Lasso) #3

You can download sample data sets from any URL (MIDAS, GitHub, etc.) using SampleData module:

import SampleData
loadedNodes = SampleData.SampleDataLogic().downloadFromSource(SampleData.SampleDataSource(

( #4

Thanks all for the provided information. I used a workaround which works for me as I want the sample to be downloaded in a specific location.

I have only a problem with midas link as the file is created before it is downloaded completely which makes loadVolume fail. For now I am using a different server.

    print("Downloading cochlea sample image ...")
    import urllib
    imgLaWebLink = ""
    #imgLaWebLink = ""
    urllib.urlretrieve (imgLaWebLink , self.outputPath+"/imgA.nrrd" )
except Exception as e:
              print("Error: can not download sample file  ...")
              return -1
#end try-except 
[success, inputVolumeNode] = slicer.util.loadVolume( self.outputPath+"/imgA.nrrd", returnNode=True)

(Andras Lasso) #5

SampleData uses urllib under the hood but has a number of additional features that might be relevant for some use cases. For example downloaded data sets are cached (this is probably the most significant, as downloading a large data set may take tens of seconds), failed, partial, and corrupted downloads are automatically reset/restarted, nodes can be loaded from of zip files (zip files are automatically unpacked), callback can be set for progress reporting, the same data source can be registered with SampleData module to appear in the module’s GUI.