File size: 6,837 Bytes
53a6def
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
To Do:
- Refactor script to be more readable/smaller main function
"""
import json
import pandas as pd
import numpy as np
from datetime import timedelta


def read_pkl_data(dataset, data_path, path_type):
    """
    Read in pickled dataset
    --------
    :param dataset: type of dataset to read in
    :param data_path: path to generated data
    :param path_type: type of path to read from
    :return: dataframe
    """
    print('Reading in ' + dataset)

    file_path = data_path + dataset
    if path_type == 'data':
        file_path += '_proc.pkl'
    else:
        file_path += '_first_dates.pkl'
    
    return pd.read_pickle(file_path)


def fill_eth_grp_data(df):
    """
    Fill nulls in eth_grp column introduced in joining
    :param df: dataframe to update
    :return: Filled dataframe
    """
    df['eth_grp'] = df.groupby('SafeHavenID').eth_grp.apply(
        lambda x: x.ffill().bfill())
    df['eth_grp'] = df['eth_grp'].fillna('Unknown')

    return df


def fill_to_date_columns(df):
    """
    Fill nulls in to_date columns introduced in joining
    :param df: dataframe to update
    :return: Filled dataframe
    """
    to_date_cols = ['adm_to_date', 'copd_to_date', 'resp_to_date',
                    'presc_to_date', 'rescue_to_date', 'labs_to_date',
                    'anxiety_depression_to_date',
                    'anxiety_depression_presc_to_date']
    df[to_date_cols] = df.groupby('SafeHavenID')[to_date_cols].apply(
        lambda x: x.ffill().fillna(0))

    return df


def fill_yearly_columns(df):
    """
    Fill nulls in yearly columns introduced in joining
    :param df: dataframe to update
    :return: Filled dataframe
    """
    zero_cols = ['adm_per_year', 'total_hosp_days', 'mean_los',
                 'copd_per_year', 'resp_per_year', 'comorb_per_year',
                 'salbutamol_per_year',
                 'saba_inhaler_per_year', 'laba_inhaler_per_year',
                 'lama_inhaler_per_year', 'sama_inhaler_per_year',
                 'ics_inhaler_per_year', 'laba_ics_inhaler_per_year',
                 'lama_laba_ics_inhaler_per_year', 'saba_sama_inhaler_per_year',
                 'mcs_inhaler_per_year', 'rescue_meds_per_year',
                 'presc_per_year', 'labs_per_year',
                 'anxiety_depression_per_year', 'anxiety_depression_presc_per_year']
    df[zero_cols] = df[zero_cols].fillna(0)

    return df


def fill_days_since(df, typ):
    """
    Fill days_since_copd/resp/rescue
    :param df: dataframe to update
    :param typ: type of feature to fill ('copd', 'resp', 'rescue')
    :return: Filled dataframe
    """
    df['days_since_' + typ] = df.eoy - df[typ + '_date'].ffill()

    return df


def process_first_dates(df):
    """
    Process dataframe containing patient's first date in the health board region
    --------
    :param df: dataframe to process
    :return: processed dataframe
    """
    df = df.set_index('SafeHavenID')
    entry_dataset = df.idxmin(axis=1).apply(lambda x: x.split('_')[1])
    first_entry = df.min(axis=1)
    df['entry_dataset'] = entry_dataset
    df['first_entry'] = first_entry
    df_reduced = df[['entry_dataset', 'first_entry']].reset_index()

    return df_reduced


def find_closest_simd(v):
    """
    Find closest SIMD vigintile for each row 'v'
    --------
    :param v: row of data from apply statement
    :param typ: type of simd column to add
    :return: simd value
    """
    simd_years = [2009, 2012, 2016]
    bools = [v.eoy.year >= year for year in simd_years]
    if any(bools):
        simd_year = str(simd_years[np.where(bools)[0][-1]])
        v['simd_quintile'] = v['simd_' + simd_year + '_quintile']
        v['simd_decile'] = v['simd_' + simd_year + '_decile']
        v['simd_vigintile'] = v['simd_' + simd_year + '_vigintile']
    else:
        v['simd_quintile'] = np.nan
        v['simd_decile'] = np.nan
        v['simd_vigintile'] = np.nan

    return v


def main():

    # Load in config items
    with open('../../../config.json') as json_config_file:
        config = json.load(json_config_file)
    data_path = config['model_data_path']

    # Read in data
    adm = read_pkl_data('adm', data_path, 'data')
    comorb = read_pkl_data('comorb', data_path, 'data')
    presc = read_pkl_data('presc', data_path, 'data')
    labs = read_pkl_data('labs', data_path, 'data')
    demo = read_pkl_data('demo', data_path, 'data')

    # Join datasets
    df = adm.join(
        comorb, how='left').join(
        presc, how='outer').join(
        labs, how='outer')
    df = df.reset_index()

    # Fill nulls introduced in joining
    print('Filling data')
    df = fill_eth_grp_data(df)
    df = fill_to_date_columns(df)
    df = fill_yearly_columns(df)

    # Fill days_since columns
    for typ in ['copd', 'resp', 'rescue', 'adm']:
        df = df.groupby('SafeHavenID').apply(fill_days_since, typ)

    # Reduce to single column
    ds_cols = ['days_since_copd', 'days_since_resp']
    df['days_since_copd_resp'] = df[ds_cols].min(axis=1)

    # Read in first date data
    print('Adding first dates')
    adm_dates = read_pkl_data('adm', data_path, 'date')
    presc_dates = read_pkl_data('presc', data_path, 'date')
    labs_dates = read_pkl_data('labs', data_path, 'date')

    # Merge first date data
    first_dates = pd.merge(
        pd.merge(adm_dates, presc_dates, how="outer", on='SafeHavenID'),
        labs_dates, how="outer", on='SafeHavenID')

    # Save first dates if needed
    first_dates.to_pickle(data_path + 'overall_first_dates.pkl')

    # Process first_years
    date_data = process_first_dates(first_dates)

    # Merge first dates data with dataframe
    print('Merging data')
    df_merged = pd.merge(df, date_data, on='SafeHavenID', how='inner')

    # Add years in health board region
    ggc_years = (df_merged.eoy - df_merged.first_entry) / np.timedelta64(1, 'Y')
    df_merged['ggc_years'] = round(ggc_years)

    # Merge demographics
    df_merged = pd.merge(df_merged, demo, on='SafeHavenID')

    # Calculate age relative to end of year
    dt_diff = df_merged.eoy - pd.to_datetime(df_merged.obf_dob)
    df_merged['age'] = dt_diff // timedelta(days=365.2425)

    # Find closest SIMD
    df_merged = df_merged.apply(find_closest_simd, axis=1)

    # Drop additional columns
    cols2drop = ['copd_date', 'resp_date', 'adm_date', 'rescue_date',
                 'simd_2009_quintile', 'simd_2009_decile',
                 'simd_2009_vigintile', 'simd_2012_quintile',
                 'simd_2012_decile', 'simd_2012_vigintile',
                 'simd_2016_quintile', 'simd_2016_decile',
                 'simd_2016_vigintile', 'days_since_copd',
                 'days_since_resp']
    df_merged = df_merged.drop(cols2drop, axis=1)

    # Save dataset
    df_merged.to_pickle(data_path + 'merged_full.pkl')


main()