text:
import numpy as np
from numba import njit, prange
from collections import namedtuple
from .robust_means import Holland_catoni_estimator, median_of_means
from math import ceil

# TODO: definir ici une strategy

Strategy = namedtuple("Strategy", ["grad_coordinate", "n_samples_in_block", "name"])

# @njit
# def decision_function(X, fit_intercept, w, out):
#     if fit_intercept:
#         # TODO: use out= in dot and + z[0] at the same time with parallelize ?
#         out[:] = X.dot(w[1:,:]) + w[0,:]
#     else:
#         out[:] = X.dot(w)
#     return out

# @njit
# def decision_function_with_intercept(X, w, out):
#     # TODO: use out= in dot and + z[0] at the same time with parallelize ?
#     out[:] = X.dot(w[1]) + w[0]
#     return out
#
# @njit
# def decision_function_no_intercept(X, w, out):
#     # TODO: use out= in dot and + z[0] at the same time with parallelize ?
#     out[:] = X.dot(w)
#     return out

@njit
def decision_function(X, coef, intercept, out):
    # TODO: use out= in dot and + z[0] at the same time with parallelize ?

    out[:] = X.dot(coef) + intercept
    return out

# @njit
# def loss_sample(model, i, w):
#     z = inner_prod(model.X, model.fit_intercept, i, w)
#     if model.sample_weight.size == 0:
#         return model.value(model.y[i], z)
#     else:
#         return model.sample_weight[i] * model.value(model.y[i], z)
#
#
# @njit
# def loss_batch(model, w):
#     out = 0.0
#     # TODO: allocate this in fit
#     n_samples = model.y.shape[0]
#     Xw = np.empty(n_samples)
#     # TODO: inner_prods or for loop ? No need for Xw
#     Xw = inner_prods(model.X, model.fit_intercept, w, Xw)
#     if model.sample_weight.size == 0:
#         for i in range(n_samples):
#             out += model.loss(model.y[i], Xw[i]) / n_samples
#     else:
#         for i in range(n_samples):
#             out += model.sample_weight[i] * model.loss(model.y[i], Xw[i]) / n_samples
#     return out
#
#
# @njit
# def grad_sample_coef(model, i, w):
#     z = inner_prod(model.X, model.fit_intercept, i, w)
#     if model.sample_weight.size == 0:
#         return model.derivative(model.y[i], z)
#     else:
#         return model.sample_weight[i] * model.derivative(model.y[i], z)
#
#
# @njit
# def grad_sample(model, i, w, out):
#     c = grad_sample_coef(model, i, w)
#     if model.fit_intercept:
#         out[0] = c
#         out[1:] = c * model.X[i]
#     else:
#         out[:] = c * model.X[i]
#     return out

import numpy as np
from numba import njit

@njit
def trimmed_mean(x, delta=0.01):
    x.sort()
    n_excluded = ceil(5*np.log(8/delta))
    return np.mean(x[n_excluded:-n_excluded])

# x = np.arange(0, 12).astype("float64")
# x = np.random.permutation(x)
# x[4] = -1200
#
# n = x.shape[0]
# block_size = 2
# n_blocks = n // block_size
#
# if n % block_size == 0:
#     blocks_means = np.empty(n_blocks)
# else:
#     blocks_means = np.empty(n_blocks + 1)
#
# print(x)
# mom, blocks_means = median_of_means(x, block_size, blocks_means)
#
# print(blocks_means)
#
# print("mom: ", mom)

@njit
def grad_coordinate_per_sample(
    loss_derivative,
    j,
    X,
    y,
    inner_products,
    fit_intercept,
):
    n_samples = inner_products.shape[0]
    # TODO: parallel ?
    # TODO: sparse matrix ?

    place_holder = np.empty(n_samples, dtype=X.dtype)
    if fit_intercept:
        if j[0] == 0:
            # In this case it's the derivative w.r.t the intercept
            for idx in range(n_samples):
                place_holder[idx] = loss_derivative(y[idx], inner_products[idx], j[1])

        else:
            for idx in range(n_samples):
                place_holder[idx] = loss_derivative(y[idx], inner_products[idx], j[1]) * X[idx, j[0] - 1]

    else:
        # There is no intercept
        for idx in range(n_samples):
            place_holder[idx] = loss_derivative(y[idx], inner_products[idx], j[1]) * X[idx, j[0]]
    return place_holder

@njit
def grad_coordinate_erm(loss_derivative, j, X, y, inner_products, fit_intercept):
    """Computation of the derivative of the loss with respect to a coordinate using the
    empirical risk minimization (erm) stategy."""

    return np.mean(grad_coordinate_per_sample(loss_derivative, j, X, y, inner_products, fit_intercept))

def erm_strategy_factory(loss, fit_intercept, **kwargs):
    @njit
    def grad_coordinate(X, y, j, inner_products):
        return grad_coordinate_erm(
            loss.derivative, j, X, y, inner_products, fit_intercept
        )

    return Strategy(grad_coordinate=grad_coordinate, n_samples_in_block=None, name="erm")

# TODO: overlapping blocks in MOM ???

@njit
def grad_coordinate_mom(
    loss_derivative,
    j,
    X,
    y,
    inner_products,
    fit_intercept,
    n_samples_in_block,
):
    """Computation of the derivative of the loss with respect to a coordinate using the
    median of means (mom) stategy."""
    # TODO: parallel ?
    # TODO: sparse matrix ?
    return median_of_means(grad_coordinate_per_sample(loss_derivative, j, X, y, inner_products, fit_intercept),n_samples_in_block)

    # n_samples = inner_products.shape[0]
    # n_blocks = n_samples // n_samples_in_block
    # last_block_size = n_samples % n_samples_in_block
    #
    # if n_samples % n_samples_in_block == 0:
    #     grad_means_in_blocks = np.empty(n_blocks, dtype=X.dtype)
    # else:
    #     grad_means_in_blocks = np.empty(n_blocks + 1, dtype=X.dtype)
    #
    # # TODO:instanciates in the closure
    # # This shuffle or the indexes to get different blocks each time
    # idx_samples = permutation(n_samples)
    # #permutation(idx_samples)
    #
    # # Cumulative sum in the block
    # grad_block = 0.0
    # # Block counter
    # n_block = 0
    #
    # #print(j)
    # if fit_intercept:
    #     if j[0] == 0:
    #         # In this case it's the derivative w.r.t the intercept
    #         for idx in range(n_samples):
    #             i = idx_samples[idx]
    #             # Update current sum in the block
    #             # print(sum_block, "+=", x[i])
    #             grad_block += loss_derivative(y[i], inner_products[i], j[1])
    #             # sum_block += x[i]
    #             if (idx != 0) and ((idx + 1) % n_samples_in_block == 0):
    #                 # It's the end of the block, we need to save its mean
    #                 # print("sum_block: ", sum_block)
    #                 grad_means_in_blocks[n_block] = grad_block / n_samples_in_block
    #                 n_block += 1
    #                 grad_block = 0.0
    #
    #         if last_block_size != 0:
    #             grad_means_in_blocks[n_blocks] = grad_block / last_block_size
    #
    #         grad_mom = np.median(grad_means_in_blocks)
    #         return grad_mom
    #     else:
    #         for idx in range(n_samples):
    #             i = idx_samples[idx]
    #             # Update current sum in the block
    #             # print(sum_block, "+=", x[i])
    #             grad_block += loss_derivative(y[i], inner_products[i], j[1]) * X[i, j[0] - 1]
    #             # sum_block += x[i]
    #             if (idx != 0) and ((idx + 1) % n_samples_in_block == 0):
    #                 # It's the end of the block, we need to save its mean
    #                 # print("sum_block: ", sum_block)
    #                 grad_means_in_blocks[n_block] = grad_block / n_samples_in_block
    #                 n_block += 1
    #                 grad_block = 0.0
    #
    #         if last_block_size != 0:
    #             grad_means_in_blocks[n_blocks] = grad_block / last_block_size
    #
    #         grad_mom = np.median(grad_means_in_blocks)
    #         return grad_mom
    # else:
    #     # There is no intercept
    #     for idx in range(n_samples):
    #         i = idx_samples[idx]
    #         # Update current sum in the block
    #         # print(sum_block, "+=", x[i])
    #         grad_block += loss_derivative(y[i], inner_products[i], j[1]) * X[i, j[0]]
    #         # sum_block += x[i]
    #         if (idx != 0) and ((idx + 1) % n_samples_in_block == 0):
    #             # It's the end of the block, we need to save its mean
    #             # print("sum_block: ", sum_block)
    #             grad_means_in_blocks[n_block] = grad_block / n_samples_in_block
    #             n_block += 1
    #             grad_block = 0.0
    #
    #     if last_block_size != 0:
    #         grad_means_in_blocks[n_blocks] = grad_block / last_block_size
    #
    #     grad_mom = np.median(grad_means_in_blocks)
    #     return grad_mom

