Skip to content

Re using subjects in an existing project

Campbell Allen edited this page Dec 12, 2016 · 1 revision
import csv, os, sys
from panoptes_client import SubjectSet, Subject, Project, Panoptes

# debugger with breakpoints set by pdb.set_trace()
# import pdb

# ensure you credentials are set as shell ENV variables
Panoptes.connect(username=os.environ['USERNAME'], password=os.environ['PASSWORD'])

# change this for the correct project
project = Project.find(slug='tedcheese/whales-as-individuals')

set_name = 'new_subject_set_name'
try:
    # check if the subject set already exits
    subject_set = SubjectSet.where(project_id=project.id, display_name=set_name).next()
    print("Using the existing subject set with id: {}.".format(subject_set.id))
except StopIteration:
    # create a new subject set for the new data and link it to the project above
    subject_set = SubjectSet()
    subject_set.links.project = project
    subject_set.display_name = set_name
    subject_set.save()
    print("Created a new subject set with id: {}.".format(subject_set.id))

# now with the csv subject id manifest, we'll link all the data
with open('existing_subject_ids.csv', 'rb') as csvfile:
    subjects_to_upload = csv.DictReader(csvfile)
    subject_ids_to_link = []

    for row in subjects_to_upload :
        # expected header format: subject_id
        subject_ids_to_link.append(row['subject_id'])

print("Adding {} subjects to the subject set".format(len(subject_ids_to_link)))
subject_set.add(subject_ids_to_link)