<jupyter_start><jupyter_text>Final Project Degree Prediction of cardiovascular and bleeding events in patients with double antiaggregation with machine learning models. <NAME>--- 0_ExploreData  1. **Descriptive of the variables**    1. Categorical variable:        - Percentages.        - Bar plots.    2. Continuos variable:        - Histogram.                  2. **Time**: total times on variables: Months_Death, Months_reAMI, Months_Bleeding y LengthDapt_months.    1. Absolute time    2. Difference from medication time    3. **Survival Analysis**4. **Frequency table**<jupyter_code># Import librarys

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import seaborn as sns
from datetime import date, datetime
from sksurv.nonparametric import kaplan_meier_estimator

pd.options.display.max_columns = None

%pylab inline
# Import data

df = pd.read_excel('../../data/PACS_DAPT_completo.xlsx')<jupyter_output><empty_output><jupyter_text>1. Descriptive of the variables<jupyter_code># df.head()
print("Pacientes on BleeMACS registry: ", df.loc[(df['Registro']=='BLEEMACS'),].shape[0],
      "\nPacientes on RENAMI registry: ", df.loc[(df['Registro']=='RENAMI'),].shape[0],
      "\nPacientes on Vigo_Arritxaca registry: ", df.loc[(df['Registro']=='Vigo_Arritxaca'),].shape[0])
df.columns<jupyter_output><empty_output><jupyter_text>A. Categorical variable<jupyter_code>var_cat = ['Sex', 'Diabetes', 'Hypertension', 'Dyslipemia', 'PeripheralArteryDisease', 'PriorMyocardialInfarction',
           'PriorPCI','PriorCABG', 'PriorStroke', 'Cancer', 'Prior_Bleeding', 'STEMI', 'UnstableAngina',
           'Killip2orMore', 'LVEFless40', 'LVEFgroups', 'Femoral_Access', 'Multivessel', 'DrugElutingStent',
           'NoStent','Revascularization_Complete', 'ReAMI_inhospital', 'Bleeding_inhospital', 'Transfusion_IH',
           'Clopidogrel', 'Ticagrelor', 'Prasugrel', 'OralAnticoagulation', 'Betablockers', 'ACEI_ARB', 'Statin',
           'Death', 'ReInfarction', 'Bleeding', 'Transfusion']<jupyter_output><empty_output><jupyter_text>- Percentaje<jupyter_code>for el in var_cat:
    print(df[el].value_counts(normalize=True) * 100,
          '\n-----------------------------------------------------------\n')
for el in var_cat:
    print(df.groupby('Registro')[el].value_counts(normalize=True) * 100,
          '\n-----------------------------------------------------------\n')<jupyter_output>Registro        Sex   
BLEEMACS        Male      76.724811
                Female    23.275189
RENAMI          Male      79.172320
                Female    20.827680
Vigo_Arritxaca  Male      74.658188
                Female    25.341812
Name: Sex, dtype: float64 
-----------------------------------------------------------

Registro        Diabetes
BLEEMACS        No          75.989326
                Yes         24.010674
RENAMI          No          70.104025
                Yes         29.895975
Vigo_Arritxaca  No          68.871224
                Yes         31.128776
Name: Diabetes, dtype: float64 
-----------------------------------------------------------

Registro        Hypertension
BLEEMACS        Yes             58.682635
                No              41.317365
RENAMI          Yes             54.002714
                No              45.997286
Vigo_Arritxaca  Yes             67.170111
                No              32.829889
