Skip to content

Commit

Permalink
edits with Chee
Browse files Browse the repository at this point in the history
  • Loading branch information
boris-kz committed Dec 16, 2024
1 parent 612de3c commit a741be2
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 118 deletions.
59 changes: 29 additions & 30 deletions frame_2D_alg/deprecated/24.12.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,42 +156,41 @@ def add_H(HE, He_, sign=1): # unpack derHs down to numericals and sum them

return HE # root should be updated by returned HE

def trace(edge): # fill and trace across slices

adjacent_ = [(P, y,x) for P in edge.P_ for y,x in edge.rootd if edge.rootd[y,x] is P]
bi__ = defaultdict(list) # prelinks (bi-lateral)
while adjacent_:
_P, _y,_x = adjacent_.pop(0) # also pop _P__
_pre_ = bi__[_P]
for y,x in [(_y-1,_x),(_y,_x+1),(_y+1,_x),(_y,_x-1)]:
try: # if yx has _P, try to form link
def trace_P_adjacency(edge): # fill and trace across slices

P_map_ = [(P, y,x) for P in edge.P_ for y,x in edge.rootd if edge.rootd[y,x] is P]
prelink__ = defaultdict(list) # uplinks
while P_map_:
_P, _y,_x = P_map_.pop(0) # also pop _P__
_margin = prelink__[_P] # empty list per _P
for y,x in [(_y-1,_x),(_y,_x+1),(_y+1,_x),(_y,_x-1)]: # adjacent pixels
try: # form link if yx has _P
P = edge.rootd[y,x]
pre_ = bi__[P]
if _P is not P and _P not in pre_ and P not in _pre_:
pre_ += [_P]
_pre_ += [P]
except KeyError: # if yx empty, keep tracing
if (y,x) not in edge.dert_: continue # stop if yx outside the edge
margin = prelink__[P] # empty list per P
if _P is not P:
if _P.yx < P.yx and _P not in margin:
margin += [_P] # _P is higher
elif P not in _margin:
_margin += [P] # P is higher
except KeyError: # if yx empty, keep tracing
if (y,x) not in edge.dert_: continue # yx is outside the edge
edge.rootd[y,x] = _P
adjacent_ += [(_P, y,x)]
# remove redundant links
P_map_ += [(_P, y,x)]
# remove crossed links
for P in edge.P_:
yx = P.yx
for __P, _P in combinations(bi__[P], r=2):
if __P not in bi__[P] or _P not in bi__[P]: continue
__yx, _yx = __P.yx, _P.yx # center coords
# start -> end:
__yx1 = np.subtract(__P.yx_[0], __P.axis)
__yx2 = np.add(__P.yx_[-1], __P.axis)
for _P, __P in combinations(prelink__[P], r=2):
_yx, __yx = _P.yx, __P.yx
# get aligned line segments:
_yx1 = np.subtract(_P.yx_[0], _P.axis)
_yx2 = np.add(_P.yx_[-1], _P.axis)
# remove link(_P,P) crossing __P:
__yx1 = np.subtract(__P.yx_[0], __P.axis)
__yx2 = np.add(__P.yx_[-1], __P.axis)
# remove crossed uplinks:
if xsegs(yx, _yx, __yx1, __yx2):
bi__[P].remove(_P)
bi__[_P].remove(P)
# remove link(__P,P) crossing _P):
prelink__[P].remove(_P)
elif xsegs(yx, __yx, _yx1, _yx2):
bi__[P].remove(__P)
bi__[__P].remove(P)
prelink__[P].remove(__P)
# for comp_slice:
edge.pre__ = prelink__

