Skip to content

Commit a06bf05

Browse files
committed
Merge branch 'master' of https://github.com/boris-kz/CogAlg
# Conflicts: # frame_2D_alg/agg_recursion.py # frame_2D_alg/comp_slice.py # frame_2D_alg/frame_blobs.py # frame_2D_alg/vect_edge.py
2 parents a3b9e50 + 475b96e commit a06bf05

File tree

4 files changed

+45
-57
lines changed

4 files changed

+45
-57
lines changed

frame_2D_alg/agg_recursion.py

+14-17
Original file line numberDiff line numberDiff line change
@@ -311,26 +311,24 @@ def agg_H_par(focus): # draft parallel level-updating pipeline
311311

312312
frame.aggH = list(H) # convert back to list
313313

314-
def agg_H_seq(focus, image, _nestt=(1,0), _fb_={}, fb_={}): # recursive level-forming pipeline, called from cluster_C_
315-
316-
fb_ = {}
317-
if _fb_: # if feedback's coefficients, update current ave
318-
global ave, ave_L, icoef, max_dist
319-
ave *= _fb_['agg_recursion'] # for highest level, get feedback from prior agg_H?
320-
ave_L *= _fb_['agg_recursion']
321-
icoef *= _fb_['agg_recursion']
322-
max_dist *= _fb_['agg_recursion']
323-
fb_['vect_edge'] = (ave+ave_L+icoef+max_dist)/4 # feedback coefficient to the next vect_edge
324-
325-
frame = frame_blobs_root(focus, _fb_)
326-
intra_blob_root(frame, _fb_, fb_)
327-
vectorize_root(frame, _fb_, fb_)
314+
def agg_H_seq(focus, image, _nestt=(1,0), rave_=[]): # recursive level-forming pipeline, called from cluster_C_
315+
# draft:
316+
global ave, ave_L, icoef, max_dist # add derTT aves
317+
if rave_:
318+
ave_ = np.array([ave, ave_L, max_dist, icoef]) # mw_, dw_
319+
ave, ave_L, max_dist, icoef = ave_ * rave_
320+
else:
321+
rave_ = np.append(np.ones(4), np.ones(4), np.ones(4))
322+
# weight per ave?
323+
frame = frame_blobs_root(focus, rave_2FB(rave_))
324+
intra_blob_root(frame, rave_2IB(rave_)) # not sure
325+
vectorize_root(frame, rave_2VR(rave_))
328326
if not frame.nnest:
329327
return frame
330328
comb_altG_(frame.node_[-1].node_, ave*2) # PP graphs in frame.node_[2]
331329
# feedforward agg+
332330
cluster_C_(frame, rc=1) # ave *= recursion count
333-
rM,rD = 1,1 # sum derTT coefs: m_,d_ [M,D,n, I,G,gA, L,A] / Et,baseT,ext:
331+
rM,rD = 1,1 # sum derTT coefs: m_,d_ [M,D,n,o I,G,A,L] / Et, baseT, dimension
334332
rV_t = np.ones((2,8)) # d value is borrowed from corresponding ms in proportion to d mag, both scaled by fb
335333
# feedback to scale m,d aves:
336334
for fd, nest,_nest,Q in zip((0,1), (frame.nnest,frame.lnest), _nestt, (frame.node_[2:],frame.link_[1:])): # skip blob_,PP_,link_PP_
@@ -352,8 +350,7 @@ def agg_H_seq(focus, image, _nestt=(1,0), _fb_={}, fb_={}): # recursive level-f
352350
y = y+dy; x = x+dx; Y = Y+dy; X = X+dx # alter focus shape, also focus size: +/m-, res decay?
353351
if y > 0 and x > 0 and Y < image.shape[0] and X < image.shape[1]: # focus is inside the image
354352
# rerun agg+ with new bottom-level focus, aves:
355-
fb_['agg_recursion'] = np.sum(rV_t)/16 # feedback coefficient to the next agg_H (temporary use sum)
356-
agg_H_seq(image[y:Y,x:X], image, (frame.nnest,frame.lnest), _fb_=fb_)
353+
agg_H_seq(image[y:Y,x:X], image, (frame.nnest,frame.lnest), rave_=rV_t)
357354

358355
return frame
359356

frame_2D_alg/comp_slice.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def vectorize_root(frame, _fb_={}, fb_={}):
6464
mw_ *= _fb_['vect_edge']
6565
dw_ *= _fb_['vect_edge']
6666
fb_['slice_edge'] = (ave+ave_d+ave_G+ave_PPm+ave_PPd+ave_L+ave_dI+sum(mw_)+sum(dw_))/23 # feedback coefficient to the next slice_edge
67-
67+
6868
blob_ = unpack_blob_(frame)
6969
for blob in blob_:
7070
if not blob.sign and blob.G > ave_G * blob.root.olp:

