text:
"""
Programming Project 15
Ankit Khandelwal
15863
"""

from math import sqrt
from random import choices, randint

import matplotlib.pyplot as plt
import numpy as np
import scipy.sparse as sp

N = 15
xnos = 3
znos = 4
dnos = 8
data_qubits = np.ndarray((dnos, 2), list)
data_qubits[0][0] = 1
data_qubits[1][0] = 3
data_qubits[2][0] = 5
data_qubits[3][0] = 7
data_qubits[4][0] = 9
data_qubits[5][0] = 11
data_qubits[6][0] = 13
data_qubits[7][0] = 15
data_qubits[0][1] = [1, 3]
data_qubits[1][1] = [3, 3]
data_qubits[2][1] = [5, 3]
data_qubits[3][1] = [2, 2]
data_qubits[4][1] = [4, 2]
data_qubits[5][1] = [1, 1]
data_qubits[6][1] = [3, 1]
data_qubits[7][1] = [5, 1]

X_stabilizers = np.ndarray((xnos, 6), list)
X_stabilizers[0][0] = 6
X_stabilizers[0][1] = 1
X_stabilizers[0][2] = 'a'
X_stabilizers[0][3] = 7
X_stabilizers[0][4] = 11
X_stabilizers[0][5] = [1, 2]
X_stabilizers[1][0] = 8
X_stabilizers[1][1] = 3
X_stabilizers[1][2] = 7
X_stabilizers[1][3] = 9
X_stabilizers[1][4] = 13
X_stabilizers[1][5] = [3, 2]
X_stabilizers[2][0] = 10
X_stabilizers[2][1] = 5
X_stabilizers[2][2] = 9
X_stabilizers[2][3] = 'a'
X_stabilizers[2][4] = 15
X_stabilizers[2][5] = [5, 2]

Z_stabilizers = np.ndarray((znos, 6), list)
Z_stabilizers[0][0] = 2
Z_stabilizers[0][1] = 'a'
Z_stabilizers[0][2] = 1
Z_stabilizers[0][3] = 3
Z_stabilizers[0][4] = 7
Z_stabilizers[0][5] = [2, 3]
Z_stabilizers[1][0] = 4
Z_stabilizers[1][1] = 'a'
Z_stabilizers[1][2] = 3
Z_stabilizers[1][3] = 5
Z_stabilizers[1][4] = 9
Z_stabilizers[1][5] = [4, 3]
Z_stabilizers[2][0] = 12
Z_stabilizers[2][1] = 7
Z_stabilizers[2][2] = 11
Z_stabilizers[2][3] = 13
Z_stabilizers[2][4] = 'a'
Z_stabilizers[2][5] = [2, 1]
Z_stabilizers[3][0] = 14
Z_stabilizers[3][1] = 9
Z_stabilizers[3][2] = 13
Z_stabilizers[3][3] = 15
Z_stabilizers[3][4] = 'a'
Z_stabilizers[3][5] = [4, 1]

for n in range(dnos):
    d = plt.scatter(data_qubits[n][1][0], data_qubits[n][1][1], c='r', s=1000)
for n in range(xnos):
    x = plt.scatter(X_stabilizers[n][5][0], X_stabilizers[n][5][1], c='b', s=1000)
for n in range(znos):
    z = plt.scatter(Z_stabilizers[n][5][0], Z_stabilizers[n][5][1], c='g', s=1000)
plt.title('Surface Code Computer')
plt.xticks([1, 2, 3, 4, 5])
plt.yticks([1, 2, 3])
plt.legend((d, x, z), ('Data Qubits', 'X-Stabilizers', 'Z-Stabilizers'), fontsize=12, scatterpoints=1, loc=(1.04, 0),
           markerscale=.4)
plt.show()

error_table = np.zeros((4 ** dnos, 1 + xnos + znos + 1), int)

def numToBase(n, b):
    digits = np.zeros(dnos, int)
    if n == 0:
        return digits
    i = 0
    while n:
        digits[i] = (int(n % b))
        n //= b
        i = i + 1
    return digits[::-1]

for i in range(4 ** dnos):
    temp = numToBase(i, 4)
    error_table[i][-2] = i
    error_table[i][-1] = np.count_nonzero(temp == 0)
    for k in range(dnos):
        if temp[k] == 1 or temp[k] == 3:
            qubit_loc = np.argwhere((Z_stabilizers[:, 1:-1]) == (data_qubits[k][0]))
            for l in range(len(qubit_loc)):
                error_table[i][qubit_loc[l][0] + xnos] = 1 - error_table[i][qubit_loc[l][0] + xnos]
        if temp[k] == 2 or temp[k] == 3:
            qubit_loc = np.argwhere((X_stabilizers[:, 1:-1]) == (data_qubits[k][0]))
            for l in range(len(qubit_loc)):
                error_table[i][qubit_loc[l][0]] = 1 - error_table[i][qubit_loc[l][0]]
error_table = error_table[error_table[:, -1].argsort()[::-1]]

psi = np.ndarray((2 ** N, 1), complex)  # N-qubit register
H = 1 / sqrt(2) * np.array([[1, 1], [1, -1]])  # Hadamard gate

X = np.array(([0, 1], [1, 0]), int)
Z = np.array(([1, 0], [0, -1]), int)
Y = X @ Z * 1j

def kd(i, j):
    if i == j:
        return 1
    else:
        return 0

zero_ket = np.ndarray((2, 1), int)
zero_ket[0][0] = 1
zero_ket[1][0] = 0
zero_bra = zero_ket.T

one_ket = np.ndarray((2, 1), int)
one_ket[0][0] = 0
one_ket[1][0] = 1
one_bra = one_ket.T

zero_matrix = zero_ket @ zero_bra
one_matrix = one_ket @ one_bra

def C_NOTn(a, b):
    # part 1
    left = a - 1
    right = N - a
    if right > 0:
        RIGHT = sp.eye(2 ** right, dtype=int)
    if left > 0:
        LEFT = sp.eye(2 ** left, dtype=int)
    if left == 0:
        LEFT = 1
    if right == 0:
        RIGHT = 1
    part1 = sp.kron(LEFT, sp.kron(zero_matrix, RIGHT))
    # part2
    if a < b:
        a = a
        b = b
        count = 1
    else:
        temp = a
        a = b
        b = temp
        count = 2
    left = a - 1
    right = N - b
    center = N - left - right - 2
    if right > 0:
        RIGHT = sp.eye(2 ** right, dtype=int)
    if left > 0:
        LEFT = sp.eye(2 ** left, dtype=int)
    if left == 0:
        LEFT = 1
    if right == 0:
        RIGHT = 1
    if center > 0:
        CENTER = sp.eye(2 ** center, dtype=int)
    if center == 0:
        CENTER = 1
    if count == 1:
        part2 = sp.kron(LEFT, sp.kron(one_matrix, sp.kron(CENTER, sp.kron(X, RIGHT))))
    if count == 2:
        part2 = sp.kron(LEFT, sp.kron(X, sp.kron(CENTER, sp.kron(one_matrix, RIGHT))))
    return part1 + part2

def multi_gate(gate, n):
    right = N - n
    left = N - right - 1
    if right > 0:
        RIGHT = sp.eye(2 ** right, dtype=int)
    if left > 0:
        LEFT = sp.eye(2 ** left, dtype=int)
    if right == 0:
        return sp.kron(LEFT, gate)
    if left == 0:
        return sp.kron(gate, RIGHT)
    return sp.csr_matrix((sp.kron(LEFT, sp.kron(gate, RIGHT))))

def P_0(n):
    P0 = np.ndarray((1, 2 ** N), int)
    for k in range(2 ** N):
        jj = str(format(k, '0{}b'.format(N)))
        if jj[N - n] == '0':
            P0[0][k] = 1
        else:
            P0[0][k] = 0
    return P0

measure_qubits = np.concatenate(((N + 1) - X_stabilizers[:, 0], (N + 1) - \
                                 Z_stabilizers[:, 0])).reshape(1, xnos + znos)
# Needs to be custom in each code in (N+1-i) format
# where i is the qubit to be measured
PP_0 = np.ndarray((N + 1), np.ndarray)
for nn in measure_qubits:
    for n in nn:
        PP_0[n] = (P_0(n))

def P_1(n):
    P1 = np.ndarray((1, 2 ** N), int)
    for k in range(2 ** N):
        P1[0][k] = kd(k, k) - PP_0[n][0][k]
    return sp.csr_matrix(sp.diags(P1[0, :], 0))

PP_1 = np.ndarray((N + 1), np.ndarray)
for nn in measure_qubits:
    for n in nn:
        PP_1[n] = (P_1(n))
        PP_0[n] = sp.csr_matrix(sp.diags(PP_0[n][0, :], 0))
prob = np.zeros((N + 1, 2), float)
# psi is computational basis state
comp_state = randint(1, 2 ** N)
for i in range(2 ** N):
    if i == comp_state:
        psi[i] = 1
    else:
        psi[i] = 0

def error_type(change):
    index = error_table[:, 0:7].tolist().index(change.tolist())
    gate_list = numToBase(error_table[index][7], 4)
    for i in range(len(gate_list)):
        if gate_list[i] == 0:
            continue
        elif gate_list[i] == 1:
            print('X error on {} qubit.'.format(data_qubits[i][0]))
        elif gate_list[i] == 2:
            print('Z error on {} qubit.'.format(data_qubits[i][0]))
        elif gate_list[i] == 3:
            print('Y error on {} qubit.'.format(data_qubits[i][0]))

repeat = 10
results = 5 * np.ones((repeat, N), int)
syndrome_old = np.zeros(xnos + znos, int)
for ii in range(repeat):
    syndrome_new = np.zeros(xnos + znos, int)

    if ii == 1:  # Add desired error
        psi = multi_gate(X, 15) @ psi
    # psi = multi_gate(Y, 1)@psi

    # 1 Initialize
    for nn in measure_qubits:
        for n in nn:
            prob[n][0] = (((PP_0[n] @ psi).conj().T) @ (PP_0[n] @ psi)).real
            prob[n][1] = (((PP_1[n] @ psi).conj().T) @ (PP_1[n] @ psi)).real
            r = choices((0, 1), (prob[n][0], prob[n][1]))
            r = r[0]
            if r == 0:
                psi = (PP_0[n] @ psi) / sqrt(prob[n][0])
            else:
                psi = (PP_1[n] @ psi) / sqrt(prob[n][1])
            results[ii][N - n] = r

    syndome_qubits = np.concatenate((X_stabilizers[:, 0], \
                                     Z_stabilizers[:, 0])).reshape(1, xnos + znos)
    for nn in syndome_qubits:
        for n in nn:
            if results[ii][n - 1] == 1:
                psi = multi_gate(X, n) @ psi

    # 2 H
    for i in range(xnos):
        psi = multi_gate(H, X_stabilizers[i][0]) @ psi

    # 3 Up
    for i in range(xnos):
        if X_stabilizers[i][1] == 'a':
            continue
        psi = C_NOTn(X_stabilizers[i][0], X_stabilizers[i][1]) @ psi

    for i in range(znos):
        if Z_stabilizers[i][1] == 'a':
            continue
        psi = C_NOTn(Z_stabilizers[i][1], Z_stabilizers[i][0]) @ psi

    # 4 Left
    for i in range(xnos):
        if X_stabilizers[i][2] == 'a':
            continue
        psi = C_NOTn(X_stabilizers[i][0], X_stabilizers[i][2]) @ psi

    for i in range(znos):
        if Z_stabilizers[i][2] == 'a':
            continue
        psi = C_NOTn(Z_stabilizers[i][2], Z_stabilizers[i][0]) @ psi

    # 5 Right
    for i in range(xnos):
        if X_stabilizers[i][3] == 'a':
            continue
        psi = C_NOTn(X_stabilizers[i][0], X_stabilizers[i][3]) @ psi

    for i in range(znos):
        if Z_stabilizers[i][3] == 'a':
            continue
        psi = C_NOTn(Z_stabilizers[i][3], Z_stabilizers[i][0]) @ psi

    # 6 Down
    for i in range(xnos):
        if X_stabilizers[i][4] == 'a':
            continue
        psi = C_NOTn(X_stabilizers[i][0], X_stabilizers[i][4]) @ psi

    for i in range(znos):
        if Z_stabilizers[i][4] == 'a':
            continue
        psi = C_NOTn(Z_stabilizers[i][4], Z_stabilizers[i][0]) @ psi

    # 7 H
    for i in range(xnos):
        psi = multi_gate(H, X_stabilizers[i][0]) @ psi

    # 8 Measure
    for nn in measure_qubits:
        for n in nn:
            prob[n][0] = (((PP_0[n] @ psi).conj().T) @ (PP_0[n] @ psi)).real
            prob[n][1] = (((PP_1[n] @ psi).conj().T) @ (PP_1[n] @ psi)).real
            r = choices((0, 1), (prob[n][0], prob[n][1]))
            r = r[0]
            if r == 0:
                psi = (PP_0[n] @ psi) / sqrt(prob[n][0])
            else:
                psi = (PP_1[n] @ psi) / sqrt(prob[n][1])
            results[ii][N - n] = r

    for nn in syndome_qubits:
        i = 0
        for n in nn:
            syndrome_new[i] = (results[ii][n - 1])
            i = i + 1
    # print(syndrome_new)
    if ii == 0:
        syndrome_old = np.copy(syndrome_new)
    if ii != 0:
        change = abs(syndrome_new - syndrome_old)
        syndrome_old = np.copy(syndrome_new)
        error_type(change)

