pep8 changes

This commit is contained in:
Vighnesh Birodkar
2014-07-23 02:19:47 +05:30
parent 7215936f5c
commit ddf11bdfe8
+27 -26
View File
@@ -4,11 +4,12 @@ except ImportError:
import warnings
warnings.warn('"cut_threshold" requires networkx')
import numpy as np
import _ncut
import _ncut_cy
from . import _ncut
from . import _ncut_cy
from scipy.sparse import linalg
from scipy.sparse.linalg.eigen.arpack.arpack import ArpackNoConvergence as ANC
from scipy.sparse.linalg.eigen.arpack.arpack import ArpackError as APE
from scipy.sparse.linalg.eigen.arpack.arpack import ArpackNoConvergence
from scipy.sparse.linalg.eigen.arpack.arpack import ArpackError
def cut_threshold(labels, rag, thresh):
"""Combine regions seperated by weight less than threshold.
@@ -68,57 +69,59 @@ def cut_threshold(labels, rag, thresh):
def cut_n(labels, rag, thresh):
_ncut_relabel(rag,thresh)
from_ = range(labels.max()+1)
to = [ rag.node[x]['ncut label'] for x in from_ ]
_ncut_relabel(rag, thresh)
from_ = range(labels.max() + 1)
to = [rag.node[x]['ncut label'] for x in from_]
map_array = np.array(to)
return map_array[labels]
def _ncut_relabel(rag, cut_thresh = 0.0001):
def _ncut_relabel(rag, cut_thresh=0.0001):
d, w = _ncut.DW_matrix(rag)
error = False
try:
m = w.shape[0]
vals,vectors = linalg.eigsh(d-w,M=d,which='SM',k = min(100,m-2))
except ANC as e:
vals, vectors = linalg.eigsh(d - w, M=d, which='SM',
k=min(100, m - 2))
except ArpackNoConvergence as e:
vals = e.eigenvalues
vectors = e.eigenvectors
if len(vals) == 0:
error = True
except ValueError:
error = True
except APE:
except ArpackError:
error = True
if not error :
vals,vectors = np.real(vals), np.real(vectors)
if not error:
vals, vectors = np.real(vals), np.real(vectors)
index2 = _ncut_cy.argmin2(vals)
ev = np.real(vectors[:,index2])
ev = np.real(vectors[:, index2])
ev = _ncut.norml(ev)
mcut = np.inf
thresh = None
for t in np.arange(0,1,0.1):
for t in np.arange(0, 1, 0.1):
mask = ev > t
cost = _ncut.ncut_cost(mask,d,w)
if cost < mcut :
cost = _ncut.ncut_cost(mask, d, w)
if cost < mcut:
mcut = cost
thresh = t
if ( mcut < cut_thresh ):
if (mcut < cut_thresh):
mask = ev > thresh
nodes1 = [ n for i,n in enumerate(rag.nodes()) if mask[i]]
nodes2 = [ n for i,n in enumerate(rag.nodes()) if not mask[i]]
nodes1 = [n for i, n in enumerate(rag.nodes()) if mask[i]]
nodes2 = [n for i, n in enumerate(rag.nodes()) if not mask[i]]
sub1 = rag.subgraph(nodes1)
sub2 = rag.subgraph(nodes2)
_ncut_relabel(sub1,cut_thresh)
_ncut_relabel(sub1, cut_thresh)
_ncut_relabel(sub2, cut_thresh)
return
@@ -126,5 +129,3 @@ def _ncut_relabel(rag, cut_thresh = 0.0001):
new_label = rag.node[node]['labels'][0]
for n in rag.nodes():
rag.node[n]['ncut label'] = new_label