text:
#!/usr/bin/env python
# encoding: utf-8

import numpy as np

gamma = 1.4
gamma1 = gamma - 1.

def qinit(state,x0=0.5,y0=0.,r0=0.2,rhoin=0.1,pinf=5.):
    grid = state.grid

    rhoout = 1.
    pout   = 1.
    pin    = 1.

    rinf = (gamma1 + pinf*(gamma+1.))/ ((gamma+1.) + gamma1*pinf)
    vinf = 1./np.sqrt(gamma) * (pinf - 1.) / np.sqrt(0.5*((gamma+1.)/gamma) * pinf+0.5*gamma1/gamma)
    einf = 0.5*rinf*vinf**2 + pinf/gamma1

    # Create an array with fortran native ordering
    x =grid.x.centers
    y =grid.y.centers
    Y,X = np.meshgrid(y,x)
    r = np.sqrt((X-x0)**2 + (Y-y0)**2)

    state.q[0,:,:] = rhoin*(r<=r0) + rhoout*(r>r0)
    state.q[1,:,:] = 0.
    state.q[2,:,:] = 0.
    state.q[3,:,:] = (pin*(r<=r0) + pout*(r>r0))/gamma1
    state.q[4,:,:] = 1.*(r<=r0)

def auxinit(state):
    """
    aux[1,i,j] = y-coordinate of cell centers for cylindrical source terms
    """
    x=state.grid.x.centers
    y=state.grid.y.centers
    for j,ycoord in enumerate(y):
        state.aux[0,:,j] = ycoord

def shockbc(state,dim,t,qbc,num_ghost):
    """
    Incoming shock at left boundary.
    """
    for (i,state_dim) in enumerate(state.patch.dimensions):
        if state_dim.name == dim.name:
            dim_index = i
            break

    if (state.patch.dimensions[dim_index].lower ==
                        state.grid.dimensions[dim_index].lower):

        pinf=5.
        rinf = (gamma1 + pinf*(gamma+1.))/ ((gamma+1.) + gamma1*pinf)
        vinf = 1./np.sqrt(gamma) * (pinf - 1.) / np.sqrt(0.5*((gamma+1.)/gamma) * pinf+0.5*gamma1/gamma)
        einf = 0.5*rinf*vinf**2 + pinf/gamma1

        for i in xrange(num_ghost):
            qbc[0,i,...] = rinf
            qbc[1,i,...] = rinf*vinf
            qbc[2,i,...] = 0.
            qbc[3,i,...] = einf
            qbc[4,i,...] = 0.

def step_Euler_radial(solver,state,dt):
    """
    Geometric source terms for Euler equations with radial symmetry.
    Integrated using a 2-stage, 2nd-order Runge-Kutta method.
    This is a Clawpack-style source term routine.
    """

    dt2 = dt/2.
    ndim = 2

    aux=state.aux
    q = state.q

    rad = aux[0,:,:]

    rho = q[0,:,:]
    u   = q[1,:,:]/rho
    v   = q[2,:,:]/rho
    press  = gamma1 * (q[3,:,:] - 0.5*rho*(u**2 + v**2))

    qstar = np.empty(q.shape)

    qstar[0,:,:] = q[0,:,:] - dt2*(ndim-1)/rad * q[2,:,:]
    qstar[1,:,:] = q[1,:,:] - dt2*(ndim-1)/rad * rho*u*v
    qstar[2,:,:] = q[2,:,:] - dt2*(ndim-1)/rad * rho*v*v
    qstar[3,:,:] = q[3,:,:] - dt2*(ndim-1)/rad * v * (q[3,:,:] + press)

    rho = qstar[0,:,:]
    u   = qstar[1,:,:]/rho
    v   = qstar[2,:,:]/rho
    press  = gamma1 * (qstar[3,:,:] - 0.5*rho*(u**2 + v**2))

    q[0,:,:] = q[0,:,:] - dt*(ndim-1)/rad * qstar[2,:,:]
    q[1,:,:] = q[1,:,:] - dt*(ndim-1)/rad * rho*u*v
    q[2,:,:] = q[2,:,:] - dt*(ndim-1)/rad * rho*v*v
    q[3,:,:] = q[3,:,:] - dt*(ndim-1)/rad * v * (qstar[3,:,:] + press)

def dq_Euler_radial(solver,state,dt):

    """
    Geometric source terms for Euler equations with radial symmetry.
    Integrated using a 2-stage, 2nd-order Runge-Kutta method.
    This is a SharpClaw-style source term routine.
    """

    ndim = 2

    q   = state.q
    aux = state.aux

    rad = aux[0,:,:]

    rho = q[0,:,:]
    u   = q[1,:,:]/rho
    v   = q[2,:,:]/rho
    press  = gamma1 * (q[3,:,:] - 0.5*rho*(u**2 + v**2))

    dq = np.empty(q.shape)

    dq[0,:,:] = -dt*(ndim-1)/rad * q[2,:,:]
    dq[1,:,:] = -dt*(ndim-1)/rad * rho*u*v
    dq[2,:,:] = -dt*(ndim-1)/rad * rho*v*v
    dq[3,:,:] = -dt*(ndim-1)/rad * v * (q[3,:,:] + press)
    dq[4,:,:] = 0

    return dq

def shockbubble(use_petsc=False,kernel_language='Fortran',solver_type='classic',iplot=False,htmlplot=False,amr_type=None):
    """
    Solve the Euler equations of compressible fluid dynamics.
    This example involves a bubble of dense gas that is impacted by a shock.
    """
    #===========================================================================
    # Import libraries
    #===========================================================================
    if use_petsc:
        import clawpack.petclaw as pyclaw
    else:
        from clawpack import pyclaw

    if kernel_language != 'Fortran':
        raise Exception('Unrecognized value of kernel_language for Euler Shockbubble')

    if solver_type=='sharpclaw':
        solver = pyclaw.SharpClawSolver2D()
        solver.dq_src=dq_Euler_radial
        solver.weno_order=5
        solver.lim_type=2
    else:
        solver = pyclaw.ClawSolver2D()
        solver.limiters = [4,4,4,4,2]
        solver.step_source=step_Euler_radial

    #===========================================================================
    # Initialize domain
    #===========================================================================

    # resolution of each grid
    mx=160; my=40

    # number of initial AMR grids in each dimension
    msubgrid = 1

    if amr_type is None:
        # number of Domain grid cells expressed as the product of
        # grid resolution and the number of AMR sub-grids for
        # easy comparison between the two methods
        mx = mx*msubgrid
        my = my*msubgrid
    x = pyclaw.Dimension('x',0.0,2.0,mx)
    y = pyclaw.Dimension('y',0.0,0.5,my)
    domain = pyclaw.Domain([x,y])
    num_eqn = 5
    num_aux = 1
    state = pyclaw.State(domain,num_eqn,num_aux)

    state.problem_data['gamma']= gamma
    state.problem_data['gamma1']= gamma1

    qinit(state)
    auxinit(state)
    initial_solution = pyclaw.Solution(state,domain)

    from clawpack import riemann
    solver.rp = riemann.rp2_euler_5wave
    solver.cfl_max = 0.5
    solver.cfl_desired = 0.45
    solver.num_waves = 5
    solver.dt_initial=0.005
    solver.user_bc_lower=shockbc
    solver.source_split = 1
    solver.bc_lower[0]=pyclaw.BC.custom
    solver.bc_upper[0]=pyclaw.BC.extrap
    solver.bc_lower[1]=pyclaw.BC.wall
    solver.bc_upper[1]=pyclaw.BC.extrap
    #Aux variable in ghost cells doesn't matter
    solver.aux_bc_lower[0]=pyclaw.BC.extrap
    solver.aux_bc_upper[0]=pyclaw.BC.extrap
    solver.aux_bc_lower[1]=pyclaw.BC.extrap
    solver.aux_bc_upper[1]=pyclaw.BC.extrap

    #===========================================================================
    # Set up controller and controller parameters
    #===========================================================================
    claw = pyclaw.Controller()
    claw.keep_copy = True
    claw.tfinal = 0.2
    claw.solution = initial_solution
    claw.num_output_times = 1

    if amr_type is not None:
        if amr_type == 'peano':
            import clawpack.peanoclaw as amrclaw
            claw.solver = amrclaw.Solver(solver,
                                        (x.upper-x.lower)/(mx*msubgrid),
                                        qinit, auxinit)
            claw.solution = amrclaw.Solution(state, domain)
        else:
            raise Exception('unsupported amr_type %s' % amr_type)
    else:
        claw.solver = solver
        claw.solution = pyclaw.Solution(state,domain)

    # Solve
    status = claw.run()

    if htmlplot:  pyclaw.plot.html_plot(file_format=claw.output_format)
    if iplot:     pyclaw.plot.interactive_plot(file_format=claw.output_format)

    return claw.frames[claw.num_output_times].state

if __name__=="__main__":
    from clawpack.pyclaw.util import run_app_from_main
    output = run_app_from_main(shockbubble)

max_stars_repo_name: arashb/pyclaw

max_issues_repo_name: arashb/pyclaw

max_forks_repo_name: arashb/pyclaw


text:
# -*- coding: utf-8 -*-
"""
Created on Wed Jun 19 14:01:04 2019

@author: bpe043
"""

from sklearn.utils import assert_all_finite
from sklearn.utils.extmath import stable_cumsum

import warnings
import numbers

import os
import pickle
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

from sklearn import preprocessing
#from sklearn.metrics import precision_recall_curve
from sklearn.metrics import average_precision_score
from sklearn.metrics import classification_report
from sklearn.preprocessing import label_binarize

from sklearn.metrics import roc_curve, auc
from sklearn import datasets
from sklearn.multiclass import OneVsRestClassifier
from sklearn.svm import LinearSVC
from sklearn.model_selection import train_test_split

if os.path.exists('labels.txt'):
    with open('labels.txt', 'rb') as fp:
        list_of_labels = pickle.load(fp)

if os.path.exists('predictions.txt'):
    with open('predictions.txt', 'rb') as fp:
        list_of_predictions = pickle.load(fp)

if os.path.exists('prediction_values.txt'):
    with open('prediction_values.txt', 'rb') as fp:
        list_of_prediction_values = pickle.load(fp)

if os.path.exists('full_prediction_values.txt'):
    with open('full_prediction_values.txt', 'rb') as fp:
        full_prediction_values = pickle.load(fp)
# =============================================================================
# target_names = ['Label 1', 'Label 2', 'Label 3', 'Label 4', 'Label 5', 'Label 6', 'Label 7', 'Label 8', 'Label 9']
#
# cr = classification_report(list_of_labels, list_of_predictions, target_names = target_names)
#
# print(cr)
# =============================================================================

start = 0
step = 100
i = 1               # Used as the pos_label parameter for precision_recall_curve, hence starting at 1
while i < 10:

    predictions = list_of_predictions[start: start + step]
    prediction_values = list_of_prediction_values[start : start + step]

    predictions = np.array(predictions)

    precision, recall, thresholds = precision_recall_curve(predictions, prediction_values, pos_label = i)

    plt.plot(thresholds, precision, color=sns.color_palette()[1])
    plt.plot(thresholds, recall, color=sns.color_palette()[0])

    leg = plt.legend(('precision', 'recal'), frameon=True, loc='lower left')
    leg.get_frame().set_edgecolor('k')
    plt.title('Precision - Recall for label {}'.format(i))
    plt.xlabel('threshold')
    plt.ylabel('%')

    # Extra visibility factors
