text:
#!/usr/bin/env python3
# Author: Rick Gelhausen

import sys, argparse
import os
import re
import operator

import pandas as pd
import numpy as np

from itertools import cycle
import matplotlib.pyplot as plt
import seaborn as sns

########################################################################################################################
#                                                                                #
#                                   Plot contents of a given benchmark.csv file                                        #
#                                                                                #
#                                                                                #
########################################################################################################################

# List of easily differentiable color codes
colorList = ['#f032e6', '#808080', '#000080', '#808000', '#aaffc3', '#ffe119', '#800000', '#aa6e28', '#e6beff', '#008080',
             '#fabebe', '#d2f53c', '#46f0f0', '#911eb4', '#f58231', '#0082c8', '#3cb44b']

lineList = ["--", "-"]

# Differentiate between ints and other chars/strings
def isInt(i):
    try:
        return int(i)
    except:
        return i

# Split strings into lists of strings and numbers
def alphanumeric_key(s):
    return [isInt(c) for c in re.split('([0-9]+)', s)]

# Sort the way a human expects it
def human_sort(l):
    l.sort(key=alphanumeric_key)

# set axis
def set_axis_style(ax, labels):
    ax.get_xaxis().set_tick_params(direction='out')
    # ax.xaxis.set_ticks_position('bottom')
    ax.set_xticks(np.arange(1, len(labels) + 1))
    ax.set_xticklabels(labels, fontsize=12)
    #ax.set_xlim(0.25, len(labels) + 0.75)

def main(argv):
    parser = argparse.ArgumentParser(description="Script for plotting the benchmark results")
    parser.add_argument("-i", "--ifile", action="store", dest="benchmarkFile", required=True
                        , help="benchmark file to be used for plotting.")
    parser.add_argument("-o", "--ofile", action="store", dest="outFile", required=True
                        , help="the path of the output pdf.")
    parser.add_argument("-f", "--fixed", action="store", dest="fixedID", required=True
                        , help="callID that gets a fixed color (red)")
    parser.add_argument("-t", "--title", action="store", dest="title", default=""
                        , help="the title of the plot.")
    parser.add_argument("-r", "--rankThreshold", action="store", dest="ranks", nargs="+", type=int
                        , default=[5,10,50,100,200], help="list of thresholds for the violin plots.")
    parser.add_argument("-y", "--ymax", action="store", dest="ymax", default=0, type=int
                        , help="set the maximum value of the y-axis to make all plots comparable.")
    parser.add_argument("--sep", action="store", dest="separator", default=";"
                        , help="the separator used in the benchmarkFile")
    parser.add_argument("--info", action="store_true", dest="info", default=False
                        , help="create time and memory plots.")
    args = parser.parse_args()

    # if Benchmark file exists plot the data
    if os.path.exists(args.benchmarkFile):
        # Ensure that the ranks are sorted and set targetEnd
        thresholds = sorted(args.ranks)
        targetEnd = thresholds[-1]

        # Handle the input and build a dictionary for the ranks up to targetEnd (200)
        benchDF = pd.read_csv(args.benchmarkFile, sep=args.separator, header=0)
        prefix = ["srna_name", "target_ltag", "target_name"]
        intarnaIDs = [x.replace("_intarna_rank", "") for x in benchDF.columns if x not in prefix]

        rankDictionary = dict()
        # Init dictionary
        for entry in intarnaIDs:
            rankDictionary[entry] = []

        # Get the ranks for each callID
        for i in range(0, targetEnd):
            for id in intarnaIDs:
                rankDictionary[id].append(len(benchDF[benchDF[id + "_intarna_rank"] <= i]))

        # Create merged plots
        colorcycler = cycle(colorList[::-1])
        keys = list(rankDictionary.keys())
        human_sort(keys)

        # Create a subplot
        fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=(14,6))

        # PLOT 1 (ROC)
        for key in keys:
            if key == args.fixedID:
                ax1.plot(rankDictionary[args.fixedID], label=args.fixedID, color="red", zorder=30)
                continue

            ax1.plot(rankDictionary[key], label=key, color=next(colorcycler))

        # set ymax if defined
        if args.ymax != 0:
            ax1.axes.set_ylim(ymax=args.ymax)

        # Create the legend
        ax1.legend(loc="lower right", fontsize=14)
        ax1.axes.set_xlabel("# Target predictions per sRNA", fontsize=16)
        ax1.axes.set_ylabel("# True positive", fontsize=16)

        # Data preparations
        refData = rankDictionary[args.fixedID]
        rankDictionary.pop(args.fixedID, None)

        # Keys without reference key
        keys = list(rankDictionary.keys())
        human_sort(keys)

        # Create dataframe to use seaborn boxplot
        boxDataFrame = pd.DataFrame(index=np.arange(0, sum(thresholds)*len(keys)),columns=["key", "threshold", "value"])

        idx = 0
        # one idx per future row (avoid append function as it copys the dataframe every time a row is appended)
        for key in keys:
            boxData = list(map(operator.sub, rankDictionary[key], refData))
            for threshold in thresholds:
                for value in boxData[:threshold]:
                    boxDataFrame.loc[idx] = [key, threshold, value]
                    idx+=1

        # ensure values are integer
        boxDataFrame["value"] = boxDataFrame["value"].astype(int)

        # Plot the boxplots
        sns.boxplot(x="threshold", y="value", hue="key", data=boxDataFrame, ax=ax2, palette=colorList[::-1][:len(keys)])
        ax2.set_xlabel("thresholds", fontsize="16")
        ax2.set_ylabel("difference measure", fontsize="16")
        ax2.axes.yaxis.set_label_position("right")
        ax2.axes.yaxis.set_ticks_position("right")

        # Remove legend if wanted
        ax2.legend_.remove()

        ax2.axhline(y=0, color="red", linestyle="-", zorder=0)

        # Set the title, if given
        if args.title != "":
            plt.suptitle(args.title, fontsize=20)

        plt.tight_layout(rect=[0, 0.03, 1, 0.95])
        plt.savefig(args.outFile)
        plt.close()

    else:
        sys.exit("No valid benchmark file given!!!")

    # Time and Memory plots
    if (args.info):
        runTimeFile = os.path.splitext(args.benchmarkFile)[0] + "_runTimes.csv"
        memoryFile = os.path.splitext(args.benchmarkFile)[0] + "_MaxMemoryUsage.csv"

        if not os.path.exists(runTimeFile):
            sys.exit("no runTimeFile found!!")
        if not os.path.exists(memoryFile):
            sys.exit("no maxMemoryUsage file found!!")

        timeDF = pd.read_csv(runTimeFile, sep=args.separator, header=0)
        memoryDF = pd.read_csv(memoryFile, sep=args.separator, header=0)

        human_sort(intarnaIDs)

        prefix = ["srna_name", "target_ltag", "target_name"]
        intarnaIDs = [x.replace("_intarna_rank", "") for x in benchDF.columns if x not in prefix]

        # Create a subplot
        fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=(14,6))

        # Time
        timeDict = dict()
        for id in intarnaIDs:
            timeDict[id] = []

        for row in timeDF.iterrows():
            index, data = row
            timeDict[timeDF.get_value(index, "callID")] += data.tolist()[3:]

        keys = list(timeDict.keys())
        human_sort(keys)

        timeData = []
        if args.fixedID != "":
            keys.remove(args.fixedID)
        for key in keys:
            timeData.append(timeDict[key])

        if args.fixedID != "":
            timeData = [timeDict[args.fixedID]] + timeData

        # Convert time from sec to min
        timeData = [[y/60 for y in x] for x in timeData]
        if args.fixedID != "":
            colorList.append("red")

        violin_parts = ax1.violinplot(timeData, showextrema=True, showmeans=True, showmedians=True)
        for idx, pc in enumerate(violin_parts['bodies']):
            pc.set_facecolor(colorList[::-1][idx])
            pc.set_edgecolor('black')
            pc.set_alpha(1)

        for part in ("cbars", "cmins", "cmaxes", "cmeans"):
            vp = violin_parts[part]
            vp.set_edgecolor('black')

        vp = violin_parts["cmedians"]
        vp.set_edgecolor("black")
        vp.set_linestyle("--")

        ax1.axes.set_ylabel("time (in minutes)", fontsize=16)
        ax1.axes.set_xticks([])

        # Memory
        memoryDict = dict()
        for id in intarnaIDs:
            memoryDict[id] = []

        for row in memoryDF.iterrows():
            index, data = row
            memoryDict[memoryDF.get_value(index, "callID")] += data.tolist()[3:]

        keys = list(memoryDict.keys())
        human_sort(keys)

        memoryData = []
        if args.fixedID != "":
            keys.remove(args.fixedID)
        for key in keys:
            memoryData.append(memoryDict[key])
        if args.fixedID != "":
            memoryData = [memoryDict[args.fixedID]] + memoryData

        violin_parts = ax2.violinplot(memoryData, showextrema=True, showmeans=True, showmedians=True)
        for idx, pc in enumerate(violin_parts['bodies']):
            pc.set_facecolor(colorList[::-1][idx])
            pc.set_edgecolor('black')
            pc.set_alpha(1)

        for part in ("cbars", "cmins", "cmaxes", "cmeans"):
            vp = violin_parts[part]
            vp.set_edgecolor('black')

        vp = violin_parts["cmedians"]
        vp.set_edgecolor("black")
        vp.set_linestyle("--")

        ax2.axes.set_ylabel("memory (in megabytes)", fontsize=16)
        ax2.axes.yaxis.set_label_position("right")
        ax2.axes.yaxis.set_ticks_position("right")
        ax2.axes.set_xticks([])

        if args.fixedID != "":
            keys = [args.fixedID] + keys

        set_axis_style(ax1, keys)
        set_axis_style(ax2, keys)

        plt.suptitle("Time and memory consumption", fontsize=20)
        # plt.legend(handles=ax2, labels=keys)
        # print(handles, labels)
        for tick in ax1.get_xticklabels():
            tick.set_rotation(25)
        for tick in ax2.get_xticklabels():
            tick.set_rotation(25)

        plt.tight_layout(rect=[0,0.03,1,0.95])
        plt.savefig(args.outFile.replace(".pdf", "") + "_info.pdf")
        plt.close()

if __name__ == "__main__":
    main(sys.argv[1:])

max_stars_repo_name: Fulanko/IntaRNA-benchmark

max_issues_repo_name: Fulanko/IntaRNA-benchmark

max_forks_repo_name: Fulanko/IntaRNA-benchmark


text:
#-*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
import math
from sklearn.decomposition import PCA

# Compute a sequence's reward
def reward(tsp_sequence):
    tour = np.concatenate((tsp_sequence, np.expand_dims(tsp_sequence[0],0))) # sequence to tour (end=start)
    inter_city_distances = np.sqrt(np.sum(np.square(tour[:-1,:2]-tour[1:,:2]),axis=1)) # tour length
    return np.sum(inter_city_distances) # reward

# Swap city[i] with city[j] in sequence
def swap2opt(tsp_sequence,i,j):
    new_tsp_sequence = np.copy(tsp_sequence)
    new_tsp_sequence[i:j+1] = np.flip(tsp_sequence[i:j+1], axis=0) # flip or swap ?
    return new_tsp_sequence

# One step of 2opt = one double loop and return first improved sequence
def step2opt(tsp_sequence):
    seq_length = tsp_sequence.shape[0]
    distance = reward(tsp_sequence)
    for i in range(1,seq_length-1):
        for j in range(i+1,seq_length):
            new_tsp_sequence = swap2opt(tsp_sequence,i,j)
            new_distance = reward(new_tsp_sequence)
            if new_distance < distance:
                return new_tsp_sequence, new_distance
    return tsp_sequence, distance

