text:
import numpy as np
from collections import Counter
import re

def z_value(w, x, b):
    return np.dot(w, x) + b

def sigmoid(z):
    # print('z value:', z)
    return 1 / (1+ np.exp(-z))

def cross_entropy_loss(y_est, y):
    return -(y * np.log(y_est) + (1-y)*np.log(1-y_est))

def cross_entropy(w, x, b, y):
    sigma = sigmoid(z_value(w, x, b))
    L_CE = -(y * np.log(sigma) + (1-y)*np.log(1-sigma))
    return L_CE

def gradient(x, y, w, b):
    z =  z_value(w, x, b)
    del_L = []

    del_L = [(sigmoid(z)-y)*xi for xi in x]

    del_b = sigmoid(z)-y
    del_L.append(del_b)

    return np.array(del_L)

def update_weight(w, b, nu, grad):
     return np.subtract(np.hstack((w, b)), nu*grad)

def stochastic_grad(x, y, w, b, nu):
    grad = gradient(x, y, w, b)
    # print(grad)
    return np.subtract(np.hstack((w, b)), nu*grad)

def stochastic_grad_constb(x, y, w, b, nu):
    grad = gradient(x, y, w, b)
    # print(grad)
    return np.subtract(np.hstack((w, b)), nu*np.hstack([grad[:-1], b]))

def softmax(z):
    """requires z to be a vector with a value for each in the
    K classes.
    """
    sum_z = sum(np.exp(zi) for zi in z)
    return [np.exp(zi)/sum_z for zi in z]

def extraction(review, pos_words, neg_words, pronouns):
    """parse data for feature extraction
    """
    counts = Counter()

    # set all cases to lower
    review = review.lower()

    # start counting with x5 if an ! is present
    if '!' in review:
        counts['x5'] += 1

    # remove punctuations except dashes and add word boundaries
    review = re.sub(r'[^\w\-\s]','', review)
    words = review.split()
    words = [f'_{w}_' for w in words]

    ct = 0
    for w in words:
        if w in pos_words:
            # count positive words in the review, including repeats
            counts['x1'] += 1
        if w in neg_words:
            # count negative words in the review, including repeats
            counts['x2'] += 1
        if w in pronouns:
            # count pronouns in the review, including repeats
            counts['x4'] += 1
        if w == '_no_' and ct == 0:
            # set x3 = 1 if no is in review
            counts['x3'] += 1
            ct = 1

    return [counts, np.log(len(words))]

if __name__ == '__main__':
    pass
    # run_tests()

    # get positive and negative reviews from csv's
    review_file_pos = np.loadtxt('datasets/assignment2/hotelPosT-train.txt',
                                 delimiter='\t', dtype='str', encoding="utf8")
    review_file_neg = np.loadtxt('datasets/assignment2/hotelNegT-train.txt',
                                 delimiter='\t', dtype='str', encoding="utf8")
    # TEST SET:
    test_file = np.loadtxt('datasets/assignment2/HW2-testset.txt',
                                 delimiter='\t', dtype='str', encoding="utf8")

    # get word semantics
    pos_words = np.loadtxt('datasets/assignment2/positive-words.txt',
                           delimiter='\n', dtype='str')
    neg_words = np.loadtxt('datasets/assignment2/negative-words.txt',
                           delimiter='\n', dtype='str')
    pronouns = np.loadtxt('datasets/assignment2/pronouns.txt',
                           delimiter='\n', dtype='str')
    # modify words to include word boundaries
    pos_words = [f'_{w}_' for w in pos_words]
    neg_words = [f'_{w}_' for w in neg_words]
    pronouns = [f'_{w}_' for w in pronouns]

    # extract positive and negative words
    reviews_pos = review_file_pos[:, 1]
    extracts_pos = [extraction(rev, pos_words, neg_words, pronouns) for rev in reviews_pos]
    vectors_pos = [[ex['x1'], ex['x2'], ex['x3'], ex['x4'], ex['x5'], lnw] for ex, lnw in extracts_pos]
    reviews_neg = review_file_neg[:, 1]
    extracts_neg = [extraction(rev, pos_words, neg_words, pronouns) for rev in reviews_neg]
    vectors_neg = [[ex['x1'], ex['x2'], ex['x3'], ex['x4'], ex['x5'], lnw] for ex, lnw in extracts_neg]
    test_reviews = test_file[:, 1]
    # TEST SET:
    test_extracts = [extraction(rev, pos_words, neg_words, pronouns) for rev in test_reviews]
    test_vectors = [[ex['x1'], ex['x2'], ex['x3'], ex['x4'], ex['x5'], lnw] for ex, lnw in test_extracts]
    test_ids = list(test_file[:,0])

    # concatenate positive and negative vectors
    vectors = np.vstack([vectors_pos, vectors_neg]).tolist()
    review_ids = np.hstack([list(review_file_pos[:,0]), list(review_file_neg[:,0])])

    # assuming bias=1, generate csv file of results
    b = 1
    with open('datasets/assignment2/pham-son-assgn2-part1.csv', 'w')as f:
        for id, vec in zip(review_ids, vectors):
            if id in review_file_pos[:, 0]:
                bs = 1
            elif id in review_file_neg[:, 0]:
                bs = 0
            f.write(f'{id},{int(vec[0])},{int(vec[1])},{int(vec[2])},'
                    f'{int(vec[3])},{int(vec[4])},{round(vec[5],2)},{bs}\n')

    reviews_file = np.loadtxt('datasets/assignment2/pham-son-assgn2-part1.csv',
                              delimiter=',', encoding="utf8",
                              dtype='str')
    # print(reviews_file)

    # TEST SET: do same for test set
    with open('datasets/assignment2/pham-son-assgn2-part1-testset.csv', 'w')as f:
        for id, vec in zip(test_ids, test_vectors):
            if id in review_file_pos[:, 0]:
                bs = 1
            elif id in review_file_neg[:, 0]:
                bs = 0
            f.write(f'{id},{int(vec[0])},{int(vec[1])},{int(vec[2])},'
                    f'{int(vec[3])},{int(vec[4])},{round(vec[5],2)},{bs}\n')

    test_reviews_file = np.loadtxt('datasets/assignment2/pham-son-assgn2-part1-testset.csv',
                                   delimiter=',', encoding="utf8",
                                   dtype='str')
    # print(test_reviews_file)

    # get file ids and vectors
    ids = reviews_file[:,0]
    pos_ids = ids[:len(reviews_pos)]
    neg_ids = ids[len(reviews_pos):]
    vectors = reviews_file[:,1:-1].astype('float')
    bias = reviews_file[:,-1].astype('float')
    vectors_w_bias = reviews_file[:,1:].astype('float')

    # TEST SET: get file ids and vectors
    test_ids = test_reviews_file[:,0]
    test_vectors = test_reviews_file[:,1:-1].astype('float')
    test_bias = test_reviews_file[:,-1].astype('float')
    test_vectors_w_bias = test_reviews_file[:,1:].astype('float')

    # generate training and testing set
    rands = np.random.rand(vectors_w_bias.shape[0])
    splitpoint = rands < np.percentile(rands, 100)
    train_x = np.array(vectors_w_bias[splitpoint])
    train_y = np.array(ids[splitpoint])
    print(len(train_x))
    dev_x = np.array(vectors_w_bias[~splitpoint])
    dev_y = np.array(ids[~splitpoint])
    # TEST SET:
    test_x = np.array(test_vectors_w_bias)
    test_y = np.array(test_ids)

    # run learning model
    theta = [0,0,0,0,0,0,0]
    thetas = []
    nu = 1
    why = 1
    for k in range(0, 2000):
        random_idx = np.random.choice(train_x.shape[0],
                                      size=1, replace=True)
        random_sample = train_x[random_idx, :][0]
        if train_y[random_idx][0] in pos_ids:
            y_true = 1
        elif train_y[random_idx][0] in neg_ids:
            y_true = 0

        theta = stochastic_grad_constb(x=random_sample[:-1],
                                       y=y_true,
                                       w=theta[:-1],
                                       b=why,
                                       nu=nu)

    # TEST SET:
    ## for testing final set, make sure to "unsplit" the training data
    with open('datasets/assignment2/pham-son-assgn2-out.txt', 'w')as f:
        for id, review_vec in zip(test_y, test_x):
            sigma = sigmoid(z_value(w=theta[:-1], x=review_vec[:-1], b=why))
            if sigma < 0.5:
                classf = 'NEG'
            elif sigma >= 0.5:
                classf = 'POS'
            f.write(f'{id}\t{classf}\n')

max_stars_repo_name: snpham/Belleau-Wood

max_issues_repo_name: snpham/Belleau-Wood

max_forks_repo_name: snpham/Belleau-Wood


text:
########################################################################################################################
import tensorflow as tf
import tensorflow_datasets as tfds
from tensorflow.keras import datasets, layers, models, utils
from tensorflow.keras.callbacks import LearningRateScheduler
from tensorflow.keras.layers import Conv2D, Dropout, BatchNormalization, Activation, Dense, MaxPooling2D, Flatten
from tensorflow.keras.models import Sequential
from tensorflow.keras import regularizers
import tensorflow as tf
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Input, BatchNormalization, Dropout
from tensorflow.keras.models import Model, Sequential

import sklearn
from sklearn.model_selection import KFold

import numpy as np
import matplotlib.pyplot as plt
import matplotlib
import sys
import os
import time
import math
import eagerpy as ep

import foolbox
########################################################################################################################
# Load cifar-10 dataset
(X_train, y_train), (X_test, y_test) = datasets.cifar10.load_data()

# Define score containers for each fold
acc_per_fold = []
loss_per_fold = []
########################################################################################################################
# Normalize
X_train = (X_train / 255.0)
X_test = (X_test / 255.0)
class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
########################################################################################################################
# show images
plt.figure(figsize=(10,10))
for i in range(25):
    plt.subplot(5,5,i+1)
    plt.xticks([])
    plt.yticks([])
    plt.grid(False)
    plt.imshow(X_train[i])
    plt.xlabel(class_names[y_train[i][0]])
plt.show()

########################################################################################################################
# AlexNet
########################################################################################################################
# without k-fold
# Create and Train the Model for CIFAR-10
class AlexNet8(Sequential):

    def __init__(self, input_shape, num_classes):
        super().__init__()

        # 1st Block(Input + Conv2D + MaxPooling + Normalization)
        self.add(Conv2D(96, kernel_size=(3, 3), activation='relu', padding='same', input_shape=input_shape))
        self.add(BatchNormalization())
        self.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2)))

        # 2nd Block(Input + Conv2D + MaxPooling + Normalization)
        self.add(Conv2D(256, kernel_size=(3, 3), padding='same', activation='relu'))
        self.add(BatchNormalization())
        self.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2)))

        # 3rd Block(Conv2D)
        self.add(Conv2D(384, kernel_size=(3, 3), padding='same', activation='relu'))
        self.add(Conv2D(384, kernel_size=(3, 3), padding='same', activation='relu'))
        self.add(Conv2D(256, kernel_size=(3, 3), padding='same', activation='relu'))
        self.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2)))

        # FC layer
        self.add(Flatten())  # 2D -> 1D vectors
        self.add(Dense(2048, activation='relu'))
        self.add(Dropout(0.5))
        self.add(Dense(2048, activation='relu'))
        self.add(Dropout(0.5))
        self.add(Dense(num_classes, activation='softmax'))
img_height = 32
img_width = 32
img_channels = 3
input_shape = (img_height, img_width, img_channels)
num_classes = 10

# Build model
model = AlexNet8(input_shape=input_shape, num_classes=num_classes)

# compile model
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

# set model path
Model_save_path = '/home/hj/PycharmProjects/HYPER/'
if not os.path.exists(Model_save_path):
    os.mkdir(Model_save_path)
Model_path = Model_save_path + 'AlexNet(cifar10).hdf5'

