Skip to content

Commit

Permalink
Add seed offset for getting a different (but still deterministic) dis…
Browse files Browse the repository at this point in the history
…tribution of colors.
  • Loading branch information
kevinjohncutler committed May 23, 2022
1 parent a91df2c commit 36d5609
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 90 deletions.
92 changes: 10 additions & 82 deletions example.ipynb

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions ncolor/format_labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ def format_labels(labels, clean=False, min_area=9):
labels[rg0.coords[:,0], rg0.coords[:,1]] = 0
print('Warning - found mask area less than', min_area)
print('Removing it.')

fastremap.renumber(labels,in_place=True) # convenient to have unit increments from 1 to N cells
labels = fastremap.refit(labels) # put into smaller data type if possible

if np.any(labels):
fastremap.renumber(labels,in_place=True) # convenient to have unit increments from 1 to N cells
labels = fastremap.refit(labels) # put into smaller data type if possible
return labels
10 changes: 5 additions & 5 deletions ncolor/label.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
import scipy
from .format_labels import format_labels

def label(lab,n=4,conn=2):
def label(lab,n=4,conn=2,max_depth=5, offset=0):
# needs to be in standard label form
# but also needs to be in int32 data type to work properly; the formatting automatically
# puts it into the smallest datatype to save space
lab = format_labels(lab).astype(np.int32)
idx = connect(lab, conn)
idx = mapidx(idx)
colors = render_net(idx, n=n, rand=10)
colors = render_net(idx, n=n, rand=10, max_depth=max_depth, offset=offset)
lut = np.ones(lab.max()+1, dtype=np.uint8)
for i in colors: lut[i] = colors[i]
lut[0] = 0
Expand Down Expand Up @@ -67,11 +67,11 @@ def mapidx(idx):
return dic

# create a connection mapping
def render_net(conmap, n=4, rand=12, depth=0, max_depth=5):
def render_net(conmap, n=4, rand=12, depth=0, max_depth=5, offset=0):
thresh = 1e4
if depth<max_depth:
nodes = list(conmap.keys())
np.random.seed(depth+1)
np.random.seed(depth+1+offset)
np.random.shuffle(nodes)
colors = dict(zip(nodes, [0]*len(nodes)))
counter = dict(zip(nodes, [0]*len(nodes)))
Expand Down Expand Up @@ -99,7 +99,7 @@ def render_net(conmap, n=4, rand=12, depth=0, max_depth=5):
nodes.append(p)
if count==thresh:
print(n,'-color algorthm failed,trying again with',n+1,'colors. Depth',depth)
colors = render_net(conmap,n+1,rand,depth+1,max_depth)
colors = render_net(conmap,n+1,rand,depth+1,max_depth, offset)
return colors
else:
print('N-color algorthm exceeded max depth of',max_depth)
Expand Down

0 comments on commit 36d5609

Please sign in to comment.