#NEWLINE# def survival_table_from_events( death_times , event_observed , birth_times = None , columns = [ 'removed' , 'observed' , 'censored' , 'entrance' , 'at_risk' ] , weights = None ) : #NEWLINE# #INDENT# "\n    Parameters:\n        death_times: (n,) array of event times\n        event_observed: (n,) boolean array, 1 if observed event, 0 is censored event.\n        birth_times: a (n,) array of numbers representing\n          when the subject was first observed. A subject's death event is then at [birth times + duration observed].\n          If None (default), birth_times are set to be the first observation or 0, which ever is smaller.\n        columns: a 3-length array to call the, in order, removed individuals, observed deaths\n          and censorships.\n        weights: Default None, otherwise (n,1) array. Optional argument to use weights for individuals.\n    Returns:\n        Pandas DataFrame with index as the unique times in event_times. The columns named\n        'removed' refers to the number of individuals who were removed from the population\n        by the end of the period. The column 'observed' refers to the number of removed\n        individuals who were observed to have died (i.e. not censored.) The column\n        'censored' is defined as 'removed' - 'observed' (the number of individuals who\n         left the population due to event_observed)\n\n    Example:\n\n                  removed  observed  censored  entrance   at_risk\n        event_at\n        0               0         0         0        11        11\n        6               1         1         0         0        11\n        7               2         2         0         0        10\n        9               3         3         0         0         8\n        13              3         3         0         0         5\n        15              2         2         0         0         2\n\n    " #NEWLINE# ( removed , observed , censored , entrance , at_risk ) = columns #NEWLINE# death_times = np . asarray ( death_times ) #NEWLINE# if ( birth_times is None ) : #NEWLINE# #INDENT# birth_times = ( min ( 0 , death_times . min ( ) ) * np . ones ( death_times . shape [ 0 ] ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# birth_times = np . asarray ( birth_times ) #NEWLINE# if np . any ( ( birth_times > death_times ) ) : #NEWLINE# #INDENT# raise ValueError ( 'birth time must be less than time of death.' ) #NEWLINE# #UNINDENT# #UNINDENT# df = pd . DataFrame ( death_times , columns = [ 'event_at' ] ) #NEWLINE# df [ removed ] = ( 1 if ( weights is None ) else weights ) #NEWLINE# df [ observed ] = np . asarray ( event_observed ) #NEWLINE# death_table = df . groupby ( 'event_at' ) . sum ( ) #NEWLINE# death_table [ censored ] = ( death_table [ removed ] - death_table [ observed ] ) . astype ( int ) #NEWLINE# births = pd . DataFrame ( birth_times , columns = [ 'event_at' ] ) #NEWLINE# births [ entrance ] = 1 #NEWLINE# births_table = births . groupby ( 'event_at' ) . sum ( ) #NEWLINE# event_table = death_table . join ( births_table , how = 'outer' , sort = True ) . fillna ( 0 ) #NEWLINE# event_table [ at_risk ] = ( event_table [ entrance ] . cumsum ( ) - event_table [ removed ] . cumsum ( ) . shift ( 1 ) . fillna ( 0 ) ) #NEWLINE# return event_table . astype ( float )