text:
# import face_recognition
import os
import time
from milvus import *
import numpy as np
import random
from faker import Faker

fake = Faker()
# milvus = Milvus()

milvus_collection = 'partition_query'

FILE_PATH = 'bigann_base.bvecs'

VEC_NUM = 10000000
BASE_LEN = 100000
NUM = VEC_NUM // BASE_LEN

VEC_DIM = 128

SERVER_ADDR = "0.0.0.0"
SERVER_PORT = 19530

def load_bvecs_data(fname,base_len,idx):
    begin_num = base_len * idx
    # print(fname, ": ", begin_num )
    x = np.memmap(fname, dtype='uint8', mode='r')
    d = x[:4].view('int32')[0]
    data =  x.reshape(-1, d + 4)[begin_num:(begin_num+base_len), 4:]
    data = (data + 0.5) / 255
    data = data.tolist()
    return data

def create_milvus_collection(milvus):
    if not milvus.has_collection(milvus_collection)[1]:
        param = {
            'collection_name': milvus_collection,
            'dimension': VEC_DIM,
            'index_file_size':1024,
            'metric_type':MetricType.L2
        }
        status = milvus.create_collection(param)
        print(status)
        build_collection(milvus)

def build_collection(milvus):
    index_param = { 'nlist': 16384}
    status = milvus.create_index(milvus_collection, IndexType.IVF_SQ8H, index_param)
    print(status)

def create_partition(partition_tag,milvus):
    milvus.create_partition(milvus_collection, partition_tag=partition_tag)

def get_partition_tag():
    partition_tag=[]
    count = 0
    while count<NUM:
        sex = random.choice(['female','male'])
        get_time = fake.date_between(start_date="-30d", end_date="today")
        is_glasses = random.choice(['True','False'])
        p_tag = str(get_time) + "/" + sex + "/" + str(is_glasses)
        if p_tag not in partition_tag:
            partition_tag.append(p_tag)
            count = count + 1
    print(partition_tag)
    return partition_tag

def add_vectors(vectors,vectors_ids,partition_tag,milvus):
    time_start = time.time()
    status, ids = milvus.insert(collection_name=milvus_collection, records=vectors, ids=vectors_ids, partition_tag=partition_tag)
    time_end = time.time()
    print(status, "insert milvue time: ", time_end-time_start)