patient = 3
callbacks_list=[
    tf.keras.callbacks.ReduceLROnPlateau(
        monitor='val_accuracy',
        factor=0.1,
        patience=patient,
        min_lr=0.00001,
        verbose=1,
        mode='max'
    ),
    tf.keras.callbacks.ModelCheckpoint(
        filepath=Model_path,
        monitor='val_accuracy',
        save_best_only=True,
        verbose=1,
        mode='max'
    )]

# Train model
hist = model.fit(
    X_train, y_train, epochs=100, batch_size=64, validation_data=(X_test, y_test),
    callbacks=callbacks_list
)

model.save('AlexNet_cifar10')
########################################################################################################################
# Adversarial Attack with foolbox
########################################################################################################################
from foolbox import TensorFlowModel, accuracy, samples, Model, utils, attacks, plot
from foolbox.attacks import LinfPGD, LinfDeepFoolAttack

model = tf.keras.models.load_model('AlexNet_cifar10')
model.load_weights(Model_path)
preprocessing = dict()
bounds = (0, 1)
fmodel = TensorFlowModel(model, bounds=bounds, preprocessing=preprocessing)
fmodel = fmodel.transform_bounds((0, 1))

attack_labels = tf.convert_to_tensor(y_test, dtype='int64')
attack_labels = tf.reshape(attack_labels, 10000)
attack_images = tf.convert_to_tensor(X_test, dtype='float32')

predictions = model.predict(attack_images)
orig_predictions = np.argmax(predictions, axis=1)

########################################################################################################################
attack_images = ep.astensor(attack_images)
attack_labels = ep.astensor(attack_labels)

# apply the attack
attack = foolbox.attacks.FGSM()
epsilons = [
    0.0,
    0.0002,
    0.0005,
    0.0008,
    0.001,
    0.0015,
    0.002,
    0.003,
    0.01,
    0.1,
    0.3,
    0.5,
    1.0,
]

raw_advs, clipped_advs, success = attack(fmodel, attack_images[:20], attack_labels[:20], epsilons=epsilons)

# calculate and report the robust accuracy (the accuracy of the model when
# it is attacked)
robust_accuracy = 1 - success.float32().mean(axis=-1)
print("robust accuracy for perturbations with")
for eps, acc in zip(epsilons, robust_accuracy):
    print(f"  epsilon = {eps:<6}: {acc.item() * 100:4.1f} %")

print('Attack finshed!')

'''
# print(len(raw_advs)) # 13
# print(raw_advs[0]) # shpae : 20, 32, 32, 3 dtype = float32
# print(len(clipped_advs)) # 13
# print(clipped_advs[0]) # shape : 20, 32, 32, 3, dtype = float32

print(len(raw_advs))
print(len(raw_advs[0]))
print(len(raw_advs[0][0]))
print(len(raw_advs[0][0][0]))

plt.figure()
plt.imshow(raw_advs[0][0])
plt.show()

raw_advs = np.array(raw_advs, dtype='uint8')
print('converted done!')

# show images
plt.figure(figsize=(10,10))
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow(raw_advs[0][0])
plt.show()
'''

ext: py

reason: import numpy


text:
import dlib
import cv2
import numpy

from FacialLandmarkDetection import *

def draw_convex_hull(im, points, color):
    points = cv2.convexHull(points)
    cv2.fillConvexPoly(im, points, color=color)

FEATHER_AMOUNT = 3

def get_face_mask(im, landmarks):
    im = numpy.zeros(im.shape[:2], dtype=numpy.float64)

    draw_convex_hull(im,
                     landmarks,
                     color=1)

    im = numpy.array([im, im, im]).transpose((1, 2, 0))

    im = (cv2.GaussianBlur(im, (FEATHER_AMOUNT, FEATHER_AMOUNT), 0) > 0) * 1.0
    im = cv2.GaussianBlur(im, (FEATHER_AMOUNT, FEATHER_AMOUNT), 0)

    return im

data_folder_deidentification = "/media/matej/D/Diplomski/baza/rezultati/deidentification_destination_all_images_all_k_no_comparing_landmar_distances_spec"

