text:
import numpy as np

def sma(array, n=5):
    """
    Calculate the simple moving averages (SMA)

    :param array: the price array represented in python list
    :param n: the average period
    :return: the corresponding SMA which has the same length of the input array
    """
    ma = np.zeros(shape=(len(array), ))
    for i, val in enumerate(array):
        batch = array[max(i - n + 1, 0): i + 1]
        # ma.append(np.mean(batch))
        ma[i] = np.mean(batch)
    return ma

def ema(array, n=5):
    """
    Calculate the exponentially weighted averages (a.k.a. EMA) for a given array:
    v_t = beta * v_{t-1} + (1 - beta) * a_t

    (deprecated)
    The problem with the above equation is that the EMA will start off with very
    small numbers in the beginning, adding bias correction will solve this problem:
    v_t = v_t / (1 - beta ^ t)

    :param array: the price array given in form of a python list of floating numbers
    :param n: the period considered
    :return: the EMA
    """
    beta = 1 - 2 / (n + 1)
    ma = np.zeros(shape=(len(array),))
    for i, val in enumerate(array):
        if i == 0:
            ma[i] = val
            continue
        previous = ma[i - 1]
        current = beta * previous + (1 - beta) * val
        ma[i] = current
    return ma

def macd(array, a=12, b=26, c=9):
    """
    Calculates the MACD, MACD signal, and their differences (histogram)

    :param array: the price array represented as python list
    :param a: fast period length
    :param b: slow period length
    :param c: the average period length of the MACD itself
    :return: MACD, signal, and their difference
    """
    fast = ema(array, a)
    slow = ema(array, b)
    proper = fast - slow
    signal = ema(proper, c)
    diff = proper - signal
    return proper, signal, diff

def moving_std(array, n=5):
    """
    Calculate the moving standard deviation with a given sliding window

    :param array: the price array in python list to be calculated the stddev with
    :param n: sliding window size
    :return: the corresponding std dev
    """
    std = []
    for i , val in enumerate(array):
        batch = array[max(i - n + 1, 0): i + 1]
        std.append(np.std(batch))
    return std

def bbands(array, n=20, k=2):
    """
    Calculate the bollinger band and its derived indicators for a given price array

    :param array: the price array as python list
    :param n: the period considered
    :param k: the ± standard deviation bound range
    :return: the band upper bound, lower bound, %b indicator, and bandwidth
    """
    ma = sma(array, n)
    std = moving_std(array, n)
    ma = np.array(ma)
    std = np.array(std)

    upper = ma + k * std
    lower = ma - k * std
    percent_b = (array - lower) / (upper - lower + 1e-10)
    bandwidth = (upper - lower) / ma

    return upper, lower, percent_b, bandwidth

# aliases
ma = sma

"""Break down of the MACD chart:
    1. Crossovers - As shown in the chart above, when the MACD falls below the signal line,
    it is a bearish signal, which indicates that it may be time to sell. Conversely, when the
    MACD rises above the signal line, the indicator gives a bullish signal, which suggests
    that the price of the asset is likely to experience upward momentum. Many traders wait
    for a confirmed cross above the signal line before entering into a position to avoid
    getting "faked out" or entering into a position too early, as shown by the first arrow.

    2. Divergence - When the security price diverges from the MACD, it signals the end of the
    current trend. For example, a stock price that is rising and a MACD indicator that is
    falling could mean that the rally is about to end. Conversely, if a stock price is falling
    and the MACD is rising, it could mean that a bullish reversal could occur in the near-term.
    Traders often use divergence in conjunction with other technical indicators to find
    opportunities.

    3. Dramatic Rise - When the MACD rises dramatically - that is, the shorter moving average
    pulls away from the longer-term moving average - it is a signal that the security is
    overbought and will soon return to normal levels. Traders will often combine this analysis
    with the Relative Strength Index (RSI) or other technical indicators to verify overbought
    or oversold conditions.
"""

"""The Squeeze
    The squeeze is the central concept of Bollinger Bands®. When the bands come close
    together, constricting the moving average, it is called a squeeze. A squeeze signals
    a period of low volatility and is considered by traders to be a potential sign of
    future increased volatility and possible trading opportunities. Conversely, the
    wider apart the bands move, the more likely the chance of a decrease in volatility
    and the greater the possibility of exiting a trade. However, these conditions are
    not trading signals. The bands give no indication when the change may take place or
    which direction price could move.

"""


text:
from imutils.video import VideoStream
import face_recognition
import imutils
import pickle
import time
import cv2
from imutils import face_utils
import numpy as np
import imutils
import dlib
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
cap = cv2.VideoCapture(0)
if not cap.isOpened():
        print("Cannot open camera")
        exit()
while True:
        ret, frame = cap.read()
        if not ret:
            print("Can't receive frame")
            break
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        rects = detector(gray, 1)
        for (i, rect) in enumerate(rects):
            shape = predictor(gray, rect)
            shape = face_utils.shape_to_np(shape)
            for (name, (i, j)) in face_utils.FACIAL_LANDMARKS_IDXS.items():
                if(name == 'right_eye'):
                    (i,j) = (i,j)
                    break
            (x, y, w, h) = cv2.boundingRect(np.array([shape[i:j]]))
            roi = frame[y:y + h, x:x + w]
            roi = imutils.resize(roi, width=250, inter=cv2.INTER_CUBIC)
            hsv = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
            mask1_left = cv2.inRange(hsv, (0, 140, 70), (10, 255,255))
            mask2_left = cv2.inRange(hsv, (170, 140, 70), (180, 255,255))
            mask_left = mask1_left + mask2_left
            imask_left = mask_left>0
            red_left = np.zeros_like(roi, np.uint8)
            red_left[imask_left] = roi[imask_left]
            cv2.imshow('right_eye', red_left)
            for (name, (i, j)) in face_utils.FACIAL_LANDMARKS_IDXS.items():
                if(name == 'left_eye'):
                    (p,q) = (i,j)
                    break
            (x, y, w, h) = cv2.boundingRect(np.array([shape[p:q]]))
            roi = frame[y:y + h, x:x + w]
            roi = imutils.resize(roi, width=250, inter=cv2.INTER_CUBIC)
            hsv = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
            mask1_left = cv2.inRange(hsv, (0, 140, 70), (10, 255,255))
            mask2_left = cv2.inRange(hsv, (170, 140, 70), (180, 255,255))
            mask_left = mask1_left + mask2_left
            imask_left = mask_left>0
            red_left = np.zeros_like(roi, np.uint8)
            red_left[imask_left] = roi[imask_left]
            cv2.imshow('left_eye', red_left)
        if cv2.waitKey(1) == ord('q'):
            break
cap.release()
cv2.destroyAllWindows()

ext: py

max_stars_repo_path: Red Eye Detection/Redness_detection.py

max_issues_repo_path: Red Eye Detection/Redness_detection.py

max_forks_repo_path: Red Eye Detection/Redness_detection.py

reason: import numpy


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

# Copyright (c) 2021-2022 the DerivX authors
# All rights reserved.
#
# The project sponsor and lead author is Xu Rendong.
# E-mail: xrd@ustc.edu, QQ: 277195007, WeChat: xrd_ustc
# See the contributors file for names of other contributors.
#
# Commercial use of this code in source and binary forms is
# governed by a LGPL v3 license. You may get a copy from the
# root directory. Or else you should get a specific written
# permission from the project author.
#
# Individual and educational use of this code in source and
# binary forms is governed by a 3-clause BSD license. You may
# get a copy from the root directory. Certainly welcome you
# to contribute code of all sorts.
#
# Be sure to retain the above copyright notice and conditions.

import numpy as np
from scipy import stats

import utility

def sb_x1(s, k, v, u, t): # k v t != 0
    return np.log(s / k) / (v * t ** 0.5) + (1.0 + u) * v * t ** 0.5

def sb_x2(s, h, v, u, t): # h v t != 0
    return np.log(s / h) / (v * t ** 0.5) + (1.0 + u) * v * t ** 0.5

def sb_y1(s, h, k, v, u, t): # s k v t != 0
    return np.log(h ** 2 / (s * k)) / (v * t ** 0.5) + (1.0 + u) * v * t ** 0.5

def sb_y2(s, h, v, u, t): # s v t != 0
    return np.log(h / s) / (v * t ** 0.5) + (1.0 + u) * v * t ** 0.5

def sb_z(s, h, v, n, t): # s v t != 0
    return np.log(h / s) / (v * t ** 0.5) + n * v * t ** 0.5

def sb_u(r, q, v): # v != 0
    return (r - q) / v ** 2 - 0.5 # (r - q - 0.5 * v ** 2) / v ** 2

def sb_n(r, v, u): # v != 0
    return (u ** 2 + 2.0 * r / v ** 2) ** 0.5

def sb_A(phi, s, k, v, r, q, t): # k v t != 0
    u = sb_u(r, q, v)
    x1 = sb_x1(s, k, v, u, t)
    return phi * s * np.exp(-q * t) * stats.norm.cdf(phi * x1) - phi * k * np.exp(-r * t) * stats.norm.cdf(phi * (x1 - v * t ** 0.5))

def sb_B(phi, s, h, k, v, r, q, t): # h v t != 0
    u = sb_u(r, q, v)
    x2 = sb_x2(s, h, v, u, t)
    return phi * s * np.exp(-q * t) * stats.norm.cdf(phi * x2) - phi * k * np.exp(-r * t) * stats.norm.cdf(phi * (x2 - v * t ** 0.5))

def sb_C(phi, eta, s, h, k, v, r, q, t): # s k v t != 0
    u = sb_u(r, q, v)
    y1 = sb_y1(s, h, k, v, u, t)
    return phi * s * np.exp(-q * t) * (h / s) ** (2.0 * (u + 1.0)) * stats.norm.cdf(eta * y1) - phi * k * np.exp(-r * t) * (h / s) ** (2.0 * u) * stats.norm.cdf(eta * (y1 - v * t ** 0.5))

def sb_D(phi, eta, s, h, k, v, r, q, t): # s v t != 0
    u = sb_u(r, q, v)
    y2 = sb_y2(s, h, v, u, t)
    return phi * s * np.exp(-q * t) * (h / s) ** (2.0 * (u + 1.0)) * stats.norm.cdf(eta * y2) - phi * k * np.exp(-r * t) * (h / s) ** (2.0 * u) * stats.norm.cdf(eta * (y2 - v * t ** 0.5))

def sb_E(eta, x, s, h, v, r, q, t): # s h v t != 0
    u = sb_u(r, q, v)
    x2 = sb_x2(s, h, v, u, t)
    y2 = sb_y2(s, h, v, u, t)
    return x * np.exp(-r * t) * (stats.norm.cdf(eta * (x2 - v * t ** 0.5)) - (h / s) ** (2.0 * u) * stats.norm.cdf(eta * (y2 - v * t ** 0.5)))

def sb_F(eta, x, s, h, v, r, q, t, is_kop_delay): # s v t != 0
    u = sb_u(r, q, v)
    n = sb_n(r, v, u) # 立即支付
    if is_kop_delay == True: # 延迟支付
        n = (r ** 2 + v ** 4 / 4.0 + r * v ** 2) ** 0.5 / v ** 2
    z = sb_z(s, h, v, n, t)
    return x * ((h / s) ** (u + n) * stats.norm.cdf(eta * z) + (h / s) ** (u - n) * stats.norm.cdf(eta * (z - 2.0 * n * v * t ** 0.5)))

class Config(object):
    def __init__(self, s, h, k, x, v, r, q, t, p, is_call, is_knock, is_kop_delay, barrier_type):
        self.s = s # 标的价格
        self.h = h # 障碍价格
        self.k = k # 行权价格
        self.x = x # 未触及障碍所需支付资金
        self.v = v # 波动率
        self.r = r # 无风险利率
        self.q = q # 年化分红率
        self.t = t # 年化到期期限
        self.p = p # 参与率，未敲出情况下客户对收益的占比要求
        self.is_call = is_call # 看涨看跌
        self.is_kop_delay = is_kop_delay # 敲出后是立即还是延期支付资金
        self.barrier_type = barrier_type # 障碍类型

    def ToArgs(self):
        return self.__dict__