class DataGenerator(object):

    def __init__(self):
        pass

    def gen_instance(self, max_length, dimension, seed=0): # Generate random TSP instance
        if seed!=0: np.random.seed(seed)
        sequence = np.random.rand(max_length, dimension) # (max_length) cities with (dimension) coordinates in [0,1]
        pca = PCA(n_components=dimension) # center & rotate coordinates
        sequence = pca.fit_transform(sequence)
        return sequence

    def train_batch(self, batch_size, max_length, dimension): # Generate random batch for training procedure
        input_batch = []
        for _ in range(batch_size):
            input_ = self.gen_instance(max_length, dimension) # Generate random TSP instance
            input_batch.append(input_) # Store batch
        return input_batch

    def test_batch(self, batch_size, max_length, dimension, seed=0, shuffle=False): # Generate random batch for testing procedure
        input_batch = []
        input_ = self.gen_instance(max_length, dimension, seed=seed) # Generate random TSP instance
        for _ in range(batch_size):
            sequence = np.copy(input_)
            if shuffle==True:
                np.random.shuffle(sequence) # Shuffle sequence
            input_batch.append(sequence) # Store batch
        return input_batch

    def loop2opt(self, tsp_sequence, max_iter=2000): # Iterate step2opt max_iter times (2-opt local search)
        best_reward = reward(tsp_sequence)
        new_tsp_sequence = np.copy(tsp_sequence)
        for _ in range(max_iter):
            new_tsp_sequence, new_reward = step2opt(new_tsp_sequence)
            if new_reward < best_reward:
                best_reward = new_reward
            else:
                break
        return new_tsp_sequence, best_reward

    def visualize_2D_trip(self, trip): # Plot tour
        plt.figure(1)
        colors = ['red'] # First city red
        for i in range(len(trip)-1):
            colors.append('blue')

        plt.scatter(trip[:,0], trip[:,1],  color=colors) # Plot cities
        tour=np.array(list(range(len(trip))) + [0]) # Plot tour
        X = trip[tour, 0]
        Y = trip[tour, 1]
        plt.plot(X, Y,"--")

        plt.xlim(-0.75,0.75)
        plt.ylim(-0.75,0.75)
        plt.xlabel('X')
        plt.ylabel('Y')
        plt.show()

    def visualize_sampling(self, permutations): # Heatmap of permutations (x=cities; y=steps)
        max_length = len(permutations[0])
        grid = np.zeros([max_length,max_length]) # initialize heatmap grid to 0

        transposed_permutations = np.transpose(permutations)
        for t, cities_t in enumerate(transposed_permutations): # step t, cities chosen at step t
            city_indices, counts = np.unique(cities_t,return_counts=True,axis=0)
            for u,v in zip(city_indices, counts):
                grid[t][u]+=v # update grid with counts from the batch of permutations

        fig = plt.figure(1) # plot heatmap
        ax = fig.add_subplot(1,1,1)
        ax.set_aspect('equal')
        plt.imshow(grid, interpolation='nearest', cmap='gray')
        plt.colorbar()
        plt.title('Sampled permutations')
        plt.ylabel('Time t')
        plt.xlabel('City i')
        plt.show()

ext: py

max_stars_repo_name: rdjdejong/attention-learn-to-route

max_issues_repo_name: rdjdejong/attention-learn-to-route

max_forks_repo_name: rdjdejong/attention-learn-to-route

reason: import numpy


text:
from numpy import zeros, floor

from NormalScenarios import NormalScenarios
from GraphicalLasso import GraphicalLasso

def GraphLasso(s2, lam):
    # This function estimates the covariance and inverse covariance matrix
    # using the graphical lasso algorithm.
    #  INPUTS
    # s2         :[matrix](n_ x n_) initial covariance matrix
    # lambda     :[scalar] penalty for the glasso algorithm
    #  OPS
    # s2_est     :[matrix](n_ x n_) estimated covariance matrix
    # invs2_est  :[matrix](n_ x n_) inverse of the estimated matrix
    # iter       :[scalar] number of performed iterations
    # avgTol     :[scalar] average tolerance of correlation matrix entries before terminating the algorithm
    # hasError   :[flag] flag indicating whether the algorithm terminated erroneously or not

    ## Code

    # generate j_ x n_ sample with target cov = s2 and mean = 0, in order to
    # use the function created by H. Karshenas, which reads data, instead of distributional parameters
    Model = 'Riccati'
    n_ = len((s2))
    j_ = int(floor(n_*2))
    m = zeros((n_,1))
    epsi, _ = NormalScenarios(m,s2,j_,Model)

    s2_est, invs2_est, iter, hasError = GraphicalLasso(epsi.T, lam)
    return s2_est, invs2_est, iter, hasError

max_stars_repo_name: dpopadic/arpmRes

max_issues_repo_name: dpopadic/arpmRes

max_forks_repo_name: dpopadic/arpmRes

reason: from numpy


text:
import torch
import torch.utils.data
import torchvision.datasets as datasets
import torchvision.transforms as transforms
import torch.utils.data.sampler as Sampler
from lib.Datasets.preprocessing import Preprocessing
import math

import os
import struct
import gzip
import errno
import numpy as np
import xml.etree.ElementTree as ET

class SubsetRandomSamplerWithoutPerm(Sampler.Sampler):

    def __init__(self, indices):
        self.indices = indices

    def __iter__(self):
        return (self.indices[i] for i in range(len(self.indices)))

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

class CIFAR10:
    """
    CIFAR-10 dataset featuring tiny 32x32 color images of
    objects belonging to hundred different classes.
    Dataloader adapted from CIFAR10.

    Parameters:
        args (dict): Dictionary of (command line) arguments.
            Needs to contain batch_size (int) and workers(int).
        is_gpu (bool): True if CUDA is enabled.
            Sets value of pin_memory in DataLoader.

    Attributes:
        normalize (dict): Contains per-channel means and stds of the dataset.
        train_transforms (torchvision.transforms): Composition of transforms
            including conversion to Tensor, horizontal flips, random
            translations of up to 10% in each direction and normalization.
        val_transforms (torchvision.transforms): Composition of transforms
            including conversion to Tensor and normalization.
        trainset (torch.utils.data.TensorDataset): Training set wrapper.
        valset (torch.utils.data.TensorDataset): Validation set wrapper.
        train_loader (torch.utils.data.DataLoader): Training set loader with shuffling
        val_loader (torch.utils.data.DataLoader): Validation set loader.
    """

    def __init__(self, is_gpu, args):
        self.normalize = transforms.Normalize(mean=[0.4914, 0.4822, 0.4465],
                                              std=[0.2023, 0.1994, 0.2010])

        self.train_transforms, self.val_transforms = self.__get_transforms(args.patch_size)

        self.trainset, self.valset = self.get_dataset()
        self.train_loader, self.val_loader = self.get_dataset_loader(args.batch_size, args.workers, is_gpu)

        # Need to define the class dictionary by hand as the default
        # torchvision CIFAR10 data loader does not provide class_to_idx
        self.val_loader.dataset.class_to_idx = {'airplane': 0,
                                                'automobile': 1,
                                                'bird': 2,
                                                'cat': 3,
                                                'deer': 4,
                                                'dog': 5,
                                                'frog': 6,
                                                'horse': 7,
                                                'ship': 8,
                                                'truck': 9}

    def __get_transforms(self, patch_size):
        train_transforms = transforms.Compose([
            transforms.Resize(size=(patch_size, patch_size)),
            transforms.RandomCrop(patch_size, int(math.ceil(patch_size * 0.1))),
            transforms.RandomHorizontalFlip(),
            transforms.ToTensor(),
            self.normalize,
            ])

        val_transforms = transforms.Compose([
            transforms.Resize(size=(patch_size, patch_size)),
            transforms.ToTensor(),
            self.normalize,
            ])

        return train_transforms, val_transforms

    def get_dataset(self):
        """
        Uses torchvision.datasets.CIFAR10 to load dataset.
        Downloads dataset if doesn't exist already.

        Returns:
             torch.utils.data.TensorDataset: trainset, valset
        """

        trainset = datasets.CIFAR10('datasets/CIFAR10/train/', train=True, transform=self.train_transforms,
                                    target_transform=None, download=True)
        valset = datasets.CIFAR10('datasets/CIFAR10/test/', train=False, transform=self.val_transforms,
                                  target_transform=None, download=True)

        return trainset, valset

    def get_dataset_loader(self, batch_size, workers, is_gpu):
        """
        Defines the dataset loader for wrapped dataset

        Parameters:
            batch_size (int): Defines the batch size in data loader
            workers (int): Number of parallel threads to be used by data loader
            is_gpu (bool): True if CUDA is enabled so pin_memory is set to True

        Returns:
             torch.utils.data.TensorDataset: trainset, valset
        """

        train_loader = torch.utils.data.DataLoader(
            self.trainset,
            batch_size=batch_size, shuffle=True,
            num_workers=workers, pin_memory=is_gpu)

        val_loader = torch.utils.data.DataLoader(
            self.valset,
            batch_size=batch_size, shuffle=False,
            num_workers=workers, pin_memory=is_gpu)

        return train_loader, val_loader

class CIFAR100:
    """
    CIFAR-100 dataset featuring tiny 32x32 color images of
    objects belonging to hundred different classes.
    Dataloader adapted from CIFAR10.

    Parameters:
        args (dict): Dictionary of (command line) arguments.
            Needs to contain batch_size (int) and workers(int).
        is_gpu (bool): True if CUDA is enabled.
            Sets value of pin_memory in DataLoader.

    Attributes:
        normalize (dict): Contains per-channel means and stds of the dataset.
        train_transforms (torchvision.transforms): Composition of transforms
            including conversion to Tensor, horizontal flips, random
            translations of up to 10% in each direction and normalization.
        val_transforms (torchvision.transforms): Composition of transforms
            including conversion to Tensor and normalization.
        trainset (torch.utils.data.TensorDataset): Training set wrapper.
        valset (torch.utils.data.TensorDataset): Validation set wrapper.
        train_loader (torch.utils.data.DataLoader): Training set loader with shuffling.
        val_loader (torch.utils.data.DataLoader): Validation set loader.
    """

    def __init__(self, is_gpu, args):
        self.normalize = transforms.Normalize(mean=[0.5071, 0.4866, 0.4409],
                                              std=[0.2009, 0.1984, 0.2023])

        self.train_transforms, self.val_transforms = self.__get_transforms(args.patch_size)

        self.trainset, self.valset = self.get_dataset()
        self.train_loader, self.val_loader = self.get_dataset_loader(args.batch_size, args.workers, is_gpu)

        # Need to define the class dictionary by hand as the default
        # torchvision CIFAR100 data loader does not provide class_to_idx
        self.val_loader.dataset.class_to_idx = {'apples': 0,
                                                'aquariumfish': 1,
                                                'baby': 2,
                                                'bear': 3,
                                                'beaver': 4,
                                                'bed': 5,
                                                'bee': 6,
                                                'beetle': 7,
                                                'bicycle': 8,
                                                'bottles': 9,
                                                'bowls': 10,
                                                'boy': 11,
                                                'bridge': 12,
                                                'bus': 13,
                                                'butterfly': 14,
                                                'camel': 15,
                                                'cans': 16,
                                                'castle': 17,
                                                'caterpillar': 18,
                                                'cattle': 19,
                                                'chair': 20,
                                                'chimpanzee': 21,
                                                'clock': 22,
                                                'cloud': 23,
                                                'cockroach': 24,
                                                'computerkeyboard': 25,
                                                'couch': 26,
                                                'crab': 27,
                                                'crocodile': 28,
                                                'cups': 29,
                                                'dinosaur': 30,
                                                'dolphin': 31,
                                                'elephant': 32,
                                                'flatfish': 33,
                                                'forest': 34,
                                                'fox': 35,
                                                'girl': 36,
                                                'hamster': 37,
                                                'house': 38,
                                                'kangaroo': 39,
                                                'lamp': 40,
                                                'lawnmower': 41,
                                                'leopard': 42,
                                                'lion': 43,
                                                'lizard': 44,
                                                'lobster': 45,
                                                'man': 46,
                                                'maple': 47,
                                                'motorcycle': 48,
                                                'mountain': 49,
                                                'mouse': 50,
                                                'mushrooms': 51,
                                                'oak': 52,
                                                'oranges': 53,
                                                'orchids': 54,
                                                'otter': 55,
                                                'palm': 56,
                                                'pears': 57,
                                                'pickuptruck': 58,
                                                'pine': 59,
                                                'plain': 60,
                                                'plates': 61,
                                                'poppies': 62,
                                                'porcupine': 63,
                                                'possum': 64,
                                                'rabbit': 65,
                                                'raccoon': 66,
                                                'ray': 67,
                                                'road': 68,
                                                'rocket': 69,
                                                'roses': 70,
                                                'sea': 71,
                                                'seal': 72,
                                                'shark': 73,
                                                'shrew': 74,
                                                'skunk': 75,
                                                'skyscraper': 76,
                                                'snail': 77,
                                                'snake': 78,
                                                'spider': 79,
                                                'squirrel': 80,
                                                'streetcar': 81,
                                                'sunflowers': 82,
                                                'sweetpeppers': 83,
                                                'table': 84,
                                                'tank': 85,
                                                'telephone': 86,
                                                'television': 87,
                                                'tiger': 88,
                                                'tractor': 89,
                                                'train': 90,
                                                'trout': 91,
                                                'tulips': 92,
                                                'turtle': 93,
                                                'wardrobe': 94,
                                                'whale': 95,
                                                'willow': 96,
                                                'wolf': 97,
                                                'woman': 98,
                                                'worm': 99}

    def __get_transforms(self, patch_size):
        train_transforms = transforms.Compose([
            transforms.Resize(size=(patch_size, patch_size)),
            transforms.RandomCrop(patch_size, int(math.ceil(patch_size * 0.1))),
            transforms.RandomHorizontalFlip(),
            transforms.ToTensor(),
            self.normalize,
            ])

        val_transforms = transforms.Compose([
            transforms.Resize(size=(patch_size, patch_size)),
            transforms.ToTensor(),
            self.normalize,
            ])

        return train_transforms, val_transforms

    def get_dataset(self):
        """
        Uses torchvision.datasets.CIFAR100 to load dataset.
        Downloads dataset if doesn't exist already.

        Returns:
             torch.utils.data.TensorDataset: trainset, valset
        """

        trainset = datasets.CIFAR100('datasets/CIFAR100/train/', train=True, transform=self.train_transforms,
                                     target_transform=None, download=True)
        valset = datasets.CIFAR100('datasets/CIFAR100/test/', train=False, transform=self.val_transforms,
                                   target_transform=None, download=True)

        return trainset, valset

    def get_dataset_loader(self, batch_size, workers, is_gpu):
        """
        Defines the dataset loader for wrapped dataset

        Parameters:
            batch_size (int): Defines the batch size in data loader
            workers (int): Number of parallel threads to be used by data loader
            is_gpu (bool): True if CUDA is enabled so pin_memory is set to True

        Returns:
             torch.utils.data.TensorDataset: trainset, valset
        """

        # TODO: dirty hack to get things working, class local to the get_dataset_loader to dump the indices to a local variable
        train_loader = torch.utils.data.DataLoader(
            self.trainset,
            batch_size=batch_size, shuffle=True,
            num_workers=workers, pin_memory=is_gpu)

        val_loader = torch.utils.data.DataLoader(
            self.valset,
            batch_size=batch_size, shuffle=False,
            num_workers=workers, pin_memory=is_gpu)
        return train_loader, val_loader