Name: Hypertension, dtype: float64 
----------[...]<jupyter_text>- Bar plots<jupyter_code>hue = "Sex"
sns_plot = sns.countplot(x="Registro", hue=hue,data=df)
# sns_plot.figure.savefig(str('BarPlot_' + hue +'.png'))
hue = "Diabetes"
sns_plot = sns.countplot(x="Registro", hue=hue,data=df)
# sns_plot.figure.savefig(str('BarPlot_' + hue +'.png'))
hue = "Hypertension"
sns_plot = sns.countplot(x="Registro", hue=hue,data=df)
# sns_plot.figure.savefig(str('BarPlot_' + hue +'.png'))
hue = "Dyslipemia"
sns_plot = sns.countplot(x="Registro", hue=hue,data=df)
# sns_plot.figure.savefig(str('BarPlot_' + hue +'.png'))
hue = "PeripheralArteryDisease"
sns_plot = sns.countplot(x="Registro", hue=hue,data=df)
# sns_plot.figure.savefig(str('BarPlot_' + hue +'.png'))
hue = "PriorMyocardialInfarction"
sns_plot = sns.countplot(x="Registro", hue=hue,data=df)
# sns_plot.figure.savefig(str('BarPlot_' + hue +'.png'))
hue = "PriorPCI"
sns_plot = sns.countplot(x="Registro", hue=hue,data=df)
# sns_plot.figure.savefig(str('BarPlot_' + hue +'.png'))
hue = "PriorCABG"
sns_plot = sns.countplot(x="Registro", hue=hue,data=df)
# sns_plot.figure.savefig(str('BarPlot_' + hue +'.png'))
hue = "PriorStroke"
sns_plot = sns.countplot(x="Registro", hue=hue,data=df)
# sns_plot.figure.savefig(str('BarPlot_' + hue +'.png'))
hue = "Cancer"
sns_plot = sns.countplot(x="Registro", hue=hue,data=df)
# sns_plot.figure.savefig(str('BarPlot_' + hue +'.png'))
hue = "Prior_Bleeding"
sns_plot = sns.countplot(x="Registro", hue=hue,data=df)
# sns_plot.figure.savefig(str('BarPlot_' + hue +'.png'))
hue = "STEMI"
sns_plot = sns.countplot(x="Registro", hue=hue,data=df)
# sns_plot.figure.savefig(str('BarPlot_' + hue +'.png'))
hue = "UnstableAngina"
sns_plot = sns.countplot(x="Registro", hue=hue,data=df)
# sns_plot.figure.savefig(str('BarPlot_' + hue +'.png'))
hue = "Killip2orMore"
sns_plot = sns.countplot(x="Registro", hue=hue,data=df)
# sns_plot.figure.savefig(str('BarPlot_' + hue +'.png'))
hue = "LVEFless40"
sns_plot = sns.countplot(x="Registro", hue=hue,data=df)
# sns_plot.figure.savefig(str('BarPlot_' + hue +'.png'))
hue = "LVEFgroups"
sns_plot = sns.countplot(x="Registro", hue=hue,data=df)
# sns_plot.figure.savefig(str('BarPlot_' + hue +'.png'))
hue = "Femoral_Access"
sns_plot = sns.countplot(x="Registro", hue=hue,data=df)
# sns_plot.figure.savefig(str('BarPlot_' + hue +'.png'))
hue = "Multivessel"
sns_plot = sns.countplot(x="Registro", hue=hue,data=df)
# sns_plot.figure.savefig(str('BarPlot_' + hue +'.png'))
hue = "DrugElutingStent"
sns_plot = sns.countplot(x="Registro", hue=hue,data=df)
# sns_plot.figure.savefig(str('BarPlot_' + hue +'.png'))
hue = "NoStent"
sns_plot = sns.countplot(x="Registro", hue=hue,data=df)
# sns_plot.figure.savefig(str('BarPlot_' + hue +'.png'))
hue = "Revascularization_Complete"
sns_plot = sns.countplot(x="Registro", hue=hue,data=df)
# sns_plot.figure.savefig(str('BarPlot_' + hue +'.png'))
hue = "ReAMI_inhospital"
sns_plot = sns.countplot(x="Registro", hue=hue,data=df)
# sns_plot.figure.savefig(str('BarPlot_' + hue +'.png'))
hue = "Bleeding_inhospital"
sns_plot = sns.countplot(x="Registro", hue=hue,data=df)
# sns_plot.figure.savefig(str('BarPlot_' + hue +'.png'))
hue = "Transfusion_IH"
sns_plot = sns.countplot(x="Registro", hue=hue,data=df)
# sns_plot.figure.savefig(str('BarPlot_' + hue +'.png'))
hue = "Clopidogrel"
sns_plot = sns.countplot(x="Registro", hue=hue,data=df)
# sns_plot.figure.savefig(str('BarPlot_' + hue +'.png'))
hue = "Ticagrelor"
sns_plot = sns.countplot(x="Registro", hue=hue,data=df)
# sns_plot.figure.savefig(str('BarPlot_' + hue +'.png'))
hue = "Prasugrel"
sns_plot = sns.countplot(x="Registro", hue=hue,data=df)
# sns_plot.figure.savefig(str('BarPlot_' + hue +'.png'))
hue = "OralAnticoagulation"
sns_plot = sns.countplot(x="Registro", hue=hue,data=df)
# sns_plot.figure.savefig(str('BarPlot_' + hue +'.png'))
hue = "Betablockers"
sns_plot = sns.countplot(x="Registro", hue=hue,data=df)
# sns_plot.figure.savefig(str('BarPlot_' + hue +'.png'))
hue = "ACEI_ARB"
sns_plot = sns.countplot(x="Registro", hue=hue,data=df)
# sns_plot.figure.savefig(str('BarPlot_' + hue +'.png'))
hue = "Statin"
sns_plot = sns.countplot(x="Registro", hue=hue,data=df)
# sns_plot.figure.savefig(str('BarPlot_' + hue +'.png'))
hue = "Death"
sns_plot = sns.countplot(x="Registro", hue=hue,data=df)
# sns_plot.figure.savefig(str('BarPlot_' + hue +'.png'))
hue = "ReInfarction"
sns_plot = sns.countplot(x="Registro", hue=hue,data=df)
# sns_plot.figure.savefig(str('BarPlot_' + hue +'.png'))
hue = "Bleeding"
sns_plot = sns.countplot(x="Registro", hue=hue,data=df)
# sns_plot.figure.savefig(str('BarPlot_' + hue +'.png'))
hue = "Transfusion"
sns_plot = sns.countplot(x="Registro", hue=hue,data=df)
# sns_plot.figure.savefig(str('BarPlot_' + hue +'.png'))<jupyter_output><empty_output><jupyter_text>--- B. Continuos variable<jupyter_code>var_cont = ['Year', 'Age', 'Peso', 'Talla', 'Hemoglobin_Admission', 'Creatinine_Admission', 'Months_Death',
            'Months_reAMI', 'Months_Bleeding', 'LengthDapt_months']<jupyter_output><empty_output><jupyter_text>- Histograms<jupyter_code>def histograma(variable, n_bins, registro = ['BLEEMACS', 'RENAMI', 'Vigo_Arritxaca']):
    fig = plt.figure(tight_layout=True, figsize= (10,5))
    gs = gridspec.GridSpec(2, len(registro))
    i =  0
    ax = fig.add_subplot(gs[0,:])
    sns.distplot(df[variable], bins=n_bins, rug=True, kde=False, ax= ax)
    ax.set_title(variable +' -- TOTAL')
    # ax.set_ylabel('YLabel0')
    # ax.set_xlabel('XLabel0')
    for reg in registro:
        ax = fig.add_subplot(gs[1, i])
        sns.distplot(df.loc[df['Registro'] == str(reg), variable], bins=n_bins, rug=True, kde=False, ax= ax)
        ax.set_title(variable +' -- ' +reg)
    #     ax.set_ylabel('YLabel1 %d' % i)
    #     ax.set_xlabel('XLabel1 %d' % i)
        i = i+1
        if i == 0:
            for tick in ax.get_xticklabels():
                tick.set_rotation(55)
    fig.align_labels()  # same as fig.align_xlabels(); fig.align_ylabels()
