<commit_before>package ptgen

import (
	"bytes"
	"encoding/json"
	"math/rand"
	"time"

	"golang.org/x/tools/container/intsets"

	"github.com/intervention-engine/fhir/models"
)

type ConditionMetadata struct {
	ID                   int    `json:"condition_id"`
	ICD9                 string `json:"icd9code"`
	Display              string `json:"display"`
	MedicationID         int    `json:"medication_id"`
	Overnights           string `json:"overnights"`
	AbatementChance      int    `json:"abatementChance"`
	Fatal                bool   `json:"healOrDeath"`
	MortalityChance      int    `json:"mortalityChance"`
	MortalityTime        string `json:"mortalityTime"`
	RecoveryEstimate     string `json:"recoveryEstimate"`
	ProcedureChance      int    `json:"procedureChance"`
	ProcedureSuccess     int    `json:"procedureSuccess"`
	CheckUp              string `json:"checkUp"`
	ProcedureDescription string `json:"procedureDescription"`
	ProcedureCode        string `json:"procedureCode"`
	ProcedureName        string `json:"procedureCodeName"`
}

func LoadConditions() []ConditionMetadata {
	j, err := Asset("data/conditions.json")
	if err != nil {
		panic("Can't get the condition data")
	}
	decoder := json.NewDecoder(bytes.NewReader(j))
	md := []ConditionMetadata{}
	decoder.Decode(&md)
	return md
}