# def erm_strategy_factory(loss, X, y, fit_intercept):
#     @njit
#     def grad_coordinate(j, inner_products):
#         return grad_coordinate_erm(
#             loss.derivative, j, X, y, inner_products, fit_intercept
#         )
#
#     return Strategy(grad_coordinate=grad_coordinate)

def mom_strategy_factory(loss, fit_intercept, n_samples_in_block, **kwargs):
    @njit
    def grad_coordinate(X, y, j, inner_products):
        return grad_coordinate_mom(
            loss.derivative, j, X, y, inner_products, fit_intercept, n_samples_in_block
        )

    return Strategy(
        grad_coordinate=grad_coordinate, n_samples_in_block=n_samples_in_block, name="mom"
    )

@njit
def grad_coordinate_catoni(
    loss_derivative,
    j,
    X,
    y,
    inner_products,
    fit_intercept,
):
    """Computation of the derivative of the loss with respect to a coordinate using the
    catoni stategy."""
    return Holland_catoni_estimator(grad_coordinate_per_sample(loss_derivative, j, X, y, inner_products, fit_intercept))

    # # grad = 0.0
    # # TODO: parallel ?
    # # TODO: sparse matrix ?
    # n_samples = inner_products.shape[0]
    #
    # # TODO:instanciates in the closure
    #
    # place_holder = np.empty(n_samples, dtype=X.dtype)
    # if fit_intercept:
    #     if j[0] == 0:
    #         # In this case it's the derivative w.r.t the intercept
    #         for idx in range(n_samples):
    #             place_holder[idx] = loss_derivative(y[idx], inner_products[idx], j[1])
    #
    #     else:
    #         for idx in range(n_samples):
    #             place_holder[idx] = loss_derivative(y[idx], inner_products[idx], j[1]) * X[idx, j[0] - 1]
    #
    # else:
    #     # There is no intercept
    #     for idx in range(n_samples):
    #         place_holder[idx] = loss_derivative(y[idx], inner_products[idx], j[1]) * X[idx, j[0]]

def catoni_strategy_factory(loss, fit_intercept, **kwargs):
    @njit
    def grad_coordinate(X, y, j, inner_products):
        return grad_coordinate_catoni(
            loss.derivative, j, X, y, inner_products, fit_intercept
        )

    return Strategy(
        grad_coordinate=grad_coordinate, n_samples_in_block=None, name="catoni"
    )

@njit
def grad_coordinate_tmean(
    loss_derivative,
    j,
    X,
    y,
    inner_products,
    fit_intercept,
):
    return trimmed_mean(grad_coordinate_per_sample(loss_derivative, j, X, y, inner_products, fit_intercept))

def tmean_strategy_factory(loss, fit_intercept, **kwargs):
    @njit
    def grad_coordinate(X, y, j, inner_products):
        return grad_coordinate_tmean(
            loss.derivative, j, X, y, inner_products, fit_intercept
        )

    return Strategy(
        grad_coordinate=grad_coordinate, n_samples_in_block=None, name="tmean"
    )

strategies_factory = {"erm": erm_strategy_factory, "mom": mom_strategy_factory, "catoni": catoni_strategy_factory, "tmean": tmean_strategy_factory}

# erm_strategy = Strategy(grad_coordinate=grad_coordinate_erm)

# mom_strategy = TrainingStrategy(grad_coordinate=grad_coordinate_mom)

# erm_strategy_dense
# erm_strategy_sparse

@njit(parallel=True)
def row_squared_norm_dense(model):
    n_samples, n_features = model.X.shape
    if model.fit_intercept:
        norms_squared = np.ones(n_samples, dtype=model.X.dtype)
    else:
        norms_squared = np.zeros(n_samples, dtype=model.X.dtype)
    for i in prange(n_samples):
        for j in range(n_features):
            norms_squared[i] += model.X[i, j] * model.X[i, j]
    return norms_squared

def row_squared_norm(model):
    # TODO: for C and F order with aliasing
    return row_squared_norm_dense(model.no_python)

@njit(parallel=True)
def col_squared_norm_dense(X, fit_intercept):
    n_samples, n_features = X.shape
    if fit_intercept:
        norms_squared = np.zeros(n_features + 1, dtype=X.dtype)
        # First squared norm is n_samples
        norms_squared[0] = n_samples
        for j in prange(1, n_features + 1):
            for i in range(n_samples):
                norms_squared[j] += X[i, j - 1] ** 2
    else:
        norms_squared = np.zeros(n_features, dtype=X.dtype)
        for j in prange(n_features):
            for i in range(n_samples):
                norms_squared[j] += X[i, j] * X[i, j]
    return norms_squared

def col_squared_norm(model):
    # TODO: for C and F order with aliasing
    return col_squared_norm_dense(model.no_python)

#
# @njit
# def grad_batch(model, w, out):
#     out.fill(0)
#     if model.fit_intercept:
#         for i in range(model.n_samples):
#             c = grad_sample_coef(model, i, w) / model.n_samples
#             out[1:] += c * model.X[i]
#             out[0] += c
#     else:
#         for i in range(model.n_samples):
#             c = grad_sample_coef(model, i, w) / model.n_samples
#             out[:] += c * model.X[i]
#     return out
#
#
# @njit(parallel=True)
# def row_squared_norm_dense(model):
#     n_samples, n_features = model.X.shape
#     if model.fit_intercept:
#         norms_squared = np.ones(n_samples, dtype=model.X.dtype)
#     else:
#         norms_squared = np.zeros(n_samples, dtype=model.X.dtype)
#     for i in prange(n_samples):
#         for j in range(n_features):
#             norms_squared[i] += model.X[i, j] * model.X[i, j]
#     return norms_squared
#
#
# def row_squared_norm(model):
#     # TODO: for C and F order with aliasing
#     return row_squared_norm_dense(model.no_python)
#
#
# @njit(parallel=True)
# def col_squared_norm_dense(model):
#     n_samples, n_features = model.X.shape
#     if model.fit_intercept:
#         norms_squared = np.zeros(n_features + 1, dtype=model.X.dtype)
#         # First squared norm is n_samples
#         norms_squared[0] = n_samples
#         for j in prange(1, n_features + 1):
#             for i in range(n_samples):
#                 norms_squared[j] += model.X[i, j - 1] * model.X[i, j - 1]
#     else:
#         norms_squared = np.zeros(n_features, dtype=model.X.dtype)
#         for j in prange(n_features):
#             for i in range(n_samples):
#                 norms_squared[j] += model.X[i, j] * model.X[i, j]
#     return norms_squared
#
#
# def col_squared_norm(model):
#     # TODO: for C and F order with aliasing
#     return col_squared_norm_dense(model.no_python)

max_stars_repo_name: imerad/linlearn

max_issues_repo_name: imerad/linlearn

max_forks_repo_name: imerad/linlearn

reason: import numpy,from numba


text:
"""
=================
Figure subfigures
=================

Sometimes it is desirable to have a figure with two different layouts in it.
This can be achieved with
:doc:`nested gridspecs</gallery/subplots_axes_and_figures/gridspec_nested>`,
but having a virtual figure with its own artists is helpful, so
Matplotlib also has "subfigures", accessed by calling
`matplotlib.figure.Figure.add_subfigure` in a way that is analogous to
`matplotlib.figure.Figure.add_subplot`, or
`matplotlib.figure.Figure.subfigures` to make an array of subfigures.  Note
that subfigures can also have their own child subfigures.

.. note::
    ``subfigure`` is new in v3.4, and the API is still provisional.

"""
import matplotlib.pyplot as plt
import numpy as np

def example_plot(ax, fontsize=12, hide_labels=False):
    pc = ax.pcolormesh(np.random.randn(30, 30), vmin=-2.5, vmax=2.5)
    if not hide_labels:
        ax.set_xlabel('x-label', fontsize=fontsize)
        ax.set_ylabel('y-label', fontsize=fontsize)
        ax.set_title('Title', fontsize=fontsize)
    return pc

np.random.seed(19680808)
# gridspec inside gridspec
fig = plt.figure(constrained_layout=True, figsize=(10, 4))
subfigs = fig.subfigures(1, 2, wspace=0.07)

axsLeft = subfigs[0].subplots(1, 2, sharey=True)
subfigs[0].set_facecolor('0.75')
for ax in axsLeft:
    pc = example_plot(ax)
subfigs[0].suptitle('Left plots', fontsize='x-large')
subfigs[0].colorbar(pc, shrink=0.6, ax=axsLeft, location='bottom')

axsRight = subfigs[1].subplots(3, 1, sharex=True)
for nn, ax in enumerate(axsRight):
    pc = example_plot(ax, hide_labels=True)
    if nn == 2:
        ax.set_xlabel('xlabel')
    if nn == 1:
        ax.set_ylabel('ylabel')

subfigs[1].set_facecolor('0.85')
subfigs[1].colorbar(pc, shrink=0.6, ax=axsRight)
subfigs[1].suptitle('Right plots', fontsize='x-large')

fig.suptitle('Figure suptitle', fontsize='xx-large')

plt.show()