class MNIST:
    """
    MNIST dataset featuring gray-scale 28x28 images of
    hand-written characters belonging to ten different classes.
    Dataset implemented with torchvision.datasets.MNIST.

    Parameters:
        args (dict): Dictionary of (command line) arguments.
            Needs to contain batch_size (int) and workers(int).
        is_gpu (bool): True if CUDA is enabled.
            Sets value of pin_memory in DataLoader.

    Attributes:
        normalize (dict): Contains per-channel means and stds of the dataset.
        train_transforms (torchvision.transforms): Composition of transforms
            including conversion to Tensor, repeating gray-scale image to
            three channel for consistent use with different architectures
            and normalization.
        val_transforms (torchvision.transforms): Composition of transforms
            including conversion to Tensor, repeating gray-scale image to
            three channel for consistent use with different architectures
            and normalization.
        trainset (torch.utils.data.TensorDataset): Training set wrapper.
        valset (torch.utils.data.TensorDataset): Validation set wrapper.
        train_loader (torch.utils.data.DataLoader): Training set loader with shuffling.
        val_loader (torch.utils.data.DataLoader): Validation set loader.
    """

    def __init__(self, is_gpu, args):
        self.normalize = transforms.Normalize(mean=[0.1307, 0.1307, 0.1307],
                                              std=[0.3081, 0.3081, 0.3081])

        self.train_transforms, self.val_transforms = self.__get_transforms(args.patch_size)

        self.trainset, self.valset = self.get_dataset()
        self.train_loader, self.val_loader = self.get_dataset_loader(args.batch_size, args.workers, is_gpu)

        # Need to define the class dictionary by hand as the default
        # torchvision MNIST data loader does not provide class_to_idx
        self.val_loader.dataset.class_to_idx = {'0': 0,
                                                '1': 1,
                                                '2': 2,
                                                '3': 3,
                                                '4': 4,
                                                '5': 5,
                                                '6': 6,
                                                '7': 7,
                                                '8': 8,
                                                '9': 9}

    def __get_transforms(self, patch_size):
        # scale the images (e.g. to 32x32, so the same model
        # as for CIFAR10 can be used for comparison
        # for analogous reasons we also define a lambda transform
        # to duplicate the gray-scale image to 3 channels
        train_transforms = transforms.Compose([
            transforms.Resize(size=(patch_size, patch_size)),
            transforms.ToTensor(),
            transforms.Lambda(lambda x: x.repeat(3, 1, 1)),
            self.normalize,
            ])

        val_transforms = transforms.Compose([
            transforms.Resize(size=(patch_size, patch_size)),
            transforms.ToTensor(),
            transforms.Lambda(lambda x: x.repeat(3, 1, 1)),
            self.normalize,
            ])

        return train_transforms, val_transforms

    def get_dataset(self):
        """
        Uses torchvision.datasets.MNIST to load dataset.
        Downloads dataset if doesn't exist already.

        Returns:
             torch.utils.data.TensorDataset: trainset, valset
        """

        trainset = datasets.MNIST('datasets/MNIST/train/', train=True, transform=self.train_transforms,
                                  target_transform=None, download=True)
        valset = datasets.MNIST('datasets/MNIST/test/', train=False, transform=self.val_transforms,
                                target_transform=None, download=True)

        return trainset, valset

    def get_dataset_loader(self, batch_size, workers, is_gpu):
        """
        Defines the dataset loader for wrapped dataset

        Parameters:
            batch_size (int): Defines the batch size in data loader
            workers (int): Number of parallel threads to be used by data loader
            is_gpu (bool): True if CUDA is enabled so pin_memory is set to True

        Returns:
             torch.utils.data.TensorDataset: trainset, valset
        """

        # TODO: dirty hack to get things working, class local to the get_dataset_loader to dump the indices to a local variable

        train_loader = torch.utils.data.DataLoader(
            self.trainset,
            batch_size=batch_size, shuffle=True,
            num_workers=workers, pin_memory=is_gpu)

        val_loader = torch.utils.data.DataLoader(
            self.valset,
            batch_size=batch_size, shuffle=False,
            num_workers=workers, pin_memory=is_gpu)
        return train_loader, val_loader


text:
import pandas as pd
import numpy as np
from pyquantfinance.metrics import drawdown, max_drawdown, skewness, kurtosis, is_normal

def test_drawdown():
    # test 100% percent returns
    drawdown_test1 = drawdown([1, 1, 1, 1, 1])
    assert drawdown_test1["Wealth"].equals(pd.Series([2, 4, 8, 16, 32]))
    assert drawdown_test1["Previous Peak"].equals(pd.Series([2, 4, 8, 16, 32]))
    assert drawdown_test1["Drawdown"].equals(
        pd.Series([0.0, 0.0, 0.0, 0.0, 0.0]))

    # test all zero returns
    drawdown_test2 = drawdown([0, 0, 0, 0, 0])
    assert drawdown_test2["Wealth"].equals(pd.Series([1, 1, 1, 1, 1]))
    assert drawdown_test2["Previous Peak"].equals(pd.Series([1, 1, 1, 1, 1]))
    assert drawdown_test2["Drawdown"].equals(
        pd.Series([0.0, 0.0, 0.0, 0.0, 0.0]))

    # test random returns, use numpy allclose to test is if within reasonable
    # error
    drawdown_test2 = drawdown([0.43, -0.12, 1.0, 0.720, -.3])
    assert np.allclose(
        drawdown_test2["Wealth"], [
            1.43, 1.2584, 2.5168, 4.328896, 3.0302272])
    assert np.allclose(
        drawdown_test2["Previous Peak"], [
            1.43, 1.43, 2.5168, 4.328896, 4.328896])
    assert np.allclose(
        drawdown_test2["Drawdown"], [
            0.0, -0.12, 0.0, 0.0, -0.3])


text:
from math import ceil

import re
from keras_preprocessing.text import text_to_word_sequence
from os import path

import argparse
from keras.utils import to_categorical

import numpy as np
import logging
from scipy import sparse

from models import get_model
from preprocessing.fasttext import FastText
from preprocessing.reader import SemEvalDatasetReader, EvalitaDatasetReader, EvalitaPreprocDatasetReader
from utils import get_model_memory_usage
from utils.callbacks import EvalCallback, ValidationEarlyStopping

logging.getLogger().setLevel(logging.INFO)

class FileProvider:
    def __init__(self, workdir):
        self.model = path.join(workdir, 'model.h5')
        self.model_json = path.join(workdir, 'model.json')
        self.logs = path.join(workdir, 'logs')
        self.input_dir = path.join(workdir, 'input')
        self.semeval_train = path.join(self.input_dir, 'semeval_train')
        self.semeval_test = path.join(self.input_dir, 'semeval_test')
        self.evalita = path.join(self.input_dir, 'evalita_train.json')
        self.evalita_resolved_train = path.join(self.input_dir, 'evalita_resolved_train.json')
        self.evalita_train = path.join(self.input_dir, 'evalita_split_train.json')
        self.evalita_test = path.join(self.input_dir, 'evalita_split_test.json')