reason: import numpy,import scipy


text:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# File: darknet.py
# Author: Qian Ge <geqian1001@gmail.com>

import numpy as np
import tensorflow as tf
from src.net.base import BaseModel
import src.model.darknet_module as module

INIT_W = None

class DarkNet53(BaseModel):
    """ class of Darknet53 only  for inference """
    def __init__(self, n_channel, n_class, pre_trained_path, trainable=False):
        """
            Args:
                n_channel (int): number of channel of input image
                n_class (int): number of classes for classification
                pre_trained_path (str): path of pre-trained model (.npy file)
                trainable (bool): whether train this net or not (training is not implemented)
        """
        self._n_channel = n_channel
        self._n_class = n_class
        self._trainable = trainable

        # load pre-trained model
        self._pretrained_dict = None
        if pre_trained_path:
            self._pretrained_dict = np.load(
                pre_trained_path, encoding='latin1').item()

        self.layers = {}

    def _create_valid_input(self):
        """ Create inputs of the model.
            Input images should be scale to [0, 1] before fed into the model.

        """
        self.image = tf.placeholder(
            tf.float32, [None, None, None, self._n_channel], name='image')

    def create_valid_model(self):
        """ Create model for inference.
            Get top 5 prediction for each image.
        """
        self.set_is_training(is_training=False)
        self._create_valid_input()
        self.layers['logits'] = self._create_model(self.image)
        # get top 5 prediction for each image
        self.layers['top_5'] = tf.nn.top_k(
                tf.nn.softmax(self.layers['logits']), k=5, sorted=True)

    def _create_model(self, inputs):
        """ Create darknet53 classification model
            Return logits
        """
        with tf.variable_scope('DarkNet53', reuse=tf.AUTO_REUSE):
            conv_out, _, _ = module.darkent53_conv(
                inputs,
                init_w=INIT_W,
                bn=True, wd=0,
                trainable=self._trainable,
                is_training=self.is_training,
                pretrained_dict=self._pretrained_dict,
                name='darkent53_conv')

            logits = module.classification_fc(
                conv_out,
                n_class=self._n_class,
                init_w=INIT_W, wd=0,
                trainable=self._trainable,
                is_training=self.is_training,
                pretrained_dict=self._pretrained_dict,
                name='darknet53_fc')

            return logits

ext: py

reason: import numpy


text:
# modules
import pandas as pd
import numpy as np
import statsmodels.api as sm
import statsmodels.formula.api as smf
import matplotlib.pyplot as plt
import warnings

from statsmodels.nonparametric.smoothers_lowess import lowess
from matplotlib import collections as matcoll

warnings.filterwarnings('ignore')

# Building package

class Mplot:
    """
    Class for plotting different OLS model iagnostic checks.
    Reproduces the diagnostic plots in mplot package in R

    Attributses:
        mdl (object) representing statsmodels OLS model results
        n (int) representing the diagnostic check to plot
            1 = Residuals vs Fitted
            2 = Normal Q-Q
            3 = Scale-Location
            4 = Cook's Distance
            5 = Residuals vs Leverage
            6 = Cook's dist vs Leverage
            7 = 95% confidence intervals

    """

    def __init__(self, mdl, n):

        self.mdl = mdl
        self.n = n

        # fitted values
        self.fitted_values = pd.Series(self.mdl.fittedvalues)
        self.res_df = pd.DataFrame(self.fitted_values, index = self.mdl.fittedvalues.index, columns = ['fitted_values'])

        # residuals
        self.res_df['residuals'] = self.mdl.resid

        # Standardized (studentized) residuals
        self.res_df['student_residuals'] = self.mdl.get_influence().resid_studentized_internal

        # square rootof studentized residuals
        self.res_df['sqrt_student_residuals'] = np.sqrt(np.abs(self.res_df['student_residuals']))

        # leverage
        self.res_df['leverage'] = self.mdl.get_influence().hat_matrix_diag

        # cooks distance
        self.res_df['cooks_distance'] = self.mdl.get_influence().cooks_distance[0]

        # parameter details
        self.df_params = pd.DataFrame(self.mdl.params.reset_index())
        self.df_params.columns = ['var', 'coef']
        self.df_params['errors'] = self.df_params['coef'] - self.mdl.conf_int().iloc[:,0].values
        self.df_params['pvalues'] = self.mdl.pvalues.values
        self.df_params['Significant'] = self.df_params['pvalues'] < 0.05
        #self.df_params.sort_values(by='coef', ascending = True, inplace = True)

    def plot(self):
        """
        Function to generate plot for chosen diagnostic check

        Args:
            None

        Returns
            Matplotlib plot: Plot of chosen diagnostic check
        """

        plt.style.use('ggplot')

        if self.n == 1:
            fig, ax = plt.subplots(figsize = (10,8))

            # rows with top residuals
            top3 = abs(self.res_df['residuals']).sort_values(ascending = False)[:3]

            # x - cordinates
            X = self.res_df['fitted_values']
            # y - cordinates
            Y = self.res_df['residuals']
            # smoothened values for the regplot
            smoothed = lowess(self.res_df['residuals'],self.res_df['fitted_values'])

            #plotting

            ax.scatter(X, Y, linewidth = 3, alpha = 0.85, s = 6)
            ax.plot(smoothed[:,0], smoothed[:,1], color = 'k')
            ax.set_ylabel('Residuals', fontsize = 12, fontweight = 'bold')
            ax.set_xlabel('Fitted Values', fontsize = 12, fontweight = 'bold')
            ax.set_title('Residuals vs. Fitted', fontsize = 14, fontweight = 'bold')
            ax.plot([min(X),max(X)],[0,0],color = 'k',linestyle = ':', alpha = .8)
            for i in top3.index:
                ax.annotate(i,
                            xy=(X[i], Y[i]),
                            fontsize = 10, color = 'k', fontweight = 'bold'
                           )
            return plt.show()

        if self.n == 2:
            fig, ax = plt.subplots(figsize = (10,8))
            Y = self.res_df['residuals']

            # qq plot from statsmodels.api = sm
            QQplot = sm.qqplot(Y, fit = True, line = "q",ax = ax)
            # qqplot title
            QQplot.axes[0].set_title('Normal Q-Q', fontsize = 14, fontweight = 'bold')
            # x_axis title
            QQplot.axes[0].set_xlabel('Theoretical Quantiles', fontsize = 12, fontweight = 'bold')
            # y_axis title
            QQplot.axes[0].set_ylabel('Standardized Residuals', fontsize = 12, fontweight = 'bold')

            # Annotation
            # top residuals from the plot
            locator = np.arange(3)-3
            # get plot x and y values
            axes = plt.gca()
            line = axes.lines[0]
            x_values = line.get_xdata()
            y_values = line.get_ydata()
            for i, j in enumerate(locator):
                indx = np.argsort(np.abs(Y)).values[j]
                QQplot.axes[0].annotate(indx,
                                        xy = (x_values[j], y_values[j]),
                                        fontsize = 10, color = 'k', fontweight = 'bold'
                                       )
            return plt.show()

        if self.n == 3:
            fig, ax = plt.subplots(figsize = (10,8))
            X = self.res_df['fitted_values']
            Y = self.res_df['sqrt_student_residuals']
            smoothed = lowess(Y,X)

            # rows with top residuals
            top3 = abs(self.res_df['residuals']).sort_values(ascending = False)[:3]

            ax.scatter(X, Y, alpha = 0.85, linewidths = 3,color = 'r', s = 6)
            ax.plot(smoothed[:,0],smoothed[:,1],color = 'k');
            ax.set_ylabel('$\sqrt{Studentized \ Residuals}$', fontsize = 12, fontweight = 'bold')
            ax.set_xlabel('Fitted Values', fontsize = 12, fontweight = 'bold')
            ax.set_title('Scale-Location', fontsize = 14, fontweight = 'bold')
            ax.set_ylim(0,max(Y)+0.1)
            for i in top3.index:
                ax.annotate(i,
                            xy = (X[i],Y[i]),
                            fontsize = 10, color = 'k', fontweight = 'bold'
                           )
            return plt.show()

        if self.n == 4:

            # cordinates
            df_res = self.res_df.reset_index().drop('index', axis=1)
            X = df_res.index
            Y = df_res['cooks_distance']

            # rows with the highest cook distance
            top3 = abs(df_res['cooks_distance']).sort_values(ascending = False)[:3]

            # creates cordinates for vertical lines
            lines = []
            for i in range(len(X)):
                pair = [(X[i],0), (X[i], Y[i])]
                lines.append(pair)
            linecoll = matcoll.LineCollection(lines)

            # plot
            fig, ax = plt.subplots(figsize = (10,8))
            ax.add_collection(linecoll)
            plt.scatter(X,Y, alpha = 0.85, linewidths = 3,color = 'r', s = 6)
            ax.set_ylabel("Cook's distance", fontsize = 12, fontweight = 'bold')
            ax.set_xlabel('Observation number', fontsize = 12, fontweight = 'bold')
            # annotation
            for i in top3.index:
                ax.annotate(i,
                            xy = (X[i],Y[i]),
                            fontsize = 10, color = 'k', fontweight = 'bold'
                           )
            return plt.show()

        if self.n == 5:

            # studentized residuals
            student_residuals = pd.Series(self.mdl.get_influence().resid_studentized_internal)
            student_residuals.index = self.mdl.resid.index
            df = pd.DataFrame(student_residuals)
            df.columns = ['student_residuals']
            # leverage
            df['leverage'] = self.mdl.get_influence().hat_matrix_diag

            # top 3
            sorted_student_residuals = abs(df['student_residuals']).sort_values(ascending = False)
            top3 = sorted_student_residuals[:3]

            # cordinates
            x = df['leverage']
            y = df['student_residuals']
            smoothed = lowess(y, x)

            # plot
            fig, ax = plt.subplots(figsize = (8,6))
            ax.scatter(x, y, linewidths = 3,color = 'r', s = 6)
            ax.plot(smoothed[:,0],smoothed[:,1],color = 'k')
            ax.set_ylabel('Studentized Residuals', fontsize = 12, fontweight = 'bold')
            ax.set_xlabel('Leverage', fontsize = 12, fontweight = 'bold')
            ax.set_title('Residuals vs. Leverage', fontsize = 14, fontweight = 'bold')
            ax.set_ylim(min(y)-abs(min(y))*0.25,max(y)+max(y)*0.15)
            ax.set_xlim(-0.01,max(x)+max(x)*0.05)
            plt.tight_layout()
            for val in top3.index:
                ax.annotate(val,
                            xy = (x.loc[val],y.loc[val]),
                            fontsize = 10, color = 'k', fontweight = 'bold'
                           )

            xpos = max(x)+max(x)*0.01
            cooksx = np.linspace(min(x), xpos, 50)
            p = len(self.mdl.params)
            poscooks1y = np.sqrt((p*(1-cooksx))/cooksx)
            poscooks05y = np.sqrt(0.5*(p*(1-cooksx))/cooksx)
            negcooks1y = -np.sqrt((p*(1-cooksx))/cooksx)
            negcooks05y = -np.sqrt(0.5*(p*(1-cooksx))/cooksx)
            ax.plot(cooksx,poscooks1y,label = "Cook's Distance", ls = ':', color = 'r')
            ax.plot(cooksx,poscooks05y, ls = ':', color = 'r')
            ax.plot(cooksx,negcooks1y, ls = ':', color = 'r')
            ax.plot(cooksx,negcooks05y, ls = ':', color = 'r')
            ax.plot([0,0],ax.get_ylim(), ls=":", alpha = .3, color = 'k')
            ax.plot(ax.get_xlim(), [0,0], ls=":", alpha = .3, color = 'k')
            ax.annotate('1.0', xy = (xpos, poscooks1y[-1]), color = 'r')
            ax.annotate('0.5', xy = (xpos, poscooks05y[-1]), color = 'r')
            ax.annotate('1.0', xy = (xpos, negcooks1y[-1]), color = 'r')
            ax.annotate('0.5', xy = (xpos, negcooks05y[-1]), color = 'r')
            ax.legend()

            return plt.show()

        if self.n == 6:
            fig, ax = plt.subplots(figsize = (10,8))
            # cordinates
            X = self.res_df['leverage']
            Y = self.res_df['cooks_distance']
            smoothed = lowess(Y,X)

            # rows with highest cooks distance
            top3 = abs(Y).sort_values(ascending = False)[:3]

            # plot
            ax.scatter(X, Y, alpha = 0.85, linewidths = 3,color = 'r', s = 6)
            ax.plot(smoothed[:,0],smoothed[:,1],color = 'k')
            ax.set_ylabel("Cook's distance", fontsize = 12, fontweight = 'bold')
            ax.set_xlabel('Leverage', fontsize = 12, fontweight = 'bold')
            ax.set_title("Cook's dist vs. Leverage", fontsize = 14, fontweight = 'bold')
            # annotation
            for i in top3.index:
                ax.annotate(i,
                            xy = (X.loc[i],Y.loc[i]),
                            fontsize = 10, color = 'k', fontweight = 'bold'
                           )
            return plt.show()

        if self.n == 7:
            fig, ax = plt.subplots(figsize = (10,8))

            self.df_params.plot(x = 'var', y = 'coef',
                           kind = 'barh',
                           ax = ax, color = 'none', fontsize = 22,
                           ecolor = self.df_params.Significant.map({True: '#20B2AA', False: '#F08080'}),
                           capsize = 0,
                           xerr = 'errors',
                           legend = False
                          )
            # locate the coefficients on the confidence interval
            ax.scatter(y = pd.np.arange(self.df_params.shape[0]),
                       marker = 'o',
                       s = 160,
                       x = self.df_params['coef'],
                       color = self.df_params.Significant.map({True: '#20B2AA', False: '#F08080'}),
                      )

            # the zero axis line
            ax.axvline(x = 0, linestyle = '--', color = '#F08080', linewidth = 1)

            # plot title
            plt.title('95% confidence intervals', fontsize = 16, fontweight = 'bold')

            # font sizes for the axis
            ax.tick_params(axis = 'both', which = 'major', labelsize = 10)

            # font size for the coefficients
            ax.set_ylabel('Coefficients',fontsize = 12, fontweight = 'bold')

            # font size for the variables
            ax.set_xlabel('estimates',fontsize = 12, fontweight = 'bold')

            return plt.show()

