def edge_LoG(I, sigma):
LoG = laplace(gaussian(I, sigma=sigma), ksize=3)
thres = np.absolute(LoG).mean() * 1.0
output = sp.zeros(LoG.shape)
w = output.shape[1]
h = output.shape[0]
for y in range(1, h - 1):
for x in range(1, w - 1):
patch = LoG[y - 1:y + 2, x - 1:x + 2]
p = LoG[y, x]
maxP = patch.max()
minP = patch.min()
if p > 0:
zeroCross = True if minP < 0 else False
else:
zeroCross = True if maxP > 0 else False
if ((maxP - minP) > thres) and zeroCross:
output[y, x] = 1
#FIXME: It is necesary to define if return the closing of the output or just the output
#return binary_closing(output)
return output
python类zeros()的实例源码
def test_fill_missing_fields(self):
""" Test several cases for the fill_missing_fields method
"""
empty_columns = []
columns = ["test1"]
empty_df = pandas.DataFrame()
# With empty dataframe and any columns, this always returns empty dataframe
# A DataFrame with columns but not data is an empty DataFrame
self.assertTrue(Format().fill_missing_fields(empty_df, empty_columns).empty)
self.assertTrue(Format().fill_missing_fields(empty_df, columns).empty)
self.assertEqual(columns, Format().fill_missing_fields(empty_df, columns).columns)
# With a dataframe with some data, this returns a non-empty dataframe
df = empty_df.copy()
df["test"] = scipy.zeros(10)
self.assertFalse(Format().fill_missing_fields(df, empty_columns).empty)
def fill_missing_fields(self, data, columns):
""" This method fills with 0's missing fields
:param data: original Pandas dataframe
:param columns: list of columns to be filled in the DataFrame
:type data: pandas.DataFrame
:type columns: list of strings
:returns: Pandas dataframe with missing fields filled with 0's
:rtype: pandas.DataFrame
"""
for column in columns:
if column not in data.columns:
data[column] = scipy.zeros(len(data))
return data
def gap(data, refs=None, nrefs=20, ks=range(1,11), method=None):
shape = data.shape
if refs is None:
tops = data.max(axis=0)
bots = data.min(axis=0)
dists = scipy.matrix(scipy.diag(tops-bots))
rands = scipy.random.random_sample(size=(shape[0], shape[1], nrefs))
for i in range(nrefs):
rands[:, :, i] = rands[:, :, i]*dists+bots
else:
rands = refs
gaps = scipy.zeros((len(ks),))
for (i, k) in enumerate(ks):
g1 = method(n_clusters=k).fit(data)
(kmc, kml) = (g1.cluster_centers_, g1.labels_)
disp = sum([euclidean(data[m, :], kmc[kml[m], :]) for m in range(shape[0])])
refdisps = scipy.zeros((rands.shape[2],))
for j in range(rands.shape[2]):
g2 = method(n_clusters=k).fit(rands[:, :, j])
(kmc, kml) = (g2.cluster_centers_, g2.labels_)
refdisps[j] = sum([euclidean(rands[m, :, j], kmc[kml[m],:]) for m in range(shape[0])])
gaps[i] = scipy.log(scipy.mean(refdisps))-scipy.log(disp)
return gaps
def DistanceMatrix(coords):
'''Take a set of coordinates and calculate a matrix of
Euclidean distances between the points.'''
# we can assume that xs and ys are the same length
stops = coords.shape[1]
distMat = scipy.zeros((stops, stops))
# this will be symmetric, so we only need to calculate
# the upper triangular
for i in range(stops):
for j in range(i + 1, stops):
xdist = coords[0, i] - coords[0, j]
ydist = coords[1, i] - coords[1, j]
distMat[i, j] = math.sqrt(xdist**2 + ydist**2)
# add the transpose to make it symmetric
distMat = distMat + distMat.transpose()
return distMat
def compute_confusion_matrix(self,yp,yr):
'''
Compute the confusion matrix
'''
# Initialization
n = yp.size
C=int(yr.max())
self.confusion_matrix=sp.zeros((C,C))
# Compute confusion matrix
for i in range(n):
self.confusion_matrix[yp[i].astype(int)-1,yr[i].astype(int)-1] +=1
# Compute overall accuracy
self.OA=sp.sum(sp.diag(self.confusion_matrix))/n
# Compute Kappa
nl = sp.sum(self.confusion_matrix,axis=1)
nc = sp.sum(self.confusion_matrix,axis=0)
self.Kappa = ((n**2)*self.OA - sp.sum(nc*nl))/(n**2-sp.sum(nc*nl))
# TBD Variance du Kappa
def glmnet_softmax(x):
d = x.shape
nas = scipy.any(scipy.isnan(x), axis = 1)
if scipy.any(nas):
pclass = scipy.zeros([d[0], 1])*scipy.NaN
if scipy.sum(nas) < d[0]:
pclass2 = glmnet_softmax(x[~nas, :])
pclass[~nas] = pclass2
result = pclass
else:
maxdist = x[:, 1]
pclass = scipy.ones([d[0], 1])
for i in range(1, d[1], 1):
t = x[:, i] > maxdist
pclass[t] = i
maxdist[t] = x[t, i]
result = pclass
return(result)
#=========================
def softmax(x, gap = False):
d = x.shape
maxdist = x[:, 0]
pclass = scipy.zeros([d[0], 1], dtype = scipy.integer)
for i in range(1, d[1], 1):
l = x[:, i] > maxdist
pclass[l] = i
maxdist[l] = x[l, i]
if gap == True:
x = scipy.absolute(maxdist - x)
x[0:d[0], pclass] = x*scipy.ones([d[1], d[1]])
#gaps = pmin(x)# not sure what this means; gap is never called with True
raise ValueError('gap = True is not implemented yet')
result = dict()
if gap == True:
result['pclass'] = pclass
#result['gaps'] = gaps
raise ValueError('gap = True is not implemented yet')
else:
result['pclass'] = pclass;
return(result)
# end of softmax
# =========================================
def cvcompute(mat, weights, foldid, nlams):
if len(weights.shape) > 1:
weights = scipy.reshape(weights, [weights.shape[0], ])
wisum = scipy.bincount(foldid, weights = weights)
nfolds = scipy.amax(foldid) + 1
outmat = scipy.ones([nfolds, mat.shape[1]])*scipy.NaN
good = scipy.zeros([nfolds, mat.shape[1]])
mat[scipy.isinf(mat)] = scipy.NaN
for i in range(nfolds):
tf = foldid == i
mati = mat[tf, ]
wi = weights[tf, ]
outmat[i, :] = wtmean(mati, wi)
good[i, 0:nlams[i]] = 1
N = scipy.sum(good, axis = 0)
cvcpt = dict()
cvcpt['cvraw'] = outmat
cvcpt['weights'] = wisum
cvcpt['N'] = N
return(cvcpt)
# end of cvcompute
#=========================
def softmax(x, gap = False):
d = x.shape
maxdist = x[:, 0]
pclass = scipy.zeros([d[0], 1], dtype = scipy.integer)
for i in range(1, d[1], 1):
l = x[:, i] > maxdist
pclass[l] = i
maxdist[l] = x[l, i]
if gap == True:
x = scipy.absolute(maxdist - x)
x[0:d[0], pclass] = x*scipy.ones([d[1], d[1]])
#gaps = pmin(x)# not sure what this means; gap is never called with True
raise ValueError('gap = True is not implemented yet')
result = dict()
if gap == True:
result['pclass'] = pclass
#result['gaps'] = gaps
raise ValueError('gap = True is not implemented yet')
else:
result['pclass'] = pclass;
return(result)
# end of softmax
# =========================================
def cvcompute(mat, weights, foldid, nlams):
if len(weights.shape) > 1:
weights = scipy.reshape(weights, [weights.shape[0], ])
wisum = scipy.bincount(foldid, weights = weights)
nfolds = scipy.amax(foldid) + 1
outmat = scipy.ones([nfolds, mat.shape[1]])*scipy.NaN
good = scipy.zeros([nfolds, mat.shape[1]])
mat[scipy.isinf(mat)] = scipy.NaN
for i in range(nfolds):
tf = foldid == i
mati = mat[tf, ]
wi = weights[tf, ]
outmat[i, :] = wtmean(mati, wi)
good[i, 0:nlams[i]] = 1
N = scipy.sum(good, axis = 0)
cvcpt = dict()
cvcpt['cvraw'] = outmat
cvcpt['weights'] = wisum
cvcpt['N'] = N
return(cvcpt)
# end of cvcompute
#=========================
def glmnet_softmax(x):
d = x.shape
nas = scipy.any(scipy.isnan(x), axis = 1)
if scipy.any(nas):
pclass = scipy.zeros([d[0], 1])*scipy.NaN
if scipy.sum(nas) < d[0]:
pclass2 = glmnet_softmax(x[~nas, :])
pclass[~nas] = pclass2
result = pclass
else:
maxdist = x[:, 1]
pclass = scipy.ones([d[0], 1])
for i in range(1, d[1], 1):
t = x[:, i] > maxdist
pclass[t] = i
maxdist[t] = x[t, i]
result = pclass
return(result)
#=========================
def softmax(x, gap = False):
d = x.shape
maxdist = x[:, 0]
pclass = scipy.zeros([d[0], 1], dtype = scipy.integer)
for i in range(1, d[1], 1):
l = x[:, i] > maxdist
pclass[l] = i
maxdist[l] = x[l, i]
if gap == True:
x = scipy.absolute(maxdist - x)
x[0:d[0], pclass] = x*scipy.ones([d[1], d[1]])
#gaps = pmin(x)# not sure what this means; gap is never called with True
raise ValueError('gap = True is not implemented yet')
result = dict()
if gap == True:
result['pclass'] = pclass
#result['gaps'] = gaps
raise ValueError('gap = True is not implemented yet')
else:
result['pclass'] = pclass;
return(result)
# end of softmax
# =========================================
def cvcompute(mat, weights, foldid, nlams):
if len(weights.shape) > 1:
weights = scipy.reshape(weights, [weights.shape[0], ])
wisum = scipy.bincount(foldid, weights = weights)
nfolds = scipy.amax(foldid) + 1
outmat = scipy.ones([nfolds, mat.shape[1]])*scipy.NaN
good = scipy.zeros([nfolds, mat.shape[1]])
mat[scipy.isinf(mat)] = scipy.NaN
for i in range(nfolds):
tf = foldid == i
mati = mat[tf, ]
wi = weights[tf, ]
outmat[i, :] = wtmean(mati, wi)
good[i, 0:nlams[i]] = 1
N = scipy.sum(good, axis = 0)
cvcpt = dict()
cvcpt['cvraw'] = outmat
cvcpt['weights'] = wisum
cvcpt['N'] = N
return(cvcpt)
# end of cvcompute
#=========================
def glmnet_softmax(x):
d = x.shape
nas = scipy.any(scipy.isnan(x), axis = 1)
if scipy.any(nas):
pclass = scipy.zeros([d[0], 1])*scipy.NaN
if scipy.sum(nas) < d[0]:
pclass2 = glmnet_softmax(x[~nas, :])
pclass[~nas] = pclass2
result = pclass
else:
maxdist = x[:, 1]
pclass = scipy.ones([d[0], 1])
for i in range(1, d[1], 1):
t = x[:, i] > maxdist
pclass[t] = i
maxdist[t] = x[t, i]
result = pclass
return(result)
#=========================
def softmax(x, gap = False):
d = x.shape
maxdist = x[:, 0]
pclass = scipy.zeros([d[0], 1], dtype = scipy.integer)
for i in range(1, d[1], 1):
l = x[:, i] > maxdist
pclass[l] = i
maxdist[l] = x[l, i]
if gap == True:
x = scipy.absolute(maxdist - x)
x[0:d[0], pclass] = x*scipy.ones([d[1], d[1]])
#gaps = pmin(x)# not sure what this means; gap is never called with True
raise ValueError('gap = True is not implemented yet')
result = dict()
if gap == True:
result['pclass'] = pclass
#result['gaps'] = gaps
raise ValueError('gap = True is not implemented yet')
else:
result['pclass'] = pclass;
return(result)
# end of softmax
# =========================================
def cvcompute(mat, weights, foldid, nlams):
if len(weights.shape) > 1:
weights = scipy.reshape(weights, [weights.shape[0], ])
wisum = scipy.bincount(foldid, weights = weights)
nfolds = scipy.amax(foldid) + 1
outmat = scipy.ones([nfolds, mat.shape[1]])*scipy.NaN
good = scipy.zeros([nfolds, mat.shape[1]])
mat[scipy.isinf(mat)] = scipy.NaN
for i in range(nfolds):
tf = foldid == i
mati = mat[tf, ]
wi = weights[tf, ]
outmat[i, :] = wtmean(mati, wi)
good[i, 0:nlams[i]] = 1
N = scipy.sum(good, axis = 0)
cvcpt = dict()
cvcpt['cvraw'] = outmat
cvcpt['weights'] = wisum
cvcpt['N'] = N
return(cvcpt)
# end of cvcompute
#=========================
def _reshape_signal(self,sindex,kindex,ssignal,ksignal):
def reshape(signal,siglen,winsize):
length =(siglen/winsize+1)*winsize
ret=sp.zeros(length, sp.float32)
ret[0:siglen] = signal
return ret
slen = len(ssignal)-sindex
klen = len(ksignal)-kindex
length = 0
if slen>klen:
length = klen
else:
length = slen
ssignal=reshape(ssignal[sindex:sindex+length],length,self._winsize)
ksignal=reshape(ksignal[kindex:kindex+length],length,self._winsize)
return ssignal,ksignal
def istft(X, scale = 1, overlap=4):
fftsize=(X.shape[1]-1)*2
hop = fftsize / overlap
w = scipy.hanning(fftsize+1)[:-1]
x = scipy.zeros(X.shape[0]*hop)
wsum = scipy.zeros(X.shape[0]*hop)
for n,i in enumerate(range(0, len(x)-fftsize, hop)):
x[i:i+fftsize] += scipy.real(np.fft.irfft(X[n])) * w # overlap-add
wsum[i:i+fftsize] += w ** 2.
pos = wsum != 0
x[pos] /= wsum[pos]
x = x * scale
return x.astype(np.int16)
def compute_confusion_matrix(self,yp,yr):
'''
Compute the confusion matrix
'''
# Initialization
n = yp.size
C=int(yr.max())
self.confusion_matrix=sp.zeros((C,C))
# Compute confusion matrix
for i in range(n):
self.confusion_matrix[yp[i].astype(int)-1,yr[i].astype(int)-1] +=1
# Compute overall accuracy
self.OA=sp.sum(sp.diag(self.confusion_matrix))/n
# Compute Kappa
nl = sp.sum(self.confusion_matrix,axis=1)
nc = sp.sum(self.confusion_matrix,axis=0)
self.Kappa = ((n**2)*self.OA - sp.sum(nc*nl))/(n**2-sp.sum(nc*nl))
# TBD Variance du Kappa
def visulize_matches(matches, k2, k1, img2, img1):
""" Visualize SIFT keypoint matches."""
import scipy as sp
img2 = cv.cvtColor(img2, cv.COLOR_GRAY2BGR)
h1, w1 = img1.shape[:2]
h2, w2 = img2.shape[:2]
view = sp.zeros((max(h1, h2), w1 + w2, 3), sp.uint8)
view[:h1, :w1, :] = img1
view[:h2, w1:, :] = img2
view[:, :, 1] = view[:, :, 0]
view[:, :, 2] = view[:, :, 0]
for m in matches:
m = m[0]
# draw the keypoints
# print m.queryIdx, m.trainIdx, m.distance
color = tuple([sp.random.randint(0, 255) for _ in xrange(3)])
pt1 = (int(k1[m.queryIdx].pt[0]), int(k1[m.queryIdx].pt[1]))
pt2 = (int(k2[m.trainIdx].pt[0] + w1), int(k2[m.trainIdx].pt[1]))
cv.line(view, pt1, pt2, color)
return view
def get_array(coords, values):
lon_res, lat_res = get_resolution()
indices = get_grid_indices(coords)
indices['values'] = values
indices = indices.groupby(['x', 'y'])['values'].agg(['median', 'count']).reset_index()
x_size = int((LONDON_LONS[1] - LONDON_LONS[0])/lon_res) + 1
y_size = int((LONDON_LATS[1] - LONDON_LATS[0])/lat_res) + 1
x_mask = (indices['x'] < 0) | (indices['x'] >= x_size)
y_mask = (indices['y'] < 0) | (indices['y'] >= y_size)
mask = ~(x_mask | y_mask)
indices = indices.loc[mask]
arr = sp.nan*sp.zeros((x_size, y_size))
arr[indices['x'].values, indices['y'].values] = indices['median'].values
count_arr = sp.zeros((x_size, y_size))
count_arr[indices['x'].values, indices['y'].values] = indices['count'].values
return arr, count_arr
def __MR_W_D_matrix(self,img,labels):
s = sp.amax(labels)+1
vect = self.__MR_superpixel_mean_vector(img,labels)
adj = self.__MR_get_adj_loop(labels)
W = sp.spatial.distance.squareform(sp.spatial.distance.pdist(vect))
W = sp.exp(-1*W / self.weight_parameters['delta'])
W[adj.astype(np.bool)] = 0
D = sp.zeros((s,s)).astype(float)
for i in range(s):
D[i, i] = sp.sum(W[i])
return W,D
def __MR_boundary_indictor(self,labels):
s = sp.amax(labels)+1
up_indictor = (sp.zeros((s,1))).astype(float)
right_indictor = (sp.zeros((s,1))).astype(float)
low_indictor = (sp.zeros((s,1))).astype(float)
left_indictor = (sp.zeros((s,1))).astype(float)
upper_ids = sp.unique(labels[0,:]).astype(int)
right_ids = sp.unique(labels[:,labels.shape[1]-1]).astype(int)
low_ids = sp.unique(labels[labels.shape[0]-1,:]).astype(int)
left_ids = sp.unique(labels[:,0]).astype(int)
up_indictor[upper_ids] = 1.0
right_indictor[right_ids] = 1.0
low_indictor[low_ids] = 1.0
left_indictor[left_ids] = 1.0
return up_indictor,right_indictor,low_indictor,left_indictor
def alt_results(self, samples, kplanets):
titles = sp.array(["Amplitude","Period","Longitude", "Phase","Eccentricity", 'Acceleration', 'Jitter', 'Offset', 'MACoefficient', 'MATimescale', 'Stellar Activity'])
namen = sp.array([])
ndim = kplanets * 5 + self.nins*2*(self.MOAV+1) + self.totcornum + 1
RESU = sp.zeros((ndim, 5))
for k in range(kplanets):
namen = sp.append(namen, [titles[i] + '_'+str(k) for i in range(5)])
namen = sp.append(namen, titles[5]) # for acc
for i in range(self.nins):
namen = sp.append(namen, [titles[ii] + '_'+str(i+1) for ii in sp.arange(2)+6])
for c in range(self.MOAV):
namen = sp.append(namen, [titles[ii] + '_'+str(i+1) + '_'+str(c+1) for ii in sp.arange(2)+8])
for h in range(self.totcornum):
namen = sp.append(namen, titles[-1]+'_'+str(h+1))
alt_res = map(lambda v: (v[2], v[3]-v[2], v[2]-v[1], v[4]-v[2], v[2]-v[0]),
zip(*np.percentile(samples, [2, 16, 50, 84, 98], axis=0)))
logdat = '\nAlternative results with uncertainties based on the 2nd, 16th, 50th, 84th and 98th percentiles of the samples in the marginalized distributions'
logdat = '\nFormat is like median +- 1-sigma, +- 2-sigma'
for res in range(ndim):
logdat += '\n'+namen[res]+' : '+str(alt_res[res][0])+' +- '+str(alt_res[res][1:3]) +' 2% +- '+str(alt_res[res][3:5])
RESU[res] = sp.percentile(samples, [2, 16, 50, 84, 98], axis=0)[:, res]
print(logdat)
return RESU
SLIC_new_cityscapes_training_server_1.py 文件源码
项目:SLIC_cityscapes
作者: wpqmanu
项目源码
文件源码
阅读 45
收藏 0
点赞 0
评论 0
def resultimg(self, centers):
print "show result"
result = scipy.zeros(self.img.shape[:2], scipy.uint8)
width, height = result.shape[:2]
if len(result.shape)>2:
color_channels=result.shape[2]
else:
color_channels=1
colors = [scipy.array([int(random.uniform(0, 255)) for i in xrange(1)]) for j in xrange(len(centers))]
for x in xrange(width):
for y in xrange(height):
result[x, y] = colors[self.assignedindex[x][y]]
# cv2.imshow("result", result)
# cv2.waitKey(10)
cv2.imwrite(os.path.join(self.result_dir,self.filename+'_superpixel.png'), result)
SLIC_new_cityscapes_training_server_parallel_spark.py 文件源码
项目:SLIC_cityscapes
作者: wpqmanu
项目源码
文件源码
阅读 40
收藏 0
点赞 0
评论 0
def resultimg(self, centers):
print "show result"
result = scipy.zeros(self.img.shape[:2], scipy.uint8)
width, height = result.shape[:2]
if len(result.shape)>2:
color_channels=result.shape[2]
else:
color_channels=1
colors = [scipy.array([int(random.uniform(0, 255)) for i in xrange(1)]) for j in xrange(len(centers))]
for x in xrange(width):
for y in xrange(height):
result[x, y] = colors[self.assignedindex[x][y]]
# cv2.imshow("result", result)
# cv2.waitKey(10)
cv2.imwrite(os.path.join(self.result_dir,self.filename+'_superpixel.png'), result)
def update(self, centers):
# sums = [scipy.zeros(5) for i in range(len(centers))]
# nums = [0 for i in range(len(centers))]
# width, height = self.img.shape[:2]
print "E step"
new_centers=[]
nan_record=[]
for i in xrange(len(centers)):
current_region=self.xylab[self.assignedindex == i]
if current_region.size>0: #non-empty region
new_centers.append(scipy.mean(current_region, 0))
else: # empty region
nan_record.append(i)
# after we get full nan_record list, update assignment index (elimnate those indexes in reverse order)
for nan_value in nan_record[::-1]:
self.assignedindex[self.assignedindex>nan_value]=self.assignedindex[self.assignedindex>nan_value]-1
for new_center_index in range(len(new_centers)):
# print new_center_index
new_centers[new_center_index][2:]=self.labimg[math.floor(new_centers[new_center_index][0])][math.floor(new_centers[new_center_index][1])]
return new_centers,nan_record
def resultimg(self, centers):
print "show result"
result = scipy.zeros(self.img.shape[:2], scipy.uint8)
width, height = result.shape[:2]
if len(result.shape)>2:
color_channels=result.shape[2]
else:
color_channels=1
colors = [scipy.array([int(random.uniform(0, 255)) for i in xrange(1)]) for j in xrange(len(centers))]
for x in xrange(width):
for y in xrange(height):
result[x, y] = colors[self.assignedindex[x][y]]
# cv2.imshow("result", result)
# cv2.waitKey(10)
cv2.imwrite(os.path.join(self.result_dir,self.filename+'_superpixel.png'), result)
SLIC_new_cityscapes_training_server_parallel.py 文件源码
项目:SLIC_cityscapes
作者: wpqmanu
项目源码
文件源码
阅读 34
收藏 0
点赞 0
评论 0
def resultimg(self, centers):
print "show result"
result = scipy.zeros(self.img.shape[:2], scipy.uint8)
width, height = result.shape[:2]
if len(result.shape)>2:
color_channels=result.shape[2]
else:
color_channels=1
colors = [scipy.array([int(random.uniform(0, 255)) for i in xrange(1)]) for j in xrange(len(centers))]
for x in xrange(width):
for y in xrange(height):
result[x, y] = colors[self.assignedindex[x][y]]
# cv2.imshow("result", result)
# cv2.waitKey(10)
cv2.imwrite(os.path.join(self.result_dir,self.filename+'_superpixel.png'), result)