if __name__ == "__main__":
    image_name = "/media/matej/D/Diplomski/baza/rezultati/deidentification_destination_all_images_all_k_no_comparing_landmar_distances_spec/001/001_k_5.ppm"
    detector = FacialLandmarkDetector(image_name)
    image = detector.getImage()

    parts = detector.detectFacialLandmarks(draw = False, normalize=False, numpy_format = True)

    [[max_x,max_y]] = parts.max(axis=0).tolist()
    [[min_x,min_y]] = parts.min(axis=0).tolist()
    parts = numpy.vstack([parts, [(int)((max_x+min_x)/2), min_y-10]])
    parts = numpy.vstack([parts, [(int)(((max_x+min_x)/2 + min_x)/2), min_y-10]])
    parts = numpy.vstack([parts, [(int)(((max_x+min_x)/2 + max_x)/2), min_y-10]])

    mask = get_face_mask(image, parts)

    image = (image * mask).astype(numpy.uint8)
    image = image[min_y - 20:max_y + 10, min_x - 10:max_x + 10]
    cv2.imshow("bez", image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()


text:
# coding: utf-8

# # Create your own FLS in Python
#
# ## Lab session 1
#
# In this lab we provide you with the basics for creating your own Fuzzy Logic System (FLS). We will examine the theory and put it to practice at the same time.
#
# ### Theory
#
# Let us start with an overview of all the components of an FLS:
#
#
#
# The fuzzifier needs:
# - input variables, output variables
# - membership functions for its variables
#
# The rules include:
# - an antecedent containing a selected mf per variable (e.g. if x1 is low ...)
# - operators: and, or, not (in this lab we only consider one operator in a rule's antecedent)
# - a consequent containing a selected mf for the output variable (Mamdani) or a formula for the output variable (TSK)
#
# The inference mechanism does:
# - uses the firing strength of a rule to calculate the result of the implication
# - (for which it uses an implication operator: min, prod)
# - aggregates the results
#
# The aggregated result is then defuzzified and becomes a crisp value.
#
#
# ### Practice
#
# Will make a start with implementing your own FLS. This can be of great help for your project, for which you will - hopefully - implement your own FLS too, and for your understanding of Fuzzy Logic.
#
# We are going to create a Mamdani inference system that helps you decide how much money to put aside per month for buying a new laptop / TV / headphone:
# - input variables: income (0-1000), and the quality of your current device (0-10).
# - output variable: amount of money (1-500)
#
# This tutorial is by no means a perfect implementation of an FLS, it is a very simple one and would have to be adapted for being usable for the FL projects.

# ## 1. Membership Functions
#
# ### Theory
#
# Membership functions are used to fuzzify crisp inputs, representing an item's membership to a class, with 0 meaning no membership and 1 meaning the item is a perfect prototype of a class.
#
# During the Fuzzy Logic crash course we have seen 4 basic types of membership functions: triangular, trapezoidal, gaussian and generalized bell shaped membership functions. Those membership functions are represented in the image:
#
#
#
# ### Practice
#
# Implement the trapezoidal and triangular membership functions by completing the code below. The *calculate_membership()* function should return a scalar.
#
# Test your implementation by submitting your answers via https://docs.google.com/forms/d/e/1FAIpQLScUoATQNzF30YSLxJUeoFHdJdtM4DK6bPA4oIpmczqTH0FDDg/viewform?usp=sf_link.

# In[2]:

import math
import numpy as np
from collections import defaultdict, Counter
import matplotlib.pyplot as plt

# In[3]:

class TriangularMF:
    """Triangular fuzzy logic membership function class."""
    def __init__(self, name, start, top, end):
        self.name = name
        self.start = start
        self.top = top
        self.end = end

    def calculate_membership(self, x):
        if x<self.start or x>self.end:
            return 0
        if x>self.top:
            return (self.end-x)/(self.end-self.top)
        return (x-self.start)/(self.top-self.start)

class TrapezoidalMF:
    """Trapezoidal fuzzy logic membership function class."""
    def __init__(self, name, start, left_top, right_top, end):
        self.name = name
        self.start = start
        self.left_top = left_top
        self.right_top = right_top
        self.end = end

    def calculate_membership(self, x):
        if x<self.start or x>self.end:
            return 0
        if x>=self.left_top and x<=self.right_top:
            return 1
        if x>self.right_top:
            return (self.end-x)/(self.end-self.right_top)
        return (x-self.start)/(self.left_top-self.start)

# In[4]:

x_list = range(0,30)
trapezoid = TrapezoidalMF('I member', 7, 10, 18, 25)
y_list = [trapezoid.calculate_membership(x) for x in x_list]
plt.plot(x_list, y_list)
plt.show()

# In[5]:

# Test your implementation by running the following statements
# Enter your answers in the Google form to check them, round to two decimals

triangular_mf = TriangularMF("medium", 150, 250, 350)
print(triangular_mf.calculate_membership(100))
print(triangular_mf.calculate_membership(249))
print(triangular_mf.calculate_membership(300))

trapezoidal_mf = TrapezoidalMF("bad", 0, 0, 2, 4)
print(trapezoidal_mf.calculate_membership(1.2))
print(trapezoidal_mf.calculate_membership(2.3))
print(trapezoidal_mf.calculate_membership(3.9))

# ## 2. Inputs and output
#
# ### Theory
#
# The inputs and output of a FLS are represented through linguistic variables, which are variables whose values are words rather
# than numbers. A value of a linguistic variable is called a linguistic term.
#
# An example of a linguistic variable is 'income' (a variable we're using in the system we're programming), with the linguistic terms 'low', 'medium', 'high'.
#
# ### Practice
#
# Now we are going to define input and output variables, which are a collection of multiple membership functions.
#
# A variable's membership functions (self.mfs) should be a list of membership functions. Define the input variables *income* and *quality* and the output variable *money* with the name, range and membership functions represented in the image. Afterwards, check your answers through the google form.
#
# (Note that you can use the trapezoidal membership function to represent a left or right shoulder membership function.)
#
#

# In[6]:

class Variable(object):
    """General class for variables in an FLS."""
    def __init__(self, name, range, mfs):
        self.name = name
        self.range = range
        self.mfs = mfs

    def calculate_memberships(self, x):
        """Test function to check whether
        you put together the right mfs in your variables."""
        return {
            mf.name : mf.calculate_membership(x)
            for mf in self.mfs
        }

    def get_mf_by_name(self, name):
        for mf in self.mfs:
            if mf.name == name:
                return mf

class Input(Variable):
    """Class for input variables, inherits
    variables and functions from superclass Variable."""
    def __init__(self, name, range, mfs):
        super(Input, self).__init__(name, range, mfs)
        self.type = "input"

class Output(Variable):
    """Class for output variables, inherits
    variables and functions from superclass Variable."""
    def __init__(self, name, range, mfs):
        super(Output, self).__init__(name, range, mfs)
        self.type = "output"

# In[7]:

# Input variable for your income
# Your code here
mfs_income = [TrapezoidalMF('Low', -200, 0, 200, 400),
              TriangularMF('Medium', 200, 500, 800),
              TrapezoidalMF('High', 600, 800, 1000, 1200)]
income = Input("income", (0, 1000), mfs_income)

# Input variable for the quality
# Your code here
mfs_quality = [TrapezoidalMF('Bad', -2, 0, 2, 4),
              TriangularMF('Okay', 2, 5, 8),
              TrapezoidalMF('Amazing', 6, 8, 10, 12)]
quality = Input("quality", (0, 10), mfs_quality)

# Output variable for the amount of money
# Your code here
mfs_money = [TrapezoidalMF('Low', -150, 0, 100, 250),
            TriangularMF('Medium', 150, 250, 350),
            TrapezoidalMF('High', 250, 400, 500, 650)]
money = Output("money", (0, 500), mfs_money)

inputs = [income, quality]
output = money

# In[8]:

# Test your implementation by running the following statements
# Enter your answers in the Google form to check them, round to two decimals

print(income.calculate_memberships(489))
print(quality.calculate_memberships(6))
print(output.calculate_memberships(222))

# ## 3. Fuzzy Rules
#
# ### Theory
#
# A fuzzy IF-THEN rule is composed of an antecedent and a consequent, like the implications you have seen with propositional and first order logic.
# - In the antecedent conditions for input variables are connected with an operator (AND, OR, NOT), e.g. "IF x1 is mf1 AND x2 is mf3", or "IF x1 is mf1 OR x2 is mf2".
# - The AND operator represents taking the intersection of fuzzy sets, which can be accomplished by choosing a T-Norm operation, such as minimum.
# - The OR operator represents taking the union of fuzzy sets, which can be accomplished by choosing a T-Conorm operation, such as maximum.
# - The NOT operator, the complement, is calculated by 1 minus a membership value. For example: NOT x1 is mf1, would be 1 - (membership of x1 to mf1).
# - By the use of the AND, NOT and OR we combine the different parts of the antecedent into a single number, which is the firing strength of the antecedent.
# - The consequent represents an action that we undertake if the rule fires.
#
# ### Practice
#
# We are going to add some simple rules for our FLS: complete rules that do not have mixed operators. Here we represent a rule through 3 variables:
# - Antecedent, represented as a list of names of membership functions. The index of the name corresponds to the variable it belongs to, for example: ["medium", "low"], where "medium" belongs to the first variable in *inputs* and "low" corresponds to the second variable in *inputs*.
# - Operator: "and" or "or", let's choose "and".
# - Consequent: a string corresponding one of the membership functions of your output variable, for example "high".
#
# These three variables would then compose the rule "IF income is medium AND quality is low THEN money is high."
#
# Complete the *calculate_firing_strength()*, that should function and check your answers by running the test statements.

# In[9]:

class Rule:
    """Fuzzy rule class, initialized with an antecedent (list of strings),
    operator (string) and consequent (string)."""
    def __init__(self, n, antecedent, operator, consequent):
        self.number = n
        self.antecedent = antecedent
        self.operator = operator
        self.consequent = consequent
        self.firing_strength = 0
        if self.operator == 'and':
            self.operation = lambda x: min(x)
        elif self.operator == 'or':
            self.operation = lambda x: max(x)

    def calculate_firing_strength(self, datapoint, inputs):
        return self.operation([inputs[i].calculate_memberships(datapoint[i])[self.antecedent[i]] for i in range(len(inputs))])

# In[10]:

# Test your implementation by checking the following statements
# Enter your answers in the Google form to check them, round to two decimals

rule1 = Rule(1, ["Low", "Amazing"], "and", "Low")
print(rule1.calculate_firing_strength([200, 6.5], inputs))
print(rule1.calculate_firing_strength([0, 10], inputs))

rule2 = Rule(2, ["High", "Bad"], "and", "High")
print(rule2.calculate_firing_strength([100, 8], inputs))
print(rule2.calculate_firing_strength([700, 3], inputs))

# ## 4. Fuzzy Rulebase
#
#
# ### Theory
#
# A rulebase is simply a collection of all rules of the system!
#
# ### Practice
#
# Our fuzzy rulebase is a collection of all rules. Create the following rules and initalize the fuzzy rulebase:
# - IF income is low AND quality is amazing THEN money is low
# - IF income is medium AND quality is amazing THEN money is low
# - IF income is high AND quality is amazing THEN money is low
# - IF income is low AND quality is okay THEN money is low
# - IF income is medium AND quality is okay THEN money is medium
# - IF income is high AND quality is okay THEN money is medium
# - IF income is low AND quality is bad THEN money is low
# - IF income is medium AND quality is bad THEN money is medium
# - IF income is high AND quality is bad THEN money is high
#
# Implement the *calculate_firing_strengths()* function that collects the highest firing strength found per membership function of the output variable in a dictionary or Counter object.
# For example, if the firing strengths for the rules listed above are 0, 0, 0, 0.5, 0.25, 0, 0, 0, 0 the result would look like this: *{"low":0.5, "medium":0.25, "high"0}*.
#
# Check the correctness of your function with the testing statements.

# In[14]:

from collections import Counter

class Rulebase:
    """The fuzzy rulebase collects all rules for the FLS, can
    calculate the firing strengths of its rules."""
    def __init__(self, rules):
        self.rules = rules

    def calculate_firing_strengths(self, datapoint, inputs):
        result = Counter()
        for i, rule in enumerate(self.rules):
            fs = rule.calculate_firing_strength(datapoint, inputs)
            consequent = rule.consequent
            if not consequent in result or fs > result[consequent]:
                result[consequent] = fs
        return result

# In[15]:

# Add the rules listed in the question description
# Your code here
rules = [Rule(1, ['Low', 'Amazing'], 'and', 'Low'),
        Rule(2, ['Medium', 'Amazing'], 'and', 'Low'),
        Rule(3, ['High', 'Amazing'], 'and', 'Low'),
        Rule(4, ['Low', 'Okay'], 'and', 'Low'),
        Rule(5, ['Medium', 'Okay'], 'and', 'Medium'),
        Rule(6, ['High', 'Okay'], 'and', 'Medium'),
        Rule(7, ['Low', 'Bad'], 'and', 'Low'),
        Rule(8, ['Medium', 'Bad'], 'and', 'Medium'),
        Rule(9, ['High', 'Bad'], 'and', 'High')]

rulebase = Rulebase(rules)

# In[16]:

# Test your implementation of calculate_firing_strengths()
# Enter your answers in the Google form to check them, round to two decimals

datapoint = [500, 3]
print(rulebase.calculate_firing_strengths(datapoint, inputs))

datapoint = [234, 7.5]
print(rulebase.calculate_firing_strengths(datapoint, inputs))

# ## 5. Inference (aggregation and defuzzification)
#
# ### Theory
#
# In the Fuzzy Inference all parts of the fuzzy system come together: we are mapping an input to an output in the following way:
# 1. Fuzzify the input.
# 2. Calculate the firing strengths for the rules.
# 3. Use the firing strength to determine the contribution of the consequent.
# 4. Aggregate / collect all consequents.
# 5. Defuzzify
#
# As already mentioned, there is Mamdani type inference and Takagi-Sugeno-Kang (TSK) type inference:
# - With Mamdani type inference we represent the consequents of fuzzy rules as fuzzy sets (using membership functions). We use a rule's firing strength to adapt the height of the membership function in the consequent, using the implication operator (minimum or product). The consequents are then aggregated into one area (taking the maximum of all consequents for the entire input range), on which we apply a defuzzification method, such as 'largest of max', 'smallest of max' or 'centroid'.
# - With TSK type inference we represent the consequents as a function of the input variables, or a constant. To combine the consequents into one output number we calculate a weighted average, where the weights are the rules' firing strengths.
#
#
#
#
# In the following image multiple defuzzification methods are visualized:
#
#
# ### Practice
#
# We will finalize our system using Mamdani type inference by performing the following three steps:
# 1. Gathering the largest firing strength per membership function of the output variable (implemented in your rulebase in Step 3)
# 2. Discretizing the range of your output variable and applying the aggregation method (max): for every bin you find the maximum fuzzy membership value. Notice that the membership functions of the output variable are `cut off' according to the firing strengths, with the implication method (min).
# To accomplish this we perform two steps:
#     - First we find where the aggregated area starts and ends on the x-axis
#     - Second we discretize the area between start and end into 201 points (thus representing the area in 200 bins)
# 3. Applying two defuzzification methods: implement smallest of max (som) and largest of max (lom).

# In[51]:

class Reasoner:
    def __init__(self, rulebase, inputs, output, n_points, defuzzification):
        self.rulebase = rulebase
        self.inputs = inputs
        self.output = output
        self.discretise = n_points
        self.defuzzification = defuzzification

    def inference(self, datapoint):
        # 1. Calculate the highest firing strength found in the rules per
        # membership function of the output variable
        # looks like: {"low":0.5, "medium":0.25, "high":0}

        # Your code here
        firing_strengths = self.rulebase.calculate_firing_strengths(datapoint, self.inputs)

        # 2. Aggregate and discretize
        # looks like: [(0.0, 1), (1.2437810945273631, 1), (2.4875621890547261, 1), (3.7313432835820892, 1), ...]
        input_value_pairs = self.aggregate(firing_strengths)

        # 3. Defuzzify
        # looks like a scalar
        crisp_output = self.defuzzify(input_value_pairs)
        return crisp_output

    def aggregate(self, firing_strengths):

        # First find where the aggregated area starts and ends
        # Your code here
        area = np.linspace(self.output.range[0], self.output.range[1], self.discretise)

        # Second discretize this area and aggragate
        # Your code here
        pts = []
        for pt in area:
            pts.append((pt, max([min(firing_strengths[mf.name], mf.calculate_membership(pt)) for mf in self.output.mfs])))

        return pts

    def defuzzify(self, input_value_pairs):
        # Your code here
        if self.defuzzification == 'som':
            max_val = max(input_value_pairs, key=lambda x: x[1])
            return max_val[0]

        if self.defuzzification == 'lom':
            max_val = max(reversed(input_value_pairs), key=lambda x: x[1])
            return max_val[0]

        return crisp_value

# In[52]:

# Test your implementation of the fuzzy inference
# Enter your answers in the Google form to check them, round to two decimals

thinker = Reasoner(rulebase, inputs, output, 201, "som")
datapoint = [100, 1]
print(round(thinker.inference(datapoint)))

thinker = Reasoner(rulebase, inputs, output, 101, "som")
datapoint = [550, 4.5]
print(round(thinker.inference(datapoint)))

thinker = Reasoner(rulebase, inputs, output, 201, "som")
datapoint = [900, 6.5]
print(round(thinker.inference(datapoint)))

thinker = Reasoner(rulebase, inputs, output, 201, "lom")
datapoint = [100, 1]
print(round(thinker.inference(datapoint)))

thinker = Reasoner(rulebase, inputs, output, 101, "lom")
datapoint = [550, 4.5]
print(round(thinker.inference(datapoint)))

thinker = Reasoner(rulebase, inputs, output, 201, "lom")
datapoint = [900, 6.5]
print(round(thinker.inference(datapoint)))

max_stars_repo_name: Zurga/Fuzzy-Shrooms

max_issues_repo_name: Zurga/Fuzzy-Shrooms

max_forks_repo_name: Zurga/Fuzzy-Shrooms


text:
import numpy as np

print 'If not working, run in ipython from /old/xxxx model dir as "% run ../../util/read_history.py"'

model = np.load('model.npz')
history = model['history_acc']

history = np.array(history)

#print 'Max valid acc', history[:,0].max()
#print 'Max test acc', history[:,1].max()

print 'Train accuracy'
for item in history[:,0]:
	print item
print 'Test accuracy'
for item in history[:,2]:
	print item

max_stars_repo_name: pmorerio/video-gesture-autism

max_issues_repo_name: pmorerio/video-gesture-autism

max_forks_repo_name: pmorerio/video-gesture-autism


text:
# Joey Velez-Ginorio
# Rosie Aboody
# Laurie R. Santos
# Julian Jara-Ettinger
# -----------------------------
# When teaching breaks down...
# -----------------------------

"""
This script lets you define different hypothesis spaces.
"""

import itertools
import numpy as np

class GenerateHypothesisSpace():
    """
        Class which holds several hypothesis space generator functions.

    """

    def __init__(self, blockList):
        self.blockList = blockList
        self.unorderedArgs = self.unorderedArgs(self.blockList)
        self.orderedArgs = self.orderedArgs(self.blockList)

    def simpleDepthSampler(self, depth, uniform):
        """
            Samples AND, OR hypotheses at several depths.
        """
        x = lambda x: ''.join(x)    # Beautiful function to make hypotheses pretty

        hypotheses = []
        args = []

        for i in range(1,depth+1):
            args = itertools.chain(args, map(x,itertools.combinations('ABCDE',i)))

        args = [[i] for i in args]

        args = list(args)

        y = lambda y: self.Or(*y)

        for i in range(1, depth+1):
            hypotheses = itertools.chain(hypotheses, map(y,
                                                itertools.combinations('ABCDE',i)))

        hypotheses = args + list(hypotheses)

        # rid of duplicates
        hypotheses = [tuple(i) for i in hypotheses]
        hypotheses = set(hypotheses)
        hypotheses = list(hypotheses)
        hypotheses = [list(i) for i in hypotheses]

        a = 0
        if uniform:
            prior = list()
            prior = [1.0/len(hypotheses) for i in hypotheses]

        else:
            prior = list()
            for h in hypotheses:
                prior.append(1.0/self.priorHelp(h))
            normal = sum(prior)
            prior = [i/normal for i in prior]

        return [hypotheses, prior, [''.join(i) for i in self.unorderedArgs]]

    def depthSampler(self, depth, size, uniform=True):
        """
            Samples AND, OR hypotheses at several depths.

        """
        x = lambda x: ''.join(x)    # Beautiful function to make hypotheses pretty

        hypotheses = []
        args = []

        for i in range(1,depth+1):
            args = itertools.chain(args, itertools.imap(x,itertools.combinations('ABCDE',i)))

        args = list(args)

        y = lambda y: self.Or(*y)

        for i in range(1, depth+1):
            hypotheses = itertools.chain(hypotheses, itertools.imap(y,
                                            itertools.combinations(args,i)))

        hypotheses = list(hypotheses)

        hypotheses = [i for i in hypotheses if self.max_len(i) <= size]
        hypotheses = [i for i in hypotheses if self.total_len(i) <= depth]

        if uniform:
            prior = list()
            prior = [1.0/len(hypotheses) for i in hypotheses]

        else:
            prior = list()
            for h in hypotheses:
                prior.append(1.0/self.priorHelp(h))
            normal = sum(prior)
            prior = [i/(5+normal) for i in prior]

        return [hypotheses, prior, [''.join(i) for i in self.unorderedArgs]]

    """
        Same thing as depthSampler, except you first specify how many samples
        you want from the hypothesis space depthSampler gives.
    """
    def random_depth_sampler(self, samples, depth, uniform=True, th = ['BE']):

        temp = self.depthSampler(depth,uniform)
        hyps = temp[0]
        arg = temp[2]

        if len(hyps) < samples:
            print 'Desired sample size is larger than total hypothesis space, choose larger depth'
            return None

        final_hyps = list()
        final_hyps.append(th)

        for i in range(samples-1):
            ind = np.random.choice(len(hyps))
            while hyps[ind] == th:
                ind = np.random.choice(len(hyps))

            temp = hyps.pop(ind)
            final_hyps.append(temp)

        if uniform:
            prior = list()
            prior = [1.0/len(final_hyps) for i in final_hyps]

        np.random.shuffle(final_hyps)

        return [final_hyps, prior, arg]

    def random_teacher(self, num_examples):

        temp = self.depthSampler(1,uniform=True)
        examples = temp[2]

        return list(np.random.choice(examples,size=num_examples,replace=False))

    def permute_teacher(self, teacherData):

        final_teacher_data = list()

        if len(teacherData) < 8:
            final_teacher_data = itertools.permutations(teacherData)
            final_teacher_data = [list(i) for i in final_teacher_data]

        else:
            temp = teacherData
            perms = set()

            while len(perms) < 10000:
                np.random.shuffle(temp)
                perms.add(tuple(temp))

            final_teacher_data = list(perms)
            final_teacher_data = [list(i) for i in final_teacher_data]

        return final_teacher_data

    def priorHelp(self, hypothesis):
        total = 0
        for h in hypothesis:
            total += len(h)

        return total

    def unorderedArgs(self, blockList):
        """
            Generates a list of arguments for the unordered set of hypothesis
            generators. Takes a blockList, and generates every combination.

            Param:
                blockList - a list of characters
                uniform - boolean, if true, will set prior to uniform distribution
        """

        # Initialize empty action space
        args = list()
        args += blockList

        # Generate every possible combination of arguments from blockList
        for i in range(2, len(blockList)+1):
            for arg in itertools.combinations(blockList, i):
                args.append(list(arg))

        return args

    def orderedArgs(self, blockList):
        """
            Generates a list of arguments for the unordered set of hypothesis
            generators. Takes a blockList, and generates every combination.

            Param:
                blockList - a list of characters
        """

        # Initialize empty action space
        args = list()
        args += blockList

        # Generate every possible combination of arguments from blockList
        for i in range(2, len(blockList)+1):
            for arg in itertools.permutations(blockList, i):
                args.append(list(arg))

        return args

    def unorderedOr(self, uniform=True):
        """
            Hypothesis Space #1:
            Generates a list of hypotheses, including all combinations of
            Or logic rules.

            Param:
                uniform - if true, prior for H is uniform, else t.b.d

        """

        # Initializes hypothesis space and prior
        hypothesisSpace = list()
        hypothesisSpacePrior = list()

        # Add the single-block hypotheses to hypothesis space
        hypothesisSpace += [[i] for i in self.unorderedArgs[0:len(self.blockList)]]

        # Remove the single-block hypotheses from arguments
        args = self.unorderedArgs[len(self.blockList):]

        # Use args as arguments for Or(), add to hyp. space
        for arg in args:
            hypothesisSpace.append(self.Or(*arg))

        if uniform:
            # Calculate prior distribution of hypothesis space
            hypothesisSpacePrior = [1.0/len(self.unorderedArgs) for i in self.unorderedArgs]

        return [hypothesisSpace, hypothesisSpacePrior, [''.join(i) for i in self.unorderedArgs]]

    def unorderedAnd(self, uniform=True):
        """
            Hypothesis Space #1:
            Generates a list of hypotheses, including all combinations of
            And logic rules.

            Param:
                uniform - if true, prior for H is uniform, else t.b.d

        """

        # Initializes hypothesis space and prior
        hypothesisSpace = list()
        hypothesisSpacePrior = list()

        # Add the single-block hypotheses to hypothesis space
        hypothesisSpace += [[i] for i in self.unorderedArgs[0:len(self.blockList)]]

        # Remove the single-block hypotheses from arguments
        args = self.unorderedArgs[len(self.blockList):]

        # Use args as arguments for Or(), add to hyp. space
        for arg in args:
            hypothesisSpace.append(self.And(*arg))

        if uniform:
            # Calculate prior distribution of hypothesis space
            hypothesisSpacePrior = [1.0/len(self.unorderedArgs) for i in self.unorderedArgs]

        return [hypothesisSpace, hypothesisSpacePrior, [''.join(i) for i in self.unorderedArgs]]

    def unorderedAndDepth(self, depth, uniform=True):
        """
            Hypothesis Space #1:
            Generates a list of hypotheses, including all combinations of
            And logic rules.
            Param:
                uniform - if true, prior for H is uniform, else t.b.d
        """

        # Initializes hypothesis space and prior
        hypothesisSpace = list()
        hypothesisSpacePrior = list()

        # Add the single-block hypotheses to hypothesis space
        hypothesisSpace += [[i] for i in self.unorderedArgs[0:len(self.blockList)]]

        # Remove the single-block hypotheses from arguments
        args = self.unorderedArgs[len(self.blockList):]
        args = [i for i in args if len(i) <= depth]

        # Use args as arguments for Or(), add to hyp. space
        for arg in args:
            hypothesisSpace.append(self.And(*arg))

        if uniform:
            # Calculate prior distribution of hypothesis space
            hypothesisSpacePrior = [1.0/len(hypothesisSpace) for i in hypothesisSpace]

        else:
            for h in hypothesisSpace:
                hypothesisSpacePrior.append(1.0/self.priorHelp(h))
                normal = sum(hypothesisSpacePrior)
                hypothesisSpacePrior = [i/normal for i in hypothesisSpacePrior]

        return [hypothesisSpace, hypothesisSpacePrior, [''.join(i) for i in self.unorderedArgs]]

    def unorderedAndOr(self, uniform = True):
        """
            Hypothesis Space # 3:
            Generates a list of hypotheses, including all combinations of
            And and Or logic rules.

            Param:
                uniform - if ture, prior for H is uniform, else t.b.d.
        """

        # Initializes hypothesis space and prior
        hypothesisSpace = list()
        hypothesisSpacePrior = list()

        # Add hypotheses to the hypothesis space
        hypothesisSpace += [[i] for i in self.unorderedArgs[0:len(self.blockList)]]

        # Remove the single-block hypotheses from arguments so they don't get
        # needlessly duplicated.
        args = self.unorderedArgs[len(self.blockList):]

        # Use args as arguments for And() as well as Or(), add to hypothesis space
        for arg in args:
            hypothesisSpace.append(self.And(*arg))
            hypothesisSpace.append(self.Or(*arg))

        if uniform:
            # Calculate prior distribution of hypothesis space
            hypothesisSpacePrior = [1.0/len(hypothesisSpace) for i in hypothesisSpace]

        # hypothesisSpace = all possible And & Or hypotheses
        # hypothesisSpacePrior = either uniform, or simplicity biased (tba)
        # the last statement makes up the actionSpace, by taking all the combinations
        # we made in unorderedArgs and simply taking away all the spaces etc.
        return [hypothesisSpace, hypothesisSpacePrior, [''.join(i) for i in self.unorderedArgs]]

    def orderedAnd(self, uniform = True):

        """
            Hypothesis space #4
            Generates a list of ordered hypotheses, including all combinations
            of the And logical operator.
        """
        # Initializes the hypothesis space and prior
        hypothesisSpace, hypothesisSpacePrior = list(), list()

        # Add hypotheses to the hypothesis space:
        hypothesisSpace += [[i] for i in self.orderedArgs[0:len(self.blockList)]]

        # Remove the single-block hypotheses from the list of arguments
        args = self.orderedArgs[len(self.blockList):]

        # use the args as arguments for the And() function & add to hypothesisSpace
        for arg in args:
            hypothesisSpace.append(self.And(*arg))

        if uniform:
            # Calculate prior
            hypothesisSpacePrior = [1.0/len(self.orderedArgs) for i in self.orderedArgs]

        return [hypothesisSpace, hypothesisSpacePrior, [''.join(i) for i in self.unorderedArgs]]

    def orderedAndOr(self, uniform = True):

        """
            Hypothesis space # 5:
            Generates a lsit of ordered hypotheses, including all combinations of
            the And && Or logical operators
        """

        # Initializes the hypothesis space & prior
        hypothesisSpace, hypothesisSpacePrior = list(), list()

        # Add hypotheses to the hypothesis space:
        hypothesisSpace += [[i] for i in self.orderedArgs[0:len(self.blockList)]]

        # remove the single-block hypotheses from the list of arguments
        args = self.orderedArgs[len(self.blockList):]
        args2 = self.unorderedArgs[len(self.blockList):]

        # use the args as arguments for the And() and Or() functions and add to hyopthesisSpace
        for arg in args:
            hypothesisSpace.append(self.And(*arg))
        for arg in args2:
            hypothesisSpace.append(self.Or(*arg))

        if uniform:
            # calculate prior
            hypothesisSpacePrior = [1.0/len(hypothesisSpace) for i in hypothesisSpace]

        return [hypothesisSpace, hypothesisSpacePrior, [''.join(i) for i in self.unorderedArgs]]

    def Or(self, *args):
        """
            Logical Or, e.g. Or('A','B') = ['A','B']
            Can handle 2 or more arguments

            Param:
                *args - May accept any argument, but
                for the model, block characters generally used

        """

        # Our return list
        temp = list()

        # Sift through arguments to add to final list
        for arg in args:

            # If argument is a tuple, convert to list then add
            if type(arg) is tuple:
                temp.append(list(arg))

            # Standard character, add as usual
            else:
                temp.append(arg)

        return temp

    # def And(self, *args):

    #   """
    #       Logical And, e.g. And('A','B') -> ['AB','BA']
    #       Can handle 2 or more arguments.

    #       Param:
    #           *args - May accept any argument, but
    #           for the model, block characters generally used.

    #   """

    #   args = list(args)

    #   # Convert arguments to list if not already
    #   for i in range(len(args)):
 #          if type(args[i]) is not list:
 #              args[i] = list([args[i]])

 #      # Initialize final list
    #   final = list()

    #   # Generate all permutations of arguments
    #   temp = list(itertools.permutations(args))

    #   # Compute all products within each permutation
    #   for arg in temp:
    #       final.append(list([''.join(s) for s in list(itertools.product(*arg))]))

    #   return [''.join(i) for i in final]

    def And(self,*args):

        args = list(args)

        for i in range(len(args)):

            if type(args[i]) is not list:
                args[i] = list([args[i]])

        return [''.join(s) for s in list(itertools.product(*args))]

    def total_len(self, hyp):
        total = 0
        for h in hyp:
            total += len(h)
        return total

    def max_len(self, hyp):
    	max_len = 0
    	for h in hyp:
    		if max_len < len(h):
    			max_len = len(h)
    	return max_len

max_stars_repo_name: joeyginorio/CurseOfKnowledge

max_issues_repo_name: joeyginorio/CurseOfKnowledge

max_forks_repo_name: joeyginorio/CurseOfKnowledge


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

# ------------------------------------------------------------------------------
#
#   Copyright 2018-2019 Fetch.AI Limited
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#
# ------------------------------------------------------------------------------

"""This module contains the helpers methods for the controller agent."""

import math
import numpy as np
import random
from typing import Dict, List, Tuple

QUANTITY_SHIFT = 1  # Any non-negative integer is fine.

def generate_good_pbk_to_name(nb_goods: int) -> Dict[str, str]:
    """
    Generate public keys for things.

    :param nb_goods: the number of things.
    :return: a dictionary mapping goods' public keys to names.
    """
    max_number_of_digits = math.ceil(math.log10(nb_goods))
    string_format = 'tac_good_{:0' + str(max_number_of_digits) + '}'
    return {string_format.format(i) + '_pbk': string_format.format(i) for i in range(nb_goods)}

def determine_scaling_factor(money_endowment: int) -> float:
    """
    Compute the scaling factor based on the money amount.

    :param money_endowment: the endowment of money for the agent
    :return: the scaling factor
    """
    scaling_factor = 10.0 ** (len(str(money_endowment)) - 1)
    return scaling_factor

def generate_good_endowments(nb_goods: int, nb_agents: int, base_amount: int, uniform_lower_bound_factor: int, uniform_upper_bound_factor: int) -> List[List[int]]:
    """
    Compute good endowments per agent. That is, a matrix of shape (nb_agents, nb_goods).

    :param nb_goods: the number of goods.
    :param nb_agents: the number of agents.
    :param base_amount: the base amount of instances per good
    :param uniform_lower_bound_factor: the lower bound of the uniform distribution for the sampling of the good instance number.
    :param uniform_upper_bound_factor: the upper bound of the uniform distribution for the sampling of the good instance number.
    :return: the endowments matrix.
    """
    # sample good instances
    instances_per_good = _sample_good_instances(nb_agents, nb_goods, base_amount,
                                                uniform_lower_bound_factor, uniform_upper_bound_factor)
    # each agent receives at least two good
    endowments = [[base_amount] * nb_goods for _ in range(nb_agents)]
    # randomly assign additional goods to create differences
    for good_id in range(nb_goods):
        for _ in range(instances_per_good[good_id] - (base_amount * nb_agents)):
            agent_id = random.randint(0, nb_agents - 1)
            endowments[agent_id][good_id] += 1
    return endowments

def generate_utility_params(nb_agents: int, nb_goods: int, scaling_factor: float) -> List[List[float]]:
    """
    Compute the preference matrix. That is, a generic element e_ij is the utility of good j for agent i.

    :param nb_agents: the number of agents.
    :param nb_goods: the number of goods.
    :param scaling_factor: a scaling factor for all the utility params generated.
    :return: the preference matrix.
    """
    utility_params = _sample_utility_function_params(nb_goods, nb_agents, scaling_factor)
    return utility_params

def _sample_utility_function_params(nb_goods: int, nb_agents: int, scaling_factor: float) -> List[List[float]]:
    """
    Sample utility function params for each agent.

    :param nb_goods: the number of goods
    :param nb_agents: the number of agents
    :param scaling_factor: a scaling factor for all the utility params generated.
    :return: a matrix with utility function params for each agent
    """
    decimals = 4 if nb_goods < 100 else 8
    utility_function_params = []
    for i in range(nb_agents):
        random_integers = [random.randint(1, 101) for _ in range(nb_goods)]
        total = sum(random_integers)
        normalized_fractions = [round(i / float(total), decimals) for i in random_integers]
        if not sum(normalized_fractions) == 1.0:
            normalized_fractions[-1] = round(1.0 - sum(normalized_fractions[0:-1]), decimals)
        utility_function_params.append(normalized_fractions)

    # scale the utility params
    for i in range(len(utility_function_params)):
        for j in range(len(utility_function_params[i])):
            utility_function_params[i][j] *= scaling_factor

    return utility_function_params

def _sample_good_instances(nb_agents: int, nb_goods: int, base_amount: int,
                           uniform_lower_bound_factor: int, uniform_upper_bound_factor: int) -> List[int]:
    """
    Sample the number of instances for a good.

    :param nb_agents: the number of agents
    :param nb_goods: the number of goods
    :param base_amount: the base amount of instances per good
    :param uniform_lower_bound_factor: the lower bound factor of a uniform distribution
    :param uniform_upper_bound_factor: the upper bound factor of a uniform distribution
    :return: the number of instances I sampled.
    """
    a = base_amount * nb_agents + nb_agents * uniform_lower_bound_factor
    b = base_amount * nb_agents + nb_agents * uniform_upper_bound_factor
    # Return random integer in range [a, b]
    nb_instances = [round(np.random.uniform(a, b)) for _ in range(nb_goods)]
    return nb_instances

def generate_money_endowments(nb_agents: int, money_endowment: int) -> List[int]:
    """
    Compute the initial money amounts for each agent.

    :param nb_agents: number of agents.
    :param money_endowment: money endowment per agent.
    :return: the list of initial money amounts.
    """
    return [money_endowment] * nb_agents

def generate_equilibrium_prices_and_holdings(endowments: List[List[int]], utility_function_params: List[List[float]], money_endowment: int, scaling_factor: float, quantity_shift: int = QUANTITY_SHIFT) -> Tuple[List[float], List[List[float]], List[float]]:
    """
    Compute the competitive equilibrium prices and allocation.

    :param endowments: endowments of the agents
    :param utility_function_params: utility function params of the agents (already scaled)
    :param money_endowment: money endowment per agent.
    :param scaling_factor: a scaling factor for all the utility params generated.
    :param quantity_shift: a factor to shift the quantities in the utility function (to ensure the natural logarithm can be used on the entire range of quantities)
    :return: the lists of equilibrium prices, equilibrium good holdings and equilibrium money holdings
    """
    endowments_a = np.array(endowments, dtype=np.int)
    scaled_utility_function_params_a = np.array(utility_function_params, dtype=np.float)  # note, they are already scaled
    endowments_by_good = np.sum(endowments_a, axis=0)
    scaled_params_by_good = np.sum(scaled_utility_function_params_a, axis=0)
    eq_prices = np.divide(scaled_params_by_good, quantity_shift * len(endowments) + endowments_by_good)
    eq_good_holdings = np.divide(scaled_utility_function_params_a, eq_prices) - quantity_shift
    eq_money_holdings = np.transpose(np.dot(eq_prices, np.transpose(endowments_a + quantity_shift))) + money_endowment - scaling_factor
    return eq_prices.tolist(), eq_good_holdings.tolist(), eq_money_holdings.tolist()


text:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    :
# @Author  : yangsen
# @Site    :
# @File    : AdaBoosting.py
# @Software: PyCharm
import numpy as np
from sklearn.datasets import load_iris
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
import logging
# logging.basicConfig(level=logging.WARNING)

class AdaBoosting(object):
    def __init__(self, verbose=0, n_estimators=10):
        self.n_estimators = n_estimators
        if verbose == 0:
            logging.basicConfig(level=logging.NOTSET)

    def fit(self, instances, labels):
        assert instances.shape[0] != labels[0]
        self.instances = instances
        self.labels = labels
        self.instances_num = instances.shape[0]
        self.instances_dim = instances.shape[1]
        weights = np.full(self.instances_num, 1/self.instances_num)

        # 减少决策桩计算。对样本各维度数值进行排序，并配置对应的label
        # 找到label反转的位置。该位置才需要计算决策结果
        self._decision_preprocess()

        # 所有弱分类器
        self.decision_infos = []
        # 所有弱分类器的权重
        self.decision_weights = []
        # 所有步骤的样本权重，可以不存。
        self.step_weights = [weights]
        for i in range(self.n_estimators):
            decision_info = self.decision_stump(weights)
            logging.info(decision_info)
            self.decision_infos.append(decision_info)
            logging.info("min_error_rate: %s" % decision_info['min_error_rate'])
            decision_weight = 0.5 * np.log((1-decision_info['min_error_rate']) / (decision_info['min_error_rate']+1e-16))
            logging.info("decision_weight: %s" % decision_weight)

            self.decision_weights.append(decision_weight)
            # 对样本集进行预测，计算错误率，以及错误的样本，调整样本权重
            decision_label = self._decision_stump_instance_result(decision_info, instances=self.instances)
            decision_result = decision_label == self.labels

            # 分类正确的平分0.5，分类错误的评分0.5
            true_num = np.sum(decision_result)
            logging.info("true_num: %s" % true_num)

            # 标准weight计算
            new_weights = weights * np.exp(- decision_weight * decision_label * self.labels)
            weights = new_weights / new_weights.sum()

            # 升级版
            # 错误的平分0.5权重，正确的按原权重比例分0.5权重。
            # new_weights = np.zeros(self.instances_num)
            # if self.instances_num-true_num != 0:
            #     new_weights[decision_result == False] = 0.5/(self.instances_num-true_num)
            # new_weights[decision_result] = 0.5 * weights[decision_result] / weights[decision_result].sum()
            # weights = new_weights

            logging.info("weights: %s" % weights)
            logging.info("weights_sum: %s" % weights.sum())

            error_rate = self.error_rate(instances=self.instances, labels=self.labels)
            logging.warn("error rate: %s", error_rate)
            if error_rate == 0:
                break
            logging.info("--"*20)
            self.step_weights.append(weights)

        # 各决策树权重
        logging.info(self.decision_weights)
        logging.info("training finished!")

    def error_rate(self, instances, labels):
        error_rate = 1 - np.sum(self.predict(instances) == labels) / instances.shape[0]
        return error_rate

    def predict(self, instances):
        predict = np.zeros(instances.shape[0])
        for i in range(len(self.decision_infos)):
            decision_info = self.decision_infos[i]
            decision_weight = self.decision_weights[i]
            predict += decision_weight*self._decision_stump_instance_result(decision_info, instances)
        pred_result = np.sign(predict)
        # 如果正好为0，则设置默认为1
        pred_result[pred_result == 0] = 1
        return pred_result

    @staticmethod
    def _decision_stump_instance_result(decision_info, instances):
        """

        :param decision_info:
        :return:  [true, false, true]  #分类正确与错误
        """
        feature = instances.T[decision_info['decision_feature_index']]
        decision_feature_value = decision_info['decision_feature_value']

        decision_label = np.zeros(instances.shape[0])
        if decision_info['decision_type'] == '<':
            decision_label[feature < decision_feature_value] = 1
            decision_label[feature >= decision_feature_value] = -1
        else:
            decision_label[feature >= decision_feature_value] = 1
            decision_label[feature < decision_feature_value] = -1
        return decision_label

    def _decision_preprocess(self):
        """
        决策依据预计算
        :return:
        """
        feature_matrix = np.zeros((self.instances_dim, self.instances_num))
        label_matrix = np.zeros((self.instances_dim, self.instances_num))
        argsort_matrix = np.zeros((self.instances_dim, self.instances_num))

        features_decision_pos = []
        for i in range(self.instances_dim):
            dim = self.instances[:, i]
            tmp = np.stack((dim, self.labels))
            argsort = np.argsort(dim)
            dim_sort = tmp.T[argsort].T
            # dim_sort = np.array(sorted(tmp.T, key=(lambda x: x[0]))).T

            argsort_matrix[i] = argsort
            feature_matrix[i] = dim_sort[0]
            label_matrix[i] = dim_sort[1]
            # 找到label反转的位置
            feature_decision_pos = []
            for idx in range(1, self.instances_num):
                if label_matrix[i][idx-1] != label_matrix[i][idx]:
                    feature_decision_pos.append(idx)
            features_decision_pos.append(feature_decision_pos)

        self.feature_matrix = feature_matrix
        self.label_matrix = label_matrix
        self.features_decision_pos = features_decision_pos
        self.argsort_matrix = argsort_matrix.astype('int32')

    def decision_stump(self, weights):
        """
        根据权重计算决策桩，单层决策树
        :param weights: shape=(instances_num,) 样本对应权重
        :return:
        """
        min_error_rate = 1
        decision_feature_index = -1
        decision_feature_value = 0
        decision_type = '<'  # '<', '>'
        decision_value = 1  # 即满足decision条件的样本值为1

        # weight_matrix = self.label_matrix * weights
        for feature_index in range(self.instances_dim):
            # 该feature维度的样本权重
            # 此处出问题了。 self.label_matrix 按特征有序。 weights 原始样本序。
            feature_weight = self.label_matrix[feature_index] * weights[self.argsort_matrix[feature_index]]

            features_decision_pos = self.features_decision_pos[feature_index]
            for reverse_index in features_decision_pos:

                # 左边的错误率
                left_error = abs(feature_weight[:reverse_index][(self.label_matrix[feature_index] == -1)[:reverse_index]]).sum()
                # 右边的错误率
                right_error = abs(feature_weight[reverse_index:][(self.label_matrix[feature_index] == 1)[reverse_index:]]).sum()
                # 左预测1，右预测-1
                less_error = abs(left_error) + abs(right_error)
                # 左预测-1，右预测1
                greater_error = 1 - less_error

                if less_error < min_error_rate:
                    decision_feature_index = feature_index
                    decision_feature_value = (self.feature_matrix[feature_index][reverse_index]+self.feature_matrix[feature_index][reverse_index-1])/2
                    decision_type = '<'
                    min_error_rate = less_error
                if greater_error < min_error_rate:
                    decision_feature_index = feature_index
                    decision_feature_value = (self.feature_matrix[feature_index][reverse_index]+self.feature_matrix[feature_index][reverse_index-1])/2
                    decision_type = '>'
                    min_error_rate = greater_error

        return {'decision_feature_index': decision_feature_index,
                'decision_feature_value': decision_feature_value,
                'decision_type': decision_type,
                'min_error_rate': min_error_rate,
                'decision_value': decision_value
                }

def iris():
    iris = load_iris()
    X = iris.data
    y = iris.target
    # 转换为 1/-1 的二分类数据
    data_index = y != 1
    X = X[data_index]
    y = y[data_index]-1
    # print(y)

    # split train test
    return train_test_split(
        X, y, test_size=0.33, random_state=1)

def cancer():
    breast_cancer = load_breast_cancer()
    X = breast_cancer.data
    y = (breast_cancer.target * 2) - 1
    return train_test_split(
        X, y, test_size=0.33, random_state=3)

if __name__ == "__main__":

    # X_train, X_test, y_train, y_test = iris()
    # iris数据集什么情况？
    # (X_train[:, 2] > 3.2).astype('int') * 2 - 1 == y_train

    X_train, X_test, y_train, y_test = cancer()
    adaboost = AdaBoosting(verbose=1, n_estimators=40)
    adaboost.fit(X_train, y_train)
    # adaboost.fit(np.array([0,1,2,3,4,5,6,7,8,9])[::-1].reshape((10, 1)),
    #              np.array([1,1,1,-1,-1,-1,1,1,1,-1])[::-1])
    # adaboost.fit(np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]).reshape((10, 1)),
    #              np.array([1, 1, 1, -1, -1, -1, 1, 1, 1, -1]))

    error_rate = adaboost.error_rate(X_test, y_test)
    print("my ada: %s" % (1 - error_rate))

    from sklearn.linear_model import LogisticRegression
    clf = LogisticRegression(random_state=0, solver='lbfgs').fit(X_train, (y_train+1)/2)
    # lr_pred = clf.predict(X_test)
    print("LR: %s" % clf.score(X_test, (y_test+1)/2))

    from sklearn.ensemble import AdaBoostClassifier
    from sklearn.tree import DecisionTreeClassifier
    bdt = AdaBoostClassifier(DecisionTreeClassifier(max_depth=1), n_estimators=200)
    bdt.fit(X_train, y_train)
    print("sklearn ada: %s" % bdt.score(X_test, y_test))


text:
# Imports
import matplotlib.pyplot as plt
import numpy as np
from typing import Any, Callable

def compute_responsibility(S:np.ndarray, R:np.ndarray, A:np.ndarray,
                           dampfac:float=0.5)->np.ndarray:
    '''Computes responsibilities
    Params:
        S: n by n matrix of similarities
        R: n by n matrix of current responsibilities
        A: n by n matrix of current availabilities
        dampfac: Damping factor used to calculate new responsibilities. Acts as weight
                 for the weighted sum of the old and new responsibilities.
    Returns:
        A matrix containing the new responsibilities.
    '''

    # Prepare the contents of the max
    to_max = A+S

    # "Empty" diagonal because it is not used in the max
    np.fill_diagonal(to_max,-np.inf)

    # Get row indices for subsetting the maximum values
    row_indices = np.arange(to_max.shape[0])

    # Get column indices of maximum values
    max_indices = np.argmax(to_max, axis=1)

    # Get maximum of each row
    row_max = to_max[row_indices, max_indices]
    # Assign -inf to previous maxima to get maxima without
    # "actual maxima"
    to_max[row_indices, max_indices] = -np.inf

    # Get secondary maximum of each row
    row_max_without = to_max[row_indices,
                                 np.argmax(to_max, axis=1)]

    # Create matrix of max(a(i, k') + s(i,k'))
    max_AS = np.zeros_like(S) + row_max.reshape(-1,1)

    # Modify values of those indices where there were maxima
    # because k' \neq k
    max_AS[row_indices, max_indices] = row_max_without

    return (1-dampfac) * (S - max_AS) + dampfac * R

def compute_availability(R:np.ndarray, A:np.ndarray,
                         dampfac:float=0.5)->np.ndarray:
    '''Computes availabilities
    Params:
        R: n by n matrix of current responsibilities
        A: n by n matrix of current availabilities
        dampfac: Damping factor used to calculate new availabilities. Acts as weight
                 for the weighted sum of the old and new availabilities.
    Returns:
        A matrix containing the new responsibilities.
    '''
    R = R.copy()
    Rdiag = np.diag(R).copy()
    np.fill_diagonal(R,0)  # Fill diagonal with 0
    R = np.where(R<0, 0, R) # Replace all negative responsibilities with 0

    # Compute availabilities

    # First, make matrix with column sum in each cell of that col
    # Note: This is still without diagonal / negative values
    Rsums = np.sum(R, axis=0)

    # a(i,k) = min(0, r(k,k) + sum(max(0,r(i',k)))
    new_A = np.minimum(  # min()
        0,  # 0
        Rdiag +  # r(k,k)
        Rsums - R  # sum(max(0,r(i', k)))
    )

    # Compute self-availabilities
    # Note that diagonal in R is 0
    np.fill_diagonal(new_A, np.sum(R, axis=0))
    return (1-dampfac)*new_A + dampfac*A

def sqeucl(u:np.ndarray, v:np.ndarray, axis:int=-1)->float:
    '''Given two vectors, calculates squared Euclidean Distance'''
    return(sqeucl_norm(u-v, axis=axis))

def sqeucl_norm(u, axis=-1):
    '''Calculates the squared euclidean norm of u along axis.'''
    return (u**2).sum(axis)

def neg_sqeucl(u:np.ndarray=None, v:np.ndarray=None,
               M:np.ndarray=None, axis:int=-1)->float:
    ''' Given two vectors, calculates negative squared Euclidean Distance.
        If fed with a matrix M, calculates pairwise row distances of M.
    Params:
        u, v: Pair of vectors to calculate distance for.
        M: optional, matrix to calculate pairwise row distances for.
        axis: Axis to calculate distance along (for broadcasting).
    Returns:
        A scalar for single vectors u, v. Otherwise a distance matrix.
    '''
    if M is not None:
        return -1 * (np.sum(M**2,axis=-1).reshape(-1,1) + \
                np.sum(M**2,axis=-1) - \
                2 * np.dot(M,M.T))
    else:
        if u is None or v is None:
            raise ValueError("If M is not given, both u and v have to given.")
        return(-sqeucl(u,v, axis=axis))

def cosim_one(u:np.ndarray, v:np.ndarray, axis:int=-1)->float:
    '''Given two vectors, calculates Cosine Similarity'''
    return (u@v)/(np.sqrt(u@u)*np.sqrt(v@v))

def cosim(u:np.ndarray=None, v:np.ndarray=None, M:np.ndarray=None):
    ''' Given two vectors, calculates cosine similarity.
        If fed with a matrix M, calculates pairwise row similarities of M.
    Params:
        u, v: Pair of vectors to calculate distance for.
        M: optional, matrix to calculate pairwise row distances for.
    Returns:
        A scalar for single vectors u, v. Otherwise a distance matrix.
    '''
    if M is not None:
        return np.dot(M,M.T)/(np.sqrt(np.sum(M**2,axis=-1).reshape(-1,1))*np.sqrt(np.sum(M**2,axis=-1)))
    else:
        if u is None or v is None:
            raise ValueError("If M is not given, both u and v have to given.")
        return(cosim_one(u,v))

def compute_similarity(M:np.ndarray,
                       func:Callable[[np.ndarray, np.ndarray], float]=neg_sqeucl,
                       measure="eucl",
                       u:np.ndarray=None, v:np.ndarray=None, axis:int=0)->np.ndarray:
    '''Computes negative euclidean distance (Similarities)
    Params:
        M: An n by p matrix to calculate similarities for
        func similarity measure
        u, v: Pair of vectors. If given, calculates similarity just for these two.
        axis: Treat either rows (0) as "observations" to calculate similarities for,
              or columns (1). If there are n observations with p features, similarities
              of observations will be calculated with axis=0 (default).
        measure: Only valid when M is given. Similarity measure for the similarity matrix.
                 Either "eucl" (default) for negative squared euclidean distance or "cos"
                 for cosine similarity.
    '''

    if M is None and (u is None or v is None):
        raise ValueError("Please specify either M or u AND v.")

    # If u and v are given, just calculate their similarity
    if u != None and v != None:
        return(func(u,v))

    # Else calculate similarity matrix for M
    if measure == "eucl":
        if axis == 0:
            return neg_sqeucl(M=M)
        if axis == 1:
            return neg_sqeucl(M=M.T)
    elif measure == "cos":
        if axis == 0:
            return cosim(M=M)
        if axis == 1:
            return cosim(M=M.T)
    else:
        raise ValueError(f"'measure' must either be 'eucl' or 'cos', not '{measure}'.")

def give_preferences(S:np.ndarray, preference:Any="median")->np.ndarray:
    '''Takes a similarity matrix and assigns equal "preferences" (values on the diagonal)
       according to the median of all similarities (excluding the diagonal).
    Params:
        S: Similarity matrix (n x n)
        preferences: Either "median", "min", a scalar numeric, or an n-dimensional np.ndarray
                     For details see README.
    '''
    indices = np.where(~np.eye(S.shape[0],dtype=bool))
    if preference == "median":
        m = np.median(S[indices])
    elif preference == "min":
        m = np.min(S[indices])
    elif type(preference) == np.ndarray:
        m = preference
    else:
        try:
            m = float(preference)
        except ValueError:
            raise ValueError("Parameter 'preference' must either be 'median', 'min', a np.ndarray or a scalar.")

    np.fill_diagonal(S, m)
    return S

def affinity_prop(X:np.ndarray, maxiter:int=100, preference:Any="median",
                  damping_factor:float=0.7, local_thresh:int=0,
                  message_thresh:int=0, calc_sim:bool=True,
                  sim_measure:str="eucl", calc_sim_axis:int=0, verbose:bool=True):
    '''Performs affinity propagation clustering.
    Params:
        X: Input matrix with data to cluster.
        maxiter: Maximum iterations after which to stop the clustering if it
                 does not converge before.
        preference: Either 'median' (default), 'min', a vector of the same size as the input data,
                    or a fixed scalar value. Determines the initial "preferences", i.e., self-similarities:
                    values on the diagonal of the similarity matrix. Details in the README.
        damping_factor: Damping factor used to calculate new availabilities and
                        responsibilities. Acts as weight for the weighted sum of
                        the old and new availabilities or responsibilities,
                        respectively. Lower values will lead to slower convergence, higher values will
                        prevent oscillation.
        local_thresh: Number of iterations without any change in the outcome labels before the algorithm stops.
        message_thresh: Threshold passed into np.allclose() used to stop the algorithm after messages
                        fall below that threshold.
        calc_sim: Whether or not to calculate a similarity matrix from the input data. Set to False if input
                  is already a similarity matrix (default: True).
        sim_measure: Similarity measure for the similarity matrix. Either "eucl" (default) for negative
                     squared euclidean distance or "cos" for cosine similarity.
        calc_sim_axis: Indicates whether similarity should be calculated between rows (0, default) or columns (1).
        verbose: Whether to be verbose.
    Returns:
        3-tuple of exemplars, labels (indicating the exemplar for every point),
        and centers (locations of the exemplars).
    '''

    X = np.asarray(X)

    # Convert input data into similarity matrix and add preferences
    if calc_sim:
        S = compute_similarity(X, axis=calc_sim_axis, measure=sim_measure)
    else:
        S = X
    S = give_preferences(S, preference=preference)

    # Initialize messages
    A = np.zeros_like(S)
    R = np.zeros_like(S)

    # Remove degeneracies to avoid oscillating
    S = S+1e-12*np.random.normal(size=A.shape) * (np.max(S)-np.min(S))

    # Initialize counting / indexing variables
    count_equal = 0
    i = 0
    converged = False

    if local_thresh == 0 and message_thresh == 0:
        while i < maxiter:
            R = compute_responsibility(S, R, A, dampfac=damping_factor)
            A = compute_availability(R, A, dampfac=damping_factor)
            i += 1

    else:
        # Stop if either maxiter is reached or local decisions stay constant
        while i < maxiter:
            E_old = R+A
            labels_old = np.argmax(E_old, axis=1)
            R = compute_responsibility(S, R, A, dampfac=damping_factor)
            A = compute_availability(R, A, dampfac=damping_factor)
            E_new = R+A
            labels_cur = np.argmax(E_new, axis=1)

            # Check if local decisions stayed constant
            if np.all(labels_cur == labels_old):
                count_equal += 1
            else:
                count_equal = 0

            # Check if messages changed
            if (message_thresh != 0 and np.allclose(E_old, E_new, atol=message_thresh)) or\
                (local_thresh != 0 and count_equal > local_thresh):
                converged = True
                break
            i += 1

    if verbose:
        if converged:
            print(f"Converged after {i} iterations.")
        else:
            print(f"Stopped after {maxiter} iterations.")

    E = R+A # Pseudomarginals
    labels = np.argmax(E, axis=1)
    exemplars = np.unique(labels)
    centers = X[exemplars]

    # Replace indices by increasing numbers
    replace = dict(zip(exemplars, range(len(exemplars))))
    mp = np.arange(0,max(labels)+1)
    mp[list(replace.keys())] = list(replace.values())
    labels = mp[labels]

    return exemplars, labels, centers

def cplot(data:np.ndarray, labels:np.ndarray, cmap:str="Set1")->None:
    '''Given two-dimensional data and cluster labels, plots data
       cluster assignment.
    Params:
        data: n by 2-dimensional array of input data
        labels: Array of cluster assignments of data
        cmap: Optional cmap for the plot (Default:"Set1")
    '''
    fig, ax = plt.subplots(figsize=(5, 5))
    ax.scatter(data.T[0], data.T[1], c=labels, cmap=cmap)
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)
    plt.show()
    return None