if __name__ == '__main__':
    """##### Parameter parsing"""

    parser = argparse.ArgumentParser(description='Train the emoji task')
    parser.add_argument('--embeddings', default=None,
                        help='The directory with the precomputed embeddings')
    parser.add_argument('--workdir', required=True,
                        help='Work path')
    parser.add_argument('--evalita', default=False, action='store_true', help='Train and test on EVALITA2018 dataset')
    parser.add_argument('--semeval', default=False, action='store_true', help='Train and test on SemEval2018 dataset')
    parser.add_argument('--batch-size', type=int, default=256,
                        help='The size of a mini-batch')
    parser.add_argument('--max-epoch', type=int, default=20,
                        help='The maximum epoch number')
    parser.add_argument('--max-seq-length', type=int, default=40,
                        help='Maximum sequence length')
    parser.add_argument('--max-char-length', type=int, default=5000,
                        help='Maximum character length')

    args = parser.parse_args()
    files = FileProvider(args.workdir)
    logging.info("Starting training with parameters: {0}".format(vars(args)))

    """##### Loading the dataset"""

    if args.semeval:
        raw_train = SemEvalDatasetReader(files.semeval_train)
        raw_test = SemEvalDatasetReader(files.semeval_test)
    else:
        raw_train, raw_test = EvalitaDatasetReader(files.evalita).split()
    raw_train, raw_val = raw_train.split(test_size=0.1)

    """##### Initializing embeddings"""
    logging.info("Initializing embeddings")

    fasttext = FastText(args.embeddings)
    embedding_size = fasttext.size
    embedding_matrix = fasttext.embeddings
    logging.info("Restored %d embeddings" % (fasttext.num_original_vectors))

    #def text_to_word_sequence(text):
    #    return re.compile("\s+").split(text.lower().strip())

    max_seq_length = 0
    max_char_length = 0
    max_perm_char_length = 0
    for text in raw_train.X:
        words = text_to_word_sequence(text)
        ngrams = list()
        for word in words:
            ngrams += fasttext.compute_ngrams(word)
        if len(words) > max_seq_length:
            max_seq_length = len(words)
        if len(ngrams) > max_char_length:
            max_char_length = len(ngrams)
        if len(ngrams) > max_perm_char_length and len(words) <= args.max_seq_length:
            max_perm_char_length = len(ngrams)
    logging.info("Max lengths in training set. seq: %d char: %d" % (max_seq_length, max_char_length))
    max_seq_length = min(max_seq_length, args.max_seq_length)
    max_char_length = min(max_char_length, args.max_char_length)

    def convert_input(raw_input):
        output = []
        output_mask = []
        counter = 0
        for text in raw_input.X:
            inp, mask = fasttext.transform_input(text_to_word_sequence(text), max_chars=max_char_length, max_words=max_seq_length)
            output.append(inp)
            output_mask.append(mask)
            counter += 1
            if counter % 10000 == 0:
                print(text, len(text))
                print(inp[:50], inp.shape)
                print("1st row", mask[0,:50], mask.shape)
                print("2nd row", mask[1,:50], mask.shape)
                print("  Converting input %d / %d" % (counter, len(raw_input.X)))
        output = np.stack(output)
        output_mask = np.stack(output_mask)
        print("Input shapes: %s %s (type: %s, size: %.1fGB)" %
              (str(output.shape), str(output_mask.shape), str(output_mask.dtype), float(output_mask.nbytes) / (1024 ** 3)))
        return output, output_mask, raw_input.Y

    X_train, X_train_mask, Y_train = convert_input(raw_train)
    X_val, X_val_mask, Y_val = convert_input(raw_val)
    X_test, X_test_mask, Y_test = convert_input(raw_test)

    Y_dictionary = raw_train.Y_dictionary
    Y_class_weights = len(Y_train) / np.power(np.bincount(Y_train), 1.1)
    Y_class_weights *= 1.0 / np.min(Y_class_weights)
    logging.info("Class weights: %s" % str(Y_class_weights))

    del raw_train
    del raw_val
    del raw_test

    # Rescaling embeddings
    means = np.mean(embedding_matrix, axis=0)
    variance = np.sqrt(np.mean((embedding_matrix - means) ** 2, axis=0))
    embedding_matrix = (embedding_matrix - means) / variance

    """##### Model definition"""
    logging.info("Initializing model")

    params = {
        "vocabulary_size": fasttext.num_original_vectors,
        "embedding_size": embedding_size,
        "max_seq_length": max_seq_length,
        "max_char_length": max_char_length,
        "embedding_matrix": embedding_matrix,
        "y_dictionary": Y_dictionary
    }
    model = get_model("base_lstm_subword").apply(params)

    """##### Load model"""

    # needs also storing&restoring of the current epoch, also not sure Adam weights are preserved
    #if path.exists(files.model):
    #    model.load_weights(files.model)

    """##### Continue with model"""

    print(model.summary())

    Y_train_one_hot = to_categorical(Y_train, num_classes=len(Y_dictionary))

    callbacks = {
        "test": EvalCallback("test", [X_test, X_test_mask], Y_test),
        #"train": EvalCallback("train", [X_train, X_train_mask], Y_train, period=3),
        "val": EvalCallback("validation", [X_val, X_val_mask], Y_val)
    }
    callbacks["stop"] = ValidationEarlyStopping(monitor=callbacks["val"])

    def generator():
        while True:
            pointer = 0
            batch_size = args.batch_size
            while pointer+batch_size < X_train.shape[0]:
                yield ({'main_input': X_train[pointer:pointer+batch_size], 'mask_input': X_train_mask[pointer:pointer+batch_size]}, Y_train_one_hot[pointer:pointer+batch_size])
                pointer += batch_size
            yield ({'main_input': X_train[pointer:], 'mask_input': X_train_mask[pointer:]}, Y_train_one_hot[pointer:])

    usage, shapes, trainable, non_trainable = get_model_memory_usage(args.batch_size, model)
    print("Model memory usage: %2fGB (%2f, %2f, %2f)" % (usage, shapes, trainable, non_trainable))

    model.fit_generator(generator(),
        steps_per_epoch=ceil(X_train.shape[0] / args.batch_size),
        class_weight=Y_class_weights,
        epochs=args.max_epoch,
        shuffle=True,
        callbacks=[callback for callback in callbacks.values()])

    logging.info("Saving model to json")

    model_json = model.to_json()
    with open(files.model_json, "w", encoding="utf-8") as json_file:
        json_file.write(model_json)

    logging.info("Saving model weights")

    model.save_weights(files.model)

    logging.info("Evaluating")

    callbacks["test"].evaluate()

reason: import numpy,from scipy


text:
import numpy as np
import pickle
import os

PROJECT_ROOT_DIR = "../../results"
DATA_ID = "../../results/data"
FIGURE_ID = "../../results/figures"

if not os.path.exists(PROJECT_ROOT_DIR):
    os.mkdir(PROJECT_ROOT_DIR)
if not os.path.exists(FIGURE_ID):
    os.makedirs(FIGURE_ID)
if not os.path.exists(DATA_ID):
    os.makedirs(DATA_ID)

def image_path(fig_id):
    return os.path.join(FIGURE_ID, fig_id)

def data_path(data_id):
    return os.path.join(DATA_ID, data_id)

def identity(func):
    return func

def saver(object, filename):
    pickle.dump(object, open(filename, "wb"))

def loader(filename):
    object = pickle.load(open(filename, "rb"))

    return object

def unpack_list(list_):
    list_flat = []
    for l in list_:
        list_flat.append(l.flatten())

    list_flat = np.concatenate(list_flat).reshape(-1, 1)

    return list_flat

def gaussian(x, mean, var):
    if type(mean) == float:
        mean = np.array([[mean]])

    if type(var) == float:
        var = np.array([[var]])

    var_inv = np.linalg.inv(var)
    diag = np.diag((x - mean) @ var_inv @ (x - mean).T).reshape(-1, 1)
    y = np.exp(-0.5 * diag)

    return y

def random_mixed_gaussian(x, n_gaussians=3):
    dim = x.shape[-1]
    mean = np.random.uniform(0, 1, (n_gaussians, 1, dim))

    var = []
    for i in range(n_gaussians):
        var_ = np.random.uniform(-0.001, 0.001, (dim, dim))
        var_[np.diag_indices(dim)] = np.random.uniform(0.005, 0.05, dim)
        var.append(var_)

    alpha = np.random.uniform(-1, 1, n_gaussians)

    y = 0
    for i in range(n_gaussians):
        y += alpha[i] * gaussian(x, mean[i], var[i])

    return y

def scaler(x, mode="uniform", a=0, b=np.pi):
    if mode == "uniform":
        x = x - np.min(x, axis=0)
        x = (b - a) * x / np.max(x, axis=0)
        x = x + a
    if mode == "standard":
        x = (x - np.mean(x, axis=0)) / np.std(x, axis=0)

    return x

def generate_meshgrid(x):

    x = np.meshgrid(*x)
    x = [np.ravel(x_).reshape(-1, 1) for x_ in x]
    x = np.hstack(x)

    return x

def r2(models, x, y):
    y_avg = np.mean(y)

    if type(models) != list:
        models = [models]

    r2_scores = []
    for model in models:
        y_pred = model.predict(x)
        r2 = 1 - np.mean((y_pred - y)**2) / np.mean((y - y_avg)**2)
        r2_scores.append(r2)

    return sum(r2_scores) / len(r2_scores)

max_stars_repo_name: KristianWold/qcnpy

max_issues_repo_name: KristianWold/qcnpy

max_forks_repo_name: KristianWold/qcnpy


text:
# !/usr/bin/env python3

# Copyright (c) Facebook, Inc. and its affiliates.
# All rights reserved.

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

import numpy as np
import torch
import torch.nn as nn

from ss_baselines.common.utils import Flatten
from ss_baselines.av_nav.models.visual_cnn import conv_output_dim, layer_init

class MapCNN(nn.Module):
    r"""A Simple CNN for processing map inputs (acoustic map or geometric map)

    Args:
        observation_space: The observation_space of the agent
        output_size: The size of the embedding vector
    """

    def __init__(self, observation_space, output_size, map_type='gm'):
        super().__init__()
        self._map_type = map_type
        self._n_input_gm = observation_space.spaces[map_type].shape[2]

        cnn_dims = np.array(
            observation_space.spaces[map_type].shape[:2], dtype=np.float32
        )
        # input image of dimension N reduces to (ceil((N-f+1)/s),ceil((N-f+1)/s),Number of filters)
        # where f is the filter size and s is the stride length
        # kernel size for different CNN layers
        if self._map_type == 'gm':
            if cnn_dims[0] == 200:
                self._cnn_layers_kernel_size = [(8, 8), (4, 4), (3, 3)]

                # strides for different CNN layers
                self._cnn_layers_stride = [(4, 4), (2, 2), (1, 1)]
            else:
                assert cnn_dims[0] == 400
                self._cnn_layers_kernel_size = [(8, 8), (4, 4), (3, 3)]

                # strides for different CNN layers
                self._cnn_layers_stride = [(5, 5), (4, 4), (2, 2)]
        elif self._map_type == 'am':
            self._cnn_layers_kernel_size = [(5, 5), (3, 3), (3, 3)]

            # strides for different CNN layers
            self._cnn_layers_stride = [(2, 2), (1, 1), (1, 1)]

        for kernel_size, stride in zip(
            self._cnn_layers_kernel_size, self._cnn_layers_stride
        ):
            cnn_dims = conv_output_dim(
                dimension=cnn_dims,
                padding=np.array([0, 0], dtype=np.float32),
                dilation=np.array([1, 1], dtype=np.float32),
                kernel_size=np.array(kernel_size, dtype=np.float32),
                stride=np.array(stride, dtype=np.float32),
            )

        self.cnn = nn.Sequential(
            nn.Conv2d(
                in_channels=self._n_input_gm,
                out_channels=32,
                kernel_size=self._cnn_layers_kernel_size[0],
                stride=self._cnn_layers_stride[0],
            ),
            nn.ReLU(True),
            nn.Conv2d(
                in_channels=32,
                out_channels=64,
                kernel_size=self._cnn_layers_kernel_size[1],
                stride=self._cnn_layers_stride[1],
            ),
            nn.ReLU(True),
            nn.Conv2d(
                in_channels=64,
                out_channels=32,
                kernel_size=self._cnn_layers_kernel_size[2],
                stride=self._cnn_layers_stride[2],
            ),
            #  nn.ReLU(True),
            Flatten(),
            nn.Linear(32 * cnn_dims[0] * cnn_dims[1], output_size),
            nn.ReLU(True),
        )

        layer_init(self.cnn)

    def forward(self, observations):
        cnn_input = []

        gm_observations = observations[self._map_type]
        # permute tensor to dimension [BATCH x CHANNEL x HEIGHT X WIDTH]
        gm_observations = gm_observations.permute(0, 3, 1, 2)
        cnn_input.append(gm_observations)

        cnn_input = torch.cat(cnn_input, dim=1)

        return self.cnn(cnn_input)

max_stars_repo_name: tynguyen/sound-spaces

max_issues_repo_name: tynguyen/sound-spaces

max_forks_repo_name: tynguyen/sound-spaces


text:
from motor import select_event
import numpy

def test_select_event_none():
    propensity_event_type = numpy.array([0, 0, 0, 0])
    rand_num = numpy.random.rand(1)
    event_type = select_event(propensity_event_type, rand_num)
    expected_event_type = None
    assert event_type == expected_event_type

def test_select_event_attach():
    propensity_event_type = numpy.array([1, 0, 0, 0])
    rand_num = numpy.random.rand(1)
    event_type = select_event(propensity_event_type, rand_num)
    expected_event_type = "attach"
    assert event_type == expected_event_type

def test_select_event_detach():
    propensity_event_type = numpy.array([0, 1, 0, 0])
    rand_num = numpy.random.rand(1)
    event_type = select_event(propensity_event_type, rand_num)
    expected_event_type = "detach"
    assert event_type == expected_event_type

def test_select_event_detach_end():
    propensity_event_type = numpy.array([0, 0, 1, 0])
    rand_num = numpy.random.rand(1)
    event_type = select_event(propensity_event_type, rand_num)
    expected_event_type = "detach_end"
    assert event_type == expected_event_type

def test_select_event_forward_hop():
    propensity_event_type = numpy.array([0, 0, 0, 1])
    rand_num = numpy.random.rand(1)
    event_type = select_event(propensity_event_type, rand_num)
    expected_event_type = "forward_hop"
    assert event_type == expected_event_type

max_stars_repo_name: mwakok/Molecular-Motor-Dynamics

max_issues_repo_name: mwakok/Molecular-Motor-Dynamics

max_forks_repo_name: mwakok/Molecular-Motor-Dynamics


text:
import numpy as np
import numpy.linalg as linalg
import sys, time, random

import shared as SH
import auxilary as aux
import graph as graph
import plot as myplt
import complement as cmpl #Actually use this
import cpfunctions as cp
import rdfunctions as rd

from copy import copy, deepcopy