# =============================================================================
#     plt.xticks([0.90, 0.91, 0.92, 0.93, 0.94, 0.95, 0.96, 0.97, 0.98, 0.99, 1.0])
#     plt.yticks([0.80, 0.81, 0.82, 0.83, 0.84, 0.85, 0.86, 0.87, 0.88, 0.89, 0.90, 0.91, 0.92, 0.93, 0.94, 0.95, 0.96, 0.97, 0.98, 0.99, 1.0], fontsize = 6)
#     plt.grid()
#     plt.axis([0.8, 1.05, 0.8, 1.025])
# =============================================================================

    plt.savefig('Plots/Precision - Recall for label {}.png'.format(i))

    plt.show()

    stop = True
    i += 1
    start += 100

ext: py

max_stars_repo_name: uit-hdl/rhd-codes

max_issues_repo_name: uit-hdl/rhd-codes

max_forks_repo_name: uit-hdl/rhd-codes

reason: import numpy


text:
#!/usr/bin/env python
# coding: utf-8

# In[59]:

import glfw
from OpenGL.GL import *
import OpenGL.GL.shaders
import numpy as np
import sys
import random

import transformations as tr
import easy_shaders as es
import scene_graph as sg
import basic_shapes as bs
import acuario as acuario
import pez as pez1
import pez2 as pez2
import pez3 as pez3

PROJECTION_ORTHOGRAPHIC = 0
PROJECTION_FRUSTUM = 1
PROJECTION_PERSPECTIVE = 2

archivo = sys.argv[1]
Data = None
with open(archivo) as file:
    data = json.load(file)
    Data = data

def createColorCube(i, j, k, X, Y, Z, color):
    l_x = X[i, j, k]
    r_x = X[i+1, j, k]
    b_y = Y[i, j, k]
    f_y = Y[i, j+1, k]
    b_z = Z[i, j, k]
    t_z = Z[i, j, k+1]
    c = color
    #   positions    colors
    vertices = [
    # Z+: number 1
        l_x, b_y,  t_z, c[0],c[1],c[2],
         r_x, b_y,  t_z, c[0],c[1],c[2],
         r_x,  f_y,  t_z, c[0],c[1],c[2],
        l_x,  f_y,  t_z, c[0],c[1],c[2],
    # Z-: number 6
        l_x, b_y, b_z, c[0],c[1],c[2],
         r_x, b_y, b_z, c[0],c[1],c[2],
         r_x,  f_y, b_z, c[0],c[1],c[2],
        l_x,  f_y, b_z, c[0],c[1],c[2],
    # X+: number 5
         r_x, b_y, b_z, c[0],c[1],c[2],
         r_x,  f_y, b_z, c[0],c[1],c[2],
         r_x,  f_y,  t_z, c[0],c[1],c[2],
         r_x, b_y,  t_z, c[0],c[1],c[2],
    # X-: number 2
        l_x, b_y, b_z, c[0],c[1],c[2],
        l_x,  f_y, b_z, c[0],c[1],c[2],
        l_x,  f_y,  t_z, c[0],c[1],c[2],
        l_x, b_y,  t_z, c[0],c[1],c[2],
    # Y+: number 4
        l_x,  f_y, b_z, c[0],c[1],c[2],
        r_x,  f_y, b_z, c[0],c[1],c[2],
        r_x,  f_y, t_z, c[0],c[1],c[2],
        l_x,  f_y, t_z, c[0],c[1],c[2],
    # Y-: number 3
        l_x, b_y, b_z, c[0],c[1],c[2],
        r_x, b_y, b_z, c[0],c[1],c[2],
        r_x, b_y, t_z, c[0],c[1],c[2],
        l_x, b_y, t_z, c[0],c[1],c[2],
        ]

    # Defining connections among vertices
    # We have a triangle every 3 indices specified
    indices = [
        0, 1, 2, 2, 3, 0,
        4, 5, 6, 6, 7, 4,
        4, 5, 1, 1, 0, 4,
        6, 7, 3, 3, 2, 6,
        5, 6, 2, 2, 1, 5,
        7, 4, 0, 0, 3, 7]

    return bs.Shape(vertices, indices)

def merge(destinationShape, strideSize, sourceShape):

    # current vertices are an offset for indices refering to vertices of the new shape
    offset = len(destinationShape.vertices)
    destinationShape.vertices += sourceShape.vertices
    destinationShape.indices += [(offset/strideSize) + index for index in sourceShape.indices]

class Controller:
    def __init__(self):
        self.fillPolygon = True
        self.projection = PROJECTION_PERSPECTIVE
        self.TipoA = False
        self.TipoB = False
        self.TipoC = False
        self.ZOOM = 0

# We will use the global controller as communication with the callback function
controller = Controller()

def on_key(window, key, scancode, action, mods):

    if action != glfw.PRESS:
        return

    global controller

    if key == glfw.KEY_SPACE:
        controller.fillPolygon = not controller.fillPolygon

    elif key == glfw.KEY_1:
        print('Orthographic projection')
        controller.projection = PROJECTION_ORTHOGRAPHIC

    elif key == glfw.KEY_2:
        print('Frustum projection')
        controller.projection = PROJECTION_FRUSTUM

    elif key == glfw.KEY_3:
        print('Perspective projection')
        controller.projection = PROJECTION_PERSPECTIVE

    elif key == glfw.KEY_ESCAPE:
        glfw.set_window_should_close(window, True)

    elif key == glfw.KEY_A:
        print('Tipo A')
        controller.TipoA = not controller.TipoA

    elif key == glfw.KEY_B:
        print('Tipo B')
        controller.TipoB = not controller.TipoB

    elif key == glfw.KEY_C:
        print('Tipo C')
        controller.TipoC = not controller.TipoC

if __name__ == "__main__":

    # Initialize glfw
    if not glfw.init():
        sys.exit()

    width = 600
    height = 600

    window = glfw.create_window(width, height, "Aquarium view", None, None)

    if not window:
        glfw.terminate()
        sys.exit()

    glfw.make_context_current(window)

    # Connecting the callback function 'on_key' to handle keyboard events
    glfw.set_key_callback(window, on_key)

    # Assembling the shader program
    pipeline = es.SimpleModelViewProjectionShaderProgram()
    mvpPipeline = es.SimpleModelViewProjectionShaderProgram()

    # Telling OpenGL to use our shader program
    glUseProgram(pipeline.shaderProgram)

    # Setting up the clear screen color
    glClearColor(0.08, 0.4, 1, 1.0)

    # As we work in 3D, we need to check which part is in front,
    # and which one is at the back
    glEnable(GL_DEPTH_TEST)

    # Creating shapes on GPU memory
    gpuAxis = es.toGPUShape(bs.createAxis(7))

    # Load potentials and grid

    file = Data["filename"]
    ta = int(Data["t_a"])
    tb = int(Data["t_b"])
    tc = int(Data["t_c"])

    load_voxels = np.load(file)

    W = ub.shape[0]
    L = ub.shape[1]
    H = ub.shape[2]

    Y, X, Z = np.meshgrid[np.linspace(0, L ,L), np.linspace(0, W, W), np.linspace(0, H, H)]

    isosurfaceA = bs.Shape([], [])
    isosurfaceB = bs.Shape([], [])
    isosurfaceC = bs.Shape([], [])

    # Now let's draw voxels!
    for i in range(X.shape[0]-1):
        for j in range(X.shape[1]-1):
            for k in range(X.shape[2]-1):
                Temp = load_voxels[i,j,k]
                if Temp <= (ta+2) and Temp >= (ta-1):
                    temp_shape = createColorCube(i,j,k, X,Y, Z, [0.9, 0.9, 0])
                    merge(destinationShape=isosurfaceA, strideSize=6, sourceShape=temp_shape)
                if Temp <= (tb+2) and Temp >= (tb-1):
                    temp_shape = createColorCube(i,j,k, X,Y, Z, [0, 0.6, 1])
                    merge(destinationShape=isosurfaceB, strideSize=6, sourceShape=temp_shape)
                if Temp <= (tc+2) and Temp >= (tc-1):
                    temp_shape = createColorCube(i,j,k, X,Y, Z, [1, 0.2, 0])
                    merge(destinationShape=isosurfaceC, strideSize=6, sourceShape=temp_shape)

    gpu_surfaceA = es.toGPUShape(isosurfaceA)
    gpu_surfaceB = es.toGPUShape(isosurfaceB)
    gpu_surfaceC = es.toGPUShape(isosurfaceC)

    # Acuario y peces
    AquariumNode = acuario.createAquarium()

    # Tipo A : Agua tibia (peces globo)
    PosicionesA = []
    for vertice in range(0,len(isosurfaceA.vertices), ta):
        # Guardo los vértices de cada cubo en el que sé se cumple la temperatura
        Pos = (isosurfaceA.vertices[vertice], isosurfaceA.vertices[vertice+1], isosurfaceA.vertices[vertice+2])
        PosicionesA.append(Pos)

    GpuPecesA = []
    GpuPosicionesA = []
    for k in range(5):
        # Creo los nodos de peces y los guardo para dibujarlos
        pez_tipoA = pez3.createFish([0.52,0.25,0], [0.7,0.5,0.3])   # pez-globo para agua tibia
        pez_tipoA_pos = random.choice(PosicionesA)
        GpuPecesA.append(pez_tipoA)
        GpuPosicionesA.append(pez_tipoA_pos)

    # Tipo B : Agua fria (peces cril)
    PosicionesB = []
    for vertice in range(0,len(isosurfaceB.vertices), tb):
        # Guardo los vértices de cada cubo en el que sé se cumple la temperatura
        Pos = (isosurfaceB.vertices[vertice], isosurfaceB.vertices[vertice+1], isosurfaceB.vertices[vertice+2])
        PosicionesB.append(Pos)

    GpuPecesB = []
    GpuPosicionesB = []
    for k in range(3):
        # Creo los nodos de peces y los guardo para dibujarlos
        pez_tipoB = pez2.createFish([0,1,0], [0.5,1,0.5])   # Cril para agua fria
        pez_tipoB_pos = random.choice(PosicionesB)
        GpuPecesB.append(pez_tipoB)
        GpuPosicionesB.append(pez_tipoB_pos)

    # Tipo C : Agua caliente (peces payaso)
    PosicionesC = []
    for vertice in range(0,len(isosurfaceC.vertices), tc):
        # Guardo los vértices de cada cubo en el que sé se cumple la temperatura
        Pos = (isosurfaceC.vertices[vertice], isosurfaceC.vertices[vertice+1], isosurfaceC.vertices[vertice+2])
        PosicionesC.append(Pos)

    GpuPecesC = []
    GpuPosicionesC = []
    for k in range(7):
        # Creo los nodos de peces y los guardo para dibujarlos
        pez_tipoC = pez1.createFish([1,0,0], [1,0.5,0.5])   # Semi-pez-payaso para agua caliente
        pez_tipoC_pos = random.choice(PosicionesC)
        GpuPecesC.append(pez_tipoC)
        GpuPosicionesC.append(pez_tipoC_pos)

    t0 = glfw.get_time()
    camera_theta = np.pi/4

    while not glfw.window_should_close(window):
        # Using GLFW to check for input events
        glfw.poll_events()

        # Getting the time difference from the previous iteration
        t1 = glfw.get_time()
        dt = t1 - t0
        t0 = t1

        if (glfw.get_key(window, glfw.KEY_LEFT) == glfw.PRESS):
            camera_theta -= 2 * dt

        if (glfw.get_key(window, glfw.KEY_RIGHT) == glfw.PRESS):
            camera_theta += 2* dt

        if (glfw.get_key(window, glfw.KEY_UP) == glfw.PRESS):
            controller.ZOOM += 2 * dt

        if (glfw.get_key(window, glfw.KEY_DOWN) == glfw.PRESS):
            controller.ZOOM -= 2 * dt

        # Setting up the view transform

        camX = 1.5 + (9 - controller.ZOOM) * np.sin(camera_theta)
        camY = 3 + (9 - controller.ZOOM) * np.cos(camera_theta)

        viewPos = np.array([camX, camY, (11 - controller.ZOOM)])

        view = tr.lookAt(
            viewPos,
            np.array([1.5,3,0]),
            np.array([0,0,1])
        )

        glUniformMatrix4fv(glGetUniformLocation(pipeline.shaderProgram, "view"), 1, GL_TRUE, view)

        # Setting up the projection transform

        if controller.projection == PROJECTION_ORTHOGRAPHIC:
            projection = tr.ortho(-8, 8, -8, 8, 0.1, 100)

        elif controller.projection == PROJECTION_FRUSTUM:
            projection = tr.frustum(-5, 5, -5, 5, 9, 100)

        elif controller.projection == PROJECTION_PERSPECTIVE:
            projection = tr.perspective(60, float(width)/float(height), 0.1, 100)

        else:
            raise Exception()

        glUniformMatrix4fv(glGetUniformLocation(pipeline.shaderProgram, "projection"), 1, GL_TRUE, projection)

        # Clearing the screen in both, color and depth
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

        # Filling or not the shapes depending on the controller state
        if (controller.fillPolygon):
            glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
        else:
            glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)

        # Drawing shapes with different model transformations

        glUniformMatrix4fv(glGetUniformLocation(pipeline.shaderProgram, "model"), 1, GL_TRUE,
                           tr.matmul([tr.translate(1.5,3,0), tr.uniformScale(1)]))
        pipeline.drawShape(gpuAxis, GL_LINES)

        if controller.TipoA:
            glUniformMatrix4fv(glGetUniformLocation(pipeline.shaderProgram, "model"), 1, GL_TRUE,
                           tr.matmul([tr.scale(3/W, 6/L, 4/H)]))
            pipeline.drawShape(gpu_surfaceA)
        if controller.TipoB:
            glUniformMatrix4fv(glGetUniformLocation(pipeline.shaderProgram, "model"), 1, GL_TRUE,
                           tr.matmul([tr.scale(3/W, 6/L, 4/H)]))
            pipeline.drawShape(gpu_surfaceB)
        if controller.TipoC:
            glUniformMatrix4fv(glGetUniformLocation(pipeline.shaderProgram, "model"), 1, GL_TRUE,
                           tr.matmul([tr.scale(3/W, 6/L, 4/H)]))
            pipeline.drawShape(gpu_surfaceC)

        AquariumNode.transform = tr.matmul([tr.translate(1.5,3,0), tr.rotationZ(np.pi/2)])
        sg.drawSceneGraphNode(AquariumNode, mvpPipeline, "model")

        for k in range(0, len(GpuPecesA)):
            GpuPecesA[k].transform = tr.matmul([tr.translate(GpuPosicionesA[k][0], GpuPosicionesA[k][1],
                                                            GpuPosicionesA[k][2]),
                                                tr.uniformScale(0.3), tr.rotationZ(random.randint(0, 6))])
            sg.drawSceneGraphNode(GpuPecesA[k], mvpPipeline, "model")

        for k in range(0, len(GpuPecesB)):
            GpuPecesB[k].transform = tr.matmul([tr.translate(GpuPosicionesB[k][0], GpuPosicionesB[k][1],
                                                            GpuPosicionesB[k][2]),
                                                tr.uniformScale(0.2), tr.rotationZ(random.randint(0, 6))])
            sg.drawSceneGraphNode(GpuPecesB[k], mvpPipeline, "model")

        for k in range(0, len(GpuPecesC)):
            GpuPecesC[k].transform = tr.matmul([tr.translate(GpuPosicionesC[k][0], GpuPosicionesC[k][1],
                                                            GpuPosicionesC[k][2]),
                                                tr.uniformScale(0.5), tr.rotationZ(random.randint(0, 6))])
            sg.drawSceneGraphNode(GpuPecesC[k], mvpPipeline, "model")

        # Once the drawing is rendered, buffers are swap so an uncomplete drawing is never seen.
        glfw.swap_buffers(window)

    glfw.terminate()

