def saveHintonPlot(self, matrix, num_tests, max_weight=None, ax=None):
"""Draw Hinton diagram for visualizing a weight matrix."""
fig,ax = plt.subplots(1,1)
if not max_weight:
max_weight = 2**np.ceil(np.log(np.abs(matrix).max())/np.log(2))
ax.patch.set_facecolor('gray')
ax.set_aspect('equal', 'box')
ax.xaxis.set_major_locator(plt.NullLocator())
ax.yaxis.set_major_locator(plt.NullLocator())
for (x, y), w in np.ndenumerate(matrix):
color = 'white' if w > 0 else 'black'
size = np.sqrt(np.abs(0.5*w/num_tests)) # Need to scale so that it is between 0 and 0.5
rect = plt.Rectangle([x - size / 2, y - size / 2], size, size,
facecolor=color, edgecolor=color)
ax.add_patch(rect)
ax.autoscale_view()
ax.invert_yaxis()
plt.savefig(self.figures_path + self.save_prefix + '-Hinton.eps')
plt.close()
python类ndenumerate()的实例源码
def cal_hist(self, t1, t2, data1_maxlen, hist_size):
mhist = np.zeros((data1_maxlen, hist_size), dtype=np.float32)
d1len = len(self.data1[t1])
if self.use_hist_feats:
assert (t1, t2) in self.hist_feats
caled_hist = np.reshape(self.hist_feats[(t1, t2)], (d1len, hist_size))
if d1len < data1_maxlen:
mhist[:d1len, :] = caled_hist[:, :]
else:
mhist[:, :] = caled_hist[:data1_maxlen, :]
else:
t1_rep = self.embed[self.data1[t1]]
t2_rep = self.embed[self.data2[t2]]
mm = t1_rep.dot(np.transpose(t2_rep))
for (i,j), v in np.ndenumerate(mm):
if i >= data1_maxlen:
break
vid = int((v + 1.) / 2. * ( hist_size - 1.))
mhist[i][vid] += 1.
mhist += 1.
mhist = np.log10(mhist)
return mhist
def cal_hist(self, t1, t2, data1_maxlen, hist_size):
mhist = np.zeros((data1_maxlen, hist_size), dtype=np.float32)
t1_cont = list(self.data1[t1])
t2_cont = list(self.data2[t2])
d1len = len(t1_cont)
if self.use_hist_feats:
assert (t1, t2) in self.hist_feats
caled_hist = np.reshape(self.hist_feats[(t1, t2)], (d1len, hist_size))
if d1len < data1_maxlen:
mhist[:d1len, :] = caled_hist[:, :]
else:
mhist[:, :] = caled_hist[:data1_maxlen, :]
else:
t1_rep = self.embed[t1_cont]
t2_rep = self.embed[t2_cont]
mm = t1_rep.dot(np.transpose(t2_rep))
for (i,j), v in np.ndenumerate(mm):
if i >= data1_maxlen:
break
vid = int((v + 1.) / 2. * ( hist_size - 1.))
mhist[i][vid] += 1.
mhist += 1.
mhist = np.log10(mhist)
return mhist
def cal_hist(self, t1, t2, data1_maxlen, hist_size):
mhist = np.zeros((data1_maxlen, hist_size), dtype=np.float32)
t1_cont = list(self.data1[t1])
t2_cont = list(self.data2[t2])
d1len = len(t1_cont)
if self.use_hist_feats:
assert (t1, t2) in self.hist_feats
curr_pair_feats = list(self.hist_feats[(t1, t2)])
caled_hist = np.reshape(curr_pair_feats, (d1len, hist_size))
if d1len < data1_maxlen:
mhist[:d1len, :] = caled_hist[:, :]
else:
mhist[:, :] = caled_hist[:data1_maxlen, :]
else:
t1_rep = self.embed[t1_cont]
t2_rep = self.embed[t2_cont]
mm = t1_rep.dot(np.transpose(t2_rep))
for (i,j), v in np.ndenumerate(mm):
if i >= data1_maxlen:
break
vid = int((v + 1.) / 2. * ( hist_size - 1.))
mhist[i][vid] += 1.
mhist += 1.
mhist = np.log10(mhist)
return mhist
def cal_hist(self, t1, t2, data1_maxlen, hist_size):
mhist = np.zeros((data1_maxlen, hist_size), dtype=np.float32)
t1_cont = list(self.data1[t1])
t2_cont = list(self.data2[t2])
d1len = len(t1_cont)
if self.use_hist_feats:
assert (t1, t2) in self.hist_feats
caled_hist = np.reshape(self.hist_feats[(t1, t2)], (d1len, hist_size))
if d1len < data1_maxlen:
mhist[:d1len, :] = caled_hist[:, :]
else:
mhist[:, :] = caled_hist[:data1_maxlen, :]
else:
t1_rep = self.embed[t1_cont]
t2_rep = self.embed[t2_cont]
mm = t1_rep.dot(np.transpose(t2_rep))
for (i,j), v in np.ndenumerate(mm):
if i >= data1_maxlen:
break
vid = int((v + 1.) / 2. * ( hist_size - 1.))
mhist[i][vid] += 1.
mhist += 1.
mhist = np.log10(mhist)
return mhist
def cal_hist(self, t1, t2, data1_maxlen, hist_size):
mhist = np.zeros((data1_maxlen, hist_size), dtype=np.float32)
t1_cont = list(self.data1[t1])
t2_cont = list(self.data2[t2])
d1len = len(t1_cont)
if self.use_hist_feats:
assert (t1, t2) in self.hist_feats
curr_pair_feats = list(self.hist_feats[(t1, t2)])
caled_hist = np.reshape(curr_pair_feats, (d1len, hist_size))
if d1len < data1_maxlen:
mhist[:d1len, :] = caled_hist[:, :]
else:
mhist[:, :] = caled_hist[:data1_maxlen, :]
else:
t1_rep = self.embed[t1_cont]
t2_rep = self.embed[t2_cont]
mm = t1_rep.dot(np.transpose(t2_rep))
for (i,j), v in np.ndenumerate(mm):
if i >= data1_maxlen:
break
vid = int((v + 1.) / 2. * ( hist_size - 1.))
mhist[i][vid] += 1.
mhist += 1.
mhist = np.log10(mhist)
return mhist
def write_pml_elems(sorted_pml_elems, pmlfile="elems_pml.dyn"):
"""Create a new elements file that the PML elements.
:param sorted_pml_elems:
:param pmlfile: default = elems_pml.dyn
:returns:
"""
from numpy import ndenumerate
pml = open(pmlfile, 'w')
pml.write('$ PML elements generated by bc.py\n')
pml.write('*ELEMENT_SOLID\n')
for i, e in ndenumerate(sorted_pml_elems):
pml.write('%i,%i,%i,%i,%i,%i,%i,%i,%i,%i\n' % (e['id'], e['pid'],
e['n1'], e['n2'],
e['n3'], e['n4'],
e['n5'], e['n6'],
e['n7'], e['n8']))
pml.write('*END\n')
pml.close()
return 0
def generate_data(sample_size=200, pd=[[0.4, 0.4], [0.1, 0.1]]):
pd = np.array(pd)
pd /= pd.sum()
offset = 50
bins = np.r_[np.zeros((1,)), np.cumsum(pd)]
bin_counts = np.histogram(np.random.rand(sample_size), bins)[0]
data = np.empty((0, 2))
targets = []
for ((i, j), p), count in zip(np.ndenumerate(pd), bin_counts):
xs = np.random.uniform(low=0.0, high=50.0, size=count) + j * offset
ys = np.random.uniform(low=0.0, high=50.0, size=count) + -i * offset
data = np.vstack((data, np.c_[xs, ys]))
if i == j:
targets.extend([1] * count)
else:
targets.extend([-1] * count)
return np.c_[data, targets]
def finite_diff_gradients(self, f, delta=1e-6):
"""
f is called without parameters, the changes in the parameters happen as a side effect
"""
gradients = dict()
fx = f()
for p in self.parameters_to_optimise:
original = self.parameters[p].get_value()
grad = np.zeros_like(original)
if np.prod(original.shape) > 1:
for index, _ in np.ndenumerate(original):
xh = original.copy()
xh[index] += delta
self.parameters[p].set_value(xh)
grad[index] = (f() - fx) / delta
self.parameters[p].set_value(original)
else:
xh = original.copy()
xh += delta
self.parameters[p].set_value(xh)
grad = (f() - fx) / delta
self.parameters[p].set_value(original)
gradients[p] = grad
return gradients
def test_reduce_sum_axis_zero():
ctx = ndarray.gpu(0)
shape = (500, 200, 100)
to_shape = (200, 100)
x = np.random.uniform(0, 20, shape).astype(np.float32)
arr_x = ndarray.array(x, ctx=ctx)
arr_y = ndarray.empty(to_shape, ctx=ctx)
gpu_op.reduce_sum_axis_zero(arr_x, arr_y)
y = arr_y.asnumpy()
y_ = np.sum(x, axis=0)
for index, _ in np.ndenumerate(y):
v = y[index]
v_ = y_[index]
if abs((v - v_) / v_) > 1e-4:
print(index, v, v_)
np.testing.assert_allclose(np.sum(x, axis=0), y, rtol=1e-5)
def _dateToISO(indict):
"""
covert datetimes to iso strings inside of datamodel attributes
"""
retdict = dmcopy(indict)
if isinstance(indict, dict):
for key in indict:
if isinstance(indict[key], datetime.datetime):
retdict[key] = retdict[key].isoformat()
elif hasattr(indict[key], '__iter__'):
for idx, el in enumerate(indict[key]):
if isinstance(el, datetime.datetime):
retdict[key][idx] = el.isoformat()
else:
if isinstance(indict, datetime.datetime):
retdict = retdict.isoformat()
elif hasattr(indict, '__iter__'):
retdict = numpy.asanyarray(indict)
for idx, el in numpy.ndenumerate(indict):
if isinstance(el, datetime.datetime):
retdict[idx] = el.isoformat()
return retdict
def _flatten_data(data, chart_cfg, switch_zy=False):
plot_axes_def = [(0, XAxis), (1, YAxis)]
# Inject categories into the axis definitions of the plot
if isinstance(data, NDFrame):
for i, plot_axis in plot_axes_def[:data.ndim]:
categories = data.axes[i]
# Skip numeric indices
if not categories.is_numeric():
chart_cfg = chart_cfg.inherit_many(plot_axis(categories=list(categories)))
data = [list(index) + [value] for index, value in list(np.ndenumerate(data))]
if switch_zy:
for i in xrange(len(data)):
tmp = data[i][-1]
data[i][-1] = data[i][-2]
data[i][-2] = tmp
return data, chart_cfg
def returnAmplitudeFromListOfFunctionValues(self, listOfFunctionValues, additive=False):
"""Helper function for the amplitude setting function
[f(x), g(y), ...]
additive=True => F(x, y, ...) = f(x) + g(y) + ...
additive=False => F(x, y, ...) = f(x) * g(y) * ..."""
output = self.functionSpaceZero()
if additive:
initialValue = 0.0
else:
initialValue = 1.0
for indexTuple, value in np.ndenumerate(output):
newValue = initialValue
for tupleIndex, tupleValue in enumerate(indexTuple):
if additive:
newValue += listOfFunctionValues[tupleIndex][tupleValue]
else:
newValue *= listOfFunctionValues[tupleIndex][tupleValue]
output[indexTuple] = newValue
return output
def generate(width, height, s, output):
canvas = np.zeros((height, width), dtype='float64')
max_d = math.sqrt(width**2 + height**2) / 2
offset_angular = 0
if (width >= height):
offset_angular = math.pi / 4
for (i, j), _ in np.ndenumerate(canvas):
y = height // 2 - i
x = j - width // 2
d = math.sqrt(x**2 + y**2)
t = math.atan2(y, x)
canvas[i,j] = (255 / 4) * \
(2 + radial_sin(d, s, t) + angular_sin (
d, t, max_d, s, offset_angular))
f = open(output, 'wb')
w = png.Writer(width, height, greyscale=True)
w.write(f, canvas)
f.close()
def sample(self):
self._update_m()
indices = np.ndenumerate(self.count_k_by_j)
lgg.debug('Sample m...')
for ind in indices:
j, k = ind[0]
count = ind[1]
if count > 0:
# Sample number of tables in j serving dishe k
params = self.prob_jk(j, k)
sample = categorical(params) + 1
else:
sample = 0
self.m[j, k] = sample
self.m_dotk = self.m.sum(0)
self.purge_empty_tables()
return self.m
def cartopy_xlim(self, geobounds):
"""Return the x extents in projected coordinates for cartopy.
Returns:
:obj:`list`: A pair of [xmin, xmax].
See Also:
:mod:`cartopy`, :mod:`matplotlib`
"""
try:
_ = len(geobounds)
except TypeError:
x_extents= self._cart_extents(geobounds)[0]
else:
extents = self._cart_extents(geobounds)
x_extents = np.empty(extents.shape, np.object)
for idxs, extent in np.ndenumerate(extents):
x_extents[idxs] = extent[0]
return x_extents
def cartopy_ylim(self, geobounds):
"""Return the y extents in projected coordinates for cartopy.
Returns:
:obj:`list`: A pair of [ymin, ymax].
See Also:
:mod:`cartopy`, :mod:`matplotlib`
"""
try:
_ = len(geobounds)
except TypeError:
y_extents= self._cart_extents(geobounds)[1]
else:
extents = self._cart_extents(geobounds)
y_extents = np.empty(extents.shape, np.object)
for idxs, extent in np.ndenumerate(extents):
y_extents[idxs] = extent[1]
return y_extents
def detect_face_12net(cls_prob,roi,out_side,scale,width,height,threshold):
in_side = 2*out_side+11
stride = 0
if out_side != 1:
stride = float(in_side-12)/(out_side-1)
boundingBox = []
for (x,y), prob in np.ndenumerate(cls_prob):
if(prob >= threshold):
original_x1 = int((stride*x + 1)*scale)
original_y1 = int((stride*y + 1)*scale)
original_w = int((12.0 -1)*scale)
original_h = int((12.0 -1)*scale)
original_x2 = original_x1 + original_w
original_y2 = original_y1 + original_h
rect = []
x1 = int(round(max(0 , original_x1 + original_w * roi[0][x][y])))
y1 = int(round(max(0 , original_y1 + original_h * roi[1][x][y])))
x2 = int(round(min(width , original_x2 + original_w * roi[2][x][y])))
y2 = int(round(min(height, original_y2 + original_h * roi[3][x][y])))
if x2>x1 and y2>y1:
rect = [x1,y1,x2,y2,prob]
boundingBox.append(rect)
return NMS(boundingBox,0.5,'iou')
def hinton(matrix, max_weight=None, ax=None):
"""Draw Hinton diagram for visualizing a weight matrix."""
ax = ax if ax is not None else plt.gca()
if not max_weight:
max_weight = 2 ** np.ceil(np.log(np.abs(matrix).max()) / np.log(2))
ax.patch.set_facecolor('gray')
ax.set_aspect('equal', 'box')
ax.xaxis.set_major_locator(plt.NullLocator())
ax.yaxis.set_major_locator(plt.NullLocator())
for (x, y), w in np.ndenumerate(matrix):
color = 'white' if w > 0 else 'black'
size = np.sqrt(np.abs(w) / max_weight)
rect = plt.Rectangle([x - size / 2, y - size / 2], size, size,
facecolor=color, edgecolor=color)
ax.add_patch(rect)
ax.autoscale_view()
ax.invert_yaxis()
def sample_crp_tablecounts(concentration,customers,colweights):
m = np.zeros_like(customers)
tot = customers.sum()
randseq = np.random.random(tot)
starts = np.empty_like(customers)
starts[0,0] = 0
starts.flat[1:] = np.cumsum(np.ravel(customers)[:customers.size-1])
for (i,j), n in np.ndenumerate(customers):
w = colweights[j]
for k in xrange(n):
m[i,j] += randseq[starts[i,j]+k] \
< (concentration * w) / (k + concentration * w)
return m
### Entropy
def sample_crp_tablecounts(concentration,customers,colweights):
m = np.zeros_like(customers)
tot = customers.sum()
randseq = np.random.random(tot)
starts = np.empty_like(customers)
starts[0,0] = 0
starts.flat[1:] = np.cumsum(np.ravel(customers)[:customers.size-1])
for (i,j), n in np.ndenumerate(customers):
w = colweights[j]
for k in xrange(n):
m[i,j] += randseq[starts[i,j]+k] \
< (concentration * w) / (k + concentration * w)
return m
### Entropy
def hmm_trans_matrix(self):
# NOTE: more general version, allows different delays, o/w we could
# construct with np.kron
if self._hmm_trans_matrix is None:
ps, delays = map(np.array,zip(*[(d.p,d.delay) for d in self.dur_distns]))
starts, ends = cumsum(delays,strict=True), cumsum(delays,strict=False)
trans_matrix = self._hmm_trans_matrix = np.zeros((ends[-1],ends[-1]))
for (i,j), Aij in np.ndenumerate(self.trans_matrix):
block = trans_matrix[starts[i]:ends[i],starts[j]:ends[j]]
if i == j:
block[:-1,1:] = np.eye(block.shape[0]-1)
block[-1,-1] = 1-ps[i]
else:
block[-1,0] = ps[j]*Aij
return self._hmm_trans_matrix
def mf_bwd_trans_matrix(self):
rs = self.rs
starts, ends = cumsum(rs,strict=True), cumsum(rs,strict=False)
trans_matrix = np.zeros((ends[-1],ends[-1]))
Elnps, Eln1mps = zip(*[d._fixedr_distns[d.ridx]._mf_expected_statistics() for d in self.dur_distns])
Eps, E1mps = np.exp(Elnps), np.exp(Eln1mps) # NOTE: actually exp(E[ln(p)]) etc
enters = self.mf_bwd_enter_rows(rs,Eps,E1mps)
for (i,j), Aij in np.ndenumerate(self.mf_trans_matrix):
block = trans_matrix[starts[i]:ends[i],starts[j]:ends[j]]
block[-1,:] = Aij * eE1mps[i] * enters[j]
if i == j:
block[...] += np.diag(np.repeat(eEps[i],rs[i])) \
+ np.diag(np.repeat(eE1mps[i],rs[i]-1),k=1)
assert np.all(trans_matrix >= 0)
return trans_matrix
def hmm_trans_matrix_orig(self):
rs, ps, delays = self.rs, self.ps, self.delays
starts, ends = cumsum(rs+delays,strict=True), cumsum(rs+delays,strict=False)
trans_matrix = np.zeros((ends[-1],ends[-1]))
enters = self.bwd_enter_rows
for (i,j), Aij in np.ndenumerate(self.trans_matrix):
block = trans_matrix[starts[i]:ends[i],starts[j]:ends[j]]
if delays[i] == 0:
block[-1,:rs[j]] = Aij * enters[j] * (1-ps[i])
else:
block[-1,:rs[j]] = Aij * enters[j]
if i == j:
block[:rs[i],:rs[i]] += \
np.diag(np.repeat(ps[i],rs[i])) + np.diag(np.repeat(1-ps[i],rs[i]-1),k=1)
if delays[i] > 0:
block[rs[i]-1,rs[i]] = (1-ps[i])
block[rs[i]:,rs[i]:] = np.eye(delays[i],k=1)
assert np.allclose(trans_matrix.sum(1),1.)
return trans_matrix
def hmm_trans_matrix_1(self):
rs, ps, delays = self.rs, self.ps, self.delays
starts, ends = cumsum(rs+delays,strict=True), cumsum(rs+delays,strict=False)
trans_matrix = np.zeros((ends[-1],ends[-1]))
enters = self.bwd_enter_rows
for (i,j), Aij in np.ndenumerate(self.trans_matrix):
block = trans_matrix[starts[i]:ends[i],starts[j]:ends[j]]
block[-1,:rs[j]] = Aij * enters[j] * (1-ps[i])
if i == j:
block[-rs[i]:,-rs[i]:] += \
np.diag(np.repeat(ps[i],rs[i])) + np.diag(np.repeat(1-ps[i],rs[i]-1),k=1)
if delays[i] > 0:
block[:delays[i]:,:delays[i]] = np.eye(delays[i],k=1)
block[delays[i]-1,delays[i]] = 1
assert np.allclose(trans_matrix.sum(1),1.)
return trans_matrix
def hmm_trans_matrix_2(self):
rs, ps, delays = self.rs, self.ps, self.delays
starts, ends = cumsum(rs+delays,strict=True), cumsum(rs+delays,strict=False)
trans_matrix = np.zeros((ends[-1],ends[-1]))
enters = self.bwd_enter_rows
for (i,j), Aij in np.ndenumerate(self.trans_matrix):
block = trans_matrix[starts[i]:ends[i],starts[j]:ends[j]]
block[-1,0] = Aij * (1-ps[i])
if i == j:
block[-rs[i]:,-rs[i]:] += \
np.diag(np.repeat(ps[i],rs[i])) + np.diag(np.repeat(1-ps[i],rs[i]-1),k=1)
if delays[i] > 0:
block[:delays[i]:,:delays[i]] = np.eye(delays[i],k=1)
block[delays[i]-1,-rs[i]:] = enters[i]
assert np.allclose(trans_matrix.sum(1),1.)
return trans_matrix
def convert_numpy_array_to_line_chart(array, ntype):
array = np.sort(array)[::-1]
rows = []
previous_count = None
for (index,), count in np.ndenumerate(array):
if index == 0 or index == len(array)-1:
rows.append([index, ntype(count)])
elif previous_count != count:
previous_index = rows[-1][0]
if previous_index != index - 1:
rows.append([index - 1, ntype(previous_count)])
rows.append([index, ntype(count)])
previous_count = count
return rows
def cal_hist(t1_rep, t2_rep, qnum, hist_size):
#qnum = len(t1_rep)
mhist = np.zeros((qnum, hist_size), dtype=np.float32)
mm = t1_rep.dot(np.transpose(t2_rep))
for (i,j), v in np.ndenumerate(mm):
if i >= qnum:
break
vid = int((v + 1.) / 2. * (hist_size - 1.))
mhist[i][vid] += 1.
mhist += 1.
mhist = np.log10(mhist)
return mhist.flatten()
def cal_binsum(t1_rep, t2_rep, qnum, bin_num):
mbinsum = np.zeros((qnum, bin_num), dtype=np.float32)
mm = t1_rep.dot(np.transpose(t2_rep))
for (i, j), v in np.ndenumerate(mm):
if i >= qnum:
break
vid = int((v + 1.) / 2. * (bin_num - 1.))
mbinsum[i][vid] += v
#mhist += 1. # smooth is not needed for computing bin sum
#mhist = np.log10(mhist) # not needed for computing bin sum
return mbinsum.flatten()
def cal_binsum(t1_rep, t2_rep, qnum, bin_num):
mbinsum = np.zeros((qnum, bin_num), dtype=np.float32)
mm = t1_rep.dot(np.transpose(t2_rep))
for (i, j), v in np.ndenumerate(mm):
if i >= qnum:
break
vid = int((v + 1.) / 2. * (bin_num - 1.))
mbinsum[i][vid] += v
#mhist += 1. # smooth is not needed for computing bin sum
#mhist = np.log10(mhist) # not needed for computing bin sum
return mbinsum.flatten()