Пример #1
0
def comp_angle(blob, render, verbose):

    AveB = int(aveB * blob.rdn)
    if render:  # don't render small blobs
        if blob.A < 100: render = False
    spliced_layers = []  # to extend root_blob sublayers

    # root fork is frame_blobs or comp_r
    ext_dert__, ext_mask__ = extend_dert(
        blob)  # dert__ boundaries += 1, for cross-comp in larger kernels

    if -blob.M > AveB:  # comp_a fork, replace with borrow_M if known
        blob.rng = 0
        blob.f_comp_a = 1
        adert__, mask__ = comp_a(ext_dert__,
                                 ext_mask__)  # compute abs ma, no indep eval
        if verbose: print('\na fork\n')
        blob.prior_forks.extend('a')

        if mask__.shape[0] > 2 and mask__.shape[
                1] > 2 and False in mask__:  # min size in y and x, least one dert in dert__
            sign__ = (
                -adert__[3] * adert__[9]
            ) > ave * pcoef  # -m * ma: variable value of comp_slice_, no ave_ma in comp_a

            cluster_sub_eval(
                blob, adert__, sign__, mask__, render,
                verbose)  # forms sub_blobs of fork p sign in unmasked area
            spliced_layers = [
                spliced_layers + sublayers for spliced_layers, sublayers in
                zip_longest(spliced_layers, blob.sublayers, fillvalue=[])
            ]

    return spliced_layers
Пример #2
0
def intra_blob(
    blob, **kwargs
):  # slice_blob or recursive input rng+ | angle cross-comp within input blob

    Ave = int(ave * blob.rdn)
    AveB = int(aveB * blob.rdn)
    verbose = kwargs.get('verbose')

    if kwargs.get('render') is not None:  # don't render small blobs
        if blob.A < 100: kwargs['render'] = False
    spliced_layers = []  # to extend root_blob sub_layers

    # root fork is frame_blobs or comp_r
    ext_dert__, ext_mask__ = extend_dert(
        blob)  # dert__ boundaries += 1, for cross-comp in larger kernels

    if -blob.M > AveB:  # comp_a fork, replace with borrow_M if known
        blob.rng = 0
        blob.f_comp_a = 1
        adert__, mask__ = comp_a(ext_dert__,
                                 ext_mask__)  # compute abs ma, no indep eval
        if kwargs.get('verbose'): print('\na fork\n')
        blob.prior_forks.extend('a')

        if mask__.shape[0] > 2 and mask__.shape[
                1] > 2 and False in mask__:  # min size in y and x, least one dert in dert__
            sign__ = (
                -adert__[3] * adert__[9]
            ) > ave * pcoef  # -m * ma: variable value of comp_slice_, no ave_ma in comp_a

            cluster_sub_eval(
                blob, adert__, sign__, mask__,
                **kwargs)  # forms sub_blobs of fork p sign in unmasked area
            spliced_layers = [
                spliced_layers + sub_layers for spliced_layers, sub_layers in
                zip_longest(spliced_layers, blob.sub_layers, fillvalue=[])
            ]

    elif blob.M > AveB:  # comp_r fork
        blob.rng += 1
        blob.f_comp_a = 0
        dert__, mask__ = comp_r(ext_dert__, Ave, blob.rng, ext_mask__)
        if kwargs.get('verbose'): print('\na fork\n')
        blob.prior_forks.extend('r')

        if mask__.shape[0] > 2 and mask__.shape[
                1] > 2 and False in mask__:  # min size in y and x, at least one dert in dert__
            sign__ = dert__[3] > 0  # m__: inverse deviation of g

            cluster_sub_eval(
                blob, dert__, sign__, mask__,
                **kwargs)  # forms sub_blobs of sign in unmasked area
            spliced_layers = [
                spliced_layers + sub_layers for spliced_layers, sub_layers in
                zip_longest(spliced_layers, blob.sub_layers, fillvalue=[])
            ]

    return spliced_layers