edge.pre__ = {_P:[P for P in bi__[_P] if _P.yx < P.yx] for _P in edge.P_}
8 changes: 4 additions & 4 deletions frame_2D_alg/vectorize_edge_blob/agg_recursion.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from functools import reduce
from frame_blobs import frame_blobs_root, intra_blob_root, imread
from comp_slice import comp_latuple, comp_md_
from trace_edge import comp_node_, comp_link_, sum2graph, get_rim, CH, CG, ave, ave_d, ave_L, vectorize_root, comp_area, extend_box
from vect_edge import comp_node_, comp_link_, sum2graph, get_rim, CH, CG, ave, ave_d, ave_L, vectorize_root, comp_area, extend_box
'''
Cross-compare and cluster Gs within a frame, potentially unpacking their node_s first,
alternating agglomeration and centroid clustering.
Expand Down Expand Up @@ -34,10 +34,10 @@ def cluster_eval(G, N_, fd):
if m > ave * r:
mlay = CH().add_H([L.derH for L in L_]) # mfork, else no new layer
frame.derH = CH(H=[mlay], n=mlay.n, root=frame, Et=copy(mlay.Et)); mlay.root=frame.derH
vd = d - ave_d * r
if vd > 0: # no cross-projection
vd = d * (m/ave) - ave_d * r
if vd > 0:
for L in L_:
L.root_ = [frame]; L.extH = CH(); L.rimt = [[],[]]
L.extH, L.root, L.mL_t, L.rimt, L.aRad, L.visited_, L.Et, L.n = CH(), frame, [[],[]], [[],[]], 0, [L], copy(L.derH.Et), L.derH.n
lN_,lL_, md = comp_link_(L_) # comp new L_, root.link_ was compared in root-forming for alt clustering
vd *= md / ave
if lL_: # recursive der+ eval_: cost > ave_match, add by feedback if < _match?
Expand Down
19 changes: 9 additions & 10 deletions frame_2D_alg/vectorize_edge_blob/comp_slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ def vectorize_root(frame):
for blob in blob_:
if not blob.sign and blob.G > aveG * blob.root.rdn:
edge = slice_edge(blob)
if edge.G*(len(edge.P_) - 1) > ave_PPm: # eval PP, rdn=1
if edge.G * (len(edge.P_) - 1) > ave_PPm: # eval PP, rdn=1
comp_slice(edge)

def comp_slice(edge): # root function

edge.Et, edge.vertuple = np.zeros(2), np.array([np.zeros(6), np.zeros(6)]) # m_,d_
edge.Et, edge.vertuple, edge.n = np.zeros(2), np.array([np.zeros(6), np.zeros(6)]), 0 # m_,d_,n
for P in edge.P_: # add higher links
P.vertuple = np.array([np.zeros(6), np.zeros(6)])
P.rim = []; P.lrim = []; P.prim = []
Expand Down Expand Up @@ -125,7 +125,7 @@ def comp_md_(_d_,d_, rn=.1, dir=1): # dir may be -1
def convert_to_dP(_P,P, derLay, angle, distance, Et):

link = CdP(nodet=[_P,P], Et=Et, vertuple=derLay, angle=angle, span=distance, yx=np.add(_P.yx, P.yx)/2)
# bidirectional, regardless of clustering:
# bilateral, regardless of clustering:
_P.vertuple += link.vertuple; P.vertuple += link.vertuple
_P.lrim += [link]; P.lrim += [link]
_P.prim += [P]; P.prim +=[_P] # all Ps are dPs if fd
Expand All @@ -138,22 +138,21 @@ def form_PP_(root, iP_, fd): # form PPs of dP.valt[fd] + connected Ps val
for P in iP_: P.merged = 0
for P in iP_: # dP from link_ if fd
if P.merged: continue
_prim_ = P.prim; _lrim_ = P.lrim
_P_ = {P}; link_ = set(); Et = np.zeros(2); n = 0
_prim_ = P.prim; _lrim_ = P.lrim; I, G, M, Ma, L, _ = P.latuple
_P_ = {P}; link_ = set(); Et = np.array([M,G]); n = L
while _prim_:
prim_,lrim_ = set(),set()
for _P,_L in zip(_prim_,_lrim_):
if _L.Et[fd] < aves[fd] or _P.merged:
continue
_P_.add(_P); link_.add(_L); Et += _L.Et; n += P.latuple[4] # L
_P_.add(_P); link_.add(_L); I, G, M, Ma, L, _ = _P.latuple
Et += _L.Et + np.array([M,G]); n += L # L is a multiplier to 1 for _L.vertuple[1]
prim_.update(set(_P.prim) - _P_)
lrim_.update(set(_P.lrim) - link_)
_P.merged = 1
_prim_, _lrim_ = prim_, lrim_
if not link_:
PPt_ += [P]; continue
PPt = sum2PP(root, list(_P_), list(link_), Et, n)
PPt_ += [PPt]
PPt_ += [PPt]; root.Et += Et; root.n += n

This comment has been minimized.

Copy link
@khanh93vn

khanh93vn Dec 19, 2024

Collaborator

This comment has been minimized.

Copy link
@boris-kz

boris-kz via email Dec 19, 2024

Author Owner

This comment has been minimized.

Copy link
@khanh93vn

khanh93vn Dec 19, 2024

Collaborator

Thank you. I've pushed the update


return PPt_

Expand All @@ -171,7 +170,7 @@ def sum2PP(root, P_, dP_, Et, n): # sum links in Ps and Ps in PP
link_ += [dP]
a = dP.angle; A = np.add(A,a); S += np.hypot(*a) # span, links are contiguous but slanted
else: # single P PP
S,A = P_[0].latuple[4:] # latuple is I, G, M, Ma, L, (Dy, Dx)
S,A = P_[0].latuple[4:] # [I, G, M, Ma, L, (Dy, Dx)]
box = [np.inf,np.inf,0,0]
for P in P_:
if not fd: # else summed from P_ nodets on top
Expand Down
83 changes: 40 additions & 43 deletions frame_2D_alg/vectorize_edge_blob/slice_edge.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,41 +106,40 @@ def select_max(edge):

def trace_P_adjacency(edge): # fill and trace across slices

P_map_ = [(P, y,x) for P in edge.P_ for y,x in edge.rootd if edge.rootd[y,x] is P]
prelink__ = defaultdict(list) # uplinks
while P_map_:
_P, _y,_x = P_map_.pop(0) # also pop _P__
margin_rim = [(P, y,x) for P in edge.P_ for y,x in edge.rootd if edge.rootd[y,x] is P]
prelink__ = defaultdict(list) # bilateral
while margin_rim: # breadth-first search for neighbors
_P, _y,_x = margin_rim.pop(0) # also pop _P__
_margin = prelink__[_P] # empty list per _P
for y,x in [(_y-1,_x),(_y,_x+1),(_y+1,_x),(_y,_x-1)]: # adjacent pixels
try: # form link if yx has _P
P = edge.rootd[y,x]
margin = prelink__[P] # empty list per P
if _P is not P:
if _P.yx < P.yx and _P not in margin:
margin += [_P] # _P is higher
elif P not in _margin:
_margin += [P] # P is higher
except KeyError: # if yx empty, keep tracing
if (y,x) not in edge.dert_: continue # yx is outside the edge
edge.rootd[y,x] = _P
P_map_ += [(_P, y,x)]
if (y,x) not in edge.dert_: continue # yx is outside the edge
if (y,x) not in edge.rootd: # assign root, keep tracing
edge.rootd[y, x] = _P
margin_rim += [(_P, y, x)]
continue
# form link if yx has _P
P = edge.rootd[y,x]
margin = prelink__[P] # empty list per P
if _P is not P and _P not in margin and P not in _margin:
margin += [_P]; _margin += [P]
# remove crossed links
for P in edge.P_:
yx = P.yx
for _P, __P in combinations(prelink__[P], r=2):
_yx, __yx = _P.yx, __P.yx
for _P in edge.P_:
_yx = _P.yx
for P, __P in combinations(prelink__[_P], r=2):
if {__P,P}.intersection(prelink__[_P]) != {__P, P}: continue # already removed
yx, __yx = P.yx, __P.yx
# get aligned line segments:
_yx1 = np.subtract(_P.yx_[0], _P.axis)
_yx2 = np.add(_P.yx_[-1], _P.axis)
yx1 = np.subtract(P.yx_[0], P.axis)
yx2 = np.add(P.yx_[-1], P.axis)
__yx1 = np.subtract(__P.yx_[0], __P.axis)
__yx2 = np.add(__P.yx_[-1], __P.axis)
# remove crossed uplinks:
if xsegs(yx, _yx, __yx1, __yx2):
prelink__[P].remove(_P)
elif xsegs(yx, __yx, _yx1, _yx2):
prelink__[P].remove(__P)
prelink__[P].remove(_P); prelink__[_P].remove(P)
elif xsegs(_yx, __yx, yx1, yx2):
prelink__[_P].remove(__P); prelink__[__P].remove(_P)
# for comp_slice:
edge.pre__ = prelink__
edge.pre__ = {_P:[P for P in prelink__[_P] if _P.yx > P.yx] for _P in prelink__}

# --------------------------------------------------------------------------------------------------------------
# utility functions
Expand Down Expand Up @@ -232,25 +231,23 @@ def xsegs(yx1, yx2, yx3, yx4):
v_, u_ = zip(*vu_)
plt.quiver(x_, y_, u_, v_, scale=100)
if show_slices:
for P in edge.P_:
y_, x_ = zip(*(P.yx_ - yx0))
if len(P.yx_) == 1:
v, u = P.axis
y_ = y_[0]-v/2, y_[0]+v/2
x_ = x_[0]-u/2, x_[0]+u/2
plt.plot(x_, y_, "k-", linewidth=3)
yp, xp = P.yx - yx0
pre_set = set()
for _P in edge.pre__[P]:
assert _P.id not in pre_set # verify pre-link uniqueness
pre_set.add(_P.id)
for _P in edge.P_:
_y_, _x_ = zip(*(_P.yx_ - yx0))
if len(_P.yx_) == 1:
v, u = _P.axis
_y_ = _y_[0]-v/2, _y_[0]+v/2
_x_ = _x_[0]-u/2, _x_[0]+u/2
plt.plot(_x_, _y_, "k-", linewidth=3)
_yp, _xp = _P.yx - yx0
assert len(set(edge.pre__[_P])) == len(edge.pre__[_P]) # verify pre-link uniqueness
for P in edge.pre__[_P]:
assert _P.yx > P.yx # verify up-link
_yp, _xp = _P.yx - yx0
yp, xp = P.yx - yx0
plt.plot([_xp, xp], [_yp, yp], "ko--", alpha=0.5)
yx_ = [yx for yx in edge.rootd if edge.rootd[yx] is P]
if yx_:
y_, x_ = zip(*(yx_ - yx0))
plt.plot(x_, y_, 'o', alpha=0.5)
_cyx_ = [_yx for _yx in edge.rootd if edge.rootd[_yx] is _P]
if _cyx_:
_cy_, _cx_ = zip(*(_cyx_ - yx0))
plt.plot(_cx_, _cy_, 'o', alpha=0.5)

ax = plt.gca()
ax.set_aspect('equal', adjustable='box')
Expand Down
Loading

0 comments on commit a741be2

Please sign in to comment.