Skip to content

Commit

Permalink
Bug fixes to Bcl_knn, Bds_nostratify, Bfx_haralick and Bfs_clean
Browse files Browse the repository at this point in the history
  • Loading branch information
dipaco committed Dec 9, 2016
1 parent 6a77245 commit 4c25c3c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
5 changes: 4 additions & 1 deletion balu/Classification/Bcl_knn.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ def Bcl_knn(*args):
if train:
options['kdtree'] = KDTree(X, metric='euclidean')
#options['X'] = X
options['d'] = d
if len(d.shape) < 2:
options['d'] = d[:, None]
else:
options['d'] = d
output = options

if test:
Expand Down
8 changes: 5 additions & 3 deletions balu/DataSelectionAndGeneration/Bds_nostratify.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,19 @@ def Bds_nostratify(X, d, s):
With collaboration from:
Diego Patiño ([email protected]) -> Translated implementation into python (2016)
"""
if len(d.shape) < 2:
d = d[:, None]

N = X.shape[0]
rn = np.random.rand(N)
j = np.argsort(rn)
Xr = X[j, :]
dr = d[j]
dr = d[j, 0]
r = np.floor(s * N)
R = np.array([[0, r], [r, N]]).astype(int)
X1 = Xr[R[0, 0]:R[0, 1], :]
d1 = dr[R[0, 0]:R[0, 1], :]
d1 = dr[R[0, 0]:R[0, 1]]
X2 = Xr[R[1, 0]:R[1, 1], :]
d2 = dr[R[1, 0]:R[1, 1], :]
d2 = dr[R[1, 0]:R[1, 1]]

return X1, d1, X2, d2
16 changes: 7 additions & 9 deletions balu/FeatureExtraction/Bfx_haralick.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ def Bfx_haralick(I, R=None, options={}):
options = {'dharalick': [1, 2, 3, 4, 5]} #3 and 5 pixels distance for coocurrence
I = balu_imageload('testimg1.jpg') #input image
R, _, _ = Bim_segbalu(I) #segmentation
R = I[:, :, 1] > 128
J = I[:, :, 1] #green channel
X, Xn = Bfx_haralick(J, R, options) #Haralick features
Bio_printfeatures(X, Xn)
Expand All @@ -72,7 +71,6 @@ def Bfx_haralick(I, R=None, options={}):
With collaboration from:
Diego Patiño ([email protected]) -> Translated implementation into python (2016)
"""

I = I.astype(float)

if R is None:
Expand Down Expand Up @@ -137,7 +135,7 @@ def Bcoocurrencematrix(I, R, Io, Jo):
Diego Patiño ([email protected]) -> Translated implementation into python (2016)
"""

V = np.floor(I / 32.0) + 1
V = np.floor(I / 32.0)
N, M = I.shape
Z1 = np.zeros((N+40, M+40))
Z2 = Z1.copy()
Expand Down Expand Up @@ -167,7 +165,7 @@ def Bcoocurrencematrix(I, R, Io, Jo):
i2 = np.insert(i1[1:], i1.size - 1, -1)
d = i2 - i1
for i in range(d.size - 1):
P[X[i1[i], 1] - 1, X[i1[i], 0] - 1] = d[i]
P[int(X[i1[i], 1]), int(X[i1[i], 0])] = d[i]

else:
P = -np.ones((8, 8))
Expand Down Expand Up @@ -275,12 +273,12 @@ def Bcoocurrencefeatures(P):
# 12,13 Information Measures of Correlation
HXY = f9
pxipyj = pxi[i] * pyj[j]
HXY1 = np.dot(-Pij.T, np.log(pxipyj + 1e-20))
HXY1 = np.dot(-Pij.T, np.log(pxipyj + 1e-20))[0][0]
HXY2 = np.dot(-pxipyj.T, np.log(pxipyj+1e-20))
HX = np.dot(-pxi.T, np.log(pxi + 1e-20))
HY = np.dot(-pyj.T, np.log(pyj + 1e-20))
f12 = ((HXY-HXY1) / np.max(HX, HY))[0][0]
f13 = (1 - np.exp(-2 * (HXY2-HXY)))[0][0]
HX = np.dot(-pxi.T, np.log(pxi + 1e-20))[0][0]
HY = np.dot(-pyj.T, np.log(pyj + 1e-20))[0][0]
f12 = ((HXY-HXY1) / max(HX, HY))
f13 = (1 - np.exp(-2 * (HXY2-HXY)))

# 14 Maximal Corrleation Coefficient
f14 = eigQ[1]
Expand Down
5 changes: 4 additions & 1 deletion balu/FeatureSelection/Bfs_clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ def Bfs_clean(X, show=False):

# eliminating correlated features
warnings.filterwarnings('ignore')
C = np.abs(np.corrcoef(f, rowvar=0))
if f.shape[0] == 1:
return p
else:
C = np.abs(np.corrcoef(f, rowvar=0))
ii, jj = np.where(C > 0.99)

if ii.size > 0:
Expand Down

0 comments on commit 4c25c3c

Please sign in to comment.