def _transform(self, tables):
        #group services by hadm_id and compute whether first service per hadm_id is a surgery
        services = tables['services']
        curr_service_cats = pd.Series(services['curr_service'].cat.categories)
        surgery_cats = curr_service_cats[curr_service_cats.str.contains('SURG')]
        surgery_codes = surgery_cats.index
        services['curr_service_code'] = services['curr_service'].cat.codes
        services.sort_index(by=['hadm_id', 'transfertime'], inplace=True)
        first_services = services.groupby('hadm_id', sort=False)['curr_service_code'].first().reset_index()
        first_services['is_surgery'] = first_services['curr_service_code'].isin(surgery_codes)

        #get the other tables
        icu = tables['icustayevents']
        adm = tables['admissions']    
        pat = tables['patients']
    
        #assign_prefixes to all of them before the join
        df_prefix = [
            (first_services, 's'),
            (icu, 'icu'),
            (adm, 'adm'),
            (pat, 'pat')
        ]
        for df, prefix in df_prefix:
            df.columns = [prefix + '.' + col for col in df.columns]

        j_df = left_join(icu, adm, left_on='icu.hadm_id', right_on='adm.hadm_id')
        j_df = left_join(j_df, pat, left_on='icu.subject_id', right_on='pat.subject_id')
        j_df = left_join(j_df, first_services, left_on='icu.hadm_id', right_on='s.hadm_id')

        df = pd.DataFrame(index=j_df.index)
        df['icustay_id'] = j_df['icu.icustay_id']
        df['pre_icu_los'] = j_df['icu.intime'] - j_df['adm.admittime']
        df['intime'] = j_df['icu.intime']
        df['outtime'] = j_df['icu.outtime']
        df['deathtime'] = j_df['adm.deathtime']
        df['age'] = j_df['icu.intime'] - j_df['pat.dob']
        df['elective_surgery'] = (j_df['adm.admission_type'].isin(['ELECTIVE'])) & j_df['s.is_surgery']
        df['emergency_surgery'] = (j_df['adm.admission_type'].isin(['EMERGENCY', 'URGENT'])) & j_df['s.is_surgery']
        
        df['age_group'] = 1
        df.loc[df['age'] / np.timedelta64(1, 'M') <= 1, 'age_group'] = 0
        df.loc[df['age'] / np.timedelta64(1, 'M') > 16, 'age_group'] = 2
        df['age_group'] = pd.Categorical.from_codes(df['age_group'], categories=['neonate', 'middle', 'adult'])
        
        df['diagnosis'] = j_df['adm.diagnosis']
        
        df['icustay_expire_flag'] = j_df['adm.deathtime'] <= j_df['icu.outtime'] # covers when deathtime is before, due to typographical errors
        df['icustay_expire_flag'] |= (j_df['adm.dischtime'] <= j_df['icu.outtime']) & j_df['adm.discharge_location'].isin(['DEAD/EXPIRED'])

        df['hospital_expire_flag'] = j_df['adm.discharge_location'].isin(['DEAD/EXPIRED'])

        return df
    def _transform(self, tables):
        pat = tables['patients']
        adm = tables['admissions']
        icu = tables['icustayevents']

        if self.subject_ids is not None:
            pat = pat[pat['subject_id'].isin(self.subject_ids)]
            adm = adm[adm['subject_id'].isin(self.subject_ids)]
            icu = icu[icu['subject_id'].isin(self.subject_ids)]
        if self.hadm_ids is not None:
            adm = adm[adm['hadm_id'].isin(self.hadm_ids)]
            icu = icu[icu['hadm_id'].isin(self.hadm_ids)]
        if self.icustay_ids is not None:
            icu = icu[icu['icustay_id'].isin(self.icustay_ids)]

        df = left_join(pat, adm, 'subject_id')

        df['hadm_death'] = df['deathtime'].notnull()
        df['time_of_death'] = df['dod_hosp'].fillna(df['dod_ssn'])
        df['time_until_death'] = df['time_of_death'] - df['dischtime']

        df = AdmICUStayMapper().transform({
            'icustayevents': icu,
            'admissions': df
        })

        df['icustay_death'] = (df['deathtime'] >= df['intime']) & (df['deathtime'] <= df['outtime'])

        return df[['subject_id', 'hadm_id', 'icustay_id', 'icustay_death', 'hadm_death', 'time_of_death', 'time_until_death']]
    def _transform(self, tables):
        pat = tables['patients']
        adm = tables['admissions']
        icu = tables['icustayevents']

        if self.subject_ids is not None:
            pat = pat[pat['subject_id'].isin(self.subject_ids)]
            adm = adm[adm['subject_id'].isin(self.subject_ids)]
            icu = icu[icu['subject_id'].isin(self.subject_ids)]
        if self.hadm_ids is not None:
            adm = adm[adm['hadm_id'].isin(self.hadm_ids)]
            icu = icu[icu['hadm_id'].isin(self.hadm_ids)]
        if self.icustay_ids is not None:
            icu = icu[icu['icustay_id'].isin(self.icustay_ids)]

        df = left_join(pat, adm, 'subject_id')

        df['hadm_death'] = df['deathtime'].notnull()
        df['time_of_death'] = df['dod_hosp'].fillna(df['dod_ssn'])
        df['time_until_death'] = df['time_of_death'] - df['dischtime']

        df = AdmICUStayMapper().transform({
            'icustayevents': icu,
            'admissions': df
        })

        df['icustay_death'] = (df['deathtime'] >= df['intime']) & (
            df['deathtime'] <= df['outtime'])

        return df[[
            'subject_id', 'hadm_id', 'icustay_id', 'icustay_death',
            'hadm_death', 'time_of_death', 'time_until_death'
        ]]
    def _transform(self, tables):
        labevents = tables['labevents']
        if self.lab_item_ids is not None:
            labevents = labevents[labevents['itemid'].isin(self.lab_item_ids)]
        icustayevents = tables['icustayevents']

        df = left_join(
            labevents[['hadm_id', 'charttime']],
            icustayevents[['hadm_id', 'icustay_id', 'intime', 'outtime']],
            'hadm_id')
        df = df[(df['charttime'] >= df['intime']) & (df['charttime'] <= df['outtime'])]
        icustay_ids = df['icustay_id'].unique()
        return icustayevents[icustayevents['icustay_id'].isin(icustay_ids)]
    def _transform(self, tables):
        labevents = tables['labevents']
        if self.lab_item_ids is not None:
            labevents = labevents[labevents['itemid'].isin(self.lab_item_ids)]
        icustayevents = tables['icustayevents']

        df = left_join(
            labevents[['hadm_id', 'charttime']],
            icustayevents[['hadm_id', 'icustay_id', 'intime',
                           'outtime']], 'hadm_id')
        df = df[(df['charttime'] >= df['intime'])
                & (df['charttime'] <= df['outtime'])]
        icustay_ids = df['icustay_id'].unique()
        return icustayevents[icustayevents['icustay_id'].isin(icustay_ids)]
    def _transform(self, tables):
        #group services by hadm_id and compute whether first service per hadm_id is a surgery
        services = tables['services']
        curr_service_cats = pd.Series(services['curr_service'].cat.categories)
        surgery_cats = curr_service_cats[curr_service_cats.str.contains(
            'SURG')]
        surgery_codes = surgery_cats.index
        services['curr_service_code'] = services['curr_service'].cat.codes
        services.sort_index(by=['hadm_id', 'transfertime'], inplace=True)
        first_services = services.groupby(
            'hadm_id', sort=False)['curr_service_code'].first().reset_index()
        first_services['is_surgery'] = first_services[
            'curr_service_code'].isin(surgery_codes)

        #get the other tables
        icu = tables['icustayevents']
        adm = tables['admissions']
        pat = tables['patients']

        #assign_prefixes to all of them before the join
        df_prefix = [(first_services, 's'), (icu, 'icu'), (adm, 'adm'),
                     (pat, 'pat')]
        for df, prefix in df_prefix:
            df.columns = [prefix + '.' + col for col in df.columns]

        j_df = left_join(icu,
                         adm,
                         left_on='icu.hadm_id',
                         right_on='adm.hadm_id')
        j_df = left_join(j_df,
                         pat,
                         left_on='icu.subject_id',
                         right_on='pat.subject_id')
        j_df = left_join(j_df,
                         first_services,
                         left_on='icu.hadm_id',
                         right_on='s.hadm_id')

        df = pd.DataFrame(index=j_df.index)
        df['icustay_id'] = j_df['icu.icustay_id']
        df['pre_icu_los'] = j_df['icu.intime'] - j_df['adm.admittime']
        df['intime'] = j_df['icu.intime']
        df['outtime'] = j_df['icu.outtime']
        df['deathtime'] = j_df['adm.deathtime']
        df['age'] = j_df['icu.intime'] - j_df['pat.dob']
        df['elective_surgery'] = (j_df['adm.admission_type'].isin(
            ['ELECTIVE'])) & j_df['s.is_surgery']
        df['emergency_surgery'] = (j_df['adm.admission_type'].isin(
            ['EMERGENCY', 'URGENT'])) & j_df['s.is_surgery']

        df['age_group'] = 1
        df.loc[df['age'] / np.timedelta64(1, 'M') <= 1, 'age_group'] = 0
        df.loc[df['age'] / np.timedelta64(1, 'M') > 16, 'age_group'] = 2
        df['age_group'] = pd.Categorical.from_codes(
            df['age_group'], categories=['neonate', 'middle', 'adult'])

        df['diagnosis'] = j_df['adm.diagnosis']

        df['icustay_expire_flag'] = j_df['adm.deathtime'] <= j_df[
            'icu.outtime']  # covers when deathtime is before, due to typographical errors
        df['icustay_expire_flag'] |= (
            j_df['adm.dischtime'] <= j_df['icu.outtime']
        ) & j_df['adm.discharge_location'].isin(['DEAD/EXPIRED'])

        df['hospital_expire_flag'] = j_df['adm.discharge_location'].isin(
            ['DEAD/EXPIRED'])

        return df
 def _transform(self, tables):
     ie = tables['icustayevents']
     adm = tables['admissions']
     df = left_join(ie, adm, ['hadm_id', 'subject_id'])
     return df
 def _transform(self, tables):
     ie = tables['icustayevents']
     adm = tables['admissions']
     df = left_join(ie, adm, ['hadm_id', 'subject_id'])
     return df