text:
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
from torch.nn import init
import numpy as np

from layers import GaussianSample, GaussianMerge, GumbelSoftmax
from inference import log_gaussian, log_standard_gaussian
from utils import dynamic_partition, dynamic_stitch, batch_normalization

from inference import loglik

class Perceptron(nn.Module):
    def __init__(self, dims, activation_fn=F.relu, output_activation=None):
        super(Perceptron, self).__init__()
        self.dims = dims
        self.activation_fn = activation_fn
        self.output_activation = output_activation

        self.layers = nn.ModuleList(list(map(lambda d: nn.Linear(*d), list(zip(dims, dims[1:])))))

    def forward(self, x):
        for i, layer in enumerate(self.layers):
            x = layer(x)
            if i == len(self.layers) - 1 and self.output_activation is not None:
                x = self.output_activation(x)
            else:
                x = self.activation_fn(x)

        return x

class Encoder(nn.Module):
    def __init__(self, dims, sample_layer=GaussianSample):
        """
        Inference network

        Attempts to infer the probability distribution
        p(z|x) from the data by fitting a variational
        distribution q_φ(z|x). Returns the two parameters
        of the distribution (µ, log σ²).

        :param dims: dimensions of the networks
           given by the number of neurons on the form
           [input_dim, [hidden_dims], latent_dim].
        """
        super(Encoder, self).__init__()

        [x_dim, h_dim, z_dim] = dims
        neurons = [x_dim, *h_dim]
        linear_layers = [nn.Linear(neurons[i - 1], neurons[i]) for i in range(1, len(neurons))]

        self.hidden = nn.ModuleList(linear_layers)
        self.sample = sample_layer(h_dim[-1], z_dim)

    def forward(self, x):
        for layer in self.hidden:
            x = F.relu(layer(x))
        return self.sample(x)

class Decoder(nn.Module):
    def __init__(self, dims):
        """
        Generative network

        Generates samples from the original distribution
        p(x) by transforming a latent representation, e.g.
        by finding p_θ(x|z).

        :param dims: dimensions of the networks
            given by the number of neurons on the form
            [latent_dim, [hidden_dims], input_dim].
        """
        super(Decoder, self).__init__()

        [z_dim, h_dim, x_dim] = dims

        neurons = [z_dim, *h_dim]
        linear_layers = [nn.Linear(neurons[i - 1], neurons[i]) for i in range(1, len(neurons))]
        self.hidden = nn.ModuleList(linear_layers)

        self.reconstruction = nn.Linear(h_dim[-1], x_dim)

        self.output_activation = nn.Sigmoid()

    def forward(self, x):
        for layer in self.hidden:
            x = F.relu(layer(x))
        return self.output_activation(self.reconstruction(x))

class HIDecoder(nn.Module):
    def __init__(self, dims, types_list):
        """
        Generative network

        Generates samples from the original distribution
        p(x) by transforming a latent representation, e.g.
        by finding p_θ(x|z).

        :param dims: dimensions of the networks
            given by the number of neurons on the form
            [latent_dim, [hidden_dims], input_dim].
        """
        super(HIDecoder, self).__init__()

        [z_dim, h_dim, gamma_dim, x_dim] = dims

        self.types_list = types_list
        self.gamma_dim_partition = gamma_dim * np.ones(len(types_list), dtype=int)
        self.gamma_dim_output = np.sum(self.gamma_dim_partition)
        self.hidden = None
        gamma_input_dim = z_dim
        if h_dim is not None or h_dim != [] or h_dim != 0:
            neurons = [z_dim, *h_dim]
            linear_layers = [nn.Linear(neurons[i - 1], neurons[i]) for i in range(1, len(neurons))]
            self.hidden = nn.ModuleList(linear_layers)
            gamma_input_dim = h_dim[-1]
        # deterministic homogeneous gamma layer
        self.gamma_layer = nn.Linear(gamma_input_dim, self.gamma_dim_output)
        self.obs_layer = self.get_obs_layers(gamma_dim)
        # self.reconstruction = nn.Linear(h_dim[-1], x_dim)
        # self.output_activation = nn.Sigmoid()

    def get_obs_layers(self, gamma_dim):
        # Different layer models for each type of variable
        obs_layers = []
        for type in self.types_list:
            type_dim = type['dim']
            if type['type'] == 'real' or type['type'] == 'pos':
                obs_layers.append(nn.ModuleList([nn.Linear(gamma_dim, type_dim),  # mean
                                                nn.Linear(gamma_dim, type_dim)]))  # sigma
            elif type['type'] == 'count':
                obs_layers.append(nn.ModuleList([nn.Linear(gamma_dim, type_dim)]))  # lambda
            elif type['type'] == 'cat':
                obs_layers.append(nn.ModuleList[nn.Linear(gamma_dim, type_dim - 1)])  # log pi
            elif type['type'] == 'ord':
                obs_layers.append(nn.ModuleList[nn.Linear(gamma_dim, type_dim - 1),  # theta
                                                nn.Linear(gamma_dim, 1)])           # mean, single value
        return nn.ModuleList(obs_layers)

    def forward(self, z, batch_x, miss_list, norm_params):
        if self.hidden is not None:
            for layer in self.hidden:
                z = F.relu(layer(z))
        # Deterministic homogeneous representation gamma = g(z)
        gamma = self.gamma_layer(z)
        gamma_grouped = self.gamma_partition(gamma)
        theta = self.theta_estimation_from_gamma(gamma_grouped, miss_list)

        log_p_x, log_p_x_missing, samples_x, params_x = self.loglik_and_reconstruction(theta, batch_x,
                                                                                miss_list, norm_params)
        return log_p_x, log_p_x_missing, samples_x, params_x

    def loglik_and_reconstruction(self, theta, batch_data, miss_list, normalization_params):
        log_p_x = []
        log_p_x_missing = []
        samples_x = []
        params_x = []

        # independent gamma_d -> Compute log(p(xd|gamma_d))
        for i, d in enumerate(batch_data.T):
            d = d.unsqueeze(1)
            # Select the likelihood for the types of variables
            loglik_function = getattr(loglik, 'loglik_' + self.types_list[i]['type'])
            out = loglik_function([d, miss_list[:, i]], self.types_list[i], theta[i], normalization_params[i])

            log_p_x.append(out['log_p_x'].unsqueeze(1))
            log_p_x_missing.append(out['log_p_x_missing'].unsqueeze(1))  # Test-loglik element
            samples_x.append(out['samples'])
            params_x.append(out['params'])
        # return log_p_x, log_p_x_missing, samples_x, params_x
        return torch.cat(log_p_x, dim=1), torch.cat(log_p_x_missing, dim=1), torch.cat(samples_x, dim=1), params_x

    def gamma_partition(self, gamma):
        grouped_samples_gamma = []
        # First element must be 0 and the length of the partition vector must be len(types_dict)+1
        if len(self.gamma_dim_partition) != len(self.types_list):
            raise Exception("The length of the partition vector must match the number of variables in the data + 1")
        # Insert a 0 at the beginning of the cumsum vector
        partition_vector_cumsum = np.insert(np.cumsum(self.gamma_dim_partition), 0, 0)
        for i in range(len(self.types_list)):
            grouped_samples_gamma.append(gamma[:, partition_vector_cumsum[i]:partition_vector_cumsum[i + 1]])
        return grouped_samples_gamma

    def theta_estimation_from_gamma(self, gamma, miss_list):
        theta = []
        # independent yd -> Compute p(xd|yd)
        for i, d in enumerate(gamma):  # gamma is a list
            # Partition the data in missing data (0) and observed data (1)
            missing_y, observed_y = dynamic_partition(d, miss_list[:, i], 2)
            condition_indices = dynamic_partition(torch.arange(d.size()[0]), miss_list[:, i], 2)
            # Different layer models for each type of variable
            params = self.observed_data_layer(observed_y, missing_y, condition_indices, i)
            theta.append(params)
        return theta

    def observed_data_layer(self, observed_data, missing_data, condition_indices, i):
        outputs = []
        for obs_layer_param in self.obs_layer[i]:
            obs_output = obs_layer_param(observed_data)
            with torch.no_grad():
                miss_output = obs_layer_param(missing_data)
            # Join back the data
            output = dynamic_stitch(condition_indices, [miss_output, obs_output])
            if len(self.obs_layer[i]) == 1:
                return output
            outputs.append(output)
        return outputs

