text: import copy, math, os, pickle, time, pandas as pd, numpy as np
  
  ID_COLS = ['subject_id', 'hadm_id', 'icustay_id']
  
  def simple_imputer(df,train_subj):
      idx = pd.IndexSlice
      df = df.copy()
      
      df_out = df.loc[:, idx[:, ['mean', 'count']]]
      icustay_means = df_out.loc[:, idx[:, 'mean']].groupby(ID_COLS).mean()
      global_means = df_out.loc[idx[train_subj,:], idx[:, 'mean']].mean(axis=0)
      
      df_out.loc[:,idx[:,'mean']] = df_out.loc[:,idx[:,'mean']].groupby(ID_COLS).fillna(
          method='ffill'
      ).groupby(ID_COLS).fillna(icustay_means).fillna(global_means)
      
      df_out.loc[:, idx[:, 'count']] = (df.loc[:, idx[:, 'count']] > 0).astype(float)
      df_out.rename(columns={'count': 'mask'}, level='Aggregation Function', inplace=True)
      
      is_absent = (1 - df_out.loc[:, idx[:, 'mask']])
      hours_of_absence = is_absent.cumsum()
      time_since_measured = hours_of_absence - hours_of_absence[is_absent==0].fillna(method='ffill')
      time_since_measured.rename(columns={'mask': 'time_since_measured'}, level='Aggregation Function', inplace=True)
  
      df_out = pd.concat((df_out, time_since_measured), axis=1)
      df_out.loc[:, idx[:, 'time_since_measured']] = df_out.loc[:, idx[:, 'time_since_measured']].fillna(100)
      
      df_out.sort_index(axis=1, inplace=True)
      return df_out