def test_interp_no_extrapolate_low_bad_ordering(self): functx = asarray([2, 1, 0.8]) functy = asarray([20, 10, 8]) x = asarray([1.3,1.5,1.7]) y = interp(x, functy, functx, extrapolate_low=False) y_ans = x * 10.0 self.assert_(allclose(y, y_ans, rtol=0.05)) x = asarray([0.5,1.5,2.5]) y = interp(x, functy, functx, extrapolate_low=False) y_ans = x * 10.0 self.assert_(allclose(y, [8.,15.,25.], rtol=0.05))
def test_interp_no_extrapolate_low(self): functx = asarray([1,2]) functy = asarray([10,20]) x = asarray([1.3,1.5,1.7]) y = interp(x, functy, functx, extrapolate_low=False) y_ans = x * 10.0 self.assert_(allclose(y, y_ans, rtol=0.05)) x = asarray([0.5,1.5,2.5]) y = interp(x, functy, functx, extrapolate_low=False) y_ans = x * 10.0 self.assert_(allclose(y, [10.,15.,25.], rtol=0.05))
def test_interp(self): functx = asarray([1,2]) functy = asarray([10,20]) x = asarray([0.5,1.5,2.5]) y = interp(x, functy, functx) y_ans = x * 10.0 self.assert_(allclose(y, y_ans, rtol=0.05))
def test_interp_no_extrapolate_high_local_max(self): functx = asarray([2, 1, 3]) functy = asarray([30, 10, 20]) x = asarray([0.5, 1.5, 2.5, 3.0, 4.0]) y = interp(x, functy, functx, extrapolate_high=False) #print "y", y self.assert_(allclose(y, [0., 20., 25., 20., 20.], rtol=0.05))
def distribution(self, ground_motion, site_classes, Mw, event_periods, event_activity=None): """ Calculate the distribution of surface motion, given log of ground (bedrock) motion, site classes of events, event magnitudes, and event periods. ground_motion: ndarray [site, event, period] returns: (log_mean, log_sigma). ndarrays with same shape as ground_motion Implementation: bin all the pga and magnitudes loop over the models site classes: get the amplification this site_class get the indices of the sites that fall in the current site_class get the gm, pga, and mag for the sites in this site_class interpolate model amplification and std, then take the amp factors ... and std from the right bins in the interpolated model Dimension of final distribution = [ground_motion_samples]*[sites]*... If you sample it, it will return [samples * ground_motion_samples]*[sites]*... parameters: event_activity: Being phased out of here. Don't use. Don't clean up though, just incase we need to spawn. """ if not Mw.size == ground_motion.shape[1]: raise ValueError # bin all the pga and magnitudes event_pga_bins_indices = self._bin_indices(ground_motion[:, :, 0], self.pga_bins) event_mag_bins_indices = self._bin_indices(Mw, self.moment_magnitude_bins) # initialise mean and sigma log_amplification = zeros(ground_motion.shape, dtype=float) log_sigma = zeros(ground_motion.shape, dtype=float) for site_class in self.log_amplifications.keys(): # interpolate model amplification and std, then take the amp # factors and std from the right bins in the interpolated model log_amp = interp( event_periods, self.log_amplifications[site_class], self.periods, axis=2) log_stds = interp(event_periods, self.log_stds[site_class], self.periods, axis=2) num_periods = len(event_periods) num_events = len(Mw) num_sites = len(site_classes) try: in_zone = [s == site_class for s in site_classes] in_zone = array(in_zone) * 1 # upcast from boolean code = """ int m,n; for (int i=0; i<num_sites; ++i){ if (in_zone(i)==1){ for (int j=0; j<num_events; ++j){ m=event_mag_bins_indices(j); n=event_pga_bins_indices(i,j); for (int k=0; k<num_periods; ++k){ log_amplification(i,j,k)=log_amp(m,n,k); log_sigma(i,j,k)=log_stds(m,n,k); } } } } return_val = 0; """ try: weave.inline(code, ['event_pga_bins_indices', 'event_mag_bins_indices', 'log_amp', 'log_stds', 'log_amplification', 'log_sigma', 'num_sites', 'num_events', 'num_periods', 'in_zone'], type_converters=weave_converters.eqrm, compiler='gcc') except IOError: raise util.WeaveIOError except: print "No gcc found. Running Regolith_amplification_model.\ distribution_function in pure python" for i in range(num_sites): if site_classes[i] == site_class: for j in range(num_events): m = event_mag_bins_indices[j] n = event_pga_bins_indices[i, j] for k in range(num_periods): log_amplification[i, j, k] = log_amp[m, n, k] log_sigma[i, j, k] = log_stds[m, n, k] log_mean = log(ground_motion) + log_amplification return log_mean, log_sigma
def distribution(self, ground_motion, site_classes, Mw, event_periods, event_activity=None): """ Calculate the distribution of surface motion, given log of ground (bedrock) motion, site classes of events, event magnitudes, and event periods. ground_motion: ndarray [site, event, period] returns: (log_mean, log_sigma). ndarrays with same shape as ground_motion Implementation: bin all the pga and magnitudes loop over the models site classes: get the amplification this site_class get the indices of the sites that fall in the current site_class get the gm, pga, and mag for the sites in this site_class interpolate model amplification and std, then take the amp factors ... and std from the right bins in the interpolated model Dimension of final distribution = [ground_motion_samples]*[sites]*... If you sample it, it will return [samples * ground_motion_samples]*[sites]*... parameters: event_activity: Being phased out of here. Don't use. Don't clean up though, just incase we need to spawn. """ if not Mw.size == ground_motion.shape[1]: raise ValueError # bin all the pga and magnitudes event_pga_bins_indices = self._bin_indices(ground_motion[:, :, 0], self.pga_bins) event_mag_bins_indices = self._bin_indices(Mw, self.moment_magnitude_bins) # initialise mean and sigma log_amplification = zeros(ground_motion.shape, dtype=float) log_sigma = zeros(ground_motion.shape, dtype=float) for site_class in self.log_amplifications.keys(): # interpolate model amplification and std, then take the amp # factors and std from the right bins in the interpolated model log_amp = interp(event_periods, self.log_amplifications[site_class], self.periods, axis=2) log_stds = interp(event_periods, self.log_stds[site_class], self.periods, axis=2) num_periods = len(event_periods) num_events = len(Mw) num_sites = len(site_classes) try: in_zone = [s == site_class for s in site_classes] in_zone = array(in_zone) * 1 # upcast from boolean code = """ int m,n; for (int i=0; i<num_sites; ++i){ if (in_zone(i)==1){ for (int j=0; j<num_events; ++j){ m=event_mag_bins_indices(j); n=event_pga_bins_indices(i,j); for (int k=0; k<num_periods; ++k){ log_amplification(i,j,k)=log_amp(m,n,k); log_sigma(i,j,k)=log_stds(m,n,k); } } } } return_val = 0; """ try: weave.inline(code, [ 'event_pga_bins_indices', 'event_mag_bins_indices', 'log_amp', 'log_stds', 'log_amplification', 'log_sigma', 'num_sites', 'num_events', 'num_periods', 'in_zone' ], type_converters=weave_converters.eqrm, compiler='gcc') except IOError: raise util.WeaveIOError except: print "No gcc found. Running Regolith_amplification_model.\ distribution_function in pure python" for i in range(num_sites): if site_classes[i] == site_class: for j in range(num_events): m = event_mag_bins_indices[j] n = event_pga_bins_indices[i, j] for k in range(num_periods): log_amplification[i, j, k] = log_amp[m, n, k] log_sigma[i, j, k] = log_stds[m, n, k] log_mean = log(ground_motion) + log_amplification return log_mean, log_sigma