#     fig.savefig(str('HistPlot_' + variable +'.png'))
histograma('Age',20)
histograma('Peso', 20, ['RENAMI', 'Vigo_Arritxaca'])
histograma('Talla', 20, ['RENAMI', 'Vigo_Arritxaca'])
histograma('Hemoglobin_Admission', 50)
histograma('Creatinine_Admission', 50)
histograma('Months_Death', 100)
histograma('Months_reAMI', 100)
histograma('Months_Bleeding', 100)
histograma('LengthDapt_months', 100)<jupyter_output><empty_output><jupyter_text>------ 2. Time Total times on variables: Months_Death, Months_reAMI, Months_Bleeding y LengthDapt_months.  A. Absolute time<jupyter_code>def tiempos(variable, n_bins1=100, n_bins2=500, xlim = [9,15]):
    fig, (ax1, ax2) = plt.subplots(1 ,2, figsize= (15,5))
    fig.suptitle(variable.name)
    sns.distplot(variable, bins=n_bins1, rug=False, kde=False, ax= ax1)
#     ax1.hist(variable, bins=n_bins1)
    sns.distplot(variable, bins=n_bins2, rug=False, kde=False, ax= ax2)
#     ax2.hist(variable, bins=n_bins2)
    ax2.set_xlim(xlim)
