<s> from chatto_transform . transforms . transform_base import Transform <EOL> from chatto_transform . schema . mimic import care_value_schema as cvs <EOL> from chatto_transform . schema . schema_base import MultiSchema , PartialSchema <EOL> from chatto_transform . schema . mimic . mimic_schema import cptevents_schema , admissions_schema , patients_schema , ioevents_schema , icustayevents_schema <EOL> from chatto_transform . lib . chunks import left_join , horizontal_merge , from_chunks <EOL> from chatto_transform . sessions . mimic import mimic_common <EOL> import itertools <EOL> import pandas as pd <EOL> import numpy as np <EOL> from math import ceil <EOL> class VasopressorDays ( Transform ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , carevue_only = False , metavision_only = False ) : <EOL> if all ( [ carevue_only , metavision_only ] ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> self . carevue = not metavision_only <EOL> self . metavision = not carevue_only <EOL> def input_schema ( self ) : <EOL> return PartialSchema . from_schema ( ioevents_schema ) <EOL> def _load ( self ) : <EOL> valid_vaso_ids = [ ] <EOL> if self . metavision : <EOL> valid_vaso_ids . extend ( VasoIOEventsMetavision . valid_vaso_ids ) <EOL> if self . carevue : <EOL> valid_vaso_ids . extend ( VasoIOEventsCarevue . valid_vaso_ids ) <EOL> ioevents_cond = "<STR_LIT>" . format ( '<STR_LIT:U+002CU+0020>' . join ( map ( str , sorted ( valid_vaso_ids ) ) ) ) <EOL> ioevents_cond += "<STR_LIT>" <EOL> ioevents = mimic_common . load_table ( ioevents_schema , ioevents_cond ) <EOL> return ioevents <EOL> def _transform ( self , tables ) : <EOL> io_carevue = VasoIOEventsCarevue ( ) . transform ( tables ) <EOL> io_metavision = VasoIOEventsMetavision ( ) . transform ( tables ) <EOL> ioevents = from_chunks ( [ io_carevue , io_metavision ] ) <EOL> vaso_days = VasoDayCounts ( ) . transform ( ioevents ) <EOL> return vaso_days <EOL> class VasoIOEventsCarevue ( Transform ) : <EOL> vaso_mappings = { <EOL> '<STR_LIT>' : [ <NUM_LIT> , <NUM_LIT> ] , <EOL> '<STR_LIT>' : [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> '<STR_LIT>' : [ <NUM_LIT> , <NUM_LIT> ] , <EOL> '<STR_LIT>' : [ <NUM_LIT> ] , <EOL> '<STR_LIT>' : [ <NUM_LIT> , <NUM_LIT> ] , <EOL> '<STR_LIT>' : [ <NUM_LIT> , <NUM_LIT> ] , <EOL> '<STR_LIT>' : [ <NUM_LIT> ] <EOL> } <EOL> valid_vaso_ids = list ( itertools . chain . from_iterable ( vaso_mappings . values ( ) ) ) <EOL> vaso_types = list ( vaso_mappings . keys ( ) ) <EOL> vaso_ids_ser = pd . Series ( index = valid_vaso_ids ) <EOL> for vaso , vaso_ids in vaso_mappings . items ( ) : <EOL> vaso_ids_ser . loc [ vaso_ids ] = vaso <EOL> vaso_ids_ser = vaso_ids_ser . astype ( '<STR_LIT>' ) <EOL> def input_schema ( self ) : <EOL> return PartialSchema . from_schema ( ioevents_schema ) <EOL> def output_schema ( self ) : <EOL> return cvs . vaso_days_input <EOL> def _load ( self ) : <EOL> ioevents_cond = "<STR_LIT>" . format ( '<STR_LIT:U+002CU+0020>' . join ( map ( str , sorted ( self . valid_vaso_ids ) ) ) ) <EOL> ioevents_cond += "<STR_LIT>" <EOL> ioevents = mimic_common . load_table ( ioevents_schema , ioevents_cond ) <EOL> return ioevents <EOL> def _transform ( self , ioevents ) : <EOL> ioevents = ioevents [ ioevents [ '<STR_LIT>' ] . isin ( self . valid_vaso_ids ) ] <EOL> ioevents = ioevents [ ( ioevents [ '<STR_LIT>' ] != <NUM_LIT:0> ) | ( ioevents [ '<STR_LIT>' ] != <NUM_LIT:0> ) ] <EOL> ioevents [ '<STR_LIT>' ] = self . vaso_ids_ser . cat . codes . loc [ ioevents [ '<STR_LIT>' ] ] . values <EOL> ioevents [ '<STR_LIT>' ] = pd . Categorical . from_codes ( <EOL> codes = ioevents [ '<STR_LIT>' ] , <EOL> categories = self . vaso_ids_ser . cat . categories , <EOL> name = '<STR_LIT>' <EOL> ) <EOL> ioevents [ '<STR_LIT:date>' ] = pd . to_datetime ( ioevents [ '<STR_LIT>' ] . dt . date ) <EOL> ioevents = ioevents [ cvs . vaso_days_input . col_names ( ) ] <EOL> return ioevents <EOL> class VasoIOEventsMetavision ( Transform ) : <EOL> vaso_mappings = { <EOL> '<STR_LIT>' : [ <NUM_LIT> ] , <EOL> '<STR_LIT>' : [ <NUM_LIT> ] , <EOL> '<STR_LIT>' : [ <NUM_LIT> ] , <EOL> '<STR_LIT>' : [ <NUM_LIT> ] , <EOL> '<STR_LIT>' : [ <NUM_LIT> ] , <EOL> '<STR_LIT>' : [ <NUM_LIT> ] , <EOL> '<STR_LIT>' : [ <NUM_LIT> ] <EOL> } <EOL> valid_vaso_ids = list ( itertools . chain . from_iterable ( vaso_mappings . values ( ) ) ) <EOL> vaso_types = list ( vaso_mappings . keys ( ) ) <EOL> vaso_ids_ser = pd . Series ( index = valid_vaso_ids ) <EOL> for vaso , vaso_ids in vaso_mappings . items ( ) : <EOL> vaso_ids_ser . loc [ vaso_ids ] = vaso <EOL> vaso_ids_ser = vaso_ids_ser . astype ( '<STR_LIT>' ) <EOL> def input_schema ( self ) : <EOL> return PartialSchema . from_schema ( ioevents_schema ) <EOL> def output_schema ( self ) : <EOL> return cvs . vaso_days_input <EOL> def _load ( self ) : <EOL> ioevents_cond = "<STR_LIT>" . format ( '<STR_LIT:U+002CU+0020>' . join ( map ( str , sorted ( self . valid_vaso_ids ) ) ) ) <EOL> ioevents_cond += "<STR_LIT>" <EOL> ioevents = mimic_common . load_table ( ioevents_schema , ioevents_cond ) <EOL> return ioevents <EOL> def _transform ( self , ioevents ) : <EOL> ioevents = ioevents [ ioevents [ '<STR_LIT>' ] . isin ( self . valid_vaso_ids ) ] <EOL> ioevents = ioevents [ ( ioevents [ '<STR_LIT>' ] != <NUM_LIT:0> ) | ( ioevents [ '<STR_LIT>' ] != <NUM_LIT:0> ) ] <EOL> ioevents [ '<STR_LIT>' ] = self . vaso_ids_ser . cat . codes . loc [ ioevents [ '<STR_LIT>' ] ] . values <EOL> ioevents [ '<STR_LIT>' ] = pd . Categorical . from_codes ( <EOL> codes = ioevents [ '<STR_LIT>' ] , <EOL> categories = self . vaso_ids_ser . cat . categories , <EOL> name = '<STR_LIT>' <EOL> ) <EOL> ioevents [ '<STR_LIT>' ] = pd . to_datetime ( ioevents [ '<STR_LIT>' ] . dt . date ) <EOL> ioevents [ '<STR_LIT>' ] = pd . to_datetime ( ioevents [ '<STR_LIT>' ] . dt . date ) <EOL> ioevents = ioevents [ [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ] <EOL> ioevents = ExpandDateRange ( ) . transform ( ioevents ) <EOL> ioevents = ioevents [ cvs . vaso_days_input . col_names ( ) ] <EOL> return ioevents <EOL> class ExpandDateRange ( Transform ) : <EOL> def input_schema ( self ) : <EOL> return cvs . date_range <EOL> def output_schema ( self ) : <EOL> return cvs . date <EOL> def _transform ( self , df ) : <EOL> df [ '<STR_LIT>' ] = df [ '<STR_LIT>' ] - df [ '<STR_LIT>' ] + pd . Timedelta ( days = <NUM_LIT:1> ) <EOL> invalid_range_mask = df [ '<STR_LIT>' ] < <NUM_LIT:1> <EOL> df = df [ ~ invalid_range_mask ] <EOL> max_days = ceil ( df [ '<STR_LIT>' ] . max ( ) . days ) <EOL> df [ '<STR_LIT:date>' ] = df [ '<STR_LIT>' ] <EOL> del df [ '<STR_LIT>' ] , df [ '<STR_LIT>' ] <EOL> expanded = [ df ] <EOL> expand_per_row = False <EOL> for i in range ( <NUM_LIT:2> , max_days ) : <EOL> prev = expanded [ - <NUM_LIT:1> ] <EOL> rows_as_long_as = prev [ prev [ '<STR_LIT>' ] >= pd . Timedelta ( days = i ) ] <EOL> rows_as_long_as . loc [ : , '<STR_LIT:date>' ] += pd . Timedelta ( days = <NUM_LIT:1> ) <EOL> if len ( rows_as_long_as ) < max_days / len ( rows_as_long_as . columns ) ** <NUM_LIT:2> : <EOL> expand_per_row = True <EOL> break <EOL> expanded . append ( rows_as_long_as ) <EOL> if expand_per_row : <EOL> rala = rows_as_long_as <EOL> rala_cat = rala . select_dtypes ( include = [ '<STR_LIT>' ] ) <EOL> rala_categories = { } <EOL> for col in rala_cat . columns : <EOL> rala_categories [ col ] = rala [ col ] . cat . categories <EOL> rala [ col ] = rala [ col ] . cat . codes <EOL> for j , row in rala . iterrows ( ) : <EOL> date = row [ '<STR_LIT:date>' ] <EOL> n_days = row [ '<STR_LIT>' ] . days - i <EOL> expanded_row = pd . DataFrame ( index = np . arange ( n_days + <NUM_LIT:1> ) , columns = row . index ) <EOL> for col in expanded_row . columns : <EOL> expanded_row [ col ] = row [ col ] <EOL> expanded_row [ '<STR_LIT:date>' ] = pd . date_range ( date , date + pd . Timedelta ( days = n_days ) ) <EOL> for col , categories in rala_categories . items ( ) : <EOL> expanded_row [ col ] = pd . Categorical . from_codes ( <EOL> codes = expanded_row [ col ] , <EOL> categories = categories , <EOL> name = col <EOL> ) <EOL> expanded . append ( expanded_row ) <EOL> df = from_chunks ( expanded ) <EOL> return df <EOL> class VasoDayCounts ( Transform ) : <EOL> vaso_types = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> def input_schema ( self ) : <EOL> return cvs . vaso_days_input <EOL> def output_schema ( self ) : <EOL> return cvs . vaso_days_output <EOL> def _transform ( self , ioevents ) : <EOL> grp = ioevents . groupby ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> total_vaso_days = grp [ '<STR_LIT:date>' ] . nunique ( ) <EOL> grp2 = ioevents . groupby ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> vaso_days = grp2 [ '<STR_LIT:date>' ] . nunique ( ) . unstack ( '<STR_LIT>' ) <EOL> vaso_days [ '<STR_LIT>' ] = total_vaso_days <EOL> vaso_days . loc [ : , self . vaso_types + [ '<STR_LIT>' ] ] *= pd . Timedelta ( days = <NUM_LIT:1> ) <EOL> vaso_days . columns = [ col + '<STR_LIT>' if col in self . vaso_types <EOL> else col <EOL> for col in vaso_days . columns ] <EOL> vaso_days . fillna ( <NUM_LIT:0> , inplace = True ) <EOL> vaso_days = vaso_days . reset_index ( ) <EOL> return vaso_days <EOL> class VentilatorDays ( Transform ) : <EOL> def __init__ ( self , subject_ids = None , hadm_ids = None ) : <EOL> if subject_ids is not None : <EOL> subject_ids = list ( subject_ids ) <EOL> self . subject_ids = subject_ids <EOL> if hadm_ids is not None : <EOL> hadm_ids = list ( hadm_ids ) <EOL> self . hadm_ids = hadm_ids <EOL> cpt_cond = """<STR_LIT>""" <EOL> def input_schema ( self ) : <EOL> return MultiSchema ( { <EOL> '<STR_LIT>' : PartialSchema . from_schema ( cptevents_schema ) , <EOL> '<STR_LIT>' : PartialSchema . from_schema ( icustayevents_schema ) <EOL> } ) <EOL> def _load ( self ) : <EOL> conds = [ ] <EOL> if self . subject_ids is not None : <EOL> conds . append ( '<STR_LIT>' . format ( '<STR_LIT:U+002CU+0020>' . join ( map ( str , self . subject_ids ) ) ) ) <EOL> if self . hadm_ids is not None : <EOL> conds . append ( '<STR_LIT>' . format ( '<STR_LIT:U+002CU+0020>' . join ( map ( str , self . hadm_ids ) ) ) ) <EOL> cpt_cond = '<STR_LIT>' . join ( conds + [ self . cpt_cond ] ) or None <EOL> cptevents = mimic_common . load_table ( cptevents_schema , cpt_cond ) <EOL> icu_cond = '<STR_LIT>' . join ( conds ) or None <EOL> icustayevents = mimic_common . load_table ( icustayevents_schema , icu_cond ) <EOL> return { '<STR_LIT>' : cptevents , '<STR_LIT>' : icustayevents } <EOL> def _transform ( self , tables ) : <EOL> cpt = tables [ '<STR_LIT>' ] <EOL> cpt = cpt [ cpt [ '<STR_LIT>' ] . isin ( [ '<STR_LIT>' ] ) ] <EOL> cpt = cpt [ cpt [ '<STR_LIT:description>' ] . isin ( [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] ) ] <EOL> cpt = cpt [ [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ] <EOL> icu = tables [ '<STR_LIT>' ] <EOL> if self . subject_ids is not None : <EOL> cpt = cpt [ cpt [ '<STR_LIT>' ] . isin ( self . subject_ids ) ] <EOL> icu = icu [ icu [ '<STR_LIT>' ] . isin ( self . subject_ids ) ] <EOL> if self . hadm_ids is not None : <EOL> cpt = cpt [ cpt [ '<STR_LIT>' ] . isin ( self . hadm_ids ) ] <EOL> icu = icu [ icu [ '<STR_LIT>' ] . isin ( self . hadm_ids ) ] <EOL> df = AdmICUStayMapper ( ) . transform ( { <EOL> '<STR_LIT>' : icu , <EOL> '<STR_LIT>' : cpt <EOL> } ) <EOL> df = df [ pd . to_datetime ( df [ '<STR_LIT>' ] . dt . date ) <= df [ '<STR_LIT>' ] ] <EOL> df = df [ pd . to_datetime ( df [ '<STR_LIT>' ] . dt . date ) >= df [ '<STR_LIT>' ] ] <EOL> grp = df . groupby ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> ventdays = grp [ '<STR_LIT>' ] . nunique ( ) . reset_index ( ) <EOL> ventdays [ '<STR_LIT>' ] = ventdays [ '<STR_LIT>' ] * pd . Timedelta ( days = <NUM_LIT:1> ) <EOL> del ventdays [ '<STR_LIT>' ] <EOL> return ventdays <EOL> class DaysUntilDeath ( Transform ) : <EOL> def __init__ ( self , subject_ids = None , hadm_ids = None , icustay_ids = None ) : <EOL> if subject_ids is not None : <EOL> subject_ids = list ( subject_ids ) <EOL> self . subject_ids = subject_ids <EOL> if hadm_ids is not None : <EOL> hadm_ids = list ( hadm_ids ) <EOL> self . hadm_ids = hadm_ids <EOL> if icustay_ids is not None : <EOL> icustay_ids = list ( icustay_ids ) <EOL> self . icustay_ids = icustay_ids <EOL> def input_schema ( self ) : <EOL> return MultiSchema ( { <EOL> '<STR_LIT>' : PartialSchema . from_schema ( admissions_schema ) , <EOL> '<STR_LIT>' : PartialSchema . from_schema ( patients_schema ) , <EOL> '<STR_LIT>' : PartialSchema . from_schema ( icustayevents_schema ) <EOL> } ) <EOL> def output_schema ( self ) : <EOL> return cvs . days_until_death <EOL> def _load ( self , incr_data = None ) : <EOL> if incr_data is None : <EOL> incr_data = { } <EOL> adm_conds = [ ] <EOL> icu_conds = [ ] <EOL> pat_cond = None <EOL> if self . subject_ids is not None : <EOL> pat_cond = '<STR_LIT>' . format ( '<STR_LIT:U+002CU+0020>' . join ( map ( str , self . subject_ids ) ) ) <EOL> adm_conds . append ( pat_cond ) <EOL> icu_conds . append ( pat_cond ) <EOL> if self . hadm_ids is not None : <EOL> adm_cond = '<STR_LIT>' . format ( '<STR_LIT:U+002CU+0020>' . join ( map ( str , self . hadm_ids ) ) ) <EOL> adm_conds . append ( adm_cond ) <EOL> icu_conds . append ( adm_cond ) <EOL> if self . icustay_ids is not None : <EOL> icu_cond = '<STR_LIT>' . format ( '<STR_LIT:U+002CU+0020>' . join ( map ( str , self . icustay_ids ) ) ) <EOL> icu_conds . append ( icu_cond ) <EOL> if '<STR_LIT>' not in incr_data : <EOL> adm_cond = '<STR_LIT>' . join ( adm_conds ) or None <EOL> incr_data [ '<STR_LIT>' ] = mimic_common . load_table ( admissions_schema , adm_cond ) <EOL> if '<STR_LIT>' not in incr_data : <EOL> icu_cond = '<STR_LIT>' . join ( icu_conds ) or None <EOL> incr_data [ '<STR_LIT>' ] = mimic_common . load_table ( icustayevents_schema , icu_cond ) <EOL> if '<STR_LIT>' not in incr_data : <EOL> incr_data [ '<STR_LIT>' ] = mimic_common . load_table ( patients_schema , pat_cond ) <EOL> return incr_data <EOL> def _transform ( self , tables ) : <EOL> pat = tables [ '<STR_LIT>' ] <EOL> adm = tables [ '<STR_LIT>' ] <EOL> icu = tables [ '<STR_LIT>' ] <EOL> if self . subject_ids is not None : <EOL> pat = pat [ pat [ '<STR_LIT>' ] . isin ( self . subject_ids ) ] <EOL> adm = adm [ adm [ '<STR_LIT>' ] . isin ( self . subject_ids ) ] <EOL> icu = icu [ icu [ '<STR_LIT>' ] . isin ( self . subject_ids ) ] <EOL> if self . hadm_ids is not None : <EOL> adm = adm [ adm [ '<STR_LIT>' ] . isin ( self . hadm_ids ) ] <EOL> icu = icu [ icu [ '<STR_LIT>' ] . isin ( self . hadm_ids ) ] <EOL> if self . icustay_ids is not None : <EOL> icu = icu [ icu [ '<STR_LIT>' ] . isin ( self . icustay_ids ) ] <EOL> df = left_join ( pat , adm , '<STR_LIT>' ) <EOL> df [ '<STR_LIT>' ] = df [ '<STR_LIT>' ] . notnull ( ) <EOL> df [ '<STR_LIT>' ] = df [ '<STR_LIT>' ] . fillna ( df [ '<STR_LIT>' ] ) <EOL> df [ '<STR_LIT>' ] = df [ '<STR_LIT>' ] - df [ '<STR_LIT>' ] <EOL> df = AdmICUStayMapper ( ) . transform ( { <EOL> '<STR_LIT>' : icu , <EOL> '<STR_LIT>' : df <EOL> } ) <EOL> df [ '<STR_LIT>' ] = ( df [ '<STR_LIT>' ] >= df [ '<STR_LIT>' ] ) & ( df [ '<STR_LIT>' ] <= df [ '<STR_LIT>' ] ) <EOL> return df [ [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ] <EOL> class AdmICUStayMapper ( Transform ) : <EOL> def input_schema ( self ) : <EOL> return cvs . icu_hadm_map <EOL> def _load ( self , incr_data = None ) : <EOL> if incr_data is None : <EOL> incr_data = { } <EOL> if '<STR_LIT>' not in incr_data : <EOL> incr_data [ '<STR_LIT>' ] = mimic_common . load_table ( mimic_schema . icustayevents_schema ) <EOL> if '<STR_LIT>' not in incr_data : <EOL> incr_data [ '<STR_LIT>' ] = mimic_common . load_table ( mimic_schema . admissions_schema ) <EOL> return incr_data <EOL> def _transform ( self , tables ) : <EOL> ie = tables [ '<STR_LIT>' ] <EOL> adm = tables [ '<STR_LIT>' ] <EOL> df = left_join ( ie , adm , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> return df </s>
