Created
January 4, 2020 20:51
-
-
Save chelseatroy/4d23c881d4af8b5cd0eeeab2238a688e to your computer and use it in GitHub Desktop.
Original Code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from os import getenv | |
from ..abstract_operation import AbstractOperation | |
from panoptes_client import Panoptes, Project, Subject, SubjectSet | |
from theia.utils import PanoptesUtils | |
class UploadSubject(AbstractOperation): | |
def apply(self, filenames): | |
if self.pipeline.multiple_subject_sets: | |
scope = self.bundle | |
else: | |
scope = self.pipeline | |
self._connect() | |
target_set = self._get_subject_set(scope, self.project.id, scope.name_subject_set()) | |
for filename in filenames: | |
new_subject = self._create_subject(self.project.id, filename) | |
target_set.add(new_subject) | |
def _connect(self): | |
Panoptes.connect( | |
endpoint=PanoptesUtils.base_url(), | |
client_id=PanoptesUtils.client_id(), | |
client_secret=PanoptesUtils.client_secret() | |
) | |
def _get_subject_set(self, scope, project_id, set_name): | |
subject_set = None | |
if not scope.subject_set_id: | |
subject_set = self._create_subject_set(project_id, set_name) | |
scope.subject_set_id = subject_set.id | |
scope.save() | |
else: | |
subject_set = SubjectSet.find(scope.subject_set_id) | |
return subject_set | |
def _create_subject(self, project_id, filename, metadata=None): | |
subject = Subject() | |
subject.links.project = Project.find(project_id) | |
subject.add_location(filename) | |
if metadata: | |
subject.metadata.update(metadata) | |
subject.save() | |
return subject | |
def _create_subject_set(self, project_id, subject_set_name): | |
project = Project.find(project_id) | |
subject_set = SubjectSet() | |
subject_set.display_name = subject_set_name | |
subject_set.links.project = project | |
subject_set.save() | |
return subject_set |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment