text:
from ae_measure2 import *
import pandas
from minisom import MiniSom
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from sklearn.metrics import davies_bouldin_score
from sklearn import metrics
from sklearn.decomposition import PCA

def find_feature_vectors(ae_file, filter_csv, dt, channel_number, sig_length = 1024):
    '''This will find the feature vectors from channel, specify the channel number starting with index 0
    Inputs: ae_file(.txt): Voltage-data series
            filter_csv(.csv): filter_csv of important events
            dt(float) = sampling rate
            channel_number(int) = channel number wanting to be analyzed, starting with index 0
            sig_length(int) = signal length (default = 1024)
    Outputs: list_of_feature_vectors(array-like): a list of lists of feature vector values
    '''

    total_signal_length = sig_length/10 #note: converts signal length to microseconds
    start_string = 'Start_channel' +str(channel_number+1) #note: the .csv file starts with channel 1, not 0. Must add 1
    start_energy = 'Energy_ch'+ str(channel_number+1)
    list_of_feature_vectors = []

    waveform_channel = channel_number

    v1, ev = filter_ae(ae_file, filter_csv, waveform_channel, sig_length)
    df = pandas.read_csv(filter_csv)

    for i in range(len(ev)):

        #start_time = df[start_string][i] this is going to be changed

        energy = df[start_energy][i]
        ln_energy = np.log(energy)

        waveform_channel_array = np.array(v1[i])
        max_amplitude = np.max(waveform_channel_array)
        index_location_max = np.argmax(waveform_channel_array)
        max_location_time = index_location_max / 10

        threshold = .1 * max_amplitude  #Note: the threshold is taken as 10 percent of the maximum amplitude
        imin, imax = np.nonzero(v1[i] > threshold)[0][[0, -1]]
        start_time = imin / 10 #Note: converts index location to a start time (microseconds)
        end_time = imax / 10 #Note: converts index location to an end time (microseconds)

        rise_time = max_location_time - start_time
        duration = end_time - start_time #point of question: would the duration now be start to end time
        decay_time = duration - rise_time

        risingpart = v1[i][imin:index_location_max] #Note: this is the rising portion of the waveform
        wr, zr = fft(dt, risingpart, low_pass=None, high_pass=None)
        risingfrequency = get_freq_centroid(wr, zr, low_pass=None, high_pass=None)

        decaypart = v1[i][index_location_max:imax] #Note: this is the falling portion of the waveform
        wd, zd = fft(dt, decaypart, low_pass=None, high_pass=None)
        decayfrequency = get_freq_centroid(wd, zd, low_pass=None, high_pass=None)

        wa, za = fft(dt, v1[i], low_pass=None, high_pass=None)
        avgfrequency = get_freq_centroid(wa, za, low_pass=None, high_pass=None)

        Rln = np.log(rise_time)
        RDln = np.log(rise_time / duration)
        ARln = np.log(max_amplitude / rise_time)
        ADTln = np.log(max_amplitude / decay_time)
        AFln = np.log(max_amplitude / avgfrequency)

        fvector = [Rln, avgfrequency, risingfrequency, ln_energy, RDln, ARln, ADTln, AFln]

        list_of_feature_vectors.append(fvector)

    return list_of_feature_vectors

def PCA_analysis(list_of_feature_vectors):
    '''Conducts PCA on a list of feature vectors
    Inputs: list_of_feature_vectors (array-like): a list of lists of feature vector values
    Outputs: principalComponents (array-like): coordinates for a list of principal components
             eigvalues (array-like): array of eigen values that also corresponds to the variance accounted for by each principal component
             y (float): variance explained in total by the principal components
    '''
    pca = PCA(.95) #Note: determines the number of principal components to explain no less than 0.95 variance
    principalComponents = pca.fit_transform(list_of_feature_vectors)

    eigvalues = pca.explained_variance_ratio_ #variance accounted by each principal component
    y = pca.explained_variance_ratio_.sum()

    return principalComponents, eigvalues, y

def rescaling_pc(principal_components, eigenvalues):
    ''' rescaling_pc rescales a list of principal components by respective eigenvalues
    Inputs: principal_components (array-like): An array of principal components determined by the PCA_analysis function
            eigenvalues (array-like): An array of eigenvalues that corresponds to the principal components
    Outputs: final (array-like): coordinates of rescaled principal components
    '''
    rspc = [] #holder variable that stands for rescaled principal components
    pctranspose = principal_components.T

    for i in range(len(pctranspose)):
        rescaledi = (pctranspose[i]) * np.sqrt(eigenvalues[i])
        rspc.append(rescaledi)

    final = np.array(rspc).T
    return final

def getdistancemap(ae_file,filter_csv,channel_number,n_neurons,m_neurons):
    '''This will find the 2D distance map using the MiniSom package
    Inputs: ae_file(.txt): Voltage-data series
            filter_csv(.csv): filter_csv of important events
            channel_number (float): channel number ranging from 0 to 3
            n_neurons (float): number of rows in SOM
            m_neurons (float): number of columns in SOM
    Outputs: 2D distance map made using SOM
    '''
    featurevect1 = find_feature_vectors(ae_file, filter_csv, 10 ** -7, channel_number)
    npfeaturevect1 = np.array(featurevect1)

    # Initialization and training
    som = MiniSom(n_neurons, m_neurons, npfeaturevect1.shape[1], sigma=1.5, learning_rate=.5,
                  neighborhood_function='gaussian', random_seed=0)

    som.pca_weights_init(npfeaturevect1)
    som.train(npfeaturevect1, 1000, verbose=True)  # random training

    plt.figure(figsize=(9, 9))

    plt.pcolor(som.distance_map().T, cmap='bone_r')  # plotting the distance map as background
    plt.colorbar()
    plt.title(['Distance Map',str(filter_csv),'Channel'+str(channel_number)])
    plt.show()

def get3ddistancemap(ae_file,filter_csv,channel_number,n_neurons,m_neurons):
    '''This will find the 3D distance map using the MiniSom package
    Inputs: ae_file(.txt): Voltage-data series
            filter_csv(.csv): filter_csv of important events
            channel_number (float): channel number ranging from 0 to 3
            n_neurons (float): number of rows in SOM
            m_neurons (float): number of columns in SOM
    Outputs: 3D distance map made using SOM
    '''
    featurevect1 = find_feature_vectors(ae_file, filter_csv, 10 ** -7, channel_number)
    npfeaturevect1 = np.array(featurevect1)

    # Initialization and training
    som = MiniSom(n_neurons, m_neurons, npfeaturevect1.shape[1], sigma=1.5, learning_rate=.5,
                  neighborhood_function='gaussian', random_seed=0)

    som.pca_weights_init(npfeaturevect1)
    som.train(npfeaturevect1, 1000, verbose=True)  # random training

    plt.figure(figsize=(9, 9))

    plt.pcolor(som.distance_map().T, cmap='bone_r')  # plotting the distance map as background
    plt.colorbar()
    plt.title(['Distance Map', str(filter_csv), 'Channel' + str(channel_number)])

    points = [] #dummy variable that will hold XYZ coordinates
    for i in range(n_neurons):
        for j in range(m_neurons):
            points.append([i, j , som.distance_map().T[i][j]])

    nppoints= np.array(points)
    nppointsT = nppoints.T

    # Plot the surface.
    fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
    surf = ax.plot_trisurf(nppointsT[0], nppointsT[1], nppointsT[2], cmap='bone_r',linewidth=0, antialiased=False)
    fig.colorbar(surf, shrink=0.5, aspect=5)
    plt.title(['Distance Map', str(filter_csv), 'Channel' + str(channel_number)])
    plt.show()

def getweightvectors(ae_file,filter_csv,channel_number,n_neurons,m_neurons):
    '''This will find the weight vectors using the MiniSom package
    Inputs: ae_file(.txt): Voltage-data series
            filter_csv(.csv): filter_csv of important events
            channel_number (float): channel number ranging from 0 to 3
            n_neurons (float): number of rows in SOM
            m_neurons (float): number of columns in SOM
    Outputs: reshapez (list of lists): list of weight vectors
             Will also display a 2D SOM
    '''
    featurevect1 = find_feature_vectors(ae_file, filter_csv, 10 ** -7, channel_number)
    npfeaturevect1 = np.array(featurevect1)

    # Initialization and training
    som = MiniSom(n_neurons, m_neurons, npfeaturevect1.shape[1], sigma=1.5, learning_rate=.5,
                  neighborhood_function='gaussian', random_seed=0)

    som.pca_weights_init(npfeaturevect1)
    som.train(npfeaturevect1, 1000, verbose=True)  # random training

    plt.figure(figsize=(9, 9))

    plt.pcolor(som.distance_map().T, cmap='bone_r')  # plotting the distance map as background
    z = som.get_weights()
    reshapez = np.reshape(z,(-1,len(z[0][0])))
    plt.colorbar()
    plt.title(['Distance Map', str(filter_csv), 'Channel' + str(channel_number)])
    return reshapez

def DBIplot(pclist):
    '''Takes a principal component list and generates a Davies-Bouldin Plot
    Inputs: pclist (array-like): Takes a list of principal components coordinates and generates a Davies-Bouldin Plot
    Outputs: A plot is generated that is the Davies-Bouldin index vs. the number of clusters
    '''

    clustervector = list(range(2,11))
    dbscores = []
    for i in clustervector:
        kmeans = KMeans(n_clusters=i, n_init=100, max_iter=300).fit(pclist)
        labels = kmeans.labels_

        davies_bouldin_score1 = davies_bouldin_score(pclist, labels)
        dbscores.append(davies_bouldin_score1)
    plt.figure()
    plt.plot(clustervector, dbscores)
    plt.show()

def convergenceplot(pclist):
    '''Generates a convergence plot based on the number of initializations
    Inputs: pclist (array-like): Takes a list of principal components coordinates
    Outputs: Convergence plot based on the number of initializations
    '''
    inertia = []
    for i in range(1,100):
        kmeans = KMeans(n_clusters=4, n_init=i).fit(pclist)
        inertia.append(kmeans.inertia_)

    plt.plot(np.log(inertia))
    plt.show()

def iterationplot(pclist):
    '''Generates a convergence plot based on the number of initializations
    Inputs: pclist (array-like): Takes a list of principal components coordinates
    Outputs: Convergence plot based on the number of initializations
    '''
    inertia = []
    for i in range(1, 300):
        kmeans = KMeans(n_clusters=4, n_init=100, max_iter=i).fit(pclist)
        inertia.append(kmeans.inertia_)

    plt.plot(np.log(inertia))
    plt.show()