class Barrier_Single(object):
    def __init__(self):
        self.s = 0.0 # 标的价格
        self.h = 0.0 # 障碍价格
        self.k = 0.0 # 行权价格
        self.x = 0.0 # 未触及障碍所需支付资金
        self.v = 0.0 # 波动率
        self.r = 0.0 # 无风险利率
        self.q = 0.0 # 年化分红率
        self.t = 0.0 # 年化到期期限
        self.p = 0.0 # 参与率，未敲出情况下客户对收益的占比要求
        self.is_call = True # 看涨看跌
        self.is_kop_delay = False # 敲出后是立即还是延期支付资金
        self.barrier_type = 0 # 障碍类型

        self.up_in    = 1 # 向上敲入
        self.down_in  = 2 # 向下敲入
        self.up_out   = 3 # 向上敲出
        self.down_out = 4 # 向下敲出

        self.error_message = ""

    def GetError(self):
        return self.error_message

    def InitArgs(self, config): # config：dict
        try:
            self.s = config["s"]
            self.h = config["h"]
            self.k = config["k"]
            self.x = config["x"]
            self.v = config["v"]
            self.r = config["r"]
            self.q = config["q"]
            self.t = config["t"]
            self.p = config["p"]
            self.is_call = config["is_call"]
            self.is_kop_delay = config["is_kop_delay"]
            self.barrier_type = config["barrier_type"]
            return 0
        except Exception as e:
            self.error_message = "参数设置发生异常！%s" % e
        return -1

    def CalcPrice(self):
        result = 0.0
        if self.barrier_type == self.up_in:
            if self.is_call == True: # 看涨认购
                eta = -1.0
                phi = 1.0
                # k == h 时下面两组 result 相同
                if self.k >= self.h:
                    A = sb_A(phi, self.s, self.k, self.v, self.r, self.q, self.t)
                    E = sb_E(eta, self.x, self.s, self.h, self.v, self.r, self.q, self.t)
                    result = self.p * A + E
                if self.k < self.h:
                    B = sb_B(phi, self.s, self.h, self.k, self.v, self.r, self.q, self.t)
                    C = sb_C(phi, eta, self.s, self.h, self.k, self.v, self.r, self.q, self.t)
                    D = sb_D(phi, eta, self.s, self.h, self.k, self.v, self.r, self.q, self.t)
                    E = sb_E(eta, self.x, self.s, self.h, self.v, self.r, self.q, self.t)
                    result = self.p * (B - C + D) + E
            else: # 看跌认沽
                eta = -1.0
                phi = -1.0
                # k == h 时下面两组 result 相同
                if self.k >= self.h:
                    A = sb_A(phi, self.s, self.k, self.v, self.r, self.q, self.t)
                    B = sb_B(phi, self.s, self.h, self.k, self.v, self.r, self.q, self.t)
                    D = sb_D(phi, eta, self.s, self.h, self.k, self.v, self.r, self.q, self.t)
                    E = sb_E(eta, self.x, self.s, self.h, self.v, self.r, self.q, self.t)
                    result = self.p * (A - B + D) + E
                if self.k < self.h:
                    C = sb_C(phi, eta, self.s, self.h, self.k, self.v, self.r, self.q, self.t)
                    E = sb_E(eta, self.x, self.s, self.h, self.v, self.r, self.q, self.t)
                    result = self.p * C + E
        elif self.barrier_type == self.down_in:
            if self.is_call == True: # 看涨认购
                eta = 1.0
                phi = 1.0
                # k == h 时下面两组 result 相同
                if self.k >= self.h:
                    C = sb_C(phi, eta, self.s, self.h, self.k, self.v, self.r, self.q, self.t)
                    E = sb_E(eta, self.x, self.s, self.h, self.v, self.r, self.q, self.t)
                    result = self.p * C + E
                if self.k < self.h:
                    A = sb_A(phi, self.s, self.k, self.v, self.r, self.q, self.t)
                    B = sb_B(phi, self.s, self.h, self.k, self.v, self.r, self.q, self.t)
                    D = sb_D(phi, eta, self.s, self.h, self.k, self.v, self.r, self.q, self.t)
                    E = sb_E(eta, self.x, self.s, self.h, self.v, self.r, self.q, self.t)
                    result = self.p * (A - B + D) + E
            else: # 看跌认沽
                eta = 1.0
                phi = -1.0
                # k == h 时下面两组 result 相同
                if self.k >= self.h:
                    B = sb_B(phi, self.s, self.h, self.k, self.v, self.r, self.q, self.t)
                    C = sb_C(phi, eta, self.s, self.h, self.k, self.v, self.r, self.q, self.t)
                    D = sb_D(phi, eta, self.s, self.h, self.k, self.v, self.r, self.q, self.t)
                    E = sb_E(eta, self.x, self.s, self.h, self.v, self.r, self.q, self.t)
                    result = self.p * (B - C + D) + E
                if self.k < self.h:
                    A = sb_A(phi, self.s, self.k, self.v, self.r, self.q, self.t)
                    E = sb_E(eta, self.x, self.s, self.h, self.v, self.r, self.q, self.t)
                    result = self.p * A + E
        elif self.barrier_type == self.up_out:
            if self.is_call == True: # 看涨认购
                eta = -1.0
                phi = 1.0
                # k == h 时下面两组 result 相同
                if self.k >= self.h:
                    F = sb_F(eta, self.x, self.s, self.h, self.v, self.r, self.q, self.t, self.is_kop_delay)
                    result = F # self.p * 0.0 + F
                if self.k < self.h:
                    A = sb_A(phi, self.s, self.k, self.v, self.r, self.q, self.t)
                    B = sb_B(phi, self.s, self.h, self.k, self.v, self.r, self.q, self.t)
                    C = sb_C(phi, eta, self.s, self.h, self.k, self.v, self.r, self.q, self.t)
                    D = sb_D(phi, eta, self.s, self.h, self.k, self.v, self.r, self.q, self.t)
                    F = sb_F(eta, self.x, self.s, self.h, self.v, self.r, self.q, self.t, self.is_kop_delay)
                    result = self.p * (A - B + C - D) + F
            else: # 看跌认沽
                eta = -1.0
                phi = -1.0
                # k == h 时下面两组 result 相同
                if self.k >= self.h:
                    B = sb_B(phi, self.s, self.h, self.k, self.v, self.r, self.q, self.t)
                    D = sb_D(phi, eta, self.s, self.h, self.k, self.v, self.r, self.q, self.t)
                    F = sb_F(eta, self.x, self.s, self.h, self.v, self.r, self.q, self.t, self.is_kop_delay)
                    result = self.p * (B - D) + F
                if self.k < self.h:
                    A = sb_A(phi, self.s, self.k, self.v, self.r, self.q, self.t)
                    C = sb_C(phi, eta, self.s, self.h, self.k, self.v, self.r, self.q, self.t)
                    F = sb_F(eta, self.x, self.s, self.h, self.v, self.r, self.q, self.t, self.is_kop_delay)
                    result = self.p * (A - C) + F
        elif self.barrier_type == self.down_out:
            if self.is_call == True: # 看涨认购
                eta = 1.0
                phi = 1.0
                # k == h 时下面两组 result 相同
                if self.k >= self.h:
                    A = sb_A(phi, self.s, self.k, self.v, self.r, self.q, self.t)
                    C = sb_C(phi, eta, self.s, self.h, self.k, self.v, self.r, self.q, self.t)
                    F = sb_F(eta, self.x, self.s, self.h, self.v, self.r, self.q, self.t, self.is_kop_delay)
                    result = self.p * (A - C) + F
                if self.k < self.h:
                    B = sb_B(phi, self.s, self.h, self.k, self.v, self.r, self.q, self.t)
                    D = sb_D(phi, eta, self.s, self.h, self.k, self.v, self.r, self.q, self.t)
                    F = sb_F(eta, self.x, self.s, self.h, self.v, self.r, self.q, self.t, self.is_kop_delay)
                    result = self.p * (B - D) + F
            else: # 看跌认沽
                eta = 1.0
                phi = -1.0
                # k == h 时下面两组 result 相同
                if self.k >= self.h:
                    A = sb_A(phi, self.s, self.k, self.v, self.r, self.q, self.t)
                    B = sb_B(phi, self.s, self.h, self.k, self.v, self.r, self.q, self.t)
                    C = sb_C(phi, eta, self.s, self.h, self.k, self.v, self.r, self.q, self.t)
                    D = sb_D(phi, eta, self.s, self.h, self.k, self.v, self.r, self.q, self.t)
                    F = sb_F(eta, self.x, self.s, self.h, self.v, self.r, self.q, self.t, self.is_kop_delay)
                    result = self.p * (A - B + C - D) + F
                if self.k < self.h:
                    F = sb_F(eta, self.x, self.s, self.h, self.v, self.r, self.q, self.t, self.is_kop_delay)
                    result = F # self.p * 0.0 + F
        else:
            self.error_message = "障碍类型 %d 不存在！" % self.barrier_type
            raise Exception(self.error_message)
        return result

    def CalcPayoff(self):
        result = 0.0
        if self.barrier_type == self.up_in:
            if self.is_call == True: # 看涨认购
                if self.s >= self.h: # s >= h 已 向上敲入
                    result = max(self.s - self.k, 0.0)
                else: # s < h 未 向上敲入
                    result = self.x
            else: # 看跌认沽
                if self.s >= self.h: # s >= h 已 向上敲入
                    result = max(self.k - self.s, 0.0)
                else: # s < h 未 向上敲入
                    result = self.x
        elif self.barrier_type == self.down_in:
            if self.is_call == True: # 看涨认购
                if self.s <= self.h: # s <= h 已 向下敲入
                    result = max(self.s - self.k, 0.0)
                else: # s > h 未 向下敲入
                    result = self.x
            else: # 看跌认沽
                if self.s <= self.h: # s <= h 已 向下敲入
                    result = max(self.k - self.s, 0.0)
                else: # s > h 未 向下敲入
                    result = self.x
        elif self.barrier_type == self.up_out:
            if self.is_call == True: # 看涨认购
                if self.s >= self.h: # s >= h 已 向上敲出
                    result = self.x
                else: # s < h 未 向上敲出
                    result = max(self.s - self.k, 0.0)
            else: # 看跌认沽
                if self.s >= self.h: # s >= h 已 向上敲出
                    result = self.x
                else: # s < h 未 向上敲出
                    result = max(self.k - self.s, 0.0)
        elif self.barrier_type == self.down_out:
            if self.is_call == True: # 看涨认购
                if self.s <= self.h: # s <= h 已 向下敲出
                    result = self.x
                else: # s > h 未 向下敲出
                    result = max(self.s - self.k, 0.0)
            else: # 看跌认沽
                if self.s <= self.h: # s <= h 已 向下敲出
                    result = self.x
                else: # s > h 未 向下敲出
                    result = max(self.k - self.s, 0.0)
        else:
            self.error_message = "障碍类型 %d 不存在！" % self.barrier_type
            raise Exception(self.error_message)
        return result

reason: import numpy,from scipy


text:
import tensorflow as tf
import numpy as np
import random
from collections import deque
from Config import Categorical_DQN_Config
from utils import conv, dense
import math