tiempos(df['Months_Death'])
print('Months_Death con muerte = SI')
tiempos(df.loc[df['Death'] == 'Yes', 'Months_Death'], xlim =[0,12])
tiempos(df['Months_reAMI'])
print('Months_reAMI con ReInfarction = SI')
tiempos(df.loc[df['ReInfarction'] == 'Yes', 'Months_reAMI'], xlim =[0,15])
tiempos(df['Months_Bleeding'])
print('Months_Bleeding con Bleeding = SI')
tiempos(df.loc[df['Bleeding'] == 'Yes', 'Months_Bleeding'], xlim =[0,15])
tiempos(df['LengthDapt_months'])<jupyter_output><empty_output><jupyter_text>--- B. Difference from medication time<jupyter_code>def diferencia(save,title, variable, n_bins1=100, n_bins2=1000, xlim = [-0.5,1.5]):
    fig, (ax1, ax2) = plt.subplots(1 ,2, figsize= (15,5))
    fig.suptitle(title)
    sns.distplot(variable, bins=n_bins1, rug=False, kde=False, ax= ax1)
    sns.distplot(variable, bins=n_bins2, rug=False, kde=False, ax= ax2)
    ax2.set_xlim(xlim)
#     fig.savefig(str('HistPlot_' + save +'.png'))
diferencia('difREAMI',
    'Diferencia entre el tiempo de infarto y de medicación en pacientes de Vigo_Arritxaca',
    df.loc[(df['Registro']=='Vigo_Arritxaca') & (df['ReInfarction'] == 'Yes'), 'Months_reAMI'] - df.loc[(df['Registro']=='Vigo_Arritxaca') & (df['ReInfarction'] == 'Yes'), 'LengthDapt_months'])
diferencia('DifBLEEDING',
    'Diferencia entre el tiempo de sangrado y de medicación en pacientes de Vigo_Arritxaca',
    df.loc[(df['Registro']=='Vigo_Arritxaca') & (df['Bleeding'] == 'Yes'), 'Months_Bleeding'] - df.loc[(df['Registro']=='Vigo_Arritxaca') & (df['Bleeding'] == 'Yes'), 'LengthDapt_months'])<jupyter_output><empty_output><jupyter_text>------ 3. Survival Analysis<jupyter_code>#variables que es necesario realizar el cambio

replace_vars = ['ReInfarction','Bleeding', 'Transfusion', 'Death']

#creamos un diccionario con los cambios
dic = {'Yes': np.True_, 'No': np.False_, np.nan:np.nan,'NaN':np.nan, 1:1, 0:0}

#Realizamos el cambio
for i in replace_vars:
    #print(i)
    df[i] = df[i].apply(lambda x:dic[x])
