text:
import numpy as np
import scipy.linalg as la
from matplotlib import pyplot as plt

class Bohemian:
    def __init__(self, n, U=None):
        self._size = n
        self._numberOfMatrixEntries = n*n
        if U is not None:
            self._matrix = np.zeros((n, n), dtype=type(U[0]))
            if self._numberOfMatrixEntries != len(U):
                raise ValueError('Expected list of length {}, got {} instead'.format(self._numberOfMatrixEntries, len(U)))
            k = 0
            for i in range(n):
                for j in range(n):
                    self._matrix[i, j] = U[k]
                    k += 1
        else:
            self._matrix = np.zeros((n, n), dtype=int)

    def makeMatrix(self, U):
        if self._numberOfMatrixEntries != len(U):
            raise ValueError('Expected list of length {}, got {} instead'.format(self._numberOfMatrixEntries, len(U)))
        if type(self._matrix[0, 0]) != type(U[0]):
            self._matrix = np.zeros((self._size, self._size), dtype = type(U[0]))
        k = 0
        for i in range(self._size):
            for j in range(self._size):
                self._matrix[i, j] = U[k]
                k += 1

    def resizeMatrix(self, n):
        self._size = n
        self._matrix = np.zeros((n, n), dtype=type(self._matrix[0,0]))

    def getMatrix(self):
        return(self._matrix)

    def getSize(self):
        return(self._size)

    def getNumberOfMatrixEntries(self):
        return(self._numberOfMatrixEntries)

    def characteristicPolynomial(self):
        # Use the method of Fadeev-Leverrier to compute
        # the characteristic polynomial.  Uses integer arithmetic
        # although numpy insists on floats sometimes.
        # This algorithm is inefficient with O(n^4) operations in general
        # and with the possibility of very large numbers in it anyway.
        # But it will work over anything that can multiply and integer divide.
        M = np.zeros((self._size, self._size), dtype=type(self._matrix[0,0]))
        # Rely on flint arithmetic
        c = np.zeros(self._size + 1, dtype=type(self._matrix[0,0]))
        c[self._size] = 1
        B = np.zeros((self._size, self._size), dtype=type(self._matrix[0,0])) # A.M = 0 b/c M=0
        for k in range(self._size):
            M = B + c[self._size-k]*np.identity(self._size)
            for i in range(self._size):
                for jay in range(self._size):
                    B[i,jay] = 0
                    for ell in range(self._size):
                        B[i,jay] += self._matrix[i,ell]*M[ell,jay]
            # Hard-coding matrix multiply
            c[self._size-k-1] = 0
            for i in range(self._size):
                c[self._size-k-1] += B[i,i]
            c[self._size-k-1] = -c[self._size-k-1]/(k+1) # Division must be exact; result might be "flint"
        return c

    def determinant(self):
        c = self.characteristicPolynomial()
        return (-1)**self._size*c[0]

    def eig(self):
        eigval, _ = la.eig(self._matrix)
        return(eigval)

    def plotEig(self):
        e = self.eig()
        x = e.real
        y = e.imag
        plt.scatter(x, y)
        plt.ylabel('Imaginary')
        plt.xlabel('Real')
        plt.show()

class Symmetric(Bohemian):
    def __init__(self, n, U=None):
        self._size = n
        self._numberOfMatrixEntries = n*(n+1)//2
        if U is not None:
            self._matrix = np.zeros((n, n), dtype=type(U[0]))
            if self._numberOfMatrixEntries != len(U):
                raise ValueError('Expected list of length {}, got {} instead'.format(self._numberOfMatrixEntries, len(U)))
            k = 0
            for i in range(n):
                for j in range(i, n):
                    self._matrix[i, j] = U[k]
                    k += 1
            for i in range(n):
                for j in range(i+1, n):
                    self._matrix[j, i] = self._matrix[i, j]
        else:
            self._matrix = np.zeros((n, n), dtype=int)

    def makeMatrix(self, U):
        if self._numberOfMatrixEntries != len(U):
            raise ValueError('Expected list of length {}, got {} instead'.format(self._numberOfMatrixEntries, len(U)))
        if type(self._matrix[0, 0]) != type(U[0]):
            self._matrix = np.zeros((self._size, self._size), dtype = type(U[0]))
        k = 0
        for i in range(self._size):
            for j in range(i, self._size):
                self._matrix[i, j] = U[k]
                k += 1
        for i in range(self._size):
            for j in range(i+1, self._size):
                self._matrix[j, i] = self._matrix[i, j]

class SkewSymmetric(Bohemian):
    def __init__(self, n, U=None):
        self._size = n
        self._numberOfMatrixEntries = n*(n-1)//2
        if U is not None:
            self._matrix = np.zeros((n, n), dtype=type(U[0]))
            if self._numberOfMatrixEntries != len(U):
                raise ValueError('Expected list of length {}, got {} instead'.format(self._numberOfMatrixEntries, len(U)))
            k = 0
            for i in range(n):
                for j in range(i+1, n):
                    self._matrix[i, j] = U[k]
                    k += 1
            for i in range(n):
                for j in range(i+1, n):
                    self._matrix[j, i] = -self._matrix[i, j]
        else:
            self._matrix = np.zeros((n, n), dtype=int)

    def makeMatrix(self, U):
        if self._numberOfMatrixEntries != len(U):
            raise ValueError('Expected list of length {}, got {} instead'.format(self._numberOfMatrixEntries, len(U)))
        if type(self._matrix[0, 0]) != type(U[0]):
            self._matrix = np.zeros((self._size, self._size), dtype = type(U[0]))
        k = 0
        for i in range(self._size):
            for j in range(i+1, self._size):
                self._matrix[i, j] = U[k]
                k += 1
        for i in range(self._size):
            for j in range(i+1, self._size):
                self._matrix[j, i] = -self._matrix[i, j]

# Add a class of skew-symmetric tridiagonal matrices.
class SkewSymmetricTridiagonal(Bohemian):
    def __init__(self, n, U=None):
        self._size = n
        self._numberOfMatrixEntries = n-1
        if U is not None:
            self._matrix = np.zeros((n, n), dtype=type(U[0]))
            if self._numberOfMatrixEntries != len(U):
                raise ValueError('Expected list of length {}, got {} instead'.format(self._numberOfMatrixEntries, len(U)))
            k = 0
            for i in range(n-1):
                self._matrix[i, i+1] = U[k]
                k += 1
            for i in range(n-1):
                self._matrix[i+1, i] = -self._matrix[i, i+1]
        else:
            self._matrix = np.zeros((n, n), dtype=int)

    def makeMatrix(self, U):
        if self._numberOfMatrixEntries != len(U):
            raise ValueError('Expected list of length {}, got {} instead'.format(self._numberOfMatrixEntries, len(U)))
        if type(self._matrix[0, 0]) != type(U[0]):
            self._matrix = np.zeros((self._size, self._size), dtype = type(U[0]))
        k = 0
        for i in range(self._size-1):
            self._matrix[i, i+1] = U[k]
            k += 1
        for i in range(self._size-1):
            self._matrix[i+1, i] = -self._matrix[i, i+1]

    def characteristicPolynomial(self):
        # This routine is special for skew-symmetric
        # tridiagonal matrices.  There is a fast recurrence
        # for computing characteristic polynomials for this class.
        # See R. M. Corless, https://doi.org/10.5206/mt.v1i2.14360
        # We here use numpy.polynomial.Polynomial arithmetic
        # to simulate symbolic computation.  What is returned
        # is an array of coefficients (which can later be cast
        # as a tuple or Polynomial, as desired)
        c = np.zeros(self._size + 1, dtype=type(self._matrix[0,0]))
        c[self._size] = 1
        p0 = np.polynomial.Polynomial([1])
        p1 = np.polynomial.Polynomial([0,1])
        mu = np.polynomial.Polynomial([0,1])
        for k in range(self._size-1):
            p = mu*p1 + self._matrix[k,k+1]**2*p0
            p0 = p1
            p1 = p
        # Leading coefficient is already 1.
        for k in range(self._size):
            c[k] = p.coef[k]
        return c

# Add a class of upper Hessenberg Toeplitz Zero Diagonal matrices with -1 on the subdiagonal
# This code has been minimally tested, but has passed those tests.
class UHTZD(Bohemian):
    def __init__(self, n, U=None):
        self._size = n
        self._numberOfMatrixEntries = n-1
        if U is not None:
            self._matrix = np.zeros((n, n), dtype=type(U[0]))
            if self._numberOfMatrixEntries != len(U):
                raise ValueError('Expected list of length {}, got {} instead'.format(self._numberOfMatrixEntries, len(U)))
            k = 0
            for j in range(1,n):
                self._matrix[0, j] = U[k]
                k += 1
                for i in range(1,n-j):
                    self._matrix[i,j+i] = self._matrix[0,j]
            for i in range(n-1):
                self._matrix[i+1, i] = -1
        else:
            self._matrix = np.zeros((n, n), dtype=int)

    def makeMatrix(self, U):
        if self._numberOfMatrixEntries != len(U):
            raise ValueError('Expected list of length {}, got {} instead'.format(self._numberOfMatrixEntries, len(U)))
        if type(self._matrix[0, 0]) != type(U[0]):
            self._matrix = np.zeros((self._size, self._size), dtype = type(U[0]))
        k = 0
        for j in range(1,self._size):
            self._matrix[0, j] = U[k]
            k += 1
            for i in range(1,self._size-j):
                self._matrix[i,j+i] = self._matrix[0,j]
        for i in range(self._size-1):
            self._matrix[i+1, i] = -1

    def characteristicPolynomial(self):
        # This routine is special for upper Hessenberg Toeplitz
        # zero diagonal matrices.  There is a fast recurrence
        # for computing characteristic polynomials for this class.
        # P(n) = mu*P(n - 1) + add((-1)^k*t[k]*P(n - k), k = 1 .. n)
        # See https://doi.org/10.1016/j.laa.2020.03.037
        # We here use numpy.polynomial.Polynomial arithmetic
        # to simulate symbolic computation.  What is returned
        # is an array of coefficients (which can later be cast
        # as a tuple or Polynomial, as desired)
        c = np.zeros(self._size + 1, dtype=type(self._matrix[0,0]))
        c[self._size] = 1
        plist = []
        plist.append( np.polynomial.Polynomial([1]) )
        plist.append( np.polynomial.Polynomial([0,1]) )
        mu = np.polynomial.Polynomial([0,1])
        for n in range(2,self._size+1):
            s = 0
            for j in range(1,n):
                 s += (-1)**j*self._matrix[0,j]*plist[n-j-1]
            p = mu*plist[n-1] - s
            plist.append( p )
        # So many opportunities for sign errors and off-by-one errors :(
        for k in range(self._size):
            c[k] = plist[self._size].coef[k]
        return c

# Add a class of Unit upper Hessenberg matrices with 1 on the subdiagonal
class UnitUpperHessenberg(Bohemian):
    def __init__(self, n, U=None):
        self._size = n
        self._numberOfMatrixEntries = n*(n+1)//2
        if U is not None:
            self._matrix = np.zeros((n, n), dtype=type(U[0]))
            if self._numberOfMatrixEntries != len(U):
                raise ValueError('Expected list of length {}, got {} instead'.format(self._numberOfMatrixEntries, len(U)))
            k = 0
            for i in range(n):
                for j in range(i,n):
                    self._matrix[i, j] = U[k]
                    k += 1
            for i in range(n-1):
                self._matrix[i+1, i] = 1
        else:
            self._matrix = np.zeros((n, n), dtype=int)

    def makeMatrix(self, U):
        if self._numberOfMatrixEntries != len(U):
            raise ValueError('Expected list of length {}, got {} instead'.format(self._numberOfMatrixEntries, len(U)))
        if type(self._matrix[0, 0]) != type(U[0]):
            self._matrix = np.zeros((self._size, self._size), dtype = type(U[0]))
        k = 0
        for i in range(self._size):
            for j in range(i,self._size):
                self._matrix[i, j] = U[k]
                k += 1
        for i in range(self._size-1):
            self._matrix[i+1, i] = 1

    def characteristicPolynomial(self):
        # This routine is special for unit upper Hessenberg matrices.
        # The cost is O(n^2)
        # for computing characteristic polynomials for this class.
        # P(m) = mu*P(m - 1) - add(h[k,m-1]*P(k), k = 0 .. m-1)
        # See https://doi.org/10.1016/j.laa.2020.03.037
        # We here use numpy.polynomial.Polynomial arithmetic
        # to simulate symbolic computation.  What is returned
        # is an array of coefficients (which can later be cast
        # as a tuple or Polynomial, as desired)
        c = np.zeros(self._size + 1, dtype=type(self._matrix[0,0]))
        c[self._size] = 1
        plist = []
        plist.append( np.polynomial.Polynomial([1]) )
        plist.append( np.polynomial.Polynomial([-self._matrix[0,0],1]) )
        mu = np.polynomial.Polynomial([0,1])
        for m in range(2,self._size+1):
            s = 0
            for i in range(m):
                 s += self._matrix[i,m-1]*plist[i]
            p = mu*plist[m-1] - s
            plist.append( p )
        #
        for k in range(self._size):
            c[k] = plist[self._size].coef[k]
        return c