max_stars_repo_name: thiesben/affinity-propagation-python

max_issues_repo_name: thiesben/affinity-propagation-python

max_forks_repo_name: thiesben/affinity-propagation-python


text:
#run python3 decom_sequence_generator.py file_with_the_following_format
# the file for this script should be image_name va1l val2.... valn.
import sys
import numpy as np
import csv
import ast
import datetime
import argparse
from operator import itemgetter
from functools import reduce
from scipy.spatial import distance
import math
import sequence

def key_func(x):
    # For some year like 2011 the year is 2 digits so the date format should ne %m%d%y but for others like 2015 it should be %m%d%Y
    try:
        #date = ""
        if '(' in x:
            date_ = x.split('D_')[-1].split('(')[0].strip()
        else:
            date_ = x.split('D_')[-1].split('.')[0].strip()
        mdy = date_.split('_')
        m = mdy[0]
        d = mdy[1]
        y = mdy[2]
        if len(m) == 1:
            m = '0' + m
        if len(d) == 1:
            d = '0' + d
        date_  = m + d + y
        if len(date_) == 6: #the format that has 2 digits for year
            return datetime.datetime.strptime(date_, '%m%d%y')
        else:
            return datetime.datetime.strptime(date_, '%m%d%Y')

    except:
        print(x)
        import bpython
        bpython.embed(locals())
        exit()