Пример #3
0
def intra_blob(
        blob, **kwargs):  # recursive input rng+ | angle cross-comp within blob

    if kwargs.get(
            'render'
    ) is not None:  # stop rendering sub-blobs when blob is too small
        if blob.S < 100:
            kwargs['render'] = False

    spliced_layers = []  # to extend root_blob sub_layers
    ext_dert__, ext_mask = extend_dert(blob)

    if blob.fia:  # comp_a -> P_blobs or comp_aga

        dert__, mask = comp_a(
            ext_dert__,
            ext_mask)  # -> ga sub_blobs -> P_blobs (comp_d, comp_P)
        if mask.shape[0] > 2 and mask.shape[
                1] > 2 and False in mask:  # min size in y and x, least one dert in dert__

            # P_blobs eval, tentative:
            if blob.G * (1 - blob.Ga / 6 * blob.S) - aveB * blob.rdn > 0:
                # G reduced by Ga value, max_ga=6?
                # G is second deviation, or replace with Adj_blobs borrow value?
                # flatten day and dax: this should done for both if blob.fia forks, or a general default?
                dert__ = list(dert__)
                dert__ = (dert__[0], dert__[1], dert__[2], dert__[3],
                          dert__[4], dert__[5][0], dert__[5][1], dert__[6][0],
                          dert__[6][1], dert__[7], dert__[8])

                crit__ = dert__[3] * (
                    1 - dert__[7] /
                    6) - ave * blob.rdn  # max_ga=6, separate from g and ga?
                sub_frame = cluster_derts_P(dert__, crit__, mask,
                                            ave * blob.rdn)
                sub_blobs = sub_frame['blob__']
                blob.Ls = len(sub_blobs)  # for visibility and next-fork rd
                blob.sub_layers = [sub_blobs]  # 1st layer of sub_blobs

            # comp_aga eval, tentative:
            elif blob.G / (1 - blob.Ga / 6 * blob.S) - aveB > 0:  # max_ga=6
                # G increased by Ga value,
                # G is second deviation or specific borrow value?
                # flatten day and dax?
                crit__ = dert__[3] / (1 - dert__[7] / 100) - ave * blob.rdn
                # similar to eval per blob, replace 100 with max_ga value?
                # record separately from g and ga?
                sub_blobs = cluster_derts(dert__, crit__, mask)
                blob.Ls = len(sub_blobs)  # for visibility and next-fork rd
                blob.sub_layers = [sub_blobs]  # 1st layer of sub_blobs

                for sub_blob in sub_blobs:  # evaluate for comp_aga only, not comp_r | P_blobs?
                    G = blob.G
                    adj_G = blob.adj_blobs[2]
                    borrow = min(abs(G),
                                 abs(adj_G) /
                                 2)  # or adjacent M if negative sign?
                    # same eval as in root elif?:
                    if sub_blob.G + borrow > ave * blob.rdn:  # also if +Ga, +Gaga, +Gr, +Gagr, +Grr...
                        # comp_aga: runnable but not correct, the issue of nested day and dax need to be fixed first
                        sub_blob.fia = 1
                        sub_blob.rdn = sub_blob.rdn + 1 + 1 / blob.Ls
                        blob.sub_layers += intra_blob(sub_blob, **kwargs)

            spliced_layers = [
                spliced_layers + sub_layers for spliced_layers, sub_layers in
                zip_longest(spliced_layers, blob.sub_layers, fillvalue=[])
            ]

    else:  # comp_r -> comp_r or comp_a
        if blob.M > aveB:
            dert__, mask = comp_r(ext_dert__, blob.fca, ext_mask)
            crit__ = dert__[4] - ave * blob.rdn

            if mask.shape[0] > 2 and mask.shape[
                    1] > 2 and False in mask:  # min size in y and x, least one dert in dert__

                sub_blobs = cluster_derts(dert__,
                                          crit__,
                                          mask,
                                          verbose=False,
                                          **kwargs)  # -> m sub_blobs
                # replace lines below with generic sub_eval()?

                blob.Ls = len(sub_blobs)  # for visibility and next-fork rdn
                blob.sub_layers = [sub_blobs]  # 1st layer of sub_blobs

                for sub_blob in sub_blobs:  # evaluate for intra_blob comp_a | comp_r | P_blobs:
                    G = blob.G
                    adj_G = blob.adj_blobs[2]
                    borrow = min(abs(G),
                                 abs(adj_G) /
                                 2)  # or adjacent M if negative sign?

                    if sub_blob.G + borrow > ave * blob.rdn:  # also if +Ga, +Gaga, +Gr, +Gagr, +Grr...
                        # comp_a:
                        sub_blob.rdn = sub_blob.rdn + 1 + 1 / blob.Ls
                        blob.sub_layers += intra_blob(sub_blob, **kwargs)

                    elif sub_blob.M > ave * blob.rdn:  # if +Mr, +Mrr...
                        # comp_r:
                        sub_blob.rdn = sub_blob.rdn + 1 + 1 / blob.Ls
                        sub_blob.fcr = 1
                        sub_blob.rng = blob.rng * 2
                        blob.sub_layers += intra_blob(sub_blob, **kwargs)

                spliced_layers = [
                    spliced_layers + sub_layers
                    for spliced_layers, sub_layers in zip_longest(
                        spliced_layers, blob.sub_layers, fillvalue=[])
                ]

        elif blob.G > aveB:
            dert__, mask = comp_a(ext_dert__, ext_mask)  # -> m sub_blobs
            crit__ = dert__[3] - ave * blob.rdn
            # call new sub_eval(), same as in comp_r fork

    return spliced_layers