class Categorical_DQN():
    def __init__(self,env,config):
        self.sess = tf.InteractiveSession()
        self.config = config
        self.v_max = self.config.v_max
        self.v_min = self.config.v_min
        self.atoms = self.config.atoms

        self.epsilon = self.config.INITIAL_EPSILON
        self.state_shape = env.observation_space.shape
        self.action_dim = env.action_space.n

        self.time_step = 0

        target_state_shape = [1]
        target_state_shape.extend(self.state_shape)
        self.state_input = tf.placeholder(tf.float32,target_state_shape)
        self.action_input = tf.placeholder(tf.int32,[1,1])
        self.m_input = tf.placeholder(tf.float32,[self.atoms])

        self.delta_z = (self.v_max - self.v_min) / (self.atoms - 1)
        self.z = [self.v_min + i * self.delta_z for i in range(self.atoms)]

        self.build_cate_dqn_net()

        self.saver = tf.train.Saver()

        self.sess.run(tf.global_variables_initializer())

        self.save_model()
        self.restore_model()

    def build_layers(self, state, action, c_names, units_1, units_2, w_i, b_i, reg=None):
        with tf.variable_scope('conv1'):
            conv1 = conv(state, [5, 5, 3, 6], [6], [1, 2, 2, 1], w_i, b_i)
        with tf.variable_scope('conv2'):
            conv2 = conv(conv1, [3, 3, 6, 12], [12], [1, 2, 2, 1], w_i, b_i)
        with tf.variable_scope('flatten'):
            flatten = tf.contrib.layers.flatten(conv2)

        with tf.variable_scope('dense1'):
            dense1 = dense(flatten, units_1, [units_1], w_i, b_i)
        with tf.variable_scope('dense2'):
            dense2 = dense(dense1, units_2, [units_2], w_i, b_i)
        with tf.variable_scope('concat'):
            concatenated = tf.concat([dense2, tf.cast(action, tf.float32)], 1)
        with tf.variable_scope('dense3'):
            dense3 = dense(concatenated, self.atoms, [self.atoms], w_i, b_i) # 返回
        return tf.nn.softmax(dense3)

    def build_cate_dqn_net(self):
        with tf.variable_scope('target_net'):
            c_names = ['target_net_arams',tf.GraphKeys.GLOBAL_VARIABLES]
            w_i = tf.random_uniform_initializer(-0.1,0.1)
            b_i = tf.constant_initializer(0.1)
            self.z_target = self.build_layers(self.state_input,self.action_input,c_names,24,24,w_i,b_i)

        with tf.variable_scope('eval_net'):
            c_names = ['eval_net_params',tf.GraphKeys.GLOBAL_VARIABLES]
            w_i = tf.random_uniform_initializer(-0.1,0.1)
            b_i = tf.constant_initializer(0.1)
            self.z_eval = self.build_layers(self.state_input,self.action_input,c_names,24,24,w_i,b_i)

        self.q_eval = tf.reduce_sum(self.z_eval * self.z)
        self.q_target = tf.reduce_sum(self.z_target * self.z)

        self.cross_entropy_loss = -tf.reduce_sum(self.m_input * tf.log(self.z_eval))
        self.optimizer = tf.train.AdamOptimizer(self.config.LEARNING_RATE).minimize(self.cross_entropy_loss)

        eval_params = tf.get_collection("eval_net_params")
        target_params = tf.get_collection('target_net_params')

        self.update_target_net = [tf.assign(t, e) for t, e in zip(target_params, eval_params)]

    def train(self,s,r,action,s_,gamma):
        self.time_step += 1
        list_q_ = [self.sess.run(self.q_target,feed_dict={self.state_input:[s_],self.action_input:[[a]]}) for a in range(self.action_dim)]
        a_ = tf.argmax(list_q_).eval()
        m = np.zeros(self.atoms)
        p = self.sess.run(self.z_target,feed_dict = {self.state_input:[s_],self.action_input:[[a_]]})[0]
        m = np.zeros(self.atoms)
        for j in range(self.atoms):
            Tz = min(self.v_max,max(self.v_min,r+gamma * self.z[j]))
            bj = (Tz - self.v_min) / self.delta_z # 分在第几个块里
            l,u = math.floor(bj),math.ceil(bj) # 上下界

            pj = p[j]

            m[int(l)] += pj * (u - bj)
            m[int(u)] += pj * (bj - l)

        self.sess.run(self.optimizer,feed_dict={self.state_input:[s] , self.action_input:[action], self.m_input: m })

        if self.time_step % self.config.UPDATE_TARGET_NET == 0:
            self.sess.run(self.update_target_net)

    def save_model(self):
        print("Model saved in : ", self.saver.save(self.sess, self.config.MODEL_PATH))

    def restore_model(self):
        self.saver.restore(self.sess, self.config.MODEL_PATH)
        print("Model restored.")

    def greedy_action(self,s):
        self.epsilon = max(self.config.FINAL_EPSILON, self.epsilon * self.config.EPSILIN_DECAY)
        if random.random() <= self.epsilon:
            return random.randint(0, self.action_dim - 1)
        return np.argmax(
            [self.sess.run(self.q_target,feed_dict={self.state_input:[s],self.action_input:[[a]]}) for a in range(self.action_dim)])


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

# In[1]:

# Fish Stock Classification

# In[2]:

"""Prediction/Classification model (Multiclass Classification) for prediciting the Species of a Fish using various features such as Height,length,weight,etc"""

# In[3]:

#importing Libraries

import pandas as pd
import numpy as np
import os

# In[4]:

dataset = pd.read_csv(r'C:\Users\IDEAPAD 320\Desktop\MLR\Fish.csv')
safedataset = pd.read_csv(r'C:\Users\IDEAPAD 320\Desktop\MLR\Fish.csv')

# In[5]:

dataset.head()

# In[7]:

#visualising the dataset to know if we can find some obvious patters

import matplotlib.pyplot as plt

X = dataset.iloc[:, [1,2,3,4,5,6]].values
y = dataset.iloc[:, 0].values

from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111,projection = '3d')
_w = dataset["Weight"]
_h = dataset["Height"]
_wd = dataset["Width"]
_l1 = dataset["Length1"]
_l2 = dataset["Length2"]
_l3 = dataset["Length3"]
ax.scatter(_w,_h,_wd,c = 'b', marker = 'o' , s = 5)
ax.scatter(_l1,_l2,_l3,c = 'r', marker = 'o' , s = 5)
ax.scatter(_w,_l2,_h,c = 'g', marker = 'o' , s = 5)
ax.set_xlabel("1")
ax.set_ylabel("2")
ax.set_zlabel("3")
plt.show()

# In[8]:

#preparing the depended vector
factor = pd.factorize(dataset['Species'])
dataset.Species = factor[0]
definitions = factor[1]
print(dataset.Species.head())
print(definitions)

# In[9]:

#safedataset will still have our dataset while we manipulated the dataset
safedataset.head()

# In[10]:

dataset.head()

# In[11]:

# Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0)

# In[14]:

# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

# In[15]:

# Applying PCA
from sklearn.decomposition import PCA
pca = PCA(n_components = 2)
X_train = pca.fit_transform(X_train)
X_test = pca.transform(X_test)

# In[16]:

# Fitting Random Forest Classification to the Training set
from sklearn.ensemble import RandomForestClassifier
classifier = RandomForestClassifier(n_estimators = 30, criterion = 'gini', random_state = 0)
classifier.fit(X_train, y_train)

# In[17]:

# Predicting the Test set results
y_pred = classifier.predict(X_test)

# In[18]:

# Making the Confusion Matrix
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)

# In[19]:

#scoring accuracy
classifier.score(X_test,y_test)

# In[26]:

#finding best parameters through grid search
from sklearn.model_selection import GridSearchCV
parameters = [{'n_estimators': [1, 10, 20, 30,40,50], 'criterion': ['entropy']},
              {'n_estimators': [1, 10, 20, 30,40,50], 'criterion': ['gini']}]
grid_search = GridSearchCV(estimator = classifier,
                           param_grid = parameters,
                           scoring = 'accuracy',
                           cv = 10,
                           n_jobs = -1)

# In[27]:

grid_search = grid_search.fit(X_train, y_train)
best_accuracy = grid_search.best_score_
best_parameters = grid_search.best_params_

# In[28]:

#just a warning regarding deprecation

# In[29]:

#getting best parameters ( although a ran it already and replaced the test params with ideal)
best_parameters

# In[30]:

best_accuracy

# In[31]:

'''Following method can be used to save the model and predicting from it without training it again'''

# In[32]:

from sklearn.externals import joblib
joblib.dump(classifier,'fishclassifier-joblib')

# In[33]:

"""The above file can be loaded and used for prediction as shown"""

# In[34]:

model = joblib.load('fishclassifier-joblib')

# In[ ]:

max_stars_repo_path: Machine Learning/Fish-Stock-Classification-using-Random-Forest/Fish Stock Classification (1).py

max_stars_repo_name: Storiesbyharshit/Data-Science-Portfolio

max_issues_repo_name: Storiesbyharshit/Data-Science-Portfolio

max_forks_repo_name: Storiesbyharshit/Data-Science-Portfolio


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

# BCDI: tools for pre(post)-processing Bragg coherent X-ray diffraction imaging data
#   (c) 07/2017-06/2019 : CNRS UMR 7344 IM2NP
#   (c) 07/2019-05/2021 : DESY PHOTON SCIENCE
#   (c) 06/2021-present : DESY CFEL
#       authors:
#         Jerome Carnis, carnis_jerome@yahoo.fr

import numpy as np
import unittest
import bcdi.utils.utilities as util

def run_tests(test_class):
    suite = unittest.TestLoader().loadTestsFromTestCase(test_class)
    runner = unittest.TextTestRunner(verbosity=2)
    return runner.run(suite)

class TestInRange(unittest.TestCase):
    """Tests on the function utilities.in_range."""

    def setUp(self):
        # executed before each test
        self.extent = (-10, 99, -20, 89, 10, 119)

    # def tearDown(self):
    #     executed after each test

    def test_inrange_in_range(self):
        self.assertTrue(util.in_range(point=(0, 0, 20), extent=self.extent))

    def test_inrange_not_in_range_low_z(self):
        self.assertFalse(util.in_range(point=(-11, 0, 20), extent=self.extent))

    def test_inrange_not_in_range_high_z(self):
        self.assertFalse(util.in_range(point=(100, 0, 20), extent=self.extent))

    def test_inrange_not_in_range_low_y(self):
        self.assertFalse(util.in_range(point=(0, -21, 20), extent=self.extent))

    def test_inrange_not_in_range_high_y(self):
        self.assertFalse(util.in_range(point=(0, 90, 20), extent=self.extent))

    def test_inrange_not_in_range_low_x(self):
        self.assertFalse(util.in_range(point=(0, 0, 9), extent=self.extent))

    def test_inrange_not_in_range_high_x(self):
        self.assertFalse(util.in_range(point=(0, 0, 120), extent=self.extent))

    def test_inrange_lower_edge_z(self):
        self.assertTrue(util.in_range(point=(-10, 0, 20), extent=self.extent))

    def test_inrange_larger_edge_z(self):
        self.assertTrue(util.in_range(point=(99, 0, 20), extent=self.extent))

    def test_inrange_lower_edge_y(self):
        self.assertTrue(util.in_range(point=(0, -20, 20), extent=self.extent))

    def test_inrange_larger_edge_y(self):
        self.assertTrue(util.in_range(point=(0, 89, 20), extent=self.extent))

    def test_inrange_lower_edge_x(self):
        self.assertTrue(util.in_range(point=(0, 0, 10), extent=self.extent))

    def test_inrange_larger_edge_x(self):
        self.assertTrue(util.in_range(point=(0, 0, 119), extent=self.extent))

class TestIsFloat(unittest.TestCase):
    """
    Tests on the function utilities.is_float.

    def is_float(string)
    """

    def test_string_float(self):
        self.assertTrue(util.is_float("12.0"))

    def test_string_int(self):
        self.assertTrue(util.is_float("12"))

    def test_string_complex(self):
        self.assertFalse(util.is_float("12 + 1j"))

    def test_string_none(self):
        self.assertFalse(util.is_float("None"))

    def test_string_not_numeric(self):
        self.assertFalse(util.is_float("abc"))

    def test_none(self):
        with self.assertRaises(TypeError):
            util.is_float(None)

    def test_array(self):
        with self.assertRaises(TypeError):
            util.is_float(np.ones(3))

if __name__ == "__main__":
    run_tests(TestInRange)
    run_tests(TestIsFloat)

max_stars_repo_name: clatlan/bcdi

max_issues_repo_name: clatlan/bcdi

max_forks_repo_name: clatlan/bcdi


text:
# import the necessary packages
from tensorflow.keras.applications import VGG16
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Dropout
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Input
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.preprocessing.image import load_img
from tensorflow.keras.utils import to_categorical
from sklearn.preprocessing import LabelBinarizer
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import numpy as np
import cv2
import os
import pickle

# initialize the list of data (images), class labels, target bounding
# box coordinates, and image paths
print("[INFO] loading dataset...")
data = []
labels = []
bboxes = []
imagePaths = []

# loop over all CSV files in the annotations directory
for csvPath in os.listdir('../input/bbox-regression1/bbox_dataset/annotations'):
	# load the contents of the current CSV annotations file
	rows = open(os.path.join('../input/bbox-regression1/bbox_dataset/annotations',csvPath)).read().strip().split("\n")

	# loop over the rows
	for row in rows:
		# break the row into the filename, bounding box coordinates,
		# and class label
		row = row.split(",")
		(filename, startX, startY, endX, endY, label) = row

		# derive the path to the input image, load the image (in
		# OpenCV format), and grab its dimensions
		imagePath = os.path.sep.join(['../input/bbox-regression1/bbox_dataset/images', label,
			filename])
		image = cv2.imread(imagePath)
		(h, w) = image.shape[:2]

		# scale the bounding box coordinates relative to the spatial
		# dimensions of the input image
		startX = float(startX) / w
		startY = float(startY) / h
		endX = float(endX) / w
		endY = float(endY) / h

		# load the image and preprocess it
		image = load_img(imagePath, target_size=(224, 224))
		image = img_to_array(image)

		# update our list of data, class labels, bounding boxes, and
		# image paths
		data.append(image)
		labels.append(label)
		bboxes.append((startX, startY, endX, endY))
		imagePaths.append(imagePath)

# convert the data, class labels, bounding boxes, and image paths to
# NumPy arrays, scaling the input pixel intensities from the range
# [0, 255] to [0, 1]
data = np.array(data, dtype="float32") / 255.0
labels = np.array(labels)
bboxes = np.array(bboxes, dtype="float32")
imagePaths = np.array(imagePaths)

# perform one-hot encoding on the labels
lb = LabelBinarizer()
labels = lb.fit_transform(labels)

# only there are only two labels in the dataset, then we need to use
# Keras/TensorFlow's utility function as well
if len(lb.classes_) == 2:
	labels = to_categorical(labels)

# partition the data into training and testing splits using 80% of
# the data for training and the remaining 20% for testing
split = train_test_split(data, labels, bboxes, imagePaths,
	test_size=0.20, random_state=42)

# unpack the data split
(trainImages, testImages) = split[:2]
(trainLabels, testLabels) = split[2:4]
(trainBBoxes, testBBoxes) = split[4:6]
(trainPaths, testPaths) = split[6:]