def main():
    milvus = Milvus(host=SERVER_ADDR, port=SERVER_PORT)
    create_milvus_collection(milvus)
    partition_tag = get_partition_tag()
    count = 0
    while count < (VEC_NUM // BASE_LEN):
        vectors = load_bvecs_data(FILE_PATH,BASE_LEN,count)
        vectors_ids = [id for id in range(count*BASE_LEN,(count+1)*BASE_LEN)]
        create_partition(partition_tag[count],milvus)
        add_vectors(vectors,vectors_ids,partition_tag[count],milvus)

        count = count + 1

if __name__ == '__main__':
    main()

max_stars_repo_name: naetimus/bootcamp

max_issues_repo_name: naetimus/bootcamp

max_forks_repo_name: naetimus/bootcamp


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

# # Apriori

# ## Importing the libraries

# In[1]:

get_ipython().system('pip install apyori ')

# In[2]:

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

# ## Data Preprocessing

# In[3]:

dataset = pd.read_csv('Market_Cart_Optimisation.csv', header = None)
transactions = []
for i in range(0, 7501):
  transactions.append([str(dataset.values[i,j]) for j in range(0, 20)])
for i in range(0,10):
  print(transactions[i])

# ## Training the Apriori model on the dataset

# In[4]:

from apyori import apriori
rules = apriori(transactions = transactions, min_support = 0.003, min_confidence = 0.2, min_lift = 3, min_length = 2, max_length = 2)

# ## Visualising the results

# ### Displaying the first results coming directly from the output of the apriori function
#

# In[5]:

results = list(rules)
results

# ### Putting the results well organised into a Pandas DataFrame

# In[6]:

def inspect(results):
    lhs         = [tuple(result[2][0][0])[0] for result in results]
    rhs         = [tuple(result[2][0][1])[0] for result in results]
    supports    = [result[1] for result in results]
    confidences = [result[2][0][2] for result in results]
    lifts       = [result[2][0][3] for result in results]
    return list(zip(lhs, rhs, supports, confidences, lifts))
resultsinDataFrame = pd.DataFrame(inspect(results), columns = ['Left Hand Side', 'Right Hand Side', 'Support', 'Confidence', 'Lift'])

# ### Displaying the results

# In[7]:

resultsinDataFrame.nlargest(n = 10, columns = 'Lift')

# #Prediction system

# #Building Model

# In[8]:

import pandas as pd
from mlxtend.preprocessing import TransactionEncoder
from mlxtend.frequent_patterns import apriori
dataset=[['Bread','Milk','Chips'],
        ['Bread','Choclate','Eggs'],
        ['Milk','Choclate','Chips','Cola'],
        ['Bread','Milk','Choclate','Chips'],
        ['Bread','Milk','Cola']]
te=TransactionEncoder()
te=te.fit(dataset)
te_ary=te.transform(dataset)
df=pd.DataFrame(te_ary,columns=te.columns_)
market=set(te.columns_)
Freq_itenset=apriori(df,min_support=0.5,use_colnames=True)
from mlxtend.frequent_patterns import association_rules
rules=association_rules(Freq_itenset,metric='support',min_threshold=0.5)
rules[rules['support']==rules['support'].max()]

# ##Prediction program

# In[ ]:

print('------------------------------Cart Addition App------------------------------\nPRESS EXIT TO LEAVE')
print('Items which can be bought : %s\nEnter first item :'%(market))
i,item=1,''
cart=set()
sugg=set()
while i>0:
    item=input('Enter an item :')
    item=item[0].upper()+item[1:].lower()
    if item in cart:
        print('already in cart')
    elif item in market:
        item=set([item])
        cart|=item
        market-=item
        g=rules[rules['antecedents'].apply(lambda x:set(item).issubset(set(x)))]
        le=g['consequents'].index
        a=set()
        for o in le:
            t=set(list(g['consequents'][o])); a|=t
        a-=cart
        a-=item
        sugg=a
        print('*********************************************************************')
        print('Your cart consists of', cart)
        print('*********************************************************************')
        print('suggestions : %s'%(sugg))
        print('Remaining items in the market: %s'%(market))
        sugg=set()
        if len(market)==0:
            print('All items are taken')
            print('*********************************************************************')
            print(' final cart is : %s'%(cart))
            print('*********************************************************************')
            break
    elif item=='Exit':
        print('*********************************************************************')
        print('final cart is : %s'%(cart))
        print('*********************************************************************')
        break
    else:
        print('item is not available')

# In[ ]:

ext: py

max_stars_repo_path: Market Cart Optimization code.py

max_issues_repo_path: Market Cart Optimization code.py

max_forks_repo_path: Market Cart Optimization code.py

reason: import numpy


text:
#Code written by Rakshith Sathish
#The work is made public with MIT License

import numpy as np
import torch
import torch.nn as nn
from torch import optim
import tqdm
import time
from torch.utils import data
import os
import torch.nn
import torch.nn.functional as F
from torch.autograd import  Variable
import matplotlib.pyplot as plt
plt.switch_backend('agg')
from sklearn.metrics import confusion_matrix
from SUMNet_bn import SUMNet
from LUNA_loader import lunaLoader
import lovasz_losses as L

def dice_coefficient(pred, target):
	predC = torch.argmax(F.softmax(pred,dim=1),dim=1)
	c = confusion_matrix(target.view(-1).cpu().numpy(), predC.view(-1).cpu().numpy(),labels=[0,1])
	TP = np.diag(c)
	FP = c.sum(axis=0) - np.diag(c)
	FN = c.sum(axis=1) - np.diag(c)
	TN = c.sum() - (FP + FN + TP)
	return (TP,FP,FN)

savePath = 'Results/SUMNet_new/Adam_1e-4_ep100_CE+Lov/'
if not os.path.isdir(savePath):
	os.makedirs(savePath)

trainDset = lunaLoader(is_transform=True, split='train',img_size=256)
valDset = lunaLoader(is_transform=True, split='val',img_size=256)

trainDataLoader = data.DataLoader(trainDset,batch_size=16,shuffle=True,num_workers=4,pin_memory=True)
validDataLoader = data.DataLoader(valDset,batch_size=16,shuffle=False,num_workers=4,pin_memory=True)

n_classes = 2
net = SUMNet(in_ch=1,out_ch=n_classes)

use_gpu = torch.cuda.is_available()
if use_gpu:
	net = net.cuda()

optimizerS = optim.Adam(net.parameters(), lr = 1e-4, weight_decay = 1e-5)
criterionS = nn.CrossEntropyLoss()

epochs = 100
trainLoss = []
validLoss = []
trainDiceCoeff = []
validDiceCoeff = []
start = time.time()

bestValidDice = 0.0

for epoch in range(epochs):
	epochStart = time.time()
	trainRunningLoss = 0
	validRunningLoss = 0
	trainBatches = 0
	validBatches = 0

	train_tp = np.zeros(n_classes)
	train_fp = np.zeros(n_classes)
	train_fn = np.zeros(n_classes)

	val_tp = np.zeros(n_classes)
	val_fp = np.zeros(n_classes)
	val_fn = np.zeros(n_classes)

	net.train(True)
	for data1 in tqdm.tqdm(trainDataLoader):
		imgs, mask = data1
		# print(imgs.shape)
		if use_gpu:
			inputs = imgs.cuda()
			labels = mask.cuda()

		cpmap = net(Variable(inputs))
		cpmapD = F.softmax(cpmap,dim=1)

		LGce = criterionS(cpmap,labels.long())
		L_lov = L.lovasz_softmax(F.softmax(cpmap,dim=1),labels)
		LGseg = LGce+L_lov

		optimizerS.zero_grad()

		LGseg.backward()

		optimizerS.step()

		trainRunningLoss += LGseg.item()

		train_cf = dice_coefficient(cpmapD,labels)
		train_tp += train_cf[0]
		train_fp += train_cf[1]
		train_fn += train_cf[2]
		trainBatches += 1
		# break

	train_dice = (2*train_tp)/(2*train_tp + train_fp + train_fn )
	trainLoss.append(trainRunningLoss/trainBatches)
	trainDiceCoeff.append(train_dice)

	print("\n{}][{}]| LGseg: {:.4f} | "
		.format(epoch,epochs,LGseg.item()))

	with torch.no_grad():
		for data1 in tqdm.tqdm(validDataLoader):
			imgs, mask = data1
			if use_gpu:
				inputs = imgs.cuda()
				labels = mask.cuda()

			cpmap = net(Variable(inputs))
			cpmapD = F.softmax(cpmap.data,dim=1)

			val_cf = dice_coefficient(cpmapD,labels)
			val_tp += val_cf[0]
			val_fp += val_cf[1]
			val_fn += val_cf[2]
			validRunningLoss += LGseg.item()
			validBatches += 1
			# break

		val_dice = (2*val_tp)/(2*val_tp + val_fp + val_fn )
		validLoss.append(validRunningLoss/validBatches)
		validDiceCoeff.append(val_dice)
	# scheduler.step(validRunningLoss/validBatches)
	if (val_dice[1] > bestValidDice):
		bestValidDice = val_dice[1]
		torch.save(net.state_dict(), savePath+'sumnet_best.pt')

	plt.figure()
	plt.plot(range(len(trainLoss)),trainLoss,'-r',label='Train')
	plt.plot(range(len(validLoss)),validLoss,'-g',label='Valid')
	if epoch==0:
		plt.legend()
	plt.savefig(savePath+'LossPlot.png')
	plt.close()
	epochEnd = time.time()-epochStart
	print('Epoch: {:.0f}/{:.0f} | Train Loss: {:.3f} | Valid Loss: {:.3f}'\
		  .format(epoch+1, epochs, trainRunningLoss/trainBatches, validRunningLoss/validBatches))
	print('\nDice | Train  | BG {:.3f} | Nodule {:.3f} |\n Valid | BG: {:.3f} | Nodule {:.3f} |'
		  .format(train_dice[0],train_dice[1], val_dice[0], val_dice[1]))

	print('\nTime: {:.0f}m {:.0f}s'.format(epochEnd//60,epochEnd%60))
	trainLoss_np = np.array(trainLoss)
	validLoss_np = np.array(validLoss)
	trainDiceCoeff_np = np.array(trainDiceCoeff)
	validDiceCoeff_np = np.array(validDiceCoeff)

	print('Saving losses')

	torch.save(trainLoss_np, savePath+'trainLoss.pt')
	torch.save(validLoss_np, savePath+'validLoss.pt')
	torch.save(trainDiceCoeff_np, savePath+'trainDice.pt')
	torch.save(validDiceCoeff_np, savePath+'validDice.pt')
	# break

end = time.time()-start
print('Training completed in {:.0f}m {:.0f}s'.format(end//60,end%60))
plt.figure()
plt.plot(range(len(trainLoss)),trainLoss,'-r')
plt.plot(range(len(validLoss)),validLoss,'-g')
plt.title('Loss plot')
plt.savefig(savePath+'trainLossFinal.png')
plt.close()

trainDiceCoeff_bg = [x[0] for x in trainDiceCoeff]
trainDiceCoeff_nodule = [x[1] for x in trainDiceCoeff]
plt.figure()
plt.plot(range(len(trainDiceCoeff_bg)),trainDiceCoeff_bg,'-r',label='BG')
plt.plot(range(len(trainDiceCoeff_nodule)),trainDiceCoeff_nodule,'-g',label='Nodule')
plt.legend()
plt.title('Dice coefficient: Train')
plt.savefig(savePath+'trainDice.png')
plt.close()

validDiceCoeff_bg = [x[0] for x in validDiceCoeff]
validDiceCoeff_nodule = [x[1] for x in validDiceCoeff]
plt.figure()
plt.plot(range(len(validDiceCoeff_bg)),validDiceCoeff_bg,'-r',label='BG')
plt.plot(range(len(validDiceCoeff_nodule)),validDiceCoeff_nodule,'-g',label='Nodule')
plt.legend()
plt.title('Dice coefficient: Valid')
plt.savefig(savePath+'validDice.png')
plt.close()


text:
import datetime
import json

import matplotlib.pyplot as plt
import mysql.connector
import numpy as np
import requests
from mysql.connector import errorcode
from scipy.cluster.vq import kmeans2, whiten
import Timeframe

# Since downloaded dataset is in .csv file, create a new table in mysql database with the same field names,
# and load all the data into the created table

query = "CREATE TABLE trip_data1_2010(medallion VARCHAR(20),hack_license VARCHAR(20), vendor_id varchar(10),rate_code INT,store_and_fwd_flag varchar(5),pickup_datetime datetime, dropoff_datetime datetime,passenger_count INT,trip_time_in_secs INT,trip_distance DOUBLE,pickup_longitude DECIMAL(20,10),pickup_latitude DECIMAL(20,10), dropoff_longitude DECIMAL(20,10),dropoff_latitude DECIMAL(20,10))"
cursor.execute(query)
load_query = "LOAD DATA LOCAL INFILE 'E:/FOIL2013/trip_data_1/trip_data_1.csv'" \
    "INTO TABLE tripdata FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n' IGNORE 1 LINES"

jfk_query = "CREATE TABLE jfk_data_12 AS SELECT *, ( 3959 * acos ( cos ( radianss(40.645574) )" \
            " * cos( radians(pickup_latitude)) * cos( radians( pickup_longitude ) - radians(-73.784866))"\
            " + sin ( radians(40.645574)) * sin( radians( pickup_latitude))))" \
            "AS distance FROM trip_data_12 HAVING distance < 2 and passenger_count<=4"
cursor.execute(jfk_query)

# Implementation of K-means clustering and Trip Matching Algorithm
def kmeanscluster(starttime , endtime):
    global numpool

    # Connecting to MySQL Database where the database is stored using MySQL Connector package

    try:
        cnn = mysql.connector.connect(
            user='root',
            password='root',
            host='localhost',
            port=8889,
            database='pool')
        # print("it works")
    except mysql.connector.Error as e:
        if e.errno == errorcode.ER_ACCESS_DENIED_CHANGE_USER_ERROR:
            print("something in os wrong with username and password")
        elif e.errno == errorcode.ER_BAD_DB_ERROR:
            print("database doesn't exist")
        else:
            print(e)

    cursor = cnn.cursor()

    # This function is to select a particular pool's source and its destination points, Total
    # number of records and passenger count for each trip

    def selecteachpool():
        # This query is to select fields with filtering data for a particular pool.
        query = "Select medallion, dropoff_longitude, dropoff_latitude, passenger_count from jfk_data_12 WHERE pickup_datetime >= '" + str(starttime) + "'and pickup_datetime < '" + str(endtime) + "'"
        cursor.execute(query)
        result_set = cursor.fetchall()
        coord_list = []
        passenger_count = []
        for row in result_set:
            temp = [float(row[1]), float(row[2])]
            coord_list.append(temp)

        for row in result_set:
            passenger_count.append(int(row[3]))

        recordcount = 0
        for outerlist in coord_list:
            recordcount += 1
        return coord_list, passenger_count, recordcount

    # Returns the list of dropoff latitude and longitude points
    def getcoord_list():
        return coord_list

    # calculating value of k to run k-means clustering Algorithm
    def getkvalue(recordcount):
        kvalue = (int)(recordcount / 4)
        # print("No of records:", count)
        return kvalue

    # Run K-means clustering Algorithm using scipy-cluster package.
    def kmeanscluster(coord_list):
        coordinates = np.array(coord_list)
        return kmeans2(whiten(coordinates), kvalue, missing='warn',)

    # To plot the k-means clutering results in form of graph
    def plotkmeanscluster(coord_list, labels):
        coordinates = np.array(coord_list)
        plt.scatter(coordinates[:, 0], coordinates[:, 1], c=labels, iter = 5)
        plt.show()

    # To get the cluster points after running k-means clustering Algorithm
    def getclusterpoints(coord_list, labels, kvalue):
        clusterpt = [0 for x in range(kvalue)]
        for i in range(0, kvalue):
            clusterpt[i] = []
        listitem = 0
        for i in labels:
            clusterpt[i].append(coord_list[listitem])
            listitem = listitem + 1
        return clusterpt

    # Print those cluster points(dropoff latitudes and dropoff longitudes)
    def printclusterpoints(clusterpt, kvalue):
        print("\nkmeans clustering result \n")
        for i in range(0, kvalue):
            print("cluster number:", i, clusterpt[i])

    # After clustering is formed, merge trips with maximum capacity of 4
    def carassignment(clusterpt, kvalue):
        carassign = []
        carcount = -1
        for i in range(0, kvalue):
            for j in range(0, len(clusterpt[i])):
                if (j % 4 == 0):
                    carassign.append([])
                    carcount = carcount + 1
                    carassign[carcount].append(clusterpt[i][j])
                else:
                    carassign[carcount].append(clusterpt[i][j])
        return carassign, carcount + 1

    # Get the dropoff latitude and longitude points after we merged the trips
    def getcarassignment():
        return carassign, carcount

    # print trips after we merged the trips
    def printridesshared(carassign, carcount):
        print("\nRide shared cars and its destination\n")
        for i in range(0, carcount):
            print("car number:", i, carassign[i])

    coord_list, passenger_count, recordcount = selecteachpool()
    if (len(coord_list) < 4): # Skipping pools where the trips are too small or zero
        return
    kvalue = getkvalue(recordcount)
    meanpoints, labels = kmeanscluster(coord_list)
    # plotkmeanscluster(coord_list,labels)

    clusterpt = getclusterpoints(coord_list, labels, kvalue)
    # printclusterpoints(clusterpt, kvalue)
    carassign, carcount = carassignment(clusterpt, kvalue)
    # printridesshared(carassign, carcount)

    cnn.commit()
    cursor.close()
    cnn.close

    # To get distance and Time between two latitude and longitude points using graphhopper API

    def getDistance(plat,plong,dlat,dlong):
        requestString = 'http://localhost:8989/route?point=' + str(plat) + '%2C' + str(plong) + '&point=' + str(
            dlat) + '%2C' + str(dlong) + '&vehicle=car'
        r = requests.get(requestString)

        res = json.loads(r.text)

        return_list = []
        if ('paths' in res):
            return_list.append(res['paths'][0]['distance'])
            return_list.append(res['paths'][0]['time'])
            return return_list
        else:
            return_list.append(-250)
            return_list.append(-250)
            return return_list

    # To get the total travel distance before ridesharing
    def getwithoutridesharingdistance(coord_list):
        # Initialize some global and local variables
        global tnormaldist
        global wolength
        wolength = wolength + len(coord_list)
        normaldistance = 0
        # Calculate  distance between JFK and all the destination points
        for i in range(0, len(coord_list)):
            dist = getDistance(40.645574, -73.784866, coord_list[i][1], coord_list[i][0])
            normaldistance = normaldistance + dist[0]
        normaldistance = normaldistance/1000
        tnormaldist = tnormaldist + normaldistance # Sum up all the trip distances
        print("\nDistance without ride sharing:",normaldistance,"km")
        return normaldistance

    # To get the total travel distance After ridesharing
    def getwithridesharingdistance(carassign,carcount):
        # Initialize some global and local variables
        global tridesharedist
        global wlength
        wlength = wlength + len(carassign)
        ridesharedist = 0
        # Calculate  distance travelled by trips after ridesharing
        # For that, I took JFK as source and First destination point as destinaation
        # After first passenger dropped off, Keeping Previous Destination point as source and current destination
        # point as destination and repeat the process till all the passengers for a trip dropped off.
        for i in range(0,carcount):
            initlat = 40.645574
            initlog = -73.784866
            for j in range (0,len(carassign[i])):
                if(j!=0):
                    initlat = carassign[i][j-1][1]
                    initlog = carassign[i][j-1][0]
                distance = getDistance(initlat, initlog, carassign[i][j][1], carassign[i][j][0])
                ridesharedist = ridesharedist + distance[0]

        ridesharedist = ridesharedist/1000
        tridesharedist = tridesharedist + ridesharedist # Sum up all the trip distances
        print("Distance with ride sharing:",ridesharedist,"km")
        return ridesharedist

    # This function returns the distance saved in percentage
    def getsaveddistance(normaldistance,ridesharedist):
        global totaldistance
        distancesaved = (1-(ridesharedist/normaldistance))*100
        totaldistance = totaldistance + distancesaved

    normaldistance = getwithoutridesharingdistance(coord_list)
    ridesharedist = getwithridesharingdistance(carassign, carcount)
    getsaveddistance(normaldistance, ridesharedist)
    numpool = numpool + 1 # To calculate the total number of pools

# Initializing some global variables

starttime = Timeframe.starttime
endtime = Timeframe.endtime
numpool = 0
totaldistance = 0
tnormaldist = 0
tridesharedist = 0
wolength = 0
wlength = 0

programstarttime = datetime.datetime.now() # Record the system time before start of the Algorithm
print("\nAlgorithm start time:",programstarttime)

# Run the Algorithm for each pool window until the end time.

while endtime <= Timeframe.untildatetime:
    print("\npool : ",starttime, "-",endtime)
    kmeanscluster(starttime,endtime)
    starttime = starttime + Timeframe.windowsize
    endtime = endtime + Timeframe.windowsize

programendtime = datetime.datetime.now() # Record the system time After end of the Algorithm
print("\nAlgorithm End time:",programendtime)

# Calculate the difference between start and the end time
timeobj = programendtime - programstarttime

print ("Total time to run the Algorithm:", timeobj)
print("Average run time for each pool:",timeobj/numpool)

print ("\nTotal distance without ridesharing:",tnormaldist,"km")
print ("Total distance with ridesharing:",tridesharedist,"km")

print("\nNumber of pools:",numpool)

print ("\nAverage distance without ridesharing for eachpool:",tnormaldist/numpool,"km")
print ("Average distance with ridesharing for each pool",tridesharedist/numpool,"km")

reason: import numpy,from scipy


text:
import numpy as np
import equations as eq
import math
import sys

#def f1(xlist):
    #"""Solves x = 2y+z-s+sqrt(t)"""
    #return 2*x[1]+x[2]-x[3]+math.sqrt(x[4])

def f1(myList):
    """Solves x = Sqrt((120-y)/8)"""
    return math.sqrt((120-myList[1])/8)
    #return 120-8*myList[0]**2

def f2(myList):
    """Solves y = x^3-1"""
    return myList[0]**3-1
    #return (myList[1]+1)**(1/3)

def f3(mylist)

def f(fn,var):
    return fn(var)

def gauss(eqns,erdes=0.025):
    """
    'eqns' is the equation names. [eq1,eq2,eq3].
    'erdes' is the desired error.

    Example Input: gauss([f1,f2],0.01)
    """
    noFun = len(eqns)
    var = [3.14688]*noFun #[x,y,z]
    varNew = var[:]
    error = [10000]*noFun
    varDif=[3.14688]*noFun
    nHistory =[]
    count = 0

    while max(error) > erdes:
        for i in range(noFun):
            #solve each function
            varNew[i] = f(eqns[i],var)

        for i in range(noFun):
            error[i] = abs((varNew[i]-var[i])/varNew[i])
            varDif[i]=(abs(varNew[i]-var[i])/2)
            #print('i:',i,'varNew[i]:',varNew[i],'var[i]:',var[i])
            #print('varDif:',varDif)

        for i in range(noFun):
            if varDif[i]==max(varDif):
                n=i
                nHistory.append(varNew[n])
                #print('n:',n)

        #print('count:',count,'error:',error)
        count += 1
        var = varNew[:]
        if count == 6: #This Must always be an even Number. #It hasn't converged within 10 iterations. So, Is it diverging?
            var = [3.14688]*noFun #[x,y,z]
            varNew = var[:]
            halfLength = len(nHistory)/2
            firstHalf = 0
            secondHalf = 0
            for i in range(int(halfLength)):
                firstHalf = firstHalf + nHistory[i]
                secondHalf = secondHalf + nHistory[i+int(halfLength)]
            half1 = firstHalf/halfLength
            half2 = secondHalf/halfLength
            if abs(half1) < abs(half2):
                print('This function Diverges. Re-do.')
                sys.exit(0)
            else:
                print('It converges. I shall continue.')
    return varNew,count,error

max_stars_repo_path: GaussSidel/gauss 6.py

max_issues_repo_path: GaussSidel/gauss 6.py

max_forks_repo_path: GaussSidel/gauss 6.py


text:
import numpy as np
from streamad.evaluate import (
    NumentaAwareMetircs,
    PointAwareMetircs,
    SeriesAwareMetircs,
)

def test_point_aware_metrics():
    values_real = np.array([0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0])
    values_pred = np.array([0, 0, 0, None, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0])

    metric = PointAwareMetircs(anomaly_threshold=0.8)

    (precision, recall, f1,) = metric.evaluate(values_real, values_pred)
    assert 0.0 <= precision <= 1.0
    assert 0.0 <= recall <= 1.0
    assert 0.0 <= f1 <= 1.0

def test_series_aware_metrics():
    values_real = np.array([0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0])
    values_pred = np.array([0, 0, 0, None, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0])

    # Flat bias
    metric = SeriesAwareMetircs(
        anomaly_threshold=0.8, bias_p="flat", bias_r="flat"
    )

    (precision, recall, f1,) = metric.evaluate(values_real, values_pred)
    assert 0.0 <= precision <= 1.0
    assert 0.0 <= recall <= 1.0
    assert 0.0 <= f1 <= 1.0

    # Front bias
    metric = SeriesAwareMetircs(
        anomaly_threshold=0.8, bias_p="flat", bias_r="front"
    )

    (precision, recall, f1,) = metric.evaluate(values_real, values_pred)
    assert 0.0 <= precision <= 1.0
    assert 0.0 <= recall <= 1.0
    assert 0.0 <= f1 <= 1.0

    # Middle bias
    metric = SeriesAwareMetircs(
        anomaly_threshold=0.8, bias_p="flat", bias_r="middle"
    )

    (precision, recall, f1,) = metric.evaluate(values_real, values_pred)
    assert 0.0 <= precision <= 1.0
    assert 0.0 <= recall <= 1.0
    assert 0.0 <= f1 <= 1.0

    # Back bias
    metric = SeriesAwareMetircs(
        anomaly_threshold=0.8, bias_p="flat", bias_r="back"
    )

    (precision, recall, f1,) = metric.evaluate(values_real, values_pred)
    assert 0.0 <= precision <= 1.0
    assert 0.0 <= recall <= 1.0
    assert 0.0 <= f1 <= 1.0

def test_numenta_aware_metrics():
    values_real = np.array([0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0])
    values_pred = np.array([0, 0, 0, None, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0])

    metric = NumentaAwareMetircs(anomaly_threshold=0.8)

    (precision, recall, f1,) = metric.evaluate(values_real, values_pred)
    assert 0.0 <= precision <= 1.0
    assert 0.0 <= recall <= 1.0
    assert 0.0 <= f1 <= 1.0

max_stars_repo_name: Fengrui-Liu/streamingTS

max_issues_repo_name: Fengrui-Liu/streamingTS

max_forks_repo_name: Fengrui-Liu/streamingTS


text:
import tensorflow as tf
import numpy as np
import hyperchamber as hc

from hypergan.losses.base_loss import BaseLoss

TINY=1e-8
class FDivergenceLoss(BaseLoss):

    def _create(self, d_real, d_fake):
        gan = self.gan
        config = self.config

        gfx = None
        gfg = None

        pi = config.pi or 2

        g_loss_type = config.g_loss_type or 'gan'

        alpha = config.alpha or 0.5

        if config.type == 'kl':
            bounded_x = tf.minimum(tf.constant(np.exp(9), dtype=tf.float32), d_real)
            bounded_g = tf.minimum(10., d_fake)
            gfx = bounded_x
            gfg = bounded_g
        elif config.type == 'js':
            gfx = np.log(2) - tf.log(1+tf.exp(-d_real))
            gfg = np.log(2) - tf.log(1+tf.exp(-d_fake))
        elif config.type == 'js_weighted':
            gfx = -pi*np.log(pi) - tf.log(1+tf.exp(-d_real))
            gfg = -pi*np.log(pi) - tf.log(1+tf.exp(-d_fake))
        elif config.type == 'gan':
            gfx = -tf.log(1+tf.exp(-d_real))
            gfg = -tf.log(1+tf.exp(-d_fake))
        elif config.type == 'reverse_kl':
            gfx = -tf.exp(-d_real)
            gfg = -tf.exp(-d_fake)
        elif config.type == 'pearson' or config.type == 'jeffrey' or config.type == 'alpha2':
            gfx = d_real
            gfg = d_fake
        elif config.type == 'squared_hellinger':
            gfx = 1-tf.exp(-d_real)
            gfg = 1-tf.exp(-d_fake)
        elif config.type == 'neyman':
            gfx = 1-tf.exp(-d_real)
            gfx = tf.minimum(gfx, 1.9)
            gfg = 1-tf.exp(-d_fake)

        elif config.type == 'total_variation':
            gfx = 0.5*tf.nn.tanh(d_real)
            gfg = 0.5*tf.nn.tanh(d_fake)

        elif config.type == 'alpha1':
            gfx = 1./(1-alpha) - tf.log(1+tf.exp(-d_real))
            gfg = 1./(1-alpha) - tf.log(1+tf.exp(-d_fake))

        else:
            raise "Unknown type " + config.type

        conjugate = None

        if config.type == 'kl':
            conjugate = tf.exp(gfg-1)
        elif config.type == 'js':
            bounded = tf.minimum(tf.log(2.)-TINY, gfg)
            conjugate = -tf.log(2-tf.exp(bounded))
        elif config.type == 'js_weighted':
            c = -pi*np.log(pi)-TINY
            c = tf.constant(c, dtype=tf.float32)
            bounded = gfg#tf.maximum(gfg, c)
            conjugate = (1-pi)*tf.log((1-pi)/((1-pi)*tf.exp(bounded/pi)))
        elif config.type == 'gan':
            conjugate = -tf.log(1-tf.exp(gfg))
        elif config.type == 'reverse_kl':
            conjugate = -1-tf.log(-gfg)
        elif config.type == 'pearson':
            conjugate = 0.25 * tf.square(gfg)+gfg
        elif config.type == 'neyman':
            conjugate = 2 - 2 * tf.sqrt(tf.nn.relu(1-gfg)+1e-2)
        elif config.type == 'squared_hellinger':
            conjugate = gfg/(1.-gfg)
        elif config.type == 'jeffrey':
            raise "jeffrey conjugate not implemented"

        elif config.type == 'alpha2' or config.type == 'alpha1':
            bounded = gfg
            bounded = 1./alpha * (bounded * ( alpha - 1) + 1)
            conjugate = tf.pow(bounded, alpha/(alpha - 1.)) - 1. / alpha

        elif config.type == 'total_variation':
            conjugate = gfg
        else:
            raise "Unknown type " + config.type

        gf_threshold  = None # f' in the paper

        if config.type == 'kl':
            gf_threshold = 1
        elif config.type == 'js':
            gf_threshold = 0
        elif config.type == 'gan':
            gf_threshold = -np.log(2)
        elif config.type == 'reverse_kl':
            gf_threshold = -1
        elif config.type == 'pearson':
            gf_threshold = 0
        elif config.type == 'squared_hellinger':
            gf_threshold = 0

        self.gf_threshold=gf_threshold

        d_loss = -gfx+conjugate
        g_loss = -conjugate

        if g_loss_type == 'gan':
            g_loss = -conjugate
        elif g_loss_type == 'total_variation':
            # The inverse of derivative(1/2*x - 1)) = 0.5
            # so we use the -conjugate for now
            g_loss = -conjugate
        elif g_loss_type == 'js':
            # https://www.wolframalpha.com/input/?i=inverse+of+derivative(-(u%2B1)*log((1%2Bu)%2F2)%2Bu*log(u))
            g_loss = -tf.exp(d_fake)
        elif g_loss_type == 'js_weighted':
            # https://www.wolframalpha.com/input/?i=inverse+of+derivative(-(u%2B1)*log((1%2Bu)%2F2)%2Bu*log(u))
            p = pi
            u = d_fake
            #inner = (-4.*u*tf.exp(p/u) + tf.exp(2.)*tf.square(u)-2.*tf.exp(2.)*u+tf.exp(2.))/tf.square(u)
            #inner = tf.nn.relu(inner) + 1e-3
            #g_loss = (1.-u)/(2.*u) - tf.sqrt(inner)/(2.*tf.exp(1.))
            exp_bounded = p/u
            exp_bounded = tf.minimum(4., exp_bounded)
            inner = (-4.*u*tf.exp(exp_bounded) +np.exp(2.)*tf.square(u)-2.*np.exp(2.)*u+np.exp(2.))/tf.square(u)
            inner = tf.nn.relu(inner)
            u = tf.maximum(0.1,u)
            sqrt = tf.sqrt(inner+1e-2) / (2*np.exp(1))
            g_loss = (1.-u)/(2.*u)# + sqrt
        elif g_loss_type == 'pearson':
            g_loss = -(d_fake-2.0)/2.0
        elif g_loss_type == 'neyman':
            g_loss = 1./tf.sqrt(1-d_fake) # does not work, causes 'nan'
        elif g_loss_type == 'squared_hellinger':
            g_loss = -1.0/(tf.square(d_fake-1)+1e-2)
        elif g_loss_type == 'reverse_kl':
            g_loss = -d_fake
        elif g_loss_type == 'kl':
            g_loss = -gfg * tf.exp(gfg)
        elif g_loss_type == 'alpha1':
            a = alpha
            bounded = d_fake
            g_loss = (1.0/(a*(a-1))) * (tf.exp(a*bounded) - 1 - a*(tf.exp(bounded) - 1))
        elif g_loss_type == 'alpha2':
            a = alpha
            bounded = tf.minimum(d_fake, 4.)
            g_loss = -(1.0/(a*(a-1))) * (tf.exp(a*bounded) - 1 - a*(tf.exp(bounded) - 1))
        else:
            raise "Unknown g_loss_type " + config.type

        self.gfg = gfg
        self.gfx = gfx

        self.d_real = d_real
        self.d_fake = d_fake

        return [d_loss, g_loss]

    def g_regularizers(self):
        regularizer = None
        config = self.config
        pi = config.pi or 2
        alpha = config.alpha or 0.5

        ddfc = 0

        if config.regularizer is None:
            return []
        if config.regularizer == 'kl':
            # https://www.wolframalpha.com/input/?i=derivative(derivative(exp(t-1)))
            bounded = tf.minimum(4., self.gfg)
            ddfc = tf.exp(bounded - 1)
        elif config.regularizer == 'js':
            # https://www.wolframalpha.com/input/?i=derivative(derivative(-log(2-exp(t))))
            ddfc = -(2*tf.exp(self.gfg)) / (tf.square(2-tf.exp(self.gfg))+1e-2)
        elif config.regularizer == 'js_weighted':
            # https://www.wolframalpha.com/input/?i=derivative(derivative((1-C)*log(((1-C)%2F(1-C*exp(x%2FC)))))
            ddfc = -((pi-1)*tf.exp(self.gfg/pi))/(pi*tf.square(pi*tf.exp(self.gfg/pi)-1))
        elif config.regularizer == 'gan':
            # https://www.wolframalpha.com/input/?i=derivative(derivative(-log(1-exp(t))))
            ddfc = (2*tf.exp(self.gfg)) / (tf.square(1-tf.exp(self.gfg))+1e-2)
        elif config.regularizer == 'reverse_kl':
            # https://www.wolframalpha.com/input/?i=derivative(derivative(-1-log(-x)))
            ddfc = 1.0/tf.square(self.gfg)
        elif config.regularizer == 'pearson':
            # https://www.wolframalpha.com/input/?i=derivative(derivative(0.25*x*x+%2B+x))
            ddfc = 0.5
        elif config.regularizer == 'jeffrey':
            raise "jeffrey regularizer not implemented"
        elif config.regularizer == 'squared_hellinger':
            # https://www.wolframalpha.com/input/?i=derivative(derivative(t%2F(1-t)))
            ddfc = 2 / (tf.pow(self.gfg - 1, 3)+1e-2)
            #ddfc = 0
        elif config.regularizer == 'neyman':
            # https://www.wolframalpha.com/input/?i=derivative(derivative(2-2*sqrt(1-t)))
            ddfc = 1.0/(2*tf.pow(1-self.gfg, 3/2))
        elif config.regularizer == 'total_variation':
            # https://www.wolframalpha.com/input/?i=derivative(derivative(t))
            ddfc = 0
        elif config.regularizer == 'alpha1' or config.regularizer == 'alpha2':
            # https://www.wolframalpha.com/input/?i=derivative(derivative(1%2FC*(x*(C-1)%2B1)%5E(C%2FC-1)-1%2FC))
            ddfc = -tf.pow((alpha - 1)*self.gfg+1, (1/(alpha-1)-1))
        regularizer = ddfc * tf.nn.l2_normalize(self.gfg, [0]) * (config.regularizer_lambda or 1)
        self.metrics['fgan_regularizer'] = self.gan.ops.squash(regularizer)
        return [regularizer ]

    def d_regularizers(self):
        return []

max_issues_repo_name: KonradLinkowski/HyperGAN

max_forks_repo_name: KonradLinkowski/HyperGAN


text:
import numpy as np

from ._Epsilon import Epsilon

class ReinforcementComparison(Epsilon):
    """
    Agente que soluciona el problema del el Bandido Multibrazo
    (Multi-Armed Bandit) mediante el uso del algoritmo de
    comparación de refuerzo (reinforcement comparison)

    Parámetros
    ----------
    bandits : array of Bandit
        Vector con los bandidos con los que se debe jugar
    alpha : float
        Parámetro con el peso a usar para la actualización de la recompensa
    beta : floar
        Parámetro entre 0 y 1 que representa la tasa de aprendizaje

    Métodos
    -------
    run :
        Realiza una serie de tiradas con los bandidos seleccionados
        por el algoritmo
    update:
        Actualiza los valores adicionales después de una tirada
    select :
        Selecciona un bandido para jugar en la próxima tirada
    average_reward :
        Obtención de la recompensa promedio
    plot :
        Representación gráfica del histórico de tiradas

    References
    ----------
    Richard S. Sutton and Andrew G. Barto. "Reinfocement Learning: An
    Introduction". MIT Press, 1998.
    """

    def __init__(self, bandits, alpha=0.001, beta=0.1):
        self.alpha = alpha
        self.beta = beta

        self._pi = [0] * len(bandits)
        self._r = [0] * len(bandits)

        super(ReinforcementComparison, self).__init__(bandits)

    def update(self, bandit, reward):
        self._r[bandit] = (1 - self.alpha) * self._r[bandit] + self.alpha * reward
        self._pi[bandit] += self.beta * (reward - self._r[bandit])

    def select(self):
        # Calculo de la probabilidad de seleccionar un bandido
        prob = np.exp(self._pi)
        prob /= np.sum(prob)
        prob = np.cumsum(prob)

        # Selección del bandido
        return np.where(prob > np.random.random())[0][0]

max_stars_repo_name: analyticslane/mablane

max_issues_repo_name: analyticslane/mablane

max_forks_repo_name: analyticslane/mablane


text:
import pdb
import scipy.io
import math
import numpy as np
import torch
import scipy.stats
import matplotlib.pyplot as plt
import myMath.utils
import stats.gaussianProcesses.eval

def getCIFs(C, d, latents):
    nTrials = latents.shape[0]
    nLatents = latents.shape[2]
    nSamples = latents.shape[1]
    nNeurons = C.shape[0]
    embeddings = torch.empty((nTrials, nSamples, nNeurons))
    for r in range(nTrials):
        embeddings[r,:,:] = torch.matmul(latents[r,:,:], torch.transpose(C, 0, 1))+d[:,0]
    CIFs = torch.exp(embeddings)
    return(CIFs)

def computeSpikeRates(trialsTimes, spikesTimes):
    nTrials = len(spikesTimes)
    nNeurons = len(spikesTimes[0])
    spikesRates = torch.empty((nTrials,nNeurons))
    for r in range(nTrials):
        trialDuration = torch.max(trialsTimes[r])-torch.min(trialsTimes[r])
        for n in range(nNeurons):
            spikesRates[r,n] = len(spikesTimes[r][n])/trialDuration
    return spikesRates

def saveDataForMatlabEstimations(qMu0, qSVec0, qSDiag0, C0, d0,
                                 indPointsLocs0,
                                 legQuadPoints, legQuadWeights,
                                 kernelsTypes, kernelsParams0,
                                 spikesTimes,
                                 indPointsLocsKMSRegEpsilon, trialsLengths,
                                 emMaxIter, eStepMaxIter, mStepEmbeddingMaxIter,
                                 mStepKernelsMaxIter, mStepIndPointsMaxIter,
                                 saveFilename):
    nTrials = len(spikesTimes)
    nNeurons = len(spikesTimes[0])
    nLatents = len(qMu0)
    # indPointsLocsKMSEpsilon = np.array(indPointsLocsKMSEpsilon)
    mdict = dict(nTrials=nTrials, nNeurons=nNeurons, nLatents=nLatents,
                 C0=C0.numpy(), d0=torch.reshape(input=d0, shape=(-1,1)).numpy(),
                 legQuadPoints=legQuadPoints.numpy(),
                 legQuadWeights=legQuadWeights.numpy(),
                 indPointsLocsKMSRegEpsilon=indPointsLocsKMSRegEpsilon,
                 trialsLengths=trialsLengths,
                 emMaxIter=emMaxIter, eStepMaxIter=eStepMaxIter,
                 mStepEmbeddingMaxIter=mStepEmbeddingMaxIter,
                 mStepKernelsMaxIter=mStepKernelsMaxIter,
                 mStepIndPointsMaxIter=mStepIndPointsMaxIter)
    for k in range(nLatents):
        mdict.update({"kernelType_{:d}".format(k): kernelsTypes[k]})
        mdict.update({"qMu0_{:d}".format(k): qMu0[k].numpy().astype(np.float64)})
        mdict.update({"qSVec0_{:d}".format(k): qSVec0[k].numpy().astype(np.float64)})
        mdict.update({"qSDiag0_{:d}".format(k): qSDiag0[k].numpy().astype(np.float64)})
        mdict.update({"kernelsParams0_{:d}".format(k): kernelsParams0[k].numpy().astype(np.float64)})
        mdict.update({"indPointsLocs0_{:d}".format(k): indPointsLocs0[k].numpy().astype(np.float64)})
        mdict.update({"qMu0_{:d}".format(k): qMu0[k].numpy().astype(np.float64)})
        mdict.update({"qSVec0_{:d}".format(k): qSVec0[k].numpy().astype(np.float64)})
        mdict.update({"qSDiag0_{:d}".format(k): qSDiag0[k].numpy().astype(np.float64)})
    for r in range(nTrials):
        for n in range(nNeurons):
            mdict.update({"spikesTimes_{:d}_{:d}".format(r, n): spikesTimes[r][n].numpy().astype(np.float64)})
    scipy.io.savemat(file_name=saveFilename, mdict=mdict)

def getSRQSigmaFromVec(vec, nIndPoints):
    srQSigma = torch.zeros((nIndPoints, nIndPoints), dtype=torch.double)
    trilIndices = torch.tril_indices(nIndPoints, nIndPoints)
    srQSigma[trilIndices[0,:],trilIndices[1,:]] = vec
    return srQSigma

def buildQSigmasFromSRQSigmaVecs(srQSigmaVecs):
    K = len(srQSigmaVecs)
    R = srQSigmaVecs[0].shape[0]
    qSigmas = [[None] for k in range(K)]
    for k in range(K):
        # srQSigmaVecs[k] \in nTrials x Pk x 1
        # Pk = ((nIndPointsK+1)*nIndPointsK)/2
        # nIndPointsK = (-1+sqrt(1+8*Pk))/2
        Pk = srQSigmaVecs[k].shape[1]
        nIndPointsK = int((-1+math.sqrt(1+8*Pk))/2)
        qSigmas[k] = torch.empty((R, nIndPointsK, nIndPointsK), dtype=torch.double)
        for r in range(R):
            qSRSigmaKR = getSRQSigmaFromVec(vec=srQSigmaVecs[k][r,:,0], nIndPoints=nIndPointsK)
            qSigmas[k][r,:,:] = torch.matmul(qSRSigmaKR, torch.transpose(qSRSigmaKR, 0, 1))
    return(qSigmas)

def getQSVecsAndQSDiagsFromQSRSigmaVecs(srQSigmaVecs):
    # srQSigmaVecs[k] \in nTrial x Pk
    nLatents = len(srQSigmaVecs)
    nTrials = srQSigmaVecs[0].shape[0]
    qSVec = [[] for k in range(nLatents)]
    qSDiag = [[] for k in range(nLatents)]
    for k in range(nLatents):
        Pk = srQSigmaVecs[k].shape[1]
        nIndPointsK = int((-1.0+math.sqrt(1+8*Pk))/2.0)
        qSVec[k] = torch.empty(nTrials, nIndPointsK, 1, dtype=torch.double)
        qSDiag[k] = torch.empty(nTrials, nIndPointsK, 1, dtype=torch.double)
        for r in range(nTrials):
            qSRSigmaKR = getSRQSigmaFromVec(vec=srQSigmaVecs[k][r,:,0], nIndPoints=nIndPointsK)
            qSigmaKR = torch.matmul(qSRSigmaKR, torch.transpose(qSRSigmaKR, 0, 1))
            qSDiagKR = torch.diag(qSigmaKR)
            qSigmaKR = qSigmaKR - torch.diag(qSDiagKR)
            eValKR, eVecKR = torch.eig(qSigmaKR, eigenvectors=True)
            maxEvalIKR = torch.argmax(eValKR, dim=0)[0]
            qSVecKR = eVecKR[:,maxEvalIKR]*torch.sqrt(eValKR[maxEvalIKR,0])
            qSVec[k][r,:,0] = qSVecKR
            qSDiag[k][r,:,0] = qSDiagKR
    return qSVec, qSDiag

def clock(func):
    def clocked(*args,**kargs):
        t0 = time.perf_counter()
        result = func(*args,**kargs)
        elapsed = time.perf_counter()-t0
        name = func.__name__
        if len(args)>0:
            arg_str = ', '.join(repr(arg) for arg in args)
        else:
            arg_str = None
        if len(kargs)>0:
            keys = kargs.keys()
            values = kargs.values()
            karg_str = ', '.join(key + "=" + repr(value) for key in keys for value in values)
        else:
            karg_str = None
        if arg_str is not None and karg_str is not None:
            print('[%0.8fs] %s(%s,%s) -> %r' % (elapsed, name, arg_str, karg_str, result))
        elif arg_str is not None:
            print('[%0.8fs] %s(%s) -> %r' % (elapsed, name, arg_str, result))
        elif karg_str is not None:
            print('[%0.8fs] %s(%s) -> %r' % (elapsed, name, karg_str, result))
        else:
            print('[%0.8fs] %s() -> %r' % (elapsed, name, result))
        return result
    return clocked

def chol3D(K):
    # begin debug
#     print("Waning: debug code on in miscUtils.py:chol3D")
#     nTrial = K.shape[0]
#     for r in range(nTrial):
#         Kr = torch.matmul(K[r,:,:], torch.transpose(K[r,:,:], 0, 1))
#         eigRes = torch.eig(Kr)
#         cNum = eigRes.eigenvalues[0,0]/eigRes.eigenvalues[-1,0]
#         print("Condition number for trial {:d}: {:f}".format(r, cNum))
    # pdb.set_trace()
    # end debug
    Kchol = torch.zeros(K.shape, dtype=K.dtype, device=K.device)
    nTrial = K.shape[0]
    for i in range(nTrial):
        Kchol[i,:,:] = torch.cholesky(K[i,:,:])
    return Kchol

def getLegQuadPointsAndWeights(nQuad, trialsLengths, dtype=torch.double):
    nTrials = len(trialsLengths)
    legQuadPoints = torch.empty((nTrials, nQuad, 1), dtype=dtype)
    legQuadWeights = torch.empty((nTrials, nQuad, 1), dtype=dtype)
    for r in range(nTrials):
        legQuadPoints[r,:,0], legQuadWeights[r,:,0] = myMath.utils.leggaussVarLimits(n=nQuad, a=0, b=trialsLengths[r])
    return legQuadPoints, legQuadWeights

def getTrialsTimes(trialsLengths, dt):
    nTrials = len(trialsLengths)
    trialsTimes = [[] for r in range(nTrials)]
    for r in range(nTrials):
        trialsTimes[r] = torch.linspace(0, trialsLengths[r], round(trialsLengths[r]/dt))
    return trialsTimes

def getLatentsMeansAndSTDs(meansFuncs, kernels, trialsTimes):
    nTrials = len(trialsTimes)
    nLatents = len(kernels)
    latentsMeans = [[] for r in range(nTrials)]
    latentsSTDs = [[] for r in range(nTrials)]

    for r in range(nTrials):
        latentsMeans[r] = torch.empty((nLatents, len(trialsTimes[r])))
        latentsSTDs[r] = torch.empty((nLatents, len(trialsTimes[r])))
        for k in range(nLatents):
            gp = stats.gaussianProcesses.eval.GaussianProcess(mean=meansFuncs[k], kernel=kernels[k])
            latentsMeans[r][k,:] = gp.mean(t=trialsTimes[r])
            latentsSTDs[r][k,:] = gp.std(t=trialsTimes[r])
    return latentsMeans, latentsSTDs

def getLatentsSTDs(kernels, trialsTimes):
    nTrials = len(trialsTimes)
    nLatents = len(kernels)
    latentsSTDs = [[] for r in range(nTrials)]

    for r in range(nTrials):
        latentsSTDs[r] = torch.empty((nLatents, len(trialsTimes[r])))
        for k in range(nLatents):
            latentsSTDs[r][k,:] = kernels[k].buildKernelMatrixDiag(X=trialsTimes[r]).sqrt()
            pdb.set_trace()
    return latentsSTDs

# def getLatentsMeanFuncsSamples(latentsMeansFuncs, trialsTimes, dtype):
#     nTrials = len(latentsMeansFuncs)
#     nLatents = len(latentsMeansFuncs[0])
#     latentsMeansFuncsSamples = [[] for r in range(nTrials)]
#     for r in range(nTrials):
#         latentsMeansFuncsSamples[r] = torch.empty((nLatents, len(trialsTimes[r])), dtype=dtype)
#         for k in range(nLatents):
#             latentsMeansFuncsSamples[r][k,:] = latentsMeansFuncs[r][k](t=trialsTimes[r])

def getLatentsSamplesMeansAndSTDsFromSampledMeans(nTrials, sampledMeans, kernels, trialsTimes, latentsGPRegularizationEpsilon, dtype):
    nLatents = len(kernels)
    latentsSamples = [[] for r in range(nTrials)]
    latentsMeans = [[] for r in range(nTrials)]
    latentsSTDs = [[] for r in range(nTrials)]

    for r in range(nTrials):
        latentsSamples[r] = torch.empty((nLatents, len(trialsTimes[r])), dtype=dtype)
        latentsMeans[r] = torch.empty((nLatents, len(trialsTimes[r])), dtype=dtype)
        latentsSTDs[r] = torch.empty((nLatents, len(trialsTimes[r])), dtype=dtype)
        for k in range(nLatents):
            print("Procesing trial {:d} and latent {:d}".format(r+1, k+1))
            mean = sampledMeans[r,:,k]
            cov = kernels[k].buildKernelMatrix(trialsTimes[r])
            cov = cov + latentsGPRegularizationEpsilon*torch.eye(cov.shape[0])
            std = torch.diag(cov).sqrt()
            mn = scipy.stats.multivariate_normal(mean=mean, cov=cov)
            sample = torch.from_numpy(mn.rvs())
            latentsSamples[r][k,:] = sample
            latentsMeans[r][k,:] = mean
            latentsSTDs[r][k,:] = std
            plt.plot(trialsTimes[r], mean, label="mean")
            plt.plot(trialsTimes[r], sample, label="sample")
            plt.xlabel("Time (sec)")
            plt.ylabel("Value")
            plt.title("Latent {:d}".format(k))
            plt.legend()
            plt.show()
    return latentsSamples, latentsMeans, latentsSTDs

max_stars_repo_name: joacorapela/svGPFA

max_issues_repo_name: joacorapela/svGPFA

max_forks_repo_name: joacorapela/svGPFA

reason: import numpy,import scipy


text:
#!/usr/bin/env python3

"""
Copyright (c) Facebook, Inc. and its affiliates.

Plots of deviation of a subpop. from the full pop., both cumulative and classic

*
This implementation considers only responses r that are restricted to taking
values 0 or 1 (Bernoulli variates).
*

Functions
---------
cumulative
    Cumulative difference between observations from a subpop. & the full pop.
equiscore
    Reliability diagram with roughly equispaced average scores over bins
equisamps
    Reliability diagram with an equal number of observations per bin
exactplot
    Reliability diagram with exact values plotted

This source code is licensed under the MIT license found in the LICENSE file in
the root directory of this source tree.
"""

import math
import os
import subprocess
import random
import numpy as np
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt

def cumulative(r, s, inds, majorticks, minorticks, filename='cumulative.pdf',
               title='subpop. deviation is the slope as a function of $k/n$',
               fraction=1):
    """
    Cumulative difference between observations from a subpop. & the full pop.

    Saves a plot of the difference between the normalized cumulative sums of r
    for the subpopulation indices inds and the normalized cumulative sums of r
    from the full population interpolated to the subpopulation indices,
    with majorticks major ticks and minorticks minor ticks on the lower axis,
    labeling the major ticks with the corresponding values from s.

    Parameters
    ----------
    r : array_like
        class labels (0 for incorrect and 1 for correct classification)
    s : array_like
        scores (must be unique and in strictly increasing order)
    inds : array_like
        indices of the subset within s that defines the subpopulation
        (must be unique and in strictly increasing order)
    majorticks : int
        number of major ticks on the lower axis
    minorticks : int
        number of minor ticks on the lower axis
    filename : string, optional
        name of the file in which to save the plot
    title : string, optional
        title of the plot
    fraction : float, optional
        proportion of the full horizontal axis to display

    Returns
    -------
    float
        Kuiper statistic
    float
        Kolmogorov-Smirnov statistic
    float
        quarter of the full height of the isosceles triangle
        at the origin in the plot
    """

    def histcounts(nbins, x):
        # Counts the number of entries of x
        # falling into each of nbins equispaced bins.
        j = 0
        nbin = np.zeros(nbins)
        for k in range(len(x)):
            if x[k] > x[-1] * (j + 1) / nbins:
                j += 1
            if j == nbins:
                break
            nbin[j] += 1
        return nbin

    def aggregate(r, s, inds):
        # Counts the fraction that are 1s of entries of r in a bin
        # around each entry of s corresponding to the subset of s
        # specified by the indices inds. The bin ranges from halfway
        # to the nearest entry of s from inds on the left to halfway
        # to the nearest entry of s from inds on the right.
        ss = s[inds]
        q = np.insert(np.append(ss, [1e20]), 0, [-1e20])
        t = np.asarray([(q[k] + q[k + 1]) / 2 for k in range(len(q) - 1)])
        rc = np.zeros((len(inds)))
        sc = np.zeros((len(inds)))
        j = 0
        for k in range(len(s)):
            if s[k] > t[j + 1]:
                j += 1
                if j == len(inds):
                    break
            if s[k] >= t[0]:
                sc[j] += 1
                rc[j] += r[k]
        return rc / sc

    assert all(s[k] < s[k + 1] for k in range(len(s) - 1))
    assert all(inds[k] < inds[k + 1] for k in range(len(inds) - 1))
    assert len(r) == len(s)
    plt.figure()
    ax = plt.axes()
    # Aggregate r according to inds and s.
    rt = aggregate(r, s, inds)
    # Subsample r and s.
    rs = r[inds]
    ss = s[inds]
    # Accumulate and normalize.
    f = np.cumsum(rs) / int(len(rs) * fraction)
    ft = np.cumsum(rt) / int(len(rt) * fraction)
    # Plot the difference.
    plt.plot(np.insert(f - ft, 0, [0])[:(int(len(f) * fraction) + 1)], 'k')
    # Make sure the plot includes the origin.
    plt.plot(0, 'k')
    # Add an indicator of the scale of 1/sqrt(n) to the vertical axis.
    rtsub = rt[:int(len(rt) * fraction)]
    lenscale = np.sqrt(np.sum(rtsub * (1 - rtsub))) / len(rtsub)
    plt.plot(2 * lenscale, 'k')
    plt.plot(-2 * lenscale, 'k')
    kwargs = {
        'head_length': 2 * lenscale, 'head_width': len(rtsub) / 20, 'width': 0,
        'linewidth': 0, 'length_includes_head': True, 'color': 'k'}
    plt.arrow(.1e-100, -2 * lenscale, 0, 4 * lenscale, shape='left', **kwargs)
    plt.arrow(.1e-100, 2 * lenscale, 0, -4 * lenscale, shape='right', **kwargs)
    plt.margins(x=0, y=0)
    # Label the major ticks of the lower axis with the values of ss.
    sl = [
        '{:.2f}'.format(x)
        for x in np.insert(
            ss[:len(rtsub)], 0, [0])[::(len(rtsub) // majorticks)].tolist()]
    plt.xticks(
        np.arange(majorticks) * len(rtsub) // majorticks, sl[:majorticks])
    if len(rtsub) >= 300 and minorticks >= 50:
        # Indicate the distribution of s via unlabeled minor ticks.
        plt.minorticks_on()
        ax.tick_params(which='minor', axis='x')
        ax.tick_params(which='minor', axis='y', left=False)
        ax.set_xticks(np.cumsum(histcounts(minorticks, ss[:len(rtsub)])),
                      minor=True)
    # Label the axes.
    plt.xlabel('$S_{i_k}$ (the subscript on $S$ is $i_k$)')
    plt.ylabel('$F_k - \\tilde{F}_k$')
    plt.twiny()
    plt.xlabel('$k/n$')
    # Title the plot.
    plt.title(title)
    # Clean up the whitespace in the plot.
    plt.tight_layout()
    # Save the plot.
    plt.savefig(filename, bbox_inches='tight')
    plt.close()
    # Calculate summary statistics.
    fft = np.insert(f - ft, 0, [0])[:(int(len(f) * fraction) + 1)]
    kuiper = np.max(fft) - np.min(fft)
    kolmogorov_smirnov = np.max(np.abs(fft))
    return kuiper, kolmogorov_smirnov, lenscale

def equiscore(r, s, inds, nbins, filename='equiscore.pdf'):
    """
    Reliability diagram with roughly equispaced average scores over bins

    Plots a reliability diagram with roughly equispaced average scores
    for the bins, for both the full population and the subpopulation specified
    by the indices inds.

    Parameters
    ----------
    r : array_like
        class labels (0 for incorrect and 1 for correct classification)
    s : array_like
        scores (must be in non-decreasing order)
    inds : array_like
        indices of the subset within s that defines the subpopulation
        (must be unique and in strictly increasing order)
    nbins : int
        number of bins
    filename : string, optional
        name of the file in which to save the plot

    Returns
    -------
    None
    """

    def bintwo(nbins, a, b, q, qmax):
        # Counts the number of entries of q falling into each of nbins bins,
        # and calculates the averages per bin of the arrays a and b,
        # returning np.nan as the "average" for any bin that is empty.
        j = 0
        bina = np.zeros(nbins)
        binb = np.zeros(nbins)
        nbin = np.zeros(nbins)
        for k in range(len(q)):
            if q[k] > qmax * (j + 1) / nbins:
                j += 1
            if j == nbins:
                break
            bina[j] += a[k]
            binb[j] += b[k]
            nbin[j] += 1
        # Normalize the sum for each bin to compute the arithmetic average.
        bina = np.divide(bina, nbin, where=nbin != 0)
        bina[np.where(nbin == 0)] = np.nan
        binb = np.divide(binb, nbin, where=nbin != 0)
        binb[np.where(nbin == 0)] = np.nan
        return nbin, bina, binb

    assert all(s[k] <= s[k + 1] for k in range(len(s) - 1))
    assert all(inds[k] < inds[k + 1] for k in range(len(inds) - 1))
    plt.figure()
    _, binr, bins = bintwo(nbins, r, s, s, s[inds[-1]])
    _, binrs, binss = bintwo(nbins, r[inds], s[inds], s[inds], s[inds[-1]])
    plt.plot(bins, binr, '*:', color='gray')
    plt.plot(binss, binrs, '*:', color='black')
    plt.xlim((0, s[inds[-1]]))
    plt.ylim((0, 1))
    plt.xlabel('average of $S_k$ for $k$ in the bin')
    plt.ylabel('average of $R_k$ for $k$ in the bin')
    plt.title('reliability diagram')
    plt.tight_layout()
    plt.savefig(filename, bbox_inches='tight')
    plt.close()

def equisamps(
        r, s, inds, nbins, filename='equisamps.pdf',
        title='reliability diagram (equal number of subpop. scores per bin)'):
    """
    Reliability diagram with an equal number of observations per bin

    Plots a reliability diagram with an equal number of observations per bin,
    for both the full population and the subpop. specified by indices inds.

    Parameters
    ----------
    r : array_like
        class labels (0 for incorrect and 1 for correct classification)
    s : array_like
        scores (must be in non-decreasing order)
    inds : array_like
        indices of the subset within s that defines the subpopulation
        (must be unique and in strictly increasing order)
    nbins : int
        number of bins
    filename : string, optional
        name of the file in which to save the plot
    title : string, optional
        title of the plot

    Returns
    -------
    None
    """

    def hist(a, nbins):
        # Calculates the average of a in nbins bins,
        # each containing len(a) // nbins entries of a
        ns = len(a) // nbins
        return np.sum(np.reshape(a[:nbins * ns], (nbins, ns)), axis=1) / ns

    assert all(s[k] <= s[k + 1] for k in range(len(s) - 1))
    assert all(inds[k] < inds[k + 1] for k in range(len(inds) - 1))
    plt.figure()
    binr = hist(r, nbins)
    bins = hist(s, nbins)
    plt.plot(bins, binr, '*:', color='gray')
    rs = r[inds]
    ss = s[inds]
    binrs = hist(rs, nbins)
    binss = hist(ss, nbins)
    plt.plot(binss, binrs, '*:', color='black')
    plt.xlim((0, max(np.max(bins), np.max(binss))))
    plt.ylim((0, 1))
    plt.xlabel('average of $S_k$ for $k$ in the bin')
    plt.ylabel('average of $R_k$ for $k$ in the bin')
    plt.title(title)
    plt.tight_layout()
    plt.savefig(filename, bbox_inches='tight')
    plt.close()

def exactplot(r, s, inds, filename='exact.pdf', title='exact expectations'):
    """
    Reliability diagram with exact values plotted

    Plots a reliability diagram at full resolution with fractional numbers,
    for both the full population and the subpop. specified by indices inds.
    The entries of r should be the expected values of class labels,
    not necessarily just 0s and 1s.

    Parameters
    ----------
    r : array_like
        expected value of class labels
    s : array_like
        scores (must be in non-decreasing order)
    inds : array_like
        indices of the subset within s that defines the subpopulation
        (must be unique and in strictly increasing order)
    filename : string, optional
        name of the file in which to save the plot
    title : string, optional
        title of the plot

    Returns
    -------
    None
    """
    assert all(s[k] <= s[k + 1] for k in range(len(s) - 1))
    assert all(inds[k] < inds[k + 1] for k in range(len(inds) - 1))
    plt.figure()
    plt.plot(s, r, '*', color='gray')
    rs = r[inds]
    ss = s[inds]
    plt.plot(ss, rs, '*', color='black')
    plt.xlim((0, 1))
    plt.ylim((0, 1))
    plt.xlabel('score $S_k$')
    plt.ylabel('expected value ($P_k$) of outcome $R_k$')
    plt.title(title)
    plt.tight_layout()
    plt.savefig(filename, bbox_inches='tight')
    plt.close()

if __name__ == '__main__':
    #
    # Generate directories with plots as specified via the code below,
    # with each directory named m_len(inds)_nbins_iex
    # (where m, inds, nbins, and iex are defined in the code below).
    #
    # Set parameters.
    # minorticks is the number of minor ticks on the lower axis.
    minorticks = 100
    # majorticks is the number of major ticks on the lower axis.
    majorticks = 10
    # m is the number of members from the full population.
    m = 50000
    # n determines the number of observations for the subpopulation.
    n = 2500
    # Store processes for converting from pdf to jpeg in procs.
    procs = []
    # Consider 3 examples.
    for iex in range(3):
        # nbins is the number of bins for the reliability diagrams.
        for nbins in [10, 50]:
            # nbins must divide n evenly.
            assert n % nbins == 0

            if iex == 0:
                # Define the indices of the subset for the subpopulation.
                inds = np.arange(0, m, m // n) + m // n // 2
                inds1 = np.arange(0, m // 4, m // n // 4) + m // 2 - m // 8
                inds2 = np.arange(0, m // 2, m // n // 2) + m // 2 - m // 4
                inds3 = np.arange(0, m, m // n)
                inds = np.concatenate((inds1, inds2, inds3))
                # Indices must be sorted and unique.
                inds = np.unique(inds)
                inds = inds[0:(m // (m // len(inds)))]

                # Construct scores.
                sl = np.arange(0, 1, 1 / m) + 1 / (2 * m)
                s = np.square(sl)
                # The scores must be in non-decreasing order.
                s = np.sort(s)

                # Construct perturbations to the scores for sampling rates.
                d = .25
                tl = -np.arange(-d, d, 2 * d / m) - d / m
                t = d - 1.1 * np.square(np.square(tl)) / d**3
                e = .7
                ul = -np.arange(-e, e, 2 * e / m) - e / m
                u = e - np.abs(ul)
                ins = np.arange(m // 2 - m // 50, m // 2 + m // 50)
                u[ins] = t[ins]
                u2 = 2 * t - u
                t += np.sin(np.arange((m))) * (u - t)

                # Construct the exact sampling probabilities.
                exact = s + t
                exact[inds] = s[inds] + u[inds]
                exact[inds + 1] = s[inds] + u2[inds]

            if iex == 1:
                # Define the indices of the subset for the subpopulation.
                np.random.seed(987654321)
                inds = np.sort(np.random.permutation((m))[:n])

                # Construct scores.
                s = np.arange(0, 1, 1 / m) + 1 / (2 * m)
                s = np.sqrt(s)
                # The scores must be in non-decreasing order.
                s = np.sort(s)

                # Construct perturbations to the scores for sampling rates.
                d = math.sqrt(1 / 2)
                tl = np.arange(-d, d, 2 * d / m) - d / m
                t = (1 + np.sin(np.arange((m)))) / 2 * (np.square(tl) - d**2)
                u = np.square(tl) - d**2
                u *= .75 * np.round(1 + np.sin(10 * np.arange((m)) / m)) / 2

                # Construct the exact sampling probabilities.
                exact = s + t
                exact[inds] = s[inds] + u[inds]

            if iex == 2:
                # Define the indices of the subset for the subpopulation.
                np.random.seed(987654321)
                inds = np.arange(0, m ** (3 / 4), 1)
                inds = np.unique(np.round(np.power(inds, 4 / 3))).astype(int)
                inds = inds[0:(50 * (len(inds) // 50))]

                # Construct scores.
                s = np.arange(0, 1, 1 / m) + 1 / (2 * m)
                # The scores must be in non-decreasing order.
                s = np.sort(s)

                # Construct perturbations to the scores for sampling rates.
                tl = np.arange(0, 1, 1 / m) + 1 / (2 * m)
                t = np.power(tl, 1 / 4) - tl
                t *= (1 + np.sin(np.arange((m)))) / 2
                u = np.power(tl, 1 / 4) - tl
                u *= .5 * (1 + np.sin(
                    50 * np.power(np.arange(0, m**4, m**3), 1 / 4) / m))

                # Construct the exact sampling probabilities.
                exact = s + t
                exact[inds] = s[inds] + u[inds]

            # Set a unique directory for each collection of experiments
            # (creating the directory if necessary).
            dir = 'unweighted'
            try:
                os.mkdir(dir)
            except FileExistsError:
                pass
            dir = 'unweighted/' + str(m) + '_' + str(len(inds))
            dir = dir + '_' + str(nbins)
            dir = dir + '_' + str(iex)
            try:
                os.mkdir(dir)
            except FileExistsError:
                pass
            dir = dir + '/'
            print(f'./{dir} is under construction....')

            # Generate a sample of classifications into two classes,
            # correct (class 1) and incorrect (class 0),
            # avoiding numpy's random number generators
            # that are based on random bits --
            # they yield strange results for many seeds.
            random.seed(987654321)
            uniform = np.asarray([random.random() for _ in range(m)])
            r = (uniform <= exact).astype(float)

            # Generate five plots and a text file reporting metrics.
            filename = dir + 'cumulative.pdf'
            kuiper, kolmogorov_smirnov, lenscale = cumulative(
                r, s, inds, majorticks, minorticks, filename)
            filename = dir + 'metrics.txt'
            with open(filename, 'w') as f:
                f.write('n:\n')
                f.write(f'{len(inds)}\n')
                f.write('lenscale:\n')
                f.write(f'{lenscale}\n')
                f.write('Kuiper:\n')
                f.write(f'{kuiper:.4}\n')
                f.write('Kolmogorov-Smirnov:\n')
                f.write(f'{kolmogorov_smirnov:.4}\n')
                f.write('Kuiper / lenscale:\n')
                f.write(f'{(kuiper / lenscale):.4}\n')
                f.write('Kolmogorov-Smirnov / lenscale:\n')
                f.write(f'{(kolmogorov_smirnov / lenscale):.4}\n')
            filename = dir + 'cumulative_exact.pdf'
            _, _, _ = cumulative(
                exact, s, inds, majorticks, minorticks, filename,
                title='exact expectations')
            filename = dir + 'equiscore.pdf'
            equiscore(r, s, inds, nbins, filename)
            filename = dir + 'equisamps.pdf'
            equisamps(r, s, inds, nbins, filename)
            filepdf = dir + 'exact.pdf'
            filejpg = dir + 'exact.jpg'
            exactplot(exact, s, inds, filepdf)
            args = ['convert', '-density', '1200', filepdf, filejpg]
            procs.append(subprocess.Popen(args))
    print('waiting for conversion from pdf to jpg to finish....')
    for iproc, proc in enumerate(procs):
        proc.wait()
        print(f'{iproc + 1} of {len(procs)} conversions are done....')

max_stars_repo_name: facebookresearch/fbcdgraph

max_issues_repo_name: facebookresearch/metamulti

max_forks_repo_name: facebookresearch/metamulti


text:
# 2020.06.05
# activate learning methods
# @yifan
import sys
import numpy as np
from scipy.spatial import distance
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from scipy.stats import entropy
import time

def entropy_query(prob, num_feature):
    #prob is the output of the clf.predict_proba on the pool of unlabeled data
    #num_feature is the number of images you want to select using the entropy method
    entropies = np.zeros(prob.shape[0])
    for i in range(prob.shape[0]):
        entropies[i] = np.sum(-prob[i]*np.log(prob[i]+.0000001))
    th = np.sort(entropies)[prob.shape[0]-num_feature]
    num_feature_idx = entropies <= th
    num_feature_idx = entropies >= th
    return num_feature_idx

def coreset(x_pool, x_train,k):
    #x_pool is the pool of unlabeled data
    #x_train is the current set of labeled data
    #k is the number of images to select from x_pool
    dists = distance.cdist(x_pool, x_train, 'euclidean')
    nearesttolabeleds = np.min(dists,axis=1)
    th = np.sort(nearesttolabeleds)[nearesttolabeleds.shape[0]-k]
    num_feature_idx = nearesttolabeleds >= th
    return num_feature_idx

class QBC():
    def __init__(self, learners, init=0.01, n_increment=200, n_iter=40, percent=0.05):
        self.init = init
        self.n_increment = n_increment
        self.n_learner = len(learners)
        self.n_iter = n_iter
        self.num_class = 3
        self.learners = learners
        self.percent = percent
        self.trained = False
        self.acc_t = []
        self.acc_v = []

    def metric(self, prob):
        return entropy(prob, base=self.num_class, axis=1)

    def fit(self, x, y, xv=None, yv=None):
        self.trained = True
        self.num_class = np.unique(y).shape[0]
        #x, xt, y, yt = train_test_split(x, y, train_size=self.init, random_state=42, stratify=y)
        idx = np.random.choice(x.shape[0], (int)(x.shape[0]*self.percent))
        x_train, y_train = x[idx], y[idx]
        x_pool = np.delete(x, idx, axis=0)
        y_pool = np.delete(y, idx, axis=0)
        acc_t, acc_v, s = [], [], []
        for k in range(self.n_iter):
            print('       start iter -> %3s'%str(k))
            t0 = time.time()
            for i in range(self.n_learner):
                self.learners[i].fit(x_train, y_train)
            pt = self.predict_proba(x_pool)
            at = accuracy_score(y_pool, np.argmax(pt, axis=1))
            acc_t.append(at)
            s.append(y_pool.shape[0])
            ht = self.metric(pt)
            try:
                xv.shape
                print('           test shape: %s, val shape: %s'%(str(x_pool.shape), str(xv.shape)))
                pv = self.predict_proba(xv)
                av = accuracy_score(yv, np.argmax(pv, axis=1))
                print('           <Acc> test: %s, val: %s'%(at, av))
                acc_v.append(av)
                hv = self.metric(pv)
                print('           <Entropy> test: %s, val: %s'%(np.mean(ht), np.mean(hv)))
            except:
                pass
            idx = np.argsort(ht)[-self.n_increment:]
            x_train = np.concatenate((x_train, x_pool[idx]), axis=0)
            y_train = np.concatenate((y_train, y_pool[idx]), axis=0)
            x_pool = np.delete(x_pool, idx, axis=0)
            y_pool = np.delete(y_pool, idx, axis=0)
            print('       end iter -> %3s using %10s seconds\n'%(str(k),str(time.time()-t0)))
        self.acc_t = acc_t
        self.acc_v = acc_v
        return s, acc_t, acc_v

    def predict_proba(self, x):
        assert (self.trained == True), "Must call fit first!"
        pred = np.zeros((x.shape[0], self.num_class))
        for i in range(self.n_learner):
            pred += self.learners[i].predict_proba(x)
        return pred / np.sum(pred, axis=1, keepdims=True)

    def predict(self, x):
        assert (self.trained == True), "Must call fit first!"
        pred = self.predict_proba(x)
        return np.argmax(pred, axis=1)

    def score(self, x, y):
        assert (self.trained == True), "Must call fit first!"
        pred = self.predict(x)
        return accuracy_score(y, pred)

max_stars_repo_name: yifan-fanyi/FF-Face

max_issues_repo_name: yifan-fanyi/FF-Face

max_forks_repo_name: yifan-fanyi/FF-Face


text:
import cv2
import numpy as np
from datasets.factory import get_imdb
from rcnn_feature import *
from utils.nms import *
from rcnn_load_model import *
import theanets
from utils.selective_search import *
# import matplotlib
# matplotlib.use('MacOSX')
import matplotlib.pyplot as plt
from sklearn.linear_model import LogisticRegression
import os

def rcnn_demo(i):
    imdb = get_imdb('voc_2007_test')
    img = cv2.imread(imdb.image_path_at(i))
    rcnn_model = np.load('rcnn_model2.npy').all()
    rcnn_load_model(rcnn_model, False)
    # rcnn_model = np.load('rcnn_model.npz')['rcnn_model'].all()

    # thresh = -1
    dets = rcnn_detect(imdb.image_index[i], imdb, rcnn_model)
    for i in xrange(len(dets)):
        if isinstance(dets[i], list):
            continue
        vis_detections(img, rcnn_model.classes[i], dets[i])
    # all_dets = np.array([])
    # for i in xrange(len(dets)):
    #     if dets[i] is not []:
    #         all_dets = np.concatenate((all_dets, np.concatenate((i, dets[i]), 1)))
    #
    # ord = np.argsort(all_dets[:, -1])[::-1]
    # for i in xrange(ord.shape[0]):
    #     score = all_dets[ord[i], -1]
    #     if score < 0:
    #         break
    #     cls_name = rcnn_model.classes[all_dets[ord[i], 0]]
    #     vis_detections(img, cls_name, dets)

def vis_detections(im, class_name, dets, thresh=0.8):
    """Draw detected bounding boxes."""
    inds = np.where(dets[:, -1] >= thresh)[0]
    if len(inds) == 0:
        return

    im = im[:, :, (2, 1, 0)]
    fig, ax = plt.subplots(figsize=(12, 12))
    ax.imshow(im, aspect='equal')
    for i in inds:
        bbox = dets[i, :4]
        score = dets[i, -1]

        ax.add_patch(
            plt.Rectangle((bbox[0], bbox[1]),
                          bbox[2] - bbox[0],
                          bbox[3] - bbox[1], fill=False,
                          edgecolor='red', linewidth=3.5)
            )
        ax.text(bbox[0], bbox[1] - 2,
                '{:s} {:.3f}'.format(class_name, score),
                bbox=dict(facecolor='blue', alpha=0.5),
                fontsize=14, color='white')

    ax.set_title(('{} detections with '
                  'p({} | box) >= {:.1f}').format(class_name, class_name,
                                                  thresh),
                  fontsize=14)
    plt.axis('off')
    plt.tight_layout()
    plt.draw()

def rcnn_detect(imidx, imdb, rcnn_model):

    d = rcnn_load_cached_pool5_features(rcnn_model.cache_name, imdb.name, imidx)
    d['feat'] = rcnn_pool5_to_fcX(d['feat'], rcnn_model.opts.layer, rcnn_model)
    # boxes = selective_search(img, ks = 500)
    # boxes = boxes.swapaxes(0, 1).swapaxes(2, 3)
    # feat = rcnn_extract_features(img, boxes, rcnn_model)
    # feat = rcnn_scale_features(feat, rcnn_model.training_opts.feat_norm_mean)
    scores = rcnn_model.classifier.predict_proba(d['feat'].astype('f'))
    # scores = feat * rcnn_model.detectors.W + rcnn_model.detectors.B

    scores_idx = np.argmax(scores, 1)
    num_classes = len(rcnn_model.classes)
    dets = [[] for _ in xrange(num_classes)]
    for i in xrange(1, num_classes):
        # I = np.where(scores[:, i] > thresh)
        I = np.where(scores_idx == i)[0]
        if I.size == 0:
            continue
        scored_boxes = np.concatenate((d['boxes'][I, :], scores[I, i].reshape((scores[I, i].size, 1))), 1)
        keep = nms(scored_boxes, 0.3)
        dets[i] = scored_boxes[keep, :]

    return dets

if __name__ == "__main__":
    rcnn_demo(12)
    rcnn_demo(15)
    os.system('sleep 1000')