<commit_before>#!/usr/bin/env python
import os
import datetime
import pytz
import random
import math

# Set the settings environment variable before loading the models.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'qiprofile_rest.settings')

from qiprofile_rest import choices
from qiprofile_rest.models import (Subject, SubjectDetail, Session, SessionDetail,
                                   Modeling, Series, Scan, Registration, Intensity,
                                   Probe,  Encounter, BreastPathology, TNM,
                                   NottinghamGrade, HormoneReceptorStatus)

PROJECT = 'QIN_Test'

COLLECTIONS = ['Breast', 'Sarcoma']

DATE_0 = datetime.datetime(2013, 1, 4, tzinfo=pytz.utc)
"""The first image acquisition date."""

FXL_K_TRANS_AVG = 0.2
"""The average Standard Model Ktrans value."""

DELTA_K_TRANS_FACTOR = 1.3
"""
The detal Ktrans multiplier.
The FXR (Shutter Speed Model) Ktrans = FXL_K_TRANS_AVG * DELTA_K_TRANS_FACTOR.
"""

TAU_I_0 = 0.4875
"""The first tau_i."""

V_E_0 = 0.6113
"""The first v_e vale."""

AVG_BOLUS_ARRIVAL_NDX = 2
"""Bolus arrivals are spread evenly around the third visit."""

REG_PARAMS = dict(
    algorithm='ANTS',
    transforms='[Rigid, Affine, SyN]',
    metric='[MI, MI, CC]',
    metric_weight='[1, 1, 1]',
    number_of_iterations='[[10000, 500, 200, 100], [10000, 500, 200, 100], [100, 20, 10]]',
    radius_or_number_of_bins='[32, 32, 4]',
    sampling_strategy='[Regular, Regular, None]',
    sampling_percentage='[0.3, 0.3, None]',
    smoothing_sigmas='[[3,2,1,0], [3,2,1,0], [2,1,0]]',
    shrink_factors='[[8,4,2,1], [8,4,2,1], [4,2,1]]',
    transform_parameters='[(0.1,), (0.1,), (0.1, 3, 0)]'
)

def seed():
    """
    :Note: this is a destructive operation. Existing objects are deleted
    from the database.

    @return: three :obj:`PROJECT` subjects for each collection in
        :obj:`COLLECTIONS`
    """
    # Initialize the pseudo-random generator.
    random.seed()
    # Make the subjects.
    # Note: itertools chain on a generator iterates over
    # the collections in python 1.7.1, but not python 1.7.2.
    # The work-around is to build the subject collection
    # in the for loop below.
    coll_sbjs = []
    for coll in COLLECTIONS:
        coll_sbjs.extend(_seed_collection(coll))

    return coll_sbjs


def clear():
    """Removes the seeded documents."""
    for coll in COLLECTIONS:
        _clear_collection(coll)


def _seed_collection(collection):
    return [_seed_subject(collection, sbj_nbr)
            for sbj_nbr in range(1, 4)]


def _clear_collection(collection):
    for sbj_nbr in range(1, 4):
        try:
            sbj = Subject.objects.get(number=sbj_nbr, project=PROJECT,
                                      collection=collection)
            sbj.delete()
        except Subject.DoesNotExist:
            pass


def _seed_subject(collection, subject_number):
    """
    If the given subject is already in the database, then the
    subject is ignored. Otherwise, a new subject is created
    and populated with detail information.

    @param collection: the subject collection name
    @param subject_number: the subject number
    @return: the subject with the given collection and number
    """
    try:
        sbj = Subject.objects.get(number=subject_number, project=PROJECT,
                                  collection=collection)
    except Subject.DoesNotExist:
        sbj = _create_subject(collection, subject_number)

    return sbj


def _create_subject(collection, subject_number):
    sbj = Subject(number=subject_number, project=PROJECT,
                collection=collection)
    detail = _create_subject_detail(sbj)
    detail.save()
    sbj.detail = detail
    sbj.save()

    return sbj


def _create_subject_detail(subject):
    # The patient demographics.
    yr = int(40 * random.random()) + 1950
    birth_date = datetime.datetime(yr, 7, 7, tzinfo=pytz.utc)
    races = [choices.RACE_CHOICES[int(len(choices.RACE_CHOICES) * random.random())][0]]

    # The biopsy encounter.
    biopsy_date = DATE_0.replace(year=2012, month=10)
    biopsy_path = _create_pathology()
    biopsy = Encounter(encounter_type='Biopsy', date=biopsy_date, outcome=biopsy_path)

    # The post-treatment encounter.
    post_treatment_date = DATE_0.replace(month=5)
    post_treatment_tnm = _create_tnm()
    post_treatment = Encounter(encounter_type='Assessment', date=post_treatment_date,
                               outcome=post_treatment_tnm)

    encounters = [biopsy, post_treatment]

    # Make the sessions.
    sessions = [_create_session(subject, sess_nbr) for sess_nbr in range(1, 5)]

    # Make the subject detail.
    return SubjectDetail(birth_date=birth_date, races=races, encounters=encounters,
                         sessions=sessions)<commit_msg>Fix TNM attribute for second encounter in seed data.<commit_after>#!/usr/bin/env python
import os
import datetime
import pytz
import random
import math