# load the VGG16 network, ensuring the head FC layers are left off
vgg = VGG16(weights="imagenet", include_top=False,
	input_tensor=Input(shape=(224, 224, 3)))

# freeze all VGG layers so they will *not* be updated during the
# training process
vgg.trainable = False

# flatten the max-pooling output of VGG
flatten = vgg.output
flatten = Flatten()(flatten)

# construct a fully-connected layer header to output the predicted
# bounding box coordinates
bboxHead = Dense(128, activation="relu")(flatten)
bboxHead = Dense(64, activation="relu")(bboxHead)
bboxHead = Dense(32, activation="relu")(bboxHead)
bboxHead = Dense(4, activation="sigmoid",
	name="bounding_box")(bboxHead)

# construct a second fully-connected layer head, this one to predict
# the class label
softmaxHead = Dense(512, activation="relu")(flatten)
softmaxHead = Dropout(0.5)(softmaxHead)
softmaxHead = Dense(512, activation="relu")(softmaxHead)
softmaxHead = Dropout(0.5)(softmaxHead)
softmaxHead = Dense(len(lb.classes_), activation="softmax",
	name="class_label")(softmaxHead)

# put together our model which accept an input image and then output
# bounding box coordinates and a class label
model = Model(
	inputs=vgg.input,
	outputs=(bboxHead, softmaxHead))

# define a dictionary to set the loss methods -- categorical
# cross-entropy for the class label head and mean absolute error
# for the bounding box head
losses = {
	"class_label": "categorical_crossentropy",
	"bounding_box": "mean_squared_error",
}

# define a dictionary that specifies the weights per loss (both the
# class label and bounding box outputs will receive equal weight)
lossWeights = {
	"class_label": 1.0,
	"bounding_box": 1.0
}

# initialize the optimizer, compile the model, and show the model
# summary
opt = Adam(lr=0.0001)
model.compile(loss=losses, optimizer=opt, metrics=["accuracy"], loss_weights=lossWeights)
print(model.summary())

# construct a dictionary for our target training outputs
trainTargets = {
	"class_label": trainLabels,
	"bounding_box": trainBBoxes
}

# construct a second dictionary, this one for our target testing
# outputs
testTargets = {
	"class_label": testLabels,
	"bounding_box": testBBoxes
}

# train the network for bounding box regression and class label
# prediction
print("[INFO] training model...")
H = model.fit(
	trainImages, trainTargets,
	validation_data=(testImages, testTargets),
	batch_size=32,
	epochs=20,
	verbose=1)

# serialize the model to disk
print("[INFO] saving object detector model...")
model.save('bboxweights.h5', save_format="h5")

# serialize the label binarizer to disk
print("[INFO] saving label binarizer...")
f = open('lb.pickle', "wb")
f.write(pickle.dumps(lb))
f.close()


text:
import sys
sys.path.append("/usr/local/lib/python2.7/site-packages")

import librosa
import essentia
from essentia.standard import *

import math
import gammatone as gt
from gammatone import filters
import numpy as np

import argparse

path = 'original_Bubble.wav'

FS = 44100

def feature_extract(x , start, end, frame_len, fs = FS):
    hop = frame_len / 2
    w_hann = Windowing(type='hann')
    W = w_hann(np.ones(frame_len, dtype='single'))
    spectrum = Spectrum()

    # spectral feature
    Env = Envelope()

    # sub-band init
    central_freq = gt.filters.centre_freqs(fs, 30, 20)
    erb_filter = gt.filters.make_erb_filters(fs, central_freq, width=1.0)

    loader = essentia.standard.MonoLoader(filename=x)
    audio = loader()

    feature = np.zeros((30, 5))

    # extract sub-band
    SB = gt.filters.erb_filterbank(audio[fs * start:fs * end], erb_filter)
    SB = np.array(SB, dtype='single')

    R = np.zeros((SB.shape[1] + frame_len * 2), dtype='single')
    for a, sb in enumerate(SB):
        r = np.zeros(len(sb) + frame_len * 2)

        frame_num = 0
        SKts = []
        for frame in FrameGenerator(sb, frameSize=frame_len, hopSize=hop):

            # Envelope
            Env = np.power(sum(np.power(w_hann(frame), 2)) / sum(np.power(W, 2)), 0.5)

            # compression
            SKt = np.power(Env, 0.3)
            SKts.append(SKt)

            # residual
            r[frame_num * hop:(frame_num * hop) + frame_len] = w_hann(frame / Env)
            frame_num += 1

        SKts = np.array(SKts, dtype='single')

        # feature computing
        m1 = sum(SKts) / frame_num
        m2 = sum(np.power(SKts - m1, 2)) / frame_num
        m3 = sum(np.power(SKts - m1, 3)) / frame_num
        m4 = sum(np.power(SKts - m1, 4)) / frame_num

        M1 = m1
        M2 = m2 / np.power(m1, 2)
        M3 = m3 / np.power(m2, 1.5)
        M4 = m4 / np.power(m2, 2)

        feature[a][0] = M1
        feature[a][1] = M2
        feature[a][2] = M3
        feature[a][3] = M4

        feature[a][4] = np.var(sb)
        # residual
        R += gt.filters.erb_filterbank(r, np.reshape(erb_filter[a], (1, 10)))[0]

    # All pole filter extract with LPC
    lpc = LPC(order=199)
    frame_num = 0
    A = np.zeros(200)
    s = Spectrum()
    for frame in FrameGenerator(R, frameSize=fs, hopSize=fs / 2):
        a, k = lpc(s(w_hann(frame)))
        A += a
        frame_num += 1

    A /= frame_num
    return feature, A

def synth(feature, itertime, sample_len, learning_rate, momentum, AR_coe, fs = FS):
    frame_len = 2048
    hop = frame_len / 2

    # white noise
    mean = 0
    std = 1
    num_samples = sample_len
    sample = np.random.normal(mean, std, size=num_samples)

    # gammatone filter-bank
    central_freq = gt.filters.centre_freqs(fs, 30, 20)
    erb_filter = gt.filters.make_erb_filters(fs, central_freq, width=1.0)

    # compress and Envelope
    Env = Envelope()

    # window
    w_hann = Windowing(type='hann')
    W = w_hann(np.ones(frame_len, dtype='single'))
    # learning rate
    lr = learning_rate
    # momentum
    alpha = momentum

    mom_M1 = 0
    mom_M2 = 0
    mom_M3 = 0
    mom_M4 = 0

    _iter = 0

    stactic = np.zeros((30, 5))

    SB_sample = np.array(gt.filters.erb_filterbank(sample, erb_filter), dtype='single')
    new_sample = np.zeros((30, sample_len))
    SKts_full = []
    for a, sb in enumerate(SB_sample):
        SKts = []
        frame_num = 0
        for frame in FrameGenerator(sb, frameSize=frame_len, hopSize=hop):

            Env = np.power(sum(np.power(w_hann(frame), 2)) / sum(np.power(W, 2)), 0.5)
            # compression
            SKt = np.power(Env, 0.3)

            SKts.append(SKt)

        SKts = np.array(SKts, dtype='single')
        SKts_full.append(SKts)

    SKts_full = np.array(SKts_full, dtype='single')

    frame_num = SKts_full.shape[1]
    while (_iter < itertime):
        for a in xrange(30):
            # gradient decent in features
            # M1~4 caculation
            m1 = sum(SKts_full[a]) / frame_num
            m2 = sum(np.power(SKts_full[a] - m1, 2, dtype=np.float128)) / frame_num
            m3 = sum(np.power(SKts_full[a] - m1, 3, dtype=np.float128)) / frame_num
            m4 = sum(np.power(SKts_full[a] - m1, 4, dtype=np.float128)) / frame_num

            M1 = m1
            M2 = m2 / np.power(m1, 2)
            M3 = m3 / np.power(m2, 1.5)
            M4 = m4 / np.power(m2, 2)

            stactic[a][0] = M1
            stactic[a][1] = M2
            stactic[a][2] = M3
            stactic[a][3] = M4

            # errors
            err_M1 = (feature[a][0] - M1)
            err_M2 = (feature[a][1] - M2)
            err_M3 = (feature[a][2] - M3)
            err_M4 = (feature[a][3] - M4)

            # update in each envelope
            for b in xrange(SKts_full.shape[1]):
                # M2 update
                delta_2 = (1.0 / frame_num) * 2 * (SKts_full[a][b] - M1)
                #                       why not m1^2
                deri_M2 = (err_M2) * (delta_2 * np.power(m1, 2) - m2 * 2 * m1 * (1 / frame_num)) / np.power(m1, 4,
                                                                                dtype=np.float128)
                SKts_full[a][b] += lr * ((alpha * deri_M2) + (1 - alpha) * mom_M2)
                mom_M2 = deri_M2

                # M3 update
                delta_3 = (1.0 / frame_num) * 3 * np.power((SKts_full[a][b] - M1), 2)
                deri_M3 = (err_M3) * (delta_3 * np.power(m2, 1.5) - m3 * 1.5 * np.power(m2, 0.5) * delta_2) / np.power(
                    m2, 3)
                SKts_full[a][b] += lr / 10.0 * ((alpha * deri_M3) + (1 - alpha) * mom_M3)
                mom_M3 = deri_M3

                # M4 update
                delta_4 = (1.0 / frame_num) * 4 * np.power((SKts_full[a][b] - M1), 3)
                deri_M4 = (err_M4) * (delta_4 * np.power(m2, 2) - m4 * 2 * m2 * delta_2) / np.power(m2, 4)
                SKts_full[a][b] += lr / 100.0 * ((alpha * deri_M4) + (1 - alpha) * mom_M4)
                mom_M4 = deri_M4

            # mean adjustment
            SKts_full[a] = SKts_full[a] * (feature[a][0] / M1)

            error = sum(np.power(feature - stactic, 2))

        _iter += 1

    # residual synthesize

    A = np.array(AR_coe, dtype='single')
    B = np.ones(1, dtype='single')
    h = IIR(denominator=A, numerator=B)
    residual = h(np.array(sample, dtype='single'))

    SB_resi = np.array(gt.filters.erb_filterbank(residual, erb_filter), dtype='single')
    # envelope combined with residual
    SB_result = np.zeros((30, sample_len + frame_len * 2))
    result = np.zeros(sample_len + frame_len * 2)
    for a, sb in enumerate(SB_resi):
        frame_num = 0
        for frame in FrameGenerator(sb, frameSize=frame_len, hopSize=hop):
            Env = np.power(sum(np.power(w_hann(frame), 2)) / sum(np.power(W, 2)), 0.5)

            # compression
            SKt = np.power(Env, 0.3)

            # new envelope combined with filtered residual
            SB_result[a][frame_num * hop:(frame_num * hop) + frame_len] = w_hann(frame / Env) * SKts_full[a][frame_num]

            frame_num += 1

        # varience adjustment
        SB_result[a] *= np.power(feature[a][4] / np.var(SB_result[a]), 0.5)

        # subband combined
        result += gt.filters.erb_filterbank(SB_result[a], np.reshape(erb_filter[a], (1, 10)))[0]

    return result, stactic

def main():
    # parse arguments
    parser = argparse.ArgumentParser()
    parser.add_argument('-i', '--input_path', help = "path to inpupt file (source audio)",
                        type = str, default = 'original_Bubble.wav')
    parser.add_argument('-o', '--output_name', help = "name of output file",
                        type = str, default = 'out.wav')
    parser.add_argument('-l', '--output_length', help = "length of output file(in seconds)",
                        type = int, default = 5)
    parser.add_argument('-fs', '--sample_rate', help="sample rate",
                        type = int, default = FS)
    parser.add_argument('-it', '--iter_time', help="Maximum iteration time for gradient decent(in seconds)",
                        type = int, default = 60)
    parser.add_argument('-lr', '--learning_rate', help="learning rate for gradient decent",
                        type = float, default = 0.3)

    args = parser.parse_args()
    print(args)

    input_path = args.input_path
    output_name = args.output_name
    sample_rate = args.sample_rate
    out_len = args.output_length
    iter_time = args.iter_time
    learning_rate = args.learning_rate

    # feature is the source's statistic, a is the all pole filter parameter from LPC
    print("Analyzing")
    feature, a = feature_extract(input_path, 5, 30, 2048, fs = sample_rate)

    _len = sample_rate * out_len

    print("Synthesizing")
    s, stactic = synth(feature, iter_time, _len, learning_rate, 1, a, fs = sample_rate)

    librosa.output.write_wav(output_name, s, sample_rate, norm=False)
    print("Finish!")

if __name__ == '__main__':
    main()


text:
import argparse
import os
import pickle
import subprocess as sp
import time

import numpy as np
from tqdm.auto import tqdm

BASE_FREQ_HZ = 27.5  # A0
OCTAVE_RANGE = 8  # 8 * 12 = 96 unique pitches (8 higher than the range of a piano)

