<commit_before>"""
MODELS for trials we're running
"""
import datetime
import random
import traceback

from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.contenttypes import generic
from django.core.mail import send_mail
from django.core.urlresolvers import reverse
from django.db import models
import letter
import numpy as np
from scipy import stats as scistats
from sorl import thumbnail
from statsmodels.stats.power import tt_ind_solve_power
from statsmodels.stats.weightstats import ttest_ind

from rm import exceptions
from rm.suffrage.models import VotableMixin, Vote
from rm.trials import managers, tasks

td = lambda: datetime.date.today()
POSTIE = letter.DjangoPostman()
Avg = models.Avg
User = get_user_model()


class Trial(VotableMixin, models.Model):
    """
    An individual trial that we are running.
    """
    ANYONE     = 'an'
    INVITATION = 'in'
    RECRUITMENT_CHOICES = (
        (ANYONE,     'Anyone can join'),
        (INVITATION, "Only people I've invited can join")
        )

    IMMEDIATE = 'im'
    HOURS     = 'ho'
    DATE      = 'da'
    ON_DEMAND = 'de'
    INSTRUCTION_CHOICES = (
        (IMMEDIATE, 'Straight away after randomisation'),
        # (HOURS, 'X hours after randomisation'),
        (DATE, 'On this date...'),
        (ON_DEMAND, 'On Demand')
        )

    ONCE      = 'on'
    WHENEVER  = 'wh'
    DATED     = 'da'
    REGULARLY = 're'

    REPORT_STYLE_CHOICES = (
        (ONCE,      'Once only'),
        (WHENEVER,  'Whenever they want'),
        (DATED,     'On date x'),
        # (REGULARLY, 'Regularly')
        )

    DAILY   = 'da'
    WEEKLY  = 'we'
    MONTHLY = 'mo'

    FREQ_CHOICES = (
        (DAILY,   'Once per day'),
        (WEEKLY,  'Once per week'),
        (MONTHLY, 'Once per month')
        )


    MANUALLY = 'ma'
    REPORT_NUM = 're'
    ENDING_CHOICES = (
        (MANUALLY, 'Manually'),
        (REPORT_NUM, 'After X have reported'),
        (DATED, 'On date Y')
        )

    HELP_PART = """Who can participate in this trial?
(Everyone? People with an infant under 6 months? People who binge drink Alcohol?)"""
    HELP_A = """These are the instructions that will be sent to group A"""
    HELP_B = """These are the instructions that will be sent to group B"""

    # Step 1
    title          = models.CharField(max_length=200, blank=True, null=True)

    # Step 2
    reporting_style = models.CharField("Reporting Style", max_length=2,
                                       choices=REPORT_STYLE_CHOICES, default=ONCE)
    reporting_freq = models.CharField("Reporting frequency", max_length=2,
                                      choices=FREQ_CHOICES, default=DAILY)
    reporting_date = models.DateField('Reporting date', blank=True, null=True)

    # Step 3
    min_participants  = models.IntegerField(
        "I think I need a minimum of x participants for a meaningful answer")

    # Step 4
    recruitment       = models.CharField(max_length=2, choices=RECRUITMENT_CHOICES,
                                         default=ANYONE)

    # Step 5
    description       = models.TextField(blank=True, null=True)
    image             = thumbnail.ImageField(upload_to='uploads', blank=True, null=True)
    secret_info       = models.TextField(blank=True, null=True)

    # Step 6
    group_a           = models.TextField("Group A Instructions", help_text=HELP_A)
    group_b           = models.TextField("Group B Instructions", help_text=HELP_B)
    instruction_delivery = models.CharField(max_length=2, choices=INSTRUCTION_CHOICES,
                                            default=IMMEDIATE)
    instruction_hours_after = models.IntegerField(blank=True, null=True)
    instruction_date = models.DateField(blank=True, null=True)

    # Step 7
    ending_style   = models.CharField(max_length=2, choices=ENDING_CHOICES, default=MANUALLY)
    ending_reports = models.IntegerField(blank=True, null=True)
    ending_date    = models.DateField(blank=True, null=True)


    # Currently unused power calcs
    group_a_expected  = models.IntegerField(blank=True, null=True)
    group_b_impressed = models.IntegerField(blank=True, null=True)

    # popularity
    votes             = generic.GenericRelation(Vote)

    # Metadata
    owner             = models.ForeignKey(settings.AUTH_USER_MODEL)
    n1trial           = models.BooleanField(default=False)
    featured          = models.BooleanField(default=False)
    stopped           = models.BooleanField(default=False)
    is_edited         = models.BooleanField(default=False)
    created           = models.DateTimeField(default=lambda: datetime.datetime.now())
    private           = models.BooleanField(default=False)
    hide              = models.BooleanField(default=False, blank=True, null=True)
    parent            = models.ForeignKey('self', blank=True, null=True,
                                          related_name='child')

    # Currently unused advanced user participants
    participants      = models.TextField(help_text=HELP_PART, blank=True, null=True)


    objects = managers.RmTrialManager()

    def __unicode__(self):
        """
        Nice printing representation
        """
        return '#({0}) {1}'.format(self.pk, self.title)

    def get_absolute_url(self):
        return reverse('trial-detail', kwargs={'pk': self.pk})

    def save(self):
        """
        Check for recruiting status

        Return: None<commit_msg>make hide a nb<commit_after>"""
