text:
from __future__ import print_function

import argparse
import collections
import os
from datetime import datetime, timedelta

import numpy as np
import tensorflow as tf
import urllib3
from minio import Minio
from minio.error import (BucketAlreadyExists, BucketAlreadyOwnedByYou,
                         ResponseError)
from tensorflow.keras.callbacks import ModelCheckpoint
from tensorflow.keras.layers import (LSTM, Activation, Dense, Dropout,
                                     Embedding, TimeDistributed)
from tensorflow.keras.models import Sequential, load_model
from tensorflow.keras.utils import to_categorical
from tqdm import tqdm

from DataManager import DataFile
import string

DATA_PATH = "./tmp_lstm_data"

def read_words(filename):
    with tf.io.gfile.GFile(filename, "r") as f:
        return f.read().replace("\n", "<eos>").split()

def build_vocab(filename):
    data = read_words(filename)

    counter = collections.Counter(data)
    count_pairs = sorted(counter.items(), key=lambda x: (-x[1], x[0]))

    words, _ = list(zip(*count_pairs))
    word_to_id = dict(zip(words, range(len(words))))

    return word_to_id

def file_to_word_ids(filename, word_to_id):
    data = read_words(filename)
    return [word_to_id[word] for word in data if word in word_to_id]

def load_data():
    # get the data paths
    train_path = os.path.join(DATA_PATH, "requests.train.txt")
    valid_path = os.path.join(DATA_PATH, "requests.valid.txt")
    test_path = os.path.join(DATA_PATH, "requests.test.txt")

    # build the complete vocabulary, then convert text data to list of integers
    word_to_id = build_vocab(train_path)
    train_data = file_to_word_ids(train_path, word_to_id)
    valid_data = file_to_word_ids(valid_path, word_to_id)
    test_data = file_to_word_ids(test_path, word_to_id)
    vocabulary = len(word_to_id)
    reversed_dictionary = dict(zip(word_to_id.values(), word_to_id.keys()))

    # print(train_data[:5])
    # print(word_to_id)
    # print(vocabulary)
    # print(" ".join([reversed_dictionary[x] for x in train_data[:10]]))
    return train_data, valid_data, test_data, vocabulary, reversed_dictionary

class KerasBatchGenerator(object):

    def __init__(self, data, num_steps, batch_size, vocabulary, skip_step=5):
        self.data = data
        self.num_steps = num_steps
        self.batch_size = batch_size
        self.vocabulary = vocabulary
        # this will track the progress of the batches sequentially through the
        # data set - once the data reaches the end of the data set it will reset
        # back to zero
        self.current_idx = 0
        # skip_step is the number of words which will be skipped before the next
        # batch is skimmed from the data set
        self.skip_step = skip_step

    def generate(self):
        x = np.zeros((self.batch_size, self.num_steps))
        y = np.zeros((self.batch_size, self.num_steps, self.vocabulary))
        while True:
            for i in range(self.batch_size):
                if self.current_idx + self.num_steps >= len(self.data):
                    # reset the index back to the start of the data set
                    self.current_idx = 0
                x[i, :] = self.data[self.current_idx:self.current_idx + self.num_steps]
                temp_y = self.data[self.current_idx +
                                   1:self.current_idx + self.num_steps + 1]
                # convert all of temp_y into a one hot representation
                y[i, :, :] = to_categorical(
                    temp_y, num_classes=self.vocabulary)
                self.current_idx += self.skip_step
            yield x, y

