text:
import torch
import numpy as np
from cnnseq import utils
import argparse
from cnnseq.CNN import ConvNetHistFlatten, ConvNetHist, ConvNet, train, test, test_with_cnfn_test_func
from cnnseq.CNFN2D import CNFN_2MF
from cnnseq.utils_models import CLASS, CLASS2D, get_num_classes
import os
import json

"""
    CNN Classifier for Scene classification with emotion label 1/0 (positive/negative)

    data_type:
        hist/histFlatten:
            batch_size=90
            data_filename='video_feats_HSLhist_data'
        HSL:
            batch_size=30
            data_filename='video_feats_HSL_10fps_data'

    Fuzzy is only supported with 2 classes, HSL data input

    Organized version of *ANFIS-RNN/cnn_classifier_hist.py*
    Author: Gwena Cunha
"""

def get_parser():
    parser = argparse.ArgumentParser(description="Convolutional AutoEncoder")

    parser.add_argument('--model_type', type=str, default='vanilla', help='Options: [vanilla, fuzzy]')

    # wav2mid2wav audio - 2 epochs: 70min, 20 epochs:78min, 40 epochs
    # Original audio (no norm) - 2 epochs: 5.4min, 40 epochs: 51.46min (best_test: 76.7837%)
    parser.add_argument('--num_epochs', type=int, default=40, help='Number of epochs to save results')
    parser.add_argument('--save_step', type=int, default=1, help='Save results every N steps')
    parser.add_argument('--batch_size', type=int, default=30)
    parser.add_argument('--gpu_num', type=list, default=[0, 1])
    parser.add_argument('--optimizer_name', type=str, default='adam',
                        help='Optimizer: [adam, sgd, asgd, adagrad, adadelta, rmsprop, rprop]')
    # lr 0.1: 91%, 60%, 97%, 94%, 97%, 60% 80%
    # lr 0.01: asgd 98%
    # lr 0.001 (at 4ep): adadelta (57.43% CNN, 57.43% CNFN), adam (95.9% CNN, 58.93% CNFN)
    # lr 0.01 (at 4ep): adadelta (87.86% CNN, 57.43% CNFN)
    # lr 0.1 (at 4ep): adam (57.4318% CNFN), sgd (42.5682% CNFN), asgd (57.4318% CNFN), adagrad (57.4318% CNFN),
    #  adadelta (57.2413% CNFN), rmsprop (57.4318% CNFN), rprop (57.4318% CNFN)
    # OrigAudio: epochs 40 - test 68.9834, best_test 70.5767, best_train 69.8631%, 6.0537 min
    # Instrumental: test 68.2307, best_test 70.7527, best_train 70.0293%, 5.9623 min
    parser.add_argument('--learning_rate', type=float, default=0.001)  # 1e-3)  # 0.029
    parser.add_argument('--criterion_type', type=str, default='CrossEntropy', help='Options: [MSE, CrossEntropy]')
    parser.add_argument('--momentum', type=float, default=0.98)  # 0.7, 0.98
    parser.add_argument('--weight_decay', type=float, default=1e-4)  # 5e-4
    parser.add_argument('--scheduler_factor', type=float,
                        default=0.8, help='Factor by which the learning rate will be changed after plateauing')

    # CNFN
    parser.add_argument('--LR', type=json.loads, default={"FUZZ": 0.4250,
                                                          "CONV": {"conv1": 0.4276, "conv2": 14.3721, "conv3": 70.4043},
                                                          "DEFUZZ": 0.0956,
                                                          "FC": 0.00259})  # 0.029, FC: learning rate used in optimizer

    parser.add_argument('--IN_CHANNEL', type=int, default=3)
    parser.add_argument('--IN_SIZE', type=list, default=[100, 100])
    parser.add_argument('--CLASS_NUM', type=int, default=2)
    parser.add_argument('--KERNELS_SIZE', type=list, default=[4, 6, 5])
    parser.add_argument('--KERNELS_NUM', type=list, default=[3, 3, 3])
    parser.add_argument('--SIGMAF', type=float, default=0.13)
    parser.add_argument('--SIGMAC', type=float, default=0.13)
    parser.add_argument('--NUM_MF', type=int, default=2)
    parser.add_argument('--FC_WT', type=list, default=[108, 64, 2])
    parser.add_argument('--DR', type=float, default=0.2)  # 0.2
    parser.add_argument('--NORM_LIMIT', type=int, default=3)
    parser.add_argument('--EPS', type=float, default=1e-20)
    parser.add_argument('--SEED', type=int, default=1)
    parser.add_argument('--MODE', type=str, default='List')

    # Target emotion
    parser.add_argument('--target_bin', type=str, default='bin', help='Use only for emotion_res=1D or 2D. Options: [bin, num]')
    parser.add_argument('--emotion_res', type=str, default='1D', help='Emotion resolution. Options: [1D, 2D, 2D_raw (TODO)]')

    parser.add_argument('--num_classes', type=int, default=-1, help='Number of classes')

    # Data
    parser.add_argument('--data_dir', type=str,
                        # default='/media/ceslea/DATA/VideoEmotion/DataWithEmotionTags_noText_correctedAudio_hsio/',
                        # default=utils.project_dir_name() + 'data/',
                        # default='/media/root/DATA/PycharmProjects/Scene2Wav/datasets/data_npz/',
                        default='/mnt/Gwena/Scene2Wav/datasets/data_npz/',
                        help='Data directory')
    parser.add_argument('--data_type', type=str, default='HSL', help='HSL histogram or pure HSL. Options: [hist, histFlatten, HSL]')
    # parser.add_argument('--data_filename', type=str, default='video_feats_HSL_10fps_data', help='Data file')
    #parser.add_argument('--data_filename', type=str, default='video_feats_HSLboth_10fps_data_divPerVideo',
    #                    help='Data file')
    # parser.add_argument('--data_filename', type=str, default='video_feats_HSL_10fps_origAudio_10secs_pad',
    #                    help='Data file')
    parser.add_argument('--data_filename', type=str, default='video_feats_HSL_10fps_3secs_intAudio_pad',
                        help='Data file')
    parser.add_argument('--results_dir', type=str, default='./results_cnn4_3secs/', help='Directory to save results')
    #parser.add_argument('--results_dir', type=str,
    #                    # default='./results_cnn/res_fuzzy_HSL_bin_1D_MSE_ep_4_bs_30_lr_0.001_we_1e-05_asgd/',
    #                    default='./results_cnn/res_vanilla_HSL_bin_1D_CrossEntropy_ep_40_bs_30_lr_0.001_we_0.0001_adam_94.22perf/',
    #                    help='Directory to save results')
    parser.add_argument('--saved_model', type=str, default='cnn_model.pth', help='Path to save model')
    parser.add_argument('--saved_model_parameters', type=str, default='parameters.json',
                        help='Path to save models parameters')
    parser.add_argument('--log_filename', type=str, default='log.txt', help='Log text file')
    parser.add_argument('--saved_model_best_train', type=str, default='cnn_model_best_train.pth', help='Path to save model')
    parser.add_argument('--log_filename_best_train', type=str, default='log_best_train.txt', help='Log text file')
    parser.add_argument('--saved_model_best_test', type=str, default='cnn_model_best_test.pth', help='Path to save model')
    parser.add_argument('--log_filename_best_test', type=str, default='log_best_test.txt', help='Log text file')
    # parser.add_argument('--save_ext', type=str, default='npz', help='Method of Options: [npz, json, pkl]')
    parser.add_argument('--feature_pickle_filename', type=str, default='feats.pkl',
                        help='Pickle with saved features and its respective labels')
    parser.add_argument('--mode', type=str, default='train', help='Options: [train, test]')
    args = parser.parse_args()
    return args

def label_tensor(label, bs=1, tensor_type='float'):
    # print("Label batch")
    label_shape = np.shape(label)
    # print("label_shape: {}".format(label_shape))
    c = label_shape[0]  # c, a = text_shape
    dataloader_text = {}
    # for i in range(0, c-bs+1, bs):
    for i in range(0, c, bs):
        idx_fin = min(i+bs, c)
        d = label[i:idx_fin]
        # print("Bs: {}-{}, samples: {}, shape: {}".format(i, idx_fin, idx_fin-i, np.shape(d)))
        d_tensor = torch.tensor(d)
        if tensor_type == 'float':
            dataloader_text[i] = d_tensor.float()
        else:
            dataloader_text[i] = d_tensor.long()
    # print("dataset label: {}".format(np.shape(dataloader_text.keys())))
    return dataloader_text

def feats_tensor(attributes, bs=1, data_type='hist'):
    # print("Feats tensor")
    dataset = attributes
    dataloader = {}

    # print("np.shape(dataset): {}".format(np.shape(dataset)))
    if data_type == 'histFlatten':
        c, a = np.shape(dataset)  # d_num, 96
    elif data_type == 'hist':
        c, a, b = np.shape(dataset)  # d_num, 3, 32
    else:
        c, a, d1, d2 = np.shape(dataset)  # d_num, 3, 100, 100

    for i in range(0, c, bs):
        idx_fin = min(i+bs, c)
        d = dataset[i:idx_fin]
        # print("Bs: {}-{}, samples: {}, d shape: {}".format(i, idx_fin, idx_fin-i, np.shape(d)))
        if data_type == 'histFlatten':
            d_reshape = np.reshape(d, (idx_fin - i, 1, a))
        elif data_type == 'hist':
            d_reshape = np.reshape(d, (idx_fin - i, a, b))
        else:
            d_reshape = np.reshape(d, (idx_fin - i, a, d1, d2))
        d_tensor = torch.tensor(d_reshape)
        dataloader[i] = d_tensor.float()
    # print("dataloader keys: {}".format(np.shape(dataloader.keys())))
    return dataloader

def load_data1(data, target_bin=True, data_type='hist', emotion_res='1D', do_normalize_data=False):
    if 'label' in data:
        y, y2D, y2D_raw = data['label'], data['label2D'], data['label2D_raw']
    else:  # doesn't support 2D emotion label
        y, y2D, y2D_raw = data['emotion'], data['emotion'], data['emotion']
    if 'hist' in data_type:
        X = data['hist_data']
    else:
        X = data['HSL_data']
    # X = (splices x num_frames x hist_bins x channels) = (624, 90, 3, 32)
    print("Loading data - X: {}, y: {}, y2D: {}, y2D_raw: {}".format(np.shape(X), np.shape(y), np.shape(y2D), np.shape(y2D_raw)))

    s = X.shape
    if do_normalize_data:
        X = utils.normalize_data(X, s)

    if data_type == 'histFlatten':
        att_train = X.reshape(s[0] * s[1], s[2] * s[3])
    elif data_type == 'hist':
        att_train = X.reshape(s[0] * s[1], s[2], s[3])
    else:
        att_train = X.reshape(s[0] * s[1], s[2], s[3], s[4])

    # Target train
    # Expand labels so that each splice frame has a label
    #   Before: 1 video, 38 frames -> 1 label
    #   After expansion: 1 video, 38 frames -> 38 labels
    print("Loading label with data type {}".format(data_type))
    if emotion_res == '2D':
        y = y2D
    else:
        if emotion_res == '2D_raw':
            y = y2D_raw
    if target_bin and (emotion_res == '1D' or emotion_res == '2D'):
        y_name = []
        y_tmp = y
        for i, y_t in enumerate(y):
            y_t = int(y_t)
            if emotion_res == '1D':
                y_name.append(CLASS[y_t])
            else:
                y_t -= 1  # classes ranging from 1 to 4 (change to 0 to 3)
                y_tmp[i] = y_t
                y_name.append(CLASS2D[y_t])
        y = y_tmp
        num_classes = max(y).astype(np.int) + 1
        print("Num classes: {}".format(num_classes))
        y_bin = np.zeros([len(y), num_classes])
        print("y_bin shape: {}".format(np.shape(y_bin)))
        for i in range(0, len(y)):
            y_bin[i, y[i].astype(np.int)] = 1

        label_train = utils.expand_array(y_bin, X.shape[1])
    else:
        label_train = utils.expand_array(y, X.shape[1])
    return att_train, label_train