Пример #4
0
def intra_blob(
    blob, **kwargs
):  # slice_blob or recursive input rng+ | angle cross-comp within input blob

    Ave = int(ave * blob.rdn)
    AveB = int(aveB * blob.rdn)
    verbose = kwargs.get('verbose')

    if kwargs.get('render') is not None:  # don't render small blobs
        if blob.A < 100: kwargs['render'] = False
    spliced_layers = []  # to extend root_blob sub_layers

    if blob.f_root_a:
        # root fork is comp_a -> slice_blob
        if blob.mask__.shape[0] > 2 and blob.mask__.shape[
                1] > 2 and False in blob.mask__:  # min size in y and x, at least one dert in dert__

            if (
                    -blob.Dert.M * blob.Dert.Ma - AveB > 0
            ) and blob.Dert.Dx:  # vs. G reduced by Ga: * (1 - Ga / (4.45 * A)), max_ga=4.45
                blob.f_comp_a = 0
                blob.prior_forks.extend('p')
                if kwargs.get('verbose'): print('\nslice_blob fork\n')
                segment_by_direction(blob, verbose=True)
                # derP_ = slice_blob(blob, [])  # cross-comp of vertically consecutive Ps in selected stacks
                # blob.PP_ = derP_2_PP_(derP_, blob.PP_)  # form vertically contiguous patterns of patterns
    else:
        # root fork is frame_blobs or comp_r
        ext_dert__, ext_mask__ = extend_dert(
            blob)  # dert__ boundaries += 1, for cross-comp in larger kernels

        if blob.Dert.G > AveB:  # comp_a fork, replace G with borrow_M when known

            adert__, mask__ = comp_a(ext_dert__, Ave, blob.prior_forks,
                                     ext_mask__)  # compute ma and ga
            blob.f_comp_a = 1
            if kwargs.get('verbose'): print('\na fork\n')
            blob.prior_forks.extend('a')

            if mask__.shape[0] > 2 and mask__.shape[
                    1] > 2 and False in mask__:  # min size in y and x, least one dert in dert__
                sign__ = adert__[3] * adert__[
                    8] > 0  # g * (ma / ave: deviation rate, no independent value, not co-measurable with g)

                dert__ = tuple([
                    adert__[0], adert__[1], adert__[2], adert__[3], adert__[4],
                    adert__[5][0], adert__[5][1], adert__[6][0], adert__[6][1],
                    adert__[7], adert__[8]
                ])  # flatten adert

                cluster_sub_eval(
                    blob, dert__, sign__, mask__,
                    **kwargs)  # forms sub_blobs of sign in unmasked area
                spliced_layers = [
                    spliced_layers + sub_layers
                    for spliced_layers, sub_layers in zip_longest(
                        spliced_layers, blob.sub_layers, fillvalue=[])
                ]

        elif blob.Dert.M > AveB * 1.2:  # comp_r fork, ave M = ave G * 1.2

            dert__, mask__ = comp_r(ext_dert__, Ave, blob.f_root_a, ext_mask__)
            blob.f_comp_a = 0
            if kwargs.get('verbose'): print('\na fork\n')
            blob.prior_forks.extend('r')

            if mask__.shape[0] > 2 and mask__.shape[
                    1] > 2 and False in mask__:  # min size in y and x, at least one dert in dert__
                sign__ = dert__[4] > 0  # m__ is inverse deviation of SAD

                cluster_sub_eval(
                    blob, dert__, sign__, mask__,
                    **kwargs)  # forms sub_blobs of sign in unmasked area
                spliced_layers = [
                    spliced_layers + sub_layers
                    for spliced_layers, sub_layers in zip_longest(
                        spliced_layers, blob.sub_layers, fillvalue=[])
                ]

    return spliced_layers
