1- from neuroscout_cli .commands .base import Command
2- from neuroscout_cli import __version__ as VERSION
3- from datalad .api import install , get , unlock
4- from pathlib import Path
5- from shutil import copy
61import json
72import tarfile
83import logging
4+ import sys
5+
6+ from pathlib import Path
7+ from shutil import copy
8+ from packaging import version
9+ from neuroscout_cli .commands .base import Command
10+ from neuroscout_cli import __version__ as VERSION
11+ from datalad .api import install , get , unlock
912from bids .utils import convert_JSON
1013from bids import BIDSLayout
11- from packaging import version
12- import sys
1314
1415
1516class Install (Command ):
@@ -20,17 +21,22 @@ class Install(Command):
2021 def __init__ (self , options , * args , ** kwargs ):
2122 super ().__init__ (options , * args , ** kwargs )
2223 self .install_dir = Path (self .options .pop ('--install-dir' ))
24+ self .resources = None
25+ self .dataset_dir = None
26+ self .bundle_dir = None
27+ self .preproc_dir = None
2328
2429 def download_bundle (self ):
30+ """ Download analysis bundle and setup preproc dir """
2531 # Download bundle
2632 if not self .bundle_cache .exists ():
2733 logging .info ("Downloading bundle..." )
2834 self .api .analyses .get_bundle (self .bundle_id , self .bundle_cache )
2935
3036 # Un-tarzip, and read in JSON files
31- with tarfile .open (self .bundle_cache ) as tf :
37+ with tarfile .open (self .bundle_cache ) as tF :
3238 self .resources = json .loads (
33- tf .extractfile ('resources.json' ).read ().decode ("utf-8" ))
39+ tF .extractfile ('resources.json' ).read ().decode ("utf-8" ))
3440
3541 self .dataset_dir = self .install_dir / \
3642 self .resources ['dataset_name' ]
@@ -40,33 +46,36 @@ def download_bundle(self):
4046 # Extract to bundle_dir
4147 if not self .bundle_dir .exists ():
4248 self .bundle_dir .mkdir (parents = True , exist_ok = True )
43- tf .extractall (self .bundle_dir )
49+ tF .extractall (self .bundle_dir )
4450 logging .info (
45- "Bundle installed at {}" .format (
46- self .bundle_dir .absolute ()))
51+ "Bundle installed at %s" , self .bundle_dir .absolute ()
52+ )
53+
54+ # Set up preproc dir w/ DataLad (but don't download)
55+ self .preproc_dir = Path (self .dataset_dir ) / 'derivatives'
56+
57+ if not self .preproc_dir .exists ():
58+ # Use datalad to install the preproc dataset
59+ install (source = self .resources ['preproc_address' ],
60+ path = str (self .preproc_dir ))
61+
62+ for option in ['preproc' , 'fmriprep' ]:
63+ if (self .preproc_dir / option ).exists ():
64+ self .preproc_dir = self .preproc_dir / option
65+ break
4766
4867 # Check version
4968 self ._check_version ()
5069
5170 return self .bundle_dir .absolute ()
5271
5372 def download_data (self ):
73+ """ Use DataLad to download necessary data to disk """
5474 bundle_dir = self .download_bundle ()
55- self .preproc_dir = Path (self .dataset_dir ) / 'derivatives'
5675 with (bundle_dir / 'model.json' ).open () as f :
5776 model = convert_JSON (json .load (f ))
5877
5978 try :
60- if not self .preproc_dir .exists ():
61- # Use datalad to install the preproc dataset
62- install (source = self .resources ['preproc_address' ],
63- path = str (self .preproc_dir ))
64-
65- for option in ['preproc' , 'fmriprep' ]:
66- if (self .preproc_dir / option ).exists ():
67- self .preproc_dir = self .preproc_dir / option
68- break
69-
7079 # Get all JSON files
7180 jsons = list (self .preproc_dir .rglob ('*.json' ))
7281 if jsons :
@@ -87,12 +96,12 @@ def download_data(self):
8796 if self .options .pop ('--unlock' , False ):
8897 unlock (paths )
8998
90- except Exception as e :
91- if hasattr (e , 'failed' ):
92- message = e .failed [0 ]['message' ]
99+ except Exception as exp :
100+ if hasattr (exp , 'failed' ):
101+ message = exp .failed [0 ]['message' ]
93102 raise ValueError ("Datalad failed. Reason: {}" .format (message ))
94103 else :
95- raise ( e )
104+ raise exp
96105
97106 # Copy meta-data to root of dataset_dir
98107 copy (list (self .bundle_dir .glob ('task-*json' ))[0 ], self .preproc_dir )
@@ -108,7 +117,7 @@ def _check_version(self):
108117 "\n "
109118 "-----------------------------------------------------------\n "
110119 "Insufficient version of neurosout-cli! \n "
111- f"This bundle requires v{ str (req )} or greater , and you current have v{ VERSION } \n "
120+ f"This bundle requires v{ str (req )} + , and you have v{ VERSION } \n "
112121 "Please upgrade neuroscout-cli by running: \n "
113122 "'docker pull neuroscout/neuroscout-cli' to continue. \n "
114123 "-----------------------------------------------------------\n "
0 commit comments