def load_data(data_dir, data_filename, target_bin=True, data_type='hist', emotion_res='1D', do_normalize_data=False):
    """ Loads data, assumes that train and test .npz files already exits (see main.py to create it)
    """
    train_data_path = "{data_dir}{path}_train.npz".format(data_dir=data_dir, path=data_filename)
    test_data_path = "{data_dir}{path}_test.npz".format(data_dir=data_dir, path=data_filename)

    # Load data
    data_train = np.load(train_data_path)
    att_train, label_train = load_data1(data_train, target_bin=target_bin, data_type=data_type, emotion_res=emotion_res, do_normalize_data=do_normalize_data)

    data_test = np.load(test_data_path)
    att_test, label_test = load_data1(data_test, target_bin=target_bin, data_type=data_type, emotion_res=emotion_res, do_normalize_data=do_normalize_data)

    return att_train, label_train, att_test, label_test

if __name__ == '__main__':

    from timeit import default_timer as timer

    start_time = timer()

    # Settings
    args = get_parser()
    utils.ensure_dir(args.results_dir)

    if args.mode is 'test':
        # Load json file and get model's information
        json_path = os.path.join(args.results_dir, args.saved_model_parameters)
        with open(json_path, 'r') as fp:
            print("Loading JSON file for testing: " + json_path)
            params = json.load(fp)
            args.target_bin = params['target_bin']
            args.data_type = params['data_type']
            args.emotion_res = params['emotion_res']
            args.batch_size = params['batch_size']
            args.data_filename = params['data_filename']
            args.data_dir = params['data_dir']
            args.num_classes = params['num_classes']

    # Load data, separate into test and train and return
    print("\nLoad data...")
    target_bin = True if args.target_bin == 'bin' else False
    # att_train, _, label_train, _, att_test, _, label_test, _ = load_data(args.data_dir, args.data_filename,
    #                                                                     target_bin=target_bin,
    #                                                                     data_type=args.data_type,
    #                                                                     emotion_res=args.emotion_res,
    #                                                                     do_normalize_data=False)

    # IF using MSEloss, use tensor_type='float'. If using CrossEntropy, use , tensor_type='long'
    target_bin2 = target_bin
    tensor_type = 'float'
    if args.criterion_type == 'CrossEntropy':
        target_bin2 = False
        tensor_type = 'long'
    att_train, label_train, att_test, label_test = load_data(args.data_dir, args.data_filename,
                                                             target_bin=target_bin2, data_type=args.data_type,
                                                             emotion_res=args.emotion_res, do_normalize_data=False)
    print("\nLoaded data - train: {}, {}, test: {}, {}".format(np.shape(att_train), np.shape(label_train),
                                                               np.shape(att_test), np.shape(label_test)))
    print("\nTrain: min {}, max {}".format(np.amin(np.amin(np.amin(np.amin(att_train)))),
                                           np.amax(np.amax(np.amax(np.amax(att_train))))))

    # Dataset: (num_lines, num_words_embedded, size_embedding)
    dataloader_train = feats_tensor(att_train, args.batch_size, data_type=args.data_type)
    dataloader_label_train = label_tensor(label_train, args.batch_size, tensor_type=tensor_type)
    dataloader_test = feats_tensor(att_test, args.batch_size, data_type=args.data_type)
    dataloader_label_test = label_tensor(label_test, args.batch_size, tensor_type=tensor_type)
    print("Tensor shape train - label: {}, att: {}".format(np.shape(dataloader_label_train.keys()),
                                                           np.shape(dataloader_train.keys())))
    print("Tensor shape test - label: {}, att: {}".format(np.shape(dataloader_label_test.keys()),
                                                           np.shape(dataloader_test.keys())))
    # for l, d in zip(dataloader_label_train.keys(), dataloader_train.keys()):
    #     print("l: {}, d: {}".format(np.shape(dataloader_label_train[l]), np.shape(dataloader_train[d])))

    if args.num_classes == -1:
        args.num_classes = get_num_classes(target_bin, args.emotion_res)

    # Train CNN
    max_train_acc = 0
    if args.mode == 'train':
        if args.model_type == 'vanilla':
            print("\nCNN model...")
            if args.data_type == 'histFlatten':
                model = ConvNetHistFlatten(num_classes=args.num_classes, dropout=args.DR).cuda(args.gpu_num[0])
            elif args.data_type == 'hist':
                model = ConvNetHist(num_classes=args.num_classes, dropout=args.DR).cuda(args.gpu_num[0])
            else:
                model = ConvNet(num_classes=args.num_classes, dropout=args.DR).cuda(args.gpu_num[0])
        else:
            print("\nFuzzy-CNN model...")
            #np.random.seed(args.SEED)
            #torch.manual_seed(args.SEED)
            #torch.cuda.manual_seed(args.SEED)
            model = CNFN_2MF(vars(args)).cuda(args.gpu_num[0])

        lab, img, res_dir, max_train_acc = train(model, args, dataloader_label_train, dataloader_train, dataloader_label_test,
                                                 dataloader_test, bs=1, target_bin=target_bin, criterion_type=args.criterion_type)

        ## Test the model with last sample
        # last_test_label = {'0': lab.cpu()}
        # last_test_img = {'0': img.cpu()}
        # acc, euc_dist_mean = test(args, last_test_label, last_test_img, bs=1, target_bin=target_bin, num_classes=args.num_classes)
        # print("Last sample - Accuracy: {}%, Euclidian distance: {}".format(acc, euc_dist_mean))

    # Test model with test data
    #acc, euc_dist_mean = test(args, dataloader_label_test, dataloader_test, bs=1, target_bin=target_bin,
    #                          num_classes=args.num_classes, best_model=True)
    #print("Test data - Accuracy: {}%, Euclidian distance: {}".format(acc, euc_dist_mean))

    if args.criterion_type == 'MSE':
        print("Test")
        test_acc, _ = test(args, dataloader_label_test, dataloader_test, target_bin=target_bin, best_model=False)
        test_acc_best, _ = test(args, dataloader_label_test, dataloader_test, target_bin=target_bin, best_model=True)
    else:
        print("Test with CNFN test func")  # 57.02726473175022%
        test_acc = test_with_cnfn_test_func(args, dataloader_label_test, dataloader_test, best_model=False)
        test_acc_best = test_with_cnfn_test_func(args, dataloader_label_test, dataloader_test, best_model=True)
        train_acc_best = test_with_cnfn_test_func(args, dataloader_label_test, dataloader_test, best_model=True,
                                                  best_train=True)

    print("Test data - Accuracy: test {:.4f}, best_test {:.4f}, best_train {:.4f}%, max_train_acc {:.4f}%".
          format(test_acc, test_acc_best, train_acc_best, max_train_acc))

    print("Time elapsed: {:.4f} min".format((timer()-start_time)/60))


text:
"""
Shortest-path based pairwise graph embedding
"""

from collections import defaultdict
from collections import namedtuple
from itertools import chain
from warnings import warn

import numpy as np
import scipy.sparse
import scipy.sparse.csgraph
import scipy.stats
import torch
from torch import nn
from torch.nn import functional as F

from .cpp import batch_dijkstra
from .utils import check_numpy, sliced_argmax

beta_quantile = lambda x, alpha=0.01, beta=0.5: scipy.stats.beta.ppf(x, alpha, beta)
inverse_softplus = lambda x: np.where(x > 10.0, x, np.log(np.expm1(np.clip(x, 1e-6, 10.0))))
inverse_sigmoid = lambda prob, eps=1e-6: -np.log(1. / np.clip(prob, eps, 1.0 - eps) - 1)