# In[ ]:

max_stars_repo_path: Tarea3a - EDPs/aquarium-viewer.py

max_stars_repo_name: Nicolas-Francisco/Computer-Graphics

max_issues_repo_path: Tarea3a - EDPs/aquarium-viewer.py

max_issues_repo_name: Nicolas-Francisco/Computer-Graphics

max_forks_repo_path: Tarea3a - EDPs/aquarium-viewer.py

max_forks_repo_name: Nicolas-Francisco/Computer-Graphics


text:
from typing import Any, Callable, Dict, Optional, Tuple, Union

import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import Layer
from tensorflow.keras.initializers import TruncatedNormal

def get_kernel_initializer(filters: int,
                           kernel_size: Tuple[int, int]) -> TruncatedNormal:
    stddev = np.sqrt(2 / (kernel_size[0] * kernel_size[1] * filters))
    return TruncatedNormal(stddev=stddev)

class InstanceNormalization(tf.keras.layers.Layer):
    """Instance Normalization Layer (https://arxiv.org/abs/1607.08022)

    Attributes:
        epsilon: small number
        scale:
        offset:
    """

    def __init__(self, epsilon: float = 1e-5) -> None:
        super(InstanceNormalization, self).__init__()
        self.epsilon = epsilon

    def build(self, input_shape) -> None:
        self.scale = self.add_weight(
            name='scale',
            shape=input_shape[-1:],
            initializer=tf.random_normal_initializer(1., 0.02),
            trainable=True
        )

        self.offset = self.add_weight(
            name='offset',
            shape=input_shape[-1:],
            initializer='zeros',
            trainable=True
        )

    def call(self, x: tf.Tensor) -> tf.Tensor:
        mean, variance = tf.nn.moments(x, axes=[1, 2], keepdims=True)
        inv = tf.math.rsqrt(variance + self.epsilon)
        normalized = (x - mean) * inv
        return self.scale * normalized + self.offset

class SequentialConv2DLayer(Layer):
    """Encapsulate a sequence of Conv2D operations.

    (Conv2D => Normalization => Dropout => Activation) * nlayer
    Default:     batchNorm       0.5        relu           2

    Attributes:
        ishape: input sample shape.
        nlayer: number of Conv2D layers.
        padding: padding scheme for Conv2D, 'same' or 'valid'.
        strides: strides arg for Conv2D.
        activation: activation function.
        drop_rate: drop_rate arg for Conv2D.
        norm_type: normalization layer, 'batchnorm' or 'instancenorm'.
        layers: list of layers.
        kernel_shape: convolution kernel shape.
    """

    def __init__(self,
                 kernel_shape: Tuple[int, int, int],
                 nlayer: int = 2,
                 padding: str = 'same',
                 strides: int = 1,
                 activation: Optional[Union[str, Callable]] = 'relu',
                 drop_rate: float = 0.,
                 norm_type: Optional[Union[str, Callable]] = 'instancenorm',
                 **kwargs) -> None:
        super(SequentialConv2DLayer, self).__init__(**kwargs)

        self.ishape = None  # shape = [rows/height, cols/width, channels]
        self.nlayer = nlayer
        self.padding = padding
        self.strides = strides
        self.activation = activation
        self.drop_rate = drop_rate
        self.norm_type = norm_type
        self.layers = []
        self.kernel_shape = kernel_shape

        for _ in range(self.nlayer):
            self.layers.append(
                tf.keras.layers.Conv2D(
                    filters=kernel_shape[2],
                    kernel_size=kernel_shape[:2],
                    strides=strides,
                    padding=padding,
                    kernel_initializer=get_kernel_initializer(
                        filters=kernel_shape[2],
                        kernel_size=kernel_shape[:2]
                    )
                )
            )

            if self.norm_type:
                if isinstance(self.norm_type, str):
                    if self.norm_type.lower() == 'batchnorm':
                        self.layers.append(
                            tf.keras.layers.BatchNormalization())
                    if self.norm_type.lower() == 'instancenorm':
                        self.layers.append(InstanceNormalization())
                elif isinstance(self.norm_type, Callable):
                    self.layers.append(self.norm_type)

            if self.drop_rate > 0.:
                self.layers.append(tf.keras.layers.Dropout(self.drop_rate))

            if self.activation:
                if isinstance(self.activation, str):
                    self.layers.append(
                        tf.keras.layers.Activation(self.activation))
                elif isinstance(self.activation, Callable):
                    self.layers.append(self.activation)

    def call(self,
             inputs: tf.Tensor,
             training: bool = False,
             **kwargs) -> tf.Tensor:
        x = inputs

        for layer in self.layers:
            if not training and isinstance(layer, tf.keras.layers.Dropout):
                continue
            x = layer(x)

        return x

    def get_config(self) -> Dict[str, Any]:
        return dict(kernel_shape=self.kernel_shape,
                    nlayer=self.nlayer,
                    padding=self.padding,
                    strides=self.strides,
                    activation=self.activation,
                    drop_rate=self.drop_rate,
                    norm_type=self.norm_type,
                    **super(SequentialConv2DLayer, self).get_config())

class Conv2DTransposeLayer(Layer):
    """Encapsulate Conv2DTranspose operation.

    Conv2DTranspose => Normalization => Dropout => Activation
    Default:              batchNorm        0.5        relu

    Attributes:
        kernel_shape: kernel size and filters.
        padding: padding scheme for Conv2DTranspose.
        strides: strides arg for Conv2DTranspose.
        activation: activation arg for Conv2DTranspose.
        drop_rate: dropout rate.
        norm_type: normalization layer, 'batchnorm' or 'instancenorm'.
        layers: list of layers.
    """

    def __init__(self,
                 kernel_shape: Tuple[int, int, int],
                 padding: str = 'valid',
                 strides: int = 2,
                 activation: Optional[Union[str, Callable]] = 'relu',
                 drop_rate: float = 0.,
                 norm_type: Optional[Union[str, Callable]] = 'instancenorm',
                 **kwargs) -> None:
        super(Conv2DTransposeLayer, self).__init__(**kwargs)

        self.kernel_shape = kernel_shape
        self.padding = padding
        self.strides = strides
        self.activation = activation
        self.drop_rate = drop_rate
        self.norm_type = norm_type
        self.layers = []

        self.layers.append(
            tf.keras.layers.Conv2DTranspose(
                filters=kernel_shape[2],
                kernel_size=kernel_shape[:2],
                strides=strides,
                padding=padding,
                kernel_initializer=get_kernel_initializer(
                    filters=kernel_shape[2],
                    kernel_size=kernel_shape[:2]
                )
            )
        )

        if self.norm_type:
            if isinstance(self.norm_type, str):
                if self.norm_type.lower() == 'batchnorm':
                    self.layers.append(tf.keras.layers.BatchNormalization())
                if self.norm_type.lower() == 'instancenorm':
                    self.layers.append(InstanceNormalization())
            elif isinstance(self.norm_type, Callable):
                self.layers.append(self.norm_type)

        if self.drop_rate > 0.:
            self.layers.append(tf.keras.layers.Dropout(self.drop_rate))

        if self.activation:
            if isinstance(self.activation, str):
                self.layers.append(
                    tf.keras.layers.Activation(self.activation))
            elif isinstance(self.activation, Callable):
                self.layers.append(self.activation)

    def call(self,
             inputs: tf.Tensor,
             training: bool = False,
             **kwargs) -> tf.Tensor:
        x = inputs

        for layer in self.layers:
            if not training and isinstance(layer, tf.keras.layers.Dropout):
                continue
            x = layer(x)

        return x

    def get_config(self) -> Dict[str, Any]:
        return dict(kernel_shape=self.kernel_shape,
                    padding=self.padding,
                    strides=self.strides,
                    activation=self.activation,
                    drop_rate=self.drop_rate,
                    norm_type=self.norm_type,
                    **super(Conv2DTransposeLayer, self).get_config())

