# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Construct a DF that contains basically everything

# +
import sys
if '..' not in sys.path: sys.path.append('..')

import pandas as pd
from src import adni
import numpy as np
# -

# %load_ext autoreload
# %autoreload 2

adni1_input_dir = '../data/ADNI1/SPM_preprocessed_normalized'

base_dir = '../data/ADNI1'
adni = adni.Adni(root=os.path.abspath(base_dir) + '/', processed=True)
adni.set_meta('../data/meta')

adni.load(show_output=False)
df = adni.to_df(show_output=False)

df

# ## Add RID and site key

df['RID'] = df.apply(lambda row: int(row['subject.subjectIdentifier'].split('_')[-1]), axis=1)
df['Site'] = df.apply(lambda row: int(row['subject.subjectIdentifier'].split('_')[0]), axis=1)

# ## Add MCI progression

mci_progression_data = pd.read_csv('../data/misc/ADNI1/MCI_progression.csv')


# +
def mci_conversion(row):
    if row['subject.researchGroup'] == 'MCI':
        if row['RID'] in mci_progression_data['RID']:
            return True
        else:
            return False
    else:
        return None

df['MCI_progression'] = df.apply(mci_conversion, axis = 1)
# -

# ## Impute missing MMSE data

mmse_detailed_data = pd.read_csv('../data/misc/MMSE.csv', parse_dates = ['EXAMDATE'], low_memory = False)


def retrieve_MMSE(row):
    if not pd.isna(row['subject.visit.assessment.component.assessmentScore_MMSCORE']):
        return row['subject.visit.assessment.component.assessmentScore_MMSCORE']
    scores = []
    differences = []
    for index, MMSErow in mmse_detailed_data.loc[(mmse_detailed_data['EXAMDATE'].isna() == False) & (mmse_detailed_data['RID'] == row['RID'])].iterrows():
        scores.append(MMSErow['MMSCORE'])
        differences.append(abs((row['subject.study.series.dateAcquired'] - MMSErow['EXAMDATE']).total_seconds()))
    return scores[np.argmin(differences)]


df['Imputed_MMSE'] = df.apply(retrieve_MMSE, axis = 1)



StarcoderdataJupyterScriptsDedupFiltered