class GraphEmbedding(nn.Module):
    Edges = namedtuple("Edges", ["adjacent", "p_adjacent", "weights"])
    INF = torch.tensor(1e38, dtype=torch.float32)
    NEG_INF = -INF

    def __init__(self, edges_from, edges_to, *, initial_weights=1.0, initial_probs=0.9,
                 sparse=True, directed=True, default_distance=0.0, default_distance_trainable=True, **defaults):
        """
        Trainable graph embedding, a default class with many options

        :param edges_from: an array of sources of all edges, int[num_edges]
        :param edges_to: an array of destinations of all edges, int[num_edges]
        :param initial_weights: a scalar or array of distances of all edges, float[] or [num_edges]
        :param initial_probs: a scalar or array of probabilities of all edges, float[] or [num_edges]
        :param sparse: if True, gradients w.r.t. layer parameters are gonna be sparse (same as in nn.Embedding)
        :param directed: if False, forall i, j, edge(i, j) and edge(j, i) have shared weight and probability
        :param default_distance: a distance to return if agent was unable to find path to any of target vertices
        :param default_distance_trainable: if True, trains default distance alongside all other distances
        :param defaults: default parameters for forward pass, see lib.cpp.batch_dijkstra
        """
        super().__init__()
        if np.ndim(initial_weights) == 0:
            initial_weights = np.full(edges_from.shape, float(initial_weights), dtype=np.float32)
        if np.ndim(initial_probs) == 0:
            initial_probs = np.full(edges_from.shape, float(initial_probs), dtype=np.float32)
        assert len(edges_from) == len(edges_to) == len(initial_weights) == len(initial_probs)
        assert not (edges_from == edges_to).any(), "graph should not contain loops"
        assert np.min(initial_weights) >= 0.0
        edges_from, edges_to, initial_weights, initial_probs = map(
            check_numpy, [edges_from, edges_to, initial_weights, initial_probs])

        num_vertices = max(edges_from.max(), edges_to.max()) + 1
        initial_adjacency_logits = inverse_sigmoid(initial_probs)
        initial_weight_logits = inverse_softplus(initial_weights)

        adjacency = [[] for _ in range(num_vertices)]
        edge_to_weight_logits, edge_to_adjacency_logits = {}, {}
        for from_i, to_i, adj_logit, weight_logit in zip(
                edges_from, edges_to, initial_adjacency_logits, initial_weight_logits):
            edge = (int(from_i), int(to_i))
            if not directed:
                from_i, to_i = edge = tuple(sorted(edge))
            adjacency[from_i].append(to_i)
            edge_to_adjacency_logits[edge] = adj_logit
            edge_to_weight_logits[edge] = weight_logit

        adjacency = list(map(sorted, adjacency))
        edge_sources = list(chain(*([i] * len(adjacency[i]) for i in range(len(adjacency)))))
        edge_targets = list(chain(*adjacency))
        lengths = list(map(len, adjacency))

        # compute slices and edge_indices, add special "fake" edge at index 0
        self.edge_sources = np.array([0] + edge_sources, dtype=np.int32)
        self.edge_targets = np.array([0] + edge_targets, dtype=np.int32)
        self.slices = np.cumsum([1] + lengths).astype("int32")
        self.edge_adjacency_logits = nn.Parameter(torch.randn(len(self.edge_targets), 1), requires_grad=True)
        self.edge_weight_logits = nn.Parameter(torch.randn(len(self.edge_targets), 1), requires_grad=True)
        self.default_distance = nn.Parameter(torch.tensor(float(default_distance)).view(1, 1),
                                             requires_grad=default_distance_trainable)
        self.num_vertices, self.num_edges = num_vertices, len(self.edge_targets)

        # initialize weight and adjacency logits
        with torch.no_grad():
            flat_i = 1  # start from 1 to skip first "fake" edge
            for from_i, to_ix in enumerate(adjacency):
                for to_i in to_ix:
                    edge = (int(from_i), int(to_i))
                    self.edge_adjacency_logits.data[flat_i, 0] = float(edge_to_adjacency_logits[edge])
                    self.edge_weight_logits.data[flat_i, 0] = float(edge_to_weight_logits[edge])
                    flat_i += 1

        self.defaults = defaults
        self.directed = directed
        self.sparse = sparse

        if not directed:
            # prepare a special arrays that convert undirected edges to directed by duplicating them
            directed_edges_by_source = defaultdict(set)
            directed_edge_to_ix = {}

            for i, from_i, to_i in zip(np.arange(self.num_edges), self.edge_sources, self.edge_targets):
                if i == 0: continue
                directed_edges_by_source[from_i].add(to_i)
                directed_edges_by_source[to_i].add(from_i)
                directed_edge_to_ix[from_i, to_i] = i
                directed_edge_to_ix[to_i, from_i] = i

            directed_edges = [0]
            directed_slices = [1]
            directed_to_undirected_reorder = [0]
            for from_i in range(self.num_vertices):
                directed_edges_from_i = sorted(directed_edges_by_source[from_i])
                directed_slices.append(directed_slices[-1] + len(directed_edges_from_i))
                directed_edges.extend(directed_edges_from_i)
                directed_to_undirected_reorder.extend(
                    directed_edge_to_ix[from_i, to_i] for to_i in directed_edges_from_i)

            self.directed_edge_indices = np.array(directed_edges, dtype='int32')
            self.directed_slices = np.array(directed_slices, dtype='int32')
            self.reorder_undirected_to_directed = nn.Parameter(
                torch.as_tensor(directed_to_undirected_reorder, dtype=torch.int64), requires_grad=False)

    def _get_logits(self, sliced_logits, sliced_indices):
        """ A private helper function that returns logits of corresponding to indices with (maybe) sparse grad """
        if not self.directed:
            sliced_indices = self.reorder_undirected_to_directed[sliced_indices]
        return F.embedding(sliced_indices, sliced_logits, sparse=self.sparse).view(*sliced_indices.shape)

    def _get_default_distance(self):
        """ A special magic that returns default distance in a way that gradients wrt that distance will be sparse"""
        return F.embedding(torch.zeros(1, dtype=torch.int64, device=self.default_distance.device),
                           self.default_distance, sparse=self.sparse).view([])

    def forward(self, from_ix, to_ix, **parameters):
        """
        Computes path from from_ix to to_ix, possibly returns a few other values
        :param from_ix: a vector of initial vertex indices, int32[batch_size]
        :param to_ix: a vector or matrix of target vertex indices, int32[batch_size] or [batch_size, num_targets]
        :param max_length: maximum length of paths (edges beyond this length are discarded, starting from from_ix)
        :param k_nearest: if given, returns distances and paths to this many nearest neighbors of each vertex
        :param deterministic: if True, always keeps edges with p > 0.5 and drops edges with p < 0.5, else samples;
            if not specified(default), uses deterministic=True in train mode, False in eval mode
        :param parameters: see lib.cpp.batch_dijkstra
        :return: a dict of many things {
            target_paths: from_ix->to_ix edge sequences from end to start, int32[*to_ix.shape, max_length]
            target_distances: sum of edge weights along path to each target (if exists), float32[*to_ix.shape]
            logp_target_paths: sum of edge log-probs along path to each target (if exists), float32[*to_ix.shape]
            found_target: whether pathfinding was successful, uint8[*to_ix.shape]
                if found_target[i] is 0, target_paths[i] and logp_target_paths[i] will be zeros,
                target_distances equals default_distance

            (v-- if k_nearest != 0, otherwise None)
            nearest_vertices: vertex ids of k_nearest neighbors of each vertex in from_ix, excluding oneself
            nearest_paths: edge sequences between from_ix and k_nearest vertices, pads with zeros if can't find enough
                shape int32[batch_size, k_nearest, max_length_nearest or max_length]
            nearest_distances: sum of edge weights on path to each neighbor, zero pad, float32[batch_size, k_nearest]
        }
        """
        # set defaults
        parameters = dict(self.defaults, **parameters)
        assert parameters.get('max_length') is not None, "Please specify max_length in either init or forward"
        parameters['deterministic'] = parameters.get('deterministic', not self.training)
        parameters['k_nearest'] = parameters.get('k_nearest', 0)

        assert from_ix.device == to_ix.device == torch.device('cpu'), "gpu not supported (yet)"
        from_ix = from_ix.to(dtype=torch.int32)
        to_ix = to_ix.to(dtype=torch.int32)

        # make sure padding edge has weight 0 and probability 1
        with torch.no_grad():
            self.edge_adjacency_logits.data[:1].fill_(self.INF)
            self.edge_weight_logits.data[:1].fill_(self.NEG_INF)

        if self.directed:
            slices, edge_indices = self.slices, self.edge_targets
            edge_adjacency_logits = self.edge_adjacency_logits
            edge_weight_logits = self.edge_weight_logits
        else:
            slices, edge_indices = self.directed_slices, self.directed_edge_indices
            edge_adjacency_logits = F.embedding(
                torch.as_tensor(self.reorder_undirected_to_directed),
                self.edge_adjacency_logits, sparse=self.sparse
            )  # [num_edges, 1]
            edge_weight_logits = F.embedding(
                torch.as_tensor(self.reorder_undirected_to_directed),
                self.edge_weight_logits, sparse=self.sparse
            )  # [num_edges, 1]

        target_paths, nearest_paths = batch_dijkstra(
            slices, edge_indices,
            edge_adjacency_logits.data.numpy().flatten(),
            edge_weight_logits.data.numpy().flatten(),
            from_ix.data.numpy(), to_ix.data.numpy(),
            **parameters
        )

        target_paths = torch.as_tensor(target_paths, dtype=torch.int64)  # [batch_size, max_length]
        target_distances = F.softplus(self._get_logits(self.edge_weight_logits, target_paths)).sum(dim=(-1))
        logp_target_paths = -F.softplus(-self._get_logits(self.edge_adjacency_logits, target_paths)).sum(dim=(-1))
        # ^--[batch_size, num_targets]

        # handle paths that are not found
        not_found_target = target_paths[..., 0] == 0
        if torch.any(not_found_target):
            is_not_loop = (from_ix[:, None] != to_ix.reshape(to_ix.shape[0], -1)).reshape(to_ix.shape)
            not_found_target = not_found_target & is_not_loop

        target_distances = torch.where(not_found_target, self._get_default_distance(), target_distances)

        if parameters['k_nearest'] != 0:
            nearest_paths = torch.as_tensor(np.copy(nearest_paths), dtype=torch.int64)
            nearest_distances = F.softplus(self._get_logits(self.edge_weight_logits, nearest_paths)).sum(dim=(-1))
            nearest_vertices = edge_indices[nearest_paths[..., 0]]
            # ^--[batch_size, k_nearest]
        else:
            nearest_paths = nearest_distances = nearest_vertices = None

        return dict(
            target_paths=target_paths,
            target_distances=target_distances,
            logp_target_paths=logp_target_paths,
            found_target=~not_found_target,

            nearest_paths=nearest_paths,
            nearest_distances=nearest_distances,
            nearest_vertices=nearest_vertices,
        )

    def get_edges(self, vertex):
        """ Returns information on edges corresponding to a single vertex(int scalar) """
        if self.directed:
            slices, edge_indices = self.slices, self.edge_targets
        else:
            slices, edge_indices = self.directed_slices, self.directed_edge_indices

        begin_i, end_i = slices[vertex], slices[vertex + 1]
        edge_span = torch.arange(begin_i, end_i, dtype=torch.int64,
                                 device=self.edge_adjacency_logits.device)

        return self.Edges(
            torch.as_tensor(edge_indices[begin_i: end_i]),
            torch.sigmoid(self._get_logits(self.edge_adjacency_logits, edge_span)),
            F.softplus(self._get_logits(self.edge_weight_logits, edge_span)),
        )

    def compute_l0_prior_penalty(self, lambd=1.0, free_edge=False, start_i=1, end_i=None, batch_size=None):
        """
        Computes negative L0_prior = - mean(1 - P(edge))
        :param lambd: scalar regularization coefficient, see https://arxiv.org/abs/1712.01312 for details
        :param free_edge: if True, the most likely edge of every vertex is not penalized
        :param batch_size: if specified, averages over this many random edges (default: all edges)
        """
        device = self.edge_adjacency_logits.device
        total_edges = (self.num_edges if self.directed else len(self.directed_edge_indices))
        end_i = end_i or total_edges
        regularized_indices = np.arange(start_i, end_i)

        if free_edge:
            if self.directed:
                logits = check_numpy(self.edge_adjacency_logits)
                slices = self.slices
            else:
                logits = check_numpy(self.edge_adjacency_logits)[self.reorder_undirected_to_directed.cpu()]
                slices = self.directed_slices

            argmax_indices = slices[:-1] + sliced_argmax(logits.flatten(), slices)
            is_argmax = np.in1d(regularized_indices, argmax_indices)
            regularized_indices = regularized_indices[~is_argmax]

        if batch_size is not None:
            batch = torch.randint(0, len(regularized_indices), (batch_size,), device=device)
            regularized_indices = torch.as_tensor(regularized_indices)[batch]

        p_keep_edge = torch.sigmoid(self._get_logits(self.edge_adjacency_logits, regularized_indices))
        return lambd * p_keep_edge.mean()

    def compute_pairwise_distances(self, indices=None, edge_threshold=0.5, default=None):
        """
        Computes distances between all pairs of points,
        :param edge_threshold: minimal probability of edge for it to be kept
        :param indices: if given, comptues distances from only those indices (but TO all indices)
        :param default: use this distance if no path exists between two vertices (default=inf)
        :returns: distances matrix [len(indices) or num_vertices, num_vertices]
        """
        if indices is None:
            indices = np.arange(self.num_vertices)

        edges = defaultdict(lambda: float('inf'))  # {(from, to) -> weight}
        for ix in range(self.num_vertices):
            adj, p_adj, weights = self.get_edges(ix)
            adj = adj[p_adj >= edge_threshold].detach().numpy()
            weights = weights[p_adj >= edge_threshold].detach().numpy()
            for (target, weight) in zip(adj, weights):
                edges[int(ix), int(target)] = min(edges[int(ix), int(target)], weight)

        from_to, weights = zip(*edges.items())
        froms, tos = zip(*from_to)
        sparse_edges = scipy.sparse.coo_matrix((weights, (froms, tos)), shape=[self.num_vertices, self.num_vertices])
        distances = scipy.sparse.csgraph.dijkstra(sparse_edges, directed=self.directed, indices=indices)
        if default is None:
            default = float(self._get_default_distance().item())
        distances[np.isinf(distances)] = default
        return distances

    def pruned(self, threshold=0.5):
        """
        Prunes graph edges by their adjacency probabilities
        :param threshold: prunes all edges that have probability less than threshold
        :return: GraphEmbedding
        """
        probs = torch.sigmoid(self.edge_adjacency_logits[1:, 0]).data.numpy()
        saved_mask = probs >= threshold

        edges_from = self.edge_sources[1:]
        edges_to = self.edge_targets[1:]
        weights = F.softplus(self.edge_weight_logits[1:, 0]).data.numpy()

        return type(self)(edges_from[saved_mask], edges_to[saved_mask], initial_weights=weights[saved_mask],
                          initial_probs=probs[saved_mask], sparse=self.sparse, directed=self.directed,
                          default_distance=self._get_default_distance().item(),
                          default_distance_trainable=self.default_distance.requires_grad,
                          **self.defaults)

    def report_model_size(self, threshold=0.5, bits_per_float=32, bits_per_int=32):
        """
        Reports model size
        :param threshold: prunes all edges that have probability less than threshold
        :return: a dict with 'size_bits', 'num_parameters' and a few other statistics
        """
        num_edges = check_numpy(
            torch.sigmoid(self.edge_adjacency_logits.flatten()[1:]) >= threshold
        ).astype('int64').sum()
        num_slices = len(self.slices)
        num_vertices = num_slices + 1
        trainable_default = int(self.default_distance.requires_grad)
        num_parameters = num_vertices + 2 * num_edges + trainable_default
        params_per_vertex = num_parameters / num_vertices
        size_bits = (num_edges + num_vertices) * bits_per_int + (num_edges + trainable_default) * bits_per_float
        return locals()

    def extra_repr(self):
        edges_kept = np.sum(check_numpy(self.edge_adjacency_logits >= 0).astype('int64'))
        return "{} vertices, {} edges total, {} edges kept, {:.5} sparsity rate, default distance = {}".format(
            self.num_vertices, self.num_edges, edges_kept, 1. - edges_kept / self.num_edges,
            self.default_distance.item(),
        )

