def max_edge(arr, threshold=0.5, axis=1):
""" Find strongest decreasing edge on each row """
if axis == 0:
arr = arr.T
if np.issubdtype(arr.dtype, np.unsignedinteger):
arr = arr.astype(np.int)
derivative = -np.diff(arr)
index = np.argmax(derivative, axis=1)
values = np.max(derivative, axis=1)
r_dev = index + 0.5
r_dev[values < threshold * values.max()] = np.nan
return r_dev
python类issubdtype()的实例源码
def center(a, width, fillchar=' '):
"""
Return a copy of `a` with its elements centered in a string of
length `width`.
Calls `str.center` element-wise.
Parameters
----------
a : array_like of str or unicode
width : int
The length of the resulting strings
fillchar : str or unicode, optional
The padding character to use (default is space).
Returns
-------
out : ndarray
Output array of str or unicode, depending on input
types
See also
--------
str.center
"""
a_arr = numpy.asarray(a)
width_arr = numpy.asarray(width)
size = long(numpy.max(width_arr.flat))
if numpy.issubdtype(a_arr.dtype, numpy.string_):
fillchar = asbytes(fillchar)
return _vec_string(
a_arr, (a_arr.dtype.type, size), 'center', (width_arr, fillchar))
def ljust(a, width, fillchar=' '):
"""
Return an array with the elements of `a` left-justified in a
string of length `width`.
Calls `str.ljust` element-wise.
Parameters
----------
a : array_like of str or unicode
width : int
The length of the resulting strings
fillchar : str or unicode, optional
The character to use for padding
Returns
-------
out : ndarray
Output array of str or unicode, depending on input type
See also
--------
str.ljust
"""
a_arr = numpy.asarray(a)
width_arr = numpy.asarray(width)
size = long(numpy.max(width_arr.flat))
if numpy.issubdtype(a_arr.dtype, numpy.string_):
fillchar = asbytes(fillchar)
return _vec_string(
a_arr, (a_arr.dtype.type, size), 'ljust', (width_arr, fillchar))
def rjust(a, width, fillchar=' '):
"""
Return an array with the elements of `a` right-justified in a
string of length `width`.
Calls `str.rjust` element-wise.
Parameters
----------
a : array_like of str or unicode
width : int
The length of the resulting strings
fillchar : str or unicode, optional
The character to use for padding
Returns
-------
out : ndarray
Output array of str or unicode, depending on input type
See also
--------
str.rjust
"""
a_arr = numpy.asarray(a)
width_arr = numpy.asarray(width)
size = long(numpy.max(width_arr.flat))
if numpy.issubdtype(a_arr.dtype, numpy.string_):
fillchar = asbytes(fillchar)
return _vec_string(
a_arr, (a_arr.dtype.type, size), 'rjust', (width_arr, fillchar))
def test_simple(self):
a = [[1, 2], [3, 4]]
a_str = [[b'1', b'2'], [b'3', b'4']]
modes = ['raise', 'wrap', 'clip']
indices = [-1, 4]
index_arrays = [np.empty(0, dtype=np.intp),
np.empty(tuple(), dtype=np.intp),
np.empty((1, 1), dtype=np.intp)]
real_indices = {'raise': {-1: 1, 4: IndexError},
'wrap': {-1: 1, 4: 0},
'clip': {-1: 0, 4: 1}}
# Currently all types but object, use the same function generation.
# So it should not be necessary to test all. However test also a non
# refcounted struct on top of object.
types = np.int, np.object, np.dtype([('', 'i', 2)])
for t in types:
# ta works, even if the array may be odd if buffer interface is used
ta = np.array(a if np.issubdtype(t, np.number) else a_str, dtype=t)
tresult = list(ta.T.copy())
for index_array in index_arrays:
if index_array.size != 0:
tresult[0].shape = (2,) + index_array.shape
tresult[1].shape = (2,) + index_array.shape
for mode in modes:
for index in indices:
real_index = real_indices[mode][index]
if real_index is IndexError and index_array.size != 0:
index_array.put(0, index)
assert_raises(IndexError, ta.take, index_array,
mode=mode, axis=1)
elif index_array.size != 0:
index_array.put(0, index)
res = ta.take(index_array, mode=mode, axis=1)
assert_array_equal(res, tresult[real_index])
else:
res = ta.take(index_array, mode=mode, axis=1)
assert_(res.shape == (2,) + index_array.shape)
def test_large_types(self):
for t in [np.int32, np.int64, np.float32, np.float64, np.longdouble]:
a = t(51)
b = a ** 4
msg = "error with %r: got %r" % (t, b)
if np.issubdtype(t, np.integer):
assert_(b == 6765201, msg)
else:
assert_almost_equal(b, 6765201, err_msg=msg)
def test_shape_and_dtype(self):
sizes = (4, 5, 3, 2)
# Test both lists and arrays
for func in (range, np.arange):
arrays = np.ix_(*[func(sz) for sz in sizes])
for k, (a, sz) in enumerate(zip(arrays, sizes)):
assert_equal(a.shape[k], sz)
assert_(all(sh == 1 for j, sh in enumerate(a.shape) if j != k))
assert_(np.issubdtype(a.dtype, int))
def test_asfarray(self):
a = asfarray(np.array([1, 2, 3]))
assert_equal(a.__class__, np.ndarray)
assert_(np.issubdtype(a.dtype, np.float))
def test_type(self):
# Check the type of the returned histogram
a = np.arange(10) + .5
h, b = histogram(a)
assert_(np.issubdtype(h.dtype, int))
h, b = histogram(a, normed=True)
assert_(np.issubdtype(h.dtype, float))
h, b = histogram(a, weights=np.ones(10, int))
assert_(np.issubdtype(h.dtype, int))
h, b = histogram(a, weights=np.ones(10, float))
assert_(np.issubdtype(h.dtype, float))
def test_objects(self):
from decimal import Decimal
p = np.poly1d([Decimal('4.0'), Decimal('3.0'), Decimal('2.0')])
p2 = p * Decimal('1.333333333333333')
assert_(p2[1] == Decimal("3.9999999999999990"))
p2 = p.deriv()
assert_(p2[1] == Decimal('8.0'))
p2 = p.integ()
assert_(p2[3] == Decimal("1.333333333333333333333333333"))
assert_(p2[2] == Decimal('1.5'))
assert_(np.issubdtype(p2.coeffs.dtype, np.object_))
p = np.poly([Decimal(1), Decimal(2)])
assert_equal(np.poly([Decimal(1), Decimal(2)]),
[1, Decimal(-3), Decimal(2)])
def _round_ifneeded(arr, dtype):
"""
Rounds arr inplace if destination dtype is integer.
Parameters
----------
arr : ndarray
Input array.
dtype : dtype
The dtype of the destination array.
"""
if np.issubdtype(dtype, np.integer):
arr.round(out=arr)
def dtype_min_max(dtype):
'''Get the min and max value for a numeric dtype'''
if np.issubdtype(dtype, np.integer):
info = np.iinfo(dtype)
else:
info = np.finfo(dtype)
return info.min, info.max
def rgb_preprocess(img):
if np.issubdtype(img.dtype, np.float):
# assuming 0., 1. range
return (img*255).clip(0, 255).astype(np.uint8)
if not img.dtype == np.uint8:
raise ValueError('only uint8 or float for 3-channel images')
return img
def to_netcdf(ds, *args, **kwargs):
"""
Store the given dataset as a netCDF file
This functions works essentially the same as the usual
:meth:`xarray.Dataset.to_netcdf` method but can also encode absolute time
units
Parameters
----------
ds: xarray.Dataset
The dataset to store
%(xarray.Dataset.to_netcdf.parameters)s
"""
to_update = {}
for v, obj in six.iteritems(ds.variables):
units = obj.attrs.get('units', obj.encoding.get('units', None))
if units == 'day as %Y%m%d.%f' and np.issubdtype(
obj.dtype, np.datetime64):
to_update[v] = xr.Variable(
obj.dims, AbsoluteTimeEncoder(obj), attrs=obj.attrs.copy(),
encoding=obj.encoding)
to_update[v].attrs['units'] = units
if to_update:
ds = ds.update(to_update, inplace=False)
return xarray_api.to_netcdf(ds, *args, **kwargs)
def _decode_ds(cls, ds, gridfile=None, inplace=False, decode_coords=True,
decode_times=True):
"""
Static method to decode coordinates and time informations
This method interpretes absolute time informations (stored with units
``'day as %Y%m%d.%f'``) and coordinates
Parameters
----------
%(CFDecoder.decode_coords.parameters)s
decode_times : bool, optional
If True, decode times encoded in the standard NetCDF datetime
format into datetime objects. Otherwise, leave them encoded as
numbers.
decode_coords : bool, optional
If True, decode the 'coordinates' attribute to identify coordinates
in the resulting dataset."""
if decode_coords:
ds = cls.decode_coords(ds, gridfile=gridfile,
inplace=inplace)
if decode_times:
for k, v in six.iteritems(ds.variables):
# check for absolute time units and make sure the data is not
# already decoded via dtype check
if v.attrs.get('units', '') == 'day as %Y%m%d.%f' and (
np.issubdtype(v.dtype, float)):
decoded = xr.Variable(
v.dims, AbsoluteTimeDecoder(v), attrs=v.attrs,
encoding=v.encoding)
ds = ds.update({k: decoded}, inplace=inplace)
return ds
def maybe_format(item):
"""Pretty-format a string, integer, float, or percent
Parameters
----------
item : pandas.Series
A single-item series containing a .name attribute and a value in the
first (0th) index
"""
value = item[0]
if pd.isnull(value):
return 'N/A'
elif isinstance(value, str):
return value
elif 'percent' in item.name.lower():
return '{:.2f}%'.format(value)
elif isinstance(value, pd.Timestamp):
return str(np.datetime64(value, 'D'))
elif (isinstance(value, float) # this must go before ints!
or np.issubdtype(value, np.number)):
if value >= 1e3:
return locale.format("%d", int(value), grouping=True)
else:
return locale.format("%.3g", value, grouping=True)
elif (isinstance(value, int)
or np.issubdtype(value, np.integer)):
return locale.format("%d", value, grouping=True)
else:
raise TypeError
def write_frame(self, frame, fraction=1.):
"""Write given frame to the file
Using temporary files, the sparsified version of the input is written.
Arguments:
frame (int array) - 1D dense array with photon counts in each pixel
fraction (float, optional) - What fraction of photons to write
If fraction is less than 1, then each photon is written randomly with
the probability = fraction. by default, all photons are written. This
option is useful for performing tests with lower photons/frame.
"""
if len(frame.shape) != 1 or not np.issubdtype(frame.dtype, np.integer):
raise ValueError('write_frame needs 1D array of integers: '+str(frame.shape)+' '+str(frame.dtype))
place_ones = np.where(frame == 1)[0]
place_multi = np.where(frame > 1)[0]
count_multi = frame[place_multi]
if fraction < 1.:
sel = (np.random.random(len(place_ones)) < fraction)
place_ones = place_ones[sel]
sel = (np.random.random(count_multi.sum()) < fraction)
count_multi = np.array([a.sum() for a in np.split(sel, count_multi.cumsum())])[:-1]
place_multi = place_multi[count_multi>0]
count_multi = count_multi[count_multi>0]
self.num_data += 1
self.mean_count += len(place_ones) + count_multi.sum()
self.ones.append(len(place_ones))
self.multi.append(len(place_multi))
place_ones.astype(np.int32).tofile(self.f[0])
place_multi.astype(np.int32).tofile(self.f[1])
count_multi.astype(np.int32).tofile(self.f[2])
def create_bins(data, n_bins):
"""
Create bins from the data value
:param data: a list or a 1-dim array of data to determine the bins
:param n_bins: number of bins to create
:return: a list of Bin object
"""
if data is None or len(data) <= 0:
raise ValueError('Empty input array!')
if n_bins <= 0:
raise ValueError('Less than one bin makes no sense.')
insufficient_distinct = False
n_unique_values = len(np.unique([value for value in data if not is_number_and_nan(value)]))
if n_unique_values < n_bins:
insufficient_distinct = True
warnings.warn("Insufficient unique values for requested number of bins. " +
"Number of bins will be reset to number of unique values.")
n_bins = n_unique_values
# cast into a numpy array to infer the dtype
data_as_array = np.array(data)
is_numeric = np.issubdtype(data_as_array.dtype, np.number)
if is_numeric:
bins = _create_numerical_bins(data_as_array, n_bins)
else:
bins = _create_categorical_bins(data_as_array, n_bins)
if (not insufficient_distinct) and (len(bins) < n_bins):
warnings.warn('Created less bins than requested.')
return bins
#------- private methods for numerical binnings-------#
def get_column_names_by_type(df, dtype):
return [c for c in df.columns if np.issubdtype(df.dtypes[c], dtype)]
def from_subvolume(subvolume, **kwargs):
if subvolume.label_mask is not None and np.issubdtype(subvolume.label_mask.dtype, np.bool):
target = mask_to_output_target(subvolume.label_mask)
else:
target = subvolume.label_mask
return Region(subvolume.image,
target=target,
seed_vox=subvolume.seed,
**kwargs)