How to download a file from datastore using python script?

Hi,

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

Thanks!

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 https://github.com/Slicer/Slicer/blob/2e5dcf178d0d26d359a10437aa16223e3a594e2f/Modules/Scripted/SegmentStatistics/SegmentStatistics.py#L653-L654 for an example.

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

import SampleData
loadedNodes = SampleData.SampleDataLogic().downloadFromSource(SampleData.SampleDataSource(
    nodeNames='fixed', 
    fileNames='fixed.nrrd', 
    uris='http://slicer.kitware.com/midas3/download/item/157188/small-mr-eye-fixed.nrrd'))

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.

try:         
    print("Downloading cochlea sample image ...")
    import urllib
    imgLaWebLink = "https://mtixnat.uni-koblenz.de/owncloud/index.php/s/eMvm9LHNHEHoZg3/download"
    #imgLaWebLink = "http://slicer.kitware.com/midas3/download/item/381221/P100001_DV_L_a"
    urllib.urlretrieve (imgLaWebLink , self.outputPath+"/imgA.nrrd" )
except Exception as e:
              print("Error: can not download sample file  ...")
              print(e)   
              return -1
#end try-except 
[success, inputVolumeNode] = slicer.util.loadVolume( self.outputPath+"/imgA.nrrd", returnNode=True)

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.

1 Like