Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add out-of-sample harmonization for new sites #56

Merged
merged 1 commit into from
Oct 7, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions BrainChart/processes.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,40 @@ def DoSPARE(self,data, ADModel, BrainAgeModel):

def DoHarmonization(self, data, model):
print('Running harmonization.')


data['Sex'] = data['Sex'].map({'M':1,'F':0})
bayes_data, stand_mean = nh.harmonizationApply(data[[x for x in model['ROIs']]].values,
data[['SITE','Age','Sex','DLICV_baseline']],
model,True)

Raw_ROIs_Residuals = data[model['ROIs']].values - stand_mean

# create list of new SITEs to loop through
new_sites = set(data['SITE'].value_counts().index.tolist())^set(model['SITE_labels'])

var_pooled = model['var_pooled']

for site in new_sites:
missing = np.array(data['SITE']==site,dtype=bool)
training = np.array(data['UseForComBatGAMHarmonization'],dtype=bool)
new_site_is_train = np.logical_and(missing, training)

if np.count_nonzero(new_site_is_train)<25:
print('New site `'+site+'` has less than 25 reference data points. Skipping harmonization.')
continue

gamma_hat_site = np.mean(((Raw_ROIs_Residuals[new_site_is_train,:])/np.dot(np.sqrt(var_pooled),np.ones((1,Raw_ROIs_Residuals[new_site_is_train,:].shape[0]))).T),0)
gamma_hat_site = gamma_hat_site[:,np.newaxis]
delta_hat_site = pow(np.std(((Raw_ROIs_Residuals[new_site_is_train,:])/np.dot(np.sqrt(var_pooled),np.ones((1,Raw_ROIs_Residuals[new_site_is_train,:].shape[0]))).T),0),2)
delta_hat_site = delta_hat_site[:,np.newaxis]

bayes_data[missing,:] = ((Raw_ROIs_Residuals[missing,:]/np.dot(np.sqrt(var_pooled),np.ones((1,Raw_ROIs_Residuals[missing,:].shape[0]))).T) - np.dot(gamma_hat_site,np.ones((1,Raw_ROIs_Residuals[missing,:].shape[0]))).T)*np.dot(np.sqrt(var_pooled),np.ones((1,Raw_ROIs_Residuals[missing,:].shape[0]))).T/np.dot(np.sqrt(delta_hat_site),np.ones((1,Raw_ROIs_Residuals[missing,:].shape[0]))).T + stand_mean[missing,:]


if ('H_MUSE_Volume_47' not in data.keys()):
data = pd.concat([data.reset_index(), pd.DataFrame(bayes_data, columns=['H_' + s for s in model['ROIs']])],
axis=1)

axis=1)
start_index = len(model['SITE_labels'])
sex_icv_effect = np.dot(data[['Sex','DLICV_baseline']],model['B_hat'][start_index:(start_index+2),:])
ROIs_ICV_Sex_Residuals = ['RES_ICV_Sex_' + x for x in model['ROIs']]
Expand Down