#The main method
def CreateRoad(travaxis, critical, parentvector ,originlist, ellipselist, debug = False):
	#Start of initialize
	errorout = open("debuginfo.txt", "w")
	print originlist, ellipselist
	statelist, firstlink, slicevector, startpt = rd.initialize(travaxis, parentvector, originlist, ellipselist, False)
	returnvec, pastvector, presentvector = rd.initRecursion()

	if debug == True:
		print "Originlist = ",  originlist
		print "ellipselist = ",  ellipselist
		print "num = ",  SH.num
		print "dim = ",  SH.dim
		print "slicevector = ",  slicevector
		print "adjcoordinates = ",  SH.adjcoordinates

	#CPcalculate - Calculates the Critical Points, for all but the primA ellipse
	#The returned list - Criticalpts, is a list of length n-1, where n is the ellipses being considered for this slice
	#Create separate lists for the travaxis and other axes, both of length n-1
	Criticalpts = cp.CPcalculate(travaxis, originlist, ellipselist, False)
	CriticalX, 	CriticalYZ = cp.axisrange(Criticalpts, False	)

	#Begin the iteration
	for iteration in range(SH.iterate):
		presentvector = []
		nextslice = rd.getNextSlice(startpt, iteration)		#nextslice is a value

		if iteration == 0 or iteration == SH.iterate -1 :
			## ## All this goes to a different function

			if debug == True: print "in Slice "+ str(iteration) + " slicevector == ", slicevector
			slicevector[travaxis] = nextslice 			#No need for the old information
			if debug == True: print "in Slice 0, slicevector == ", slicevector
			returnvec, presentvector = rd.LastorFirstSlice(returnvec, presentvector, slicevector)
			if iteration == SH.iterate -1 :
				pastvector = aux.complete_link(pastvector, presentvector, False)
			else:
				pastvector = presentvector[:]

			continue

		#Considerlist - holds the list of the ellipses that need to be considered for the intersect function
		#The returned list is of dimension n-1. If the vaule is 1, the ellipse is to be considered, else not
		considerlist = cp.EllSliceintersect(CriticalX, nextslice)

		#The returned list is of dimension n-1.
		considerYZ = cp.EllUnderConsider(considerlist , CriticalYZ, CriticalX,  nextslice, travaxis, False)

		slicevector[travaxis] = nextslice 			#No need for the old slice information?
				#Technically if there were any CPs they should have been dealt with by this stage
#############################################
		#The Intersect and creation of presentvector
#############################################

		#first for the first ellipse
		for axis2 in range(1,2):# (travaxis+1, SH.dim):
			solution, valid = aux.intersect(ellipselist[0], originlist[0], slicevector[(travaxis):], axis2)
			#print >> errorout, "Sent == ", ellipselist[0], originlist[0], slicevector[(travaxis):], axis2
			vector = slicevector[:]

			if valid ==1 :		#Later check also that it is not inside any of the other ellipsoids
				if solution[0] == solution[1] and firstlink!= 1:
					print "Error, this is not supposed to happen, both the solutions are equal. But letting it be for Now. iteration == ", iteration
					print solution, slicevector
					#sys.exit(0)
				for sol in solution:
					print >> errorout, "For axis == ", axis2, " for vector == ",vector, " found solution == ", sol

					vector[travaxis + 1] = sol

					VectorNum = rd.AddToVertices(vector[:])
					presentvector.append( VectorNum )
					#returnvec[0].append( VectorNum )
				#Some processing needs to be done

		for index, listitem in enumerate(considerlist):
			if listitem == 0 :
				continue
			#else: Now the rest of the code for this loop

			# We have to find the correct ellipse and remember to account for the first ellipse being primA
			if debug == True :print "Working for obstacle, index no. ", index
			ellipse = ellipselist[index + 1]
			origin = originlist[index + 1]

			# also we have to extract the correct vector using the slice vector and the list in consider YZ
			vector = slicevector[:]

			#The rest of slice vector and the vector from considerYZ shud have same len, check this
			if not len(considerYZ[index]) == SH.dim - (travaxis + 1):
				print "Check2, Error! This is not supposed to happen, both the matrix should have equal dimensions."
				sys.exit(0)

			for i in range(SH.dim - (travaxis + 1) ):
				vector[	(travaxis + 1) + i] = considerYZ[index][i]
			if debug == True :print "For the slicevector ", slicevector, "gotten the following vector to run intersect along, ", vector

			#Run Intersect
			for axis2 in range(1,2):# (travaxis+1, SH.dim):
				solution, valid = aux.intersect(ellipse, origin, vector[(travaxis):], axis2, False)
				if valid ==1 :
					if solution[0] == solution[1]:
						print "Check2, Error! This is not supposed to happen, both the solutions are equal."
						sys.exit(0)
					for sol in solution:
						vector[travaxis + 1] = sol
						VectorNum = rd.AddToVertices(vector)
						presentvector.append( VectorNum )
						#returnvec[0].append( VectorNum )
					if debug == True :print "got solution == ", solution
###########################################
#Intersect Done

		#Last Step of this iteration
		pastvector = aux.link(pastvector, presentvector, [], False)

		#Now Begins Work of Critical points
		nextslice = rd.getNextSlice(startpt, iteration + 1)		#nextslice is a value of next iteration
		#ActiveCP and restCP have the value for any critcal points found at this stage. The active is along the travaxis and restCP is for recursion
		activeCP, restCP = cp.RecursionPoints(CriticalX, CriticalYZ, slicevector[travaxis], nextslice, False)
		#To find which critical points from higher level have to be attached at this stage
		CritAtThisSlice = cp.RecurCheck (critical, slicevector[travaxis], nextslice, False)

		if len(activeCP) + len(CritAtThisSlice)> 1:
			print "System Limitation. We are just working with one critical point between two slices presently. A solution could be to increase the iterate variable, so slices come closer"
			print "It could be be the critical points from higher dimension and this one are crowding together."
			sys.exit(0)
		if len(activeCP) + len(CritAtThisSlice)== 0: continue

		#We work on the CritAtThisSlice one first
		# if len(CritAtThisSlice) > 0:						#If count is greater than 0, first recursion worked
		# 	print activeCP, restCP, CritAtThisSlice, slicevector
		# 	raw_input("reached here")

		if (SH.dim - travaxis) == 2: 						#Reached the 2D case - Base Case
			CPvector = slicevector[:]						#CPvector will be manipulated to represent the critical val

			# print "CritAtThisSlice at slicevector == ", CritAtThisSlice, slicevector, activeCP
			try:
				point = CritAtThisSlice[0][0]					#System Limitation, only account for one critical point
			except IndexError:
				point = [activeCP[0][0]] + restCP[0][0]
			for index, term in enumerate(point):
				CPvector[travaxis + index] = term			#CPvector will be added to the Graph after this
			VectorNum = rd.AddToVertices(CPvector)
			try:
				startendflag = CritAtThisSlice[0][1]
			except IndexError:
				startendflag = restCP[0][1]
			if startendflag == "start": returnvec[1].append(VectorNum)
			elif startendflag == "end": returnvec[2].append(VectorNum)
			else:
				print "There is an error in the travaxis == ", travaxis, "The Criticalpt here does not have appended text (start/end)"
				sys.exit(0)

			aux.link(pastvector, [VectorNum], [], False)	#Add this VectorNum to the network
			pastvector.append(VectorNum)					#Added to pastvectors as will be needed in next slice
			continue										#Done for this iteration

		else:												#Not 2D. We have to prepare to call the lower dimension
			nextslice = activeCP[0][0]						#The travaxis value for the recursion slice
			considerlist = cp.EllSliceintersect(CriticalX, nextslice)
			considerYZ = cp.EllUnderConsider(considerlist , CriticalYZ, CriticalX,  nextslice, travaxis, False)
			slicevector[travaxis] = nextslice

			#Get the Ellipselist and the originlist for the lower dimensions
			RecursionEll, RecursionOri = cp.ReduceEllipsoids(considerlist, considerYZ, slicevector, travaxis, deepcopy(ellipselist), deepcopy(originlist), False)

			#obtainedvec is the otherside of returnvec
			obtainedvec = CreateRoad(travaxis+1, restCP, slicevector ,RecursionOri, RecursionEll, debug = False)

			presentvector = obtainedvec[0][:]
			presentvector.extend(obtainedvec[2][:])		#For Doubts regarding the obtainedvec/returnvec refer to README.md
			aux.endRecur_link(pastvector, presentvector, False)

			pastvector = obtainedvec[0][:]
			pastvector.extend(obtainedvec[1][:])		#Preparing the real pastvector for the next slice.

	for edgeindex, edge in enumerate( SH.adjmatrix) :
		print >> errorout, edgeindex, "edge == ", edge
	for coorindex, coor in enumerate( SH.adjcoordinates) :
		print >> errorout, coorindex, "coor == ", coor
	return returnvec

max_stars_repo_name: rohun-tripati/UGP-Robotics

max_issues_repo_name: rohun-tripati/UGP-Robotics

max_forks_repo_name: rohun-tripati/UGP-Robotics


text:
import numpy as np
from numpy.linalg import norm
from numba import jit

def main():
    vertices = [np.array([0.0, 0.0]),
                np.array([0.0, 10.0]),
                np.array([10.0, 10.0]),
                np.array([10.0, 0.0]),
                np.array([0.0, 0.0])]
    p = np.array([5, 5])

    tot = 0.0
    for i in range(len(vertices)-1):
        v = vertices[i]
        v_next = vertices[i+1]
        v_p = v-p
        v_next_p = v_next-p
        dot = np.dot(v_p, v_next_p)
        cross = np.cross(v_p, v_next_p)
        cos_theta = dot/(norm(v_p)*norm(v_next_p))
        theta = np.arccos(cos_theta)
        sgn = cross/abs(cross)
        tot += sgn*theta
    deg = tot*180/np.pi
    print('tot=', deg)

if __name__ == '__main__':
    main()

max_stars_repo_name: terasakisatoshi/pythonCodes

max_issues_repo_name: terasakisatoshi/pythonCodes

max_forks_repo_name: terasakisatoshi/pythonCodes

reason: import numpy,from numpy,from numba


text:
import sys, os
sys.path.append("..")
import helper as h
from constants import SINC
import numpy as np
import matplotlib.pyplot as plt

eigengrid = h.r_window_to_matrix_eig(SINC) # h.window_pad_to_box_rfft(SINC,pad_factor=10.0)
eigengrid_hann = h.r_window_to_matrix_eig(SINC * np.hanning(len(SINC)))
eigengrid_hamm = h.r_window_to_matrix_eig(SINC * np.hamming(len(SINC)))

plt.imshow(abs(eigengrid),aspect="auto")
plt.show(block=True)

plt.subplots(figsize=(10,5))
plt.subplot(121)
plt.semilogy(np.mean(1/abs(eigengrid**2),axis=1),".",label="SINC")
plt.semilogy(np.mean(1/abs(eigengrid_hann**2),axis=1),".",alpha=0.4,label="sinc hanning")
plt.semilogy(np.mean(1/abs(eigengrid_hamm**2),axis=1),".",alpha=0.4,label="sinc hamming")
plt.title("All terms Log Scale")
plt.ylabel("Log R[n]")
plt.xlabel("n")
plt.grid(which="both")
plt.legend()

plt.subplot(122)
plt.plot(np.mean(1/abs(eigengrid[:1000]**2),axis=1),".",label="sinc")
plt.plot(np.mean(1/abs(eigengrid_hann[:1000]**2),axis=1),".",alpha=0.4,label="sinc hanning")
plt.plot(np.mean(1/abs(eigengrid_hamm[:1000]**2),axis=1),".",alpha=0.4,label="sinc hamming")
plt.title("First few terms")
plt.ylabel("R[n]")
plt.xlabel("n")
plt.legend()
plt.suptitle("Quantization Error Increase from Inverse PFB",fontsize=18)
plt.tight_layout()
plt.show(block=True)

max_stars_repo_name: dcxSt/pfb-mod

max_issues_repo_name: dcxSt/pfb-mod

max_forks_repo_name: dcxSt/pfb-mod


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

# In[128]:

import re
import matplotlib.pyplot as plt
import pylab as pl
import numpy as np
import math
import os
from collections import defaultdict
from matplotlib.patches import Rectangle
from matplotlib.patches import Rectangle
import numpy as np
import pylab as P

# In[140]:

def compute_ripley(path):
    regex = re.compile(r"(\d+):\s+(\d+\.\d+):(\d+)")
    collapsed_count_lst_dct = defaultdict(list)
    random_count_lst_dct = defaultdict(list)

    ripleys = [ripley for ripley in os.listdir(path) if 'collapsed' not in ripley]

    for ripley in ripleys:
        with open(os.path.join(path, ripley)) as f:
            print(ripley)
            for line in f:
                line = line.rstrip('\n')
                r = re.search(regex, line)
                if r is not None:
                    random_count_lst_dct[r.group(1)].append(math.log10(max(int(r.group(3)),1)))
                else:
                    print(line)
    with open(os.path.join(path, 'ripley_collapsed.txt')) as f:
        for line in f:
            line = line.rstrip('\n')
            r = re.search(regex, line)
            if r is not None:
                collapsed_count_lst_dct[r.group(1)].append(math.log10(max(int(r.group(3)),1)))
    #print(random_count_lst_dct)
    random_count_arr = []
    for k in random_count_lst_dct.keys():
        random_count_arr.append(random_count_lst_dct[k])
    random_count_arr = np.asarray(random_count_arr)
    print(random_count_arr.shape)
    collapsed_count_arr = []
    for k in collapsed_count_lst_dct.keys():
        collapsed_count_arr.append(collapsed_count_lst_dct[k])
    collapsed_count_arr = np.asarray(collapsed_count_arr)
    print(collapsed_count_arr.shape)
    k = np.mean(collapsed_count_arr, axis=0)
    low = np.percentile(random_count_arr, 2.5, axis=0)
    high = np.percentile(random_count_arr, 47.5, axis=0)
    return k, low, high

# In[165]:

d_lst = list(pl.frange(0.1,0.5,0.01))
k_pggan_ffhq, l_pggan_ffhq, h_pggan_ffhq = compute_ripley('legacy/pggan/monte_carlo_sampling_10m_ffhq/ripley')
k_pggan_celebahq, l_pggan_celebahq, h_pggan_celebahq = compute_ripley('legacy/pggan/monte_carlo_sampling_10m_celebahq/ripley')
k_sgan_celebahq, l_sgan_celebahq, h_sgan_celebahq = compute_ripley('legacy/sgan/monte_carlo_sampling_10m_celebahq/ripley')
k_sgan_ffhq, l_sgan_ffhq, h_sgan_ffhq = compute_ripley('legacy/sgan/monte_carlo_sampling_10m_ffhq/ripley')

# In[211]:

def plot_ripley(k,l,h,fig_name,shaded=True):
    d_lst = list(pl.frange(0.1,0.5,0.01))
    k = P.plot(d_lst, k, color='r', marker='o', linestyle='dashed', linewidth=0.5, markersize=0.5, label=r'$\mathcal{N}_{R_{obs}}$')[0]
    if shaded:
        P.fill_between(d_lst, l, h, color='b', alpha=0.5, label="filled_region")
        rect = Rectangle((0, 0), 1, 1, fc="b", alpha=0.5)
        P.legend([k, rect], [r'$\mathcal{N}_{R_{obs}}$', r'$\mathcal{N}_{R_{ref}}$'], loc='center right', shadow=True, facecolor='white', framealpha=1)
    else:
        line1 = P.plot(d_lst, l, color='b', marker='^', linestyle='dashed', linewidth=0.5, markersize=0.5, label=r'$L_{\frac{\alpha}{2}}(\mathcal{N}_{R_{ref}})$')[0]
        line2 = P.plot(d_lst, h, color='b', marker='v', linestyle='dashed', linewidth=0.5, markersize=0.5, label=r'$H_{1-\frac{\alpha}{2}}(\mathcal{N}_{R_{ref}})$')[0]
        plt.legend(loc='center right', shadow=True, facecolor='white', framealpha=1)
    #P.yticks([])
    P.yticks(np.arange(0, 7, 0.5))
    P.xlabel(r'$d$')
    P.ylabel(r'$log_{10}(N(d))$')
    #P.title("StyleGAN-CelebAHQ-1024")
    P.savefig(fig_name, dpi=800, bbox_inches='tight', pad_inches=0)
    #P.show()

# In[212]:

plot_ripley(k=k_sgan_celebahq, l=l_sgan_celebahq, h=h_sgan_celebahq, fig_name='SGAN_CelebAHQ_1024.pdf', shaded=True)

# In[213]:

plot_ripley(k=k_sgan_ffhq, l=l_sgan_ffhq, h=h_sgan_ffhq, fig_name='SGAN_FFHQ_1024.pdf', shaded=True)

# In[168]:

plot_ripley(k=k_pggan_celebahq, l=l_pggan_celebahq, h=h_pggan_celebahq, fig_name='PGGAN_CelebAHQ_1024.pdf', shaded=True)

# In[214]:

plot_ripley(k=k_pggan_ffhq, l=l_pggan_ffhq, h=h_pggan_ffhq, fig_name='PGGAN_FFHQ_1024.pdf', shaded=True)

# In[215]:

def plot_ripley_combined(k_min,k_max,l,h,fig_name):
    d_lst = list(pl.frange(0.1,0.5,0.01))
    P.fill_between(d_lst, k_min, k_max, color='r', alpha=0.5, label="filled_region_obs")
    P.fill_between(d_lst, l, h, color='b', alpha=0.5, label="filled_region_ref")

    rect_r = Rectangle((0, 0), 1, 1, fc="r", alpha=0.5)
    rect_b = Rectangle((0, 0), 1, 1, fc="b", alpha=0.5)

    P.yticks(np.arange(0, 7, 0.5))
    #P.yticks([])
    P.legend([rect_r, rect_b], [r'$\mathcal{N}_{R_{obs}}$', r'$\mathcal{N}_{R_{ref}}$'],
             loc='center right', shadow=True, facecolor='white', framealpha=1)
    P.xlabel(r'$d$')
    P.ylabel(r'$log_{10}(N(d))$')
    #P.title("StyleGAN-Randomness-128")
    P.savefig(fig_name, dpi=800, bbox_inches='tight', pad_inches=0)
    #P.show()

# In[216]:

k_sgan_1, l_sgan_1, h_sgan_1 = compute_ripley('legacy/sgan/monte_carlo_sampling_1m_128_1/ripley')
k_sgan_2, l_sgan_2, h_sgan_2 = compute_ripley('legacy/sgan/monte_carlo_sampling_1m_128_2/ripley')
k_sgan_3, l_sgan_3, h_sgan_3 = compute_ripley('legacy/sgan/monte_carlo_sampling_1m_128_3/ripley')
k_sgan_4, l_sgan_4, h_sgan_4 = compute_ripley('legacy/sgan/monte_carlo_sampling_1m_128_4/ripley')
k_sgan_5, l_sgan_5, h_sgan_5 = compute_ripley('legacy/sgan/monte_carlo_sampling_1m_128_5/ripley')
k_sgan_max = np.amax(np.stack([k_sgan_1, k_sgan_2, k_sgan_3, k_sgan_4, k_sgan_5], axis=0), axis=0)
k_sgan_min = np.amin(np.stack([k_sgan_1, k_sgan_2, k_sgan_3, k_sgan_4, k_sgan_5], axis=0), axis=0)
l_sgan = np.amin(np.stack([l_sgan_1, l_sgan_2, l_sgan_3, l_sgan_4, l_sgan_5], axis=0), axis=0)
h_sgan = np.amax(np.stack([h_sgan_1, h_sgan_2, h_sgan_3, h_sgan_4, h_sgan_5], axis=0), axis=0)

plot_ripley_combined(k_min=k_sgan_min,k_max=k_sgan_max,l=l_sgan,h=h_sgan,fig_name='SGAN_Randomness_128.pdf')

# In[217]:

k_sgan_1, l_sgan_1, h_sgan_1 = compute_ripley('legacy/sgan/monte_carlo_sampling_1m_128_1_7707_finetune/ripley')
k_sgan_2, l_sgan_2, h_sgan_2 = compute_ripley('legacy/sgan/monte_carlo_sampling_1m_128_1_8307_finetune/ripley')
k_sgan_3, l_sgan_3, h_sgan_3 = compute_ripley('legacy/sgan/monte_carlo_sampling_1m_128_1_8908_finetune/ripley')
k_sgan_4, l_sgan_4, h_sgan_4 = compute_ripley('legacy/sgan/monte_carlo_sampling_1m_128_1_9508_finetune/ripley')
k_sgan_5, l_sgan_5, h_sgan_5 = compute_ripley('legacy/sgan/monte_carlo_sampling_1m_128_1_10108_finetune/ripley')
k_sgan_6, l_sgan_6, h_sgan_6 = compute_ripley('legacy/sgan/monte_carlo_sampling_1m_128_1_10708_finetune/ripley')
k_sgan_7, l_sgan_7, h_sgan_7 = compute_ripley('legacy/sgan/monte_carlo_sampling_1m_128_1_11309_finetune/ripley')
k_sgan_8, l_sgan_8, h_sgan_8 = compute_ripley('legacy/sgan/monte_carlo_sampling_1m_128_1_12000_finetune/ripley')

k_sgan_max = np.amax(np.stack([k_sgan_1, k_sgan_2, k_sgan_3, k_sgan_4, k_sgan_5, k_sgan_6, k_sgan_7, k_sgan_8], axis=0), axis=0)
k_sgan_min = np.amin(np.stack([k_sgan_1, k_sgan_2, k_sgan_3, k_sgan_4, k_sgan_5, k_sgan_6, k_sgan_7, k_sgan_8], axis=0), axis=0)
l_sgan = np.amin(np.stack([l_sgan_1, l_sgan_2, l_sgan_3, l_sgan_4, l_sgan_5, l_sgan_6, l_sgan_7, l_sgan_8], axis=0), axis=0)
h_sgan = np.amax(np.stack([h_sgan_1, h_sgan_2, h_sgan_3, h_sgan_4, h_sgan_5, h_sgan_6, h_sgan_7, h_sgan_8], axis=0), axis=0)
plot_ripley_combined(k_min=k_sgan_min,k_max=k_sgan_max,l=l_sgan,h=h_sgan,fig_name='SGAN_Finetune_128.pdf')

# In[218]:

k_sgan_128, l_sgan_128, h_sgan_128 = compute_ripley('legacy/sgan/monte_carlo_sampling_1m_128_1/ripley')
k_sgan_256, l_sgan_256, h_sgan_256 = compute_ripley('legacy/sgan/monte_carlo_sampling_1m_256_1/ripley')
k_sgan_512, l_sgan_512, h_sgan_512 = compute_ripley('legacy/sgan/monte_carlo_sampling_1m_512_1/ripley')
k_sgan_1024, l_sgan_1024, h_sgan_1024 = compute_ripley('legacy/sgan/monte_carlo_sampling_1m_1024_1/ripley')

#line1 = P.plot(d_lst, l_pggan_celebahq, color='b', marker='o', linestyle='dashed', linewidth=0.5, markersize=0.5, label=r'$K_{R_{obs}}(\theta)$')[0]
#line2 = P.plot(d_lst, h_pggan_celebahq, color='b', marker='o', linestyle='dashed', linewidth=0.5, markersize=0.5, label=r'$K_{R_{obs}}(\theta)$')[0]
k_sgan_max = np.amax(np.stack([k_sgan_128, k_sgan_256, k_sgan_512, k_sgan_1024], axis=0), axis=0)
k_sgan_min = np.amin(np.stack([k_sgan_128, k_sgan_256, k_sgan_512, k_sgan_1024], axis=0), axis=0)
l_sgan = np.amin(np.stack([l_sgan_128, l_sgan_256, l_sgan_512, l_sgan_1024], axis=0), axis=0)
h_sgan = np.amax(np.stack([h_sgan_128, h_sgan_256, h_sgan_512, h_sgan_1024], axis=0), axis=0)
plot_ripley_combined(k_min=k_sgan_min,k_max=k_sgan_max,l=l_sgan,h=h_sgan,fig_name='SGAN_Architecture.pdf')

# In[219]:

k_sgan_128, l_sgan_128, h_sgan_128 = compute_ripley('legacy/pggan/monte_carlo_sampling_1m_128/ripley')
k_sgan_1024, l_sgan_1024, h_sgan_1024 = compute_ripley('legacy/pggan/monte_carlo_sampling_1m_1024/ripley')