ext: py

max_stars_repo_name: yandex-research/graph-glove

max_issues_repo_name: yandex-research/graph-glove

max_forks_repo_name: yandex-research/graph-glove

reason: import numpy,import scipy


text:
import argparse
from tqdm import tqdm

import dnnlib
import legacy
import torch
from wrapper import Generator
import numpy as np

def concat_style(s_lst, layers):
    result = {layer:list() for layer in layers}
    for layer in layers:
        for s_ in s_lst:
            result[layer].append(s_[layer])
    for layer in layers:
        result[layer] = torch.cat(result[layer])
    return result

if __name__ == '__main__':
    parser = argparse.ArgumentParser()

    parser.add_argument('--ckpt', type=str, default='pretrained/ffhq.pkl')
    parser.add_argument('--dataset_name', type=str, default='')
    args = parser.parse_args()

    device = torch.device('cuda:0')
    ckpt = args.ckpt
    dataset_name = args.dataset_name

    G = Generator(ckpt, device)

    # get intermediate latent of 100000 samples
    seed = 2324
    w_lst = list()

    z = torch.from_numpy(np.random.RandomState(seed).randn(100_000, G.G.z_dim))
    for i in tqdm(range(1000)): # 100 * 1000 = 100000 # 1000
        start, end = 100 * i, 100 * (i+1)
        z_ = z[start:end].to(device)
        # apply truncation_psi=.7, first 8 layers
        w_ = G.mapping(z_.to(device), truncation_psi=0.7, truncation_cutoff=8)
        w_lst.append(w_.cpu())
    w_lst = torch.cat(w_lst)
    torch.save(w_lst, f'tensor/W{dataset_name}.pt')

    # get style of first 2000 sample in W.pt
    sample_ws = w_lst[:2000] # 2000
    sample_s = G.mapping_stylespace(sample_ws.to(device))
    for layer in G.style_layers:
        sample_s[layer] = sample_s[layer].cpu()
    torch.save(sample_s, f'tensor/S{dataset_name}.pt')
    del sample_s

    # get  std, mean of 100000 style samples
    s_lst = list()
    for i in tqdm(range(1000)): # 100 * 1000
        start, end = 100 * i, 100 * (i+1)
        w_ = w_lst[start:end]
        s_ = G.mapping_stylespace(w_.to(device))
        for layer in G.style_layers:
            s_[layer] = s_[layer].cpu()
        s_lst.append(s_)
    s_lst = concat_style(s_lst, G.style_layers)

    s_mean = {layer: torch.mean(s_lst[layer], axis=0) for layer in G.style_layers}
    s_std = {layer: torch.std(s_lst[layer], axis=0) for layer in G.style_layers}
    torch.save(s_mean, f'tensor/S_mean{dataset_name}.pt')
    torch.save(s_std, f'tensor/S_std{dataset_name}.pt')
    print("Done.")

max_stars_repo_name: soushirou/StyleCLIP-pytorch

max_issues_repo_name: soushirou/StyleCLIP-pytorch

max_forks_repo_name: soushirou/StyleCLIP-pytorch

reason: import numpy


text:
from hbu_journal.data.ntu import PretrainDataset
from hbu_journal.data.ntugems import CachedNTUGems
import numpy as np
from edflow.data.dataset import (
        ProcessedDataset, JoinedDataset,
        ConcatenatedDataset, SubDataset,
        ExtraLabelsDataset, RandomlyJoinedDataset)
from edflow.iterators.batches import save_image
from hbu_journal.data.data import TargetProcessing