Пример #5
0
def intra_blob(blob,
               **kwargs):  # recursive input rng+ | der+ cross-comp within blob
    # fig: flag input is g | p, fcr: flag comp over rng+ | der+
    if kwargs.get(
            'render'
    ) is not None:  # stop rendering sub-blobs when blob is too small
        if blob.S < 100:
            kwargs['render'] = False

    spliced_layers = []  # to extend root_blob sub_layers
    ext_dert__, ext_mask = extend_dert(blob)
    if blob.fca:  # comp_a
        dert__, mask = comp_a(ext_dert__,
                              ext_mask)  # -> xy_blobs (comp_d, comp_P)
    else:  # comp_r
        dert__, mask = comp_r(ext_dert__, blob.fcr, ext_mask)  # -> m sub_blobs

    if mask.shape[0] > 2 and mask.shape[
            1] > 2 and False in mask:  # min size in y and x, least one dert in dert__
        sub_blobs = cluster_derts(dert__,
                                  mask,
                                  ave * blob.rdn,
                                  blob.fcr,
                                  blob.fca,
                                  verbose=False,
                                  **kwargs)

        # fork params:
        blob.Ls = len(sub_blobs)  # for visibility and next-fork rdn
        blob.sub_layers = [sub_blobs]  # 1st layer of sub_blobs

        for sub_blob in sub_blobs:  # evaluate for intra_blob comp_a | comp_r | xy_blobs:

            G = blob.G
            adj_G = blob.adj_blobs[2]
            borrow = min(abs(G),
                         abs(adj_G) / 2)  # or adjacent M if negative sign?

            # +Ga, +Gaga, +Gr, +Gagr, +Grr and so on
            if sub_blob.G + borrow > ave * blob.rdn:  # comp_a
                sub_blob.rdn = sub_blob.rdn + 1 + 1 / blob.Ls
                blob.sub_layers += intra_blob(sub_blob, **kwargs)

            # +Ma, +Maga, +Magr and so on (root fork of xy_blobs always = comp_a)
            elif blob.fca == 1 and sub_blob.M > ave * blob.rdn:  # xy_blobs
                image_to_blobs(sub_blob.root_dert__,
                               verbose=False,
                               render=False)

            # +Mr, +Mrr and so on
            elif sub_blob.M > ave * blob.rdn:  # comp_r
                sub_blob.rdn = sub_blob.rdn + 1 + 1 / blob.Ls
                sub_blob.fcr = 1
                sub_blob.rng = blob.rng * 2
                blob.sub_layers += intra_blob(sub_blob, **kwargs)

        spliced_layers = [
            spliced_layers + sub_layers for spliced_layers, sub_layers in
            zip_longest(spliced_layers, blob.sub_layers, fillvalue=[])
        ]
    return spliced_layers
