Title: Run local analysis and upload back to Flywheel
Date: 15-Apr-2020
Description:
Find and download input file from flywheel, process locally, create an analysis container and upload local process outputs to it.
Topics that will be covered:
# Install specific packages required for this notebook
!pip install flywheel-sdk nipype
# Import packages
from getpass import getpass
import logging
import os
from pathlib import Path
import flywheel
import nipype
from nipype.interfaces.image import Reorient
from permission import check_user_permission
# Instantiate a logger
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s %(message)s')
log = logging.getLogger('root')
Get a API_KEY. More on this in the Flywheel SDK doc here.
API_KEY = getpass('Enter API_KEY here: ')
Instantiate the Flywheel API client
fw = flywheel.Client(API_KEY if 'API_KEY' in locals() else os.environ.get('FW_KEY'))
Show Flywheel logging information
log.info('You are now logged in as %s to %s', fw.get_current_user()['email'], fw.get_config()['site']['api_url'])
Before starting off, we want to check your permission on the Flywheel Instance in order to proceed in this notebook.
min_reqs = {
"site": "user",
"group": "ro",
"project": ['analyses_create_sdk','files_view_contents','files_download','files_create_upload',]
}
GROUP_ID = input('Please enter the Group ID that you will use in this notebook: ')
PROJECT_LABEL = input('Please enter the Project Labelt that you will use in this notebook: ')
check_user_permission
will return True if both the group and project meet the minimum requirement, else a compatible list will be printed.
check_user_permission(fw, min_reqs, group=GROUP_ID, project=PROJECT_LABEL)
# Flywheel path to the acquistion container with the file we want to process locally
FW_PATH_TO_ACQ = '<you-group>/<your-project>/<subject.label>/<session.label>/<acquisition.label>'
# Filename of the file we want to process locally
FILENAME = '<the-input-filename-here.ext>'
# Path where the input files will be download
DOWNLOAD_PATH = '/tmp'
Find the flywheel acquisition container by performing a lookup
acquisition = fw.lookup(FW_PATH_TO_ACQ)
Find the file in that acquisition container by name
file = acquisition.get_file(FILENAME)
dest_path = str(Path(DOWNLOAD_PATH) / FILENAME)
file.download(dest_path)
This is a very simple processing which is just reorienting the nifti image
reorient = Reorient(orientation='LPS')
reorient.inputs.in_file = dest_path
res = reorient.run()
out_file = res.outputs.out_file
log.info('Output file saved to: %s', out_file)
Create an analysis container attached to the session with reference to the input files
session = fw.get_session(acquisition.parents.session)
analysis = session.add_analysis(label='My Analysis label', inputs=[file.ref()])
analysis.upload_output(out_file)
analysis = analysis.reload()
assert analysis.files[0].name == os.path.basename(out_file)
assert analysis.files[0].size > 0