max_stars_repo_name: G-Geofrey/mplot-package

max_issues_repo_name: G-Geofrey/mplot-package

max_forks_repo_name: G-Geofrey/mplot-package

reason: import numpy,import statsmodels,from statsmodels


text:
"""Module for feature computation.

Created on Wed Oct 14 22:13:28 2020

@author: hanjo
"""
import numpy as np
import pandas as pd
from Bio.SeqUtils.ProtParam import ProteinAnalysis
from xifaims import const
from xirt import features as xirtf
from modlamp.descriptors import GlobalDescriptor
from pyteomics import electrochem

def get_electrochem_charge(peptide, pH=2.8):
    """Compute charge using pyteomics."""
    return electrochem.charge(peptide, pH=pH)

def only_upper(seq):
    """
    Return only upper sequences.

    Parameters
    ----------
    seq : TYPE
        DESCRIPTION.

    Returns
    -------
    TYPE
        DESCRIPTION.

    """
    return "".join([i for i in seq if i.isupper()])

def get_structure_perc(seq, structure="helix"):
    """Use biopython to compute helix, turn, sheet % from the amino acid composition."""
    bio_seq = ProteinAnalysis(seq)
    helix, turn, sheets = bio_seq.secondary_structure_fraction()

    if structure == "helix":
        return helix

    elif structure == "turn":
        return turn

    else:
        return sheets

def compute_features(df, onehot=True):
    """
    Compute features for peptide sequences.

    Parameters
    ----------
    df : df
        df with psms.

    Returns
    -------
    df_features : df
        feature df.

    """
    # https://www.ncbi.nlm.nih.gov/pmc/articles/PMC5750047/

    df["seq1seq2"] = df["PepSeq1"] + df["PepSeq2"]

    # computes standard features
    df_features = pd.DataFrame(index=df.index)
    df_features["length1"] = df["PeptideLength1"]
    df_features["length2"] = df["PeptideLength2"]
    df_features["length1+length2"] = df["PeptideLength1"] + \
        df["PeptideLength2"]
    df_features["mass"] = df["exp mass"]
    df_features["log10mass"] = np.log10(df["exp mass"])

    # modifications
    df_features["loop"] = df["seq1seq2"].str.count("loop")
    df_features["oh"] = df["seq1seq2"].str.count("oh")
    df_features["nh2"] = df["seq1seq2"].str.count("nh2")

    if onehot:
        ohc = pd.get_dummies(df["exp charge"])
        ohc.columns = ["charge_" + str(i) for i in ohc.columns]
        df_features = df_features.join(ohc)
    else:
        df_features["p.charge"] = df["exp charge"]

    df_features["DE"] = df["seq1seq2"].str.count("D") + df["seq1seq2"].str.count("E")
    df_features["KR"] = df["seq1seq2"].str.count("K") + df["seq1seq2"].str.count("R")
    df_features["aromatics"] = df["seq1seq2"].str.count("F") + \
        df["seq1seq2"].str.count("W") + df["seq1seq2"].str.count("Y")

    df_features["proline"] = df["seq1seq2"].str.count("P")
    df_features["Glycine"] = df["seq1seq2"].str.count("G")

    # charge
    #df_features["charge_cmp"] = df["seq1seq2"].apply(get_electrochem_charge)

    # biopython
    df_features["helix"] = df["seq1seq2"].apply(get_structure_perc, args=("helix",))
    df_features["sheet"] = df["seq1seq2"].apply(get_structure_perc, args=("sheet",))
    df_features["turn"] = df["seq1seq2"].apply(get_structure_perc, args=("turn",))
    df_features["pi"] = df["seq1seq2"].apply(xirtf.get_pi)

    # modlamp features
    seqs = [only_upper(i) for i in df["seq1seq2"]]
    glob = GlobalDescriptor(seqs)

    glob.calculate_charge(ph=2.8, amide=False)
    df_features["charge_glob"] = np.ravel(glob.descriptor)

    glob.charge_density(ph=2.8, amide=False)
    df_features["charge_density"] = np.ravel(glob.descriptor)

    glob.hydrophobic_ratio()
    df_features["hydrophobic_ratio"] = np.ravel(glob.descriptor)

    df_features["intrinsic_size_sum"] = df["seq1seq2"].apply(
        fsummarizer, args=(const.intrnsicsize_map, np.sum))
    df_features["intrinsic_size_std"] = df["seq1seq2"].apply(
        fsummarizer, args=(const.intrnsicsize_map, np.std))
    df_features["intrinsic_size_max"] = df["seq1seq2"].apply(
        fsummarizer, args=(const.intrnsicsize_map, np.max))

    df_features["mv_sum"] = df["seq1seq2"].apply(
        fsummarizer, args=(const.molecularvolumne_map, np.sum))
    df_features["mv_size_std"] = df["seq1seq2"].apply(
        fsummarizer, args=(const.molecularvolumne_map, np.std))
    df_features["mv_size_max"] = df["seq1seq2"].apply(
        fsummarizer, args=(const.molecularvolumne_map, np.max))

    df_features["polarity_sum"] = df["seq1seq2"].apply(
        fsummarizer, args=(const.polarity_map, np.sum))
    df_features["polarity_size_std"] = df["seq1seq2"].apply(
        fsummarizer, args=(const.polarity_map, np.std))
    df_features["polarity_size_max"] = df["seq1seq2"].apply(
        fsummarizer, args=(const.polarity_map, np.max))

    df_features["secondstruc_sum"] = df["seq1seq2"].apply(
        fsummarizer, args=(const.secondarystructure_map, np.sum))
    df_features["secondstruc_size_std"] = df["seq1seq2"].apply(
        fsummarizer, args=(const.secondarystructure_map, np.std))
    df_features["secondstruc_size_max"] = df["seq1seq2"].apply(
        fsummarizer, args=(const.secondarystructure_map, np.max))

    df_features["estatic_sum"] = df["seq1seq2"].apply(
        fsummarizer, args=(const.electrostaticcharge_map, np.sum))
    df_features["sstatic_size_std"] = df["seq1seq2"].apply(
        fsummarizer, args=(const.electrostaticcharge_map, np.std))
    df_features["static_size_max"] = df["seq1seq2"].apply(
        fsummarizer, args=(const.electrostaticcharge_map, np.max))

    df_features["m/z"] = df["exp m/z"]

    return df_features

def fsummarizer(pepseq, prop_dict, func):
    """
    Summarize pepide sequence with by mapping amino acids to prop_dict entires and a summary func.

    Parameters
    ----------
    pepseq : str
        sequence.
    prop_dict : dict
        amino acid property mapping.
    func : function
        Summary function (sum, std, max, ..).

    Returns
    -------
    func applied to mapped AA outcomes.

    """
    return func([prop_dict[i] for i in pepseq if i.isupper()])

max_stars_repo_name: Rappsilber-Laboratory/xiFAIMS

max_issues_repo_name: Rappsilber-Laboratory/xiFAIMS

max_forks_repo_name: Rappsilber-Laboratory/xiFAIMS


text:
import os
import gym
import numpy as np
from gym import spaces
from scipy.misc import imresize

"""
the wrapper is taken from the openai baselines

"""

class ResizeWrapper(gym.ObservationWrapper):
    def __init__(self, env=None, shape=(3, 84, 84)):
        super(ResizeWrapper, self).__init__(env)
        self.observation_space.shape = shape
        self.observation_space = spaces.Box(
            self.observation_space.low[0, 0, 0],
            self.observation_space.high[0, 0, 0],
            shape,
            dtype=self.observation_space.dtype)
        self.shape = shape

    def observation(self, observation):
        return imresize(observation[35:195], self.shape[1:]).astype(np.float32).reshape(self.shape)/255.

def make_atari(env_id):
    env = gym.make(env_id)
    env = ResizeWrapper(env)
    return env

reason: import numpy,from scipy


text:
import pandas as pd
import numpy as np
from numpy.testing import assert_allclose

import tempfile

import pytest

from thermostat_nw.exporters import metrics_to_csv
from thermostat_nw.multiple import (
    multiple_thermostat_calculate_epa_field_savings_metrics,
)

from .fixtures.single_stage import (
    thermostat_type_1,
    thermostat_type_2,
    thermostat_type_3,
    thermostat_type_4,
    thermostat_type_5,
    metrics_type_1_data,
)
from thermostat_nw.columns import EXPORT_COLUMNS
import six

@pytest.fixture(scope="session")
def metrics_type_1(thermostat_type_1):
    metrics_type_1 = thermostat_type_1.calculate_epa_field_savings_metrics(
        core_cooling_day_set_method="entire_dataset",
        core_heating_day_set_method="entire_dataset",
    )
    return metrics_type_1

@pytest.fixture(scope="session")
def metrics_type_1_multiple(thermostat_type_1):
    metrics_type_1 = multiple_thermostat_calculate_epa_field_savings_metrics(
        [thermostat_type_1], how="entire_dataset"
    )
    return metrics_type_1

RTOL = 1e-3
ATOL = 1e-3

def test_calculate_epa_field_savings_metrics_type_1(
    metrics_type_1, metrics_type_1_data
):
    assert len(metrics_type_1) == len(metrics_type_1_data)

    for key in metrics_type_1[0].keys():
        test_value = metrics_type_1[0][key]
        target_value = metrics_type_1_data[0][key]
        if isinstance(test_value, six.string_types):
            assert test_value == target_value
        else:
            assert_allclose(test_value, target_value, rtol=RTOL, atol=ATOL)

    for key in metrics_type_1[1].keys():
        test_value = metrics_type_1[1][key]
        target_value = metrics_type_1_data[1][key]
        if isinstance(test_value, six.string_types):
            assert test_value == target_value
        else:
            assert_allclose(test_value, target_value, rtol=RTOL, atol=ATOL)

def test_multiple_thermostat_calculate_epa_field_savings_metrics_type_1(
    metrics_type_1_multiple, metrics_type_1_data
):
    # Test multiprocessing thermostat code
    assert len(metrics_type_1_multiple) == len(metrics_type_1_data)

    for key in metrics_type_1_multiple[0].keys():
        test_value = metrics_type_1_multiple[0][key]
        target_value = metrics_type_1_data[0][key]
        if isinstance(test_value, six.string_types):
            assert test_value == target_value
        else:
            assert_allclose(test_value, target_value, rtol=RTOL, atol=ATOL)

    for key in metrics_type_1_multiple[1].keys():
        test_value = metrics_type_1_multiple[1][key]
        target_value = metrics_type_1_data[1][key]
        if isinstance(test_value, six.string_types):
            assert test_value == target_value
        else:
            assert_allclose(test_value, target_value, rtol=RTOL, atol=ATOL)

def test_calculate_epa_field_savings_metrics_type_2(thermostat_type_2):
    metrics_type_2_entire = thermostat_type_2.calculate_epa_field_savings_metrics(
        core_cooling_day_set_method="entire_dataset",
        core_heating_day_set_method="entire_dataset",
    )
    assert len(metrics_type_2_entire) == 2

    metrics_type_2_yearly = thermostat_type_2.calculate_epa_field_savings_metrics(
        core_cooling_day_set_method="year_end_to_end",
        core_heating_day_set_method="year_mid_to_mid",
    )
    assert len(metrics_type_2_yearly) == 9

def test_calculate_epa_field_savings_metrics_type_3(thermostat_type_3):
    metrics_type_3 = thermostat_type_3.calculate_epa_field_savings_metrics(
        core_cooling_day_set_method="entire_dataset",
        core_heating_day_set_method="entire_dataset",
    )
    assert len(metrics_type_3) == 2

def test_calculate_epa_field_savings_metrics_type_4(thermostat_type_4):
    metrics_type_4 = thermostat_type_4.calculate_epa_field_savings_metrics(
        core_cooling_day_set_method="entire_dataset",
        core_heating_day_set_method="entire_dataset",
    )
    assert len(metrics_type_4) == 1

def test_calculate_epa_field_savings_metrics_type_5(thermostat_type_5):
    metrics_type_5 = thermostat_type_5.calculate_epa_field_savings_metrics(
        core_cooling_day_set_method="entire_dataset",
        core_heating_day_set_method="entire_dataset",
    )
    assert len(metrics_type_5) == 1

def test_metrics_to_csv(metrics_type_1):

    fd, fname = tempfile.mkstemp()
    df = metrics_to_csv(metrics_type_1, fname)

    assert isinstance(df, pd.DataFrame)
    assert df.columns[0] == "sw_version"
    assert df.columns[1] == "ct_identifier"

    with open(fname, "r") as f:
        lines = f.readlines()
        assert len(lines) == 3
        column_heads = lines[0].strip().split(",")
        assert column_heads == EXPORT_COLUMNS

reason: import numpy,from numpy


text:
# Important note: you do not have to modify this file for your homework.

import util
import numpy as np

def calc_grad(X, Y, theta):
    """Compute the gradient of the loss with respect to theta."""
    count, _ = X.shape

    probs = 1. / (1 + np.exp(-X.dot(theta)))
    grad = (Y - probs).dot(X)

    return grad

def logistic_regression(X, Y):
    """Train a logistic regression model."""
    theta = np.zeros(X.shape[1])
    learning_rate = 0.1

    i = 0
    while True:
        i += 1
        prev_theta = theta
        theta = theta_step(X, Y, theta, i, learning_rate)
        if np.linalg.norm(prev_theta - theta) < 1e-15:
            print('Converged in %d iterations' % i)
            break
    return

def theta_step(X, Y, theta, i, learning_rate):
    prev_theta = theta
    grad = calc_grad(X, Y, theta)
    theta = theta + learning_rate * grad
    if i % 10000 == 0:
        print('Finished %d iterations' % i)
        print(f'Theta: {theta}')
        print(f'Size of theta: {theta @ theta}')
        print(f'Size of grad: {grad @ grad}')
    return theta

def theta_step_n(X, Y, theta, learning_rate, n):
    for i in range(n):
        theta = theta_step(X, Y, theta, i, learning_rate)
    return theta

def main():
    print('==== Training model on data set A ====')
    Xa, Ya = util.load_csv('ds1_a.csv', add_intercept=True)
    logistic_regression(Xa, Ya)

    print('\n==== Training model on data set B ====')
    Xb, Yb = util.load_csv('ds1_b.csv', add_intercept=True)
    logistic_regression(Xb, Yb)

if __name__ == '__main__':
    main()


text:
from sqlite3.dbapi2 import Cursor
import cv2
import numpy as np
from PIL import Image
import pickle
import sqlite3

recognizer= cv2.createLBPHFaceRecognizer()
recognizer.load('trainner/trainner.yml')
cascadePath="Classifiers/face.xml"
faceCascade=cv2.CascadeClassifier(cascadePath)
path='dataset'

def getProfile(id):
    conn=sqlite3.connect("faceBase.py")
    cmd="SELECT * FROM People WHERE ID="+str(id)
    cursor=conn.execute(cmd)
    profile=None
    for row in cursor:
        profile=row
    conn.close()
    return profile

cam=cv2.VideoCapture(1)
font=cv2.cv.InitFont(cv2.cv.CV_FONT_HERSHEY_SIMPLEX, 1, 1,0, 1, 1)

while(True):
    ret,img=cam.read()
    gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    faces=faceCascade.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=5, minSize=(100,100), flags=cv2.CASCADE_SCALE_IMAGE)
    for (x,y,w,h) in faces:
        sampleNum=sampleNum+1
        cv2.imwrite("dataset/user."+id+'.'+str(sampleNum)+"jpg",gray[y:y+h,x:x+w])
        cv2.rectangle(im , (x-50,y-50), (x+w+50,y+h+50),(225,0,0),2)

    cv2.imshow('im',im)
    cv2.waitKey(100)
    if sampleNum>20:
        cam.realease()
        cv2.destroyAllWindows()
        break


text:
#
# For licensing see accompanying LICENSE file.
# Copyright (C) 2019 Apple Inc. All Rights Reserved.
#

from torchvision.datasets import CIFAR100
import numpy as np

class CIFAR100WithIdx(CIFAR100):
    """
    Extends CIFAR100 dataset to yield index of element in addition to image and target label.
    """

    def __init__(self,
                 root,
                 train=True,
                 transform=None,
                 target_transform=None,
                 download=False,
                 rand_fraction=0.0):
        super(CIFAR100WithIdx, self).__init__(root=root,
                                              train=train,
                                              transform=transform,
                                              target_transform=target_transform,
                                              download=download)

        assert (rand_fraction <= 1.0) and (rand_fraction >= 0.0)
        self.rand_fraction = rand_fraction

        if self.rand_fraction > 0.0:
            self.data = self.corrupt_fraction_of_data()

    def corrupt_fraction_of_data(self):
        """Corrupts fraction of train data by permuting image-label pairs."""

        # Check if we are not corrupting test data
        assert self.train is True, 'We should not corrupt test data.'

        nr_points = len(self.data)
        nr_corrupt_instances = int(np.floor(nr_points * self.rand_fraction))
        print('Randomizing {} fraction of data == {} / {}'.format(self.rand_fraction,
                                                                  nr_corrupt_instances,
                                                                  nr_points))
        # We will corrupt the top fraction data points
        corrupt_data = self.data[:nr_corrupt_instances, :, :, :]
        clean_data = self.data[nr_corrupt_instances:, :, :, :]

        # Corrupting data
        rand_idx = np.random.permutation(np.arange(len(corrupt_data)))
        corrupt_data = corrupt_data[rand_idx, :, :, :]

        # Adding corrupt and clean data back together
        return np.vstack((corrupt_data, clean_data))

    def __getitem__(self, index):
        """
        Args:
            index (int):  index of element to be fetched

        Returns:
            tuple: (sample, target, index) where index is the index of this sample in dataset.
        """
        img, target = super().__getitem__(index)
        return img, target, index


text:
"""
Author: Peratham Wiriyathammabhum

"""
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

def kmeanspp(X, k=3, thres=10**-5, max_iters=200):
	"""
	Perform k-means++ clustering on an input row matrix X.
	See: http://www.ciml.info/dl/v0_9/ciml-v0_9-ch13.pdf
	"""
	ni, nd = X.shape
	mu = np.zeros((k, nd))
	krows = np.random.choice(ni, 1, replace=False)
	nmu = X[krows]
	nmu = kmeanspp_init(nmu, X, k)
	nite = 0
	while not terminating_cond(mu, nmu, thres, nite, max_iters):
		mu = nmu

		# e-step: centroid assignments from parameters
		dist = ((X[:,None,:] - mu[None,:,:])**2).sum(axis=2)
		print('[Info] iter: {} SSE: {}'.format(nite, dist.sum()))
		# m-step: estimate latent parameters (centroids)
		cidx = np.argmin(dist, axis=1)
		for i in range(k):
			nmu[i] = X[cidx==i].mean(axis=0)

		nite += 1
		if nite % 10 == 0:
			print('[Info] iter: {}'.format(nite))
	return mu

def kmeanspp_init(nmu, X, k):
	ni, nd = X.shape
	for i in range(1, k):
		dist = ((X[:,None,:] - nmu[None,:,:])**2).sum(axis=2)
		dist = dist.min(axis=1)
		dist /= dist.sum()
		newm = np.random.choice(ni, 1, replace=False, p = dist)
		newrow = X[newm]
		nmu = np.append(nmu, newrow, axis=0)
	return nmu

def terminating_cond(mu, nmu, thres, nite, max_iters):
	if nite >= max_iters:
		print('[Info] terminate at iter: {}'.format(nite))
		return True
	# elif np.linalg.norm(mu - nmu) < thres:
	# 	print('[Info] terminate at iter: {}'.format(nite))
	# 	return True
	else:
		return False

def main(opts):
	k = opts['k']
	max_iters = opts['max_iters']
	from sklearn import datasets
	iris = datasets.load_iris()
	X = iris.data
	mu = kmeanspp(X, k=k, thres=10**-5, max_iters=max_iters)

	# plot
	dist = ((X[:,None,:] - mu[None,:,:])**2).sum(axis=2)
	print('[Info] SSE: {}'.format(dist.sum()))
	cidx = np.argmin(dist, axis=1)
	fig = plt.figure()
	ax = fig.add_subplot(111, projection='3d')
	for c in range(k):
		cluster_members = [X[i] for i in range(len(X)) if cidx[i] == c]
		cluster_members = np.array(cluster_members)

		ax.scatter(cluster_members[:,0], cluster_members[:,1], cluster_members[:,2], s= 0.5)
	input("Press Enter to continue...")
	"""
	For details see:
	https://blog.paperspace.com/speed-up-kmeans-numpy-vectorization-broadcasting-profiling/
	https://github.com/siddheshk/Faster-Kmeans
	"""
	return

if __name__ == '__main__':
	import argparse

	parser = argparse.ArgumentParser(description='run k-means.')
	parser.add_argument('--k', dest='k',
					  help='number of clusters',
					  default=3, type=int)
	parser.add_argument('--max_iters', dest='max_iters',
					  help='number of iterations to train',
					  default=200, type=int)
	args = parser.parse_args()
	opts = vars(args)

	main(opts)

max_stars_repo_name: perathambkk/ml-techniques

max_issues_repo_name: perathambkk/ml-techniques

max_forks_repo_name: perathambkk/ml-techniques


text:
#!/usr/bin/env python2
import rospy
import cv2
import numpy
from std_msgs.msg import String
from sensor_msgs.msg import Image,CompressedImage
from cv_bridge import CvBridge, CvBridgeError

class image_converter:

    def __init__(self):
        self.id=0
        self.cap = cv2.VideoCapture(self.id)
        #read frame or not
        self.ret = False
        #self.image_pub = rospy.Publisher("image_topic",CompressedImage,queue_size=1)
        self.image_pub = rospy.Publisher("image_topic",Image,queue_size=1)
        self.bridge = CvBridge()
        self.show()

    def show(self):
        while(1):
        # get a frame
            self.ret, frame = self.cap.read()
            if(self.ret == False):
                self.id = self.id+1
                self.cap = cv2.VideoCapture(self.id)
                self.ret, frame = self.cap.read()
            # show a frame
            frame=cv2.resize(frame, None, fx=0.3, fy=0.3)
            # cv2.imshow("capture", frame)
            #print(frame.shape)
            # if cv2.waitKey(1) & 0xFF == ord('q'):
            #     break

            try:
                #self.image_pub.publish(self.bridge.cv2_to_compressed_imgmsg(frame, "jpg"))
                self.image_pub.publish(self.bridge.cv2_to_imgmsg(frame, "bgr8"))
            except CvBridgeError as e:
                print(e)

if __name__ == '__main__':
    rospy.init_node('usbcam', anonymous=True)
    rospy.loginfo("Starting usbcam node")
    ic = image_converter()
    cv2.destroyAllWindows()

max_stars_repo_name: bianzhenkun/IntelligentShip

max_issues_repo_name: bianzhenkun/IntelligentShip

max_forks_repo_name: bianzhenkun/IntelligentShip


text:
#!/usr/bin/env python3
#
# Author: Liang Yu
# Copyright 2019-

import numpy as np
import os
import iscetest
from nisar.workflows import rdr2geo

class Rdr2GeoOpts:
    '''
    class to emulate argparse terminal input
    member values set to test basic functionality
    values can be adjusted to meet test requirements
    '''
    product = iscetest.data + 'envisat.h5'
    dem = iscetest.data + 'srtm_cropped.tif'
    freq = 'A'
    outdir = 'rdr2geo'
    mask = False

    gpu = True

def checkError(f_test, f_ref, dtype, tol, test_type):
    '''
    calculate error for file in vrt
    '''
    # retrieve data
    test = np.fromfile(f_test, dtype=dtype)
    ref = np.fromfile(f_ref, dtype=dtype)

    # calculate average error
    diff = np.abs(test - ref)
    diff = diff[diff < 5]
    error = np.mean(diff)

    # error check
    fname = os.path.basename(f_test)
    assert (error < tol), f'NISAR Python {test_type} rdr2geo fail at {fname}: {error} >= {tol}'

def testCudaRdr2Geo():
    '''
    run rdr2geo on GPU
    '''
    # init inputs
    opts = Rdr2GeoOpts()
    # unique cuda output for cuda geo2rdr to process from
    opts.outdir = 'rdr2geo_cuda'

    # run resamp
    rdr2geo.main(opts)

    # vrt constituent files to compare
    fnames = ['x.rdr', 'y.rdr', 'z.rdr', 'inc.rdr', 'hdg.rdr', 'localInc.rdr', 'localPsi.rdr']
    # dtypes of vrt constituent files
    dtypes = [np.float64, np.float64, np.float64, np.float32, np.float32, np.float32, np.float32]
    # tolerances per vrt constituent file
    tols = [1.0e-5, 1.0e-5, 0.15, 1.0e-4, 1.0e-4, 0.02, 0.02]

    # check errors
    for fname, dtype, tol in zip(fnames, dtypes, tols):
        checkError(os.path.join(opts.outdir, fname),
                os.path.join(iscetest.data, 'topo', fname),
                dtype,
                tol,
                'CUDA')

if __name__ == '__main__':
    testCudaRdr2Geo()

# end of file


text:
#!/usr/bin/env python

import sys
import pylandau
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mlp
from scipy.optimize import curve_fit

import ScopeTrace

merge_bins = 1

raw = True
plotting = True
fitting = True

#plotting = False
#fitting = False

def fit_pulse(trace):
    # fit the highest peak -> find the highest value
    x_array = np.array(trace.xvalues)
    y_array = np.array(trace.inverted())

    # find start value for position (if multiple max y values, selects the first)
    idx = np.where(y_array == y_array.max())
    idx = idx[0][0]
    #x_values_peak = x_array[idx]

    pname = [ 'Position','Amplitude','Width']
    mpv = x_array[idx]           # position of peak
    amp = y_array.max()          # height of peak
    width = 1                    # width of peak

    # The uncertainty on the y-values is assumed to be the same for each separate bin otherwise
    # you have to specify the uncertainties explicitely
    par, pcov = curve_fit(pylandau.landau, x_array, y_array, p0=(mpv, width, amp))

    plot_pulse(trace,baseline)
    plt.plot(x_array,mylandau(x_array,par[0],par[1],par[2]),label='Landau Fit')

    return pname, par, pcov

def mylandau(xvals,peak,width,ampl):
    # transform the input to the landau offered by the package and return the scaled values
    # mylandau has 3 parameters: peak position, peak width and peak amplitude
    nxvals = np.array([]) # n - refers to normalized values
    for xval in xvals:
        nxvals = np.append(nxvals,(xval-peak)/width)
    yvals = pylandau.landau(nxvals)

    return yvals * ampl

def plot_pulse(trace,baseline):

    # get x and y values of the traces
    recs = trace.xvalues
    adcs = trace.inverted()

    # define the figure
    fig = mlp.pyplot.gcf()
    fig.set_size_inches(18.5, 14.5)
    plt.figure(1)
    plt.plot(recs,adcs)

    # draw horizontal
    plt.axhline(y=0,color='b', linestyle='-', linewidth=2)

    # make plot nicer
    plt.xlabel('x-trace [n%s]'%trace.horizontal_units, fontsize=28)
    plt.ylabel('y-reading [%s]'%trace.vertical_units, fontsize=28)

    # tick marker sizes
    ax = plt.gca()
    ax.xaxis.set_tick_params(labelsize=16)
    ax.yaxis.set_tick_params(labelsize=20)

    ymin,ymax = ax.get_ylim()
    dy = ymax-ymin
    xmin,xmax = ax.get_xlim()
    dx = xmax-xmin
    plt.text(xmin-0.1*dx,ymin-0.05*dy, r'Source:  %s'%(trace.source), fontsize=20)

    # save plot for later viewing
    plt.subplots_adjust(top=0.99, right=0.99, bottom=0.10, left=0.10)
    #plt.show()

def plot_trace(trace):

    # get x and y values of the traces
    recs = trace.xvalues
    adcs = trace.yvalues

    # define the figure
    fig = mlp.pyplot.gcf()
    fig.set_size_inches(18.5, 14.5)
    plt.figure(1)
    plt.plot(recs,adcs)

    # tick marker sizes
    ax = plt.gca()
    ax.xaxis.set_tick_params(labelsize=16)
    ax.yaxis.set_tick_params(labelsize=20)

    ymin,ymax = ax.get_ylim()
    dy = ymax-ymin
    xmin,xmax = ax.get_xlim()
    dx = xmax-xmin
    plt.text(xmin-0.1*dx,ymin-0.05*dy, r'Source:  %s'%(trace.source), fontsize=20)

    # save plot for later viewing
    plt.subplots_adjust(top=0.99, right=0.99, bottom=0.10, left=0.10)

#---------------------------------------------------------------------------------------------------
# M A I N
#---------------------------------------------------------------------------------------------------
# initial settings
mlp.rcParams['axes.linewidth'] = 2

print(" Analysing: %d files as given in the command line"%(len(sys.argv[1:])))

for filename in sys.argv[1:]:

    # read the raw data
    print(" File: %s"%(filename))
    with open(filename,"r") as file:
        data = file.read()

    # decode the scope trace
    trace = ScopeTrace.ScopeTrace(data,merge_bins)
    x_array = np.array(trace.xvalues)
    if trace.reading_error>1 or len(trace.yvalues)<10:
        print(" WARNING -- skipping this file has reading errors: %s"%(filename))
        continue

    if raw:
        print(' Show the raw data.')
        plot_trace(trace)
        plt.show()

    # find baseline and jitter
    baseline,jitter = trace.find_baseline_and_jitter(0,250)

    print(' Filename: %s,  Baseline: %10.6f,  Jitter: %10.6f'%(filename,baseline,jitter))

    if plotting:
        print(' Show the data after finding a baseline and inverting the pulse.')
        plot_pulse(trace,baseline)
        plt.show()

    if fitting:
        print(' Perform a fit and determine the Landau distribution parameters.')
        pname, par, pcov = fit_pulse(trace)
        plt.show()
        print(" Fit results - Parameters: ")
        print(par)
        print(" Fit results - Covariance matrix: ")
        print(pcov)
        print(" ========================================")
        for i in range(0,len(par)):
            print(" P(%d,%9s) = %f +- %f"%(i,pname[i],par[i],np.sqrt(pcov[i][i])))

max_stars_repo_name: Sangbaek/JLabExperiments

max_issues_repo_name: Sangbaek/JLabExperiments

max_forks_repo_name: Sangbaek/JLabExperiments


text:
import numpy as np
import cv2
from keras.models import load_model
from skimage.transform import resize, pyramid_reduce
import PIL
from PIL import Image
from keras.preprocessing import image

model = load_model('/home/puneeth/Desktop/only_names.h5')
uniq_labels = ['A', 'B', 'E', 'H', 'N', 'P', 'T', 'U', 'nothing']
def prediction(pred):
    return(chr(pred+ 65))

def crop_image(image, x, y, width, height):
    return image[y:y + height, x:x + width]

def keras_predict(model, image):
    data = np.asarray( image, dtype="int32" )

    pred_probab = model.predict(data)[0]
    pred_class = list(pred_probab).index(max(pred_probab))
    return max(pred_probab), pred_class