def labeldistancemap(ae_file,filter_csv,channel_number,n_neurons,m_neurons, labellist):
    '''This will get the labeled 2D distance map by cluster
        Inputs: ae_file(.txt): Voltage-data series
                filter_csv(.csv): filter_csv of important events
                channel_number (float): channel number ranging from 0 to 3
                n_neurons (float): number of rows in SOM
                m_neurons (float): number of columns in SOM
                labellist (array-like): list of labels found using kmeans on weight vectors
        Outputs: 3D distance map made using SOM
    '''
    #reshape label list to fit the original grid
    reshapelabels= np.reshape(labellist,(n_neurons,m_neurons))
    featurevect1 = find_feature_vectors(ae_file, filter_csv, 10 ** -7, channel_number)
    npfeaturevect1 = np.array(featurevect1)

    # Initialization and training
    som = MiniSom(n_neurons, m_neurons, npfeaturevect1.shape[1], sigma=1.5, learning_rate=.5,
                  neighborhood_function='gaussian', random_seed=0)

    som.pca_weights_init(npfeaturevect1)
    som.train(npfeaturevect1, 1000, verbose=True)  # random training

    plt.figure(figsize=(9, 9))

    plt.pcolor(som.distance_map().T, cmap='bone_r')  # plotting the distance map as background
    plt.colorbar()
    plt.title(['Distance Map',str(filter_csv),'Channel'+str(channel_number)])

    #Plot the clusters on the map
    currentx = .4
    currenty = .5
    for i in range(len(reshapelabels)):
        for j in range(len(reshapelabels[i])):
            plt.text(currentx,currenty, str(reshapelabels[i][j]), fontsize=14, color = 'red')
            currentx += 1
        currentx = .4
        currenty += 1

    plt.show()

def DBI2plot(pclist,weight_vects,experiment,channel):
    '''Takes a principal component list and generates a Davies-Bouldin Plot for the Moevus and de Oliveira frameworks
    Inputs: pclist (array-like): Takes a list of rescaled principal components coordinates and generates a Davies-Bouldin Plot
            weight_vects (array-like): Takes a list of weight vectors from the getweightvectors functuon and generates DBI Plot
    Outputs: A plot is generated that is the Davies-Bouldin index vs. the number of clusters for both frameworks
    '''

    clustervector = list(range(2,11)) #Note: list of clusters [2 3 4 ... 10]
    dbscores = []
    dbscores2 = []
    for i in clustervector:
        kmeans = KMeans(n_clusters=i, n_init=100, max_iter=300).fit(pclist)
        labels = kmeans.labels_
        davies_bouldin_score1 = davies_bouldin_score(pclist, labels)
        dbscores.append(davies_bouldin_score1)

    for i in clustervector:
        kmeans2 = KMeans(n_clusters=i, n_init=100, max_iter=300).fit(weight_vects)
        labels2 = kmeans2.labels_
        davies_bouldin_score2 = davies_bouldin_score(weight_vects, labels2)
        dbscores2.append(davies_bouldin_score2)

    plt.figure()
    plt.ylabel('Davies Bouldin Index')
    plt.xlabel('Number of Clusters')
    plt.title(('DBI Plot '+ 'Experiment ' +str(experiment)+ ' Channel '+ str(channel)))
    plt.plot(clustervector, dbscores, label = 'Moevus')
    plt.plot(clustervector, dbscores2, label = 'deOliveira')
    plt.legend()
    plt.show()

def determineSOMARI(ae_file, filter_csv, channelx, channely, clusters, n_neurons, m_neurons):
    ''' determineSOMARI takes in inputs and returns an Adjusted Rand Index Value between two channels using a self-organizing map
    Inputs: ae_file(.txt): Voltage-data series
            filter_csv(.csv): filter_csv of important events
            channelx (int): first channel wanted to compare (ranging from 0 to 3)
            channely (int): second channel wanted to compare (ranging from 0 to 3)
            clusters (int): number of clusters
            n_neurons (int): number of rows in SOM
            m_neurons (int): number of columns in SOM
    Outputs: ARI value (float): Adjusted Rand Index Value
    '''
    weightvect1 = getweightvectors(ae_file,filter_csv,channelx,n_neurons,m_neurons)
    kmeans1 = KMeans(n_clusters=clusters, n_init=100, max_iter=300).fit(weightvect1)
    labels1 = kmeans1.labels_

    weightvect2 = getweightvectors(ae_file, filter_csv, channely, n_neurons, m_neurons)
    kmeans2 = KMeans(n_clusters=clusters, n_init=100, max_iter=300).fit(weightvect2)
    labels2 = kmeans2.labels_

    ARI = metrics.adjusted_rand_score(labels1, labels2)
    return ARI

def assigningclusters(ae_file, filter_csv, channel_number, n_neurons, m_neurons, labellist):
    ''' This function is used to determine which event goes into which cluster. This is helpful for making cumulative AE plots
    Inputs: ae_file(.txt): Voltage-data series
            filter_csv(.csv): filter_csv of important events
            channel_number (float): channel number ranging from 0 to 3
            n_neurons (float): number of rows in SOM
            m_neurons (float): number of columns in SOM
            labellist: label list from kmeans of weight vectors. Assigns a label to a node.
    Outputs: Clusterlist (list): list of cluster labels that correspond to each AE event. Useful in Cumulative AE plots.
    '''
    featurevect1 = find_feature_vectors(ae_file, filter_csv, 10 ** -7, channel_number)
    npfeaturevect1 = np.array(featurevect1)
    reshapelabels = np.reshape(labellist, (n_neurons, m_neurons))

    # Initialization and training
    som = MiniSom(n_neurons, m_neurons, npfeaturevect1.shape[1], sigma=1.5, learning_rate=.5,
                  neighborhood_function='gaussian', random_seed=0)

    som.pca_weights_init(npfeaturevect1)
    som.train(npfeaturevect1, 1000, verbose=True)  # random training

    plt.figure(figsize=(9, 9))

    plt.pcolor(som.distance_map().T, cmap='bone_r')  # plotting the distance map as background

    clusterlist = []
    # need to get cluster list
    for i in range(len(npfeaturevect1)):
        x = som.winner(npfeaturevect1[i])
        getlabel = reshapelabels[x[0]][x[1]]
        clusterlist.append(getlabel)

    plt.colorbar()
    plt.title(['Distance Map', str(filter_csv), 'Channel' + str(channel_number)])

    plt.show()
    return clusterlist

def separatedAEplot(filter_csv, labels,channel_number):
    ''' Generates an AE vs. Stress plots and separates the events by cluster
    Inputs: filter_csv (.csv file): A filter csv file of important events
            labels (array-like): cluster labels from the kmeans funtion
            channel_number (int): channel number starting with index of 0
    Outputs: An AE vs Stress plot where the clusters are separated by color
    Note: Depending on the labels you put, this function will only work for 10 clusters. More can be added by adding another
          stressesX variable, an elif statement in the for loop, another aeX block, and axX.scatter statement.
    '''
    stresses0 = []
    stresses1 = []
    stresses2 = []
    stresses3 = []
    stresses4 = []
    stresses5 = []
    stresses6 = []
    stresses7 = []
    stresses8 = []
    stresses9 = []
    df = pandas.read_csv(filter_csv)
    for i in range(len(df['Adjusted_Stress_MPa'])):
        stress = df['Adjusted_Stress_MPa'][i]
        if labels[i] == 0:
            stresses0.append(stress)

        elif labels[i] == 1:
            stresses1.append(stress)

        elif labels[i] == 2:
            stresses2.append(stress)
        elif labels[i] == 3:
            stresses3.append(stress)
        elif labels[i] == 4:
            stresses4.append(stress)
        elif labels[i] == 5:
            stresses5.append(stress)
        elif labels[i] == 6:
            stresses6.append(stress)
        elif labels[i] == 7:
            stresses7.append(stress)
        elif labels[i] == 8:
            stresses8.append(stress)
        elif labels[i] == 9:
            stresses9.append(stress)

    ae0 = range(len(stresses0))
    ae0 = np.array(ae0)
    aecumulative0 = ae0/len(df['Adjusted_Stress_MPa'])

    ae1 = range(len(stresses1))
    ae1 = np.array(ae1)
    aecumulative1 = ae1/len(df['Adjusted_Stress_MPa'])

    ae2 = range(len(stresses2))
    ae2 = np.array(ae2)
    aecumulative2 = ae2/len(df['Adjusted_Stress_MPa'])

    ae3 = range(len(stresses3))
    ae3 = np.array(ae3)
    aecumulative3 = ae3 / len(df['Adjusted_Stress_MPa'])

    ae4 = range(len(stresses4))
    ae4 = np.array(ae4)
    aecumulative4 = ae4 / len(df['Adjusted_Stress_MPa'])

    ae5 = range(len(stresses5))
    ae5 = np.array(ae5)
    aecumulative5 = ae5 / len(df['Adjusted_Stress_MPa'])

    ae6 = range(len(stresses6))
    ae6 = np.array(ae6)
    aecumulative6 = ae6 / len(df['Adjusted_Stress_MPa'])

    ae7 = range(len(stresses7))
    ae7 = np.array(ae7)
    aecumulative7 = ae7 / len(df['Adjusted_Stress_MPa'])

    ae8 = range(len(stresses8))
    ae8 = np.array(ae8)
    aecumulative8 = ae8 / len(df['Adjusted_Stress_MPa'])

    ae9 = range(len(stresses9))
    ae9 = np.array(ae9)
    aecumulative9 = ae9 / len(df['Adjusted_Stress_MPa'])

    fig = plt.figure()
    ax1 = fig.add_subplot(111)

    ax1.scatter(stresses0,aecumulative0, c='tab:blue')
    ax1.scatter(stresses1,aecumulative1, c ='tab:orange')
    ax1.scatter(stresses2,aecumulative2, c = 'tab:green')
    ax1.scatter(stresses3, aecumulative3, c = 'tab:red')
    ax1.scatter(stresses4, aecumulative4, c='tab:purple')
    ax1.scatter(stresses5, aecumulative5, c='tab:brown')
    ax1.scatter(stresses6, aecumulative6, c='tab:pink')
    ax1.scatter(stresses7, aecumulative7, c='tab:gray')
    ax1.scatter(stresses8, aecumulative8, c='tab:olive')
    ax1.scatter(stresses9, aecumulative9, c='tab:cyan')
    plt.xlabel('Stress (MPa)')
    plt.ylabel('Normalized AE Cumulative Score')
    plt.title('Normalized AE Plot '+str(filter_csv)+' Channel '+str(channel_number))
    plt.show()