##############################################################################
# It is possible to mix subplots and subfigures using
# `matplotlib.figure.Figure.add_subfigure`.  This requires getting
# the gridspec that the subplots are laid out on.

fig, axs = plt.subplots(2, 3, constrained_layout=True, figsize=(10, 4))
gridspec = axs[0, 0].get_subplotspec().get_gridspec()

# clear the left column for the subfigure:
for a in axs[:, 0]:
    a.remove()

# plot data in remaining axes:
for a in axs[:, 1:].flat:
    a.plot(np.arange(10))

# make the subfigure in the empty gridspec slots:
subfig = fig.add_subfigure(gridspec[:, 0])

axsLeft = subfig.subplots(1, 2, sharey=True)
subfig.set_facecolor('0.75')
for ax in axsLeft:
    pc = example_plot(ax)
subfig.suptitle('Left plots', fontsize='x-large')
subfig.colorbar(pc, shrink=0.6, ax=axsLeft, location='bottom')

fig.suptitle('Figure suptitle', fontsize='xx-large')
plt.show()

##############################################################################
# Subfigures can have different widths and heights.  This is exactly the
# same example as the first example, but *width_ratios* has been changed:

fig = plt.figure(constrained_layout=True, figsize=(10, 4))
subfigs = fig.subfigures(1, 2, wspace=0.07, width_ratios=[2, 1])

axsLeft = subfigs[0].subplots(1, 2, sharey=True)
subfigs[0].set_facecolor('0.75')
for ax in axsLeft:
    pc = example_plot(ax)
subfigs[0].suptitle('Left plots', fontsize='x-large')
subfigs[0].colorbar(pc, shrink=0.6, ax=axsLeft, location='bottom')

axsRight = subfigs[1].subplots(3, 1, sharex=True)
for nn, ax in enumerate(axsRight):
    pc = example_plot(ax, hide_labels=True)
    if nn == 2:
        ax.set_xlabel('xlabel')
    if nn == 1:
        ax.set_ylabel('ylabel')

subfigs[1].set_facecolor('0.85')
subfigs[1].colorbar(pc, shrink=0.6, ax=axsRight)
subfigs[1].suptitle('Right plots', fontsize='x-large')

fig.suptitle('Figure suptitle', fontsize='xx-large')

plt.show()

##############################################################################
# Subfigures can be also be nested:

fig = plt.figure(constrained_layout=True, figsize=(10, 8))

fig.suptitle('fig')

subfigs = fig.subfigures(1, 2, wspace=0.07)

subfigs[0].set_facecolor('coral')
subfigs[0].suptitle('subfigs[0]')

subfigs[1].set_facecolor('coral')
subfigs[1].suptitle('subfigs[1]')

subfigsnest = subfigs[0].subfigures(2, 1, height_ratios=[1, 1.4])
subfigsnest[0].suptitle('subfigsnest[0]')
subfigsnest[0].set_facecolor('r')
axsnest0 = subfigsnest[0].subplots(1, 2, sharey=True)
for nn, ax in enumerate(axsnest0):
    pc = example_plot(ax, hide_labels=True)
subfigsnest[0].colorbar(pc, ax=axsnest0)

subfigsnest[1].suptitle('subfigsnest[1]')
subfigsnest[1].set_facecolor('g')
axsnest1 = subfigsnest[1].subplots(3, 1, sharex=True)

axsRight = subfigs[1].subplots(2, 2)

plt.show()

ext: py

reason: import numpy


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

"""Tests for area attention."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from absl.testing import parameterized
import numpy as np
from tensor2tensor.layers import area_attention
import tensorflow.compat.v1 as tf

class AreaAttentionTest(parameterized.TestCase, tf.test.TestCase):

  def testComputeAreaFeatures1D(self):
    features = tf.constant([[[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]],
                            [[1.1, 2.1], [3.1, 4.1], [5.1, 6.1], [7.1, 8.1],
                             [9.1, 10.1]]],
                           dtype=tf.float32)
    area_mean, area_std, area_sum, area_height, area_widths = (
        area_attention.compute_area_features(features, max_area_width=3,
                                             epsilon=0.))
    with self.test_session() as session:
      session.run(tf.global_variables_initializer())
      res1, res2, res3, res4, res5 = session.run([area_mean, area_std, area_sum,
                                                  area_height, area_widths])
    self.assertAllClose(((((1, 2), (3, 4), (5, 6), (7, 8), (9, 10),
                           (2, 3), (4, 5), (6, 7), (8, 9),
                           (3, 4), (5, 6), (7, 8)),
                          ((1.1, 2.1), (3.1, 4.1), (5.1, 6.1), (7.1, 8.1),
                           (9.1, 10.1),
                           (2.1, 3.1), (4.1, 5.1), (6.1, 7.1), (8.1, 9.1),
                           (3.1, 4.1), (5.1, 6.1), (7.1, 8.1)))),
                        res1,
                        msg="mean_1d")
    expected_std = np.array([[[0, 0], [0, 0], [0, 0], [0, 0], [0, 0],
                              [1, 1], [1, 1], [1, 1], [1, 1],
                              [1.63299, 1.63299], [1.63299, 1.63299],
                              [1.63299, 1.63299]],
                             [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0],
                              [1, 1], [1, 1], [1, 1], [1, 1],
                              [1.63299, 1.63299], [1.63299, 1.63299],
                              [1.63299, 1.63299]]])
    self.assertAllClose(expected_std, res2, atol=1e-2, msg="std_1d")
    self.assertAllClose([[[1, 2], [3, 4], [5, 6], [7, 8], [9, 10],
                          [4, 6], [8, 10], [12, 14], [16, 18],
                          [9, 12], [15, 18], [21, 24]],
                         [[1.1, 2.1], [3.1, 4.1], [5.1, 6.1], [7.1, 8.1],
                          [9.1, 10.1],
                          [4.2, 6.2], [8.2, 10.2], [12.2, 14.2], [16.2, 18.2],
                          [9.3, 12.3], [15.3, 18.3], [21.3, 24.3]]],
                        res3,
                        msg="sum_1d")
    self.assertAllEqual([[[1], [1], [1], [1], [1],
                          [1], [1], [1], [1],
                          [1], [1], [1]],
                         [[1], [1], [1], [1], [1],
                          [1], [1], [1], [1],
                          [1], [1], [1]]],
                        res4,
                        msg="height_1d")
    self.assertAllEqual([[[1], [1], [1], [1], [1],
                          [2], [2], [2], [2],
                          [3], [3], [3]],
                         [[1], [1], [1], [1], [1],
                          [2], [2], [2], [2],
                          [3], [3], [3]]],
                        res5,
                        msg="width_1d")

  def testComputeAreaFeatures2D(self):
    features = tf.constant([[[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12]],
                            [[1.1, 2.1], [3.1, 4.1], [5.1, 6.1], [7.1, 8.1],
                             [9.1, 10.1], [11.1, 12.1]]],
                           dtype=tf.float32)
    area_mean, area_std, area_sum, area_height, area_widths = (
        area_attention.compute_area_features(features, max_area_width=3,
                                             max_area_height=2,
                                             height=2, epsilon=0.))
    with self.test_session() as session:
      session.run(tf.global_variables_initializer())
      res1, _, res3, res4, res5 = session.run([area_mean, area_std, area_sum,
                                               area_height, area_widths])
    expected_means = [[[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12],
                       [2, 3], [4, 5], [8, 9], [10, 11],
                       [3, 4], [9, 10],
                       [4, 5], [6, 7], [8, 9],
                       [5, 6], [7, 8],
                       [6, 7]],
                      [[1.1, 2.1], [3.1, 4.1], [5.1, 6.1], [7.1, 8.1],
                       [9.1, 10.1], [11.1, 12.1],
                       [2.1, 3.1], [4.1, 5.1], [8.1, 9.1], [10.1, 11.1],
                       [3.1, 4.1], [9.1, 10.1],
                       [4.1, 5.1], [6.1, 7.1], [8.1, 9.1],
                       [5.1, 6.1], [7.1, 8.1],
                       [6.1, 7.1]]]
    self.assertAllClose(expected_means, res1, msg="mean_1d")
    expected_heights = [[[1], [1], [1], [1], [1], [1],
                         # 1x2
                         [1], [1], [1], [1],
                         # 1x3
                         [1], [1],
                         # 2x1
                         [2], [2], [2],
                         # 2x2
                         [2], [2],
                         # 2x3
                         [2]],
                        [[1], [1], [1], [1], [1], [1],
                         # 1x2
                         [1], [1], [1], [1],
                         # 1x3
                         [1], [1],
                         # 2x1
                         [2], [2], [2],
                         # 2x2
                         [2], [2],
                         # 2x3
                         [2]]]
    self.assertAllEqual(expected_heights, res4, msg="height_1d")
    expected_widths = [[[1], [1], [1], [1], [1], [1],
                        # 1x2
                        [2], [2], [2], [2],
                        # 1x3
                        [3], [3],
                        # 2x1
                        [1], [1], [1],
                        # 2x2
                        [2], [2],
                        # 2x3
                        [3]],
                       [[1], [1], [1], [1], [1], [1],
                        # 1x2
                        [2], [2], [2], [2],
                        # 1x3
                        [3], [3],
                        # 2x1
                        [1], [1], [1],
                        # 2x2
                        [2], [2],
                        # 2x3
                        [3]]]
    self.assertAllEqual(expected_widths, res5, msg="width_1d")
    sizes = np.multiply(np.array(expected_heights), np.array(expected_widths))
    expected_sums = np.multiply(np.array(expected_means), sizes)
    self.assertAllClose(expected_sums, res3, msg="sum_1d")

  def testAreaMean(self):
    batch_size = 256
    feature_len = 100
    memory_height = 10
    heads = 2
    key_len = 2
    depth = 128
    max_area_height = 3
    max_area_width = 3
    queries = tf.random_uniform([batch_size, heads, key_len, depth],
                                minval=-10.0, maxval=10.0)
    features = tf.random_uniform([batch_size, heads, feature_len, depth],
                                 minval=-10.0, maxval=10.0)
    target_values = tf.random_uniform([batch_size, heads, key_len, depth],
                                      minval=-0.2, maxval=0.2)
    keys = tf.layers.dense(features, units=depth)
    values = tf.layers.dense(features, units=depth)
    mean_attention = area_attention.dot_product_area_attention(
        queries, keys, values,
        bias=None,
        area_key_mode="mean",
        name="mean_key",
        max_area_width=max_area_width,
        max_area_height=max_area_height,
        memory_height=memory_height)
    mean_gradients = tf.gradients(
        tf.reduce_mean(
            tf.pow(target_values - mean_attention, 2)), features)
    with self.test_session() as session:
      session.run(tf.global_variables_initializer())
      result = session.run([mean_gradients])
    self.assertFalse(np.any(np.logical_not(np.isfinite(result))))

  def test2DAreaMax(self):
    batch_size = 256
    feature_len = 100
    memory_height = 10
    heads = 2
    key_len = 6
    depth = 128
    max_area_height = 3
    max_area_width = 3
    queries = tf.random_uniform([batch_size, heads, key_len, depth],
                                minval=-10.0, maxval=10.0)
    features = tf.random_uniform([batch_size, heads, feature_len, depth],
                                 minval=-10.0, maxval=10.0)
    target_values = tf.random_uniform([batch_size, heads, key_len, depth],
                                      minval=-0.2, maxval=0.2)
    keys = tf.layers.dense(features, units=depth)
    values = tf.layers.dense(features, units=depth)
    max_attention = area_attention.dot_product_area_attention(
        queries, keys, values,
        bias=None,
        area_key_mode="max",
        area_value_mode="max",
        name="max_key",
        max_area_width=max_area_width,
        max_area_height=max_area_height,
        memory_height=memory_height)
    max_gradients = tf.gradients(tf.reduce_mean(
        tf.pow(target_values - max_attention, 2)), features)
    with self.test_session() as session:
      session.run(tf.global_variables_initializer())
      result1, result2 = session.run([max_gradients, max_attention])
    self.assertFalse(np.any(np.logical_not(np.isfinite(result1))))
    self.assertFalse(np.any(np.logical_not(np.isfinite(result2))))

  def test1DAreaMax(self):
    batch_size = 256
    feature_len = 100
    heads = 2
    key_len = 15
    depth = 128
    max_area_width = 3
    queries = tf.random_uniform([batch_size, heads, key_len, depth],
                                minval=-10.0, maxval=10.0)
    features = tf.random_uniform([batch_size, heads, feature_len, depth],
                                 minval=-10.0, maxval=10.0)
    feature_length = tf.constant(
        np.concatenate(
            (np.random.randint(max_area_width, feature_len, [batch_size - 1]),
             np.array([feature_len])), axis=0), tf.int32)
    base_mask = tf.expand_dims(tf.sequence_mask(feature_length), 1)
    mask = tf.expand_dims(base_mask, 3)
    mask = tf.tile(mask, [1, heads, 1, depth])
    features = tf.where(mask, features, tf.zeros_like(features))
    # [batch, 1, 1, memory_length]
    bias_mask = tf.expand_dims(base_mask, 1)
    bias = tf.where(
        bias_mask,
        tf.zeros_like(bias_mask, tf.float32),
        tf.ones_like(bias_mask, tf.float32) * -1e9)
    target_values = tf.random_uniform([batch_size, heads, key_len, depth],
                                      minval=-0.2, maxval=0.2)
    keys = tf.layers.dense(features, units=depth)
    values = tf.layers.dense(features, units=depth)
    max_attention = area_attention.dot_product_area_attention(
        queries, keys, values,
        bias=bias,
        area_key_mode="max",
        area_value_mode="max",
        name="max_key",
        max_area_width=max_area_width)
    max_gradients = tf.gradients(
        tf.reduce_mean(
            tf.pow(target_values - max_attention, 2)), features)
    with self.test_session() as session:
      session.run(tf.global_variables_initializer())
      result1, result2 = session.run([max_gradients, max_attention])
    self.assertFalse(np.any(np.logical_not(np.isfinite(result1))))
    self.assertFalse(np.any(np.logical_not(np.isfinite(result2))))

if __name__ == "__main__":
  tf.test.main()


text:
import numpy
from vispy import gloo, app
from vispy.util.transforms import perspective, translate, xrotate, yrotate, zrotate

VERTEX = """
#version 120
attribute vec3 position;
attribute vec4 colour;

