How to use the psiturk.models.Participant function in PsiTurk

To help you get started, we’ve selected a few PsiTurk examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Dallinger / Dallinger / examples / rogers / tests.py View on Github external
exp.networks()[0].max_size,
                        len(exp.networks()[0].nodes(failed=True)),
                        p_times[-1]),
                        end="\r")
                else:
                    print("Running simulated experiment... participant {} of {}, {} participants failed.".format(
                        num_completed_participants+1,
                        exp.networks()[0].max_size,
                        len(exp.networks()[0].nodes(failed=True))),
                        end="\r")
                sys.stdout.flush()

                worker_id = str(random.random())
                assignment_id = str(random.random())
                from psiturk.models import Participant
                p = Participant(workerid=worker_id, assignmentid=assignment_id, hitid=hit_id)
                self.db.add(p)
                self.db.commit()
                p_id = p.uniqueid
                p_ids.append(p_id)
                p_start_time = timenow()

                while True:
                    assign_start_time = timenow()
                    network = exp.get_network_for_participant(participant_id=p_id)
                    if network is None:
                        break
                    else:
                        agent = exp.make_node_for_participant(
                            participant_id=p_id,
                            network=network)
                        exp.add_node_to_network(
github NYUCCL / psiTurk / tests / test_tasks.py View on Github external
def test_campaign_goal_met_cancel(patch_aws_services, campaign, mocker, caplog, stubber):
    from psiturk.tasks import do_campaign_round
    
    campaign_args = {
        'campaign': campaign,
        'job_id': campaign.campaign_job_id
    }
    
    from psiturk.experiment import app
    mocker.patch.object(app.apscheduler,
        'remove_job', lambda *args, **kwargs: True)
        
    import psiturk.tasks
    mocker.patch.object(psiturk.models.Participant, 'count_completed', lambda *args, **kwargs: campaign.goal)
    
    import psiturk.experiment
    remove_job_mock = mocker.patch.object(psiturk.experiment.app.apscheduler, 'remove_job')
    do_campaign_round(**campaign_args)
    remove_job_mock.assert_called()
github NYUCCL / psiTurk / psiturk / experiment.py View on Github external
def worker_submitted():
    ''' Submit worker '''
    if not 'uniqueId' in request.args:
        resp = {"status": "bad request"}
        return jsonify(**resp)
    else:
        unique_id = request.args['uniqueId']
        app.logger.info("Submitted experiment for %s" % unique_id)
        try:
            user = Participant.query.\
                filter(Participant.uniqueid == unique_id).one()
            user.status = SUBMITTED
            db_session.add(user)
            db_session.commit()
            status = "success"
        except exc.SQLAlchemyError:
            status = "database error"
        resp = {"status": status}
        return jsonify(**resp)
github NYUCCL / psiTurk / psiturk / experiment.py View on Github external
request.accept_languages.best

        # Set condition here and insert into database.
        participant_attributes = dict(
            assignmentid=assignment_id,
            workerid=worker_id,
            hitid=hit_id,
            cond=subj_cond,
            counterbalance=subj_counter,
            ipaddress=worker_ip,
            browser=browser,
            platform=platform,
            language=language,
            mode=mode
        )
        part = Participant(**participant_attributes)
        db_session.add(part)
        db_session.commit()

    else:
        # A couple possible problems here:
        # 1: They've already done an assignment, then we should tell them they
        #    can't do another one
        # 2: They've already worked on this assignment, and got too far to
        #    start over.
        # 3: They're in the database twice for the same assignment, that should
        #    never happen.
        # 4: They're returning and all is well.
        nrecords = 0
        for record in matches:
            other_assignment = False
            if record.assignmentid != assignment_id:
github NYUCCL / psiTurk / psiturk / models.py View on Github external
def _validate_greater_than_already_completed(self, goal):
        count_completed = Participant.count_completed(codeversion=self.codeversion, mode=self.mode)
        assert goal > count_completed, 'Goal ({}) must be greater than the count of already-completed ({}).'.format(goal, count_completed)
github NYUCCL / psiTurk / psiturk / amt_services_wrapper.py View on Github external
'''
        Can accept either an assignment_id or the return of a mturk boto grab...
        '''
        query = Participant.query.order_by(Participant.beginhit.desc())

        if with_psiturk_status:
            query = query.filter(Participant.status == with_psiturk_status)

        if isinstance(try_this, str):  # then assume that it's an assignment_id
            assignment_id = try_this
            query = query.filter(Participant.assignmentid == assignment_id)

        elif isinstance(try_this, dict):  # then assume that it's a return from mturk
            assignment = try_this
            assignment_id = assignment['assignmentId']
            query = query.filter(Participant.workerid == assignment['workerId'])\
                .filter(Participant.assignmentid == assignment_id)
        else:
            raise PsiturkException('Unrecognized `try_this` value-type: {}'.format(type(try_this)))

        local_assignment = query.first()
        if local_assignment:
            return local_assignment
        else:
            return try_this
github NYUCCL / psiTurk / psiturk / amt_services_wrapper.py View on Github external
def _try_fetch_local_assignment(self, try_this, with_psiturk_status=None):
        '''
        Can accept either an assignment_id or the return of a mturk boto grab...
        '''
        query = Participant.query.order_by(Participant.beginhit.desc())

        if with_psiturk_status:
            query = query.filter(Participant.status == with_psiturk_status)

        if isinstance(try_this, str):  # then assume that it's an assignment_id
            assignment_id = try_this
            query = query.filter(Participant.assignmentid == assignment_id)

        elif isinstance(try_this, dict):  # then assume that it's a return from mturk
            assignment = try_this
            assignment_id = assignment['assignmentId']
            query = query.filter(Participant.workerid == assignment['workerId'])\
                .filter(Participant.assignmentid == assignment_id)
        else:
            raise PsiturkException('Unrecognized `try_this` value-type: {}'.format(type(try_this)))

        local_assignment = query.first()
        if local_assignment:
            return local_assignment
        else:
            return try_this
github NYUCCL / psiTurk / psiturk / amt_services_wrapper.py View on Github external
def _get_local_submitted_assignments(self, hit_id=None):
        query = Participant.query.filter(Participant.status == SUBMITTED)
        if hit_id:
            query = query.filter(Participant.hitid == hit_id)
        assignments = query.all()
        return assignments