# # Executable part of the code
# U = [-1, 1, -1, -1, 0, 1, 1, 1, -1, 1, 1, 1, 0, 0, 1, 0]
# A = Bohemian(4, U)
# M = A.getMatrix()
# print('Matrix:\n', M)
# print('Number of Matrix Entries:', A.getNumberOfMatrixEntries())
# print('Characteristic Polynomial:', A.characteristicPolynomial())
# print('Determinant:', A.determinant())
# print('Eigenvalues:', A.eig())
#
# print(' ')
#
# U = [-1, 1, -1, -1, 0, 1, 1, 1, -1, 1]
# A = Symmetric(4, U)
# M = A.getMatrix()
# print('Matrix:\n', M)
# print('Number of Matrix Entries:', A.getNumberOfMatrixEntries())
# print('Characteristic Polynomial:', A.characteristicPolynomial())
# print('Determinant:', A.determinant())
# print('Eigenvalues:', A.eig())

ext: py

reason: import numpy,import scipy


text:
import bpy, os, sys, time
import numpy as np
from mathutils import *

file_dir = os.path.dirname(__file__)
sys.path.append(file_dir)
from materials_cycles_converter import *

# Utility function to separate list of floats into tuples.
def extract_parameters(parameters):
	location = (parameters[0], - parameters[2], parameters[1])
	direction = (parameters[3], - parameters[5], parameters[4])
	up = (parameters[6], - parameters[8], parameters[7])
	return [location, direction, up]

# Read file of camera extrinsics and intrinsics.
def read_camera_file(path):
	with open(path) as camera_file:
		return [extract_parameters([float(x) for x in line.split()]) for line in camera_file]

# Adapted from https://blender.stackexchange.com/questions/5210/pointing-the-camera-in-a-particular-direction-programmatically
def look_at_euler(camera, direction):
	world_camera = camera.matrix_world.to_translation()
	rot_quat = Vector(direction).to_track_quat("-Z", "Y")
	return rot_quat.to_euler()

# Add a camera to scene if one does not already exist.
def add_camera():
	scene = bpy.context.scene
	if scene.camera is None:
		bpy.ops.object.camera_add(view_align = False, enter_editmode = False)
		scene.camera = bpy.data.objects["Camera"]

# Set camera location and view direction.
def set_camera(location, direction):
	scene = bpy.context.scene
	camera = scene.camera
	camera.location = location
	camera.rotation_euler = look_at_euler(camera, direction)

# Add Z buffer output node and link to render layer.
def setup_z_buffer():
	scene = bpy.context.scene
	scene.use_nodes = True

	nodes = scene.node_tree.nodes

	render_layers = nodes.new("CompositorNodeRLayers")
	z_buffer = nodes.new("CompositorNodeOutputFile")
	#z_buffer.use_alpha = False
	z_buffer.format.file_format = "OPEN_EXR"

	scene.node_tree.links.new(
		render_layers.outputs["Depth"],
		z_buffer.inputs[0]
	)

	return z_buffer

# Save Z buffer as Numpy file.
def save_z_buffer(width, height, filename):
	blender_z_data = bpy.data.images["Viewer Node"].pixels
	z_data = np.array(blender_z_data)
	print(z_data.shape)
	np.save(filename, z_data)

# Render scene.
def render(name = "output", path = "//render", up = (0, 1, 0), z_buffer = None, width = 1024, height = 512, tile_size = 512, samples = 500):
	scene = bpy.context.scene

	# Set output resolution and tile sizes.
	scene.render.resolution_x = width
	scene.render.resolution_y = height
	scene.render.tile_x = tile_size
	scene.render.tile_y = tile_size
	scene.render.image_settings.file_format = "JPEG"
	# scene.render.use_render_cache = True
	scene.render.layers.active.cycles.use_denoising = True

	# Settings for Cycles renderer to be faster.
	bpy.context.user_preferences.addons["cycles"].preferences.compute_device_type = "CUDA"
	for device in bpy.context.user_preferences.addons["cycles"].preferences.devices:
		device.use = False

	if len(sys.argv) > 10:
		device_index = int(sys.argv[10])
	else:
		device_index = 0

	bpy.context.user_preferences.addons["cycles"].preferences.devices[device_index].use = True
	scene.cycles.device = "GPU"
	scene.cycles.samples = samples
	scene.cycles.caustics_reflective = False
	scene.cycles.caustics_refractive = False
	scene.cycles.min_bounces = 2
	scene.cycles.max_bounces = 4

	# Set up 360 degree lens.
	camera = scene.camera.data
	camera.type = "PANO"
	camera.cycles.panorama_type = "EQUIRECTANGULAR"

	# Turn off stereo for 2D capture.
	scene.render.use_multiview = False
	z_buffer.base_path = ""
	z_buffer.file_slots[0].path = os.path.join(path, "depth_top", name + ".exr")

	scene.render.filepath = os.path.join(path, "top", name)
	bpy.ops.render.render(write_still = True)
	os.rename(os.path.join(path, "depth_top", name + ".exr0001.exr"), os.path.join(path, "depth_top", name + ".exr"))

	#save_z_buffer(width, height, os.path.join(path, "depth", name))

	scene.camera.location = scene.camera.location - 0.25 * Vector(up)

	z_buffer.file_slots[0].path = os.path.join(path, "depth_bottom", name + ".exr")

	scene.render.filepath = os.path.join(path, "bottom", name)
	bpy.ops.render.render(write_still = True)
	os.rename(os.path.join(path, "depth_bottom", name + ".exr0001.exr"), os.path.join(path, "depth_bottom", name + ".exr"))

	scene.camera.location = scene.camera.location + 0.25 * Vector(up)

	# Turn on stereo for 3D capture.
	# scene.render.use_multiview = True
	# camera.stereo.use_spherical_stereo = True

	# scene.render.filepath = path + "/3d/" + name
	# bpy.ops.render.render(write_still = True)

# Get render output arguments.
render_path = sys.argv[6]
name = sys.argv[7]

# Blender project or SUNCG.
if len(sys.argv) < 9:
	mode = "blender"
else:
	mode = sys.argv[8]

if mode == "blender":
	# Just render the scene if it is a Blender project.
	render(name = name, path = render_path)
elif mode == "suncg":
	# Convert materials to Cycles nodes, and add light, then render
	# for each camera.
	suncg_path = sys.argv[9]

	# Load scene.
	bpy.ops.import_scene.obj(filepath = suncg_path + "/"  + name + "/house.obj")
	AutoNode()

	# Add camera to scene.
	add_camera()

	# Load camera parameters.
	print("Reading camera file: " + suncg_path + "/../cameras/"  + name + "/room_camera.txt")
	if os.path.exists(suncg_path + "/../cameras/"  + name + "/room_camera.txt"):
		cameras = read_camera_file(suncg_path + "/../cameras/"  + name + "/room_camera.txt")
	else:
		cameras = []

	# Setup Z buffer.
	z_buffer = setup_z_buffer()

	# Render for each camera.
	index = 0
	for parameters in cameras:
		location = parameters[0]
		direction = parameters[1]
		up = parameters[2]
		set_camera(location, direction)
		rendered = False
		while not rendered:
			try:
				render(name = name + "_{}".format(index), path = render_path, up = up, z_buffer = z_buffer)
				rendered = True
			except BaseException as error:
				print(error)
				print("Failed to render. Will try again in 120 seconds.")
				time.sleep(120)
		index = index + 1

reason: import numpy


text:
import cv2
import numpy as np

from sklearn.model_selection import train_test_split
from pandas.io.parsers import read_csv

def find_all_dataset(basePath,correction=0.25):
    lines = read_csv(basePath + '/driving_log.csv').values
    images = []
    angles = []
    for line in lines:
        angle = float(line[3])
        # Center image
        images.append(basePath + '/' + line[0].strip())
        angles.append(angle)
        # Left image
        images.append(basePath + '/' + line[1].strip())
        angles.append(angle + correction)
        # Right image
        images.append(basePath + '/' + line[2].strip())
        angles.append(angle - correction)

    dataset = list(zip(images,angles))
    print(len(dataset))

    train_dataset, valid_dataset = train_test_split(dataset,test_size=0.2)

    return train_dataset,valid_dataset

def new_random_brightness_image(image):
    image1 = cv2.cvtColor(image,cv2.COLOR_RGB2HSV)
    image1 = np.array(image1, dtype = np.float64)
    random_bright = .5+np.random.uniform()
    image1[:,:,2] = image1[:,:,2]*random_bright
    image1[:,:,2][image1[:,:,2]>255]  = 255
    image1 = np.array(image1, dtype = np.uint8)
    image1 = cv2.cvtColor(image1,cv2.COLOR_HSV2RGB)
    return image1

def shift_image(image, angle, move=150):
    # Translation
    rx = move * np.random.uniform(low=-0.5,high=0.5)
    ry = 10 * np.random.uniform(low=-0.5,high=0.5)
    M = np.float32([[1, 0, rx], [0, 1, ry]])

    image1 = cv2.warpAffine(image, M, (image.shape[1], image.shape[0]))
    angle1 = angle + rx / move * 2 * 0.2

    return image1, angle1

def add_random_shadow_image(image):
    '''
    https://chatbotslife.com/using-augmentation-to-mimic-human-driving-496b569760a9
    '''
    top_y = 320*np.random.uniform()
    top_x = 0
    bot_x = 160
    bot_y = 320*np.random.uniform()
    image_hls = cv2.cvtColor(image,cv2.COLOR_RGB2HLS)
    shadow_mask = 0*image_hls[:,:,1]
    X_m = np.mgrid[0:image.shape[0],0:image.shape[1]][0]
    Y_m = np.mgrid[0:image.shape[0],0:image.shape[1]][1]

    shadow_mask[((X_m-top_x)*(bot_y-top_y) -(bot_x - top_x)*(Y_m-top_y) >=0)]=1
    if np.random.randint(2) == 1:
        random_bright = .5
        cond1 = (shadow_mask == 1)
        cond0 = (shadow_mask == 0)
        if np.random.randint(2) == 1:
            image_hls[:,:,1][cond1] = image_hls[:,:,1][cond1]*random_bright
        else:
            image_hls[:,:,1][cond0] = image_hls[:,:,1][cond0]*random_bright
    image1 = cv2.cvtColor(image_hls,cv2.COLOR_HLS2RGB)
    return image1

def augment_data(image,angle):
    '''
    https://chatbotslife.com/using-augmentation-to-mimic-human-driving-496b569760a9
    '''
    images = []
    angles = []

    # Flipping
    images.append(cv2.flip(image,1))
    angles.append(angle * -1.0)

    # Brightness
    images.append(new_random_brightness_image(image))
    angles.append(angle)

    # Translation for driving on slop
    image1, angle1 = shift_image(image, angle, move=150)
    images.append(image1)
    angles.append(angle1)

    # shadow
    image2 = add_random_shadow_image(image)
    images.append(image2)
    angles.append(angle)

    return images,angles

def augment_data_single(image,angle):
    '''
    https://chatbotslife.com/using-augmentation-to-mimic-human-driving-496b569760a9
    '''

    images = []
    angles = []

    # Brightness
    img = new_random_brightness_image(image)

    # Translation for driving on slop
    img, angle1 = shift_image(img, angle, move=150)

    # shadow
    img = add_random_shadow_image(img)

    # Flipping
    flip = np.random.randint(2)
    if flip == 0:
        img = cv2.flip(img, 1)
        angle1 = angle1 * -1.0

    images.append(img)
    angles.append(angle1)

    return images, angles


text:
# Copyright (c) Facebook, Inc. and its affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

import logging
from typing import Dict, List, Optional, Tuple

import numpy as np