func GenerateConditions(ctx Context, md []ConditionMetadata) []models.Condition {
	conditions := []models.Condition{}
	if ctx.Hypertention == "Hypertension" {
		ht := generateCondition("Hypertension", 2, md)
		conditions = append(conditions, ht)
		complication := rand.Intn(5)
		if complication == 1 {
			chf := models.Condition{}
			chfmd := conditionByName("Congestive Heart Failure", md)
			chf.Code = &models.CodeableConcept{Coding: []models.Coding{{Code: chfmd.ICD9, System: "http://hl7.org/fhir/sid/icd-9"}}, Text: chfmd.Display}
			chf.OnsetDateTime = &models.FHIRDateTime{Time: ht.OnsetDateTime.Time.AddDate(rand.Intn(2), rand.Intn(10), rand.Intn(28)), Precision: models.Date}
			conditions = append(conditions, chf)
		}
		if complication == 2 {
			phd := models.Condition{}
			phdmd := conditionByName("Pulmonary Heart Disease", md)
			phd.Code = &models.CodeableConcept{Coding: []models.Coding{{Code: phdmd.ICD9, System: "http://hl7.org/fhir/sid/icd-9"}}, Text: phdmd.Display}
			phd.OnsetDateTime = &models.FHIRDateTime{Time: ht.OnsetDateTime.Time.AddDate(rand.Intn(2), rand.Intn(10), rand.Intn(28)), Precision: models.Date}
			conditions = append(conditions, phd)
		}
	}
	if ctx.Diabetes == "Diabetes" {
		dia := generateCondition("Diabetes", 2, md)
		conditions = append(conditions, dia)
	}

	if ctx.Smoker == "Smoker" {
		complication := rand.Intn(5)
		if complication == 1 {
			e := generateCondition("Emphysema", 2, md)
			conditions = append(conditions, e)
		}
		if complication == 2 {
			lc := generateCondition("Lung Cancer", 2, md)
			conditions = append(conditions, lc)
		}
	}
	// per http://www.cdc.gov/dhdsp/data_statistics/fact_sheets/fs_atrial_fibrillation.htm
	var afibChance int
	if time.Now().Sub(ctx.BirthDate) > (65 * 365 * 24 * time.Hour) {
		afibChance = 9
	} else {
		afibChance = 2
	}

	afibDiceRoll := rand.Intn(100)
	if afibDiceRoll <= afibChance {
		afib := generateCondition("Atrial Fibrillation", 3, md)
		conditions = append(conditions, afib)
	}

	otherConditions := rand.Intn(3)
	previouslySelected := &intsets.Sparse{}
	for index := 0; index < otherConditions; index++ {
		randomCondition := 2 + rand.Intn(76)
		if !previouslySelected.Has(randomCondition) {
			rmd := conditionByID(randomCondition, md)
			rc := generateCondition(rmd.Display, index, md)
			previouslySelected.Insert(randomCondition)
			conditions = append(conditions, rc)
		}
	}

	return conditions<commit_msg>More accurate calculation of whether the person is over 65<commit_after>package ptgen

import (
	"bytes"
	"encoding/json"
	"math/rand"
	"time"

	"golang.org/x/tools/container/intsets"

	"github.com/intervention-engine/fhir/models"
)

type ConditionMetadata struct {
	ID                   int    `json:"condition_id"`
	ICD9                 string `json:"icd9code"`
	Display              string `json:"display"`
	MedicationID         int    `json:"medication_id"`
	Overnights           string `json:"overnights"`
	AbatementChance      int    `json:"abatementChance"`
	Fatal                bool   `json:"healOrDeath"`
	MortalityChance      int    `json:"mortalityChance"`
	MortalityTime        string `json:"mortalityTime"`
	RecoveryEstimate     string `json:"recoveryEstimate"`
	ProcedureChance      int    `json:"procedureChance"`
	ProcedureSuccess     int    `json:"procedureSuccess"`
	CheckUp              string `json:"checkUp"`
	ProcedureDescription string `json:"procedureDescription"`
	ProcedureCode        string `json:"procedureCode"`
	ProcedureName        string `json:"procedureCodeName"`
}

func LoadConditions() []ConditionMetadata {
	j, err := Asset("data/conditions.json")
	if err != nil {
		panic("Can't get the condition data")
	}
	decoder := json.NewDecoder(bytes.NewReader(j))
	md := []ConditionMetadata{}
	decoder.Decode(&md)
	return md
}

func GenerateConditions(ctx Context, md []ConditionMetadata) []models.Condition {
	conditions := []models.Condition{}
	if ctx.Hypertention == "Hypertension" {
		ht := generateCondition("Hypertension", 2, md)
		conditions = append(conditions, ht)
		complication := rand.Intn(5)
		if complication == 1 {
			chf := models.Condition{}
			chfmd := conditionByName("Congestive Heart Failure", md)
			chf.Code = &models.CodeableConcept{Coding: []models.Coding{{Code: chfmd.ICD9, System: "http://hl7.org/fhir/sid/icd-9"}}, Text: chfmd.Display}
			chf.OnsetDateTime = &models.FHIRDateTime{Time: ht.OnsetDateTime.Time.AddDate(rand.Intn(2), rand.Intn(10), rand.Intn(28)), Precision: models.Date}
			conditions = append(conditions, chf)
		}
		if complication == 2 {
			phd := models.Condition{}
			phdmd := conditionByName("Pulmonary Heart Disease", md)
			phd.Code = &models.CodeableConcept{Coding: []models.Coding{{Code: phdmd.ICD9, System: "http://hl7.org/fhir/sid/icd-9"}}, Text: phdmd.Display}
			phd.OnsetDateTime = &models.FHIRDateTime{Time: ht.OnsetDateTime.Time.AddDate(rand.Intn(2), rand.Intn(10), rand.Intn(28)), Precision: models.Date}
			conditions = append(conditions, phd)
		}
	}
	if ctx.Diabetes == "Diabetes" {
		dia := generateCondition("Diabetes", 2, md)
		conditions = append(conditions, dia)
	}

	if ctx.Smoker == "Smoker" {
		complication := rand.Intn(5)
		if complication == 1 {
			e := generateCondition("Emphysema", 2, md)
			conditions = append(conditions, e)
		}
		if complication == 2 {
			lc := generateCondition("Lung Cancer", 2, md)
			conditions = append(conditions, lc)
		}
	}
	// per http://www.cdc.gov/dhdsp/data_statistics/fact_sheets/fs_atrial_fibrillation.htm
	var afibChance int
	if time.Now().AddDate(-65, 0, 0).After(ctx.BirthDate) {
		afibChance = 9
	} else {
		afibChance = 2
	}

	afibDiceRoll := rand.Intn(100)
	if afibDiceRoll <= afibChance {
		afib := generateCondition("Atrial Fibrillation", 3, md)
		conditions = append(conditions, afib)
	}

	otherConditions := rand.Intn(3)
	previouslySelected := &intsets.Sparse{}
	for index := 0; index < otherConditions; index++ {
		randomCondition := 2 + rand.Intn(76)
		if !previouslySelected.Has(randomCondition) {
			rmd := conditionByID(randomCondition, md)
			rc := generateCondition(rmd.Display, index, md)
			previouslySelected.Insert(randomCondition)
			conditions = append(conditions, rc)
		}
	}

	return conditions
{'fraction_non_alphanumeric': 0.13737292669876938, 'fraction_numerical': 0.012172284644194757, 'mean_word_length': 4.048615800135044, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7005584', 'n_tokens_mistral': 2747, 'n_tokens_neox': 2487, 'n_words': 506}