def DBIandARIplot(weight_vects,experiment,channel):
    '''Takes a weight_vector list and generates a Davies-Bouldin Plot and Average ARI plot
    Inputs: weight_vects (array-like): Takes a list of weight vectors and generates a Davies-Bouldin Plot
            experiment (integer): experiment number from 'Average_deOliveira_ARI.csv' file
            channel(int): channel number ranging from 0 to 3
    Outputs: A plot is generated that is the Davies-Bouldin index vs. the number of clusters and ARI vs. Number of Clusters
    '''
    df = pandas.read_csv('Average_deOliveira_ARI.csv')
    avgari = df[str(experiment)]
    npavgari = np.array(avgari)
    clustervector = list(range(2,11))
    dbscores2 = []

    for i in clustervector:
        kmeans2 = KMeans(n_clusters=i, n_init=100, max_iter=300).fit(weight_vects)
        labels2 = kmeans2.labels_

        davies_bouldin_score2 = davies_bouldin_score(weight_vects, labels2)
        dbscores2.append(davies_bouldin_score2)

    plt.figure()
    plt.plot(clustervector, dbscores2, label = 'deOliveira DBI')
    plt.ylabel('Davies Bouldin Index')
    plt.xlabel('Number of Clusters')
    plt.title(('DBI and ARI plot '+ 'Experiment ' +str(experiment)+ ' Channel '+ str(channel)))

    ax2 = plt.twinx()
    ax2.plot(clustervector, npavgari, label = 'Average ARI', c = 'r')
    ax2.set_ylabel('Adjusted Rand Index')
    fig = plt.gcf()
    fig.legend(loc = 'lower left')
    plt.show()


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

# @Time : 2019/6/10 下午2:26
# @Author : Ethan
# @Site :
# @File : uti.py
# @Software: PyCharm

import numpy as np
import os
import sys
from PIL import Image
import  PIL.ImageOps
import matplotlib.image as  mping
import matplotlib.pyplot as plt
from models.my_crnn import CRNN
import torch
import cv2

from decimal import Decimal
from decimal import getcontext

# image_path ='/media/ethan/0207380E139801C9/t10k-images'
# output_result_path = "/media/ethan/0207380E139801C9/work_dir/ocr/crnn_chinese_characters_rec/to_lmdb/traifapiao.txt"
#
# with open(output_result_path,'w') as file:
# 	images_list = os.listdir(image_path)
# 	for i in images_list:
# 		label = i.split('_')[0]
# 		file.write(i+' '+label)
# 		file.write("\n")
# image_list = os.listdir(image_path)[0]
# image= Image.open('test_images/number3.png')
# print(image.size[0],image.size[1])
# plt.imshow(image)
# plt.show()
# txt_file = open('to_lmdb/111.txt','a+')
#
# with open(output_result_path,'r') as file:
# 	max_length = 10
# 	for line in file.readlines():
# 		image = line.split(' ')[0]
# 		label = line.split(' ')[1].replace('\n','')
# 		if len(label)<max_length:
# 			label = str(label)
# 			label+= (max_length-len(label))*'0'
# 			txt_file.write(image+' '+label)
# 			txt_file.write('\n')
# 		else:
# 			label = str(label)
# 			label = label[:max_length]
# 			txt_file.write(image+' '+label)
# 	txt_file.close()

# image = Image.open(path)
# X =np.zeros((46,300,3))
# X[:]=255
# w,h = image.size
# X[:,:w]= image
# X = X/255.0
# plt.imshow(X)
# plt.show()
# path = 'to_lmdb/t10k-images'
# ### 随机拼接手写数字
# name_list = os.listdir(path)
# number_file = open('to_lmdb/number_write.txt','w')
# #
# #
# t = 0
# while t<15000:
# 	ims = []
# 	number_list =[]
# 	select_list = np.random.choice(name_list, size=10)
# 	for line in select_list:
# 		label = line.split('_')[0]
# 		number_list.append(label)
# 	for item in select_list:
# 		img = Image.open(os.path.join(path, item))
# 		ims.append(img)
# 	w, h = ims[0].size
# 	result = Image.new(ims[0].mode, (w * 10, h))
# 	for i, im in enumerate(ims):
# 		result.paste(im, box=(i * w, 0))
# 	target_path = os.path.join('to_lmdb/number', '%d.jpg' % t)
# 	number_file.write('%d.jpg'%t+' '+ ''.join(number_list))
# 	number_file.write('\n')
# 	result = PIL.ImageOps.invert(result)
# 	result.save(target_path)
# 	t += 1

# p拼接

# #padding图片到相同的宽度
# def matrixtoimage(data):
# 	data =data*255
# 	new_image = Image.fromarray(data.astype(np.uint8)).convert('L')
# 	return  new_image
#
# max_w = 0
# for item in os.listdir(path):
# 	image=Image.open(os.path.join(path,item))
# 	w,h = image.size
# 	#找到图片中的最大宽度
# 	if w>max_w:
# 		max_w=w
# i=0
# for item in os.listdir(path):
# 	image = Image.open(os.path.join(path,item))
# 	w,h = image.size
# 	X = np.zeros((h,max_w,3))
# 	X[:,:w] = image
# 	X/=255.0
# 	new_img = matrixtoimage(X)
# 	target_image_path = os.path.join('to_lmdb/train_images', '%d.jpg' %i )
# 	if i==54:
# 		i+=2
# 	else:
# 		i+=1
# 	new_img.save(target_image_path)

#
# ## padding图片
# path = 'to_lmdb/fapiao.txt'
# file =  open('to_lmdb/fapiao1.txt','w')
#
# max_length = 0
# with open(path,'r') as f:
# 	for line in f.readlines():
# 		image = line.split(' ')[0]
# 		label = line.split(' ')[1].replace('\n','')
# 		if len(label)>max_length:
# 			max_length = len(label)
#
# with open(path,'r') as f:
# 	for line in f.readlines():
# 		image = line.split(' ')[0]
# 		label = line.split(' ')[1].replace('\n','')
# 		if len(label)<max_length:
# 			label = str(label)
# 			label+= (max_length-len(label))*'0'
# 			file.write(image+' '+label)
# 			file.write('\n')
# 		else:
# 			label = str(label)
# 			label = label[:max_length]
# 			file.write(image+' '+label)
# 			file.write('\n')
# 	file.close()

#

#
# def numbertostr(str_num):
# 	num_to_ch_dic = {0: '零', '.': '点', 1: '壹', 2: '贰', 3: '叁', 4: '肆', 5: '伍', 6: '陆', 7: '柒', 8: '捌', 9: '玖', 10: 'z'}
# 	li_dw = ['萬', '亿', '萬']
# 	character_num = ['壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']
# 	li_dot = ['分', '角']
# 	li_mod = list("拾佰仟")
# 	# str_num = "1000000001000001"
# 	final_str = ''
#
# 	detail_dw = list("分角圆拾佰仟")
# 	for i in li_dw:
# 		detail_dw.append(i)
# 		for j in li_mod:
# 			detail_dw.append(j)
# 	detail_dw.reverse()
# 	# print(detail_dw)
# 	max_len = len(detail_dw)
#
# 	li_num = list(str(int(Decimal(str_num) * 100)).rjust(max_len, '0'))
# 	# print(li_num)
# 	li_dw.append('圆')
# 	index_tmp = 0
# 	while index_tmp < max_len:
# 		if detail_dw[index_tmp] in li_dw:
# 			if li_num[index_tmp] == '0':
# 				li_num[index_tmp] = '10'
# 		li_num[index_tmp] = num_to_ch_dic[int(li_num[index_tmp])]
# 		index_tmp = index_tmp + 1
# 	# print(li_num)
# 	li_num_str = ''.join(li_num)
# 	# print(li_num_str)
# 	li_num_str = li_num_str.replace('零零零z', '---z')
# 	li_num_str = li_num_str.replace('零零z', '--z')
# 	li_num_str = li_num_str.replace('z零零', 'z--')
# 	li_num_str = li_num_str.replace('z零', 'z-')
# 	li_num_str = li_num_str.replace('零z', '-z')
# 	# print(li_num_str)
# 	li_num = list(li_num_str)
# 	index_tmp = 0
#
# 	start_sign = 0
# 	while index_tmp < max_len:
# 		if start_sign == 0:
# 			if li_num[index_tmp] in character_num:
# 				start_sign = 1
# 				final_str = li_num[index_tmp] + detail_dw[index_tmp]
# 		elif start_sign == 1:
# 			if li_num[index_tmp] == 'z':
# 				final_str = final_str + detail_dw[index_tmp] + '零'
# 			elif li_num[index_tmp] == '-':
# 				final_str = final_str + ''
# 			elif li_num[index_tmp] == '零':
# 				final_str = final_str + '零'
# 			else:
# 				final_str = final_str + li_num[index_tmp] + detail_dw[index_tmp]
# 		index_tmp = index_tmp + 1
# 	if start_sign == 0:
# 		final_str = '零圆零分整'
# 	else:
# 		# print(final_str)
# 		final_str = final_str.replace('零零', '零')
# 		final_str = final_str.replace('亿萬', '亿零')
# 		final_str = final_str + '整'
# 		final_str = final_str.replace('圆零整', '圆整')
# 		final_str = final_str.replace('角零整', '角整')
# 		final_str = final_str.replace('零零', '零')
#
# 	return  final_str

# path ='to_lmdb/number/9.jpg'
# img = Image.open(path).convert('L')
# invert_img = PIL.ImageOps.invert(img)
#
# # plt.imshow(invert_img,cmap=plt.get_cmap('gray'))
# # plt.show()
# invert_img.save('ttt.jpg')

##整合发票数据
# bignumber='零壹贰叁肆伍陆柒捌玖拾佰仟万亿元角分整圆'
#
# path ='to_lmdb/alltext'
# all_txt = open('to_lmdb/bignumber.txt','w')
# name_list =sorted(os.listdir(path))
# 	# file = open(os.path.join(path,item),'r')
# i=0
# for item in name_list:
# 	with open(os.path.join(path,item),'r',encoding='utf-8') as file:
# 		for line in file.readlines():
# 			print(line)
# 			if line is not None:
# 				for char in line:
# 					if char in bignumber:
# 						all_txt.write(line)
# 						i+=1
# 						break
# 					else:
# 						continue
# print(i)

#已有图像

import torch
from torch.autograd import Variable

from graphviz import Digraph

def make_dot(var, params=None):
    """
    画出 PyTorch 自动梯度图 autograd graph 的 Graphviz 表示.
    蓝色节点表示有梯度计算的变量Variables;
    橙色节点表示用于 torch.autograd.Function 中的 backward 的张量 Tensors.

    Args:
        var: output Variable
        params: dict of (name, Variable) to add names to node that
            require grad (TODO: make optional)
    """
    if params is not None:
        assert all(isinstance(p, Variable) for p in params.values())
        param_map = {id(v): k for k, v in params.items()}

    node_attr = dict(style='filled', shape='box', align='left',
                              fontsize='12', ranksep='0.1', height='0.2')
    dot = Digraph(node_attr=node_attr, graph_attr=dict(size="12,12"))
    seen = set()

    def size_to_str(size):
        return '(' + (', ').join(['%d' % v for v in size]) + ')'

    output_nodes = (var.grad_fn,) if not isinstance(var, tuple) else tuple(v.grad_fn for v in var)

    def add_nodes(var):
        if var not in seen:
            if torch.is_tensor(var):
                # note: this used to show .saved_tensors in pytorch0.2, but stopped
                # working as it was moved to ATen and Variable-Tensor merged
                dot.node(str(id(var)), size_to_str(var.size()), fillcolor='orange')
            elif hasattr(var, 'variable'):
                u = var.variable
                name = param_map[id(u)] if params is not None else ''
                node_name = '%s\n %s' % (name, size_to_str(u.size()))
                dot.node(str(id(var)), node_name, fillcolor='lightblue')
            elif var in output_nodes:
                dot.node(str(id(var)), str(type(var).__name__), fillcolor='darkolivegreen1')
            else:
                dot.node(str(id(var)), str(type(var).__name__))
            seen.add(var)
            if hasattr(var, 'next_functions'):
                for u in var.next_functions:
                    if u[0] is not None:
                        dot.edge(str(id(u[0])), str(id(var)))
                        add_nodes(u[0])
            if hasattr(var, 'saved_tensors'):
                for t in var.saved_tensors:
                    dot.edge(str(id(t)), str(id(var)))
                    add_nodes(t)

    # 多输出场景 multiple outputs
    if isinstance(var, tuple):
        for v in var:
            add_nodes(v.grad_fn)
    else:
        add_nodes(var.grad_fn)
    return dot

ext: py

max_stars_repo_name: seyoulala/cptn-crnn

max_issues_repo_name: seyoulala/cptn-crnn

max_forks_repo_name: seyoulala/cptn-crnn

reason: import numpy


text:
"""
This module contains functions which deal with data loading and filtering.
"""

import csv

import numpy as np

__all__ = ["COLUMNS_COUNT", "TUNE", "SETTING", "NAME", "TYPE", "METER", "MODE",
           "ABC", "DATE", "USERNAME", "extract_columns", "is_matching",
           "read_csv", "find_charset", "create_encoding", "encode_tune_text",
           "decode_tune_matrix"]

COLUMNS_COUNT = 9
# Constant indices of csv row elements.
(TUNE, SETTING, NAME, TYPE,
 METER, MODE, ABC, DATE, USERNAME) = range(COLUMNS_COUNT)

def extract_columns(row, columns_to_extract):
    """ Extract selected columns from the given row. """
    new_row = []
    for column in columns_to_extract:
        new_row.append(row[column])
    return new_row

def is_matching(row, tune_types=None):
    """ Check, if the given tune row matches filtering parameters. """
    matches = True
    if tune_types is not None:
        type_matches = False
        for tune_type in tune_types:
            type_matches = type_matches or row[TYPE] == tune_type
        matches = matches and type_matches
    return matches

def read_csv(file_path, columns_to_extract, filtering_params):
    """ Read tunes from given csv path. """
    with open(file_path, "r", encoding="utf-8") as file:
        # Quotes inside abc text are escaped with \ character.
        csv_reader = csv.reader(file, escapechar="\\", doublequote=False)
        read_rows = []
        # Skip the header row.
        next(csv_reader)
        for row in csv_reader:
            if is_matching(row, **filtering_params):
                read_rows.append(extract_columns(row, columns_to_extract))
    return read_rows

def find_charset(texts):
    """ Find set of unique characters from given list of texts. """
    charset = set()
    for text in texts:
        charset = charset.union(set(text))
    return charset

def create_encoding(charset):
    """ Create encode and decode dictionaries. """
    encoder = {char: i for i, char in enumerate(charset)}
    decoder = {i: char for i, char in enumerate(charset)}
    return encoder, decoder

def encode_tune_text(tune_text, encoder):
    """ Encode given tune text to one-hot format with provided encoder. """
    tune_matrix = np.zeros([len(tune_text), len(encoder)])
    for i, char in enumerate(tune_text):
        tune_matrix[i, encoder[char]] = 1.0
    return tune_matrix

def decode_tune_matrix(tune_matrix, decoder):
    """ Decode given 2D one-hot tune representation to string format. """
    chars_indices = np.argmax(tune_matrix, axis=1)
    tune_text = ""
    for char_index in chars_indices:
        tune_text += decoder[char_index]
    return tune_text

max_stars_repo_name: puchake/bucketnet

max_issues_repo_name: puchake/bucketnet

max_forks_repo_name: puchake/bucketnet


text:
#!/usr/bin/env python3

import numpy as np
import matplotlib.pyplot as plt

def subfigures(a):
    fig, ax = plt.subplots(1,2)
    ax[0].plot(a[:,0],a[:,1])
    ax[1].scatter(a[:,0],a[:,1],c=a[:,2],s=a[:,3])
    plt.show()

def main():
    n = 10
    a = np.random.randint(0, 10, (n, 3))
    print(a)
    print(a.dtype)
    print(a.shape)
    a = np.concatenate([np.arange(n)[:, np.newaxis], a], axis=1)
    subfigures(a)

if __name__ == "__main__":
    main()

max_stars_repo_name: davide-butera/data-analysis-with-python

max_issues_repo_name: davide-butera/data-analysis-with-python

max_forks_repo_name: davide-butera/data-analysis-with-python


text:
import numpy as np
from poap.controller import BasicWorkerThread, SerialController, ThreadController

from pySOT.experimental_design import SymmetricLatinHypercube
from pySOT.optimization_problems import Ackley
from pySOT.strategy import DYCORSStrategy, EIStrategy, LCBStrategy, RandomStrategy, SOPStrategy, SRBFStrategy
from pySOT.surrogate import CubicKernel, GPRegressor, LinearTail, RBFInterpolant

num_threads = 4
ackley = Ackley(dim=10)

def check_strategy(controller):
    """Make sure the strategy object is correct."""

    # Check the strategy object
    assert controller.strategy.num_evals == controller.strategy.max_evals
    assert controller.strategy.init_pending == 0
    assert controller.strategy.pending_evals == 0
    assert controller.strategy.X.shape == (controller.strategy.num_evals, ackley.dim)
    assert controller.strategy.fX.shape == (controller.strategy.num_evals, 1)
    assert controller.strategy.Xpend.shape == (0, ackley.dim)
    assert len(controller.strategy.fevals) == controller.strategy.num_evals

    # Check that the strategy and controller have the same information
    for i in range(controller.strategy.num_evals):
        idx = np.where((controller.strategy.X == controller.fevals[i].params[0]).all(axis=1))[0]
        assert np.all(controller.fevals[i].params[0] == controller.strategy.X[idx, :])
        assert controller.fevals[i].value == controller.strategy.fX[idx]
        assert np.all(controller.fevals[i].params[0] <= ackley.ub)
        assert np.all(controller.fevals[i].params[0] >= ackley.lb)

def test_srbf_serial():
    max_evals = 200
    rbf = RBFInterpolant(dim=ackley.dim, lb=ackley.lb, ub=ackley.ub, kernel=CubicKernel(), tail=LinearTail(ackley.dim))
    slhd = SymmetricLatinHypercube(dim=ackley.dim, num_pts=2 * (ackley.dim + 1))

    # Create a strategy and a controller
    controller = SerialController(ackley.eval)
    controller.strategy = SRBFStrategy(
        max_evals=max_evals, opt_prob=ackley, exp_design=slhd, surrogate=rbf, asynchronous=True
    )
    controller.run()

    check_strategy(controller)

def test_srbf_sync():
    max_evals = 200
    rbf = RBFInterpolant(dim=ackley.dim, lb=ackley.lb, ub=ackley.ub, kernel=CubicKernel(), tail=LinearTail(ackley.dim))
    slhd = SymmetricLatinHypercube(dim=ackley.dim, num_pts=2 * (ackley.dim + 1))

    # Create a strategy and a controller
    controller = ThreadController()
    controller.strategy = SRBFStrategy(
        max_evals=max_evals, opt_prob=ackley, exp_design=slhd, surrogate=rbf, asynchronous=False, batch_size=num_threads
    )

    for _ in range(num_threads):
        worker = BasicWorkerThread(controller, ackley.eval)
        controller.launch_worker(worker)
    controller.run()

    check_strategy(controller)

def test_srbf_async():
    max_evals = 200
    rbf = RBFInterpolant(dim=ackley.dim, lb=ackley.lb, ub=ackley.ub, kernel=CubicKernel(), tail=LinearTail(ackley.dim))
    slhd = SymmetricLatinHypercube(dim=ackley.dim, num_pts=2 * (ackley.dim + 1))

    # Create a strategy and a controller
    controller = ThreadController()
    controller.strategy = SRBFStrategy(
        max_evals=max_evals, opt_prob=ackley, exp_design=slhd, surrogate=rbf, asynchronous=True, batch_size=None
    )

    for _ in range(num_threads):
        worker = BasicWorkerThread(controller, ackley.eval)
        controller.launch_worker(worker)
    controller.run()

    check_strategy(controller)

#######################################################################

def test_dycors_serial():
    max_evals = 200
    rbf = RBFInterpolant(dim=ackley.dim, lb=ackley.lb, ub=ackley.ub, kernel=CubicKernel(), tail=LinearTail(ackley.dim))
    slhd = SymmetricLatinHypercube(dim=ackley.dim, num_pts=2 * (ackley.dim + 1))

    # Create a strategy and a controller
    controller = SerialController(ackley.eval)
    controller.strategy = DYCORSStrategy(
        max_evals=max_evals, opt_prob=ackley, exp_design=slhd, surrogate=rbf, asynchronous=True
    )
    controller.run()

    check_strategy(controller)

def test_dycors_sync():
    max_evals = 200
    rbf = RBFInterpolant(dim=ackley.dim, lb=ackley.lb, ub=ackley.ub, kernel=CubicKernel(), tail=LinearTail(ackley.dim))
    slhd = SymmetricLatinHypercube(dim=ackley.dim, num_pts=2 * (ackley.dim + 1))

    # Create a strategy and a controller
    controller = ThreadController()
    controller.strategy = DYCORSStrategy(
        max_evals=max_evals, opt_prob=ackley, exp_design=slhd, surrogate=rbf, asynchronous=False, batch_size=num_threads
    )

    for _ in range(num_threads):
        worker = BasicWorkerThread(controller, ackley.eval)
        controller.launch_worker(worker)
    controller.run()

    check_strategy(controller)

def test_dycors_async():
    max_evals = 200
    rbf = RBFInterpolant(dim=ackley.dim, lb=ackley.lb, ub=ackley.ub, kernel=CubicKernel(), tail=LinearTail(ackley.dim))
    slhd = SymmetricLatinHypercube(dim=ackley.dim, num_pts=2 * (ackley.dim + 1))

    # Create a strategy and a controller
    controller = ThreadController()
    controller.strategy = DYCORSStrategy(
        max_evals=max_evals, opt_prob=ackley, exp_design=slhd, surrogate=rbf, asynchronous=True, batch_size=None
    )

    for _ in range(num_threads):
        worker = BasicWorkerThread(controller, ackley.eval)
        controller.launch_worker(worker)
    controller.run()

    check_strategy(controller)

#######################################################################

def test_ei_serial():
    max_evals = 50
    gp = GPRegressor(dim=ackley.dim, lb=ackley.lb, ub=ackley.ub)
    slhd = SymmetricLatinHypercube(dim=ackley.dim, num_pts=2 * (ackley.dim + 1))

    # Create a strategy and a controller
    controller = SerialController(ackley.eval)
    controller.strategy = EIStrategy(
        max_evals=max_evals, opt_prob=ackley, exp_design=slhd, surrogate=gp, asynchronous=True
    )
    controller.run()

    check_strategy(controller)

def test_ei_sync():
    max_evals = 50
    gp = GPRegressor(dim=ackley.dim, lb=ackley.lb, ub=ackley.ub)
    slhd = SymmetricLatinHypercube(dim=ackley.dim, num_pts=2 * (ackley.dim + 1))

    # Create a strategy and a controller
    controller = ThreadController()
    controller.strategy = EIStrategy(
        max_evals=max_evals, opt_prob=ackley, exp_design=slhd, surrogate=gp, asynchronous=False, batch_size=num_threads
    )

    for _ in range(num_threads):
        worker = BasicWorkerThread(controller, ackley.eval)
        controller.launch_worker(worker)
    controller.run()

    check_strategy(controller)

def test_ei_async():
    max_evals = 50
    gp = GPRegressor(dim=ackley.dim, lb=ackley.lb, ub=ackley.ub)
    slhd = SymmetricLatinHypercube(dim=ackley.dim, num_pts=2 * (ackley.dim + 1))

    # Create a strategy and a controller
    controller = ThreadController()
    controller.strategy = EIStrategy(
        max_evals=max_evals, opt_prob=ackley, exp_design=slhd, surrogate=gp, asynchronous=True, batch_size=None
    )

    for _ in range(num_threads):
        worker = BasicWorkerThread(controller, ackley.eval)
        controller.launch_worker(worker)
    controller.run()

    check_strategy(controller)

#######################################################################

def test_lcb_serial():
    max_evals = 50
    gp = GPRegressor(dim=ackley.dim, lb=ackley.lb, ub=ackley.ub)
    slhd = SymmetricLatinHypercube(dim=ackley.dim, num_pts=2 * (ackley.dim + 1))

    # Create a strategy and a controller
    controller = SerialController(ackley.eval)
    controller.strategy = LCBStrategy(
        max_evals=max_evals, opt_prob=ackley, exp_design=slhd, surrogate=gp, asynchronous=True
    )
    controller.run()

    check_strategy(controller)

def test_lcb_sync():
    max_evals = 50
    gp = GPRegressor(dim=ackley.dim, lb=ackley.lb, ub=ackley.ub)
    slhd = SymmetricLatinHypercube(dim=ackley.dim, num_pts=2 * (ackley.dim + 1))

    # Create a strategy and a controller
    controller = ThreadController()
    controller.strategy = LCBStrategy(
        max_evals=max_evals, opt_prob=ackley, exp_design=slhd, surrogate=gp, asynchronous=False, batch_size=num_threads
    )

    for _ in range(num_threads):
        worker = BasicWorkerThread(controller, ackley.eval)
        controller.launch_worker(worker)
    controller.run()

    check_strategy(controller)

def test_lcb_async():
    max_evals = 50
    gp = GPRegressor(dim=ackley.dim, lb=ackley.lb, ub=ackley.ub)
    slhd = SymmetricLatinHypercube(dim=ackley.dim, num_pts=2 * (ackley.dim + 1))

    # Create a strategy and a controller
    controller = ThreadController()
    controller.strategy = LCBStrategy(
        max_evals=max_evals, opt_prob=ackley, exp_design=slhd, surrogate=gp, asynchronous=True, batch_size=None
    )

    for _ in range(num_threads):
        worker = BasicWorkerThread(controller, ackley.eval)
        controller.launch_worker(worker)
    controller.run()

    check_strategy(controller)

#######################################################################

def test_random_sampling():
    max_evals = 500
    controller = ThreadController()
    controller.strategy = RandomStrategy(opt_prob=ackley, max_evals=max_evals)

    for _ in range(num_threads):
        worker = BasicWorkerThread(controller, ackley.eval)
        controller.launch_worker(worker)
    controller.run()

    assert len(controller.fevals) == max_evals
    for rec in controller.fevals:
        assert np.all(rec.params[0] <= ackley.ub)
        assert np.all(rec.params[0] >= ackley.lb)

#######################################################################

def test_sop_serial():
    max_evals = 200
    rbf = RBFInterpolant(dim=ackley.dim, lb=ackley.lb, ub=ackley.ub, kernel=CubicKernel(), tail=LinearTail(ackley.dim))
    slhd = SymmetricLatinHypercube(dim=ackley.dim, num_pts=2 * (ackley.dim + 1))

    # Create a strategy and a controller
    controller = SerialController(ackley.eval)
    controller.strategy = SOPStrategy(
        max_evals=max_evals, opt_prob=ackley, exp_design=slhd, surrogate=rbf, asynchronous=True, ncenters=4
    )
    controller.run()

    check_strategy(controller)

def test_sop_sync():
    max_evals = 200
    rbf = RBFInterpolant(dim=ackley.dim, lb=ackley.lb, ub=ackley.ub, kernel=CubicKernel(), tail=LinearTail(ackley.dim))
    slhd = SymmetricLatinHypercube(dim=ackley.dim, num_pts=2 * (ackley.dim + 1))

    # Create a strategy and a controller
    controller = ThreadController()
    controller.strategy = SOPStrategy(
        max_evals=max_evals,
        opt_prob=ackley,
        exp_design=slhd,
        surrogate=rbf,
        asynchronous=False,
        ncenters=num_threads,
        batch_size=num_threads,
    )

    for _ in range(num_threads):
        worker = BasicWorkerThread(controller, ackley.eval)
        controller.launch_worker(worker)
    controller.run()

    check_strategy(controller)

def test_sop_async():
    max_evals = 200
    rbf = RBFInterpolant(dim=ackley.dim, lb=ackley.lb, ub=ackley.ub, kernel=CubicKernel(), tail=LinearTail(ackley.dim))
    slhd = SymmetricLatinHypercube(dim=ackley.dim, num_pts=2 * (ackley.dim + 1))

    # Create a strategy and a controller
    controller = ThreadController()
    controller.strategy = SOPStrategy(
        max_evals=max_evals, opt_prob=ackley, exp_design=slhd, surrogate=rbf, asynchronous=True, ncenters=num_threads
    )

    for _ in range(num_threads):
        worker = BasicWorkerThread(controller, ackley.eval)
        controller.launch_worker(worker)
    controller.run()

    check_strategy(controller)

if __name__ == "__main__":
    test_srbf_serial()
    test_srbf_sync()
    test_srbf_async()

    test_dycors_serial()
    test_dycors_sync()
    test_dycors_async()

    test_ei_serial()
    test_ei_sync()
    test_ei_async()

    test_lcb_serial()
    test_lcb_sync()
    test_lcb_async()

    test_random_sampling()

    test_sop_serial()
    test_sop_sync()
    test_sop_async()

max_stars_repo_name: WY-Wang/pySOT

max_issues_repo_name: WY-Wang/pySOT

max_forks_repo_name: WY-Wang/pySOT


text:
''' Util functions for training and evaluation.

Author: Charles R. Qi
Date: September 2017
'''

import numpy as np

def get_batch(dataset, idxs, start_idx, end_idx,
              num_point, num_channel,
              from_rgb_detection=False):
    ''' Prepare batch data for training/evaluation.
    batch size is determined by start_idx-end_idx

    Input:
        dataset: an instance of FrustumDataset class
        idxs: a list of data element indices
        start_idx: int scalar, start position in idxs
        end_idx: int scalar, end position in idxs
        num_point: int scalar
        num_channel: int scalar
        from_rgb_detection: bool
    Output:
        batched data and label
    '''
    if from_rgb_detection:
        return get_batch_from_rgb_detection(dataset, idxs, start_idx, end_idx,
            num_point, num_channel)

    bsize = end_idx-start_idx
    batch_data = np.zeros((bsize, num_point, num_channel))
    batch_cls_label = np.zeros((bsize,), dtype=np.int32)
    batch_label = np.zeros((bsize, num_point), dtype=np.int32)
    batch_center = np.zeros((bsize, 3))
    batch_heading_class = np.zeros((bsize,), dtype=np.int32)
    batch_heading_residual = np.zeros((bsize,))
    batch_size_class = np.zeros((bsize,), dtype=np.int32)
    batch_size_residual = np.zeros((bsize, 3))
    batch_rot_angle = np.zeros((bsize,))
    if dataset.extra_feature:
        batch_feature_vec = np.zeros((bsize, len(dataset[0][-1])))
    for i in range(bsize):
        if dataset.extra_feature:
            ps,seg,center,hclass,hres,sclass,sres,rotangle,cls_label,feature_vec = \
                dataset[idxs[i+start_idx]]
            batch_feature_vec[i] = feature_vec
        else:
            ps,seg,center,hclass,hres,sclass,sres,rotangle,cls_label = \
                dataset[idxs[i+start_idx]]
        batch_data[i,...] = ps[:,0:num_channel]
        batch_cls_label[i] = cls_label
        batch_label[i,:] = seg
        batch_center[i,:] = center
        batch_heading_class[i] = hclass
        batch_heading_residual[i] = hres
        batch_size_class[i] = sclass
        batch_size_residual[i] = sres
        batch_rot_angle[i] = rotangle
    if dataset.extra_feature:
        return batch_data, batch_cls_label, batch_label, batch_center, \
            batch_heading_class, batch_heading_residual, \
            batch_size_class, batch_size_residual, \
            batch_rot_angle, batch_feature_vec
    else:
        return batch_data, batch_cls_label, batch_label, batch_center, \
            batch_heading_class, batch_heading_residual, \
            batch_size_class, batch_size_residual, batch_rot_angle

def get_batch_from_rgb_detection(dataset, idxs, start_idx, end_idx,
                                 num_point, num_channel):
    bsize = end_idx-start_idx
    batch_data = np.zeros((bsize, num_point, num_channel))
    batch_rot_angle = np.zeros((bsize,))
    batch_prob = np.zeros((bsize,))
    if dataset.one_hot:
        batch_one_hot_vec = np.zeros((bsize,3)) # for car,ped,cyc
    for i in range(bsize):
        if dataset.one_hot:
            ps,rotangle,prob,onehotvec = dataset[idxs[i+start_idx]]
            batch_one_hot_vec[i] = onehotvec
        else:
            ps,rotangle,prob = dataset[idxs[i+start_idx]]
        batch_data[i,...] = ps[:,0:num_channel]
        batch_rot_angle[i] = rotangle
        batch_prob[i] = prob
    if dataset.one_hot:
        return batch_data, batch_rot_angle, batch_prob, batch_one_hot_vec
    else:
        return batch_data, batch_rot_angle, batch_prob

max_stars_repo_name: JenningsL/frustum-pointnets

