我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用numpy.nanpercentile()。
def _nanpercentile(a, q, axis=None, out=None, overwrite_input=False, interpolation='linear', keepdims=False): """ Private function that doesn't support extended axis or keepdims. These methods are extended to this function using _ureduce See nanpercentile for parameter usage """ if axis is None: part = a.ravel() result = _nanpercentile1d(part, q, overwrite_input, interpolation) else: result = np.apply_along_axis(_nanpercentile1d, axis, a, q, overwrite_input, interpolation) # apply_along_axis fills in collapsed axis with results. # Move that axis to the beginning to match percentile's # convention. if q.ndim != 0: result = np.rollaxis(result, axis) if out is not None: out[...] = result return result
def test_out(self): mat = np.random.rand(3, 3) nan_mat = np.insert(mat, [0, 2], np.nan, axis=1) resout = np.zeros(3) tgt = np.percentile(mat, 42, axis=1) res = np.nanpercentile(nan_mat, 42, axis=1, out=resout) assert_almost_equal(res, resout) assert_almost_equal(res, tgt) # 0-d output: resout = np.zeros(()) tgt = np.percentile(mat, 42, axis=None) res = np.nanpercentile(nan_mat, 42, axis=None, out=resout) assert_almost_equal(res, resout) assert_almost_equal(res, tgt) res = np.nanpercentile(nan_mat, 42, axis=(0, 1), out=resout) assert_almost_equal(res, resout) assert_almost_equal(res, tgt)
def test_allnans(self): mat = np.array([np.nan]*9).reshape(3, 3) for axis in [None, 0, 1]: with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') assert_(np.isnan(np.nanpercentile(mat, 60, axis=axis)).all()) if axis is None: assert_(len(w) == 1) else: assert_(len(w) == 3) assert_(issubclass(w[0].category, RuntimeWarning)) # Check scalar assert_(np.isnan(np.nanpercentile(np.nan, 60))) if axis is None: assert_(len(w) == 2) else: assert_(len(w) == 4) assert_(issubclass(w[0].category, RuntimeWarning))
def test_multiple_percentiles(self): perc = [50, 100] mat = np.ones((4, 3)) nan_mat = np.nan * mat # For checking consistency in higher dimensional case large_mat = np.ones((3, 4, 5)) large_mat[:, 0:2:4, :] = 0 large_mat[:, :, 3:] *= 2 for axis in [None, 0, 1]: for keepdim in [False, True]: with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') val = np.percentile(mat, perc, axis=axis, keepdims=keepdim) nan_val = np.nanpercentile(nan_mat, perc, axis=axis, keepdims=keepdim) assert_equal(nan_val.shape, val.shape) val = np.percentile(large_mat, perc, axis=axis, keepdims=keepdim) nan_val = np.nanpercentile(large_mat, perc, axis=axis, keepdims=keepdim) assert_equal(nan_val, val) megamat = np.ones((3, 4, 5, 6)) assert_equal(np.nanpercentile(megamat, perc, axis=(1, 2)).shape, (2, 3, 6))
def convert_percentile_to_thresh(g, percentile): """ Figure out what value in g corresponds to the given percentile. Args: @param g: @type g: igraph.Graph @param percentile: between 0 and 100 @type percentile: float Returns: thresh """ assert percentile < 100 and percentile > 0, ( "percentile must be between 0 and 100. percentile: {}".format(percentile)) thresh = np.nanpercentile(np.abs(g.es["weight"]), percentile) return thresh
def median_percentile(data, des_percentiles='68+95+99'): """ :param data: :param des_percentiles: string with +separated values of the percentiles :return: """ median = np.nanmedian(data, axis=0) out = np.array(map(int, des_percentiles.split("+"))) for i in range(out.size): assert 0 <= out[i] <= 100, 'Percentile must be >0 <100; instead is %f' % out[i] list_percentiles = np.empty((2*out.size,), dtype=out.dtype) list_percentiles[0::2] = out # Compute the percentile list_percentiles[1::2] = 100 - out # Compute also the mirror percentile percentiles = np.nanpercentile(data, list_percentiles, axis=0) return [median, percentiles]
def plotTimeVsLvls(ax, runs, *args, **kwargs): """Plots Time vs TOL of @runs, as returned by MIMCDatabase.readRunData() ax is in instance of matplotlib.axes """ ax.set_xlabel(r'$\ell$') ax.set_ylabel('Time (s)') ax.set_yscale('log') fnNorm = kwargs.pop("fnNorm") if "__calc_moments" in kwargs: _, _, Tl, M, _ = kwargs.pop("__calc_moments") else: _, _, Tl, M, _ = __calc_moments(runs, seed=kwargs.pop('seed', None), direction=kwargs.pop('direction', None), fnNorm=fnNorm) ax.xaxis.set_major_locator(MaxNLocator(integer=True)) min_tl = np.nanpercentile(Tl, 5, axis=1) med = np.nanmean(Tl, axis=1) max_tl = np.nanpercentile(Tl, 95, axis=1) line = ax.errorbar(np.arange(0, len(Tl)), med, yerr=[med-min_tl, max_tl-med], *args, **kwargs) return line[0].get_xydata(), [line]
def _nanpercentile(a, q, axis=None, out=None, overwrite_input=False, interpolation='linear', keepdims=False): """ Private function that doesn't support extended axis or keepdims. These methods are extended to this function using _ureduce See nanpercentile for parameter usage """ if axis is None: part = a.ravel() result = _nanpercentile1d(part, q, overwrite_input, interpolation) else: result = np.apply_along_axis(_nanpercentile1d, axis, a, q, overwrite_input, interpolation) if out is not None: out[...] = result return result
def predict_uncertainty(self, df): """Prediction intervals for yhat and trend. Parameters ---------- df: Prediction dataframe. Returns ------- Dataframe with uncertainty intervals. """ sim_values = self.sample_posterior_predictive(df) lower_p = 100 * (1.0 - self.interval_width) / 2 upper_p = 100 * (1.0 + self.interval_width) / 2 series = {} for key in ['yhat', 'trend']: series['{}_lower'.format(key)] = np.nanpercentile( sim_values[key], lower_p, axis=1) series['{}_upper'.format(key)] = np.nanpercentile( sim_values[key], upper_p, axis=1) return pd.DataFrame(series)
def _nanpercentile(a, q, axis=None, out=None, overwrite_input=False, interpolation='linear'): """ Private function that doesn't support extended axis or keepdims. These methods are extended to this function using _ureduce See nanpercentile for parameter usage """ if axis is None: part = a.ravel() result = _nanpercentile1d(part, q, overwrite_input, interpolation) else: result = np.apply_along_axis(_nanpercentile1d, axis, a, q, overwrite_input, interpolation) # apply_along_axis fills in collapsed axis with results. # Move that axis to the beginning to match percentile's # convention. if q.ndim != 0: result = np.rollaxis(result, axis) if out is not None: out[...] = result return result
def _nanpercentile(a, q, axis=None, out=None, overwrite_input=False, interpolation='linear'): """ Private function that doesn't support extended axis or keepdims. These methods are extended to this function using _ureduce See nanpercentile for parameter usage """ if axis is None or a.ndim == 1: part = a.ravel() result = _nanpercentile1d(part, q, overwrite_input, interpolation) else: result = np.apply_along_axis(_nanpercentile1d, axis, a, q, overwrite_input, interpolation) # apply_along_axis fills in collapsed axis with results. # Move that axis to the beginning to match percentile's # convention. if q.ndim != 0: result = np.rollaxis(result, axis) if out is not None: out[...] = result return result
def test_multiple_percentiles(self): perc = [50, 100] mat = np.ones((4, 3)) nan_mat = np.nan * mat # For checking consistency in higher dimensional case large_mat = np.ones((3, 4, 5)) large_mat[:, 0:2:4, :] = 0 large_mat[:, :, 3:] *= 2 for axis in [None, 0, 1]: for keepdim in [False, True]: with suppress_warnings() as sup: sup.filter(RuntimeWarning, "All-NaN slice encountered") val = np.percentile(mat, perc, axis=axis, keepdims=keepdim) nan_val = np.nanpercentile(nan_mat, perc, axis=axis, keepdims=keepdim) assert_equal(nan_val.shape, val.shape) val = np.percentile(large_mat, perc, axis=axis, keepdims=keepdim) nan_val = np.nanpercentile(large_mat, perc, axis=axis, keepdims=keepdim) assert_equal(nan_val, val) megamat = np.ones((3, 4, 5, 6)) assert_equal(np.nanpercentile(megamat, perc, axis=(1, 2)).shape, (2, 3, 6))
def temp_water(is_water, swir2, tirs1): """Use water to mask tirs and find 82.5 pctile Equation 7 and 8 (Zhu and Woodcock, 2012) Parameters ---------- is_water: ndarray, boolean water mask, water is True, land is False swir2: ndarray tirs1: ndarray Output ------ float: 82.5th percentile temperature over water """ # eq7 th_swir2 = 0.03 clearsky_water = is_water & (swir2 < th_swir2) # eq8 clear_water_temp = tirs1.copy() clear_water_temp[~clearsky_water] = np.nan return np.nanpercentile(clear_water_temp, 82.5)
def test_nan_percentile(): # create array of shape(5,100,100) - image of size 100x100 with 5 layers test_arr = np.random.randint(0, 10000, 50000).reshape(5, 100, 100).astype(np.float32) np.random.shuffle(test_arr) # place random NaNs random_nans = np.random.randint(0, 50000, 500).astype(np.float32) for r in random_nans: test_arr[test_arr == r] = np.NaN # Test with single q q = 45 input_arr = np.array(test_arr, copy=True) std_np_func = np.nanpercentile(input_arr, q=q, axis=0) new_func = nan_percentile(input_arr, q=q) assert np.allclose(std_np_func, new_func) # Test with all qs qs = range(0, 100) input_arr = np.array(test_arr, copy=True) std_np_func = np.nanpercentile(input_arr, q=qs, axis=0) new_func = nan_percentile(input_arr, q=qs) assert np.allclose(std_np_func, new_func)
def _nanpercentile1d(arr1d, q, overwrite_input=False, interpolation='linear'): """ Private function for rank 1 arrays. Compute percentile ignoring NaNs. See nanpercentile for parameter usage """ c = np.isnan(arr1d) s = np.where(c)[0] if s.size == arr1d.size: warnings.warn("All-NaN slice encountered", RuntimeWarning) if q.ndim == 0: return np.nan else: return np.nan * np.ones((len(q),)) elif s.size == 0: return np.percentile(arr1d, q, overwrite_input=overwrite_input, interpolation=interpolation) else: if overwrite_input: x = arr1d else: x = arr1d.copy() # select non-nans at end of array enonan = arr1d[-s.size:][~c[-s.size:]] # fill nans in beginning of array with non-nans of end x[s[:enonan.size]] = enonan # slice nans away return np.percentile(x[:-s.size], q, overwrite_input=True, interpolation=interpolation)
def test_mutation(self): # Check that passed array is not modified. ndat = _ndat.copy() np.nanpercentile(ndat, 30) assert_equal(ndat, _ndat)
def test_keepdims(self): mat = np.eye(3) for axis in [None, 0, 1]: tgt = np.percentile(mat, 70, axis=axis, out=None, overwrite_input=False) res = np.nanpercentile(mat, 70, axis=axis, out=None, overwrite_input=False) assert_(res.ndim == tgt.ndim) d = np.ones((3, 5, 7, 11)) # Randomly set some elements to NaN: w = np.random.random((4, 200)) * np.array(d.shape)[:, None] w = w.astype(np.intp) d[tuple(w)] = np.nan with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always', RuntimeWarning) res = np.nanpercentile(d, 90, axis=None, keepdims=True) assert_equal(res.shape, (1, 1, 1, 1)) res = np.nanpercentile(d, 90, axis=(0, 1), keepdims=True) assert_equal(res.shape, (1, 1, 7, 11)) res = np.nanpercentile(d, 90, axis=(0, 3), keepdims=True) assert_equal(res.shape, (1, 5, 7, 1)) res = np.nanpercentile(d, 90, axis=(1,), keepdims=True) assert_equal(res.shape, (3, 1, 7, 11)) res = np.nanpercentile(d, 90, axis=(0, 1, 2, 3), keepdims=True) assert_equal(res.shape, (1, 1, 1, 1)) res = np.nanpercentile(d, 90, axis=(0, 1, 3), keepdims=True) assert_equal(res.shape, (1, 1, 7, 1))
def test_empty(self): mat = np.zeros((0, 3)) for axis in [0, None]: with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') assert_(np.isnan(np.nanpercentile(mat, 40, axis=axis)).all()) assert_(len(w) == 1) assert_(issubclass(w[0].category, RuntimeWarning)) for axis in [1]: with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') assert_equal(np.nanpercentile(mat, 40, axis=axis), np.zeros([])) assert_(len(w) == 0)
def test_scalar(self): assert_(np.nanpercentile(0., 100) == 0.)
def test_extended_axis_invalid(self): d = np.ones((3, 5, 7, 11)) assert_raises(IndexError, np.nanpercentile, d, q=5, axis=-5) assert_raises(IndexError, np.nanpercentile, d, q=5, axis=(0, -5)) assert_raises(IndexError, np.nanpercentile, d, q=5, axis=4) assert_raises(IndexError, np.nanpercentile, d, q=5, axis=(0, 4)) assert_raises(ValueError, np.nanpercentile, d, q=5, axis=(1, 1))
def interactive(img, cmap='gray', window=(2, 98)): import ipywidgets as ipy # Get some information about the image bbox = Box.fromMask(img) window_vals = np.nanpercentile(img.get_data(), window) # Setup figure fig, axes = plt.subplots(1, 3, figsize=(8, 4)) implots = [None, None, None] for i in range(3): sl = Slice(bbox, bbox.center, i, 256, orient='clin') sl_img = sl.sample(img, order=0) sl_color = colorize(sl_img, cmap, window_vals) implots[i] = axes[i].imshow(sl_color, origin='lower', extent=sl.extent, cmap=cmap, vmin = 0.1) axes[i].axis('off') def wrap_sections(x, y, z): for i in range(3): sl = Slice(bbox, np.array((x, y, z)), i, 256, orient='clin') sl_img = sl.sample(img, order=0) sl_color = colorize(sl_img, cmap, window_vals) implots[i].set_data(sl_color) plt.show() # Setup widgets slider_x = ipy.FloatSlider(min=bbox.start[0], max=bbox.end[0], value=bbox.center[0]) slider_y = ipy.FloatSlider(min=bbox.start[1], max=bbox.end[1], value=bbox.center[1]) slider_z = ipy.FloatSlider(min=bbox.start[2], max=bbox.end[2], value=bbox.center[2]) widgets = ipy.interactive(wrap_sections, x=slider_x, y=slider_y, z=slider_z) # Now do some manual layout hbox = ipy.HBox(widgets.children[0:3]) # Set the sliders to horizontal layout vbox = ipy.VBox((hbox, widgets.children[3])) # iplot.widget.children[-1].layout.height = '350px' return vbox
def plot_aligned(data_all, transitions, label = 'data', dt = time_bin, phase = 0): phase_names = ['hatch', 'L1', 'L2', 'L3', 'L4', 'L5']; ttref0 = np.max(transitions[:, 0]); ntt = data_all.shape[1] + ttref0 + 500; nw = data_all.shape[0]; tr = phase; data_algn = np.zeros((nw, ntt)); tref = np.max(transitions[:, tr]); for wid in range(nworms): tt = transitions[wid, tr]; ts = transitions[wid, 0]; te = transitions[wid, -1]; nt = te - ts; t0 = ts + tref - tt; t1 = t0 + nt; #print 'tref=%s, ts =%d, te=%d, tt =%d, nt = %d, t0=%d, t1=%d, ntt=%d' % (tref, ts,te,tt, nt, t0,t1, ntt) data_algn[wid, t0 : t1] = data_all[wid, ts:te]; #rmax = np.nanpercentile(roam_24hr, 95); rmax = 1; plt.imshow(data_algn, interpolation = 'none', aspect = 'auto', cmap = plt.cm.viridis, vmax = rmax ) cb = plt.colorbar(fraction = 0.025, shrink = 0.5, pad = 0.01) cb.ax.set_ylabel(label + ' [au]', rotation=270, labelpad = 20) days = np.array(24. * 60 * 60 / time_bin * np.arange(6), dtype = int); labl = ['%d'%d for d in 24 *np.linspace(0,5, 6)]; plt.xticks(days, labl); plt.xlabel('time [hrs]'); plt.ylabel('worm id'); plt.tight_layout() plt.title(phase_names[tr]); plt.tight_layout()
def downstddv(x): if x.size < 4: return np.nan median = np.nanpercentile(x, 50) return np.nanstd(x[x < median])
def __get_stats(data, groupby=0, staton=1): import itertools data = sorted(data, key=lambda xx: xx[groupby]) x = [] y = [] for k, itr in itertools.groupby(data, key=lambda xx: xx[groupby]): all_y = [d[staton] for d in itr] y.append([np.nanpercentile(all_y, 5), np.nanpercentile(all_y, 50), np.nanpercentile(all_y, 95)]) x.append(k) return np.array(x), np.array(y)
def p16(s, v): try: return np.nanpercentile(v, 16) except AttributeError: return np.percentile(v, 16)