Title: Add subjects to collection excluding those in a CSV
Date: 6 April 2020
Description:
# Install specific packages required for this notebook
!pip install flywheel-sdk tqdm pandas
# Import packages
from getpass import getpass
import logging
import os
from pathlib import Path
import re
import pandas as pd
from tqdm.notebook import tqdm
import pandas as pd
import flywheel
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'])
# Source project.label to be used for adding to collection
PROJECT_LABEL = 'MyProject'
# Target collection label
COLLECTION_LABEL = 'MyCollection'
# CSV file path
CSV_PATH = 'path/to/file.csv'
SUBJECT_COL = 'SubjectID'
collection = fw.collections.find_first(f'label={COLLECTION_LABEL}')
if not collection:
collection_id = fw.add_collection({'label': COLLECTION_LABEL})
collection = fw.get(collection_id)
project = fw.projects.find_first(f'label={PROJECT_LABEL}')
Load the csv file and extract subject label to be excluded
df = pd.read_csv(CSV_PATH, dtype={'SubjectID': str})
subjects_to_exclude = df[SUBJECT_COL].values
df.head()
Loops through the subjects in the project and add to collection unless the subject.label
is part of the subject to be excluded (e.g. in subjects_to_exclude
).
subjects = project.subjects()
for sub in tqdm(subjects):
if sub.label not in subjects_to_exclude:
for ses in sub.sessions():
collection.add_sessions(ses.id)