uniform mat4 view;
uniform mat4 model;
uniform mat4 projection;

varying vec4 v_colour;

void main(){
    v_colour = colour;
    gl_Position = projection * view * model * vec4(position, 1.0);
}
"""
FRAGMENT = """
#version 120
varying vec4 v_colour;

void main(){
    gl_FragColor = v_colour;
}
"""

class Canvas(app.Canvas):
    """Canvas class for the 3d wireframe visualisation, extending Vispy's Canvas
    class.

    Attributes:
        limit -- the upper limit of the dataset.
        data -- a list containing all the values of the generated dataset.
        bgcolour -- background colour.
        fgcolour -- foreground colour.
        program -- handle to Program from vispy.gloo.
        dimension -- the width and/or height of the wireframe grid.
        grid -- a grid containing arrays pertaining to colours and vertex positions.
        projection -- the projection matrix.
        view -- the view matrix.
        model -- the model matrix.
        camerapos -- the position of the camera in world space.
        zoom -- the distance of the camera from the grid.

    Keyword Arguments:
        limit, data, bgcolour, fgcolour
    For other args, see vispy.app.Canvas.
    """
    def __init__(self, *args, **kwargs):
        # visualisation args
        self.limit = kwargs.pop('limit', None)
        self.data = kwargs.pop('data', None)
        self.bgcolour = kwargs.pop('bgcolour', None)
        self.fgcolour = kwargs.pop('fgcolour', None)
        self.colour_check()
        # ##################
        app.Canvas.__init__(self, *args, **kwargs)
        self.program = gloo.Program(VERTEX, FRAGMENT)
        self.dimension = int(numpy.ceil(self.limit ** 0.5))
        self.grid = self.make_grid()
        self.program['position'] = self.grid['position']
        self.program['colour'] = self.grid['colour']
        self.projection = perspective(90., self.size[0] /
                                      float(self.size[1]), 0.5, 500.)
        self.program['projection'] = self.projection
        self.view = numpy.eye(4, dtype=numpy.float32)
        self.camerapos = [-1,0,-5]
        self.zoom = self.camerapos[2]
        translate(self.view, self.camerapos[0], self.camerapos[1], self.camerapos[2])
        self.program['view'] = self.view
        self.model = numpy.eye(4, dtype=numpy.float32)
        self.model_vars = {"r": {'x': -45, 'y': 0}, "t": [1.5, 0, 1.5]}
        xrotate(self.model, self.model_vars['r']['x'])
        yrotate(self.model, self.model_vars['r']['y'])
        translate(self.model, self.model_vars['t'][0], self.model_vars['t'][1],
                  self.model_vars['t'][2])
        self.program['model'] = self.model

    def on_initialize(self, event):
        """Initialisation event."""
        gloo.set_state(clear_color=self.bgcolour)

    def on_draw(self, event):
        """Draw event."""
        gloo.clear()
        self.program.draw('lines')

    def on_mouse_wheel(self, event):
        """Mouse wheel event. This is used to zoom the camera."""
        delta = event.delta[1]
        step = 1 if delta > 0 else -1
        self.zoom += step
        translate(self.view, 0, 0, step)
        self.program['view'] = self.view
        self.update()

    def on_mouse_move(self, event):
        """Mouse move event. This is used to both pan and rotate the camera."""
        if not event.is_dragging:
            return
        x, y = event.pos
        dx = +2 * ((x - event.last_event.pos[0]) / float(self.size[0]))
        dy = -2 * ((y - event.last_event.pos[1]) / float(self.size[1]))
        if event.button == 1:
            # LEFT CLICK -> PAN
            translate(self.view, 0.75 * dx * abs(self.zoom), 0.75 * dy * abs(self.zoom), 0)
            self.program['view'] = self.view
        elif event.button == 2:
            # RIGHT CLICK -> ROTATE
            self.model = numpy.eye(4, dtype=numpy.float32)
            # compare displacement of mouse in x and y to determine which way to
            # rotate
            if abs(dx) > abs(dy):
                # rotate y
                if dx < 0:
                    self.model_vars['r']['y'] -= 3
                else:
                    self.model_vars['r']['y'] += 3
            else:
                # rotate x
                if dy < 0:
                    self.model_vars['r']['x'] += 3
                else:
                    self.model_vars['r']['x'] -= 3
            xrotate(self.model, self.model_vars['r']['x'])
            yrotate(self.model, self.model_vars['r']['y'])
            translate(self.model, self.model_vars['t'][0], self.model_vars['t'][1],
                      self.model_vars['t'][2])
            self.program['model'] = self.model
        self.update()

    def make_grid(self):
        """Initialises the data buffers which will be sent to the shaders.

        The output arrays are all based on the attributes and parameters created
        during the initialisation of the class (constructor).
        """
        n = self.dimension ** 2
        points = []
        # initialise all points in the grid
        for z in range(self.dimension):
            for x in range(self.dimension):
                y = 0.5 if ((self.dimension*z) + x + 1) in self.data else 0
                points.append([x-(self.dimension/2.), y, z-(self.dimension/2.)])
        positions = []
        # order the points like for vertices so grid draws correctly
        for x in range(self.dimension):
            # horiz
            mult = x * self.dimension
            for p in range(self.dimension-1):
                # append beginning and end
                positions.append(points[mult+p])
                positions.append(points[mult+p+1])
        for z in range(n-self.dimension):
            # vert
            positions.append(points[z])
            positions.append(points[z+self.dimension])
        grid = numpy.zeros(4*(self.dimension-1)*self.dimension,
                           dtype=[("position", 'f4', 3), ("colour", 'f4', 4)])
        grid['position'] = numpy.array(positions)
        grid['colour'] = self.fgcolour
        return grid

    def colour_check(self):
        """Determines whether colours have been passed to the class. They might
        not be present because the colours are optional arguments.

        If there are no colours present, use default values, otherwise normalise
        the colours and apply them to the respective instance variable.
        """
        if self.bgcolour is None:
            self.bgcolour = (1,1,1,1)
        else:
            self.bgcolour = (self.bgcolour[0]/255.,
                             self.bgcolour[1]/255.,
                             self.bgcolour[2]/255.,
                             self.bgcolour[3]/255.)
        if self.fgcolour is None:
            self.fgcolour = (0,0,0,1)
        else:
            self.fgcolour = (self.fgcolour[0]/255.,
                             self.fgcolour[1]/255.,
                             self.fgcolour[2]/255.,
                             self.fgcolour[3]/255.)

    def get_data(self):
        """Return the list of pixels currently displayed in the Canvas as a 3d
        numpy array.
        """
        return gloo.wrappers.read_pixels()

max_stars_repo_name: fxcqz/Primes

max_issues_repo_name: fxcqz/Primes

max_forks_repo_name: fxcqz/Primes


text:
"""
The classifier is designed to classify text injected into the platform via a REST API.
This will update the model and provide predictions as to the relevance of the
crawled pages.
"""
import json
import os
import pickle

from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
from sklearn.externals import joblib
from sklearn.ensemble import RandomForestClassifier

import numpy as np
import flask
from flask import request, flash, current_app as app

from app.models.model import set_sparkler_defaults, get_connection

STORAGE_LOCATION = os.getenv('STORAGE_LOCATION', '')

def load_vocab():
    """Load Vocabulary"""
    if os.path.exists('keywords.txt'):
        with open('keywords.txt', 'rb') as file:
            keywords_content = file.read()
    else:
        with open('keywords.txt', 'wb') as fwrite:
            fwrite.write('This is a test')
            keywords_content = 'This is a test'
    count_vect = CountVectorizer(lowercase=True, stop_words='english')
    count_vect.fit_transform([keywords_content])
    keywords = count_vect.vocabulary_
    return keywords

def new_model(name_label):
    """
    Create a new model
    :param name:
    :return:
    """
    obj = {
        'name': name_label,
        '_key': name_label
    }
    model = get_connection().createDocument(obj)
    model.save()
    set_sparkler_defaults(name_label)
    print('create new model')

def update_model(model_name, annotations):
    """
    Update a model
    :param model_name:
    :param annotations:
    :return:
    """

    model = get_connection()[model_name]

    if model['url_text'] is not None:
        url_text = model['url_text']
    else:
        url_text = None

    if model['url_details'] is not None:
        url_details = model['url_details']
    else:
        url_details = None
    # clf = MLPClassifier(max_iter=1000, learning_rate='adaptive',)
    clf = RandomForestClassifier(n_estimators=100)
    count_vect = CountVectorizer(lowercase=True, stop_words='english')
    tfidftransformer = TfidfTransformer()

    if url_text is None:
        print('An error occurred while accessing the application context variables')
        return '-1'

    labeled = np.array(annotations)

    if model['labeled'] is not None:
        # add the old docs to the new
        prev_url_text = model['url_text']
        prev_labeled = model['labeled']
        prev_url_details = model['url_details']
        url_text = np.append(url_text, prev_url_text, axis=0)
        labeled = np.append(labeled, prev_labeled, axis=0)
        url_details = np.append(url_details, prev_url_details, axis=0)

    features = count_vect.fit_transform(url_text)
    features = tfidftransformer.fit_transform(features).toarray().astype(np.float64)

    print('No. of features: ' + str(len(features)) + ' and No. of labels: ' + str(len(labeled)))

    print(np.unique(labeled))
    clf.fit(features, labeled, )

    # save the model
    model['url_text'] = url_text
    if isinstance(url_details, np.ndarray):
        model['url_details'] = url_details.tolist()
    else:
        model['url_details'] = url_details
    model['labeled'] = labeled.tolist()
    encoded_model = {'countvectorizer': count_vect, 'tfidftransformer': tfidftransformer,
                     'clf': clf}
    with open(STORAGE_LOCATION+'/models/' + model['name'] + '.pickle', 'wb') as handle:
        pickle.dump(encoded_model, handle, protocol=pickle.HIGHEST_PROTOCOL)
    model.save()
    #predicted = clf.predict(features)
    #accuracy = (labeled == predicted).sum() / float(len(labeled))

    return json.dumps(whack_a_mole_model(get_metrics(model)))

def get_metrics(model):
    """
    Get the metrics
    :param model:
    :return:
    """
    unique, counts = np.unique(model['labeled'], return_counts=True)
    dictionary = dict(zip(unique, counts))

    return dictionary

def predict(model_name, txt):
    """
    Run a prediction
    :param model_name:
    :param txt:
    :return:
    """
    model = get_connection()[model_name]
    encoded_model = {}

    if os.path.isfile(STORAGE_LOCATION+'/models/' + model['name'] + '.pickle'):
        with open(STORAGE_LOCATION+'/models/' + model['name'] + '.pickle', 'rb') as handle:
            encoded_model = pickle.load(handle)

    # if 'countvectorizer' in encoded_model:
    #     test = encoded_model['countvectorizer']
    if model is None:
        app.logger.info('Model not found')
        return -1
    if 'countvectorizer' not in encoded_model or encoded_model['countvectorizer'] is None:
        app.logger.info('No Count Vectorizer')
        return -1

    app.logger.info('Sorting Count Vectorizer out ' + model['name'])
    count_vect = encoded_model['countvectorizer']
    tfidftransformer = encoded_model['tfidftransformer']
    clf = encoded_model['clf']

    features = count_vect.transform([txt])
    features = tfidftransformer.transform(features).toarray().astype(np.float64)

    predicted = clf.predict(features)
    return predicted[0]

def import_model(model_name):
    """
    Import a model
    :param model_name:
    :return:
    """
    print('importing model: '+model_name)
    print('importing')
    filename = 'model.pkl'

    if 'file' not in request.files:
        flash('No file part')
        return '-1'
    file = request.files['file']
    if file.filename == '':
        flash('No selected file')
        return '-1'
    if file:
        file.save(os.path.join(flask.current_app.root_path,
                               flask.current_app.config['UPLOAD_FOLDER'], filename))
    else:
        flash('An error occurred while uploading the file')
        return '-1'

    model = joblib.load(os.path.join(flask.current_app.root_path,
                                     flask.current_app.config['UPLOAD_FOLDER'], filename))

    #accuracy = model['accuracy']

    setattr(flask.current_app, 'model', model)

    dictionary = get_metrics(model)
    dictionary = whack_a_mole_model(dictionary)
    json_dictionary = json.dumps(dictionary)

    return json_dictionary

def export_model(model_name):
    """
    Export a model
    :param model_name:
    :return:
    """
    return flask.send_from_directory(directory=STORAGE_LOCATION+'/models/',
                                     filename=model_name + '.pickle')

def check_model(model_name):
    """
    Check a model
    :param model_name:
    :return:
    """
    model = get_connection()[model_name]
    if model is None:
        return str(-1)

    dictionary = get_metrics(model)

    dictionary = whack_a_mole_model(dictionary)

    return flask.jsonify(dictionary)

def whack_a_mole_model(dictionary):
    """
    Too lazy to do it properly, but we need to replace numpy types in dicts.
    :param dictionary:
    :return:
    """
    if dictionary is not None:
        try:
            dictionary = {int(k):int(v) for k, v in dictionary.items()}
        except TypeError:
            dictionary = {str(k):int(v) for k, v in dictionary.items()}

    return dictionary

max_stars_repo_name: octodemo/sparkler

max_issues_repo_name: octodemo/sparkler

max_forks_repo_name: octodemo/sparkler


text:
# -*- coding: utf-8 -*-

"""Manager for Bio2BEL FamPlex."""

from typing import Mapping

import networkx as nx
from tqdm import tqdm

from bio2bel.manager.bel_manager import BELManagerMixin
from pybel import BELGraph
from pybel.dsl import Protein
from .equivalences import append_equivalences_graph, get_equivalences_df
from .relations import build_relations_graph, get_relations_df

__all__ = [
    'Manager',
]

class Manager(BELManagerMixin):
    """Protein family and complex hierarchy."""

    def __init__(self, *args, **kwargs):  # noqa:D107
        relations_df = get_relations_df()
        self.graph = build_relations_graph(relations_df)
        equivalences_df = get_equivalences_df()
        append_equivalences_graph(equivalences_df, self.graph)

    @classmethod
    def _get_connection(cls):
        pass

    @staticmethod
    def is_populated() -> bool:
        """Return if the database is populated."""
        return True

    def summarize(self) -> Mapping[str, int]:
        """Summarize the database."""
        return dict(
            relations=self.count_relations(),
            entries=self.count_entries(),
        )

    def count_entries(self) -> int:
        """Count the number of entries in the database."""
        return self.graph.number_of_nodes()

    def count_relations(self) -> int:
        """Count the number of relationships in the database."""
        return self.graph.number_of_edges()

    def to_bel(self) -> BELGraph:
        """Generate a BEL graph."""
        return self.graph

    def normalize_terms(self, graph: BELGraph, use_tqdm: bool = False) -> None:
        """Normalize FamPlex nodes in the graph."""
        it = graph.nodes()
        if use_tqdm:
            it = tqdm(it, total=graph.number_of_nodes())
        m = {
            node: Protein(namespace=node.namespace, name=node.name, identifier=node.name)
            for node in it
            if isinstance(node, Protein) and node.namespace.upper() == 'FPLX'
        }
        nx.relabel_nodes(graph, m, copy=False)

reason: import networkx


text:
#!/usr/bin/env python
"""
   Class to perform an iterative unfolding,
   stopping when either max_iter or test
   statistic tolerance is reached.

