def make_grid(I, ncols=8):
assert isinstance(I, np.ndarray), 'plugin error, should pass numpy array here'
assert I.ndim == 4 and I.shape[1] == 3
nimg = I.shape[0]
H = I.shape[2]
W = I.shape[3]
ncols = min(nimg, ncols)
nrows = int(np.ceil(float(nimg) / ncols))
canvas = np.zeros((3, H * nrows, W * ncols))
i = 0
for y in range(nrows):
for x in range(ncols):
if i >= nimg:
break
canvas[:, y * H:(y + 1) * H, x * W:(x + 1) * W] = I[i]
i = i + 1
return canvas
python类ndarray()的实例源码
def sizeFiltering(contours):
"""
this function filters out the smaller retroreflector (as well as any noise) by size
"""
if len(contours) == 0:
print "sizeFiltering: Error, no contours found"
return 0
big = contours[0]
for c in contours:
if type(c) and type(big) == numpy.ndarray:
if cv2.contourArea(c) > cv2.contourArea(big):
big = c
else:
print type(c) and type(big)
return 0
x,y,w,h = cv2.boundingRect(big)
return big
def generate(self, sess, conv_hidden, start_word_id, temperature, max_length, stop_word_id):
state = sess.run(self.cell.zero_state(1, tf.float32))
x = [[start_word_id]]
sent = [start_word_id]
for _ in xrange(max_length):
if type(conv_hidden) is np.ndarray:
#if conv_hidden != None:
probs, state = sess.run([self.probs, self.state], \
{self.x: x, self.initial_state: state, self.conv_hidden: conv_hidden})
else:
probs, state = sess.run([self.probs, self.state], \
{self.x: x, self.initial_state: state})
sent.append(self.sample(probs[0], temperature))
if sent[-1] == stop_word_id:
break
x = [[ sent[-1] ]]
return sent
#generate a sequence of words, given a topic
def __keytransform__(self, key):
if isinstance(key[0], np.ndarray):
shape = key[0].shape
dtype = key[0].dtype
i = key[1]
zero = True if len(key) == 2 else key[2]
elif isinstance(key[0], tuple):
if len(key) == 3:
shape, dtype, i = key
zero = True
elif len(key) == 4:
shape, dtype, i, zero = key
else:
raise TypeError("Wrong type of key for work array")
assert isinstance(zero, bool)
assert isinstance(i, int)
self.fillzero = zero
return (shape, np.dtype(dtype), i)
def apply(self, old_values, step):
"""Apply the boundary.
Args:
old_values: Old values of the points in the boundary.
step: Time step of the simulation (required if signals are to be applied).
Returns:
New values for the points in the boundary.
"""
if np.ndim(self.value) == 0 or \
(np.ndim(self.value) == 1 and type(self.value) == list):
# if a single value or a list of single values for each index is given
return self.additive * old_values + self.value
elif type(self.value) == np.ndarray:
# if a signal is given
return self.additive * old_values + self.value[step]
else:
# if a list of signals for each index is given
return [self.additive * old_values[ii] + signal[step]
for ii, signal in enumerate(self.value)]
def test_accuracy_full_batch(tokens, features, mini_batch_size, word_attn, sent_attn, th=0.5):
p = []
l = []
cnt = 0
g = gen_minibatch1(tokens, features, mini_batch_size, False)
for token, feature in g:
if cnt % 100 == 0:
print(cnt)
cnt +=1
# print token.size()
# y_pred = get_predictions(token, word_attn, sent_attn)
# print y_pred
y_pred = get_predictions(token, feature, word_attn, sent_attn)
# print y_pred
# _, y_pred = torch.max(y_pred, 1)
# y_pred = y_pred[:, 1]
# print y_pred
p.append(np.ndarray.flatten(y_pred.data.cpu().numpy()))
p = [item for sublist in p for item in sublist]
p = np.array(p)
return p
def __init__(self, N, V, tree_prior, config):
"""Initialize a model with an empty subsample.
Args:
N (int): Number of rows in the dataset.
V (int): Number of columns (features) in the dataset.
tree_prior: A [K]-shaped numpy array of prior edge log odds, where
K is the number of edges in the complete graph on V vertices.
config: A global config dict.
"""
assert isinstance(N, int)
assert isinstance(V, int)
assert isinstance(tree_prior, np.ndarray)
assert isinstance(config, dict)
K = V * (V - 1) // 2 # Number of edges in complete graph.
assert V <= 32768, 'Invalid # features > 32768: {}'.format(V)
assert tree_prior.shape == (K, )
assert tree_prior.dtype == np.float32
self._config = config.copy()
self._num_rows = N
self._tree_prior = tree_prior
self._tree = TreeStructure(V)
assert self._tree.num_vertices == V
self._program = make_propagation_program(self._tree.tree_grid)
self._added_rows = set()
def __init__(self, data, tree_prior, config):
"""Initialize a model with an empty subsample.
Args:
data: An [N, V]-shaped numpy array of real-valued data.
tree_prior: A [K]-shaped numpy array of prior edge log odds, where
K is the number of edges in the complete graph on V vertices.
config: A global config dict.
"""
assert isinstance(data, np.ndarray)
data = np.asarray(data, np.float32)
assert len(data.shape) == 2
N, V = data.shape
D = config['model_latent_dim']
E = V - 1 # Number of edges in the tree.
TreeTrainer.__init__(self, N, V, tree_prior, config)
self._data = data
self._latent = np.zeros([N, V, D], np.float32)
# This is symmetric positive definite.
self._vert_ss = np.zeros([V, D, D], np.float32)
# This is arbitrary (not necessarily symmetric).
self._edge_ss = np.zeros([E, D, D], np.float32)
# This represents (count, mean, covariance).
self._feat_ss = np.zeros([V, D, 1 + 1 + D], np.float32)
def set_type2000_format(format=list):
"""
Sets the data format returned when reading in type 2000 files.
The default is 'list', meaning a list of NumPy arrays, where the
length of each array is equal to the frame size. To return type 2000
data as a 2-d array, <format> should be 'numpy.ndarray', e.g.:
import bluefile, numpy
bluefile.set_type2000_format(numpy.ndarray)
Note that <format> is expected to a type object.
"""
global _type2000_format
if format not in [ list, numpy.ndarray ]:
raise TypeError, 'Only list and numpy.ndarray are supported'
_type2000_format = format
def array_stream(func):
"""
Decorates streaming functions to make sure that the stream
is a stream of ndarrays. Objects that are not arrays are transformed
into arrays. If the stream is in fact a single ndarray, this ndarray
is repackaged into a sequence of length 1.
The first argument of the decorated function is assumed to be an iterable of
arrays, or an iterable of objects that can be casted to arrays.
"""
@wraps(func) # thanks functools
def decorated(arrays, *args, **kwargs):
if isinstance(arrays, ndarray):
arrays = (arrays,)
return func(map(atleast_1d, arrays), *args, **kwargs)
return decorated
def iload(files, load_func, **kwargs):
"""
Create a stream of arrays from files, which are loaded lazily.
Parameters
----------
pattern : iterable of str or str
Either an iterable of filenames or a glob-like pattern str.
load_func : callable, optional
Function taking a filename as its first arguments
kwargs
Keyword arguments are passed to ``load_func``.
Yields
------
arr: `~numpy.ndarray`
Loaded data.
"""
if isinstance(files, str):
files = iglob(files)
files = iter(files)
yield from map(partial(load_func, **kwargs), files)
# pmap does not support local functions
def _get_fixed_params(im):
""" Parameters that the user has no influence on. Mostly chosen
bases on the input images.
"""
p = Parameters()
if not isinstance(im, np.ndarray):
return p
# Dimension of the inputs
p.FixedImageDimension = im.ndim
p.MovingImageDimension = im.ndim
# Always write result, so I can verify
p.WriteResultImage = True
# How to write the result
tmp = DTYPE_NP2ITK[im.dtype.name]
p.ResultImagePixelType = tmp.split('_')[-1].lower()
p.ResultImageFormat = "mhd"
# Done
return p
def __init__(self, skimage_function=None, **function_params):
'''
:param skimage_function: transformation function from skimage
'''
accepted_types = [
list, np.ndarray, np.array
]
if skimage_function is None:
raise Exception('Please specify transform function from scikit-image'
' http://scikit-image.org/docs/dev/api/skimage.transform.html')
def image_transform_function(img):
return skimage_function(img, **function_params)
super(ImageTransformer, self).__init__(data_types=accepted_types,
columns=None,
transform_function=image_transform_function)
def test_image_transformation():
s = XSeries([generate_image(False) for _ in range(100)])
try:
image_transformer = ImageTransformer().fit()
assert False
except:
assert True
image_transformer = ImageTransformer(skimage_transform.hough_circle, radius=5).fit()
s_transformed = image_transformer.transform(s)
assert s_transformed.data_type == np.ndarray
image_transformer = ImageTransformer(skimage_transform.resize, output_shape=(10, 10)).fit()
s_transformed = image_transformer.transform(s)
assert s_transformed.data_type == np.ndarray
def compute_nearest_neighbors(submatrix, balltree, k, row_start):
""" Compute k nearest neighbors on a submatrix
Args: submatrix (np.ndarray): Data submatrix
balltree: Nearest neighbor index (from sklearn)
k: number of nearest neigbors to compute
row_start: row offset into larger matrix
Returns a COO sparse adjacency matrix of nearest neighbor relations as (i,j,x)"""
nn_dist, nn_idx = balltree.query(submatrix, k=k+1)
# Remove the self-as-neighbors
nn_idx = nn_idx[:,1:]
nn_dist = nn_dist[:,1:]
# Construct a COO sparse matrix of edges and distances
i = np.repeat(row_start + np.arange(nn_idx.shape[0]), k)
j = nn_idx.ravel().astype(int)
return (i, j, nn_dist.ravel())
def plot_loop(self, image, it):
"""
The actual function that updates the data in the plot initialized in :meth:`~.init`
:param image: The image that is recorded with :class:`~.PlotRecorder`. It should be a 2-D numpy array
:param it: The iteration number (independent of the actual x value)
:return:
"""
logger.debug("Plotting %s in %s", self.var_name, self.entity_name)
assert isinstance(image, np.ndarray), "The passed in image should by a numpy array"
assert len(image.shape) == 2, "The image to be shown should be 2-dimensional"
if it == 0:
self.im = self.ax.imshow(image, **self.imshow_kwargs)
else:
if it % self.plot_frequency == 0:
self.im.set_array(image)
def _prepare_image(I):
assert isinstance(I, np.ndarray), 'plugin error, should pass numpy array here'
assert I.ndim == 2 or I.ndim == 3 or I.ndim == 4
if I.ndim == 4: # NCHW
if I.shape[1] == 1: # N1HW
I = np.concatenate((I, I, I), 1) # N3HW
assert I.shape[1] == 3
I = make_grid(I) # 3xHxW
if I.ndim == 3 and I.shape[0] == 1: # 1xHxW
I = np.concatenate((I, I, I), 0) # 3xHxW
if I.ndim == 2: # HxW
I = np.expand_dims(I, 0) # 1xHxW
I = np.concatenate((I, I, I), 0) # 3xHxW
I = I.transpose(1, 2, 0)
return I
def check_numeric_input(self, input_name, input_value):
if type(input_value) is np.ndarray:
if input_value.size == self.P:
setattr(self, input_name, input_value)
elif input_value.size == 1:
setattr(self, input_name, input_value*np.ones(self.P))
else:
raise ValueError("length of %s is %d; should be %d" % (input_name, input_value.size, self.P))
elif type(input_value) is float or type(input_value) is int:
setattr(self, input_name, float(input_value)*np.ones(self.P))
elif type(input_value) is list:
if len(input_value) == self.P:
setattr(self, input_name, np.array([float(x) for x in input_value]))
elif len(input_value) == 1:
setattr(self, input_name, np.array([float(x) for x in input_value]) * np.ones(self.P))
else:
raise ValueError("length of %s is %d; should be %d" % (input_name, len(input_value), self.P))
else:
raise ValueError("user provided %s with an unsupported type" % (input_name))
def load_nifti(filename, with_affine=False):
"""
load image from NIFTI file
Parameters
----------
filename : str
filename of NIFTI file
with_affine : bool
if True, returns affine parameters
Returns
-------
data : np.ndarray
image data
"""
img = nib.load(filename)
data = img.get_data()
data = np.copy(data, order="C")
if with_affine:
return data, img.affine
return data
def update_image_property(self, property_name, property_data, erase_property=False):
if isinstance(property_data,list) or isinstance(property_data,np.ndarray):
assert len(property_data) == len(self._labels)
property_keys = self._labels
elif isinstance(property_data,dict) or isinstance(property_data,array_dict):
property_keys = np.sort(property_data.keys())
property_data = [property_data[l] for l in property_keys]
if property_name in self._properties.keys():
if erase_property:
self._properties[property_name] = array_dict(property_data,keys=property_keys)
else:
for l,v in zip(property_keys,property_data):
self._properties[property_name][l] = v
else:
print "Creating property ",property_name," on image"
self._properties[property_name] = array_dict(property_data,keys=property_keys)
def spatial_image_analysis_property(sia, property_name, labels=None):
if property_name == 'volume':
property_data = sia.volume(labels)
elif property_name == 'neighborhood_size':
property_data = sia.neighbors_number(labels)
elif property_name == 'shape_anisotropy':
inertia_axes_vectors, inertia_axes_values = sia.inertia_axis(labels)
property_data = [fractional_anisotropy(inertia_axes_values[l]) for l in labels]
elif property_name == 'gaussian_curvature':
property_data = sia.gaussian_curvature_CGAL(labels)
else:
property_data = dict(zip(labels,labels))
if isinstance(property_data,np.ndarray) or isinstance(property_data,list):
property_data = array_dict(property_data, keys=labels)
elif isinstance(property_data,dict):
property_data = array_dict(property_data)
return property_data
def __init__(self, buf, offset = 0):
# Accelerate class attributes
self._encode = self.encode
self._dtype = self.dtype
self._xxh = self.xxh
# Initialize buffer
if offset:
self._buf = self._likebuf = buffer(buf, offset)
else:
self._buf = buf
self._likebuf = _likebuffer(buf)
# Parse header and map index
self.index_elements, self.index_offset = self._Header.unpack_from(self._buf, 0)
self.index = numpy.ndarray(buffer = self._buf,
offset = self.index_offset,
dtype = self.dtype,
shape = (self.index_elements, 3))
def validate_dict(self,a_dict):
#Check keys
for key,val in self.dict.items():
if not key in a_dict.keys():
raise ValueError('key:',key,'was not in a_dict.keys()')
for key,val in a_dict.items():
#Check same keys
if not key in self.dict.keys():
raise ValueError('argument key:',key,'was not in self.dict')
if isinstance(val,np.ndarray):
#print('ndarray')
my_val=self.dict[key]
if not np.all(val.shape[1:]==my_val.shape[1:]):
raise ValueError('key:',key,'value shape',val.shape,'does\
not match existing shape',my_val.shape)
else: #scalar
a_val=np.array([[val]])#[1,1]shape array
my_val=self.dict[key]
if not np.all(my_val.shape[1:]==a_val.shape[1:]):
raise ValueError('key:',key,'value shape',val.shape,'does\
not match existing shape',my_val.shape)
def roiChanged(self):
if self.image is None:
return
image = self.getProcessedImage()
if image.ndim == 2:
axes = (0, 1)
elif image.ndim == 3:
axes = (1, 2)
else:
return
data, coords = self.roi.getArrayRegion(image.view(np.ndarray), self.imageItem, axes, returnMappedCoords=True)
if data is not None:
while data.ndim > 1:
data = data.mean(axis=1)
if image.ndim == 3:
self.roiCurve.setData(y=data, x=self.tVals)
else:
while coords.ndim > 2:
coords = coords[:,:,0]
coords = coords - coords[:,0,np.newaxis]
xvals = (coords**2).sum(axis=0) ** 0.5
self.roiCurve.setData(y=data, x=xvals)
def dataType(obj):
if hasattr(obj, '__len__') and len(obj) == 0:
return 'empty'
if isinstance(obj, dict):
return 'dictOfLists'
elif isSequence(obj):
first = obj[0]
if (hasattr(obj, 'implements') and obj.implements('MetaArray')):
return 'MetaArray'
elif isinstance(obj, np.ndarray):
if obj.ndim == 1:
if obj.dtype.names is None:
return 'listOfValues'
else:
return 'recarray'
elif obj.ndim == 2 and obj.dtype.names is None and obj.shape[1] == 2:
return 'Nx2array'
else:
raise Exception('array shape must be (N,) or (N,2); got %s instead' % str(obj.shape))
elif isinstance(first, dict):
return 'listOfDicts'
else:
return 'listOfValues'
def _plotMetaArray(self, arr, x=None, autoLabel=True, **kargs):
inf = arr.infoCopy()
if arr.ndim != 1:
raise Exception('can only automatically plot 1 dimensional arrays.')
## create curve
try:
xv = arr.xvals(0)
except:
if x is None:
xv = np.arange(arr.shape[0])
else:
xv = x
c = PlotCurveItem(**kargs)
c.setData(x=xv, y=arr.view(np.ndarray))
if autoLabel:
name = arr._info[0].get('name', None)
units = arr._info[0].get('units', None)
self.setLabel('bottom', text=name, units=units)
name = arr._info[1].get('name', None)
units = arr._info[1].get('units', None)
self.setLabel('left', text=name, units=units)
return c
def setPen(self, *args, **kargs):
"""Set the pen(s) used to draw the outline around each spot.
If a list or array is provided, then the pen for each spot will be set separately.
Otherwise, the arguments are passed to pg.mkPen and used as the default pen for
all spots which do not have a pen explicitly set."""
update = kargs.pop('update', True)
dataSet = kargs.pop('dataSet', self.data)
if len(args) == 1 and (isinstance(args[0], np.ndarray) or isinstance(args[0], list)):
pens = args[0]
if 'mask' in kargs and kargs['mask'] is not None:
pens = pens[kargs['mask']]
if len(pens) != len(dataSet):
raise Exception("Number of pens does not match number of points (%d != %d)" % (len(pens), len(dataSet)))
dataSet['pen'] = pens
else:
self.opts['pen'] = fn.mkPen(*args, **kargs)
dataSet['sourceRect'] = None
if update:
self.updateSpots(dataSet)
def setBrush(self, *args, **kargs):
"""Set the brush(es) used to fill the interior of each spot.
If a list or array is provided, then the brush for each spot will be set separately.
Otherwise, the arguments are passed to pg.mkBrush and used as the default brush for
all spots which do not have a brush explicitly set."""
update = kargs.pop('update', True)
dataSet = kargs.pop('dataSet', self.data)
if len(args) == 1 and (isinstance(args[0], np.ndarray) or isinstance(args[0], list)):
brushes = args[0]
if 'mask' in kargs and kargs['mask'] is not None:
brushes = brushes[kargs['mask']]
if len(brushes) != len(dataSet):
raise Exception("Number of brushes does not match number of points (%d != %d)" % (len(brushes), len(dataSet)))
dataSet['brush'] = brushes
else:
self.opts['brush'] = fn.mkBrush(*args, **kargs)
#self._spotPixmap = None
dataSet['sourceRect'] = None
if update:
self.updateSpots(dataSet)
def setSymbol(self, symbol, update=True, dataSet=None, mask=None):
"""Set the symbol(s) used to draw each spot.
If a list or array is provided, then the symbol for each spot will be set separately.
Otherwise, the argument will be used as the default symbol for
all spots which do not have a symbol explicitly set."""
if dataSet is None:
dataSet = self.data
if isinstance(symbol, np.ndarray) or isinstance(symbol, list):
symbols = symbol
if mask is not None:
symbols = symbols[mask]
if len(symbols) != len(dataSet):
raise Exception("Number of symbols does not match number of points (%d != %d)" % (len(symbols), len(dataSet)))
dataSet['symbol'] = symbols
else:
self.opts['symbol'] = symbol
self._spotPixmap = None
dataSet['sourceRect'] = None
if update:
self.updateSpots(dataSet)
def setSize(self, size, update=True, dataSet=None, mask=None):
"""Set the size(s) used to draw each spot.
If a list or array is provided, then the size for each spot will be set separately.
Otherwise, the argument will be used as the default size for
all spots which do not have a size explicitly set."""
if dataSet is None:
dataSet = self.data
if isinstance(size, np.ndarray) or isinstance(size, list):
sizes = size
if mask is not None:
sizes = sizes[mask]
if len(sizes) != len(dataSet):
raise Exception("Number of sizes does not match number of points (%d != %d)" % (len(sizes), len(dataSet)))
dataSet['size'] = sizes
else:
self.opts['size'] = size
self._spotPixmap = None
dataSet['sourceRect'] = None
if update:
self.updateSpots(dataSet)