This repository has been archived by the owner on Sep 13, 2021. It is now read-only.
forked from thejackal-himself/subject_match
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subject_match_class.py
203 lines (180 loc) · 8.85 KB
/
subject_match_class.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
# matches patients and controls according age and gender
#
# cli:
# cwlVersion: v1.0-extended
# class: pythonclass
# baseCommand: result = subject_match_class.SubjectMatch(patients_df,controls_df,max_yeardiff)
#
# inputs:
# patients_df:
# type: pandas.DataFrame
# inputBinding:
# position: 0
# doc: "pandas dataframe with colums ['subjectID','age','gender'], age in years, gender='m','f','o','u'"
# controls_df:
# type: pandas.DataFrame
# inputBinding:
# position: 1
# doc: "pandas dataframe with colums ['subjectID','age','gender'], age in years, gender='m','f','o','u'"
# max_yeardiff:
# type: int?
# inputBinding:
# positions: 2
# doc: "acceptable devation from age in year in both directions, default = 2"
# outputs:
# results.matching:
# type: pandas.DataFrame
# doc: ""pandas dataframe with colums 'patientID', 'age_p', 'gender_p','controlID', 'age_c', 'gender_c']."
#
# s:author:
# - class: s:Person
# s:identifier: https://orcid.org/0000-0002-7238-5339
# s:email: mailto:[email protected]
# s:name: Dagmar Krefting
#
# s:dateCreated: "2019-07-19"
# s:license: https://spdx.org/licenses/Apache-2.0
#
# s:keywords: edam:topic_3063, edam:topic_2082
# doc: 3063: medical informatics, 2082: matrix
# s:programmingLanguage: python3
#
# $namespaces:
# s: https://schema.org/
# edam: http://edamontology.org/
#
# $schemas:
# - https://schema.org/docs/schema_org_rdfa.html
# - http://edamontology.org/EDAM_1.18.owl
#
#%% import libraries
import pandas as pd
import numpy as np
#%% class definition
class SubjectMatch:
def __init__(self, patients, controls, max_yeardiff=2):
self._patients = patients
self._controls = controls
self._max_yeardiff = max_yeardiff
self._matching = None
#%%
@property # a method that is presented to the user like an attribute
def matching(self):
"""return or calculate and return age and gender matching"""
# check if already calculated
if self._matching is not None:
return self._matching
# split into gender-matched subgroups
genders = ['m','f','o']
self._matching = pd.DataFrame(columns=['patientID', 'age_p', 'gender_p','controlID','age_c','gender_c'])
for gender in genders:
patients_gender = self._patients[self._patients['gender'] == gender]
controls_gender = self._controls[self._controls['gender'] == gender]
print(gender)
print(len(patients_gender))
print(len(controls_gender))
if (len(patients_gender) > 0) & (len(controls_gender) > 0):
# call function to calculate matching
self._matching_tmp = self._calc_matching(patients_gender,controls_gender,self._max_yeardiff)
self._matching = self._matching.append(self._matching_tmp)
return self._matching
@staticmethod
def _calc_matching(patients,controls,max_yeardiff,debug=True):
"""find the candidates and choose the one that fits best"""
final_matches = pd.DataFrame(columns=['patientID', 'age_p', 'gender_p','controlID','age_c','gender_c'])
patients_age = np.array(patients['age'])
controls_age = np.array(controls['age'])
# create matrix, this gives you one patient per row, with as many columns as the controls
patients_age_matrix = np.tile(patients_age,(len(controls_age),1)).transpose()
# create matrix with the age differences
age_diff = abs(patients_age_matrix - controls_age)
# mask with max_yeardiff
age_diff_mask = (age_diff <= max_yeardiff).astype(int)
if debug:
print('patient_age_matrix:')
print(patients_age_matrix)
print('age_diff:')
print(age_diff)
print('age_diff_mask:')
print(age_diff_mask)
print('sum(sum(age_diff_mask)): ', end="")
print(sum(sum(age_diff_mask)))
# loop until no matches exist any longer
while sum(sum(age_diff_mask)) > 0:
# sum along the columns (getting an array in the length of the controls
# find the index of the patients, where a patient has only one control that matches
patient_singlematch = np.where(sum(age_diff_mask.transpose()) == 1)
patient_singlematch = patient_singlematch[0]
# debug info
if debug:
print('sum(age_diff_mask.transpose()): ',end="")
print(sum(age_diff_mask.transpose()))
print('patient_singlematch: ', end="")
print(patient_singlematch)
print('len(patient_singlematch): ',end="")
print(len(patient_singlematch))
print('len(final_matches): ', end="")
print(len(final_matches))
# if this list is not empty, set patient
if len(patient_singlematch) > 0:
current_patient = patient_singlematch[0]
next_row = len(final_matches)
# set this patient to the final_matches dataframe, first entry to a new row, the following to the same row
final_matches.loc[next_row,'patientID'] = patients.iloc[current_patient]['subjectID']
final_matches.loc[len(final_matches)-1,'age_p'] = patients.iloc[current_patient]['age']
final_matches.loc[len(final_matches)-1,'gender_p'] = patients.iloc[current_patient]['gender']
# debug info
if debug:
print(patients.iloc[current_patient]['subjectID'])
# find the matching control
print(age_diff_mask[patient_singlematch[0], :])
# get the corresponding control
patient_singlematch_control_idx = np.where(age_diff_mask[patient_singlematch[0],:] == 1)
current_control = patient_singlematch_control_idx[0][0]
#set controls
final_matches.loc[len(final_matches)-1,'controlID'] = controls.iloc[current_control]['subjectID']
final_matches.loc[len(final_matches)-1,'age_c'] = controls.iloc[current_control]['age']
final_matches.loc[len(final_matches)-1,'gender_c'] = controls.iloc[current_control]['gender']
# debug info
if debug:
print('patient_singlematch_control_idx: ', end="")
print(patient_singlematch_control_idx[0])
print('controls.iloc[current_control][subjectID]')
print(controls.iloc[current_control]['subjectID'])
# remove this control from the age_diff_mask and set the age diff to 100 (to never be the minimum
age_diff_mask[:, current_control] = 0
age_diff[:, current_control] = 100
if debug:
print('age_diff_mask after deletion of control')
print(age_diff_mask)
print(age_diff)
# if only multiple matching controls exist
else:
# choose the next patient
patient_match = np.where(sum(age_diff_mask.transpose()) > 1)
patient_match = patient_match[0]
current_patient = patient_match[0]
# set this patient to the final_matches dataframe
final_matches.loc[len(final_matches),'patientID'] = patients.iloc[current_patient]['subjectID']
final_matches.loc[len(final_matches)-1,'age_p'] = patients.iloc[current_patient]['age']
final_matches.loc[len(final_matches)-1,'gender_p'] = patients.iloc[current_patient]['gender']
# debug info
if debug:
print('patient_match: ', end="")
print(patient_match[0])
print(min(age_diff[patient_match[0], :]))
# find the best matching control
selected_patient = age_diff[patient_match[0],:]
patient_match_control_idx = np.where(selected_patient == min(selected_patient))
current_control = patient_match_control_idx[0][0]
# set the selected control to final_matches
final_matches.loc[len(final_matches)-1,'controlID'] = controls.iloc[current_control]['subjectID']
final_matches.loc[len(final_matches)-1,'age_c'] = controls.iloc[current_control]['age']
final_matches.loc[len(final_matches)-1,'gender_c'] = controls.iloc[current_control]['gender']
# remove this control from the age_diff_mask
age_diff_mask[:, current_control] = 0
age_diff_mask[current_patient, :] = 0
age_diff[:, current_control] = 100
if debug:
print(selected_patient)
return final_matches