Пример #6
0
def intra_blob(
        blob, **kwargs):  # recursive input rng+ | angle cross-comp within blob

    Ave = int(ave * blob.rdn)
    AveB = int(aveB * blob.rdn)

    if kwargs.get(
            'render'
    ) is not None:  # stop rendering sub-blobs when blob is too small
        if blob.S < 100:
            kwargs['render'] = False

    spliced_layers = []  # to extend root_blob sub_layers
    ext_dert__, ext_mask = extend_dert(blob)

    if blob.fia:  # comp_a -> P_blobs or comp_aga

        dert__, mask = comp_a(
            ext_dert__, Ave,
            ext_mask)  # -> ga sub_blobs -> P_blobs (comp_d, comp_P)
        if mask.shape[0] > 2 and mask.shape[
                1] > 2 and False in mask:  # min size in y and x, least one dert in dert__

            # cluster_derts_P eval, tentative, no cluster_derts_P yet:
            if blob.G * (1 - blob.Ga /
                         (4.45 * blob.S)) - AveB > 0:  # max_ga=4.45?
                # G reduced by Ga value, base G is second deviation or specific borrow value
                # flatten day and dax, not generalized for nested day and dax yet:
                dert__ = list(dert__)
                dert__ = (dert__[0], dert__[1], dert__[2], dert__[3],
                          dert__[4], dert__[5][0], dert__[5][1], dert__[6][0],
                          dert__[6][1], dert__[7], dert__[8])

                crit__ = dert__[3] * (
                    1 - dert__[7] / 4.45
                ) - Ave  # max_ga=4.45, record separately from g and ga?
                # ga is not signed, thus additional eval, different Ave?
                blob.fca = 0
                sub_eval(blob, dert__, crit__, mask, **kwargs)

            # comp_aga eval, inverse relative ga value, tentative, no comp_aga yet:
            elif blob.G / (
                    1 - blob.Ga / (4.45 * blob.S)
            ) - AveB > 0:  # max_ga=4.45, init G is 2nd deviation or borrow value
                # G increased by Ga value,  flatten day and dax?
                crit__ = dert__[3] / (
                    1 - dert__[7] / 4.45
                ) - Ave  # ~ eval per blob, record separately from g and ga?
                # ga is not signed, thus additional eval, different Ave?
                blob.fca = 1
                sub_eval(blob, dert__, crit__, mask, **kwargs)

            spliced_layers = [
                spliced_layers + sub_layers for spliced_layers, sub_layers in
                zip_longest(spliced_layers, blob.sub_layers, fillvalue=[])
            ]

    else:  # comp_r -> comp_r or comp_a
        if blob.M > AveB:
            dert__, mask = comp_r(ext_dert__, Ave, blob.fia, ext_mask)
            crit__ = dert__[4]  # signed inverse deviation of SAD

            if mask.shape[0] > 2 and mask.shape[
                    1] > 2 and False in mask:  # min size in y and x, least one dert in dert__
                sub_eval(blob, dert__, crit__, mask, **kwargs)
                spliced_layers = [
                    spliced_layers + sub_layers
                    for spliced_layers, sub_layers in zip_longest(
                        spliced_layers, blob.sub_layers, fillvalue=[])
                ]

        elif blob.G > AveB:
            dert__, mask = comp_a(ext_dert__, Ave, ext_mask)  # -> m sub_blobs
            crit__ = dert__[3]  # signed deviation of g

            if mask.shape[0] > 2 and mask.shape[
                    1] > 2 and False in mask:  # min size in y and x, least one dert in dert__
                sub_eval(blob, dert__, crit__, mask, **kwargs)
                spliced_layers = [
                    spliced_layers + sub_layers
                    for spliced_layers, sub_layers in zip_longest(
                        spliced_layers, blob.sub_layers, fillvalue=[])
                ]

    return spliced_layers