import torch
import torch.nn as nn
from dataclasses import dataclass, field
from fairseq import utils
from fairseq.data.data_utils import compute_mask_indices
from fairseq.data.dictionary import Dictionary
from fairseq.dataclass import ChoiceEnum, FairseqDataclass
from fairseq.models import BaseFairseqModel, register_model
from fairseq.models.wav2vec.wav2vec2 import (
    ConvFeatureExtractionModel,
    TransformerEncoder,
)
from fairseq.modules import GradMultiply, LayerNorm
from fairseq.tasks.hubert_pretraining import (
    HubertPretrainingConfig,
    HubertPretrainingTask,
)
from omegaconf import II

logger = logging.getLogger(__name__)

EXTRACTOR_MODE_CHOICES = ChoiceEnum(["default", "layer_norm"])
MASKING_DISTRIBUTION_CHOICES = ChoiceEnum(["static", "uniform", "normal", "poisson"])

@dataclass
class HubertConfig(FairseqDataclass):
    label_rate: int = II("task.label_rate")

    extractor_mode: EXTRACTOR_MODE_CHOICES = field(
        default="default",
        metadata={
            "help": "mode for feature extractor. default has a single group "
            "norm with d groups in the first conv block, whereas layer_norm "
            "has layer norms in every block (meant to use with normalize=True)"
        },
    )
    encoder_layers: int = field(
        default=12, metadata={"help": "num encoder layers in the transformer"}
    )
    encoder_embed_dim: int = field(
        default=768, metadata={"help": "encoder embedding dimension"}
    )
    encoder_ffn_embed_dim: int = field(
        default=3072, metadata={"help": "encoder embedding dimension for FFN"}
    )
    encoder_attention_heads: int = field(
        default=12, metadata={"help": "num encoder attention heads"}
    )
    activation_fn: ChoiceEnum(utils.get_available_activation_fns()) = field(
        default="gelu", metadata={"help": "activation function to use"}
    )

    # dropouts
    dropout: float = field(
        default=0.1,
        metadata={"help": "dropout probability for the transformer"},
    )
    attention_dropout: float = field(
        default=0.1,
        metadata={"help": "dropout probability for attention weights"},
    )
    activation_dropout: float = field(
        default=0.0,
        metadata={"help": "dropout probability after activation in FFN"},
    )
    encoder_layerdrop: float = field(
        default=0.0,
        metadata={"help": "probability of dropping a tarnsformer layer"},
    )
    dropout_input: float = field(
        default=0.0,
        metadata={"help": "dropout to apply to the input (after feat extr)"},
    )
    dropout_features: float = field(
        default=0.0,
        metadata={"help": "dropout to apply to the features (after feat extr)"},
    )

    final_dim: int = field(
        default=0,
        metadata={
            "help": "project final representations and targets to this many "
            "dimensions. set to encoder_embed_dim is <= 0"
        },
    )
    untie_final_proj: bool = field(
        default=False,
        metadata={"help": "use separate projection for each target"},
    )
    layer_norm_first: bool = field(
        default=False,
        metadata={"help": "apply layernorm first in the transformer"},
    )
    conv_feature_layers: str = field(
        default="[(512,10,5)] + [(512,3,2)] * 4 + [(512,2,2)] * 2",
        metadata={
            "help": "string describing convolutional feature extraction "
            "layers in form of a python list that contains "
            "[(dim, kernel_size, stride), ...]"
        },
    )
    conv_bias: bool = field(
        default=False, metadata={"help": "include bias in conv encoder"}
    )
    logit_temp: float = field(
        default=0.1, metadata={"help": "temperature to divide logits by"}
    )
    target_glu: bool = field(
        default=False, metadata={"help": "adds projection + glu to targets"}
    )
    feature_grad_mult: float = field(
        default=1.0,
        metadata={"help": "multiply feature extractor var grads by this"},
    )

    # masking
    mask_length: int = field(default=10, metadata={"help": "mask length"})
    mask_prob: float = field(
        default=0.65,
        metadata={"help": "probability of replacing a token with mask"},
    )
    mask_selection: MASKING_DISTRIBUTION_CHOICES = field(
        default="static", metadata={"help": "how to choose mask length"}
    )
    mask_other: float = field(
        default=0,
        metadata={
            "help": "secondary mask argument "
            "(used for more complex distributions), "
            "see help in compute_mask_indicesh"
        },
    )
    no_mask_overlap: bool = field(
        default=False, metadata={"help": "whether to allow masks to overlap"}
    )
    mask_min_space: int = field(
        default=1,
        metadata={"help": "min space between spans (if no overlap is enabled)"},
    )

    # channel masking
    mask_channel_length: int = field(
        default=10,
        metadata={"help": "length of the mask for features (channels)"},
    )
    mask_channel_prob: float = field(
        default=0.0,
        metadata={"help": "probability of replacing a feature with 0"},
    )
    mask_channel_selection: MASKING_DISTRIBUTION_CHOICES = field(
        default="static",
        metadata={"help": "how to choose mask length for channel masking"},
    )
    mask_channel_other: float = field(
        default=0,
        metadata={
            "help": "secondary mask argument "
            "(used for more complex distributions), "
            "see help in compute_mask_indicesh"
        },
    )
    no_mask_channel_overlap: bool = field(
        default=False,
        metadata={"help": "whether to allow channel masks to overlap"},
    )
    mask_channel_min_space: int = field(
        default=1,
        metadata={"help": "min space between spans (if no overlap is enabled)"},
    )

    # positional embeddings
    conv_pos: int = field(
        default=128,
        metadata={"help": "number of filters for convolutional positional embeddings"},
    )
    conv_pos_groups: int = field(
        default=16,
        metadata={"help": "number of groups for convolutional positional embedding"},
    )

    latent_temp: Tuple[float, float, float] = field(
        default=(2, 0.5, 0.999995),
        metadata={"help": "legacy (to be removed)"},
    )

    # loss computation
    skip_masked: bool = field(
        default=False,
        metadata={"help": "skip computing losses over masked frames"},
    )
    skip_nomask: bool = field(
        default=False,
        metadata={"help": "skip computing losses over unmasked frames"},
    )

@register_model("hubert", dataclass=HubertConfig)
class HubertModel(BaseFairseqModel):
    def __init__(
        self,
        cfg: HubertConfig,
        task_cfg: HubertPretrainingConfig,
        dictionaries: List[Dictionary],
    ) -> None:
        super().__init__()
        logger.info(f"HubertModel Config: {cfg}")

        feature_enc_layers = eval(cfg.conv_feature_layers)  # noqa
        self.embed = feature_enc_layers[-1][0]

        self.feature_extractor = ConvFeatureExtractionModel(
            conv_layers=feature_enc_layers,
            dropout=0.0,
            mode=cfg.extractor_mode,
            conv_bias=cfg.conv_bias,
        )
        feature_ds_rate = np.prod([s for _, _, s in feature_enc_layers])
        self.feat2tar_ratio = cfg.label_rate * feature_ds_rate / task_cfg.sample_rate

        self.post_extract_proj = (
            nn.Linear(self.embed, cfg.encoder_embed_dim)
            if self.embed != cfg.encoder_embed_dim
            else None
        )

        self.mask_prob = cfg.mask_prob
        self.mask_selection = cfg.mask_selection
        self.mask_other = cfg.mask_other
        self.mask_length = cfg.mask_length
        self.no_mask_overlap = cfg.no_mask_overlap
        self.mask_min_space = cfg.mask_min_space

        self.mask_channel_prob = cfg.mask_channel_prob
        self.mask_channel_selection = cfg.mask_channel_selection
        self.mask_channel_other = cfg.mask_channel_other
        self.mask_channel_length = cfg.mask_channel_length
        self.no_mask_channel_overlap = cfg.no_mask_channel_overlap
        self.mask_channel_min_space = cfg.mask_channel_min_space

        self.dropout_input = nn.Dropout(cfg.dropout_input)
        self.dropout_features = nn.Dropout(cfg.dropout_features)

        self.feature_grad_mult = cfg.feature_grad_mult
        self.logit_temp = cfg.logit_temp
        self.skip_masked = cfg.skip_masked
        self.skip_nomask = cfg.skip_nomask

        final_dim = cfg.final_dim if cfg.final_dim > 0 else cfg.encoder_embed_dim

        self.mask_emb = nn.Parameter(
            torch.FloatTensor(cfg.encoder_embed_dim).uniform_()
        )

        self.encoder = TransformerEncoder(cfg)
        self.layer_norm = LayerNorm(self.embed)

        self.target_glu = None
        if cfg.target_glu:
            self.target_glu = nn.Sequential(
                nn.Linear(final_dim, final_dim * 2), nn.GLU()
            )

        self.untie_final_proj = cfg.untie_final_proj
        if self.untie_final_proj:
            self.final_proj = nn.Linear(
                cfg.encoder_embed_dim, final_dim * len(dictionaries)
            )
        else:
            self.final_proj = nn.Linear(cfg.encoder_embed_dim, final_dim)

        # modules below are not needed during fine-tuning
        if any([d is None for d in dictionaries]):
            logger.info("cannot find dictionary. assume will be used for fine-tuning")
        else:
            self.num_classes = [len(d) for d in dictionaries]
            self.label_embs_concat = nn.Parameter(
                torch.FloatTensor(sum(self.num_classes), final_dim)
            )
            nn.init.uniform_(self.label_embs_concat)

    def upgrade_state_dict_named(self, state_dict, name):
        """Upgrade a (possibly old) state dict for new versions of fairseq."""

        super().upgrade_state_dict_named(state_dict, name)
        return state_dict

    @classmethod
    def build_model(cls, cfg: HubertConfig, task: HubertPretrainingTask):
        """Build a new model instance."""

        model = HubertModel(cfg, task.cfg, task.dictionaries)
        return model

    def apply_mask(self, x, padding_mask, target_list):
        B, T, C = x.shape
        if self.mask_prob > 0:
            mask_indices = compute_mask_indices(
                (B, T),
                padding_mask,
                self.mask_prob,
                self.mask_length,
                self.mask_selection,
                self.mask_other,
                min_masks=2,
                no_overlap=self.no_mask_overlap,
                min_space=self.mask_min_space,
            )
            mask_indices = torch.from_numpy(mask_indices).to(x.device)
            x[mask_indices] = self.mask_emb
        else:
            mask_indices = None

        if self.mask_channel_prob > 0:
            mask_channel_indices = compute_mask_indices(
                (B, C),
                None,
                self.mask_channel_prob,
                self.mask_channel_length,
                self.mask_channel_selection,
                self.mask_channel_other,
                no_overlap=self.no_mask_channel_overlap,
                min_space=self.mask_channel_min_space,
            )
            mask_channel_indices = (
                torch.from_numpy(mask_channel_indices)
                .to(x.device)
                .unsqueeze(1)
                .expand(-1, T, -1)
            )
            x[mask_channel_indices] = 0

        return x, mask_indices

    def compute_nce(self, x, pos, negs):
        neg_is_pos = (pos == negs).all(-1)
        pos = pos.unsqueeze(0)
        targets = torch.cat([pos, negs], dim=0)

        logits = torch.cosine_similarity(x.float(), targets.float(), dim=-1).type_as(x)
        logits /= self.logit_temp
        if neg_is_pos.any():
            logits[1:][neg_is_pos] = float("-inf")
        logits = logits.transpose(0, 1)  # (num_x, num_cls+1)
        return logits

    def forward_features(self, source: torch.Tensor) -> torch.Tensor:
        if self.feature_grad_mult > 0:
            features = self.feature_extractor(source)
            if self.feature_grad_mult != 1.0:
                features = GradMultiply.apply(features, self.feature_grad_mult)
        else:
            with torch.no_grad():
                features = self.feature_extractor(source)
        return features

    def forward_targets(
        self,
        features: torch.Tensor,
        target_list: List[torch.Tensor],
    ) -> Tuple[torch.Tensor, torch.Tensor]:
        # Trim features to ensure labels exist and then get aligned labels
        feat_tsz = features.size(2)
        targ_tsz = min([t.size(1) for t in target_list])
        if self.feat2tar_ratio * feat_tsz > targ_tsz:
            feat_tsz = int(targ_tsz / self.feat2tar_ratio)
            features = features[..., :feat_tsz]
        target_inds = torch.arange(feat_tsz).float() * self.feat2tar_ratio
        target_list = [t[:, target_inds.long()] for t in target_list]
        return features, target_list

    def forward_padding_mask(
        self,
        features: torch.Tensor,
        padding_mask: torch.Tensor,
    ) -> torch.Tensor:
        extra = padding_mask.size(1) % features.size(1)
        if extra > 0:
            padding_mask = padding_mask[:, :-extra]
        padding_mask = padding_mask.view(padding_mask.size(0), features.size(1), -1)
        padding_mask = padding_mask.all(-1)
        return padding_mask

    def forward(
        self,
        source: torch.Tensor,
        target_list: Optional[List[torch.Tensor]] = None,
        padding_mask: Optional[torch.Tensor] = None,
        mask: bool = True,
        features_only: bool = False,
        output_layer: Optional[int] = None,
    ) -> Dict[str, torch.Tensor]:
        """output layer is 1-based"""
        features = self.forward_features(source)
        if target_list is not None:
            features, target_list = self.forward_targets(features, target_list)

        features_pen = features.float().pow(2).mean()

        features = features.transpose(1, 2)
        features = self.layer_norm(features)
        unmasked_features = features.clone()

        if padding_mask is not None:
            padding_mask = self.forward_padding_mask(features, padding_mask)

        if self.post_extract_proj is not None:
            features = self.post_extract_proj(features)

        features = self.dropout_input(features)
        unmasked_features = self.dropout_features(unmasked_features)

        if mask:
            x, mask_indices = self.apply_mask(features, padding_mask, target_list)
        else:
            x = features
            mask_indices = None

        # feature: (B, T, D), float
        # target: (B, T), long
        # x: (B, T, D), float
        # padding_mask: (B, T), bool
        # mask_indices: (B, T), bool
        x, _ = self.encoder(
            x,
            padding_mask=padding_mask,
            layer=None if output_layer is None else output_layer - 1,
        )

        if features_only:
            return {"x": x, "padding_mask": padding_mask, "features": features}

        def compute_pred(proj_x, target, label_embs):
            # compute logits for the i-th label set
            y = torch.index_select(label_embs, 0, target.long())
            negs = label_embs.unsqueeze(1).expand(-1, proj_x.size(0), -1)
            if self.target_glu:
                y = self.target_glu(y)
                negs = self.target_glu(negs)
            # proj_x: (S, D)
            # y: (S, D)
            # negs: (Neg, S, D)
            return self.compute_nce(proj_x, y, negs)

        label_embs_list = self.label_embs_concat.split(self.num_classes, 0)

        if not self.skip_masked:
            masked_indices = torch.logical_and(~padding_mask, mask_indices)
            proj_x_m = self.final_proj(x[masked_indices])
            if self.untie_final_proj:
                proj_x_m_list = proj_x_m.chunk(len(target_list), dim=-1)
            else:
                proj_x_m_list = [proj_x_m for _ in range(len(target_list))]
            logit_m_list = [
                compute_pred(proj_x_m, t[masked_indices], label_embs_list[i])
                for i, (proj_x_m, t) in enumerate(zip(proj_x_m_list, target_list))
            ]
        else:
            logit_m_list = [None for _ in target_list]

        if not self.skip_nomask:
            nomask_indices = torch.logical_and(~padding_mask, ~mask_indices)
            proj_x_u = self.final_proj(x[nomask_indices])
            if self.untie_final_proj:
                proj_x_u_list = proj_x_u.chunk(len(target_list), dim=-1)
            else:
                proj_x_u_list = [proj_x_u for _ in range(len(target_list))]

            logit_u_list = [
                compute_pred(proj_x_u, t[nomask_indices], label_embs_list[i])
                for i, (proj_x_u, t) in enumerate(zip(proj_x_u_list, target_list))
            ]
        else:
            logit_u_list = [None for _ in target_list]

        result = {
            "logit_m_list": logit_m_list,
            "logit_u_list": logit_u_list,
            "padding_mask": padding_mask,
            "features_pen": features_pen,
        }
        return result

    def extract_features(
        self,
        source: torch.Tensor,
        padding_mask: Optional[torch.Tensor] = None,
        mask: bool = False,
        ret_conv: bool = False,
        output_layer: Optional[int] = None,
    ) -> Tuple[torch.Tensor, torch.Tensor]:
        res = self.forward(
            source,
            padding_mask=padding_mask,
            mask=mask,
            features_only=True,
            output_layer=output_layer,
        )
        feature = res["features"] if ret_conv else res["x"]
        return feature, res["padding_mask"]

    def get_logits(self, net_output, is_masked=True):
        if is_masked:
            logits_list = net_output["logit_m_list"]
        else:
            logits_list = net_output["logit_u_list"]
        logits_list = [x.float() for x in logits_list if x is not None]
        return logits_list

    def get_targets(self, net_output, is_masked=True):
        logits_list = self.get_logits(net_output, is_masked)
        targets_list = [x.new_zeros(x.size(0), dtype=torch.long) for x in logits_list]
        return targets_list

    def get_extra_losses(self, net_output):
        extra_losses = []
        names = []

        if "features_pen" in net_output:
            extra_losses.append(net_output["features_pen"])
            names.append("features_pen")

        return extra_losses, names

    def remove_pretraining_modules(self):
        self.target_glu = None
        self.final_proj = None

