-
Notifications
You must be signed in to change notification settings - Fork 1
/
joint_preprocess.py
154 lines (138 loc) · 5.89 KB
/
joint_preprocess.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import pandas as pd
import numpy as np
from scipy import stats
import os
import ujson as json
from tqdm import tqdm
import tensorflow as tf
import matplotlib.pyplot as plt
plt.switch_backend('agg')
def stat(seq_length):
print('Seq len info :')
seq_len = np.asarray(seq_length)
idx = np.arange(0, len(seq_len), dtype=np.int32)
print(stats.describe(seq_len))
plt.figure(figsize=(16, 9))
plt.subplot(121)
plt.plot(idx[:], seq_len[:], 'ro')
plt.grid(True)
plt.xlabel('index')
plt.ylabel('seq_len')
plt.title('Scatter Plot')
plt.subplot(122)
plt.hist(seq_len, bins=10, label=['seq_len'])
plt.grid(True)
plt.xlabel('seq_len')
plt.ylabel('freq')
plt.title('Histogram')
plt.savefig('./seq_len_stats.jpg', format='jpg')
# plt.show()
def preprocess_data(data_path):
train_samples, test_samples = [], []
total = 0
max_len = 0
seq_len = []
dead_len, live_len = 0, 0
tasks = ['5849', '25000']
for num, t in enumerate(tasks):
print('Reading raw files of task ' + t)
path = os.path.join(data_path, t)
train_path = os.path.join(path, 'train')
for file in tqdm(os.listdir(train_path)):
total += 1
if file.startswith('0'):
dead = 0
else:
dead = 1
raw_sample = pd.read_csv(os.path.join(train_path, file), sep=',')
raw_sample = raw_sample.fillna(0)
medicine = raw_sample.iloc[:, 209:].as_matrix()
index = raw_sample.iloc[:, 3:208].as_matrix()
length = index.shape[0]
if length > max_len:
max_len = length
sample = {'patient_id': total,
'index': index,
'medicine': medicine,
'label_mor': dead,
'label_dis': num}
train_samples.append(sample)
seq_len.append(length)
if dead == 0:
dead_len += length
else:
live_len += length
test_path = os.path.join(path, 'test')
for file in tqdm(os.listdir(test_path)):
total += 1
if file.startswith('0'):
dead = 0
else:
dead = 1
raw_sample = pd.read_csv(os.path.join(test_path, file), sep=',')
raw_sample = raw_sample.fillna(0)
medicine = raw_sample.iloc[:, 209:].as_matrix()
index = raw_sample.iloc[:, 3:208].as_matrix()
length = index.shape[0]
if length > max_len:
max_len = length
sample = {'patient_id': total,
'index': index,
'medicine': medicine,
'label_mor': dead,
'label_dis': num,
'task': t}
test_samples.append(sample)
index_dim = train_samples[0]['index'].shape[1]
medicine_dim = train_samples[0]['medicine'].shape[1]
train_eval_samples = {}
for sample in train_samples:
train_eval_samples[str(sample['patient_id'])] = {'label_mor': sample['label_mor'],
'label_dis': sample['label_dis']}
test_eval_samples = {}
for sample in test_samples:
test_eval_samples[str(sample['patient_id'])] = {'label_mor': sample['label_mor'],
'label_dis': sample['label_dis'],
'task': sample['task']}
return train_samples, test_samples, train_eval_samples, test_eval_samples, max_len, (index_dim, medicine_dim)
def save(filename, obj, message=None):
if message is not None:
print('Saving {}...'.format(message))
with open(filename, 'w') as fh:
json.dump(obj, fh)
def build_features(samples, data_type, max_len, dim, out_file):
print('Processing {} examples...'.format(data_type))
writer = tf.python_io.TFRecordWriter(out_file)
total = 0
meta = {}
for sample in tqdm(samples):
total += 1
index = np.zeros([max_len, dim[0]], dtype=np.float32)
medicine = np.zeros([max_len, dim[1]], dtype=np.float32)
seq_len = min(len(sample['index']), max_len)
index[:seq_len] = sample['index'][:seq_len]
medicine[:seq_len] = sample['medicine'][:seq_len]
record = tf.train.Example(features=tf.train.Features(feature={
'patient_id': tf.train.Feature(int64_list=tf.train.Int64List(value=[sample['patient_id']])),
'index': tf.train.Feature(bytes_list=tf.train.BytesList(value=[index.tostring()])),
'medicine': tf.train.Feature(bytes_list=tf.train.BytesList(value=[medicine.tostring()])),
'seq_len': tf.train.Feature(int64_list=tf.train.Int64List(value=[seq_len])),
'label_mor': tf.train.Feature(int64_list=tf.train.Int64List(value=[sample['label_mor']])),
'label_dis': tf.train.Feature(int64_list=tf.train.Int64List(value=[sample['label_dis']]))
}))
writer.write(record.SerializeToString())
print('Build {} instances of features in total'.format(total))
meta['total'] = total
writer.close()
return meta
def run_prepare(config, flags):
train_samples, dev_samples, train_eval_samples, dev_eval_samples, max_len, dim = preprocess_data(config.raw_dir)
train_meta = build_features(train_samples, 'train', config.max_len, dim, flags.train_record_file)
save(flags.train_eval_file, train_eval_samples, message='train eval')
save(flags.train_meta, train_meta, message='train meta')
del train_samples, train_eval_samples, train_meta
dev_meta = build_features(dev_samples, 'dev', config.max_len, dim, flags.dev_record_file)
save(flags.dev_eval_file, dev_eval_samples, message='dev eval')
save(flags.dev_meta, dev_meta, message='dev meta')
del dev_samples, dev_eval_samples, dev_meta
return max_len, dim