Exemplo n.º 1
0
def make_subjectset_from_collection(request):
    # if this is a POST request we need to process the form data
    if request.method == 'POST':

        # create a form instance and populate it with data from the request:
        form = SearchForm(request.POST)
        # check whether it's valid:
        if form.is_valid():
            project = panoptes_client.Project.find(1104)
            username = str(form.cleaned_data['username'])
            collection_display_name = str(
                form.cleaned_data['collection_display_name'])

            subjects_in_collection = retrieve_subjects_from_collection(
                username, collection_display_name)
            subject_set = panoptes_client.SubjectSet()

            subject_set.links.project = project
            subject_set.display_name = '{0}'.format(collection_display_name)

            subject_set.save()
            subject_set.add(subjects_in_collection)

            #workflow = panoptes_client.Workflow()
            #workflow.display_name = '{0}'.format(collection_display_name)
            #project.add_workflows(workflow)
            #workflow.add_subject_sets(subject_set)

            return HttpResponse('Success!', content_type="text/plain")
Exemplo n.º 2
0
    try:
        project = utils.connect(projid)
    except PanoptesAPIException:
        return False

    if opts.subject:
        try:
            subject_set = pan.SubjectSet.find(opts.subject)
        except PanoptesAPIException as e:
            log.error("Could not find subject set id")
            for arg in e.args:
                log.error("> " + arg)
            return False
    else:
        log.info("Creating new subject set")
        subject_set = pan.SubjectSet()
        subject_set.links.project = project

        while(True):
            name = input("Enter subject set display name: ")
            try:
                subject_set.display_name = name
                subject_set.save()
            except PanoptesAPIException as e:
                log.error("Could not set subject set display name")
                for arg in e.args:
                    if arg == 'You must be logged in to access this resource.':
                        log.error("User credentials invalid")
                        exit(False)
                    log.error("> " + arg)
                    if arg == 'Validation failed:' \
Exemplo n.º 3
0
def upload(imgdir, projid, opts, **kwargs):
    '''
    %prog upload imgdir zoo_proj_id
    Does:
        - Uploads images from the specified image directory to zooniverse
          project specified by zoo_project_id.
        - Will also generate a manifest if one is not already present inside
          imgdir.
    Note:
        - This program uploads only images listed in the manifest.csv file.
    Args:
        - imgdir
            -type: str
            -desc: The directory of the images to be uploaded
        - proj_id
            -type: str
            -desc: The zooniverse project id to upload the images to.
    Returns:
        None
    '''

    if opts.quiet:
        log.setLevel(logging.INFO)

    if not osp.isdir(imgdir):
        log.error("Image directory '{}' does not exist".format(imgdir))
        return False

    try:
        project = utils.connect(projid)
    except PanoptesAPIException:
        return False

    if opts.subject:
        try:
            subject_set = pan.SubjectSet.find(opts.subject)
        except PanoptesAPIException as e:
            log.error("Could not find subject set id")
            for arg in e.args:
                log.error("> " + arg)
            return False
    else:
        log.info("Creating new subject set")
        subject_set = pan.SubjectSet()
        subject_set.links.project = project

        while (True):
            name = input("Enter subject set display name: ")
            try:
                subject_set.display_name = name
                subject_set.save()
            except PanoptesAPIException as e:
                log.error("Could not set subject set display name")
                for arg in e.args:
                    if arg == 'You must be logged in to access this resource.':
                        log.error("User credentials invalid")
                        exit(False)
                    log.error("> " + arg)
                    if arg == 'Validation failed:' \
                              + ' Display name has already been taken':
                        log.info("To use {} as the display name," +
                                 " get the subject set id from zooniverse" +
                                 " and call this command with --subject <id>")
                        if not utils.get_yn('Try again?'):
                            exit(False)
                continue

            break

    # NOTE: This would need to be cross-platform and efficient
    #       I am removing this feature and leaving file compression to
    #       the user.
    '''
    if opts.convert:
        log.info("Compressing and converting to jpg")
        log.critical("Warning: All jpg files will be overwritten.")

        if utils.get_yn("Continue?"):
            utils.convert(imgdir)
    '''

    if not osp.isfile(osp.join(imgdir, 'manifest.csv')):
        log.info("Generating manifest")
        if opts.extension:
            manif_gen_succeeded = manifest(imgdir, ext=opts.extension)
        else:
            manif_gen_succeeded = manifest(imgdir)
        if not manif_gen_succeeded:
            log.error("No images to upload.")
            return False

    mfile = open(osp.join(imgdir, 'manifest.csv'), 'r')
    fieldnames = mfile.readline().strip().split(",")
    mfile.seek(0)
    reader = csv.DictReader(mfile)

    if 'filename' not in fieldnames:
        log.error("Manifest file must have a 'filename' column")
        return False

    log.info("Loading images from manifest...")
    error_count = 0
    success_count = 0
    project.reload()

    for row in reader:
        try:
            # getsize returns file size in bytes
            filesize = osp.getsize(row['filename']) / 1000
            if filesize > 256:
                log.warning(
                    "File size of {}KB is larger than recommended 256KB".
                    format(filesize))

            temp_subj = pan.Subject()
            temp_subj.add_location(row['filename'])
            temp_subj.metadata.update(row)
            temp_subj.links.project = project
            temp_subj.save()
            subject_set.add(temp_subj)
        except PanoptesAPIException as e:
            error_count += 1
            log.error("Error on row: {}".format(row))
            for arg in e.args:
                log.error("> " + arg)
            try:
                log.info("Trying again...")
                subject_set.add(temp_subj)
            except PanoptesAPIException as e2:
                for arg in e2.args:
                    log.error("> " + arg)
                log.info("Skipping")
                continue
            success_count += 1

        success_count += 1
        log.debug("{}- {} - success".format(success_count,
                                            str(osp.basename(
                                                row['filename']))))

    log.info("DONE")
    log.info("Summary:")
    log.info("  Upload completed at: " +
             dt.now().strftime("%H:%M:%S %m/%d/%y"))
    log.info("  {} of {} images loaded".format(success_count,
                                               success_count + error_count))
    log.info("\n")
    log.info("Remember to link your workflow to this subject set")

    return True