max_stars_repo_name: Mu-Y/fairseq

max_issues_repo_name: Mu-Y/fairseq

max_forks_repo_name: Mu-Y/fairseq


text:
# -*- coding: utf-8 -*-

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import codecs
import yaml
import numpy as np
import tensorflow as tf

from tensorflow import gfile
from seq2seq.data.vocab import create_vocabulary_lookup_table
from seq2seq.data import vocab, input_pipeline
from seq2seq.training import utils as training_utils
from seq2seq.test import utils as test_utils
from seq2seq.models import BasicSeq2Seq, AttentionSeq2Seq, CopyGenSeq2Seq
from seq2seq.data.vocab import Vocab

def t(file_path, default_value=None):
  x = create_vocabulary_lookup_table(file_path)
  print(x)
  return x

from seq2seq.graph_utils import templatemethod

class test_property:

  def __init__(self, x):
    self._x = x

  @property
  @templatemethod("trial")
  def trial(self):
      print("call trial here")
      w = tf.get_variable('w', [])
      return tf.reduce_sum(self._x) * w

def my_trail(x, share_variable_name):
  var1 = tf.get_variable(share_variable_name, shape=[])
  return tf.reduce_sum(x) * var1

template_my = tf.make_template("template_my", my_trail, share_variable_name="my_v")

def test_model(source_path, target_path, vocab_path):

  tf.logging.set_verbosity(tf.logging.INFO)
  batch_size = 2

  # Build model graph
  mode = tf.contrib.learn.ModeKeys.TRAIN
  params_ = AttentionSeq2Seq.default_params().copy()
  params_.update({
      "vocab_source": vocab_path,
      "vocab_target": vocab_path,
    }
  )
  model = AttentionSeq2Seq( params = params_, mode = mode)

  tf.logging.info(vocab_path)

  input_pipeline_ = input_pipeline.ParallelTextInputPipeline(
    params={
      "source_files":[source_path],
      "target_files":[target_path]
    },
    mode=mode
  )
  input_fn = training_utils.create_input_fn(
    pipeline=input_pipeline_, batch_size=batch_size)
  features, labels = input_fn()
  fetches = model(features, labels, None)

  fetches = [_ for _ in fetches if _ is not None]

  with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    sess.run(tf.local_variables_initializer())
    sess.run(tf.tables_initializer())
    with tf.contrib.slim.queues.QueueRunners(sess):
      fetches_ = sess.run(fetches)

  return model, fetches_

TEST_PARAMS = yaml.load("""
embedding.dim: 5
encoder.params:
  rnn_cell:
    dropout_input_keep_prob: 0.8
    num_layers: 2
    residual_connections: True,
    cell_class: LSTMCell
    cell_params:
      num_units: 4
decoder.params:
  rnn_cell:
    num_layers: 2
    cell_class: LSTMCell
    cell_params:
      num_units: 4
""")

def test_copy_gen_model(record_path, vocab_path=None):

  tf.logging.set_verbosity(tf.logging.INFO)

  vocab = Vocab(vocab_path)
  batch_size = 2

  # Build model graph
  mode = tf.contrib.learn.ModeKeys.TRAIN
  params_ = CopyGenSeq2Seq.default_params().copy()
  params_.update(TEST_PARAMS)
  params_.update({
      "vocab_source": vocab_path,
      "vocab_target": vocab_path,
    }
  )
  print(params_)

  model = CopyGenSeq2Seq( params = params_, mode = mode, vocab_instance=vocab)

  tf.logging.info(vocab_path)

  input_pipeline_ = input_pipeline.FeaturedTFRecordInputPipeline(
    params={
      "files":[record_path],
      "shuffle": True
    },
    mode=mode
  )
  input_fn = training_utils.create_input_fn(
    pipeline=input_pipeline_, batch_size=batch_size)
  features, labels = input_fn()
  fetches = model(features, labels, None)
  fetches = [_ for _ in fetches if _ is not None]
  from tensorflow.python import debug as tf_debug

  with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    sess.run(tf.local_variables_initializer())
    sess.run(tf.tables_initializer())
    with tf.contrib.slim.queues.QueueRunners(sess):
      # sess = tf_debug.LocalCLIDebugWrapperSession(sess)
      fetches_ = sess.run(fetches)
      print("yes")

  return model, fetches_

if __name__ == "__main__":

  vocab_path = "/home/bigdata/active_project/test_seq2seq_py2/yard_seq2seq/q2q_sim_95/data/vocab/shared.vocab.txt"
  source_path = "/home/bigdata/active_project/test_seq2seq_py2/yard_seq2seq/q2q_sim_95/data/debug/sources.txt"
  target_path = "/home/bigdata/active_project/test_seq2seq_py2/yard_seq2seq/q2q_sim_95/data/debug/targets.txt"
  record_path = "/home/bigdata/active_project/test_seq2seq_py2/yard_seq2seq/q2q_sim_95/data/debug/debug.tfrecord"
  from os.path import join
  test_copy_gen_model(record_path,
                     vocab_path=vocab_path)

  # test_model(source_path, target_path, vocab_path)


text:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

normal = pd.read_excel('C:/Users/arpic/tumor100.xlsx', sheet_name='tumor', skiprows = [0,1045])
tumor = pd.read_excel('C:/Users/arpic/tumor100.xlsx', sheet_name='normal', skiprows = [0,1045])

x1 = normal.values[:,0]
y1 = normal.values[:,15]

x2 = tumor.values[:,0]
y2 = tumor.values[:, 15]

x3 = normal.values[:, 0]
y3 = normal.values[:,10]
x4 = tumor.values[:, 0]
y4 = tumor.values[:, 10]

"""print(x1,y1)
x1_line = x1.copy()
y1_line = np.ones(len(x_line)) * max(y1) / 2

print(x2,y2)
x2_line = x2.copy()
y2_line - np.ones(len(x2_line)) * max(y2) / 2

new_y1=y1-y1_line
n1Len=len(x1)
x1zero=np.zeros((n1Len,))
y1zero=np.zeros((n1Len,))

new_y2=y2-y2_line
n2Len=len(x2)
x2zero=np.zeros((n2Len,))
y2zero=np.zeros((n2Len,))"""

"""for i in range(nLen-1):
    if np.dot(y[i], y[i+1]) == 0:
        if y[i]==0:
            xzero[i]=i
            yzero[i]=0
        if y[i+1] == 0:
            xzero[i+1]=i+1
            yzero[i+1]=0
    elif np.dot(y[i],y[i+1]) < 0:
        yzero[i] = np.dot(abs(y[i]) * y_line[i+1] + abs(y[i+1])*y_line[i], 1/(abs(y[i+1])+abs(y[i])))
        xzero[i] = x_line[i]
    else:
        pass
n = []
for i in range(nLen):
    if xzero[i] != 0:
        n.append(xzero[i])
        print(n)
#print(n[1]-n[0])"""

plt.plot(x1,y1, 'co')
plt.xlabel("Wavelength for Normal Vals (15)")
plt.ylabel("Intensity for Normal Vals (15)")
plt.show()

plt.plot(x2,y2, 'mo')
plt.xlabel("Wavelength for Tumor  Vals (15)")
plt.ylabel("Intensity for Tumor Vals (15)")
plt.show()

plt.plot(x3, y3, 'yo')
plt.xlabel("Wavelength for Normal Vals (10) ")
plt.ylabel("Intensity for Normal Vals (10)")
plt.show()

plt.plot(x4, y4, 'go')
plt.xlabel("Wavelength for Tumor Vals (10)")
plt.ylabel("Intensity for Tumor Vals (10)")
plt.show()

max_stars_repo_name: ornaminty/work-for-internship-

max_issues_repo_name: ornaminty/work-for-internship-

max_forks_repo_name: ornaminty/work-for-internship-


text:
from typing import Union

import numpy as np
from scipy.sparse import isspmatrix, spmatrix
from sklearn.metrics import silhouette_score

# Convert sparse matrix to dense matrix.
to_dense_matrix = lambda X: np.array(X.todense()) if isspmatrix(X) else np.asarray(X)