def sort_dates(donors2imgs): #sorts the dates by getting a list of img_names for each donor and sorting that
    for key in donors2imgs:
        donors2imgs[key] = sorted(donors2imgs[key], key=key_func)
    return donors2imgs

def convert_to_time(img_name):
    if '(' in img_name:
        date_ = img_name.split('D_')[-1].split('(')[0].strip()
    else:
        date_ = img_name.split('D_')[-1].split('.')[0].strip()
    mdy = date_.split('_')
    m = mdy[0]
    d = mdy[1]
    y = mdy[2]
    if len(m) == 1:
        m = '0' + m
    if len(d) == 1:
        d = '0' + d
    date_  = m + d + y
    if len(date_) == 6: #the format that has 2 digits for year
        return datetime.datetime.strptime(date_, '%m%d%y')
    else:
        return datetime.datetime.strptime(date_, '%m%d%Y')

def cal_day_from_deth(donors2imgs_sorted):
    for key in donors2imgs_sorted:
        day2imgs = {}
        first_img = True
        for img in donors2imgs_sorted[key]:
            if first_img == True:
                start_time = convert_to_time(img)
                first_img = False
            img_time = convert_to_time(img)
            time_from_start = (img_time - start_time).days
            if time_from_start not in day2imgs:
                day2imgs[time_from_start] = []
            day2imgs[time_from_start].append(img)
        donors2imgs_sorted[key] = day2imgs
    return donors2imgs_sorted
    # this a dictionary with each donor_id as keys and values are another
    #dictionary with keys being xth days since day one and the values are a
    #list of images that belong to day xth for that donor.

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--embeding_file', type = str)

    args = parser.parse_args()

    embedings_file = args.embeding_file #sys.argv[1] # This should be a pca version the embedings

    donors2imgs = {}
    donors2img2embed = {}
    imgname2add = {}

    with open(embedings_file, 'r') as csv_file:
        data = csv.reader(csv_file,delimiter = '\n')
        vectors = []
        for row in data:
            row = row[0]
            row= row.split('JPG')
            img_name = row[0] + 'JPG'
            embeding = row[1].strip()
            embeding = embeding.replace(' ', ',')
            embeding = ast.literal_eval("[" + embeding[1:-1] + "]") # this embeding is a list now
            donor_id = img_name.split('/Daily')[0].split('/')[-1]
            if donor_id not in donors2img2embed and donor_id not in donors2imgs:
                donors2img2embed[donor_id] = {}
                donors2imgs[donor_id] = [] # a list for all of the images belonging to the same donor
            donors2img2embed[donor_id][img_name] = embeding
            # this a dictionary with each donor_id as keys and values are another dictionary
            # with keys being an image and the values being the feature vector for that imag

            donors2imgs[donor_id].append(img_name)

        donors2imgs_sorted = sort_dates(donors2imgs) # this sorts the images for a donor based on their dates
        donor2day2imgs = cal_day_from_deth(donors2imgs_sorted)

        day2clus2emb = sequence.sequence_finder(donors2img2embed, donor2day2imgs)

