|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +"""Module for implementing the Artificial Neural Net model for clipping, as |
| 5 | +developed by Kleckner et al. This code is based on Xavier Bellagamba's python |
| 6 | +NN implementation of "A neural network for automated quality screening of |
| 7 | +ground motion records from small magnitude earthquakes" |
| 8 | +DOI: 10.1193/122118EQS292M |
| 9 | +""" |
| 10 | + |
| 11 | +import csv |
| 12 | +import numpy as np |
| 13 | +import pkg_resources |
| 14 | +import os |
| 15 | + |
| 16 | +# Path to model data |
| 17 | +NN_PATH = os.path.join('data', 'nn_clipping') |
| 18 | +NN_PATH = pkg_resources.resource_filename('gmprocess', NN_PATH) |
| 19 | + |
| 20 | + |
| 21 | +class clipNet(): |
| 22 | + ''' |
| 23 | + Class allowing the instantiation and use of simple (1 or 2 layers) |
| 24 | + neural networks |
| 25 | + ''' |
| 26 | + |
| 27 | + def __init__(self): |
| 28 | + ''' |
| 29 | + Instantiate an empty neural network (no weights, functions, or |
| 30 | + biases loaded |
| 31 | + ''' |
| 32 | + self.n_input = 0 |
| 33 | + self.n_neuron_H1 = 0 |
| 34 | + self.n_neuron_H2 = -1 |
| 35 | + self.n_output = 0 |
| 36 | + self.activation_H1 = 'NA' |
| 37 | + self.activation_H2 = 'NA' |
| 38 | + self.activation_output = 'NA' |
| 39 | + self.w_H1 = [] |
| 40 | + self.w_H2 = [] |
| 41 | + self.b_H1 = [] |
| 42 | + self.b_H2 = [] |
| 43 | + self.w_output = [] |
| 44 | + self.b_output = [] |
| 45 | + |
| 46 | + data_path = os.path.join(NN_PATH, 'masterF.txt') |
| 47 | + with open(data_path) as masterF: |
| 48 | + readCSV = csv.reader(masterF) |
| 49 | + for row in readCSV: |
| 50 | + if len(row) == 7: |
| 51 | + self.n_input = int(row[0]) |
| 52 | + self.n_neuron_H1 = int(row[1]) |
| 53 | + # self.n_neuron_H2 = int(row[3]) |
| 54 | + self.n_output = int(row[5]) |
| 55 | + self.activation_H1 = row[2] |
| 56 | + # self.activation_H2 = row[4] |
| 57 | + self.activation_output = row[6] |
| 58 | + elif len(row) == 5: |
| 59 | + self.n_input = int(row[0]) |
| 60 | + self.n_neuron_H1 = int(row[1]) |
| 61 | + self.n_output = int(row[3]) |
| 62 | + self.activation_H1 = row[2] |
| 63 | + self.activation_output = row[4] |
| 64 | + |
| 65 | + masterF.close() |
| 66 | + |
| 67 | + # Load weights and biases |
| 68 | + # Weights first hidden layer |
| 69 | + data_path = os.path.join(NN_PATH, 'weight_1.csv') |
| 70 | + self.w_H1 = np.asarray(loadCSV(data_path)) |
| 71 | + |
| 72 | + # Biases first hidden layer |
| 73 | + data_path = os.path.join(NN_PATH, 'bias_1.csv') |
| 74 | + self.b_H1 = np.asarray(loadCSV(data_path)) |
| 75 | + |
| 76 | + # Weights output layer |
| 77 | + data_path = os.path.join(NN_PATH, 'weight_output.csv') |
| 78 | + self.w_output = np.asarray(loadCSV(data_path)) |
| 79 | + |
| 80 | + # Biases output layer |
| 81 | + data_path = os.path.join(NN_PATH, 'bias_output.csv') |
| 82 | + self.b_output = np.asarray(loadCSV(data_path)) |
| 83 | + |
| 84 | + # Second hidden layer |
| 85 | + if self.n_neuron_H2 != -1: |
| 86 | + # Weights second hidden layer |
| 87 | + data_path = os.path.join(NN_PATH, 'weight_2.csv') |
| 88 | + self.w_H2 = np.asarray(loadCSV(data_path)) |
| 89 | + |
| 90 | + # Biases second hidden layer |
| 91 | + data_path = os.path.join(NN_PATH, 'bias_2.csv') |
| 92 | + self.b_H2 = np.asarray(loadCSV(data_path)) |
| 93 | + |
| 94 | + def evaluate(self, v_input): |
| 95 | + ''' |
| 96 | + Use a populated neural network (i.e. from the input, returns the |
| 97 | + classification score or the regression result). |
| 98 | +
|
| 99 | + Args: |
| 100 | + v_input (list or np.array): |
| 101 | + Values to correspond to the following paramters: mag, dist, 6M |
| 102 | + amplitude check, histogram check, ping check. |
| 103 | +
|
| 104 | + Returns: |
| 105 | + np.array: numpy array containing the results. |
| 106 | + ''' |
| 107 | + # Transform input if required |
| 108 | + if isinstance(v_input, list): |
| 109 | + v_input = np.asarray(v_input) |
| 110 | + |
| 111 | + t1 = np.array([8.8, 445.8965938, 1., 1., 1.]) |
| 112 | + t2 = np.array([4, 0.68681514, 0., 0., 0.]) |
| 113 | + t3 = np.array([0., 0., 0., 0., 0.]) |
| 114 | + v_input = 2.0 / (t1 - t2) * (v_input - t3) |
| 115 | + |
| 116 | + v_inter = np.array([]) |
| 117 | + |
| 118 | + # First layer |
| 119 | + if self.activation_H1 == 'sigmoid': |
| 120 | + v_inter = sigmoid(np.dot(v_input.T, self.w_H1) + self.b_H1) |
| 121 | + elif self.activation_H1 == 'tanh': |
| 122 | + v_inter = tanh(np.dot(v_input.T, self.w_H1) + self.b_H1) |
| 123 | + elif self.activation_H1 == 'relu': |
| 124 | + v_inter = relu(np.dot(v_input.T, self.w_H1) + self.b_H1) |
| 125 | + else: |
| 126 | + v_inter = relu(np.dot(v_input.T, self.w_H1) + self.b_H1.T) |
| 127 | + |
| 128 | + # If second layer exist |
| 129 | + if self.n_neuron_H2 != -1: |
| 130 | + if self.activation_H2 == 'sigmoid': |
| 131 | + v_inter = sigmoid(np.dot(v_inter, self.w_H2) + self.b_H2) |
| 132 | + elif self.activation_H2 == 'tanh': |
| 133 | + v_inter = tanh(np.dot(v_inter, self.w_H2) + self.b_H2) |
| 134 | + else: |
| 135 | + v_inter = np.dot(v_inter, self.w_H2) + self.b_H2 |
| 136 | + |
| 137 | + # Final layer |
| 138 | + if self.activation_output == 'sigmoid': |
| 139 | + v_inter = sigmoid(np.dot(v_inter, self.w_output) + self.b_output) |
| 140 | + elif self.activation_output == 'tanh': |
| 141 | + v_inter = tanh(np.dot(v_inter, self.w_output) + self.b_output) |
| 142 | + else: |
| 143 | + v_inter = sigmoid(np.dot(v_inter, self.w_output) + self.b_output) |
| 144 | + |
| 145 | + return v_inter |
| 146 | + |
| 147 | + |
| 148 | +def loadCSV(data_path, row_ignore=0, col_ignore=0): |
| 149 | + ''' |
| 150 | + Load csv files from a given path and returns a list of list. |
| 151 | + For all imported data, check if is a number. If so, returns a |
| 152 | + float. If not, returns a string. |
| 153 | +
|
| 154 | + Args: |
| 155 | + data_path (string): |
| 156 | + path to the csv to load. |
| 157 | + row_ignore (int): |
| 158 | + number of rows to ignore. |
| 159 | + col_ignore (int): |
| 160 | + number of columns to ignore. |
| 161 | +
|
| 162 | + Returns: |
| 163 | + list of list: containing the data from the csv |
| 164 | + ''' |
| 165 | + |
| 166 | + M = [] |
| 167 | + with open(data_path) as csvfile: |
| 168 | + readCSV = csv.reader(csvfile) |
| 169 | + |
| 170 | + # Skip header |
| 171 | + for i in range(row_ignore): |
| 172 | + next(csvfile) |
| 173 | + |
| 174 | + for row in readCSV: |
| 175 | + # Input vector |
| 176 | + single_line = [] |
| 177 | + for i in range(col_ignore, len(row)): |
| 178 | + if isNumber(row[i]): |
| 179 | + single_line.append(float(row[i])) |
| 180 | + else: |
| 181 | + single_line.append(row[i]) |
| 182 | + M.append(single_line) |
| 183 | + |
| 184 | + return M |
| 185 | + |
| 186 | + |
| 187 | +def sigmoid(v_input): |
| 188 | + ''' |
| 189 | + Performs a sigmoid operation on the input (1/(e(-x)+1)) |
| 190 | +
|
| 191 | + Args: |
| 192 | + v_input (float): |
| 193 | + a number defined on R (real). |
| 194 | +
|
| 195 | + Returns: |
| 196 | + float: sigmoid result (a number between 0 and 1). |
| 197 | + ''' |
| 198 | + v_act = [] |
| 199 | + for x in v_input: |
| 200 | + v_act.append(1. / (1 + np.exp(-x))) |
| 201 | + return v_act |
| 202 | + |
| 203 | + |
| 204 | +def tanh(v_input): |
| 205 | + ''' |
| 206 | + Performs a hyperbolic tangent operation on the input (2/(e(2x)+1)) |
| 207 | +
|
| 208 | + Args: |
| 209 | + v_input (float): |
| 210 | + a number defined on R (real). |
| 211 | +
|
| 212 | + Returns: |
| 213 | + float: tanh result (a number between -1 and 1). |
| 214 | + ''' |
| 215 | + v_act = [] |
| 216 | + for x in v_input: |
| 217 | + v_act.append(np.tanh(x)) |
| 218 | + return v_act |
| 219 | + |
| 220 | + |
| 221 | +def relu(v_input): |
| 222 | + ''' |
| 223 | + Performs a hyperbolic tangent operation on the input (2/(e(2x)+1)) |
| 224 | +
|
| 225 | + Args: |
| 226 | + v_input (float): |
| 227 | + a number defined on R (real). |
| 228 | +
|
| 229 | + Returns: |
| 230 | + float: tanh result (a number between -1 and 1). |
| 231 | + ''' |
| 232 | + v_act = [] |
| 233 | + for x in v_input: |
| 234 | + v_act.append(np.maximum(0.0, x)) |
| 235 | + return v_act |
| 236 | + |
| 237 | + |
| 238 | +def isNumber(s): |
| 239 | + ''' |
| 240 | + Check if given input is a number. |
| 241 | +
|
| 242 | + Args: |
| 243 | + s (any type): |
| 244 | + Data to test. |
| 245 | +
|
| 246 | + Returns: |
| 247 | + bool: True if is a number, False if isn't |
| 248 | + ''' |
| 249 | + try: |
| 250 | + float(s) |
| 251 | + return True |
| 252 | + |
| 253 | + except ValueError: |
| 254 | + return False |
0 commit comments