def ecp_silhouette(
    matrix: Union[np.ndarray, spmatrix],
    cluster_labels: np.ndarray,
) -> float:
    """
    Here we evaluate the clustering performance by calculating the Silhouette Coefficient.
    The silhouette analysis is used to choose an optimal value for clustering resolution.

    The Silhouette Coefficient is a widely used method for evaluating clustering performance,
    where a higher Silhouette Coefficient score relates to a model with better defined clusters and
    indicates a good separation between the celltypes.

    Advantages of the Silhouette Coefficient:
        * The score is bounded between -1 for incorrect clustering and +1 for highly dense clustering.
          Scores around zero indicate overlapping clusters.
        * The score is higher when clusters are dense and well separated,
          which relates to a standard concept of a cluster.

    Original Code Repository: https://scikit-learn.org/stable/modules/clustering.html#silhouette-coefficient

    Args:
        matrix: A dense or sparse matrix of feature.
        cluster_labels: A array of labels for each cluster.

    Returns:
        Mean Silhouette Coefficient for all clusters.

    Examples:
        >>> silhouette_score(matrix=adata.obsm["X_pca"], cluster_labels=adata.obs["leiden"].values)
    """
    return silhouette_score(matrix, cluster_labels, metric="euclidean")

reason: import numpy,from scipy


text:
## ทำนายพลังงานการกลายเป็นอะตอม (atomization energy) ของโมเลกุลด้วย QM 7 dataset

import numpy as np
import tensorflow as tf
# print(tf.__version__)
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt

# 1. Load dataset
dataset = np.load('dataset/qm7.npz')
print(dataset.files)
input = dataset['input']
output = dataset['output']

print(f"shape of input: {input.shape}")
print(f"shape of output: {output.shape}")
# split dataset ==> training set & test set
X_train, X_test, y_train, y_test = train_test_split(input, output, test_size=0.2, random_state=42)
print(f"shape of X_train: {X_train.shape}")
print(f"shape of X_test: {X_test.shape}")
print(f"shape of y_train: {y_train.shape}")
print(f"shape of y_test: {y_test.shape}")
print("")

# 2. Design and build model
## Sequential API
# model = tf.keras.Sequential([
#     tf.keras.layers.Input(shape=(621,)),            # input layer
#     tf.keras.layers.Dense(256, activation='relu'),  # hidden layer
#     tf.keras.layers.Dense(256, activation='relu'),  # hidden layer
#     tf.keras.layers.Dense(256, activation='relu'),  # hidden layer
#     tf.keras.layers.Dense(1)
# ])

## Functional API
input = tf.keras.Input(shape=(621,)) # input layer
hidden = tf.keras.layers.Dense(256, activation='relu')(input)  # 1st hidden layer
hidden = tf.keras.layers.Dense(256, activation='relu')(hidden)  # 2nd hidden layer
hidden = tf.keras.layers.Dense(256, activation='relu')(hidden)  # 3rd hidden layer
output = tf.keras.layers.Dense(1)(hidden)

model = tf.keras.Model(input, output, name='FunctionalAPI')

# 3. Compile model
model.compile(optimizer='adam', loss=tf.keras.losses.mean_squared_error)

model.summary()

# 4. Fit model
history = model.fit(
    X_train,
    y_train,
    epochs=100
)
model.save('model/demo_100epochs_relu_3hidden.h5')

# 5. Check loss
def plot_loss(history):
    plt.plot(history.history['loss'], label='loss')
    plt.xlabel('Epoch')
    plt.ylabel('Error [Energy]')
    plt.legend()
    plt.grid(True)
    plt.show()

plot_loss(history)

# 6. Prediction
## x1, x2, x2, ... ==> f(x)
## f(x) ==> y
y_predict = model.predict(X_test).flatten()
print(y_predict.shape)
print(y_predict)

def plot_pred(y_test, y_predict):
    plt.axes(aspect='equal')
    plt.scatter(y_test, y_predict, s=1, c='b')
    plt.xlabel('True values [energy]')
    plt.ylabel('Prediction [energy]')
    lims = [-2300, -500]
    plt.xlim(lims)
    plt.ylim(lims)
    plt.plot(lims, lims, c='r')
    plt.show()

plot_pred(y_test, y_predict)

# 7. Calculate MSE and RMSE
error = tf.keras.metrics.mean_squared_error(y_test, y_predict)
print(f"MSE  : {error}")
print(f"RMSE : {tf.sqrt(error)}")


text:
from hydroDL.data import dbBasin
import json
import os
from hydroDL import kPath
import numpy as np
import pandas as pd

sd = '1982-01-01'
ed = '2018-12-31'
dataName = 'test'
dictSiteName = 'dict{}.json'.format(dataName)
dirSel = os.path.join(kPath.dirData, 'USGS', 'inventory', 'siteSel')
with open(os.path.join(dirSel, dictSiteName)) as f:
    dictSite = json.load(f)
siteNoLst = dictSite['rmTK'][:10]
# DF = dbBasin.DataFrameBasin.new(dataName, siteNoLst, sdStr=sd, edStr=ed)
DF = dbBasin.DataFrameBasin(dataName)

# normalization
DFN = dbBasin.func.localNorm(DF, subset='all')
DFN.saveAs(dataName+'N')

seed = 0
rate = 0.2
rng = np.random.default_rng(seed)

# random subset
mask = np.ones([len(DF.t), len(DF.siteNoLst)]).astype(bool)
for indS, siteNo in enumerate(DF.siteNoLst):
    obsB = np.any(~np.isnan(DF.c[:, indS, :]), axis=1)
    obsD = np.where(obsB)[0]
    nPick = int(sum(obsB*rate))
    ind = rng.choice(obsD, nPick, replace=False)
    mask[ind, indS] = False
DF.saveSubset('pkR20', mask=mask)
DF.saveSubset('rmR20', mask=~mask)

# last 20% subset
mask = np.ones([len(DF.t), len(DF.siteNoLst)]).astype(bool)
for indS, siteNo in enumerate(DF.siteNoLst):
    obsB = np.any(~np.isnan(DF.c[:, indS, :]), axis=1)
    obsD = np.where(obsB)[0]
    nPick = int(sum(obsB*rate))
    ind = obsD[-nPick:]
    mask[ind, indS] = False
DF.saveSubset('pkL20',  mask=mask)
DF.saveSubset('rmL20',  mask=~mask)

# pick by year
yrIn = np.arange(1985, 2020, 5).tolist()
t1 = dbBasin.func.pickByYear(DF.t, yrIn)
t2 = dbBasin.func.pickByYear(DF.t, yrIn, pick=False)
DF.createSubset('pkYr5', dateLst=t1)
DF.createSubset('rmYr5', dateLst=t2)

# before after 2010
DF.saveSubset('B10', ed='2009-12-31')
DF.saveSubset('A10', sd='2010-01-01')

# examine test/train rate
trainLst = ['rmYr5', 'rmR20', 'rmL20', 'rmRT20',  'B10']
testLst = ['rmYr5', 'pkR20', 'pkL20', 'pkRT20',  'A10']
df = pd.DataFrame(index=DF.varC, columns=trainLst)
aLst = list()
bLst = list()
for trainSet, testSet in zip(trainLst, testLst):
    a = DF.extractSubset(DF.c, trainSet)
    b = DF.extractSubset(DF.c, testSet)
    aLst.append(a)
    bLst.append(b)

max_stars_repo_name: fkwai/geolearn

max_issues_repo_name: fkwai/geolearn

max_forks_repo_name: fkwai/geolearn


text:
""" Implementation of the vanilla Deep Q Network.
"""

import pathlib
from datetime import datetime
from typing import List, Optional, Union

import gym
import numpy as np
import tensorflow as tf

from pyderl.agents.base_agent import BaseAgent
from pyderl.agents.dqn.replay_buffer import ReplayBuffer
from pyderl.utils.linear_interpolator import LinearInterpolator

class DQNAgent(BaseAgent):
    """ Implementation of the original/vanilla version of the Deep Q Network, as
    described by :cite:`mnih:2015`.
    """

    def __init__(self,
                 network: tf.keras.Model,
                 num_actions: int,
                 gamma: float = 0.99,
                 learning_rate: float = 5e-4,
                 replay_buffer_size: int = 100000) -> None:
        self._num_actions = num_actions
        self._gamma = gamma

        self.replay_buffer = ReplayBuffer(
            input_shape=network.input_shape[1:],
            size=replay_buffer_size,
        )

        self.online_model = network
        self.target_model = tf.keras.models.clone_model(self.online_model)
        self.update_target_network()

        self.optimizer = tf.optimizers.Adam(learning_rate=learning_rate,
                                            clipnorm=1.0)
        self.loss = tf.keras.losses.Huber()

    def act(self,
            obs: Union[np.ndarray, tf.Tensor],
            exploration_chance: float = 0.01) -> int:
        """ Returns an action chosen according to the agent's policy.

        Note:
            This method doesn't accept a batch of observations as input, just a
            single observation!

        Args:
            obs (Union[np.ndarray, tf.Tensor]): Observation of the environment's
                current state.
            exploration_chance (float): The probability of the agent choosing
                a random action instead of following its greedy policy.

        Returns:
            The action chosen by the agent.
        """
        # Exploring:
        if np.random.uniform(low=0, high=1) < exploration_chance:
            action = np.random.choice(self._num_actions)
        # Exploiting:
        else:
            # Preparing the input observation:
            obs_tensor = tf.convert_to_tensor(obs)
            obs_tensor = tf.expand_dims(obs_tensor, axis=0)

            # Predicting the Q values associated with each action:
            q_values = self.online_model(obs_tensor, training=False)

            # Selecting the optimal action:
            action = tf.argmax(q_values[0]).numpy()

        return action

    def _experience_replay(self, obs, actions,
                           rewards, next_obs, dones) -> None:
        """ Triggers a new learning session. """
        # Predicting target Q-values:
        q_target = tf.reduce_max(self.target_model(next_obs), axis=1)
        q_target = q_target * (1 - dones)  # set Q = 0 when the episode is done
        q_target = rewards + self._gamma * q_target

        # Predicting online Q-values and calculating loss:
        actions_mask = tf.one_hot(indices=actions, depth=self._num_actions)
        with tf.GradientTape() as tape:
            q_values = self.online_model(obs)
            q_action = tf.reduce_sum(
                tf.multiply(q_values, actions_mask), axis=1,
            )

            assert q_action.shape == q_target.shape
            loss = self.loss(y_true=q_target, y_pred=q_action)

        # Gradient descent step:
        gradients = tape.gradient(loss, self.online_model.trainable_variables)
        self.optimizer.apply_gradients(
            zip(gradients, self.online_model.trainable_variables)
        )

    def update_target_network(self) -> None:
        """ Updates the weights of the target network. """
        self.target_model.set_weights(self.online_model.get_weights())

    def train(self,
              env: gym.Env,
              total_timesteps: int = 100000,
              exploration_fraction: float = 0.3,
              min_exploration_chance: float = 0.05,
              train_freq: int = 4,
              batch_size: int = 32,
              learning_starts_step: int = 1000,
              target_network_update_freq: int = 500,
              checkpoint: bool = False,
              checkpoint_path: Optional[str] = None,
              checkpoint_freq: int = 1000,
              verbose: bool = True,
              render_env: bool = False) -> List[float]:
        """ Trains the agent on the given environment.

        Args:
            env (gym.Env): Environment in which the agent will be trained on.
            total_timesteps (int): Total number of steps to be taken on the
                given environment.
            exploration_fraction (float): Fraction/percentage of the training
                period over which the exploration rate is annealed.
            min_exploration_chance (float): Final value of the exploration rate.
            train_freq (int): A training session, in which the model's
                parameters are updated, is performed every `train_freq` steps.
            batch_size (int): Size of the batch sampled from the replay buffer
                during a training session.
            learning_starts_step (int): Amount of steps to wait before starting
                regular training sessions. During this period, the agent is only
                collecting transitions from the environment.
            target_network_update_freq (int): The update frequency of the target
                network.
            checkpoint (bool): Whether or not to save the agent's model
                periodically during the training.
            checkpoint_path (Optional[str]): Path to the directory in which the
                checkpoints will be stored. If `None`, a new directory with a
                default name will be created and used.
            checkpoint_freq (int): Frequency of the checkpoints.
            verbose (bool): Whether or not to print the training progress.
            render_env (bool): Whether or not to render the environment during
                the training.

        Returns:
            A list containing the rewards obtained by the agent on each episode.
        """
        # Creating checkpoint directory:
        if checkpoint:
            if checkpoint_path is None:
                t = datetime.now().strftime('%Y-%m-%d-%H-%M-%S')
                checkpoint_path = f"./dqn_checkpoints_{t}"

            pathlib.Path(checkpoint_path).mkdir(parents=True, exist_ok=True)

        # Linear interpolator for the exploration chance:
        exploration_chance = LinearInterpolator(
            initial_value=1,
            final_value=min_exploration_chance,
            num_timesteps=int(total_timesteps * exploration_fraction),
        )

        # Storage for the total rewards obtained in each episode:
        episodes_rewards = [0.0]

        # Training:
        obs = env.reset()
        for t in range(total_timesteps):
            # Rendering env:
            if render_env:
                env.render()

            # Choosing action and updating env:
            action = self.act(obs, exploration_chance.value(t))
            next_obs, reward, done, _ = env.step(action)

            # Storing the transition into the replay buffer:
            self.replay_buffer.add(obs, action, reward, next_obs, done)
            obs = next_obs

            # Checking if the episode is done:
            episodes_rewards[-1] += reward
            if done:
                # Printing info:
                if verbose:
                    # TODO: ETA based on avg time per step
                    avg_reward = np.mean(episodes_rewards
                                         if len(episodes_rewards) <= 50
                                         else episodes_rewards[-50:])
                    print(
                        f"[Episode {len(episodes_rewards)}]"
                        f"[Timestep {t + 1}/{total_timesteps}] "
                        f"Reward: {episodes_rewards[-1]}  |  "
                        f"Avg. reward (50 past episodes): {avg_reward:.2f}  |  "
                        f"Exploration chance: {exploration_chance.value(t):.2%}"
                    )

                # Resetting:
                obs = env.reset()
                episodes_rewards.append(0.0)

            # Experience replay:
            if t >= learning_starts_step and (t % train_freq) == 0:
                experience = self.replay_buffer.make_batch(
                    min(batch_size, len(self.replay_buffer))
                )
                self._experience_replay(*experience)

            # Updating target network:
            if (t >= learning_starts_step
                    and (t % target_network_update_freq) == 0):
                self.update_target_network()

            # Checkpoint
            if checkpoint and (t % checkpoint_freq) == 0:
                # TODO
                raise NotImplementedError()

        return episodes_rewards