k_sgan_max = np.amax(np.stack([k_sgan_128, k_sgan_1024], axis=0), axis=0)
k_sgan_min = np.amin(np.stack([k_sgan_128, k_sgan_1024], axis=0), axis=0)
l_sgan = np.amin(np.stack([l_sgan_128, l_sgan_1024], axis=0), axis=0)
h_sgan = np.amax(np.stack([h_sgan_128, h_sgan_1024], axis=0), axis=0)

plot_ripley_combined(k_min=k_sgan_min,k_max=k_sgan_max,l=l_sgan,h=h_sgan,fig_name='PGGAN_Architecture.pdf')

# In[187]:

def plot_ripley_calibration(k1,k2,l1,h1,l2,h2,fig_name):
    d_lst = list(pl.frange(0.1,0.5,0.01))

    k1 = P.plot(d_lst, k1, color='r', marker='o', linestyle='dashed', linewidth=0.5, markersize=0.5, label=r'$\mathcal{N}_{R_{obs}}$')[0]
    k2 = P.plot(d_lst, k2, color='b', marker='o', linestyle='dashed', linewidth=0.5, markersize=0.5, label=r'$\mathcal{N}_{R_{obs}}^\prime$')[0]
    P.fill_between(d_lst, l1, h1, color='r', alpha=0.5, label="filled_region_ref_1")
    P.fill_between(d_lst, l2, h2, color='b', alpha=0.5, label="filled_region_ref_2")

    rect_r = Rectangle((0, 0), 1, 1, fc="r", alpha=0.5)
    rect_b = Rectangle((0, 0), 1, 1, fc="b", alpha=0.5)

    P.yticks(np.arange(0, 7, 0.5))
    P.legend([k1, k2, rect_r, rect_b], [r'$\mathcal{N}_{R_{obs}}$', r'$\mathcal{N}_{R_{obs}}^\prime$',
                                        r'$\mathcal{N}_{R_{ref}}$', r'$\mathcal{N}_{R_{ref}}^\prime$'],
             loc='center right', shadow=True, facecolor='white', framealpha=1)
    P.xlabel(r'$d$')
    P.ylabel(r'$log_{10}(N(d))$')
    #P.title("StyleGAN-Randomness-128")
    P.savefig(fig_name, dpi=800, bbox_inches='tight', pad_inches=0)

# In[188]:

k_sgan_1, l_sgan_1, h_sgan_1 = compute_ripley('legacy/sgan/monte_carlo_sampling_1m_128_1_7707_finetune/ripley')
k_sgan_2, l_sgan_2, h_sgan_2 = compute_ripley('legacy/sgan/monte_carlo_sampling_1m_128_1_8307_finetune/ripley')
k_sgan_3, l_sgan_3, h_sgan_3 = compute_ripley('legacy/sgan/monte_carlo_sampling_1m_128_1_8908_finetune/ripley')
k_sgan_4, l_sgan_4, h_sgan_4 = compute_ripley('legacy/sgan/monte_carlo_sampling_1m_128_1_9508_finetune/ripley')
k_sgan_5, l_sgan_5, h_sgan_5 = compute_ripley('legacy/sgan/monte_carlo_sampling_1m_128_1_10108_finetune/ripley')
k_sgan_6, l_sgan_6, h_sgan_6 = compute_ripley('legacy/sgan/monte_carlo_sampling_1m_128_1_10708_finetune/ripley')
k_sgan_7, l_sgan_7, h_sgan_7 = compute_ripley('legacy/sgan/monte_carlo_sampling_1m_128_1_11309_finetune/ripley')
k_sgan_8, l_sgan_8, h_sgan_8 = compute_ripley('legacy/sgan/monte_carlo_sampling_1m_128_1_12000_finetune/ripley')

# In[189]:

plot_ripley_calibration(k1=k_sgan_1, k2=k_sgan_4, l1=l_sgan_1, h1=h_sgan_1, l2=l_sgan_4, h2=h_sgan_4 , fig_name='CelebAHQ_GMM_128.pdf')

# In[190]:

plot_ripley_calibration(k1=k_sgan_1, k2=k_sgan_5, l1=l_sgan_1, h1=h_sgan_1, l2=l_sgan_5, h2=h_sgan_5 , fig_name='CelebAHQ_IS_128.pdf')

max_stars_repo_name: VITA-Group/BlackBoxGANCollapse

max_issues_repo_name: VITA-Group/BlackBoxGANCollapse

max_forks_repo_name: VITA-Group/BlackBoxGANCollapse


text:
# -*- coding: utf-8 -*-
from itertools import cycle
import matplotlib.pyplot as plt
from matplotlib import ticker
import numpy as np
import os
import pandas as pd
import seaborn as sns

import simulation  # to avoid circular imports
from global_variables import DIM, RTT_SYSTEM_ID

def make_dirs_safe(path):
    """ Make directory of input path, if it does not exist yet. """
    dirname = os.path.dirname(path)
    if not os.path.exists(dirname):
        os.makedirs(dirname)

def savefig(fig, name):
    make_dirs_safe(name)
    fig.savefig(name, bbox_inches='tight', pad_inches=0)

def add_scalebar(ax, size=5, size_vertical=1, loc='lower left'):
    """ Add a scale bar to the plot.

    :param ax: axis to use.
    :param size: size of scale bar.
    :param size_vertical: height (thckness) of the bar
    :param loc: location (same syntax as for matplotlib legend)
    """
    from mpl_toolkits.axes_grid1.anchored_artists import AnchoredSizeBar
    import matplotlib.font_manager as fm
    fontprops = fm.FontProperties(size=8)
    scalebar = AnchoredSizeBar(ax.transData,
                               size,
                               '{} m'.format(size),
                               loc,
                               pad=0.1,
                               color='black',
                               frameon=False,
                               size_vertical=size_vertical,
                               fontproperties=fontprops)
    ax.add_artist(scalebar)

def remove_ticks(ax):
    """ Remove all ticks and margins from plot. """
    for ax_name in ['x', 'y']:
        ax.tick_params(axis=ax_name,
                       which='both',
                       bottom=False,
                       top=False,
                       left=False,
                       labelbottom=False,
                       labelleft=False)
    plt.subplots_adjust(top=1, bottom=0, right=1, left=0, hspace=0, wspace=0.1)
    plt.margins(0, 0)
    ax.xaxis.set_major_locator(plt.NullLocator())
    ax.yaxis.set_major_locator(plt.NullLocator())

def plot_cdf_raw(values, ax, **kwargs):
    """Plot the cdf of values.

    :param values: values to plot.
    :param ax: axis to plot.
    :param kwargs: any other kwargs that are passed to plot function.
    """
    probabilities = np.linspace(0, 1, len(values))
    ax.plot(np.sort(values), probabilities, **kwargs)
    ax.yaxis.set_major_locator(plt.FixedLocator(np.linspace(0, 1, 5)))
    ax.set_ylabel('cdf')
    ax.grid(True)

def plot_distances(data_df, anchors_df):
    import itertools
    colors = itertools.cycle(plt.get_cmap('tab10').colors)
    rtt_ids = anchors_df[anchors_df.system_id == RTT_SYSTEM_ID].anchor_id.unique()
    fig, axs = plt.subplots(1, len(rtt_ids), sharey=True)
    fig.set_size_inches(15, 5)
    for ax, anchor_id in zip(axs, rtt_ids):
        color = next(colors)
        data = data_df[data_df.anchor_id == anchor_id]
        anchor_name = anchors_df.loc[anchors_df.anchor_id == anchor_id, 'anchor_name']
        ax.plot(data.seconds, data.distance_gt, linestyle=':', label=anchor_name, color=color)
        ax.plot(data.seconds, data.distance, linestyle='-', color=color)
        ax.set_ylim(0, 15)

def plot_noise(key,
               save_figures,
               error_types=None,
               min_noise=None,
               max_noise=None,
               background_alpha=0.1,
               start=1,
               anchors=False,
               lines=None,
               ax=None):
    """Plot results of the simulations of the influence of noise on the recovery.

    :param key: directory of the data
    :param error_types: which data to load from the directory ('absolute-errors', 'relative-errors' or 'errors')"""

    if error_types is None:
        error_types = ['absolute-errors', 'relative-errors', 'errors']

    if lines is None:
        lines = ["-", "--", "-.", ":"]

    linecycler = cycle(lines)

    resultfolder = 'results/{}/'.format(key)
    results = simulation.read_results(resultfolder + 'result_')
    parameters = simulation.read_params(resultfolder + 'parameters.json')

    min_measurements = (DIM + 2) * parameters["complexities"][0] - 1

    if anchors:
        second_dim = parameters['anchors']
    else:
        second_dim = parameters['noise_sigmas']

    max_measurements = np.min(parameters["positions"]) * np.min(parameters["anchors"])
    if 'sampling_strategy' in parameters:
        if parameters['sampling_strategy'] == 'single_time':
            max_measurements = np.min(parameters["positions"])

    for error_type in error_types:
        error = np.mean(results[error_type], axis=-1)
        error = error.squeeze()
        measurements = np.arange(min_measurements, max_measurements + 1)[::-1]
        if len(second_dim) == 1:
            error = error[:, None]

        if ax is None:
            _, ax1 = plt.subplots()
        else:
            ax1 = ax

        if len(second_dim) == 1:
            error = error.T
        for idx, _ in enumerate(second_dim[min_noise:max_noise]):
            plot = ax1.loglog(measurements, error.T[:len(measurements), idx], alpha=background_alpha)
            z = np.polyfit(np.log(measurements[:-start]), np.log(error[idx, :len(measurements[:-start])]), 1)
            pol = np.poly1d(z)
            print(("anchors {}" if anchors else "noise: {}").format(second_dim[idx]))
            print("fitted slope: {:.2f}".format(z[0]))
            ax1.loglog(measurements,
                       np.exp(pol(np.log(measurements))),
                       c=plot[0].get_color(),
                       label=("{} anchors" if anchors else r"noise: {}").format(second_dim[idx]),
                       linestyle=next(linecycler))

        plt.xlabel("number of measurements")
        if error_type == "errors":
            plt.ylabel("errors")
        else:
            plt.ylabel(" ".join(error_type.split("-") + ["on distances"]))
        ax1.legend(loc='upper right', mode='expand', ncol=len(second_dim[min_noise:max_noise]))
        ax1.set_xticks(measurements[::int(len(measurements) / 5)])
        ax1.get_xaxis().set_major_formatter(ticker.ScalarFormatter())
        ax1.get_xaxis().set_minor_formatter(ticker.NullFormatter())
        ax1.grid(b=True)
        # plt.title(key)
        if save_figures:
            plt.savefig(resultfolder + "oversapling_" + error_type + ".pdf", bbox_inches="tight")
    return plt.gca()

def read_plot_df(name, folder='experiments/robot_test/'):
    """
    read dataset of given name.

    :param name: name of dataset, for example circle2_double.csv
    :return:
        - data_df: calibrated dataset, with columns [distance, distance_mean_0, ...]
        - plot_df: same dataset with one column distance, and one column distance_type.
    """
    datafile = folder + name
    datafile_name = datafile.split('.')[0]
    calibrate_name = datafile_name + '_calibrated.pkl'

    data_df = pd.read_pickle(calibrate_name)

    data_df = data_df.drop(columns=['theta_x', 'theta_y', 'theta_z'], errors='ignore')
    print('read', calibrate_name)

    id_vars = [c for c in data_df.columns if c[:8] != 'distance']
    plot_df = data_df.melt(id_vars=id_vars, var_name="distance_type", value_name="distance")
    if 'RTT' in plot_df.system_id.unique():
        print('Warning: using old RTT system id.')
        plot_df = plot_df[plot_df.system_id == 'RTT']
    elif 'Range' in plot_df.system_id.unique():
        plot_df = plot_df[plot_df.system_id == 'Range']
    else:
        raise NameError('no valid system id in {}'.format(plot_df.system_id.unique()))

    plot_df.sort_values(['timestamp', 'anchor_name'], inplace=True)
    plot_df.reset_index(inplace=True, drop=True)
    plot_df.loc[:, 'distance'] = plot_df.distance.astype(np.float32)
    plot_df.loc[:, 'timestamp'] = plot_df.timestamp.astype(np.float32)

    return data_df, plot_df