max_issues_repo_name: JenningsL/frustum-pointnets

max_forks_repo_name: JenningsL/frustum-pointnets


text:
import cv2
import numpy as np
import requests

download_url ="https://auth.anuvaad.org/download/"
token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyTmFtZSI6Im5hcmVzaC5rdW1hckB0YXJlbnRvLmNvbSIsInBhc3N3b3JkIjoiYickMmIkMTIkdThZQ3ZFTlhqTkMyUUFjdzB4NzZZdUVwTVh2SXgwREJ0MU1NWllyRlZXL042d2ZFbDE0UEsnIiwiZXhwIjoxNjE4NTY2NTkxfQ.RUikS1OgMDqBZ0R2_ZfnOfQMHaCDQPf5iWGd1tBCojM'

headers = {
    'auth-token' :token }
def download_file(download_url,headers,outputfile,f_type='json'):
    download_url =download_url+str(outputfile)
    res = requests.get(download_url,headers=headers)
    if f_type == 'json':
        return res.json()
    else :
        return res.content

def get_energy_density(path,save_base_path):
    #image   = cv2.imread("/home/naresh/anuvaad/anuvaad-etl/anuvaad-extractor/document-processor/ocr/tesseract/"+image_path,0)
    #image   = cv2.imread(image_path,0)
    path = path.split('upload')[1]

    image = download_file(download_url,headers,path,f_type='image')
    nparr = np.frombuffer(image, np.uint8)
    image = cv2.imdecode(nparr, cv2.IMREAD_GRAYSCALE)
    #save_path = save_base_path+str(uuid.uuid4()) + '.jpg'
    #img_save_path = cv2.imwrite(save_path,image)
    #image = cv2.imread(save_path,0)

    binary  = cv2.adaptiveThreshold(image,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2)

    distance_transform =cv2.distanceTransform(binary.copy(), distanceType=cv2.DIST_L2, maskSize=5)

    #energy_density = 1 /  np.log(distance_transform + np.exp(1))
    energy_density = 1 /  (distance_transform + 1)**2

    return energy_density

def get_equilibrium_delta(boundry,energy_density ,axis):

    # y is axis 1, x is axis 0
    boundry_energy  = energy_density[int(boundry[0]) : int(boundry[3]) , int(boundry[1]) : int(boundry[2])]
    inital_boundry = boundry_energy.shape[1 - axis]  * 0.5
    delta = inital_boundry - np.argmin(boundry_energy.sum(axis=axis))
    return delta

def correct_region(region,energy_density):
    image_height      = energy_density.shape[0]
    box = region['boundingBox']['vertices']

    box_height =  box[3]['y'] - box[0]['y']
    box_widht  =  box[1]['x'] - box[0]['x']
    box_left   =  box[0]['x']
    box_right  =  box[1]['x']
    box_top    =  box[0]['y']
    box_bottom =  box[3]['y']

    if box_height > 0:
        #order : top, left, right, bottom
        boundry_top    = [ max(box_top - box_height * 0.5 ,0), box_left ,box_right ,box_top + box_height * 0.5]
        boundry_bottom = [ box_bottom - box_height * 0.5 , box_left ,box_right ,min(box_bottom + box_height * 0.5,image_height)]

        top_delta    = get_equilibrium_delta(boundry_top , energy_density,axis=1)
        bottom_delta = get_equilibrium_delta(boundry_bottom, energy_density,axis=1)
        #print(top_delta, bottom_delta)
        region['boundingBox']= {'vertices'  : [{'x':box_left,'y':int(box_top - top_delta)},\
                                                                 {'x':box_right,'y':int(box_top - top_delta)},\
                                                                 {'x':box_right,'y':int(box_bottom -bottom_delta)},\
                                                                 {'x':box_left,'y': int(box_bottom -bottom_delta)}]}
        return region
    else:
        return region

def get_corrected_regions(regions,energy_density):
    corrected_regions = []
    for idx, region in enumerate(regions ):
             corrected_regions.append(correct_region(region,energy_density))
    return corrected_regions

def coord_adjustment(page_path, regions,save_base_path):
    energy_density    = get_energy_density(page_path,save_base_path)
    corrected_regions = get_corrected_regions(regions,energy_density)

    return corrected_regions

max_stars_repo_name: srihari-nagaraj/anuvaad

max_issues_repo_name: srihari-nagaraj/anuvaad

max_forks_repo_name: srihari-nagaraj/anuvaad


text:
"""
Created on Thu Nov  2 12:27:48 2017

@author: Ki Wai Chau
"""
import numpy as np
import scipy.stats as ss
from scipy.special import binom

def diffusionproduct(mu, sigma, order, d, x, time, delta_t):
    """Calculate the expectations of product of a diffusion process

    This function calculates the expectations of product (up to second order)
    of the Euler approximation of a diffusion process in one time step

    :param mu: The drift function for the difussion
    :sigma sigma: The volitility function of the difussion
    :param order: The multiplication order
    :param d: The active dimension in the product.
    :param x: Currect state of the diffusion
    :param time: current time
    :param delta_t: The length of the time step under consideration
    :type mu: function
    :type sigma: function
    :type order: int
    :type d: dict
    :type x: Nxd array
    :type time: float
    :type delta_t: float
    :returns: Array of expectations for each starting position in x_0
    :rtype: N array
    """
    if order == 1:
        # Result for first order
        for key in d:
            rec = np.empty(x.shape[0])
            for i in range(x.shape[0]):
               rec[i] = x[i, key-1] + mu(x[i], time)[key-1] * delta_t
        return rec
    elif order == 2:
        factors = ()
        for key in d:
            for i in range(d[key]):
                factors = factors + (key-1,)
        rec = np.empty(x.shape[0])
        for i in range(x.shape[0]):
            temp = x[i, factors[0]] * mu(x[i], time)[factors[1]] \
            + x[i, factors[1]] * mu(x[i], time)[factors[0]] \
            + np.einsum('ij, kj->ik', sigma(x[i], time), sigma(x[i], time))[factors]\
            +  mu(x[i], time)[factors[0]] * mu(x[i], time)[factors[1]] * delta_t
            rec[i] = delta_t * temp + x[i, factors[0]] * x[i, factors[1]]
        return rec # Result for second order

def partitionfunc(degree, k, lowest_degree=0, key=1):
    '''  Calculate all possible distribution of multiplication power

    Multiplication power degree is distributed among the prescripted dimensions.
    The key input is use to track the iterations of applying the function as
    this function will be used recursively. This function returns a generator for
    dictionary.

    :param degree: The multiplication degree to be distributed
    :param k: The number of dimension for the distribution
    :param lowest_degree: The lowest possible degree for a dimension
    :param key: The current dimension under condsideration
    :type degree: int
    :type k: int
    :type l: int
    :type key: int
    :rtype: generator
    '''
    if k < 1:
        raise IndexError('The number of factors must be positive.')
    if k == 1:
        if (degree >= lowest_degree) and (degree != 0):
            yield {key:degree}
        elif degree == 0:
            yield {}
    if k > 1:
        for i in range(lowest_degree, degree+1):
            for result in partitionfunc(degree-i, k-1, lowest_degree, key+1):
                if i != 0:
                    temp = {key:i, }
                else:
                    temp = {}
                temp.update(result)
                yield temp

def star_and_bin_array(star, bins):
    """  Calculate all possible distribution of multiplication power

    This function calls partitionfunc and returns the results in array instead.
    """
    i = 0
    temp = np.zeros((int(binom(star+bins-1, star)), bins), dtype=int)
    for combination in partitionfunc(star, bins):
        for key in combination:
            temp[i, key-1] = combination[key]
        i += 1
    return temp

def step_euler(last, dt, drift, volatility, noise):
    """Approximate SDE in one time step with Euler scheme"""
    return last + drift * dt + np.dot(volatility, noise)

def euler(x0, mu, sigma, TIMESPAN, random = None):
    """Approximate SDE in one time step with Euler scheme directly with model function"""
    N = len(TIMESPAN)
    q = len(x0)
    if random is None:
        random = np.random.normal(scale=1, size=(N, q))
    process = np.empty((q, N))
    process[:, 0] = x0
    for n in range(0, N-1):
        tn = TIMESPAN[n]
        xn = process[:, n]
        delta_n = TIMESPAN[n+1] - TIMESPAN[n]
        process[:, n+1] = step_euler(xn, delta_n, mu(xn, tn), sigma(xn, tn), np.sqrt(delta_n) * random[n])
    return process

#Black and Scholes
def d1(S0, K, r, sigma, T):
    return (np.log(S0/K) + (r + sigma**2 / 2) * T)/(sigma * np.sqrt(T))

def d2(S0, K, r, sigma, T):
    return (np.log(S0 / K) + (r - sigma**2 / 2) * T) / (sigma * np.sqrt(T))

def black_scholes(type,S0, K, r, sigma, T):
    """Calculate the 1-dimension Black-Scholes put/call price"""
    if type=="C":
        return S0 * ss.norm.cdf(d1(S0, K, r, sigma, T)) - K * np.exp(-r * T) * ss.norm.cdf(d2(S0, K, r, sigma, T))
    else:
       return K * np.exp(-r * T) * ss.norm.cdf(-d2(S0, K, r, sigma, T)) - S0 * ss.norm.cdf(-d1(S0, K, r, sigma, T))

reason: import numpy,import scipy,from scipy


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

"""
梯度下降算法，进行线性回归分析
numpy
"""

import numpy as np
import matplotlib.pyplot as plt

def createData(points):
    """
    创建points个符合sin分布的点
    并引入一些噪声
    """
    X=np.linspace(-3,3,points)
    np.random.seed(6)
    Y=np.sin(X)+np.random.uniform(-0.5,0.5,points)
    return X,Y

def predict(X2,W):
    return X2.dot(W)

def gradient_descent(X2, Y, lrate, epochs, W):
    """
    梯度下降
    """
    total_expected_error = 0
    errorlist = []
    finalepoch = 0

    for i in range(epochs):
        # 计算误差
        predictedY = predict(X2,W)
        error = (predictedY - Y) ** 2
        total_error = np.sum(error)

        # d/dϴ =error*x.T
        # 梯度下降
        gradient = X2.T.dot(error) / X2.shape[0]

        # 每一百次记录一次错误情况
        if i % 100 == 0:
            errorlist.append(total_error)
            finalepoch += 1

        # 相邻两次训练没有明显提升，则结束训练
        if np.abs(total_expected_error - total_error) < 0.0005:
            return errorlist, finalepoch
        total_expected_error = total_error

        # 进行系数修正
        W += -lrate * gradient

    return errorlist, finalepoch