Exemplo n.º 4
0
def make_subjectset_from_collection(request):
    # if this is a POST request we need to process the form data
    if request.method == 'POST':

        # create a form instance and populate it with data from the request:
        form = SearchForm(request.POST)
        # check whether it's valid:
        if form.is_valid():
            username = str(form.cleaned_data['username'])
            collection_display_name = str(
                form.cleaned_data['collection_display_name'])
            workflow_name = str(form.cleaned_data['workflow_name'])

            client = panoptes_client.Panoptes()
            client.connect(username=os.environ.get('PANOPTES_USERNAME'),
                           password=os.environ.get('PANOPTES_PASSWORD'))

            subjects_in_collection, collection_display_url = retrieve_subjects_from_collection(
                username, collection_display_name)
            subject_set = panoptes_client.SubjectSet()

            subject_set.links.project = '6040'
            subject_set.display_name = '{0}'.format(collection_display_name)

            subject_set.save()
            subject_set.add(subjects_in_collection)

            workflow = panoptes_client.Workflow()
            workflow.display_name = '{0}'.format(workflow_name)
            workflow.links.project = '6040'
            workflow.primary_language = 'en'
            image_location_str = "![Example Alt Text]({0} =400x275)".format(
                collection_display_url)
            workflow.tasks = {
                u'T0': {
                    u'answers': [{
                        u'label': u'Yes'
                    }, {
                        u'label': u'No'
                    }],
                    u'help':
                    u'',
                    u'question':
                    u'Does this image look like \n{0}'.format(
                        image_location_str),
                    u'required':
                    True,
                    u'type':
                    u'single'
                }
            }
            workflow.first_task = "T0"
            workflow.save()
            workflow.mobile_friendly = True
            workflow.retirement = {
                u'criteria': u'classification_count',
                u'options': {
                    u'count': 5
                }
            }
            workflow.save()

            workflow.add_subject_sets(subject_set)

            return redirect(
                "https://www.zooniverse.org/projects/sbc538/vet-new-classes/")
        else:
            return render(request, 'subjectset_from_collection_form.html',
                          {'form': form})