reason: import numpy,from scipy


text:
#!/usr/bin/env python
"""
Implement help functions
"""
import pandas as pd
import numpy as np

def delta_date_feature(dates):
    """
    Given a 2d array containing dates (in any format recognized by
    pd.to_datetime), it returns the delta in days between each date and the
    most recent date in its column
    """
    date_sanitized = pd.DataFrame(dates).apply(pd.to_datetime)
    return (date_sanitized
            .apply(lambda d: (d.max() - d).dt.days, axis=0)
            .to_numpy())


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

A = np.random.rand(5,5)
plt.figure(1)
plt.imshow(A, interpolation='nearest')
plt.grid(True)

plt.figure(2)
plt.imshow(A, interpolation='bilinear')
plt.grid(True)

plt.figure(3)
plt.imshow(A, interpolation='bicubic')
plt.grid(True)

plt.show()


text:
import numpy as np
import pandas as pd

def get_data():
    """Python function to prepare data."""

    return pd.DataFrame(
        data={
            "int_vals": [1, np.nan, 3],
            "float_vals": [2.6, 6.3, np.nan],
            "str_vals": ["val1", "val2", "val3"],
        },
        index = ["idx1", "idx2", "idx3"]
    )

max_stars_repo_name: NoobQuant/CodingExamples