max_stars_repo_name: Talendar/pyderl

max_issues_repo_name: Talendar/pyderl

max_forks_repo_name: Talendar/pyderl


text:
import numpy as np
import pytest
from stardist import star_dist3D, Rays_GoldenSpiral, relabel_image_stardist3D
from utils import random_image, real_image3d, check_similar, circle_image

@pytest.mark.parametrize('img', (real_image3d()[1], random_image((33, 44, 55))))
@pytest.mark.parametrize('n_rays', (4, 16, 32))
@pytest.mark.parametrize('grid', ((1, 1, 1), (1, 2, 4)))
def test_types(img, n_rays, grid):
    mode = "cpp"
    rays = Rays_GoldenSpiral(n_rays)
    gt = star_dist3D(img, rays=rays, grid=grid, mode=mode)
    for dtype in (np.int8, np.int16, np.int32,
                  np.uint8, np.uint16, np.uint32):
        x = star_dist3D(img.astype(dtype), rays=rays, grid=grid, mode=mode)
        print("test_stardist3D (mode {mode}) for shape {img.shape} and type {dtype}".format(
            mode=mode, img=img, dtype=dtype))
        check_similar(gt, x)

@pytest.mark.gpu
@pytest.mark.parametrize('img', (real_image3d()[1], random_image((33, 44, 55))))
@pytest.mark.parametrize('n_rays', (4, 16, 32))
@pytest.mark.parametrize('grid', ((1, 1, 1), (1, 2, 4)))
def test_types_gpu(img, n_rays, grid):
    mode = "opencl"
    rays = Rays_GoldenSpiral(n_rays)
    gt = star_dist3D(img, rays=rays, grid=grid, mode=mode)
    for dtype in (np.int8, np.int16, np.int32,
                  np.uint8, np.uint16, np.uint32):
        x = star_dist3D(img.astype(dtype), rays=rays, grid=grid, mode=mode)
        print("test_stardist3D (mode {mode}) for shape {img.shape} and type {dtype}".format(
            mode=mode, img=img, dtype=dtype))
        check_similar(gt, x)

@pytest.mark.gpu
@pytest.mark.parametrize('img', (real_image3d()[1], random_image((33, 44, 55))))
@pytest.mark.parametrize('n_rays', (4, 16, 32))
@pytest.mark.parametrize('grid', ((1, 1, 1), (1, 2, 4)))
def test_cpu_gpu(img, n_rays, grid):
    rays = Rays_GoldenSpiral(n_rays)
    s_cpp = star_dist3D(img, rays=rays, grid=grid, mode="cpp")
    s_ocl = star_dist3D(img, rays=rays, grid=grid, mode="opencl")
    check_similar(s_cpp, s_ocl)

@pytest.mark.parametrize('n_rays', (64,128))
@pytest.mark.parametrize('eps', ((1,1,1),(.4,1.3,.7)))
def test_relabel_consistency(n_rays, eps, plot = False):
    """ test whether an already star-convex label image gets perfectly relabeld"""

    rays = Rays_GoldenSpiral(n_rays, anisotropy = 1./np.array(eps))
    # img = random_image((128, 123))
    lbl1 = circle_image(shape=(32,32,32), radius=8, eps = eps)

    lbl1 = relabel_image_stardist3D(lbl1, rays)

    lbl2 = relabel_image_stardist3D(lbl1, rays)

    rel_error = 1-np.count_nonzero(np.bitwise_and(lbl1>0, lbl2>0))/np.count_nonzero(lbl1>0)
    print(rel_error)
    assert rel_error<1e-1

    if plot:
        import matplotlib.pyplot as plt
        plt.figure(num=1, figsize=(8,4))
        plt.subplot(2,3,1);plt.imshow(np.max(lbl1,0));plt.title("GT")
        plt.subplot(2,3,2);plt.imshow(np.max(lbl2,0));plt.title("Reco")
        plt.subplot(2,3,3);plt.imshow(np.max(1*(lbl1>0)+2*(lbl2>0),0));plt.title("Overlay")
        plt.subplot(2,3,4);plt.imshow(np.max(lbl1,1));plt.title("GT")
        plt.subplot(2,3,5);plt.imshow(np.max(lbl2,1));plt.title("Reco")
        plt.subplot(2,3,6);plt.imshow(np.max(1*(lbl1>0)+2*(lbl2>0),1));plt.title("Overlay")
        plt.tight_layout()
        plt.show()

    return lbl1, lbl2

if __name__ == '__main__':
    lbl1, lbl2 = test_relabel_consistency(128,eps = (.5,1,1.2), plot = True)

    # from utils import circle_image

    # rays = Rays_GoldenSpiral(4)
    # lbl = circle_image((64,) * 3)

    # a = star_dist3D(lbl, rays=rays, grid=(1, 2, 2), mode="cpp")
    # b = star_dist3D(lbl, rays=rays, grid=(1, 2, 2), mode="opencl")
    # print(np.amax(np.abs(a - b)))

max_stars_repo_name: wejarrard/stardist

max_issues_repo_name: lucas-sancere/stardist

max_forks_repo_name: lucas-sancere/stardist


text:
#Schrodinger 1D equation solved by finding eigenvectors of the hamiltonian
from __future__ import division
import numpy as np
import scipy
import scipy.integrate
import scipy.linalg
import scipy.misc
import scipy.special
from matplotlib import pyplot as plt
from scipy import integrate
from matplotlib import animation

l = 100
N = 2**10
Large_val = 10000000

def f_M(u):
	return (np.arctan(u) * 2/np.pi + 1/(3465*np.pi/2*(1+u**2)**7)*(3465*u**13 + 23100*u**11 + 65373*u**9 + 101376*u**7 + 92323*u**5 + 48580*u**3 - 3465*u))/u**2

def v(x):
	u = x/4.13337988676
	ans, err = integrate.quad(f_M, u, np.inf)
	return -ans

X, dx = np.linspace(0.01, l, N, retstep=True)

H = np.diag(map(v, X))
H[0,0] = Large_val

for i in range(N):
    H[i,i] += 2/dx**2
    if(i > 0):
        H[i-1, i] -= 1/dx**2
    if i<N-1:
        H[i+1, i] -= 1/dx**2

num_energies = 10
E, psi_E = scipy.linalg.eigh(H)
#plt.plot(np.linspace(0,N,N)[:num_energies], E[:num_energies], "bo", label="Calculated energies")
#plt.title("Energies for the first" +  " $" + str(num_energies) + "$ states")
#plt.xlabel("State index")
#plt.ylabel("Energy")
#plt.legend(loc='lower right')
#plt.show()

psi = []

for n in range(10):
	psi.append(psi_E[:,n]/X)
	norm = np.sqrt(scipy.integrate.simps(psi[n]**2, X))
	psi[n]/=norm
	plt.figure()
	plt.loglog(X, psi[n]**2, label="$n=%i$" % n)
	plt.title("eigenstate #" + str(n))
	plt.xlabel("$r$ position")
	plt.ylabel("Normalized amplitude $\psi^2$")
	plt.loglog(X, map(lambda x: psi[n][1]**2/(1+(x/4.13337988676)**2)**8, X), label = "soliton fit")
	plt.grid()
	plt.legend(loc='lower left')
	plt.xlim(0.1, l)
	plt.savefig("SolitonEigenvector" + str(n) + ".png")
	plt.close()

lin_comb = [1, 0, 0, 0, 0, 0, 1, 1]

def phi_sum(t):
	s = np.zeros(N)
	for i in range(len(lin_comb)):
		for j in range(len(lin_comb)):
			s += lin_comb[i]*lin_comb[j]*psi[i]*psi[j]*np.cos((E[i]-E[j])*t)
	return s

# First set up the figure, the axis, and the plot element we want to animate
fig = plt.figure()
ax = plt.axes(xlim=(0.15, l), ylim=(10**-6, 1))
line, = ax.loglog(X, phi_sum(0))

# initialization function: plot the background of each frame
def init():
    line.set_data(X, phi_sum(0))
    return line,

# animation function.  This is called sequentially
def animate(i):
    x = X
    y = phi_sum(i*0.1)
    line.set_data(x, y)
    return line,

# call the animator.  blit=True means only re-draw the parts that have changed.
anim = animation.FuncAnimation(fig, animate, init_func=init,
                               frames = 10**4, interval=10, blit=True)

# save the animation as an mp4.  This requires ffmpeg or mencoder to be
# installed.  The extra_args ensure that the x264 codec is used, so that
# the video can be embedded in html5.  You may need to adjust this for
# your system: for more information, see
# http://matplotlib.sourceforge.net/api/animation_api.html
anim.save('basic_animation.mp4', fps=30, extra_args=['-vcodec', 'libx264'])

plt.show()

max_stars_repo_name: benvchurch/project-eva

max_issues_repo_name: benvchurch/project-eva

max_forks_repo_name: benvchurch/project-eva

reason: import numpy,import scipy,from scipy


text:
from typing import Any, List, Optional
import numpy as np

class StandardScaler:
    def __init__(self,
                 means: np.ndarray = None,
                 stds: np.ndarray = None,
                 replace_nan_token: Any = None):

        self.means = means
        self.stds = stds
        self.replace_nan_token = replace_nan_token

    def fit(self, X):
        X = np.array(X).astype(float)
        self.means = np.nanmean(X, axis=0)
        self.stds = np.nanstd(X, axis=0)
        self.means = np.where(np.isnan(self.means), np.zeros(self.means.shape), self.means)
        self.stds = np.where(np.isnan(self.stds), np.ones(self.stds.shape), self.stds)
        self.stds = np.where(self.stds == 0, np.ones(self.stds.shape), self.stds)
        return self

    def transform(self, X) -> np.ndarray:
        X = np.array(X).astype(float)
        transformed_with_nan = (X - self.means) / self.stds
        transformed_with_none = np.where(np.isnan(transformed_with_nan), self.replace_nan_token, transformed_with_nan)
        return transformed_with_none

    def inverse_transform(self, X):
        X = np.array(X).astype(float)
        transformed_with_nan = X * self.stds + self.means
        transformed_with_none = np.where(np.isnan(transformed_with_nan), self.replace_nan_token, transformed_with_nan)
        return transformed_with_none

    def inverse_transform_variance(self, X):
        X = np.array(X).astype(float)
        transformed_with_nan = X * (self.stds ** 2)
        transformed_with_none = np.where(np.isnan(transformed_with_nan), self.replace_nan_token, transformed_with_nan)
        return transformed_with_none