class CropConcatLayer(Layer):
    """Crop a downstream output tensor and concat with a upstream output.
    """

    def call(self, x1: tf.Tensor, x2: tf.Tensor) -> tf.Tensor:
        """implement crop-concat operation.

        Args:
            x1: downstream output.
            x2: upstream output.

        Returns: tf.Tensor
        """
        # the shape of the tensor is [batch, rows, cols, channels]
        x1_shape = tf.shape(x1)
        x2_shape = tf.shape(x2)

        h_diff = x1_shape[1] - x2_shape[1]
        w_diff = x1_shape[2] - x2_shape[2]

        h_diff_half = h_diff // 2
        w_diff_half = w_diff // 2

        down_layer_cropped = x1[:,
                                h_diff_half: (x1_shape[1] - h_diff_half),
                                w_diff_half: (x1_shape[2] - w_diff_half),
                                :]

        return tf.concat([down_layer_cropped, x2], axis=-1)

def crop_to_shape(X: tf.Tensor, shape: Tuple[int, int, int]) -> tf.Tensor:

    x_diff = X.shape[0] - shape[0]
    y_diff = X.shape[1] - shape[1]

    if x_diff == y_diff == 0:
        return X

    x_diff_half = x_diff // 2
    y_diff_half = y_diff // 2

    return X[x_diff_half:x_diff_half - x_diff,
             y_diff_half:y_diff_half - y_diff]

def crop_labels_to_shape(shape: Tuple[int, int, int]) -> Callable:
    def crop(img, label):
        return img, crop_to_shape(label, shape)
    return crop

def crop_image_labels_to_shape(shape: Tuple[int, int, int]) -> Callable:
    def crop(img, label):
        return crop_to_shape(img, shape), crop_to_shape(label, shape)
    return crop

max_stars_repo_name: phylsix/dOD

max_issues_repo_name: phylsix/dOD

max_forks_repo_name: phylsix/dOD


text:
import argparse

import numpy as np

from matplotlib import pyplot as plt

def main(FLAGS):

    some_data = np.random.rand(256, 256)

    print(FLAGS.data_dir)

    plt.matshow(some_data)

    plt.show()

if __name__ == '__main__':

    # Instantiates an arg parser
    parser = argparse.ArgumentParser()

    # Establishes default arguments
    parser.add_argument("--data_dir",
                        type=str,
                        default="C:\\my\\data\\path\\",
                        help="The complete desired input filepath.")

    # Parses known arguments
    FLAGS, unparsed = parser.parse_known_args()

    # Runs the tensorflow app
    main(FLAGS)

max_stars_repo_name: JustinFletcher/patterns

max_issues_repo_name: JustinFletcher/patterns

max_forks_repo_name: JustinFletcher/patterns


text:
import numpy as np
import cudf
from cuml.solvers import SGD as cumlSGD

X = cudf.DataFrame()
X['col1'] = np.array([1, 1, 2, 2], dtype=np.float32)
X['col2'] = np.array([1, 2, 2, 3], dtype=np.float32)

print("\n\n***** Running fit *****\n")
print("Input Dataframe:")
print(X)

y = cudf.Series(np.array([6.0, 8.0, 9.0, 11.0], dtype=np.float32))
print("Input Labels:")
print(y)
reg_sgd = cumlSGD(learning_rate='constant', eta0=0.005,
                  epochs=2000, fit_intercept=True,
                  batch_size=2, tol=0.0)
reg_sgd.fit(X, y)
reg = reg_sgd.fit(X, y)
print("Coefficients:")
print(reg.coef_)
print("intercept:")
print(reg.intercept_)

max_stars_repo_name: raydouglass/cuml

max_issues_repo_name: raydouglass/cuml

max_forks_repo_name: raydouglass/cuml


text:
import pandas as pd
import numpy as np

def get_data(filename):
    df = pd.read_csv(filename,delim_whitespace=True,names=['word','label'])
    beg_indices = list(df[df['word'] == 'BOS'].index)+[df.shape[0]]
    sents,labels,intents = [],[],[]
    for i in range(len(beg_indices[:-1])):
        sents.append(df[beg_indices[i]+1:beg_indices[i+1]-1]['word'].values)
        labels.append(df[beg_indices[i]+1:beg_indices[i+1]-1]['label'].values)
        intents.append(df.loc[beg_indices[i+1]-1]['label'])
    return np.array(sents, dtype=object), np.array(labels, dtype=object), np.array(intents, dtype=object)

def get_data2(filename):
    with open(filename) as f:
        contents = f.read()
    sents,labels,intents = [],[],[]
    for line in contents.strip().split('\n'):
        words,labs = [i.split(' ') for i in line.split('\t')]
        sents.append(words[1:-1])
        labels.append(labs[1:-1])
        intents.append(labs[-1])
    return np.array(sents, dtype=object), np.array(labels, dtype=object), np.array(intents, dtype=object)

read_method = {'Data/data2/atis-2.dev.w-intent.iob':get_data,
               'Data/data2/atis.train.w-intent.iob':get_data2,
               'Data/data2/atis.test.w-intent.iob':get_data,
              'Data/data2/atis-2.train.w-intent.iob':get_data2}

def fetch_data(fname):
    func = read_method[fname]
    return func(fname)


text:
from .BaseModel import BaseModel
from .Function import Function
from .utils import *
import numpy as np
import math

from sklearn.metrics import mean_squared_error
from scipy.optimize import curve_fit

class ExponentialModel(BaseModel):

    def __init__(self):
        pass

    def get_model_name(self):
        return "Exponential"

    def get_function_positive(self):
        return lambda x, a, b1, c: a * np.exp(b1*x) + c

    def get_function_negative(self):
        return lambda x, a, b1, c: a * np.exp(-b1 * x) + c

    def get_equation_string_positive(self, coef):
        return f"({round_sig(coef[0])}) \\cdot e^" + "{" + f"{round_sig(coef[1], 1)} \\cdot x" + "}" + " {:+g}".format(round(coef[2],1))

    def get_equation_string_negative(self, coef):
        # Weird bug that returns -ve coef[1]
        if coef[1] < 0:
            coef[1] = coef[1] * (- 1)
            return self.get_equation_string_positive(coef)

        return f"({round_sig(coef[0])}) \\cdot e^" + "{-" + f"{round_sig(coef[1], 1)} \\cdot x" + "}" + " {:+g}".format(round(coef[2],1))

    def get_best_fit(self, complexity_level, x0, y0):

        x0 = np.array(x0)
        y0 = np.array(y0)

        func_pos = self.get_function_positive()
        func_neg = self.get_function_negative()

        # Attempts both positive and negative exponential fitting
        try:
            coef_pos, _ = curve_fit(func_pos, x0, y0, maxfev=2000)
            y1_pos = func_pos(x0, *coef_pos.tolist())
            mse_pos = mean_squared_error(y0, y1_pos)
        except RuntimeError:
            return None

        try:
            coef_neg, _ = curve_fit(func_neg, x0, y0, maxfev=2000)
            y1_neg = func_neg(x0, *coef_neg.tolist())
            mse_neg = mean_squared_error(y0, y1_neg)
        except RuntimeError:
            return None

        # Returns the equation with less mean squared error
        if mse_pos < mse_neg:
            y1, mse = y1_pos, mse_pos
            equation_string = self.get_equation_string_positive(coef_pos)
        else:
            y1, mse = y1_neg, mse_neg
            equation_string = self.get_equation_string_negative(coef_neg)

        return Function(self.get_model_name(), complexity_level, f"y = {equation_string}", (x0, y1), round_sig(mse))

reason: import numpy,from scipy


text:
from nefct import nef_class
import numpy as np
import attr

__all__ = ('DetectorDirectAConfig', 'DetectorDirectBConfig', 'DetectorConfig')

@nef_class
class DetectorDirectAConfig:
    number: int
    size: float
    offset: float = attr.ib(default=0.0)

    @property
    def unit_size(self):
        return self.size / self.number

    @property
    def meshgrid(self):
        return (0.5 + np.arange(
            self.number)) * self.unit_size - self.size / 2 + self.offset

@nef_class
class DetectorDirectBConfig:
    number: int
    size: float
    offset: float = attr.ib(default=0.0)

    @property
    def unit_size(self):
        return self.size / self.number

    @property
    def meshgrid(self):
        return (0.5 + np.arange(
            self.number)) * self.unit_size - self.size / 2 + self.offset

@nef_class
class DetectorConfig:
    direct_a: DetectorDirectAConfig
    direct_b: DetectorDirectBConfig

    @property
    def shape(self):
        return [self.direct_a.number, self.direct_b.number]

    @property
    def size(self):
        return [self.direct_a.size, self.direct_b.size]

    @property
    def center(self):
        return [self.direct_a.offset, self.direct_b.offset]

    @property
    def unit_size(self):
        return [self.direct_a.unit_size, self.direct_b.unit_size]

    @property
    def meshgrid(self):
        return np.meshgrid(self.direct_a.meshgrid,
                           self.direct_b.meshgrid,
                           indexing='ij')


text:
# Copyright 2021 Huawei Technologies Co., Ltd
#
# 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.
# ============================================================================s
"""Loss Computation of Generator and Discriminator"""

import numpy as np

import mindspore
import mindspore.ops.operations as P
from mindspore import dtype as mstype
from mindspore import nn, Tensor, ops
from mindspore.ops import constexpr

class ClassificationLoss(nn.Cell):
    """Define classification loss for AttGAN"""

    def __init__(self):
        super().__init__()
        self.bce_loss = P.BinaryCrossEntropy(reduction='sum')

    def construct(self, pred, label):
        weight = ops.Ones()(pred.shape, mindspore.float32)
        pred_ = P.Sigmoid()(pred)
        x = self.bce_loss(pred_, label, weight) / pred.shape[0]
        return x

@constexpr
def generate_tensor(batch_size):
    np_array = np.random.randn(batch_size, 1, 1, 1)
    return Tensor(np_array, mindspore.float32)

class GradientWithInput(nn.Cell):
    """Get Discriminator Gradient with Input"""

    def __init__(self, discriminator):
        super().__init__()
        self.reduce_sum = ops.ReduceSum()
        self.discriminator = discriminator
        self.discriminator.set_train(mode=True)

    def construct(self, interpolates):
        decision_interpolate, _ = self.discriminator(interpolates)
        decision_interpolate = self.reduce_sum(decision_interpolate, 0)
        return decision_interpolate

class WGANGPGradientPenalty(nn.Cell):
    """Define WGAN loss for AttGAN"""

    def __init__(self, discriminator):
        super().__init__()
        self.gradient_op = ops.GradOperation()

        self.reduce_sum = ops.ReduceSum()
        self.reduce_sum_keep_dim = ops.ReduceSum(keep_dims=True)

        self.sqrt = ops.Sqrt()
        self.discriminator = discriminator
        self.GradientWithInput = GradientWithInput(discriminator)

    def construct(self, x_real, x_fake):
        """get gradient penalty"""
        batch_size = x_real.shape[0]
        alpha = generate_tensor(batch_size)
        alpha = alpha.expand_as(x_real)
        x_fake = ops.functional.stop_gradient(x_fake)
        x_hat = x_real + alpha * (x_fake - x_real)

        gradient = self.gradient_op(self.GradientWithInput)(x_hat)
        gradient_1 = ops.reshape(gradient, (batch_size, -1))
        gradient_1 = self.sqrt(self.reduce_sum(gradient_1 * gradient_1, 1))
        gradient_penalty = self.reduce_sum((gradient_1 - 1.0) ** 2) / x_real.shape[0]
        return gradient_penalty