MODELS for trials we're running
"""
import datetime
import random
import traceback

from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.contenttypes import generic
from django.core.mail import send_mail
from django.core.urlresolvers import reverse
from django.db import models
import letter
import numpy as np
from scipy import stats as scistats
from sorl import thumbnail
from statsmodels.stats.power import tt_ind_solve_power
from statsmodels.stats.weightstats import ttest_ind

from rm import exceptions
from rm.suffrage.models import VotableMixin, Vote
from rm.trials import managers, tasks

td = lambda: datetime.date.today()
POSTIE = letter.DjangoPostman()
Avg = models.Avg
User = get_user_model()


class Trial(VotableMixin, models.Model):
    """
    An individual trial that we are running.
    """
    ANYONE     = 'an'
    INVITATION = 'in'
    RECRUITMENT_CHOICES = (
        (ANYONE,     'Anyone can join'),
        (INVITATION, "Only people I've invited can join")
        )

    IMMEDIATE = 'im'
    HOURS     = 'ho'
    DATE      = 'da'
    ON_DEMAND = 'de'
    INSTRUCTION_CHOICES = (
        (IMMEDIATE, 'Straight away after randomisation'),
        # (HOURS, 'X hours after randomisation'),
        (DATE, 'On this date...'),
        (ON_DEMAND, 'On Demand')
        )

    ONCE      = 'on'
    WHENEVER  = 'wh'
    DATED     = 'da'
    REGULARLY = 're'

    REPORT_STYLE_CHOICES = (
        (ONCE,      'Once only'),
        (WHENEVER,  'Whenever they want'),
        (DATED,     'On date x'),
        # (REGULARLY, 'Regularly')
        )

    DAILY   = 'da'
    WEEKLY  = 'we'
    MONTHLY = 'mo'

    FREQ_CHOICES = (
        (DAILY,   'Once per day'),
        (WEEKLY,  'Once per week'),
        (MONTHLY, 'Once per month')
        )


    MANUALLY = 'ma'
    REPORT_NUM = 're'
    ENDING_CHOICES = (
        (MANUALLY, 'Manually'),
        (REPORT_NUM, 'After X have reported'),
        (DATED, 'On date Y')
        )

    HELP_PART = """Who can participate in this trial?
(Everyone? People with an infant under 6 months? People who binge drink Alcohol?)"""
    HELP_A = """These are the instructions that will be sent to group A"""
    HELP_B = """These are the instructions that will be sent to group B"""

    # Step 1
    title          = models.CharField(max_length=200, blank=True, null=True)

    # Step 2
    reporting_style = models.CharField("Reporting Style", max_length=2,
                                       choices=REPORT_STYLE_CHOICES, default=ONCE)
    reporting_freq = models.CharField("Reporting frequency", max_length=2,
                                      choices=FREQ_CHOICES, default=DAILY)
    reporting_date = models.DateField('Reporting date', blank=True, null=True)

    # Step 3
    min_participants  = models.IntegerField(
        "I think I need a minimum of x participants for a meaningful answer")

    # Step 4
    recruitment       = models.CharField(max_length=2, choices=RECRUITMENT_CHOICES,
                                         default=ANYONE)

    # Step 5
    description       = models.TextField(blank=True, null=True)
    image             = thumbnail.ImageField(upload_to='uploads', blank=True, null=True)
    secret_info       = models.TextField(blank=True, null=True)

    # Step 6
    group_a           = models.TextField("Group A Instructions", help_text=HELP_A)
    group_b           = models.TextField("Group B Instructions", help_text=HELP_B)
    instruction_delivery = models.CharField(max_length=2, choices=INSTRUCTION_CHOICES,
                                            default=IMMEDIATE)
    instruction_hours_after = models.IntegerField(blank=True, null=True)
    instruction_date = models.DateField(blank=True, null=True)

    # Step 7
    ending_style   = models.CharField(max_length=2, choices=ENDING_CHOICES, default=MANUALLY)
    ending_reports = models.IntegerField(blank=True, null=True)
    ending_date    = models.DateField(blank=True, null=True)


    # Currently unused power calcs
    group_a_expected  = models.IntegerField(blank=True, null=True)
    group_b_impressed = models.IntegerField(blank=True, null=True)

    # popularity
    votes             = generic.GenericRelation(Vote)

    # Metadata
    owner             = models.ForeignKey(settings.AUTH_USER_MODEL)
    n1trial           = models.BooleanField(default=False)
    featured          = models.BooleanField(default=False)
    stopped           = models.BooleanField(default=False)
    is_edited         = models.BooleanField(default=False)
    created           = models.DateTimeField(default=lambda: datetime.datetime.now())
    private           = models.BooleanField(default=False)
    hide              = models.NullBooleanField(default=False, blank=True, null=True)
    parent            = models.ForeignKey('self', blank=True, null=True,
                                          related_name='child')

    # Currently unused advanced user participants
    participants      = models.TextField(help_text=HELP_PART, blank=True, null=True)


    objects = managers.RmTrialManager()

    def __unicode__(self):
        """
        Nice printing representation
        """
        return '#({0}) {1}'.format(self.pk, self.title)

    def get_absolute_url(self):
        return reverse('trial-detail', kwargs={'pk': self.pk})

    def save(self):
        """
        Check for recruiting status

        Return: None
{'fraction_non_alphanumeric': 0.09836065573770492, 'fraction_numerical': 0.00348017217693928, 'mean_word_length': 2.5616438356164384, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3350840', 'n_tokens_mistral': 3358, 'n_tokens_neox': 3199, 'n_words': 806}