def max_add_feat(active_chl,rng,pf):
    rng = rng
    min_per=1
    addfeat_data = 0

    header = preproc.get_header(pf)
    header_non_act = np.copy(header).tolist()
    header_non_act.remove(active_chl)
    header_non_act.remove('Elapsed time')
    for chl in header_non_act:
        sig_add = preproc.get_column(chl,pf)[2:]
        sig_add = preproc.check(sig_add,return_series=1)[0]
        chl_add = sig_add[rng[0]:rng[-1]]
        if not discard_add_feat(sig_add,rng):
            idx_isnan = np.isnan(chl_add)
            idx = np.arange(len(chl_add))
            idx_nan = idx[idx_isnan]
            percent = len(idx_nan)/float(len(idx))
            if min_per ==1:
                min_per=percent
                min_chl = sig_add
            elif percent<=min_per:
                min_per = percent
                min_chl = sig_add
            if min_per==0:
                break
    addfeat_data = min_chl[rng[0]-50000:rng[0]]
    if addfeat_data!=0:
        if np.isnan(addfeat_data).any():
            idx = np.arange(len(addfeat_data))
            idx_isnan = np.isnan(addfeat_data)
            idx = idx[idx_isnan].tolist()
            idx_start = idx[-1]
            addfeat_data = addfeat_data[idx_start:]
    return chl_add, addfeat_data
def add_feat(rng, active_chl,pf):  ## name of active channel
    active_chl= active_chl
    rng = rng
    pf = pf
    tr=0
    add_feat_pred,add_feat_tr = max_add_feat(active_chl,rng,pf)
    print add_feat_pred,add_feat_tr
    active_chl_array = preproc.get_column(active_chl,pf)[2:]
    active_chl_array = preproc.check(active_chl_array,return_series=1)[0]
    tr = gen_tr_data(rng,active_chl_array)
    if tr!=0 and add_feat_tr!=0:
        tr_len = min(len(tr),len(add_feat_tr))
        tr = tr[-tr_len:]
        add_feat_tr = add_feat_tr[-tr_len:]
        return tr, add_feat_tr, add_feat_pred
    else:
        return 0
 def interpolate(self):
      sig_main = preproc.get_column(self.col,self.pf)
      res = preproc.check(sig_main[2:],return_series=1)
      if res[-1]>=0.9:
          return sig_main
      sig_main_gaps = res[3]
      sig_array = res[0]
      active_chl = self.col
      for gap in sig_main_gaps:
           rng = gap
           data = gen_add_feat.add_feat(rng,active_chl,self.pf)
           if data!=0:
                tr, add_feat_tr,add_feat_pred = data[0],data[1],data[2]
                nn = NeuralnetInterpolator(tr,add_feat_tr,add_feat_pred,self.window_length,self.bool_var)
                new_sig = nn.interpolate()
                sig_array[rng[0]:rng[-1]] = new_sig
           else:
                pass
      sig_interpolated = sig_array
      return sig_interpolated
def summarize(file_name):
     file_path = '/Users/aubrey9012/Downloads/medical_ICU-features/data/mimic/'+str(file_name)+'.csv'
     output_path = '/Users/aubrey9012/Downloads/medical_ICU-features/data/output/summary_of_'+str(file_name)+'.csv'
     f = open(file_path, 'r')
     output = open(output_path, 'wt')
     writer = csv.writer(output)
     reader = csv.reader(f)
     h = reader.next()
     header = [str(i)[1:-1] for i in h]
     print header
     f.close()
     f = open(file_path, 'r')
     key = ['black_cnt', 'gaps', 'gap_indeces', 'missing_value_proportion']
     for col in header:
          writer.writerow([col])
          writer.writerow(key)
          sig = preproc.get_column(col, file_path)
          sig = sig[2:]
          res = preproc.check(sig, return_series =False)
          writer.writerow(res)
     f.close()
     output.close()
     return