class GenLoss(nn.Cell):
    """Define total Generator loss"""

    def __init__(self, args, generator, discriminator):
        super().__init__()
        self.generator = generator
        self.discriminator = discriminator

        self.lambda_1 = Tensor(args.lambda_1, mstype.float32)
        self.lambda_2 = Tensor(args.lambda_2, mstype.float32)
        self.lambda_3 = Tensor(args.lambda_3, mstype.float32)
        self.lambda_gp = Tensor(args.lambda_gp, mstype.float32)

        self.cyc_loss = P.ReduceMean()
        self.cls_loss = nn.BCEWithLogitsLoss()
        self.rec_loss = nn.L1Loss("mean")

    def construct(self, img_a, att_a, att_a_, att_b, att_b_):
        """Get generator loss"""
        # generate
        zs_a = self.generator(img_a, mode="enc")
        img_fake = self.generator(zs_a, att_b_, mode="dec")
        img_recon = self.generator(zs_a, att_a_, mode="dec")

        # discriminate
        d_fake, dc_fake = self.discriminator(img_fake)

        # generator loss
        gf_loss = - self.cyc_loss(d_fake)
        gc_loss = self.cls_loss(dc_fake, att_b)
        gr_loss = self.rec_loss(img_a, img_recon)

        g_loss = gf_loss + self.lambda_2 * gc_loss + self.lambda_1 * gr_loss

        return (img_fake, g_loss, gf_loss, gc_loss, gr_loss)

class DisLoss(nn.Cell):
    """Define total discriminator loss"""

    def __init__(self, args, generator, discriminator):
        super().__init__()
        self.generator = generator
        self.discriminator = discriminator

        self.cyc_loss = P.ReduceMean()
        self.cls_loss = nn.BCEWithLogitsLoss()
        self.WGANLoss = WGANGPGradientPenalty(discriminator)

        self.lambda_1 = Tensor(args.lambda_1, mstype.float32)
        self.lambda_2 = Tensor(args.lambda_2, mstype.float32)
        self.lambda_3 = Tensor(args.lambda_3, mstype.float32)
        self.lambda_gp = Tensor(args.lambda_gp, mstype.float32)

    def construct(self, img_a, att_a, att_a_, att_b, att_b_):
        """Get discriminator loss"""
        # generate
        img_fake = self.generator(img_a, att_b_, mode="enc-dec")

        # discriminate
        d_real, dc_real = self.discriminator(img_a)
        d_fake, _ = self.discriminator(img_fake)

        # discriminator losses
        d_real_loss = - self.cyc_loss(d_real)
        d_fake_loss = self.cyc_loss(d_fake)
        df_loss = d_real_loss + d_fake_loss

        df_gp = self.WGANLoss(img_a, img_fake)

        dc_loss = self.cls_loss(dc_real, att_a)

        d_loss = df_loss + self.lambda_gp * df_gp + self.lambda_3 * dc_loss

        return (d_loss, d_real_loss, d_fake_loss, dc_loss, df_gp)

max_stars_repo_name: leelige/mindspore

max_issues_repo_name: leelige/mindspore

max_forks_repo_name: leelige/mindspore


text:
"""
    Authors: The Vollab Developers 2004-2021
    License: BSD 3 clause
"""

import numpy as np

class HestonMonteCarlo:  # pylint: disable=too-many-instance-attributes
    """
    Monte Carlo method for generating samples from the Heston stochastic volatility model .
    """

    @staticmethod
    def create_with_constant_time_step(step_size=0.1,
                                       num_steps=50,
                                       num_paths=(4 * 2048),
                                       spot=50.0,
                                       drift=0.0,
                                       nu=0.0174,
                                       lmbda=1.3253,
                                       nu_bar=0.0354,
                                       eta=0.3877,
                                       rho=-0.7165):
        time_axis = np.linspace(0.0, num_steps * step_size, 1 + num_steps)
        return HestonMonteCarlo(time_axis, num_paths,
                                spot,
                                drift,
                                nu,
                                lmbda,
                                nu_bar,
                                eta,
                                rho)

    def __init__(self,  # pylint: disable=too-many-arguments
                 time_axis,
                 num_paths=(4 * 2048),
                 spot=50.0,
                 drift=0.0,
                 nu=0.0174,
                 lmbda=1.3253,
                 nu_bar=0.0354,
                 eta=0.3877,
                 rho=-0.7165):
        """
        Member-wise initialisation.

        Args:
            time_axis: The time axis.
            num_paths: The number of sample paths.
            spot: The starting point of the process.
            drift: The drift of the process.
            nu: The initial variance.
            lmbda: The mean-reversion speed of volatility.
            nu_bar: The long-run volatility.
            eta: The volatility of volatility.
            rho: The correlation between the process and it's volatility.
        """
        self.time_axis = time_axis
        self.num_paths = num_paths
        self.spot = spot
        self.drift = drift
        self.nu = nu  # pylint: disable=invalid-name
        self.lmbda = lmbda
        self.nu_bar = nu_bar
        self.eta = eta
        self.rho = rho
        self.norms = None

    def process_step(self, log_value, nu, norm, time_step):  # pylint: disable=invalid-name
        """
        Return the updated process value.

        Args:
            log_value: The current value of the process.
            nu: The current variance.
            norm: A standard random normal variate.
            time_step: The size of the times step.

        Returns:
            The updated process value.

        """
        return log_value + (self.drift - 0.5 * nu) * time_step + np.sqrt(nu * time_step) * norm

    def get_norms(self):
        """
        Get the vector of standard random normals used for each variance step.

        Returns:
            Normals used for process step.

        """
        return self.norms[:, 0].reshape([self.num_steps, self.num_paths])

    def get_initial_vol(self):
        """
        Get the initial volatility.

        Returns:
            The initial volatility.
        """
        return np.sqrt(self.nu)

    def variance_step(self, nu, norm, time_step):  # pylint: disable=invalid-name
        """
            Return the updated variance using Milstein method and reflecting condition.

        Args:
            nu: The variance.
            norm: A standard random normal variate.
            time_step: The size of the times step.

        Returns:
            The updated variance.
        """

        temp1 = np.sqrt(nu) + 0.5 * self.eta * np.sqrt(time_step) * norm
        temp2 = -1.0 * self.lmbda * (nu - self.nu_bar) * time_step
        temp3 = -0.25 * self.eta * self.eta * time_step
        nu_new = temp1 * temp1 + temp2 + temp3
        return np.where(np.less(nu_new, np.zeros([len(nu_new)])), -nu_new, nu_new)

    def sample(self):
        """
        Sample the Heston model.

        Returns:
            Samples paths of the process and the variance.
        """
        num_times = len(self.time_axis)
        num_steps = num_times - 1

        means = np.zeros([2])
        covs = np.zeros([2, 2])
        covs[0, 0] = 1.0
        covs[1, 1] = 1.0
        covs[0, 1] = self.rho
        covs[1, 0] = self.rho
        self.norms = np.random.multivariate_normal(means, covs, self.num_paths * num_steps)
        norms1 = self.norms[:, 1].reshape([num_steps, self.num_paths])
        norms2 = self.norms[:, 0].reshape([num_steps, self.num_paths])

        log_process = np.full([self.num_paths], np.log(self.spot))
        process = np.empty([num_times, self.num_paths])
        process[0, :] = self.spot
        variance = np.empty([num_times, self.num_paths])
        variance[0, :] = self.nu

        time_steps = np.diff(self.time_axis)

        for idx, time_step in enumerate(time_steps):
            log_process = self.process_step(log_process,
                                            variance[idx],
                                            norms2[idx],
                                            time_step)
            variance[1 + idx, :] = self.variance_step(variance[idx],
                                                      norms1[idx],
                                                      time_step)
            process[1 + idx, :] = np.exp(log_process)

        return process, variance

max_stars_repo_name: rexsutton/vollab

max_issues_repo_name: rexsutton/vollab

max_forks_repo_name: rexsutton/vollab


text:
# ========================================================================
#
# Imports
#
# ========================================================================
import argparse
import os
import numpy as np
import scipy.spatial.qhull as qhull
import pandas as pd
from mpi4py import MPI
import stk
import utilities

# ========================================================================
#
# Main
#
# ========================================================================
if __name__ == "__main__":

    # Parse arguments
    parser = argparse.ArgumentParser(
        description="An post-processing tool for a sideset"
    )
    parser.add_argument(
        "-m", "--mfile", help="Root name of files to postprocess", required=True
    )
    parser.add_argument("--auto_decomp", help="Auto-decomposition", action="store_true")
    parser.add_argument("-p", "--part", help="Part to post-process", required=True)
    args = parser.parse_args()

    fdir = os.path.dirname(args.mfile)
    pfx = os.path.splitext(os.path.basename(args.mfile))[0]

    comm = MPI.COMM_WORLD
    size = comm.Get_size()
    rank = comm.Get_rank()
    par = stk.Parallel.initialize()
    printer = utilities.p0_printer(par)

    mesh = stk.StkMesh(par)
    printer("Reading meta data for mesh: ", args.mfile)
    mesh.read_mesh_meta_data(args.mfile, auto_decomp=args.auto_decomp)
    printer("Done reading meta data")

    printer("Loading bulk data for mesh: ", args.mfile)
    mesh.populate_bulk_data()
    printer("Done reading bulk data")

    num_time_steps = mesh.stkio.num_time_steps
    max_time = mesh.stkio.max_time
    tsteps = mesh.stkio.time_steps
    printer(f"""Num. time steps = {num_time_steps}\nMax. time step  = {max_time}""")

    idf = pd.DataFrame(
        {
            "t": tsteps,
            "u": np.zeros(len(tsteps)),
            "tke": np.zeros(len(tsteps)),
            "sdr": np.zeros(len(tsteps)),
        }
    )
    for k, tstep in enumerate(tsteps):
        ftime, missing = mesh.stkio.read_defined_input_fields(tstep)
        printer(f"Loaded fields for time: {ftime}")

        coords = mesh.meta.coordinate_field

        # Extract fields at the parts
        m_part = mesh.meta.get_part(args.part)
        sel = m_part & mesh.meta.locally_owned_part
        fields = [
            mesh.meta.get_field("velocity"),
            mesh.meta.get_field("turbulent_ke"),
            mesh.meta.get_field("specific_dissipation_rate"),
        ]
        names = ["x", "y", "z", "u", "v", "w", "tke", "sdr"]

        optional_fields = {"beta": "k_ratio", "rk": "avg_res_adequacy_parameter"}
        for key, value in optional_fields.items():
            fld = mesh.meta.get_field(value)
            if not fld.is_null:
                fields.append(fld)
                names.append(key)

        cnt = 0
        nnodes = sum(bkt.size for bkt in mesh.iter_buckets(sel, stk.StkRank.NODE_RANK))
        data = np.zeros((nnodes, len(names)))
        for bkt in mesh.iter_buckets(sel, stk.StkRank.NODE_RANK):
            arr = coords.bkt_view(bkt)
            for fld in fields:
                vals = fld.bkt_view(bkt)
                if len(vals.shape) == 1:  # its a scalar
                    vals = vals.reshape(-1, 1)
                arr = np.hstack((arr, vals))
            data[cnt : cnt + bkt.size, :] = arr
            cnt += bkt.size

        lst = comm.gather(data, root=0)
        comm.Barrier()
        if rank == 0:
            df = pd.DataFrame(np.vstack(lst), columns=names)
            if tstep == tsteps[-1]:
                df.to_csv(os.path.join(fdir, f"{pfx}-f_{args.part}.dat"), index=False)
            means = df.groupby("y", as_index=False).mean().sort_values(by=["y"])
            Ly = df.y.max() - df.y.min()
            idf.iloc[k].u = np.trapz(means.u, means.y) / Ly
            idf.iloc[k].tke = np.trapz(means.tke, means.y) / Ly
            idf.iloc[k].sdr = np.trapz(means.sdr, means.y) / Ly

    if rank == 0:
        idf.to_csv(os.path.join(fdir, f"{pfx}-{args.part}.dat"), index=False)

max_stars_repo_name: marchdf/channelFlowHybrid

max_issues_repo_name: marchdf/channelFlowHybrid

