-
Notifications
You must be signed in to change notification settings - Fork 19
/
vioscreen_repo.py
1658 lines (1484 loc) · 71.2 KB
/
vioscreen_repo.py
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import pandas as pd
from microsetta_private_api.repo.base_repo import BaseRepo
from microsetta_private_api.model.vioscreen import (
VioscreenSession, VioscreenPercentEnergy,
VioscreenPercentEnergyComponent,
VioscreenDietaryScore, VioscreenDietaryScoreComponent,
VioscreenSupplements, VioscreenSupplementsComponent,
VioscreenFoodComponents, VioscreenFoodComponentsComponent,
VioscreenEatingPatterns, VioscreenEatingPatternsComponent,
VioscreenMPeds, VioscreenMPedsComponent,
VioscreenFoodConsumption, VioscreenFoodConsumptionComponent,
VioscreenComposite)
from werkzeug.exceptions import NotFound
class VioscreenSessionRepo(BaseRepo):
COLS = ["sessionId", "username", "protocolId", "status",
"startDate", "endDate", "cultureCode", "created",
"modified"]
@property
def _sql_cols(self):
return ', '.join(self.COLS)
def __init__(self, transaction):
super().__init__(transaction)
def upsert_session(self, session):
"""Insert or update a vioscreen session
Parameters
----------
session : VioscreenSession
The session object to insert or update
Returns
-------
bool
True if something was updated or inserted
"""
# upsert based off of https://stackoverflow.com/a/36799500/19741
with self._transaction.cursor() as cur:
doupdateset = ["%s = EXCLUDED.%s" % (a, a) for a in self.COLS
if a not in ('sessionId', 'username')]
doupdateset = ','.join(doupdateset)
cur.execute(f"""INSERT INTO ag.vioscreen_sessions (
{self._sql_cols}
)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)
ON CONFLICT (sessionId)
DO UPDATE SET
{doupdateset}
""",
tuple([getattr(session, attr) for attr in self.COLS]))
return cur.rowcount == 1
def get_session(self, sessionId):
"""Obtain a session model for a sessionId
Parameters
----------
sessionId : str
The session ID to retrieve
Returns
-------
VioscreenSession or None
An instance of a VioscreenSession model or None if a session
was not found
"""
with self._transaction.cursor() as cur:
cur.execute(f"""SELECT {self._sql_cols}
FROM ag.vioscreen_sessions
WHERE sessionId = %s""",
(sessionId, ))
row = cur.fetchone()
if row is None:
return None
else:
return VioscreenSession(*row)
def get_sessions_by_username(self, username):
"""Obtain all sessions associated with a username
Parameters
----------
username : str
The username to search for
Returns
-------
list of VioscreenSession, or None
Instances of the observed vioscreen sessions or None if the
username was not found
"""
with self._transaction.cursor() as cur:
cur.execute(f"""SELECT {self._sql_cols}
FROM ag.vioscreen_sessions
WHERE username = %s""",
(username, ))
rows = cur.fetchall()
# TODO: test if we can add a unique constraint
# on username
if len(rows) == 0:
return None
else:
return [VioscreenSession(*row) for row in rows]
def get_unfinished_sessions(self):
"""Obtain the sessions for that appear incomplete
An incomplete session is one that meets any of the following criteria:
1) the ag.vioscreen_registry.vio_id does not exist in the
ag.vioscreen_sessions table
2) the ag.vioscreen_sessions.endDate is null
3) the ag.vioscreen_sessions.status is not "Finished"
In the event a user has multiple sessions:
* if ALL sessions are incomplete, all sessions will be returned
* if ANY session is complete, no sessions will be returned
The operating model for AG/TMI has been a single session per vioscreen
username (i.e., vio_id). As such, *if* a vioscreen vio_id has multiple
sessions associated with it, we do not actually know right now how to
appropriately handle a scenario where *multiple* sessions are finished.
Returns
-------
list of VioscreenSession
In the event of criteria (1) noted above, only the username
attribute of an instance will be not None
"""
with self._transaction.cursor() as cur:
# criteria 1, vio_ids which are not in vioscreen_sessions
cur.execute("""SELECT distinct(vio_id)
FROM ag.vioscreen_registry
WHERE vio_id NOT IN (
SELECT distinct(username)
FROM ag.vioscreen_sessions)""")
not_in_vioscreen_sessions = [VioscreenSession.from_registry(u[0])
for u in cur.fetchall()]
# criteria 2 and 3
cur.execute(f"""SELECT {self._sql_cols}
FROM ag.vioscreen_sessions""")
# array_agg doesn't work over these datatypes... ugh. this
# almost certainly could be done better directly within SQL
# but another problem for another day
records = pd.DataFrame(cur.fetchall(), columns=self.COLS)
incomplete_sessions = []
for user, rows in records.groupby('username'):
sessions = rows.apply(lambda row: VioscreenSession(*row),
axis=1)
are_incomplete = [not s.is_complete for s in sessions]
if all(are_incomplete):
incomplete_sessions.extend(sessions)
return not_in_vioscreen_sessions + incomplete_sessions
def get_ffq_status_by_sample(self, sample_uuid):
"""Obtain the FFQ status for a given sample
Parameters
----------
sample_uuid : UUID4
The UUID to check the status of
Returns
-------
(bool, bool, str or None)
The first index if True indicates the FFQ is completed.
The second index if True indicates the FFQ has been started,
but may or may not be completed.
The third index is the exact status from Vioscreen, or None
if there is no FFQ associated with the sample.
"""
with self._transaction.cursor() as cur:
cur.execute("""SELECT status
FROM ag.vioscreen_sessions AS vs
JOIN ag.vioscreen_registry AS vr
ON vs.username=vr.vio_id
WHERE sample_id=%s""", (sample_uuid, ))
res = cur.fetchall()
if len(res) == 0:
return (False, False, None)
elif len(res) == 1:
status = res[0][0]
is_complete = status == 'Finished'
is_taken = status in ('Started', 'Review', 'Finished')
return (is_complete, is_taken, status)
else:
raise ValueError("A sample should not have multiple FFQs")
def get_missing_ffqs(self):
"""The set of valid sessions which lack FFQ data
An valid session but missing session is one that meets
the following criteria:
1) the ag.vioscreen_sessions entry is "Finished"
2) the ag.vioscreen_sessions entry endDate is not null
3) the sessionId is not present in one of the vioscreen FFQ
decomposed tables
Returns
-------
list of VioscreenSession
The missing sessions
"""
with self._transaction.cursor() as cur:
# criteria 1 and 2, check endDate and status
cur.execute("""SELECT sessionid
FROM ag.vioscreen_sessions
WHERE endDate IS NOT NULL
AND status = 'Finished'""")
complete_sessions = {i[0] for i in cur.fetchall()}
# criteria 3, determine sessions which we have ffq data for
cur.execute("""SELECT distinct(sessionid)
FROM ag.vioscreen_percentenergy""")
sessions_with_data = {i[0] for i in cur.fetchall()}
sessions_without_data = complete_sessions - sessions_with_data
without_data = []
if sessions_without_data:
# construct session objects for all of our missing sessions
cur.execute(f"""SELECT {self._sql_cols}
FROM ag.vioscreen_sessions
WHERE sessionId IN %s""",
(tuple(sessions_without_data), ))
for row in cur.fetchall():
without_data.append(VioscreenSession(*row))
return without_data
class VioscreenPercentEnergyRepo(BaseRepo):
# code : (long description, short description, units)
_CODES = {'%mfatot': ('Percent of calories from Monounsaturated Fat',
'Monounsaturated Fat', '%'),
'%pfatot': ('Percent of calories from Polyunsaturated Fat',
'Polyunsaturated Fat', '%'),
'%carbo': ('Percent of calories from Carbohydrate',
'Carbohydrate', '%'),
'%sfatot': ('Percent of calories from Saturated Fat',
'Saturated Fat', '%'),
'%alcohol': ('Percent of calories from Alcohol', 'Alcohol', '%'),
'%protein': ('Percent of calories from Protein', 'Protein', '%'),
'%adsugtot': ('Percent of calories from Added Sugar',
'Added Sugar', '%'),
'%fat': ('Percent of calories from Fat', 'Fat', '%')}
def __init__(self, transaction):
super().__init__(transaction)
def insert_percent_energy(self, vioscreen_percent_energy):
"""Add percent energy data for a session
Parameters
----------
vioscreen_percent_energy : VioscreenPercentEnergy
The observed percent energy data
Returns
-------
Returns number of rows modified
"""
with self._transaction.cursor() as cur:
energy_components = vioscreen_percent_energy.energy_components
inserts = []
for energy_component in energy_components:
# checking if code exists in lookup
self._get_code_info(energy_component.code)
inserts.append((vioscreen_percent_energy.sessionId,
energy_component.code,
energy_component.amount))
cur.executemany("""INSERT INTO ag.vioscreen_percentenergy
(sessionId, code, amount)
VALUES (%s, %s, %s)""",
inserts)
return cur.rowcount
def get_percent_energy(self, sessionId):
"""Obtain the percent energy data for a sessionId
Parameters
----------
sessionId : str
The session ID to retrieve data for
Returns
-------
VioscreenPercentEnergy or None
The observed percent energy data or None if the session ID is
invalid
"""
with self._transaction.cursor() as cur:
cur.execute("""SELECT code, amount
FROM ag.vioscreen_percentenergy
WHERE sessionId = %s""",
(sessionId,))
rows = cur.fetchall()
if len(rows) > 0:
components = []
for code, amount in rows:
codeInfo = self._get_code_info(code)
vpec = VioscreenPercentEnergyComponent(code=code,
description=codeInfo[0], # noqa
short_description=codeInfo[1], # noqa
units=codeInfo[2],
amount=amount)
components.append(vpec)
return VioscreenPercentEnergy(sessionId=sessionId,
energy_components=components)
else:
return None
def _get_code_info(self, code):
"""Obtain the detail about a particular energy component by its code
Parameters
----------
code : str
The code to obtain detail for
Returns
-------
tuple
The description, short description and units for a
particular code
Raises
------
NotFound
A NotFound error is raised if the code is unrecognized
"""
if code not in self._CODES:
raise NotFound("No such code: " + code)
return self._CODES[code]
class VioscreenDietaryScoreRepo(BaseRepo):
# scoresType : { code: (name, lower limit, upper limit) }
_CODES = {
'Hei2010': {
'TotalVegetables': ('Total Vegetables', 0.0, 5.0),
'GreensAndBeans': ('Greens and Beans', 0.0, 5.0),
'TotalFruit': ('Total Fruit', 0.0, 5.0),
'WholeFruit': ('Whole Fruit', 0.0, 5.0),
'WholeGrains': ('Whole Grains', 0.0, 10.0),
'Dairy': ('Dairy', 0.0, 10.0),
'TotalProteins': ('Total Protein Foods', 0.0, 5.0),
'SeafoodAndPlantProteins': ('Seafood and Plant Proteins',
0.0, 5.0),
'FattyAcids': ('Fatty Acids', 0.0, 10.0),
'RefinedGrains': ('Refined Grains', 0.0, 10.0),
'Sodium': ('Sodium', 0.0, 10.0),
'EmptyCalories': ('Empty Calories', 0.0, 20.0),
'TotalScore': ('Total HEI Score', 0.0, 100.0)},
'Hei2015': {
'TotalVegetables': ('Total Vegetables', 0.0, 5.0),
'GreensAndBeans': ('Greens and Beans', 0.0, 5.0),
'TotalFruit': ('Total Fruit', 0.0, 5.0),
'WholeFruit': ('Whole Fruit', 0.0, 5.0),
'WholeGrains': ('Whole Grains', 0.0, 10.0),
'Dairy': ('Dairy', 0.0, 10.0),
'TotalProteins': ('Total Protein Foods', 0.0, 5.0),
'SeafoodAndPlantProteins': ('Seafood and Plant Proteins',
0.0, 5.0),
'FattyAcids': ('Fatty Acids', 0.0, 10.0),
'RefinedGrains': ('Refined Grains', 0.0, 10.0),
'Sodium': ('Sodium', 0.0, 10.0),
'SaturatedFat': ('SaturatedFat', 0.0, 10.0),
'AddedSugars': ('AddedSugars', 0.0, 10.0),
'TotalScore': ('Total HEI Score', 0.0, 100.0)},
}
def __init__(self, transaction):
super().__init__(transaction)
def insert_dietary_scores(self, vioscreen_dietary_scores):
"""Add in dietary score results for a session
Parameters
----------
vioscreen_dietary_scores : list of VioscreenDietaryScore
Dietary score models to insert
Returns
-------
int
The number of inserted rows
"""
count = 0
with self._transaction.cursor() as cur:
for model in vioscreen_dietary_scores:
scores = model.scores
inserts = []
for score in scores:
# checking if code exists in lookup
self._get_code_info(model.scoresType, score.code)
inserts.append((model.sessionId,
model.scoresType,
score.code,
score.score))
cur.executemany("""INSERT INTO ag.vioscreen_dietaryscore
(sessionId, scoresType, code, score)
VALUES (%s, %s, %s, %s)""",
inserts)
count += cur.rowcount
return count
def get_dietary_scores(self, sessionId):
"""Obtain the dietary score detail for a particular session
Parameters
----------
sessionId : str
The session ID to query
Returns
-------
list of VioscreenDietaryScore or None
The dietary score detail, or None if no record was found
"""
sql = """SELECT scoresType, code, score
FROM ag.vioscreen_dietaryscore
WHERE sessionId = %s"""
df = pd.read_sql(sql, self._transaction.conn, params=(sessionId, ))
if len(df) == 0:
return None
scores = []
# note the header is cast to lowerase
for type_, grp in df.groupby('scorestype'):
components = []
for _, row in grp.iterrows():
code = row['code']
score = row['score']
codeInfo = self._get_code_info(type_, code)
vdsc = VioscreenDietaryScoreComponent(code=code,
name=codeInfo[0],
score=score,
lowerLimit=codeInfo[1],
upperLimit=codeInfo[2])
components.append(vdsc)
scores.append(VioscreenDietaryScore(sessionId=sessionId,
scoresType=type_,
scores=components))
return scores
def _get_code_info(self, scoresType, code):
"""Obtain the detail about a particular score type by its code
Parameters
----------
scoresType : str
The score type (e.g. Hei2010) to obtain detail for
code : str
The code to obtain detail for
Returns
-------
tuple
The name, lower and upper limit for a particular code
and score type
Raises
------
NotFound
A NotFound error is raised if the code or score type are
unrecognized
"""
if scoresType not in self._CODES:
raise NotFound("No such scoresType: " + scoresType)
scores = self._CODES[scoresType]
if code not in scores:
raise NotFound("No such code: " + code)
return scores[code]
def get_dietary_scores_by_component(self, scoresType, code):
"""Obtain the all the dietary scores for a particular component
Parameters
----------
scoresType : str
The scoresType to query
code : str
The code to query
Returns
-------
Float Array or None
The dietary scores, or None if no record was found
"""
with self._transaction.cursor() as cur:
cur.execute("""SELECT score
FROM ag.vioscreen_dietaryscore
WHERE scoresType = %s AND code = %s""",
(scoresType, code))
rows = cur.fetchall()
if len(rows) > 0:
scores = []
for score in rows:
scores.append(score[0])
return scores
else:
return None
def get_dietary_scores_descriptions(self):
return self._CODES
class VioscreenSupplementsRepo(BaseRepo):
def __init__(self, transaction):
super().__init__(transaction)
def insert_supplements(self, vioscreen_supplements):
"""Add in supplement results for a session
Parameters
----------
vioscreen_supplements : VioscreenSupplements
An instance of a supplements model
Returns
-------
int
The number of inserted rows
"""
with self._transaction.cursor() as cur:
components = vioscreen_supplements.supplements_components
inserts = [(vioscreen_supplements.sessionId,
component.supplement,
component.frequency,
component.amount,
component.average)
for component in components]
cur.executemany("""INSERT INTO ag.vioscreen_supplements
(sessionId, supplement, frequency,
amount, average)
VALUES (%s, %s, %s, %s, %s)""",
inserts)
return cur.rowcount
def get_supplements(self, sessionId):
"""Obtain the supplement detail for a particular session
Parameters
----------
sessionId : str
The session ID to query
Returns
-------
VioscreenSupplements or None
The supplements detail, or None if no record was found
"""
with self._transaction.cursor() as cur:
cur.execute("""SELECT supplement, frequency, amount, average
FROM ag.vioscreen_supplements
WHERE sessionId = %s""",
(sessionId,))
rows = cur.fetchall()
if len(rows) > 0:
components = []
for row in rows:
vsc = VioscreenSupplementsComponent(supplement=row[0],
frequency=row[1],
amount=row[2],
average=row[3])
components.append(vsc)
return VioscreenSupplements(sessionId=sessionId,
supplements_components=components)
else:
return None
class VioscreenFoodComponentsRepo(BaseRepo):
# code : (description, units, valueType)
_CODES = {'acesupot': ('Acesulfame Potassium', 'mg', 'Amount'),
'addsugar': ('Added Sugars (by Available Carbohydrate)',
'g', 'Amount'),
'adsugtot': ('Added Sugars (by Total Sugars)', 'g', 'Amount'),
'alanine': ('Alanine', 'g', 'Amount'),
'alcohol': ('Alcohol', 'g', 'Amount'),
'alphacar': ('Alpha-Carotene (provitamin A carotenoid)', 'mcg',
'Amount'),
'alphtoce': ('Total Vitamin E Activity (total alpha-tocopherol equivalents)', # noqa
'mg', 'Amount'),
'alphtoco': ('Alpha-Tocopherol', 'mg', 'Amount'),
'arginine': ('Arginine', 'g', 'Amount'),
'ash': ('Ash', 'g', 'Amount'),
'aspartam': ('Aspartame', 'mg', 'Amount'),
'aspartic': ('Aspartic Acid', 'g', 'Amount'),
'avcarb': ('Available Carbohydrate', 'g', 'Amount'),
'betacar': ('Beta-Carotene (provitamin A carotenoid)', 'mcg',
'Amount'),
'betacryp': ('Beta-Cryptoxanthin (provitamin A carotenoid)',
'mcg', 'Amount'),
'betaine': ('Betaine', 'mg', 'Amount'),
'betatoco': ('Beta-Tocopherol', 'mg', 'Amount'),
'biochana': ('Biochanin A', 'mg', 'Amount'),
'caffeine': ('Caffeine', 'mg', 'Amount'),
'calcium': ('Calcium', 'mg', 'Amount'),
'calories': ('Energy', 'kcal', 'Amount'),
'carbo': ('Total Carbohydrate', 'g', 'Amount'),
'cholest': ('Cholesterol', 'mg', 'Amount'),
'choline': ('Choline', 'g', 'Amount'),
'clac9t11': ('CLA cis-9, trans-11', 'g', 'Amount'),
'clat10c12': ('CLA trans-10, cis-12', 'g', 'Amount'),
'copper': ('Copper', 'mg', 'Amount'),
'coumest': ('Coumestrol', 'mg', 'Amount'),
'cystine': ('Cystine', 'g', 'Amount'),
'daidzein': ('Daidzein', 'mg', 'Amount'),
'delttoco': ('Delta-Tocopherol', 'mg', 'Amount'),
'erythr': ('Erythritol', 'g', 'Amount'),
'fat': ('Total Fat', 'g', 'Amount'),
'fiber': ('Total Dietary Fiber', 'g', 'Amount'),
'fibh2o': ('Soluble Dietary Fiber', 'g', 'Amount'),
'fibinso': ('Insoluble Dietary Fiber', 'g', 'Amount'),
'fol_deqv': ('Dietary Folate Equivalents', 'mcg', 'Amount'),
'fol_nat': ('Natural Folate (food folate)', 'mcg', 'Amount'),
'fol_syn': ('Synthetic Folate (folic acid)', 'mcg', 'Amount'),
'formontn': ('Formononetin', 'mg', 'Amount'),
'fructose': ('Fructose', 'g', 'Amount'),
'galactos': ('Galactose', 'g', 'Amount'),
'gammtoco': ('Gamma-Tocopherol', 'mg', 'Amount'),
'genistn': ('Genistein', 'mg', 'Amount'),
'glucose': ('Glucose', 'g', 'Amount'),
'glutamic': ('Glutamic Acid', 'g', 'Amount'),
'glycine': ('Glycine', 'g', 'Amount'),
'glycitn': ('Glycitein', 'mg', 'Amount'),
'grams': ('Total Grams', 'g', 'Amount'),
'histidin': ('Histidine', 'g', 'Amount'),
'inositol': ('Inositol', 'g', 'Amount'),
'iron': ('Iron', 'mg', 'Amount'),
'isoleuc': ('Isoleucine', 'g', 'Amount'),
'isomalt': ('Isomalt', 'g', 'Amount'),
'joules': ('Energy', 'kj', 'Amount'),
'lactitol': ('Lactitol', 'g', 'Amount'),
'lactose': ('Lactose', 'g', 'Amount'),
'leucine': ('Leucine', 'g', 'Amount'),
'lutzeax': ('Lutein + Zeaxanthin', 'mcg', 'Amount'),
'lycopene': ('Lycopene', 'mcg', 'Amount'),
'lysine': ('Lysine', 'g', 'Amount'),
'magnes': ('Magnesium', 'mg', 'Amount'),
'maltitol': ('Maltitol', 'g', 'Amount'),
'maltose': ('Maltose', 'g', 'Amount'),
'mangan': ('Manganese', 'mg', 'Amount'),
'mannitol': ('Mannitol', 'g', 'Amount'),
'methhis3': ('3-Methylhistidine', 'mg', 'Amount'),
'methion': ('Methionine', 'g', 'Amount'),
'mfa141': ('MUFA 14:1 (myristoleic acid)', 'g', 'Amount'),
'mfa161': ('MUFA 16:1 (palmitoleic acid)', 'g', 'Amount'),
'mfa181': ('MUFA 18:1 (oleic acid)', 'g', 'Amount'),
'mfa201': ('MUFA 20:1 (gadoleic acid)', 'g', 'Amount'),
'mfa221': ('MUFA 22:1 (erucic acid)', 'g', 'Amount'),
'mfatot': ('Total Monounsaturated Fatty Acids (MUFA)', 'g',
'Amount'),
'natoco': ('Natural Alpha-Tocopherol (RRR-alpha-tocopherol or d-alpha-tocopherol)', # noqa
'mg', 'Amount'),
'niacin': ('Niacin (vitamin B3)', 'mg', 'Amount'),
'niacineq': ('Niacin Equivalents', 'mg', 'Amount'),
'nitrogen': ('Nitrogen', 'g', 'Amount'),
'omega3': ('Omega-3 Fatty Acids', 'g', 'Amount'),
'oxalic': ('Oxalic Acid', 'mg', 'Amount'),
'pantothe': ('Pantothenic acid', 'mg', 'Amount'),
'pectins': ('Pectins', 'g', 'Amount'),
'pfa182': ('PUFA 18:2 (linoleic acid)', 'g', 'Amount'),
'pfa183': ('PUFA 18:3 (linolenic acid)', 'g', 'Amount'),
'pfa183n3': ('PUFA 18:3 n-3 (alpha-linolenic acid [ALA])', 'g',
'Amount'),
'pfa184': ('PUFA 18:4 (parinaric acid)', 'g', 'Amount'),
'pfa204': ('PUFA 20:4 (arachidonic acid)', 'g', 'Amount'),
'pfa205': ('PUFA 20:5 (eicosapentaenoic acid [EPA])', 'g',
'Amount'),
'pfa225': ('PUFA 22:5 (docosapentaenoic acid [DPA])', 'g',
'Amount'),
'pfa226': ('PUFA 22:6 (docosahexaenoic acid [DHA])', 'g',
'Amount'),
'pfatot': ('Total Polyunsaturated Fatty Acids (PUFA)', 'g',
'Amount'),
'phenylal': ('Phenylalanine', 'g', 'Amount'),
'phosphor': ('Phosphorus', 'mg', 'Amount'),
'phytic': ('Phytic Acid', 'mg', 'Amount'),
'pinitol': ('Pinitol', 'g', 'Amount'),
'potass': ('Potassium', 'mg', 'Amount'),
'proline': ('Proline', 'g', 'Amount'),
'protanim': ('Animal Protein', 'g', 'Amount'),
'protein': ('Total Protein', 'g', 'Amount'),
'protveg': ('Vegetable Protein', 'g', 'Amount'),
'retinol': ('Retinol', 'mcg', 'Amount'),
'ribofla': ('Riboflavin (vitamin B2)', 'mg', 'Amount'),
'sacchar': ('Saccharin', 'mg', 'Amount'),
'satoco': ('Synthetic Alpha-Tocopherol (all rac-alpha-tocopherol or dl-alpha-tocopherol)', # noqa
'mg', 'Amount'),
'selenium': ('Selenium', 'mcg', 'Amount'),
'serine': ('Serine', 'g', 'Amount'),
'sfa100': ('SFA 10:0 (capric acid)', 'g', 'Amount'),
'sfa120': ('SFA 12:0 (lauric acid)', 'g', 'Amount'),
'sfa140': ('SFA 14:0 (myristic acid)', 'g', 'Amount'),
'sfa160': ('SFA 16:0 (palmitic acid)', 'g', 'Amount'),
'sfa170': ('SFA 17:0 (margaric acid)', 'g', 'Amount'),
'sfa180': ('SFA 18:0 (stearic acid)', 'g', 'Amount'),
'sfa200': ('SFA 20:0 (arachidic acid)', 'g', 'Amount'),
'sfa220': ('SFA 22:0 (behenic acid)', 'g', 'Amount'),
'sfa40': ('SFA 4:0 (butyric acid)', 'g', 'Amount'),
'sfa60': ('SFA 6:0 (caproic acid)', 'g', 'Amount'),
'sfa80': ('SFA 8:0 (caprylic acid)', 'g', 'Amount'),
'sfatot': ('Total Saturated Fatty Acids (SFA)', 'g', 'Amount'),
'sodium': ('Sodium', 'mg', 'Amount'),
'solidfat': ('Solid Fats', 'g', 'Amount'),
'sorbitol': ('Sorbitol', 'g', 'Amount'),
'starch': ('Starch', 'g', 'Amount'),
'sucpoly': ('Sucrose polyester', 'g', 'Amount'),
'sucrlose': ('Sucralose', 'mg', 'Amount'),
'sucrose': ('Sucrose', 'g', 'Amount'),
'tagatose': ('Tagatose', 'mg', 'Amount'),
'tfa161t': ('TRANS 16:1 (trans-hexadecenoic acid)', 'g',
'Amount'),
'tfa181t': ('TRANS 18:1 (trans-octadecenoic acid [elaidic acid])', # noqa
'g', 'Amount'),
'tfa182t': ('TRANS 18:2 (trans-octadecadienoic acid [linolelaidic acid]; incl. c-t, t-c, t-t)', # noqa
'g', 'Amount'),
'thiamin': ('Thiamin (vitamin B1)', 'mg', 'Amount'),
'threonin': ('Threonine', 'g', 'Amount'),
'totaltfa': ('Total Trans-Fatty Acids (TRANS)', 'g', 'Amount'),
'totcla': ('Total Conjugated Linoleic Acid (CLA 18:2)', 'g',
'Amount'),
'totfolat': ('Total Folate', 'mcg', 'Amount'),
'totsugar': ('Total Sugars', 'g', 'Amount'),
'tryptoph': ('Tryptophan', 'g', 'Amount'),
'tyrosine': ('Tyrosine', 'g', 'Amount'),
'valine': ('Valine', 'g', 'Amount'),
'vita_iu': ('Total Vitamin A Activity', 'IU', 'Amount'),
'vita_rae': ('Total Vitamin A Activity (Retinol Activity Equivalents)', # noqa
'mcg', 'Amount'),
'vita_re': ('Total Vitamin A Activity (Retinol Equivalents)',
'mcg', 'Amount'),
'vitb12': ('Vitamin B-12 (cobalamin)', 'mcg', 'Amount'),
'vitb6': ('Vitamin B-6 (pyridoxine, pyridoxyl, & pyridoxamine)',
'mg', 'Amount'),
'vitc': ('Vitamin C (ascorbic acid)', 'mg', 'Amount'),
'vitd': ('Vitamin D (calciferol)', 'mcg', 'Amount'),
'vitd2': ('Vitamin D2 (ergocalciferol)', 'mcg', 'Amount'),
'vitd3': ('Vitamin D3 (cholecalciferol)', 'mcg', 'Amount'),
'vite_iu': ('Vitamin E', 'IU', 'Amount'),
'vitk': ('Vitamin K (phylloquinone)', 'mcg', 'Amount'),
'water': ('Water', 'g', 'Amount'),
'xylitol': ('Xylitol', 'g', 'Amount'),
'zinc': ('Zinc', 'mg', 'Amount'),
'oxalicm': ('Oxalic Acid from Mayo', 'mg', 'Amount'),
'vitd_iu': ('Vitamin D', 'IU', 'Amount'),
'omega3_epadha': ('Omega-3 Fatty Acids [EPA + DHA]', 'g',
'Amount'),
'omega6_la': ('pfa182 + pfa204, la = linoleic acid', 'g',
'Amount')}
def __init__(self, transaction):
super().__init__(transaction)
def insert_food_components(self, vioscreen_food_components):
"""Add in food components results for a session
Parameters
----------
vioscreen_food_components : VioscreenFoodComponents
An instance of a food components model
Returns
-------
int
The number of inserted rows
"""
with self._transaction.cursor() as cur:
components = vioscreen_food_components.components
inserts = []
for component in components:
# checking if code exists in lookup
self._get_code_info(component.code)
inserts.append((vioscreen_food_components.sessionId,
component.code,
component.amount))
cur.executemany("""INSERT INTO ag.vioscreen_foodcomponents
(sessionId, code, amount)
VALUES (%s, %s, %s)""",
inserts)
return cur.rowcount
def get_food_components(self, sessionId):
"""Obtain the food components detail for a particular session
Parameters
----------
sessionId : str
The session ID to query
Returns
-------
VioscreenFoodComponents or None
The food components detail, or None if no record was found
"""
with self._transaction.cursor() as cur:
cur.execute("""SELECT code, amount
FROM ag.vioscreen_foodcomponents
WHERE sessionId = %s""",
(sessionId,))
rows = cur.fetchall()
if len(rows) > 0:
components = []
for code, amount in rows:
codeInfo = self._get_code_info(code)
vfcc = VioscreenFoodComponentsComponent(code=code,
description=codeInfo[0], # noqa
units=codeInfo[1], # noqa
amount=amount,
valueType=codeInfo[2]) # noqa
components.append(vfcc)
return VioscreenFoodComponents(sessionId=sessionId,
components=components)
else:
return None
def _get_code_info(self, code):
"""Obtain the detail about a particular food component by its code
Parameters
----------
code : str
The code to obtain detail for
Returns
-------
tuple
The description, units and valueType for a
particular code
Raises
------
NotFound
A NotFound error is raised if the code is unrecognized
"""
if code not in self._CODES:
raise NotFound("No such code: " + code)
return self._CODES[code]
def get_food_components_by_code(self, code):
"""Obtain the all the amounts for a particular code
Parameters
----------
code : str
The code to query
Returns
-------
Float Array or None
The amounts, or None if no record was found
"""
with self._transaction.cursor() as cur:
cur.execute("""SELECT amount
FROM ag.vioscreen_foodcomponents
WHERE code = %s""",
(code,))
rows = cur.fetchall()
if len(rows) > 0:
amounts = []
for amount in rows:
amounts.append(amount[0])
return amounts
else:
return None
def get_food_components_descriptions(self):
return self._CODES
class VioscreenEatingPatternsRepo(BaseRepo):
# code : (description, units, valueType)
_CODES = {'ADDEDFATS': ('Eating Pattern', 'PerDay', 'Amount'),
'ALCOHOLSERV': ('Eating Pattern', 'PerDay', 'Amount'),
'ANIMALPROTEIN': ('Eating Pattern', 'PerDay', 'Amount'),
'CALCDAIRYSERV': ('Eating Pattern', 'PerDay', 'Amount'),
'CALCSERV': ('Eating Pattern', 'PerDay', 'Amount'),
'FISHSERV': ('Eating Pattern', 'PerWeek', 'Amount'),
'FRIEDFISH': ('Eating Pattern', 'PerWeek', 'Amount'),
'FRTSUMM': ('Eating Pattern', 'PerDay', 'Amount'),
'GRAINSERV': ('Eating Pattern', 'PerDay', 'Amount'),
'JUICESERV': ('Eating Pattern', 'PerDay', 'Amount'),
'LOWFATDAIRYSERV': ('Eating Pattern', 'PerDay', 'Amount'),
'NOFRYFISHSERV': ('Eating Pattern', 'PerWeek', 'Amount'),
'NONFATDAIRY': ('Eating Pattern', 'PerDay', 'Amount'),
'PLANTPROTEIN': ('Eating Pattern', 'PerDay', 'Amount'),
'SALADSERV': ('Eating Pattern', 'PerDay', 'Amount'),
'SOYFOODS': ('Eating Pattern', 'PerDay', 'Amount'),
'VEGSUMM': ('Eating Pattern', 'PerDay', 'Amount')}
def __init__(self, transaction):
super().__init__(transaction)
def insert_eating_patterns(self, vioscreen_eating_patterns):
"""Add in eating patterns results for a session
Parameters
----------
vioscreen_eating_patterns : VioscreenEatingPatterns
An instance of a eating patterns model
Returns
-------
int
The number of inserted rows
"""
with self._transaction.cursor() as cur:
components = vioscreen_eating_patterns.components
inserts = []
for component in components:
# checking if code exists in lookup
self._get_code_info(component.code)
inserts.append((vioscreen_eating_patterns.sessionId,
component.code,
component.amount))
cur.executemany("""INSERT INTO ag.vioscreen_eatingpatterns
(sessionId, code, amount)
VALUES (%s, %s, %s)""",
inserts)
return cur.rowcount
def get_eating_patterns(self, sessionId):
"""Obtain the eating patterns detail for a particular session
Parameters
----------
sessionId : str
The session ID to query
Returns
-------
VioscreenEatingPatterns or None
The eating patterns detail, or None if no record was found
"""
with self._transaction.cursor() as cur:
cur.execute("""SELECT code, amount
FROM ag.vioscreen_eatingpatterns
WHERE sessionId = %s""",
(sessionId,))
rows = cur.fetchall()
if len(rows) > 0:
components = []
for code, amount in rows:
codeInfo = self._get_code_info(code)
vepc = VioscreenEatingPatternsComponent(code=code,
description=codeInfo[0], # noqa
units=codeInfo[1], # noqa
amount=amount,
valueType=codeInfo[2]) # noqa
components.append(vepc)
return VioscreenEatingPatterns(sessionId=sessionId,
components=components)
else:
return None
def _get_code_info(self, code):
"""Obtain the detail about a particular eating pattern by its code
Parameters
----------
code : str
The code to obtain detail for
Returns
-------
tuple
The description, units and valueType for a
particular code
Raises
------