class VariationalAutoencoder(nn.Module):
    def __init__(self, dims):
        """
        Variational Autoencoder [Kingma 2013] model
        consisting of an encoder/decoder pair for which
        a variational distribution is fitted to the
        encoder. Also known as the M1 model in [Kingma 2014].

        :param dims: x, z and hidden dimensions of the networks
        """
        super(VariationalAutoencoder, self).__init__()

        [x_dim, z_dim, h_dim] = dims
        self.z_dim = z_dim
        self.flow = None

        self.encoder = Encoder([x_dim, h_dim, z_dim])
        self.decoder = Decoder([z_dim, list(reversed(h_dim)), x_dim])
        self.kl_divergence = 0

        for m in self.modules():
            if isinstance(m, nn.Linear):
                init.xavier_normal_(m.weight.data)
                if m.bias is not None:
                    m.bias.data.zero_()

    def _kld(self, z, q_param, p_param=None):
        """
        Computes the KL-divergence of
        some element z.

        KL(q||p) = -∫ q(z) log [ p(z) / q(z) ]
                 = -E[log p(z) - log q(z)]

        :param z: sample from q-distribution
        :param q_param: (mu, log_var) of the q-distribution
        :param p_param: (mu, log_var) of the p-distribution
        :return: KL(q||p)
        """
        (mu, log_var) = q_param

        if self.flow is not None:
            f_z, log_det_z = self.flow(z)
            qz = log_gaussian(z, mu, log_var) - sum(log_det_z)
            z = f_z
        else:
            qz = log_gaussian(z, mu, log_var)

        if p_param is None:
            pz = log_standard_gaussian(z)
        else:
            (mu, log_var) = p_param
            pz = log_gaussian(z, mu, log_var)

        kl = qz - pz

        return kl

    def add_flow(self, flow):
        self.flow = flow

    def forward(self, x, y=None):
        """
        Runs a data point through the model in order
        to provide its reconstruction and q distribution
        parameters.

        :param x: input data
        :return: reconstructed input
        """
        z, z_mu, z_log_var = self.encoder(x)

        self.kl_divergence = self._kld(z, (z_mu, z_log_var))

        x_mu = self.decoder(z)

        return x_mu

    def sample(self, z):
        """
        Given z ~ N(0, I) generates a sample from
        the learned distribution based on p_θ(x|z).
        :param z: (torch.autograd.Variable) Random normal variable
        :return: (torch.autograd.Variable) generated sample
        """
        return self.decoder(z)

class GumbelAutoencoder(nn.Module):
    def __init__(self, dims, n_samples=100):
        super(GumbelAutoencoder, self).__init__()

        [x_dim, z_dim, h_dim] = dims
        self.z_dim = z_dim
        self.n_samples = n_samples

        self.encoder = Perceptron([x_dim, *h_dim])
        self.sampler = GumbelSoftmax(h_dim[-1], z_dim, n_samples)
        self.decoder = Perceptron([z_dim, *reversed(h_dim), x_dim], output_activation=F.sigmoid)

        self.kl_divergence = 0

        for m in self.modules():
            if isinstance(m, nn.Linear):
                init.xavier_normal_(m.weight.data)
                if m.bias is not None:
                    m.bias.data.zero_()

    def _kld(self, qz):
        k = Variable(torch.FloatTensor([self.z_dim]), requires_grad=False)
        kl = qz * (torch.log(qz + 1e-8) - torch.log(1.0 / k))
        kl = kl.view(-1, self.n_samples, self.z_dim)
        return torch.sum(torch.sum(kl, dim=1), dim=1)

    def forward(self, x, y=None, tau=1):
        x = self.encoder(x)

        sample, qz = self.sampler(x, tau)
        self.kl_divergence = self._kld(qz)

        x_mu = self.decoder(sample)

        return x_mu

    def sample(self, z):
        return self.decoder(z)

class LadderEncoder(nn.Module):
    def __init__(self, dims):
        """
        The ladder encoder differs from the standard encoder
        by using batch-normalization and LReLU activation.
        Additionally, it also returns the transformation x.

        :param dims: dimensions [input_dim, [hidden_dims], [latent_dims]].
        """
        super(LadderEncoder, self).__init__()
        [x_dim, h_dim, self.z_dim] = dims
        self.in_features = x_dim
        self.out_features = h_dim

        self.linear = nn.Linear(x_dim, h_dim)
        self.batchnorm = nn.BatchNorm1d(h_dim)
        self.sample = GaussianSample(h_dim, self.z_dim)

    def forward(self, x):
        x = self.linear(x)
        x = F.leaky_relu(self.batchnorm(x), 0.1)
        return x, self.sample(x)

class LadderDecoder(nn.Module):
    def __init__(self, dims):
        """
        The ladder dencoder differs from the standard encoder
        by using batch-normalization and LReLU activation.
        Additionally, it also returns the transformation x.

        :param dims: dimensions of the networks
            given by the number of neurons on the form
            [latent_dim, [hidden_dims], input_dim].
        """
        super(LadderDecoder, self).__init__()

        [self.z_dim, h_dim, x_dim] = dims

        self.linear1 = nn.Linear(x_dim, h_dim)
        self.batchnorm1 = nn.BatchNorm1d(h_dim)
        self.merge = GaussianMerge(h_dim, self.z_dim)

        self.linear2 = nn.Linear(x_dim, h_dim)
        self.batchnorm2 = nn.BatchNorm1d(h_dim)
        self.sample = GaussianSample(h_dim, self.z_dim)

    def forward(self, x, l_mu=None, l_log_var=None):
        if l_mu is not None:
            # Sample from this encoder layer and merge
            z = self.linear1(x)
            z = F.leaky_relu(self.batchnorm1(z), 0.1)

        # Sample from the decoder and send forward
        z = self.linear2(x)
        z = F.leaky_relu(self.batchnorm2(z), 0.1)
        z, p_mu, p_log_var = self.sample(z)

        if l_mu is None:
            return z

        return z, (q_z, (q_mu, q_log_var), (p_mu, p_log_var))

class LadderVariationalAutoencoder(VariationalAutoencoder):
    def __init__(self, dims):
        """
        Ladder Variational Autoencoder as described by
        [Sønderby 2016]. Adds several stochastic
        layers to improve the log-likelihood estimate.

        :param dims: x, z and hidden dimensions of the networks
        """
        [x_dim, z_dim, h_dim] = dims
        super(LadderVariationalAutoencoder, self).__init__([x_dim, z_dim[0], h_dim])

        neurons = [x_dim, *h_dim]
        encoder_layers = [LadderEncoder([neurons[i - 1], neurons[i], z_dim[i - 1]]) for i in range(1, len(neurons))]
        decoder_layers = [LadderDecoder([z_dim[i - 1], h_dim[i - 1], z_dim[i]]) for i in range(1, len(h_dim))][::-1]

        self.encoder = nn.ModuleList(encoder_layers)
        self.decoder = nn.ModuleList(decoder_layers)
        self.reconstruction = Decoder([z_dim[0], h_dim, x_dim])

        for m in self.modules():
            if isinstance(m, nn.Linear):
                init.xavier_normal_(m.weight.data)
                if m.bias is not None:
                    m.bias.data.zero_()

    def forward(self, x):
        # Gather latent representation
        # from encoders along with final z.
        latents = []
        for encoder in self.encoder:
            x, (z, mu, log_var) = encoder(x)
            latents.append((mu, log_var))

        latents = list(reversed(latents))

        self.kl_divergence = 0
        for i, decoder in enumerate([-1, *self.decoder]):
            # If at top, encoder == decoder,
            # use prior for KL.
            l_mu, l_log_var = latents[i]
            if i == 0:
                self.kl_divergence += self._kld(z, (l_mu, l_log_var))

            # Perform downward merge of information.
            else:
                z, kl = decoder(z, l_mu, l_log_var)
                self.kl_divergence += self._kld(*kl)

        x_mu = self.reconstruction(z)
        return x_mu

    def sample(self, z):
        for decoder in self.decoder:
            z = decoder(z)
        return self.reconstruction(z)

max_stars_repo_name: NightmareNyx/semi-supervised-pytorch

max_issues_repo_name: NightmareNyx/semi-supervised-pytorch

max_forks_repo_name: NightmareNyx/semi-supervised-pytorch


text:
import sys
sys.path.append('/home/kevinteng/Desktop/BrainTumourSegmentation')
import tensorflow as tf
import numpy as np
import os

def threeD_to_twoD02(save_dir, input_path, n):
    if not os.path.exists(save_dir):
        os.makedirs(save_dir)
    patients = sorted(os.listdir(input_path))
    n_patients = len(patients)
    stacks = n_patients // n
    remainder = n_patients % n
    for j in range(stacks + 1):
        np_stack00 = 0  # flush ram
        np_stack00 = []
        # divisible stack
        if j < stacks:
            n_end = n * (j + 1)
        # remainder
        else:
            n_end = n * j + remainder
        # debugger
        print("i: {} with n_end:{}".format(j, n_end))

        for idx in range(n * j, n_end):
            merge_path_01 = os.path.join(input_path + patients[idx])
            med_img = np.load(merge_path_01).astype(np.float32)
            for i in range(med_img.shape[0]):  # first dimension is the number of slices
                if np.max(med_img[i, :, :, 0]) == np.min(med_img[i, :, :, 0]):  # remove empty slices
                    continue
                else:
                    np_stack00.append(med_img[i])
        np_stack00 = tf.convert_to_tensor(np_stack00)
        np.save(save_dir + 'stack_{}.npy'.format(j), np.array(np_stack00))