def main():

	cap = cv2.VideoCapture(0)

	while(True):

	    ret, frame = cap.read()
	    im2 = crop_image(frame, 300,300,300,300)
		# load the model we save
		img = img.resize((64,64,3)
		x = image.img_to_array(img)
		x = np.expand_dims(x, axis=0)
		# print(uniq_labels)
		images = np.vstack([x])
		classes = model.predict_classes(images, batch_size=10)
	    cv2.putText(frame, curr, (700, 300), cv2.FONT_HERSHEY_COMPLEX, 4.0, (255, 255, 255), lineType=cv2.LINE_AA)
	    cv2.rectangle(frame, (300, 300), (600, 600), (255, 255, 00), 3)
	    cv2.imshow("frame",frame)
	    print(uniq_labels[classes[0]])
	    # cv2.imshow('frame',blurred)
	    if cv2.waitKey(1) & 0xFF == ord('q'):
	        break
if __name__ == '__main__':
    main()

cap.release()
cv2.destroyAllWindows()


text:
import pickle, time, warnings
import numpy as np

import torch
from torch.utils.data import Dataset, IterableDataset, DataLoader, Sampler, BatchSampler

from utils.tools import Config as cfg
from utils.tools import DataProcessing as DP

class PointCloudsDataset(Dataset):
    def __init__(self, dir, labels_available=True):
        self.paths = list(dir.glob(f"*.npy"))
        self.labels_available = labels_available

    def __getitem__(self, idx):
        path = self.paths[idx]

        points, labels = self.load_npy(path)

        points_tensor = torch.from_numpy(points).float()
        labels_tensor = torch.from_numpy(labels).long()

        return points_tensor, labels_tensor

    def __len__(self):
        return len(self.paths)

    def load_npy(self, path):
        r"""
            load the point cloud and labels of the npy file located in path

            Args:
                path: str
                    path of the point cloud
                keep_zeros: bool (optional)
                    keep unclassified points
        """
        cloud_npy = np.load(path, mmap_mode="r").T
        points = cloud_npy[:, :-1] if self.labels_available else points

        if self.labels_available:
            labels = cloud_npy[:, -1]

            # balance training set
            points_list, labels_list = [], []
            for i in range(len(np.unique(labels))):
                try:
                    idx = np.random.choice(len(labels[labels == i]), 8000)
                    points_list.append(points[labels == i][idx])
                    labels_list.append(labels[labels == i][idx])
                except ValueError:
                    continue
            if points_list:
                points = np.stack(points_list)
                labels = np.stack(labels_list)
                labeled = labels > 0
                points = points[labeled]
                labels = labels[labeled]

        return points, labels

class CloudsDataset(Dataset):
    def __init__(self, dir, data_type="npy"):
        self.path = dir
        self.paths = list(dir.glob(f"*.{data_type}"))
        self.size = len(self.paths)
        self.data_type = data_type
        self.input_trees = {"training": [], "validation": []}
        self.input_labels = {"training": [], "validation": []}
        self.input_names = {"training": [], "validation": []}
        self.val_proj = []
        self.val_labels = []
        self.val_split = "1_"

        self.load_data()

    def load_data(self):
        for i, file_path in enumerate(self.paths):
            t0 = time.time()
            cloud_name = file_path.stem
            if self.val_split in cloud_name:
                cloud_split = "validation"
            else:
                cloud_split = "training"

            # Name of the input files
            kd_tree_file = self.path / "{:s}_KDTree.pkl".format(cloud_name)
            sub_npy_file = self.path / "{:s}.npy".format(cloud_name)

            data = np.load(sub_npy_file, mmap_mode="r").T

            sub_labels = data[:, -1].copy()

            # Read pkl with search tree
            with open(kd_tree_file, "rb") as f:
                search_tree = pickle.load(f)

            # The points information is in tree.data
            self.input_trees[cloud_split].append(search_tree)
            self.input_labels[cloud_split].append(sub_labels)
            self.input_names[cloud_split].append(cloud_name)

        print("\nPreparing reprojected indices for testing")

        # Get validation and test reprojected indices

        for i, file_path in enumerate(self.paths):
            t0 = time.time()
            cloud_name = file_path.stem

            # Validation projection and labels
            if self.val_split in cloud_name:
                proj_file = self.path / "{:s}_proj.pkl".format(cloud_name)
                with open(proj_file, "rb") as f:
                    proj_idx, labels = pickle.load(f)

                self.val_proj += [proj_idx]
                self.val_labels += [labels]
                print("{:s} done in {:.1f}s".format(cloud_name, time.time() - t0))

    def __getitem__(self, idx):
        pass

    def __len__(self):
        # Number of clouds
        return self.size

class ActiveLearningSampler(IterableDataset):
    def __init__(self, dataset, batch_size=6, split="training"):
        self.dataset = dataset
        self.split = split
        self.batch_size = batch_size
        self.possibility = {}
        self.min_possibility = {}

        if split == "training":
            self.n_samples = cfg.train_steps
        else:
            self.n_samples = cfg.val_steps

        # Random initialisation for weights
        self.possibility[split] = []
        self.min_possibility[split] = []
        for i, tree in enumerate(self.dataset.input_colors[split]):
            self.possibility[split] += [np.random.rand(tree.data.shape[0]) * 1e-3]
            self.min_possibility[split] += [float(np.min(self.possibility[split][-1]))]

    def __iter__(self):
        return self.spatially_regular_gen()

    def __len__(self):
        return (
            self.n_samples
        )  # not equal to the actual size of the dataset, but enable nice progress bars

    def spatially_regular_gen(self):
        # Choosing the least known point as center of a new cloud each time.

        for i in range(self.n_samples * self.batch_size):  # num_per_epoch
            # t0 = time.time()
            if cfg.sampling_type == "active_learning":
                # Generator loop

                # Choose a random cloud
                cloud_idx = int(np.argmin(self.min_possibility[self.split]))

                # choose the point with the minimum of possibility as query point
                point_ind = np.argmin(self.possibility[self.split][cloud_idx])

                # Get points from tree structure
                points = np.array(
                    self.dataset.input_trees[self.split][cloud_idx].data, copy=False
                )

                # Center point of input region
                center_point = points[point_ind, :].reshape(1, -1)

                # Add noise to the center point
                noise = np.random.normal(scale=3.5 / 10, size=center_point.shape)
                pick_point = center_point + noise.astype(center_point.dtype)

                if len(points) < cfg.num_points:
                    queried_idx = self.dataset.input_trees[self.split][cloud_idx].query(
                        pick_point, k=len(points)
                    )[1][0]
                else:
                    queried_idx = self.dataset.input_trees[self.split][cloud_idx].query(
                        pick_point, k=cfg.num_points
                    )[1][0]

                queried_idx = DP.shuffle_idx(queried_idx)
                # Collect points and colors
                queried_pc_xyz = points[queried_idx]
                queried_pc_xyz = queried_pc_xyz - pick_point
                queried_pc_labels = self.dataset.input_labels[self.split][cloud_idx][
                    queried_idx
                ]

                dists = np.sum(
                    np.square((points[queried_idx] - pick_point).astype(np.float32)),
                    axis=1,
                )
                delta = np.square(1 - dists / np.max(dists))
                self.possibility[self.split][cloud_idx][queried_idx] += delta
                self.min_possibility[self.split][cloud_idx] = float(
                    np.min(self.possibility[self.split][cloud_idx])
                )

                if len(points) < cfg.num_points:
                    queried_pc_xyz, queried_idx, queried_pc_labels = DP.data_aug(
                        queried_pc_xyz, queried_pc_labels, queried_idx, cfg.num_points
                    )

            # Simple random choice of cloud and points in it
            elif cfg.sampling_type == "random":

                cloud_idx = np.random.choice(len(self.min_possibility[self.split]), 1)[
                    0
                ]
                points = np.array(
                    self.dataset.input_trees[self.split][cloud_idx].data, copy=False
                )
                queried_idx = np.random.choice(
                    len(self.dataset.input_trees[self.split][cloud_idx].data),
                    cfg.num_points,
                )
                queried_pc_xyz = points[queried_idx]
                queried_pc_labels = self.dataset.input_labels[self.split][cloud_idx][
                    queried_idx
                ]

            queried_pc_xyz = torch.from_numpy(queried_pc_xyz).float()
            queried_pc_labels = torch.from_numpy(queried_pc_labels).long()
            queried_idx = torch.from_numpy(queried_idx).float()  # keep float here?
            cloud_idx = torch.from_numpy(np.array([cloud_idx], dtype=np.int32)).float()

            points = queried_pc_xyz

            yield points, queried_pc_labels

def data_loaders(dir, sampling_method="active_learning", **kwargs):
    if sampling_method == "active_learning":
        dataset = CloudsDataset(dir / "train")
        batch_size = kwargs.get("batch_size", 6)
        val_sampler = ActiveLearningSampler(
            dataset, batch_size=batch_size, split="validation"
        )
        train_sampler = ActiveLearningSampler(
            dataset, batch_size=batch_size, split="training"
        )
        return DataLoader(train_sampler, **kwargs), DataLoader(val_sampler, **kwargs)

    if sampling_method == "naive":
        train_dataset = PointCloudsDataset(dir / "train")
        val_dataset = PointCloudsDataset(dir / "val")
        return (
            DataLoader(train_dataset, shuffle=True, **kwargs),
            DataLoader(val_dataset, **kwargs),
        )

    raise ValueError(f"Dataset sampling method '{sampling_method}' does not exist.")

if __name__ == "__main__":
    dataset = CloudsDataset("datasets/s3dis/subsampled/train")
    batch_sampler = ActiveLearningSampler(dataset)
    for data in batch_sampler:
        xyz, labels, idx, cloud_idx = data
        print("Number of points:", len(xyz))
        print("Point position:", xyz[1])
        print("Label:", labels[1])
        print("Index of point:", idx[1])
        print("Cloud index:", cloud_idx)
        break

max_stars_repo_name: sen-aritra/MyRandLa

max_issues_repo_name: sen-aritra/MyRandLa

max_forks_repo_name: sen-aritra/MyRandLa


text:
import random
from builtins import range

from Bird import Bird

class Population:
    # TODO: NOT WORKING BECAUSE BIRDS ARE NOT GETTING ADDED AFTER NEW GENERATION
    def __init__(self, population_size=10):
        """
        Constructor for the population object which has a list of species
        :param population_size: Number of total genomes inclusive of all species
        """
        self.population_size = population_size
        self.generation = 0
        self.max_fitness = 0
        self.species_list = []  # List of species
        self.stale_species = int(self.population_size * 3 / 10)
        self.population = self.initialize_population()

    def initialize_population(self):
        """
        Initializes population by creating random genomes and adding them to appropriate species
        """
        from NEAT.Genome import Genome
        import numpy as np
        birds = list()

        for i in range(self.population_size):
            random_genome_connections = Genome.get_random_connection_genes(input_nodes=2, output_nodes=1, init=True)
            genome = Genome(random_genome_connections, input_nodes=2, output_nodes=1)
            self.add_to_species(genome)
            birds.append(Bird(100, np.random.randint(20, 500), genome=genome, show_bird=True))

        return birds

    def get_population(self):
        return self.population.copy()

    def add_to_species(self, genome):
        """
        Adds a genome to the appropriate species
        :param genome: The genome to be added in a species
        """
        from NEAT.Species import Species
        for species in self.species_list:  # Check for every species
            if Species.is_same_species(genome, species):
                species.genomes.append(genome)  # Append to already existing species
                return
        self.species_list.append(Species(genome))  # Make a new species and append it to list

    def calculate_total_average_fitness(self):
        """
        Calculates the total average fitness of the whole population
        """
        from functools import reduce
        return float(
            reduce(
                lambda sum, species1: sum + species1.calculate_average_fitness(),
                self.species_list, 0.0) / len(self.species_list)
        )

    def create_new_generation(self):
        """
        Creates a new generation of population by using crossover and removing stale and weak species
        """
        import math
        # Remove bottom half of the genomes in each species based on fitness
        self.remove_bottom_half_genomes_in_species()

        # Remove stale species
        # TODO: Don't know how this works. Copied from https://github.com/NeatMonster/NEATFlappyBird
        self.remove_stale_species()

        for species in self.species_list:
            species.calculate_average_fitness()

        # Remove weak species
        # TODO: Don't know how this works. Copied from https://github.com/NeatMonster/NEATFlappyBird
        self.remove_weak_species()

        # Calculate the total average fitness of this generation
        sum = self.total_average_fitness()
        if sum == 0:
            self.initialize_population()
            return
        children = list()
        for species in self.species_list:
            # Don't know why
            # TODO: Don't know how this works. Copied from https://github.com/NeatMonster/NEATFlappyBird
            breed = math.floor(species.average_fitness / sum * self.population_size) - 1.0
            for i in range(0, int(breed)):
                children.append(species.make_child())
        # Remove every genomes in every species except the strongest of each
        self.remove_all_genomes_in_species_except_strongest()

        # Create children
        while len(children) + len(self.species_list) < self.population_size:
            random_species = self.species_list[random.choice(list(self.species_list))]
            children.append(random_species.make_child())

        for child in children:
            self.add_to_species(child)
        self.generation += 1

    def remove_stale_species(self):
        """
        Removes the stale species, which have not created any considerable offsprings in the stale species
        threshold generation
        """
        survived = list()
        for species in self.species_list:
            species.genomes = list(reversed(sorted(species.genomes, key=lambda genome: genome.fitness)))

            if species.genomes[0].fitness > species.top_fitness:
                species.topFitness = species.genomes[0].fitness
                species.staleness = 0
            else:
                species.staleness += 1
            if species.staleness < self.stale_species or species.top_fitness >= self.max_fitness:
                survived.append(species)

            self.species_list.clear()
            self.species_list = survived

    def remove_weak_species(self):
        """
        Removes the weak species which have an average fitness less than the threshold below
        TODO: DON'T KNOW HOW THIS WORKS !
        """
        import math
        survived = list()
        sum = self.total_average_fitness()
        if sum == 0:
            self.initialize_population()
            return
        for species in self.species_list:
            breed = math.floor(species.average_fitness / sum * self.population_size)
            if breed >= 1.0:
                survived.append(species)
        self.species_list.clear()
        self.species_list = survived

    def total_average_fitness(self):
        """
        Calculates the average fitness of all species
        """
        from functools import reduce
        return reduce(lambda sum, species_1: sum + species_1.average_fitness, self.species_list, 0.0)

    def remove_bottom_half_genomes_in_species(self):
        """
        Removes the bottom half genomes in each species depending of the genome fitness
        """
        for species in self.species_list:
            species.genomes = list(reversed(sorted(species.genomes, key=lambda genome: genome.fitness)))
            half_point = int(len(species.genomes) / 2)
            while len(species.genomes) > half_point:
                species.genomes.remove(species.genomes[len(species.genomes) - 1])

    def remove_all_genomes_in_species_except_strongest(self):
        """
        Removes all genomes in each species except the strongest ones
        """
        for species in self.species_list:
            species.genomes = list(reversed(sorted(species.genomes, key=lambda genome: genome.fitness)))
            while len(species.genomes) > 1:
                species.genomes.remove(species.genomes[len(species.genomes) - 1])

    def get_best(self):
        maxf = 0
        maxg = None
        for species in self.species_list:
            for genome in species.genomes:
                if genome.fitness > maxf:
                    maxf = genome.fitness
                    maxg = genome
        return maxg


text:
import os
import pickle
import numpy as np

from utils.Semantic.SemanticFeatureSelection import SemanticFeatureSelection
from utils.PatternFeatureReduction import PatternFeatureSelection

class SemanticPatternFeatureSelection:
    """
    class to implement the sematic and pattern feature selection pipeline

    it allows to find the semantic units using the SemanticFeatureSelection followed
    by fitting a pattern using the PatternFeatureSelection
    """
    def __init__(self, config, model):
        self.config = config

        self.semantic = SemanticFeatureSelection(config, model)
        self.pattern = PatternFeatureSelection(config)

    def fit(self, data, activation=None, feature_channel_last=True, fit_semantic=True, fit_pattern=True):
        # fit semantic feature map selection
        if fit_semantic:
            print("[SEM-PAT] Start Fitting Semantic Features")
            preds = self.semantic.fit(data, activation=None, feature_channel_last=False)  # activation needs to be set to None to get the raw output
            print("[SEM-PAT] Semantic fitted")
        else:
            print("[SEM-PAT] Start Transforming Semantic Features")
            print()
            print("[SEM-PAT] !!!!! LOADING SEMANTIC MODEL !!!!!!")
            print("[SEM-PAT] BE SURE THAT THIS IS THE BEHAVIOUR YOU WANT")
            print()
            self.semantic.load(os.path.join("models/saved", self.config['config_name'], "NormBase"))
            preds = self.semantic.transform(data, activation=None, feature_channel_last=False)
            print("[SEM-PAT] Semantic Transformed")
        print()

        # extend dimension to fit the number of template
        preds = np.repeat(np.expand_dims(preds, axis=0), len(self.config['rbf_template']), axis=0)

        # fit patterns
        if fit_pattern:
            print("[SEM-PAT] Start Fitting Pattern Features")
            preds = self.pattern.fit(preds, feature_channel_last=feature_channel_last)
            print("[SEM-PAT] Pattern fitted")
        else:
            print("[SEM-PAT] Start Transforming Pattern Features")
            preds = self.pattern.transform(preds, feature_channel_last=feature_channel_last)
            print("[SEM-PAT] Pattern transformed")

        return preds

    def transform(self, data, activation=None, feature_channel_last=True, use_scales=False):
        print("[SEM-PAT] Transform")
        preds = self.semantic.transform(data, activation=None, feature_channel_last=False)

        # extend dimension to fit the number of template
        preds = np.repeat(np.expand_dims(preds, axis=0), len(self.config['rbf_template']), axis=0)

        preds = self.pattern.transform(preds, feature_channel_last=feature_channel_last, use_scales=use_scales)
        print("[SEM-PAT] Semantic Pattern Transformed")

        return preds

    def update_patterns(self, template, sigmas, mask=None, zeros=None):
        self.pattern.update_patterns(template, sigmas, mask=mask, zeros=zeros)

    def save(self, path):
        self.semantic.save(path)
        self.pattern.save(path)

    def load(self, path):
        self.semantic.load(path)
        self.pattern.load(path)

max_stars_repo_name: michaelStettler/BVS

max_issues_repo_name: michaelStettler/BVS

max_forks_repo_name: michaelStettler/BVS


text:
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from preprocessing import Preprocessing
import torch.utils.data
import os
import torchvision.models as models
from torchsummary import summary

class CNN_NET(nn.Module):
    def __init__(self, in_channels, label_num, dropout_rate):
        super(CNN_NET, self).__init__()
        self.conv1 = nn.Conv2d(in_channels, 32, kernel_size=3)
        self.conv2 = nn.Conv2d(32, 64, kernel_size=3)
        self.conv2_drop = nn.Dropout2d(dropout_rate)
        self.conv3 = nn.Conv2d(64, 128, kernel_size=3)
        self.conv3_drop = nn.Dropout2d(dropout_rate)
        self.conv4 = nn.Conv2d(128, 128, kernel_size=3)
        self.conv4_drop = nn.Dropout2d(dropout_rate)
        self.linear1 = nn.Linear(18432,512)
        self.linear2 = nn.Linear(512, label_num)
        self.drop = dropout_rate

    def forward(self, x):
        x = F.relu(F.max_pool2d(self.conv1(x), 2))
        x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2))
        x = F.relu(F.max_pool2d(self.conv3(x), 2))
        x = F.relu(F.max_pool2d(self.conv4_drop(self.conv4(x)), 2))
        x = x.view(-1, 18432)
        x = F.relu(self.linear1(x))
        x = F.dropout(x, p=self.drop, training=False)
        x = self.linear2(x)
        return F.log_softmax(x, dim=1)

