Note
Click here to download the full example code
01. Generate microbiology data
8 import factory
9 import inspect
10
11 # Specific
12 from datetime import timedelta
13 from random import randrange
14
15
16 MICROORGANISMS = ['O%s'%i for i in range(10)]
17 ANTIMICROBIALS = ['A%s'%i for i in range(10)]
18 SPECIMENS = ['BLDCUL', 'URICUL']
19 METHODS = ['DD']
20 SENSITIVITIES = ['S', 'I', 'R']
21
22 # -----------------------
23 # Classes
24 # -----------------------
25 class Base:
26 """"""
27 def __init__(self, **attributes):
28 for key, value in attributes.items():
29 self.__dict__[key] = value
30
31 def __str__(self):
32 attrs = [i
33 for i in inspect.getmembers(self)
34 if not i[0].startswith('_') and
35 not inspect.ismethod(i[1])
36 ]
37 return ' | '.join(['%s:%s' % (k,v) for k,v in attrs])
38
39 class Sample(Base):
40 pass
41
42 class Test(Base):
43 pass
44
45
46 # -----------------------
47 # Factories
48 # -----------------------
49 class SampleFactory(factory.Factory):
50 """Factory to create samples."""
51 class Meta:
52 model = Sample
53
54 date_collected = factory.Faker('date_time')
55 laboratory_number = factory.Sequence(lambda n: 'LAB%08d' % n)
56 specimen = factory.Iterator(SPECIMENS)
57
58
59 class TestFactory(factory.Factory):
60 """Factory to create susceptibility tests."""
61 sample = factory.SubFactory(SampleFactory)
62 microorganism = factory.Iterator(MICROORGANISMS)
63 antimicrobial = factory.Iterator(ANTIMICROBIALS)
64 method = factory.Iterator(METHODS)
65 sensitivity = factory.Iterator(SENSITIVITIES)
66
67 @factory.lazy_attribute
68 def date_received(self):
69 return self.sample.date_collected + timedelta(hours=self.offset1)
70
71 @factory.lazy_attribute
72 def date_outcome(self):
73 return self.date_received + timedelta(hours=self.offset2)
74
75 class Meta:
76 model = Test
77
78 class Params:
79 offset1 = randrange(3, 24)
80 offset2 = randrange(30, 60)
81
82
83
84
85 if __name__ == '__main__':
86
87 # Libraries
88 import random
89 from random import randrange
90
91 def create_sample_random(norgs, nabxs):
92 """Creates samples (randomly)"""
93 s = SampleFactory()
94 for o in random.sample(MICROORGANISMS, norgs):
95 for a in random.sample(ANTIMICROBIALS, nabxs):
96 t = TestFactory(sample=s, antimicrobial=a, microorganism=o)
97 print(t)
98
99 def create_sample_seq(norgs, nabxs):
100 """Creates samples (Iterator)
101
102 .. note: Ensure that N_MAX_ORGS and N_MAX_ABXS are
103 lower than the lengths of the corresponding
104 items.
105 """
106 s = SampleFactory()
107 for o in range(norgs):
108 t = TestFactory.create_batch(nabxs, sample=s)
109 for e in t:
110 print(e)
111
112
113 # Constants
114 N_SAMPLES = 2
115 N_MAX_ORGS = 4
116 N_MAX_ABXS = 6
117
118 print("\nSEQUENTIAL:")
119 for ni in range(N_SAMPLES):
120 create_sample_seq(
121 norgs=randrange(N_MAX_ORGS),
122 nabxs=randrange(N_MAX_ABXS)
123 )
124
125 print("\nRANDOM:")
126 for ni in range(N_SAMPLES):
127 create_sample_random(
128 norgs=randrange(N_MAX_ORGS),
129 nabxs=randrange(N_MAX_ABXS)
130 )
Total running time of the script: ( 0 minutes 0.000 seconds)