def center_crop(**kwargs):
    image = kwargs["target"]
    h,w,c = image.shape
    image = image[:,w//4:w//4+w//2,:3]
    return {"image": image}

def add_labels(data, idx):
    # reid code uses the keys
    # "image", "pid", "name", "pid"
    labels = dict()
    S = data.labels["S"][idx]
    P = data.labels["P"][idx]
    file_id = data.labels["file_id"][idx]
    labels["pid"] = "S{:03}P{:03}".format(S, P)
    labels["name"] = file_id
    # furthermore, keypoints and box are not properly prepared to be stacked,
    # replace with zero since we do not need them anyway
    labels["keypoints"] = 0
    labels["box"] = 0
    return labels

# NTUGems

def get_split_indices(data):
    splits = dict()

    # fixed training person ids
    train_person_ids = {1, 2, 4, 5, 8, 9, 13, 14, 15, 16, 17, 18, 19, 25, 27, 28, 31, 35}
    train_indices = [i for i in range(len(data)) if data.labels["P"][i] in train_person_ids]
    splits["train"] = train_indices

    # remaining as validation
    train_set = set(train_indices)
    val_indices = [i for i in range(len(data)) if not i in train_set]

    # for each combination of person, camera, action randomly select
    # one frame for query and ten frames for gallery
    prng = np.random.RandomState(1)
    val_pids = sorted(set(data.labels["P"][i] for i in val_indices))
    val_cids = sorted(set(data.labels["C"][i] for i in val_indices))
    val_aids = sorted(set(data.labels["A"][i] for i in val_indices))
    query_indices = list()
    gallery_indices = list()
    for pid in val_pids:
        pid_indices = [i for i in val_indices if data.labels["P"][i] == pid]
        for cid in val_cids:
            cid_indices = [i for i in pid_indices if data.labels["C"][i] == cid]
            for aid in val_aids:
                aid_indices = [i for i in cid_indices if data.labels["A"][i] == aid]
                # select query examples
                query_examples = prng.choice(aid_indices, 1, replace = False)
                query_indices += list(query_examples)
                # remove query examples
                aid_indices = [i for i in aid_indices if not i in query_examples]
                # select gallery examples
                gallery_examples = prng.choice(aid_indices, 10, replace = False)
                gallery_indices += list(gallery_examples)
    splits["query"] = query_indices
    splits["gallery"] = gallery_indices
    return splits

def get_data(config, split):
    data = CachedNTUGems()
    indices = get_split_indices(data)[split]
    data = SubDataset(data, indices)

    # apply generic preprocessing (in particular resizing and masking)
    data = ProcessedDataset(data, TargetProcessing(config.get("spatial_size", 256), 1))
    # crop images
    data = ProcessedDataset(data, center_crop)
    # add required labels
    data = ExtraLabelsDataset(data, add_labels)
    return data

def Train(config):
    data = get_data(config, "train")
    # get multiple views of same pid
    #data = JoinedDataset(data, "pid", config.get("n_views", 4))
    data = RandomlyJoinedDataset(data, "pid", config.get("n_views", 4))
    return data

def Query(config):
    data = get_data(config, "query")
    return data

def Gallery(config):
    data = get_data(config, "gallery")
    return data

def Eval(config):
    data = ConcatenatedDataset(Query(config), Gallery(config))
    return data

if __name__ == "__main__":
    config = {"spatial_size": 256}
    d = Train(config)

    example = d[0]
    print(example.keys())
    print(len(example["image"]))
    print(example["pid"])
    print(example["name"])

    headers = ["", "Train", "Query", "Gallery"]
    datas = [Train(config), Query(config), Gallery(config)]
    pids = ["pids"]+[len(set(d.labels["pid"])) for d in datas]
    frames = ["frames"]+[len(d) for d in datas]
    rows = [headers, pids, frames]

    row_format ="{:>15}" * (len(headers))
    for row in rows:
        print(row_format.format(*row))

max_stars_repo_name: jhaux/triplet-reid

max_issues_repo_name: jhaux/triplet-reid

max_forks_repo_name: jhaux/triplet-reid


text:
import logging
import unittest

import numpy as np
import pandas as pd
from sklearn.neural_network import MLPClassifier

import pandas_ml_utils as pdu
from pandas_ml_utils.constants import *
from pandas_ml_utils.model.features_and_labels.target_encoder import OneHotEncodedTargets
from test.config import TEST_FILE
from test.utils import SMA

logger = logging.getLogger()
logger.setLevel(logging.DEBUG)

class MultiModelTest(unittest.TestCase):
    maxDiff = None

    def test_invalid_multi_model(self):
        """expect"""
        self.assertRaises(ValueError,
                          lambda: pdu.MultiModel(pdu.MultiModel(pdu.SkModel(MLPClassifier(), pdu.FeaturesAndLabels([], {})))))

    def test_multi_model_binary_classifications(self):
        """given"""
        df = pd.read_csv(TEST_FILE, index_col='Date')
        df["sma"] = SMA(df["spy_Close"])
        df["is_above_1.0"] = (df["spy_Close"] / df["sma"]) > 1
        df["is_above_1.2"] = (df["spy_Close"] / df["sma"]) > 1.2

        model = pdu.MultiModel(
            pdu.SkModel(
                MLPClassifier(activation='tanh', hidden_layer_sizes=(60, 50), random_state=42),
                pdu.FeaturesAndLabels(features=['vix_Close'],
                                      labels={"a": ["is_above_1.0"], "b": ["is_above_1.2"]},
                                      targets=lambda frame, t: frame["sma"].rename(f"sma {t}"),
                                      gross_loss=lambda frame: frame["spy_Close"] - frame["sma"])))

        """when"""
        fit = df.fit(model, test_size=0.4, test_validate_split_seed=42,)
        fit_summary_df = fit.training_summary.df
        bt_summary_df = df.backtest(fit.model).df
        predict_df = df.predict(fit.model, tail=1)

        """then"""
        self.assertListEqual(fit_summary_df.columns.tolist(),
                             [('a', PREDICTION_COLUMN_NAME, 'is_above_1.0'), ('b', PREDICTION_COLUMN_NAME, 'is_above_1.2'),
                              ('a', LABEL_COLUMN_NAME, 'is_above_1.0'), ('b', LABEL_COLUMN_NAME, 'is_above_1.2'),
                              ('a', GROSS_LOSS_COLUMN_NAME, 'a'), ('b', GROSS_LOSS_COLUMN_NAME, 'b'),
                              ('a', TARGET_COLUMN_NAME, 'sma a'), ('b', TARGET_COLUMN_NAME, 'sma b')])

        self.assertListEqual(predict_df.columns.tolist(),
                             [('a', PREDICTION_COLUMN_NAME, 'is_above_1.0'), ('b', PREDICTION_COLUMN_NAME, 'is_above_1.2'),
                              ('a', TARGET_COLUMN_NAME, 'sma a'), ('b', TARGET_COLUMN_NAME, 'sma b')])

        self.assertEqual(bt_summary_df.shape, (6706, 20))

    def test_multi_model_multi_class_classifications(self):
        """given"""
        df = pd.read_csv(TEST_FILE, index_col='Date')
        df["sma"] = SMA(df["spy_Close"])
        df["is_above_1.0"] = (df["spy_Close"] / df["sma"]) + 1
        df["is_above_1.2"] = (df["spy_Close"] / df["sma"]) + 2

        model = pdu.MultiModel(
            pdu.SkModel(
                MLPClassifier(activation='tanh', hidden_layer_sizes=(60, 50), random_state=42),
                pdu.FeaturesAndLabels(features=['vix_Close'],
                                      labels={"1": OneHotEncodedTargets("is_above_1.0", np.linspace(-0.1, 0.1, 5, endpoint=True) + 1),
                                              "2": OneHotEncodedTargets("is_above_1.2", np.linspace(-0.1, 0.1, 5, endpoint=True) + 2)},
                                      targets=lambda frame, t: (frame["sma"] + int(t)).rename(f"sma {t}"),
                                      gross_loss=lambda frame: frame["spy_Close"] - frame["sma"])))

        """when"""
        fit = df.fit(model, test_size=0.4, test_validate_split_seed=42,)
        fit_summary_df = fit.training_summary.df
        bt_summary_df = df.backtest(fit.model).df
        predict_df = df.predict(fit.model, tail=1)

        """then"""
        print(fit_summary_df.columns.tolist())
        self.assertListEqual(fit_summary_df.columns.tolist(),
                             [('1', 'prediction', '(-inf, 0.95]'), ('1', 'prediction', '(0.95, 1.0]'), ('1', 'prediction', '(1.0, 1.05]'), ('1', 'prediction', '(1.05, inf]'),
                              ('2', 'prediction', '(-inf, 1.95]'), ('2', 'prediction', '(1.95, 2.0]'), ('2', 'prediction', '(2.0, 2.05]'), ('2', 'prediction', '(2.05, inf]'),
                              ('1', 'label', '(-inf, 0.95]'), ('1', 'label', '(0.95, 1.0]'), ('1', 'label', '(1.0, 1.05]'), ('1', 'label', '(1.05, inf]'),
                              ('2', 'label', '(-inf, 1.95]'), ('2', 'label', '(1.95, 2.0]'), ('2', 'label', '(2.0, 2.05]'), ('2', 'label', '(2.05, inf]'),
                              ('1', GROSS_LOSS_COLUMN_NAME, '1'), ('2', GROSS_LOSS_COLUMN_NAME, '2'),
                              ('1', TARGET_COLUMN_NAME, 'sma 1'), ('2', TARGET_COLUMN_NAME, 'sma 2')])

        self.assertListEqual(predict_df.columns.tolist(),
                             [('1', 'prediction', '(-inf, 0.95]'), ('1', 'prediction', '(0.95, 1.0]'), ('1', 'prediction', '(1.0, 1.05]'), ('1', 'prediction', '(1.05, inf]'),
                              ('2', 'prediction', '(-inf, 1.95]'), ('2', 'prediction', '(1.95, 2.0]'), ('2', 'prediction', '(2.0, 2.05]'), ('2', 'prediction', '(2.05, inf]'),
                              ('1', TARGET_COLUMN_NAME, 'sma 1'), ('2', TARGET_COLUMN_NAME, 'sma 2')])

        self.assertEqual(bt_summary_df.shape, (6706, 32))


text:
# Test plotting N-dimensional things
import hdviz
import numpy as np
import pytest

def test_plot_3d():
    x = np.random.normal(size=(100, 3))
    a = hdviz.create_plotter(3, no_3d=True)
    a.add_pointset(x, label="points", alpha=0.7)
    assert a.num_pointsets() == 1
    ax = a.plot(panelsize=2, square=True)
    assert ax.shape == (1, 3)
    with pytest.raises(RuntimeError):
        a.plot(figsize=(7, 10))
    a.clear_data()
    # todo test: that data was cleared

def test_plot_4d():
    x = np.random.normal(size=(100, 4))
    a = hdviz.create_plotter(4)
    a.add_pointset(x, label="points", alpha=0.7)
    t = -2.0 + 4.0 * np.linspace(0, 1, 100)
    y1 = t
    y2 = 0.5 * np.sin(t)
    y3 = np.cos(4 * t)
    y4 = -0.3 * t * t
    lin = np.stack((y1, y2, y3, y4)).T
    ls = np.stack((lin, lin + 0.5))
    a.add_lineset(ls, color="red")
    ax = a.plot(panelsize=3)
    assert ax.shape == (2, 3)

def test_plot_5d():
    x = np.random.normal(size=(100, 5))
    a = hdviz.create_plotter(5)
    a.add_pointset(x, label="points", alpha=0.7, color="pink")
    t = -2.0 + 4.0 * np.linspace(0, 1, 100)
    f1 = t
    f2 = 0.5 * np.sin(t)
    f3 = np.cos(4 * t)
    f4 = -0.3 * t * t
    f5 = 0.0 * t
    v = np.stack((f1, f2, f3, f4, f5)).T
    a.add_quiverset(x, v, color="black")
    ax = a.plot()
    assert ax.shape == (3, 4)

max_stars_repo_name: jtimonen/hdviz

max_issues_repo_name: jtimonen/hdviz

max_forks_repo_name: jtimonen/hdviz


text:
"""
Word Vectors module
"""

import os
import pickle
import tempfile

from errno import ENOENT
from multiprocessing import Pool

import numpy as np

# Conditionally import Word Vector libraries as they aren't installed by default
try:
    import fasttext
    from pymagnitude import converter, Magnitude

    WORDS = True
except ImportError:
    WORDS = False

from .base import Vectors
from ..pipeline import Tokenizer

# Multiprocessing helper methods
# pylint: disable=W0603
VECTORS = None

def create(config, scoring):
    """
    Multiprocessing helper method. Creates a global embeddings object to be accessed in a new subprocess.

    Args:
        config: vector configuration
        scoring: scoring instance
    """

    global VECTORS

    # Create a global embedding object using configuration and saved
    VECTORS = WordVectors(config, scoring)

def transform(document):
    """
    Multiprocessing helper method. Transforms document into an embeddings vector.

    Args:
        document: (id, data, tags)

    Returns:
        (id, embedding)
    """

    return (document[0], VECTORS.transform(document))

class WordVectors(Vectors):
    """
    Builds sentence embeddings/vectors using weighted word embeddings.
    """

    def load(self, path):
        # Ensure that vector path exists
        if not path or not os.path.isfile(path):
            raise IOError(ENOENT, "Vector model file not found", path)

        # Load magnitude model. If this is a training run (uninitialized config), block until vectors are fully loaded
        return Magnitude(path, case_insensitive=True, blocking=not self.initialized)

    def encode(self, data):
        # Iterate over each data element, tokenize (if necessary) and build an aggregated embeddings vector
        embeddings = []
        for tokens in data:
            # Convert to tokens if necessary
            if isinstance(tokens, str):
                tokens = Tokenizer.tokenize(tokens)

            # Generate weights for each vector using a scoring method
            weights = self.scoring.weights(tokens) if self.scoring else None

            # pylint: disable=E1133
            if weights and [x for x in weights if x > 0]:
                # Build weighted average embeddings vector. Create weights array os float32 to match embeddings precision.
                embedding = np.average(self.lookup(tokens), weights=np.array(weights, dtype=np.float32), axis=0)
            else:
                # If no weights, use mean
                embedding = np.mean(self.lookup(tokens), axis=0)

            embeddings.append(embedding)

        return np.array(embeddings, dtype=np.float32)

    def index(self, documents, batchsize=1):
        # Use default single process indexing logic
        if "parallel" in self.config and not self.config["parallel"]:
            return super().index(documents, batchsize)

        # Customize indexing logic with multiprocessing pool to efficiently build vectors
        ids, dimensions, stream = [], None, None

        # Shared objects with Pool
        args = (self.config, self.scoring)

        # Convert all documents to embedding arrays, stream embeddings to disk to control memory usage
        with Pool(os.cpu_count(), initializer=create, initargs=args) as pool:
            with tempfile.NamedTemporaryFile(mode="wb", suffix=".npy", delete=False) as output:
                stream = output.name
                for uid, embedding in pool.imap(transform, documents):
                    if not dimensions:
                        # Set number of dimensions for embeddings
                        dimensions = embedding.shape[0]

                    ids.append(uid)
                    pickle.dump(embedding.reshape(1, -1), output, protocol=4)

        return (ids, dimensions, len(ids), stream)

    def lookup(self, tokens):
        """
        Queries word vectors for given list of input tokens.

        Args:
            tokens: list of tokens to query

        Returns:
            word vectors array
        """

        return self.model.query(tokens)

    @staticmethod
    def isdatabase(path):
        """
        Checks if this is a SQLite database file which is the file format used for word vectors databases.

        Args:
            path: path to check

        Returns:
            True if this is a SQLite database
        """

        if isinstance(path, str) and os.path.isfile(path) and os.path.getsize(path) >= 100:
            # Read 100 byte SQLite header
            with open(path, "rb") as f:
                header = f.read(100)

            # Check for SQLite header
            return header.startswith(b"SQLite format 3\000")

        return False

    @staticmethod
    def build(data, size, mincount, path):
        """
        Builds fastText vectors from a file.

        Args:
            data: path to input data file
            size: number of vector dimensions
            mincount: minimum number of occurrences required to register a token
            path: path to output file
        """

        # Train on data file using largest dimension size
        model = fasttext.train_unsupervised(data, dim=size, minCount=mincount)

        # Output file path
        print(f"Building {size} dimension model")

        # Output vectors in vec/txt format
        with open(path + ".txt", "w", encoding="utf-8") as output:
            words = model.get_words()
            output.write(f"{len(words)} {model.get_dimension()}\n")

            for word in words:
                # Skip end of line token
                if word != "</s>":
                    vector = model.get_word_vector(word)
                    data = ""
                    for v in vector:
                        data += " " + str(v)

                    output.write(word + data + "\n")

        # Build magnitude vectors database
        print("Converting vectors to magnitude format")
        converter.convert(path + ".txt", path + ".magnitude", subword=True)

max_stars_repo_name: malywonsz/txtai

max_issues_repo_name: malywonsz/txtai

max_forks_repo_name: malywonsz/txtai


text:
from typing import Dict, Union

import pyomo.environ as pyo

from oogeso import dto
from oogeso.core.devices.base import Device

class HeatPump(Device):
    """
    Heat pump or electric heater (el to heat)
    """

    carrier_in = ["el"]
    carrier_out = ["heat"]
    serial = []

    def __init__(
        self,
        dev_data: dto.DeviceHeatPumpData,  # Fixme: Correct?
        carrier_data_dict: Dict[str, dto.CarrierElData],  # Fixme: Correct?
    ):
        super().__init__(dev_data=dev_data, carrier_data_dict=carrier_data_dict)
        self.dev_data = dev_data
        self.id = dev_data.id
        self.carrier_data = carrier_data_dict

    def _rules(self, pyomo_model: pyo.Model, t: int) -> Union[pyo.Expression, pyo.Constraint.Skip]:
        dev = self.id
        # heat out = el in * efficiency
        lhs = pyomo_model.varDeviceFlow[dev, "heat", "out", t]
        rhs = pyomo_model.varDeviceFlow[dev, "el", "in", t] * pyomo_model.paramDevice[dev]["eta"]
        return pyo.Expression(lhs == rhs)

    def define_constraints(self, pyomo_model: pyo.Model):
        """Specifies the list of constraints for the device"""

        list_to_reconstruct = super().define_constraints(pyomo_model)

        constr = pyo.Constraint(pyomo_model.setHorizon, rule=self._rules)
        # add constraint to model:
        setattr(pyomo_model, "constr_{}_{}".format(self.id, "misc"), constr)
        return list_to_reconstruct

    def get_flow_var(self, pyomo_model: pyo.Model, t: int):
        return pyomo_model.varDeviceFlow[self.id, "el", "in", t]

max_stars_repo_name: oogeso/oogeso

max_issues_repo_name: oogeso/oogeso

max_forks_repo_name: oogeso/oogeso

reason: import pyomo


text:
import json
import cv2
import numpy as np
import tensorflow as tf
from tensorflow.keras import layers
from pathlib import Path
import os
import time
import argparse
from model_builder import ModelBuilder

arg_parser = argparse.ArgumentParser(
    description='Run the preprocessing pipeline.'
)
arg_parser.add_argument(
    '-d', '--dictionary_path', type=str, default='dictionary.txt',
    help='Path to where dictionary will be created.')
arg_parser.add_argument(
    '-s', '--dataset_path', type=str, default='dataset',
    help='Path of where to look for the dataset.')
arg_parser.add_argument(
    '-c', '--checkpoint', type=str, default=None,
    help='Checkpoint to start from.'
)
args = arg_parser.parse_args()

buffer_size = 30000
batch_size = 16
dataset_root = Path(args.dataset_path)
dictionary_path = Path(args.dictionary_path)
num_frames_initial = 253
frame_sampling_rate = 15
num_frames = int(num_frames_initial / frame_sampling_rate) + 1
frame_shape = (64, 64)
input_shape = [batch_size, num_frames, frame_shape[0], frame_shape[1]]
num_epochs = 500
noise_dim = 20
num_examples_to_generate = 8
learning_rate = 1e-4

file_batches = tf.data.Dataset.list_files(
    str(dataset_root / '*/*/*.avi')).shuffle(buffer_size).batch(batch_size)

words_in_dir = set()
for word_dir in dataset_root.glob('*/*'):
    words_in_dir.add(word_dir.parts[-1])

index_by_word = {}
with open(str(dictionary_path)) as f:
    for i, word in enumerate(f.readlines()):
        stripped = word.strip()
        if (stripped in words_in_dir):
            index_by_word[stripped] = i

num_words = len(index_by_word)

model_builder = ModelBuilder(
    num_frames,
    frame_shape,
    num_words,
    noise_dim,
    learning_rate)

generator = model_builder.build_generator()
discriminator = model_builder.build_discriminator()

cross_entropy = tf.keras.losses.BinaryCrossentropy(from_logits=True)

def discriminator_loss(real_output, fake_output):
    real_loss = cross_entropy(tf.ones_like(real_output), real_output)
    fake_loss = cross_entropy(tf.zeros_like(fake_output), fake_output)
    total_loss = real_loss + fake_loss
    return total_loss

def generator_loss(fake_output):
    return cross_entropy(tf.ones_like(fake_output), fake_output)

generator_optimizer, discriminator_optimizer = model_builder.get_optimizers()

checkpoint_dir = './training_checkpoints'
checkpoint_prefix = 'ckpt'
checkpoint = model_builder.get_checkpoint(
    checkpoint_dir, checkpoint_prefix, generator_optimizer,
    discriminator_optimizer, generator, discriminator)
checkpoint_manager = tf.train.CheckpointManager(
    checkpoint, checkpoint_dir, max_to_keep=3)

if (args.checkpoint is not None):
    checkpoint.restore(args.checkpoint)

seed = tf.random.normal([num_examples_to_generate, noise_dim])

rand_gen = tf.random.Generator.from_seed(1234)
rand = rand_gen.uniform(
    [num_examples_to_generate],
    minval=0,
    maxval=num_words - 1,
    dtype=tf.dtypes.int32)
test_labels = tf.one_hot(rand, num_words)

@tf.function
def train_step(images, labels):
    noise = tf.random.normal([batch_size, noise_dim])
    rand = rand_gen.uniform(
        [batch_size],
        minval=0,
        maxval=num_words - 1,
        dtype=tf.dtypes.int32)
    gen_labels = tf.one_hot(rand, num_words)
    with tf.GradientTape() as gen_tape, tf.GradientTape() as disc_tape:
        generated_images = generator([noise, gen_labels], training=True)

        real_output = discriminator([images, labels], training=True)
        fake_output = discriminator(
            [generated_images, gen_labels],
            training=True)

        gen_loss = generator_loss(fake_output)
        disc_loss = discriminator_loss(real_output, fake_output)

    gradients_of_generator = gen_tape.gradient(
        gen_loss, generator.trainable_variables)
    gradients_of_discriminator = disc_tape.gradient(
        disc_loss, discriminator.trainable_variables)

    generator_optimizer.apply_gradients(
        zip(gradients_of_generator, generator.trainable_variables))
    discriminator_optimizer.apply_gradients(
        zip(gradients_of_discriminator, discriminator.trainable_variables))

    return gen_loss, disc_loss

def get_video_data(file_batch):
    train_data = np.empty(
        (batch_size,
         num_frames,
         frame_shape[0],
         frame_shape[1],
         1))
    train_labels = np.zeros((batch_size, num_words))
    for file_index, file_path in enumerate(file_batch):
        train_file = Path(file_path.numpy().decode('utf-8'))
        label = train_file.parts[-2]
        label_index = index_by_word[label]

        cap = cv2.VideoCapture(str(train_file))
        video_data = np.zeros((num_frames, frame_shape[0], frame_shape[1], 1))
        frame_num = -1
        while (True):
            ret, frame = cap.read()
            frame_num += 1
            frame_mod = frame_num % frame_sampling_rate
            if (frame_mod != 0):
                continue
            if (frame is None):
                break

            frame_index = int(frame_num / frame_sampling_rate)

            resized = cv2.resize(frame, frame_shape)
            gray_frame = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY)
            normalized_frame = (
                gray_frame /
                255.0).reshape(
                (gray_frame.shape[0],
                 gray_frame.shape[1],
                 1))
            video_data[frame_index] = normalized_frame

        cap.release()

        train_data[file_index] = video_data
        train_labels[file_index, label_index] = 1

    return train_data, train_labels

