text:
'''
    P_blobs is a terminal fork of intra_blob. It calls comp_g and then comp_P (edge tracing) per terminated stack.

    Pixel-level parameters are accumulated in contiguous spans of same-sign gradient, first horizontally then vertically.
    Horizontal spans are Ps: 1D patterns, and vertical spans are first stacks of Ps, then blobs of stacks.

    This processing adds a level of encoding per row y, defined relative to y of current input row, with top-down scan:
    1Le, line y-1: form_P( dert_) -> 1D pattern P: contiguous row segment, a slice of a blob
    2Le, line y-2: scan_P_(P, hP) -> hP, up_connect_, down_connect_count: vertical connections per stack of Ps
    3Le, line y-3: form_stack(hP, stack) -> stack: merge vertically-connected _Ps into non-forking stacks of Ps
    4Le, line y-4+ stack depth: form_blob(stack, blob): merge connected stacks in blobs referred by up_connect_, recursively

    Higher-row elements include additional parameters, derived while they were lower-row elements.
    Resulting blob structure (fixed set of parameters per blob):
    - Dert: summed pixel-level dert params, Dx, surface area S, vertical depth Ly
    - sign = s: sign of gradient deviation
    - box  = [y0, yn, x0, xn],
    - dert__,  # 2D array of pixel-level derts: (p, dy, dx, g, m) tuples
    - stack_,  # contains intermediate blob composition structures: stacks and Ps, not meaningful on their own
    ( intra_blob structure extends Dert, adds fork params and sub_layers)

    Blob is 2D pattern: connectivity cluster defined by the sign of gradient deviation. Gradient represents 2D variation
    per pixel. It is used as inverse measure of partial match (predictive value) because direct match (min intensity)
    is not meaningful in vision. Intensity of reflected light doesn't correlate with predictive value of observed object
    (predictive value is physical density, hardness, inertia that represent resistance to change in positional parameters)

    Comparison range is fixed for each layer of search, to enable encoding of input pose parameters: coordinates, dimensions,
    orientation. These params are essential because value of prediction = precision of what * precision of where.
    Clustering is by nearest-neighbor connectivity only, to avoid overlap among the blobs.

    frame_blobs is a complex function with a simple purpose: to sum pixel-level params in blob-level params. These params
    were derived by pixel cross-comparison (cross-correlation) to represent predictive value per pixel, so they are also
    predictive on a blob level, and should be cross-compared between blobs on the next level of search and composition.
    Old diagrams: https://kwcckw.github.io/CogAlg/
'''
"""
usage: frame_blobs_find_adj.py [-h] [-i IMAGE] [-v VERBOSE] [-n INTRA] [-r RENDER]
                      [-z ZOOM]
optional arguments:
  -h, --help            show this help message and exit
  -i IMAGE, --image IMAGE
                        path to image file
  -v VERBOSE, --verbose VERBOSE
                        print details, useful for debugging
  -n INTRA, --intra INTRA
                        run intra_blobs after frame_blobs
  -r RENDER, --render RENDER
                        render the process
  -z ZOOM, --zoom ZOOM  zooming ratio when rendering
"""

from time import time
from collections import deque
from pathlib import Path
import sys
import numpy as np
from operator import attrgetter

from class_cluster import ClusterStructure, NoneType
from class_bind import AdjBinder
# from comp_pixel import comp_pixel
from class_stream import BlobStreamer
from utils import (pairwise, imread)
from comp_P_draft import cluster_P_

ave = 30  # filter or hyper-parameter, set as a guess, latter adjusted by feedback
aveG = 50  # filter for comp_g, assumed constant direction

class CP(ClusterStructure):
    I = int  # default type at initialization
    Dy = int
    Dx = int
    G = int
    M = int
    Dyy = int
    Dyx = int
    Dxy = int
    Dxx = int
    Ga = int
    Ma = int
    L = int
    x0 = int
    sign = NoneType
    dert_ = list
    gdert_ = list
    Dg = int
    Mg = int

class Cstack(ClusterStructure):
    I = int
    Dy = int
    Dx = int
    G = int
    M = int
    Dyy = int
    Dyx = int
    Dxy = int
    Dxx = int
    Ga = int
    Ma = int
    S = int
    Ly = int
    y0 = int
    Py_ = list
    blob = NoneType
    down_connect_cnt = int
    sign = NoneType
    fPP = bool  # PPy_ if 1, else Py_

class CBlob(ClusterStructure):
    Dert = dict
    box = list
    stack_ = list
    sign = NoneType
    open_stacks = int
    root_dert__ = object
    dert__ = object
    mask = object
    adj_blobs = list
    fopen = bool
    margin = list

# Functions:
# prefix '_' denotes higher-line variable or structure, vs. same-type lower-line variable or structure
# postfix '_' denotes array name, vs. same-name elements of that array

def P_blobs(dert__, mask, crit__, verbose=False, render=False):
    frame = dict(rng=1, dert__=dert__, mask=None, I=0, Dy=0, Dx=0, G=0, M=0, Dyy=0, Dyx=0, Dxy=0, Dxx=0, Ga=0, Ma=0, blob__=[])
    stack_ = deque()  # buffer of running vertical stacks of Ps
    height, width = dert__[0].shape

    if render:
        def output_path(input_path, suffix):
            return str(Path(input_path).with_suffix(suffix))

        streamer = BlobStreamer(CBlob, dert__[1],
                                record_path=output_path(arguments['image'],
                                                        suffix='.im2blobs.avi'))
    stack_binder = AdjBinder(Cstack)

    if verbose:
        start_time = time()
        print("Converting the image(frame) to blobs...")

    for y, dert_ in enumerate(zip(*dert__)):  # first and last row are discarded
        if verbose:
            print(f"\rProcessing line {y + 1}/{height}, ", end="")
            print(f"{len(frame['blob__'])} blobs converted", end="")
            sys.stdout.flush()

        P_binder = AdjBinder(CP)  # binder needs data about clusters of the same level
        P_ = form_P_(zip(*dert_), crit__[y], mask[y], P_binder)  # horizontal clustering

        if render:
            render = streamer.update_blob_conversion(y, P_)
        P_ = scan_P_(P_, stack_, frame, P_binder)  # vertical clustering, adds P up_connects and _P down_connect_cnt
        stack_ = form_stack_(P_, frame, y)
        stack_binder.bind_from_lower(P_binder)

    while stack_:  # frame ends, last-line stacks are merged into their blobs
        form_blob(stack_.popleft(), frame)

    blob_binder = AdjBinder(CBlob)
    blob_binder.bind_from_lower(stack_binder)
    assign_adjacents(blob_binder)  # add adj_blobs to each blob

    if verbose:  # print out at the end
        nblobs = len(frame['blob__'])
        print(f"\rImage has been successfully converted to "
              f"{nblobs} blob{'s' if nblobs != 1 else 0} in "
              f"{time() - start_time:.3} seconds", end="")
        blob_ids = [blob_id for blob_id in range(CBlob.instance_cnt)]
        merged_percentage = len([*filter(lambda bid: CBlob.get_instance(bid) is None, blob_ids)]) / len(blob_ids)
        print(f"\nPercentage of merged blobs: {merged_percentage}")

    if render:  # rendering mode after blob conversion
        path = output_path(arguments['image'],
                           suffix='.im2blobs.jpg')
        streamer.end_blob_conversion(y, img_out_path=path)

    return frame  # frame of blobs

'''
Parameterized connectivity clustering functions below:
- form_P sums dert params within P and increments its L: horizontal length.
- scan_P_ searches for horizontal (x) overlap between Ps of consecutive (in y) rows.
- form_stack combines these overlapping Ps into vertical stacks of Ps, with one up_P to one down_P
- form_blob merges terminated or forking stacks into blob, removes redundant representations of the same blob
  by multiple forked P stacks, then checks for blob termination and merger into whole-frame representation.
dert: tuple of derivatives per pixel, initially (p, dy, dx, g), will be extended in intra_blob
Dert: params of cluster structures (P, stack, blob): summed dert params + dimensions: vertical Ly and area S
'''

def form_P_(idert_, crit_, mask_, binder):  # segment dert__ into P__, in horizontal ) vertical order

    P_ = deque()  # row of Ps
    s_ = crit_ > 0
    x0 = 0
    try:
        while mask_[x0]:  # skip until not masked
            next(idert_)
            x0 += 1
    except IndexError:
        return P_  # the whole line is masked, return an empty P

    dert_ = [[*next(idert_)]]  # get first dert from idert_ (generator/iterator)
    (I, Dy, Dx, G, M, Dyy, Dyx, Dxy, Dxx, Ga, Ma), L = dert_[0], 1  # initialize P params
    _s = s_[x0]
    _mask = mask_[x0]  # mask bit per dert

    for x, (p, dy, dx, g, m, dyy, dyx, dxy, dxx, ga, ma) in enumerate(idert_, start=x0 + 1):  # loop left to right in each row of derts
        mask = mask_[x]
        if ~mask:  # current dert is not masked
            s = s_[x]
            if ~_mask and s != _s:  # prior dert is not masked and sign changed, terminate and pack P:
                P = CP(I=I, Dy=Dy, Dx=Dx, G=G, M=M, Dyy=Dyy, Dyx=Dyx, Dxy=Dxy, Dxx=Dxx, Ga=Ga, Ma=Ma, L=L, x0=x0, sign=_s, dert_=dert_)
                P_.append(P)
                I= Dy= Dx= G= M= Dyy= Dyx= Dxy= Dxx= Ga= Ma= L= 0; x0 = x; dert_ = []  # initialize P params
            elif _mask:
                I= Dy= Dx= G= M= Dyy= Dyx= Dxy= Dxx= Ga= Ma= L= 0; x0 = x; dert_ = []  # initialize P params
        elif ~_mask:
            # dert is masked, prior dert is not masked, pack P:
            P = CP(I=I, Dy=Dy, Dx=Dx, G=G, M=M, Dyy=Dyy, Dyx=Dyx, Dxy=Dxy, Dxx=Dxx, Ga=Ga, Ma=Ma, L=L, x0=x0, sign=_s, dert_=dert_)
            P_.append(P)

        if ~mask:  # accumulate P params:
            I += p
            Dy += dy
            Dx += dx
            G += g
            M += m
            Dyy += dyy
            Dyx += dyx
            Dxy += dxy
            Dxx += dxx
            Ga += ga
            Ma += Ma
            L += 1
            dert_.append([p, dy, dx, g, m, dyy, dyx, dxy, dxx, ga, ma])  # accumulate dert into dert_ if no sign change
            _s = s  # prior sign
        _mask = mask

    if ~_mask:  # terminate and pack last P in a row if prior dert is unmasked
        P = CP(I=I, Dy=Dy, Dx=Dx, G=G, M=M, Dyy=Dyy, Dyx=Dyx, Dxy=Dxy, Dxx=Dxx, Ga=Ga, Ma=Ma, L=L, x0=x0, sign=_s, dert_=dert_)
        P_.append(P)

    for _P, P in pairwise(P_):
        if _P.x0 + _P.L == P.x0:  # check if Ps are adjacents
            binder.bind(_P, P)

    return P_

def scan_P_(P_, stack_, frame, binder):  # merge P into higher-row stack of Ps which have same sign and overlap by x_coordinate
    '''
    Each P in P_ scans higher-row _Ps (in stack_) left-to-right, testing for x-overlaps between Ps and same-sign _Ps.
    Overlap is represented as up_connect in P and is added to down_connect_cnt in _P. Scan continues until P.x0 >= _P.xn:
    no x-overlap between P and next _P. Then P is packed into its up_connect stacks or initializes a new stack.
    After such test, loaded _P is also tested for x-overlap to the next P.
    If negative, a stack with loaded _P is removed from stack_ (buffer of higher-row stacks) and tested for down_connect_cnt==0.
    If so: no lower-row connections, the stack is packed into connected blobs (referred by its up_connect_),
    else the stack is recycled into next_stack_, for next-row run of scan_P_.
    It's a form of breadth-first flood fill, with connects as vertices per stack of Ps: a node in connectivity graph.
    '''
    next_P_ = deque()  # to recycle P + up_connect_ that finished scanning _P, will be converted into next_stack_

    if P_ and stack_:  # if both input row and higher row have any Ps / _Ps left

        P = P_.popleft()  # load left-most (lowest-x) input-row P
        stack = stack_.popleft()  # higher-row stacks
        _P = stack.Py_[-1]  # last element of each stack is higher-row P
        up_connect_ = []  # list of same-sign x-overlapping _Ps per P

        while True:  # while both P_ and stack_ are not empty

            x0 = P.x0  # first x in P
            xn = x0 + P.L  # first x in next P
            _x0 = _P.x0  # first x in _P
            _xn = _x0 + _P.L  # first x in next _P

            if stack.G > 0:  # check for overlaps in 8 directions, else a blob may leak through its external blob
                if _x0 - 1 < xn and x0 < _xn + 1:  # x overlap between loaded P and _P
                    if P.sign == stack.sign:  # sign match
                        stack.down_connect_cnt += 1
                        up_connect_.append(stack)  # buffer P-connected higher-row stacks into P' up_connect_
                    else:
                        binder.bind(_P, P)

            else:  # -G, check for orthogonal overlaps only: 4 directions, edge blobs are more selective
                if _x0 < xn and x0 < _xn:  # x overlap between loaded P and _P
                    if P.sign == stack.sign:  # sign match
                        stack.down_connect_cnt += 1
                        up_connect_.append(stack)  # buffer P-connected higher-row stacks into P' up_connect_
                    else:
                        binder.bind(_P, P)

            if (xn < _xn or  # _P overlaps next P in P_
                    xn == _xn and stack.sign):  # check in 8 directions
                next_P_.append((P, up_connect_))  # recycle _P for the next run of scan_P_
                up_connect_ = []
                if P_:
                    P = P_.popleft()  # load next P
                else:  # terminate loop
                    if stack.down_connect_cnt != 1:  # terminate stack, merge it into up_connects' blobs
                        form_blob(stack, frame)
                    break
            else:  # no next-P overlap
                if stack.down_connect_cnt != 1:  # terminate stack, merge it into up_connects' blobs
                    form_blob(stack, frame)
                if stack_:  # load stack with next _P
                    stack = stack_.popleft()
                    _P = stack.Py_[-1]
                else:  # no stack left: terminate loop
                    next_P_.append((P, up_connect_))
                    break

    # terminate Ps and stacks that continue at row's end
    while P_:
        next_P_.append((P_.popleft(), []))  # no up_connect
    while stack_:
        form_blob(stack_.popleft(), frame)  # down_connect_cnt==0

    return next_P_  # each element is P + up_connect_ refs