Пример #7
0
def intra_blob_root(
        root_blob, render, verbose, fBa
):  # recursive evaluation of cross-comp slice| range| angle per blob

    # deep_blobs = []  # for visualization
    spliced_layers = []
    if fBa: blob_ = root_blob.asublayers[0]
    else: blob_ = root_blob.rsublayers[0]

    for blob in blob_:  # fork-specific blobs, print('Processing blob number ' + str(bcount))

        blob.prior_forks = root_blob.prior_forks.copy(
        )  # increment forking sequence: g -> r|a, a -> p
        blob.root_dert__ = root_blob.dert__
        blob_height = blob.box[1] - blob.box[0]
        blob_width = blob.box[3] - blob.box[2]

        if blob_height > 3 and blob_width > 3:  # min blob dimensions: Ly, Lx
            if root_blob.fBa:
                # comp_slice fork in angle blobs
                # add evaluate splice_blobs(root_blob), normally in frame_bblobs?
                AveB = aveB * (
                    blob.rdn + 1
                )  # comp_slice is doubling the costs, likely higher, adjust per nsub_blobs?
                if (AveB - blob.G) + (
                        blob.G - AveB *
                        pcoef) > 0:  # val_comp_slice_blob = dev_G + inv_dev_Ga
                    blob.fBa = 0
                    blob.rdn = root_blob.rdn + 1
                    comp_slice_root(blob, verbose=True)
                    blob.prior_forks.extend('p')
                    if verbose:
                        print(
                            '\nslice_blob fork\n'
                        )  # if render and blob.A < 100: deep_blobs.append(blob)
            else:
                ext_dert__, ext_mask__ = extend_dert(
                    blob)  # dert__+= 1: cross-comp in larger kernels
                ''' 
                comp_r || comp_a, gap or overlap version:
                if aveBa < 1: blobs of ~average G are processed by both forks
                if aveBa > 1: blobs of ~average G are not processed:
                '''
                if blob.G < aveB * blob.rdn:  # below-average G, eval for comp_r
                    # root values for sub_blobs:
                    blob.fBa = 0
                    blob.rng = root_blob.rng + 1
                    blob.rdn = root_blob.rdn + 1.5  # comp cost * fork rdn
                    # comp_r 4x4:
                    new_dert__, new_mask__ = comp_r(ext_dert__, blob.rng,
                                                    ext_mask__)
                    sign__ = ave * (blob.rdn +
                                    1) - new_dert__[3] > 0  # m__ = ave - g__
                    blob.prior_forks.extend('r')
                    # if min Ly and Lx, dert__>=1: form, splice sub_blobs:
                    if new_mask__.shape[0] > 2 and new_mask__.shape[
                            1] > 2 and False in new_mask__:
                        cluster_fork_recursive(blob,
                                               spliced_layers,
                                               new_dert__,
                                               sign__,
                                               new_mask__,
                                               verbose,
                                               render,
                                               fBa=0)

                if blob.G > aveB * aveBa * blob.rdn:  # above-average G, eval for comp_a
                    # root values for sub_blobs:
                    blob.fBa = 1
                    blob.rdn = root_blob.rdn + 1.5  # comp cost * fork rdn
                    # comp_a 2x2:
                    new_dert__, new_mask__ = comp_a(ext_dert__, ext_mask__)
                    Ave = ave * (blob.rdn + 1)
                    sign__ = (new_dert__[1] - Ave) + (
                        Ave * pcoef - new_dert__[2]
                    ) > 0  # val_comp_slice_= dev_gr + inv_dev_ga
                    blob.prior_forks.extend('a')
                    # if min Ly and Lx, dert__>=1: form, splice sub_blobs:
                    if new_mask__.shape[0] > 2 and new_mask__.shape[
                            1] > 2 and False in new_mask__:
                        cluster_fork_recursive(blob,
                                               spliced_layers,
                                               new_dert__,
                                               sign__,
                                               new_mask__,
                                               verbose,
                                               render,
                                               fBa=1)
            '''
            exclusive forks version:
            
            vG = blob.G - ave_G  # deviation of gradient, from ave per blob, combined max rdn = blob.rdn+1:
            vvG = abs(vG) - ave_vG * blob.rdn  # 2nd deviation of gradient, from fixed costs of if "new_dert__" loop below
            # vvG = 0 maps to max G for comp_r if vG < 0, and to min G for comp_a if vG > 0:
            
            if blob.sign:  # sign of pixel-level g, which corresponds to sign of blob vG, so we don't need the later
                if vvG > 0:  # below-average G, eval for comp_r...
                elif vvG > 0:  # above-average G, eval for comp_a...
            '''
    if verbose:
        print("\rFinished intra_blob")  # print_deep_blob_forking(deep_blobs)

    return spliced_layers