max_forks_repo_name: marchdf/channelFlowHybrid

reason: import numpy,import scipy


text:
import tensorflow as tf
import numpy as np
import tensorflow_datasets as tfds
import pandas as pd

ds = tfds.load('malaria', split = 'train', shuffle_files = True)
assert isinstance(ds, tf.data.Dataset)
arr = tfds.as_numpy(ds)
images = []
labels = []
for item in arr:
    images.append(item['image'])
    labels.append(item['label'])
print(len(images))

images = np.array(images)
labels = np.array(labels)
np.savez('malaria_numpy', images = images, labels = labels)

max_stars_repo_name: ZovcIfzm/DeepHashing

max_issues_repo_name: ZovcIfzm/DeepHashing

max_forks_repo_name: ZovcIfzm/DeepHashing


text:
from os import path
import subprocess
import pandas as pd
import numpy as np
from plio.io.io_bae import read_gpf, save_gpf

def run_pc_align(args):
    """
    Use subprocess to call the external program, pc_align. Relies on
    pc_align to decide if arguments are valid or not.
    Pipe STDERR to STDOUT.

    Parameters
    ----------
    args : list
           list of arguments to be passed to pc_align

    """

    align_args = ["pc_align"]
    align_args.extend(args)
    run_align = subprocess.run(align_args,check=True,stderr=subprocess.STDOUT,encoding='utf-8')

    return run_align

def update_gpf(gpf_df,tp_df,outname,all_pts=False):
    """
    Update a GPF DataFrame with new lat/long/height values from another DataFrame,
    Change point types based on user input, and set sigmas of updated points == 1 meter.

    Parameters
    ----------
    gpf_df : pd.DataFrame
             Pandas DataFrame of a Socet GPF file. Format obtained from read_gpf(),
             and subsequently indexed on point_id field.

    tp_df : pd.DataFrame
            Pandas DataFrame of a Socet GPF file. Format obtained from read_gpf(),
            and subsequently indexed on point_id field. Should be (at least) a
            subset of gpf_df

    outname: str
                   Path to the output GPF

    all_pts : boolean
                 If True, update all active points in gpf_df, regardless of point type.
                 If False, update only active tiepoints in gpf_df, and then change
                     active non-tiepoints to tiepoints.

    """

    gpf_df.update(tp_df)

    ## Build boolean masks of gpf_df to enable selective updating of point types
    ## transformed tie point mask
    if all_pts:
        tfm_tp_mask = (gpf_df['stat'] == 1)
    else:
        ## non-tie point mask
        non_tp_mask = ((gpf_df['stat'] == 1) & (gpf_df['known'] > 0))
        tfm_tp_mask = ((gpf_df['stat'] == 1) & (gpf_df['known'] == 0))
        ## Change non-Tie Points to Tie Point
        gpf_df.loc[non_tp_mask, 'known'] = 0

    ## Change transformed Tie Points to XYZ Control, sigmas = 1.0, residuals = 0.0
    gpf_df.loc[tfm_tp_mask, 'known'] = 3
    gpf_df.loc[tfm_tp_mask, 'sig0':'sig2'] = 1.0
    gpf_df.loc[tfm_tp_mask, 'res0':'res2'] = 0.0

    ## Convert the 'stat' and 'known' columns to unsigned integers
    gpf_df.known = pd.to_numeric(gpf_df['known'], downcast = 'unsigned')
    gpf_df.stat = pd.to_numeric(gpf_df['stat'], downcast = 'unsigned')

    save_gpf(gpf_df, outname)

    return

def ascii_dtm2csv(ascii_dtm, outname):
    """
    Read an ASCII DTM from Socet Set into a pandas data frame,
    write out to CSV with latitude and longitude columns swapped
    to match the default format for pc_align

    Parameters
    ----------
    ascii_dtm : str
                path to the input ASCII DTM from Socet Set

    outname : str
              path to the output CSV

    """

    d = np.genfromtxt(ascii_dtm, skip_header=14, dtype='unicode')
    ref_df = pd.DataFrame(d, columns=["long","lat","z"])
    ref_df = ref_df.apply(pd.to_numeric)

    ## Extract lat/long/z and write out CSV. Note swapped lat/long columns
    ref_df.to_csv(path_or_buf=outname, header=False, index=False, columns=['lat','long','z'])

    return

max_stars_repo_name: dpmayerUSGS/APPL-Tools

max_issues_repo_name: dpmayerUSGS/APPL-Tools

max_forks_repo_name: dpmayerUSGS/APPL-Tools


text:
import sys

import numpy as np
import cv2

im = cv2.imread('sudoku.png')
im3 = im.copy()

gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray,(5,5),0)
thresh = cv2.adaptiveThreshold(blur,255,1,1,11,2)

#################      Now finding Contours         ###################

contours,hierarchy = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)

samples =  np.empty((0,100))
responses = []
keys = [i for i in range(48,58)]

for cnt in contours:
    if cv2.contourArea(cnt)>50 and cv2.contourArea(cnt)<1500:
        [x,y,w,h] = cv2.boundingRect(cnt)

        if  h<28:
            cv2.rectangle(im,(x,y),(x+w,y+h),(0,0,255),2)
            roi = thresh[y:y+h,x:x+w]
            roismall = cv2.resize(roi,(10,10))
            cv2.imshow('norm',im)
            key = cv2.waitKey(0)

            if key == 27:  # (escape to quit)
                sys.exit()
            elif key in keys:
                responses.append(int(chr(key)))
                sample = roismall.reshape((1,100))
                samples = np.append(samples,sample,0)

responses = np.array(responses,np.float32)
responses = responses.reshape((responses.size,1))
print "training complete"

np.savetxt('generalsamples.data',samples)
np.savetxt('generalresponses.data',responses)


text:
# coding: gbk
"""
@author: sdy
@email: sdy@epri.sgcc.com.cn

Active learning
"""

import numpy as np
import pandas as pd
import tensorflow as tf
from tensorflow.keras.models import load_model, model_from_json
from tensorflow.keras import backend as K
import datetime
import os
import sys

from model.ghnet_model import GHNet
from model.ghnet_util import dataset_predict
from data.ghnet_data import GHData

if __name__ == '__main__':

    path = "/home/sdy/python/db/2018_11"
    if os.name == 'nt':
        path = "d:/python/db/2018_11"

    all_types = ['cct', 'sst', 'vs', 'v_curve']
    res_type = 'cct'
    res_name = res_type
    res_path = path + "/" + res_name
    input_dic = {'generator': ['p', 'v'],
                 'station': ['pl', 'ql']}

    dr_percs = [0.5, 0.5, 0.5, 0.5, 0.5, 0.5]
    net_path = path + "/net"
    net = GHNet("inf", input_dic, dr_percs=dr_percs)
    net.load_net(net_path)
    net1 = GHNet("inf", input_dic, dr_percs=dr_percs)
    net1.load_net(net_path)

    data_set = GHData(path,
                      net_path,
                      net.input_layer)
    data_set.load_x(x_ratio_thr=-1.0)
    data_set.load_y(res_type)
    data_set.normalize()
    drops = np.array(range(len(data_set.column_valid)))[~data_set.column_valid]
    net.drop_inputs(drops)
    net1.drop_inputs(drops)

    n_batch = 16
    n_epochs = 10
    n_al_epochs = 10
    only_real = True
    y_columns = list(range(data_set.y.shape[1]))
    y_columns = [2, 11, 23]
    net.build_multi_reg_k(len(y_columns),
                          activation=tf.keras.layers.LeakyReLU())
    net1.build_multi_reg_k(len(y_columns),
                           activation=tf.keras.layers.LeakyReLU())
    data_set.split_dataset_random(train_perc=0.0,
                                  val_perc=0.1, notused_perc=0.9)
    data_set.make_dataset_tensors(y_columns=y_columns,
                                  only_real=only_real)
    val_sample_size = data_set.val_labels.shape[0]
    validation_steps = val_sample_size // n_batch
    val_gen = data_set.dataset_generator_multi(
        data_set.val_datas, data_set.val_labels, n_batch)

    for al in range(n_al_epochs):
        al_res = dataset_predict(net.pre_model, data_set, role=0)[:, 1]
        al_res = al_res.reshape((-1, len(y_columns)))
        al_res1 = dataset_predict(net1.pre_model, data_set, role=0)[:, 1]
        al_res1 = al_res1.reshape((-1, len(y_columns)))
        delta = np.abs(al_res - al_res1).mean(axis=1)
        al_as = delta.argsort()[::-1]
        new_train_num = min(np.sum(delta > 0.02), 200)
        idx = data_set.notused_indices[al_as[:new_train_num]]
        data_set.sample_prop.role[idx] = 1
        print("add %d new train samples." % new_train_num)
        data_set.make_dataset_tensors(y_columns=y_columns,
                                      only_real=only_real)

        train_sample_size = data_set.train_labels.shape[0]
        steps_per_epoch = train_sample_size // n_batch
        train_gen = data_set.dataset_generator_multi(
            data_set.train_datas, data_set.train_labels, n_batch)
        net.model.fit_generator(train_gen,
                                epochs=n_epochs,
                                steps_per_epoch=steps_per_epoch,
                                validation_data=val_gen,
                                validation_steps=validation_steps)
        net1.model.fit_generator(train_gen,
                                 epochs=n_epochs,
                                 steps_per_epoch=steps_per_epoch,
                                 validation_data=val_gen,
                                 validation_steps=validation_steps)
        val_res = dataset_predict(net.pre_model, data_set, role=2)
        val_res = val_res[np.isnan(val_res[:,0])==False]
        print("val error = ", np.abs(val_res[:, 2]).mean())

max_stars_repo_name: yarwyc/PowerAI

max_issues_repo_name: yarwyc/PowerAI

max_forks_repo_name: yarwyc/PowerAI


text:
###############################################################
#
# Copyright 2017 Sandia Corporation. Under the terms of
# Contract DE-AC04-94AL85000 with Sandia Corporation, the
# U.S. Government retains certain rights in this software.
# This software is distributed under the BSD-3-Clause license.
#
##############################################################

import numpy as np
import matplotlib.pyplot as plt

from turbulencekepspreprocessor import TurbulenceKEpsDataProcessor
from tbnn import NetworkStructure, TBNN

"""
In this example, a Tensor Basis Neural Network (TBNN) is trained on the data for a simple turbulent channel flow.
The inputs are the mean strain rate tensor and the mean rotation rate tensor (normalized by k and epsilon).
The output is the Reynolds stress anisotropy tensor.

Data:
The channel data set is based on a profile from the channel flow DNS of Moser et al. at Re_tau=590.
The tke, epsilon, and velocity gradient are from a k-epsilon RANS of the same flow.
The Reynolds stresses are from the DNS.

Reference for DNS data: Moser, R. D., Kim, J., & Mansour, N. N. (1999).
"Direct numerical simulation of turbulent channel flow up to Re= 590". Phys. Fluids, 11(4), 943-945.

Reference for data driven turbulence modeling TBNN implementation:
Ling, J., Kurzawski, A. and Templeton, J., 2016. Reynolds averaged turbulence modelling using deep neural
networks with embedded invariance. Journal of Fluid Mechanics, 807, pp.155-166.
"""

def load_channel_data():
    """
    Loads in channel flow data
    :return:
    """

    # Load in data from channel.txt
    data = np.loadtxt('channel.txt', skiprows=4)
    k = data[:, 0]
    eps = data[:, 1]
    grad_u_flat = data[:, 2:11]
    stresses_flat = data[:, 11:]

    # Reshape grad_u and stresses to num_points X 3 X 3 arrays
    num_points = data.shape[0]
    grad_u = np.zeros((num_points, 3, 3))
    stresses = np.zeros((num_points, 3, 3))
    for i in range(3):
        for j in range(3):
            grad_u[:, i, j] = grad_u_flat[:, i*3+j]
            stresses[:, i, j] = stresses_flat[:, i*3+j]
    return k, eps, grad_u, stresses