class GaborWaveletTransform:
    def __init__(self, partial_transforms, freqs, strides, sampling_rate_hz=44_100.0):
        self.sampling_rate_hz = sampling_rate_hz
        self.partial_transforms = partial_transforms
        self.strides = strides
        self.freqs = freqs

    @classmethod
    def from_audio(cls, audio, sampling_rate_hz=44_100.0, pitch_oversampling=1):
        audio = np.mean(audio, axis=1).astype(audio.dtype)
        assert audio.flags["C_CONTIGUOUS"]  # necessary for strided convolution
        dsize = audio.itemsize
        st = time.time()

        pitches_per_octave = 12 * pitch_oversampling

        beta = 16
        # 6 SDs from gaussian center
        base_width = 6 * beta * (sampling_rate_hz / BASE_FREQ_HZ) // 2 * 2 + 1

        octave_transforms = []
        octave_freqs = []
        octave_strides = []
        for octave in tqdm(range(OCTAVE_RANGE), desc="Computing octaves of transform"):
            filter_width = int((base_width / 2 ** octave) // 2 * 2 + 1)
            octave_base_freq = BASE_FREQ_HZ * 2 ** octave
            t = (
                np.arange(-(filter_width // 2), filter_width // 2 + 1)[:, np.newaxis]
                / sampling_rate_hz
            )
            freqs = octave_base_freq * 2 ** (np.arange(pitches_per_octave) / pitches_per_octave)
            octave_freqs.append(freqs)
            # doing this properly with complex numbers takes about 2x as long in numpy for some reason
            # TODO: try removing DC component of wavelets (subtract gaussian with same width to make
            # sum of filter 0)
            # TODO: turn filter bank generation into func
            # TODO (perf): compare speed computing one conv at a time
            wbank = np.hstack(
                [
                    freqs
                    / sampling_rate_hz
                    * fn(2 * np.pi * t * freqs)
                    * np.exp(-((t * freqs / beta) ** 2))
                    for fn in [np.sin, np.cos]
                ]
            ).astype(np.float32)

            oct_scale = 2 ** (OCTAVE_RANGE - octave - 1)
            skip_len = round(sampling_rate_hz / octave_base_freq * beta / 2 / oct_scale) * oct_scale
            octave_strides.append(skip_len)
            # ensure initial conv value has wavelet centered at t=0
            padded = np.pad(audio, filter_width // 2)
            n_skip = (len(padded) - filter_width) // skip_len + 1
            # TODO (perf): try downsampling before convolution for more speed?
            # TODO: turn strided convolution with multiple kernels at once into func
            view = np.lib.stride_tricks.as_strided(
                padded, shape=(n_skip, filter_width), strides=(dsize * skip_len, dsize)
            )
            res = view.dot(wbank)
            res = np.sqrt(res[:, :pitches_per_octave] ** 2 + res[:, -pitches_per_octave:] ** 2)
            octave_transforms.append(res)

        print(f"Elapsed: {time.time() - st:.3f} s")
        return cls(
            octave_transforms, octave_freqs, octave_strides, sampling_rate_hz=sampling_rate_hz
        )

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Compute Gabor wavelet transform.")
    parser.add_argument("input_filename", help="audio file to process")
    parser.add_argument("output_filename", help="filename to write transform to")
    parser.add_argument("--channels", type=int, default=2, help="number of channels in audio data")
    parser.add_argument(
        "--sampling_rate_hz", type=float, default=44_100.0, help="sampling rate of audio"
    )
    parser.add_argument("--bit_depth", type=int, default=16, help="audio bit depth")
    parser.add_argument(
        "--pitch-oversampling", type=int, default=1, help="pitches to sample per half step"
    )
    args = parser.parse_args()

    assert args.bit_depth in {8, 16, 32}

    command = [
        "ffmpeg",
        "-i",
        args.input_filename,
        "-f",
        f"s{args.bit_depth}le",
        "-acodec",
        f"pcm_s{args.bit_depth}le",
        "-ar",
        str(args.sampling_rate_hz),
        "-ac",
        str(args.channels),
        "-",
    ]

    print("Loading audio data...")
    with open("ffmpeg_log.txt", "w") as fp:
        pipe = sp.Popen(command, stdout=sp.PIPE, stderr=fp, bufsize=10 ** 8)

    raw_bytes = pipe.stdout.read()
    audio = np.fromstring(raw_bytes, dtype=f"int{args.bit_depth}")
    audio = audio.reshape((audio.shape[0] // args.channels, args.channels))

    transform = GaborWaveletTransform.from_audio(
        audio, sampling_rate_hz=args.sampling_rate_hz, pitch_oversampling=args.pitch_oversampling
    )
    with open(args.output_filename, "wb") as f:
        pickle.dump(transform, f)

max_stars_repo_name: cericson/music-viz

max_issues_repo_name: cericson/music-viz

max_forks_repo_name: cericson/music-viz


text:
# Copyright (c) 2020 Institution of Parallel and Distributed System, Shanghai Jiao Tong University
# ServerlessBench is licensed under the Mulan PSL v1.
# You can use this software according to the terms and conditions of the Mulan PSL v1.
# You may obtain a copy of Mulan PSL v1 at:
#     http://license.coscl.org.cn/MulanPSL
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
# PURPOSE.
# See the Mulan PSL v1 for more details.
#

# This file defines some helper functions
import time
import random
import numpy as np
def binarySearch(nums, target):
    """
    :type nums: List[int]
    :type target: int
    :rtype: int
    """
    upper = len(nums) - 1
    lower = 0
    while(lower <= upper):
        mid = int((upper + lower) / 2)
        if target > nums[mid]:
            lower = mid + 1
        elif target < nums[mid - 1]:
            upper = mid - 1
        else:
            return mid

    return mid
def getTime():
    return int(round(time.time() * 1000))

def alu(times):
    startTime = getTime()
    base = 10000
    a = random.randint(10, 100)
    b = random.randint(10, 100)
    temp = 0
    while True:
        for i in range(base):
            if i % 4 == 0:
                temp = a + b
            elif i % 4 == 1:
                temp = a - b
            elif i % 4 == 2:
                temp = a * b
            else:
                temp = a / b
        endTime = getTime()
        if endTime - startTime > times:
            break
    return temp

def getRandFloatRefByCDF(CDFFilename):
    fCDF = open(CDFFilename, 'r')
    values = []
    P = []
    for line in fCDF:
        lineSplit = line.split(",")
        values.append(float(lineSplit[0]))
        P.append(float(lineSplit[1]))

    randP = random.random()
    randValue = values[binarySearch(P, randP)]
    fCDF.close()
    return randValue

def getRandValueRefByCDF(CDFFilename):
    fCDF = open(CDFFilename, 'r')
    values = []
    P = []
    for line in fCDF:
        lineSplit = line.split(",")
        values.append(int(lineSplit[0]))
        P.append(float(lineSplit[1]))

    randP = random.random()
    randValue = values[binarySearch(P, randP)]
    fCDF.close()
    return randValue

# Generate Gaussion distribution data series according to the mean value
def genNorm(mean):
    CV = getRandCV()
    print("CV: %d" %CV)
    Norm=np.random.normal(loc=mean, scale=CV*mean, size=10)
    print(Norm)

def getRandCV():
    return random.randint(0,2) * 0.5

if __name__=='__main__':
    genNorm(10)

max_stars_repo_name: hunhoffe/ServerlessBench

max_issues_repo_name: hunhoffe/ServerlessBench

max_forks_repo_name: hunhoffe/ServerlessBench


text:
"""
Created on Sun Jul  4 12:29:20 2021

@author: Fiora
"""
""" Exercise 2
    Affine and metric transformations
"""

import numpy as np
import numpy.matlib
from PIL import Image
from matplotlib import pyplot as plt
from matplotlib.pyplot import ginput
from matplotlib import rcParams
from skimage.transform import warp, AffineTransform, ProjectiveTransform
from skimage.color import rgb2gray
import scipy
from scipy import linalg, matrix

def crop_imm(imms, ss):

    ttx, tty = np.where(imms[:,:,0]>0)
    mm, nn = np.max(ttx), np.max(tty)
    mu, nu = np.min(ttx), np.min(tty)
    left, top, right, bottom = mu, nu, nn, mm
    immx = Image.fromarray(((imms*255).astype(np.uint8)))
    im_cropped = immx.crop((left, top, right, bottom))
    # im_resh = im_cropped.resize((ss[0], ss[1]),  Image.ANTIALIAS)
    return im_cropped

def projToAffine(imm):
    """ Exercise 2 questions 1,2 and 3:
        select 8 points, use them to compute parallel lines and points at infinity.
        Use points at infinity to build line at infinity.
        Build the homography H mapping the projective immage to an affine one
    """

    ### pick up and draw the points to define the line at infinity
    plt.imshow(imm)
    plt.title('Choose 8 point: 2 points per line: parallel lines along two orthogonal dir.')
    l1p = plt.ginput(8,timeout=60, show_clicks=True)
    #### first line
    x_values = [l1p[0][0], l1p[1][0]]
    y_values = [l1p[0][1], l1p[1][1]]
    plt.plot(x_values, y_values,'r', label = "line 1", linewidth=2)

    #### second line
    x_values = [l1p[2][0], l1p[3][0]]
    y_values = [l1p[2][1], l1p[3][1]]
    plt.plot(x_values, y_values,'r', label = "line 2", linewidth=2)

    #### third line
    x_values = [l1p[4][0], l1p[5][0]]
    y_values = [l1p[4][1], l1p[5][1]]
    plt.plot(x_values, y_values,'b', label = "line 3", linewidth=2)
    #### fourth line
    x_values = [l1p[6][0], l1p[7][0]]
    y_values = [l1p[6][1], l1p[7][1]]
    plt.plot(x_values, y_values,'b', label = "line 4", linewidth=2)
    #

    ### pick up and draw the points to define the line at infinity

    """ compute the points at infinity """
    lp = np.array(l1p,dtype= float)
    lp = np.column_stack([lp, np.matlib.repmat(1, len(lp),1)])
    l1 = np.cross(lp[0,:], lp[1,:])
    l1 = l1/l1[2]
    l2 = np.cross(lp[2,:], lp[3,:])
    l2 = l2/l2[2]
    l3 = np.cross(lp[4,:], lp[5,:])
    l3 = l3/l3[2]
    l4 = np.cross(lp[6,:], lp[7,:])
    l4 = l4/l4[2]

    p1_at_inf = np.cross(l1, l2)
    p1_at_inf = p1_at_inf/p1_at_inf[2]

    p2_at_inf = np.cross(l3,l4)
    p2_at_inf = p2_at_inf/p2_at_inf[2]

    ### Show the point at infinity
    plt.scatter(p1_at_inf[0], p1_at_inf[1], c='g', label = 'point 1 at inf')
    plt.scatter(p2_at_inf[0], p2_at_inf[1], c='g', label = 'point 2 at inf')
    x_values = [p1_at_inf[0]/p1_at_inf[2], p2_at_inf[0]/p2_at_inf[2]]
    y_values = [p1_at_inf[1]/p1_at_inf[2], p2_at_inf[1]/p2_at_inf[2]]
    plt.plot(x_values, y_values,'g', label = "line at  inf", linewidth=2)

    plt.legend()
    "The line at infinity"
    l_at_inf = np.cross(p1_at_inf, p2_at_inf)
    l_at_inf = l_at_inf/l_at_inf[2]

    """ The rectification matrix """

    H = np.row_stack([np.array([1,0,0]),np.array([0,1,0]), l_at_inf])

    ### verify that H^(-1T) * l_at_inf =[0 0 1]^T

    Linf = np.dot(np.linalg.inv(H.T), l_at_inf)

    ### warping
    tform = ProjectiveTransform(H)
    immTr = warp(np.array(imm), tform.inverse, output_shape =(imm.size[1], imm.size[0]))
    imm_aff = crop_imm(immTr, imm.size)
    plt.figure(2)
    plt.imshow(imm_aff)
    plt.title('Affine Image')
    return imm_aff, H

def affineToMetric(aff_imm, imm):
    """ LAST QUESTION of Exercise 2: note that we obtained an affine rectification
        and not a metric rectification.
        To obtain a metric rectification from the affine one we need to find 2 constraints
        These two constraints are given by two pairs of orthogonal lines L1, M1
        and L2, M2 in the affine transformation corresponding to two pairs of
        orthogonal lines in real world. These two constraints are
        enough  to compute the dual conic at infinity
    """

    plt.imshow(aff_imm)
    plt.title(
        'Choose 8 points so as to obtain a pair of orthogonal lines: lines of the orthogonal pair should not be parallel')
    l1A = plt.ginput(8, timeout=60, show_clicks=True)

    #first line
    x_values = [l1A[0][0], l1A[1][0]]
    y_values = [l1A[0][1], l1A[1][1]]
    plt.plot(x_values, y_values,'r', label = "line 1", linewidth=2)

    #### second line
    x_values = [l1A[2][0], l1A[3][0]]
    y_values = [l1A[2][1], l1A[3][1]]
    plt.plot(x_values, y_values,'r', label = "line 2", linewidth=2)

    #### third line
    x_values = [l1A[4][0], l1A[5][0]]
    y_values = [l1A[4][1], l1A[5][1]]
    plt.plot(x_values, y_values,'b', label = "line 3", linewidth=2)

    #### fourth line
    x_values = [l1A[6][0], l1A[7][0]]
    y_values = [l1A[6][1], l1A[7][1]]
    plt.plot(x_values, y_values,'b', label = "line 4", linewidth=2)

    plt.legend()

    LM = np.array(l1A,dtype= float)
    LM = np.column_stack([LM, np.matlib.repmat(1, len(LM),1)])
    """ Now we have to solve:
        L1^T  [KK^T 0] M1 = 0
              [ 0^T 0]
       Let L1 =[l11, l12,L13]^T and M1 =[m11,m12,m13]^T,
       then the above equation reduces to
       (l11*m11, l11*m12+l12*m11, l12*m12)s = 0
       where s is the upper triangular part of KK^T
    """
    L1 = np.cross(LM[0,:], LM[1,:])
    L1 =L1/L1[2]
    M1 = np.cross(LM[2,:],LM[3,:])
    M1 = M1/M1[2]

    constraint1 = (L1[0]*M1[0], L1[0]*M1[1]+L1[1]*M1[0], L1[1]*M1[1])

    L2 = np.cross(LM[4,:], LM[5,:])
    L2 =L2/L2[2]
    M2 = np.cross(LM[6,:],LM[7,:])
    M2 = M2/M2[2]

    constraint2 = (L2[0]*M2[0], L2[0]*M2[1]+L2[1]*M2[0], L2[1]*M2[1])

    ### We now obtain s as the null space of vecs
    C = np.row_stack([constraint1,constraint2])

    s = scipy.linalg.null_space(C)
    s1 = np.insert(s,2,s[1])
    S = np.reshape(s1,(2,2))

    """ If S is singular you might have chosen wrong points"""
    ##verify S is non singular you might have chosen wrong points
    np.all(np.linalg.eigvals(S) > 0)
    ### Verify:
    cc1 = np.dot(L1[:2],np.dot(S,M1[:2]))
    cc2 = np.dot(L2[:2],np.dot(S,M2[:2]))
    np.allclose(cc1, 0.0, rtol=1e-05, atol=1e-08, equal_nan=False)
    np.allclose(cc2, 0.0, rtol=1e-05, atol=1e-08, equal_nan=False)

    ## To obtain K compute now the cholesky factorization of S
    K = linalg.cholesky(S, lower = False)
    Kx = K.copy()
    Kx[1,0] = K[0,1]
    ###############################

    HA = np.row_stack([np.column_stack([Kx,np.zeros(2)]), np.array([0,0,1])])

    ### Verify
    np.allclose(K@K.T-S, 0.0, rtol=1e-05, atol=1e-08, equal_nan=False)
    tform = AffineTransform(HA.T)
    immM = warp(np.array(aff_imm), tform,  output_shape = (2*imm.size[0],2*imm.size[1]))
    imm_metric = crop_imm(immM, imm.size)
    plt.figure(2)
    plt.imshow(imm_metric)

    return imm_metric

def main():
    plt.close('all')
    imm = Image.open('piastrelle.png')
    aff_imm,Hp = projToAffine(imm)
    metr_imm = affineToMetric(aff_imm, imm)
    plt.close('all')
    rcParams['figure.figsize'] = 18,11
    fig, axs = plt.subplots(1, 3)
    fig = plt.gcf()
    fig.suptitle("Transformation from projective to affine and from affine to metric", fontsize=14)
    axs[0].imshow(imm)
    axs[0].title.set_text('original image')
    axs[1].imshow(aff_imm)
    axs[1].title.set_text('affine transformation')
    axs[2].imshow(metr_imm)
    axs[2].title.set_text('metric transformation')

reason: import numpy,import scipy,from scipy


text:
import unittest
import numpy as np
import MTSGL.proximal

class TestProximalSGL(unittest.TestCase):

	def test_proximal_sgl_inf(self):
		# see proximal_sgl.m for MATLAB code to get solutions
		try:
			np.testing.assert_array_almost_equal(
				MTSGL.proximal.proximal_sgl(np.array([1, 2, -6, -8, 0]), 1.5, 'inf', 0.4),
				np.array([0.4, 1.4, -5.4, -6.5, 0.])
			)
			res = True
		except AssertionError as err:
			res = False
			print(err)
		self.assertTrue(res, "did not yield the correct proximal (q='inf')")

	def test_proximal_sgl_2(self):
		try:
			np.testing.assert_array_almost_equal(
				MTSGL.proximal.proximal_sgl(np.array([1, 2, -6, -8, 0]), 1.5, 2, 0.4),
				np.array([0.36118923, 1.26416229, -4.87605456, -6.68200069, 0.])
			)
			res = True
		except AssertionError as err:
			res = False
			print(err)
		self.assertTrue(res, "did not yield the correct proximal (q=2)")

	def test_proximal_sgl_gl(self):
		try:
			np.testing.assert_array_almost_equal(
				MTSGL.proximal.proximal_sgl(np.array([1, 2, -6, -8, 0]), 1.5, 'inf', 0.),
				np.array([1., 2., -6., -6.5, 0.])
			)
			res = True
		except AssertionError as err:
			res = False
			print(err)
		self.assertTrue(res, "did not yield the correct proximal (q=2, group Lasso)")

	def test_proximal_sgl_lasso(self):
		try:
			np.testing.assert_array_almost_equal(
				MTSGL.proximal.proximal_sgl(np.array([1, 2, -6, -8, 0]), 1.5, 2, 1.),
				np.array([0., 0.5, -4.5, -6.5, 0.])
			)
			res = True
		except AssertionError as err:
			res = False
			print(err)
		self.assertTrue(res, "did not yield the correct proximal (Lasso)")

	def test_proximal_sgl_bad_q(self):
		self.assertRaises(
			ValueError,
			MTSGL.proximal.proximal_sgl,
			np.array([1]), 1.5, 3, 0.4
		)

	def test_proximal_sgl_bad_tau(self):
		self.assertRaises(
			ValueError,
			MTSGL.proximal.proximal_sgl,
			np.array([1]), -1.5, 3, 0.4
		)

	def test_proximal_sgl_bad_alpha(self):
		self.assertRaises(
			ValueError,
			MTSGL.proximal.proximal_sgl,
			np.array([1]), 1.5, 3, 1.4
		)

if __name__ == '__main__':
	unittest.main()


text:
# Mohammad Saad
# Model for Deeper Depth Prediction
# 2/19/2018

import torch
import torch.nn as nn
import torchvision.transforms as transforms
from torch.autograd import Variable

import numpy as np

class ResidualBlock(nn.Module):

	def __init__(self, in_channels, d1, d2, stride = 1):
		super(ResidualBlock, self).__init__()

		# leading into d1
		self.conv1 = nn.Conv2d(in_channels, d1, 1, stride = stride, bias = False)
		self.bn1 = nn.BatchNorm2d(d1)
		self.relu1 = nn.ReLU(inplace = True)

		# leading into d1-2
		self.conv2 = nn.Conv2d(d1, d1, 3, padding = 1, bias = False)
		self.bn2 = nn.BatchNorm2d(d1)
		self.relu2 = nn.ReLU(inplace = True)

		# leading into d2
		self.conv3 = nn.Conv2d(d1, d2, 1, bias = False)
		self.bn3 = nn.BatchNorm2d(d2)

		# if not self.skip:
		#     self.conv4 = nn.Conv2d(in_channels, d2, stride, bias = False)
		#     self.bn4 = nn.BatchNorm2d(d2)

		# final Relu at end of layer
		self.relu3 = nn.ReLU(inplace = True)

	def forward(self, x):

		out = self.conv1(x)
		out = self.bn1(out)
		out = self.relu1(out)

		out = self.conv2(out)
		out = self.bn2(out)
		out = self.relu2(out)

		out = self.conv3(out)
		out = self.bn3(out)

		out += x

		out = self.relu3(out)

		return out

class ProjectionBlock(nn.Module):

	def __init__(self, in_channels, d1, d2, stride = 1):
		super(ProjectionBlock, self).__init__()

		# feeding into first d1 block
		self.conv1 = nn.Conv2d(in_channels, d1, 1, stride = stride, bias = False)
		self.bn1 = nn.BatchNorm2d(d1)
		self.relu1 = nn.ReLU(inplace = True)

		# feeding into second d1 block
		self.conv2 = nn.Conv2d(d1, d1, 3, padding = 1, bias = False)
		self.bn2 = nn.BatchNorm2d(d1)
		self.relu2 = nn.ReLU(inplace = True)

		# feeding into first d2 block
		self.conv3 = nn.Conv2d(d1, d2, 1, bias = False)
		self.bn3 = nn.BatchNorm2d(d2)

		# feeding into second d2 block
		self.conv4 = nn.Conv2d(in_channels, d2, 1, stride = stride, bias = False)
		self.bn4 = nn.BatchNorm2d(d2)

		self.relu3 = nn.ReLU(inplace = True)

	def forward(self, x):
		out = self.conv1(x)
		out = self.bn1(out)
		out = self.relu1(out)

		out = self.conv2(out)
		out = self.bn2(out)
		out = self.relu2(out)

		out = self.conv3(out)
		out = self.bn3(out)

		# do residual branch
		residual = x
		residual = self.conv4(residual)
		residual = self.bn4(residual)

		out += residual

		out = self.relu3(out)

		return out

# Fast Up Convolution from the paper, including the interleaving step
class FastUpConvolution(nn.Module):
	def __init__(self, in_channels, out_channels, batch_size):
		super(FastUpConvolution, self).__init__()

		self.batch_size = batch_size

		# do 4 convolutions on the same output with different kernels
		self.conv1 = nn.Conv2d(in_channels, out_channels, (3,3))
		self.conv2 = nn.Conv2d(in_channels, out_channels, (2,3))
		self.conv3 = nn.Conv2d(in_channels, out_channels, (3,2))
		self.conv4 = nn.Conv2d(in_channels, out_channels, (2,2))

		self.bn = nn.BatchNorm2d(out_channels)

	# this code comes from here: https://github.com/iapatil/depth-semantic-fully-conv
	def prepare_indices(self, before, row, col, after, dims):

		x0, x1, x2, x3 = np.meshgrid(before, row, col, after)
		x_0 = torch.from_numpy(x0.reshape([-1]))
		x_1 = torch.from_numpy(x1.reshape([-1]))
		x_2 = torch.from_numpy(x2.reshape([-1]))
		x_3 = torch.from_numpy(x3.reshape([-1]))

		linear_indices = x_3 + dims[3] * x_2  + 2 * dims[2] * dims[3] * x_0 * 2 * dims[1] + 2 * dims[2] * dims[3] * x_1
		return linear_indices

	# interleaving operation
	def interleave_helper(self, tensors, axis):
		tensor_shape = list(tensors[0].size())

		# pretty much a tensorflow equivalent. prepend a [-1], stack the tensors, then reshape them

		tensor_shape[axis+1] *= 2
		return torch.stack(tensors, axis + 1).view(tensor_shape)

	# this code comes from here: https://github.com/iapatil/depth-semantic-fully-conv
	def interleave(self, out1, out2, out3, out4):

		out1 = out1.permute(0, 2, 3, 1)
		out2 = out2.permute(0, 2, 3, 1)
		out3 = out3.permute(0, 2, 3, 1)
		out4 = out4.permute(0, 2, 3, 1)

		dims = out1.size()
		dim1 = dims[1] * 2
		dim2 = dims[2] * 2

		A_row_indices = range(0, dim1, 2)

		A_col_indices = range(0, dim2, 2)
		B_row_indices = range(1, dim1, 2)
		B_col_indices = range(0, dim2, 2)
		C_row_indices = range(0, dim1, 2)
		C_col_indices = range(1, dim2, 2)
		D_row_indices = range(1, dim1, 2)
		D_col_indices = range(1, dim2, 2)

		all_indices_before = range(int(self.batch_size))
		all_indices_after = range(dims[3])

		A_linear_indices = self.prepare_indices(all_indices_before, A_row_indices, A_col_indices, all_indices_after, dims)
		B_linear_indices = self.prepare_indices(all_indices_before, B_row_indices, B_col_indices, all_indices_after, dims)
		C_linear_indices = self.prepare_indices(all_indices_before, C_row_indices, C_col_indices, all_indices_after, dims)
		D_linear_indices = self.prepare_indices(all_indices_before, D_row_indices, D_col_indices, all_indices_after, dims)

		A_flat = (out1.permute(1, 0, 2, 3)).contiguous().view(-1)
		B_flat = (out2.permute(1, 0, 2, 3)).contiguous().view(-1)
		C_flat = (out3.permute(1, 0, 2, 3)).contiguous().view(-1)
		D_flat = (out4.permute(1, 0, 2, 3)).contiguous().view(-1)

		size_ = A_linear_indices.size()[0] + B_linear_indices.size()[0]+C_linear_indices.size()[0]+D_linear_indices.size()[0]

		Y_flat = torch.FloatTensor(size_).zero_()

		Y_flat.scatter_(0, A_linear_indices.squeeze(),A_flat.data)
		Y_flat.scatter_(0, B_linear_indices.squeeze(),B_flat.data)
		Y_flat.scatter_(0, C_linear_indices.squeeze(),C_flat.data)
		Y_flat.scatter_(0, D_linear_indices.squeeze(),D_flat.data)

		Y = Y_flat.view(-1, dim1, dim2, dims[3])
		Y=Variable(Y.permute(0,3,1,2))

		return Y

	def forward(self, x):
		out1 = self.conv1(nn.functional.pad(x, (1,1,1,1)))
		out2 = self.conv2(nn.functional.pad(x, (1,1,1,0)))
		out3 = self.conv3(nn.functional.pad(x, (1,0,1,1)))
		out4 = self.conv4(nn.functional.pad(x, (1,0,1,0)))

		out = self.interleave(out1, out2, out3, out4)

		out = self.bn(out)

		return out

class FastUpProjection(nn.Module):

	def __init__(self, in_channels, out_channels, batch_size):
		super(FastUpProjection, self).__init__()

		self.UpConv1 = FastUpConvolution(in_channels, out_channels, batch_size)
		self.relu1 = nn.ReLU(inplace = True)
		self.bn = nn.BatchNorm2d(out_channels)

		self.UpConv2 = FastUpConvolution(in_channels, out_channels, batch_size)

		self.conv1 = nn.Conv2d(out_channels, out_channels, 3, padding = 1)
		self.relu2 = nn.ReLU(inplace = True)

	def forward(self, x):
		out1 = self.UpConv1(x)
		out2 = self.UpConv2(x)

		out1 = self.relu1(out1)
		out1 = self.conv1(out1)
		out1 = self.bn(out1)

		out = out1 + out2
		out = self.relu2(out)

		return out

class Model(nn.Module):

	def __init__(self, batch_size):
		super(Model, self).__init__()
		self.batch_size = batch_size

		self.conv1 = nn.Conv2d(3, 64, kernel_size = 7, stride = 2, padding = 4)
		self.bn1 = nn.BatchNorm2d(64)
		self.relu1 = nn.ReLU(inplace = True)
		self.max_pool1 = nn.MaxPool2d(3, stride = 2)

		self.proj1 = ProjectionBlock(64, d1 = 64, d2 = 256, stride = 1)
		self.res1_1 = ResidualBlock(256, d1 = 64, d2 = 256, stride = 1)
		self.res1_2 =  ResidualBlock(256, d1 = 64, d2 = 256, stride = 1)

		self.proj2 = ProjectionBlock(256, d1 = 128, d2 = 512, stride = 2)
		self.res2_1 = ResidualBlock(512, d1 = 128, d2 = 512, stride = 1)
		self.res2_2 = ResidualBlock(512, d1 = 128, d2 = 512, stride = 1)
		self.res2_3 = ResidualBlock(512, d1 = 128, d2 = 512, stride = 1)

		self.proj3 = ProjectionBlock(512, d1 = 256, d2 = 1024, stride = 2)
		self.res3_1 = ResidualBlock(1024, d1 = 256, d2 = 1024)
		self.res3_2 = ResidualBlock(1024, d1 = 256, d2 = 1024)
		self.res3_3 = ResidualBlock(1024, d1 = 256, d2 = 1024)
		self.res3_4 = ResidualBlock(1024, d1 = 256, d2 = 1024)
		self.res3_5 = ResidualBlock(1024, d1 = 256, d2 = 1024)

		self.proj4 = ProjectionBlock(1024, d1 = 512, d2 = 2048, stride = 2)
		self.res4_1 = ResidualBlock(2048, d1 = 512, d2 = 2048)
		self.res4_2 = ResidualBlock(2048, d1 = 512, d2 = 2048)

		self.conv2 = nn.Conv2d(2048, 1024, kernel_size = 1)
		self.bn2 = nn.BatchNorm2d(1024)

		self.UpProj1 = FastUpProjection(1024, 512, self.batch_size)
		self.UpProj2 = FastUpProjection(512, 256, self.batch_size)
		self.UpProj3 = FastUpProjection(256, 128, self.batch_size)
		self.UpProj4 = FastUpProjection(128, 64, self.batch_size)

		self.conv3 = nn.Conv2d(64, 1, kernel_size = 3, padding = 1)
		self.relu2 = nn.ReLU(inplace = True)

	def forward(self, x):
		out = self.conv1(x)
		out = self.bn1(out)
		out = self.relu1(out)
		out = self.max_pool1(out)
		print(out.size())

		out = self.proj1(out)
		out = self.res1_1(out)
		out = self.res1_2(out)
		print(out.size())

		out = self.proj2(out)
		out = self.res2_1(out)
		out = self.res2_2(out)
		out = self.res2_3(out)
		print(out.size())

		out = self.proj3(out)
		out = self.res3_1(out)
		out = self.res3_2(out)
		out = self.res3_3(out)
		out = self.res3_4(out)
		out = self.res3_5(out)
		print(out.size())

		out = self.proj4(out)
		out = self.res4_1(out)
		out = self.res4_2(out)
		print(out.size())

		out = self.conv2(out)
		out = self.bn2(out)
		print(out.size())

		out = self.UpProj1(out)
		print(out.size())

		out = self.UpProj2(out)

		out = self.UpProj3(out)
		print(out.size())

		out = self.UpProj4(out)
		print(out.size())

		out = self.conv3(out)
		out = self.relu2(out)
		print(out.size())

		# insert upsampling here?

		return out


text:
"""
A module for data loading, processing and saving.
"""

import glob
import json
import numpy as np
from .utils import get_filepaths_from_shortcut
from .selection import *
from .preprocessing import remove_baseline

#module_path = os.path.abspath(os.path.dirname(__file__))
#shortcuts_path = module_path+'/shortcuts.json'
#print(module_path)

# Check if shortcuts.json exists
if len(glob.glob('shortcuts.json')):
    pass
else: # if not, create with default example
    default_example = {
        "Name_example":{
          "basepath" : "/home/user/Documents/project_name/data",
          'data':{
            "raw" : "filename located in /raw/",
            "interim" : "filename located in /interim/",
            "processed" : {
              "data_label" : "filename",
              "example" : "example.mat #located in base/processed",
              "another_example" : "smoothed.pickle"
            },
            "results" : "another_filepath_or_dict"
          }
        }
    }
    json.dump(default_example, open('shortcuts.json','w'), indent='\t')

SHORTCUTS = json.load(open('shortcuts.json','r') )
if 'Name_example' in SHORTCUTS:
    del SHORTCUTS['Name_example']
# If there are saves in shortcuts, make sure folders and files exist.
for label in SHORTCUTS:
    if label != 'groups':
        all_files = get_filepaths_from_shortcut(SHORTCUTS[label])
        try:
            file_exists = np.array([len(glob.glob(filepath))==1 for filepath in all_files if len(filepath)>0])
            assert all(file_exists)
        except AssertionError:
            raise IOError('Filepaths %s do not exist'%all_files[np.where(file_exists == 0)])

max_stars_repo_name: EstevaoVieira/spikelearn

max_issues_repo_name: EstevaoVieira/spikelearn

max_forks_repo_name: EstevaoVieira/spikelearn


text:
import tensorflow as tf
import numpy as np

#the following code has been taken or adapted from hmishra2250's github under the MIT liscence. github link: https://github.com/hmishra2250/NTM-One-Shot-TF

def cosine_similarity(x, y, eps=1e-6):
	z = tf.matmul(x, tf.transpose(y, perm=[0,1]))
	z /= tf.sqrt(tf.multiply(tf.expand_dims(tf.reduce_sum(tf.multiply(x,x), 2), 2), tf.expand_dims(tf.reduce_sum(tf.multiply(y,y), 2), 1)) + eps)
	return z

def shared_float32(x, name=""):
	return tf.Variable(tf.cast(np.asarray(x, dtype=np.float32), tf.float32), name=name)

def update_tensor(V, dim2, val):
	val = tf.cast(val, V.dtype)

	def body(foo, body_args):
		v, d2, chg = body_args

		d2_int = tf.cast(d2, tf.int32)
		if len(chg.get_shape().as_list()) == 0:
			chg = [chg]
		else:
			chg = tf.reshape(chg, shape=[1]+chg.get_shape().as_list())
		oob = lambda : tf.slice(tf.concat([v[:d2_int], chg], axis=0), tf.range(0, len(v.get_shape().as_list())), v.get_shape().as_list())
		inb = lambda : tf.slice(tf.concat([v[:d2_int], chg, v[d2_int + 1:]], axis=0), tf.constant(0, shape=[len(v.get_shape().as_list())]), v.get_shape().as_list())
		return tf.cond(tf.less(d2_int + 1, v.get_shape().as_list()[0]), inb, oob)

	Z = tf.scan(body, elems=(V, dim2, val), initializer=tf.constant(1, shape=V.get_shape().as_list()[1:], dtype=tf.float32), name="Scan_Update")
	return Z

def shared_glorot_uniform(shape, dtype=tf.float32, name='', n=None):
	if isinstance(shape,int):
		high = np.sqrt(6. / shape)
		shape = [shape]
	else:
		high = np.sqrt(6. / (np.sum(shape[:2]) * np.prod(shape[2:])))
	shape = shape if n is None else [n] + list(shape)
	return tf.Variable(tf.random_uniform(shape, minval=-high, maxval=high, dtype=dtype, name=name))

def shared_zeros(shape, dtype=tf.float32, name='', n=None):
	shape = shape if n is None else (n,) + tuple(shape)
	return tf.Variable(tf.zeros(shape, dtype=dtype), name=name)

def shared_one_hot(shape, dtype=tf.float32, name='', n=None):
	shape = (shape,) if isinstance(shape,int) else shape
	shape = shape if n is None else (n,) + shape
	initial_vector = np.zeros(shape, dtype=np.float32)
	initial_vector[...,0] = 1
	return tf.Variable(tf.cast(initial_vector, tf.float32), name=name)

def weight_and_bias_init(shape, dtype=tf.float32, name='', n=None):
	return (shared_glorot_uniform(shape, dtype=dtype, name='W_' + name, n=n), shared_zeros((shape[1],), dtype=dtype, name='b_' + name, n=n))

def accuracy_instance(predictions, targets, n=[1, 2, 3, 4, 5, 10], nb_classes=5, nb_samples_per_class=10, batch_size=1, num_outputs=30):
	targets = tf.cast(targets, predictions.dtype)

	accuracy = tf.constant(value=0, shape=(batch_size, nb_samples_per_class), dtype=tf.float32)
	indices = tf.constant(value=0, shape=(batch_size, num_outputs+1), dtype=tf.float32)

	def step_(step_arg1, step_arg2):
		accuracy, indices = step_arg1
		p, t = step_arg2

		p = tf.cast(p, tf.int32)
		t = tf.cast(t, tf.int32)
		##Accuracy Update
		batch_range = tf.cast(tf.range(0, batch_size), dtype=tf.int32)
		gather = tf.cast(tf.gather_nd(indices,tf.stack([tf.range(0,p.get_shape().as_list()[0]), t], axis=1)), tf.int32)
		index = tf.cast(tf.stack([batch_range, gather], axis=1), dtype=tf.int64)
		val = tf.cast(tf.equal(p, t), tf.float32)
		delta = tf.SparseTensor(indices=index, values=val, dense_shape=tf.cast(accuracy.get_shape().as_list(), tf.int64))
		accuracy = accuracy + tf.sparse_tensor_to_dense(delta)
		##Index Update
		index = tf.cast(tf.stack([batch_range, t], axis=1), dtype=tf.int64)
		val = tf.constant(1.0, shape=[batch_size])
		delta = tf.SparseTensor(indices=index, values=val, dense_shape=tf.cast(indices.get_shape().as_list(), dtype=tf.int64))
		indices = indices + tf.sparse_tensor_to_dense(delta)
		return [accuracy, indices]

	print(predictions)
	print(targets)
	accuracy, indices = tf.scan(step_, elems=(tf.transpose(predictions, perm=[1, 0]), tf.transpose(targets, perm=[1, 0])),initializer=[accuracy, indices], name="Scan_Metric_Last")

	accuracy = accuracy[-1]

	accuracy = tf.reduce_mean(accuracy / nb_classes , axis=0)

	return accuracy

def accuracy(predictions, target, inst, num_classes=5, batch_size=10):
	'''acc = [[],[],[]]
	inst = tf.reshape(inst, [num_classes*batch_size, 3])
	for i in range(inst.get_shape()[0]):
		for j in range(3):
			foo = tf.cond(tf.reduce_all(tf.equal(target[inst[i][j]], predictions[inst[i][j]])), lambda: tf.constant(1), lambda: tf.constant(0))
			acc[j].append(foo)
	temp = []
	temp.append(tf.reduce_mean(tf.stack(acc[0])))
	temp.append(tf.reduce_mean(tf.stack(acc[1])))
	temp.append(tf.reduce_mean(tf.stack(acc[2])))
	return tf.stack([tf.reduce_mean(tf.stack(acc[0])),tf.reduce_mean(tf.stack(acc[1])),tf.reduce_mean(tf.stack(acc[2]))])'''
	acc = []
	for i in range(predictions.get_shape().as_list()[0]):
		foo = tf.cond(tf.reduce_all(tf.equal(tf.split(target, 6, axis=1), tf.split(predictions, 6, axis=1))), lambda: tf.constant(1), lambda: tf.constant(0))
		acc.append(foo)
	return tf.reduce_mean(tf.cast(foo, tf.float32))

def five_hot_decode(x):
	#print(x.get_shape().as_list()[:-1])
	x = np.reshape(x, newshape=np.shape(x)[:-1] + (6, 5))
	# = tf.reshape(x, tf.stack([None, 6, 5]))
	def f(a):
		return sum([a[i] * 5 ** i for i in range(6)])
	return np.apply_along_axis(f, -1, np.argmax(x, axis=-1))

def test_f( y, output):
	correct = [0] * 10
	total = [0] * 10
	y_decode = five_hot_decode(y)
	output_decode = five_hot_decode(output)
	'''y = tf.split(y, 6, axis=1)
	y = tf.argmax(y, axis=2)
	output = tf.split(output, 6, axis=1)
	output= tf.argmax(output, axis=2)'''
	for i in range(np.shape(y_decode)[0]):
		y_i = y_decode[i]
		output_i = output_decode[i]
		# print(y_i)
		# print(output_i)
		class_count = {}
		if y_i not in class_count:
			class_count[y_i] = 0
		else:
			class_count[y_i] += 1
		total[class_count[y_i]] += 1
		if y_i == output_i:
			print("yay")
			correct[class_count[y_i]] += 1

	return [float(correct[i]) / total[i] if total[i] > 0. else 0. for i in range(10)]

max_stars_repo_name: thefxperson/ISEF-JOCR

max_issues_repo_name: thefxperson/ISEF-JOCR

max_forks_repo_name: thefxperson/ISEF-JOCR


text:
import numpy as np # type: ignore
import json
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--input-file", type=str,
  help="JSON file containing seqlevel cross-entropies")

args = parser.parse_args()

def standard_error(npa):
  return np.std(npa, ddof=1) / np.sqrt(np.size(npa))

def ninetyfive_confidence(npa):
  return standard_error(npa) * 1.96

with open(args.input_file, "r") as f:
  xent_j = json.load(f)

xents = np.array(xent_j["xents"])

print("mean:", np.mean(xents))
print("stderr:", standard_error(xents))
print("95conf:", ninetyfive_confidence(xents))
print("sstdev:", np.std(xents, ddof=1))


text:
import numpy as np
import pandas as pd
from os.path import join
from datetime import datetime
from nilearn.input_data import NiftiMasker

data_dir = "/home/data/nbc/physics-learning/data/pre-processed"
sink_dir = "/home/data/nbc/physics-learning/retrieval-graphtheory/output"

subjects = [
    "101",
    "102",
    "103",
    "104",
    "106",
    "107",
    "108",
    "110",
    "212",
    "214",
    "215",
    "216",
    "217",
    "218",
    "219",
    "320",
    "321",
    "323",
    "324",
    "325",
    "327",
    "328",
    "330",
    "331",
    "333",
    "334",
    "335",
    "336",
    "337",
    "338",
    "339",
    "340",
    "341",
    "342",
    "343",
    "344",
    "345",
    "346",
    "347",
    "348",
    "349",
    "350",
    "451",
    "453",
    "455",
    "458",
    "459",
    "460",
    "462",
    "463",
    "464",
    "465",
    "467",
    "468",
    "469",
    "470",
    "502",
    "503",
    "571",
    "572",
    "573",
    "574",
    "577",
    "578",
    "581",
    "582",
    "584",
    "585",
    "586",
    "587",
    "588",
    "589",
    "591",
    "592",
    "593",
    "594",
    "595",
    "596",
    "597",
    "598",
    "604",
    "605",
    "606",
    "607",
    "608",
    "609",
    "610",
    "612",
    "613",
    "614",
    "615",
    "617",
    "618",
    "619",
    "620",
    "621",
    "622",
    "623",
    "624",
    "625",
    "626",
    "627",
    "629",
    "630",
    "631",
    "633",
    "634",
]
# subjects = ['101', '103']

sessions = ["pre", "post"]

df = pd.DataFrame(index=subjects, columns=["average_head_size"])

masker = NiftiMasker()

for subject in subjects:
    head_size = 0
    for i in np.arange(0, len(sessions)):
        bet_mask = join(
            data_dir,
            subject,
            "session-{0}".format(i),
            "anatomical/anatomical-0/fsl/anatomical-bet_mask.nii.gz",
        )
        mask_array = masker.fit_transform(bet_mask)
        head_size += np.sum(mask_array)
    df.at[subject] = head_size / 2
df.to_csv(join(sink_dir, "head-size_{0}.csv".format(str(datetime.now()))))

max_stars_repo_name: NBCLab/IDConn

max_issues_repo_name: NBCLab/IDConn

max_forks_repo_name: NBCLab/IDConn


text:
"""
author: Paul Bruillard, harsh
"""

import jax.numpy as jnp
from jax import jit
from cjax.utils.math_trees import *
from typing import Any
from functools import partial

def get_rotation_pytree(src: Any, dst: Any) -> Any:
    """
    Takes two n-dimensional vectors/Pytree and returns an
    nxn rotation matrix mapping cjax to dst.
    Raises Value Error when unsuccessful.
    """

    def __assert_rotation(R):
        if R.ndim != 2:
            print("R must be a matrix")
        a, b = R.shape
        if a != b:
            print("R must be square")
        if (
            not jnp.isclose(jnp.abs(jnp.eye(a) - jnp.dot(R, R.T)).max(), 0.0, rtol=0.5)
        ) or (
            not jnp.isclose(jnp.abs(jnp.eye(a) - jnp.dot(R.T, R)).max(), 0.0, rtol=0.5)
        ):
            print("R is not diagonal")

    if not pytree_shape_array_equal(src, dst):
        print("cjax and dst must be 1-dimensional arrays with the same shape.")

    x = pytree_normalized(src)
    y = pytree_normalized(dst)
    n = len(dst)

    # compute angle between x and y in their spanning space
    theta = jnp.arccos(jnp.dot(x, y))  # they are normalized so there is no denominator
    if jnp.isclose(theta, 0):
        print("x and y are co-linear")
    # construct the 2d rotation matrix connecting x to y in their spanning space
    R = jnp.array([[jnp.cos(theta), -jnp.sin(theta)], [jnp.sin(theta), jnp.cos(theta)]])
    __assert_rotation(R)
    # get projections onto Span<x,y> and its orthogonal complement
    u = x
    v = pytree_normalized(pytree_sub(y, (jnp.dot(u, y) * u)))
    P = jnp.outer(u, u.T) + jnp.outer(
        v, v.T
    )  # projection onto 2d space spanned by x and y
    Q = jnp.eye(n) - P  # projection onto the orthogonal complement of Span<x,y>
    # lift the rotation matrix into the n-dimensional space
    uv = jnp.hstack((u[:, None], v[:, None]))

    R = Q + jnp.dot(uv, jnp.dot(R, uv.T))
    __assert_rotation(R)
    if jnp.any(jnp.logical_not(jnp.isclose(jnp.dot(R, x), y, rtol=0.25))):
        print("Rotation matrix did not work")
    return R

def get_rotation_array(src: Any, dst: Any) -> Any:
    """
    Takes two n-dimensional vectors and returns an
    nxn rotation matrix mapping cjax to dst.
    Raises Value Error when unsuccessful.
    """

    def __assert_rotation(R):
        if R.ndim != 2:
            raise ValueError("R must be a matrix")
        a, b = R.shape
        if a != b:
            raise ValueError("R must be square")
        if (not np.isclose(np.abs(np.eye(a) - np.dot(R, R.T)).max(), 0)) or (
            not np.isclose(np.abs(np.eye(a) - np.dot(R.T, R)).max(), 0)
        ):
            raise ValueError("R is not diagonal")

    def __normalize(x):
        return x / np.sqrt(np.sum(x ** 2))

    if src.shape != dst.shape or src.ndim != 1:
        raise ValueError(
            "cjax and dst must be 1-dimensional arrays with the same shape."
        )
    x = __normalize(src.copy())
    y = __normalize(dst.copy())

    # compute angle between x and y in their spanning space
    theta = np.arccos(np.dot(x, y))  # they are normalized so there is no denominator
    if np.isclose(theta, 0):
        raise ValueError("x and y are co-linear")
    # construct the 2d rotation matrix connecting x to y in their spanning space
    R = np.array([[np.cos(theta), -np.sin(theta)], [np.sin(theta), np.cos(theta)]])
    __assert_rotation(R)
    # get projections onto Span<x,y> and its orthogonal complement
    u = x
    v = __normalize((y - (np.dot(u, y) * u)))
    P = np.outer(u, u.T) + np.outer(
        v, v.T
    )  # projection onto 2d space spanned by x and y
    Q = np.eye(n) - P  # projection onto the orthogonal complement of Span<x,y>
    # lift the rotation matrix into the n-dimensional space
    uv = np.hstack((u[:, None], v[:, None]))
    R = Q + np.dot(uv, np.dot(R, uv.T))
    __assert_rotation(R)
    if np.any(np.logical_not(np.isclose(np.dot(R, x), y))):
        raise ValueError("Rotation matrix did not work")
    return R

@partial(jit, static_argnums=(0))
def projection_affine(n_dim, u, n, u_0):
    """
    Args:
        n_dim: affine transformation space
        u: random point to be projected on n as L
        n: secant normal vector
        u_0: secant starting point
    Returns:
    """
    n_norm = l2_norm(n)
    I = jnp.eye(n_dim)
    p2 = jnp.dot(I, n)[:, None] / n_norm ** 2 * n
    u_0 = lax.reshape(u_0, (n_dim, 1))
    I = jnp.eye(n_dim)
    t1 = jnp.block([[I, u_0], [jnp.zeros(shape=(1, n_dim)), 1.0]])
    t2 = jnp.block(
        [[p2, jnp.zeros(shape=(n_dim, 1))], [jnp.zeros(shape=(1, n_dim)), 1.0]]
    )
    t3 = jnp.block([[I, -1 * u_0], [jnp.zeros(shape=(1, n_dim)), 1.0]])
    P = jnp.matmul(jnp.matmul(t1, t2), t3)
    pr = jnp.matmul(P, jnp.hstack([u, 1.0]))
    pr = lax.slice(pr, [0], [n_dim])
    return pr

if __name__ == "__main__":
    n = 5
    # key = random.PRNGKey(10)
    # k1, k2 = random.split(key, 2)
    # cjax = random.normal(k1, [n])
    # dst = random.normal(k2, [n])
    # R = get_rotation_pytree(cjax, dst)
    # transformed_vector = np.dot(R, cjax)
    # print(jnp.dot(transformed_vector, dst))

    # n = 3
    # cjax= np.array([0.0,0.0,1.0])
    # dst = np.array([3.0,3.0,3.0])
    # sample = np.array([1.0,2.0,0.0])
    # R = get_rotation_array(cjax, dst)
    # transformed_vector = np.dot(R, sample) + dst
    # print(transformed_vector)

reason: import jax,from jax


text:
import numpy as np
from huiAudioCorpus.model.Audio import Audio

class AudioAddSilenceTransformer:

    def __init__(self,startDurationSeconds: float,  endDurationSeconds: float):
        self.startDurationSeconds = startDurationSeconds
        self.endDurationSeconds = endDurationSeconds

    def transform(self, audio: Audio):
        silenceAudioFront = self.generateSilence(self.startDurationSeconds, audio.samplingRate)
        silenceAudioBack = self.generateSilence(self.endDurationSeconds, audio.samplingRate)
        newAudio = silenceAudioFront+ audio + silenceAudioBack
        return newAudio

    def generateSilence(self,duration: float, samplingRate: int):
        silenceDataPoints = int(duration*samplingRate)
        silence = np.zeros(silenceDataPoints)
        silenceAudio =  Audio(silence, samplingRate, 's', 's')
        return silenceAudio

max_stars_repo_name: iisys-hof/HUI-Audio-Corpus-German

max_issues_repo_name: iisys-hof/HUI-Audio-Corpus-German

max_forks_repo_name: iisys-hof/HUI-Audio-Corpus-German