max_stars_repo_name: wangdingyan/hybridUQ

max_issues_repo_name: wangdingyan/hybridUQ

max_forks_repo_name: wangdingyan/hybridUQ


text:
import numpy as np
import torch
import paddle

'''

'''
ppppppppppp = 0.5
batch_size = 2

'''
I_3 = torch.eye(3)
device = I_3.device
G_inv = I_3

_constant_cache = dict()

def constant(value, shape=None, dtype=None, device=None, memory_format=None):
    value = np.asarray(value)
    if shape is not None:
        shape = tuple(shape)
    if dtype is None:
        dtype = torch.get_default_dtype()
    if device is None:
        device = torch.device('cpu')
    if memory_format is None:
        memory_format = torch.contiguous_format

    key = (value.shape, value.dtype, value.tobytes(), shape, dtype, device, memory_format)
    tensor = _constant_cache.get(key, None)
    if tensor is None:
        tensor = torch.as_tensor(value.copy(), dtype=dtype, device=device)
        if shape is not None:
            tensor, _ = torch.broadcast_tensors(tensor, torch.empty(shape))
        tensor = tensor.contiguous(memory_format=memory_format)
        _constant_cache[key] = tensor
    return tensor

def matrix(*rows, device=None):
    assert all(len(row) == len(rows[0]) for row in rows)
    elems = [x for row in rows for x in row]
    ref = [x for x in elems if isinstance(x, torch.Tensor)]
    if len(ref) == 0:
        return constant(np.asarray(rows), device=device)
    assert device is None or device == ref[0].device
    elems = [x if isinstance(x, torch.Tensor) else constant(x, shape=ref[0].shape, device=ref[0].device) for x in elems]
    bbbbbbb = torch.stack(elems, dim=-1)
    bbbbbbbb = bbbbbbb.reshape(ref[0].shape + (len(rows), -1))
    return bbbbbbbb

def scale2d(sx, sy, **kwargs):
    return matrix(
        [sx, 0,  0],
        [0,  sy, 0],
        [0,  0,  1],
        **kwargs)

def scale2d_inv(sx, sy, **kwargs):
    return scale2d(1 / sx, 1 / sy, **kwargs)

debug_percentile = None

www = torch.rand([batch_size], device=device)
i = torch.floor(www * 2)

i = torch.where(torch.rand([batch_size], device=device) < ppppppppppp, i, torch.zeros_like(i))
if debug_percentile is not None:
    i = torch.full_like(i, torch.floor(debug_percentile * 2))

aaaaaaa = scale2d_inv(1 - 2 * i, 1)
G_inv = G_inv @ aaaaaaa
'''

I_3 = paddle.eye(3)
G_inv = I_3

_constant_cache = dict()

def constant(value, shape=None, dtype=None):
    value = np.asarray(value)
    if shape is not None:
        shape = tuple(shape)
    if dtype is None:
        dtype = paddle.get_default_dtype()

    key = (value.shape, value.dtype, value.tobytes(), shape, dtype)
    tensor = _constant_cache.get(key, None)
    if tensor is None:
        # tensor = paddle.to_tensor(value.copy(), dtype=dtype)
        tensor = paddle.ones(shape, dtype=dtype) * value
        _constant_cache[key] = tensor
    return tensor

def matrix(*rows):
    assert all(len(row) == len(rows[0]) for row in rows)
    elems = [x for row in rows for x in row]
    ref = [x for x in elems if isinstance(x, paddle.Tensor)]
    if len(ref) == 0:
        return constant(np.asarray(rows))
    elems = [x if isinstance(x, paddle.Tensor) else constant(x, shape=ref[0].shape) for x in elems]
    bbbbbbb = paddle.stack(elems, axis=-1)
    bbbbbbbb = bbbbbbb.reshape((tuple(ref[0].shape) + (len(rows), -1)))
    return bbbbbbbb

def scale2d(sx, sy, **kwargs):
    return matrix(
        [sx, 0,  0],
        [0,  sy, 0],
        [0,  0,  1],
        **kwargs)

def scale2d_inv(sx, sy, **kwargs):
    return scale2d(1 / sx, 1 / sy, **kwargs)

debug_percentile = None

www = paddle.rand([batch_size], dtype=paddle.float32)
i = paddle.floor(www * 2)

i = paddle.where(paddle.rand([batch_size], dtype=paddle.float32) < ppppppppppp, i, paddle.zeros_like(i))
if debug_percentile is not None:
    i = paddle.full_like(i, paddle.floor(debug_percentile * 2))

aaaaaaa = scale2d_inv(1 - 2 * i, 1)
G_inv = G_inv @ aaaaaaa

print()


text:
import sympy as sp  # needed for symbolic interpretation
import pathlib  # needed to create folder
import matplotlib.pyplot as plt  # needed for graphs
from sympy.abc import E, M, j, e, k, n, o, x, i, d
import numpy as np
import os
from tqdm import tqdm

print('Program started running!')
#######################
# Some starting setup #
#######################
dpisiz = 1000
# clrs is created to rotate through some colors I prefer when using sympy.plot and matplotlib.plot
clrs = [(1, 'tab:blue'), (2, 'tab:orange'), (3, 'tab:green'), (4, 'tab:red'), (5, 'tab:purple'), (6, 'tab:gray')]
# lnsstyle is needed to switch linestyle when using matplotlib
lnsstyle = [0] * 6
lnsstyle = ['-', '--', '-.', ':', '--', '-']

#####################################
# Function to save images to folder #
#####################################

def savim(dir, name):
    path = pathlib.Path(f"./{dir}")
    path.mkdir(exist_ok=True, parents=True)
    plt.savefig(f'./{dir}/{name}.png', dpi=dpisiz)
    print(f'Saved image location: ./{dir}/{name}.png')

###################################################################
#                           PART I                                #
###################################################################

###############################################################
# Splitting Sum to two products to make it cleaner to look at #
###############################################################

jn_frc1 = (-1) ** k / (sp.factorial(k) * sp.factorial(n + k))
jn_frc2 = sp.Pow(x / 2, (n + 2 * k))
sum = sp.Sum(jn_frc1 * jn_frc2, (k, 0, o))

bessel = [0] * 6  # Zero list to fill with the bessel functions
plots_bsl = [0] * 6  # Zero list to fill with the bessel plots
knumber = 50  # overflows at 83 firstly for J_5

#########################################################################
#          Using symply.plot to plot our Bessel functions               #
#########################################################################
for b in tqdm(range(6)):
    bessel[b] = sp.simplify(sum.subs([(o, knumber), (n, b)]).doit())
    plots_bsl[b] = sp.plot(bessel[b], (x, 0.001, 15), show=False, line_color=clrs[b][1], label=f'J_{b}(x)', legend=True)
    if b == 0:
        print("\n Our initial plot, so appending not possible")  # Appending other plots to the first one
    else:
        plots_bsl[0].append(plots_bsl[b][0])

#########################################################################
#          Setting ylabel, saving and showing our plot                  #
#########################################################################
plots_bsl[0].ylabel = 'J_n(x)'
plots_bsl[0].save(f'pr1_task3/img_{knumber}')
# plots_bsl[0].show()

##################################################################################
# Because sympy.plot doesn't have some features I use (e.g. dpi, linestyle, etc) #
# I switch to matploblib (which is the backend of sympy.plot) by switching to    #
# the lambda (anonymous) function using sp.lambdify to a linspace of domain (0,15) #
##################################################################################

xx = np.linspace(0, 15, 10000)
lmbd_plots_bsl = [0] * 6  # Creating an empty array to fill with the lambda functions

print('Lambdifying our Sympy Sums')
for b in tqdm(range(0, 6)):
    lmbd_plots_bsl[b] = sp.lambdify(x, sp.simplify(sum.subs([(o, knumber), (n, b)]).doit()))(xx)

# Creating our figure
fig = plt.figure()
ax = plt.axes()

print('Plotting our bessel functions')
# Plotting our bessel functions
for b in tqdm(range(0, 6)):
    ax.plot(xx, lmbd_plots_bsl[b], color=clrs[b][1], label=f'J_{b}(x)', linestyle=lnsstyle[b])
ax.legend()
ax.set_title("Problem 1 - Task 3 - Bessel functions of the first kind")
ax.set_ylabel('J_n(x)')
ax.set_xlabel('x')
savim('pr1_task3', f'limg_{knumber}')

###################################################################
#                           PART II                               #
###################################################################

d = knumber  # kmax

# Interpreting J_j earlier for better readability
J_j = sp.Sum((-1) ** knumber / (sp.factorial(k) * sp.factorial(j + k)) * (j * e / 2) ** (j + 2 * k), (k, 0, d))

# Our sum with j our index of summation starting from 1 to o
sumE = M + sp.Sum(2 / j * J_j * sp.sin(j * M), (j, 1, o))
# Substituting the eccentricities e=0.3 and e=0.9
# and our upper bound of summation which is either 3 or 10
sum_j3_e03 = sp.simplify(sumE.subs([(e, 0.3), (o, 3)]).doit())
sum3_j10_e03 = sp.simplify(sumE.subs([(e, 0.3), (o, 10)]).doit())
sum_j3_e09 = sp.simplify(sumE.subs([(e, 0.9), (o, 3)]).doit())
sum3_j10_e09 = sp.simplify(sumE.subs([(e, 0.9), (o, 10)]).doit())

# Switching to lambda (anonymous) function
xxx = np.linspace(0, 2 * np.pi, 10000)

lmbd_sum_j3_e03 = sp.lambdify(M, sum_j3_e03)(xxx)
lmbd_sum3_j10_e03 = sp.lambdify(M, sum3_j10_e03)(xxx)
lmbd_sum_j3_e09 = sp.lambdify(M, sum_j3_e09)(xxx)
lmbd_sum3_j10_e09 = sp.lambdify(M, sum3_j10_e09)(xxx)

# Getting data from Task 1 to compare

data = np.genfromtxt(fname="data.csv", delimiter=',')
fig2 = plt.figure()
ax2 = plt.axes()
ax2.set_ylabel('E')
ax2.set_title("Problem 1 - Task 3 - Fourier-Bessel expansion of E with e=0.3")
ax2.set_xlabel('M')
ax2.plot(data[:, 0], data[:, 2], label='E for e=0.3', color='tab:pink')
ax2.plot(xxx, lmbd_sum_j3_e03, label='E for j=3, e=0.3', linestyle='dashed', color='tab:red')
ax2.plot(xxx, lmbd_sum3_j10_e03, label='E for j=10, e=0.3', linestyle='dotted', color='tab:blue')
ax2.legend()
savim('pr1_task3', 'e_03')
# fig2.show()
fig3 = plt.figure()
ax3 = plt.axes()
ax3.set_ylabel('E')
ax3.set_xlabel('M')
ax3.set_title("Problem 1 - Task 3 - Fourier-Bessel expansion of E with e=0.9")
ax3.plot(data[:, 0], data[:, 5], label='E for e=0.9', color='black')
ax3.plot(xxx, lmbd_sum_j3_e09, label='E for j=3, e=0.9', linestyle='dashed', color='tab:red')
ax3.plot(xxx, lmbd_sum3_j10_e09, label='E for j=10, e=0.9', linestyle='dotted', color='tab:blue')
ax3.legend()
savim('pr1_task3', 'e_09')
# fig3.show()

####################################################################################################
# As we can see from our results, for lower eccentricities both n=3 and n=10 are nearly            #
# identical compared to the Newton-Raphson method.                                                 #
# However for 0.9, this is no longer the case. In this case there are sinusoidal curves appearing  #
# everywhere in the curve with different amplitude (Higher at the ends).                            #
####################################################################################################

# Getting data from Task 2 to compare
# Starting with e=0.9

En3_e9f = np.genfromtxt(os.path.join(os.getcwd(), "pr1_task2data", "En3_e9f.csv"), delimiter=',')
En10_e9f = np.genfromtxt(os.path.join(os.getcwd(), "pr1_task2data", "En10_e9f.csv"), delimiter=',')
# Creating our figure
fig4 = plt.figure()
ax4 = plt.axes()
ax4.set_ylabel('E')
ax4.set_xlabel('M')
ax4.set_title("Problem 1 - Task 3 - Comparing with Task 2 for e=0.9")
ax4.plot(data[:, 0], En3_e9f[:], label='E(Task2) for n=3, e=0.9 ', color='black')
ax4.plot(data[:, 0], En10_e9f[:], label='E(Task2) for n=10, e=0.9', color='tab:pink')
ax4.plot(xxx, lmbd_sum_j3_e09, label='E for j=3, e=0.9', linestyle='dashed', color='tab:red')
ax4.plot(xxx, lmbd_sum3_j10_e09, label='E for j=10, e=0.9', linestyle='dotted', color='tab:blue')
ax4.legend()
savim('pr1_task3', 'e_09withtask2data')
# fig4.show()