def plot_results(predicted_stresses, true_stresses):
    """
    Create a plot with 9 subplots.  Each subplot shows the predicted vs the true value of that
    stress anisotropy component.  Correct predictions should lie on the y=x line (shown with
    red dash).
    :param predicted_stresses: Predicted Reynolds stress anisotropy (from TBNN predictions)
    :param true_stresses: True Reynolds stress anisotropy (from DNS)
    """
    fig = plt.figure()
    fig.patch.set_facecolor('white')
    on_diag = [0, 4, 8]
    for i in range(9):
            plt.subplot(3, 3, i+1)
            ax = fig.gca()
            ax.set_aspect('equal')
            plt.plot([-1., 1.], [-1., 1.], 'r--')
            plt.scatter(true_stresses[:, i], predicted_stresses[:, i])
            plt.xlabel('True value')
            plt.ylabel('Predicted value')
            idx_1 = i / 3
            idx_2 = i % 3
            plt.title('A' + str(idx_1) + str(idx_2))
            if i in on_diag:
                plt.xlim([-1./3., 2./3.])
                plt.ylim([-1./3., 2./3.])
            else:
                plt.xlim([-0.5, 0.5])
                plt.ylim([-0.5, 0.5])
    plt.tight_layout()
    plt.show()

def main():
    # Define parameters:
    num_layers = 2  # Number of hidden layers in the TBNN
    num_nodes = 20  # Number of nodes per hidden layer
    max_epochs = 2000  # Max number of epochs during training
    min_epochs = 1000  # Min number of training epochs required
    interval = 100  # Frequency at which convergence is checked
    average_interval = 4  # Number of intervals averaged over for early stopping criteria
    split_fraction = 0.8  # Fraction of data to use for training
    enforce_realizability = True  # Whether or not we want to enforce realizability constraint on Reynolds stresses
    num_realizability_its = 5  # Number of iterations to enforce realizability
    seed = 12345 # use for reproducibility, set equal to None for no seeding

    # Load in data
    k, eps, grad_u, stresses = load_channel_data()

    # Calculate inputs and outputs
    data_processor = TurbulenceKEpsDataProcessor()
    Sij, Rij = data_processor.calc_Sij_Rij(grad_u, k, eps)
    x = data_processor.calc_scalar_basis(Sij, Rij, is_train=True)  # Scalar basis
    tb = data_processor.calc_tensor_basis(Sij, Rij, quadratic_only=False)  # Tensor basis
    y = data_processor.calc_output(stresses)  # Anisotropy tensor

    # Enforce realizability
    if enforce_realizability:
        for i in range(num_realizability_its):
            y = TurbulenceKEpsDataProcessor.make_realizable(y)

    # Split into training and test data sets
    if seed:
        np.random.seed(seed) # sets the random seed for Theano
    x_train, tb_train, y_train, x_test, tb_test, y_test = \
        TurbulenceKEpsDataProcessor.train_test_split(x, tb, y, fraction=split_fraction, seed=seed)

    # Define network structure
    structure = NetworkStructure()
    structure.set_num_layers(num_layers)
    structure.set_num_nodes(num_nodes)

    # Initialize and fit TBNN
    tbnn = TBNN(structure)
    tbnn.fit(x_train, tb_train, y_train, max_epochs=max_epochs, min_epochs=min_epochs, interval=interval, average_interval=average_interval)

    # Make predictions on train and test data to get train error and test error
    labels_train = tbnn.predict(x_train, tb_train)
    labels_test = tbnn.predict(x_test, tb_test)

    # Enforce realizability
    if enforce_realizability:
        for i in range(num_realizability_its):
            labels_train = TurbulenceKEpsDataProcessor.make_realizable(labels_train)
            labels_test = TurbulenceKEpsDataProcessor.make_realizable(labels_test)

    # Determine error
    rmse_train = tbnn.rmse_score(y_train, labels_train)
    rmse_test = tbnn.rmse_score(y_test, labels_test)
    print("RMSE on training data:", rmse_train)
    print("RMSE on test data:", rmse_test)

    # Plot the results
    plot_results(y_test, labels_test)

if __name__ == "__main__":
    main()

max_stars_repo_name: YuyangL/TBNN

max_issues_repo_name: YuyangL/TBNN

max_forks_repo_name: YuyangL/TBNN


text:
from __future__ import division
from __future__ import print_function

import argparse
import os
import sys
import time
from types import SimpleNamespace

import math
import numpy as np
import torch
import torch.optim as optim
from tqdm import tqdm

from models.pytorch.gnn_framework import GNN
from multitask_benchmark.util.util import load_dataset, total_loss, total_loss_multiple_batches, \
    specific_loss_multiple_batches

def build_arg_parser():
    """
    :return:    argparse.ArgumentParser() filled with the standard arguments for a training session.
                    Might need to be enhanced for some train_scripts.
    """
    parser = argparse.ArgumentParser()

    parser.add_argument('--data', type=str, default='../../data/multitask_dataset.pkl', help='Data path.')
    parser.add_argument('--no-cuda', action='store_true', default=False, help='Disables CUDA training.')
    parser.add_argument('--only_nodes', action='store_true', default=False, help='Evaluate only nodes labels.')
    parser.add_argument('--only_graph', action='store_true', default=False, help='Evaluate only graph labels.')
    parser.add_argument('--seed', type=int, default=42, help='Random seed.')
    parser.add_argument('--epochs', type=int, default=10000, help='Number of epochs to train.')
    parser.add_argument('--lr', type=float, default=0.003, help='Initial learning rate.')
    parser.add_argument('--weight_decay', type=float, default=1e-6, help='Weight decay (L2 loss on parameters).')
    parser.add_argument('--hidden', type=int, default=16, help='Number of hidden units.')
    parser.add_argument('--dropout', type=float, default=0.0, help='Dropout rate (1 - keep probability).')
    parser.add_argument('--patience', type=int, default=1000, help='Patience')
    parser.add_argument('--conv_layers', type=int, default=None, help='Graph convolutions')
    parser.add_argument('--variable_conv_layers', type=str, default='N', help='Graph convolutions function name')
    parser.add_argument('--fc_layers', type=int, default=3, help='Fully connected layers in readout')
    parser.add_argument('--loss', type=str, default='mse', help='Loss function to use.')
    parser.add_argument('--print_every', type=int, default=50, help='Print training results every')
    parser.add_argument('--final_activation', type=str, default='LeakyReLu',
                        help='final activation in both FC layers for nodes and S2S for Graph')
    parser.add_argument('--skip', action='store_true', default=False,
                        help='Whether to use the model with skip connections.')
    parser.add_argument('--gru', action='store_true', default=False,
                        help='Whether to use a GRU in the update function of the layers.')
    parser.add_argument('--fixed', action='store_true', default=False,
                        help='Whether to use the model with fixed middle convolutions.')
    parser.add_argument('--variable', action='store_true', default=False,
                        help='Whether to have a variable number of comvolutional layers.')
    return parser

# map from names (as passed as parameters) to function determining number of convolutional layers at runtime
VARIABLE_LAYERS_FUNCTIONS = {
    'N': lambda adj: adj.shape[1],
    'N/2': lambda adj: adj.shape[1] // 2,
    '4log2N': lambda adj: int(4 * math.log2(adj.shape[1])),
    '2log2N': lambda adj: int(2 * math.log2(adj.shape[1])),
    '3sqrtN': lambda adj: int(3 * math.sqrt(adj.shape[1]))
}

def execute_train(gnn_args, args):
    """
    :param gnn_args: the description of the model to be trained (expressed as arguments for GNN.__init__)
    :param args: the parameters of the training session
    """
    args.cuda = not args.no_cuda and torch.cuda.is_available()

    np.random.seed(args.seed)
    torch.manual_seed(args.seed)
    if args.cuda:
        torch.cuda.manual_seed(args.seed)

    device = 'cuda' if args.cuda else 'cpu'
    print('Using device:', device)

    # load data
    adj, features, node_labels, graph_labels = load_dataset(args.data, args.loss, args.only_nodes, args.only_graph,
                                                            print_baseline=True)

    # model and optimizer
    gnn_args = SimpleNamespace(**gnn_args)

    # compute avg_d on the training set
    if 'avg_d' in gnn_args.first_conv_descr['args'] or 'avg_d' in gnn_args.middle_conv_descr['args']:
        dlist = [torch.sum(A, dim=-1) for A in adj['train']]
        avg_d = dict(lin=sum([torch.mean(D) for D in dlist]) / len(dlist),
                     exp=sum([torch.mean(torch.exp(torch.div(1, D)) - 1) for D in dlist]) / len(dlist),
                     log=sum([torch.mean(torch.log(D + 1)) for D in dlist]) / len(dlist))
    if 'avg_d' in gnn_args.first_conv_descr['args']:
        gnn_args.first_conv_descr['args']['avg_d'] = avg_d
    if 'avg_d' in gnn_args.middle_conv_descr['args']:
        gnn_args.middle_conv_descr['args']['avg_d'] = avg_d

    gnn_args.device = device
    gnn_args.nfeat = features['train'][0].shape[2]
    gnn_args.nodes_out = node_labels['train'][0].shape[-1]
    gnn_args.graph_out = graph_labels['train'][0].shape[-1]
    if gnn_args.variable:
        assert gnn_args.conv_layers is None, "If model is variable, you shouldn't specify conv_layers (maybe you " \
                                             "meant variable_conv_layers?) "
    else:
        assert gnn_args.conv_layers is not None, "If the model is not variable, you should specify conv_layers"
    gnn_args.conv_layers = VARIABLE_LAYERS_FUNCTIONS[
        args.variable_conv_layers] if gnn_args.variable else args.conv_layers
    model = GNN(**vars(gnn_args))
    optimizer = optim.Adam(model.parameters(), lr=args.lr, weight_decay=args.weight_decay)

    pytorch_total_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
    print("Total params", pytorch_total_params)

    def move_cuda(dset):
        assert args.cuda, "Cannot move dataset on CUDA, running on cpu"
        if features[dset][0].is_cuda:
            # already on CUDA
            return
        features[dset] = [x.cuda() for x in features[dset]]
        adj[dset] = [x.cuda() for x in adj[dset]]
        node_labels[dset] = [x.cuda() for x in node_labels[dset]]
        graph_labels[dset] = [x.cuda() for x in graph_labels[dset]]

    if args.cuda:
        model.cuda()
        # move train, val to CUDA (delay moving test until needed)
        move_cuda('train')
        move_cuda('val')

    def train(epoch):
        """
        Execute a single epoch of the training loop

        :param epoch:int the number of the epoch being performed (0-indexed)
        """
        t = time.time()

        # train step
        model.train()
        for batch in range(len(adj['train'])):
            optimizer.zero_grad()
            output = model(features['train'][batch], adj['train'][batch])
            loss_train = total_loss(output, (node_labels['train'][batch], graph_labels['train'][batch]), loss=args.loss,
                                    only_nodes=args.only_nodes, only_graph=args.only_graph)
            loss_train.backward()
            optimizer.step()

        # validation epoch
        model.eval()
        output_zip = [model(features['val'][batch], adj['val'][batch]) for batch in range(len(adj['val']))]
        output = ([x[0] for x in output_zip], [x[1] for x in output_zip])

        loss_val = total_loss_multiple_batches(output, (node_labels['val'], graph_labels['val']), loss=args.loss,
                                               only_nodes=args.only_nodes, only_graph=args.only_graph)

        return loss_train.data.item(), loss_val

    def compute_test():
        """
        Evaluate the current model on all the sets of the dataset, printing results.
        This procedure is destructive on datasets.
        """
        model.eval()

        sets = list(features.keys())
        for dset in sets:
            # move data on CUDA if not already on it
            if args.cuda:
                move_cuda(dset)

            output_zip = [model(features[dset][batch], adj[dset][batch]) for batch in range(len(adj[dset]))]
            output = ([x[0] for x in output_zip], [x[1] for x in output_zip])
            loss_test = total_loss_multiple_batches(output, (node_labels[dset], graph_labels[dset]), loss=args.loss,
                                                    only_nodes=args.only_nodes, only_graph=args.only_graph)
            print("Test set results ", dset, ": loss= {:.4f}".format(loss_test))
            print(dset, ": ",
                  specific_loss_multiple_batches(output, (node_labels[dset], graph_labels[dset]), loss=args.loss,
                                                 only_nodes=args.only_nodes, only_graph=args.only_graph))

            # free unnecessary data
            del output_zip
            del output
            del loss_test
            del features[dset]
            del adj[dset]
            del node_labels[dset]
            del graph_labels[dset]
            torch.cuda.empty_cache()

    sys.stdout.flush()
    # Train model
    t_total = time.time()
    loss_values = []
    bad_counter = 0
    best = args.epochs + 1
    best_epoch = -1

    sys.stdout.flush()
    with tqdm(range(args.epochs), leave=True, unit='epoch') as t:
        for epoch in t:
            loss_train, loss_val = train(epoch)
            loss_values.append(loss_val)
            t.set_description('loss.train: {:.4f}, loss.val: {:.4f}'.format(loss_train, loss_val))
            if loss_values[-1] < best:
                # save current model
                torch.save(model.state_dict(), '{}.pkl'.format(epoch))
                # remove previous model
                if best_epoch >= 0:
                    os.remove('{}.pkl'.format(best_epoch))
                # update training variables
                best = loss_values[-1]
                best_epoch = epoch
                bad_counter = 0
            else:
                bad_counter += 1

            if bad_counter == args.patience:
                print('Early stop at epoch {} (no improvement in last {} epochs)'.format(epoch + 1, bad_counter))
                break

    print("Optimization Finished!")
    print("Total time elapsed: {:.4f}s".format(time.time() - t_total))

    # Restore best model
    print('Loading {}th epoch'.format(best_epoch + 1))
    model.load_state_dict(torch.load('{}.pkl'.format(best_epoch)))

    # Testing
    compute_test()