.. codeauthor: Zig Hampel
"""

__version__ = "$Id"

try:
    import numpy as np
    import pandas as pd
    import sys
    import os

    import ROOT
    from ROOT import TCanvas, TFile, TH2F, TH1F, TF1, TGraph, TGraphErrors, TGraphAsymmErrors
    from ROOT import gROOT, gPad, gRandom, gSystem, TDirectory
    gROOT.Reset()

    import matplotlib as mpl
    import matplotlib.pyplot as plt
    from matplotlib.ticker import ScalarFormatter

    from Mix import Mixer
    from Utils import *
    from Plotter import *
    from RootReader import mkDir
except ImportError as e:
    print e
    raise ImportError

class IterativeUnfolder():
    '''Common base class for Unfolder
    '''
    def __init__(self, name, maxIter=100, smoothIter=False, n_c=[], MixFunc=[],
                 RegFunc=[], TSFunc=[], Stack=False, verbose=False, **kwargs):
        self.name = name
        self.maxIter = maxIter
        self.smoothIter = smoothIter
        # Stacking Info
        self.StackFlag = Stack
        self.nStack = len(RegFunc)
        # Stacking Bin Indices Initializer
        self.StackInd = [[] for i in range(self.nStack)]
        # Mixing, Regularizing, Test Statistic Functions
        self.Mix = MixFunc
        self.Rglzr = RegFunc
        self.tsFunc = TSFunc
        # If user doesn't provide prior...
        self.n_c = n_c.copy()
        # Count Iterations
        self.counter = 0

        # Lists to Append Iteration Results
        #  Unfolded Results
        self.n_c_iters = [[] for i in range(self.nStack)]
        self.n_c_labels = []
        self.n_c_labels.append("Prior")
        self.n_c_final = []
        self.covM = []
        self.statErr = []
        self.sysErr = []
        #  Regularization Results
        self.ParamOut = [[] for i in range(self.nStack)]
        for j in range(self.nStack):
            [self.ParamOut[j].append([]) for i in xrange(0,RegFunc[j].nParams)]
        self.N_measured_out = []
        self.N_measured_out.append(np.sum(n_c))

        #  Test Statistic Results
        self.TS_out = [[] for i in range(self.nStack)]

        # Set Stacking Bin Indices
        self.SetStackInd()

        # ROOT Output Info
        self.RootName = ""
        self.SubDirName = ""

        # X & Y labels for causes and effects
        self.Cxlab = r'$Causes$'
        self.Cylab = r'$N_{C}$'
        self.Exlab = r'$Effect$'
        self.Eylab = r'$N_{E}$'

    def GetIParams(self,ind):
        return self.ParamOut[ind]

    def GetINMeasured(self):
        return self.N_measured_out

    def GetITS(self,ind):
        return self.TS_out[ind]

    def GetINC(self,ind):
        return self.n_c_iters[ind], self.n_c_labels

    def SetStackInd(self):
        iind = 0
        for iS in range(self.nStack):
            iaxis = self.Rglzr[iS].xarray.copy()
            self.StackInd[iS] = [range(iind,iind+len(iaxis))]
            iind += len(iaxis)

    def passTol(self):
        passFlag = True
        for iS in range(self.nStack):
            passFlag *= self.tsFunc[iS].PassTol()
        return passFlag

    def IUnfold(self, df_outfile=None):

        iter_counts_dict = {}
        iter_stat_err_dict = {}
        iter_sys_err_dict = {}

        print("\n====Starting Iterative Unfolding! Exciting!!!===\n")
        print("Iter: %i"%self.counter)
        # First Mixing
        n_c_update = self.Mix.Smear(self.n_c)
        self.counter += 1
        n_update = np.sum(n_c_update)

        # Calculate Chi2 of first mix with prior
        for iS in range(self.nStack):
            iind = self.StackInd[iS]
            inc = self.n_c[iind]
            self.n_c_iters[iS].append(inc)
            incu = n_c_update[iind]
            TS_cur, TS_del, TS_prob = self.tsFunc[iS].GetStats(incu,inc)
            self.TS_out[iS].append(TS_cur)
        prev_fit = self.n_c.copy()

        reg_fit = np.zeros(self.n_c.shape)
        # Perform Iterative Unfolding
        while (not self.passTol() and self.counter < self.maxIter):
            self.n_c_labels.append("Iter %s"%self.counter)

            # Updated unfolded distribution
            n_c = n_c_update.copy()

            iter_counts_dict[self.counter] = n_c
            iter_stat_err_dict[self.counter] = self.Mix.getStatErr()
            iter_sys_err_dict[self.counter] = self.Mix.getMCErr()

            # For test statistic purposes.
            # Don't want to compare reg_fit to n_c_update
            n_c_prev = n_c_update.copy()
            # Save n_update for root output
            self.N_measured_out.append(n_update)

            # Regularize unfolded dist in favor of 'smooth physics' prior
            for iS in range(self.nStack):
                iind = self.StackInd[iS][0]
                incu = n_c_update[iind]
                self.n_c_iters[iS].append(incu)
                reg_fit_iS, FitParams = self.Rglzr[iS].Regularize(incu)
                reg_fit[iind] = reg_fit_iS.copy()

                # Save fit parameters for root output
                for j in xrange(0,self.Rglzr[iS].nParams):
                    self.ParamOut[iS][j].append(FitParams[j])

                # Smoothed n_c for next iteration
                if (self.smoothIter):
                    n_c[iind] = reg_fit_iS.copy()
            #n_reg_events = np.sum(reg_fit)

            # Mix w/n_c from previous iter
            n_c_update = self.Mix.Smear(n_c)
            self.counter += 1
            n_update = np.sum(n_c_update)

            print("Iter: %i"%self.counter)
            # Calculate Chi2 wrt previous regularization
            for iS in range(self.nStack):
                iind = self.StackInd[iS][0]
                inc = self.n_c[iind]
                incu = n_c_update[iind]
                incp = n_c_prev[iind]
                TS_cur, TS_del, TS_prob = self.tsFunc[iS].GetStats(incu,incp)
                #TS_cur, TS_del, TS_prob = self.tsFunc[iS].GetStats(n_c_update[iind],n_c_prev[iind])
                self.TS_out[iS].append(TS_cur)
            prev_fit = reg_fit.copy()

        # Final Iteration
        n_c_final = n_c_update.copy()

        iter_counts_dict[self.counter] = n_c_final
        iter_stat_err_dict[self.counter] = self.Mix.getStatErr()
        iter_sys_err_dict[self.counter] = self.Mix.getMCErr()

        self.n_c_final = n_c_final.copy()
        # Calculate error estimates
        self.covM = self.Mix.getCov()
        self.statErr = self.Mix.getStatErr()
        self.sysErr = self.Mix.getMCErr()
        # Save n_update for root output
        self.N_measured_out.append(n_update)
        # Regularize unfolded dist in favor of 'smooth physics' prior
        for iS in range(self.nStack):
            iind = self.StackInd[iS]
            incu = n_c_final[iind]
            self.n_c_iters[iS].append(incu)
            final_fit, FitParams = self.Rglzr[iS].Regularize(incu)

            for j in xrange(0,self.Rglzr[iS].nParams):
                self.ParamOut[iS][j].append(FitParams[j])

        ## Plot results of iterations
        self.n_c_labels.append("Final")

        # Save unfolded distributions to disk
        if df_outfile is not None:
            with pd.HDFStore(df_outfile) as store:
                store['counts'] = pd.DataFrame.from_dict(iter_counts_dict)
                store['sys_err'] = pd.DataFrame.from_dict(iter_sys_err_dict)
                store['stat_err'] = pd.DataFrame.from_dict(iter_stat_err_dict)

    # Set the Labels for ROOT Plots
    def SetAxesLabels(self,Exlab,Eylab,Cxlab,Cylab):
        self.Cxlab = Cxlab
        self.Cylab = Cylab
        self.Exlab = Exlab
        self.Eylab = Eylab

    # Save ROOT Output
    def WriteResults(self,OutFileName="",NCName="NC",NEName="NE",Eaxis=[],BinList=[],subdirName="",**kwargs):
        Cxlab= self.Cxlab
        Cylab= self.Cylab
        Exlab= self.Exlab
        Eylab= self.Eylab

        n_c_final_tot = self.n_c_final.copy()
        Vc = self.covM.copy()
        n_c_err_tot = np.sqrt(Vc.diagonal())

        for iS in range(self.nStack):
            sind = self.StackInd[iS]
            n_c_final = n_c_final_tot[sind]
            n_c_err = n_c_err_tot[sind]
            # Get the results
            ParamOut = self.GetIParams(iS)
            TS_out = self.GetITS(iS)
            N_measured_out = self.GetINMeasured()
            n_c_iters, n_c_labels = self.GetINC(iS)

            nIter = self.counter

            tsFunc = self.tsFunc[iS]

            Rglzr = self.Rglzr[iS]
            Caxis = Rglzr.xarray.copy()
            Cedges = Rglzr.xedges.copy()

            # Final Unfolded Cause Dist
            NCFINAL = TH1F(NCName,NCName,len(Caxis),Cedges)
            for i in xrange(0,len(Caxis)):
                NCFINAL.SetBinContent(i+1,n_c_final[i])
                NCFINAL.SetBinError(i+1,n_c_err[i])
            NCFINAL.SetTitle("Final Unfolded Cause Distribution")
            NCFINAL.GetXaxis().SetTitle(Cxlab)
            NCFINAL.GetYaxis().SetTitle(Cylab)
            NCFINAL.SetStats(0)
            NCFINAL.GetXaxis().SetLimits(Cedges[0],Cedges[-1])
            NCFINAL.GetYaxis().SetRangeUser(np.min(n_c_final[(n_c_final>0)])/2,np.max(n_c_final)*2)

            # Save Statistical and MC (Systematic) Errors
            errStat = self.statErr.copy()
            errSys = self.sysErr.copy()
            ERRSTAT = TH1F(NCName+"_errStat",NCName+"_errStat",len(Caxis),Cedges)
            ERRSYS = TH1F(NCName+"_errSys",NCName+"_errSys",len(Caxis),Cedges)
            for i in xrange(0,len(Caxis)):
                ERRSTAT.SetBinContent(i+1,errStat[i])
                ERRSYS.SetBinContent(i+1,errSys[i])

            # Linear space covering iterations
            ArrIter = np.linspace(0,nIter-1,nIter)

            # Observed Effects Dist
            NOBSERVED = TH1F(NEName,"Observed Effects Distribution",len(Eaxis)-1,Eaxis)
            NOBSERVED.GetXaxis().SetTitle(Exlab)
            NOBSERVED.GetYaxis().SetTitle(Eylab)
            NOBSERVED.SetStats(0)
            for j in xrange(0,len(Eaxis)-1):
                binval = self.Mix.NEobs[j]
                NOBSERVED.SetBinContent(j+1,binval)
                NOBSERVED.SetBinError(j+1,np.sqrt(binval))

            # Number of Events Measured vs Iteration
            NMEASURED = TGraph(nIter+1,np.append(ArrIter,nIter),np.asarray(N_measured_out))
            NMEASURED.SetTitle("Number of Events Measured vs Iteration")
            NMEASURED.GetXaxis().SetTitle('Iteration')
            NMEASURED.GetYaxis().SetTitle('N_{measured}')

            # Regularization Parameters's Values vs Iteration
            PARAMS = []
            for j in xrange(0,Rglzr.nParams):
                PARAMS.append(TGraph(nIter,ArrIter,np.asarray(ParamOut[j])))
                PARAMS[j].SetTitle(Rglzr.ParamNames[j]+' of Regularization Fit vs Iteration')
                PARAMS[j].GetXaxis().SetTitle('Iteration')
                PARAMS[j].GetYaxis().SetTitle(Rglzr.ParamNames[j])

            # Test Statistic vs Iteration
            TS_TGRAPH = TGraph(nIter,ArrIter,np.asarray(TS_out))
            TS_TGRAPH.SetTitle('Test Statistic vs Iteration')
            TS_TGRAPH.GetXaxis().SetTitle('Iteration')
            TS_TGRAPH.GetYaxis().SetTitle(tsFunc.name)

            # Final Covariance and Coefficient of Correlation Matrices
            COV = TH2F("COVM","Covariance Matrix - %s"%self.Mix.ErrorType,len(Caxis),Cedges,len(Caxis),Cedges)
            COV.GetXaxis().SetTitle(Cxlab)
            COV.GetYaxis().SetTitle(Cxlab)
            COV.SetStats(0)
            CORR = TH2F("CORR","Coefficient of Correlation Matrix - %s"%self.Mix.ErrorType,len(Caxis),Cedges,len(Caxis),Cedges)
            CORR.GetXaxis().SetTitle(Cxlab)
            CORR.GetYaxis().SetTitle(Cxlab)
            CORR.SetStats(0)
            for j in xrange(0,len(Caxis)):
                diag = Vc[j,j]
                for k in xrange(0,len(Caxis)):
                    val = Vc[j,k]
                    COV.SetBinContent(j+1,k+1,val)
                    if (diag > 0):
                        CORR.SetBinContent(j+1,k+1,val/diag)

            # Save Each Iteration of True Distribution
            NCUNFOLDED = []
            for j in xrange(0,nIter+1):
                NCUNFOLDED.append(TH1F("%s_%i"%(NCName,j),"Cause Distribution - %s"%n_c_labels[j],len(Caxis),Cedges))
                NCUNFOLDED[j].GetXaxis().SetTitle(Cxlab)
                NCUNFOLDED[j].GetYaxis().SetTitle(Cylab)
                NCUNFOLDED[j].SetStats(0)
                n_c_j = np.asarray(n_c_iters[j]).copy()
                for i in xrange(0,len(Caxis)):
                    NCUNFOLDED[j].SetBinContent(i+1,n_c_j[i])

            # Write to ROOT File
            RFile = ROOT.TFile(OutFileName, "UPDATE")
            RFile.cd()

            self.RootName = OutFileName
            self.SubDirName = subdirName

            iDir = subdirName + "/" + BinList[iS]

            tdir = RFile.GetDirectory(subdirName)
            if ( not tdir ):
                tdir = RFile.mkdir(subdirName,"%s Directory"%self.name)
            RFile.cd(subdirName)

            if ( not RFile.GetDirectory(iDir) ):
                pdir = RFile.mkdir(iDir,"%s Directory"%BinList[iS])
                RFile.cd(iDir)

                NCFINAL.Write(NCName)
                ERRSTAT.Write()
                ERRSYS.Write()
                COV.Write("CovM")
                CORR.Write("CorrM")
                NOBSERVED.Write(NEName)
                NMEASURED.Write("%sMeasured"%NCName.lower())
                TS_TGRAPH.Write("%s"%(tsFunc.name))
                for j in xrange(0,Rglzr.nParams):
                    PARAMS[j].Write("%s"%(Rglzr.ParamNames[j]))

                # Write Unfolded Dists to Nested Directory
                ncDirName = "%s/%s"%(iDir,NCName.lower())
                ncdir = RFile.mkdir(ncDirName,"Unfolded Distributions for All Iterations")
                RFile.cd(ncDirName)
                for j in xrange(0,nIter+1):
                    NCUNFOLDED[j].Write("%s_%i"%(NCName,j))

                print "\n\n========== Writing ROOT File ==============="
                print "\nResults ROOT file saved as %s\n"%OutFileName
                print "\tSub-Directory is %s\n"%(iDir)
                print "============= Done Writing ================="
            else:
                print "\n\nDirectory %s already exists in file %s!"%(iDir,OutFileName)
                print "Consider running with a different configuration."
                print "Results will not be written out to %s.\n"%OutFileName

            RFile.Close()

max_stars_repo_name: jrbourbeau/cr-composition

max_issues_repo_name: jrbourbeau/cr-composition

max_forks_repo_name: jrbourbeau/cr-composition


text:
import argparse
import errno
import json
import os
import time

import sys

import numpy as np

import torch
from torch.autograd import Variable
import torch.nn.functional as F

### Import Data Utils ###
sys.path.append('../')

from model import DeepSpeech, supported_rnns
import user_defined_input

import params

###########################################################
# Comand line arguments, handled by params except seed    #
###########################################################
parser = argparse.ArgumentParser(description='DeepSpeech training')
parser.add_argument('--checkpoint', dest='checkpoint', action='store_true', help='Enables checkpoint saving of model')
parser.add_argument('--save_folder', default='models/', help='Location to save epoch models')
parser.add_argument('--model_path', default='models/deepspeech_final.pth.tar',
                    help='Location to save best validation model')
parser.add_argument('--continue_from', default='', help='Continue from checkpoint model')

parser.add_argument('--seed', default=0xdeadbeef, type=int, help='Random Seed')

parser.add_argument('--acc', default=23.0, type=float, help='Target WER')

parser.add_argument('--start_epoch', default=0, type=int, help='Number of epochs at which to start from')
parser.add_argument('--write_to', default='result_PyTorch', type=str, help='where to save the performance')
parser.add_argument('--epochs', default=1, type=int, help='number of epochs to run')

def to_np(x):
    return x.data.cpu().numpy()

class AverageMeter(object):
    """Computes and stores the average and current value"""

    def __init__(self):
        self.reset()

    def reset(self):
        self.val = 0
        self.avg = 0
        self.sum = 0
        self.count = 0

    def update(self, val, n=1):
        self.val = val
        self.sum += val * n
        self.count += n
        self.avg = self.sum / self.count

def main():
    args = parser.parse_args()

    torch.manual_seed(args.seed)
    torch.cuda.manual_seed_all(args.seed)

    if params.rnn_type == 'gru' and params.rnn_act_type != 'tanh':
      print("ERROR: GRU does not currently support activations other than tanh")
      sys.exit()

    if params.rnn_type == 'rnn' and params.rnn_act_type != 'relu':
      print("ERROR: We should be using ReLU RNNs")
      sys.exit()

    print("=======================================================")
    for arg in vars(args):
      print("***%s = %s " %  (arg.ljust(25), getattr(args, arg)))
    print("=======================================================")

    save_folder = args.save_folder

    loss_results, cer_results, wer_results = torch.Tensor(params.epochs), torch.Tensor(params.epochs), torch.Tensor(params.epochs)
    best_wer = None
    try:
        os.makedirs(save_folder)
    except OSError as e:
        if e.errno == errno.EEXIST:
            print('Directory already exists.')
        else:
            raise

    with open(params.labels_path) as label_file:
        labels = str(''.join(json.load(label_file)))

    rnn_type = params.rnn_type.lower()
    assert rnn_type in supported_rnns, "rnn_type should be either lstm, rnn or gru"

    model = DeepSpeech(rnn_hidden_size = params.hidden_size,
                       nb_layers       = params.hidden_layers,
                       labels          = labels,
                       rnn_type        = supported_rnns[rnn_type],
                       audio_conf      = None,
                       bidirectional   = True,
                       rnn_activation  = params.rnn_act_type,
                       bias            = params.bias)

    parameters = model.parameters()
    optimizer = torch.optim.SGD(parameters, lr=params.lr,
                                momentum=params.momentum, nesterov=False,
                                weight_decay = params.l2)
    cuda = torch.device('cuda')
    criterion = torch.nn.CTCLoss(reduction='none').to(cuda)

    avg_loss = 0
    start_epoch = 0
    start_iter = 0
    avg_training_loss = 0
    if params.cuda:
        model.cuda()

    print(model)
    print("Number of parameters: %d" % DeepSpeech.get_param_size(model))

    batch_time = AverageMeter()
    data_time = AverageMeter()
    losses = AverageMeter()
    ctc_time = AverageMeter()
    forward_time = AverageMeter()
    backward_time = AverageMeter()

    filename = "/scratch-ml00/wang603/deepspeechData/deepspeech_train.pickle"
    batchedData = user_defined_input.Batch(filename)

    def train_one_epoch(epoch):
        avg_loss = 0
        for i in range(batchedData.numBatches):
#            if i == 1: return
            end = time.time()
            inputs, targets, input_percentages, target_sizes = batchedData.batch(last=False)

            # making all inputs Tensor
            inputs = torch.from_numpy(inputs)
            targets = torch.from_numpy(targets)
            input_percentages = torch.from_numpy(input_percentages)
            target_sizes = torch.from_numpy(target_sizes)
            # measure data loading time
            data_time.update(time.time() - end)
            inputs = Variable(inputs, requires_grad=False)
            target_sizes = Variable(target_sizes, requires_grad=False)
            targets = Variable(targets, requires_grad=False)

            if params.cuda:
                inputs = inputs.cuda()

            # measure forward pass time
            forward_start_time = time.time()
            out = model(inputs)
            # out = out.transpose(0, 1)  # TxNxH

            seq_length = out.size(0)
            sizes = Variable(input_percentages.mul_(int(seq_length)).int(), requires_grad=False)

            # measure ctc loss computing time
            ctc_start_time = time.time()
            out = out.log_softmax(2)  #.detach().requires_grad_()
            # print(sizes.shape)
            # print(out.shape)
            loss = criterion(out, targets, sizes, target_sizes)
            ctc_time.update(time.time() - ctc_start_time)

            loss = loss / inputs.size(0)  # average the loss by minibatch

            loss_sum = loss.sum()
            inf = float("inf")
            if loss_sum == inf or loss_sum == -inf:
                print("WARNING: received an inf loss, setting loss value to 0")
                loss_value = 0
            else:
                loss_value = loss_sum.data.item()

            avg_loss += loss_value
            losses.update(loss_value, inputs.size(0))

            forward_time.update(time.time() - forward_start_time)

            # measure backward pass time
            backward_start_time = time.time()
            # compute gradient
            optimizer.zero_grad()
            loss_sum.backward()

            torch.nn.utils.clip_grad_norm(model.parameters(), params.max_norm)
            # SGD step
            optimizer.step()

            if params.cuda:
                torch.cuda.synchronize()

            backward_time.update(time.time() - backward_start_time)

            # measure elapsed time
            batch_time.update(time.time() - end)

            if (i % 20 == 0):
                print('Epoch: [{0}][{1}/{2}]\t'
                  'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
                  'Data {data_time.val:.3f} ({data_time.avg:.3f})\t'
                  'Forward {forward_time.val:.3f} ({forward_time.avg:.3f})\t'
                  'CTC Time {ctc_time.val:.3f} ({ctc_time.avg:.3f})\t'
                  'Backward {backward_time.val:.3f} ({backward_time.avg:.3f})\t'
                  'Loss {loss.val:.4f} ({loss.avg:.4f})\t'.format(
                (epoch + 1), (i + 1), batchedData.numBatches, batch_time=batch_time,
                data_time=data_time, forward_time=forward_time, ctc_time=ctc_time,
                backward_time=backward_time, loss=losses))

            del loss
            del out

        avg_loss /= batchedData.numBatches #  len(train_loader)

        print('Training Summary Epoch: [{0}]\t'
            'Average Loss {loss:.3f}\t'

            .format(epoch + 1, loss=avg_loss, ))

        return avg_loss

    model.train()
    loss_save = []
    time_save = []
    for epoch in range(start_epoch, args.epochs):
        startTime = time.time()
        loss_save.append(train_one_epoch(epoch))
        endTime = time.time()
        time_save.append(endTime - startTime)
        print("epoch {} used {} seconds".format(epoch, endTime - startTime))

    time_save.sort()
    median_time = time_save[int(args.epochs / 2)]
    with open(args.write_to, "w") as f:
        f.write("unit: " + "1 epoch\n")
        for loss in loss_save:
            f.write("{}\n".format(loss))
        f.write("run time: " + str(0.0) + " " + str(median_time) + "\n")

if __name__ == '__main__':
    main()

max_stars_repo_name: supunab/Lantern

max_issues_repo_name: douxiansheng/Lantern

max_forks_repo_name: douxiansheng/Lantern