def form_stack_(P_, frame, y):  # Convert or merge every P into its stack of Ps, merge blobs

    next_stack_ = deque()  # converted to stack_ in the next run of scan_P_

    while P_:
        P, up_connect_ = P_.popleft()
        I, Dy, Dx, G, M, Dyy, Dyx, Dxy, Dxx, Ga, Ma, L, x0, s, dert_, _, _, _ = P.unpack()
        xn = x0 + L  # next-P x0
        if not up_connect_:
            # initialize new stack for each input-row P that has no connections in higher row, as in the whole top row:
            blob = CBlob(Dert=dict(I=0, Dy=0, Dx=0, G=0, M=0, Dyy=0, Dyx=0, Dxy=0, Dxx=0, Ga=0, Ma=0, S=0, Ly=0),
                         box=[y, x0, xn], stack_=[], sign=s, open_stacks=1)
            new_stack = Cstack(I=I, Dy=Dy, Dx=Dx, G=G, M=M, Dyy=Dyy, Dyx=Dyx, Dxy=Dxy, Dxx=Dxx, Ga=Ga, Ma=Ma, S=L, Ly=1,
                               y0=y, Py_=[P], blob=blob, down_connect_cnt=0, sign=s, fPP=0)
            new_stack.hid = blob.id
            blob.stack_.append(new_stack)

        else:
            if len(up_connect_) == 1 and up_connect_[0].down_connect_cnt == 1:
                # P has one up_connect and that up_connect has one down_connect=P: merge P into up_connect stack:
                new_stack = up_connect_[0]
                new_stack.accumulate(I=I, Dy=Dy, Dx=Dx, G=G, M=M, Dyy=Dyy, Dyx=Dyx, Dxy=Dxy, Dxx=Dxx, Ga=Ga, Ma=Ma, S=L, Ly=1)
                new_stack.Py_.append(P)  # Py_: vertical buffer of Ps
                new_stack.down_connect_cnt = 0  # reset down_connect_cnt
                blob = new_stack.blob

            else:  # P has >1 up_connects, or 1 up_connect that has >1 down_connect_cnt:
                blob = up_connect_[0].blob
                # initialize new_stack with up_connect blob:
                new_stack = Cstack(I=I, Dy=Dy, Dx=Dx, G=G, M=M, Dyy=Dyy, Dyx=Dyx, Dxy=Dxy, Dxx=Dxx, Ga=Ga, Ma=Ma, S=L, Ly=1,
                                   y0=y, Py_=[P], blob=blob, down_connect_cnt=0, sign=s, fPP=0)
                new_stack.hid = blob.id
                blob.stack_.append(new_stack)  # stack is buffered into blob

                if len(up_connect_) > 1:  # merge blobs of all up_connects
                    if up_connect_[0].down_connect_cnt == 1:  # up_connect is not terminated
                        form_blob(up_connect_[0], frame)  # merge stack of 1st up_connect into its blob

                    for up_connect in up_connect_[1:len(up_connect_)]:  # merge blobs of other up_connects into blob of 1st up_connect
                        if up_connect.down_connect_cnt == 1:
                            form_blob(up_connect, frame)

                        if not up_connect.blob is blob:
                            Dert, box, stack_, s, open_stacks = up_connect.blob.unpack()[:5]  # merged blob
                            I, Dy, Dx, G, M, Dyy, Dyx, Dxy, Dxx, Ga, Ma, S, Ly = Dert.values()
                            accum_Dert(blob.Dert, I=I, Dy=Dy, Dx=Dx, G=G, M=M, Dyy=Dyy, Dyx=Dyx, Dxy=Dxy, Dxx=Dxx, Ga=Ga, Ma=Ma, S=S, Ly=Ly)
                            blob.open_stacks += open_stacks
                            blob.box[0] = min(blob.box[0], box[0])  # extend box y0
                            blob.box[1] = min(blob.box[1], box[1])  # extend box x0
                            blob.box[2] = max(blob.box[2], box[2])  # extend box xn
                            for stack in stack_:
                                if not stack is up_connect:
                                    stack.blob = blob  # blobs in other up_connects are refs to blob in first up_connect
                                    stack.hid = blob.id
                                    blob.stack_.append(stack)  # buffer of merged root stacks.

                            up_connect.blob = blob
                            up_connect.hid = blob.id
                            blob.stack_.append(up_connect)
                        blob.open_stacks -= 1  # overlap with merged blob.

        blob.box[1] = min(blob.box[1], x0)  # extend box x0
        blob.box[2] = max(blob.box[2], xn)  # extend box xn
        P.hid = new_stack.id
        next_stack_.append(new_stack)

    return next_stack_  # input for the next line of scan_P_

def form_blob(stack, frame):  # increment blob with terminated stack, check for blob termination and merger into frame

    I, Dy, Dx, G, M, Dyy, Dyx, Dxy, Dxx, Ga, Ma, S, Ly, y0, Py_, blob, down_connect_cnt, sign, fPP = stack.unpack()
    # terminated stack is merged into continued or initialized blob (all connected stacks):
    accum_Dert(blob.Dert, I=I, Dy=Dy, Dx=Dx, G=G, M=M, Dyy=Dyy, Dyx=Dyx, Dxy=Dxy, Dxx=Dxx, Ga=Ga, Ma=Ma, S=S, Ly=Ly)

    blob.open_stacks += down_connect_cnt - 1  # incomplete stack cnt + terminated stack down_connect_cnt - 1: stack itself
    # open stacks contain Ps of a current row and may be extended with new x-overlapping Ps in next run of scan_P_
    if blob.open_stacks == 0:  # number of incomplete stacks == 0: blob is terminated and packed in frame:
        last_stack = stack
        [y0, x0, xn], stack_, s, open_stacks = blob.unpack()[1:5]
        yn = last_stack.y0 + last_stack.Ly

        mask = np.ones((yn - y0, xn - x0), dtype=bool)  # mask box, then unmask Ps:
        for stack in stack_:
            form_PPy_(stack)  # evaluate to convert stack.Py_ to stack.PPy_

            cluster_P_(stack, ave)  # root function of comp_P: edge tracing and vectorization function

            if stack.fPP:  # Py_ is PPy_
                for y, (PP_sign, PP_G, P_) in enumerate(stack.Py_, start=stack.y0 - y0):
                    for P in P_:
                        x_start = P.x0 - x0
                        x_stop = x_start + P.L
                        mask[y, x_start:x_stop] = False
            else:
                for y, P in enumerate(stack.Py_, start=stack.y0 - y0):
                    x_start = P.x0 - x0
                    x_stop = x_start + P.L
                    mask[y, x_start:x_stop] = False

        dert__ = tuple(derts[y0:yn, x0:xn] for derts in frame['dert__'])  # slice each dert array of the whole frame

        fopen = 0  # flag: blob on frame boundary
        if x0 == 0 or xn == frame['dert__'][0].shape[1] or y0 == 0 or yn == frame['dert__'][0].shape[0]:
            fopen = 1

        blob.root_dert__ = frame['dert__']
        blob.box = (y0, yn, x0, xn)
        blob.dert__ = dert__
        blob.mask = mask
        blob.adj_blobs = [[], 0, 0, 0, 0]
        blob.fopen = fopen

        frame.update(I=frame['I'] + blob.Dert['I'],
                     Dy=frame['Dy'] + blob.Dert['Dy'],
                     Dx=frame['Dx'] + blob.Dert['Dx'],
                     G=frame['G'] + blob.Dert['G'],
                     M=frame['M'] + blob.Dert['M'],
                     Dyy=frame['Dyy'] + blob.Dert['Dyy'],
                     Dyx=frame['Dyx'] + blob.Dert['Dyx'],
                     Dxy=frame['Dxy'] + blob.Dert['Dxy'],
                     Dxx=frame['Dxx'] + blob.Dert['Dxx'],
                     Ga=frame['Ga'] + blob.Dert['Ga'],
                     Ma=frame['Ma'] + blob.Dert['Ma'])

        frame['blob__'].append(blob)

def form_PPy_(stack):
    ave_PP = 100  # min summed value of gdert params

    if stack.G > aveG:
        stack_Dg = stack_Mg = 0
        PPy_ = []  # may replace stack.Py_
        P = stack.Py_[0]
        Py_ = [P]; PP_G = P.G   # initialize PP params
        _PP_sign = PP_G > aveG and P.L > 1

        for P in stack.Py_[1:]:
            PP_sign = P.G > aveG and P.L > 1  # PP sign
            if _PP_sign == PP_sign:  # accum PP:
                Py_.append(P)
                PP_G += P.G
            else:  # sign change, terminate PP:
                if PP_G > aveG:
                    Py_, Dg, Mg = comp_g(Py_)  # adds gdert_, Dg, Mg per P in Py_
                    stack_Dg += abs(Dg)  # in all high-G Ps, regardless of direction
                    stack_Mg += Mg
                PPy_.append((_PP_sign, PP_G, Py_))  # pack PP
                Py_ = [P]; PP_G = P.G  # initialize PP params
            _PP_sign = PP_sign

        PP = _PP_sign, PP_G, Py_  # terminate last PP
        if PP_G > aveG:
            Py_, Dg, Mg = comp_g(Py_)  # adds gdert_, Dg, Mg per P
            stack_Dg += abs(Dg)  # stack params?
            stack_Mg += Mg
        if stack_Dg + stack_Mg < ave_PP:  # separate comp_P values, revert to Py_ if below-cost
            PPy_.append(PP)
            stack.Py_ = PPy_
            stack.fPP = 1  # flag PPy_ vs. Py_ in stack

def comp_g(Py_):  # cross-comp of gs in P.dert_, in PP.Py_
    gP_ = []
    gP_Dg = gP_Mg = 0

    for P in Py_:
        Dg=Mg=0
        gdert_ = []
        _g = P.dert_[0][3]  # first g
        for dert in P.dert_[1:]:
            g = dert[3]
            dg = g - _g
            mg = min(g, _g)
            gdert_.append((dg, mg))  # no g: already in dert_
            Dg+=dg  # P-wide cross-sign, P.L is too short to form sub_Ps
            Mg+=mg
            _g = g
        P.gdert_ = gdert_
        P.Dg = Dg
        P.Mg = Mg
        gP_.append(P)
        gP_Dg += Dg
        gP_Mg += Mg  # positive, for stack evaluation to set fPP

    return gP_, gP_Dg, gP_Mg

def form_gP_(gdert_):
    # probably not needed.

    gP_ = []  # initialization
    _g, _Dg, _Mg = gdert_[0]  # first gdert
    _s = _Mg > 0  # initial sign, should we use ave here?

    for (g, Dg, Mg) in gdert_[1:]:
        s = Mg > 0  # current sign
        if _s != s:  # sign change
            gP_.append([_s, _Dg, _Mg])  # pack gP
            # update params
            _s = s
            _Dg = Dg
            _Mg = Mg
        else:  # accumulate params
            _Dg += Dg  # should we abs the value here?
            _Mg += Mg

    gP_.append([_s, _Dg, _Mg])  # pack last gP
    return gP_