frame_2D_alg/frame_blobs.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,12 @@ def __init__(rnode_, blob):
186186
rnode_.rng = blob.root.rng + 1
187187

188188
def intra_blob_root(frame, _fb_={}, fb_={}):
189-
189+
190190
if _fb_: # update ave based on feedback coefficients
191191
global aveR
192192
aveR *= _fb_['intra_blob'] # get feedback coefficient from slice_edge
193193
fb_['frame_blobs'] = aveR # feedback coefficient to the next frame_blobs
194-
194+
195195
frame.olp = frame.rng = 1
196196
for blob in frame.blob_:
197197
rblob(blob)

frame_2D_alg/vect_edge.py

+28-37
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,8 @@
3838
capitalized variables are usually summed small-case variables
3939
'''
4040

41-
# module-specific:
4241
ave, ave_d, ave_rn, ave_ro, ave_G, ave_L, max_dist, icoef, med_cost, ave_dI, mw_, dw_ = 5, 10, 2, 2, 100, 1, 2, 0.15, 10, 20, np.ones(8), np.ones(8)
4342

44-
4543
class CLay(CBase): # flat layer if derivation hierarchy
4644
name = "lay"
4745
def __init__(l, **kwargs):
@@ -50,7 +48,6 @@ def __init__(l, **kwargs):
5048
l.root = kwargs.get('root', None) # higher node or link
5149
l.node_ = kwargs.get('node_', []) # concat across fork tree
5250
l.link_ = kwargs.get('link_', [])
53-
l.baseT = kwargs.get('baseT', np.zeros(5))
5451
l.derTT = kwargs.get('derTT', np.zeros((2,8))) # m_,d_ [M,D,n,o, I,G,A,L], sum across fork tree,
5552
# add weights for cross-similarity, along with vertical aves, for both m_ and d_?
5653
# altL = CLay from comp altG
@@ -112,7 +109,7 @@ def __init__(G, **kwargs):
112109
G.Et = kwargs.get('Et', np.zeros(4)) # sum all params M,D,n,o
113110
G.yx = kwargs.get('yx', np.zeros(2)) # init PP.yx = [(y+Y)/2,(x,X)/2], then ave node yx
114111
G.box = kwargs.get('box', np.array([np.inf,-np.inf,np.inf,-np.inf])) # y,Y,x,X, area: (Y-y)*(X-x)
115-
G.baseT = kwargs.get('baseT',np.zeros(5)) # I,G,Dy,Dx,L # L=box
112+
G.baseT = kwargs.get('baseT',np.zeros(4)) # I,G,Dy,Dx # from slice_edge
116113
G.derTT = kwargs.get('derTT',np.zeros((2,8))) # m,d / Et,baseT: [M,D,n,o, I,G,A,L], summed across derH lay forks
117114
G.derTTe = kwargs.get('derTTe',np.zeros((2,8))) # sum across link.derHs
118115
G.derH = kwargs.get('derH',[]) # each lay is [m,d]: Clay(Et,node_,link_,derTT), sum|concat links across fork tree
@@ -151,42 +148,33 @@ class CL(CBase): # link or edge, a product of comparison between two nodes or l
151148
def __init__(l, **kwargs):
152149
super().__init__()
153150
l.nodet = kwargs.get('nodet',[]) # e_ in kernels, else replaces _node,node: not used in kernels
151+
l.L = 0 # distance between nodes
154152
l.Et = kwargs.get('Et', np.zeros(4))
155153
l.fd = kwargs.get('fd',0)
156154
l.yx = kwargs.get('yx', np.zeros(2)) # [(y+Y)/2,(x,X)/2], from nodet
157-
l.baseT = kwargs.get('baseT', np.zeros(5))
155+
l.baseT = kwargs.get('baseT', np.zeros(4))
158156
l.derTT = kwargs.get('derTT', np.zeros((2,8))) # m_,d_ [M,D,n,o, I,G,A,L], sum across derH
159157
l.derH = kwargs.get('derH', []) # list of single-fork CLays
160-
l.box = kwargs.get('box', np.array([np.inf,-np.inf,np.inf,-np.inf])) # y,Y,x,X, area: (Y-y)*(X-x)
161158
# add med, rimt, extH in der+
162159
def __bool__(l): return bool(l.nodet)
163160

164-
def vectorize_root(frame, _fb_={}, fb_={}): # init for agg+:
165-
166-
if _fb_: # update ave based on feedback coefficients
167-
global ave, ave_d, ave_rn, ave_ro, ave_G, ave_L, max_dist, icoef, med_cost, ave_dI, mw_, dw_
168-
ave *= _fb_['agg_recursion'] # get feedback coefficient from comp_slice
169-
ave_d *= _fb_['agg_recursion']
170-
ave_rn *= _fb_['agg_recursion']
171-
ave_ro *= _fb_['agg_recursion']
172-
ave_G *= _fb_['agg_recursion']
173-
ave_L *= _fb_['agg_recursion']
174-
max_dist *= _fb_['agg_recursion']
175-
icoef *= _fb_['agg_recursion']
176-
med_cost *= _fb_['agg_recursion']
177-
ave_dI *= _fb_['agg_recursion']
178-
mw_ *= _fb_['agg_recursion']
179-
dw_ *= _fb_['agg_recursion']
180-
fb_['comp_slice'] = (ave+ave_d+ave_rn+ave_ro+ave_G+ave_L+max_dist+icoef+med_cost+ave_dI+sum(mw_)+sum(dw_))/26 # feedback coefficient to the next comp_slice
181-
161+
def vectorize_root(frame, rave_=[]): # init for agg+:
162+
# draft:
163+
global ave, ave_d, ave_rn, ave_ro, ave_G, ave_L, max_dist, icoef, med_cost, ave_dI, mw_, dw_
164+
if rave_:
165+
ave_ = np.array([ave, ave_d, ave_rn, ave_ro, ave_G, ave_L, max_dist, icoef, med_cost, ave_dI, mw_, dw_])
166+
ave, ave_d, ave_rn, ave_ro, ave_G, ave_L, max_dist, icoef, med_cost, ave_dI, mw_, dw_ = ave_ * rave_
167+
else:
168+
rave_ = np.append(np.ones(10), np.ones(10), np.ones(10)) # weight per ave?
182169
blob_ = unpack_blob_(frame)
183170
frame2G(frame, derH=[CLay(root=frame)], node_=[blob_], root=None)
184171
edge_ = [] # cluster, unpack
185172
for blob in blob_:
186173
if not blob.sign and blob.G > ave_G * blob.root.olp:
187-
edge = slice_edge(blob, _fb_, fb_)
174+
# convert rave_ to slice_edge coefs:
175+
edge = slice_edge(blob, rave_2SE(rave_))
188176
if edge.G * (len(edge.P_)-1) > ave: # eval PP
189-
comp_slice(edge)
177+
comp_slice(edge, rave_2CS(rave_))
190178
if edge.Et[0] * (len(edge.node_)-1)*(edge.rng+1) > ave:
191179
G_ = [PP2G(PP)for PP in edge.node_ if PP[-1][0] > ave] # Et, no altGs
192180
if len(G_) > ave_L: # no comp node_,link_,PPd_
@@ -207,7 +195,9 @@ def vectorize_root(frame, _fb_={}, fb_={}): # init for agg+:
207195
frame.link_ += [sum_G_(lG_)]; frame.lnest = 1
208196
frame.baseT = np.sum([G.baseT for G in PP_+ G_], axis=0)
209197
frame.derTT = np.sum([L.derTT for L in L_+ lG_], axis=0)
210-
return frame
198+
199+
rave = [] # convert rave for comp_slice
200+
return frame, rave
211201

212202
def cluster_edge(iG_, frame): # edge is CG but not a connectivity cluster, just a set of clusters in >ave G blob, unpack by default
213203

@@ -372,13 +362,13 @@ def base_comp(_N, N, dir=1): # comp Et, Box, baseT, derTT
372362
nM = M*rn; dM = _M - nM; mM = min(_M,nM) / max(_M,nM)
373363
nD = D*rn; dD = _D - nD; mD = min(_D,nD) / max(_D,nD)
374364
# comp baseT:
375-
_I,_G,_Dy,_Dx,_L = _N.baseT; I,G,Dy,Dx,L = N.baseT # I, G|D, angle, dimension
365+
_I,_G,_Dy,_Dx = _N.baseT; I,G,Dy,Dx = N.baseT # I, G|D, angle
376366
I *= rn; dI = _I - I; mI = abs(dI) / ave_dI
377367
G *= rn; dG = _G - G; mG = min(_G,G) / max(_G,G)
378368
mA, dA = comp_angle((_Dy,_Dx),(Dy*rn,Dx*rn))
379-
L *= rn
380369
if N.fd: # dimension is distance
381-
mL, dL = min(_L,L)/ max(_L,L), _L - L
370+
_L,L = _N.L, N.L
371+
mL,dL = min(_L,L)/ max(_L,L), _L - L
382372
else: # dimension is box area
383373
_y0,_x0,_yn,_xn =_N.box; _A = (_yn-_y0) * (_xn-_x0)
384374
y0, x0, yn, xn = N.box; A = (yn - y0) * (xn - x0)
@@ -399,12 +389,12 @@ def comp_N(_N,N, ave, fd, angle=None, dist=None, dir=1): # compare links, relat
399389
dderH = []
400390

401391
[m_,d_], rn = base_comp(_N, N, dir)
402-
baseT = np.array([(_N.baseT[0] +N.baseT[0]) /2, np.sum(d_* dw_), *angle, dist])
392+
baseT = np.array([(_N.baseT[0] +N.baseT[0]) /2, np.sum(d_* dw_), *angle])
403393
derTT = np.array([m_, d_])
404-
M = np.sum(m_*mw_); D = np.sum(np.abs(d_*dw_)) # feedback-weighted summation
394+
M = np.sum(m_*mw_); D = np.sum(np.abs(d_*dw_)) # feedback-weighted sum
405395
Et = np.array([M,D, 8, (_N.Et[3]+N.Et[3]) /2]) # n comp vars, inherited olp
406396

407-
Link = CL(fd=fd, nodet=[_N,N], baseT=baseT, derTT=derTT, yx=np.add(_N.yx,N.yx)/2, box=extend_box(_N.box, N.box))
397+
Link = CL(fd=fd, nodet=[_N,N], baseT=baseT, derTT=derTT, yx=np.add(_N.yx,N.yx)/2, L=dist)
408398
# spec / lay:
409399
if M > ave and (len(N.derH) > 2 or isinstance(N,CL)): # else derH is redundant to dext,vert
410400
dderH = comp_H(_N.derH, N.derH, rn, Link, Et, fd) # comp shared layers, if any
@@ -456,7 +446,7 @@ def sum2graph(root, grapht, fd, minL=0, maxL=None): # sum node and link params
456446
else: N_ += [N] # roots if minL
457447
N.root = graph
458448
yx_ += [N.yx]
459-
graph.box = extend_box(graph.box, N.box) # pre-compute graph.area += N.area?
449+
graph.box = extend_box(graph.box, N.box) # pre-compute graph.area += N.area? or from N.yx?
460450
graph.Et += N.Et * icoef # deeper, lower weight
461451
if i and not fd: graph.baseT += N.baseT
462452
# not in CL
@@ -570,7 +560,7 @@ def blob2G(G, **kwargs):
570560
G.lnest = kwargs.get('lnest',0) # link_H if > 0, link_[-1] is top L_
571561
G.derH = [] # sum from nodes, then append from feedback, maps to node_tree
572562
G.extH = [] # sum from rims
573-
G.baseT = np.zeros(5) # I,G,Dy,Dx
563+
G.baseT = np.zeros(4) # I,G,Dy,Dx
574564
G.derTT = kwargs.get('derTT', np.zeros((2,8))) # m_,d_ base params
575565
G.derTTe = kwargs.get('derTTe', np.zeros((2,8)))
576566
G.box = kwargs.get('box', np.array([np.inf,np.inf,-np.inf,-np.inf])) # y0,x0,yn,xn
@@ -584,8 +574,9 @@ def blob2G(G, **kwargs):
584574
def PP2G(PP):
585575
root, P_, link_, vert, latuple, A, S, box, yx, Et = PP
586576

587-
baseT = np.array((*latuple[:2], latuple[-2], *latuple[-1])) # I,G,Dy,Dx,L
588-
derTT = np.hstack((vert, np.zeros((2,2)))) # [I,G,A, M,D,L] -> [I,G,gA, M,D,n, empty L,A]
577+
baseT = np.array((*latuple[:2], *latuple[-1])) # I,G,Dy,Dx
578+
[mI,mG,mA,mM,mD,mL], [dI,dG,dA,dM,dD,dL] = vert
579+
derTT = np.array([np.array([mM,mD,mL,1,mI,mG,*mA,mL]), np.array([dM,dD,dL,1,dI,dG,*dA,dL])])
589580
y,x,Y,X = box; dy,dx = Y-y,X-x # A = (dy,dx); L = np.hypot(dy,dx)
590581
G = CG(root=root, fd=0, Et=Et, node_=P_, link_=[], baseT=baseT, derTT=derTT, box=box, yx=yx, aRad=np.hypot(dy/2, dx/2),
591582
derH=[[CLay(node_=P_,link_=link_, derTT=deepcopy(derTT)), CLay()]]) # empty dfork

0 commit comments

Comments
 (0)