max_issues_repo_name: NoobQuant/CodingExamples

max_forks_repo_name: NoobQuant/CodingExamples


text:
# # -*- coding: utf-8 -*-
# import numpy as np
#
# import scipy.io as sio
#
#
# def sampling(groundTruth):              #divide dataset into train and test datasets
#     labeled = {}
#     test = {}
#     valid = {}
#     m = max(groundTruth)
#     labeled_indices = []
#     test_indices = []
#     valid_indices = []
#     unlabeled_indices = []
#
#     for i in range(m+1):
#         indices = [j for j, x in enumerate(groundTruth.ravel().tolist()) if x == i]
#         if i == 0:
#             np.random.shuffle(indices)
#             unlabeled_indices = indices
#         else:
#             if i == 4 or i == 9 or i ==3:
#                 np.random.shuffle(indices)
#                 test[i] = indices[70:]
#                 valid[i] = indices[17:70]
#                 labeled[i] = indices[:17]
#                 labeled_indices += labeled[i]
#                 test_indices += test[i]
#                 valid_indices += valid[i]
#             else:
#                 if i == 1 or i == 8:
#                     np.random.shuffle(indices)
#                     test[i] = indices[1000:]
#                     valid[i] = indices[285:1000]
#                     labeled[i] = indices[:285]
#                     labeled_indices += labeled[i]
#                     test_indices += test[i]
#                     valid_indices += valid[i]
#                 else:
#                     np.random.shuffle(indices)
#                     test[i] = indices[170:]
#                     valid[i] = indices[42:170]
#                     labeled[i] = indices[:42]
#                     labeled_indices += labeled[i]
#                     test_indices += test[i]
#                     valid_indices += valid[i]
#     np.random.shuffle(labeled_indices)
#     np.random.shuffle(test_indices)
#     np.random.shuffle(valid_indices)
#
#     return labeled_indices, test_indices, valid_indices, unlabeled_indices
#
#
# mat_gt = sio.loadmat("/home/asdf/Documents/juyan/paper/pavia/Pavia_gt.mat")
# gt_IN = mat_gt['pavia_gt']
# new_gt_IN = gt_IN
#
# gt = new_gt_IN.reshape(np.prod(new_gt_IN.shape[:2]),)
#
#
# labeled_indices, test_indices, valid_indices, unlabeled_indices = sampling(gt)
#
# print(len(labeled_indices))
# print(len(valid_indices))
# print(len(unlabeled_indices))
# print(len(test_indices))
#
# np.save('/home/asdf/Documents/juyan/paper/pavia/labeled_index.npy', labeled_indices)
# np.save('/home/asdf/Documents/juyan/paper/pavia/valid_index.npy', valid_indices)
# np.save('/home/asdf/Documents/juyan/paper/pavia/unlabeled_index.npy', unlabeled_indices)
# np.save('/home/asdf/Documents/juyan/paper/pavia/test_index.npy', test_indices)
#
#
# -*- coding: utf-8 -*-
import numpy as np