input_path = "/home/kevinteng/Desktop/ssd02/BraTS2020_preprocessed05/Training_pre/"
save_dir = "/home/kevinteng/Desktop/ssd02/BraTS2020_stack05/"

if __name__ == "__main__":
    threeD_to_twoD02(save_dir, input_path, n=30)

ext: py

reason: import numpy


text:
import sys
import numpy as np
import pandas as pd
import os
from meta_data import CVMetaData

meta_data = CVMetaData(sys.argv)

# Read boundary files for VF gap
half_VT_width = 1.397
boundary_trace_dir = meta_data.working_dir + "/solid_trace/"
file_prefix = "BoundaryTrace-"
total_timesteps = len(os.listdir(boundary_trace_dir))
gap = np.zeros(total_timesteps + 1)
gap_time = np.zeros(total_timesteps + 1)
for index in range(1, total_timesteps + 1, 1):
    current_filename = f'{boundary_trace_dir}{file_prefix}{index:06}'
    current_data = pd.read_table(
        current_filename, sep=" ",
        header=0, names=["ID", "X", "Y", "P"], index_col="ID")
    gap[index] = 1.397 - max(current_data["Y"])
    gap_time[index] = index * 5e-7
    # Print progress
    total_length = 50
    progress = int(np.floor(index / (total_timesteps + 1) * total_length))
    output = f"Reading progress: ({index}/{total_timesteps + 1})["
    output = output + progress * "█" + (total_length - progress) * " " + "]"
    print(output, end='\r')
print()
print("Save data...")

# Save to a file
save_data = pd.DataFrame(data={"Time": gap_time, "Gap": gap})
save_data.to_csv(meta_data.working_dir + "/gap.csv", index=None)

max_stars_repo_name: yufeimi/CVplot

max_issues_repo_name: yufeimi/CVplot

max_forks_repo_name: yufeimi/CVplot


text:
import numpy as np
import pandas as pd
from scipy import stats
import statsmodels.api as sm

import seaborn as sns
import matplotlib.cm as cm
import matplotlib.pyplot as plt
from matplotlib.ticker import ScalarFormatter

from functools import wraps

from . import utils
from . import performance as perf

from .plot_utils import (
    print_table, customize, ICTS, ICHIST, ICQQ, QRETURNBAR, QRETURNVIOLIN,
    QRETURNTS, ICGROUP, AUTOCORR, TBTURNOVER, ICHEATMAP, CUMRET, TDCUMRET,
    CUMRETQ, AVGCUMRET, EVENTSDIST, MISSIINGEVENTSDIST
)

DECIMAL_TO_BPS = 10000

def plotting_context(context='notebook', font_scale=1.5, rc=None):
    """
    Create alphalens default plotting style context.

    Under the hood, calls and returns seaborn.plotting_context() with
    some custom settings. Usually you would use in a with-context.

    Parameters
    ----------
    context : str, optional
        Name of seaborn context.
    font_scale : float, optional
        Scale font by factor font_scale.
    rc : dict, optional
        Config flags.
        By default, {'lines.linewidth': 1.5}
        is being used and will be added to any
        rc passed in, unless explicitly overriden.

    Returns
    -------
    seaborn plotting context

    Example
    -------
    with alphalens.plotting.plotting_context(font_scale=2):
        alphalens.create_full_tear_sheet(..., set_context=False)

    See also
    --------
    For more information, see seaborn.plotting_context().
    """
    if rc is None:
        rc = {}

    rc_default = {'lines.linewidth': 1.5}

    # Add defaults if they do not exist
    for name, val in rc_default.items():
        rc.setdefault(name, val)

    return sns.plotting_context(context=context, font_scale=font_scale, rc=rc)

def axes_style(style='darkgrid', rc=None):
    """Create alphalens default axes style context.

    Under the hood, calls and returns seaborn.axes_style() with
    some custom settings. Usually you would use in a with-context.

    Parameters
    ----------
    style : str, optional
        Name of seaborn style.
    rc : dict, optional
        Config flags.

    Returns
    -------
    seaborn plotting context

    Example
    -------
    with alphalens.plotting.axes_style(style='whitegrid'):
        alphalens.create_full_tear_sheet(..., set_context=False)

    See also
    --------
    For more information, see seaborn.plotting_context().

    """
    if rc is None:
        rc = {}

    rc_default = {}

    # Add defaults if they do not exist
    for name, val in rc_default.items():
        rc.setdefault(name, val)

    return sns.axes_style(style=style, rc=rc)

def plot_returns_table(alpha_beta,
                       mean_ret_quantile,
                       mean_ret_spread_quantile):
    returns_table = pd.DataFrame()
    returns_table = returns_table.append(alpha_beta)
    returns_table.loc["Mean Period Wise Return Top Quantile (bps)"] = \
        mean_ret_quantile.iloc[-1] * DECIMAL_TO_BPS
    returns_table.loc["Mean Period Wise Return Bottom Quantile (bps)"] = \
        mean_ret_quantile.iloc[0] * DECIMAL_TO_BPS
    returns_table.loc["Mean Period Wise Spread (bps)"] = \
        mean_ret_spread_quantile.mean() * DECIMAL_TO_BPS

    print("收益分析")
    print_table(returns_table.apply(lambda x: x.round(3)))

def plot_turnover_table(autocorrelation_data, quantile_turnover):
    turnover_table = pd.DataFrame()

    for ret_name in quantile_turnover.keys():
        for quantile, p_data in quantile_turnover[ret_name].iteritems():
            turnover_table.loc["Quantile {} Mean Turnover ".format(quantile),
                               "{}".format(ret_name)] = p_data.mean()

    auto_corr = pd.DataFrame()
    for ret_name, p_data in autocorrelation_data.items():
        auto_corr.loc["Mean Factor Rank Autocorrelation",
                      "{}".format(ret_name)] = p_data.mean()

    print("换手率分析")
    print_table(turnover_table.apply(lambda x: x.round(3)))
    print_table(auto_corr.apply(lambda x: x.round(3)))

def plot_information_table(ic_data):
    ic_summary_table = pd.DataFrame()
    ic_summary_table["IC Mean"] = ic_data.mean()
    ic_summary_table["IC Std."] = ic_data.std()
    ic_summary_table["Risk-Adjusted IC"] = \
        ic_data.mean() / ic_data.std()
    t_stat, p_value = stats.ttest_1samp(ic_data, 0)
    ic_summary_table["t-stat(IC)"] = t_stat
    ic_summary_table["p-value(IC)"] = p_value
    ic_summary_table["IC Skew"] = stats.skew(ic_data)
    ic_summary_table["IC Kurtosis"] = stats.kurtosis(ic_data)

    print("IC 分析")
    print_table(ic_summary_table.apply(lambda x: x.round(3)).T)

def plot_quantile_statistics_table(factor_data):
    quantile_stats = factor_data.groupby('factor_quantile') \
        .agg(['min', 'max', 'mean', 'std', 'count'])['factor']
    quantile_stats['count %'] = quantile_stats['count'] \
        / quantile_stats['count'].sum() * 100.

    print("分位数统计")
    utils.print_table(quantile_stats)

def plot_ic_ts(ic, ax=None):
    """
    Plots Spearman Rank Information Coefficient and IC moving
    average for a given factor.

    Parameters
    ----------
    ic : pd.DataFrame
        DataFrame indexed by date, with IC for each forward return.
    ax : matplotlib.Axes, optional
        Axes upon which to plot.

    Returns
    -------
    ax : matplotlib.Axes
        The axes that were plotted on.
    """
    ic = ic.copy()

    num_plots = len(ic.columns)
    if ax is None:
        f, ax = plt.subplots(num_plots, 1, figsize=(18, num_plots * 7))
        ax = np.asarray([ax]).flatten()

    ymin, ymax = (None, None)
    for a, (period_num, ic) in zip(ax, ic.iteritems()):
        ic.plot(alpha=0.7, ax=a, lw=0.7, color='steelblue')
        ic.rolling(window=22).mean().plot(
            ax=a,
            color='forestgreen',
            lw=2,
            alpha=0.8
        )

        a.axhline(0.0, linestyle='-', color='black', lw=1, alpha=0.8)
        a.set(ylabel='IC', xlabel="")
        a.set_title(ICTS.get("TITLE").format(period_num))
        a.legend(ICTS.get("LEGEND"), loc='upper right')
        a.text(
            .05,
            .95,
            ICTS.get("TEXT").format(ic.mean(), ic.std()),
            fontsize=16,
            bbox={
                'facecolor': 'white',
                'alpha': 1,
                'pad': 5
            },
            transform=a.transAxes,
            verticalalignment='top'
        )

        curr_ymin, curr_ymax = a.get_ylim()
        ymin = curr_ymin if ymin is None else min(ymin, curr_ymin)
        ymax = curr_ymax if ymax is None else max(ymax, curr_ymax)

    for a in ax:
        a.set_ylim([ymin, ymax])

    return ax

