Skip to content
This repository was archived by the owner on Dec 27, 2022. It is now read-only.

Commit 08fa933

Browse files
committed
Add unsigned s3 client. Allow caching of downloaded files. Fix typo in mri_convert.
1 parent 64edf20 commit 08fa933

File tree

3 files changed

+30
-20
lines changed

3 files changed

+30
-20
lines changed

preafq/base.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
from collections import namedtuple
66

7+
import boto3
8+
from botocore import UNSIGNED
9+
from botocore.client import Config
10+
11+
712
# Define the different namedtuple return types
813
InputFiles = namedtuple(
914
'InputFiles',
@@ -15,3 +20,6 @@
1520
'InputFilesWithSession',
1621
['subject', 'site', 'session', 'files', 'file_type']
1722
)
23+
24+
# Global s3 client to preserve anonymous config
25+
s3_client = boto3.client('s3', config=Config(signature_version=UNSIGNED))

preafq/fetch_bids_s3.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
import boto3
1313

14-
from .base import InputFiles, InputFilesWithSession
14+
from .base import InputFiles, InputFilesWithSession, s3_client
1515

1616

1717
mod_logger = logging.getLogger(__name__)
@@ -108,12 +108,10 @@ def get_s3_keys(prefix, site, bucket='fcp-indi'):
108108
list
109109
All the keys matching the prefix and site in the S3 bucket
110110
"""
111-
s3 = boto3.client('s3')
112-
113111
# Avoid duplicate trailing slash in prefix
114112
prefix = prefix.rstrip('/')
115113

116-
response = s3.list_objects_v2(
114+
response = s3_client.list_objects_v2(
117115
Bucket=bucket,
118116
Prefix=prefix + '/' + site + '/',
119117
)
@@ -127,7 +125,7 @@ def get_s3_keys(prefix, site, bucket='fcp-indi'):
127125
)
128126

129127
while response['IsTruncated']:
130-
response = s3.list_objects_v2(
128+
response = s3_client.list_objects_v2(
131129
Bucket=bucket,
132130
Prefix=prefix + '/' + site + '/',
133131
ContinuationToken=response['NextContinuationToken']
@@ -230,7 +228,8 @@ def get_subject_id(key):
230228
return s3_registers
231229

232230

233-
def download_register(subject_keys, bucket='fcp-indi', directory='./input'):
231+
def download_register(subject_keys, bucket='fcp-indi', directory='./input',
232+
overwrite=False):
234233
"""
235234
Parameters
236235
----------
@@ -246,6 +245,9 @@ def download_register(subject_keys, bucket='fcp-indi', directory='./input'):
246245
directory : string
247246
Local directory to which to save files
248247
248+
overwrite : bool
249+
Flag to overwrite existing files
250+
249251
Returns
250252
-------
251253
files : InputFiles namedtuple
@@ -256,7 +258,6 @@ def download_register(subject_keys, bucket='fcp-indi', directory='./input'):
256258
'files' : local file paths,
257259
'file_type' : 'local',
258260
"""
259-
s3 = boto3.client('s3')
260261
subject = subject_keys.subject
261262
site = subject_keys.site
262263

@@ -275,10 +276,13 @@ def download_register(subject_keys, bucket='fcp-indi', directory='./input'):
275276
def download_from_s3(fname_, bucket_, key_):
276277
# Create the directory and file if necessary
277278
Path(op.dirname(fname_)).mkdir(parents=True, exist_ok=True)
278-
Path(fname_).touch(exist_ok=True)
279+
try:
280+
Path(fname_).touch(exist_ok=overwrite)
279281

280-
# Download the file
281-
s3.download_file(Bucket=bucket_, Key=key_, Filename=fname_)
282+
# Download the file
283+
s3_client.download_file(Bucket=bucket_, Key=key_, Filename=fname_)
284+
except FileExistsError:
285+
mod_logger.info('File {fname:s} already exists. Continuing...')
282286

283287
s3keys = subject_keys.files
284288
files = input_files.files
@@ -366,14 +370,12 @@ def determine_directions(input_files,
366370
''.format(files=required_json - set(json_files))
367371
)
368372

369-
s3 = boto3.client('s3')
370-
371373
def get_json(json_file):
372374
if input_type == 'local':
373375
with open(json_file, 'r') as fp:
374376
meta = json.load(fp)
375377
else:
376-
response = s3.get_object(
378+
response = s3_client.get_object(
377379
Bucket=bucket,
378380
Key=json_file,
379381
)

preafq/preafq.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def move_t1_to_freesurfer(t1_file):
3030
"""
3131
freesurfer_path = op.join(op.dirname(t1_file), 'freesurfer')
3232

33-
convert_cmd = 'mriconvert {in_:s} {out_:s}'.format(
33+
convert_cmd = 'mri_convert {in_:s} {out_:s}'.format(
3434
in_=t1_file, out_=op.join(freesurfer_path, 'mri', 'orig.mgz')
3535
)
3636

@@ -104,12 +104,12 @@ def pre_afq_individual(input_s3_keys, s3_prefix, out_bucket,
104104
out_dir = op.join(workdir, 'output')
105105

106106
run_preAFQ(
107-
dwi_file=input_files.files['dwi'],
108-
dwi_file_AP=input_files.files['epi_ap'],
109-
dwi_file_PA=input_files.files['epi_pa'],
110-
bvec_file=input_files.files['bvec'],
111-
bval_file=input_files.files['bval'],
112-
subjects_dir=op.dirname(input_files.files['t1w']),
107+
dwi_file=input_files.files['dwi'][0],
108+
dwi_file_AP=input_files.files['epi_ap'][0],
109+
dwi_file_PA=input_files.files['epi_pa'][0],
110+
bvec_file=input_files.files['bvec'][0],
111+
bval_file=input_files.files['bval'][0],
112+
subjects_dir=op.dirname(input_files.files['t1w'][0]),
113113
working_dir=scratch_dir,
114114
out_dir=out_dir,
115115
)

0 commit comments

Comments
 (0)