The following are code examples for showing how to use . They are extracted from open source Python projects. You can vote up the examples you like or vote down the exmaples you don’t like. You can also save this page to your account.
Example 1
def test_minmax_func(self): # Tests minimum and maximum. (x, y, a10, m1, m2, xm, ym, z, zm, xf) = self.d # max doesn't work if shaped xr = np.ravel(x) xmr = ravel(xm) # following are true because of careful selection of data assert_equal(max(xr), maximum(xmr)) assert_equal(min(xr), minimum(xmr)) assert_equal(minimum([1, 2, 3], [4, 0, 9]), [1, 0, 3]) assert_equal(maximum([1, 2, 3], [4, 0, 9]), [4, 2, 9]) x = arange(5) y = arange(5) - 2 x[3] = masked y[0] = masked assert_equal(minimum(x, y), where(less(x, y), x, y)) assert_equal(maximum(x, y), where(greater(x, y), x, y)) assert_(minimum(x) == 0) assert_(maximum(x) == 4) x = arange(4).reshape(2, 2) x[-1, -1] = masked assert_equal(maximum(x), 2)
Example 2
def ntron_pulse(amplitude=1.0, rise_time=80e-12, hold_time=170e-12, fall_time=1.0e-9, sample_rate=12e9): delay = 2.0e-9 # Wait a few TCs for the rising edge duration = delay + hold_time + 6.0*fall_time # Wait 6 TCs for the slow decay pulse_points = int(duration*sample_rate) if pulse_points < 320: duration = 319/sample_rate # times = np.arange(0, duration, 1/sample_rate) times = np.linspace(0, duration, 320) else: pulse_points = 64*np.ceil(pulse_points/64.0) duration = (pulse_points-1)/sample_rate # times = np.arange(0, duration, 1/sample_rate) times = np.linspace(0, duration, pulse_points) rise_mask = np.less(times, delay) hold_mask = np.less(times, delay + hold_time)*np.greater_equal(times, delay) fall_mask = np.greater_equal(times, delay + hold_time) wf = rise_mask*np.exp((times-delay)/rise_time) wf += hold_mask wf += fall_mask*np.exp(-(times-delay-hold_time)/fall_time) return amplitude*wf
Example 3
def ntron_pulse(amplitude=1.0, rise_time=80e-12, hold_time=170e-12, fall_time=1.0e-9, sample_rate=12e9): delay = 2.0e-9 # Wait a few TCs for the rising edge duration = delay + hold_time + 6.0*fall_time # Wait 6 TCs for the slow decay pulse_points = int(duration*sample_rate) if pulse_points < 320: duration = 319/sample_rate # times = np.arange(0, duration, 1/sample_rate) times = np.linspace(0, duration, 320) else: pulse_points = 64*np.ceil(pulse_points/64.0) duration = (pulse_points-1)/sample_rate # times = np.arange(0, duration, 1/sample_rate) times = np.linspace(0, duration, pulse_points) rise_mask = np.less(times, delay) hold_mask = np.less(times, delay + hold_time)*np.greater_equal(times, delay) fall_mask = np.greater_equal(times, delay + hold_time) wf = rise_mask*np.exp((times-delay)/rise_time) wf += hold_mask wf += fall_mask*np.exp(-(times-delay-hold_time)/fall_time) return amplitude*wf
Example 4
def _reset(self): """Resets wait counter and cooldown counter. """ if self.mode not in ['auto', 'min', 'max']: warnings.warn('Learning Rate Plateau Reducing mode %s is unknown, ' 'fallback to auto mode.' % (self.mode), RuntimeWarning) self.mode = 'auto' if (self.mode == 'min' or (self.mode == 'auto' and 'acc' not in self.monitor)): self.monitor_op = lambda a, b: np.less(a, b - self.epsilon) self.best = np.Inf else: self.monitor_op = lambda a, b: np.greater(a, b + self.epsilon) self.best = -np.Inf self.cooldown_counter = 0 self.wait = 0 self.lr_epsilon = self.min_lr * 1e-4
Example 5
def test_minmax_func(self): # Tests minimum and maximum. (x, y, a10, m1, m2, xm, ym, z, zm, xf) = self.d # max doesn't work if shaped xr = np.ravel(x) xmr = ravel(xm) # following are true because of careful selection of data assert_equal(max(xr), maximum(xmr)) assert_equal(min(xr), minimum(xmr)) assert_equal(minimum([1, 2, 3], [4, 0, 9]), [1, 0, 3]) assert_equal(maximum([1, 2, 3], [4, 0, 9]), [4, 2, 9]) x = arange(5) y = arange(5) - 2 x[3] = masked y[0] = masked assert_equal(minimum(x, y), where(less(x, y), x, y)) assert_equal(maximum(x, y), where(greater(x, y), x, y)) assert_(minimum(x) == 0) assert_(maximum(x) == 4) x = arange(4).reshape(2, 2) x[-1, -1] = masked assert_equal(maximum(x), 2)
Example 6
def get_local_minima(x, y): """ This function ... :param x: :param y: :return: """ m = argrelextrema(y, np.less)[0].tolist() # Find the indx of the absolute minimum (should also be included, is not for example when it is at the edge) index = np.argmin(y) if index not in m: m.append(index) x_minima = [x[i] for i in m] y_minima = [y[i] for i in m] return x_minima, y_minima # -----------------------------------------------------------------
Example 7
def get_local_minima(x, y): """ This function ... :param x: :param y: :return: """ m = argrelextrema(y, np.less)[0].tolist() # Find the indx of the absolute minimum (should also be included, is not for example when it is at the edge) index = np.argmin(y) if index not in m: m.append(index) x_minima = [x[i] for i in m] y_minima = [y[i] for i in m] return x_minima, y_minima # -----------------------------------------------------------------
Example 8
def _reset(self): """Resets wait counter and cooldown counter. """ if self.mode not in ['auto', 'min', 'max']: logging.warning('Learning Rate Plateau Reducing mode %s is unknown, ' 'fallback to auto mode.' % (self.mode)) self.mode = 'auto' if (self.mode == 'min' or (self.mode == 'auto' and 'acc' not in self.monitor)): self.monitor_op = lambda a, b: np.less(a, b - self.epsilon) self.best = np.Inf else: self.monitor_op = lambda a, b: np.greater(a, b + self.epsilon) self.best = -np.Inf self.cooldown_counter = 0 self.wait = 0 self.lr_epsilon = self.min_lr * 1e-4
Example 9
def compute_by_noise_pow(self, signal, n_pow): s_spec = np.fft.fftpack.fft(signal * self._window) s_amp = np.absolute(s_spec) s_phase = np.angle(s_spec) gamma = self._calc_aposteriori_snr(s_amp, n_pow) xi = self._calc_apriori_snr(gamma) self._prevGamma = gamma nu = gamma * xi / (1.0 + xi) self._G = (self._gamma15 * np.sqrt(nu) / gamma) * np.exp(-nu / 2.0) *\ ((1.0 + nu) * spc.i0(nu / 2.0) + nu * spc.i1(nu / 2.0)) idx = np.less(s_amp ** 2.0, n_pow) self._G[idx] = self._constant idx = np.isnan(self._G) + np.isinf(self._G) self._G[idx] = xi[idx] / (xi[idx] + 1.0) idx = np.isnan(self._G) + np.isinf(self._G) self._G[idx] = self._constant self._G = np.maximum(self._G, 0.0) amp = self._G * s_amp amp = np.maximum(amp, 0.0) amp2 = self._ratio * amp + (1.0 - self._ratio) * s_amp self._prevAmp = amp spec = amp2 * np.exp(s_phase * 1j) return np.real(np.fft.fftpack.ifft(spec))
Example 10
def compute_by_noise_pow(self, signal, n_pow): s_spec = np.fft.fftpack.fft(signal * self._window) s_amp = np.absolute(s_spec) s_phase = np.angle(s_spec) gamma = self._calc_aposteriori_snr(s_amp, n_pow) xi = self._calc_apriori_snr(gamma) # xi = self._calc_apriori_snr2(gamma,n_pow) self._prevGamma = gamma nu = gamma * xi / (1.0 + xi) self._G = xi / (1.0 + xi) * np.exp(0.5 * spc.exp1(nu)) idx = np.less(s_amp ** 2.0, n_pow) self._G[idx] = self._constant idx = np.isnan(self._G) + np.isinf(self._G) self._G[idx] = xi[idx] / (xi[idx] + 1.0) idx = np.isnan(self._G) + np.isinf(self._G) self._G[idx] = self._constant self._G = np.maximum(self._G, 0.0) amp = self._G * s_amp amp = np.maximum(amp, 0.0) amp2 = self._ratio * amp + (1.0 - self._ratio) * s_amp self._prevAmp = amp spec = amp2 * np.exp(s_phase * 1j) return np.real(np.fft.fftpack.ifft(spec))
Example 11
def compute_by_noise_pow(self, signal, n_pow): s_spec = np.fft.fftpack.fft(signal * self._window) s_amp = np.absolute(s_spec) s_phase = np.angle(s_spec) gamma = self._calc_aposteriori_snr(s_amp, n_pow) # xi = self._calc_apriori_snr2(gamma,n_pow) xi = self._calc_apriori_snr(gamma) self._prevGamma = gamma u = 0.5 - self._mu / (4.0 * np.sqrt(gamma * xi)) self._G = u + np.sqrt(u ** 2.0 + self._tau / (gamma * 2.0)) idx = np.less(s_amp ** 2.0, n_pow) self._G[idx] = self._constant idx = np.isnan(self._G) + np.isinf(self._G) self._G[idx] = xi[idx] / (xi[idx] + 1.0) idx = np.isnan(self._G) + np.isinf(self._G) self._G[idx] = self._constant self._G = np.maximum(self._G, 0.0) amp = self._G * s_amp amp = np.maximum(amp, 0.0) amp2 = self._ratio * amp + (1.0 - self._ratio) * s_amp self._prevAmp = amp spec = amp2 * np.exp(s_phase * 1j) return np.real(np.fft.fftpack.ifft(spec))
Example 12
def iterate_until_button_press(buttons, game_state, text_ending_place, text_starting_place): # while a button was not clicked this method checks if mouse is in the button and if it is # changes its colour button_clicked = 0 while button_clicked == 0: pygame.display.update() user_events = event.events() # the first button is the title which is unclickable, thus iterating from 1 to len(buttons) for num in range(1, len(buttons)): if np.all((np.less(text_starting_place[num] - config.menu_spacing, user_events["mouse_pos"]), np.greater(text_ending_place[num] + config.menu_spacing, user_events["mouse_pos"]))): if user_events["clicked"]: button_clicked = num else: game_state.canvas.surface.blit( buttons[num][1], text_starting_place[num]) else: game_state.canvas.surface.blit( buttons[num][0], text_starting_place[num]) if user_events["closed"] or user_events["quit_to_main_menu"]: button_clicked = len(buttons)-1 return button_clicked
Example 13
def test_NotImplemented_not_returned(self): # See gh-5964 and gh-2091. Some of these functions are not operator # related and were fixed for other reasons in the past. binary_funcs = [ np.power, np.add, np.subtract, np.multiply, np.divide, np.true_divide, np.floor_divide, np.bitwise_and, np.bitwise_or, np.bitwise_xor, np.left_shift, np.right_shift, np.fmax, np.fmin, np.fmod, np.hypot, np.logaddexp, np.logaddexp2, np.logical_and, np.logical_or, np.logical_xor, np.maximum, np.minimum, np.mod ] # These functions still return NotImplemented. Will be fixed in # future. # bad = [np.greater, np.greater_equal, np.less, np.less_equal, np.not_equal] a = np.array('1') b = 1 for f in binary_funcs: assert_raises(TypeError, f, a, b)
Example 14
def test_minmax_func(self): # Tests minimum and maximum. (x, y, a10, m1, m2, xm, ym, z, zm, xf) = self.d # max doesn't work if shaped xr = np.ravel(x) xmr = ravel(xm) # following are true because of careful selection of data assert_equal(max(xr), maximum(xmr)) assert_equal(min(xr), minimum(xmr)) assert_equal(minimum([1, 2, 3], [4, 0, 9]), [1, 0, 3]) assert_equal(maximum([1, 2, 3], [4, 0, 9]), [4, 2, 9]) x = arange(5) y = arange(5) - 2 x[3] = masked y[0] = masked assert_equal(minimum(x, y), where(less(x, y), x, y)) assert_equal(maximum(x, y), where(greater(x, y), x, y)) assert_(minimum(x) == 0) assert_(maximum(x) == 4) x = arange(4).reshape(2, 2) x[-1, -1] = masked assert_equal(maximum(x), 2)
Example 15
def test_NotImplemented_not_returned(self): # See gh-5964 and gh-2091. Some of these functions are not operator # related and were fixed for other reasons in the past. binary_funcs = [ np.power, np.add, np.subtract, np.multiply, np.divide, np.true_divide, np.floor_divide, np.bitwise_and, np.bitwise_or, np.bitwise_xor, np.left_shift, np.right_shift, np.fmax, np.fmin, np.fmod, np.hypot, np.logaddexp, np.logaddexp2, np.logical_and, np.logical_or, np.logical_xor, np.maximum, np.minimum, np.mod ] # These functions still return NotImplemented. Will be fixed in # future. # bad = [np.greater, np.greater_equal, np.less, np.less_equal, np.not_equal] a = np.array('1') b = 1 for f in binary_funcs: assert_raises(TypeError, f, a, b)
Example 16
def test_minmax_func(self): # Tests minimum and maximum. (x, y, a10, m1, m2, xm, ym, z, zm, xf) = self.d # max doesn't work if shaped xr = np.ravel(x) xmr = ravel(xm) # following are true because of careful selection of data assert_equal(max(xr), maximum(xmr)) assert_equal(min(xr), minimum(xmr)) assert_equal(minimum([1, 2, 3], [4, 0, 9]), [1, 0, 3]) assert_equal(maximum([1, 2, 3], [4, 0, 9]), [4, 2, 9]) x = arange(5) y = arange(5) - 2 x[3] = masked y[0] = masked assert_equal(minimum(x, y), where(less(x, y), x, y)) assert_equal(maximum(x, y), where(greater(x, y), x, y)) assert_(minimum(x) == 0) assert_(maximum(x) == 4) x = arange(4).reshape(2, 2) x[-1, -1] = masked assert_equal(maximum(x), 2)
Example 17
def __init__(self, monitor='val_loss', patience=0, verbose=0, mode='auto'): super(Callback, self).__init__() self.monitor = monitor self.patience = patience self.verbose = verbose self.wait = 0 self.best_epoch = 0 if mode == 'min': self.monitor_op = np.less self.best = np.Inf elif mode == 'max': self.monitor_op = np.greater self.best = -np.Inf else: if 'acc' in self.monitor: self.monitor_op = np.greater self.best = -np.Inf else: self.monitor_op = np.less self.best = np.Inf
Example 18
def __init__(self, monitor='val_loss', mode='auto', verbose=0): super(BestWeight, self).__init__() self.monitor = monitor self.mode = mode self.best_weights = None self.verbose = verbose if mode == 'min': self.monitor_op = np.less self.best = np.Inf elif mode == 'max': self.monitor_op = np.greater self.best = -np.Inf else: if 'acc' in self.monitor: self.monitor_op = np.greater self.best = -np.Inf else: self.monitor_op = np.less self.best = np.Inf
Example 19
def test_NotImplemented_not_returned(self): # See gh-5964 and gh-2091. Some of these functions are not operator # related and were fixed for other reasons in the past. binary_funcs = [ np.power, np.add, np.subtract, np.multiply, np.divide, np.true_divide, np.floor_divide, np.bitwise_and, np.bitwise_or, np.bitwise_xor, np.left_shift, np.right_shift, np.fmax, np.fmin, np.fmod, np.hypot, np.logaddexp, np.logaddexp2, np.logical_and, np.logical_or, np.logical_xor, np.maximum, np.minimum, np.mod ] # These functions still return NotImplemented. Will be fixed in # future. # bad = [np.greater, np.greater_equal, np.less, np.less_equal, np.not_equal] a = np.array('1') b = 1 for f in binary_funcs: assert_raises(TypeError, f, a, b)
Example 20
def test_minmax_func(self): # Tests minimum and maximum. (x, y, a10, m1, m2, xm, ym, z, zm, xf) = self.d # max doesn't work if shaped xr = np.ravel(x) xmr = ravel(xm) # following are true because of careful selection of data assert_equal(max(xr), maximum(xmr)) assert_equal(min(xr), minimum(xmr)) assert_equal(minimum([1, 2, 3], [4, 0, 9]), [1, 0, 3]) assert_equal(maximum([1, 2, 3], [4, 0, 9]), [4, 2, 9]) x = arange(5) y = arange(5) - 2 x[3] = masked y[0] = masked assert_equal(minimum(x, y), where(less(x, y), x, y)) assert_equal(maximum(x, y), where(greater(x, y), x, y)) assert_(minimum(x) == 0) assert_(maximum(x) == 4) x = arange(4).reshape(2, 2) x[-1, -1] = masked assert_equal(maximum(x), 2)
Example 21
def _reset(self): """Resets wait counter and cooldown counter. """ if self.mode not in ['auto', 'min', 'max']: warnings.warn('Learning Rate Plateau Reducing mode %s is unknown, ' 'fallback to auto mode.' % (self.mode), RuntimeWarning) self.mode = 'auto' if (self.mode == 'min' or (self.mode == 'auto' and 'acc' not in self.monitor)): self.monitor_op = lambda a, b: np.less(a, b - self.epsilon) self.best = np.Inf else: self.monitor_op = lambda a, b: np.greater(a, b + self.epsilon) self.best = -np.Inf self.cooldown_counter = 0 self.wait = 0 self.lr_epsilon = self.min_lr * 1e-4
Example 22
def test_NotImplemented_not_returned(self): # See gh-5964 and gh-2091. Some of these functions are not operator # related and were fixed for other reasons in the past. binary_funcs = [ np.power, np.add, np.subtract, np.multiply, np.divide, np.true_divide, np.floor_divide, np.bitwise_and, np.bitwise_or, np.bitwise_xor, np.left_shift, np.right_shift, np.fmax, np.fmin, np.fmod, np.hypot, np.logaddexp, np.logaddexp2, np.logical_and, np.logical_or, np.logical_xor, np.maximum, np.minimum, np.mod ] # These functions still return NotImplemented. Will be fixed in # future. # bad = [np.greater, np.greater_equal, np.less, np.less_equal, np.not_equal] a = np.array('1') b = 1 for f in binary_funcs: assert_raises(TypeError, f, a, b)
Example 23
def test_minmax_func(self): # Tests minimum and maximum. (x, y, a10, m1, m2, xm, ym, z, zm, xf) = self.d # max doesn't work if shaped xr = np.ravel(x) xmr = ravel(xm) # following are true because of careful selection of data assert_equal(max(xr), maximum(xmr)) assert_equal(min(xr), minimum(xmr)) assert_equal(minimum([1, 2, 3], [4, 0, 9]), [1, 0, 3]) assert_equal(maximum([1, 2, 3], [4, 0, 9]), [4, 2, 9]) x = arange(5) y = arange(5) - 2 x[3] = masked y[0] = masked assert_equal(minimum(x, y), where(less(x, y), x, y)) assert_equal(maximum(x, y), where(greater(x, y), x, y)) assert_(minimum(x) == 0) assert_(maximum(x) == 4) x = arange(4).reshape(2, 2) x[-1, -1] = masked assert_equal(maximum(x), 2)
Example 24
def _reset(self): """Resets wait counter and cooldown counter. """ if self.mode not in ['auto', 'min', 'max']: warnings.warn('Learning Rate Plateau Reducing mode %s is unknown, ' 'fallback to auto mode.' % (self.mode), RuntimeWarning) self.mode = 'auto' if (self.mode == 'min' or (self.mode == 'auto' and 'acc' not in self.monitor)): self.monitor_op = lambda a, b: np.less(a, b - self.epsilon) self.best = np.Inf else: self.monitor_op = lambda a, b: np.greater(a, b + self.epsilon) self.best = -np.Inf self.cooldown_counter = 0 self.wait = 0 self.lr_epsilon = self.min_lr * 1e-4
Example 25
def plot_kde(data): bw = 1.06 * st.stdev(data) / (len(data) ** .2) kde = KernelDensity(kernel='gaussian', bandwidth=bw).fit( np.array(data).reshape(-1, 1)) s = np.linspace(0, 1) e = kde.score_samples(s.reshape(-1, 1)) plt.plot(s, e) mi, ma = argrelextrema(e, np.less)[0], argrelextrema(e, np.greater)[0] logger.info("Minima: %s" % s[mi]) logger.info("Maxima: %s" % s[ma]) plt.plot(s[:mi[0] + 1], e[:mi[0] + 1], 'r', s[mi[0]:mi[1] + 1], e[mi[0]:mi[1] + 1], 'g', s[mi[1]:], e[mi[1]:], 'b', s[ma], e[ma], 'go', s[mi], e[mi], 'ro') plt.xlabel('Probability')
Example 26
def on_epoch_end(self, epoch, logs={}): current = self.monitor(self.previous_weights, self.model.get_weights()) self.previous_weights = self.model.get_weights() if current is None: warnings.warn('Early stopping requires %s available!' % (self.monitor), RuntimeWarning) if np.less(current, self.threshold_value): if current == 0: self.model.stop_training = True if self.verbose > 0: print('Epoch %05d: early stopping: ratio weights = 0' % (epoch)) elif self.wait >= self.patience: if self.verbose > 0: print('Epoch %05d: early stopping: ratio weights below %.4f' % (epoch, self.threshold_value)) self.model.stop_training = True self.wait += 1 else: self.wait = 0
Example 27
def atmin(a,lowerlimit=None,dimension=None,inclusive=1): """ Returns the minimum value of a, along dimension, including only values less than (or equal to, if inclusive=1) lowerlimit. If the limit is set to None, all values in the array are used. Usage: atmin(a,lowerlimit=None,dimension=None,inclusive=1) """ if inclusive: lowerfcn = N.greater else: lowerfcn = N.greater_equal if dimension == None: a = N.ravel(a) dimension = 0 if lowerlimit == None: lowerlimit = N.minimum.reduce(N.ravel(a))-11 biggest = N.maximum.reduce(N.ravel(a)) ta = N.where(lowerfcn(a,lowerlimit),a,biggest) return N.minimum.reduce(ta,dimension)
Example 28
def atmax(a,upperlimit,dimension=None,inclusive=1): """ Returns the maximum value of a, along dimension, including only values greater than (or equal to, if inclusive=1) upperlimit. If the limit is set to None, a limit larger than the max value in the array is used. Usage: atmax(a,upperlimit,dimension=None,inclusive=1) """ if inclusive: upperfcn = N.less else: upperfcn = N.less_equal if dimension == None: a = N.ravel(a) dimension = 0 if upperlimit == None: upperlimit = N.maximum.reduce(N.ravel(a))+1 smallest = N.minimum.reduce(N.ravel(a)) ta = N.where(upperfcn(a,upperlimit),a,smallest) return N.maximum.reduce(ta,dimension)
Example 29
def _evoked_from_epoch_data(self, data, info, picks, n_events, kind): """Helper to create an evoked object from epoch data""" info = deepcopy(info) evoked = EvokedArray(data, info, tmin=self.times[0], comment=self.name, nave=n_events, kind=kind, verbose=self.verbose) # XXX: above constructor doesn't recreate the times object precisely evoked.times = self.times.copy() # pick channels if picks is None: picks = _pick_data_channels(evoked.info, exclude=[]) ch_names = [evoked.ch_names[p] for p in picks] evoked.pick_channels(ch_names) if len(evoked.info['ch_names']) == 0: raise ValueError('No data channel found when averaging.') if evoked.nave < 1: warn('evoked object is empty (based on less than 1 epoch)') return evoked
Example 30
def calculate_accuracy(threshold, dist, actual_issame): predict_issame = np.less(dist, threshold) tp = np.sum(np.logical_and(predict_issame, actual_issame)) fp = np.sum(np.logical_and(predict_issame, np.logical_not(actual_issame))) tn = np.sum(np.logical_and(np.logical_not(predict_issame), np.logical_not(actual_issame))) fn = np.sum(np.logical_and(np.logical_not(predict_issame), actual_issame)) tpr = 0 if (tp+fn==0) else float(tp) / float(tp+fn) fpr = 0 if (fp+tn==0) else float(fp) / float(fp+tn) acc = float(tp+tn)/dist.size return tpr, fpr, acc
Example 31
def calculate_val_far(threshold, dist, actual_issame): predict_issame = np.less(dist, threshold) true_accept = np.sum(np.logical_and(predict_issame, actual_issame)) false_accept = np.sum(np.logical_and(predict_issame, np.logical_not(actual_issame))) n_same = np.sum(actual_issame) n_diff = np.sum(np.logical_not(actual_issame)) val = float(true_accept) / float(n_same) far = float(false_accept) / float(n_diff) return val, far
Example 32
def build_feature_files(base_directory, new_directory, data_loader, n=None, negative_example_keep_prob=1.0): os.makedirs(new_directory, exist_ok=False) episode_paths = frame.episode_paths(base_directory) label_counts = [0, 0] if n is not None: np.random.shuffle(episode_paths) episode_paths = episode_paths[:n] for episode_path in tqdm.tqdm(episode_paths): try: features, labels = data_loader.load_features_and_labels([episode_path]) except: traceback.print_exc() else: keep = np.logical_or(labels, (np.less( np.random.rand(len(labels)), negative_example_keep_prob))) labels = labels[keep] for i in range(len(label_counts)): label_counts[i] += np.count_nonzero(labels == i) features = {k: v[keep] for k, v in features.items()} new_path = path_relative_to_new_directory(base_directory, new_directory, episode_path, ".features") os.makedirs(os.path.dirname(new_path), exist_ok=True) with open(new_path, 'wb') as f: pickle.dump((features, labels), f) return label_counts
Example 33
def unk_filter(data): if config['voc_size'] == -1: return copy.copy(data) else: mask = (np.less(data, config['voc_size'])).astype(dtype='int32') data = copy.copy(data * mask + (1 - mask)) return data
Example 34
def unk_filter(data): if config['voc_size'] == -1: return copy.copy(data) else: mask = (np.less(data, config['voc_size'])).astype(dtype='int32') data = copy.copy(data * mask + (1 - mask)) return data # training
Example 35
def unk_filter(data): if config['voc_size'] == -1: return copy.copy(data) else: mask = (np.less(data, config['voc_size'])).astype(dtype='int32') data = copy.copy(data * mask + (1 - mask)) return data # training
Example 36
def unk_filter(data): if config['voc_size'] == -1: return copy.copy(data) else: mask = (np.less(data, config['voc_size'])).astype(dtype='int32') data = copy.copy(data * mask + (1 - mask)) return data # training
Example 37
def unk_filter(data): if config['voc_size'] == -1: return copy.copy(data) else: mask = (np.less(data, config['voc_size'])).astype(dtype='int32') data = copy.copy(data * mask + (1 - mask)) return data
Example 38
def unk_filter(data): if config['voc_size'] == -1: return copy.copy(data) else: mask = (np.less(data, config['voc_size'])).astype(dtype='int32') data = copy.copy(data * mask + (1 - mask)) return data # training
Example 39
def __init__(self, custom_model, filepath, monitor='val_loss', verbose=0, save_best_only=False, save_weights_only=False, mode='auto', period=1): super(CustomModelCheckpoint, self).__init__() self.custom_model = custom_model self.monitor = monitor self.verbose = verbose self.filepath = filepath self.save_best_only = save_best_only self.save_weights_only = save_weights_only self.period = period self.epochs_since_last_save = 0 if mode not in ['auto', 'min', 'max']: warnings.warn('CustomModelCheckpoint mode %s is unknown, ' 'fallback to auto mode.' % (mode), RuntimeWarning) mode = 'auto' if mode == 'min': self.monitor_op = np.less self.best = np.Inf elif mode == 'max': self.monitor_op = np.greater self.best = -np.Inf else: if 'acc' in self.monitor or self.monitor.startswith('fmeasure'): self.monitor_op = np.greater self.best = -np.Inf else: self.monitor_op = np.less self.best = np.Inf
Example 40
def clip(self, a, m, M, out=None): # use slow-clip selector = np.less(a, m) + 2*np.greater(a, M) return selector.choose((a, m, M), out=out) # Handy functions
Example 41
def test_roundtrip_str(self): x = self.x.real.ravel() s = "@".join(map(str, x)) y = np.fromstring(s, sep="@") # NB. str imbues less precision nan_mask = ~np.isfinite(x) assert_array_equal(x[nan_mask], y[nan_mask]) assert_array_almost_equal(x[~nan_mask], y[~nan_mask], decimal=5)
Example 42
def test_NotImplemented_not_returned(self): # See gh-5964 and gh-2091. Some of these functions are not operator # related and were fixed for other reasons in the past. binary_funcs = [ np.power, np.add, np.subtract, np.multiply, np.divide, np.true_divide, np.floor_divide, np.bitwise_and, np.bitwise_or, np.bitwise_xor, np.left_shift, np.right_shift, np.fmax, np.fmin, np.fmod, np.hypot, np.logaddexp, np.logaddexp2, np.logical_and, np.logical_or, np.logical_xor, np.maximum, np.minimum, np.mod ] # These functions still return NotImplemented. Will be fixed in # future. # bad = [np.greater, np.greater_equal, np.less, np.less_equal, np.not_equal] a = np.array('1') b = 1 for f in binary_funcs: assert_raises(TypeError, f, a, b)
Example 43
def test_datetime_compare(self): # Test all the comparison operators a = np.datetime64('2000-03-12T18:00:00.000000') b = np.array(['2000-03-12T18:00:00.000000', '2000-03-12T17:59:59.999999', '2000-03-12T18:00:00.000001', '1970-01-11T12:00:00.909090', '2016-01-11T12:00:00.909090'], dtype='datetime64[us]') assert_equal(np.equal(a, b), [1, 0, 0, 0, 0]) assert_equal(np.not_equal(a, b), [0, 1, 1, 1, 1]) assert_equal(np.less(a, b), [0, 0, 1, 0, 1]) assert_equal(np.less_equal(a, b), [1, 0, 1, 0, 1]) assert_equal(np.greater(a, b), [0, 1, 0, 1, 0]) assert_equal(np.greater_equal(a, b), [1, 1, 0, 1, 0])
Example 44
def test_datetime_compare_nat(self): dt_nat = np.datetime64('NaT', 'D') dt_other = np.datetime64('2000-01-01') td_nat = np.timedelta64('NaT', 'h') td_other = np.timedelta64(1, 'h') for op in [np.equal, np.less, np.less_equal, np.greater, np.greater_equal]: if op(dt_nat, dt_nat): assert_warns(FutureWarning, op, dt_nat, dt_nat) if op(dt_nat, dt_other): assert_warns(FutureWarning, op, dt_nat, dt_other) if op(dt_other, dt_nat): assert_warns(FutureWarning, op, dt_other, dt_nat) if op(td_nat, td_nat): assert_warns(FutureWarning, op, td_nat, td_nat) if op(td_nat, td_other): assert_warns(FutureWarning, op, td_nat, td_other) if op(td_other, td_nat): assert_warns(FutureWarning, op, td_other, td_nat) assert_warns(FutureWarning, np.not_equal, dt_nat, dt_nat) assert_(np.not_equal(dt_nat, dt_other)) assert_(np.not_equal(dt_other, dt_nat)) assert_warns(FutureWarning, np.not_equal, td_nat, td_nat) assert_(np.not_equal(td_nat, td_other)) assert_(np.not_equal(td_other, td_nat))
Example 45
def test_result_values(self): for f, fcmp in zip(self.nanfuncs, [np.greater, np.less]): for row in _ndat: with warnings.catch_warnings(record=True): warnings.simplefilter('always') ind = f(row) val = row[ind] # comparing with NaN is tricky as the result # is always false except for NaN != NaN assert_(not np.isnan(val)) assert_(not fcmp(val, row).any()) assert_(not np.equal(val, row[:ind]).any())
Example 46
def test_basic_ufuncs(self): # Test various functions such as sin, cos. (x, y, a10, m1, m2, xm, ym, z, zm, xf) = self.d assert_equal(np.cos(x), cos(xm)) assert_equal(np.cosh(x), cosh(xm)) assert_equal(np.sin(x), sin(xm)) assert_equal(np.sinh(x), sinh(xm)) assert_equal(np.tan(x), tan(xm)) assert_equal(np.tanh(x), tanh(xm)) assert_equal(np.sqrt(abs(x)), sqrt(xm)) assert_equal(np.log(abs(x)), log(xm)) assert_equal(np.log10(abs(x)), log10(xm)) assert_equal(np.exp(x), exp(xm)) assert_equal(np.arcsin(z), arcsin(zm)) assert_equal(np.arccos(z), arccos(zm)) assert_equal(np.arctan(z), arctan(zm)) assert_equal(np.arctan2(x, y), arctan2(xm, ym)) assert_equal(np.absolute(x), absolute(xm)) assert_equal(np.angle(x + 1j*y), angle(xm + 1j*ym)) assert_equal(np.angle(x + 1j*y, deg=True), angle(xm + 1j*ym, deg=True)) assert_equal(np.equal(x, y), equal(xm, ym)) assert_equal(np.not_equal(x, y), not_equal(xm, ym)) assert_equal(np.less(x, y), less(xm, ym)) assert_equal(np.greater(x, y), greater(xm, ym)) assert_equal(np.less_equal(x, y), less_equal(xm, ym)) assert_equal(np.greater_equal(x, y), greater_equal(xm, ym)) assert_equal(np.conjugate(x), conjugate(xm))
Example 47
def test_masked_where_condition(self): # Tests masking functions. x = array([1., 2., 3., 4., 5.]) x[2] = masked assert_equal(masked_where(greater(x, 2), x), masked_greater(x, 2)) assert_equal(masked_where(greater_equal(x, 2), x), masked_greater_equal(x, 2)) assert_equal(masked_where(less(x, 2), x), masked_less(x, 2)) assert_equal(masked_where(less_equal(x, 2), x), masked_less_equal(x, 2)) assert_equal(masked_where(not_equal(x, 2), x), masked_not_equal(x, 2)) assert_equal(masked_where(equal(x, 2), x), masked_equal(x, 2)) assert_equal(masked_where(not_equal(x, 2), x), masked_not_equal(x, 2)) assert_equal(masked_where([1, 1, 0, 0, 0], [1, 2, 3, 4, 5]), [99, 99, 3, 4, 5])
Example 48
def test_testUfuncs1(self): # Test various functions such as sin, cos. (x, y, a10, m1, m2, xm, ym, z, zm, xf, s) = self.d self.assertTrue(eq(np.cos(x), cos(xm))) self.assertTrue(eq(np.cosh(x), cosh(xm))) self.assertTrue(eq(np.sin(x), sin(xm))) self.assertTrue(eq(np.sinh(x), sinh(xm))) self.assertTrue(eq(np.tan(x), tan(xm))) self.assertTrue(eq(np.tanh(x), tanh(xm))) with np.errstate(divide='ignore', invalid='ignore'): self.assertTrue(eq(np.sqrt(abs(x)), sqrt(xm))) self.assertTrue(eq(np.log(abs(x)), log(xm))) self.assertTrue(eq(np.log10(abs(x)), log10(xm))) self.assertTrue(eq(np.exp(x), exp(xm))) self.assertTrue(eq(np.arcsin(z), arcsin(zm))) self.assertTrue(eq(np.arccos(z), arccos(zm))) self.assertTrue(eq(np.arctan(z), arctan(zm))) self.assertTrue(eq(np.arctan2(x, y), arctan2(xm, ym))) self.assertTrue(eq(np.absolute(x), absolute(xm))) self.assertTrue(eq(np.equal(x, y), equal(xm, ym))) self.assertTrue(eq(np.not_equal(x, y), not_equal(xm, ym))) self.assertTrue(eq(np.less(x, y), less(xm, ym))) self.assertTrue(eq(np.greater(x, y), greater(xm, ym))) self.assertTrue(eq(np.less_equal(x, y), less_equal(xm, ym))) self.assertTrue(eq(np.greater_equal(x, y), greater_equal(xm, ym))) self.assertTrue(eq(np.conjugate(x), conjugate(xm))) self.assertTrue(eq(np.concatenate((x, y)), concatenate((xm, ym)))) self.assertTrue(eq(np.concatenate((x, y)), concatenate((x, y)))) self.assertTrue(eq(np.concatenate((x, y)), concatenate((xm, y)))) self.assertTrue(eq(np.concatenate((x, y, x)), concatenate((x, ym, x))))
Example 49
def test_testMinMax2(self): # Test of minumum, maximum. assert_(eq(minimum([1, 2, 3], [4, 0, 9]), [1, 0, 3])) assert_(eq(maximum([1, 2, 3], [4, 0, 9]), [4, 2, 9])) x = arange(5) y = arange(5) - 2 x[3] = masked y[0] = masked assert_(eq(minimum(x, y), where(less(x, y), x, y))) assert_(eq(maximum(x, y), where(greater(x, y), x, y))) assert_(minimum(x) == 0) assert_(maximum(x) == 4)
Example 50
def test_testUfuncRegression(self): f_invalid_ignore = [ 'sqrt', 'arctanh', 'arcsin', 'arccos', 'arccosh', 'arctanh', 'log', 'log10', 'divide', 'true_divide', 'floor_divide', 'remainder', 'fmod'] for f in ['sqrt', 'log', 'log10', 'exp', 'conjugate', 'sin', 'cos', 'tan', 'arcsin', 'arccos', 'arctan', 'sinh', 'cosh', 'tanh', 'arcsinh', 'arccosh', 'arctanh', 'absolute', 'fabs', 'negative', 'floor', 'ceil', 'logical_not', 'add', 'subtract', 'multiply', 'divide', 'true_divide', 'floor_divide', 'remainder', 'fmod', 'hypot', 'arctan2', 'equal', 'not_equal', 'less_equal', 'greater_equal', 'less', 'greater', 'logical_and', 'logical_or', 'logical_xor']: try: uf = getattr(umath, f) except AttributeError: uf = getattr(fromnumeric, f) mf = getattr(np.ma, f) args = self.d[:uf.nin] with np.errstate(): if f in f_invalid_ignore: np.seterr(invalid='ignore') if f in ['arctanh', 'log', 'log10']: np.seterr(divide='ignore') ur = uf(*args) mr = mf(*args) self.assertTrue(eq(ur.filled(0), mr.filled(0), f)) self.assertTrue(eqmask(ur.mask, mr.mask))