def plot_cdfs(plot_df, filename=''):
    colors = sns.color_palette('deep')

    fig, axarr = plt.subplots(2, 4)
    fig.set_size_inches(15, 10)
    axarr = axarr.reshape((-1, ))
    for i, (anchor_name, df) in enumerate(plot_df.sort_values("anchor_name").groupby("anchor_name")):

        axarr[i].set_title(anchor_name)
        df = df.sort_values("timestamp")
        gt_df = df[df.distance_type == "distance_gt"]

        color_cycle = cycle(colors)
        for distance_type in sorted(df.distance_type.unique()):
            if distance_type == "distance_tango" or distance_type == "distance_gt":
                continue
            meas_df = df[df.distance_type == distance_type]

            np.testing.assert_allclose(meas_df.timestamp.values, gt_df.timestamp.values)

            errors = np.abs(meas_df.distance.values - gt_df.distance.values)
            plot_cdf_raw(errors, ax=axarr[i], color=next(color_cycle), label=distance_type)
            axarr[i].set_ylabel('')
    axarr[i].legend(loc='lower left', bbox_to_anchor=[1.0, 0])
    axarr[0].set_ylabel('cdf [-]')
    axarr[4].set_ylabel('cdf [-]')
    for j in range(i + 1, len(axarr)):
        axarr[j].axis('off')
    [axarr[i].set_xlabel('absolute distance error [m]') for i in range(4, len(axarr))]

    if filename != '':
        savefig(fig, filename)

def plot_times(plot_df, filename=''):
    colors = sns.color_palette('deep')

    fig, axarr = plt.subplots(2, 4)
    fig.set_size_inches(15, 10)
    axarr = axarr.reshape((-1, ))
    for i, (anchor_name, df) in enumerate(plot_df.sort_values("anchor_name").groupby("anchor_name")):
        axarr[i].set_title(anchor_name)
        df = df.sort_values("timestamp")

        color_cycle = cycle(colors)
        for distance_type in sorted(df.distance_type.unique()):
            meas_df = df[df.distance_type == distance_type]
            axarr[i].plot(meas_df.timestamp.values,
                          meas_df.distance.values,
                          color=next(color_cycle),
                          label=distance_type)
    axarr[i].legend(loc='lower left', bbox_to_anchor=[1.0, 0])
    for j in range(i + 1, len(axarr)):
        axarr[j].axis('off')

    if filename != '':
        savefig(fig, filename)

def plot_rssis(plot_df, filename=''):
    fig, axarr = plt.subplots(2, 4, sharey=True, sharex=True)
    fig.set_size_inches(15, 10)
    axarr = axarr.reshape((-1, ))
    for i, (anchor_name, df) in enumerate(plot_df.sort_values("anchor_name").groupby("anchor_name")):
        axarr[i].set_title(anchor_name)
        df = df.sort_values("timestamp")
        gt_df = df[df.distance_type == "distance_gt"]

        meas_df = df[df.distance_type == "distance_median_all"]

        assert np.allclose(meas_df.timestamp.values, gt_df.timestamp.values)

        errors = np.abs(meas_df.distance.values - gt_df.distance.values)
        rssis = meas_df.rssi.values

        axarr[i].scatter(rssis, errors, alpha=0.2)
    for j in range(i + 1, len(axarr)):
        axarr[j].axis('off')

    if filename != '':
        savefig(fig, filename)

def plot_tango_components(data_df, filename=''):
    fig = plt.figure()
    data = data_df[data_df.system_id == "GT"]
    sns.scatterplot(data=data, x='timestamp', y='px', linewidth=0.0, label='x')
    sns.scatterplot(data=data, x='timestamp', y='py', linewidth=0.0, label='y')
    sns.scatterplot(data=data, x='timestamp', y='pz', linewidth=0.0, label='z')
    plt.legend()

    if filename != '':
        savefig(fig, filename)

def plot_tango_2d(data_df, anchors_df, filename=''):
    tango_df = data_df.loc[data_df.system_id == "GT"]

    fig = plt.figure()
    sns.scatterplot(data=tango_df, x='px', y='py', hue='timestamp', linewidth=0.0)
    sns.scatterplot(data=anchors_df, x='px', y='py', hue='anchor_name', linewidth=0.0, style='system_id', legend=False)
    plt.arrow(0, 0, 1, 0, color='red', head_width=0.2)
    plt.arrow(0, 0, 0, 1, color='green', head_width=0.2)
    delta = 0.1
    for i, a in anchors_df.iterrows():
        plt.annotate(a.anchor_name, (a.px + delta, a.py + delta))
    plt.legend()
    plt.axis('equal')
    if filename != '':
        savefig(fig, filename)

def plot_complexities(traj, times, results, points_gt=None, ax=None, verbose=False):
    """ Create complexity plots.

    :param results: results dict of form {
      'label1': (coeffs1, points1(if applicable)),
      'label2': (coeffs2, points2(if applicable))
    }
    :param points_gt: points of shape (N x dim).

    """
    from other_algorithms import error_measure
    if ax is None:
        fig, ax = plt.subplots()

    if points_gt is not None:
        assert points_gt.shape[1] == traj.dim
        ax.plot(points_gt[:, 0], points_gt[:, 1], color='black', ls=':', linewidth=1., label='GPS')

    i = 0
    measure = 'mse'
    for label, (Chat, points) in results.items():
        color = f'C{i}'
        i += 1
        if Chat is not None:
            traj.set_coeffs(coeffs=Chat)
            traj.plot_pretty(times=times, color=color, label=label, ax=ax)

            if verbose:
                points_est = traj.get_sampling_points(times=times).T
                err = error_measure(points_gt, points_est, measure=measure)
                print(f'{label} {measure} from coeffs: {err:.2e}')

        if points is not None:
            for x in points:
                ax.scatter(*x, color=color, label=label, s=4.0)
                label = None
    remove_ticks(ax)
    return ax

def plot_probabilities(probabilities,
                       params,
                       ax,
                       directory="results/probabilities/",
                       save=False,
                       label="estimated",
                       variance=None,
                       **kwargs):
    key = "_d{}_c{}_p{}_full{}".format(params["n_dimensions"], params["n_constraints"], params["n_times"],
                                       params["full_matrix"])

    x = np.array(params["n_measurements_list"]) / params["max_rank"]

    for a_idx, n_anchors in enumerate(params["n_anchors_list"]):
        plt.step(x,
                 probabilities[:, a_idx],
                 label="{} probability, {} anchors".format(label, n_anchors),
                 color="C{}".format(a_idx),
                 where='post',
                 **kwargs)
        if variance is not None:
            plt.fill_between(x,
                             probabilities[:, a_idx] - variance[:, a_idx],
                             probabilities[:, a_idx] + variance[:, a_idx],
                             color="C{}".format(a_idx),
                             alpha=0.3)
    plt.xlabel("number of measurements")
    formatter_text = '%g (D+1)K + (K-1)' if params["full_matrix"] else '%g (D+1)K'
    ax.xaxis.set_major_formatter(ticker.FormatStrFormatter(formatter_text))
    ax.xaxis.set_major_locator(ticker.MultipleLocator(base=1))
    ax.set_ylim(0)
    plt.legend()
    ax.grid(b=True)
    params["directory"] = directory
    if save:
        plt.ylim(bottom=0)
        matrix_type = "full" if params["full_matrix"] else "left"
        fname = directory + matrix_type + "_matrix_anchors" + key + ".pdf"
        make_dirs_safe(fname)
        plt.savefig(fname)

def plot_dataset_subsample(traj, results, times, srls=False, rls=True):
    """ Create Figure 7, second row.

    see plot_dataset_complexities for parameters.
    """
    n_measurements_list = sorted(results.n_measurements.unique())[::-1]
    fig, axs = plt.subplots(1, len(n_measurements_list), sharex=True, sharey=True)
    j = 0
    for n_measurements in n_measurements_list:
        df = results[results.n_measurements == n_measurements]
        ax = axs[j]
        df_ours = df[df.method == 'ours-weighted']

        coeffs = np.empty([traj.dim, traj.n_complexity, 0])
        colors = {0: 0, 1: 2, 2: 3}
        for i in range(len(df_ours)):
            df_our = df_ours.iloc[i]
            Chat = df_our.result

            coeffs = np.dstack([coeffs, Chat])
            traj.set_coeffs(coeffs=Chat)
            traj.plot_pretty(times=times, color=f'C{colors[i]}', ax=ax)

        Chat_avg = np.mean(coeffs, axis=2)
        traj.set_coeffs(coeffs=Chat_avg)
        traj.plot_pretty(times=times, color='C0', ax=ax)

        i = 1
        if srls:
            df_srls = df[df.method == 'srls']
            points = df_srls.result.values[0]
            ax.scatter(*points.T, color=f'C{i}', s=4.0)
            i += 1

        if rls:
            df_rls = df[df.method == 'rls']
            points = df_rls.result.values[0]
            ax.scatter(*points.T, color=f'C{i}', s=4.0)

        #ax.plot(full_df.px, full_df.py, ls=':', linewidth=1., color='black')
        remove_ticks(ax)
        ax.set_title('N = {}'.format(n_measurements), y=-0.22)
        j += 1
    add_scalebar(axs[0], 20, loc='lower left')
    return fig, axs

def plot_dataset_complexities(traj, results, times, srls=False, rls=True):
    """ Create Figure 7, first row.

    :param traj: instance of class Trajectory.
    :param results: generated using create_complexities from results_generation.
    :param times: times at which we want to plot trajectory.
    :param srls, rls: whether or not to plot SRLS or RLS estimate.
    """
    n_complexity_list = sorted(results.n_complexity.unique())
    fig, axs = plt.subplots(1, len(n_complexity_list), sharex=True, sharey=True)
    j = 0
    for n_complexity in n_complexity_list:
        df = results[results.n_complexity == n_complexity]
        ax = axs[j]
        df_ours = df[df.method == 'ours-weighted']

        Chat = df_ours.result.values[0]

        traj.set_coeffs(coeffs=Chat)
        traj.plot_pretty(times=times, color='C0', ax=ax)

        i = 1
        if srls:
            df_srls = df[df.method == 'srls']
            points = df_srls.result.values[0]
            ax.scatter(*points.T, color=f'C{i}', s=4.0)
            i += 1

        if rls:
            df_rls = df[df.method == 'rls']
            points = df_rls.result.values[0]
            ax.scatter(*points.T, color=f'C{i}', s=4.0)

        #ax.plot(full_df.px, full_df.py, ls=':', linewidth=1., color='black')
        remove_ticks(ax)
        ax.set_title('N = {}'.format(n_complexity), y=-0.22)
        j += 1
    add_scalebar(axs[0], 20, loc='lower left')
    return fig, axs

max_stars_repo_name: LCAV/continuous-localization

max_issues_repo_name: LCAV/continuous-localization

max_forks_repo_name: LCAV/continuous-localization


text:
from PIL import Image
import os
import glob
import numpy as np
import tqdm

def demontage(path):
	im = Image.open(path)
	xes = list(enumerate(range(25, im.size[0], 250)))
	n_cols = len(xes)
	for xi, x in xes:
		for yi, y in enumerate(range(25, im.size[1], 250)):
			im2 = im.crop((x, y, x+200, y+200))
			im2.save("minis/{:02d}.{:03d}.png".format(i, yi * n_cols + xi))

def montage(input_paths, width, height):
	out_path = "{:02d}x{:02d}".format(width, height)
	if not os.path.exists(out_path):
		os.mkdir(out_path)
	chunk = width * height
	for i in tqdm.tqdm(range(0, len(input_paths), chunk)):

		# Load a chunk of input images
		inputs = [[np.zeros((200, 200, 3)) for _ in range(width)] for _ in range(height)]
		for j in range(chunk):
			if i + j < len(input_paths):
				inputs[j // width][j % width] = np.array(Image.open(input_paths[i + j]))

		# Stitch them together
		stitched = np.vstack([np.hstack(row) for row in inputs])
		stitched = np.uint8(stitched)

		# Save them
		Image.fromarray(stitched, 'RGB').save(os.path.join(out_path, "{:03d}.png".format(i)))

if __name__ == '__main__':
	# for i in range(55):
	# 	demontage("debate_montages/{}.png".format(i))

	for width, height in [(2, 4), (4, 2)]:
		montage(sorted(glob.glob('minis/*')), width, height)

max_stars_repo_name: scanner-research/tvnews-ingest-pipeline

max_issues_repo_name: scanner-research/tvnews-ingest-pipeline

max_forks_repo_name: scanner-research/tvnews-ingest-pipeline