{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"# Flyhweel Software Developer Kit (SDK) Basics and Helpful Hints\n",
"\n",
"**Date modified:** 28-July-2022
\n",
"**Authors:** Amy Hegarty, Lena Sherbakov, Intermountain Neuroimaging Consortium\n",
"\n",
"**Description:**
\n",
"The following includes a set of basic SDK commands for Flywheel. Examples included below:\n",
"\n",
" - Basic Flywheel Heirarchy (finding a session)\n",
"\n",
"**Links**
\n",
"\n",
" - Flywheel SDK documentation [here](https://flywheel-io.gitlab.io/product/backend/sdk/branches/master/python/index.html)\n",
" - Flywheel python SDK examples [here](https://gitlab.com/flywheel-io/public/flywheel-tutorials/-/blob/master/python/)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Welcome to Intermountain Neuroimaging Consortium!\n"
]
}
],
"source": [
"print(\"Welcome to Intermountain Neuroimaging Consortium!\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"## Import Python Dependencies"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"import os\n",
"import logging\n",
"import flywheel"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"## Define Supporting Functions"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"## Flywheel API Key and Client\n",
"An API Key is required to interact with the datasets on flywheel db. More on this in the Flywheel SDK doc [here](https://flywheel-io.gitlab.io/product/backend/sdk/branches/master/python/getting_started.html#api-key). Here we pull the API key directly from the user's os enviornment, set within the flywheel command line interface. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"# Instantiate a logger\n",
"logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s %(message)s')\n",
"log = logging.getLogger('root')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"# Create client, using CLI credentials\n",
"fw = flywheel.Client()\n",
"\n",
"# who am I logged in as?\n",
"log.info('You are now logged in as %s to %s', fw.get_current_user()['email'], fw.get_config()['site']['api_url'])"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"## Viewing a Flywheel Project\n",
"The `fw.lookup` function can be used to find any container in flywheel by path. This is an easy way to find projects, subjects, or sessions. Lets view a Flywheel project and list all sessions and their scan date."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"project = fw.lookup('/')\n",
"\n",
"# lookup function pulls a lite version of the project object. Get the actual project object...\n",
"project = fw.get(project.id)\n",
"\n",
"# loop through all project sessions\n",
"for session in project.sessions.find():\n",
"\n",
" # Loop through all sessions in the project container.\n",
" dt = session.timestamp\n",
" if not dt:\n",
" dt = \"NAN\"\n",
" else:\n",
" dt = dt.strftime(\" %x %X\")\n",
" print('%s: Subject: %s Session: %s\\tScanning Date: %s' % (session.id, session.subject.label, session.label, dt))\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"## Viewing a Flywheel Analysis \n",
"Similarly, we can view analyses that have been either uploaded to Flywheel or generated by running flywheel gears. Here, lets look for all analyses attached to a single session."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"# start by finding a session of interest\n",
"session = fw.lookup('///')\n",
"\n",
"# we need to take an extra step to get the full session object, not just basic info \n",
"session_object = fw.get_session(session.id)\n",
"\n",
"# print all the analyses for this session\n",
"for analysis in session_object.analyses:\n",
" if hasattr(analysis.job,'state'):\n",
" print('%s: %s %s' % (analysis.id, analysis.label, analysis.job.state))\n",
" else:\n",
" print('%s: %s' % (analysis.id, analysis.label))"
]
},
{
"cell_type": "markdown",
"source": [
"## Update Metadata In Flywheel\n",
"In this example, lets rename an acquisition to add an \"include-BIDS\" suffix used to indicate to skip BIDS curation for that acquisition."
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%% md\n"
}
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"for acq in session_object.acquisitions():\n",
" # get the full acquisition object\n",
" acq = fw.get(acq.id)\n",
" acq.update({'label':acq.label+\"_ignore-BIDS\"})"
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
}
},
{
"cell_type": "markdown",
"source": [
"
\n",
"\n",
"Looking for more? Check out our github repository with some commonly used SDK examples! (here)<>\n",
"\n",
"### *Thats all Folks!*"
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%% md\n"
}
}
}
],
"metadata": {
"kernelspec": {
"display_name": "Python (myenv)",
"language": "python",
"name": "myenv"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.12"
}
},
"nbformat": 4,
"nbformat_minor": 4
}