def train(dataset, epochs):
    for epoch in range(epochs):
        start = time.time()

        gen_losses = []
        disc_losses = []
        for file_batch in file_batches:
            train_data, train_labels = get_video_data(file_batch)
            gen_loss, disc_loss = train_step(train_data, train_labels)
            gen_losses.append(gen_loss)
            disc_losses.append(disc_loss)

        # Save the model every 10 epochs
        if (epoch + 1) % 10 == 0:
            checkpoint_manager.save()

        print('Time for epoch {} is {} sec. min gen loss: {}, min disc loss: {}.'.format(
            epoch + 1, time.time() - start, min(gen_losses), min(disc_losses)))

train(file_batches, num_epochs)

max_stars_repo_name: RLashofRegas/speach-to-sign-language

max_issues_repo_name: RLashofRegas/speach-to-sign-language

max_forks_repo_name: RLashofRegas/speach-to-sign-language


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

'''
Video mixing script
'''

import argparse
import random
import numpy as np
from reparo.utils import read_video, write_video

def main(args):

    frames, fps_setting, frame_setting = read_video(args.video, max_frames=200)

    # Add noise inbetween them
    for _ in range(int(args.noise * len(frames))):
        frames.append((255 * np.random.rand(*frame_setting, 3)).astype(np.uint8))

    # Mix frames up
    random.shuffle(frames)

    # Write video
    write_video(frames, args.outfile, fps=fps_setting, frame=frame_setting)

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Video mixer script',
                                     formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument("video", type=str, help="The path to the corrupted video")
    parser.add_argument("--outfile", type=str, default='result.mp4', help="Path to output file")
    parser.add_argument("--noise", type=float, default=0.05, help="Proportion of noisy frames added")

    args = parser.parse_args()

    main(args)

max_stars_repo_name: frgfm/video-mender

max_issues_repo_name: frgfm/video-mender

max_forks_repo_name: frgfm/video-mender


text:
"""
Soft actor critic per "Soft Actor-Critic Algorithms and Applications" Haarnoja
et. al 2019.
"""
import sys
import copy
import operator
import functools

import torch
import torch.nn.functional as F
import torch.nn as nn
from torch.optim import Adam
from torch.distributions import Normal
import numpy as np

from tensorboardX import SummaryWriter

sys.path.insert(0, "..")
from neural_nets.base_network import BaseNN, BasePolicy, Checkpointer  # NOQA
from rlmethods.base_rl import BaseRL

DEVICE = "cuda" if torch.cuda.is_available() else "cpu"

