# cleaning and converting to time series events = map_itemids_to_variables(events, var_map) events = clean_events(events) if events.shape[0] == 0: # no valid events for this subject continue timeseries = convert_events_to_timeseries(events, variables=variables) # extracting separate episodes for i in range(stays.shape[0]): stay_id = stays.ICUSTAY_ID.iloc[i] intime = stays.INTIME.iloc[i] outtime = stays.OUTTIME.iloc[i] episode = get_events_for_stay(timeseries, stay_id, intime, outtime) if episode.shape[0] == 0: # no data for this episode continue episode = add_hours_elpased_to_events( episode, intime).set_index('HOURS').sort_index(axis=0) if stay_id in episodic_data.index: episodic_data.loc[stay_id, 'Weight'] = get_first_valid_from_timeseries( episode, 'Weight') episodic_data.loc[stay_id, 'Height'] = get_first_valid_from_timeseries( episode, 'Height') episodic_data.loc[episodic_data.index == stay_id].to_csv( os.path.join(args.subjects_root_path, subject_dir,
if events.shape[0] == 0: sys.stdout.write('no valid events!\n') continue timeseries = convert_events_to_timeseries(events, variables=variables) sys.stdout.write('extracting separate episodes...') sys.stdout.flush() for i in range(stays.shape[0]): stay_id = stays.ICUSTAY_ID.iloc[i] sys.stdout.write(' {}'.format(stay_id)) sys.stdout.flush() intime = stays.INTIME.iloc[i] outtime = stays.OUTTIME.iloc[i] episode = get_events_for_stay(timeseries, stay_id, intime, outtime) if episode.shape[0] == 0: sys.stdout.write(' (no data!)') sys.stdout.flush() continue episode = add_hours_elpased_to_events(episode, intime).set_index('HOURS').sort_index(axis=0) episodic_data.Weight.ix[stay_id] = get_first_valid_from_timeseries(episode, 'Weight') episodic_data.Height.ix[stay_id] = get_first_valid_from_timeseries(episode, 'Height') episodic_data.ix[episodic_data.index==stay_id].to_csv(os.path.join(args.subjects_root_path, subject_dir, 'episode{}.csv'.format(i+1)), index_label='Icustay') columns = list(episode.columns) columns_sorted = sorted(columns, key=(lambda x: "" if x == "Hours" else x)) episode = episode[columns_sorted] episode.to_csv(os.path.join(args.subjects_root_path, subject_dir, 'episode{}_timeseries.csv'.format(i+1)), index_label='Hours') sys.stdout.write(' DONE!\n')