time, survival_prob = kaplan_meier_estimator(df["ReInfarction"], df["Months_reAMI"])
plt.step(time, survival_prob, where="post", label = 'Reinfarto')
# plt.xlim([0,12])
plt.ylabel("est. probability of survival $\hat{S}(t)$")
plt.xlabel("time $t$")
df.loc[(df.Months_reAMI >=90),['Registro','ReInfarction','Months_reAMI']]
time, survival_prob = kaplan_meier_estimator(df.loc[df.Registro == "Vigo_Arritxaca","Bleeding"], df.loc[df.Registro == "Vigo_Arritxaca","Months_Bleeding"])
plt.step(time, survival_prob, where="post")
plt.ylim([.65,1])
plt.ylabel("est. probability of survival $\hat{S}(t)$")
plt.xlabel("time $t$")
df.loc[(df.Bleeding == True)&(df.Months_Bleeding >=70),['Registro','Bleeding','Months_Bleeding']]
time, survival_prob = kaplan_meier_estimator(df["Death"], df["Months_Death"])
plt.step(time, survival_prob, where="post", label = 'Fallecimiento')
plt.ylim([.65,1])
plt.ylabel("est. probability of survival $\hat{S}(t)$")
plt.xlabel("time $t$")
df.loc[(df.Bleeding == True)&(df.Months_Bleeding >=70),['Registro','Death','Months_Death']]
time, survival_prob = kaplan_meier_estimator(df["ReInfarction"], df["Months_reAMI"])
plt.step(time, survival_prob, where="post", label = 'Reinfarto')
time, survival_prob = kaplan_meier_estimator(df["Bleeding"], df["Months_Bleeding"])
plt.step(time, survival_prob, where="post", label = 'Hemorragia')
time, survival_prob = kaplan_meier_estimator(df["Death"], df["Months_Death"])
plt.step(time, survival_prob, where="post", label = 'Fallecimiento')
# plt.xlim([0,24])
plt.ylabel("est. probability of survival $\hat{S}(t)$")
plt.xlabel("time $t$")

plt.legend(loc="lower left", fontsize = 10)
# plt.savefig('supervivencia.png')<jupyter_output><empty_output><jupyter_text>------ 4. Frequency tables  This part will be done after pre-processing the data.<jupyter_code># Import data

df_pre = pd.read_csv('../../data/PACS_DAPT_preprocess.csv')
df_pre = df_pre.drop('Unnamed: 0', axis=1)
Train = df_pre.loc[(df_pre.Registro == 'BLEEMACS')|(df_pre.Registro == 'RENAMI'), ].copy()
Test = df_pre.loc[(df_pre.Registro == 'Vigo_Arritxaca'), ].copy()
print('Pacientes con ReAMI antes de los 12 meses',
     '\n-------------------------------------------------------',
     '\nConjunto de entrenamiento:',
     '\n- Sí:', Train.loc[(Train['reAMIless12Dapt'] == 1) , 'Id'].count(),
     '\n- No:', Train.loc[(Train['reAMIless12Dapt'] == 0) , 'Id'].count(),
     '\n-------------------------------------------------------',
     '\nConjunto de validación:',
     '\n- Sí:', Test.loc[(Test['reAMIless12Dapt'] == 1) , 'Id'].count(),
     '\n- No:', Test.loc[(Test['reAMIless12Dapt'] == 0) , 'Id'].count())
print('Pacientes con ReAMI antes de los 12 meses',
     '\n-------------------------------------------------------',
     '\nConjunto de entrenamiento:',
     '\n', Train['reAMIless12Dapt'].value_counts(normalize=True) * 100,
     '\n-------------------------------------------------------',
     '\nConjunto de validación:',
     '\n', Test['reAMIless12Dapt'].value_counts(normalize=True) * 100)
print('Pacientes con hemorragia antes de los 12 meses',
     '\n-------------------------------------------------------',
     '\nConjunto de entrenamiento:',
     '\n- Sí:', Train.loc[(Train['bleedingless12Dapt'] == 1) , 'Id'].count(),
     '\n- No:', Train.loc[(Train['bleedingless12Dapt'] == 0) , 'Id'].count(),
     '\n-------------------------------------------------------',
     '\nConjunto de validación:',
     '\n- Sí:', Test.loc[(Test['bleedingless12Dapt'] == 1) , 'Id'].count(),
     '\n- No:', Test.loc[(Test['bleedingless12Dapt'] == 0) , 'Id'].count())
print('Pacientes con ReAMI antes de los 12 meses',
     '\n-------------------------------------------------------',
     '\nConjunto de entrenamiento:',
     '\n', Train['bleedingless12Dapt'].value_counts(normalize=True) * 100,
     '\n-------------------------------------------------------',
     '\nConjunto de validación:',
     '\n', Test['bleedingless12Dapt'].value_counts(normalize=True) * 100)<jupyter_output>Pacientes con ReAMI antes de los 12 meses 
------------------------------------------------------- 
Conjunto de entrenamiento: 
 0    97.42242
1     2.57758
Name: bleedingless12Dapt, dtype: float64 
------------------------------------------------------- 
Conjunto de validación: 
 0    98.044515