if __name__ == '__main__':
    # 创建数据并绘图展示
    X, Y = createData(100)
    p=plt.plot(X,Y,'ro')
    plt.axis([-4,4,-2.0,2.0])
    plt.show()

    # 在X前面增加一列1
    X2 = np.c_[np.ones(len(X)), X]

    # 初始化系数
    W = np.random.uniform(size=X2.shape[1], )

    # 训练5000次
    total_error, finalepoch = gradient_descent(X2, Y, 0.001, 5000, W)

    # 展示错误下降情况
    plt.plot(range(finalepoch), total_error)
    plt.xlabel("epochs in 100's")
    plt.ylabel("error")
    plt.show()

    #展示拟合结果
    plt.plot(X, Y, 'ro')
    plt.plot(X, predict(X2, W), 'b')
    plt.show()

max_stars_repo_name: neohope/MLStudy

max_issues_repo_name: neohope/MLStudy

max_forks_repo_name: neohope/MLStudy


text:
import tkinter as tk
import numpy as np
from PIL import Image, ImageTk

import client
import draw

import sys
import time
import io

img_ref = None

def update(conn, root, canvas, img_id, textvar, logview):
    global img_ref

    # pull new log data
    log_raw = conn.send_recv(0x01)
    if log_raw is None: return
    log_data = log_raw.decode('utf-8')
    if len(log_data) > 0:
        print(log_data)
        logview.configure(state='normal')
        logview.insert("end", log_data)
        logview.see("end")
        logview.configure(state='disabled')

    frame = conn.send_recv(0x02)
    if frame is None: return
    draw_data = b''
    if len(frame) > 0:
        img = Image.open(io.BytesIO(frame))
        draw_data = conn.send_recv(0x03)
        if draw_data is None: return
        draw.draw(img, draw_data)
        img_ref = ImageTk.PhotoImage(img)
        canvas.itemconfigure(img_id, image=img_ref)

    telem_data = conn.send_recv(0x04)
    if telem_data is None: return
    telem = np.frombuffer(telem_data, dtype='>f8')
    if len(telem) > 0:
        textvar.set(
            """
Forward power:    %.3f
Turn power:       %.3f
Forward position: %.1f
Heading:          %.3f
Path index:       %.0f
Speed:            %.3f
Turret position:  %.3f
Turret target:    %.3f
Shooter power:    %.3f
Shooter velocity: %.0f
Rings detected:   %.0f
            """ % tuple(telem)
        )

    if len(log_raw) > 0 or len(frame) > 0 or len(draw_data) > 0 or len(telem_data) > 0:
        print("-> Data: %d bytes log, %d bytes frame, %d bytes draw, %d bytes telemetry" % (len(log_raw), len(frame), len(draw_data), len(telem_data)))

    root.after(16, update, conn, root, canvas, img_id, textvar, logview)

def main():
    conn = client.Connection('192.168.43.1', 8813)
    conn.connect()

    root = tk.Tk()
    img_width = 800
    img_height = 448

    canvas = tk.Canvas(root, width=img_width, height=img_height)
    canvas.grid(column=0, row=0, columnspan=2)
    canvas_img = canvas.create_image(0, 0, image=None, anchor='nw')

    frame = tk.Frame(root)
    logview = tk.Text(frame, width=150, height=13)
    logscroll = tk.Scrollbar(frame, orient='vertical', command=logview.yview)
    logview.configure(yscrollcommand=logscroll.set)
    logview.grid(column=0, row=0)
    logscroll.grid(column=1, row=0, sticky="ns")
    frame.grid(column=0, row=1, sticky="w")
    logview.configure(state='disabled')

    textvar = tk.StringVar()
    textvar.set(
        """
        Forward power:
        Turn power:
        Forward position:
        Heading:
        Path index:
        Speed:
        Turret position:
        Turret target:
        Shooter power:
        Shooter velocity:
        Rings detected:
        """
    )
    telem_message = tk.Message(root, textvariable=textvar, anchor='nw', justify='left', font='monospace 9', bg='#ffffff', width=300)
    telem_message.grid(column=1, row=1, sticky="ew")

    root.resizable(False, False)

    root.after(16, update, conn, root, canvas, canvas_img, textvar, logview)
    root.mainloop()

if __name__ == '__main__':
    main()


text:
import cv2
import numpy as np
import os
import sys
import glob

path = r"/home/net/MORYSO/Assignment/stereo2depth/Input_images*.*"

print('loading images...')
pathname = os.path.dirname(sys.argv[0])

# filename = os.path.splitext(os.path.basename(sys.argv[0]))[0]
filename = r'/home/net/MORYSO/stereo2depth/stero2depth.py'
print('Loading images from :' + path)