def assign_adjacents(blob_binder):  # adjacents are connected opposite-sign blobs
    '''
    Assign adjacent blobs bilaterally according to adjacent pairs' ids in blob_binder.
    '''
    for blob_id1, blob_id2 in blob_binder.adj_pairs:
        assert blob_id1 < blob_id2
        blob1 = blob_binder.cluster_cls.get_instance(blob_id1)
        blob2 = blob_binder.cluster_cls.get_instance(blob_id2)

        y01, yn1, x01, xn1 = blob1.box
        y02, yn2, x02, xn2 = blob2.box

        if y01 < y02 and x01 < x02 and yn1 > yn2 and xn1 > xn2:
            pose1, pose2 = 0, 1  # 0: internal, 1: external
        elif y01 > y02 and x01 > x02 and yn1 < yn2 and xn1 < xn2:
            pose1, pose2 = 1, 0  # 1: external, 0: internal
        else:
            pose1 = pose2 = 2  # open, no need for fopen?

        # bilateral assignments
        blob1.adj_blobs[0].append((blob2, pose2))
        blob2.adj_blobs[0].append((blob1, pose1))
        blob1.adj_blobs[1] += blob2.Dert['S']
        blob2.adj_blobs[1] += blob1.Dert['S']
        blob1.adj_blobs[2] += blob2.Dert['G']
        blob2.adj_blobs[2] += blob1.Dert['G']
        blob1.adj_blobs[3] += blob2.Dert['M']
        blob2.adj_blobs[3] += blob1.Dert['M']
        blob1.adj_blobs[4] += blob2.Dert['Ma']
        blob2.adj_blobs[4] += blob1.Dert['Ma']

# -----------------------------------------------------------------------------
# Utilities

def accum_Dert(Dert: dict, **params) -> None:
    Dert.update({param: Dert[param] + value for param, value in params.items()})

max_issues_repo_name: Risingabhi/CogAlg

max_forks_repo_name: Risingabhi/CogAlg


text:
import random
import time
import torch
import torch.nn.utils
import datetime
import os
import numpy as np
import collections
from game import Game2048
from agent_net import AgentNet
from agent_player import AgentPlayer
from experience_buffer import ExperienceBuffer
from experience_unroller import ExperienceUnroller
from tensorboardX import SummaryWriter

exp_capacity = 10000
initial_exp_gathering = 5000
target_sync_interval = 3000
evaluation_interval = 2000

epsilon_initial = 0.01
epsilon_final = 0.01
epsilon_decay_time = 200000
epsilon_decay_amount = epsilon_initial - epsilon_final

batch_size = 16
GAMMA = 0.99
learning_rate = 0.0001
GRAD_CLIP = 5
EXP_UNROLL_STEPS = 0

PRIO_ALPHA = 0.6
PRIO_BETA_INITIAL = 0.4
PRIO_BETA_RISE_TIME = 100000

EVAL_GAMES = 10

def silentPlayout(game):
    turn = 0

    while True:
        turn += 1
        dir = random.randrange(4)
        _, ended, _ = game.swipe(dir)
        if ended:
            if game.score == game.target_score:
                return turn, game.score, True

            return turn, game.score, False

def playSomeRandomGames(game):
    for _ in range(150):
        game.reset()
        turns, score, won = silentPlayout(game)

        if won:
            print(f'Victory! Target score of {2**score} reached after {turns} turns.')
        else:
            print(f'Game Over! No space for new number after {turns} turns (final score: {2**score}).')

def playAndPrintEpisode(player, eps = 0.1):
    s, a, r, e, _ = player.playEpisode(eps)
    game = player.game

    for i in range(0,len(s)):
        print(f'#{i} State:')
        print(f'{s[i]}')
        print(f'Action: {a[i]} ({game.MOVE_STRING[a[i]]}), Reward: {r[i]}, Ended: {e[i]}')

    print(f'Final score: {game.totalScore}')

def playSomeGames(game, net, count):
    agent = AgentPlayer(net, game)

    avgScore = 0
    maxScore = 0
    minScore = game.TARGET_SCORE + 1
    avgTotal = 0
    minTotal = 2**30
    maxTotal = 0
    for i in range(count):
        agent.playEpisode(0)
        avgScore += game.score
        maxScore = max(maxScore, game.score)
        minScore = min(minScore, game.score)
        avgTotal += game.totalScore
        maxTotal = max(maxTotal, game.totalScore)
        minTotal = min(minTotal, game.totalScore)

    return minScore, avgScore / count, maxScore, minTotal, avgTotal / count, maxTotal