1     1.955485
Name: bleedingless12Dapt, dtype: float64<jupyter_text>Table on the number of reinfarctions, bleedings and deaths.<jupyter_code>print('Pacientes con ReAMI antes de los 12 meses',
     '\n-------------------------------------------------------',
     '\nConjunto de entrenamiento:',
     '\n- Sí:', df_pre.loc[(df_pre['reAMIless12Dapt'] == 1) , 'Id'].count(),
     '\n- No:', df_pre.loc[(df_pre['reAMIless12Dapt'] == 0) , 'Id'].count(),
     '\n-------------------------------------------------------')
print('Pacientes con ReAMI antes de los 12 meses',
     '\n-------------------------------------------------------',
     '\nConjunto de entrenamiento:',
     '\n', df_pre['reAMIless12Dapt'].value_counts(normalize=True) * 100,
     '\n-------------------------------------------------------')
print('Pacientes con hemorragia antes de los 12 meses',
     '\n-------------------------------------------------------',
     '\nConjunto de entrenamiento:',
     '\n- Sí:', df_pre.loc[(df_pre['bleedingless12Dapt'] == 1) , 'Id'].count(),
     '\n- No:', df_pre.loc[(df_pre['bleedingless12Dapt'] == 0) , 'Id'].count(),
     '\n-------------------------------------------------------')
print('Pacientes con ReAMI antes de los 12 meses',
     '\n-------------------------------------------------------',
     '\nConjunto de entrenamiento:',
     '\n', df_pre['bleedingless12Dapt'].value_counts(normalize=True) * 100,
     '\n-------------------------------------------------------')
print('Pacientes con muerte antes de los 12 meses',
     '\n-------------------------------------------------------',
     '\nConjunto de entrenamiento:',
     '\n- Sí:', df_pre.loc[(df_pre['Death'] == 1) & (df_pre['Months_Death'] <= 12), 'Id'].count(),
     '\n- Porcentaje: ', df_pre.loc[(df_pre['Death'] == 1) & (df_pre['Months_Death'] <= 12), 'Id'].count() * 100 / 26076,
     '\n-------------------------------------------------------')
print('Pacientes con muerte Hemorragia antes de los 12 meses',
     '\n-------------------------------------------------------',
     '\nConjunto de entrenamiento:',
     '\n- Sí:', df_pre.loc[(df_pre['Death'] == 1) & (df_pre['Months_Death'] <= 12) & (df_pre['bleedingless12Dapt'] == 1), 'Id'].count(),
     '\n- Porcentaje: ', df_pre.loc[(df['Death'] == 1) & (df_pre['Months_Death'] <= 12) & (df_pre['bleedingless12Dapt'] == 1), 'Id'].count()*100/633,
     '\n-------------------------------------------------------')
print('Pacientes con muerte Hemorragia antes de los 12 meses',
     '\n-------------------------------------------------------',
     '\nConjunto de entrenamiento:',
     '\n- Sí:', df_pre.loc[(df_pre['Death'] == 1) & (df_pre['Months_Death'] <= 12) & (df_pre['reAMIless12Dapt'] == 1), 'Id'].count(),
     '\n- Porcentaje: ', df_pre.loc[(df_pre['Death'] == 1) & (df_pre['Months_Death'] <= 12) & (df_pre['reAMIless12Dapt'] == 1), 'Id'].count() * 100 / 683,
     '\n-------------------------------------------------------')<jupyter_output>Pacientes con muerte Hemorragia antes de los 12 meses 
------------------------------------------------------- 
Conjunto de entrenamiento: 
- Sí: 154 
- Porcentaje:  22.547584187408493 
-------------------------------------------------------
{'fraction_non_alphanumeric': 0.25320142620398733, 'fraction_numerical': 0.02531009893034701, 'mean_word_length': 3.118717683557394, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '262498', 'license': 'Apache-2.0', 'n_tokens_mistral': 7905, 'n_tokens_neox': 7329, 'n_words': 1275, 'path': 'src/0_ExploreData/0_ExploreDatabase.ipynb', 'repo_name': 'PabloPerSa/TFG', 'size': '19926'}