-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprotected_outcome_calculations.py
1599 lines (1568 loc) · 152 KB
/
protected_outcome_calculations.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 sys
import numpy as np
import pandas as pd
import argparse
import json
import os
from datetime import datetime
from datetime import date
import time
# --------------------------------------------------------------------#
# In this script we calculate all the outcomes for the AMP-SCZ study.
# --------------------------------------------------------------------#
# --------------------------------------------------------------------#
# We first have to define all functions needed for the outcome
# calculations later.
# --------------------------------------------------------------------#
def pull_data(network, id):
# we pull the data from the json file. Therefore, we provide the network, i.e. pronet/prescient.
# network = pronet/prescient defined outside the script argv[0], id = subject id, taken from the updated file that Grace creates daily.
site=id[0:2]
sub_data = '/data/predict1/data_from_nda/{0}/PHOENIX/PROTECTED/{0}{1}/raw/{2}/surveys/{2}.{0}.json'.format(network, site, id)
with open(sub_data, 'r') as f:
json_data = json.load(f)
sub_data_all = pd.DataFrame.from_dict(json_data, orient="columns")
#replacing empty cells with NaN
sub_data_all = sub_data_all.apply(lambda x: x.str.strip()).replace('', np.nan)
return sub_data_all
def create_fake_df(var_list, all_visits, voi):
# we create a fake dataframe that includes all visits and the outcome measures.
# var_list = list with all variables that are needed for the calculation.
# all_visits = list with all visist of the amp-scz,
# voi = visits that are applicable for this outcome.
df_fake = {'variable': [var_list]*len(all_visits), 'redcap_event_name': all_visits}
df_fake = pd.DataFrame(df_fake)
df_fake['value_fake'] = '-300'
df_fake['value_fake'] = np.where(df_fake['redcap_event_name'].str.contains(voi), '-900', df_fake['value_fake'])
return df_fake
def create_fake_df_date(var_list, all_visits, voi):
# we create a fake dataframe that includes all visits and the outcome measures.
# var_list = list with all variables that are needed for the calculation.
# all_visits = list with all visist of the amp-scz,
# voi = visits that are applicable for this outcome.
df_fake = {'variable': [var_list]*len(all_visits), 'redcap_event_name': all_visits}
df_fake = pd.DataFrame(df_fake)
df_fake['value_fake'] = '1903-03-03'
df_fake['value_fake'] = np.where(df_fake['redcap_event_name'].str.contains(voi), '1909-09-09', df_fake['value_fake'])
return df_fake
def finalize_df(df_created, df_1, df_2, var_list, voi, fill_type):
# used for all dataframes in the end to create it in the way we need it for appropriate handling of all timepoints etc.(creating a clean version)
# df_created = comes from the create_fake_df function. A dataframe that includes all variables, visits and initially value_fake with -300
# df_1 = the dataframe with the actual calculated value
# df_2 = the dataframe with all visits that are relevant for the participants. used to define whether individual is chr or hc
# var_list = list of all variables needed for the calculation
# voi = visit of interest for the variable of calculation to define whether missing values are -900 or -300
# fill_type = defines the final output variable. Either float or int
string_series = pd.Series(var_list)
df_visit_pas = df_2[['redcap_event_name']]
if string_series.str.contains('nsipr').any() == True or string_series.str.contains('chrpas').any() == True:
df_1['nine'] = df_1[var_list].isin([9]).any(axis=1)
df_1['nine_str'] = df_1[var_list].isin(['9']).any(axis=1)
df_1['na'] = df_1[var_list].isnull().any(axis = 1)
df_1['NA'] = df_1[var_list].isin(['NA']).any(axis = 1)
df_1['na999'] = df_1[var_list].isin(['999']).any(axis = 1)
df_1['na999nostring'] = df_1[var_list].isin([999]).any(axis = 1)
df_1['na900'] = df_1[var_list].isin(['-900']).any(axis = 1)
df_1['na900nostring'] = df_1[var_list].isin([-900]).any(axis = 1)
df_1['na300'] = df_1[var_list].isin(['-300']).any(axis = 1)
df_1['na300nostring'] = df_1[var_list].isin([-300]).any(axis = 1)
df_1['na3'] = df_1[var_list].isin(['-3']).any(axis = 1)
df_1['na3nostring'] = df_1[var_list].isin([-3]).any(axis = 1)
df_1['na9'] = df_1[var_list].isin(['-9']).any(axis = 1)
df_1['na9nostring'] = df_1[var_list].isin([-9]).any(axis = 1)
if string_series.str.contains('nsipr').any() == True or string_series.str.contains('chrpas').any() == True:
df_calculated = df_1[['value','nine','nine_str', 'na', 'NA', 'na999nostring', 'na999', 'na900nostring','na900', 'na300nostring', 'na300', 'na3nostring','na3', 'na9nostring','na9', 'redcap_event_name']]
else:
df_calculated = df_1[['value', 'na', 'NA', 'na999nostring', 'na999', 'na900nostring','na900', 'na300nostring', 'na300', 'na3nostring','na3', 'na9nostring','na9', 'redcap_event_name']]
df_2['redcap_event_name'] = df_2['redcap_event_name'].astype(str)
df_visits = df_2[['redcap_event_name']]
df_visits = df_visits[df_visits['redcap_event_name'].str.contains(voi)]
df_concat = pd.merge(df_visits, df_calculated, on = 'redcap_event_name', how = 'left')
df2gether = pd.merge(df_created, df_concat, on = 'redcap_event_name', how = 'left')
if fill_type == 'str':
df2gether['value'] = np.where(df2gether['na'] == True, '-900', df2gether['value'])
df2gether['value'] = np.where(df2gether['na999'] == True, '-900', df2gether['value'])
df2gether['value'] = np.where(df2gether['na999nostring'] == True, '-900', df2gether['value'])
df2gether['value'] = np.where(df2gether['na900'] == True, '-900', df2gether['value'])
df2gether['value'] = np.where(df2gether['na900nostring'] == True, '-900', df2gether['value'])
df2gether['value'] = np.where(df2gether['na300'] == True, '-300', df2gether['value'])
df2gether['value'] = np.where(df2gether['na300nostring'] == True, '-300', df2gether['value'])
df2gether['value'] = np.where(df2gether['na3'] == True, '-300', df2gether['value'])
df2gether['value'] = np.where(df2gether['na3nostring'] == True, '-300', df2gether['value'])
df2gether['value'] = np.where(df2gether['na9'] == True, '-900', df2gether['value'])
df2gether['value'] = np.where(df2gether['na9nostring'] == True, '-900', df2gether['value'])
df2gether['value'] = np.where(df2gether['NA'] == True, '-900', df2gether['value'])
df2gether['value'] = np.where(pd.isna(df2gether['value']),df2gether['value_fake'], df2gether['value'])
if df_visits['redcap_event_name'].str.contains('arm_1').any():
df2gether['value'] = np.where(df2gether['redcap_event_name'].str.contains('arm_2'), '-300', df2gether['value'])
elif df_visits['redcap_event_name'].str.contains('arm_2').any():
df2gether['value'] = np.where(df2gether['redcap_event_name'].str.contains('arm_1'), '-300', df2gether['value'])
elif df_visit_pas['redcap_event_name'].str.contains('arm_2').any() and voi == 'month_1_arm_1':
df2gether['value'] = -300
df2gether['value'] = df2gether['value'].astype(str)
df2gether['value'] = np.where(pd.isna(df2gether['value']),df2gether['value_fake'], df2gether['value'])
else:
if string_series.str.contains('nsipr').any() == True or string_series.str.contains('chrpas').any() == True:
df2gether['value'] = np.where(df2gether['nine'] == True, -900, df2gether['value'])
df2gether['value'] = np.where(df2gether['nine_str'] == True, -900, df2gether['value'])
df2gether['value'] = np.where(df2gether['na'] == True, -900, df2gether['value'])
df2gether['value'] = np.where(df2gether['na999'] == True, -900, df2gether['value'])
df2gether['value'] = np.where(df2gether['na999nostring'] == True, -900, df2gether['value'])
df2gether['value'] = np.where(df2gether['na900'] == True, -900, df2gether['value'])
df2gether['value'] = np.where(df2gether['na900nostring'] == True, -900, df2gether['value'])
df2gether['value'] = np.where(df2gether['na300'] == True, -300, df2gether['value'])
df2gether['value'] = np.where(df2gether['na300nostring'] == True, -300, df2gether['value'])
df2gether['value'] = np.where(df2gether['na3'] == True, -300, df2gether['value'])
df2gether['value'] = np.where(df2gether['na3nostring'] == True, -300, df2gether['value'])
df2gether['value'] = np.where(df2gether['na9'] == True, -900, df2gether['value'])
df2gether['value'] = np.where(df2gether['na9nostring'] == True, -900, df2gether['value'])
df2gether['value'] = np.where(df2gether['NA'] == True, -900, df2gether['value'])
df2gether['value'] = np.where(pd.isna(df2gether['value']),df2gether['value_fake'], df2gether['value'])
if df_visits['redcap_event_name'].str.contains('arm_1').any():
df2gether['value'] = np.where(df2gether['redcap_event_name'].str.contains('arm_2'), -300, df2gether['value'])
elif df_visits['redcap_event_name'].str.contains('arm_2').any():
df2gether['value'] = np.where(df2gether['redcap_event_name'].str.contains('arm_1'), -300, df2gether['value'])
elif df_visit_pas['redcap_event_name'].str.contains('arm_2').any() and voi == 'month_1_arm_1':
df2gether['value'] = -300
df2gether['value'] = df2gether['value'].astype(float)
df2gether['value'] = np.where(pd.isna(df2gether['value']),df2gether['value_fake'], df2gether['value'])
clean_df = df2gether[['variable', 'redcap_event_name', 'value']]
if fill_type == 'float':
clean_df['value'] = np.round(clean_df['value'].astype(fill_type),3)
else:
clean_df['value'] = clean_df['value'].astype(fill_type)
return clean_df
def finalize_df_date(df_created, df_1, df_2, var_list, voi, fill_type):
# used for all dataframes in the end to create it in the way we need it for appropriate handling of all timepoints etc.(creating a clean version)
# df_created = comes from the create_fake_df function. A dataframe that includes all variables, visits and initially value_fake with -300
# df_1 = the dataframe with the actual calculated value
# df_2 = the dataframe with all visits that are relevant for the participants. used to define whether individual is chr or hc
# var_list = list of all variables needed for the calculation
# voi = visit of interest for the variable of calculation to define whether missing values are -900 or -300
# fill_type = defines the final output variable. Either float or int
string_series = pd.Series(var_list)
df_visit_pas = df_2[['redcap_event_name']]
df_1['na'] = df_1[var_list].isnull().any(axis = 1)
df_1['NA'] = df_1[var_list].isin(['NA']).any(axis = 1)
df_1['na999'] = df_1[var_list].isin(['999']).any(axis = 1)
df_1['na999nostring'] = df_1[var_list].isin([999]).any(axis = 1)
df_1['na900'] = df_1[var_list].isin(['-900']).any(axis = 1)
df_1['na900nostring'] = df_1[var_list].isin([-900]).any(axis = 1)
df_1['na300'] = df_1[var_list].isin(['-300']).any(axis = 1)
df_1['na300nostring'] = df_1[var_list].isin([-300]).any(axis = 1)
df_1['na3'] = df_1[var_list].isin(['-3']).any(axis = 1)
df_1['na3nostring'] = df_1[var_list].isin([-3]).any(axis = 1)
df_1['na9'] = df_1[var_list].isin(['-9']).any(axis = 1)
df_1['na9nostring'] = df_1[var_list].isin([-9]).any(axis = 1)
df_1['nadate9'] = df_1[var_list].isin(['1909-09-09']).any(axis = 1)
df_1['nadate3'] = df_1[var_list].isin(['1903-03-03']).any(axis = 1)
df_calculated = df_1[['value', 'nadate3', 'nadate9','na', 'NA', 'na999nostring', 'na999', 'na900nostring','na900', 'na300nostring', 'na300', 'na3nostring','na3', 'na9nostring','na9', 'redcap_event_name']]
df_2['redcap_event_name'] = df_2['redcap_event_name'].astype(str)
df_visits = df_2[['redcap_event_name']]
df_visits = df_visits[df_visits['redcap_event_name'].str.contains(voi)]
df_concat = pd.merge(df_visits, df_calculated, on = 'redcap_event_name', how = 'left')
df2gether = pd.merge(df_created, df_concat, on = 'redcap_event_name', how = 'left')
if fill_type == 'str':
df2gether['value'] = np.where(df2gether['nadate9'] == True, '1909-09-09', df2gether['value'])
df2gether['value'] = np.where(df2gether['nadate3'] == True, '1909-09-09', df2gether['value'])
df2gether['value'] = np.where(df2gether['na'] == True, '1909-09-09', df2gether['value'])
df2gether['value'] = np.where(df2gether['na999'] == True, '1909-09-09', df2gether['value'])
df2gether['value'] = np.where(df2gether['na999nostring'] == True, '1909-09-09', df2gether['value'])
df2gether['value'] = np.where(df2gether['na900'] == True, '1909-09-09', df2gether['value'])
df2gether['value'] = np.where(df2gether['na900nostring'] == True, '1909-09-09', df2gether['value'])
df2gether['value'] = np.where(df2gether['na300'] == True, '1903-03-03', df2gether['value'])
df2gether['value'] = np.where(df2gether['na300nostring'] == True, '1903-03-03', df2gether['value'])
df2gether['value'] = np.where(df2gether['na3'] == True, '1903-03-03', df2gether['value'])
df2gether['value'] = np.where(df2gether['na3nostring'] == True, '1903-03-03', df2gether['value'])
df2gether['value'] = np.where(df2gether['na9'] == True, '1909-09-09', df2gether['value'])
df2gether['value'] = np.where(df2gether['na9nostring'] == True, '1909-09-09', df2gether['value'])
df2gether['value'] = np.where(df2gether['NA'] == True, '1909-09-09', df2gether['value'])
df2gether['value'] = np.where(pd.isna(df2gether['value']),df2gether['value_fake'], df2gether['value'])
if df_visits['redcap_event_name'].str.contains('arm_1').any():
df2gether['value'] = np.where(df2gether['redcap_event_name'].str.contains('arm_2'), '1903-03-03', df2gether['value'])
elif df_visits['redcap_event_name'].str.contains('arm_2').any():
df2gether['value'] = np.where(df2gether['redcap_event_name'].str.contains('arm_1'), '1903-03-03', df2gether['value'])
elif df_visit_pas['redcap_event_name'].str.contains('arm_2').any() and voi == 'month_1_arm_1':
df2gether['value'] = '1903-03-03'
df2gether['value'] = df2gether['value'].astype(str)
df2gether['value'] = np.where(pd.isna(df2gether['value']),df2gether['value_fake'], df2gether['value'])
clean_df = df2gether[['variable', 'redcap_event_name', 'value']]
clean_df['value'] = clean_df['value'].astype(fill_type)
return clean_df
def create_total_division(outcome, df_1, df_2, var_list, division, visit_of_interest, all_visits, fill_type):
# we create the total (sum) score and if applicable divide it.
# outcome = string that defines the outcome name, e.g., bprs_total
# df_1 = the dataframe including the actual variables needed to calculate the outcome. This is most of the time df_all, but sometimes is the dataframe created by previous calculations, e.g., promis_df
# df_2 = the dataframe with all visits that are relevant for the participants. used to define whether individual is chr or hc
# var_list = list of all variables needed for the calculation
# visit_of_interest = visit of interest for the variable of calculation to define whether missing values are -900 or -300
# all_visits = list with all visist of the amp-scz,
# fill_type = defines the final output variable. Either float or int
df_fake = create_fake_df(outcome, all_visits, visit_of_interest)
if df_2['redcap_event_name'].str.contains('arm_1').any():
df_1 = df_1[df_1['redcap_event_name'].str.contains('arm_1')]
elif df_2['redcap_event_name'].str.contains('arm_2').any():
df_1 = df_1[df_1['redcap_event_name'].str.contains('arm_2')]
df_1 = df_1[df_1['redcap_event_name'].str.contains(visit_of_interest)]
df_1['value'] = df_1[var_list].fillna(-900).astype(fill_type).sum(axis = 1)/division
final_df = finalize_df(df_fake, df_1, df_2, var_list, visit_of_interest, fill_type)
return final_df
def create_max(outcome, df_1, df_2, var_list, visit_of_interest, all_visits, fill_type):
# we create the total (max) score
# outcome = string that defines the outcome name, e.g., bprs_total
# df_1 = the dataframe including the actual variables needed to calculate the outcome. This is most of the time df_all, but sometimes is the dataframe created by previous calculations, e.g., promis_df
# df_2 = the dataframe with all visits that are relevant for the participants. used to define whether individual is chr or hc
# var_list = list of all variables needed for the calculation
# visit_of_interest = visit of interest for the variable of calculation to define whether missing values are -900 or -300
# all_visits = list with all visist of the amp-scz,
# fill_type = defines the final output variable. Either float or int
df_fake = create_fake_df(outcome, all_visits, visit_of_interest)
df_1['value'] = df_1[var_list].fillna(-900).astype(fill_type).max(axis = 1)
final_df = finalize_df(df_fake, df_1, df_2, var_list, visit_of_interest, fill_type)
return final_df
def create_min_date(outcome, df_1, df_2, var_list, visit_of_interest, all_visits, fill_type):
# we create the total (min) date
# outcome = string that defines the outcome name, e.g., bprs_total
# df_1 = the dataframe including the actual variables needed to calculate the outcome. This is most of the time df_all, but sometimes is the dataframe created by previous calculations, e.g., promis_df
# df_2 = the dataframe with all visits that are relevant for the participants. used to define whether individual is chr or hc
# var_list = list of all variables needed for the calculation
# visit_of_interest = visit of interest for the variable of calculation to define whether missing values are -900 or -300
# all_visits = list with all visist of the amp-scz,
# fill_type = defines the final output variable. Either float or int
df_fake = create_fake_df_date(outcome, all_visits, visit_of_interest)
pd.set_option('display.max_columns', None)
# the problem here is that I have to update again the date calculation because at the moment it always will pick the dates with 1909-09-09
df_1[var_list] = df_1[var_list].fillna('2090-09-09').astype(fill_type)
df_1[var_list] = np.where(df_1[var_list] == '1909-09-09', '2090-09-09', df_1[var_list])
df_1[var_list] = np.where(df_1[var_list] == '1903-03-03', '2033-03-03', df_1[var_list])
df_1[var_list] = df_1[var_list].apply(pd.to_datetime, format = '%Y-%m-%d', errors = 'coerce')
df_1['value'] = df_1[var_list].min(axis = 1)
df_1['value'] = df_1['value'].astype(fill_type)
df_1['value'] = np.where(df_1['value'] == '2090-09-09', '1909-09-09', df_1['value'])
df_1['value'] = np.where(df_1['value'] == '2033-03-03', '1903-03-03', df_1['value'])
df_1[var_list] = df_1[var_list].astype(fill_type)
final_df = finalize_df_date(df_fake, df_1, df_2, var_list, visit_of_interest, fill_type)
final_df['value'] = pd.to_datetime(final_df['value'], format='%Y-%m-%d').dt.date
return final_df
def create_mul(outcome, df_1, df_2, var_list, visit_of_interest, all_visits, fill_type):
# we create the total (multiplied) score.
# outcome = string that defines the outcome name, e.g., bprs_total
# df_1 = the dataframe including the actual variables needed to calculate the outcome. This is most of the time df_all, but sometimes is the dataframe created by previous calculations, e.g., promis_df
# df_2 = the dataframe with all visits that are relevant for the participants. used to define whether individual is chr or hc
# var_list = list of all variables needed for the calculation
# visit_of_interest = visit of interest for the variable of calculation to define whether missing values are -900 or -300
# all_visits = list with all visist of the amp-scz,
# fill_type = defines the final output variable. Either float or int
df_fake = create_fake_df(outcome, all_visits, visit_of_interest)
df_1['value'] = df_1[var_list].fillna(-900).astype(fill_type).prod(axis = 1)
final_df = finalize_df(df_fake, df_1, df_2, var_list, visit_of_interest, fill_type)
return final_df
def create_decline(outcome, df_1, df_2, var_list, visit_of_interest, all_visits, fill_type):
# we create the subtraction between two variables
# outcome = string that defines the outcome name, e.g., bprs_total
# df_1 = the dataframe including the actual variables needed to calculate the outcome. This is most of the time df_all, but sometimes is the dataframe created by previous calculations, e.g., promis_df
# df_2 = the dataframe with all visits that are relevant for the participants. used to define whether individual is chr or hc
# var_list = list of all variables needed for the calculation
# visit_of_interest = visit of interest for the variable of calculation to define whether missing values are -900 or -300
# all_visits = list with all visist of the amp-scz,
# fill_type = defines the final output variable. Either float or int
df_fake = create_fake_df(outcome, all_visits, visit_of_interest)
df_1[var_list[0]] = df_1[var_list[0]].fillna(-900).astype(fill_type)
df_1[var_list[1]] = df_1[var_list[1]].fillna(-900).astype(fill_type)
df_1['value'] = df_1[var_list[0]] - df_1[var_list[1]]
final_df = finalize_df(df_fake, df_1, df_2, var_list, visit_of_interest, fill_type)
return final_df
def create_use_value(outcome, df_1, df_2, var_list, visit_of_interest, all_visits, fill_type):
# we create the total (sum) score and if applicable divide it.
# outcome = string that defines the outcome name, e.g., bprs_total
# df_1 = the dataframe including the actual variables needed to calculate the outcome. This is most of the time df_all, but sometimes is the dataframe created by previous calculations, e.g., promis_df
# df_2 = the dataframe with all visits that are relevant for the participants. used to define whether individual is chr or hc
# var_list = list of all variables needed for the calculation
# visit_of_interest = visit of interest for the variable of calculation to define whether missing values are -900 or -300
# all_visits = list with all visist of the amp-scz,
# fill_type = defines the final output variable. Either float or int
df_fake = create_fake_df(outcome, all_visits, visit_of_interest)
df_1['value'] = df_1[var_list[0]]
final_df = finalize_df(df_fake, df_1, df_2, var_list, visit_of_interest, fill_type)
return final_df
def create_condition_value(outcome, df_1, df_2, visit_of_interest, all_visits, fill_type, given_value):
# after setting up the specific condition (if), we create the dataframe with the given value.
# outcome = string that defines the outcome name, e.g., bprs_total
# df_1 = the dataframe including the actual variables needed to calculate the outcome. This is most of the time df_all, but sometimes is the dataframe created by previous calculations, e.g., promis_df
# df_2 = the dataframe with all visits that are relevant for the participants. used to define whether individual is chr or hc
# visit_of_interest = visit of interest for the variable of calculation to define whether missing values are -900 or -300
# all_visits = list with all visist of the amp-scz,
# fill_type = defines the final output variable. Either float or int
# given_value = as soon as a condition is met we have defined values from Dominic Oliver that are filled in here.
df_fake = create_fake_df(outcome, all_visits, visit_of_interest)
df_1['value'] = given_value
df_calculated = df_1['value']
df_2['redcap_event_name'] = df_2['redcap_event_name'].astype(str)
df_visits = df_2[['redcap_event_name']]
df_visits = df_visits[df_visits['redcap_event_name'].str.contains(visit_of_interest)]
df_concat = pd.concat([df_visits, df_calculated], axis = 1)
df2gether = pd.merge(df_fake, df_concat, on = 'redcap_event_name', how = 'left').replace(np.nan, pd.NA).astype('object')
if df_visits['redcap_event_name'].str.contains('arm_1').any():
df2gether['value'] = np.where(df2gether['redcap_event_name'].str.contains('arm_2'), '-300', df2gether['value'])
elif df_visits['redcap_event_name'].str.contains('arm_2').any():
df2gether['value'] = np.where(df2gether['redcap_event_name'].str.contains('arm_1'), '-300', df2gether['value'])
df2gether['value'] = np.where(pd.isnull(df2gether['value']),df2gether['value_fake'], df2gether['value'])
clean_df = df2gether[['variable', 'redcap_event_name', 'value']]
clean_df['value'] = clean_df['value'].astype(fill_type)
return clean_df
def create_assist(outcome, df_1, df_2, assist_var1, assist_var2, var_list, division, visit_of_interest, all_visits, fill_type):
# ASSIST is a special case because we need one extra variable (whether or not individuals use the substance itself. If not we do not need to calculate the sum.
# outcome = string that defines the outcome name, e.g., bprs_total
# df_1 = the dataframe including the actual variables needed to calculate the outcome. This is most of the time df_all, but sometimes is the dataframe created by previous calculations, e.g., promis_df
# df_2 = the dataframe with all visits that are relevant for the participants. used to define whether individual is chr or hc
# assist_var = variable whether individuals use the substance at all.
# var_list = list of all variables needed for the calculation
# division = as the common function (create_total_division) is called here we also give a division number (in this case 1).
# visit_of_interest = visit of interest for the variable of calculation to define whether missing values are -900 or -300
# all_visits = list with all visist of the amp-scz,
# fill_type = defines the final output variable. Either float or int
final_df = create_total_division(outcome, df_1, df_2, var_list, division, visit_of_interest, all_visits, fill_type)
final_df_pastmonth_zero = create_total_division(outcome, df_1, df_2, var_list[4:5], division, visit_of_interest, all_visits, fill_type)
final_df_pastmonth_zero['outcome_pastmonth_zero'] = final_df_pastmonth_zero['value']
final_df_pastmonth_zero = final_df_pastmonth_zero[['outcome_pastmonth_zero', 'redcap_event_name']]
df_1[assist_var1] = df_1[assist_var1].fillna(-900).astype(int)
df_1['no_use'] = df_1[[assist_var1]].isin([0]).any(axis = 1)
df_1[assist_var2] = df_1[assist_var2].fillna(-900).astype(int)
df_1['no_use_pastmonth'] = df_1[[assist_var2]].isin([0]).any(axis = 1)
df_1 = df_1[['no_use', 'no_use_pastmonth', 'redcap_event_name']]
df_assist_created = pd.merge(final_df, df_1, on = 'redcap_event_name', how = 'left')
df_assist_created = pd.merge(df_assist_created, final_df_pastmonth_zero, on = 'redcap_event_name', how = 'left')
df_assist_created['value'] = np.where(df_assist_created['no_use'] == True, 0, df_assist_created['value'])
df_assist_created['value'] = np.where(df_assist_created['no_use_pastmonth'] == True, df_assist_created['outcome_pastmonth_zero'], df_assist_created['value'])
final_assist = df_assist_created[['variable', 'redcap_event_name', 'value']]
return final_assist
def create_sips_groups_scr(new_sips_group, df, onsetdate_sips_group, visit_of_interest, all_visits, psychosis_onset_df, sips_vars_interest_screening,\
psychosis_str):
# create the different sips group assignments at screening
# df: dataframe (df_all) including all variables
# onsetdate_sips_group: onset date of the sips-group for all symptoms
# visit_of_interest: the visits (here probably voi_8) that are of interest for the questionnaire (psychs)
# all_visits: list with all visits (all_visits_list)
# psychosis_onset_df: dataframe with the psychosis conversion date (conversion_date_fu)
# sips_vars_interest_fu: vars of interest for the follow-up visits (e.g. bips symptoms)
# psychosis_str = variable indicating psychosis conversion or not *psychs_fu_ac1_conv
# new_sips_group = string iwth the name for the new sips-group
# visit_of interest_2 = same as in visit of interest (psychs visits) but including also screenign here.
conversion_sips_group_date_scr = create_min_date('conversion_sips_group_date', df, df, onsetdate_sips_group, visit_of_interest, all_visits, 'str')
conversion_sips_group_date_scr['conversion_sips_group_date'] = pd.to_datetime(conversion_sips_group_date_scr['value'])
conversion_sips_group_date_scr = conversion_sips_group_date_scr[['redcap_event_name', 'conversion_sips_group_date']]
conversion_date_scr_match = psychosis_onset_df.copy()
conversion_date_scr_match['conversion_date']=conversion_date_scr_match['value']
conversion_date_scr_match = conversion_date_scr_match[['redcap_event_name', 'conversion_date']]
df_all_copy=df.copy()
combined_vars = ['redcap_event_name', psychosis_str] + sips_vars_interest_screening
df_all_copy=df_all_copy[combined_vars]
sips_conv_vars = [psychosis_str] + sips_vars_interest_screening
df_all_copy[sips_conv_vars] = df_all_copy[sips_conv_vars].astype(str)
sips_merged = pd.merge(pd.merge(conversion_date_scr_match, conversion_sips_group_date_scr, on = 'redcap_event_name', how = 'left'),df_all_copy, on = 'redcap_event_name', how = 'left')
sips_merged['sips_iv'] = np.where((sips_merged[sips_vars_interest_screening]=='1').any(axis=1), '1',\
np.where((sips_merged[sips_vars_interest_screening]=='0').all(axis=1), '0','-900'))
sips_merged['psychs_fu_ac8_new']=np.where((sips_merged['sips_iv']=='1') & ((sips_merged[psychosis_str]=='0')|\
(not any (date in ('1909-09-09', '1903-03-03') for date in sips_merged[['conversion_date', 'conversion_sips_group_date']])) &\
(sips_merged['conversion_sips_group_date']<sips_merged['conversion_date'])), '1',\
np.where((sips_merged['sips_iv']=='0')|\
((sips_merged[psychosis_str]=='1')&\
(not any (date in ('1909-09-09', '1903-03-03') for date in sips_merged[['conversion_date', 'conversion_sips_group_date']])) &\
(sips_merged['conversion_sips_group_date']>sips_merged['conversion_date'])), '0','-900'))
sips_new_final = create_use_value(new_sips_group, sips_merged, df_all, ['psychs_fu_ac8_new'], visit_of_interest, all_visits, 'int')
# create the overall lifetime bips variable
return sips_new_final
def create_sips_groups(new_sips_group,sips_group_lifetime, df, df_sips_group_scr, onsetdate_sips_group, visit_of_interest, all_visits, conversion_df, sips_vars_interest_fu,\
conv_str, visit_of_interest_2):
# create the different sips group assignments at follow-up
# df: dataframe (df_all) including all variables
# sips_vars_interest_scr: all screening variables needed to define baseline diagnosis
# onsetdate_sips_group: onset date of the sips-group for all symptoms
# visit_of_interest: the visits (here probably voi_8) that are of interest for the questionnaire (psychs)
# all_visits: list with all visits (all_visits_list)
# conversion_df: dataframe with the psychosis conversion date (conversion_date_fu)
# sips_vars_interest_fu: vars of interest for the follow-up visits (e.g. bips symptoms)
# conv_str = variable indicating psychosis conversion or not *psychs_fu_ac1_conv
# new_sips_group = string iwth the name for the new sips-group
# sips_group_lifetime = string with the name for the sips-group lifetime
# visit_of interest_2 = same as in visit of interest (psychs visits) but including also screenign here.
sips_scr = df_sips_group_scr.copy()
sips_vars_scr = ['redcap_event_name', 'value']
sips_scr = sips_scr[sips_vars_scr]
sips_scr['iv_scr']=np.where((sips_scr['value']==1), '1',\
np.where((sips_scr['value']==0), '0','-900'))
sips_scr['value_1']=sips_scr['value']
sips_scr=sips_scr[['redcap_event_name', 'iv_scr', 'value_1']]
conversion_sips_group_date_fu = create_min_date('conversion_sips_group_date', df, df, onsetdate_sips_group, visit_of_interest, all_visits, 'str')
conversion_sips_group_date_fu['conversion_sips_group_date'] = pd.to_datetime(conversion_sips_group_date_fu['value'])
conversion_sips_group_date_fu = conversion_sips_group_date_fu[['redcap_event_name', 'conversion_sips_group_date']]
conversion_date_fu_match = conversion_df.copy()
conversion_date_fu_match['conversion_date']=conversion_date_fu_match['value']
conversion_date_fu_match = conversion_date_fu_match[['redcap_event_name', 'conversion_date']]
df_all_copy=df.copy()
combined_vars = ['redcap_event_name', conv_str] + sips_vars_interest_fu
df_all_copy=df_all_copy[combined_vars]
sips_conv_vars = [conv_str] + sips_vars_interest_fu
df_all_copy[sips_conv_vars] = df_all_copy[sips_conv_vars].astype(str)
sips_merged = pd.merge(pd.merge(conversion_date_fu_match, conversion_sips_group_date_fu, on = 'redcap_event_name', how = 'left'),df_all_copy, on = 'redcap_event_name', how = 'left')
sips_merged['sips_iv'] = np.where((sips_merged[sips_vars_interest_fu]=='1').any(axis=1), '1',\
np.where((sips_merged[sips_vars_interest_fu]=='0').all(axis=1), '0','-900'))
sips_merged['psychs_fu_ac8_new']=np.where((sips_merged['sips_iv']=='1') & ((sips_merged[conv_str]=='0')|\
(not any (date in ('1909-09-09', '1903-03-03') for date in sips_merged[['conversion_date', 'conversion_sips_group_date']])) &\
(sips_merged['conversion_sips_group_date']<sips_merged['conversion_date'])), '1',\
np.where((sips_merged['sips_iv']=='0')|\
((sips_merged[conv_str]=='1')&\
(not any (date in ('1909-09-09', '1903-03-03') for date in sips_merged[['conversion_date', 'conversion_sips_group_date']])) &\
(sips_merged['conversion_sips_group_date']>sips_merged['conversion_date'])), '0','-900'))
sips_new_final = create_use_value(new_sips_group, sips_merged, df_all, ['psychs_fu_ac8_new'], visit_of_interest, all_visits, 'int')
# create the overall lifetime bips variable
sips_ac = pd.merge(sips_new_final, sips_scr, on = 'redcap_event_name', how = 'left')
sips_ac['value'] = sips_ac['value'].astype(str)
sips_ac['sips_iv_yesno'] = np.where((sips_ac['value'] == '1'), 1, 0)
sips_ac['cumulative_sum'] = sips_ac['sips_iv_yesno'].shift(1).fillna(0).cumsum()
sips_ac['sips_scr_yesno'] = np.where((sips_ac['iv_scr']=='1'), 1, 0)
sips_ac['cumulative_sum_scr'] =sips_ac['sips_scr_yesno'].shift(1).fillna(0).cumsum()
sips_ac['psychs_fu_ac8'] = np.where((sips_ac['cumulative_sum_scr'] > 0)|(sips_ac['cumulative_sum'] > 0)|(sips_ac['value']=='1')|(sips_ac['iv_scr']=='1'), '1', '0')
sips_ac[['value', 'iv_scr', 'redcap_event_name', 'psychs_fu_ac8']] = sips_ac[['value', 'iv_scr', 'redcap_event_name', 'psychs_fu_ac8']].astype(str).apply(lambda x: x.str.strip())
sips_ac['psychs_fu_ac8_final']=np.where(((((sips_ac['value']=='-900')|(sips_ac['value']=='-300'))&(sips_ac['value'] != '1'))&\
(sips_ac['redcap_event_name']!='screening_arm_1')&(sips_ac['redcap_event_name'] !='screening_arm_2'))|\
((sips_ac['iv_scr']=='-900') & \
((sips_ac['redcap_event_name'] == 'screening_arm_1')|(sips_ac['redcap_event_name'] =='screening_arm_2'))),\
'-900', sips_ac['psychs_fu_ac8'])
sips_ac_final = create_use_value(sips_group_lifetime, sips_ac, df, ['psychs_fu_ac8_final'], visit_of_interest_2, all_visits, 'int')
return sips_new_final, sips_ac_final
# --------------------------------------------------------------------#
# Here we load the data
# --------------------------------------------------------------------#
all_visits_list = ['screening_arm_1', 'baseline_arm_1', 'floating_forms_arm_1', 'month_1_arm_1', 'month_2_arm_1', 'month_3_arm_1', 'month_4_arm_1', 'month_5_arm_1', 'month_6_arm_1',\
'month_7_arm_1', 'month_8_arm_1', 'month_9_arm_1', 'month_10_arm_1', 'month_11_arm_1', 'month_12_arm_1', 'month_18_arm_1','month_24_arm_1', 'conversion_arm_1',\
'screening_arm_2', 'baseline_arm_2', 'floating_forms_arm_2', 'month_1_arm_2', 'month_2_arm_2', 'month_3_arm_2', 'month_4_arm_2', 'month_5_arm_2', 'month_6_arm_2',\
'month_7_arm_2', 'month_8_arm_2', 'month_9_arm_2', 'month_10_arm_2', 'month_11_arm_2', 'month_12_arm_2', 'month_18_arm_2','month_24_arm_2', 'conversion_arm_2']
network = sys.argv[1]
Network = network.capitalize()
version = sys.argv[2]
# list of ids to include depending on the network
ids = pd.read_csv('/data/pnl/home/gj936/U24/Clinical_qc/flowqc/REAL_DATA/{0}_sub_list.txt'.format(network), sep= '\n', index_col = False, header = None)
# Load the data. Depending on which network you load the data from you have to apply some different wrangling.
if Network == 'Pronet':
if version == 'test' or version == 'create_control':
id_list = ['YA16606', 'YA01508', 'LA00145', 'LA00834', 'OR00697', 'PI01355', 'HA04408']
elif version == 'run_outcome':
id_list = ids.iloc[:, 0].tolist()
elif Network == 'Prescient':
if version == 'test' or version == 'create_control':
id_list = ['ME00772', 'ME78581','BM90491', 'ME33634', 'ME20845', 'BM73097', 'ME21922']
elif version == 'run_outcome':
id_list = ids.iloc[2:, 0].tolist()
id_list = [s.split(' ')[1] if ' ' in s else s for s in id_list]
subject_list = []
start_time = time.time()
for i, id in enumerate(id_list, 1):
print(f"Iteration {i}: ID: {id}")
elapsed_time = time.time()-start_time
print(f"Elapsed time: {elapsed_time:.2f} second")
# load the json data
site=id[0:2]
sub_data = '/data/predict1/data_from_nda/{0}/PHOENIX/PROTECTED/{0}{1}/raw/{2}/surveys/{2}.{0}.json'.format(Network, site, id)
if os.path.isfile(sub_data):
print(f"File {sub_data} is present")
df_all = pull_data(Network, id)
else:
print(f"File {sub_data} does not exist, skipping...")
continue
# --------------------------------------------------------------------#
# We first extract/create some important variables for the script
# --------------------------------------------------------------------#
# create the visits relevant for several variables:
voi_0 = ''
voi_1 = 'basel|month_1_arm_1|month_2_|month_3_arm_1|month_6_arm_1|month_12_arm_1|month_18_arm_1|month_24_arm_1|conversion_arm_1'
voi_2 = 'basel'
voi_3 = 'month_1_arm_1'
voi_4 = 'basel|month_1_arm_1|month_2_|month_3_arm_1|month_4_arm_1|month_5_arm_1|month_6_arm_1|month_7_arm_1|month_8_arm_1|month_9_arm_1|month_10_arm_1|month_11_arm_1|month_12_arm_1|month_18_arm_1|month_24_arm_1|conversion_arm_1'
voi_5 = 'basel|month_2_|month_6_|month_12_arm_1|month_18_arm_1|month_24_arm_1|conversion_arm_1'
voi_6 = 'screening'
voi_7 = 'basel|month_2_|month_6_arm_1|month_12_arm_1|month_18_arm_1|month_24_arm_1|conversion_arm_1'
voi_8 = 'basel|month_1_arm_1|month_2_|month_3_arm_1|month_6_arm_1|month_12_|month_18_arm_1|month_24_|conversion_'
voi_9 = 'screening|basel|month_1_|month_2_|month_3_|month_4_|month_5_|month_6|month_7_|month_8_|month_9|month_10_|month_11_|month_12_|month_18_|month_24_|conversion_|floating'
voi_10= 'screening|basel|month_1_arm_1|month_2_|month_3_arm_1|month_6_arm_1|month_12_|month_18_arm_1|month_24_|conversion_'
if df_all['redcap_event_name'].astype(str).str.contains('arm_1').any():
print("subject is arm_1 meaning chr")
group = 'chr'
elif df_all['redcap_event_name'].astype(str).str.contains('arm_2').any():
print("subject is arm_2 meaning hc")
group = 'hc'
if df_all['chrdemo_sexassigned'].astype(str).str.contains('1').any():
print("subject is male")
sex = 'male'
elif df_all['chrdemo_sexassigned'].astype(str).str.contains('2').any():
print("subject is female")
sex = 'female'
else:
print("subject is unknown sex")
sex = 'unknown'
baseln_df = df_all[df_all['redcap_event_name'].str.contains('basel')]
# if id == 'ME61146':
# baseln_df = df_all[df_all['redcap_event_name'].str.contains('baseline_arm_1')]
if group == 'chr':
age_1 = baseln_df['chrdemo_age_yrs_chr'].fillna(-900).to_numpy(dtype=float)
age_2 = baseln_df['chrdemo_age_mos_chr'].fillna(-900).to_numpy(dtype=float)/12
if age_2 <0:
age_2 = age_2 * 12
elif group == 'hc':
age_1 = baseln_df['chrdemo_age_yrs_hc'].fillna(-900).to_numpy(dtype=float)
age_2 = baseln_df['chrdemo_age_mos_hc'].fillna(-900).to_numpy(dtype=float)/12
if age_2 <0:
age_2 = age_2 * 12
age_3 = baseln_df['chrdemo_age_mos3'].fillna(-900).to_numpy(dtype=float)
age_4 = baseln_df['chrdemo_age_mos2'].fillna(-900).to_numpy(dtype=float)/12
if age_4 <0:
age_4 = age_4 * 12
if age_1 != -900 and age_1 != -3 and age_1 != -9:
age = age_1
elif age_2 != -900 and age_2 != -3 and age_2 != -9:
age = age_2
elif age_3 != -900 and age_3 != -3 and age_3 != -9:
age = age_3
else:
print("What is the problem with age")
age = age_4
if age.size == 0:
age = -900
print("age")
print( age)
df_all['chrpas_pmod_adult3v1'] = np.where(df_all['chrpas_pmod_adult3v1'] == '1909-09-09', -900, df_all['chrpas_pmod_adult3v1'])
df_all['chrpas_pmod_adult3v1'] = df_all['chrpas_pmod_adult3v1'].fillna(-900).astype(int)
df_all['chrpas_pmod_adult3v1'] = np.where(df_all['chrpas_pmod_adult3v1'] == 9, -900, df_all['chrpas_pmod_adult3v1'])
df_all['chrpas_pmod_adult3v3'] = np.where(df_all['chrpas_pmod_adult3v3'] == '1909-09-09', -900, df_all['chrpas_pmod_adult3v3'])
df_all['chrpas_pmod_adult3v3'] = df_all['chrpas_pmod_adult3v3'].fillna(-900).astype(int)
df_all['chrpas_pmod_adult3v3'] = np.where(df_all['chrpas_pmod_adult3v3'] == 9, -900, df_all['chrpas_pmod_adult3v3'])
month1_df = df_all[df_all['redcap_event_name'].str.contains('month_1_')]
married_1 = np.nan_to_num(month1_df['chrpas_pmod_adult3v1'].to_numpy(dtype=int), nan = -900)
married_1 = np.where(married_1.size == 0, -900, married_1)
print("Married: currently or previously married")
print(married_1)
married_2 = np.nan_to_num(month1_df['chrpas_pmod_adult3v3'].to_numpy(dtype=int), nan = -900)
married_2 = np.where(married_2.size == 0, -900, married_2)
print("Married: never married ")
print(married_2)
if married_1.size == 0:
print("married_1")
print(married_1)
married_1 = -900
if married_2.size == 0:
print("married_2")
print(married_2)
married_2 = -900
# --------------------------------------------------------------------#
# CDSS
# --------------------------------------------------------------------#
cdss = create_total_division('chrcdss_total', df_all, df_all, ['chrcdss_calg1', 'chrcdss_calg2', 'chrcdss_calg3', 'chrcdss_calg4',\
'chrcdss_calg5', 'chrcdss_calg6', 'chrcdss_calg7', 'chrcdss_calg8', 'chrcdss_calg9'], 1, voi_1, all_visits_list, 'int')
cdss['data_type'] = 'Integer'
# --------------------------------------------------------------------#
# Perceived Discrimination Scale
# --------------------------------------------------------------------#
pdt = create_total_division('chrpds_perceived_discrimination_total', df_all, df_all, ['chrdim_dim_yesno_q1_1','chrdlm_dim_yesno_q1_2','chrdlm_dim_sex','chrdlm_dim_yesno_age','chrdlm_dim_yesno_q4_1',\
'chrdlm_dim_yesno_q5','chrdlm_dim_yesno_q3','chrdlm_dim_yesno_q6','chrdlm_dim_yesno_other'], 1, voi_2, all_visits_list, 'int')
pdt['data_type'] = 'Integer'
# --------------------------------------------------------------------#
# OASIS
# --------------------------------------------------------------------#
oasis = create_total_division('chroasis_total', df_all, df_all, ['chroasis_oasis_1','chroasis_oasis_2', 'chroasis_oasis_3', 'chroasis_oasis_4', 'chroasis_oasis_5'], 1, voi_1, all_visits_list, 'int')
oasis['data_type'] = 'Integer'
# --------------------------------------------------------------------#
# Perceived Stress Scale
# --------------------------------------------------------------------#
pss_df = df_all
pss_df['chrpss_pssp2_1'] = 4 - pss_df['chrpss_pssp2_1'].astype(float)
pss_df['chrpss_pssp2_2'] = 4 - pss_df['chrpss_pssp2_2'].astype(float)
pss_df['chrpss_pssp2_4'] = 4 - pss_df['chrpss_pssp2_4'].astype(float)
pss_df['chrpss_pssp2_5'] = 4 - pss_df['chrpss_pssp2_5'].astype(float)
pss = create_total_division('chrpss_perceived_stress_scale_total', pss_df, df_all, ['chrpss_pssp1_1','chrpss_pssp1_2', 'chrpss_pssp1_3','chrpss_pssp2_1', 'chrpss_pssp2_2','chrpss_pssp2_3',\
'chrpss_pssp2_4','chrpss_pssp2_5', 'chrpss_pssp3_1','chrpss_pssp3_4'], 1, voi_1, all_visits_list, 'int')
pss['data_type'] = 'Integer'
# --------------------------------------------------------------------#
# BPRS
# --------------------------------------------------------------------#
bprs0 = create_use_value('chrbprs_bprs_total', df_all, df_all, ['chrbprs_bprs_total'], voi_4, all_visits_list, 'int')
bprs1 = create_total_division('chrbprs_affect_subscale', df_all, df_all, ['chrbprs_bprs_depr', 'chrbprs_bprs_suic', 'chrbprs_bprs_guil'], 1, voi_4, all_visits_list, 'int')
bprs2 = create_total_division('chrbprs_positive_symptom_subscale', df_all, df_all, ['chrbprs_bprs_unus', 'chrbprs_bprs_hall', 'chrbprs_bprs_susp'], 1, voi_4, all_visits_list, 'int')
bprs3 = create_total_division('chrbprs_negative_symptom_subscale', df_all, df_all, ['chrbprs_bprs_blun', 'chrbprs_bprs_motr', 'chrbprs_bprs_emot'], 1, voi_4, all_visits_list, 'int')
bprs4 = create_total_division('chrbprs_activation_subscale', df_all, df_all, ['chrbprs_bprs_exci', 'chrbprs_bprs_mohy', 'chrbprs_bprs_elat'], 1, voi_4, all_visits_list, 'int')
bprs5 = create_total_division('chrbprs_disorganization_subscale', df_all, df_all, ['chrbprs_bprs_diso', 'chrbprs_bprs_conc', 'chrbprs_bprs_self'], 1, voi_4, all_visits_list, 'int')
bprs = pd.concat([bprs0,bprs1, bprs2, bprs3, bprs4, bprs5], axis = 0)
bprs['data_type'] = 'Integer'
# --------------------------------------------------------------------#
# GF-R
# --------------------------------------------------------------------#
gfr = create_decline('chrgfrs_decline_in_global_role_functioning', df_all, df_all, ['chrgfr_gf_role_high', 'chrgfr_gf_role_scole'], voi_2, all_visits_list, 'int')
gfr['data_type'] = 'Integer'
# --------------------------------------------------------------------#
# GF- S
# --------------------------------------------------------------------#
gfs = create_decline('chrgfss_decline_in_global_social_functioning', df_all, df_all, ['chrgfs_gf_social_high', 'chrgfs_gf_social_scale'], voi_2, all_visits_list, 'int')
gfs['data_type'] = 'Integer'
# --------------------------------------------------------------------#
# Pubertal Developmental Scale
# --------------------------------------------------------------------#
if age > 18:
pds_female = create_condition_value('chrpds_total_score_female_sex', df_all, df_all, voi_2, all_visits_list, 'int', -300)
pds_male = create_condition_value('chrpds_total_score_male_sex', df_all, df_all, voi_2, all_visits_list, 'int', -300)
elif age < 19 and sex == 'female':
pds_female = create_total_division('chrpds_total_score_female_sex',df_all,df_all,['chrpds_pds_1_p','chrpds_pds_2_p','chrpds_pds_3_p','chrpds_pds_f4_p','chrpds_pds_f5b_p'],1, voi_2, all_visits_list, 'int')
pds_male = create_condition_value('chrpds_total_score_male_sex', df_all, df_all, voi_2, all_visits_list, 'int', -300)
elif age < 19 and sex == 'male':
pds_male = create_total_division('chrpds_total_score_male_sex',df_all,df_all,['chrpds_pds_1_p', 'chrpds_pds_2_p', 'chrpds_pds_3_p', 'chrpds_pds_m4_p', 'chrpds_pds_m5_p'], 1, voi_2, all_visits_list, 'int')
pds_female = create_condition_value('chrpds_total_score_female_sex', df_all, df_all, voi_2, all_visits_list, 'int', -300)
elif sex != 'female' and sex != 'male':
print("sex is unknown")
pds_female = create_condition_value('chrpds_total_score_female_sex', df_all, df_all, voi_2, all_visits_list, 'int', -900)
pds_male = create_condition_value('chrpds_total_score_male_sex', df_all, df_all, voi_2, all_visits_list, 'int', -900)
elif np.isnan(age) and sex == 'female':
print("age is unknown")
pds_female = create_total_division('chrpds_total_score_female_sex',df_all,df_all,['chrpds_pds_1_p','chrpds_pds_2_p','chrpds_pds_3_p','chrpds_pds_f4_p','chrpds_pds_f5b_p'],1, voi_2, all_visits_list, 'int')
pds_male = create_condition_value('chrpds_total_score_male_sex', df_all, df_all, voi_2, all_visits_list, 'int', -300)
elif np.isnan(age) and sex == 'male':
print("age is unknown")
pds_female = create_condition_value('chrpds_total_score_female_sex', df_all, df_all, voi_2, all_visits_list, 'int', -300)
pds_male = create_total_division('chrpds_total_score_male_sex',df_all,df_all,['chrpds_pds_1_p', 'chrpds_pds_2_p', 'chrpds_pds_3_p', 'chrpds_pds_m4_p', 'chrpds_pds_m5_p'], 1, voi_2, all_visits_list, 'int')
# menarche
if age > 18 or sex == 'male':
pds_menarche = create_condition_value('chrpds_pds_f5b_p', df_all, df_all, voi_2, all_visits_list, 'int', -300)
elif age < 19 and sex == 'female':
pds_menarche = create_use_value('chrpds_pds_f5b_p', df_all, df_all, ['chrpds_pds_f5b_p'], voi_2, all_visits_list, 'int')
else:
pds_menarche = create_condition_value('chrpds_pds_f5b_p', df_all, df_all, voi_2, all_visits_list, 'int', -900)
pds_final = pd.concat([pds_female, pds_male, pds_menarche], axis = 0)
pds_final['data_type'] = 'Integer'
# --------------------------------------------------------------------#
# SOFAS: Screening
# --------------------------------------------------------------------#
sofas_1 = create_use_value('chrsofas_premorbid', df_all, df_all, ['chrsofas_premorbid'], voi_6, all_visits_list, 'int')
sofas_2 = create_use_value('chrsofas_currscore12mo', df_all, df_all, ['chrsofas_currscore12mo'], voi_6, all_visits_list, 'int')
sofas_3 = create_use_value('chrsofas_currscore', df_all, df_all, ['chrsofas_currscore'], voi_6, all_visits_list, 'int')
sofas_4 = create_use_value('chrsofas_lowscore', df_all, df_all, ['chrsofas_lowscore'], voi_6, all_visits_list, 'int')
sofas_screening = pd.concat([sofas_1, sofas_2, sofas_3, sofas_4], axis = 0)
sofas_screening['data_type'] = 'Integer'
# --------------------------------------------------------------------#
# SOFAS
# --------------------------------------------------------------------#
sofas_5 = create_use_value('chrsofas_currscore_fu', df_all, df_all, ['chrsofas_currscore_fu'], voi_8, all_visits_list, 'int')
sofas_6 = create_use_value('chrsofas_currscore12mo_fu', df_all, df_all, ['chrsofas_currscore12mo_fu'], voi_8, all_visits_list, 'int')
sofas_fu = pd.concat([sofas_5, sofas_6], axis = 0)
sofas_fu['data_type'] = 'Integer'
# --------------------------------------------------------------------#
# NSI-PR
# --------------------------------------------------------------------#
nsipr_1 = create_total_division('chrnsipr_motivation_and_pleasure_dimension', df_all, df_all, ['chrnsipr_item1_rating', 'chrnsipr_item2_rating', 'chrnsipr_item3_rating', 'chrnsipr_item4_rating',\
'chrnsipr_item5_rating', 'chrnsipr_item6_rating', 'chrnsipr_item7_rating'], 7, voi_1, all_visits_list, 'float')
nsipr_2 = create_total_division('chrnsipr_diminished_expression_dimension', df_all, df_all, ['chrnsipr_item8_rating', 'chrnsipr_item9_rating', 'chrnsipr_item10_rating',\
'chrnsipr_item11_rating'], 4, voi_1, all_visits_list, 'float')
nsipr_3 = create_total_division('chrnsipr_avolition_domain', df_all, df_all, ['chrnsipr_item1_rating', 'chrnsipr_item2_rating'], 2, voi_1, all_visits_list, 'float')
nsipr_4 = create_total_division('chrnsipr_asociality_domain', df_all, df_all, ['chrnsipr_item3_rating', 'chrnsipr_item4_rating', 'chrnsipr_item5_rating'], 3, voi_1, all_visits_list, 'float')
nsipr_5 = create_total_division('chrnsipr_anhedonia_domain', df_all, df_all, ['chrnsipr_item6_rating', 'chrnsipr_item7_rating'], 2, voi_1, all_visits_list, 'float')
nsipr_6 = create_total_division('chrnsipr_blunted_affect_domain', df_all, df_all, ['chrnsipr_item8_rating', 'chrnsipr_item9_rating', 'chrnsipr_item10_rating'], 3, voi_1, all_visits_list, 'float')
nsipr_7 = create_use_value('chrnsipr_item11_rating', df_all, df_all, ['chrnsipr_item11_rating'], voi_1, all_visits_list, 'float')
nsipr = pd.concat([nsipr_1, nsipr_2, nsipr_3, nsipr_4, nsipr_5, nsipr_6, nsipr_7], axis = 0)
nsipr['data_type'] = 'Float'
# --------------------------------------------------------------------#
# Promis
# --------------------------------------------------------------------#
promis_df = df_all
promis_df['chrpromis_sleep20'] = 6 - promis_df['chrpromis_sleep20'].astype(float)
promis_df['chrpromis_sleep44'] = 6 - promis_df['chrpromis_sleep44'].astype(float)
promis_df['chrpromise_sleep108'] = 6 - promis_df['chrpromise_sleep108'].astype(float)
promis_df['chrpromis_sleep72'] = 6 - promis_df['chrpromis_sleep72'].astype(float)
promis_df['chrpromis_sleep67'] = 6 - promis_df['chrpromis_sleep67'].astype(float)
promis = create_total_division('chrpromis_total', promis_df, df_all, ['chrpromis_sleep109','chrpromis_sleep116','chrpromis_sleep20','chrpromis_sleep44','chrpromise_sleep108','chrpromis_sleep72',\
'chrpromis_sleep67','chrpromis_sleep115'], 1, voi_7, all_visits_list, 'int')
promis['data_type'] = 'Integer'
# --------------------------------------------------------------------#
# PGI_S
# --------------------------------------------------------------------#
pgi_s = create_use_value('chrpgi_2', df_all, df_all, ['chrpgi_2'], voi_1, all_visits_list, 'int')
pgi_s['data_type'] = 'Integer'
# --------------------------------------------------------------------#
# RA-prediction
# --------------------------------------------------------------------#
ra = create_use_value('chrpred_transition', df_all, df_all, ['chrpred_transition'], voi_2, all_visits_list, 'int')
ra_2 = create_use_value('chrpred_experience', df_all, df_all, ['chrpred_experience'], voi_2, all_visits_list, 'int')
ra = pd.concat([ra, ra_2], axis = 0)
ra['data_type'] = 'Integer'
# --------------------------------------------------------------------#
# CSSRS-baseline
# --------------------------------------------------------------------#
if group == 'chr':
df_pps = df_all[df_all['redcap_event_name'].str.contains('baseline_arm_1')]
elif group == 'hc':
df_pps = df_all[df_all['redcap_event_name'].str.contains('baseline_arm_2')]
cssrs_sil_sum = df_pps[['chrcssrsb_si1l', 'chrcssrsb_si2l']].fillna(-900).astype(int).sum(axis = 1).to_numpy(dtype=int)
cssrs_sim_sum = df_pps[['chrcssrsb_css_sim1', 'chrcssrsb_css_sim2']].fillna(-900).astype(int).sum(axis = 1).to_numpy(dtype=int)
# In the week from April, 16th - April 22nd we have decided (Sylvain and Cheryl) to give a non-applicable instead of 0 if individuals never had any suicidal ideation
if cssrs_sil_sum == 4:
cssrs1 = create_condition_value('chrcssrs_intensity_lifetime', df_all, df_all, voi_2, all_visits_list, 'int', -300)
else:
cssrs1 = create_total_division('chrcssrs_intensity_lifetime' , df_all, df_all, ['chrcssrsb_sidfrql','chrcssrsb_siddurl','chrcssrsb_sidctrl','chrcssrsb_siddtrl','chrcssrsb_sidrsnl'],\
1, voi_2, all_visits_list, 'int')
if cssrs_sil_sum == 4 or cssrs_sim_sum == 4:
cssrs2 = create_condition_value('chrcssrs_intensity_pastmonth', df_all, df_all, voi_2, all_visits_list, 'int', -300)
else:
cssrs2 = create_total_division('chrcssrs_intensity_pastmonth', df_all, df_all, ['chrcssrsb_css_sipmfreq','chrcssrsb_css_sipmdur','chrcssrsb_css_sipmctrl','chrcssrsb_css_sipmdet','chrcssrsb_css_sipmreas'],\
1, voi_2, all_visits_list, 'int')
cssrs = pd.concat([cssrs1, cssrs2], axis = 0)
cssrs['data_type'] = 'Integer'
# --------------------------------------------------------------------#
# Premorbid adjustment scale
# --------------------------------------------------------------------#
pas_child1 = create_total_division('chrpas_childhood_subtotal' , df_all, df_all, ['chrpas_pmod_child1','chrpas_pmod_child2','chrpas_pmod_child3','chrpas_pmod_child4'], 24, voi_3, all_visits_list, 'float')
pas_earlyadol = create_total_division('chrpas_early_adolescence_subtotal' , df_all, df_all, ['chrpas_pmod_adol_early1','chrpas_pmod_adol_early2','chrpas_pmod_adol_early3','chrpas_pmod_adol_early4',\
'chrpas_pmod_adol_early5'], 30, voi_3, all_visits_list, 'float')
pas_lateadol = create_total_division('chrpas_late_adolescence_subtotal' , df_all, df_all, ['chrpas_pmod_adol_late1','chrpas_pmod_adol_late2','chrpas_pmod_adol_late3','chrpas_pmod_adol_late4',\
'chrpas_pmod_adol_late5'], 30, voi_3, all_visits_list, 'float')
# for pas-adult the value for N/A can be 9. This does not fit our coding of missing/applicable. Therefore we change it here.
if (married_1 == -900 or married_1 == -9 or married_1 == -3) and (married_2 == -900 or married_2 == -9 or married_2 == -3):
pas_adult = create_total_division('chrpas_adulthood_subtotal' , df_all, df_all, ['chrpas_pmod_adult1','chrpas_pmod_adult2','chrpas_pmod_adult3v1'], 18, voi_3, all_visits_list, 'float')
elif married_2 == -900 or married_2 == -9 or married_2 == -3:
pas_adult = create_total_division('chrpas_adulthood_subtotal' , df_all, df_all, ['chrpas_pmod_adult1','chrpas_pmod_adult2','chrpas_pmod_adult3v1'], 18, voi_3, all_visits_list, 'float')
elif married_1 == -900 or married_1 == -9 or married_1 == -3:
pas_adult = create_total_division('chrpas_adulthood_subtotal' , df_all, df_all, ['chrpas_pmod_adult1','chrpas_pmod_adult2','chrpas_pmod_adult3v3'], 18, voi_3, all_visits_list, 'float')
else:
print("Something odd is going on with the married variable")
pas_child_merge=pas_child1.copy()
pas_earlyadol_merge=pas_earlyadol.copy()
pas_lateadol_merge=pas_lateadol.copy()
pas_adult_merge=pas_adult.copy()
pas_child_merge['value_child']=pas_child_merge['value']
pas_earlyadol_merge['value_early']=pas_earlyadol_merge['value']
pas_lateadol_merge['value_late']=pas_lateadol_merge['value']
pas_adult_merge['value_adult']=pas_adult_merge['value']
pas_child_early = pd.merge(pas_child_merge, pas_earlyadol_merge, on = 'redcap_event_name')
pas_child_early_late = pd.merge(pd.merge(pas_child_merge, pas_earlyadol_merge, on = 'redcap_event_name'), pas_lateadol_merge, on = 'redcap_event_name')
pas_child_early_late_adult = pd.merge(pd.merge(pd.merge(pas_child_merge, pas_earlyadol_merge, on = 'redcap_event_name'), pas_lateadol_merge, on = 'redcap_event_name'),\
pas_adult_merge, on = 'redcap_event_name')
pas_child_total=create_total_division('chrpas_total_score_only_childhood',df_all,df_all,['chrpas_pmod_child1','chrpas_pmod_child2','chrpas_pmod_child3','chrpas_pmod_child4'],24, voi_3, all_visits_list, 'float')
pas_total_upto_early = create_total_division('chrpas_total_score_upto_early_adolescence', pas_child_early, df_all, ['value_child','value_early'], 2, voi_3, all_visits_list, 'float')
pas_total_upto_late = create_total_division('chrpas_total_score_upto_late_adolescence', pas_child_early_late, df_all, ['value_child','value_early', 'value_late'], 3, voi_3, all_visits_list, 'float')
pas_total_upto_adult = create_total_division('chrpas_total_score_upto_adulthood', pas_child_early_late_adult, df_all, ['value_child','value_early','value_late','value_adult'],4,voi_3, all_visits_list, 'float')
premorbid_adjustment = pd.concat([pas_child1, pas_earlyadol, pas_lateadol, pas_adult, pas_child_total, pas_total_upto_early, pas_total_upto_late, pas_total_upto_adult], axis = 0)
premorbid_adjustment['data_type'] = 'Float'
# --------------------------------------------------------------------#
# ASSIST
# --------------------------------------------------------------------#
tobacco = create_assist('chrassist_tobacco', df_all, df_all, 'chrassist_whoassist_use1', 'chrassist_whoassist_often1',\
['chrassist_whoassist_often1', 'chrassist_whoassist_urge1','chrassist_whoassist_prob1','chrassist_whoassist_fail1',\
'chrassist_whoassist_concern1','chrassist_whoassist_control1'], 1, voi_7, all_visits_list, 'int')
alcohol = create_assist('chrassist_alcohol', df_all, df_all, 'chrassist_whoassist_use2', 'chrassist_whoassist_often2',\
['chrassist_whoassist_often2', 'chrassist_whoassist_urge2','chrassist_whoassist_prob2','chrassist_whoassist_fail2',\
'chrassist_whoassist_concern2','chrassist_whoassist_control2'], 1, voi_7, all_visits_list, 'int')
cannabis=create_assist('chrassist_cannabis', df_all, df_all, 'chrassist_whoassist_use3', 'chrassist_whoassist_often3',\
['chrassist_whoassist_often3', 'chrassist_whoassist_urge3','chrassist_whoassist_prob3','chrassist_whoassist_fail3',\
'chrassist_whoassist_concern3','chrassist_whoassist_control3'], 1, voi_7, all_visits_list, 'int')
cocaine = create_assist('chrassist_cocaine', df_all, df_all, 'chrassist_whoassist_use4', 'chrassist_whoassist_often4',\
['chrassist_whoassist_often4', 'chrassist_whoassist_urge4','chrassist_whoassist_prob4','chrassist_whoassist_fail4',\
'chrassist_whoassist_concern4','chrassist_whoassist_control4'], 1, voi_7, all_visits_list, 'int')
amphetamines=create_assist('chrassist_amphetamines',df_all, df_all, 'chrassist_whoassist_use5', 'chrassist_whoassist_often5',\
['chrassist_whoassist_often5', 'chrassist_whoassist_urge5','chrassist_whoassist_prob5','chrassist_whoassist_fail5',\
'chrassist_whoassist_concern5','chrassist_whoassist_control5'], 1, voi_7, all_visits_list, 'int')
inhalants = create_assist('chrassist_inhalants', df_all, df_all, 'chrassist_whoassist_use6', 'chrassist_whoassist_often6',\
['chrassist_whoassist_often6', 'chrassist_whoassist_urge6','chrassist_whoassist_prob6','chrassist_whoassist_fail6',\
'chrassist_whoassist_concern6','chrassist_whoassist_control6'], 1, voi_7, all_visits_list, 'int')
sedatives = create_assist('chrassist_sedatives', df_all, df_all, 'chrassist_whoassist_use7', 'chrassist_whoassist_often7',\
['chrassist_whoassist_often7', 'chrassist_whoassist_urge7','chrassist_whoassist_prob7','chrassist_whoassist_fail7',\
'chrassist_whoassist_concern7','chrassist_whoassist_control7'], 1, voi_7, all_visits_list, 'int')
hallucinogens=create_assist('chrassist_hallucinogens',df_all,df_all, 'chrassist_whoassist_use8', 'chrassist_whoassist_often8',\
['chrassist_whoassist_often8', 'chrassist_whoassist_urge8','chrassist_whoassist_prob8','chrassist_whoassist_fail8',\
'chrassist_whoassist_concern8','chrassist_whoassist_control8'], 1, voi_7, all_visits_list, 'int')
opiods = create_assist('chrassist_opiods', df_all, df_all, 'chrassist_whoassist_use9', 'chrassist_whoassist_often9',\
['chrassist_whoassist_often9', 'chrassist_whoassist_urge9','chrassist_whoassist_prob9','chrassist_whoassist_fail9',\
'chrassist_whoassist_concern9','chrassist_whoassist_control9'], 1, voi_7, all_visits_list, 'int')
other = create_assist('chrassist_other', df_all, df_all, 'chrassist_whoassist_use10', 'chrassist_whoassist_often10',\
['chrassist_whoassist_often10', 'chrassist_whoassist_urge10','chrassist_whoassist_prob10','chrassist_whoassist_fail10',\
'chrassist_whoassist_concern10','chrassist_whoassist_control10'], 1, voi_7, all_visits_list, 'int')
assist = pd.concat([tobacco, alcohol, cannabis, cocaine, amphetamines, inhalants, sedatives, hallucinogens, opiods, other], axis = 0)
assist['data_type'] = 'Integer'
# --------------------------------------------------------------------#
# Polyenvironmental risk factor
# --------------------------------------------------------------------#
if group == 'chr':
df_figs = df_all[df_all['redcap_event_name'].str.contains('screening_arm_1')]
elif group == 'hc':
df_figs = df_all[df_all['redcap_event_name'].str.contains('screening_arm_2')]
# pps based on age and gender:
if age > 24 and age < 36 and sex == 'male':
chrpps_sum1 = create_condition_value('chrpps_sum1', df_all, df_all, voi_2, all_visits_list, 'float', 2)
elif (age > 0 and age < 25) or age > 36 or sex == 'female':
chrpps_sum1 = create_condition_value('chrpps_sum1', df_all, df_all, voi_2, all_visits_list, 'float', 0)
elif age == -900 or -9 or sex == 'unknown':
chrpps_sum1 = create_condition_value('chrpps_sum1', df_all, df_all, voi_2, all_visits_list, 'float', -900)
elif age == -300 or -3:
chrpps_sum1 = create_condition_value('chrpps_sum1', df_all, df_all, voi_2, all_visits_list, 'float', -300)
else:
chrpps_sum1 = create_condition_value('chrpps_sum1', df_all, df_all, voi_2, all_visits_list, 'float', -900)
# pps 2 handedness
total_handedness_df = create_total_division('hand', df_all, df_all, ['chrpps_writing','chrpps_throwing','chrpps_toothbrush','chrpps_spoon'],1, voi_2, all_visits_list, 'float')
if group == 'chr':
total_handedness_df = total_handedness_df[total_handedness_df['redcap_event_name'].str.contains('baseline_arm_1')]
total_handedness = total_handedness_df['value'].to_numpy(dtype=float)
elif group == 'hc':
total_handedness_df = total_handedness_df[total_handedness_df['redcap_event_name'].str.contains('baseline_arm_2')]
total_handedness = total_handedness_df['value'].to_numpy(dtype=float)
if total_handedness > 0 and total_handedness < 16:
chrpps_sum2 = create_condition_value('chrpps_sum2', df_all, df_all, voi_2, all_visits_list, 'float', 2)
elif total_handedness == -300:
chrpps_sum2 = create_condition_value('chrpps_sum2', df_all, df_all, voi_2, all_visits_list, 'float', -300)
elif total_handedness == -900:
chrpps_sum2 = create_condition_value('chrpps_sum2', df_all, df_all, voi_2, all_visits_list, 'float', -900)
else:
chrpps_sum2 = create_condition_value('chrpps_sum2', df_all, df_all, voi_2, all_visits_list, 'float', 0)
# pps 7 paternal age
#paternal_age_date = df_pps['chrpps_fdobpii'].astype(str).str.contains('1903-03-03')
#print(list(df_all.filter(like='fdob').columns))
# I have changed the paternal age calculation slightly because of newly introduced missing codes in date format
df_pps['chrpps_fage'] = np.where(df_pps['chrpps_fage'] == '1909-09-09', -900,df_pps['chrpps_fage'])
paternal_age = df_pps['chrpps_fage'].fillna(-900).to_numpy(dtype=float)
paternal_age_calc = paternal_age - age
if paternal_age == -900 or paternal_age == -9 or age == -900:
chrpps_sum7 = create_condition_value('chrpps_sum7', df_all, df_all, voi_2, all_visits_list, 'float', -900)
elif paternal_age == -300 or paternal_age == -3 or age == -300:
chrpps_sum7 = create_condition_value('chrpps_sum7', df_all, df_all, voi_2, all_visits_list, 'float', -300)
elif paternal_age_calc > 45:
chrpps_sum7 = create_condition_value('chrpps_sum7', df_all, df_all, voi_2, all_visits_list, 'float', 3.5)
elif paternal_age_calc > 35:
chrpps_sum7 = create_condition_value('chrpps_sum7', df_all, df_all, voi_2, all_visits_list, 'float', 0.5)
else:
chrpps_sum7 = create_condition_value('chrpps_sum7', df_all, df_all, voi_2, all_visits_list, 'float', -0.5)
# pps 8 SES
df_pps['chrpps_focc'] = np.where(df_pps['chrpps_focc'] == '1909-09-09', -900, df_pps['chrpps_focc'])
focc = df_pps['chrpps_focc'].fillna(-900).to_numpy(dtype=float)
if focc == -900 or focc == -9:
chrpps_sum8 = create_condition_value('chrpps_sum8', df_all, df_all, voi_2, all_visits_list, 'float', -900)
elif focc == -300 or focc == -3:
chrpps_sum8 = create_condition_value('chrpps_sum8', df_all, df_all, voi_2, all_visits_list, 'float', -300)
elif focc > 6:
chrpps_sum8 = create_condition_value('chrpps_sum8', df_all, df_all, voi_2, all_visits_list, 'float', 1)
else:
chrpps_sum8 = create_condition_value('chrpps_sum8', df_all, df_all, voi_2, all_visits_list, 'float', 0)
# pps 9 family history of disorders
mother_ddx = df_figs['chrfigs_mother_ddx'].fillna(-900).to_numpy(dtype=float)
mother_mdx = df_figs['chrfigs_mother_mdx'].fillna(-900).to_numpy(dtype=float)
mother_pdx = df_figs['chrfigs_mother_pdx'].fillna(-900).to_numpy(dtype=float)
mother_napdx = df_figs['chrfigs_mother_napdx'].fillna(-900).to_numpy(dtype=float)
mother_apdx = df_figs['chrfigs_mother_apdx'].fillna(-900).to_numpy(dtype=float)
father_ddx = df_figs['chrfigs_father_ddx'].fillna(-900).to_numpy(dtype=float)
father_mdx = df_figs['chrfigs_father_mdx'].fillna(-900).to_numpy(dtype=float)
father_pdx = df_figs['chrfigs_father_pdx'].fillna(-900).to_numpy(dtype=float)
father_napdx = df_figs['chrfigs_father_napdx'].fillna(-900).to_numpy(dtype=float)
father_apdx = df_figs['chrfigs_father_apdx'].fillna(-900).to_numpy(dtype=float)
if mother_ddx == 3 or mother_mdx == 3 or mother_pdx == 3 or mother_napdx == 3 or mother_apdx == 3 or\
father_ddx == 3 or father_mdx == 3 or father_pdx == 3 or father_napdx == 3 or father_apdx == 3:
chrpps_sum9 = create_condition_value('chrpps_sum9', df_all, df_all, voi_2, all_visits_list, 'float', 5.5)
elif (mother_ddx == 2 or mother_ddx == 1 or mother_ddx == 0) and (mother_mdx == 2 or mother_mdx == 1 or mother_mdx == 0) and\
(mother_pdx == 2 or mother_pdx == 1 or mother_pdx == 0) and (mother_apdx == 2 or mother_apdx == 1 or mother_apdx == 0) and (mother_napdx == 2 or mother_napdx == 1 or mother_napdx == 0) and\
(father_ddx == 2 or father_ddx == 1 or father_ddx == 0) and (father_mdx == 2 or father_mdx == 1 or father_mdx == 0) and\
(father_pdx == 2 or father_pdx == 1 or father_pdx == 0) and (father_apdx == 2 or father_apdx == 1 or father_apdx == 0) and (father_napdx == 2 or father_napdx == 1 or father_napdx == 0):
chrpps_sum9 = create_condition_value('chrpps_sum9', df_all, df_all, voi_2, all_visits_list, 'float', -2)
if mother_ddx == -300 or mother_mdx == -300 or mother_pdx == -300 or mother_napdx == -300 or mother_apdx == -300 or\
father_ddx == -300 or father_mdx == -300 or father_pdx == -300 or father_napdx == -300 or father_apdx == -300 or\
mother_ddx == -3 or mother_mdx == -3 or mother_pdx == -3 or mother_napdx == -3 or mother_apdx == -3 or\
father_ddx == -3 or father_mdx == -3 or father_pdx == -3 or father_napdx == -3 or father_apdx == -3:
chrpps_sum9 = create_condition_value('chrpps_sum9', df_all, df_all, voi_2, all_visits_list, 'float', -300)
if mother_ddx == -900 or mother_mdx == -900 or mother_pdx == -900 or mother_napdx == -900 or mother_apdx == -900 or\
father_ddx == -900 or father_mdx == -900 or father_pdx == -900 or father_napdx == -900 or father_apdx == -900 or\
mother_ddx == -9 or mother_mdx == -9 or mother_pdx == -9 or mother_napdx == -9 or mother_apdx == -9 or\
father_ddx == -9 or father_mdx == -9 or father_pdx == -9 or father_napdx == -9 or father_apdx == -9 or\
mother_ddx == 9 or mother_mdx == 9 or mother_pdx == 9 or mother_napdx == 9 or mother_apdx == 9 or\
father_ddx == 9 or father_mdx == 9 or father_pdx == 9 or father_napdx == 9 or father_apdx == 9:
chrpps_sum9 = create_condition_value('chrpps_sum9', df_all, df_all, voi_2, all_visits_list, 'float', -900)
# pps 10 life event
sixmo_1 = df_pps['chrpps_sixmo___1'].fillna(-900).to_numpy(dtype=float)
sixmo_2 = df_pps['chrpps_sixmo___2'].fillna(-900).to_numpy(dtype=float)
sixmo_3 = df_pps['chrpps_sixmo___3'].fillna(-900).to_numpy(dtype=float)
sixmo_4 = df_pps['chrpps_sixmo___4'].fillna(-900).to_numpy(dtype=float)
sixmo_5 = df_pps['chrpps_sixmo___5'].fillna(-900).to_numpy(dtype=float)
sixmo_6 = df_pps['chrpps_sixmo___6'].fillna(-900).to_numpy(dtype=float)
sixmo_7 = df_pps['chrpps_sixmo___7'].fillna(-900).to_numpy(dtype=float)
sixmo_8 = df_pps['chrpps_sixmo___8'].fillna(-900).to_numpy(dtype=float)
sixmo_10 = df_pps['chrpps_sixmo___10'].fillna(-900).to_numpy(dtype=float)
sixmo_11 = df_pps['chrpps_sixmo___11'].fillna(-900).to_numpy(dtype=float)
sixmo_12 = df_pps['chrpps_sixmo___12'].fillna(-900).to_numpy(dtype=float)
sixmo_13 = df_pps['chrpps_sixmo___13'].fillna(-900).to_numpy(dtype=float)
sixmo_14 = df_pps['chrpps_sixmo___14'].fillna(-900).to_numpy(dtype=float)
sixmo_15 = df_pps['chrpps_sixmo___15'].fillna(-900).to_numpy(dtype=float)
if sixmo_1 == 1 or sixmo_2 == 1 or sixmo_3 == 1 or sixmo_4 == 1 or sixmo_5 == 1 or sixmo_6 == 1 or sixmo_7 == 1 or sixmo_8 == 1 or sixmo_10 == 1 or sixmo_11 == 1 or sixmo_12 == 1 or\
sixmo_13 == 1 or sixmo_14 == 1:
chrpps_sum10 = create_condition_value('chrpps_sum10', df_all, df_all, voi_2, all_visits_list, 'float', 5.5)
elif sixmo_1 == 0 and sixmo_2 == 0 and sixmo_3 == 0 and sixmo_4 == 0 and sixmo_5 == 0 and sixmo_6 == 0 and sixmo_7 == 0 and sixmo_8 == 0 and sixmo_10 == 0 and sixmo_11 == 0 and sixmo_12 == 0 and\
sixmo_13 == 0 and sixmo_14 == 0 and sixmo_15 == 1:
chrpps_sum10 = create_condition_value('chrpps_sum10', df_all, df_all, voi_2, all_visits_list, 'float', -2)
elif sixmo_1 == -900 or sixmo_2 == -900 or sixmo_3 == -900 or sixmo_4 == -900 or sixmo_5 == -900 or sixmo_6 == -900 or sixmo_7 == -900 or sixmo_8 == -900 or\
sixmo_10 == -900 or sixmo_11 == -900 or sixmo_12 == -900 or sixmo_13 == -900 or sixmo_14 == -900:
chrpps_sum10 = create_condition_value('chrpps_sum10', df_all, df_all, voi_2, all_visits_list, 'float', -900)
elif sixmo_1 == -300 or sixmo_2 == -300 or sixmo_3 == -300 or sixmo_4 == -300 or sixmo_5 == -300 or sixmo_6 == -300 or sixmo_7 == -300 or sixmo_8 == -300 or\
sixmo_10 == -300 or sixmo_11 == -300 or sixmo_12 == -300 or sixmo_13 == -300 or sixmo_14 == -300:
chrpps_sum10 = create_condition_value('chrpps_sum10', df_all, df_all, voi_2, all_visits_list, 'float', -300)
else:
chrpps_sum10 = create_condition_value('chrpps_sum10', df_all, df_all, voi_2, all_visits_list, 'float', -900)
print('not sure what is going on with chrpps_sum10')
# pps 11 tobacco
assist_1 = df_pps['chrassist_whoassist_often1'].fillna(-900).to_numpy(dtype=float)
assist_12 = df_pps['chrassist_whoassist_use1'].fillna(-900).to_numpy(dtype=float)
if assist_1 == 6:
chrpps_sum11 = create_condition_value('chrpps_sum11', df_all, df_all, voi_2, all_visits_list, 'float', 3)
elif (assist_1 == -900 or assist_1 -9) and assist_12 != 0:
chrpps_sum11 = create_condition_value('chrpps_sum11', df_all, df_all, voi_2, all_visits_list, 'float', -900)
elif (assist_1 == -300 or assist_1 == -3) and assist_12 != 0:
chrpps_sum11 = create_condition_value('chrpps_sum11', df_all, df_all, voi_2, all_visits_list, 'float', -300)
else:
chrpps_sum11 = create_condition_value('chrpps_sum11', df_all, df_all, voi_2, all_visits_list, 'float', -0.5)
# pps 12 cannabis
assist_2 = df_pps['chrassist_whoassist_often3'].fillna(-900).to_numpy(dtype=float)
assist_22 = df_pps['chrassist_whoassist_use3'].fillna(-900).to_numpy(dtype=float)
if assist_2 > 3:
chrpps_sum12 = create_condition_value('chrpps_sum12', df_all, df_all, voi_2, all_visits_list, 'float', 7)
elif (assist_2 == -900 or assist_2 == -9) and assist_22 != 0:
chrpps_sum12 = create_condition_value('chrpps_sum12', df_all, df_all, voi_2, all_visits_list, 'float', -900)
elif (assist_2 == -300 or assist_2 == -3) and assist_22 != 0:
chrpps_sum12 = create_condition_value('chrpps_sum12', df_all, df_all, voi_2, all_visits_list, 'float', -300)
else:
chrpps_sum12 = create_condition_value('chrpps_sum12', df_all, df_all, voi_2, all_visits_list, 'float', 0)
# pps 13 Childhood trauma
ctq_df = df_all
ctq_df['chrpps_special'] = 6 - ctq_df['chrpps_special'].astype(float)
ctq_df['chrpps_care'] = 6 - ctq_df['chrpps_care'].astype(float)
ctq_df['chrpps_loved'] = 6 - ctq_df['chrpps_loved'].astype(float)
ctq_df['chrpps_closefam'] = 6 - ctq_df['chrpps_closefam'].astype(float)
ctq_df['chrpps_support'] = 6 - ctq_df['chrpps_support'].astype(float)
ctq_df['chrpps_protect'] = 6 - ctq_df['chrpps_protect'].astype(float)
ctq_df['chrpps_docr'] = 6 - ctq_df['chrpps_docr'].astype(float)
ctq = create_total_division('ctq', ctq_df, df_all, ['chrpps_lazy', 'chrpps_born', 'chrpps_hate', 'chrpps_hurt', 'chrpps_emoab', 'chrpps_doc', 'chrpps_bruise', 'chrpps_belt',\
'chrpps_physab', 'chrpps_beat', 'chrpps_touch', 'chrpps_threat', 'chrpps_sexual', 'chrpps_molest', 'chrpps_sexab', 'chrpps_loved',\
'chrpps_special', 'chrpps_care', 'chrpps_closefam', 'chrpps_support', 'chrpps_hunger', 'chrpps_protect', 'chrpps_pardrunk',\
'chrpps_dirty', 'chrpps_docr'], 1, voi_2, all_visits_list, 'float')
if group == 'chr':
ctq = ctq[ctq['redcap_event_name'].str.contains('baseline_arm_1')]
ctq_final_score = ctq['value'].to_numpy(dtype=float)
elif group == 'hc':
ctq = ctq[ctq['redcap_event_name'].str.contains('baseline_arm_2')]
ctq_final_score = ctq['value'].to_numpy(dtype=float)
if ctq_final_score > 55:
chrpps_sum13 = create_condition_value('chrpps_sum13', df_all, df_all, voi_2, all_visits_list, 'float', 4)
elif ctq_final_score > -1 and ctq_final_score < 56:
chrpps_sum13 = create_condition_value('chrpps_sum13', df_all, df_all, voi_2, all_visits_list, 'float', -0.5)
elif (ctq_final_score == -900 or ctq_final_score -9):
chrpps_sum13 = create_condition_value('chrpps_sum13', df_all, df_all, voi_2, all_visits_list, 'float', -900)
elif (ctq_final_score == -300 or ctq_final_score == -3):
chrpps_sum13 = create_condition_value('chrpps_sum13', df_all, df_all, voi_2, all_visits_list, 'float', -300)
else:
prfloat("What is going on with the CTQ")
# pps 14 Trait anhedonia
trait_df = df_all
trait_df['chrpps_restaur'] = 7 - trait_df['chrpps_restaur'].astype(float)
trait = create_total_division('trait', trait_df, df_all, ['chrpps_taste', 'chrpps_restaur', 'chrpps_roller', 'chrpps_holiday', 'chrpps_tasty', 'chrpps_pleasure', 'chrpps_lookfwd',\
'chrpps_menu', 'chrpps_actor', 'chrpps_crackle', 'chrpps_rain', 'chrpps_grass', 'chrpps_air', 'chrpps_coffee', 'chrpps_hair',\
'chrpps_yawn', 'chrpps_snow'], 1, voi_2, all_visits_list, 'float')
if group == 'chr':
trait = trait[trait['redcap_event_name'].str.contains('baseline_arm_1')]
trait_final_score = trait['value'].to_numpy(dtype=float)
elif group == 'hc':
trait = trait[trait['redcap_event_name'].str.contains('baseline_arm_2')]
trait_final_score = trait['value'].to_numpy(dtype=float)
if trait_final_score < 36 and trait_final_score > -1:
chrpps_sum14 = create_condition_value('chrpps_sum14', df_all, df_all, voi_2, all_visits_list, 'float', 6.5)
elif trait_final_score > 35:
chrpps_sum14 = create_condition_value('chrpps_sum14', df_all, df_all, voi_2, all_visits_list, 'float', 0)