def test_making_membership(self):
        """"Can we add a dummy user to the demo study?"""

        study = Study.objects.get(slug='demo-study')
        user = make_user({'username': "******", 'email': "*****@*****.**", 'password': "******"})
        membership = Membership(study=study, user=user)
        membership.save()
        assert membership.condition is None  # has not auto-randomised
        allocate(membership)
        membership.add_observations()
        return membership
    def test_autorandomisation(self):

        study = Study.objects.get(slug='demo-study')
        study.auto_randomise = False
        study.save()

        user = make_user({'username': "******", 'email': "*****@*****.**", 'password': "******"})
        membership = Membership(study=study, user=user)
        membership.save()

        assert membership.condition is None

        allocate(membership)
        assert membership.condition is not None
    def test_making_membership(self):
        """"Can we add a dummy user to the demo study?"""

        study = Study.objects.get(slug='demo-study')
        user = make_user({
            'username': "******",
            'email': "*****@*****.**",
            'password': "******"
        })
        membership = Membership(study=study, user=user)
        membership.save()
        assert membership.condition is None  # has not auto-randomised
        allocate(membership)
        membership.add_observations()
        return membership
    def test_auto_add_observations(self):
        study = Study.objects.get(slug='demo-study')
        study.auto_add_observations = False
        study.save()

        user = make_user({'username': "******", 'email': "*****@*****.**", 'password': "******"})
        membership = Membership(study=study, user=user)
        membership.save()
        assert membership.condition is None

        allocate(membership)
        assert membership.condition is not None
        assert membership.observation_set.all().count() is 0

        membership.add_observations()
        assert membership.observation_set.all().count() is 1
Esempio n. 5
0
def _get_p_for_allocation(study, users, ratio):
    SAMPLESIZE = len(users)
    groups = list(study.studycondition_set.all())
    study.membership_set.all().delete()

    [setattr(i, 'weight', w) for i, w in zip(groups, ratio)]
    [i.save() for i in groups]

    normalisedweights = [i / sum(ratio) for i in ratio]
    expected = dict([(j, int(SAMPLESIZE * i))
                     for i, j in zip(normalisedweights, ratio)])

    mems = [Membership(study=study, user=user) for user in users]
    [allocate(i) for i in mems]

    # make each group min = 1 because otherwise chisq can blow up
    observed = {
        i.weight: min([1, i.membership_set.all().count()])
        for i in groups
    }
    ex_ob = [(expected[i], observed[i]) for i in list(observed.keys())]

    # XXX TODO reinstatewith python 3 functionality
    # chi, p = stats.lchisquare(*list(zip(*ex_ob)))

    return p
    def test_autorandomisation(self):

        study = Study.objects.get(slug='demo-study')
        study.auto_randomise = False
        study.save()

        user = make_user({
            'username': "******",
            'email': "*****@*****.**",
            'password': "******"
        })
        membership = Membership(study=study, user=user)
        membership.save()

        assert membership.condition is None

        allocate(membership)
        assert membership.condition is not None
Esempio n. 7
0
def randomise_membership(request, membership_id):
    """View to assign Membership to Condition"""

    mem = get_object_or_404(Membership, id=int(membership_id))
    was_allocated = allocate(mem)

    if was_allocated[0] is True:
        messages.success(request, was_allocated[1])
    else:
        messages.error(request, was_allocated[1],)
    return HttpResponseRedirect(reverse('admin:signalbox_membership_change', args=(mem.id,)))
    def test_auto_add_observations(self):
        study = Study.objects.get(slug='demo-study')
        study.auto_add_observations = False
        study.save()

        user = make_user({
            'username': "******",
            'email': "*****@*****.**",
            'password': "******"
        })
        membership = Membership(study=study, user=user)
        membership.save()
        assert membership.condition is None

        allocate(membership)
        assert membership.condition is not None
        assert membership.observation_set.all().count() is 0

        membership.add_observations()
        assert membership.observation_set.all().count() is 1
Esempio n. 9
0
def allocate_new_membership(sender, created, instance, **kwargs):
    """When a participant joins a study, we need to randomise them and add observations
    if the Study settings say so."""

    if created and instance.study.auto_randomise:
        _, _ = allocate(instance)

    if created and instance.study.auto_add_observations and instance.study.auto_randomise:
        instance.add_observations()
        # we don't execute at this point if we are testing because it makes unit
        # tests awkward (we can't test the intermediate states of some functions.)
        if not settings.TESTING:
            execute_the_todo_list()
Esempio n. 10
0
def allocate_new_membership(sender, created, instance, **kwargs):
    """When a participant joins a study, we need to randomise them and add observations
    if the Study settings say so."""

    if created and instance.study.auto_randomise:
        _, _ = allocate(instance)

    if created and instance.study.auto_add_observations and instance.study.auto_randomise:
        instance.add_observations()
        # we don't execute at this point if we are testing because it makes unit
        # tests awkward (we can't test the intermediate states of some functions.)
        if not settings.TESTING:
            execute_the_todo_list()
Esempio n. 11
0
def randomise_membership(request, membership_id):
    """View to assign Membership to Condition"""

    mem = get_object_or_404(Membership, id=int(membership_id))
    was_allocated = allocate(mem)

    if was_allocated[0] is True:
        messages.success(request, was_allocated[1])
    else:
        messages.error(
            request,
            was_allocated[1],
        )
    return HttpResponseRedirect(
        reverse('admin:signalbox_membership_change', args=(mem.id, )))
Esempio n. 12
0
def _get_p_for_allocation(study, users, ratio):
    SAMPLESIZE = len(users)
    groups = list(study.studycondition_set.all())
    study.membership_set.all().delete()

    [setattr(i, 'weight', w) for i, w in zip(groups, ratio)]
    [i.save() for i in groups]

    normalisedweights = [i / sum(ratio) for i in ratio]
    expected = dict([(j, int(SAMPLESIZE * i)) for i, j in zip(normalisedweights, ratio)])

    mems = [Membership(study=study, user=user) for user in users]
    [allocate(i) for i in mems]

    # make each group min = 1 because otherwise chisq can blow up
    observed = {i.weight: min([1, i.membership_set.all().count()]) for i in groups}
    ex_ob = [(expected[i], observed[i]) for i in observed.keys()]

    chi, p = stats.lchisquare(*zip(*ex_ob))

    return p