max_stars_repo_name: Michaelvll/pna

max_issues_repo_name: KonstantinKlepikov/pna

max_forks_repo_name: KonstantinKlepikov/pna


text:
# ---------------------------------------------------------
# IOU Tracker
# Copyright (c) 2019 TU Berlin, Communication Systems Group
# Licensed under The MIT License [see LICENSE for details]
# Written by Erik Bochinski
# ---------------------------------------------------------

import cv2
import numpy as np
from lapsolver import solve_dense
from tqdm import tqdm
from time import time

from .util import iou, load_mot
from .vis_tracker import VisTracker

def track_viou(frames_path, detections, sigma_l, sigma_h, sigma_iou, t_min, ttl, tracker_type, keep_upper_height_ratio):
    """ V-IOU Tracker.
    See "Extending IOU Based Multi-Object Tracking by Visual Information by E. Bochinski, T. Senst, T. Sikora" for
    more information.

    Args:
         frames_path (str): path to ALL frames.
                            string must contain a placeholder like {:07d} to be replaced with the frame numbers.
         detections (list): list of detections per frame, usually generated by util.load_mot
         sigma_l (float): low detection threshold.
         sigma_h (float): high detection threshold.
         sigma_iou (float): IOU threshold.
         t_min (float): minimum track length in frames.
         ttl (float): maximum number of frames to perform visual tracking.
                      this can fill 'gaps' of up to 2*ttl frames (ttl times forward and backward).
         tracker_type (str): name of the visual tracker to use. see VisTracker for more details.
         keep_upper_height_ratio (float): float between 0.0 and 1.0 that determines the ratio of height of the object
                                          to track to the total height of the object used for visual tracking.

    Returns:
        list: list of tracks.
    """
    if tracker_type == 'NONE':
        assert ttl == 1, "ttl should not be larger than 1 if no visual tracker is selected"

    tracks_active = []
    tracks_extendable = []
    tracks_finished = []
    frame_buffer = []

    for frame_num, detections_frame in enumerate(tqdm(detections), start=0):
        # load frame and put into buffer
        frame_path = frames_path.format(frame_num)
        frame = cv2.imread(frame_path)
        assert frame is not None, "could not read '{}'".format(frame_path)
        frame_buffer.append(frame)
        if len(frame_buffer) > ttl + 1:
            frame_buffer.pop(0)

        # apply low threshold to detections
        dets = [det for det in detections_frame if det['score'] >= sigma_l]

        track_ids, det_ids = associate(tracks_active, dets, sigma_iou)
        updated_tracks = []
        for track_id, det_id in zip(track_ids, det_ids):
            tracks_active[track_id]['bboxes'].append(dets[det_id]['bbox'])
            tracks_active[track_id]['max_score'] = max(tracks_active[track_id]['max_score'], dets[det_id]['score'])
            tracks_active[track_id]['classes'].append(dets[det_id]['class'])
            tracks_active[track_id]['det_counter'] += 1

            if tracks_active[track_id]['ttl'] != ttl:
                # reset visual tracker if active
                tracks_active[track_id]['ttl'] = ttl
                tracks_active[track_id]['visual_tracker'] = None

            updated_tracks.append(tracks_active[track_id])

        tracks_not_updated = [tracks_active[idx] for idx in set(range(len(tracks_active))).difference(set(track_ids))]

        for track in tracks_not_updated:
            if track['ttl'] > 0:
                if track['ttl'] == ttl:
                    # init visual tracker
                    track['visual_tracker'] = VisTracker(tracker_type, track['bboxes'][-1], frame_buffer[-2],
                                                         keep_upper_height_ratio)
                # viou forward update
                ok, bbox = track['visual_tracker'].update(frame)

                if not ok:
                    # visual update failed, track can still be extended
                    tracks_extendable.append(track)
                    continue

                track['ttl'] -= 1
                track['bboxes'].append(bbox)
                updated_tracks.append(track)
            else:
                tracks_extendable.append(track)

        # update the list of extendable tracks. tracks that are too old are moved to the finished_tracks. this should
        # not be necessary but may improve the performance for large numbers of tracks (eg. for mot19)
        tracks_extendable_updated = []
        for track in tracks_extendable:
            if track['start_frame'] + len(track['bboxes']) + ttl - track['ttl'] >= frame_num:
                tracks_extendable_updated.append(track)
            elif track['max_score'] >= sigma_h and track['det_counter'] >= t_min:
                tracks_finished.append(track)
        tracks_extendable = tracks_extendable_updated

        new_dets = [dets[idx] for idx in set(range(len(dets))).difference(set(det_ids))]
        dets_for_new = []

        for det in new_dets:
            finished = False
            # go backwards and track visually
            boxes = []
            vis_tracker = VisTracker(tracker_type, det['bbox'], frame, keep_upper_height_ratio)

            for f in reversed(frame_buffer[:-1]):
                ok, bbox = vis_tracker.update(f)
                if not ok:
                    # can not go further back as the visual tracker failed
                    break
                boxes.append(bbox)

                # sorting is not really necessary but helps to avoid different behaviour for different orderings
                # preferring longer tracks for extension seems intuitive, LAP solving might be better
                for track in sorted(tracks_extendable, key=lambda x: len(x['bboxes']), reverse=True):

                    offset = track['start_frame'] + len(track['bboxes']) + len(boxes) - frame_num
                    # association not optimal (LAP solving might be better)
                    # association is performed at the same frame, not adjacent ones
                    if 1 <= offset <= ttl - track['ttl'] and iou(track['bboxes'][-offset], bbox) >= sigma_iou:
                        if offset > 1:
                            # remove existing visually tracked boxes behind the matching frame
                            track['bboxes'] = track['bboxes'][:-offset+1]
                        track['bboxes'] += list(reversed(boxes))[1:]
                        track['bboxes'].append(det['bbox'])
                        track['max_score'] = max(track['max_score'], det['score'])
                        track['classes'].append(det['class'])
                        track['ttl'] = ttl
                        track['visual_tracker'] = None

                        tracks_extendable.remove(track)
                        if track in tracks_finished:
                            del tracks_finished[tracks_finished.index(track)]
                        updated_tracks.append(track)

                        finished = True
                        break
                if finished:
                    break
            if not finished:
                dets_for_new.append(det)

        # create new tracks
        new_tracks = [{'bboxes': [det['bbox']], 'max_score': det['score'], 'start_frame': frame_num, 'ttl': ttl,
                       'classes': [det['class']], 'det_counter': 1, 'visual_tracker': None} for det in dets_for_new]
        tracks_active = []
        for track in updated_tracks + new_tracks:
            if track['ttl'] == 0:
                tracks_extendable.append(track)
            else:
                tracks_active.append(track)

    # finish all remaining active and extendable tracks
    tracks_finished = tracks_finished + \
                      [track for track in tracks_active + tracks_extendable
                       if track['max_score'] >= sigma_h and track['det_counter'] >= t_min]

    # remove last visually tracked frames and compute the track classes
    for track in tracks_finished:
        if ttl != track['ttl']:
            track['bboxes'] = track['bboxes'][:-(ttl - track['ttl'])]
        track['class'] = max(set(track['classes']), key=track['classes'].count)

        del track['visual_tracker']

    return tracks_finished

def associate(tracks, detections, sigma_iou):
    """ perform association between tracks and detections in a frame.
    Args:
        tracks (list): input tracks
        detections (list): input detections
        sigma_iou (float): minimum intersection-over-union of a valid association

    Returns:
        (tuple): tuple containing:

        track_ids (numpy.array): 1D array with indexes of the tracks
        det_ids (numpy.array): 1D array of the associated indexes of the detections
    """
    costs = np.empty(shape=(len(tracks), len(detections)), dtype=np.float32)
    for row, track in enumerate(tracks):
        for col, detection in enumerate(detections):
            costs[row, col] = 1 - iou(track['bboxes'][-1], detection['bbox'])

    np.nan_to_num(costs)
    costs[costs > 1 - sigma_iou] = np.nan
    track_ids, det_ids = solve_dense(costs)
    return track_ids, det_ids

def track_viou_matlab_wrapper(frames_path, detections, sigma_l, sigma_h, sigma_iou, t_min, ttl, tracker_type,
                              keep_upper_height_ratio=1.):
    """
    Matlab wrapper of the v-iou tracker for the detrac evaluation toolkit.

    Args:
         detections (numpy.array): numpy array of detections, usually supplied by run_tracker.m
         sigma_l (float): low detection threshold.
         sigma_h (float): high detection threshold.
         sigma_iou (float): IOU threshold.
         t_min (float): minimum track length in frames.

    Returns:
        float: speed in frames per second.
        list: list of tracks.
    """

    detections = detections.reshape((7, -1)).transpose()
    dets = load_mot(detections, with_classes=False)
    start = time()
    tracks = track_viou(frames_path+"img{:05d}.jpg", dets, sigma_l, sigma_h, sigma_iou, int(t_min), int(ttl), tracker_type, keep_upper_height_ratio)
    end = time()

    id_ = 1
    out = []
    for track in tracks:
        for i, bbox in enumerate(track['bboxes']):
            out += [float(bbox[0]), float(bbox[1]), float(bbox[2] - bbox[0]), float(bbox[3] - bbox[1]),
                    float(track['start_frame'] + i), float(id_)]
        id_ += 1

    num_frames = len(dets)
    speed = num_frames / (end - start)

    return speed, out

max_stars_repo_name: justinkay/iou-tracker

max_issues_repo_name: justinkay/iou-tracker

max_forks_repo_name: justinkay/iou-tracker