Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Variable edge buf size. int64 coordinates. #22

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions sknw/sknw.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def mark(img, nbs): # mark the array use (0, 1, 2)

@jit(nopython=True) # trans index to r, c...
def idx2rc(idx, acc):
rst = np.zeros((len(idx), len(acc)), dtype=np.int16)
rst = np.zeros((len(idx), len(acc)), dtype=np.int64)
for i in range(len(idx)):
for j in range(len(acc)):
rst[i,j] = idx[i]//acc[j]
Expand Down Expand Up @@ -57,6 +57,8 @@ def trace(img, p, nbs, acc, buf):
c1 = 0; c2 = 0;
newp = 0
cur = 1
buf_len = len(buf)
buf_overflow = False
while True:
buf[cur] = p
img[p] = 0
Expand All @@ -74,12 +76,15 @@ def trace(img, p, nbs, acc, buf):
newp = cp
p = newp
if c2!=0:break
return (c1-10, c2-10, idx2rc(buf[:cur+1], acc))
if cur >= buf_len:
buf_overflow = True
break
return (c1-10, c2-10, idx2rc(buf[:cur+1], acc)), buf_overflow

@jit(nopython=True) # parse the image then get the nodes and edges
def parse_struc(img, nbs, acc, iso, ring):
def parse_struc(img, nbs, acc, iso, ring, buf_size):
img = img.ravel()
buf = np.zeros(131072, dtype=np.int64)
buf = np.zeros(buf_size, dtype=np.int64)
num = 10
nodes = []
for p in range(len(img)):
Expand All @@ -93,18 +98,22 @@ def parse_struc(img, nbs, acc, iso, ring):
if img[p] <10: continue
for dp in nbs:
if img[p+dp]==1:
edge = trace(img, p+dp, nbs, acc, buf)
edge, buf_overflow = trace(img, p+dp, nbs, acc, buf)
if buf_overflow:
return nodes, edges, buf_overflow
edges.append(edge)
if not ring: return nodes, edges
if not ring: return nodes, edges, False
for p in range(len(img)):
if img[p]!=1: continue
img[p] = num; num += 1
nodes.append(idx2rc([p], acc))
for dp in nbs:
if img[p+dp]==1:
edge = trace(img, p+dp, nbs, acc, buf)
edge, buf_overflow = trace(img, p+dp, nbs, acc, buf)
if buf_overflow:
return nodes, edges, buf_overflow
edges.append(edge)
return nodes, edges
return nodes, edges, False

# use nodes and edges build a networkx graph
def build_graph(nodes, edges, multi=False, full=True):
Expand All @@ -126,12 +135,14 @@ def mark_node(ske):
mark(buf, nbs)
return buf

def build_sknw(ske, multi=False, iso=True, ring=True, full=True):
buf = np.pad(ske, (1,1), mode='constant').astype(np.uint16)
def build_sknw(ske, buf_size=131072, multi=False, iso=True, ring=True, full=True):
buf = np.pad(ske, (1,1), mode='constant').astype(np.int64)
nbs = neighbors(buf.shape)
acc = np.cumprod((1,)+buf.shape[::-1][:-1])[::-1]
mark(buf, nbs)
nodes, edges = parse_struc(buf, nbs, acc, iso, ring)
nodes, edges, buf_overflow = parse_struc(buf, nbs, acc, iso, ring, buf_size)
if buf_overflow:
raise Exception('Buffer overflow')
return build_graph(nodes, edges, multi, full)

# draw the graph
Expand Down