@customize
def plot_ic_hist(ic, ax=None):
    """
    Plots Spearman Rank Information Coefficient histogram for a given factor.

    Parameters
    ----------
    ic : pd.DataFrame
        DataFrame indexed by date, with IC for each forward return.
    ax : matplotlib.Axes, optional
        Axes upon which to plot.

    Returns
    -------
    ax : matplotlib.Axes
        The axes that were plotted on.
    """

    ic = ic.copy()

    num_plots = len(ic.columns)

    v_spaces = ((num_plots - 1) // 3) + 1

    if ax is None:
        f, ax = plt.subplots(v_spaces, 3, figsize=(18, v_spaces * 6))
        ax = ax.flatten()

    for a, (period_num, ic) in zip(ax, ic.iteritems()):
        sns.distplot(ic.replace(np.nan, 0.), norm_hist=True, ax=a)
        a.set(title=ICHIST.get("TITLE") % period_num, xlabel='IC')
        a.text(
            .05,
            .95,
            ICHIST.get("LEGEND").format(ic.mean(), ic.std()),
            fontsize=16,
            bbox={
                'facecolor': 'white',
                'alpha': 1,
                'pad': 5
            },
            transform=a.transAxes,
            verticalalignment='top'
        )
        a.axvline(ic.mean(), color='w', linestyle='dashed', linewidth=2)

    if num_plots < len(ax):
        for a in ax[num_plots:]:
            a.set_visible(False)

    return ax

@customize
def plot_ic_qq(ic, theoretical_dist=stats.norm, ax=None):
    """
    Plots Spearman Rank Information Coefficient "Q-Q" plot relative to
    a theoretical distribution.

    Parameters
    ----------
    ic : pd.DataFrame
        DataFrame indexed by date, with IC for each forward return.
    theoretical_dist : scipy.stats._continuous_distns
        Continuous distribution generator. scipy.stats.norm and
        scipy.stats.t are popular options.
    ax : matplotlib.Axes, optional
        Axes upon which to plot.

    Returns
    -------
    ax : matplotlib.Axes
        The axes that were plotted on.
    """

    ic = ic.copy()

    num_plots = len(ic.columns)
    v_spaces = ((num_plots - 1) // 3) + 1

    if ax is None:
        f, ax = plt.subplots(v_spaces, 3, figsize=(18, v_spaces * 6))
        ax = ax.flatten()

    if isinstance(theoretical_dist, stats.norm.__class__):
        dist_name = ICQQ.get("NORM")
    elif isinstance(theoretical_dist, stats.t.__class__):
        dist_name = ICQQ.get("T")
    else:
        dist_name = ICQQ.get("CUSTOM")

    for a, (period, ic) in zip(ax, ic.iteritems()):
        period_num = period.replace('period_', '')
        sm.qqplot(
            ic.replace(np.nan, 0.).values,
            theoretical_dist,
            fit=True,
            line='45',
            ax=a
        )
        a.set(
            title=ICQQ.get("TITLE").format(period_num, dist_name),
            xlabel=ICQQ.get("XLABEL").format(dist_name),
            ylabel=ICQQ.get("YLABEL"),
        )

    if num_plots < len(ax):
        for a in ax[num_plots:]:
            a.set_visible(False)

    return ax

@customize
def plot_quantile_returns_bar(mean_ret_by_q,
                              by_group=False,
                              ylim_percentiles=None,
                              ax=None):
    """
    Plots mean period wise returns for factor quantiles.

    Parameters
    ----------
    mean_ret_by_q : pd.DataFrame
        DataFrame with quantile, (group) and mean period wise return values.
    by_group : bool
        Disaggregated figures by group.
    ylim_percentiles : tuple of integers
        Percentiles of observed data to use as y limits for plot.
    ax : matplotlib.Axes, optional
        Axes upon which to plot.

    Returns
    -------
    ax : matplotlib.Axes
        The axes that were plotted on.
    """

    mean_ret_by_q = mean_ret_by_q.copy()

    if ylim_percentiles is not None:
        ymin = (np.nanpercentile(mean_ret_by_q.values,
                                 ylim_percentiles[0]) * DECIMAL_TO_BPS)
        ymax = (np.nanpercentile(mean_ret_by_q.values,
                                 ylim_percentiles[1]) * DECIMAL_TO_BPS)
    else:
        ymin = None
        ymax = None

    if by_group:
        num_group = len(
            mean_ret_by_q.index.get_level_values('group').unique())

        if ax is None:
            v_spaces = ((num_group - 1) // 2) + 1
            f, ax = plt.subplots(v_spaces, 2, sharex=False,
                                 sharey=True, figsize=(18, 6 * v_spaces))
            ax = ax.flatten()

        for a, (sc, cor) in zip(ax, mean_ret_by_q.groupby(level='group')):
            (cor.xs(sc, level='group')
                .multiply(DECIMAL_TO_BPS)
                .plot(kind='bar', title=sc, ax=a))

            a.set(xlabel='', ylabel=QRETURNBAR.get("YLABEL"), ylim=(ymin, ymax))

        if num_group < len(ax):
            ax[-1].set_visible(False)

        return ax

    else:
        if ax is None:
            f, ax = plt.subplots(1, 1, figsize=(18, 6))

        mean_ret_by_q.multiply(DECIMAL_TO_BPS).plot(
            kind='bar', title=QRETURNBAR.get("TITLE"), ax=ax
        )
        ax.set(xlabel="", ylabel=QRETURNBAR.get("YLABEL"), ylim=(ymin, ymax))
        return ax

@customize
def plot_quantile_returns_violin(return_by_q,
                                 ylim_percentiles=None,
                                 ax=None):
    """
    Plots a violin box plot of period wise returns for factor quantiles.

    Parameters
    ----------
    return_by_q : pd.DataFrame - MultiIndex
        DataFrame with date and quantile as rows MultiIndex,
        forward return windows as columns, returns as values.
    ylim_percentiles : tuple of integers
        Percentiles of observed data to use as y limits for plot.
    ax : matplotlib.Axes, optional
        Axes upon which to plot.

    Returns
    -------
    ax : matplotlib.Axes
        The axes that were plotted on.
    """

    return_by_q = return_by_q.copy()

    if ylim_percentiles is not None:
        ymin = (np.nanpercentile(return_by_q.values,
                                 ylim_percentiles[0]) * DECIMAL_TO_BPS)
        ymax = (np.nanpercentile(return_by_q.values,
                                 ylim_percentiles[1]) * DECIMAL_TO_BPS)
    else:
        ymin = None
        ymax = None

    if ax is None:
        f, ax = plt.subplots(1, 1, figsize=(18, 6))

    unstacked_dr = (return_by_q
                    .multiply(DECIMAL_TO_BPS))
    unstacked_dr.columns = unstacked_dr.columns.set_names('forward_periods')
    unstacked_dr = unstacked_dr.stack()
    unstacked_dr.name = 'return'
    unstacked_dr = unstacked_dr.reset_index()

    sns.violinplot(
        data=unstacked_dr,
        x='factor_quantile',
        hue='forward_periods',
        y='return',
        orient='v',
        cut=0,
        inner='quartile',
        ax=ax
    )

    ax.set(
        xlabel='',
        ylabel=QRETURNVIOLIN.get("YLABEL"),
        title=QRETURNVIOLIN.get("TITLE"),
        ylim=(ymin, ymax)
    )

    ax.axhline(0.0, linestyle='-', color='black', lw=0.7, alpha=0.6)

    return ax

@customize
def plot_mean_quantile_returns_spread_time_series(mean_returns_spread,
                                                  std_err=None,
                                                  bandwidth=1,
                                                  ax=None):
    """
    Plots mean period wise returns for factor quantiles.

    Parameters
    ----------
    mean_returns_spread : pd.Series
        Series with difference between quantile mean returns by period.
    std_err : pd.Series
        Series with standard error of difference between quantile
        mean returns each period.
    bandwidth : float
        Width of displayed error bands in standard deviations.
    ax : matplotlib.Axes, optional
        Axes upon which to plot.

    Returns
    -------
    ax : matplotlib.Axes
        The axes that were plotted on.
    """

    if isinstance(mean_returns_spread, pd.DataFrame):
        if ax is None:
            ax = [None for a in mean_returns_spread.columns]

        ymin, ymax = (None, None)
        for (i, a), (name, fr_column) in zip(enumerate(ax),
                                             mean_returns_spread.iteritems()):
            stdn = None if std_err is None else std_err[name]
            a = plot_mean_quantile_returns_spread_time_series(fr_column,
                                                              std_err=stdn,
                                                              ax=a)
            ax[i] = a
            curr_ymin, curr_ymax = a.get_ylim()
            ymin = curr_ymin if ymin is None else min(ymin, curr_ymin)
            ymax = curr_ymax if ymax is None else max(ymax, curr_ymax)

        for a in ax:
            a.set_ylim([ymin, ymax])

        return ax

    if mean_returns_spread.isnull().all():
        return ax

    periods = mean_returns_spread.name

    title = QRETURNTS.get(
        "TITLE"
    ).format(periods.replace('period_', '') if periods is not None else '')

    if ax is None:
        f, ax = plt.subplots(figsize=(18, 6))

    mean_returns_spread_bps = mean_returns_spread * DECIMAL_TO_BPS

    mean_returns_spread_bps.plot(alpha=0.4, ax=ax, lw=0.7, color='forestgreen')
    mean_returns_spread_bps.rolling(window=22).mean().plot(
        color='orangered',
        alpha=0.7,
        ax=ax
    )
    ax.legend(
        [QRETURNTS.get("LEGEND0").format(bandwidth),
         QRETURNTS.get("LEGEND1")],
        loc='upper right'
    )

    if std_err is not None:
        std_err_bps = std_err * DECIMAL_TO_BPS
        upper = mean_returns_spread_bps.values + (std_err_bps * bandwidth)
        lower = mean_returns_spread_bps.values - (std_err_bps * bandwidth)
        ax.fill_between(mean_returns_spread.index,
                        lower,
                        upper,
                        alpha=0.3,
                        color='steelblue')

    ylim = np.nanpercentile(abs(mean_returns_spread_bps.values), 95)
    ax.set(
        ylabel=QRETURNTS.get("YLABEL"),
        xlabel="",
        title=title,
        ylim=(-ylim, ylim)
    )
    ax.axhline(0.0, linestyle='-', color='black', lw=1, alpha=0.8)

    return ax

@customize
def plot_ic_by_group(ic_group, ax=None):
    """
    Plots Spearman Rank Information Coefficient for a given factor over
    provided forward returns. Separates by group.

    Parameters
    ----------
    ic_group : pd.DataFrame
        group-wise mean period wise returns.
    ax : matplotlib.Axes, optional
        Axes upon which to plot.

    Returns
    -------
    ax : matplotlib.Axes
        The axes that were plotted on.
    """
    if ax is None:
        f, ax = plt.subplots(1, 1, figsize=(18, 6))
    ic_group.plot(kind='bar', ax=ax)

    ax.set(title=ICGROUP.get("TITLE"), xlabel="")
    ax.set_xticklabels(ic_group.index, rotation=45)

    return ax

@customize
def plot_factor_rank_auto_correlation(factor_autocorrelation,
                                      ret_type,
                                      ax=None):
    """
    Plots factor rank autocorrelation over time.
    See factor_rank_autocorrelation for more details.

    Parameters
    ----------
    factor_autocorrelation : pd.Series
        Rolling 1 period (defined by time_rule) autocorrelation
        of factor values.
    ret_type:
        return type
    ax : matplotlib.Axes, optional
        Axes upon which to plot.

    Returns
    -------
    ax : matplotlib.Axes
        The axes that were plotted on.
    """
    if ax is None:
        f, ax = plt.subplots(1, 1, figsize=(18, 6))

    factor_autocorrelation.plot(title=AUTOCORR.get("TITLE").format(ret_type), ax=ax)
    ax.set(ylabel=AUTOCORR.get("YLABEL").format(ret_type), xlabel="")
    ax.axhline(0.0, linestyle='-', color='black', lw=1)
    ax.text(
        .05,
        .95,
        AUTOCORR.get("TEXT").format(factor_autocorrelation.mean()),
        fontsize=16,
        bbox={
            'facecolor': 'white',
            'alpha': 1,
            'pad': 5
        },
        transform=ax.transAxes,
        verticalalignment='top'
    )

    return ax

@customize
def plot_top_bottom_quantile_turnover(quantile_turnover, ret_type, ax=None):
    """
    Plots period wise top and bottom quantile factor turnover.

    Parameters
    ----------
    quantile_turnover: pd.Dataframe
        Quantile turnover (each DataFrame column a quantile).
    period: int, optional
        Period over which to calculate the turnover.
    ax : matplotlib.Axes, optional
        Axes upon which to plot.

    Returns
    -------
    ax : matplotlib.Axes
        The axes that were plotted on.
    """
    if ax is None:
        f, ax = plt.subplots(1, 1, figsize=(18, 6))

    max_quantile = quantile_turnover.columns.max()
    min_quantile = quantile_turnover.columns.min()
    from IPython.display import display

    turnover = pd.DataFrame()
    turnover[TBTURNOVER.get("TURNOVER").format(max_quantile)
             ] = quantile_turnover[max_quantile]
    turnover[TBTURNOVER.get("TURNOVER").format(min_quantile)
             ] = quantile_turnover[min_quantile]
    turnover.plot(
        title=TBTURNOVER.get("TITLE").format(ret_type), ax=ax, alpha=0.6, lw=0.8
    )

    ax.set(ylabel=TBTURNOVER.get("YLABEL"), xlabel="")

    return ax

@customize
def plot_monthly_ic_heatmap(mean_monthly_ic, ax=None):
    """
    Plots a heatmap of the information coefficient or returns by month.

    Parameters
    ----------
    mean_monthly_ic : pd.DataFrame
        The mean monthly IC for N periods forward.

    Returns
    -------
    ax : matplotlib.Axes
        The axes that were plotted on.
    """

    mean_monthly_ic = mean_monthly_ic.copy()

    num_plots = len(mean_monthly_ic.columns)

    v_spaces = ((num_plots - 1) // 3) + 1

    if ax is None:
        f, ax = plt.subplots(v_spaces, 3, figsize=(18, v_spaces * 6))
        ax = ax.flatten()

    new_index_year = []
    new_index_month = []
    for date in mean_monthly_ic.index:
        new_index_year.append(date.year)
        new_index_month.append(date.month)

    mean_monthly_ic.index = pd.MultiIndex.from_arrays(
        [new_index_year, new_index_month],
        names=["year", "month"])

    for a, (periods_num, ic) in zip(ax, mean_monthly_ic.iteritems()):

        sns.heatmap(
            ic.unstack(),
            annot=True,
            alpha=1.0,
            center=0.0,
            annot_kws={"size": 7},
            linewidths=0.01,
            linecolor='white',
            cmap=cm.coolwarm_r,
            cbar=False,
            ax=a)
        a.set(ylabel='', xlabel='')

        a.set_title(ICHEATMAP.get("TITLE").format(periods_num))

    if num_plots < len(ax):
        ax[-1].set_visible(False)

    return ax

@customize
def plot_cumulative_returns(factor_returns,
                            period,
                            freq=None,
                            title=None,
                            ax=None):
    """
    Plots the cumulative returns of the returns series passed in.

    Parameters
    ----------
    factor_returns : pd.Series
        Period wise returns of dollar neutral portfolio weighted by factor
        value.
    period : pandas.Timedelta or string
        Length of period for which the returns are computed (e.g. 1 day)
        if 'period' is a string it must follow pandas.Timedelta constructor
        format (e.g. '1 days', '1D', '30m', '3h', '1D1h', etc)
    freq : pandas DateOffset
        Used to specify a particular trading calendar e.g. BusinessDay or Day
        Usually this is inferred from utils.infer_trading_calendar, which is
        called by either get_clean_factor_and_forward_returns or
        compute_forward_returns
    title: string, optional
        Custom title
    ax : matplotlib.Axes, optional
        Axes upon which to plot.

    Returns
    -------
    ax : matplotlib.Axes
        The axes that were plotted on.
    """
    if ax is None:
        f, ax = plt.subplots(1, 1, figsize=(18, 6))

    factor_returns = factor_returns.resample(freq).first()
    factor_returns = perf.cumulative_returns(factor_returns)
    factor_returns.plot(ax=ax, lw=3, color='forestgreen', alpha=0.6)
    ax.set(
        ylabel=CUMRET.get("YLABEL"),
        title=CUMRET.get("TITLE").format(period),
        xlabel=""
    )
    ax.axhline(1.0, linestyle='-', color='black', lw=1)

    return ax

@customize
def plot_cumulative_returns_by_quantile(quantile_returns,
                                        period,
                                        freq=None,
                                        ax=None,
                                        title=None):
    """
    Plots the cumulative returns of various factor quantiles.

    Parameters
    ----------
    quantile_returns : pd.DataFrame
        Returns by factor quantile
    period : pandas.Timedelta or string
        Length of period for which the returns are computed (e.g. 1 day)
        if 'period' is a string it must follow pandas.Timedelta constructor
        format (e.g. '1 days', '1D', '30m', '3h', '1D1h', etc)
    freq : pandas DateOffset
        Used to specify a particular trading calendar e.g. BusinessDay or Day
        Usually this is inferred from utils.infer_trading_calendar, which is
        called by either get_clean_factor_and_forward_returns or
        compute_forward_returns
    ax : matplotlib.Axes, optional
        Axes upon which to plot.

    Returns
    -------
    ax : matplotlib.Axes
    """

    if ax is None:
        f, ax = plt.subplots(1, 1, figsize=(18, 6))

    ret_wide = quantile_returns.unstack('factor_quantile')

    quantiles = ret_wide.columns
    ret_wide['top_bottom'] = ret_wide[max(
        quantiles)] - ret_wide[min(quantiles)]
    if ret_wide['top_bottom'].sum() < 0:
        ret_wide['top_bottom'] = -ret_wide['top_bottom']

    # from IPython.display import display
    # display(ret_wide)
    ret_wide = ret_wide.reset_index().set_index('date')
    ret_wide = ret_wide.resample(freq).first()
    cum_ret = ret_wide.apply(perf.cumulative_returns)

    cum_ret = cum_ret.loc[:, ::-1]  # we want negative quantiles as 'red'

    cum_ret.plot(lw=2, ax=ax, cmap=cm.coolwarm)
    ax.legend()

    ax.set(
        ylabel=TDCUMRET.get("YLABEL"),
        title=TDCUMRET.get("TITLE").format(period),
        xlabel=""
    )

    ax.yaxis.set_major_formatter(ScalarFormatter())
    ax.axhline(1.0, linestyle='-', color='black', lw=1)

    return ax

@customize
def plot_quantile_average_cumulative_return(
    avg_cumulative_returns,
    by_quantile=False,
    std_bar=False,
    ax=None,
    periods_before='',
    periods_after=''
):

    avg_cumulative_returns = avg_cumulative_returns.multiply(DECIMAL_TO_BPS)
    quantiles = len(avg_cumulative_returns.index.levels[0].unique())
    palette = [cm.RdYlGn_r(i) for i in np.linspace(0, 1, quantiles)]

    if by_quantile:

        if ax is None:
            v_spaces = ((quantiles - 1) // 2) + 1
            f, ax = plt.subplots(
                v_spaces,
                2,
                sharex=False,
                sharey=False,
                figsize=(18, 6 * v_spaces)
            )
            ax = ax.flatten()

        for i, (quantile, q_ret) in enumerate(
            avg_cumulative_returns.groupby(level='factor_quantile')
        ):

            mean = q_ret.loc[(quantile, 'mean')]
            mean.name = AVGCUMRET.get("COLUMN").format(quantile)
            mean.plot(ax=ax[i], color=palette[i])
            ax[i].set_ylabel(AVGCUMRET.get("YLABEL"))

            if std_bar:
                std = q_ret.loc[(quantile, 'std')]
                ax[i].errorbar(
                    std.index,
                    mean,
                    yerr=std,
                    fmt='none',
                    ecolor=palette[i],
                    label=None
                )

            ax[i].axvline(x=0, color='k', linestyle='--')
            ax[i].legend()
            i += 1

    else:

        if ax is None:
            f, ax = plt.subplots(1, 1, figsize=(18, 6))

        for i, (quantile, q_ret) in enumerate(
            avg_cumulative_returns.groupby(level='factor_quantile')
        ):

            mean = q_ret.loc[(quantile, 'mean')]
            mean.name = AVGCUMRET.get("COLUMN").format(quantile)
            mean.plot(ax=ax, color=palette[i])

            if std_bar:
                std = q_ret.loc[(quantile, 'std')]
                ax.errorbar(
                    std.index,
                    mean,
                    yerr=std,
                    fmt='none',
                    ecolor=palette[i],
                    label=None
                )
            i += 1

        ax.axvline(x=0, color='k', linestyle='--')
        ax.legend()
        ax.set(
            title=AVGCUMRET.get("YLABEL").format(periods_before, periods_after),
            xlabel=AVGCUMRET.get("XLABEL"),
            ylabel=AVGCUMRET.get("YLABEL"),
        )

    return ax

@customize
def plot_events_distribution(events, num_days=5, full_dates=None, ax=None):

    if ax is None:
        f, ax = plt.subplots(1, 1, figsize=(18, 6))

    if full_dates is None:
        full_dates = events.index.get_level_values('date').unique()

    group = pd.Series(range(len(full_dates)), index=full_dates) // num_days
    grouper_label = group.drop_duplicates()
    grouper = group.reindex(events.index.get_level_values('date'))

    count = events.groupby(grouper.values).count()
    count = count.reindex(grouper_label.values, fill_value=0)
    count.index = grouper_label.index.map(lambda x: x.strftime('%Y-%m-%d'))
    count.plot(kind="bar", grid=False, ax=ax)

    def annotateBars(x, dt, ax=ax):
        color = 'black'
        vertalign = 'top'
        ax.text(
            x,
            count.loc[dt],
            "{:d}".format(count.loc[dt]),
            rotation=45,
            color=color,
            horizontalalignment='center',
            verticalalignment=vertalign,
            fontsize=15,
            weight='heavy'
        )

    [annotateBars(x, dt, ax=ax) for x, dt in enumerate(list(count.index))]
    ax.set(
        ylabel=EVENTSDIST.get("YLABEL"),
        title=EVENTSDIST.get("TITLE"),
        xlabel=EVENTSDIST.get("XLABEL"),
    )
    return ax

@customize
def plot_missing_events_distribution(
    events, num_days=5, full_dates=None, ax=None
):

    if ax is None:
        f, ax = plt.subplots(1, 1, figsize=(18, 6))

    if full_dates is None:
        full_dates = events.index.get_level_values('date').unique()

    daily_count = events.groupby(level='date').count()
    most_common_count = np.argmax(np.bincount(daily_count))
    daily_missing = daily_count / most_common_count - 1
    daily_missing = daily_missing.reindex(full_dates, fill_value=-1.0)

    grouper = pd.Series(range(len(full_dates)), index=full_dates) // num_days
    grouper_label = grouper.drop_duplicates()

    missing = daily_missing.groupby(grouper.values).mean()
    missing = missing.reindex(grouper_label.values, fill_value=-1.0)
    missing.index = grouper_label.index.map(lambda x: x.strftime('%Y-%m-%d'))
    missing.plot(kind="bar", grid=False, ax=ax)

    def annotateBars(x, dt, ax=ax):
        color = 'black'
        vertalign = 'top'
        ax.text(
            x,
            missing.loc[dt],
            "{:+.1f}%".format(missing.loc[dt] * 100),
            rotation=45,
            color=color,
            horizontalalignment='center',
            verticalalignment=vertalign,
            fontsize=15,
            weight='heavy'
        )

    [annotateBars(x, dt, ax=ax) for x, dt in enumerate(list(missing.index))]
    ax.set(
        ylabel=MISSIINGEVENTSDIST.get("YLABEL"),
        title=MISSIINGEVENTSDIST.get("TITLE"),
        xlabel=MISSIINGEVENTSDIST.get("XLABEL")
    )

    return ax

reason: import numpy,from scipy,import statsmodels


text:
# General modules
import pandas as pd
import numpy as np
from itertools import groupby
from functools import reduce

def ReadData(x):
    """
    Specifies the rows of the rawdata file x and extracts the raw data for a 192.192 IFC.
    """

    # Set the index within read_csv
    gaps = 2
    top_header = 13
    datasets = 4
    rows = 4608

    # Autocalculating header size here, so easier to fix if format changes
    headers = [top_header + (rows+gaps)*(i+1) for i in range((datasets))]

    xprobe = pd.read_csv(x, header = headers[1], nrows = rows, index_col = "Chamber ID")
    xref = pd.read_csv(x, header = headers[0], nrows = rows, index_col = "Chamber ID")
    xprobeb = pd.read_csv(x, header = headers[3], nrows = rows, index_col = "Chamber ID")
    xrefb = pd.read_csv(x, header = headers[2], nrows = rows, index_col = "Chamber ID")

    # clean up the data
    for df in [xprobe, xref, xprobeb, xrefb]:
        _drop_lastcolumn(df)
        _format_columns(df)

    return xprobe, xref, xprobeb, xrefb

def _drop_lastcolumn(x):
    """
    drops the last column of a dataframe
    """
    x.drop(x.columns[-1], axis=1, inplace=True)

def _format_columns(x):
    """
    renames the column x by adding a t
    """
    x.columns = ['t' + str(col).lstrip() for col in x.columns]

def LabelInputs(x, y):
    # with x as the signal dataframe
    x.reset_index(inplace=True)
    splitassignment = x['Chamber ID'].str.split("-", n=1, expand=True)
    x["sampleID"] = splitassignment[0]
    x["assayID"] = splitassignment[1]
    x.set_index('Chamber ID', inplace=True)
    # assigns labels from layout sheet y to df x
    s_layout =  pd.read_excel(y, sheet_name='layout_samples', dtype=str)
    a_layout =  pd.read_excel(y, sheet_name='layout_assays', dtype=str)
    # create dictionary with assay or sample numbers and their name
    assays = pd.read_excel(y, sheet_name='assays')
    samples = pd.read_excel(y, sheet_name='samples')
    a_dict = dict(zip(a_layout.values.reshape(-1), assays.values.reshape(-1)))
    s_dict = dict(zip(s_layout.values.reshape(-1), samples.values.reshape(-1)))
    # map
    x['assay'] = x['assayID'].map(a_dict)
    x['sample'] = x['sampleID'].map(s_dict)

def ItemsToList(x, sheet, sort = 'original'):
    y = pd.read_excel(x, sheet_name = sheet)
    if sheet == 'assays':
        new_array = np.stack(y[['C1', 'C2', 'C3']].values, axis=-1)
    if sheet == 'samples':
        new_array = np.stack(y[['C1', 'C2', 'C3', 'C4', 'C5', 'C6', \
                                'C7', 'C8', 'C9', 'C10', 'C11', 'C12', \
                                'C13', 'C14', 'C15', 'C16', 'C17', 'C18', \
                                'C19', 'C20', 'C21', 'C22', 'C23', 'C24']].values, axis=-1)
    itemlist =  np.concatenate(new_array).tolist()
    if sort == 'alphabetical':
        itemlist = np.unique(itemlist)
    if sort == 'original':
        #itemlist = [x[0] for x in groupby(itemlist)]
        itemlist = reduce(lambda l, x: l+[x] if x not in l else l, itemlist, [])

    return itemlist

max_stars_repo_name: broadinstitute/carmen-rvp

max_issues_repo_name: broadinstitute/carmen-rvp

max_forks_repo_name: broadinstitute/carmen-rvp


text:
import sys
from transformers.modeling_openai import OpenAIGPTLMHeadAgenModel
import numpy as np
from transformers import *
import torch
from torch.utils.data import DataLoader
from generate_ivp import sample_sequence_ivp
import pandas as pd
from utils import *
from utils_g import *
from utils_ivp import agen_vector
import torch.nn.functional as F
from torch.nn import CrossEntropyLoss
from examples.run_generation import *

ps = [0.4]
BETA = 5
cats = {
    'pos': 0,
    'equal': 1,
    'neg': 2
}

def gen_p(model, test_dataset, descat, cat_head, cats):
    outlist = []
    outp = []
    sens = []
    for i in ps:
        for j in range(len(test_dataset)):
            sen = test_dataset[j]
            cat = '<' + cats[j] + '>'
            sen.extend(tokenizer_g.encode(cat))
            sen.append(tokenizer_g.bos_token_id)
            senlen = len(sen)
            e = torch.FloatTensor(descat[j]).to(device_g)
            # e [1,3]
            out = sample_sequence(
                model=model,
                context=sen,
                length=max_sen_len,
                top_p=i,
                e=e,
                cat_head=cat_head,
                beta=BETA,
                device=device_g
            )
            out = out[0, senlen:].tolist()
            text = tokenizer_g.decode(out, clean_up_tokenization_spaces=True, skip_special_tokens=False)
            sen = tokenizer_g.decode(sen, clean_up_tokenization_spaces=True, skip_special_tokens=False)
            end_ind = text.find('<')
            if end_ind >= 0:
                text = text[0: end_ind]
            sens.append(sen)
            outlist.append(text)
            outp.append(i)
    return outlist, outp, sens

def add_cat(orisen, dataset):
    resds = []
    descat = []
    orids = []
    es = np.zeros((1, 3))
    es[0][0] = 1
    descat = ['pos'] * len(dataset)
    es = np.repeat(es, len(dataset), axis=0)
    eq = np.zeros((1, 3))
    eq[0][1] = 1
    tem = ['equal'] * len(dataset)
    descat.extend(tem)
    eqs = np.repeat(eq, len(dataset), axis=0)
    es = np.append(es, eqs, axis=0)
    en = np.zeros((1, 3))
    en[0][2] = 1
    tem = ['neg'] * len(dataset)
    descat.extend(tem)
    ens = np.repeat(en, len(dataset), axis=0)
    es = np.append(es, ens, axis=0)
    for i in range(3):
        orids.extend(orisen)
        resds.extend(dataset)
    return orids, resds, es, descat

def gen_roc(model, cat_head):
    orisen, test_dataset = process_in_g(ROC_DEV_G, train=False)
    orids, test_dataset, es, descat = add_cat(orisen, test_dataset)
    outlist, outp, sens = gen_p(model, test_dataset, es, cat_head, descat)
    df = pd.DataFrame()
    test_dataset = pd.Series(data=test_dataset)
    descat = pd.Series(data=descat)
    df['orisen'] = orids
    df['sen'] = sens
    df['descat'] = repeatN(descat, len(ps)-1)
    print(len(df.index))
    print(len(outp))
    df['p-value'] = outp
    df['out'] = outlist
    df.to_csv('./gen_sen/res_sen_roc_g.csv')

def main(ds, mind):
    savepath = './modelgp/savedmodels' + mind

    modelg = OpenAIGPTLMHeadAgenModel.from_pretrained('openai-gpt')
    print(num_added_token_g)
    modelg.resize_token_embeddings(tokenizer_g.vocab_size + num_added_token_g)
    path = os.path.join(savepath, 'model.bin')
    modelg.load_state_dict(torch.load(path))
    modelg.to(device_g)
    model = OpenAIGPTLMHeadModel.from_pretrained('./modelmix/savedmodels3')
    model.to(device_g)
    gen_roc(model, modelg.cat_head)

if __name__ == '__main__':
    ds, mind = sys.argv[1], sys.argv[2]
    main(ds, mind)

max_stars_repo_name: michellesima/transformers

max_issues_repo_name: michellesima/transformers

max_forks_repo_name: michellesima/transformers


text:
import numpy as np

# Use Broadcasting to create a 4 x 4 ndarray that has its first
# column full of 1s, its second column full of 2s, its third
# column full of 3s, etc..
ones = np.ones((4,4))
broadcast = np.arange(1,5).reshape(1,4)
# Do not change the name of this array.
# Please don't print anything from your code! The TEST RUN button below will print your array.
X = ones * broadcast
print(X)


text:
import torch.nn as nn
import numpy as np
from cells import InvertedResidual, ConvBNReLU

class Identity(nn.Module):
    '''
    Identity cell
    '''
    def __init__(self, *args, **kwargs):
        super().__init__()
    def forward(self, x):
        return x

class MultipleChoiceBlock(nn.Module):
    '''
    Block for supernet: includes all options for each cell
    '''

    def __init__(self, layers = [], in_channels = 32, n_choices = 2):
        super(MultipleChoiceBlock, self).__init__()
        self.layers = nn.ModuleList(layers)
        self.n_choices = n_choices

    def forward(self, x, propagate):
        '''
        propagate is a binary matrix N_layers*N_cell_types
        1 in position [i,j] means selection cell j for position i
        '''

        output = 0

        for i in range(len(self.layers)):

            if propagate[i] == 0:
                continue
            output += self.layers[i](x)

        return output

def make_layers_supernet_full(first_conv_out_channels, propagate = [], put_downsampling = [], n_choices = 2):

    channels = first_conv_out_channels
    blocks = []
    for i in range(len(propagate)):

        if i in put_downsampling:
            blocks += [InvertedResidual(channels, channels*2, kernel_size=3, stride=2, expand_ratio=3, use_res_connect=0)]
            channels *= 2

        layers = [
            Identity(), #Identity
            InvertedResidual(channels, channels, kernel_size=3, stride=1, expand_ratio=3, use_res_connect=1),
            InvertedResidual(channels, channels, kernel_size=5, stride=1, expand_ratio=6, use_res_connect=1)
            ]

        blocks.append(MultipleChoiceBlock(layers = layers, in_channels = channels, n_choices = n_choices))

    return blocks, channels

class Supernet(nn.Module):
    '''
    Supernet model for training
    It has multiple choice blocks with all possible cells in all positions except downsampling ones
    '''

    def __init__(self, num_classes=10, batch_norm=True, propagate = [], put_downsampling = [4,8,12], first_conv_out_channels = 32, training = True, n_choices = 2):
        super(Supernet, self).__init__()

        #stem convolution
        self.in_conv = ConvBNReLU(3, first_conv_out_channels, kernel_size=3, stride=1)

        #main body (lsit of cells)
        blocks, out_channels = make_layers_supernet_full(first_conv_out_channels = first_conv_out_channels, propagate = propagate, put_downsampling = put_downsampling, n_choices = n_choices)
        self.blocks_list = nn.ModuleList(blocks)

        #simple classifier
        self.features_mixing = ConvBNReLU(out_channels, 1280, kernel_size=1, stride=1)
        self.out1 = nn.AdaptiveAvgPool2d((1, 1))
        self.out2 = nn.Linear(1280, num_classes)

        self.n_layers = len(propagate)
        self.n_choices = n_choices
        self.sample_random_paths = training #in training we propagate through one path, uniformly sampled
        self.propagate = propagate

    def forward(self, x):

        x = self.in_conv(x)

        mixed_blocks_cnt = 0
        for block in self.blocks_list:

            if isinstance(block, MultipleChoiceBlock):

                if self.sample_random_paths:
                    choice = np.random.randint(self.n_choices)
                    propagate = [0 for i in range(self.n_choices)]
                    propagate[choice] = 1
                else:
                    propagate = self.propagate[mixed_blocks_cnt]

                x = block(x, propagate)

                mixed_blocks_cnt += 1

            else:

                x = block(x)

        x = self.features_mixing(x)
        x = self.out1(x)
        x = x.view(x.shape[0],-1)
        x = self.out2(x)
        return x
################################################

def make_layers_supernet_single_path(first_conv_out_channels, propagate = [], put_downsampling = []):

    channels = first_conv_out_channels
    blocks = []

    for i in range(len(propagate)):

        if i in put_downsampling:
            blocks += [InvertedResidual(channels, channels*2, kernel_size=3, stride=2, expand_ratio=3, use_res_connect=0)]
            channels *= 2

        if propagate[i][0] == 1:
            blocks.append(Identity())
        elif propagate[i][1] == 1:
            blocks.append(InvertedResidual(channels, channels, kernel_size=3, stride=1, expand_ratio=3, use_res_connect=1))
        elif propagate[i][2] == 1:
            blocks.append(InvertedResidual(channels, channels, kernel_size=5, stride=1, expand_ratio=6, use_res_connect=1))

    return blocks, channels

class SinglePathSupernet(nn.Module):
    def __init__(self, num_classes=10, batch_norm=True, propagate = [], put_downsampling = [4,8,12], first_conv_out_channels = 32):
        super(SinglePathSupernet, self).__init__()

        self.in_conv = ConvBNReLU(3, first_conv_out_channels, kernel_size=3, stride=1)

        blocks, out_channels = make_layers_supernet_single_path(first_conv_out_channels = first_conv_out_channels, propagate = propagate, put_downsampling = put_downsampling)
        self.blocks_list = nn.Sequential(*blocks)

        self.features_mixing = ConvBNReLU(out_channels, 1280, kernel_size=1, stride=1)

        self.out1 = nn.AdaptiveAvgPool2d((1, 1))
        self.out2 = nn.Linear(1280, num_classes)

        self.n_layers = len(propagate)
        self.propagate = propagate

    def forward(self, x):

        #stem convolution
        x = self.in_conv(x)

        #main body (lsit of cells)
        x = self.blocks_list(x)

        #classifier
        x = self.features_mixing(x)
        x = self.out1(x)
        x = x.view(x.shape[0],-1)
        x = self.out2(x)
        return x

max_stars_repo_name: ArkadiyD/MacroNASBenchmark

max_issues_repo_name: ArkadiyD/MacroNASBenchmark

max_forks_repo_name: ArkadiyD/MacroNASBenchmark


text:
from unittest.mock import patch
import pandas as pd
import numpy as np
import ipywidgets as widgets
from . import data_visualisation
from . import parameter_selection
import string

def test_load_widgets():
    x_widget, mean_widget, std_widget, button_widget = data_visualisation.load_widgets()
    assert isinstance(x_widget, widgets.widget_selection.SelectMultiple)
    assert x_widget.value[0] == "Magnolia Heptapeta"
    assert isinstance(mean_widget, widgets.widget_bool.Checkbox)
    assert isinstance(mean_widget.value, bool)
    assert isinstance(std_widget, widgets.widget_bool.Checkbox)
    assert isinstance(std_widget.value, bool)
    assert isinstance(button_widget, widgets.widget_button.Button)

def test_run_pca():
    x_widget = widgets.SelectMultiple(
        options=["Magnolia Heptapeta", "Ilex Cornuta"],
        value=["Magnolia Heptapeta"],
        description="Species\n",
        disabled=False,
        rows=8,
    )
    true_widget = widgets.Checkbox(value=True, description="Test")
    false_widget = widgets.Checkbox(value=False, description="Test")

    df = pd.DataFrame(np.random.randint(0, 100, size=(100, 4)), columns=list("ABCD"))
    cols = ["PC1", "PC2", "PC3", "PC4"]
    df.columns = cols + df.columns.tolist()[len(cols) :]
    X_train = df.values

    y_train = np.random.choice([0, 1], size=len(df))
    df["Species"] = pd.Series(
        np.random.choice(
            ["Magnolia Heptapeta", "Ilex Cornuta"], size=len(df), p=[0.5, 0.5]
        ),
        index=df.index,
    )
    labels_map = {"Magnolia Heptapeta": 0, "Ilex Cornuta": 1}
    labels_inv_map = {"0": "Magnolia Heptapeta", "1": "Ilex Cornuta"}
    df_out = data_visualisation.run_pca(
        X_train,
        y_train,
        false_widget,
        false_widget,
        x_widget,
        labels_map,
        labels_inv_map,
    )
    df_out_mean = data_visualisation.run_pca(
        X_train,
        y_train,
        true_widget,
        false_widget,
        x_widget,
        labels_map,
        labels_inv_map,
    )
    df_out_std = data_visualisation.run_pca(
        X_train,
        y_train,
        false_widget,
        true_widget,
        x_widget,
        labels_map,
        labels_inv_map,
    )
    df_out_all = data_visualisation.run_pca(
        X_train, y_train, true_widget, true_widget, x_widget, labels_map, labels_inv_map
    )
    assert isinstance(df_out, pd.DataFrame)
    assert set(df.columns).intersection(set(df_out.columns))
    assert not np.all(df_out.values == df_out_mean.values)
    assert not np.all(df_out.values == df_out_std.values)
    assert not np.all(df_out.values == df_out_all.values)

@patch("%s.data_visualisation.Idp" % __name__)
def test_run_all_below(mock_disp):
    button_widget = widgets.Button(
        description="Update", disabled=False, button_style="primary", tooltip="Update"
    )
    button_widget.on_click(data_visualisation.run_all_below)
    assert mock_disp.display.assert_called
    assert True

@patch("%s.data_visualisation.plt" % __name__)
def test_plot_pca_variance(mock_plt):
    ncol = 25
    alphabet_string = list(string.ascii_lowercase)
    df = pd.DataFrame(
        np.random.randint(0, 100, size=(100, ncol)), columns=alphabet_string[:ncol]
    )
    _ = data_visualisation.plot_pca_variance(df)
    assert mock_plt.figure.called
    assert mock_plt.show.called

@patch("%s.data_visualisation.plt" % __name__)
def test_plot_pca(mock_plt):
    df = pd.DataFrame(np.random.randint(0, 100, size=(100, 4)), columns=list("ABCD"))
    cols = ["PC1", "PC2", "PC3", "PC4"]
    df.columns = cols + df.columns.tolist()[len(cols) :]
    df["Species"] = pd.Series(
        np.random.choice(
            ["Magnolia Heptapeta", "Ilex Cornuta"], size=len(df), p=[0.5, 0.5]
        ),
        index=df.index,
    )
    _ = data_visualisation.plot_pca(df)
    assert mock_plt.ticklabel_format.assert_called_once
    assert mock_plt.show.called

@patch("%s.parameter_selection.pickle" % __name__)
def test_hyperparam_tuning(mock_pickle):

    df = pd.DataFrame(np.random.randint(0, 100, size=(100, 4)), columns=list("ABCD"))
    cols = ["PC1", "PC2", "PC3", "PC4"]
    df.columns = cols + df.columns.tolist()[len(cols) :]
    X_train = df.values

    y_train = np.random.choice([0, 1], size=len(df))

    parameter_selection.hyperparam_tuning(
            X_train, y_train, True
        )
    assert mock_pickle.dump.called