def test_mutation(self):
# Check that passed array is not modified.
ndat = _ndat.copy()
np.nanpercentile(ndat, 30)
assert_equal(ndat, _ndat)
python类nanpercentile()的实例源码
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 _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 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 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 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 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)