MAX_FLOAT = torch.finfo(torch.float32).max
FEPS = torch.finfo(torch.float32).eps

NN_HIDDEN_WIDTH = 256

def copy_params(source, target):
    """Copies parameters from source network to target network.

    :param source: Network to copy parameters from.
    :param target: Network to copy parameters to.
    """
    for target_param, param in zip(target.parameters(), source.parameters()):
        target_param.data.copy_(param.data)

def move_average(source, target, tau=0.005):
    """Compute and set moving average for target network.

    :param source: Network with new weights.
    :param target: Network to whose weights are moved closer to source.
    :param tau: Average rate, default 0.005 as per paper.
    """
    for target_param, param in zip(target.parameters(), source.parameters()):
        target_param.data.copy_(
            target_param.data * (1.0 - tau) + param.data * tau
        )

def is_degenerate(ten):
    """Returns true if ten contains inf or nan.

    :param ten: input tensor to check for degeneracies.
    """
    contains_nan = torch.isnan(ten).any()
    contains_inf = (ten == float("inf")).any()

    return contains_nan or contains_inf

def soften_distribution(probs, alpha):
    """Apply additive smoothing to discrete probabilities.

    :param probs: Probabilities to smooth.
    :param alpha: additive factor.
    """
    soft = probs + alpha
    soft = soft / soft.sum(dim=1, keepdim=True)

    return soft

class QNetwork(BaseNN):
    """Q function network."""

    def __init__(self, state_length, action_space, hidden_layer_width):
        super().__init__()

        action_length = functools.reduce(operator.mul, action_space.shape)

        # q1
        self.q1_in_layer = nn.Linear(
            state_length + action_length, hidden_layer_width
        )
        self.q1_hidden1 = nn.Linear(hidden_layer_width, hidden_layer_width)
        self.q1_hidden2 = nn.Linear(hidden_layer_width, hidden_layer_width)
        self.q1_head = nn.Linear(hidden_layer_width, 1)

        # q2
        self.q2_in_layer = nn.Linear(
            state_length + action_length, hidden_layer_width
        )
        self.q2_hidden1 = nn.Linear(hidden_layer_width, hidden_layer_width)
        self.q2_hidden2 = nn.Linear(hidden_layer_width, hidden_layer_width)
        self.q2_head = nn.Linear(hidden_layer_width, 1)
        self.to(DEVICE)

    def forward(self, states, actions):
        state_action = torch.cat([states, actions], dim=1)

        # q1
        q1 = F.relu(self.q1_in_layer(state_action))
        q1 = F.relu(self.q1_hidden1(q1))
        q1 = F.relu(self.q1_hidden2(q1))
        q1 = self.q1_head(q1)

        # q2
        q2 = F.relu(self.q2_in_layer(state_action))
        q2 = F.relu(self.q2_hidden1(q2))
        q2 = F.relu(self.q2_hidden2(q2))
        q2 = self.q2_head(q2)

        return q1, q2

class PolicyNetwork(BasePolicy):
    """Policy network for soft actor critic."""

    def __init__(self, state_size, action_space, hidden_layer_width):
        super().__init__()

        # properties inferred from action space
        self.action_scale = (
            torch.tensor((action_space.high - action_space.low) / 2.0)
            .to(DEVICE)
            .to(torch.float)
        )

        self.action_bias = (
            torch.tensor((action_space.high + action_space.low) / 2.0)
            .to(DEVICE)
            .to(torch.float)
        )

        action_width = functools.reduce(operator.mul, action_space.shape)

        self.in_layer = nn.Linear(state_size, hidden_layer_width)
        self.hidden1 = nn.Linear(hidden_layer_width, hidden_layer_width)
        self.hidden2 = nn.Linear(hidden_layer_width, hidden_layer_width)

        # params for Gaussian
        self.means = nn.Linear(hidden_layer_width, action_width)
        self.log_stds = nn.Linear(hidden_layer_width, action_width)

        self.to(DEVICE)

    def forward(self, x):
        x = F.relu(self.in_layer(x))
        x = F.relu(self.hidden1(x))
        x = F.relu(self.hidden2(x))

        means = self.means(x)
        log_stds = self.log_stds(x)
        log_stds = torch.clamp(log_stds, min=-20.0, max=2.0)

        return means, log_stds

    def sample(self, state):
        """Generate an action based on state vector using current policy.

        :param state: Current state vector. must be Torch 32 bit float tensor.
        """
        dist = self.action_distribution(state)
        raw_action = dist.rsample()  # reparametrization trick

        # enforcing action bounds
        tanh_action = torch.tanh(raw_action)  # prevent recomputation later.
        action = tanh_action * self.action_scale + self.action_bias

        # change of variables for log prob
        raw_log_prob = dist.log_prob(raw_action)
        log_prob = raw_log_prob - torch.log(
            self.action_scale * (1 - tanh_action.pow(2)) + FEPS
        )
        log_prob = log_prob.sum(1, keepdim=True)

        return action, log_prob, dist

    def sample_action(self, state):
        return self.sample(state)[0]

    def eval_action(self, state):
        """Select action for playing. Selects mean action only.

        :param state: State to select action from.
        :return: produced action.
        """
        means, _ = self.__call__(state)
        action = self.action_scale * means + self.action_bias

        return action.detach().cpu().numpy()

    def action_distribution(self, state):
        """Returns a pytorch distribution object based on policy output.

        :param state: Input state vector.
        """
        means, stds = self.__call__(state)
        dist = Normal(means, torch.exp(stds))

        return dist

    def action_log_probs(self, state):
        """Generate an action based on state vector using current policy.

        :param state: Current state vector. must be Torch 32 bit float tensor.
        """
        dist = self.action_distribution(state)
        raw_action = dist.rsample()  # reparametrization trick

        # enforcing action bounds
        tanh_action = torch.tanh(raw_action)  # prevent recomputation later.
        action = tanh_action * self.action_scale + self.action_bias

        # change of variables for log prob
        raw_log_prob = dist.log_prob(raw_action)
        log_prob = raw_log_prob - torch.log(
            self.action_scale * (1 - tanh_action.pow(2)) + FEPS
        )
        log_prob = log_prob.sum(1, keepdim=True)

        return action, log_prob

class SoftActorCritic(BaseRL):
    """Implementation of soft actor critic."""

    def __init__(
        self,
        env,
        replay_buffer,
        feature_extractor,
        buffer_sample_size,
        gamma=0.99,
        learning_rate=3e-4,
        tbx_writer=None,
        entropy_tuning=False,
        entropy_target=-2.0,
        tau=0.005,
        log_alpha=-2.995,
        play_interval=1,
        render=False,
        checkpoint_path="./SAC_checkpoints/",
        checkpointer=None,
        checkpoint_interval=1000,
        checkpoint_name="SAC",
    ):
        # env related settings
        self.env = env
        self.render = render
        self.feature_extractor = feature_extractor
        starting_feature = self.env_reset()
        feature_size = starting_feature.shape[0]

        # buffer
        self.replay_buffer = replay_buffer
        self.buffer_sample_size = buffer_sample_size

        # set hyperparameters
        self.log_alpha = torch.tensor(log_alpha).to(DEVICE)
        self.log_alpha = self.log_alpha.detach().requires_grad_(True)
        self.gamma = gamma
        self.entropy_target = entropy_target
        self.tau = tau
        self.play_interval = play_interval
        self.lr = learning_rate

        # NNs
        if checkpointer:
            self.checkpointer = checkpointer

            try:
                self.policy = checkpointer.models["policy"].model
                self.policy_optim = checkpointer.models["policy"].optimizer

                self.q_net = checkpointer.models["q_net"].model
                self.q_optim = checkpointer.models["q_net"].optimizer

                self.log_alpha = checkpointer.models["alpha"].model
                self.alpha_optim = checkpointer.models["alpha"].optimizer

            except KeyError:
                raise KeyError("Models not found in checkpointer!")

        else:
            self.policy = PolicyNetwork(
                feature_size, env.action_space, NN_HIDDEN_WIDTH
            )
            self.q_net = QNetwork(
                feature_size, env.action_space, NN_HIDDEN_WIDTH
            )

            # optimizers
            self.policy_optim = Adam(
                self.policy.parameters(), lr=learning_rate
            )
            self.q_optim = Adam(self.q_net.parameters(), lr=learning_rate)
            self.alpha_optim = Adam([self.log_alpha], lr=1e-2)

            # initialize checkpointer
            self.checkpointer = Checkpointer(
                checkpoint_path, checkpoint_interval, checkpoint_name
            )

            self.checkpointer.add_model(
                "policy", self.policy, self.policy_optim
            )
            self.checkpointer.add_model("q_net", self.q_net, self.q_optim)
            self.checkpointer.add_model(
                "alpha", self.log_alpha, self.alpha_optim
            )

        # initialize weights of moving avg Q net
        self.avg_q_net = copy.deepcopy(self.q_net)
        copy_params(self.q_net, self.avg_q_net)

        # training meta
        self.training_i = self.checkpointer.checkpoint_counter
        self.play_i = 0
        self.entropy_tuning = entropy_tuning

        # tensorboardX settings
        if not tbx_writer:
            self.tbx_writer = SummaryWriter("runs/generic_soft_ac")
        else:
            self.tbx_writer = tbx_writer

    def env_reset(self):
        """Reset environment, but return features from feature_extractor.

        :return: state(features)
        """
        state = self.env.reset()
        return self.feature_extractor.extract_features(state)

    def env_step(self, action):
        """Takes action in environment but returns features rather than
        states, from feature_extractor.

        :param action: action to take.
        :return: state(features), reward, done, info
        """
        state, reward, done, info = self.env.step(action)
        state = self.feature_extractor.extract_features(state)

        return state, reward, done, info

    def populate_buffer(self, num_transitions):
        """
        Fill in entire replay buffer with state action pairs using current
        policy.
        """
        while len(self.replay_buffer) < self.buffer_sample_size:
            self.play(num_transitions)

    def tbx_logger(self, log_dict, training_i):
        """Logs the tag-value pairs in log_dict using TensorboardX.

        :param log_dict: {tag:value} dictionary to log.
        :param training_i: Current training iteration.
        """
        for tag, value in log_dict.items():
            self.tbx_writer.add_scalar(tag, value, training_i)

    def reset_training(self):
        """
        Resets the optimizers used in the training
        """
        self.policy_optim = Adam(self.policy.parameters(), lr=self.lr)
        self.q_optim = Adam(self.q_net.parameters(), lr=self.lr)

        self.alpha_optim = Adam([self.log_alpha], lr=1e-2)

    def train_episode(self, max_episode_length):
        """Train Soft Actor Critic"""

        # Populate the buffer
        self.populate_buffer(max_episode_length)

        # weight updates
        replay_samples = self.replay_buffer.sample(self.buffer_sample_size)
        state_batch = torch.from_numpy(replay_samples[0]).to(DEVICE)
        action_batch = torch.from_numpy(replay_samples[1]).to(DEVICE)
        reward_batch = (
            torch.from_numpy(replay_samples[2]).to(DEVICE).unsqueeze(1)
        )
        next_state_batch = torch.from_numpy(replay_samples[3]).to(DEVICE)
        dones = (
            torch.from_numpy(replay_samples[4])
            .type(torch.long)
            .to(DEVICE)
            .unsqueeze(1)
        )

        # alpha must be clamped with a minumum of zero, so use exponential.
        alpha = self.log_alpha.exp().detach()

        with torch.no_grad():
            # Figure out value function
            next_actions, log_next_actions, _ = self.policy.sample(
                next_state_batch
            )
            target_q1, target_q2 = self.avg_q_net(
                next_state_batch, next_actions
            )
            target_q = torch.min(target_q1, target_q2)
            next_state_values = target_q - alpha * log_next_actions

            # Calculate Q network target
            done_floats = dones.type(torch.float)
            q_target = reward_batch.clone()
            q_target += self.gamma * done_floats * next_state_values

        # Q net outputs values for all actions, so we index specific actions
        q1, q2 = self.q_net(state_batch, action_batch)
        q1_loss = F.mse_loss(q1, q_target)
        q2_loss = F.mse_loss(q2, q_target)

        # policy loss
        actions_pi, log_probs_pi, action_dist = self.policy.sample(state_batch)
        q1_pi, q2_pi = self.q_net(state_batch, actions_pi)
        q_pi = torch.min(q1_pi, q2_pi)
        policy_loss = ((alpha * log_probs_pi) - q_pi).mean()

        # update parameters
        self.q_optim.zero_grad()
        q1_loss.backward()
        self.q_optim.step()

        self.q_optim.zero_grad()
        q2_loss.backward()
        self.q_optim.step()

        self.policy_optim.zero_grad()
        policy_loss.backward()
        self.policy_optim.step()

        # automatic entropy tuning
        alpha_loss = (
            self.log_alpha * (log_probs_pi + self.entropy_target).detach()
        )
        alpha_loss = -alpha_loss.mean()

        if self.entropy_tuning:
            self.alpha_optim.zero_grad()
            alpha_loss.backward()
            self.alpha_optim.step()

        # Step average Q net
        move_average(self.q_net, self.avg_q_net, self.tau)

        # logging
        self.tbx_logger(
            {
                "loss/q1 loss": q1_loss.item(),
                "loss/q2 loss": q2_loss.item(),
                "loss/pi loss": policy_loss.item(),
                "loss/alpha loss": alpha_loss.item(),
                "Q/avg_q_target": q_target.mean().item(),
                "Q/avg_q1": q1.mean().item(),
                "Q/avg_q2": q2.mean().item(),
                "Q/avg_reward": reward_batch.mean().item(),
                "Q/avg_V": next_state_values.mean().item(),
                "H/alpha": alpha.item(),
                "H/pi_entropy": action_dist.entropy().mean(),
                "H/pi_log_pi": log_probs_pi.mean(),
            },
            self.training_i,
        )

        self.training_i += 1
        self.checkpointer.increment_counter()

    def play(self, max_steps, render=False, reward_network=None):
        """
        Play one complete episode in the environment's gridworld.
        Automatically appends to replay buffer, and logs with Tensorboardx.
        """

        done = False
        total_reward = np.zeros(1)
        state = self.env_reset()
        episode_length = 0
        max_steps_elapsed = False

        while not (done or max_steps_elapsed):
            # Env returns numpy state so convert to torch
            torch_state = torch.from_numpy(state).type(torch.float32)
            torch_state = torch_state.to(DEVICE).unsqueeze(0)

            torch_action, _, _ = self.policy.sample(torch_state)
            action = torch_action.detach().cpu().numpy()
            action = action.reshape(self.env.action_space.shape)

            next_state, reward, done, _ = self.env_step(action)

            if reward_network:
                # reward networks can be either r(s) or r(s,a)
                try:
                    reward = reward_network(torch_state)
                except TypeError:
                    reward = reward_network(torch_state, torch_action)

                reward = float(reward.cpu().detach().item())

            episode_length += 1

            max_steps_elapsed = episode_length > max_steps

            if max_steps_elapsed:
                self.replay_buffer.push(
                    (state, action, reward, next_state, done)
                )
            else:
                self.replay_buffer.push(
                    (state, action, reward, next_state, not done)
                )

            if render:
                self.env.render()

            state = next_state
            total_reward += reward

        self.tbx_writer.add_scalar(
            "rewards/episode_reward", total_reward.item(), self.play_i
        )

        self.tbx_writer.add_scalar(
            "rewards/episode_length", episode_length, self.play_i
        )

        self.play_i += 1

    def train(
        self, num_episodes, max_episode_length, reward_network=None,
    ):
        """Train and play environment every play_interval, appending obtained
        states, actions, rewards, and dones to the replay buffer.

        :param num_episodes: number of episodes to train for.
        :param play_interval: trainig episodes between each play session.
        """

        for _ in range(num_episodes):
            self.train_episode(max_episode_length)

            if self.training_i % self.play_interval == 0:
                self.play(
                    max_episode_length,
                    self.render,
                    reward_network=reward_network,
                )