def gen_model(vocabulary, num_steps: int = 16, hidden_size: int = 256, use_dropout: bool = True):

    model = Sequential()
    model.add(Embedding(vocabulary, hidden_size, input_length=num_steps))
    model.add(LSTM(hidden_size, return_sequences=True))
    model.add(LSTM(hidden_size//2, return_sequences=True))
    model.add(LSTM(hidden_size//4, return_sequences=True))
    if use_dropout:
        model.add(Dropout(0.5))
    model.add(TimeDistributed(Dense(vocabulary)))
    model.add(Activation('softmax'))

    model.compile(loss='categorical_crossentropy',
                  optimizer='adam', metrics=['categorical_accuracy'])

    print(model.summary())

    return model

def period(start_date, num_days):
    delta = timedelta(days=1)

    year, month, day = [int(elm) for elm in start_date.split()]
    cur_date = datetime(year, month, day)

    for _ in range(num_days):
        yield (cur_date.year, cur_date.month, cur_date.day)
        cur_date = cur_date+delta

def convert_record(record, max_depth: int = 7):
    tmp = "/".join(record.split("/")[:max_depth])
    for elm in string.punctuation:
        tmp = tmp.replace(elm, " ")
    return tmp + "\n"

def gen_data(start_date, window_size, minio_config, validation_stride: int = 100):
    os.makedirs(DATA_PATH, exist_ok=True)

    cert_reqs = "CERT_NONE"

    if cert_reqs == "CERT_NONE":
        urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

    httpClient = urllib3.PoolManager(
        timeout=urllib3.Timeout.DEFAULT_TIMEOUT,
        cert_reqs=cert_reqs,
        # ca_certs='public.crt',
        retries=urllib3.Retry(
            total=5,
            backoff_factor=0.2,
            status_forcelist=[500, 502, 503, 504]
        )
    )

    minio_url, minio_key, minio_secret, bucket = minio_config.split()
    minioClient = Minio(
        minio_url,
        access_key=minio_key,
        secret_key=minio_secret,
        secure=True,
        http_client=httpClient
    )

    start = "{} {} {}".format(*start_date.split("-"))
    size = int(window_size)

    # Train and validation set
    with open(os.path.join(DATA_PATH, "requests.train.txt"), "w") as train_set:
        with open(os.path.join(DATA_PATH, "requests.valid.txt"), "w") as validation_set:
            for year, month, day in period(start, size):
                print(f"[Original Data][{year}-{month}-{day}]")
                print("[Original Data][Download...]")
                try:
                    minioClient.fget_object(
                        f"{bucket}",
                        f'year{year}_month{month}_day{day}.json.gz',
                        './tmp.json.gz'
                    )
                except ResponseError:
                    raise
                print("[Original Data][Downloaded]")
                print("[Original Data][Open File]")
                collector = DataFile("./tmp.json.gz")
                counter = 0
                print(
                    "[Original Data][Write record in train set and validation set...]")
                for record in tqdm(collector, desc="Extracting records"):
                    cur_record = convert_record(record['FileName'])
                    # print(cur_record)
                    if counter < validation_stride:
                        train_set.write(cur_record)
                        counter += 1
                    else:
                        validation_set.write(cur_record)
                        counter = 0
                os.remove("./tmp.json.gz")

    # Test set
    last_date = datetime(year, month, day) + timedelta(days=1)
    start = f"{last_date.year} {last_date.month} {last_date.day}"
    with open(os.path.join(DATA_PATH, "requests.test.txt"), "w") as test_set:
        for year, month, day in period(start, size):
            print(f"[Original Data][{year}-{month}-{day}]")
            print("[Original Data][Download...]")
            try:
                minioClient.fget_object(
                    f"{bucket}",
                    f'year{year}_month{month}_day{day}.json.gz',
                    './tmp.json.gz'
                )
            except ResponseError:
                raise
            print("[Original Data][Downloaded]")
            print("[Original Data][Open File]")
            collector = DataFile("./tmp.json.gz")
            print("[Original Data][Write record in test set...]")
            for record in tqdm(collector, desc="Extracting records"):
                cur_record = convert_record(record['FileName'])
                if counter < validation_stride:
                    test_set.write(cur_record)
            os.remove("./tmp.json.gz")

def main():
    global DATA_PATH
    parser = argparse.ArgumentParser()
    parser.add_argument('run_opt', type=str, default="train",
                        choices=['train', 'test', 'data'])
    parser.add_argument('--data-path', type=str, default=DATA_PATH,
                        help='The full path of the training data')
    parser.add_argument('--num-epochs', type=int, default=10,
                        help='Number of epochs')
    parser.add_argument('--num-steps', type=int, default=16,
                        help='Number of steps')
    parser.add_argument('--batch-size', type=int, default=16,
                        help='Batch size')
    parser.add_argument('--hidden-size', type=int, default=512,
                        help='Hidden size')
    parser.add_argument('--gen-data-args', type=str, default="YY-MM-DD window_size",
                        help='gen_data_args')
    parser.add_argument('--minio-config', type=str, default="url key secret bucket",
                        help='minio configuration')
    args = parser.parse_args()

    if args.data_path:
        DATA_PATH = args.data_path

    # print(args)

    if args.run_opt == "data":
        gen_data(*args.gen_data_args.split(), args.minio_config)

    elif args.run_opt == "train":
        print("Loading data...")
        train_data, valid_data, test_data, vocabulary, reversed_dictionary = load_data()
        model = gen_model(vocabulary, args.num_steps, args.hidden_size)
        checkpointer = ModelCheckpoint(
            filepath=DATA_PATH + '/model-{epoch:02d}.hdf5', verbose=1
        )
        train_data_generator = KerasBatchGenerator(
            train_data, args.num_steps, args.batch_size, vocabulary,
            skip_step=args.num_steps
        )
        valid_data_generator = KerasBatchGenerator(
            valid_data, args.num_steps, args.batch_size, vocabulary,
            skip_step=args.num_steps
        )
        model.fit_generator(
            train_data_generator.generate(),
            len(train_data)//(args.batch_size*args.num_steps),
            args.num_epochs,
            validation_data=valid_data_generator.generate(),
            validation_steps=len(valid_data)//(args.batch_size*args.num_steps),
            callbacks=[checkpointer]
        )
        model.save(DATA_PATH + "/final_model.hdf5")

    elif args.run_opt == "test":
        print("Loading data...")
        train_data, valid_data, test_data, vocabulary, reversed_dictionary = load_data()
        model = load_model(DATA_PATH + "/final_model.hdf5")
        dummy_iters = 40
        example_training_generator = KerasBatchGenerator(
            train_data, args.num_steps, 1, vocabulary,
            skip_step=1
        )
        print("Training data:")
        for i in range(dummy_iters):
            dummy = next(example_training_generator.generate())
        num_predict = 10
        true_print_out = "Actual words: "
        pred_print_out = "Predicted words: "
        for i in range(num_predict):
            data = next(example_training_generator.generate())
            prediction = model.predict(data[0])
            predict_word = np.argmax(prediction[:, args.num_steps-1, :])
            true_print_out += reversed_dictionary[train_data[args.num_steps +
                                                             dummy_iters + i]] + " "
            pred_print_out += reversed_dictionary[predict_word] + " "
            print(true_print_out)
            print(pred_print_out)
        # test data set
        dummy_iters = 40
        example_test_generator = KerasBatchGenerator(
            test_data, args.num_steps, 1, vocabulary,
            skip_step=1
        )
        print("Test data:")
        for i in range(dummy_iters):
            dummy = next(example_test_generator.generate())
        num_predict = 10
        true_print_out = "Actual words: "
        pred_print_out = "Predicted words: "
        for i in range(num_predict):
            data = next(example_test_generator.generate())
            prediction = model.predict(data[0])
            predict_word = np.argmax(prediction[:, args.num_steps - 1, :])
            true_print_out += reversed_dictionary[
                test_data[
                    args.num_steps + dummy_iters + i]
            ] + " "
            pred_print_out += reversed_dictionary[predict_word] + " "
            print(true_print_out)
            print(pred_print_out)

        test_data_generator = KerasBatchGenerator(
            test_data, args.num_steps, args.batch_size, vocabulary,
            skip_step=args.num_steps
        ).generate()

        model.evaluate_generator(
            test_data_generator,
            len(test_data)//(args.batch_size*args.num_steps),
            verbose=1
        )

if __name__ == "__main__":
    main()

max_stars_repo_name: Cloud-PG/smart-cache

max_issues_repo_name: Cloud-PG/smart-cache

max_forks_repo_name: Cloud-PG/smart-cache


text:
# -*- coding: utf-8 -*-
"""
Generating samples by linearly combining two input images.
"""
from __future__ import absolute_import, print_function, division

import numpy as np
import tensorflow as tf

from niftynet.engine.image_window import ImageWindow, N_SPATIAL
from niftynet.engine.image_window_buffer import InputBatchQueueRunner
from niftynet.layer.base_layer import Layer

class LinearInterpolateSampler(Layer, InputBatchQueueRunner):
    """
    This class reads two feature vectors from files (often generated
    by running feature extractors on images in advance)
    and returns n linear combinations of the vectors.
    The coefficients are generated by::

        np.linspace(0, 1, n_interpolations)
    """

    def __init__(self,
                 reader,
                 data_param,
                 batch_size=10,
                 n_interpolations=10,
                 queue_length=10):
        self.n_interpolations = n_interpolations
        self.reader = reader
        Layer.__init__(self, name='input_buffer')
        InputBatchQueueRunner.__init__(
            self,
            capacity=queue_length,
            shuffle=False)
        tf.logging.info('reading size of preprocessed images')
        self.window = ImageWindow.from_data_reader_properties(
            self.reader.input_sources,
            self.reader.shapes,
            self.reader.tf_dtypes,
            data_param)
        # only try to use the first spatial shape available
        image_spatial_shape = list(self.reader.shapes.values())[0][:3]
        self.window.set_spatial_shape(image_spatial_shape)

        tf.logging.info('initialised window instance')
        self._create_queue_and_ops(self.window,
                                   enqueue_size=self.n_interpolations,
                                   dequeue_size=batch_size)
        tf.logging.info("initialised sampler output %s ", self.window.shapes)

        assert not self.window.has_dynamic_shapes, \
            "dynamic shapes not supported, please specify " \
            "spatial_window_size = (1, 1, 1)"

    def layer_op(self, *args, **kwargs):
        """
        This function first reads two vectors, and interpolates them
        with self.n_interpolations mixing coefficients.

        Location coordinates are set to ``np.ones`` for all the vectors.
        """
        while True:
            image_id_x, data_x, _ = self.reader(idx=None, shuffle=False)
            image_id_y, data_y, _ = self.reader(idx=None, shuffle=True)
            if not data_x or not data_y:
                break
            if image_id_x == image_id_y:
                continue
            embedding_x = data_x[self.window.names[0]]
            embedding_y = data_y[self.window.names[0]]

            steps = np.linspace(0, 1, self.n_interpolations)
            output_vectors = []
            for (_, mixture) in enumerate(steps):
                output_vector = \
                    embedding_x * mixture + embedding_y * (1 - mixture)
                output_vector = output_vector[np.newaxis, ...]
                output_vectors.append(output_vector)
            output_vectors = np.concatenate(output_vectors, axis=0)
            coordinates = np.ones(
                (self.n_interpolations, N_SPATIAL * 2 + 1), dtype=np.int32)
            coordinates[:, 0] = image_id_x
            coordinates[:, 1] = image_id_y

            output_dict = {}
            for name in self.window.names:
                coordinates_key = self.window.coordinates_placeholder(name)
                image_data_key = self.window.image_data_placeholder(name)
                output_dict[coordinates_key] = coordinates
                output_dict[image_data_key] = output_vectors
            yield output_dict

ext: py

reason: import numpy


text:
import random
import torch
from torch.utils.data import Dataset
import numpy as np
import argparse
from argparse import Namespace
import os
import yaml
import torch.utils.tensorboard as tb
from datetime import datetime
import scipy as sp
import matplotlib.pyplot as plt
import torch.nn.functional as F
import torchvision
from utils.loss_utils import get_measurements, get_transpose_measurements

def set_all_seeds(random_seed: int):
    """
    Sets random seeds in numpy, torch, and random.

    Args:
        random_seed: The seed to set.
                     Type: int.
    """
    torch.manual_seed(random_seed)
    random.seed(random_seed)
    np.random.seed(random_seed)

    return

def dict2namespace(config: dict):
    """
    Converts a given dictionary to an argparse namespace object.

    Args:
        config: The dictionary to convert to namespace.
                Type: dict.

    Returns:
        namespace: The converted namespace.
                   Type: Namespace.
    """
    namespace = argparse.Namespace()
    for key, value in config.items():
        if isinstance(value, dict):
            new_value = dict2namespace(value)
        else:
            new_value = value
        setattr(namespace, key, new_value)
    return namespace

def split_dataset(base_dataset: Dataset, hparams: Namespace):
    """
    Split a given dataset into train, val, and test sets.
    If we do not want a validation set, returns None for val.

    Args:
        base_dataset: The dataset to use for splitting.
                      Type: Dataset.
        hparams: The experiment parameters to use for splitting.
                 Type: Namespace.
                 Expected to have the constituents:
                    hparams.data.num_train - int
                    hparams.data.num_val - int
                    hparams.data.num_test - int
                    hparams.outer.use_validation - bool
                    hparams.seed - int

    Returns:
        datasets: A dict containing the train, val, and test datasets.
                  Type: dict.
    """
    num_train = hparams.data.num_train
    num_val = hparams.data.num_val
    num_test = hparams.data.num_test

    use_validation = hparams.outer.use_validation

    indices = list(range(len(base_dataset)))

    random_state = np.random.get_state()
    np.random.seed(hparams.seed+1)
    np.random.shuffle(indices)
    np.random.set_state(random_state)

    if use_validation:
        train_indices = indices[:num_train]
        val_indices = indices[num_train:num_train+num_val]
        test_indices = indices[num_train+num_val:num_train+num_val+num_test]

        train_dataset = torch.utils.data.Subset(base_dataset, train_indices)
        val_dataset = torch.utils.data.Subset(base_dataset, val_indices)
        test_dataset = torch.utils.data.Subset(base_dataset, test_indices)
    else:
        train_indices = indices[:num_train]
        test_indices = indices[num_train:num_train+num_test]

        train_dataset = torch.utils.data.Subset(base_dataset, train_indices)
        val_dataset = None
        test_dataset = torch.utils.data.Subset(base_dataset, test_indices)

    out_dict = {'train': train_dataset,
            'val': val_dataset,
            'test': test_dataset}

    return out_dict

def init_c(hparams):
    """
    Initializes the hyperparameters as a scalar, vector, or matrix.

    Args:
        hparams: The experiment parameters to use for init.
                 Type: Namespace.
                 Expected to have the consituents:
                    hparams.outer.hyperparam_type - str in [scalar, vector, matrix]
                    hparams.problem.num_measurements - int
                    hparams.outer.hyperparam_init - int or float

    Returns:
        c: The initialized hyperparameters.
           Type: Tensor.
           Shape: [] for scalar hyperparam
                  [m] for vector hyperparam
                  [m,m] for matrix hyperparam
    """
    c_type = hparams.outer.hyperparam_type
    m = hparams.problem.num_measurements
    init_val = float(hparams.outer.hyperparam_init)

    if c_type == 'scalar':
        c = torch.tensor(init_val)
    elif c_type == 'vector':
        c = torch.ones(m) * init_val
    elif c_type == 'matrix':
        c = torch.eye(m) * init_val
    else:
        raise NotImplementedError

    return c

def get_meta_optimizer(opt_params, hparams):
    """
    Initializes the meta optmizer and scheduler.

    Args:
        opt_params: The parameters to optimize over.
                    Type: Tensor.
        hparams: The experiment parameters to use for init.
                 Type: Namespace.
                 Expected to have the consituents:
                    hparams.outer.lr - float, the meta learning rate
                    hparams.outer.lr_decay - [False, float], the exponential decay factor for the learning rate
                    hparams.outer.optimizer - str in [adam, sgd]

    Returns:
        meta_opts: A dictionary containint the meta optimizer and scheduler.
                   If lr_decay is false, the meta_scheduler key has value None.
                   Type: dict.
    """
    lr = hparams.outer.lr
    lr_decay = hparams.outer.lr_decay
    opt_type = hparams.outer.optimizer

    if opt_type == 'adam':
        meta_opt = torch.optim.Adam([{'params': opt_params}], lr=lr)
    elif opt_type == 'sgd':
        meta_opt = torch.optim.SGD([{'params': opt_params}], lr=lr)
    else:
        raise NotImplementedError

    if lr_decay:
        meta_scheduler = torch.optim.lr_scheduler.ExponentialLR(meta_opt, lr_decay)
    else:
        meta_scheduler = None

    out_dict = {'meta_opt': meta_opt,
                'meta_scheduler': meta_scheduler}

    return out_dict

def parse_config(config_path):
    with open(config_path, 'r') as f:
        hparams = yaml.safe_load(f)

    if hparams['use_gpu']:
        num = hparams['gpu_num']
        if num == -1:
            hparams['device'] = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
            if torch.cuda.device_count() > 1:
                print("Let's use", torch.cuda.device_count(), "GPUs!")
        else:
            hparams['device'] = torch.device('cuda:'+str(num)) if torch.cuda.is_available() else torch.device('cpu:0')
    else:
        hparams['device'] = torch.device('cpu:0')

    if hparams['net']['model'] != 'ncsnv2':
        raise NotImplementedError
    if hparams['outer']['meta_type'] not in ['implicit', 'maml', 'mle']:
        raise NotImplementedError

    if hparams['data']['dataset'] == "celeba":
        hparams['data']['image_size'] = 64
    elif hparams['data']['dataset'] == "ffhq":
        hparams['data']['image_size'] = 256
    else:
        raise NotImplementedError

    hparams['data']['image_shape'] = (hparams['data']['num_channels'], hparams['data']['image_size'], hparams['data']['image_size'])
    hparams['data']['n_input'] = np.prod(hparams['data']['image_shape'])

    #automatically set ROI to eye region if not specified
    if hparams['outer']['ROI'] and not isinstance(hparams['outer']['ROI'], tuple):
        if hparams['data']['dataset'] == "celeba":
            hparams['outer']['ROI'] = ((27, 15),(35, 35))
        elif hparams['data']['dataset'] == "ffhq":
            hparams['outer']['ROI'] = ((90, 50),(60, 156))
        else:
            raise NotImplementedError

    #TODO implement finite difference
    if hparams['outer']['finite_difference'] or hparams['outer']['measurement_loss'] \
        or hparams['outer']['meta_loss_type'] != 'l2':
        raise NotImplementedError

    if hparams['problem']['measurement_type'] == 'circulant':
        hparams['problem']['train_indices'] = np.random.randint(0, hparams['data']['n_input'], hparams['problem']['num_measurements'])
        hparams['problem']['sign_pattern'] = np.float32((np.random.rand(1, hparams['data']['n_input']) < 0.5)*2 - 1.)
    elif hparams['problem']['measurement_type'] == 'superres':
        hparams['problem']['y_shape'] = (hparams['data']['num_channels'], hparams['data']['image_size']//hparams['problem']['downsample_factor'], hparams['data']['image_size']//hparams['problem']['downsample_factor'])
        hparams['problem']['num_measurements'] = np.prod(hparams['problem']['y_shape'])
    elif hparams['problem']['measurement_type'] == 'identity':
        hparams['problem']['y_shape'] = hparams['data']['image_shape']
        hparams['problem']['num_measurements'] = hparams['data']['n_input']
    elif hparams['problem']['measurement_type'] == 'inpaint':
        hparams['problem']['num_measurements'] = hparams['data']['n_input'] - hparams['data']['num_channels']*hparams['problem']['inpaint_size']**2

    if hparams['problem']['add_dependent_noise']:
        raise NotImplementedError

    HPARAMS = dict2namespace(hparams)

    print(yaml.dump(HPARAMS, default_flow_style=False))

    return HPARAMS

def parse_args(docstring, manual=False, config=None, doc=None):
    now = datetime.now().strftime("%Y_%m_%d_%H_%M_%S")

    parser = argparse.ArgumentParser(description=docstring)

    parser.add_argument('--config', type=str, required=True,  help='Path to the config file')
    parser.add_argument('--doc', type=str, default=now, help='A string for documentation purpose. '
                                                               'Will be the name of the log folder.')

    if manual:
        args = parser.parse_args(["--config", config, "--doc", doc])
    else:
        args = parser.parse_args()

    return args

# computes mvue from kspace and coil sensitivities
def get_mvue(kspace, s_maps):
    '''
    Get mvue estimate from coil measurements
    Parameters:
    -----------
    kspace : complex np.array of size b x c x n x n
            kspace measurements
    s_maps : complex np.array of size b x c x n x n
            coil sensitivities
    Returns:
    -------
    mvue : complex np.array of shape b x n x n
            returns minimum variance estimate of the scan
    '''
    return np.sum(sp.ifft(kspace, axes=(-1, -2)) * np.conj(s_maps), axis=1) / np.sqrt(np.sum(np.square(np.abs(s_maps)), axis=1))

def plot_images(images, title, save=False, fname=None):
    #TODO THIS NEEDS FIXING!
    """Function to plot and/or save an image"""

    fig = plt.figure(figsize=(1, 1))

    grid_img = torchvision.utils.make_grid(images.cpu(), nrow=images.shape[0]//2).permute(1, 2, 0)

    ax = fig.add_subplot(1, 1, 1, frameon=False)
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_ticks([])

    frame = plt.gca()
    frame.axes.get_xaxis().set_visible(False)
    frame.axes.get_yaxis().set_visible(False)
    frame = frame.imshow(grid_img)

    if save:
        plt.savefig(fname)
    else:
        ax.set_title(title)
        plt.show();

def get_measurement_images(images, hparams):
    A_type = hparams.problem.measurement_type

    if A_type not in ['superres', 'inpaint']:
        print("\nCan't save given measurement type\n")
        return

    if A_type == 'inpaint':
        images = get_measurements(None, images, hparams, True)
    elif A_type == 'superres':
        images = get_measurements(None, images, hparams)
        images = get_transpose_measurements(None, images, hparams)

    return images

reason: import numpy,import scipy


text:
from typing import List, Optional, Sequence, Union
import numbers
import collections

import dynet as dy
import numpy as np
import xnmt.utils as utils

from xnmt import batchers, event_trigger, losses, search_strategies, sent, vocabs
from xnmt.persistence import bare, Ref, Serializable, serializable_init
from xnmt.modelparts import transforms
from xnmt.eval import metrics

class LossCalculator(object):
  """
  A template class implementing the training strategy and corresponding loss calculation.
  """
  def calc_loss(self,
                model: 'model_base.ConditionedModel',
                src: Union[sent.Sentence, 'batchers.Batch'],
                trg: Union[sent.Sentence, 'batchers.Batch']) -> losses.FactoredLossExpr:
    if not batchers.is_batched(src):
      src = batchers.mark_as_batch([src])
    if not batchers.is_batched(trg):
      trg = batchers.mark_as_batch([trg])

    event_trigger.start_sent(src)
    return self._perform_calc_loss(model, src, trg)

  def _perform_calc_loss(self,
                         model: 'model_base.ConditionedModel',
                         src: Union[sent.Sentence, 'batchers.Batch'],
                         trg: Union[sent.Sentence, 'batchers.Batch']) -> losses.FactoredLossExpr:
    raise NotImplementedError()

class MLELoss(Serializable, LossCalculator):
  """
  Max likelihood loss calculator.
  """
  yaml_tag = '!MLELoss'
  @serializable_init
  def __init__(self) -> None:
    pass

  def _perform_calc_loss(self,
                         model: 'model_base.ConditionedModel',
                         src: Union[sent.Sentence, 'batchers.Batch'],
                         trg: Union[sent.Sentence, 'batchers.Batch']) -> losses.FactoredLossExpr:
    loss = model.calc_nll(src, trg)
    return losses.FactoredLossExpr({"mle": loss})

class PolicyMLELoss(Serializable, LossCalculator):
  yaml_tag = "!PolicyMLELoss"

  @serializable_init
  def __init__(self, model='model_base.PolicyConditionedModel'):
    self.model = model

  def _perform_calc_loss(self,
                         model: 'model_base.ConditionedModel',
                         src: Union[sent.Sentence, 'batchers.Batch'],
                         trg: Union[sent.Sentence, 'batchers.Batch']) -> losses.FactoredLossExpr:
    if self.model is None:
      model = self.model

    return losses.FactoredLossExpr({"policy_mle": model.calc_policy_nll(src, trg)})

class GlobalFertilityLoss(Serializable, LossCalculator):
  """
  A fertility loss according to Cohn+, 2016.
  Incorporating Structural Alignment Biases into an Attentional Neural Translation Model

  https://arxiv.org/pdf/1601.01085.pdf
  """
  yaml_tag = '!GlobalFertilityLoss'
  @serializable_init
  def __init__(self) -> None:
    pass

  def _perform_calc_loss(self,
                         model: 'model_base.ConditionedModel',
                         src: Union[sent.Sentence, 'batchers.Batch'],
                         trg: Union[sent.Sentence, 'batchers.Batch']) -> losses.FactoredLossExpr:
    assert hasattr(model, "attender") and hasattr(model.attender, "attention_vecs"), \
           "Must be called after MLELoss with models that have attender."

    masked_attn = model.attender.attention_vecs
    if trg.mask is not None:
      trg_mask = 1-(trg.mask.np_arr.transpose())
      masked_attn = [dy.cmult(attn, dy.inputTensor(mask, batched=True)) for attn, mask in zip(masked_attn, trg_mask)]
    loss = dy.sum_elems(dy.square(1 - dy.esum(masked_attn)))
    units = [t.len_unpadded() for t in trg]
    return losses.FactoredLossExpr({"global_fertility": losses.LossExpr(loss, units)})

class CompositeLoss(Serializable, LossCalculator):
  """
  Summing losses from multiple LossCalculator.
  """
  yaml_tag = "!CompositeLoss"
  @serializable_init
  def __init__(self, losses:List[LossCalculator], loss_weight: Optional[Sequence[numbers.Real]] = None) -> None:
    self.losses = losses
    if loss_weight is None:
      self.loss_weight = [1.0 for _ in range(len(losses))]
    else:
      self.loss_weight = loss_weight
    assert len(self.loss_weight) == len(losses)

  def _perform_calc_loss(self,
                         model: 'model_base.ConditionedModel',
                         src: Union[sent.Sentence, 'batchers.Batch'],
                         trg: Union[sent.Sentence, 'batchers.Batch']) -> losses.FactoredLossExpr:
    total_loss = {}
    for i, (loss, weight) in enumerate(zip(self.losses, self.loss_weight)):
      total_loss[str(i)] = loss._perform_calc_loss(model, src, trg) * weight
    return losses.FactoredLossExpr(total_loss)

class ReinforceLoss(Serializable, LossCalculator):
  """
  Reinforce Loss according to Ranzato+, 2015.
  SEQUENCE LEVEL TRAINING WITH RECURRENT NEURAL NETWORKS.

  (This is not the MIXER algorithm)

  https://arxiv.org/pdf/1511.06732.pdf
  """
  yaml_tag = '!ReinforceLoss'
  @serializable_init
  def __init__(self,
               baseline:Optional[Serializable]=None,
               evaluation_metric: metrics.SentenceLevelEvaluator = bare(metrics.FastBLEUEvaluator),
               search_strategy: search_strategies.SearchStrategy = bare(search_strategies.SamplingSearch),
               inv_eval: bool = True,
               decoder_hidden_dim: numbers.Integral = Ref("exp_global.default_layer_dim")) -> None:
    self.inv_eval = inv_eval
    self.search_strategy = search_strategy
    self.evaluation_metric = evaluation_metric
    self.baseline = self.add_serializable_component("baseline", baseline,
                                                    lambda: transforms.Linear(input_dim=decoder_hidden_dim, output_dim=1))

  def _perform_calc_loss(self,
                         model: 'model_base.ConditionedModel',
                         src: Union[sent.Sentence, 'batchers.Batch'],
                         trg: Union[sent.Sentence, 'batchers.Batch']) -> losses.FactoredLossExpr:
    search_outputs = model.generate_search_output(src, self.search_strategy)
    sign = -1 if self.inv_eval else 1

    # TODO: Fix units
    total_loss = collections.defaultdict(int)
    for search_output in search_outputs:
      # Calculate rewards
      eval_score = []
      for trg_i, sample_i in zip(trg, search_output.word_ids):
        # Removing EOS
        sample_i = utils.remove_eos(sample_i.tolist(), vocabs.Vocab.ES)
        ref_i = trg_i.words[:trg_i.len_unpadded()]
        score = self.evaluation_metric.evaluate_one_sent(ref_i, sample_i)
        eval_score.append(sign * score)
      reward = dy.inputTensor(eval_score, batched=True)
      # Composing losses
      baseline_loss = []
      cur_losses = []
      for state, mask in zip(search_output.state, search_output.mask):
        bs_score = self.baseline.transform(dy.nobackprop(state.as_vector()))
        baseline_loss.append(dy.squared_distance(reward, bs_score))
        logsoft = model.decoder.scorer.calc_log_probs(state.as_vector())
        loss_i = dy.cmult(logsoft, reward - bs_score)
        cur_losses.append(dy.cmult(loss_i, dy.inputTensor(mask, batched=True)))

      total_loss["polc_loss"] += dy.sum_elems(dy.esum(cur_losses))
      total_loss["base_loss"] += dy.sum_elems(dy.esum(baseline_loss))
    units = [t.len_unpadded() for t in trg]
    total_loss = losses.FactoredLossExpr({k: losses.LossExpr(v, units) for k, v in total_loss.items()})
    return losses.FactoredLossExpr({"risk": total_loss})

class MinRiskLoss(Serializable, LossCalculator):
  yaml_tag = '!MinRiskLoss'

  @serializable_init
  def __init__(self,
               evaluation_metric: metrics.Evaluator = bare(metrics.FastBLEUEvaluator),
               alpha: numbers.Real = 0.005,
               inv_eval: bool = True,
               unique_sample: bool = True,
               search_strategy: search_strategies.SearchStrategy = bare(search_strategies.SamplingSearch)) -> None:
    # Samples
    self.alpha = alpha
    self.evaluation_metric = evaluation_metric
    self.inv_eval = inv_eval
    self.unique_sample = unique_sample
    self.search_strategy = search_strategy

  def _perform_calc_loss(self,
                         model: 'model_base.ConditionedModel',
                         src: Union[sent.Sentence, 'batchers.Batch'],
                         trg: Union[sent.Sentence, 'batchers.Batch']) -> losses.FactoredLossExpr:
    batch_size = trg.batch_size()
    uniques = [set() for _ in range(batch_size)]
    deltas = []
    probs = []
    sign = -1 if self.inv_eval else 1
    search_outputs = model.generate_search_output(src, self.search_strategy)
    # TODO: Fix this
    for search_output in search_outputs:
      assert len(search_output.word_ids) == 1
      assert search_output.word_ids[0].shape == (len(search_output.state),)
      logprob = []
      for word, state in zip(search_output.word_ids[0], search_output.state):
        lpdist = model.decoder.scorer.calc_log_probs(state.as_vector())
        lp = dy.pick(lpdist, word)
        logprob.append(lp)
      sample = search_output.word_ids
      logprob = dy.esum(logprob) * self.alpha
      # Calculate the evaluation score
      eval_score = np.zeros(batch_size, dtype=float)
      mask = np.zeros(batch_size, dtype=float)
      for j in range(batch_size):
        ref_j = utils.remove_eos(trg[j].words, vocabs.Vocab.ES)
        hyp_j = utils.remove_eos(sample[j].tolist(), vocabs.Vocab.ES)
        if self.unique_sample:
          hash_val = hash(tuple(hyp_j))
          if len(hyp_j) == 0 or hash_val in uniques[j]:
            mask[j] = -1e20 # represents negative infinity
            continue
          else:
            uniques[j].add(hash_val)
          # Calc evaluation score
        eval_score[j] = self.evaluation_metric.evaluate_one_sent(ref_j, hyp_j) * sign
      # Appending the delta and logprob of this sample
      prob = logprob + dy.inputTensor(mask, batched=True)
      deltas.append(dy.inputTensor(eval_score, batched=True))
      probs.append(prob)
    sample_prob = dy.softmax(dy.concatenate(probs))
    deltas = dy.concatenate(deltas)
    risk = dy.sum_elems(dy.cmult(sample_prob, deltas))
    units = [t.len_unpadded() for t in trg]
    return losses.FactoredLossExpr({"risk": losses.LossExpr(risk, units)})

#class ConfidencePenaltyLoss(Serializable):
#  """
#  The confidence penalty.
#  part of: https://arxiv.org/pdf/1701.06548.pdf
#
#  Calculate the -entropy for the given (batched policy).
#  Entropy is used as an additional loss so that it will penalize a too confident network.
#  """
#
#  yaml_tag = "!ConfidencePenaltyLoss"
#
#  @serializable_init
#  def __init__(self, softmax): pass
#
#  def _perform_calc_loss(self,
#                         model: 'model_base.ConditionedModel',
#                         src: Union[sent.Sentence, 'batchers.Batch'],
#                         trg: Union[sent.Sentence, 'batchers.Batch']) -> losses.FactoredLossExpr:
#    neg_entropy = []
#    units = np.zeros(policy[0].dim()[1])
#    for i, ll in enumerate(policy):
#      if self.valid_pos[i] is not None:
#        ll = dy.pick_batch_elems(ll, self.valid_pos[i])
#        units[self.valid_pos[i]] += 1
#      else:
#        units += 1
#      loss = dy.sum_batches(dy.sum_elems(dy.cmult(dy.exp(ll), ll)))
#      neg_entropy.append(dy.sum_batches(loss))
#    return losses.LossExpr(dy.esum(neg_entropy), units)


text:
import numpy as np
import numpy.testing as npt
import pytest

from typing import List
from itertools import product, permutations

from quara.objects import matrix_basis
from quara.objects.matrix_basis import (
    get_normalized_pauli_basis,
)
from quara.objects.elemental_system import ElementalSystem
from quara.objects.composite_system import CompositeSystem
from quara.objects.qoperation_typical import (
    generate_gate_object,
    generate_effective_lindbladian_object,
)
from quara.objects.gate_typical import (
    get_gate_names_1qubit,
    get_gate_names_2qubit,
    get_gate_names_2qubit_asymmetric,
    get_gate_names_3qubit,
    get_gate_names_1qutrit_single_gellmann,
    generate_unitary_mat_from_gate_name,
    calc_gate_mat_from_unitary_mat,
    calc_gate_mat_from_unitary_mat_with_hermitian_basis,
    generate_gate_mat_from_gate_name,
    calc_quadrant_from_pauli_symbol,
    calc_decimal_number_from_pauli_symbol,
    calc_pauli_symbol_from_decimal_number,
    calc_hamiltonian_mat_from_gate_name_2qutrit_base_matrices,
)

def _test_gate(
    gate_name: str,
    dims: List[int] = None,
    ids: List[int] = None,
    c_sys: CompositeSystem = None,
    decimal: int = 15,
):
    # Arrange
    object_name = "effective_lindbladian"
    el = generate_effective_lindbladian_object(gate_name, object_name, dims, ids, c_sys)
    g_from_el = el.to_gate()

    object_name = "gate"
    g = generate_gate_object(gate_name, object_name, dims, ids, c_sys)

    # Act
    actual = g.hs

    # Assert
    expected = g_from_el.hs
    npt.assert_almost_equal(actual, expected, decimal=decimal)

@pytest.mark.onequbit
@pytest.mark.parametrize(
    ("gate_name"),
    [(gate_name) for gate_name in get_gate_names_1qubit()],
)
def test_calc_gate_mat_from_unitary_mat_with_hermitian_basis_1qubit_case01(
    gate_name: str,
):
    # Arrange
    b = get_normalized_pauli_basis()
    u = generate_unitary_mat_from_gate_name(gate_name)
    G_complex = calc_gate_mat_from_unitary_mat(u, b)
    G_truncate = calc_gate_mat_from_unitary_mat_with_hermitian_basis(u, b)

    # Act
    actual = G_truncate

    # Assert
    expected = generate_gate_mat_from_gate_name(gate_name)
    npt.assert_almost_equal(actual, expected, decimal=15)

@pytest.mark.onequbit
@pytest.mark.parametrize(
    ("gate_name"),
    [(gate_name) for gate_name in get_gate_names_1qubit()],
)
def test_gate_1qubit_case01(gate_name: str):
    # Arrange
    e_sys = ElementalSystem(0, matrix_basis.get_normalized_pauli_basis())
    c_sys = CompositeSystem([e_sys])
    dims = [2]
    ids = []

    _test_gate(gate_name, dims, ids, c_sys)

@pytest.mark.twoqubit
@pytest.mark.parametrize(
    ("gate_name", "decimal"),
    [(gate_name, 15) for gate_name in get_gate_names_2qubit()],
)
def test_gate_2qubit_case01(gate_name: str, decimal: int):
    # Arrange
    e_sys0 = ElementalSystem(0, matrix_basis.get_normalized_pauli_basis())
    e_sys1 = ElementalSystem(1, matrix_basis.get_normalized_pauli_basis())
    c_sys = CompositeSystem([e_sys0, e_sys1])
    dims = [2, 2]

    ids = [0, 1]
    _test_gate(gate_name=gate_name, dims=dims, ids=ids, c_sys=c_sys, decimal=decimal)

    if gate_name in get_gate_names_2qubit_asymmetric():
        ids = [1, 0]
        _test_gate(
            gate_name=gate_name, dims=dims, ids=ids, c_sys=c_sys, decimal=decimal
        )

@pytest.mark.threequbit
@pytest.mark.parametrize(
    ("gate_name", "decimal"),
    [(gate_name, 15) for gate_name in get_gate_names_3qubit()],
)
def test_gate_3qubit_case01(gate_name: str, decimal: int):
    # Arrange
    e_sys0 = ElementalSystem(0, matrix_basis.get_normalized_pauli_basis())
    e_sys1 = ElementalSystem(1, matrix_basis.get_normalized_pauli_basis())
    e_sys2 = ElementalSystem(2, matrix_basis.get_normalized_pauli_basis())
    c_sys = CompositeSystem([e_sys0, e_sys1, e_sys2])
    dims = [2, 2, 2]

    ids_base = [0, 1, 2]
    for ids in permutations(ids_base):
        _test_gate(
            gate_name=gate_name, dims=dims, ids=ids, c_sys=c_sys, decimal=decimal
        )

@pytest.mark.onequtrit
@pytest.mark.parametrize(
    ("gate_name"),
    [(gate_name) for gate_name in get_gate_names_1qutrit_single_gellmann()],
)
def test_gate_1qutrit_case01(gate_name: str):
    # Arrange
    e_sys = ElementalSystem(0, matrix_basis.get_normalized_gell_mann_basis())
    c_sys = CompositeSystem([e_sys])
    dims = [3]
    ids = []
    _test_gate(gate_name, dims, ids, c_sys)

@pytest.mark.parametrize(
    ("num_qubit"),
    [(1), (2), (3)],
)
def test_calc_decimal_number_from_pauli_symbol(num_qubit: int):
    # Arrange
    pauli_symbols = ["i", "x", "y", "z"]
    p = product(pauli_symbols, repeat=num_qubit)
    for i, pi in enumerate(p):
        symbol = ""
        for s in pi:
            symbol = symbol + s
        q = calc_quadrant_from_pauli_symbol(symbol)

        # Act
        n = calc_decimal_number_from_pauli_symbol(symbol)
        actual = n

        # Assert
        expected = i
        npt.assert_equal(actual, expected)

@pytest.mark.parametrize(
    ("num_qubit"),
    [(1), (2), (3)],
)
def test_calc_pauli_symbol_from_decimal_number(num_qubit: int):
    # Arrange
    pauli_symbols = ["i", "x", "y", "z"]
    p = product(pauli_symbols, repeat=num_qubit)
    for i, pi in enumerate(p):
        symbol = ""
        for s in pi:
            symbol = symbol + s
        q = calc_quadrant_from_pauli_symbol(symbol)
        n = calc_decimal_number_from_pauli_symbol(symbol)

        # Act
        actual = symbol

        # Assert
        expected = calc_pauli_symbol_from_decimal_number(n, num_qubit)
        npt.assert_equal(actual, expected)

@pytest.mark.threequbit
def test_generate_unitary_mat_from_gate_name_toffoli():
    # Arrange
    gate_name = "toffoli"
    dims = [2, 2, 2]
    ids = [0, 1, 2]

    # Act
    actual = generate_unitary_mat_from_gate_name(
        gate_name=gate_name, dims=dims, ids=ids
    )

    # Assert
    expected = np.array(
        [
            [1, 0, 0, 0, 0, 0, 0, 0],
            [0, 1, 0, 0, 0, 0, 0, 0],
            [0, 0, 1, 0, 0, 0, 0, 0],
            [0, 0, 0, 1, 0, 0, 0, 0],
            [0, 0, 0, 0, 1, 0, 0, 0],
            [0, 0, 0, 0, 0, 1, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 1],
            [0, 0, 0, 0, 0, 0, 1, 0],
        ],
        dtype=np.complex128,
    )
    npt.assert_almost_equal(actual, expected)

@pytest.mark.threequbit
def test_generate_unitary_mat_from_gate_name_fredkin():
    # Arrange
    gate_name = "fredkin"
    dims = [2, 2, 2]
    ids = [0, 1, 2]

    # Act
    actual = generate_unitary_mat_from_gate_name(
        gate_name=gate_name, dims=dims, ids=ids
    )

    # Assert
    expected = np.array(
        [
            [1, 0, 0, 0, 0, 0, 0, 0],
            [0, 1, 0, 0, 0, 0, 0, 0],
            [0, 0, 1, 0, 0, 0, 0, 0],
            [0, 0, 0, 1, 0, 0, 0, 0],
            [0, 0, 0, 0, 1, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 1, 0],
            [0, 0, 0, 0, 0, 1, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 1],
        ],
        dtype=np.complex128,
    )
    npt.assert_almost_equal(actual, expected)

@pytest.mark.twoqutrit
def test_calc_hamiltonian_mat_from_gate_name_2qutrit_base_matrices_case01():
    # Arrange
    gate_name = "i01x90"

    # Act
    actual = calc_hamiltonian_mat_from_gate_name_2qutrit_base_matrices(gate_name)

    # Expected
    mat0 = np.eye(3, dtype=np.complex128)
    mat1 = np.array([[0, 1, 0], [1, 0, 0], [0, 0, 0]], dtype=np.complex128)
    coeff = 0.25 * np.pi
    expected = coeff * np.kron(mat0, mat1)

    # Assert
    npt.assert_almost_equal(actual, expected)

@pytest.mark.twoqutrit
def test_calc_hamiltonian_mat_from_gate_name_2qutrit_base_matrices_case02():
    # Arrange
    gate_name = "02yi180"

    # Act
    actual = calc_hamiltonian_mat_from_gate_name_2qutrit_base_matrices(gate_name)

    # Expected
    mat0 = np.array([[0, 0, -1j], [0, 0, 0], [1j, 0, 0]], dtype=np.complex128)
    mat1 = np.eye(3, dtype=np.complex128)
    coeff = 0.5 * np.pi
    expected = coeff * np.kron(mat0, mat1)

    # Assert
    npt.assert_almost_equal(actual, expected)

@pytest.mark.twoqutrit
def test_calc_hamiltonian_mat_from_gate_name_2qutrit_base_matrices_case03():
    # Arrange
    gate_name = "12z02y90"

    # Act
    actual = calc_hamiltonian_mat_from_gate_name_2qutrit_base_matrices(gate_name)

    # Expected
    mat0 = np.array([[0, 0, 0], [0, 1, 0], [0, 0, -1]], dtype=np.complex128)
    mat1 = np.array([[0, 0, -1j], [0, 0, 0], [1j, 0, 0]], dtype=np.complex128)
    coeff = 0.25 * np.pi
    expected = coeff * np.kron(mat0, mat1)

    # Assert
    npt.assert_almost_equal(actual, expected)

@pytest.mark.twoqutrit
def test_calc_hamiltonian_mat_from_gate_name_2qutrit_base_matrices_case04():
    # Arrange
    gate_name = "i01x180_12z02y90"

    # Act
    actual = calc_hamiltonian_mat_from_gate_name_2qutrit_base_matrices(gate_name)

    # Expected
    mat0 = np.eye(3, dtype=np.complex128)
    mat1 = np.array([[0, 1, 0], [1, 0, 0], [0, 0, 0]], dtype=np.complex128)
    coeff = 0.5 * np.pi
    h_first = coeff * np.kron(mat0, mat1)

    mat0 = np.array([[0, 0, 0], [0, 1, 0], [0, 0, -1]], dtype=np.complex128)
    mat1 = np.array([[0, 0, -1j], [0, 0, 0], [1j, 0, 0]], dtype=np.complex128)
    coeff = 0.25 * np.pi
    h_second = coeff * np.kron(mat0, mat1)

    expected = h_first + h_second

    # Assert
    npt.assert_almost_equal(actual, expected)

@pytest.mark.twoqutrit
@pytest.mark.parametrize(
    ("gate_name"),
    [
        (gate_name)
        for gate_name in ["i01x90", "02yi180", "12z01y90", "i01y90_02x01z180"]
    ],
)
def test_gate_2qutrit_case01(gate_name: str):
    # Arrange
    e_sys0 = ElementalSystem(0, matrix_basis.get_normalized_gell_mann_basis())
    e_sys1 = ElementalSystem(1, matrix_basis.get_normalized_gell_mann_basis())
    c_sys = CompositeSystem([e_sys0, e_sys1])
    dims = [3, 3]
    ids = []

    # Act & Assert
    _test_gate(gate_name, dims, ids, c_sys)

max_stars_repo_name: tknrsgym/quara

max_issues_repo_name: tknrsgym/quara

max_forks_repo_name: tknrsgym/quara


text:
# IMPORTATION STANDARD

# IMPORTATION THIRDPARTY
import numpy as np
import pandas as pd
import pytest

# IMPORTATION INTERNAL
from gamestonk_terminal.stocks.due_diligence import finviz_model

@pytest.mark.vcr
def test_get_news(default_csv_path):
    result_dict = finviz_model.get_news(ticker="TSLA")
    result_df = pd.DataFrame(result_dict)

    # result_df.to_csv(default_csv_path, index=True)
    expected_df = pd.read_csv(default_csv_path)

    np.array_equal(result_df.values, expected_df.values)

@pytest.mark.vcr
def test_get_analyst_data(default_csv_path):
    result_df = finviz_model.get_analyst_data(ticker="TSLA")

    # result_df.to_csv(default_csv_path, index=True)
    expected_df = pd.read_csv(default_csv_path, index_col="date")

    pd.testing.assert_frame_equal(result_df, expected_df)


text:
# cdiazbas@iac.es

def ftsread(ini, endi, ftsdir=None):
    """
    Extract spectral data from the interpolated disk-center
    intensity atlas recorded at the Kitt-Peak National
    Observatory: Neckel and Labs (1984)

    Wavelength range: 3290 - 12508 A
    Wavelength step: 0.002 A

    CALL: atlas,xlam = ftsread(ini = waveIni ,endi = waveEndi)

    Args:
        ini (int): Initial wavelength
        endi (int): Final wavelength
        ftsdir (string, optional): FTS directory

    Returns:
        specFinal, lmbda: spectrum and wavelength
    """

    import os
    from struct import unpack
    import numpy as np

    # Atlas directory
    if ftsdir is None:
        ftsdir = str(__file__).split('/')
        sdir = '/'.join(ftsdir[0:-2])+'/data'
    else:
        sdir = ftsdir

    # Work directory
    tdir = os.getcwd()

    # Copy from the data a temporary range
    skip = (ini-3290)
    count = abs(ini-endi)
    os.system('dd if='+sdir+'/fts_cent.dat of='+tdir +
              '/tmp bs=1000 skip='+str(skip)+' count='+str(count))
    lmbda = np.arange(ini, endi, 0.002)

    # Read the tmp file
    FileTmp = open('tmp', 'rb')
    FileRead = FileTmp.read(count*500*2)
    varDecode = unpack('!'+str(int(count*500))+'h', FileRead)
    varFinal = np.array(varDecode)

    # Delete the tmp file
    os.remove(tdir+'/tmp')

    return varFinal/1e4, lmbda

max_stars_repo_name: aasensio/pyiacsun

max_issues_repo_name: aasensio/pyiacsun

max_forks_repo_name: aasensio/pyiacsun


text:
#!/usr/bin/env python

"""
Script to generate a timeline and stats per partition and worker from
controller log file:
# It uses input-statement-id to mark the beginning of an iteration.
# Total iteration time for ith iteration = T for the next stage with
input-statement-id minus T for the current stage with input-statement-id.
# Total compute time for ith iteration = sum of Ws starting after current
stage id for given input-statement-id minus current W.
# Output is 4 tables.
  1. partition_time: compute time for each partition for each iteration
  2. worker_time: average compute time per core for each worker for each
    iteration
  3. iteration_time: iteration time for each iteration
  4. marker: partition to worker mapping for each iteration
"""

import argparse
import pickle
import numpy as np
import os

def ParseArguments():
    parser = argparse.ArgumentParser(description="Process controller log file")
    parser.add_argument('--cores', type=int, required=True,
                        help="Cores per worker")
    parser.add_argument('--offset', type=int, required=True,
                        help="Offset for main workers")
    parser.add_argument('--input', type=str, required=True,
                        help="Input dir")
    parser.add_argument('--output', type=str, required=False, default='output_',
                        help="Output dir")
    parser.add_argument('--marker', type=str, required=False,
                        default='ReceiveGlobalDt',
                        help="Statement that marks beginning of an iteration")
    parser.add_argument('--count-also', action='store_true',
                        help="Also parse local fluid count from worker 0")
    parser.add_argument('--last-iter', type=int, required=False, default='-1',
                        help="Number of iterations")
    parser.add_argument('--scale', type=float, required=False, default='1.0',
                        help="Normalizing factor")
    return parser.parse_args()

"""
Stores and adds information to tables.
"""
class IterationData(object):
    def __init__(self, cores, num_partitions, num_workers, num_iterations):
        self.num_cores      = cores
        self.num_partitions = num_partitions
        self.num_workers    = num_workers
        self.num_iterations = num_iterations
        # Compute time for each partition
        self.partition_time = np.zeros(
            shape=(num_partitions, num_iterations), dtype=float)
        # Average per core compute time for each worker
        self.worker_time = np.zeros(
            shape=(num_workers, num_iterations), dtype=float)
        # Iteration time for each partition
        self.iteration_time = np.zeros(shape=(num_iterations), dtype=float)
        # Partition to worker map
        self.partition_to_worker = np.zeros(
            shape=(num_partitions, num_iterations), dtype=int)

    """
    Save data.
    """
    def Save(self, out, scale):
        sum_time = self.partition_time.sum(axis=0)
        result = sum_time[0:-1].sum()
        print("Cumulative average time = %f" % (result))
        np.save(os.path.join(out, 'partition_time'), self.partition_time/scale)
        np.save(os.path.join(out, 'worker_time'), self.worker_time/scale)
        np.save(os.path.join(out, 'iteration_time'), self.iteration_time/scale)
        np.save(os.path.join(out, 'partition_to_worker'), self.partition_to_worker)
        parameters = {
            "num_cores"      : self.num_cores,
            "num_partitions" : self.num_partitions,
            "num_workers"    : self.num_workers,
            "num_iterations" : self.num_iterations
        }
        pickle.dump(parameters, open(os.path.join(out, 'parameters.txt'), 'wt'))

    """
    Record iteration time.
    """
    def RecordIterationTime(self, itr, time):
        self.iteration_time[itr] = time

    """
    Record compute time logged for a partition for an iteration.
    """
    def RecordComputeTime(self, partition_id, itr, compute_time):
        self.partition_time[partition_id, itr] = compute_time

    """
    Record worker over which an iteration ran.
    """
    def RecordWorker(self, partition_id, itr, worker_id):
        self.partition_to_worker[partition_id, itr] = worker_id

    """
    Compute average time per partition for partitions that ran on a worker,
    for every iteration.
    """
    def ComputeWorkerComputeTime(self):
        for w in range(self.num_workers):
            partitions = (self.partition_to_worker == w)
            partition_time = self.partition_time.copy()
            partition_time[~partitions] = 0
            self.worker_time[w,:] = partition_time.sum(axis=0)/self.num_cores

"""
Parses and stores iteration data.
"""
class LogParser(object):
    def __init__(self, offset, cores, controller_log_file_name, stats_file_name,
                 num_iterations):
        # Controller log file that dumps all of log, to determine the statement
        # ID for ReceiveGlobalDt.
        self.controller_log_file_name = controller_log_file_name
        # Statement IDs for the statement that marks start of an iteration.
        self.iteration_marker = set()
        # Stats file that controller dumps at the end of application run.
        self.stats_file_name = stats_file_name
        # Iteration data
        self.iteration_data = None
        # Variable group to partitions.
        self.variable_group_to_partitions = dict()
        self.variable_group_main = None  # main variable group
        self.num_partitions_main = None  # number of partitions fo main variable
        # Total number of workers, and cores per worker.
        self.offset = offset
        self.num_cores = cores
        self.num_workers = None
        # Number of iterations.
        self.num_iterations = num_iterations

    """
    Go over log file and get Statement IDs for given statement, to mark the
    beginning of an iteration.
    """
    def InitializeIterationMarkers(self, statement_name):
        with open(self.controller_log_file_name, 'r') as controller_log_file:
            for line in controller_log_file:
                tokens = line.split()
                idx = -1
                for i,t in enumerate(tokens):
                    if statement_name in t:
                        idx = i
                if idx != -1:
                    if idx < 2:
                        continue
                    subtokens = tokens[idx-2].strip(',').split('#')
                    if len(subtokens) !=2 or subtokens[0] != 'statement':
                        continue
                    self.iteration_marker.add(int(subtokens[1]))
        print("Markers", self.iteration_marker)

    """
    Determine the main variable group, number of partitions, number of workers,
    and number of iterations. Initialize iteration data.
    """
    def InitializeIterationData(self):
        worker_set = set()
        main_group = None
        total_iterations = 0
        # Make a pass over stats file, recording information about partitions,
        # iterations and workers.
        with open(self.stats_file_name, 'r') as stats_file:
            line_num = 0
            current_variable_group = None
            for line in stats_file:
                tokens = line.split()
                category = tokens[0]
                if category == 'P':
                    current_variable_group = int(tokens[2])
                    partition_id = int(tokens[3])
                    partitions = self.variable_group_to_partitions.setdefault(
                        current_variable_group, set())
                    partitions.add(partition_id)
                    worker_id = int(tokens[5])
                    worker_set.add(worker_id)
                elif category == 'T':
                    if current_variable_group == None:
                        continue
                    statement_id = int(tokens[2])
                    if statement_id in self.iteration_marker:
                        total_iterations += 1
                        if main_group is None:
                            main_group = current_variable_group
                            print("Main group is %d" % main_group)
                        else:
                            assert(main_group == current_variable_group)
                line_num += 1
                if line_num%(10**7) == 0:
                    print("Initializing, processed %d lines" % line_num)
        # Count total number of workers.
        self.num_workers = len(worker_set) - self.offset
        # Main variable group is the variable group with non-zero iterations.
        # Verify that this is the one with maximum number of partitions.
        max_num_partitions = max(
          len(partitions) for partitions in
          self.variable_group_to_partitions.values())
        assert(len(self.variable_group_to_partitions[main_group]) ==
               max_num_partitions)
        self.variable_group_main = main_group
        self.num_partitions_main = max_num_partitions
        # Total number of iterations equals number of iterations on main
        # variable group.
        print("Total iterations X %d partitions = %d" % (max_num_partitions, total_iterations))
        #assert(total_iterations % max_num_partitions == 0)
        # Number of iterations equals number of times marker appears for
        # an iteration, minus -- we're counting iteration time as time between
        # markers.
        if self.num_iterations == -1:
            self.num_iterations = total_iterations/max_num_partitions - 1
        print("Will use %d iterations" % (self.num_iterations))
        # Finally initialize iteration data.
        self.iteration_data = IterationData(
          self.num_cores, self.num_partitions_main,
          self.num_workers, self.num_iterations)
        print("Number of partitions", self.num_partitions_main)
        print("Number of workers", self.num_workers)
        print("Number of iterations", self.num_iterations)
        print("Main variable group", self.variable_group_main)

    """
    Now go over the log, and for every partition over main variable group,
    record total time and compute time for every iteration, and also, the
    worker over which the iteration ran (began).
    """
    def ParseIterationTimes(self):
        # Store time stamp for each iteration, to compute iteration time.
        iteration_timestamps = dict()  # Stage ID to list of time stamps
        # Store total busy time for each partition, iteration.
        compute_times = list()
        # Store worker for each partition, iteration.
        iteration_workers = list()
        with open(self.stats_file_name, 'r') as stats_file:
            recording = False
            current_partition = -1
            current_worker = -1
            for p in range(self.num_partitions_main):
                compute_times.append(dict())
                iteration_workers.append(dict())
            # Go over log file and record times for each iteration.
            line_num = 0
            for line in stats_file:
                tokens = line.split()
                category = tokens[0]
                if category == 'P':
                    # Record current partition information.
                    variable_group = int(tokens[2])
                    partition_id = int(tokens[3])
                    worker_id = int(tokens[5]) - self.offset
                    if variable_group == self.variable_group_main:
                        recording = True
                        current_partition = partition_id
                        current_worker = worker_id
                    else:
                        recording = False
                elif category == 'T' and recording:
                    # Record iteration time stamp.
                    stage_id = int(tokens[1])
                    statement_id = int(tokens[2])
                    time = float(tokens[3])
                    if statement_id not in self.iteration_marker:
                        continue
                    timestamps = iteration_timestamps.setdefault(
                      stage_id, list())
                    timestamps.append(time)
                    # Record worker.
                    iteration_workers[current_partition][stage_id] = current_worker
                elif category == 'W' and recording:
                    # Record busy time.
                    stage_id = int(tokens[1])
                    time = float(tokens[3])
                    compute_times[current_partition][stage_id] = time
                line_num += 1
                if line_num%(10**7) == 0:
                    print("Processed %d lines" % line_num)
        stage_ids = iteration_timestamps.keys()
        stage_ids.sort()
        num_stages = len(stage_ids)
        # Average the timestamps for each iteration.
        #for t in iteration_timestamps.values():
        #    assert(len(t) == self.num_partitions_main)
        for i in range(self.num_iterations+1):
            print(i, len(iteration_timestamps[stage_ids[i]]))
            assert(len(iteration_timestamps[stage_ids[i]]) == self.num_partitions_main)
        iteration_timestamps = {
            sid:sum(t)/self.num_partitions_main
            for sid, t in iteration_timestamps.iteritems() }
        # Generate a sorted list of stage ids that mark beginning of an
        # iteration.
        print("Number of stages recorded = %d" % (num_stages))
        # Record iteration times.
        for i in range(min(num_stages-1, self.num_iterations)):
            sid1 = stage_ids[i]
            sid2 = stage_ids[i+1]
            self.iteration_data.RecordIterationTime(
              i, iteration_timestamps[sid2] - iteration_timestamps[sid1])
        # Record worker and busy time for each partition and iteration.
        for p in range(self.num_partitions_main):
            all_stages = compute_times[p].keys()
            all_stages.sort()
            for i in range(min(num_stages-1, self.num_iterations)):
                sid1 = stage_ids[i]
                sid2 = stage_ids[i+1]
                self.iteration_data.RecordWorker(
                  p, i, iteration_workers[p][sid1])
                time = 0
                # Find the stage just greater than sid1
                for s, sid in enumerate(all_stages):
                    if sid > sid1:
                        break
                # Add busy times till stage = sid2
                time += compute_times[p][sid]
                while sid < sid2:
                    s += 1
                    sid = all_stages[s]
                    time += compute_times[p][sid]
                if i < self.num_iterations:
                    self.iteration_data.RecordComputeTime(
                      p, i, time)
                    assert(sid == sid2)
        # Finally computer worker compute times.
        self.iteration_data.ComputeWorkerComputeTime()

    """
    Save data.
    """
    def Save(self, prefix, scale):
        self.iteration_data.Save(prefix, scale)

    """
    Run parser.
    """
    def Run(self, statement_name):
        self.InitializeIterationMarkers(statement_name)
        self.InitializeIterationData()
        self.ParseIterationTimes()

if __name__ == '__main__':
    args = ParseArguments()
    stats_file = os.path.join(args.input, 'controller.txt')
    log_file = os.path.join(args.input, 'canary_controller.INFO')
    log_parser = LogParser(args.offset, args.cores, log_file, stats_file, args.last_iter)
    log_parser.Run(args.marker)
    log_parser.Save(args.output, args.scale)

max_stars_repo_name: schinmayee/fluid-sim-lb

max_issues_repo_name: schinmayee/fluid-sim-lb

max_forks_repo_name: schinmayee/fluid-sim-lb


text:
'''
    Face recognize demo, download modle from maixhub first:
            https://maixhub.com/modelInfo?modelId=29
'''

from maix import nn, display, camera
from maix.nn.app.face import FaceRecognize
from PIL import Image, ImageDraw, ImageFont
import numpy as np
import time
from evdev import InputDevice
from select import select

def get_keys(device):
    '''
        @return is_left_pressed, is_right_pressed
    '''
    left = False
    right = False
    r,w,x = select([device], [], [], 0)
    if r:
        for event in device.read():
            if event.value == 1 and event.code == 0x02:
                right = True
            elif event.value == 1 and event.code == 0x03:
                left = True
    return left, right

class Face_Recognizer:
    def __init__(self, threshold = 0.5, nms = 0.3, max_face_num = 1):
        model = {
            "param": "res/model_int8.param",
            "bin": "res/model_int8.bin"
        }
        model_fe = {
            "param": "res/fe_res18_117.param",
            "bin": "res/fe_res18_117.bin"
        }
        self.input_size = (224, 224, 3)
        input_size_fe = (128, 128, 3)
        self.feature_len = 256
        self.features = []

        options = {
            "model_type":  "awnn",
            "inputs": {
                "input0": self.input_size
            },
            "outputs": {
                "output0": (1, 4, 2508) ,
                "431": (1, 2, 2508) ,
                "output2": (1, 10, 2508)
            },
            "mean": [127.5, 127.5, 127.5],
            "norm": [0.0078125, 0.0078125, 0.0078125],
        }
        options_fe = {
            "model_type":  "awnn",
            "inputs": {
                "inputs_blob": input_size_fe
            },
            "outputs": {
                "FC_blob": (1, 1, self.feature_len)
            },
            "mean": [127.5, 127.5, 127.5],
            "norm": [0.0078125, 0.0078125, 0.0078125],
        }

        print("-- load model:", model)
        m = nn.load(model, opt=options)
        print("-- load ok")
        print("-- load model:", model_fe)
        m_fe = nn.load(model_fe, opt=options_fe)
        print("-- load ok")

        self.recognizer = FaceRecognize(m, m_fe, self.feature_len, self.input_size, threshold, nms, max_face_num)
        print("-- init end")

    def get_faces(self, img, std_img = False):
        faces = self.recognizer.get_faces(img, std_img)
        return faces

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

    def add_user(self, name, feature):
        self.features.append([name, feature])
        return True

    def remove_user(self, name_del):
        rm = None
        for name, feature in self.features:
            if name_del == name:
                rm = [name, feature]
        if rm:
            self.features.remove(rm)
            return True
        return False

    def recognize(self, feature):
        max_score = 0
        uid = -1
        for i, user in enumerate(self.features):
            score = self.recognizer.compare(user[1], feature)
            if score > max_score:
                max_score = score
                uid = i
        if uid >= 0:
            return self.features[uid][0], max_score
        return None, 0

    def get_input_size(self):
        '''
            @return input_size (w, h, c)
        '''
        return self.input_size

    def get_feature_len(self):
        return self.feature_len

    def darw_info(self, img, box, points, disp_str, bg_color=(255, 0, 0, 255), font_color=(255, 255, 255, 255), font_size=32):
        draw = ImageDraw.Draw(img)
        # font = ImageFont.load_default()
        font = ImageFont.truetype("/home/res/baars.ttf", font_size)

        font_w, font_h = font.getsize(disp_str)
        if points:
            for p in points:
                draw.rectangle((p[0] - 1, p[1] -1 , p[0] + 1, p[1] + 1), fill=bg_color)
        if len(box) == 4:
            str_y = box[1] - font_h
            draw.rectangle((box[0], box[1], box[0] + box[2], box[1] + box[3]), fill=None, outline=bg_color, width=2)
            draw.rectangle((box[0], box[1] - font_h, box[0] + font_w, box[1]), fill=bg_color)
        else:
            str_y = box[1]
        if disp_str:
            draw.text((box[0], str_y), disp_str, fill=font_color, font=font)

max_face_num = 4
detect_threshold = 0.5
detect_nms = 0.3
score_threshold = 70
names = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]

face_recognizer = Face_Recognizer(detect_threshold, detect_nms, max_face_num = max_face_num)
camera.config(size=face_recognizer.get_input_size()[:2])

keys_device = InputDevice('/dev/input/event0')
def key_pressed():
    l, r = get_keys(keys_device)
    return l, r

key_l = False
key_r = False
while 1:
    img = camera.capture()
    if not img:
        time.sleep(0.02)
        continue
    faces = face_recognizer.get_faces(img)
    l, r = key_pressed()
    key_l = l | key_l
    key_r = r | key_r
    if faces:
        # for prob, box, landmarks, feature, std_img in faces:
        for prob, box, landmarks, feature in faces:
            # [ prob, [x,y,w,h], [[x,y], [x,y], [x,y], [x,y], [x,y]], feature ]
            if key_r:
                if len(face_recognizer) < len(names):
                    idx = len(face_recognizer)
                    print("add user: {}, {}".format(idx, names[idx]))
                    face_recognizer.add_user(names[idx], feature)
                else:
                    print("user full")
                key_r = False
            name, score = face_recognizer.recognize(feature)
            if name:
                if score > score_threshold:
                    face_recognizer.darw_info(img, box, landmarks, "{}:{:.2f}".format(name, score), font_color=(0, 0, 255, 255), bg_color=(0, 255, 0, 255))
                    print("user: {}, score: {:.2f}".format(name, score))
                else:
                    face_recognizer.darw_info(img, box, landmarks, "{}:{:.2f}".format(name, score), font_color=(255, 255, 255, 255), bg_color=(255, 0, 0, 255))
                    print("maybe user: {}, score: {:.2f}".format(name, score))
            else:
                face_recognizer.darw_info(img, box, landmarks, "", font_color=(255, 255, 255, 255), bg_color=(255, 255, 255, 255))
            # display.show(std_img)
    if len(face_recognizer) == 0:
        face_recognizer.darw_info(img, (0, 32, 0, 0), None, "<- rm      add ->", font_color=(255, 0, 0, 255), bg_color=(255, 255, 255, 0))
    if key_l:
        if len(face_recognizer) > 0:
            idx = len(face_recognizer) - 1
            print("remove user:", names[idx])
            face_recognizer.remove_user(names[idx])
        else:
            print("user empty")
        key_l = False
    display.show(img)


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

# Compute the x and y coordinates for points on a sine curve
x = np.arange(0, 3 * np.pi, 0.1)
y_sin = np.sin(x)
y_cos = np.cos(x)
# Plot the points using matplotlib
plt.plot(x, y_sin)
plt.plot(x,y_cos)
plt.show()  # You must call plt.show() to make graphics appear.

max_stars_repo_name: thundercrawl/pythonAI

max_issues_repo_name: thundercrawl/pythonAI

max_forks_repo_name: thundercrawl/pythonAI


text:
from __future__ import print_function, division
from future.utils import iteritems
from builtins import range, input
# Note: you may need to update your version of future
# sudo pip install -U future

from sklearn.model_selection import train_test_split
from sklearn.datasets import load_breast_cancer
from sklearn.preprocessing import StandardScaler
from datetime import datetime
from util import get_spiral, get_xor, get_donut, get_clouds

import numpy as np
import matplotlib.pyplot as plt

# kernels
def linear(X1, X2, c=0):
  return X1.dot(X2.T) + c

def rbf(X1, X2, gamma=None):
  if gamma is None:
    gamma = 1.0 / X1.shape[-1] # 1 / D
  # gamma = 0.05
  # gamma = 5. # for donut and spiral
  if np.ndim(X1) == 1 and np.ndim(X2) == 1:
    result = np.exp(-gamma * np.linalg.norm(X1 - X2)**2)
  elif (np.ndim(X1) > 1 and np.ndim(X2) == 1) or (np.ndim(X1) == 1 and np.ndim(X2) > 1):
    result = np.exp(-gamma * np.linalg.norm(X1 - X2, axis=1)**2)
  elif np.ndim(X1) > 1 and np.ndim(X2) > 1:
    result = np.exp(-gamma * np.linalg.norm(X1[:, np.newaxis] - X2[np.newaxis, :], axis=2)**2)
  return result

def sigmoid(X1, X2, gamma=0.05, c=1):
  return np.tanh(gamma * X1.dot(X2.T) + c)

class KernelSVM:
  def __init__(self, kernel=linear, C=1.0):
    self.C = C
    self.kernel = kernel

  def _objective(self, margins):
    return 0.5 * self.u.dot(self.K.dot(self.u)) + \
      self.C * np.maximum(0, 1 - margins).sum()

  def fit(self, X, Y, lr=1e-5, n_iters=400):
    N, D = X.shape
    self.N = N
    self.u = np.random.randn(N)
    self.b = 0

    # setup kernel matrix
    self.X = X
    self.Y = Y
    self.K = self.kernel(X, X)

    # gradient descent
    losses = []
    for _ in range(n_iters):
      margins = Y * (self.u.dot(self.K) + self.b)
      loss = self._objective(margins)
      losses.append(loss)

      idx = np.where(margins < 1)[0]
      grad_u = self.K.dot(self.u) - self.C * Y[idx].dot(self.K[idx])
      self.u -= lr * grad_u
      grad_b = -self.C * Y[idx].sum()
      self.b -= lr * grad_b

    self.support_ = np.where((Y * (self.u.dot(self.K) + self.b)) <= 1)[0]
    print("num SVs:", len(self.support_))

    # print("w:", self.w)
    # print("b:", self.b)

    # hist of margins
    m = Y * (self.u.dot(self.K) + self.b)
    plt.hist(m, bins=20)
    plt.show()

    plt.plot(losses)
    plt.title("loss per iteration")
    plt.show()

  def _decision_function(self, X):
    return self.u.dot(self.kernel(self.X, X)) + self.b

  def predict(self, X):
    return np.sign(self._decision_function(X))

  def score(self, X, Y):
    P = self.predict(X)
    return np.mean(Y == P)

def plot_decision_boundary(model, X, Y, resolution=100, colors=('b', 'k', 'r')):
  np.warnings.filterwarnings('ignore')
  fig, ax = plt.subplots()

  # Generate coordinate grid of shape [resolution x resolution]
  # and evaluate the model over the entire space
  x_range = np.linspace(X[:,0].min(), X[:,0].max(), resolution)
  y_range = np.linspace(X[:,1].min(), X[:,1].max(), resolution)
  grid = [[model._decision_function(np.array([[xr, yr]])) for yr in y_range] for xr in x_range]
  grid = np.array(grid).reshape(len(x_range), len(y_range))

  # Plot decision contours using grid and
  # make a scatter plot of training data
  ax.contour(x_range, y_range, grid.T, (-1, 0, 1), linewidths=(1, 1, 1),
             linestyles=('--', '-', '--'), colors=colors)
  ax.scatter(X[:,0], X[:,1],
             c=Y, lw=0, alpha=0.3, cmap='seismic')

  # Plot support vectors (non-zero alphas)
  # as circled points (linewidth > 0)
  mask = model.support_
  ax.scatter(X[:,0][mask], X[:,1][mask],
             c=Y[mask], cmap='seismic')

  # debug
  ax.scatter([0], [0], c='black', marker='x')

  # debug
  # x_axis = np.linspace(X[:,0].min(), X[:,0].max(), 100)
  # w = model.w
  # b = model.b
  # # w[0]*x + w[1]*y + b = 0
  # y_axis = -(w[0]*x_axis + b)/w[1]
  # plt.plot(x_axis, y_axis, color='purple')
  # margin_p = (1 - w[0]*x_axis - b)/w[1]
  # plt.plot(x_axis, margin_p, color='orange')
  # margin_n = -(1 + w[0]*x_axis + b)/w[1]
  # plt.plot(x_axis, margin_n, color='orange')

  plt.show()

def clouds():
  X, Y = get_clouds()
  Xtrain, Xtest, Ytrain, Ytest = train_test_split(X, Y, test_size=0.33)
  return Xtrain, Xtest, Ytrain, Ytest, linear, 1e-5, 500

def medical():
  data = load_breast_cancer()
  X, Y = data.data, data.target
  Xtrain, Xtest, Ytrain, Ytest = train_test_split(X, Y, test_size=0.33)
  return Xtrain, Xtest, Ytrain, Ytest, linear, 1e-3, 200

def xor():
  X, Y = get_xor()
  Xtrain, Xtest, Ytrain, Ytest = train_test_split(X, Y, test_size=0.33)
  kernel = lambda X1, X2: rbf(X1, X2, gamma=3.)
  return Xtrain, Xtest, Ytrain, Ytest, kernel, 1e-3, 500

def donut():
  X, Y = get_donut()
  Xtrain, Xtest, Ytrain, Ytest = train_test_split(X, Y, test_size=0.33)
  kernel = lambda X1, X2: rbf(X1, X2, gamma=1.)
  return Xtrain, Xtest, Ytrain, Ytest, kernel, 1e-3, 300

def spiral():
  X, Y = get_spiral()
  Xtrain, Xtest, Ytrain, Ytest = train_test_split(X, Y, test_size=0.33)
  kernel = lambda X1, X2: rbf(X1, X2, gamma=5.)
  return Xtrain, Xtest, Ytrain, Ytest, kernel, 1e-3, 500

if __name__ == '__main__':
  Xtrain, Xtest, Ytrain, Ytest, kernel, lr, n_iters = donut()
  print("Possible labels:", set(Ytrain))

  # make sure the targets are (-1, +1)
  Ytrain[Ytrain == 0] = -1
  Ytest[Ytest == 0] = -1

  # scale the data
  scaler = StandardScaler()
  Xtrain = scaler.fit_transform(Xtrain)
  Xtest = scaler.transform(Xtest)

  # now we'll use our custom implementation
  model = KernelSVM(kernel=kernel, C=1.0)

  t0 = datetime.now()
  model.fit(Xtrain, Ytrain, lr=lr, n_iters=n_iters)
  print("train duration:", datetime.now() - t0)
  t0 = datetime.now()
  print("train score:", model.score(Xtrain, Ytrain), "duration:", datetime.now() - t0)
  t0 = datetime.now()
  print("test score:", model.score(Xtest, Ytest), "duration:", datetime.now() - t0)

  if Xtrain.shape[1] == 2:
    plot_decision_boundary(model, Xtrain, Ytrain)

max_stars_repo_name: JouniVatanen/NLP-and-Deep-Learning