def train(model, device, train_loader, optimizer, epoch):
    model.train()
    for batch_idx, (x, y) in enumerate(train_loader):
        x, y = x.to(device), y.to(device)
        optimizer.zero_grad()
        x = x.float()
        y = y.long()
        output = model(x)
        loss = F.nll_loss(output, y)
        loss.backward()
        optimizer.step()
        if batch_idx % epoch == 0:
            print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
                batch_idx, batch_idx * len(x), len(train_loader.dataset),
                       100. * batch_idx / len(train_loader), loss.item()))

def test(model, device, test_loader):
    model.eval()
    test_loss = 0
    correct = 0
    with torch.no_grad():
        for x, y in test_loader:
            x, y = x.to(device), y.to(device)
            x = x.float()
            y = y.long()
            output = model(x)
            test_loss += F.cross_entropy(output, y, reduction='sum').item()
            pred = output.max(1, keepdim=True)[1]
            correct += pred.eq(y.view_as(pred)).sum().item()
    test_loss /= len(test_loader.dataset)
    print('\nDevelopment Set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format(
        test_loss, correct, len(test_loader.dataset),
        100. * correct / len(test_loader.dataset)))

def predict(model, device, inputs):
    model.eval()
    pred_list = []
    with torch.no_grad():
        for input in inputs:
            input = input[0]
            x = input.to(device)
            x = x.float()
            output = model(x)
            pred = output.max(1, keepdim=True)[1]
            pred = pred.cpu()
            preds = pred.numpy()
            for pred in preds:
                pred_list.append(list(pred)[0])
    return pred_list

if __name__ == '__main__':
    os.environ["CUDA_VISIBLE_DEVICES"] = "0"
    use_cuda = torch.cuda.is_available()
    print(use_cuda)

    model = CNN_NET(4,12,0).to('cuda')
    print(model)
    summary(model, (4,224,224))

    def pre_train(epochs=10):

        preprocessing = Preprocessing()
        data = preprocessing.read_image()
        data['image'] = torch.from_numpy(data['image'])
        data['label'] = torch.from_numpy(data['label'])
        train_set = torch.utils.data.TensorDataset(data['image'],data['label'])
        indices = np.random.randint(low=0,high=len(train_set)-1,size=len(train_set))
        test_sequence = torch.from_numpy(indices)
        test_set = torch.utils.data.Subset(train_set, test_sequence[0:400])
        train_set_split = torch.utils.data.Subset(train_set, test_sequence[400:])
        train_loader = torch.utils.data.DataLoader(dataset=train_set_split, batch_size=64, shuffle=True, num_workers=4)
        test_loader = torch.utils.data.DataLoader(dataset=test_set, batch_size=32)
        print('Total length: ', len(train_set))
        print('train_length:',len(train_set_split))
        print('test_length:',len(test_set))

        try:
            model.load_state_dict(torch.load('./model.pth'))
        except FileNotFoundError:
            print('Initialization')
        optimizer = optim.Adam(model.parameters())
        for epoch in range(epochs):
            train(model, 'cuda', train_loader, optimizer, epoch=10)
            test(model, 'cuda', test_loader)
            torch.save(model.state_dict(), './model.pth')

    def fine_tune(epochs=20, dropout=0.25, lr=1e-5):
        model = CNN_NET(4, 12, dropout).to('cuda')
        preprocessing = Preprocessing()
        data = preprocessing.read_image()
        data['image'] = torch.from_numpy(data['image'])
        data['label'] = torch.from_numpy(data['label'])
        train_set = torch.utils.data.TensorDataset(data['image'], data['label'])
        indices = np.random.randint(low=0, high=len(train_set) - 1, size=len(train_set))
        test_sequence = torch.from_numpy(indices)
        test_set = torch.utils.data.Subset(train_set, test_sequence[0:400])
        train_set_split = torch.utils.data.Subset(train_set, test_sequence[400:])
        train_loader = torch.utils.data.DataLoader(dataset=train_set, batch_size=64, shuffle=True, num_workers=4)
        test_loader = torch.utils.data.DataLoader(dataset=test_set, batch_size=32)
        print('Total length: ', len(train_set))
        print('train_length:', len(train_set_split))
        print('test_length:', len(test_set))

        try:
            model.load_state_dict(torch.load('./model.pth'))
        except FileNotFoundError:
            print('Initialization')
        optimizer = optim.Adam(model.parameters(), lr=lr)
        for epoch in range(epochs):
            train(model, 'cuda', train_loader, optimizer, epoch=10)
            test(model, 'cuda', test_loader)
            torch.save(model.state_dict(), './model.pth')

    def predict():

        INV_CLASS = {
            0: 'Black-grass',
            1: 'Charlock',
            2: 'Cleavers',
            3: 'Common Chickweed',
            4: 'Common wheat',
            5: 'Fat Hen',
            6: 'Loose Silky-bent',
            7: 'Maize',
            8: 'Scentless Mayweed',
            9: 'Shepherds Purse',
            10: 'Small-flowered Cranesbill',
            11: 'Sugar beet'
        }

        preprocessing = Preprocessing()
        model = CNN_NET(4, 12, 0).to('cuda')
        predict_data = preprocessing.test_data_read()
        predict_input = torch.from_numpy(predict_data['image'])
        predict_dataset = torch.utils.data.TensorDataset(predict_input)
        predict_set = torch.utils.data.DataLoader(predict_dataset, batch_size=32)
        model.load_state_dict(torch.load('./model.pth'))
        prediction = predict(model, 'cuda', predict_set)
        predict_data['label'] = prediction

        with open('submission.csv','w',encoding='utf-8') as f:
            f.write('file,species'+'\n')
            for i in range(len(predict_data['id'])):
                f.write(predict_data['id'][i] + ',' + INV_CLASS[prediction[i]] + '\n')

    #pre_train()
    #fine_tune(dropout=0.25, epochs=30)
    predict()


text:
import numpy as np

from holoviews.element import HeatMap, Image

from .test_plot import TestMPLPlot, mpl_renderer

class TestHeatMapPlot(TestMPLPlot):

    def test_heatmap_invert_axes(self):
        arr = np.array([[0, 1, 2], [3, 4, 5]])
        hm = HeatMap(Image(arr)).opts(plot=dict(invert_axes=True))
        plot = mpl_renderer.get_plot(hm)
        artist = plot.handles['artist']
        self.assertEqual(artist.get_array().data, arr.T[::-1].flatten())

    def test_heatmap_extents(self):
        hmap = HeatMap([('A', 50, 1), ('B', 2, 2), ('C', 50, 1)])
        plot = mpl_renderer.get_plot(hmap)
        self.assertEqual(plot.get_extents(hmap, {}), (-.5, -22, 2.5, 74))

    def test_heatmap_invert_xaxis(self):
        hmap = HeatMap([('A',1, 1), ('B', 2, 2)]).options(invert_xaxis=True)
        plot = mpl_renderer.get_plot(hmap)
        array = plot.handles['artist'].get_array()
        expected = np.array([1, np.inf, np.inf, 2])
        masked = np.ma.array(expected, mask=np.logical_not(np.isfinite(expected)))
        self.assertEqual(array, masked)

    def test_heatmap_invert_yaxis(self):
        hmap = HeatMap([('A',1, 1), ('B', 2, 2)]).options(invert_yaxis=True)
        plot = mpl_renderer.get_plot(hmap)
        array = plot.handles['artist'].get_array()
        expected = np.array([1, np.inf, np.inf, 2])
        masked = np.ma.array(expected, mask=np.logical_not(np.isfinite(expected)))
        self.assertEqual(array, masked)

max_stars_repo_name: TheoMathurin/holoviews

max_issues_repo_name: chrinide/holoviews

max_forks_repo_name: chrinide/holoviews


text:
import os
import sys
import time
import setuptools
import importlib
from setuptools.command.develop import develop
import subprocess
import platform
from loopy import __version__
import numpy
from Cython.Build import cythonize