def playAndLearn(agentNet, targetNet, player):
    expBuffer = ExperienceBuffer(exp_capacity, PRIO_ALPHA)
    expUnroller = ExperienceUnroller(EXP_UNROLL_STEPS, GAMMA)
    qGamma = GAMMA ** (EXP_UNROLL_STEPS + 1)
    device = AgentNet.device

    writer = SummaryWriter(logdir=os.path.join('tensorboard', datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')))

    reportInterval = 2000

    sampleCountTotal = 0
    sampleLastReport = initial_exp_gathering
    episodeCountTotal = 0
    timeLastReport = time.perf_counter()
    epsilon = epsilon_initial
    beta = PRIO_BETA_INITIAL

    lossFunc = torch.nn.MSELoss()
    optim = torch.optim.Adam(agentNet.parameters(), lr=learning_rate)

    lossAcc = 0
    lossCnt = 0
    epLen = 0
    episodeLengths = collections.deque(maxlen=20)
    evalScMin = evalScAvg = evalScMax = evalToMin = evalToAvg = evalToMax = 0

    try:
        while True:
            if sampleCountTotal > initial_exp_gathering:
                part = min(1.0, (sampleCountTotal - initial_exp_gathering) / epsilon_decay_time)
                epsilon = epsilon_initial - epsilon_decay_amount * part
                beta = min(1.0, PRIO_BETA_INITIAL + (1 - PRIO_BETA_INITIAL) * (sampleCountTotal - initial_exp_gathering) / PRIO_BETA_RISE_TIME)

            s, a, r, term, s1 = player.makeTurn(epsilon)
            sampleCountTotal += 1
            epLen += 1

            if term:
                episodeCountTotal += 1
                episodeLengths.append(epLen)
                epLen = 0
                game.reset()

            s, a, r, term, s1 = expUnroller.add(s, a, r, term, s1)
            if s is not None:
                expBuffer.add(s, a, r, term, s1)

            if expBuffer.count() < initial_exp_gathering:
                timeLastReport = time.perf_counter()
                continue

            if sampleCountTotal - sampleLastReport > reportInterval:
                timeCur = time.perf_counter()
                speed = (sampleCountTotal - sampleLastReport) / (timeCur - timeLastReport)
                timeLastReport = timeCur
                sampleLastReport += reportInterval
                epLengthAvg = np.mean(episodeLengths)

                writer.add_scalar('Training/Eps', epsilon, sampleCountTotal)
                writer.add_scalar('Training/Speed', speed, sampleCountTotal)
                writer.add_scalar('Training/Episode Length', epLengthAvg, sampleCountTotal)
                if lossCnt > 0:
                    writer.add_scalar('Training/Loss', lossAcc/lossCnt, sampleCountTotal)

                print(f'Played {sampleLastReport} steps ({episodeCountTotal} episodes) ({speed:8.2f} samples/s): Average steps {epLengthAvg:7.2f}, Evaluation score {evalScMin:2}, {evalScAvg:4.1f}, {evalScMax:2}, total {evalToMin:5}, {evalToAvg:7.1f}, {evalToMax:5}')
                lossAcc = lossCnt = 0

            if sampleCountTotal % evaluation_interval == 0:
                evalScMin, evalScAvg, evalScMax, evalToMin, evalToAvg, evalToMax = playSomeGames(Game2048(), agentNet, EVAL_GAMES)

                writer.add_scalar('Evaluation/Eval Score Avg', evalScAvg, sampleCountTotal)
                writer.add_scalar('Evaluation/Eval Score Min', evalScMin, sampleCountTotal)
                writer.add_scalar('Evaluation/Eval Score Max', evalScMax, sampleCountTotal)
                writer.add_scalar('Evaluation/Eval Total Score Avg', evalToAvg, sampleCountTotal)
                writer.add_scalar('Evaluation/Eval Total Score Min', evalToMin, sampleCountTotal)
                writer.add_scalar('Evaluation/Eval Total Score Max', evalToMax, sampleCountTotal)

            if sampleCountTotal % target_sync_interval == 0:
                targetNet.load_state_dict(agentNet.state_dict())

            if sampleCountTotal % 2 == 0:
                continue

            optim.zero_grad()

            states, actions, rewards, terms, newStates, idxs, weights = expBuffer.sample(batch_size, beta)

            states_t = agentNet.prepareInputs(states)
            newStates_t = agentNet.prepareInputs(newStates)
            actions_t = torch.from_numpy(actions).to(device)
            rewards_t = torch.from_numpy(rewards).to(device)
            terms_t = torch.from_numpy(terms).to(device)
            weights_t = torch.from_numpy(weights).to(device)

            stateActionQs = agentNet(states_t)
            stateActionQs = torch.gather(stateActionQs, 1, actions_t.unsqueeze(-1)).squeeze(-1)

            nextActions = agentNet(newStates_t).max(1)[1]
            nextStateQs = targetNet(newStates_t).gather(1, nextActions.unsqueeze(-1)).squeeze(-1)
            nextStateQs[terms_t] = 0.0
            nextStateQs = nextStateQs.detach()

            totalRewards_t = nextStateQs * qGamma + rewards_t
            losses_t = weights_t * (stateActionQs - totalRewards_t) ** 2
            loss = losses_t.mean()

            loss.backward()

            torch.nn.utils.clip_grad_norm_(agentNet.parameters(), GRAD_CLIP)
            optim.step()
            expBuffer.updatePriorities(idxs, losses_t.data.cpu().numpy() + 1e-5)

            lossAcc += loss.item()
            lossCnt += 1

    except KeyboardInterrupt:
        print(f'Playing stopped after {sampleCountTotal} steps ({episodeCountTotal} episodes).')
        torch.save(agentNet.state_dict(), os.path.join('models', datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')+f' SC {sampleCountTotal}'))

    writer.close()

def playThroughStdin(game: Game2048):
    # 0 - left, 1 - up, 2 - right, 3 - down
    directions = { 'W': 1, 'S': 3, 'A': 0, 'D': 2}

    game.reset()
    moveCount = 0

    try:
        while True:
            print(game.boardAsString())
            inStr = input('Enter direction of swipe: ')
            dir = directions[inStr.upper()]
            score, ended, valid = game.swipe(dir)

            if not valid:
                print('Invalid move, try again...')
                continue

            moveCount += 1

            if ended:
                print(f'Game Over. Score: {2**game.score} after {moveCount} turns.')
                return
            else:
                print(f'Swipe #{moveCount} gave {score} points.')

    except KeyboardInterrupt:
        print('Game aborted.')

def loadModel(net: AgentNet, filename: str):
    net.load_state_dict(torch.load(('models/' + filename)))
    print(f'Loaded parameters from file {filename}')

if __name__ == '__main__':
    print('Welcome!')

    dirs = ['left','up','right','down']

    game = Game2048()
    # playSomeRandomGames(game)
    # playThroughStdin(game)
    # exit()

    agentNet = AgentNet()
    loadModel(agentNet, '2021-04-18_17-43-54 SC 2674000')

    targetNet = AgentNet()
    targetNet.load_state_dict(agentNet.state_dict())

    player = AgentPlayer(agentNet, game)

    #playAndPrintEpisode(player, 0)

    playAndLearn(agentNet, targetNet, player)

    print('Done!')

ext: py

reason: import numpy


text:
# Copyright 2021, Google LLC.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Evaluation of miracle, sqkr, and privunit when coding cost is varied."""

import json
import math
import time

import matplotlib
import matplotlib.pyplot as plt
import numpy as np
from scipy import stats

from rcc_dp import modify_pi
from rcc_dp.mean_estimation import get_parameters
from rcc_dp.mean_estimation import miracle
from rcc_dp.mean_estimation import privunit
from rcc_dp.mean_estimation import sqkr

matplotlib.rcParams["ps.useafm"] = True
matplotlib.rcParams["pdf.use14corefonts"] = True
matplotlib.rcParams["text.usetex"] = True

def evaluate(work_path, config, file_open=open):
  """Evaluates miracle, sqkr, privunit methods."""
  with file_open(work_path + "/config.json", "w") as f:
    json.dump(config.to_dict(), f)

  start_time = time.time()

  budget = config.budget
  alpha = config.alpha
  # Get default values.
  d = config.d
  n = config.n
  epsilon_target = config.epsilon_target

  vary_space = config.cc_space

  print("coding space = " + str(vary_space))

  modified_miracle_mse = np.zeros((config.num_itr, len(vary_space)))
  sqkr_mse = np.zeros((config.num_itr, 1))
  privunit_mse = np.zeros((config.num_itr, 1))

  sqkr_coding_cost = epsilon_target

  for itr in range(config.num_itr):
    print("itr = %d" % itr)
    print("epsilon target = " + str(epsilon_target))
    print("n = " + str(n))
    print("d = %d" % d)
    if config.run_modified_miracle:
      eta = epsilon_target / 2.0
      print("eta = " + str(eta))
      print("alpha = " + str(alpha))

    if config.data == "unbiased_data":
      x = np.random.normal(0, 1, (d, n))
      x /= np.linalg.norm(x, axis=0)
    elif config.data == "biased_data":
      x = np.zeros((d, n))
      x[:, 0::2] = np.random.normal(10, 1, (d, (n + 1) // 2))
      x[:, 1::2] = np.random.normal(1, 1, (d, n // 2))
      x /= np.linalg.norm(x, axis=0)
    elif config.data == "same_data":
      x = np.random.normal(0, 1, (d, 1))
      x /= np.linalg.norm(x, axis=0)
      x = np.repeat(x, n, axis=1)
    else:
      raise ValueError(
          "data should be either be biased_data, unbiased_data, same_data.")

    if config.run_privunit:
      x_privunit, _ = privunit.apply_privunit(x, epsilon_target, budget)
      x_privunit = np.mean(np.array(x_privunit), axis=1, keepdims=True)
      privunit_mse[itr, 0] = np.linalg.norm(
          np.mean(x, axis=1, keepdims=True) - x_privunit)**2

    if config.run_sqkr:
      # Generate a random tight frame satisfying UP -- for sqkr.
      frame = 2**int(math.ceil(math.log(d, 2)) + 1)
      u = stats.ortho_group.rvs(dim=frame).T[:, 0:d]

      k_equiv = min(epsilon_target, sqkr_coding_cost)
      [_, _, q_perturb] = sqkr.kashin_encode(u, x, k_equiv, epsilon_target)
      x_kashin = sqkr.kashin_decode(u, k_equiv, epsilon_target, q_perturb)
      sqkr_mse[itr, 0] = np.linalg.norm(
          np.mean(x, axis=1, keepdims=True) - x_kashin)**2

    for step, vary_parameter in enumerate(vary_space):
      coding_cost = vary_parameter
      print("coding cost = %d" % coding_cost)

      if config.run_modified_miracle:
        x_modified_miracle = np.zeros((d, n))
        c1, c2, m, gamma = (
            get_parameters.get_parameters_unbiased_modified_miracle(
                alpha * epsilon_target, d, 2**coding_cost, budget))
        for i in range(n):
          _, _, pi = miracle.encoder(i + itr * n, x[:, i], 2**coding_cost, c1,
                                     c2, gamma)
          pi_all = modify_pi.modify_pi(pi, eta, epsilon_target,
                                       c1 / (np.exp(epsilon_target / 2)))
          k = np.random.choice(2**coding_cost, 1, p=pi_all[-1])[0]
          z_k = miracle.decoder(i + itr * n, k, d, 2**coding_cost)
          x_modified_miracle[:, i] = z_k / m
        x_modified_miracle = np.mean(x_modified_miracle, axis=1, keepdims=True)
        modified_miracle_mse[itr, step] = np.linalg.norm(
            np.mean(x, axis=1, keepdims=True) - x_modified_miracle)**2

    print(time.time() - start_time)

  print("--------------")
  if config.run_modified_miracle:
    print("approx miracle mse:")
    print(np.mean(modified_miracle_mse, axis=0))
  if config.run_privunit:
    print("privunit mse:")
    print(np.mean(privunit_mse, axis=0))
  if config.run_sqkr:
    print("sqkr mse:")
    print(np.mean(sqkr_mse, axis=0))

  plt.figure(figsize=((8, 5)), dpi=80)
  plt.axes((.15, .2, .83, .75))
  if config.run_modified_miracle:
    plt.errorbar(
        vary_space,
        np.mean(modified_miracle_mse, axis=0),
        yerr=np.std(modified_miracle_mse, axis=0) / np.sqrt(config.num_itr),
        linewidth=3.0,
        label="MMRC")
  if config.run_privunit:
    plt.errorbar(
        vary_space, [np.mean(privunit_mse, axis=0)[0]] * len(vary_space),
        yerr=[np.std(privunit_mse, axis=0)[0] / np.sqrt(config.num_itr)] *
        len(vary_space),
        ls="--",
        linewidth=3.0,
        label="PrivUnit$_{2}$")
  if config.run_sqkr:
    plt.errorbar(
        vary_space, [np.mean(sqkr_mse, axis=0)[0]] * len(vary_space),
        yerr=[np.std(sqkr_mse, axis=0)[0] / np.sqrt(config.num_itr)] *
        len(vary_space),
        ls="--",
        linewidth=3.0,
        label="SQKR")
  plt.xticks(fontsize=28)
  plt.yticks(fontsize=28)
  plt.ylabel(r"$\ell_{2}$ error", fontsize=28)
  plt.xlabel(r"$\#$ bits", fontsize=28)
  plt.yticks([0.04, 0.05, 0.06, 0.07, 0.08])
  plt.legend(fontsize=24, loc="center right")

  with file_open(work_path + "/rcc_dp_mse_vs_coding_cost.png", "wb") as f:
    plt.savefig(f, format="png")

  with file_open(work_path + "/time.txt", "w") as f:
    np.savetxt(f, np.array(time.time() - start_time).reshape(-1, 1))

  if config.run_modified_miracle:
    with file_open(work_path + "/modified_miracle_mse.csv", "w") as f:
      np.savetxt(f, modified_miracle_mse, delimiter=",")

  if config.run_privunit:
    with file_open(work_path + "/privunit_mse.csv", "w") as f:
      np.savetxt(f, privunit_mse, delimiter=",")

  if config.run_sqkr:
    with file_open(work_path + "/sqkr_mse.csv", "w") as f:
      np.savetxt(f, sqkr_mse, delimiter=",")

max_stars_repo_name: lamylio/federated

max_issues_repo_name: notminusone/federated

max_forks_repo_name: notminusone/federated

reason: import numpy,from scipy


text:
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Generates "babbling piano music" from a model trained with
last second conditioning. Specifically, this uses the underlying
STFTMagGAN.
"""

import os
import tensorflow as tf
import soundfile as sf
import numpy as np
from audio_synthesis.datasets import waveform_dataset
from audio_synthesis.utils import spectral
from audio_synthesis.structures import ls_conditional_spec_gan

FFT_FRAME_LENGTH = 512
FFT_FRAME_STEP = 128
N_GENERATIONS = 60
SAMPLE_RATE = 16000
CONDITIONING_START_INDEX = 2**13
GENERATION_LENGTH = 2**14
MAESTRO_PATH = 'data/MAESTRO_ls_hlf_cond_6h.npz'

def main():
    os.environ["CUDA_VISIBLE_DEVICES"] = ''
    print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU')))

    raw_maestro_conditioning = waveform_dataset.get_stft_dataset(
        MAESTRO_PATH, frame_length=FFT_FRAME_LENGTH, frame_step=FFT_FRAME_STEP
    )

    generator = ls_conditional_spec_gan.Generator(channels=2, in_shape=(4, 8, 512))

    checkpoint_path = '_results/conditioning/LSC_STFTMagGAN_HR_8192/training_checkpoints/ckpt-11'

    checkpoint = tf.train.Checkpoint(generator=generator)
    checkpoint.restore(checkpoint_path).expect_partial()

    get_waveform = lambda stft:\
        spectral.stft_2_waveform(
            stft, FFT_FRAME_LENGTH, FFT_FRAME_STEP
        )[0]
    get_stft = lambda waveform:\
        spectral.waveform_2_stft(
            waveform, FFT_FRAME_LENGTH, FFT_FRAME_STEP
        )[0]

    seed = np.expand_dims(raw_maestro_conditioning[5], 0)

    sequence = []
    for _ in range(N_GENERATIONS):
        z_in = tf.random.uniform((1, 64), -1, 1)
        gen = generator(seed, z_in)
        wav = get_waveform(gen)[0:GENERATION_LENGTH]

        sequence.append(wav)
        wav_cond = wav[CONDITIONING_START_INDEX:]
        seed = np.expand_dims(get_stft(wav_cond), 0)

    audio = np.array(sequence)
    audio = np.squeeze(audio)
    audio = np.reshape(audio, (-1))
    sf.write('stftmaggan_babble.wav', audio, 16000)

if __name__ == '__main__':
    main()

max_issues_repo_name: googleinterns/audio-synthesis

max_forks_repo_name: googleinterns/audio-synthesis


text:
"""Tests for the variant of MT2 by Colin Lally."""

import numpy
import pytest

from mt2 import mt2, mt2_lally

def test_simple_example():
    computed_val = mt2_lally(100, 410, 20, 150, -210, -300, -200, 280, 100, 100)
    assert computed_val == pytest.approx(412.628)

def test_near_massless():
    # This test is based on Fig 5 of https://arxiv.org/pdf/1411.4312.pdf
    m_vis_a = 0
    px_a = -42.017340486
    py_a = -146.365340528

    m_vis_b = 0.087252259
    px_b = -9.625614206
    py_b = 145.757295514

    px_miss = -16.692279406
    py_miss = -14.730240471

    chi_a = 0
    chi_b = 0

    computed_val = mt2_lally(
        m_vis_a, px_a, py_a, m_vis_b, px_b, py_b, px_miss, py_miss, chi_a, chi_b
    )
    assert computed_val == pytest.approx(0.09719971)

@pytest.mark.skip(reason="Currently failing due to inconsistencies")
def test_fuzz():
    batch_size = 100
    num_tests = 1000

    numpy.random.seed(42)

    def _random_batch(min_, max_):
        return numpy.random.uniform(min_, max_, (batch_size,))

    for _ in range(num_tests):
        m_vis_1 = _random_batch(0, 100)
        px_vis_1 = _random_batch(-100, 100)
        py_vis_1 = _random_batch(-100, 100)
        m_vis_2 = _random_batch(0, 100)
        px_vis_2 = _random_batch(-100, 100)
        py_vis_2 = _random_batch(-100, 100)
        px_miss = _random_batch(-100, 100)
        py_miss = _random_batch(-100, 100)
        m_invis_1 = _random_batch(0, 100)
        m_invis_2 = _random_batch(0, 100)

        args = (
            m_vis_1,
            px_vis_1,
            py_vis_1,
            m_vis_2,
            px_vis_2,
            py_vis_2,
            px_miss,
            py_miss,
            m_invis_1,
            m_invis_2,
        )

        result_lester = mt2(*args)
        result_lally = mt2_lally(*args)

        numpy.testing.assert_allclose(result_lester, result_lally, rtol=1e-12)


text:
from distutils.core import setup, Extension
import numpy.distutils.misc_util

c_ext = Extension("_ifun", ["_ifun.c", "ifun.c"])

setup(
    ext_modules=[c_ext],
    include_dirs=numpy.distutils.misc_util.get_numpy_include_dirs(),
)


text:
import tensorflow as tf

import collections
from tensorflow.python.framework import dtypes
from tensorflow.python.framework import random_seed
import numpy
import json
import pickle
import numpy as np

_Datasets = collections.namedtuple('_Datasets', ['train', 'validation', 'test'])

class _DataSet(object):

  def __init__(self,
               images,
               labels,
               dtype,
               reshape,
               num_features,
               seed):
    """Construct a _DataSet.

    Args:
      images: The images
      labels: The labels
      dtype: Output image dtype. One of [uint8, float32]. `uint8` output has
        range [0,255]. float32 output has range [0,1].
      reshape: Bool. If True returned images are returned flattened to vectors.
      num_subsets: Number of training subsets for stability
      subset_ratio: fraction of original training set that must be in each subset.
    """
     # Convert shape from [num examples, rows, columns, depth]
     # to [num examples, rows*columns] (assuming depth == 1)

    seed1, seed2 = random_seed.get_seed(seed)
    numpy.random.seed(seed)
    if reshape:
      labels = labels.reshape(labels.shape[0])
      images = images.reshape(images.shape[0], num_features)

    #if dtype == dtypes.float32:
    # Convert from [0, 255] ->  [0.0, 1.0].
    images = images.astype(numpy.float32)
    images = numpy.multiply(images, 1.0 / 255.0) #this has been compensated previously by *multiplier!

    self._num_examples = images.shape[0]
    self._images = images
    self._labels = labels
    self._epochs_completed = 0
    self._index_in_epoch = 0

  @property
  def images(self):
    return self._images

  @property
  def labels(self):
    return self._labels

  @property
  def num_examples(self):
    return self._num_examples

  @property
  def epochs_completed(self):
    return self._epochs_completed

  def next_batch(self, batch_size, fake_data=False, shuffle=True):
    """Return the next `batch_size` examples from this data set."""
    start = self._index_in_epoch

    # Shuffle for the first epoch
    if self._epochs_completed == 0 and start == 0 and shuffle:
      perm0 = numpy.arange(self._num_examples)
      numpy.random.shuffle(perm0)
      self._images = self._images[perm0]
      self._labels = self._labels[perm0]

    # Go to the next epoch
    if start + batch_size > self._num_examples:

      # Finished epoch
      self._epochs_completed += 1

      # Get the rest examples in this epoch
      rest_num_examples = self._num_examples - start
      images_rest_part = self._images[start:self._num_examples]
      labels_rest_part = self._labels[start:self._num_examples]

      # Shuffle the data
      if shuffle:
        perm = numpy.arange(self._num_examples)
        numpy.random.shuffle(perm)
        self._images = self._images[perm]
        self._labels = self._labels[perm]

      # Start next epoch
      start = 0
      self._index_in_epoch = batch_size - rest_num_examples
      end = self._index_in_epoch

      images_new_part = self._images[start:end]
      labels_new_part = self._labels[start:end]
      return numpy.concatenate((images_rest_part, images_new_part),
                               axis=0), numpy.concatenate(
                                   (labels_rest_part, labels_new_part), axis=0)

    else:
      self._index_in_epoch += batch_size
      end = self._index_in_epoch
      return self._images[start:end], self._labels[start:end]

def load_data_set(results_dir, data_set, seed=None, reshape=True, standarized=False, multiplier=1, re_size=1, dtype=dtypes.float32):

  with open(results_dir + 'configs_datasets/' + str(data_set) + '.json') as config_file:
    config = json.load(config_file)

  color = False
  if config["dataset_name"] == "cifar" or config["dataset_name"] == "mnist" \
          or config["dataset_name"] == "fashion_mnist":
    if config["dataset_name"] == "cifar":
      (X_train, y_train), (X_test, y_test) = tf.keras.datasets.cifar10.load_data()
      '''
      X_train = X_train[:,2:-2,2:-2,:]
      X_test = X_test[:,2:-2,2:-2,:]
      print(X_train.shape)
      '''
      X_train = X_train[:,::re_size,::re_size,:]
      X_test = X_test[:,::re_size,::re_size,:]

      num_features = int(int(X_train.shape[1])*int(X_train.shape[2])*X_train.shape[3])
      color = True
    if config["dataset_name"] == "mnist":
      (X_train, y_train), (X_test, y_test) = tf.keras.datasets.mnist.load_data()
      X_train = X_train[:,::re_size,::re_size]
      X_test = X_test[:,::re_size,::re_size]
      num_features = int(int(X_train.shape[1])*int(X_train.shape[2]))
    if config["dataset_name"] == "fashion_mnist":
      (X_train, y_train), (X_test, y_test) = tf.keras.datasets.fashion_mnist.load_data()
      X_train = X_train[:,::re_size,::re_size]
      X_test = X_test[:,::re_size,::re_size]
      num_features = int(int(X_train.shape[1])*int(X_train.shape[2]))

    X_val = X_train[:config["validation_size"]].astype('float')
    y_val = y_train[:config["validation_size"]]
    X_train = X_train[config["validation_size"]:].astype('float')
    y_train = y_train[config["validation_size"]:]
    X_test = X_test.astype('float')

    if standarized:

      def standarize(X, color=False):
        import numpy as np

        if not color:
          m = np.mean(X, axis=(1, 2))
          X = X - m[:, np.newaxis, np.newaxis]
          d = np.std(X, axis=(1, 2))
          X = X / d[:, np.newaxis, np.newaxis]

        else:
          for idx in range(3):
            m = np.mean(X[:,:,:,idx], axis=(1, 2))
            X[:,:,:,idx] = X[:,:,:,idx] - m[:, np.newaxis, np.newaxis]
            d = np.std(X[:,:,:,idx], axis=(1, 2))
            X[:,:,:,idx] = X[:,:,:,idx] / d[:, np.newaxis, np.newaxis]

        X[np.isnan(X)] = 0
        return X

      X_train = multiplier*standarize(X_train, color)
      X_val = multiplier*standarize(X_val, color)
      X_test = multiplier*standarize(X_test, color)

  elif config["dataset_name"] == "Gauss_MLP-1":

    for set in ["train", "val", "test"]:
      with open(results_dir + 'datasets/' + set + "_" + config["name_file"], 'rb') as dataset_file:
        tmp = pickle.load(dataset_file)

      if set == "train":
        X_train = tmp["data"]
        y_train = tmp["labels"]
      elif set == "val":
        X_val = tmp["data"][:config["validation_size"]]
        y_val = tmp["labels"][:config["validation_size"]]
      else:
        X_test = tmp["data"][:config["testing_size"]]
        y_test = tmp["labels"][:config["testing_size"]]

      del tmp

  elif config["dataset_name"] == "UCI":

    for set in ["train", "test"]:
      import numpy as np
      tmpX = np.genfromtxt(results_dir + 'datasets/UCI/imp_' + config["name_file"] + '_' + set + "X.csv", delimiter=',')
      tmpX = np.nan_to_num(tmpX, nan=0.0)
      tmpY = np.genfromtxt(results_dir + 'datasets/UCI/' + config["name_file"] + '_' + set + "Y.csv", delimiter=',')

      if set == "train":
        num_samples = np.shape(tmpY)[0]
        num_train_samples = int(np.round(num_samples*0.75))
        X_train = tmpX[:num_train_samples]
        y_train = tmpY[:num_train_samples] - 1
        X_val = tmpX[num_train_samples:]
        y_val = tmpY[num_train_samples:] - 1
      else:
        X_test = tmpX
        y_test = tmpY - 1

      del tmpX
      del tmpY

    num_features = X_train.shape[1]
    X_train = multiplier*X_train
    X_val = multiplier*X_val
    X_test = multiplier*X_test

  print("There are", X_train.shape[0], "samples in the training set.")
  print("There are", X_val.shape[0], "samples in the validation set.")
  print("There are", num_features, "features.")

  options = dict(dtype=dtype, reshape=reshape, num_features=num_features, seed=seed)

  train = _DataSet(X_train, y_train, **options)
  validation = _DataSet(X_val, y_val, **options)
  test = _DataSet(X_test, y_test, **options)

  return _Datasets(train=train, validation=validation, test=test)


text:
from fdspy.lib.func import generate_devc, generate_xyz
from tkinter import filedialog

if __name__ == "__main__":
    import numpy as np

    # domain_xyz
    # id_prefix
    # quantity

    # args = sys.argv[1:]

    # generate_

    id_prefix = str(input("ID name prefix: "))

    quantity = str(input("Measurement/Quantity: "))

    x1 = float(input("x lower limit: "))
    x2 = float(input("x upper limit: "))
    x3 = int(input("x devc count: "))
    y1 = float(input("y lower limit: "))
    y2 = float(input("y upper limit: "))
    y3 = int(input("y devc count: "))
    z1 = float(input("z lower limit: "))
    z2 = float(input("z upper limit: "))
    z3 = int(input("z devc count: "))

    if x3 > 1:
        xx = np.linspace(x1, x2, x3)
    else:
        xx = [(x1 + x2) / 2]

    if y3 > 1:
        yy = np.linspace(y1, y2, y3)
    else:
        yy = [(y1 + y2) / 2]

    if z3 > 1:
        zz = np.linspace(z1, z2, z3)
    else:
        zz = [(z1 + z2) / 2]

    xyz = generate_xyz(xx, yy, zz)

    str_devc = generate_devc(
        id_prefix=id_prefix, quantity=quantity, xyz=xyz, is_return_list=False
    )

    path_out = filedialog.asksaveasfilename(
        defaultextension="txt", filetypes=[("txt", [".txt"])]
    )

    with open(path_out, "w") as f:
        f.write(str_devc)

max_stars_repo_name: fsepy/fdspy

max_issues_repo_name: fsepy/fdspy

max_forks_repo_name: fsepy/fdspy


text:
import h5py
import numpy as np
import os
from shutil import copyfile

calib_list =  '/Users/zhangyesheng/Documents/GitHub/OANet/data/big_ben_1/test/calibration.txt'
img_list = '/Users/zhangyesheng/Documents/GitHub/OANet/data/big_ben_1/test/images.txt'
img_file = '/Users/zhangyesheng/Documents/GitHub/OANet/Rectify/pics/big_ben_test/'
calib_file = '/Users/zhangyesheng/Documents/GitHub/OANet/data/big_ben_1/test/calibration/'
calib_move_file = '/Users/zhangyesheng/Documents/GitHub/OANet/Rectify/calibration/big_ben_test/'

# f = h5py.File('/Users/zhangyesheng/Documents/GitHub/OANet/Rectify/calib/calibration_000009.h5','r')
# print(np.array(f['K']))

def find_calib():
    """
    Find calib_file name accroding to images' name
    """
    img_id = parse_img_list(img_list)

    img_query = []
    img_name_list = os.listdir(img_file)
    for img_name in img_name_list:
        if img_name[-1] != 'g':
            continue
        img_name = img_name.split('.')[0]
        img_query.append(img_name)
    print(img_query)
    left_id, right_id = img_id.index(img_query[0]),img_id.index(img_query[1])

    # print(left_id,right_id)

    with open(calib_list,'r') as f:
        calib_name_list = f.readlines()

    calib_left_name, calib_right_name = calib_name_list[left_id], calib_name_list[right_id]
    calib_left_name = calib_left_name[:-1]
    calib_right_name = calib_right_name[:-1]
    # print(calib_right_name,calib_left_name)

    calib_file_query = [os.path.join(calib_file,calib_left_name.split('/')[-1]),os.path.join(calib_file,calib_right_name.split('/')[-1])]
    calib_file_move = [os.path.join(calib_move_file,img_query[0]+'.h5'),os.path.join(calib_move_file,img_query[1]+'.h5')]
    copyfile(calib_file_query[0],calib_file_move[0])
    copyfile(calib_file_query[1],calib_file_move[1])

def parse_K(calib_file,img_left_name,img_right_name):
    """
    get K,T from the .h5 file
    """
    calib_list = os.listdir(calib_file)
    K = {img_left_name:np.ones((3,3)),img_right_name:np.ones((3,3))}
    T = {img_left_name:np.ones((3,3)),img_right_name:np.ones((3,3))}
    for calib in calib_list:
        if calib.split('.')[0] == img_left_name.split('.')[0]:
            H = h5py.File(os.path.join(calib_file,calib))
            K[img_left_name] = np.array(H['K'])
            T[img_left_name] = np.array(H['T'])

        if calib.split('.')[0] == img_right_name.split('.')[0]:
            H = h5py.File(os.path.join(calib_file,calib))
            K[img_right_name] = np.array(H['K'])
            T[img_right_name] = np.array(H['T'])
    if len(K) == 0:
        print('There is no .h5 file in %s' %calib_file)
    # print(K)
    # print(T[img_right_name].shape)
    return K, T

def parse_img_list(img_list):
    """
    get the pure images id
    """
    img_id = []
    with open(img_list,'r') as f:
        for temp in f.readlines():
            temp = temp.split('/')[-1]
            temp = temp.split('.')[0]
            img_id.append(temp)
    # print(img_id)
    return img_id

def main():
    img_nameL = "40982537_3102972880.jpg"
    img_nameR = "42002003_1635942632.jpg"

    find_calib()
    # parse_img_list(img_list)
    # parse_K(calib_move_file,img_nameL,img_nameR)

if __name__ == "__main__":
    main()

max_stars_repo_name: Easonyesheng/StereoCameraToolk

max_issues_repo_name: Easonyesheng/StereoCamera

max_forks_repo_name: Easonyesheng/StereoCamera


text:
from triangles import LoadShaders

from OpenGL.GL import *
from OpenGL.GL import shaders
from OpenGL.GLU import *
from OpenGL.GLUT import *

import numpy as np
import math
import ctypes

import sys

vertex_positions = [
	[-1, -1, 0, 1],
	[1, -1, 0, 1],
	[-1, 1, 0, 1],
	[-1, -1, 0, 1],
]

vertex_positions = np.matrix(vertex_positions, dtype="f")

vertex_indices = [
	0, 1, 2,
]

vertex_indices = np.array(vertex_indices, dtype=np.int32)

shaderInfo = [
	(GL_VERTEX_SHADER, './triangles.vert'),
	#(GL_VERTEX_SHADER, './shaders/restart.vert'),
	#(GL_FRAGMENT_SHADER, './shaders/restart.frag'),
	(GL_FRAGMENT_SHADER, './triangles.frag'),
]

vao = None
ebo = None
vbo = None

render_prog = None

def init():
	global render_prog

	global vao
	global ebo
	global vbo

	render_prog = LoadShaders(shaderInfo)
	glUseProgram(render_prog)

	vao = glGenVertexArrays(1)
	glBindVertexArray(vao)

	ebo = glGenBuffers(1)
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo)
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, vertex_indices, GL_STATIC_DRAW)

	vbo = glGenBuffers(1)
	glBindBuffer(GL_ARRAY_BUFFER, vbo)

	glBufferData(GL_ARRAY_BUFFER, vertex_positions, GL_STATIC_DRAW)

	glEnableVertexAttribArray(0)
	glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, None)

def display():
	global vao
	global render_prog

	glClearColor(0, 0, 0, 1)
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

	glUseProgram(render_prog)

	glBindVertexArray(vao)

	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo)
	glEnableVertexAttribArray(0)

	glDrawArrays(GL_TRIANGLES, 0, 3);

	glutSwapBuffers()

if __name__ == '__main__':

	glutInit(sys.argv)
	glutInitDisplayMode(GLUT_RGB)
	glutInitWindowSize(512, 512)
	glutInitContextVersion(3, 3)
	glutInitContextProfile(GLUT_CORE_PROFILE)
	glutCreateWindow(sys.argv[0])

	init()

	glutDisplayFunc(display)
	glutMainLoop()


text:
import inference.inference as inference
import inference.engine as engine
import torch
import numpy as np
import tensorrt as trt
import pycuda.driver as cuda
import pycuda.autoinit
import torch

def do_inference(engine, input):
    with engine.create_execution_context() as context:
        # h_input = cuda.pagelocked_empty(trt.volume(context.get_binding_shape(0)), dtype=trt.nptype(data_type))
        # h_output = cuda.pagelocked_empty(trt.volume(context.get_binding_shape(1)), dtype=trt.nptype(data_type))
        h_input = cuda.pagelocked_empty(trt.volume(context.get_binding_shape(0)), dtype=np.float32)
        h_output = cuda.pagelocked_empty(trt.volume(context.get_binding_shape(1)), dtype=np.float32)
        # Allocate device memory for inputs and outputs.
        d_input = cuda.mem_alloc(h_input.nbytes)
        d_output = cuda.mem_alloc(h_output.nbytes)
        # Create a stream in which to copy inputs/outputs and run inference.
        stream = cuda.Stream()

        input = np.array(input, order='C').ravel()
        np.copyto(h_input, input)

        # Transfer input data to the GPU.
        cuda.memcpy_htod_async(d_input, h_input, stream)

        # Show time spent in each layer
        # context.profiler = trt.Profiler()

        # Run inference.
        context.execute_async_v2(bindings=[int(d_input), int(d_output)], stream_handle=stream.handle)
        # Transfer predictions back from the GPU.
        cuda.memcpy_dtoh_async(h_output, d_output, stream)
        # Synchronize the stream
        stream.synchronize()
        # Return the host output.
        output = h_output.reshape((1, 3, 512, 512))
        return torch.tensor(output)
        # output = torch.tensor(output)
        # output = torch.argmax(output, dim=1)
        # print(output.sum())

reason: import numpy,import pycuda


text:
import torch
import torch.nn as nn
import numpy as np
from torchvision.models import vgg16
from collections import namedtuple

# From https://github.com/pytorch/examples/blob/master/fast_neural_style/neural_style/vgg.py
class Vgg16(torch.nn.Module):
    def __init__(self, device='cpu'):
        super(Vgg16, self).__init__()
        vgg_pretrained_features = vgg16(pretrained=True).features
        self.slice1 = torch.nn.Sequential()
        self.slice2 = torch.nn.Sequential()
        self.slice3 = torch.nn.Sequential()
        self.slice4 = torch.nn.Sequential()
        for x in range(4):
            self.slice1.add_module(str(x), vgg_pretrained_features[x].to(device))
        for x in range(4, 9):
            self.slice2.add_module(str(x), vgg_pretrained_features[x].to(device))
        for x in range(9, 16):
            self.slice3.add_module(str(x), vgg_pretrained_features[x].to(device))
        for x in range(16, 23):
            self.slice4.add_module(str(x), vgg_pretrained_features[x].to(device))

        for param in self.parameters():
            param.requires_grad = False

    def forward(self, X):
        h = self.slice1(X)
        h_relu1_2 = h
        h = self.slice2(h)
        h_relu2_2 = h
        h = self.slice3(h)
        h_relu3_3 = h
        h = self.slice4(h)
        h_relu4_3 = h
        vgg_outputs = namedtuple("VggOutputs", ['relu1_2', 'relu2_2', 'relu3_3', 'relu4_3'])
        out = vgg_outputs(h_relu1_2, h_relu2_2, h_relu3_3, h_relu4_3)
        return out

# Rest of the file based on https://github.com/irsisyphus/reconet

class SelectiveLoadModule(torch.nn.Module):
    """Only load layers in trained models with the same name."""
    def __init__(self):
        super(SelectiveLoadModule, self).__init__()

    def forward(self, x):
        return x

    def load_state_dict(self, state_dict):
        """Override the function to ignore redundant weights."""
        own_state = self.state_dict()
        for name, param in state_dict.items():
            if name in own_state:
                own_state[name].copy_(param)

class ConvLayer(torch.nn.Module):
    """Reflection padded convolution layer."""
    def __init__(self, in_channels, out_channels, kernel_size, stride, bias=True):
        super(ConvLayer, self).__init__()
        reflection_padding = int(np.floor(kernel_size / 2))
        self.reflection_pad = torch.nn.ReflectionPad2d(reflection_padding)
        self.conv2d = torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride=stride, bias=bias)

    def forward(self, x):
        out = self.reflection_pad(x)
        out = self.conv2d(out)
        return out

class ConvTanh(ConvLayer):
    def __init__(self, in_channels, out_channels, kernel_size, stride):
        super(ConvTanh, self).__init__(in_channels, out_channels, kernel_size, stride)
        self.tanh = torch.nn.Tanh()

    def forward(self, x):
        out = super(ConvTanh, self).forward(x)
        return self.tanh(out/255) * 150 + 255/2

class ConvInstRelu(ConvLayer):
    def __init__(self, in_channels, out_channels, kernel_size, stride):
        super(ConvInstRelu, self).__init__(in_channels, out_channels, kernel_size, stride)
        self.instance = torch.nn.InstanceNorm2d(out_channels, affine=True)
        self.relu = torch.nn.ReLU()

    def forward(self, x):
        out = super(ConvInstRelu, self).forward(x)
        out = self.instance(out)
        out = self.relu(out)
        return out

class UpsampleConvLayer(torch.nn.Module):
    """Upsamples the input and then does a convolution.
    This method gives better results compared to ConvTranspose2d.
    ref: http://distill.pub/2016/deconv-checkerboard/
    """
    def __init__(self, in_channels, out_channels, kernel_size, stride, upsample=None):
        super(UpsampleConvLayer, self).__init__()
        self.upsample = upsample
        reflection_padding = int(np.floor(kernel_size / 2))
        self.reflection_pad = torch.nn.ReflectionPad2d(reflection_padding)
        self.conv2d = torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride)

    def forward(self, x):
        x_in = x
        if self.upsample:
            x_in = torch.nn.functional.interpolate(x_in, scale_factor=self.upsample)
        out = self.reflection_pad(x_in)
        out = self.conv2d(out)
        return out

class UpsampleConvInstRelu(UpsampleConvLayer):
    def __init__(self, in_channels, out_channels, kernel_size, stride, upsample=None):
        super(UpsampleConvInstRelu, self).__init__(in_channels, out_channels, kernel_size, stride, upsample)
        self.instance = torch.nn.InstanceNorm2d(out_channels, affine=True)
        self.relu = torch.nn.ReLU()

    def forward(self, x):
        out = super(UpsampleConvInstRelu, self).forward(x)
        out = self.instance(out)
        out = self.relu(out)
        return out

class ResidualBlock(torch.nn.Module):
    def __init__(self, in_channels, out_channels, kernel_size=3, stride=1):
        super(ResidualBlock, self).__init__()
        self.conv1 = ConvLayer(in_channels, out_channels, kernel_size, stride)
        self.in1 = torch.nn.InstanceNorm2d(out_channels, affine=True)
        self.conv2 = ConvLayer(out_channels, out_channels, kernel_size, stride)
        self.in2 = torch.nn.InstanceNorm2d(out_channels, affine=True)
        self.relu = torch.nn.ReLU()

    def forward(self, x):
        residual = x
        out = self.relu(self.in1(self.conv1(x)))
        out = self.in2(self.conv2(out))
        out = out + residual
        return out

class ReCoNet(SelectiveLoadModule):
    def __init__(self):
        super(ReCoNet, self).__init__()

        self.conv1 = ConvInstRelu(3, 32, kernel_size=9, stride=1)
        self.conv2 = ConvInstRelu(32, 64, kernel_size=3, stride=2)
        self.conv3 = ConvInstRelu(64, 128, kernel_size=3, stride=2)

        self.res1 = ResidualBlock(128, 128)
        self.res2 = ResidualBlock(128, 128)
        self.res3 = ResidualBlock(128, 128)
        self.res4 = ResidualBlock(128, 128)
        self.res5 = ResidualBlock(128, 128)

        self.deconv1 = UpsampleConvInstRelu(128, 64, kernel_size=3, stride=1, upsample=2)
        self.deconv2 = UpsampleConvInstRelu(64, 32, kernel_size=3, stride=1, upsample=2)
        self.deconv3 = ConvTanh(32, 3, kernel_size=9, stride=1)

    def forward(self, x):
        x = self.conv1(x)
        x = self.conv2(x)
        x = self.conv3(x)

        x = self.res1(x)
        x = self.res2(x)
        x = self.res3(x)
        x = self.res4(x)
        x = self.res5(x)

        features = x

        x = self.deconv1(x)
        x = self.deconv2(x)
        x = self.deconv3(x)

        return (features, x)

max_stars_repo_name: kirualex/fast-neural-style

max_issues_repo_name: kirualex/fast-neural-style

max_forks_repo_name: kirualex/fast-neural-style


text:
import argparse

import matplotlib.pyplot as plt
import matplotlib
from matplotlib.ticker import ScalarFormatter

import astropy.units as u

from plot_mir_spectra import plot_mir_set, ann_set

if __name__ == "__main__":

    # commandline parser
    parser = argparse.ArgumentParser()
    parser.add_argument("--png", help="save figure as a png file", action="store_true")
    parser.add_argument("--eps", help="save figure as an eps file", action="store_true")
    parser.add_argument("--pdf", help="save figure as a pdf file", action="store_true")
    args = parser.parse_args()

    fontsize = 12

    font = {"size": fontsize}

    matplotlib.rc("font", **font)

    matplotlib.rc("lines", linewidth=1)
    matplotlib.rc("axes", linewidth=2)
    matplotlib.rc("xtick.major", width=2)
    matplotlib.rc("xtick.minor", width=2)
    matplotlib.rc("ytick.major", width=2)
    matplotlib.rc("ytick.minor", width=2)

    # fig, ax = pyplot.subplots(nrows=1, ncols=1, figsize=(10, 13))
    fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(10, 10))

    # kxrange = [3.0, 130.]
    kxrange = [3.0, 50.0]
    # ann_xvals = [41.0, 50.0]
    ann_xvals = [35.0, 42.0]
    spec_name = "IRS"
    norm_wave_range = [6.0, 10.0]
    # ann_wave_range = [15.0, 18.0]
    col_vals = ["b", "g"]  # , "r", "m", "c", "y"]
    # col_vals = ['k', 'k', 'k', 'k', 'k', 'k']

    # starnames = []
    # plot_mir_set(ax, starnames,
    #             ann_xvals=[6.0, 6.0], ann_wave_range=[5.0, 9.0])

    # ann_set(ax, fontsize, [35.0, 38.0], [3.9, 0.9], [45.0, 42.0],
    #        'Main Sequence', '(ordered by spectral type)')

    starnames = [
        "hd096042",
        "hd197702",
        "hd149404",
        "hd169454",
        "hd229059",
        "hd166734",
        "hd206773",
        "cygob2-5",
        "hd034921",
        "hd152408",
    ]
    plot_mir_set(
        ax,
        starnames,
        extra_off_val=0.0,
        ann_xvals=[9.0, 9.0],
        ann_wave_range=[5.0, 6.0] * u.micron,
        ann_offset=0.1,
        col_vals=col_vals,
    )

    ann_set(
        ax,
        fontsize,
        [37.0, 40.0],
        [9.4, 0.8],
        [45.0, 42.0],
        "Unusable for Extinction",
        "(ordered by wind strength)",
    )

    ax.set_yscale("linear")
    ax.set_ylim(0.5, 9.5)
    ax.set_xscale("log")
    ax.set_xlim(kxrange)
    ax.set_xlabel(r"$\lambda$ [$\mu m$]", fontsize=1.3 * fontsize)
    ax.set_ylabel(
        r"$\lambda^4 F(\lambda)/F(8 \mu m)$ + offset", fontsize=1.3 * fontsize
    )

    ax.tick_params("both", length=10, width=2, which="major")
    ax.tick_params("both", length=5, width=1, which="minor")

    ax.spines["right"].set_visible(False)
    ax.spines["top"].set_visible(False)

    ax.xaxis.set_minor_formatter(ScalarFormatter())
    ax.xaxis.set_major_formatter(ScalarFormatter())
    ax.set_xticks([3, 4, 5, 6, 7, 8, 9, 12, 15.0, 20.0, 25.0, 30.0, 40.0], minor=True)

    fig.tight_layout()

    save_file = "spit_ir_mspec_reddened_windy"
    if args.png:
        fig.savefig(save_file + ".png")
    elif args.eps:
        fig.savefig(save_file + ".eps")
    elif args.pdf:
        fig.savefig(save_file + ".pdf")
    else:
        plt.show()

reason: import astropy


text:
import numpy as np

if __name__ == "__main__":
    x = np.load('EEG.npy')
    print(x.shape)
    y = np.load('EEG_1.npy')
    print(y.shape)


text:
import pandas as pd
from Player import Player
import numpy as np
import matplotlib.pyplot as plt
#from teamstats import loadTeamStats
#from teamstats100 import loadTeamStats100
from utility.TeamData import TeamData

year = '2018/' #can change to folder csv is in
#example lakers:1610612747
class Team:
    def __init__(self, teamName):
        self.teamName = teamName
        self.df = pd.read_csv(year+teamName+'.csv')
        self.playerIDS = self.df.PLAYER_ID
        self.roster = {} #player objects
        self.teamTotalShots = 0 #total number of shots taken by the team
        self.shotDistPerPlayer = [] #player shot distribution
        self.clock = []
        self.shotQ = []
        self.fga = 0
        self.data = TeamData()
        self.teamStats = None
        self.loadTeamStats()
        self.teamStats100 = None
        self.loadTeamStats100()

#        self.shotdist = np.random.choice(self.playerIDS, p= self.shotDistPerPlayer)
        self.loadRoster()
        self.teamTotalShotAttempts()
        self.shotDist()

    def loadRoster(self):
        for i in self.playerIDS:
            player = {i: Player(i)}
            self.roster.update(player)

    def teamTotalShotAttempts(self):
        count =0
        for i in self.roster:
            count += self.roster[i].totalShots

        self.teamTotalShots = count

    def shotDist(self):
        li = []
        for i in self.roster:
            li.append(self.roster[i].totalShots/self.teamTotalShots)
        self.shotDistPerPlayer = li

    #take a shot
    #the player ID will be returned
    #you need to then reach into the player shooting chart
    def pickShooter(self):
        return np.random.choice(self.playerIDS, p= self.shotDistPerPlayer)

    def loadTeamStats(self):
        self.teamStats = self.data.loadTeamStats(self.df['TeamID'][0])
        #usage:
        #teamName.teamStats['FGA']

    def loadTeamStats100(self):
        self.teamStats100 = self.data.loadTeamStats100(self.df['TeamID'][0])

    def scat(self):
        tot = []
        for i in range(25):
            tot.append(0)

        for i in range(10000):
            t = self.getShotClock()
            print(t)
            tot[t] +=1

        x = list(range(25))
        plt.scatter(x,tot)
        plt.show()

max_stars_repo_name: tony-mtz/nba-simulation

max_issues_repo_name: tony-mtz/nba-simulation

max_forks_repo_name: tony-mtz/nba-simulation


text:
# -*- coding: utf-8 -*-
# Copyright (c) 2018-2021, earthobservations developers.
# Distributed under the MIT License. See LICENSE for more info.
from typing import Tuple, Union

import numpy as np
from sklearn.neighbors import BallTree

class Coordinates:
    """Class for storing and retrieving coordinates"""

    def __init__(self, latitudes: np.array, longitudes: np.array):
        """
        Args:
            latitudes: latitudes in degree
            longitudes: longitudes in degree

        """

        self.latitudes = latitudes
        self.longitudes = longitudes

    def get_coordinates(self) -> np.array:
        """
        Returns: coordinates in degree where the first column is the latitudes
         and the second column the longitudes

        """
        return np.array([self.latitudes, self.longitudes]).T

    def get_coordinates_in_radians(self):
        """
        Returns: coordinates in radians where the first column is the latitudes
         and the second column the longitudes

        """
        return np.radians(self.get_coordinates())

    def __eq__(self, other):
        return np.array_equal(self.latitudes, other.latitudes) and np.array_equal(self.longitudes, other.longitudes)

def derive_nearest_neighbours(
    latitudes: np.array,
    longitudes: np.array,
    coordinates: Coordinates,
    number_nearby: int = 1,
) -> Tuple[Union[float, np.ndarray], np.ndarray]:
    """
    A function that uses a k-d tree algorithm to obtain the nearest
    neighbours to coordinate pairs

    Args:
        latitudes (np.array): latitude values of stations_result being compared to
        the coordinates
        longitudes (np.array): longitude values of stations_result being compared to
        the coordinates
        coordinates (Coordinates): the coordinates for which the nearest neighbour
        is searched
        number_nearby: Number of stations_result that should be nearby

    Returns:
        Tuple of distances and ranks of nearest to most distant stations_result
    """
    points = np.c_[np.radians(latitudes), np.radians(longitudes)]

    distance_tree = BallTree(points, metric="haversine")

    return distance_tree.query(coordinates.get_coordinates_in_radians().reshape(-1, 2), k=number_nearby)

def convert_dm_to_dd(dms: float) -> float:
    """Convert degree minutes to decimal degree"""
    degrees, minutes = divmod(dms, 1)

    decimals = round(minutes / 60 * 100, 2)

    return degrees + decimals

max_stars_repo_name: gezzze/pydwd

max_issues_repo_name: gezzze/pydwd

max_forks_repo_name: gezzze/pydwd


text:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 12 10:18:07 2021

@author: jasperhajonides

- change name of behavioural and stimulus file : --> ./BehaviouralData_StimulusInfo.pkl

"""

import math
import os
import scipy.io
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
#from math import pi, sqrt, exp
import random
import pickle
from scipy import stats
import pycircstat
from temp_dec.decoding_functions import *

# =============================================================================
# paths
projectloc = '/Users/jasperhajonides/Documents/Dphil/OSF'

# labels
subject_labels = ['S01','S02','S03','S04','S05','S06','S07','S08','S09','S10','S11','S12','S13','S14','S15','S16','S17','S18','S19','S20']

#%% functions
def reject_outliers(data, m=2):
    return abs(data - np.mean(data)) < m * np.std(data)

#%% run decoding

steps = 1
nr_bins = 10
nsubs = 20
size_window = 30
pca_var = .90
is_classifier='LDA'
time_lim = [-0.4 , 0.9]
nr_tps = 260
nbin = 100
pbin=1/nbin #

# initialise arrays
classifier_output = np.zeros((nr_tps, nsubs,11))
classifier_output_tuning = np.zeros((nr_bins, nr_tps, nsubs,4))
right = np.zeros((nr_bins, nr_tps,nsubs))
left = np.zeros((nr_bins, nr_tps,nsubs))
shift = np.zeros((nr_tps, nsubs))

# load in behavioural data first
if 'thetas_all' not in  locals():
    with open ('%s' %projectloc + '/BehaviouralData_StimulusInfo.pkl', 'rb') as fp:
            [thetas_all, stimulus_nrs, presented_angles, time] = pickle.load(fp)

# loop over subjects
for sb_count in range(nsubs):
    print(sb_count)

    # read MEG data
    with open (projectloc + '/MEG_data/MEG_data_200Hz_%s' %subject_labels[sb_count], 'rb') as fp:
        [X] = pickle.load(fp)
    data = scipy.io.loadmat(projectloc + '/data/decoding_data/306ch_data_' + subject_labels[sb_count] + suffix)
    X,thetas,stimulus_nr,presented_angle,time= concatenate_data(data,sb_count,toi=[-0.4,3.5])
    X = prep_sensor_data(X,data,1,False)
    # time = data['time'][0].T

    # adam thompson. digital consultant

reason: import numpy,import scipy,from scipy


text:
from flask import (
    Blueprint, redirect, render_template, request, url_for, send_file
)
from visual.db import get_db

#--------------------- Imports to generate the plot ---------------------
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import statsmodels.formula.api as smf
import seaborn as sns

from datetime import datetime, timedelta
from dateutil.relativedelta import relativedelta
from random import randint
from pandas.plotting import register_matplotlib_converters
from os import remove
from matplotlib.dates import num2date
from io import BytesIO

#The number of simulations run to generate the ci.
#A value too high will make the app go slow.
N_SIMULATIONS = 100
#------------------------------------------------------------------------

def get_form(form):
    start_date = form['pred_from']
    months = form['pred_to']
    min_ci = form['low_perc']
    max_ci = form['high_perc']
    return start_date, months, min_ci, max_ci

def get_args(args):
    months = args.get('start')
    start_date = args.get('month')
    min_ci = args.get('minci')
    max_ci = args.get('maxci')

    try:
        # We only chekc for type compatibility as they would be
        # converted again to str when passed as arguments in the
        # template.
        datetime.strptime(start_date, '%Y-%m-%d')
        int(min_ci)
        int(max_ci)
        return start_date, months, min_ci, max_ci

    except:
        return None

bp = Blueprint('graphic', __name__)

@bp.route('/', methods=('GET', 'POST'))
@bp.route('/bitcoin', methods=('GET', 'POST'))
def bitcoin():
    if request.method == 'POST':
        form = get_form(request.form)
        return redirect(url_for('graphic.bitcoin', month=form[0], start=form[1], minci=form[2], maxci=form[3]))

    return render_template('cryptos/bitcoin.html', params=get_args(request.args))

@bp.route('/ethereum', methods=('GET', 'POST'))
def ethereum():
    if request.method == 'POST':
        form = get_form(request.form)
        return redirect(url_for('graphic.ethereum', month=form[0], start=form[1], minci=form[2], maxci=form[3]))

    return render_template('cryptos/ethereum.html', params=get_args(request.args))

@bp.route('/ripple', methods=('GET', 'POST'))
def ripple():
    if request.method == 'POST':
        form = get_form(request.form)
        return redirect(url_for('graphic.ripple', month=form[0], start=form[1], minci=form[2], maxci=form[3]))

    return render_template('cryptos/ripple.html', params=get_args(request.args))

@bp.route('/bitcoin-cash', methods=('GET', 'POST'))
def bitcoinCash():
    if request.method == 'POST':
        form = get_form(request.form)
        return redirect(url_for('graphic.bitcoinCash', month=form[0], start=form[1], minci=form[2], maxci=form[3]))

    return render_template('cryptos/bitcoin-cash.html', params=get_args(request.args))

@bp.route('/litecoin', methods=('GET', 'POST'))
def litecoin():
    if request.method == 'POST':
        form = get_form(request.form)
        return redirect(url_for('graphic.litecoin', month=form[0], start=form[1], minci=form[2], maxci=form[3]))

    return render_template('cryptos/litecoin.html', params=get_args(request.args))

@bp.route('/image_plot/<crypto>/<start_date>/<months>/<min_ci>/<max_ci>', methods=('GET',))
def plot_prediction(crypto, start_date=datetime(2018, 2, 1), months=3, min_ci=0, max_ci=90):
    """Generates a graph of the values on 2017 and 2018 of the chosen crypto.
    Then plots the intervals of confidence for future values using the least
    squares linear regression method.

    The dark grey zone is the ci for the sampling error.
    The light grey zone is the ci for the sampling error + random variation.

    The graph is returned as an an URL with a PNG image.

    crypto: string, name of the crypto.
    start_date: datetime object, the starting date for the regression.
    months: int, the number of months we want to predict.
    min_ci: int, the lower percentile we want to plot.
    max_ci: int, the higher percentile we want to plot.
    """
    sns.set_style('whitegrid')
    register_matplotlib_converters()

    # Set the first date from which we make the prediction.
    start_date = datetime.strptime(start_date, '%Y-%m-%d')

    months = int(months)
    ci = [int(min_ci), int(max_ci)]

    db = get_db()
    cur = db.cursor()
    cur.execute(
        'SELECT dt, close_price'
        ' FROM cryptos'
        ' WHERE crypto = %s', (crypto,)
    )

    data = pd.DataFrame(cur.fetchall(), columns=['date', 'close_price'])

    data['id'] = data.index.values.copy()
    data['date'] = pd.to_datetime(data['date'])
    data['close_price'] = pd.to_numeric(data['close_price'])

    # It's the same to make the regression on the id than on the date
    # converted to an int, so we make it on the id.
    # Otherwise we would have to transform dates into int type.
    formula = "close_price ~ id"
    model = smf.ols(formula, data=data[data.date > start_date])
    results = model.fit()

    first_date = data.date[0]                 # As the series is ordered,
    last_date = data.date[data.date.size-1]   # this is faster than using min and max.
    delta_days = days_between(last_date + relativedelta(months=months), first_date)
    time_df = pd.DataFrame(
        # This is more efficient than appending rows to a blank dataframe
        # because on each append pandas make a copy of the table.
        # With this method we avoid that.
        [{'id': i, 'date': first_date + timedelta(i)} for i in range(delta_days)]
    )

    fake_results = []
    fake_predictions = []
    fake_predictions2 = []

    for i in range(N_SIMULATIONS):
        fake_results.append(generate_fake_results(data[data.date > start_date], results, formula))

        prediction1 = fake_results[i].predict(time_df.id)
        fake_predictions.append(prediction1)

        # Add mixed residuals.
        prediction2 = prediction1.copy()
        prediction2 += np.random.choice(fake_results[i].resid, len(prediction2))
        fake_predictions2.append(prediction2)

    plt.figure(figsize=(20, 14))
    sns.lineplot(data.date, data.close_price, color='#2980b9')

    dark_quantiles = percentile_lines(fake_predictions, ci)
    light_quantiles = percentile_lines(fake_predictions2, ci)

    plt.fill_between(time_df.date, dark_quantiles[0], dark_quantiles[1], alpha=0.5, color='#95a5a6')
    plt.fill_between(time_df.date, light_quantiles[0], light_quantiles[1], alpha=0.3, color='#bdc3c7')

    # Make the plot more beatiful
    locs, labels = plt.yticks()
    for i in range(len(labels)):
        labels[i] = str(int(locs[i])) + ' $'
    plt.yticks(ticks=locs, labels=labels)
    plt.xticks(rotation=45)
    plt.tick_params(labelsize=20)
    plt.xlabel('', size = 30)
    plt.ylabel('', size = 30)

    plt.axvline(start_date, 0, 1, color='#34495e')

    x1,x2,_,y2 = plt.axis()
    x1 = time_df.date.min()
    x2 = time_df.date.max()
    plt.axis((x1,x2,0,y2))

    # From: https://stackoverflow.com/questions/20107414/passing-a-matplotlib-figure-to-html-flask
    img = BytesIO()
    plt.savefig(img, format='png')
    img.seek(0)

    return send_file(img, mimetype='image/png')

#--------------------- Functions to generate the plot ---------------------
def days_between(d1, d2):
    """Returns the difference in days between two
    datetime objects.
    """
    return abs((d2 - d1).days)

def generate_fake_results(original, results, formula):
    """Generates fake results from fake data.
    Fake data is generated from mixing the resiudals of the fitted model.
    Then the model is trained on this fake data.

    original: data used to generate the original results
    results: a fitted least squares model
    formula: a string with the formula

    returns: a fitted least squares model
    """
    fake = pd.DataFrame(original.id.copy())
    fake['close_price'] = results.fittedvalues + np.random.choice(results.resid, len(results.resid))
    model = smf.ols(formula, data=fake)
    return model.fit()

def percentile_lines(lines_seq, percents):
    """Returns a list with the percentile lines of the lines_seq.

    lines_seq: a list of NumPy arrays with the same shape.
    percents: a tuple or list of the desired percentiles.

    returns: a list of NumPy arrays.
    """
    lines_df = pd.DataFrame(lines_seq)
    quantiles = list()
    for perc in percents:
        quantiles.append( pd.DataFrame(lines_df).quantile(q=perc/100, axis=0) )

    return quantiles

reason: import numpy,import statsmodels


text:
from netCDF4 import Dataset
import numpy as np
import matplotlib.pyplot as plt
import math
import matplotlib as mpl

#-------------------------------------------------------------------------------

def stress_divergence_hist():

    # grid
    fileGrid = Dataset("grid.40962.nc","r")

    nVertices = len(fileGrid.dimensions["nVertices"])

    latVertex = fileGrid.variables["latVertex"][:]

    fileGrid.close()

    # ic
    fileIC = Dataset("ic_40962.nc","r")

    uVelocity = fileIC.variables["uVelocity"][:]
    vVelocity = fileIC.variables["vVelocity"][:]

    stressDivergenceUAnalytical = fileIC.variables["stressDivergenceUAnalytical"][:]
    stressDivergenceVAnalytical = fileIC.variables["stressDivergenceVAnalytical"][:]

    print("Stress divergence: ",
          np.amin(stressDivergenceUAnalytical), np.amax(stressDivergenceUAnalytical),
          np.amin(stressDivergenceVAnalytical), np.amax(stressDivergenceVAnalytical))

    fileIC.close()

    # Wachspress
    fileWach = Dataset("./output_wachspress_40962/output.2000.nc","r")

    stressDivergenceUWach = fileWach.variables["stressDivergenceU"][0,:]
    stressDivergenceVWach = fileWach.variables["stressDivergenceV"][0,:]

    stressDivergenceUWachDiff = (stressDivergenceUWach - stressDivergenceUAnalytical)
    stressDivergenceVWachDiff = (stressDivergenceVWach - stressDivergenceVAnalytical)

    print("Wachs:     ",
          np.amin(stressDivergenceUWachDiff), np.amax(stressDivergenceUWachDiff),
          np.amin(stressDivergenceVWachDiff), np.amax(stressDivergenceVWachDiff))

    fileWach.close()

    # PWL
    filePWL = Dataset("./output_pwl_40962/output.2000.nc","r")

    stressDivergenceUPWL = filePWL.variables["stressDivergenceU"][0,:]
    stressDivergenceVPWL = filePWL.variables["stressDivergenceV"][0,:]

    stressDivergenceUPWLDiff = (stressDivergenceUPWL - stressDivergenceUAnalytical)
    stressDivergenceVPWLDiff = (stressDivergenceVPWL - stressDivergenceVAnalytical)

    print("PWL:       ",
          np.amin(stressDivergenceUPWLDiff), np.amax(stressDivergenceUPWLDiff),
          np.amin(stressDivergenceVPWLDiff), np.amax(stressDivergenceVPWLDiff))

    filePWL.close()

    # Wachspress alt
    fileWachAlt = Dataset("./output_wachspress_alt_40962/output.2000.nc","r")

    stressDivergenceUWachAlt = fileWachAlt.variables["stressDivergenceU"][0,:]
    stressDivergenceVWachAlt = fileWachAlt.variables["stressDivergenceV"][0,:]

    stressDivergenceUWachAltDiff = (stressDivergenceUWachAlt - stressDivergenceUAnalytical)
    stressDivergenceVWachAltDiff = (stressDivergenceVWachAlt - stressDivergenceVAnalytical)

    print("Wachs Alt: ",
          np.amin(stressDivergenceUWachAltDiff), np.amax(stressDivergenceUWachAltDiff),
          np.amin(stressDivergenceVWachAltDiff), np.amax(stressDivergenceVWachAltDiff))

    fileWachAlt.close()

    # PWL alt
    filePWLAlt = Dataset("./output_pwl_alt_40962/output.2000.nc","r")

    stressDivergenceUPWLAlt = filePWLAlt.variables["stressDivergenceU"][0,:]
    stressDivergenceVPWLAlt = filePWLAlt.variables["stressDivergenceV"][0,:]

    stressDivergenceUPWLAltDiff = (stressDivergenceUPWLAlt - stressDivergenceUAnalytical)
    stressDivergenceVPWLAltDiff = (stressDivergenceVPWLAlt - stressDivergenceVAnalytical)

    print("PWL Alt:   ",
          np.amin(stressDivergenceUPWLAltDiff), np.amax(stressDivergenceUPWLAltDiff),
          np.amin(stressDivergenceVPWLAltDiff), np.amax(stressDivergenceVPWLAltDiff))

    filePWLAlt.close()

    # Weak
    fileWeak = Dataset("./output_weak_40962/output.2000.nc","r")

    stressDivergenceUWeak = fileWeak.variables["stressDivergenceU"][0,:]
    stressDivergenceVWeak = fileWeak.variables["stressDivergenceV"][0,:]

    stressDivergenceUWeakDiff = (stressDivergenceUWeak - stressDivergenceUAnalytical)
    stressDivergenceVWeakDiff = (stressDivergenceVWeak - stressDivergenceVAnalytical)

    print("Weak:      ",
          np.amin(stressDivergenceUWeakDiff), np.amax(stressDivergenceUWeakDiff),
          np.amin(stressDivergenceVWeakDiff), np.amax(stressDivergenceVWeakDiff))

    fileWeak.close()

    # histograms
    stressDivergenceUWachDiffHist    = []
    stressDivergenceUPWLDiffHist     = []
    stressDivergenceUWachAltDiffHist = []
    stressDivergenceUPWLAltDiffHist  = []
    stressDivergenceUWeakDiffHist    = []

    for iVertex in range(0,nVertices):

        if (latVertex[iVertex] > math.radians(20.0)):

            stressDivergenceUWachDiffHist.append(math.fabs(stressDivergenceUWachDiff[iVertex]))
            stressDivergenceUPWLDiffHist.append(math.fabs(stressDivergenceUPWLDiff[iVertex]))
            stressDivergenceUWachAltDiffHist.append(math.fabs(stressDivergenceUWachAltDiff[iVertex]))
            stressDivergenceUPWLAltDiffHist.append(math.fabs(stressDivergenceUPWLAltDiff[iVertex]))
            stressDivergenceUWeakDiffHist.append(math.fabs(stressDivergenceUWeakDiff[iVertex]))

    mpl.rc('text', usetex=True)
    mpl.rc('font', family='Times New Roman', size=8)
    mpl.rcParams['axes.linewidth'] = 0.5

    plt.figure(figsize=(3.74016, 3))

    plt.hist(stressDivergenceUWachDiffHist,    50, range=[0.0,1.0], histtype='step', lw=1, color='red', label='Wachspress')
    plt.hist(stressDivergenceUPWLDiffHist,     50, range=[0.0,1.0], histtype='step', lw=1, color='blue',  label='PWL')
    plt.hist(stressDivergenceUWachAltDiffHist, 50, range=[0.0,1.0], histtype='step', lw=1, color='darkturquoise', label='Wachs. Alt')
    plt.hist(stressDivergenceUPWLAltDiffHist,  50, range=[0.0,1.0], histtype='step', lw=1, color='darkorange',  label='PWL Alt')
    plt.hist(stressDivergenceUWeakDiffHist,    50, range=[0.0,1.0], histtype='step', lw=1, color='green',   label='Weak')

    plt.yscale('log', nonpositive='clip')

    plt.xlabel("Error")
    plt.ylabel("Frequency")

    plt.legend(["Wachs.","PWL","Wachs. Alt","PWL Alt","Weak"], frameon=False, fontsize=8)

    plt.xlim([0,1.0])

    plt.tight_layout(pad=0.5, w_pad=0.5, h_pad=0.5)

    plt.savefig("stress_divergence_hist.png",dpi=400)

#-------------------------------------------------------------------------------

if __name__ == "__main__":

    stress_divergence_hist()


text:
import numpy as np
import math
import chess
from collections import OrderedDict
from time import time
from search.util import cp

FPU = -1.0
FPU_ROOT = 0.0

class UCTNode():
    def __init__(self, board=None, parent=None, move=None, prior=0):
        self.board = board
        self.move = move
        self.is_expanded = False
        self.parent = parent  # Optional[UCTNode]
        self.children = OrderedDict()  # Dict[move, UCTNode]
        self.prior = prior  # float
        if parent == None:
            self.total_value = FPU_ROOT  # float
        else:
            self.total_value = FPU
        self.number_visits = 0  # int

    def Q(self):  # returns float
        return self.total_value / (1 + self.number_visits)

    def U(self):  # returns float
        return (math.sqrt(self.parent.number_visits)
                * self.prior / (1 + self.number_visits))

    def best_child(self, C):
        return max(self.children.values(),
                   key=lambda node: node.Q() + C*node.U())

    def select_leaf(self, C):
        current = self
        while current.is_expanded and current.children:
            current = current.best_child(C)
        if not current.board:
            current.board = current.parent.board.copy()
            current.board.push_uci(current.move)
        return current

    def expand(self, child_priors):
        self.is_expanded = True
        for move, prior in child_priors.items():
            self.add_child(move, prior)

    def add_child(self, move, prior):
        self.children[move] = UCTNode(parent=self, move=move, prior=prior)

    def backup(self, value_estimate: float):
        current = self
        # Child nodes are multiplied by -1 because we want max(-opponent eval)
        turnfactor = -1
        while current.parent is not None:
            current.number_visits += 1
            current.total_value += (value_estimate *
                                    turnfactor)
            current = current.parent
            turnfactor *= -1
        current.number_visits += 1

def UCT_search(board, num_reads, net=None, C=1.0, verbose=False, max_time=None, tree=None, send=None):
    if max_time == None:
        # search for a maximum of an hour
        max_time = 3600.0
    max_time = max_time - 0.05

    start = time()
    count = 0

    root = UCTNode(board)
    for i in range(num_reads):
        count += 1
        leaf = root.select_leaf(C)
        child_priors, value_estimate = net.evaluate(leaf.board)
        leaf.expand(child_priors)
        leaf.backup(value_estimate)
        now = time()
        delta = now - start
        if (time != None) and (delta > max_time):
            break

    bestmove, node = max(root.children.items(), key=lambda item: (item[1].number_visits, item[1].Q()))
    score = int(round(cp(node.Q()),0))
    if send != None:
        for nd in sorted(root.children.items(), key= lambda item: item[1].number_visits):
            send("info string {} {} \t(P: {}%) \t(Q: {})".format(nd[1].move, nd[1].number_visits, round(nd[1].prior*100,2), round(nd[1].Q(), 5)))
        send("info depth 1 seldepth 1 score cp {} nodes {} nps {} pv {}".format(score, count, int(round(count/delta, 0)), bestmove))

    # if we have a bad score, go for a draw
    return bestmove, score