# Doing e=0.3 now

En3_e3f = np.genfromtxt(os.path.join(os.getcwd(), "pr1_task2data", "En3_e3f.csv"), delimiter=',')
En10_e3f = np.genfromtxt(os.path.join(os.getcwd(), "pr1_task2data", "En10_e3f.csv"), delimiter=',')
# Creating our figure
fig5 = plt.figure()
ax5 = plt.axes()
ax5.set_ylabel('E')
ax5.set_xlabel('M')
ax5.set_title("Problem 1 - Task 3 - Comparing with Task 2 for e=0.3")
ax5.plot(data[:, 0], En3_e3f[:], label='E(Task2) for n=3, e=0.3 ', linestyle=':', color='black')
ax5.plot(data[:, 0], En10_e3f[:], label='E(Task2) for n=10, e=0.3', color='tab:pink')
ax5.plot(xxx, lmbd_sum_j3_e03, label='E for j=3, e=0.3', linestyle='dashed', color='tab:red')
ax5.plot(xxx, lmbd_sum3_j10_e03, label='E for j=10, e=0.3', linestyle='dotted', color='tab:blue')
ax5.legend()
savim('pr1_task3', 'e_03withtask2data')
# fig5.show()

print('Program finished running!')

#########################################################################################
# As we can see from our results, for lower eccentricities both n=3 and n=10 are nearly #
# identical compared to Kepler's equation which is created using Langrange's theorem.   #
#########################################################################################

##########################################################################################
# So as we can see our Fourier-Bessel expansion can be used for low values of e.         #
# With e increasing, this method doesn't perform as well when compared to Newton-Raphson #
# or when using Lagrange's Theorem                                                       #
##########################################################################################

max_stars_repo_path: Project 1/pr1_task3.py

max_issues_repo_path: Project 1/pr1_task3.py

max_forks_repo_path: Project 1/pr1_task3.py

reason: import numpy,import sympy,from sympy


text:
from pliers.filters import FrameSamplingFilter
from pliers.extractors import (GoogleVisionAPIFaceExtractor,
                               GoogleVisionAPILabelExtractor,
                               GoogleVisionAPIPropertyExtractor,
                               GoogleVisionAPISafeSearchExtractor,
                               GoogleVisionAPIWebEntitiesExtractor,
                               ExtractorResult,
                               merge_results)
from pliers.extractors.api.google import GoogleVisionAPIExtractor
from pliers.stimuli import ImageStim, VideoStim
import pytest
import json
from os.path import join
from ...utils import get_test_data_path
import numpy as np

@pytest.mark.skipif("'GOOGLE_APPLICATION_CREDENTIALS' not in os.environ")
def test_google_vision_api_extractor_inits():
    ext = GoogleVisionAPIExtractor(num_retries=5)
    assert ext.num_retries == 5
    assert ext.max_results == 100
    assert ext.service is not None

@pytest.mark.skipif("'GOOGLE_APPLICATION_CREDENTIALS' not in os.environ")
def test_google_vision_api_face_extractor_inits():
    ext = GoogleVisionAPIFaceExtractor(num_retries=5)
    assert ext.num_retries == 5
    assert ext.max_results == 100
    assert ext.service is not None

    # Test parsing of individual response
    filename = join(
        get_test_data_path(), 'payloads', 'google_vision_api_face_payload.json')
    response = json.load(open(filename, 'r'))
    stim = ImageStim(join(get_test_data_path(), 'image', 'obama.jpg'))
    res = ExtractorResult(response['faceAnnotations'], stim, ext)
    df = res.to_df()
    assert df['angerLikelihood'][0] == 'VERY_UNLIKELY'
    assert df['landmark_LEFT_EYE_BOTTOM_BOUNDARY_y'][0] == 257.023
    assert np.isnan(df['boundingPoly_vertex2_y'][0])

@pytest.mark.skipif("'GOOGLE_APPLICATION_CREDENTIALS' not in os.environ")
def test_google_vision_api_face_extractor():
    ext = GoogleVisionAPIFaceExtractor(num_retries=5)
    filename = join(get_test_data_path(), 'image', 'obama.jpg')
    stim = ImageStim(filename)
    result = ext.transform(stim).to_df()
    assert 'joyLikelihood' in result.columns
    assert result['joyLikelihood'][0] == 'VERY_LIKELY'
    assert float(result['face_detectionConfidence'][0]) > 0.7

@pytest.mark.skipif("'GOOGLE_APPLICATION_CREDENTIALS' not in os.environ")
def test_google_vision_multiple_face_extraction():
    filename = join(get_test_data_path(), 'image', 'thai_people.jpg')
    stim = ImageStim(filename)
    # Only first record
    ext = GoogleVisionAPIFaceExtractor()
    result1 = ext.transform(stim).to_df(handle_annotations='first')
    assert 'joyLikelihood' in result1.columns
    # All records
    ext = GoogleVisionAPIFaceExtractor()
    result2 = ext.transform(stim).to_df()
    assert 'joyLikelihood' in result2.columns
    assert result2.shape[0] > result1.shape[0]

@pytest.mark.skipif("'GOOGLE_APPLICATION_CREDENTIALS' not in os.environ")
def test_google_vision_face_batch():
    stims = ['apple', 'obama', 'thai_people']
    stim_files = [join(get_test_data_path(), 'image', '%s.jpg' % s)
                  for s in stims]
    stims = [ImageStim(s) for s in stim_files]
    ext = GoogleVisionAPIFaceExtractor()
    result = ext.transform(stims)
    result = merge_results(result, format='wide', extractor_names=False,
                           handle_annotations='first')
    assert result.shape == (2, 139)
    assert 'joyLikelihood' in result.columns
    assert result['joyLikelihood'][0] == 'VERY_LIKELY'
    assert result['joyLikelihood'][1] == 'VERY_LIKELY'

    video = VideoStim(join(get_test_data_path(), 'video', 'obama_speech.mp4'))
    conv = FrameSamplingFilter(every=10)
    video = conv.transform(video)
    result = ext.transform(video)
    result = merge_results(result, format='wide', extractor_names=False)
    assert 'joyLikelihood' in result.columns
    assert result.shape == (22, 139)

    video = VideoStim(join(get_test_data_path(), 'video', 'small.mp4'))
    video = conv.transform(video)
    result = ext.transform(video)
    result = merge_results(result, format='wide', extractor_names=False)
    assert 'joyLikelihood' not in result.columns
    assert len(result) == 0

@pytest.mark.skipif("'GOOGLE_APPLICATION_CREDENTIALS' not in os.environ")
def test_google_vision_api_label_extractor():
    ext = GoogleVisionAPILabelExtractor(num_retries=5)
    filename = join(get_test_data_path(), 'image', 'apple.jpg')
    stim = ImageStim(filename)
    result = ext.transform(stim).to_df()
    assert 'apple' in result.columns
    assert result['apple'][0] > 0.75

@pytest.mark.skipif("'GOOGLE_APPLICATION_CREDENTIALS' not in os.environ")
def test_google_vision_api_properties_extractor():
    ext = GoogleVisionAPIPropertyExtractor(num_retries=5)
    filename = join(get_test_data_path(), 'image', 'apple.jpg')
    stim = ImageStim(filename)
    result = ext.transform(stim).to_df()
    assert (158, 13, 29) in result.columns
    assert np.isfinite(result[(158, 13, 29)][0])

@pytest.mark.skipif("'GOOGLE_APPLICATION_CREDENTIALS' not in os.environ")
def test_google_vision_api_safe_search():
    ext = GoogleVisionAPISafeSearchExtractor(num_retries=5)
    filename = join(get_test_data_path(), 'image', 'obama.jpg')
    stim = ImageStim(filename)
    result = ext.transform(stim).to_df()
    assert 'adult' in result.columns
    assert result['violence'][0] == 'VERY_UNLIKELY'

@pytest.mark.skipif("'GOOGLE_APPLICATION_CREDENTIALS' not in os.environ")
def test_google_vision_api_web_entities():
    ext = GoogleVisionAPIWebEntitiesExtractor(num_retries=5)
    filename = join(get_test_data_path(), 'image', 'obama.jpg')
    stim = ImageStim(filename)
    result = ext.transform(stim).to_df()
    assert 'Barack Obama' in result.columns

max_stars_repo_name: adelavega/pliers

max_issues_repo_name: adelavega/pliers

max_forks_repo_name: adelavega/pliers


text:
import gdal
import numpy as np
import os,glob,shutil,sys
import netCDF4
import matplotlib.pyplot as plt
import subprocess
import multiprocessing

# On BRIDGE servers, gdal is a bit broken, set 'GDAL_DATA='/opt/bridge/CentOS6-64/python/anaconda-5.0.1-2.7/pkgs/libgdal-2.1.0-0/share/gdal'

################################################################################
# Input paths

# Input directory containing DEM tiles (5degree increments)
dem_dir = '/export/anthropocene/array-01/pu17449/MeritDEM/' # files have format e.g. n25e090_dem.tif

out_dir1 = '/export/anthropocene/array-01/pu17449/parameter_transfer_datasets/MeritDEMSlope'
out_dir2 = '/export/anthropocene/array-01/pu17449/parameter_transfer_datasets/MeritDEMSlope_p1deg'
processdir = '/export/anthropocene/array-01/pu17449/parameter_transfer_datasets/tmp'

if not os.path.exists(out_dir2):
	os.mkdir(out_dir2)

numthreads = 6

def processtile(ftile,out_dir1,out_dir2,processdir):
	fname = os.path.basename(ftile)
	print(fname)

	slopefile = os.path.join(out_dir1,fname[:-7]+'slope.tif')
	if not os.path.exists(slopefile):
		# Convert to EPSG:3857 projection (units in of metres not degrees)
		tmpfile = os.path.join(processdir,fname[:-4]+'_epsg3857.tif')
		cmd = ['gdalwarp','-t_srs','EPSG:3857','-r','near','-of','GTiff',ftile,tmpfile]
		#print(cmd)
		ret1 = subprocess.call(cmd)

		# Calculate slope for tile
		cmd = ['gdaldem','slope','-of','GTiff','-b','1','-s','1.0','-co','COMPRESS=DEFLATE',tmpfile,slopefile]
		#print(cmd)
		ret2 = subprocess.call(cmd)
		# Clean up tmpfile
		os.remove(tmpfile)
	else:
		ret1=ret2=0

	# Regrid to 0.5deg
	# For 0.5deg regridding
	slopefile2 = os.path.join(out_dir2,fname[:-7]+'slope_p1deg.tif')
	if not os.path.exists(slopefile2):
		cmd = ['gdalwarp', '-t_srs', 'EPSG:4326', '-tr', '0.1', '0.1', '-r', 'average' ,'-of' ,'GTiff','-co','COMPRESS=DEFLATE', slopefile, slopefile2]
		#print(cmd)
		ret3 = subprocess.call(cmd)
	else:
		ret3=0

	if ret1!=0 or ret2!=0 or ret3!=0:
		print('Error processing',ftile)
		return -5
	else:
		return 0

pool = multiprocessing.Pool(processes=numthreads)

for ftile in glob.glob(os.path.join(dem_dir,'*_dem.tif')):
	pool.apply_async(processtile,(ftile,out_dir1,out_dir2,processdir))

# close the pool and make sure the processing has finished
pool.close()
pool.join()

max_stars_repo_name: BrisClimate/flood-cascade

max_issues_repo_name: BrisClimate/flood-cascade

max_forks_repo_name: BrisClimate/flood-cascade


text:
#Batch Input Logic with multiple layers
import numpy as np
inputs=[[2.0,4.0,5.0],
        [1.4, 2.5, 3.2],
        [0.4, 5.2, 6.3]] #  3 Batches
#Layer 1
weights=[[6.5, 4.5, -0.5],
        [2.4, 4.5, 1.5],
        [-0.5,1.3, 0.1]]
biases = [2, 1, 0]
#Layer 2
weights2=[[6.5, 4.5, -0.5],
        [2.4, -4.5, 1.5],
        [-0.5,-1.3, 0.1]]
biases2 = [-1, 2, 0]

layer1_output = np.dot(inputs, np.array(weights).T) + biases
layer2_output = np.dot(layer1_output, np.array(weights2).T) + biases2
print(layer2_output)