head, tail = os.path.split(sys.argv[0])

class LoopyInstaller(develop):
    def tag_git(self):
        try:
            cmd = 'bash .git.sh'
            subprocess.run(
                cmd.split())
            cmd = 'git tag -a {0} -m {0}'.format(
                __version__)
            subprocess.run(
                cmd.split())
        except Exception as e:
            print(e)

    def install_dependencies(self):
        try:
            deplist_path = os.path.join(head, "dependencies.txt")
            deps = []
            with open(deplist_path, 'r') as f:
                for line in f:
                    deps.append(line.strip())

            _platform = platform.platform()

            if _platform.startswith("Windows"):
                _shell = True
            else:  # Linux or Mac
                _shell = False

            command = 'conda install -c anaconda -c conda-forge -c loop3d -y python=3.7'.split() + \
                deps
            print(command)
            subprocess.run(command, shell=_shell)
        except Exception as e:
            self.error(e)

    def cleanup_submodules(self):
        try:
            for submodule in self.submodules:
                subprocess.run(
                    'rm -rf {}/loopy/{}'.format(self.current_dir, submodule).split())
        except Exception as e:
            self.error(e)

    def setup_submodules(self):
        try:
            subprocess.run(
                'cp -r {0}/map2loop-2/map2loop/ {0}/loopy'.format(self.current_dir).split())
            subprocess.run(
                'cp -r {0}/LoopStructural/LoopStructural {0}/loopy'.format(self.current_dir).split())
        except Exception as e:
            self.error(e)

    def run(self):
        self.current_dir = os.getcwd()
        self.submodules = [
            'map2loop',
            'LoopStructural'
        ]
        try:
            self.tag_git()
            self.install_dependencies()
            self.setup_submodules()

        except Exception as e:
            self.error(e)

        develop.run(self)

def get_description():
    long_description = ""
    readme_file = os.path.join(head, "README.md")
    with open(readme_file, "r") as fh:
        long_description = fh.read()
    return long_description

setuptools.setup(
    name="loopy",
    version=__version__,
    author="The Loop Organisation",
    author_email="yohan.derose@monash.edu",
    description="Complete loop workflow in one package.",
    long_description=get_description(),
    long_description_content_type="text/markdown",
    url="https://github.com/Loop3D/map2loop-2",
    packages=setuptools.find_packages(),
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    cmdclass={
        'develop': LoopyInstaller,
    },
    python_requires='>=3.6',
    ext_modules=cythonize("loopy/LoopStructural/interpolators/cython/*.pyx",
                          compiler_directives={"language_level": "3"}),
    include_dirs=[numpy.get_include()],
    include_package_data=True,
    package_data={'LoopStructural': [
        'datasets/data/*.csv', 'datasets/data/*.txt']},
)


text:
"""
In  tabular MDP setting, evaluates the learning of policy evaluation for fixed policy using different guidance discount factors
"""
from __future__ import division, absolute_import, print_function
import numpy as np
import matplotlib.pyplot as plt
from tabular.data_utils import GenerateMDP, SampleTrajectories
from tabular.planing_utils import GetUniformPolicy, GetPolicyDynamics, PolicyEvaluation, PolicyIteration
from tabular.learning_utils import ModelEstimation, TD_policy_evaluation

###############  Model definition   ##################
S = 10  # number of states
A = 2  # number of actions
k = 5  #  Number of non-zero entries in each row  of transition-matrix
reward_std = 0.1
gammaEval = 0.99

n = 5  # number of trajectories to generate
depth = 10  # Length of trajectory

n_reps = 100  # number of experiment repetitions

method = 'TD-Learning'  #  'Model-Based' | 'TD-Learning'
do_correction = True  # False | True

n_TD_iter = 10000

##############  Policy Evaluation   ##################
# * Use 2 methods for estimation:
#   1. Model-based - estimate M_hat from D and do PolicyEvaluation
#   2. Model-free TD-learning  using D

gamma_grid = np.linspace(0.1, 0.99, num=100)
n_gammas = gamma_grid.shape[0]
evaluation_loss = np.zeros((n_gammas, n_reps))
train_loss = np.zeros((n_gammas, n_reps))
test_loss = np.zeros((n_gammas, n_reps))

for i_rep in range(n_reps):
    # Generate MDP:
    P, R = GenerateMDP(S, A, k)

    # Set a fixed exploration policy pi  -  uniform over actions - for gathering data and for evaluating
    pi = GetUniformPolicy(S, A)

    # The true policy dynamics model
    P_pi, R_pi = GetPolicyDynamics(P, R, pi)

    # The true value-function (w.r.t  true model and gammaEval):
    V_pi, Q_pi = PolicyEvaluation(P, R, pi, gammaEval, P_pi, R_pi)

    # Generate data:
    data = SampleTrajectories(P, R, pi, n, depth, p0=None, reward_std=reward_std)

    # Estimate model
    for i_gamma, gamma_guidance in enumerate(gamma_grid):

        if method == 'Model-Based':
            # Estimate model:
            P_est, R_est = ModelEstimation(data, S, A)

            # Estimated V_pi according to estimated model and gamma_guidance:
            V_est, Q_est = PolicyEvaluation(P_est, R_est, pi, gamma_guidance)

        elif method == 'TD-Learning':
            V_est = TD_policy_evaluation(data, S, A, gamma_guidance, n_TD_iter)

        if do_correction:
            # Correction factor:
            V_est = V_est * (1 / (1-gammaEval)) / (1 / (1-gamma_guidance))
            # V_est = V_est * gammaEval / gamma_guidance

       # Evaluate performance of estimated value:
        evaluation_loss[i_gamma, i_rep] = np. abs(V_pi - V_est).mean()
        # test_loss[i_gamma, i_rep] = -V_CE_test.mean()
        # train_loss[i_gamma, i_rep] = -V_CE_train.mean()

plt.figure()
ci_factor = 1.96/np.sqrt(n_reps)  # 95% confidence interval factor
plt.errorbar(gamma_grid, evaluation_loss.mean(axis=1), yerr=evaluation_loss.std(axis=1) * ci_factor,
             fmt='b.', label='{} trajectories'.format(n))
plt.title('Policy Evaluation, ' + method + ', correction=' + str(do_correction) +
          '\n' r'(Average absolute estimation error of $V^{\pi}(s)$)''\n (+- 95% confidence interval)')
plt.grid(True)
plt.xlabel('Guidance Discount Factor')
plt.ylabel('Evaluation Loss ')
plt.legend()

plt.show()
print('done')


text:
#!/usr/bin/env python3
import csv
import numpy as np
import matplotlib.pyplot as plt
import accuBar as accuBar
import groupBar2 as groupBar2
def getAllNeedFromCSV(a):
    print(a)
    with open(a, 'r') as f:
        reader = csv.reader(f)
        #reader = [each for each in csv.DictReader(f, delimiter=',')]
        result = list(reader)
        rows=len(result)
        print('rows=',rows)
        firstRow = result[0]
        #print(firstRow)
        index=0
        #define what may attract our interest
        idxCpu=0
        idxName=0
        idxLatency=0
        idxEnergy=0
        idxPeakPower=0
        for i in firstRow:
            #print(i)
            if(i=='cpu'):
               idxCpu=index
            if(i=='name'):
               idxName=index
            if(i=='latency'):
               idxLatency=index
            if(i=='energy'):
               idxEnergy=index
            if (i=='peakPower'):
                idxPeakPower=index
            index=index+1
        #read the valid stages
        vdataEntries=0
        latencyList=[]

        for k in range(1,rows):
            if(result[k][idxName]=='para_all'):
               print(result[k][idxLatency])
               return int(result[k][idxLatency]),float(result[k][idxEnergy]),float(result[k][idxPeakPower])
def writeCsv(fname,ar):
    with open(fname,"w") as csvfile:
        writer = csv.writer(csvfile)
        writer.writerow(["LL","LB","BL","BB"])
        writer.writerows(ar)
def main():
    fileNames=[
        'lz4_pipe2_LL_1_pe.csv',
        'lz4_pipe2_LB_1_pe.csv',
        'lz4_pipe2_BL_1_pe.csv',
        'lz4_pipe2_BB_1_pe.csv',

    ]

    legend=[
       '$^{LL}$',
       '$^{LB}$',
       '$^{BL}$',
       '$^{BB}$',

    ]
    xv=['']
    ev=[]
    pv=[]
    env=[]
    yv2=[]
    lt=[]
    for i in range(len(fileNames)):
        l,e,peak=getAllNeedFromCSV(fileNames[i])
        #print(yt)
        x=e-l*5*0.62*1e-6
        lt.append(l)
        ev.append(e)
        pv.append(peak)
        env.append(x)
    accuBar.DrawFigure(legend,([pv]),['all'],'','ppwer/mW','lz4_pipe2_2stage_peakPower',True,'')
    groupBar2.DrawFigure(legend,([ev,env]),['all','net'],'algos','energy/J',0,0,'lz4_pipe2_2stage_allEnergy',1)
    writeCsv("NET_ENG.csv",[env])
    print(lt,ev,env)
    return env
if __name__ == "__main__":
    main()

max_stars_repo_name: intellistream/PC-AMP

max_issues_repo_name: intellistream/PC-AMP

max_forks_repo_name: intellistream/PC-AMP


text:
from keras.datasets import mnist
import numpy as np
import os
from pathlib import Path
from base.base_preprocessor import BasePreprocessor
import librosa
import pretty_midi

class Wav2MidPreprocessor(BasePreprocessor):
    def __init__(self, config):
        super().__init__(config)

        # TODO: for now these are hard coded but move them to config json later
        self.config.preprocessor.sr = 22050
        self.config.preprocessor.hop_length = 512
        self.config.preprocessor.window_size = 7
        self.config.preprocessor.min_midi = 21
        self.config.preprocessor.max_midi = 108
        self.config.preprocessor.bin_multiple = 3
        self.config.preprocessor.window_size = 7

    # read a directory (recursively?) with mp3s and matchings midi files.
    def preprocess(self):
        data_dir = self.config.preprocessor.data_dir.format(self.config)
        raw_data = Path(data_dir) / 'raw'

        for file in os.listdir(raw_data):
            fn, ext = os.path.splitext(file)
            # if both mp3/wav and matching named midi file exists:
            # print(file)
            # print(fn)
            # print(ext)
            # print(Path(raw_data) / f"{fn}.mid")
            if ext in ('.mp3', '.wav') and os.path.exists(Path(raw_data) / f"{fn}.mid"):
                # process and join them
                print(('processing',file,f"{fn}.mid)"))
                wavnp = self.wav2np(Path(raw_data) / file, **self.config.preprocessor)
                times = librosa.frames_to_time(
                    np.arange(wavnp.shape[0]),
                    sr=self.config.preprocessor.sr,
                    hop_length=self.config.preprocessor.hop_length)
                midnp = self.mid2np(os.path.join(raw_data,f"{fn}.mid"), times, **self.config.preprocessor)

                np.save(Path(self.config.preprocessor.data_dir) / "preprocessed" / f"{fn}_input", wavnp)
                np.save(Path(self.config.preprocessor.data_dir) / "preprocessed" / f"{fn}_output", midnp)

    def wav2np(self, audio_fn, sr, bin_multiple, max_midi, min_midi, window_size, hop_length, **kwargs):
        bins_per_octave = 12 * bin_multiple #should be a multiple of 12
        n_bins = (max_midi - min_midi + 1) * bin_multiple

        # down-sample, mono-channel
        y, _ = librosa.load(audio_fn,sr)
        S = librosa.cqt(y,fmin=librosa.midi_to_hz(min_midi), sr=sr, hop_length=hop_length,
                          bins_per_octave=bins_per_octave, n_bins=n_bins)
        S = S.T

        # scale logarithmically
        S = librosa.amplitude_to_db(np.abs(S))

        minDB = np.min(S)
        print(np.min(S),np.max(S),np.mean(S))

        S = np.pad(S, ((window_size//2,window_size//2),(0,0)), 'constant', constant_values=minDB)

        windows = []
        for i in range(S.shape[0]-window_size+1):
            w = S[i:i+window_size,:]
            windows.append(w)
        x = np.array(windows)

        return x

    def mid2np(self, fn, times, sr, min_midi, max_midi, **kwargs):
        pm = pretty_midi.PrettyMIDI(fn)
        piano_roll = pm.get_piano_roll(fs=sr,times=times)[min_midi:max_midi+1].T
        piano_roll[piano_roll > 0] = 1
        return piano_roll