|
| 1 | +from __future__ import print_function |
| 2 | + |
| 3 | +import os |
| 4 | +import sys |
| 5 | +import logging |
| 6 | + |
| 7 | +import pandas as pd |
| 8 | +import numpy as np |
| 9 | + |
| 10 | +from sklearn.metrics import mean_squared_error |
| 11 | +from sklearn.metrics import r2_score |
| 12 | +from scipy.stats.stats import pearsonr |
| 13 | + |
| 14 | +file_path = os.path.dirname(os.path.realpath(__file__)) |
| 15 | +#lib_path = os.path.abspath(os.path.join(file_path, '..')) |
| 16 | +#sys.path.append(lib_path) |
| 17 | +lib_path2 = os.path.abspath(os.path.join(file_path, '..', '..', 'common')) |
| 18 | +sys.path.append(lib_path2) |
| 19 | + |
| 20 | +import candle |
| 21 | + |
| 22 | +logger = logging.getLogger(__name__) |
| 23 | +candle.set_parallelism_threads() |
| 24 | + |
| 25 | +additional_definitions = [ |
| 26 | +{'name':'latent_dim', |
| 27 | + 'action':'store', |
| 28 | + 'type': int, |
| 29 | + 'help':'latent dimensions'}, |
| 30 | +{'name':'residual', |
| 31 | + 'type': candle.str2bool, |
| 32 | + 'default': False, |
| 33 | + 'help':'add skip connections to the layers'}, |
| 34 | +{'name':'reduce_lr', |
| 35 | + 'type': candle.str2bool, |
| 36 | + 'default': False, |
| 37 | + 'help':'reduce learning rate on plateau'}, |
| 38 | +{'name':'warmup_lr', |
| 39 | + 'type': candle.str2bool, |
| 40 | + 'default': False, |
| 41 | + 'help':'gradually increase learning rate on start'}, |
| 42 | +{'name':'base_lr', |
| 43 | + 'type': float, |
| 44 | + 'help':'base learning rate'}, |
| 45 | +{'name':'epsilon_std', |
| 46 | + 'type': float, |
| 47 | + 'help':'epsilon std for sampling latent noise'}, |
| 48 | +{'name':'use_cp', |
| 49 | + 'type': candle.str2bool, |
| 50 | + 'default': False, |
| 51 | + 'help':'checkpoint models with best val_loss'}, |
| 52 | +#{'name':'shuffle', |
| 53 | + #'type': candle.str2bool, |
| 54 | + #'default': False, |
| 55 | + #'help':'shuffle data'}, |
| 56 | +{'name':'use_tb', |
| 57 | + 'type': candle.str2bool, |
| 58 | + 'default': False, |
| 59 | + 'help':'use tensorboard'}, |
| 60 | +{'name':'tsne', |
| 61 | + 'type': candle.str2bool, |
| 62 | + 'default': False, |
| 63 | + 'help':'generate tsne plot of the latent representation'} |
| 64 | +] |
| 65 | + |
| 66 | +required = [ |
| 67 | + 'activation', |
| 68 | + 'batch_size', |
| 69 | + 'dense', |
| 70 | + 'dropout', |
| 71 | + 'epochs', |
| 72 | + 'initialization', |
| 73 | + 'learning_rate', |
| 74 | + 'loss', |
| 75 | + 'optimizer', |
| 76 | + 'rng_seed', |
| 77 | + 'scaling', |
| 78 | + 'val_split', |
| 79 | + 'latent_dim', |
| 80 | + 'batch_normalization', |
| 81 | + 'epsilon_std', |
| 82 | + 'timeout' |
| 83 | + ] |
| 84 | + |
| 85 | +class BenchmarkAttn(candle.Benchmark): |
| 86 | + |
| 87 | + def set_locals(self): |
| 88 | + """Functionality to set variables specific for the benchmark |
| 89 | + - required: set of required parameters for the benchmark. |
| 90 | + - additional_definitions: list of dictionaries describing the additional parameters for the |
| 91 | + benchmark. |
| 92 | + """ |
| 93 | + |
| 94 | + if required is not None: |
| 95 | + self.required = set(required) |
| 96 | + if additional_definitions is not None: |
| 97 | + self.additional_definitions = additional_definitions |
| 98 | + |
| 99 | + |
| 100 | +def extension_from_parameters(params, framework=''): |
| 101 | + """Construct string for saving model with annotation of parameters""" |
| 102 | + ext = framework |
| 103 | + for i, n in enumerate(params['dense']): |
| 104 | + if n: |
| 105 | + ext += '.D{}={}'.format(i+1, n) |
| 106 | + ext += '.A={}'.format(params['activation'][0]) |
| 107 | + ext += '.B={}'.format(params['batch_size']) |
| 108 | + ext += '.E={}'.format(params['epochs']) |
| 109 | + ext += '.L={}'.format(params['latent_dim']) |
| 110 | + ext += '.LR={}'.format(params['learning_rate']) |
| 111 | + ext += '.S={}'.format(params['scaling']) |
| 112 | + |
| 113 | + if params['epsilon_std'] != 1.0: |
| 114 | + ext += '.EPS={}'.format(params['epsilon_std']) |
| 115 | + if params['dropout']: |
| 116 | + ext += '.DR={}'.format(params['dropout']) |
| 117 | + if params['batch_normalization']: |
| 118 | + ext += '.BN' |
| 119 | + if params['warmup_lr']: |
| 120 | + ext += '.WU_LR' |
| 121 | + if params['reduce_lr']: |
| 122 | + ext += '.Re_LR' |
| 123 | + if params['residual']: |
| 124 | + ext += '.Res' |
| 125 | + |
| 126 | + return ext |
| 127 | +def load_data(params, seed): |
| 128 | + |
| 129 | + # start change # |
| 130 | + if params['train_data'].endswith('h5') or params['train_data'].endswith('hdf5'): |
| 131 | + print ('processing h5 in file {}'.format(params['train_data'])) |
| 132 | + |
| 133 | + url = params['data_url'] |
| 134 | + file_train = params['train_data'] |
| 135 | + train_file = candle.get_file(file_train, url+file_train, cache_subdir='Pilot1') |
| 136 | + |
| 137 | + df_x_train_0 = pd.read_hdf(train_file, 'x_train_0').astype(np.float32) |
| 138 | + df_x_train_1 = pd.read_hdf(train_file, 'x_train_1').astype(np.float32) |
| 139 | + X_train = pd.concat([df_x_train_0, df_x_train_1], axis=1, sort=False) |
| 140 | + del df_x_train_0, df_x_train_1 |
| 141 | + |
| 142 | + df_x_test_0 = pd.read_hdf(train_file, 'x_test_0').astype(np.float32) |
| 143 | + df_x_test_1 = pd.read_hdf(train_file, 'x_test_1').astype(np.float32) |
| 144 | + X_test = pd.concat([df_x_test_0, df_x_test_1], axis=1, sort=False) |
| 145 | + del df_x_test_0, df_x_test_1 |
| 146 | + |
| 147 | + df_x_val_0 = pd.read_hdf(train_file, 'x_val_0').astype(np.float32) |
| 148 | + df_x_val_1 = pd.read_hdf(train_file, 'x_val_1').astype(np.float32) |
| 149 | + X_val = pd.concat([df_x_val_0, df_x_val_1], axis=1, sort=False) |
| 150 | + del df_x_val_0, df_x_val_1 |
| 151 | + |
| 152 | + Y_train = pd.read_hdf(train_file, 'y_train') |
| 153 | + Y_test = pd.read_hdf(train_file, 'y_test') |
| 154 | + Y_val = pd.read_hdf(train_file, 'y_val') |
| 155 | + |
| 156 | + # assumes AUC is in the third column at index 2 |
| 157 | + # df_y = df['AUC'].astype('int') |
| 158 | + # df_x = df.iloc[:,3:].astype(np.float32) |
| 159 | + |
| 160 | + # assumes dataframe has already been scaled |
| 161 | + # scaler = StandardScaler() |
| 162 | + # df_x = scaler.fit_transform(df_x) |
| 163 | + else: |
| 164 | + print ('expecting in file file suffix h5') |
| 165 | + sys.exit() |
| 166 | + |
| 167 | + |
| 168 | + print('x_train shape:', X_train.shape) |
| 169 | + print('x_test shape:', X_test.shape) |
| 170 | + |
| 171 | + return X_train, Y_train, X_val, Y_val, X_test, Y_test |
| 172 | + |
| 173 | + # start change # |
| 174 | + if train_file.endswith('h5') or train_file.endswith('hdf5'): |
| 175 | + print ('processing h5 in file {}'.format(train_file)) |
| 176 | + |
| 177 | + df_x_train_0 = pd.read_hdf(train_file, 'x_train_0').astype(np.float32) |
| 178 | + df_x_train_1 = pd.read_hdf(train_file, 'x_train_1').astype(np.float32) |
| 179 | + X_train = pd.concat([df_x_train_0, df_x_train_1], axis=1, sort=False) |
| 180 | + del df_x_train_0, df_x_train_1 |
| 181 | + |
| 182 | + df_x_test_0 = pd.read_hdf(train_file, 'x_test_0').astype(np.float32) |
| 183 | + df_x_test_1 = pd.read_hdf(train_file, 'x_test_1').astype(np.float32) |
| 184 | + X_test = pd.concat([df_x_test_0, df_x_test_1], axis=1, sort=False) |
| 185 | + del df_x_test_0, df_x_test_1 |
| 186 | + |
| 187 | + df_x_val_0 = pd.read_hdf(train_file, 'x_val_0').astype(np.float32) |
| 188 | + df_x_val_1 = pd.read_hdf(train_file, 'x_val_1').astype(np.float32) |
| 189 | + X_val = pd.concat([df_x_val_0, df_x_val_1], axis=1, sort=False) |
| 190 | + del df_x_val_0, df_x_val_1 |
| 191 | + |
| 192 | + Y_train = pd.read_hdf(train_file, 'y_train') |
| 193 | + Y_test = pd.read_hdf(train_file, 'y_test') |
| 194 | + Y_val = pd.read_hdf(train_file, 'y_val') |
| 195 | + |
| 196 | + # assumes AUC is in the third column at index 2 |
| 197 | + # df_y = df['AUC'].astype('int') |
| 198 | + # df_x = df.iloc[:,3:].astype(np.float32) |
| 199 | + |
| 200 | + # assumes dataframe has already been scaled |
| 201 | + # scaler = StandardScaler() |
| 202 | + # df_x = scaler.fit_transform(df_x) |
| 203 | + |
| 204 | + else: |
| 205 | + print ('expecting in file file suffix h5') |
| 206 | + sys.exit() |
| 207 | + |
| 208 | + |
| 209 | + print('x_train shape:', X_train.shape) |
| 210 | + print('x_test shape:', X_test.shape) |
| 211 | + |
| 212 | + return X_train, Y_train, X_val, Y_val, X_test, Y_test |
| 213 | + |
| 214 | + |
0 commit comments