text:
import math
import warnings

import numpy as np
import scipy.linalg as la
from scipy.stats import unitary_group

from converter.qiskit import QISKitError
from converter.qiskit.quantum_info import pauli_group
from converter.qiskit.quantum_info import state_fidelity as new_state_fidelity

def qft(circ, q, n):
    pass

def partial_trace(state, trace_systems, dimensions=None, reverse=True):
    pass

def __partial_trace_vec(vec, trace_systems, dimensions, reverse=True):
    pass

def __partial_trace_mat(mat, trace_systems, dimensions, reverse=True):
    pass

def vectorize(density_matrix, method='col'):
    pass

def devectorize(vectorized_mat, method='col'):
    pass

def choi_to_rauli(choi, order=1):
    pass

def chop(array, epsilon=1e-10):
    pass

def outer(vector1, vector2=None):
    pass

def random_unitary_matrix(length):
    pass

def random_density_matrix(length, rank=None, method='Hilbert-Schmidt'):
    pass

def __ginibre_matrix(nrow, ncol=None):
    pass

def __random_density_hs(N, rank=None):
    pass

def __random_density_bures(N, rank=None):
    pass

def state_fidelity(state1, state2):
    pass

def purity(state):
    pass

def concurrence(state):
    pass

def shannon_entropy(pvec, base=2):
    pass

        def logfn(x):
            pass
        def logfn(x):
            pass
        def logfn(x):
            pass

def entropy(state):
    pass

def mutual_information(state, d0, d1=None):
    pass

def entanglement_of_formation(state, d0, d1=None):
    pass

def __eof_qubit(rho):
    pass

def is_pos_def(x):
    pass

reason: import numpy,import scipy,from scipy


text:
import numpy as np
import matplotlib.pyplot as plt
from models import slip
import viability as vibly

p = {'mass': 80.0, 'stiffness': 8200.0, 'spring_resting_length': 1.0,
     'gravity': 9.81, 'angle_of_attack': 1/5*np.pi,
     'actuator_resting_length': 0}
x0 = np.array([0, 0.85, 5.5, 0, 0, 0, 0])
x0 = slip.reset_leg(x0, p)
p['x0'] = x0
p['total_energy'] = slip.compute_total_energy(x0, p)
p_map = slip.p_map
p_map.p = p
p_map.x = x0
p_map.sa2xp = slip.sa2xp
p_map.xp2s = slip.xp2s

s_grid = np.linspace(0.1, 1, 91)
s_grid = (s_grid[:-1],)
a_grid = (np.linspace(-10/180*np.pi, 70/180*np.pi, 81),)
grids = {'states': s_grid, 'actions': a_grid}
Q_map, Q_F = vibly.compute_Q_map(grids, p_map)
# Q_map, Q_F = vibly.parcompute_Q_map(grids, p_map)
Q_V, S_V = vibly.compute_QV(Q_map, grids)
S_M = vibly.project_Q2S(Q_V, grids, proj_opt=np.mean)
Q_M = vibly.map_S2Q(Q_map, S_M, s_grid, Q_V=Q_V)

################################################################################
# save data as pickle
################################################################################
import pickle

filename = '../data/dynamics/slip_map' + '.pickle'
data2save = {"grids": grids, "Q_map": Q_map, "Q_F": Q_F, "Q_V": Q_V,
             "Q_M": Q_M, "S_M": S_M, "p": p, "x0": x0}
outfile = open(filename, 'wb')
pickle.dump(data2save, outfile)
outfile.close()
# to load this data, do:
# infile = open(filename, 'rb')
# data = pickle.load(infile)
# infile.close()

################################################################################
# basic visualization
################################################################################

plt.imshow(Q_map, origin='lower')
plt.show()

max_stars_repo_name: PFMassiani/vibly

max_issues_repo_name: PFMassiani/vibly

max_forks_repo_name: PFMassiani/vibly


text:
import unittest
import numpy as np
from probreg import math_utils as mu

class MathUtilsTest(unittest.TestCase):
    def test_pairwise_squared_sum(self):
        n = 5
        dim = 3
        x = np.arange(n * dim).reshape((n, dim))
        ans = np.sum([np.sum((x[i] - x)**2) for i in range(n)]) / (n * n * dim)
        self.assertAlmostEqual(mu.squared_kernel_sum(x, x), ans)

    def test_rbf_kernel(self):
        x = np.random.rand(5, 3)
        g = mu.rbf_kernel(x, x, 1.0)
        self.assertTrue(np.allclose(g, g.T))

if __name__ == "__main__":
    unittest.main()

max_stars_repo_name: OscarPellicer/probreg

max_issues_repo_name: OscarPellicer/probreg

max_forks_repo_name: OscarPellicer/probreg


text:
import numpy as np
from numpy import genfromtxt
from main import pretty_print, encode_as_binary
from numpy.polynomial import Polynomial

error_vectors = genfromtxt('error_vectors.csv', delimiter=';')

polynomial_definitions = [
    [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, 1, 0],
    [0, 0, 0, 0, 0, 0, 0, 1],
    [1, 0, 0, 0, 1, 0, 1, 1],
    [1, 1, 0, 0, 1, 1, 1, 0],
    [0, 1, 1, 0, 0, 1, 1, 1],
    [1, 0, 1, 1, 1, 0, 0, 0],
    [0, 1, 0, 1, 1, 1, 0, 0],
    [0, 0, 1, 0, 1, 1, 1, 0],
    [0, 0, 0, 1, 0, 1, 1, 1],
]

polynomials = map(Polynomial, polynomial_definitions)

def generate_rxs():
    rxs = []
    for error_vector in error_vectors:
        polys = []
        for i in range(len(polynomials)):
            poly = error_vector[i] * polynomials[i]
            polys.append(poly)

        rxs.append(polys)
    return list(map(sum, rxs))

def compute():
    return generate_rxs()

def get_coef(poly):
    return poly.coef

def padarray(A):
    t = 8 - len(A)
    return np.pad(A, pad_width=(0, t), mode='constant')

def main():
    polys = compute()
    coeffs = list(map(get_coef, polys))
    coefficients = list(encode_as_binary, list(map(padarray, coeffs)))
    pretty_print(coefficients)

if __name__ == '__main__':
    main()

# pretty_print(error_vectors)

reason: import numpy,from numpy