-
Notifications
You must be signed in to change notification settings - Fork 4
/
generate.go
158 lines (143 loc) · 4.55 KB
/
generate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package ptgen
import (
"math"
"math/rand"
"strconv"
"strings"
"time"
"github.com/icrowley/fake"
"github.com/intervention-engine/fhir/models"
"github.com/jmcvetta/randutil"
)
// Context contains information about the patient that can be used when
// generating information
type Context struct {
Smoker string
Hypertention string
Alcohol string
Cholesterol string
Diabetes string
Height int
Weight int
BirthDate time.Time
}
func GeneratePatient() []interface{} {
ctx := NewContext()
pt := GenerateDemographics()
ctx.Height, ctx.Weight = initialHeightAndWeight(pt.Gender)
ctx.BirthDate = pt.BirthDate.Time
pt.Id = strconv.FormatInt(rand.Int63(), 10)
md := LoadConditions()
mmd := LoadMedications()
conditions := GenerateConditions(ctx, md)
var m []interface{}
m = append(m, &pt)
for i := range conditions {
c := conditions[i]
c.Patient = &models.Reference{Reference: "cid:" + pt.Id}
m = append(m, &c)
conditionMetadata := conditionByName(c.Code.Text, md)
med := GenerateMedication(conditionMetadata.MedicationID, c.OnsetDateTime, c.AbatementDateTime, mmd)
if med != nil {
med.Patient = &models.Reference{Reference: "cid:" + pt.Id}
m = append(m, med)
}
}
for i := 0; i < 3; i++ {
t := time.Now()
encounterDate := &models.FHIRDateTime{Time: t.AddDate(-i, rand.Intn(2), rand.Intn(5)), Precision: models.Date}
encounter := models.Encounter{}
encounter.Id = strconv.FormatInt(rand.Int63(), 10)
encounter.Type = []models.CodeableConcept{{Coding: []models.Coding{{Code: "99213", System: "http://www.ama-assn.org/go/cpt"}}, Text: "Office Visit"}}
encounter.Period = &models.Period{Start: encounterDate}
encounter.Patient = &models.Reference{Reference: "cid:" + pt.Id}
m = append(m, &encounter)
obs := GenerateBP(ctx)
obs = append(obs, GenerateBloodSugars(ctx)...)
obs = append(obs, GenerateWeightAndHeight(ctx)...)
for j := range obs {
o := obs[j]
o.EffectiveDateTime = encounterDate
o.Subject = &models.Reference{Reference: "cid:" + pt.Id}
o.Encounter = &models.Reference{Reference: "cid:" + encounter.Id}
m = append(m, &o)
}
}
return m
}
func GenerateDemographics() models.Patient {
patient := models.Patient{}
patient.Gender = strings.ToLower(fake.Gender())
name := models.HumanName{}
var firstName string
if patient.Gender == "male" {
firstName = fake.MaleFirstName()
} else {
firstName = fake.FemaleFirstName()
}
name.Given = []string{firstName}
name.Family = []string{fake.LastName() + fake.DigitsN(4)}
patient.Name = []models.HumanName{name}
patient.BirthDate = &models.FHIRDateTime{Time: RandomBirthDate(), Precision: models.Date}
patient.Address = []models.Address{GenerateAddress()}
return patient
}
// RandomBirthDate generates a random birth date between 65 and 85 years ago
func RandomBirthDate() time.Time {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
randomYears := r.Intn(20)
yearsAgo := randomYears + 65
randomMonth := r.Intn(11)
randomDay := r.Intn(28)
t := time.Now()
return t.AddDate(-yearsAgo, -randomMonth, -randomDay).Truncate(time.Hour * 24)
}
func GenerateAddress() models.Address {
address := models.Address{}
address.Line = []string{fake.Digits() + " " + fake.Street()}
address.City = fake.City()
address.State = fake.StateAbbrev()
address.PostalCode = fake.DigitsN(5) // NOTE: fake.Zip() gives us 7 digits
return address
}
// NewContext generates a new context with randomly populated content
func NewContext() Context {
ctx := Context{}
smokingChoices := []randutil.Choice{
{2, "Smoker"},
{3, "Non-smoker"},
{1, "Ex-smoker"}}
sc, _ := randutil.WeightedChoice(smokingChoices)
ctx.Smoker = sc.Item.(string)
alcoholChoices := []randutil.Choice{
{2, "Occasional"},
{1, "Heavy"},
{1, "None"}}
ac, _ := randutil.WeightedChoice(alcoholChoices)
ctx.Alcohol = ac.Item.(string)
cholesterolChoices := []randutil.Choice{
{3, "Optimal"},
{1, "Near Optimal"},
{2, "Borderline"},
{1, "High"},
{2, "Very High"}}
cc, _ := randutil.WeightedChoice(cholesterolChoices)
ctx.Cholesterol = cc.Item.(string)
hc, _ := randutil.ChoiceString([]string{"Normal", "Pre-hypertension", "Hypertension"})
ctx.Hypertention = hc
dc, _ := randutil.ChoiceString([]string{"Normal", "Pre-diabetes", "Diabetes"})
ctx.Diabetes = dc
return ctx
}
func initialHeightAndWeight(gender string) (h, w int) {
if gender == "male" {
h = 60 + rand.Intn(20)
} else {
h = 55 + rand.Intn(20)
}
minBMI := float64(18)
englishBMIConstant := float64(703)
minWeight := (minBMI / englishBMIConstant) * math.Pow(float64(h), float64(2))
w = int(minWeight) + rand.Intn(200)
return
}