for image, file in enumerate(glob.glob(path)):
    print(image, file)
    a = cv2.imread(file)
    #print(a)
    img = cv2.imread(file)
    assert not isinstance(img, type(None)), 'image not found'
    c = img
    cv2.imshow('Depth Image', c)
    cv2.imwrite('/home/net/MORYSO/Assignment/stereo2depth/Output_Images/output_image{}.png'.format(image), c)

    height, width = img.shape[:2]
    imgL = img[0:height//2, 0:width]
    imgR = img[height//2:height, 0:width]

    window_size = 3
    shouldirun = ()
    def Nothing(value):
        if (shouldirun == 1):
            Update(0)
        else:
                return None

    def Update(value):
        miniDisparities=16
        window_size = cv2.getTrackbarPos('windowSize','settings')
        rez = cv2.getTrackbarPos('resolution','settings')/20.0
        resL = cv2.resize(imgL,None,fx=rez, fy=rez, interpolation = cv2.INTER_AREA)
        resR = cv2.resize(imgR,None,fx=rez, fy=rez, interpolation = cv2.INTER_AREA)

        match_left = cv2.StereoSGBM_create(
            miniDisparity=0,
            numDisparities=miniDisparities,
            blockSize= cv2.getTrackbarPos('blockSize', 'settings'),
            P1=8 * 3 * window_size ** 2,
            P2=32 * 3 * window_size ** 2,
            disp12MaxDiff=1,
            uniquenessRatio=15,
            speckleWindowSize=0,
            speckleRange=2,
            preFilterCap= cv2.getTrackbarPos('preFilterCap','settings'),
            mode=cv2.STEREO_SGBM_MODE_HH
            )

        match_right = cv2.ximgproc.createRightMatcher(match_left)

        # FILTER parameters
        lmbda = cv2.getTrackbarPos('lmbda','settings') * 1000
        sigma = 1.2
        visual_multiplier = 1

        wls_filter = cv2.ximgproc.createDisparityWLSFilter(left_match=match_left)
        wls_filter.setLambda(lmbda)
        wls_filter.setSigmaColor(sigma)

        if (value == 0):
            print('calculating depth map')
        displ = match_left.compute(resL, resR)
        dispr = match_right.compute(resR, resL)
        imgLb = cv2.copyMakeBorder(imgL, top=0, bottom=0, left=np.uint16(minDisparities/rez), right=0, borderType= cv2.BORDER_CONSTANT, value=[155,155,155] )
        filteredImg = wls_filter.filter(disp1, imgLb, None, dispr)
        filteredImg = filteredImg + (cv2.getTrackbarPos('Bright','settings')-100)
        filteredImg = (cv2.getTrackbarPos('Contrast','settings')/10.0)*(filteredImg - 128) + 128
        filteredImg = np.clip(filteredImg, 0, 255)
        filteredImg = np.uint8(filteredImg)
        filteredImg = cv2.resize(filteredImg,(width,height/2),inerpolation = cv2.INTER_CUBIC)
        filteredImg = filteredImg[0:height, np.uint16(minDisparities/rez):width]
        filteredImg = cv2.resize(filteredImg, (width,height/2), interpolation = cv2.INTER_CUBIC)

        cv2.imshow('Depth Map', filteredImg)
        cv2.resizeWindow('Depth Map', 1000,500)
        if (value == 1):
            return filteredImg

        def SaveDepth(value):
            if (shouldirun == 1 & value == 1):
                cv2.imwrite(pathname + '/' + filename + '_44.jpg', Update(1), [cv2.IMWRITE_JPEG_QUALITY, 100])
                cv2.imshow('Depth Image', c)
                cv2.imwrite('/home/net/MORYSO/Assignment/stereo2depth/Output_Images/output_image{}.png'.format(image),c)
                cv2.setTrackbarPos('Save Depth','settings',0)

        def Save6DoF(value):
            if(shouldirun == 1 & value == 1):
                depth = Update(1)
                depth = cv2.cvtColor(depth, cv2.COLOR_RGB2HSV)
                dof = np.concatenate((imgR, depth), axis=0)
                cv2.imwrite(pathname + '/' + filename + '_6DoF.jpg', dof, [cv2.IMWRITE_JPEG_QUALITY, 100])
                cv2.imshow('Depth Image', c)
                cv2.imwrite('/home/net/MORYSO/Assignment/stereo2depth/Output_Images/output_image{}.png'.format(image),c)
            cv2.setTrackbarPos('Save 6DoF','settings',0)

        shouldirun = 0
        cv2.namedWindow('Depth Map', cv2.WINDOW_NORMAL)
        cv2.namedWindow('settings', cv2.WINDOW_AUTOSIZE)
        cv2.resizeWindow('settings', 500,400)
        cv2.namedWindow('Left', cv2.WINDOW_NORMAL)
        cv2.resizeWindow('Left', 300,150)
        cv2.namedWindow('Right', cv2.WINDOW_NORMAL)
        cv2.resizeWindow('Right', 300,150)
        cv2.imshow('Left', imgL)
        cv2.imshow('Right', imgR)
        cv2.createTrackbar('resolution','settings',1,20,Nothing)
        cv2.setTrackbarPos('resolution','settings',1)
        cv2.createTrackbar('blockSize','settings',0,25,Nothing)
        cv2.setTrackbarPos('blockSize','settings',5)
        cv2.createTrackbar('windowSize','settings',0,15,Nothing)
        cv2.setTrackbarPos('windowSize','settings',5)
        cv2.createTrackbar('preFilterCap','settings',0,100,Nothing)
        cv2.setTrackbarPos('preFilterCap','settings', 63)
        cv2.createTrackbar('lmbda','settings',0,100,Nothing)
        cv2.setTrackbarPos('lmbda','settings',80)
        cv2.createTrackbar('Bright','settings',0,200,Nothing)
        cv2.setTrackbarPos('Bright','settings',100)
        cv2.createTrackbar('Contrast','settings',0,30,Nothing)
        cv2.setTrackbarPos('Contrast','settings',10)
        cv2.createTrackbar('Save Depth','settings',0,1,SaveDepth)
        cv2.createTrackbar('Save 6DoF','settings',0,1,Save6DoF)
        shouldirun = 1
        Update(0)

        while(1):
            k = cv2.waitKey(1) & 0xFF
            if k == 27:
                break
        cv2.destroyAllWindows()

    if __name__ == '__main__':
        #try:
        Nothing(0)
        #except (NameError, cv2.error):
        Update(1)


text:
from lineart import style
from lineart import transform
from flat import document

import numpy as np

def z_blur_sample_line(p1, p2, n, scale):
    points = sample_line(p1, p2, n)
    points = add_z_jitter(points, scale=scale)
    return points

def sample_line(p1, p2, n):
    vector = p2 - p1
    r_sample = np.random.rand(n, 1)
    points = np.multiply(r_sample, vector) + p1
    return points

def add_z_jitter(points: np.array, scale):
    n = len(points)
    z_values = points[:, 2]
    jitter_scalar = np.multiply(scale, z_values)

    jitter = np.multiply(
        (np.random.randn(n, 2) * 2 - 1),
        np.stack((jitter_scalar, jitter_scalar), axis=1),
    )
    points = points + np.concatenate(
        (jitter, np.zeros_like(z_values).reshape(-1, 1)), axis=1
    )
    return points

def setup_tiled_page(
    tile_size=(100, 100),
    n_cols=1,
    n_rows=1,
    background=style.canvas_fill
):
    x_dim = tile_size[0]*n_cols
    y_dim = tile_size[1]*n_rows
    d = document(x_dim, y_dim, "mm")

    page = d.addpage()
    page.place(background.rectangle(0, 0, x_dim, y_dim,))
    return page

def draw_edges_on_tile(
    edges,
    col,
    row,
    tile_size,
    page,
    edge_style=style.blue_edge,
    v=False
):
    x_origin = col*tile_size
    y_origin = row*tile_size
    edges = edges[:, :, :-1]+np.array([x_origin, y_origin])
    for e in edges:
        page.place(edge_style.line(*e.flatten()))
    if v:
        for p in edges.reshape(-1, 2):
            page.place(style.debug.circle(*p, 2))
    return page

def draw_zsampled_edges_on_tile(
    edges,
    col,
    row,
    tile_size,
    page,
    n=1000,
    scatter=0.02,
    sand_size=0.01,
    point_style=style.blue_sand,
    v=False

):
    x_origin = col*tile_size
    y_origin = row*tile_size
    edges = edges + np.array([x_origin, y_origin,0])
    line_points = [z_blur_sample_line(*e, n, scatter) for e in edges]
    points = np.concatenate(line_points)

    for p in points:
        page.place(point_style.circle(*p[:2], sand_size))

    return page

def quick_draw_edges(
    edges,
    image_size=100,
    v=False,
    background=style.canvas_fill,
    edge_style=style.blue_edge,
):

    edges = edges[:, :, :-1]
    # page setup
    d = document(image_size, image_size, "mm")
    page = d.addpage()
    page.place(background.rectangle(0, 0, image_size, image_size))
    for e in edges:
        page.place(edge_style.line(*e.flatten()))
    if v:
        for p in edges.reshape(-1, 2):
            page.place(style.debug.circle(*p, 2))

    return page.image(kind="rgba", ppi=60)

def quick_draw_zsampled_edges(
    edges,
    n=1000,
    scatter=0.02,
    image_size=100,
    v=False,
    background=style.canvas_fill,
    point_style=style.blue_sand,
):

    image_size = 100
    # page setup
    d = document(image_size, image_size, "mm")
    page = d.addpage()
    page.place(background.rectangle(0, 0, image_size, image_size))
    line_points = [z_blur_sample_line(*e, n, scatter) for e in edges]
    points = np.concatenate(line_points)

    for p in points:
        page.place(point_style.circle(*p[:2], 0.1))
    return page.image(kind="rgba", ppi=60)


text:
import pandas as pd
import numpy as np

from pandas import ewma

class CryptoAnalysis(object):
    def __init__(self, data):
        self.data = data
        if isinstance(self.data, pd.DataFrame):
            print("Data is pandas Dataframe.")
        elif isinstance(self.data, np.ndarray):
            print("Data is numpy array.")
        elif isinstance(self.data, pd.Series):
            print("Data is pandas Series.")
        else:
            raise ValueError("The input data must be numpy array, pandas DataFrame or Series.")

    def SMA(self, period=3):
        pass

    def EMA(self, periods=3):
        pass

    def BBands(self):
        pass

if __name__ == '__main__':
    c = CryptoAnalysis([1, 2, 3])

max_stars_repo_name: Naghipourfar/TraderBot

max_issues_repo_name: Naghipourfar/TraderBot

max_forks_repo_name: Naghipourfar/TraderBot


text:
import math
import numpy as np  # type: ignore

def angle( x, y ):
    """
    Calculate the angle between two vectors, in degrees.

    Args:
        x (np.array): one vector.
        y (np.array): the other vector.

    Returns:
        (float):      the angle between x and y in degrees.
    """
    dot = np.dot( x, y )
    x_mod = np.linalg.norm( x )
    y_mod = np.linalg.norm( y )
    cos_angle = dot / ( x_mod * y_mod )
    return np.degrees( np.arccos( cos_angle ) )

def rotation_matrix(axis, theta):
    """
    Return the 3D rotation matrix associated with counterclockwise rotation about
    the given axis by theta radians.

    Args:
        axis (np.array): length 3 numpy array defining the axis of rotation.
        theta (float):   rotation angle in radians.

    Returns:
        (np.array):      the corredponding rotation matrix.
    """
    axis = np.asarray( axis )
    theta = np.asarray( theta )
    axis = axis / math.sqrt( np.dot( axis, axis ) )
    a = math.cos( theta / 2 )
    b, c, d = -axis * math.sin( theta / 2 )
    aa, bb, cc, dd = a*a, b*b, c*c, d*d
    bc, ad, ac, ab, bd, cd = b*c, a*d, a*c, a*b, b*d, c*d
    return np.array( [ [ aa+bb-cc-dd, 2*(bc+ad), 2*(bd-ac) ],
                       [ 2*(bc-ad), aa+cc-bb-dd, 2*(cd+ab) ],
                       [ 2*(bd+ac), 2*(cd-ab), aa+dd-bb-cc ] ] )

class Cell:

    def __init__( self, matrix ):
        """
        Initialise a Cell object.

        Args:
            matrix (np.array): 3x3 numpy array containing the cell matrix.

        Returns:
            None
        """
        assert type( matrix ) is np.ndarray
        assert matrix.shape == ( 3, 3 )
        self.matrix = matrix # 3 x 3 numpy Array
        self.inv_matrix = np.linalg.inv( matrix )

    def dr( self, r1, r2, cutoff=None ):
        """
        Calculate the distance between two fractional coordinates in the cell.

        Args:
            r1 (np.array): fractional coordinates for position 1.
            r2 (np.array): fractional coordinates for position 2.
            cutoff (optional:Bool): If set, returns None for distances greater than the cutoff. Default None (unset).

        Returns:
            (float): the distance between r1 and r2.
        """
        delta_r_cartesian = ( r1 - r2 ).dot( self.matrix )
        delta_r_squared = sum( delta_r_cartesian**2 )
        if cutoff != None:
            cutoff_squared = cutoff ** 2
            if delta_r_squared > cutoff_squared:
                return None
        return( math.sqrt( delta_r_squared ) )

    def nearest_image( self, origin, point ):
        """
        Find the fractional_coordinates of the nearest periodic image to a point of origin.

        Args:
            origin (np.array): fractional coordinates of the point of origin.
            point  (np.array): fractional coordinates of the other point.

        Returns:
            (np.array): the fractional coordinates of the nearest image of `point` to `origin`.
        """
        return( origin + self.minimum_image( origin, point ) )

    def minimum_image( self, r1, r2 ):
        """
        Find the minimum image vector from point r1 to point r2.

        Args:
            r1 (np.array): fractional coordinates of point r1.
            r2 (np.array): fractional coordinates of point r2.

        Returns:
            (np.array): the fractional coordinate vector from r1 to the nearest image of r2.
        """
        delta_r = r2 - r1
        delta_r = np.array( [ x - math.copysign( 1.0, x ) if abs(x) > 0.5 else x for x in delta_r ] )
        return( delta_r )

    def minimum_image_dr( self, r1, r2, cutoff=None ):
        """
        Calculate the shortest distance between two points in the cell,
        accounting for periodic boundary conditions.

        Args:
            r1 (np.array): fractional coordinates of point r1.
            r2 (np.array): fractional coordinates of point r2.
            cutoff (:obj: `float`, optional): if set, return zero if the minimum distance is greater than `cutoff`. Defaults to None.

        Returns:
            (float): The distance between r1 and r2.
        """
        delta_r_vector = self.minimum_image( r1, r2 )
        return( self.dr( np.zeros( 3 ), delta_r_vector, cutoff ) )

    def lengths( self ):
        """
        The cell lengths.

        Args:
            None

        Returns:
            (np.array(a,b,c)): The cell lengths.
        """
        return( np.array( [ math.sqrt( sum( row**2 ) ) for row in self.matrix ] ) )

    def angles( self ):
        """
        The cell angles (in degrees).

        Args:
            None

        Returns:
            (list(alpha,beta,gamma)): The cell angles.
        """
        ( a, b, c ) = [ row for row in self.matrix ]
        return [ angle( b, c ), angle( a, c ), angle( a, b ) ]

    def cartesian_to_fractional_coordinates( self, coordinates ):
        """
        Convert a set of Cartesian coordinates to fractional coordinates in the cell.

        Args:
            coordinates (np.array(dim(N,3))): The set of Cartesian coordinates.

        Returns:
            (np.array(dim(N,3))): The corresponding set of fractional coordinates.
        """
        return( coordinates.dot( self.inv_matrix ) )

    def fractional_to_cartesian_coordinates( self, coordinates ):
        """
        Convert a set of fractional coordinates in the cell to Cartesian coordinates.

        Args:
            coordinates (np.array(dim(N,3))): The set of fractional coordinates.

        Returns:
            (np.array(dim(N,3))): The corresponding set of Cartesian coordinates.
        """
        return( coordinates.dot( self.matrix ) )

    def inside_cell( self, r ):
        """
        Given a fractional-coordinate, if this lies outside the cell return the equivalent point inside the cell.

        Args:
            r (np.array): Fractional coordinates of a point (this may be outside the cell boundaries).

        Returns:
            (np.array): Fractional coordinates of an equivalent point, inside the cell boundaries.
        """
        centre = np.array( [ 0.5, 0.5, 0.5 ] )
        new_r = self.nearest_image( centre, r )
        return new_r

    def volume( self ):
        """
        The cell volume.

        Args:
            None

        Returns:
            (float): The cell volume.
        """
        return np.dot( self.matrix[0], np.cross( self.matrix[1], self.matrix[2] ) )

    def unit_vectors( self ):
        """
        The unit vectors for the cell vectors.

        Args:
            None

        Returns:
            (np.array): The unit vectors for the cell vectors.
        """
        return( ( self.matrix.transpose() / self.lengths() ).transpose() )

    def rotate( self, axis, theta ):
        self.matrix = np.array( [ np.dot( rotation_matrix(axis,theta), v) for v in self.matrix ] )

max_stars_repo_name: cajfisher/vasppy

max_issues_repo_name: cajfisher/vasppy

max_forks_repo_name: cajfisher/vasppy