import scipy.io as sio

def sampling(groundTruth):              #divide dataset into train and test datasets
    all = {}
    m = max(groundTruth)
    all_indices = []
    for i in range(m+1):
        indices = [j for j, x in enumerate(groundTruth.ravel().tolist()) if x == i]
        if i != 0:
            np.random.shuffle(indices)
            all[i] = indices
            all_indices += all[i]
    np.random.shuffle(all_indices)
    return all_indices

mat_gt = sio.loadmat("/home/asdf/Documents/juyan/paper/salinas/cnn/data/Salinas_gt.mat")
gt_IN = mat_gt['salinas_gt']
new_gt_IN = gt_IN
gt = new_gt_IN.reshape(np.prod(new_gt_IN.shape[:2]),)
all_indices = sampling(gt)

print(len(all_indices))

np.save('/home/asdf/Documents/juyan/paper/salinas/cnn/data/all_index.npy', all_indices)

reason: import numpy,import scipy


text:
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from onconet.models.group_norm import GroupNorm
from onconet.models.blocks.factory import RegisterBlock
import pdb

UNEXPECTED_INPUT_SIZE_ERR = "Unexpected input size! Expected a 5D tensor, instead got size {}"

@RegisterBlock('ACABlock')
class AttendCompareAggregateBlock(nn.Module):
    """Attend Compare Aggregate block."""
    expansion = 1

    def __init__(self, args, inplanes, planes, stride=1, downsample=None,
                 compression_factor=2):
        """Initializes the ACABlock.

        Arguments:
            inplanes(int): The depth (number of channels) of the input.
            planes(int): The depth (number of channels) of the output.
            downsample(nn.Module): When not none, used to downsample output to planes.

            compression_factor(int): The compression factor to use when
                performing max pooling over attention mem and attention val to speed up computation. Note, Not implemented.
        """

        super(AttendCompareAggregateBlock, self).__init__()
        self.compression_factor = compression_factor
        self.num_images = args.num_images
        self.attend_module = AttendModule(args, inplanes, compression_factor)
        self.compare_module = CompareModule(args, inplanes, self.num_images)
        self.aggregate_module = AggregateModule(args, inplanes, inplanes, self.num_images)
        self.relu = nn.ReLU(inplace=True)
        self.downsample = downsample

    def forward(self, x):
        """Computes a forward pass of the model.

        Arguments:
            x(Variable): The input to the model.

        Returns:
            The result of feeding the input through the model.
        """

        # Save residual
        if self.compression_factor > 1:
            x = F.max_pool3d(x, kernel_size=(1,3,3), stride=(1,self.compression_factor,self.compression_factor))

        residual = x

        R, V = self.attend_module(x)
        C = self.compare_module(R, V)
        Z = self.aggregate_module(R, V, C)

        out = x+Z
        out = self.relu(out)

        if self.downsample is not None:
            out = self.downsample(out)

        return out

class AttendModule(nn.Module):
    """
        Attend step of ACA. For each pixel each frame, get a attention distribution over all other frames. i.e starting from a T, H, W volume,
        produce an attention map of of T, T, H, W.

    """

    def __init__(self, args, inplanes, compression_factor=2):

        super(AttendModule, self).__init__()

        self.query_conv = nn.Conv3d(inplanes,
                                    inplanes // 2,
                                    kernel_size=1,
                                    bias=False)
        self.memory_conv = nn.Conv3d(inplanes,
                                  inplanes // 2,
                                  kernel_size=1,
                                  bias=False)
        self.value_conv = nn.Conv3d(inplanes,
                                inplanes // 2,
                                kernel_size=1,
                                bias=False)

    def forward(self, x):
        """Computes a attention step of ACA block.

        Arguments:
            x(Variable): The input to the model.

        Returns:
            R -  (B, C/2, T, T, H, W) tensor representing the attended vector at each timestep for each pixel
            V - (B, C/2, T, H, W) tensor. R = A * V
        """

        dims = x.size()

        if len(dims) == 5:
            batch_size, inplanes, time, height, width = dims
        else:
            raise Exception(UNEXPECTED_INPUT_SIZE_ERR.format(dims))

        # Compute Q, M, V all of size (B, C/2, T, H, W)
        Q = self.query_conv(x)
        M = self.memory_conv(x)
        V = self.value_conv(x)

        V_i = V # Save copy of V in shape (B, C/2, T, H, W)

        # Reshape Q, M to (B, C/2, THW)
        Q = Q.view(batch_size, inplanes//2, -1)
        M = M.view(batch_size, inplanes//2, -1)
        # Reshape Q to (B, TWH, C/2)
        Q = torch.transpose(Q, 1,2)

        # Reshape V to (BT, HW, C/2)
        V = torch.transpose(V, 1, 2) #( B, T, C/2, H, W)
        V = V.contiguous().view(batch_size * time, inplanes//2, height * width)
        V = torch.transpose(V, 1, 2)

        # Compute attention, shape is (B, THW, THW)
        # Scaling by sqrt(dim) is inspired by https://arxiv.org/pdf/1706.03762.pdf
        A = torch.bmm(Q, M) / np.sqrt(inplanes // 2)
        # Reshape A (B, THW, THW) to (BT, THW, HW)
        A = A.contiguous().view(batch_size, -1, time, height*width) #( B, TWH, T, HW)
        A = torch.transpose(A, 1, 2) #( B, T, TWH, HW)
        A = A.contiguous().view( batch_size * time, -1, height*width)
        A = F.softmax( A, dim=-1)

        # Compute R = A * V, output of shape (BT, THW, C/2)
        R = torch.bmm(A, V)

        # Reshape R to (B, C/2, T, T, H, W)
        R = R.view( batch_size, time, time, height, width, inplanes //2)
        R = R.permute(0, 5, 1, 2, 3, 4)
        return R, V_i

class CompareModule(nn.Module):
    """
        Compare step of ACA. Given a feature vec Vi, compare it in the
        style of NLI to all corresponding attended vectors in R.
    """
    def __init__(self, args, inplanes, num_images):

        super(CompareModule, self).__init__()

        self.cat_conv = nn.Conv3d(inplanes,
                                inplanes // (num_images * 3),
                                kernel_size=1,
                                bias=False)

        self.sub_conv = nn.Conv3d(inplanes // 2,
                                inplanes // (num_images * 3),
                                kernel_size=1,
                                bias=False)

        self.mul_conv = nn.Conv3d(inplanes // 2,
                                inplanes // (num_images * 3),
                                kernel_size=1,
                                bias=False)

        Norm = GroupNorm if args.replace_bn_with_gn else nn.BatchNorm2d
        self.cat_bn = Norm(inplanes // (num_images * 3))
        self.sub_bn = Norm(inplanes // (num_images * 3))
        self.mul_bn = Norm(inplanes // (num_images * 3))

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

    def forward(self, R, V):
        '''
            For each pixel in V, compare to all attended time steps in R.
            via fc(V-R), fc(V*R), fc(V;R)

            args:
            R: (B, C/2, T, T, H, W) tensor
            V:  (B, C/2, T, H, W) tensor

            returns:
            C: (B, C', T, H, W, comparison features
        '''
        dims = V.size()

        if len(dims) == 5:
            batch_size, chan, time, height, width = dims
        else:
            raise Exception(UNEXPECTED_INPUT_SIZE_ERR.format(dims))

        V_exp = V.unsqueeze(0)
        V_exp = V_exp.expand(time, batch_size, chan, time, height, width)
        V_exp = V_exp.permute(1,2,3,0,4,5)
        V_exp = V_exp.contiguous().view(batch_size, chan, time*time, height, width)

        R = R.contiguous().view(batch_size, chan, time*time, height, width)

        X_cat = torch.cat([R, V_exp], dim=1)
        X_sub = R - V_exp
        X_mul = R * V_exp

        C_cat = self.cat_bn(self.cat_conv(X_cat))
        C_sub = self.sub_bn(self.sub_conv(X_sub))
        C_mul = self.mul_bn(self.mul_conv(X_mul))
        C = torch.cat([C_cat, C_sub, C_mul], dim=1)

        C = C.view(batch_size, -1, time, height, width)

        # channel dim = (inplanes // (num_images * 3)) * 3 * num_images
        C = self.relu(C)
        return C

class AggregateModule(nn.Module):
    """
        Aggregate step of ACA. Given input features, attended features,
        and comparison features, compress down to a single space and model
        cross channel dependecies.
    """
    def __init__(self, args, inplanes, planes, num_images):

        super(AggregateModule, self).__init__()
        c_dim = (inplanes // (num_images*3))*3*num_images
        in_dim = inplanes  + c_dim

        self.agg_conv = nn.Conv3d(in_dim,
                                planes,
                                kernel_size=1,
                                bias=False)

        Norm = GroupNorm if args.replace_bn_with_gn else nn.BatchNorm2d
        self.agg_bn = Norm(planes)

    def forward(self, R, V, C):
        '''
            Args:
            R: (B, C/2, T, T, W, H),  attention features
            V: Shape (B, C/2, T, W, H), input features
            C: Shape (B, C', T, W, H), comparison features

            returns,
            Z: Shape (B, C, T, W, H), aggregated result
        '''
        R = torch.mean(R, dim=3)

        Y = torch.cat([R,V,C], dim=1)

        Z = self.agg_bn(self.agg_conv(Y))
        return Z

max_stars_repo_name: harrivle/Mirai

max_issues_repo_name: NkwamPhilip/Mirai

max_forks_repo_name: NkwamPhilip/Mirai


text:
import warnings

from numpy import VisibleDeprecationWarning
warnings.warn("`inputs_and_masks` has been renamed `transforms` since asteroid "
              "v0.1.0 and will be removed in v0.2.0", VisibleDeprecationWarning)
from .transforms import *

max_stars_repo_name: michelolzam/asteroid

max_issues_repo_name: michelolzam/asteroid

max_forks_repo_name: michelolzam/asteroid

reason: from numpy