# Set the settings environment variable before loading the models.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'qiprofile_rest.settings')

from qiprofile_rest import choices
from qiprofile_rest.models import (Subject, SubjectDetail, Session, SessionDetail,
                                   Modeling, Series, Scan, Registration, Intensity,
                                   Probe,  Encounter, BreastPathology, TNM,
                                   NottinghamGrade, HormoneReceptorStatus)

PROJECT = 'QIN_Test'

COLLECTIONS = ['Breast', 'Sarcoma']

DATE_0 = datetime.datetime(2013, 1, 4, tzinfo=pytz.utc)
"""The first image acquisition date."""

FXL_K_TRANS_AVG = 0.2
"""The average Standard Model Ktrans value."""

DELTA_K_TRANS_FACTOR = 1.3
"""
The detal Ktrans multiplier.
The FXR (Shutter Speed Model) Ktrans = FXL_K_TRANS_AVG * DELTA_K_TRANS_FACTOR.
"""

TAU_I_0 = 0.4875
"""The first tau_i."""

V_E_0 = 0.6113
"""The first v_e vale."""

AVG_BOLUS_ARRIVAL_NDX = 2
"""Bolus arrivals are spread evenly around the third visit."""

REG_PARAMS = dict(
    algorithm='ANTS',
    transforms='[Rigid, Affine, SyN]',
    metric='[MI, MI, CC]',
    metric_weight='[1, 1, 1]',
    number_of_iterations='[[10000, 500, 200, 100], [10000, 500, 200, 100], [100, 20, 10]]',
    radius_or_number_of_bins='[32, 32, 4]',
    sampling_strategy='[Regular, Regular, None]',
    sampling_percentage='[0.3, 0.3, None]',
    smoothing_sigmas='[[3,2,1,0], [3,2,1,0], [2,1,0]]',
    shrink_factors='[[8,4,2,1], [8,4,2,1], [4,2,1]]',
    transform_parameters='[(0.1,), (0.1,), (0.1, 3, 0)]'
)

def seed():
    """
    :Note: this is a destructive operation. Existing objects are deleted
    from the database.

    @return: three :obj:`PROJECT` subjects for each collection in
        :obj:`COLLECTIONS`
    """
    # Initialize the pseudo-random generator.
    random.seed()
    # Make the subjects.
    # Note: itertools chain on a generator iterates over
    # the collections in python 1.7.1, but not python 1.7.2.
    # The work-around is to build the subject collection
    # in the for loop below.
    coll_sbjs = []
    for coll in COLLECTIONS:
        coll_sbjs.extend(_seed_collection(coll))

    return coll_sbjs


def clear():
    """Removes the seeded documents."""
    for coll in COLLECTIONS:
        _clear_collection(coll)


def _seed_collection(collection):
    return [_seed_subject(collection, sbj_nbr)
            for sbj_nbr in range(1, 4)]


def _clear_collection(collection):
    for sbj_nbr in range(1, 4):
        try:
            sbj = Subject.objects.get(number=sbj_nbr, project=PROJECT,
                                      collection=collection)
            sbj.delete()
        except Subject.DoesNotExist:
            pass


def _seed_subject(collection, subject_number):
    """
    If the given subject is already in the database, then the
    subject is ignored. Otherwise, a new subject is created
    and populated with detail information.

    @param collection: the subject collection name
    @param subject_number: the subject number
    @return: the subject with the given collection and number
    """
    try:
        sbj = Subject.objects.get(number=subject_number, project=PROJECT,
                                  collection=collection)
    except Subject.DoesNotExist:
        sbj = _create_subject(collection, subject_number)

    return sbj


def _create_subject(collection, subject_number):
    sbj = Subject(number=subject_number, project=PROJECT,
                collection=collection)
    detail = _create_subject_detail(sbj)
    detail.save()
    sbj.detail = detail
    sbj.save()

    return sbj


def _create_subject_detail(subject):
    # The patient demographics.
    yr = int(40 * random.random()) + 1950
    birth_date = datetime.datetime(yr, 7, 7, tzinfo=pytz.utc)
    races = [choices.RACE_CHOICES[int(len(choices.RACE_CHOICES) * random.random())][0]]

    # The biopsy encounter.
    biopsy_date = DATE_0.replace(year=2012, month=10)
    biopsy_path = _create_pathology()
    biopsy = Encounter(encounter_type='Biopsy', date=biopsy_date, outcome=biopsy_path)

    # The post-treatment encounter.
    post_treatment_date = DATE_0.replace(month=5)
    post_treatment_tnm = _create_tnm()
    post_treatment = Encounter(encounter_type='Assessment', date=post_treatment_date,
                               outcome=BreastPathology(tnm=post_treatment_tnm))

    encounters = [biopsy, post_treatment]

    # Make the sessions.
    sessions = [_create_session(subject, sess_nbr) for sess_nbr in range(1, 5)]

    # Make the subject detail.
    return SubjectDetail(birth_date=birth_date, races=races, encounters=encounters,
                         sessions=sessions)
{'fraction_non_alphanumeric': 0.10883509207531554, 'fraction_numerical': 0.027105317608110906, 'mean_word_length': 3.1704055220017255, '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': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1194331', 'n_tokens_mistral': 3353, 'n_tokens_neox': 3054, 'n_words': 791}