text:
from __future__ import print_function

import keras.backend as K
import keras.losses as losses
import keras.optimizers as optimizers
import numpy as np

from keras.callbacks import ModelCheckpoint
from keras.layers.advanced_activations import LeakyReLU
from keras.layers import Input, RepeatVector, Reshape
from keras.layers.embeddings import Embedding
from keras.layers.pooling import MaxPooling2D, AveragePooling2D
from keras.layers.merge import Concatenate, Multiply
from keras.losses import binary_crossentropy
from keras.models import Model, Sequential
from keras.layers.pooling import GlobalAveragePooling2D

from .callbacks import *
from .multi_sampler import *

def wasserstein_loss(y_true, y_pred):
    return K.mean(y_true * y_pred)

class PretrainImageGan(RobotMultiPredictionSampler):

    def __init__(self, *args, **kwargs):
        '''
        As in the other models, we call super() to parse arguments from the
        command line and set things like our optimizer and learning rate.
        '''
        super(PretrainImageGan, self).__init__(*args, **kwargs)
        self.PredictorCb = ImageCb
        self.load_pretrained_weights = False
        self.save_encoder_decoder = True
        self.noise_iters = 1

    def _makePredictor(self, features):
        '''
        Create model to predict possible manipulation goals.
        '''
        (images, arm, gripper) = features
        img_shape, image_size, arm_size, gripper_size = self._sizes(
                images,
                arm,
                gripper)

        img_in = Input(img_shape,name="predictor_img_in")
        test_in = Input(img_shape, name="descriminator_test_in")

        encoder = self._makeImageEncoder(img_shape, perm_drop=True)
        enc = encoder([img_in])
        decoder = self._makeImageDecoder(
                self.hidden_shape,
                self.skip_shape, False, perm_drop=True)

        if self.load_pretrained_weights:
            try:
                encoder.load_weights(self.makeName(
                    "pretrain_image_encoder",
                    "image_encoder"))
                decoder.load_weights(self.makeName(
                    "pretrain_image_encoder",
                    "image_decoder"))
            except Exception as e:
                print(">> Failed to load pretrained generator weights.")

        gen_out = decoder(enc)
        image_discriminator = self._makeImageDiscriminator(img_shape)
        self.discriminator = image_discriminator

        image_discriminator.trainable = False
        o1 = image_discriminator([img_in, gen_out])

        loss = wasserstein_loss if self.use_wasserstein else "binary_crossentropy"
        weights = [1., 1.] if self.use_wasserstein else [100., 1.]
        self.model = Model([img_in], [gen_out, o1])
        self.model.compile(
                loss=["mae", loss],
                loss_weights=weights,
                optimizer=self.getOptimizer())

        self.generator = Model([img_in], [gen_out])
        self.generator.compile(
                loss=["logcosh"],
                optimizer=self.getOptimizer())

        image_discriminator.summary()

        return self.model, self.model, None, [img_in], enc

    def _getData(self, *args, **kwargs):
        features, targets = GetAllMultiData(self.num_options, *args, **kwargs)
        [img, q, g, oin, label, q_target, g_target,] = features
        return [img], [img]

    def _makeImageDiscriminator(self, img_shape):
        '''
        create image-only encoder to extract keypoints from the scene.

        Params:
        -------
        img_shape: shape of the image to encode
        '''
        img = Input(img_shape,name="img_encoder_in")
        img0 = Input(img_shape,name="img0_encoder_in")
        ins = [img, img0]
        dr = self.dropout_rate

        if self.use_wasserstein:
            loss = wasserstein_loss
            activation = "linear"
        else:
            loss = "binary_crossentropy"
            activation = "sigmoid"

        # common arguments
        kwargs = { "dropout_rate" : dr,
                   "padding" : "same",
                   "lrelu" : True,
                   "bn" : False,
                   "perm_drop" : True,
                 }

        x  = AddConv2D(img,  64, [4,4], 1, **kwargs)
        x0 = AddConv2D(img0, 64, [4,4], 1, **kwargs)
        x  = Add()([x, x0])
        x  = AddConv2D(x,    64, [4,4], 2, **kwargs)
        x  = AddConv2D(x,   128, [4,4], 2, **kwargs)
        x  = AddConv2D(x,   256, [4,4], 2, **kwargs)

        if self.use_wasserstein:
            x = Flatten()(x)
            x = AddDense(x, 1, "linear", 0., output=True, bn=False, perm_drop=True)
        else:
            x = AddConv2D(x, 1, [1,1], 1, 0., "same", activation="sigmoid",
                bn=False, perm_drop=True)
            x = GlobalAveragePooling2D()(x)

        discrim = Model(ins, x, name="image_discriminator")
        self.lr *= 2.
        discrim.compile(loss=loss, loss_weights=[1.],
                optimizer=self.getOptimizer())
        self.lr *= 0.5
        self.image_discriminator = discrim
        return discrim

    def _fit(self, train_generator, test_generator, callbacks):

        if self.gan_method == 'mae':
            # MAE
            for i in range(self.initial_epoch, self.epochs):
                for j in range(self.steps_per_epoch):
                    img, _ = next(train_generator)
                    img = img[0]
                    res = self.generator.train_on_batch(img, img)
                    print("\rEpoch {}, {}/{}: MAE loss {:.5}".format(
                        i, j, self.steps_per_epoch, res), end="")

                for c in callbacks:
                    c.on_epoch_end(i)

        elif self.gan_method == 'desc':
            for i in range(self.initial_epoch, self.epochs):
                for j in range(self.steps_per_epoch):
                    # Descriminator pass
                    img, target = next(train_generator)
                    fake = self.generator.predict(img)
                    self.discriminator.trainable = True
                    if self.use_wasserstein:
                        is_fake = np.ones((self.batch_size,1))
                        is_not_fake = -1 * np.ones((self.batch_size,1))
                    else:
                        is_fake = np.random.random((self.batch_size, 1)) * 0.1 + 0.9
                        is_not_fake = np.random.random((self.batch_size, 1)) * 0.1
                    res1 = self.discriminator.train_on_batch(
                            img + target, is_not_fake)
                    inputs = img + fake if isinstance(fake, list) else img + [fake]
                    res2 = self.discriminator.train_on_batch(inputs, is_fake)
                    self.discriminator.trainable = False
                    print("\rEpoch {}, {}/{}: D Real loss {}, Fake loss {}".format(
                        i, j, self.steps_per_epoch, res1, res2), end="")

                # Accuracy tests
                img, target = next(train_generator)
                fake = self.generator.predict(img)
                inputs = img + fake if isinstance(fake, list) else img + [fake]
                results = self.discriminator.predict(inputs)
                results2 = self.discriminator.predict(img + target)
                threshold = 0. if self.use_wasserstein else 0.5
                correct = np.count_nonzero(results >= threshold)
                correct2 = np.count_nonzero(results2 < threshold)

                print("\nAccuracy Epoch {}, real acc {}, fake acc {}".format(
                    i+1, correct/float(len(results)), correct2/float(len(results2))))

                for c in callbacks:
                    c.on_epoch_end(i)
        else: # actual gan
            d_iters = 10

            for i in range(self.initial_epoch, self.epochs):
                totals = [0, 0, 0, 0]

                for j in range(self.steps_per_epoch):

                    iter_for_step = d_iters * 10 if j % 40 == 0 else d_iters
                    if not self.use_wasserstein:
                        iter_for_step = 1

                    # Discriminator loops
                    for d in range(iter_for_step):

                        # Clip the weights for the wasserstein gan
                        if self.clip_weights > 0:
                            c = self.clip_weights
                            for l in self.discriminator.layers:
                                weights = l.get_weights()
                                weights = [np.clip(w, -c, c) for w in weights]
                                l.set_weights(weights)

                        # Descriminator pass
                        img, target = next(train_generator)
                        self.convert(img, target)
                        data = self.addNoiseIfNeeded(img)
                        fake = self.generator.predict(data)
                        self.discriminator.trainable = True
                        if self.use_wasserstein:
                            is_fake = np.ones((self.batch_size,1))
                            is_not_fake = -1 * np.ones((self.batch_size,1))
                        else:
                            is_fake = np.random.random((self.batch_size, 1)) * 0.1 + 0.9
                            is_not_fake = np.random.random((self.batch_size, 1)) * 0.1
                        res1 = self.discriminator.train_on_batch(
                                img + target, is_not_fake)

                        inputs = img + fake if isinstance(fake, list) else img + [fake]
                        res2 = self.discriminator.train_on_batch(inputs, is_fake)
                        print("D real loss[{:.5f}] fake loss[{:.5f}]".format(res1, res2))

                    self.discriminator.trainable = False

                    # Generator pass
                    img, target = next(train_generator)
                    data = self.addNoiseIfNeeded(img)
                    res = self.model.train_on_batch(
                            data, target + [is_not_fake]
                    )
                    print('Epoch {:03}/{:03}, step {:03}/{:03}: G loss[{:.5f}], G err[{:.5f}]'.format(
                        i+1, self.epochs, j, self.steps_per_epoch, res[0], res[1]))

                    totals[0] += res1
                    totals[1] += res2
                    totals[2] += res[0]
                    totals[3] += res[1]

                # Accuracy tests
                img, target = next(train_generator)
                data = self.addNoiseIfNeeded(img)
                fake = self.generator.predict(data)
                inputs = img + fake if isinstance(fake, list) else img + [fake]
                results = self.discriminator.predict(inputs)
                results2 = self.discriminator.predict(img + target)
                threshold = 0. if self.use_wasserstein else 0.5
                correct = np.count_nonzero(results >= threshold)
                correct2 = np.count_nonzero(results2 < threshold)

                d_real_acc = correct / float(len(results))
                d_fake_acc = correct2 / float(len(results2))

                print("Epoch {:03}/{:03}, testing D acc: real[{:.5f}], fake[{:.5f}]".format(
                    i+1, self.epochs, d_real_acc, d_fake_acc))

                logs = {}
                totals = [x / self.steps_per_epoch for x in totals]
                logs['D real loss'] = totals[0]
                logs['D fake loss'] = totals[1]
                logs['G loss'] = totals[2]
                logs['G l1 error'] = totals[3]
                logs['D real acc'] = d_real_acc
                logs['D fake acc'] = d_fake_acc

                for c in callbacks:
                    c.on_epoch_end(i, logs=logs)


text:
import os
import pickle
import numpy as np
import soundfile as sf
from scipy import signal
from scipy.signal import get_window
import librosa
from librosa.filters import mel
from numpy.random import RandomState
from pathlib import Path
import ipdb
from tqdm import tqdm

def butter_highpass(cutoff, fs, order=5):
    nyq = 0.5 * fs
    normal_cutoff = cutoff / nyq
    b, a = signal.butter(order, normal_cutoff, btype='high', analog=False)
    return b, a

def pySTFT(x, fft_length=1024, hop_length=256):

    x = np.pad(x, int(fft_length//2), mode='reflect')

    noverlap = fft_length - hop_length
    shape = x.shape[:-1]+((x.shape[-1]-noverlap)//hop_length, fft_length)
    strides = x.strides[:-1]+(hop_length*x.strides[-1], x.strides[-1])
    result = np.lib.stride_tricks.as_strided(x, shape=shape,
                                             strides=strides)

    fft_window = get_window('hann', fft_length, fftbins=True)
    result = np.fft.rfft(fft_window * result, n=fft_length).T

    return np.abs(result)

mel_basis = mel(16000, 1024, fmin=90, fmax=7600, n_mels=80).T
min_level = np.exp(-100 / 20 * np.log(10))
b, a = butter_highpass(30, 16000, order=5)

# audio file directory
rootDir = './wavs'
# rootDir = './kids_speech/wav/'
# spectrogram directory
rootDirs = [
     '../data/LibriTTS/train-clean-100',
     '../data/kids_speech/wavs'
]
# rootDir = '/home/shacharm/Projects/ug/data/LibriTTS/train-clean-100'
# rootDir = '/home/shacharm/Projects/ug/data/kids_speech/wavs'
targetDir = './spmel'

for rootDir in rootDirs:
    assert Path(rootDir).exists(), "{} does not exist".format(rootDirs)

    dirName, subdirList, _ = next(os.walk(rootDir))
    print('Found directory: %s' % dirName)
    SAMPLE_RATE = 16000
    for subdir in tqdm(sorted(subdirList)):

        if False:
            files = (Path(rootDir) / subdir).glob('**/*.wav')

            if not os.path.exists(os.path.join(targetDir, subdir)):
                os.makedirs(os.path.join(targetDir, subdir))
            _,_, fileList = next(os.walk(os.path.join(dirName,subdir)))

        try:
            prng = RandomState(int(subdir[1:]))
        except:
            prng = RandomState()
        for fileName in tqdm(list((Path(rootDir) / subdir).glob('**/*.wav'))):

            targetSubDir = targetDir / fileName.relative_to(rootDir).parent
            targetSubDir.mkdir(parents=True, exist_ok=True)
            targetFile = (targetSubDir / fileName.stem).with_suffix('.npy')

            if targetFile.exists():
                continue

            # Read audio file
            #x, fs = sf.read(os.path.join(dirName,subdir,fileName))
            x, fs = sf.read(str(fileName))
            x = librosa.resample(x, fs, SAMPLE_RATE)
            fs = SAMPLE_RATE

            # Remove drifting noise
            y = signal.filtfilt(b, a, x)
            # Ddd a little random noise for model roubstness
            wav = y * 0.96 + (prng.rand(y.shape[0])-0.5)*1e-06
            # Compute spect
            D = pySTFT(wav).T
            # Convert to mel and normalize
            D_mel = np.dot(D, mel_basis)
            D_db = 20 * np.log10(np.maximum(min_level, D_mel)) - 16
            S = np.clip((D_db + 100) / 100, 0, 1)
            # save spect
            np.save(targetFile, S.astype(np.float32), allow_pickle=False)

ext: py

max_stars_repo_name: uglabs/autovc

max_issues_repo_name: uglabs/autovc

max_forks_repo_name: uglabs/autovc

reason: import numpy,from numpy,from scipy


text:
import math
import numpy as np
from copy import copy

class Helper:
    @staticmethod
    def convertStringToBinary64(input_string):
        byte_array = bytearray(input_string, "utf8")

        byte_list = []
        for byte in byte_array:
            bin_byte = bin(byte)[2:]
            pad_bit_count = 8 - len(bin_byte)
            for pad_bit in range(pad_bit_count):
                bin_byte = '0' + bin_byte
            byte_list.append(bin_byte)

        if (len(byte_list) % 8 != 0):
            for pad in range(8 - (len(byte_list) % 8)):
                bin_byte = '00000000'
                byte_list.insert(0, bin_byte)

        bytes_64 = []
        for i in range(0, len(byte_list), 8):
            result_64 = ''
            for j in range(8):
                result_64 += byte_list[i+j]
            bytes_64.append(result_64)

        return bytes_64

    @staticmethod
    def convertBinary64ToString(input_binary):
        padded_out = False
        result = ""
        for binary in input_binary:
            splitted_binary = [binary[i:i+8] for i in range(0, len(binary), 8)]
            for splitted_bin in splitted_binary:
                if splitted_bin != '00000000' or padded_out:
                    padded_out = True
                    splitted_bin = '0b' + splitted_bin
                    int_splitted_bin = int(splitted_bin, 2)
                    result += chr(int_splitted_bin)

        return result

    @staticmethod
    def convertHexToBinary64(input_string):
        hex_array = ['0x'+val for val in input_string.split('0x') if val]
        byte_array = bytearray([int(val, 0) for val in hex_array])

        byte_list = []
        for byte in byte_array:
            bin_byte = bin(byte)[2:]
            pad_bit_count = 8 - len(bin_byte)
            for pad_bit in range(pad_bit_count):
                bin_byte = '0' + bin_byte
            byte_list.append(bin_byte)

        if (len(byte_list) % 8 != 0):
            for pad in range(8 - (len(byte_list) % 8)):
                bin_byte = '00000000'
                byte_list.insert(0, bin_byte)

        bytes_64 = []
        for i in range(0, len(byte_list), 8):
            result_64 = ''
            for j in range(8):
                result_64 += byte_list[i+j]
            bytes_64.append(result_64)

        return bytes_64

    @staticmethod
    def convertBinary64ToHex(input_binary):
        padded_out = False
        result = ""
        for binary in input_binary:
            splitted_binary = [binary[i:i+8] for i in range(0, len(binary), 8)]
            for splitted_bin in splitted_binary:
                if splitted_bin != '00000000' or padded_out:
                    padded_out = True
                    splitted_bin = '0b' + splitted_bin
                    hex_splitted_bin = hex(int(splitted_bin, 2))
                    result += hex_splitted_bin

        return result

    @staticmethod
    def convertIntToBinary64(int_value):
        byte_list = '{0:064b}'.format(int_value)
        return byte_list

    @staticmethod
    def xor(str1, str2):
        if len(str1) != len(str2):
            raise "Error"
        result = ""
        for i in range(len(str1)):
            if (str1[i] == str2[i]):
                result += '0'
            else:
                result += '1'
        return result

    @staticmethod
    def convertFileToBinary64(file_name):
        file = open(file_name, "rb")

        byte_list = []
        byte = file.read(1)
        while byte:
            bin_byte = bin(ord(byte))[2:]
            pad_bit_count = 8 - len(bin_byte)
            for pad_bit in range(pad_bit_count):
                bin_byte = '0' + bin_byte
            byte_list.append(bin_byte)
            byte = file.read(1)

        if not len(byte_list) % 8 == 0:
            len_pad = 8 - (len(byte_list) % 8)
        else:
            len_pad = 0

        for pad in range(len_pad):
            bin_byte = '00000000'
            byte_list.insert(0, bin_byte)

        bytes_64 = []
        for i in range(0, len(byte_list), 8):
            result_64 = ''
            for j in range(8):
                result_64 += byte_list[i+j]
            bytes_64.append(result_64)

        file.close()

        return bytes_64

    @staticmethod
    def convertBinary64ToFile(input_binary, file_name):
        padded_out = False

        result = bytearray()

        for binary in input_binary:
            splitted_binary = [binary[i:i+8] for i in range(0, len(binary), 8)]
            for splitted_bin in splitted_binary:
                if splitted_bin != '00000000' or padded_out:
                    padded_out = True
                    splitted_bin = '0b' + splitted_bin
                    int_splitted_bin = int(splitted_bin, 2)
                    result.extend(int_splitted_bin.to_bytes(1, 'big'))

        result_bytes = bytes(result)

        file = open(file_name, "wb")
        file.write(result_bytes)
        file.close()

    @staticmethod
    def convertIntToBit(input_int, bit_len):
        bin_byte = bin(input_int)[2:]
        pad_bit_count = bit_len - len(bin_byte)
        for pad_bit in range(pad_bit_count):
            bin_byte = '0' + bin_byte
        return bin_byte

    @staticmethod
    def convertBitToInt(input_bit):
        input_bit = '0b' + input_bit
        return int(input_bit, 2)

    @staticmethod
    def totalAsciiCode(string):
        asciiList = [ord(c) for c in string]
        return sum(asciiList)

    @staticmethod
    def randomNChar(n, seed):
        np.random.seed(seed)
        asciiList = np.random.randint(low=33, high=127, size=n)
        result = ""
        for i in range(len(asciiList)):
            result += chr(asciiList[i])
        return result

    @staticmethod
    def additionModulo(input_bits, b, m, split_num):
        result = ''
        split_bits = [input_bits[i:i+split_num] for i in range(0, len(input_bits), split_num)]

        for sub_bit in split_bits:
            sub_bit_num = Helper.convertBitToInt(sub_bit)
            sub_bit_num = (sub_bit_num + b) % m
            result += Helper.convertIntToBit(sub_bit_num, split_num)

        return result

    @staticmethod
    def multiplicationModulo(input_bits, b, m, split_num):
        result = ''
        split_bits = [input_bits[i:i+split_num] for i in range(0, len(input_bits), split_num)]

        for sub_bit in split_bits:
            sub_bit_num = Helper.convertBitToInt(sub_bit)
            sub_bit_num = (sub_bit_num * b) % m
            result += Helper.convertIntToBit(sub_bit_num, split_num)

        return result

    @staticmethod
    def shiftLeft(byte, n):
        result = list(byte)
        for i in range(n):
            bit = result[0]
            for j in range(7):
                result[j] = result[j+1]
            result[7] = bit

        return ''.join(result)

reason: import numpy


text:
import torch
import torch.nn as nn
from torch.utils.tensorboard import SummaryWriter
import numpy as np

import pprint
import os

from tianshou.policy import PPOPolicy
from tianshou.env import SubprocVectorEnv, DummyVectorEnv
from tianshou.trainer import onpolicy_trainer
from tianshou.data import Collector, ReplayBuffer, Batch
from tianshou.utils.net.discrete import Actor, Critic
from tianshou.utils.net.common import Net

from mytest.net import MTN, EDN, STNS
from mytest.env import create_MTLEnv, MTLEnv
from mytest.datautil import read_data_from_file, MTDataset

def train_RLPolicy(EnvArgs, TrainArgs, PPOArgs, discrete=True, device=torch.device('cpu')):
    print('-----Start train RL policy-----')
    data, label, task_interval, num_task, num_class = read_data_from_file(EnvArgs.data_path)
    databatcher = MTDataset(data, label, task_interval, num_class, EnvArgs.size_task_class)

    train_envs = SubprocVectorEnv(
        [lambda: create_MTLEnv(databatcher.copy(), EnvArgs, EnvArgs.reward_fn, EnvArgs.state_fn, discrete, device) for _ in
         range(TrainArgs.training_num)])
    test_envs = SubprocVectorEnv(
        [lambda: create_MTLEnv(databatcher.copy(), EnvArgs, EnvArgs.reward_fn, EnvArgs.state_fn, discrete, device) for _ in
         range(TrainArgs.test_num)])

    np.random.seed(TrainArgs.seed)
    torch.manual_seed(TrainArgs.seed)
    train_envs.seed(TrainArgs.seed)
    test_envs.seed(TrainArgs.seed)

    env = create_MTLEnv(databatcher, EnvArgs, EnvArgs.reward_fn, EnvArgs.state_fn, discrete, device)
    state_shape = env.observation_space.shape or env.observation_space.n
    action_shape = env.action_space.shape or env.action_space.n
    del env

    net = Net(TrainArgs.layer_num, state_shape, device=device)
    # actor_post = nn.Linear()
    actor = Actor(net, action_shape, discrete=discrete).to(device)
    if discrete:
        dist = torch.distributions.Categorical
    else:
        dist = torch.distributions.normal.Normal
    critic = Critic(net).to(device)
    optimizer_rl = torch.optim.Adam(set(list(
        actor.parameters()) + list(critic.parameters())), lr=TrainArgs.lr)

    policy = PPOPolicy(
        actor, critic, optimizer_rl, dist, PPOArgs.gamma,
        max_grad_norm=PPOArgs.max_grad_norm,
        eps_clip=PPOArgs.eps_clip,
        vf_coef=PPOArgs.vf_coef,
        ent_coef=PPOArgs.ent_coef,
        action_range=None)

    train_collector = Collector(policy, train_envs, ReplayBuffer(TrainArgs.buffer_size))
    test_collector = Collector(policy, test_envs, ReplayBuffer(TrainArgs.buffer_size))

    writer = SummaryWriter(os.path.join(TrainArgs.logdir, 'MTL', 'ppo'))

    result = onpolicy_trainer(
        policy, train_collector, test_collector, TrainArgs.epoch,
        TrainArgs.step_per_epoch, TrainArgs.collect_per_step, TrainArgs.repeat_per_collect,
        TrainArgs.test_num, TrainArgs.batch_size, writer=writer)
    pprint.pprint(result)
    print('-----End train RL policy-----')

    return policy

def train_RMTL(env, policy, max_epoch, writer):
    print('-----Start train MTL net-----')
    if isinstance(env, MTLEnv):
        env = DummyVectorEnv([lambda: env])
        inner_env = env.workers[0].env
    inner_env.setMaxEpoch(max_epoch)
    data = Batch(state={}, obs={}, act={}, rew={}, done={}, info={}, obs_next={}, policy={})
    data.obs = env.reset()
    done = False
    tag = 'train-RMTL'
    while not done:
        action = policy(data).act
        data.obs, rew, done, info = env.step(action)
        info = info[0]
        losses = np.asarray(info['losses'])
        for ind, ite in enumerate(info['iter']):
            if ite % inner_env.max_iter_epoch == 0:
                epoch = ite // inner_env.max_iter_epoch
                # print('Epoch %d, Iter: %d, Reward: %f, loss: %s' % (
                #     epoch, ite, rew, losses[:, ind]))
                loss = {}
                action = {}
                coes = {}
                for t in range(len(losses[:, ind])):
                    loss[f'task{t}'] = losses[:, ind][t]
                    action[f'task{t}'] = info['action'][t]
                    coes[f'task{t}'] = info['coes'][t]
                writer.add_scalars(tag + '/loss', loss, epoch)
                writer.add_scalars(tag + '/action', action, epoch)
                writer.add_scalars(tag + '/coes', coes, epoch)
                writer.add_scalar(tag + '/reward', rew, epoch)
    print('-----End train MTL net-----')
    return inner_env.env_net

def train_base(model, databatcher, criterion, optimizer, max_epoch, writer=None, device=torch.device('cpu')):
    base = databatcher.num_class * databatcher.batch_size
    max_iter_epoch = np.ceil(databatcher.data.shape[0] / (base * databatcher.num_task)).astype(np.int32)
    tag = ''
    for iter in range(max_iter_epoch * max_epoch):
        sampled_data, sampled_label, _, _ = databatcher.get_next_batch()
        sampled_data = torch.from_numpy(sampled_data).to(device)
        sampled_label = torch.from_numpy(np.asarray(sampled_label == 1).nonzero()[-1]).to(device)

        # outputs = []
        losses = []
        for t in range(databatcher.num_task):
            output = model(sampled_data[t * base: (t + 1) * base, :], t)
            loss = criterion(output, sampled_label[t * base: (t + 1) * base])
            losses.append(loss)
            # outputs.append(output)
        # output = torch.cat(outputs, 0)
        loss = 0
        for l in losses:
            loss += l

        # loss = criterion(output, sampled_label) #* databatcher.num_task
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        num_epoch = iter // max_iter_epoch
        if iter % max_iter_epoch == 0:
            print("Epoch %d, Iter %d, training loss %g" % (num_epoch, iter, loss))
            loss = {}
            for i, l in enumerate(losses):
                loss[f'task{i}'] = l
            writer.add_scalars('loss', loss, num_epoch)

max_stars_repo_name: ZhouSky/tianshou

max_issues_repo_name: ZhouSky/tianshou

max_forks_repo_name: ZhouSky/tianshou


text:
"""Test possibility of patching fftpack with pyfftw.

No module source outside of scipy.fftpack should contain an import of
the form `from scipy.fftpack import ...`, so that a simple replacement
of scipy.fftpack by the corresponding fftw interface completely swaps
the two FFT implementations.

Because this simply inspects source files, we only need to run the test
on one version of Python.
"""

import sys
if sys.version_info >= (3, 4):
    from pathlib import Path
    import re
    import tokenize
    from numpy.testing import assert_
    import scipy

    class TestFFTPackImport(object):
        def test_fftpack_import(self):
            base = Path(scipy.__file__).parent
            regexp = r"\s*from.+\.fftpack import .*\n"
            for path in base.rglob("*.py"):
                if base / "fftpack" in path.parents:
                    continue
                # use tokenize to auto-detect encoding on systems where no
                # default encoding is defined (e.g. LANG='C')
                with tokenize.open(str(path)) as file:
                    assert_(all(not re.fullmatch(regexp, line)
                                for line in file),
                            "{0} contains an import from fftpack".format(path))

max_stars_repo_name: anthowen/duplify

reason: from numpy,import scipy,from scipy


text:
# Licensed under a 3-clause BSD style license - see LICENSE.rst
import pytest
import numpy as np
from numpy.testing import assert_allclose
from astropy.table import Table
import astropy.units as u
from gammapy.modeling.models import PowerLawSpectralModel
from gammapy.maps import MapAxis, WcsNDMap
from gammapy.estimators.core import FluxEstimate
from gammapy.estimators import ESTIMATOR_REGISTRY

def test_estimator_registry():
    assert "Estimator" in str(ESTIMATOR_REGISTRY)

@pytest.fixture(scope="session")
def table_flux_estimate():
    axis = MapAxis.from_energy_edges((0.1, 1.0, 10.0), unit="TeV")

    cols = dict()
    cols["norm"] = np.array([1.0, 1.0])
    cols["norm_err"] = np.array([0.1, 0.1])
    cols["norm_errn"] = np.array([0.2, 0.2])
    cols["norm_errp"] = np.array([0.15, 0.15])
    cols["norm_ul"] = np.array([2.0, 2.0])
    cols["e_min"] = axis.edges[:-1]
    cols["e_max"] = axis.edges[1:]

    table = Table(cols, names=cols.keys())

    return table

@pytest.fixture(scope="session")
def map_flux_estimate():
    axis = MapAxis.from_energy_edges((0.1, 1.0, 10.0), unit="TeV")

    nmap = WcsNDMap.create(npix=5, axes=[axis])

    cols = dict()
    cols["norm"] = nmap.copy(data=1.0)
    cols["norm_err"] = nmap.copy(data=0.1)
    cols["norm_errn"] = nmap.copy(data=0.2)
    cols["norm_errp"] = nmap.copy(data=0.15)
    cols["norm_ul"] = nmap.copy(data=2.0)

    return cols

class TestFluxEstimate:
    def test_table_properties(self, table_flux_estimate):
        model = PowerLawSpectralModel(amplitude="1e-10 cm-2s-1TeV-1", index=2)
        fe = FluxEstimate(data=table_flux_estimate, reference_spectral_model=model)

        print(fe.available_quantities)
        assert fe.dnde.unit == u.Unit("cm-2s-1TeV-1")
        assert_allclose(fe.dnde.value, [1e-9, 1e-11])
        assert_allclose(fe.dnde_err.value, [1e-10, 1e-12])
        assert_allclose(fe.dnde_errn.value, [2e-10, 2e-12])
        assert_allclose(fe.dnde_errp.value, [1.5e-10, 1.5e-12])
        assert_allclose(fe.dnde_ul.value, [2e-9, 2e-11])

        assert fe.e2dnde.unit == u.Unit("TeV cm-2s-1")
        assert_allclose(fe.e2dnde.value, [1e-10, 1e-10])

        assert fe.flux.unit == u.Unit("cm-2s-1")
        assert_allclose(fe.flux.value, [9e-10, 9e-11])

        assert fe.eflux.unit == u.Unit("TeV cm-2s-1")
        assert_allclose(fe.eflux.value, [2.302585e-10, 2.302585e-10])

    def test_missing_column(self, table_flux_estimate):
        table_flux_estimate.remove_column("norm_errn")
        model = PowerLawSpectralModel(amplitude="1e-10 cm-2s-1TeV-1", index=2)
        fe = FluxEstimate(data=table_flux_estimate, reference_spectral_model=model)

        with pytest.raises(KeyError):
            fe.dnde_errn

    def test_map_properties(self, map_flux_estimate):
        model = PowerLawSpectralModel(amplitude="1e-10 cm-2s-1TeV-1", index=2)
        fe = FluxEstimate(data=map_flux_estimate, reference_spectral_model=model)

        assert fe.dnde.unit == u.Unit("cm-2s-1TeV-1")
        assert_allclose(fe.dnde.quantity.value[:, 2, 2], [1e-9, 1e-11])
        assert_allclose(fe.dnde_err.quantity.value[:, 2, 2], [1e-10, 1e-12])
        assert_allclose(fe.dnde_errn.quantity.value[:, 2, 2], [2e-10, 2e-12])
        assert_allclose(fe.dnde_errp.quantity.value[:, 2, 2], [1.5e-10, 1.5e-12])
        assert_allclose(fe.dnde_ul.quantity.value[:, 2, 2], [2e-9, 2e-11])

        assert fe.e2dnde.unit == u.Unit("TeV cm-2s-1")
        assert_allclose(fe.e2dnde.quantity.value[:, 2, 2], [1e-10, 1e-10])
        assert_allclose(fe.e2dnde_err.quantity.value[:, 2, 2], [1e-11, 1e-11])
        assert_allclose(fe.e2dnde_errn.quantity.value[:, 2, 2], [2e-11, 2e-11])
        assert_allclose(fe.e2dnde_errp.quantity.value[:, 2, 2], [1.5e-11, 1.5e-11])
        assert_allclose(fe.e2dnde_ul.quantity.value[:, 2, 2], [2e-10, 2e-10])

        assert fe.flux.unit == u.Unit("cm-2s-1")
        assert_allclose(fe.flux.quantity.value[:, 2, 2], [9e-10, 9e-11])
        assert_allclose(fe.flux_err.quantity.value[:, 2, 2], [9e-11, 9e-12])
        assert_allclose(fe.flux_errn.quantity.value[:, 2, 2], [1.8e-10, 1.8e-11])
        assert_allclose(fe.flux_errp.quantity.value[:, 2, 2], [1.35e-10, 1.35e-11])
        assert_allclose(fe.flux_ul.quantity.value[:, 2, 2], [1.8e-9, 1.8e-10])

        assert fe.eflux.unit == u.Unit("TeV cm-2s-1")
        assert_allclose(fe.eflux.quantity.value[:, 2, 2], [2.302585e-10, 2.302585e-10])
        assert_allclose(
            fe.eflux_err.quantity.value[:, 2, 2], [2.302585e-11, 2.302585e-11]
        )
        assert_allclose(
            fe.eflux_errn.quantity.value[:, 2, 2], [4.60517e-11, 4.60517e-11]
        )
        assert_allclose(
            fe.eflux_errp.quantity.value[:, 2, 2], [3.4538775e-11, 3.4538775e-11]
        )
        assert_allclose(fe.eflux_ul.quantity.value[:, 2, 2], [4.60517e-10, 4.60517e-10])

max_stars_repo_name: luca-giunti/gammapy

max_issues_repo_name: luca-giunti/gammapy

max_forks_repo_name: luca-giunti/gammapy

reason: import numpy,from numpy,import astropy,from astropy


text:
# coding=utf-8
import logging

import numpy as np
from sklearn import metrics
from sklearn.cluster import KMeans
from sklearn.linear_model import LogisticRegression
from sklearn.multiclass import OneVsRestClassifier
from sklearn.preprocessing import LabelBinarizer
from sklearn.utils import shuffle

logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.ERROR)

def _classification(embedding, labels_np, split_ratio=0.7):
    labels_np = shuffle(labels_np)
    nodes = labels_np[:, 0]
    labels = labels_np[:, 1]

    lb = LabelBinarizer()
    labels = lb.fit_transform(labels)
    train_size = int(labels_np.shape[0] * split_ratio)
    features = embedding[nodes]

    train_x = features[:train_size, :]
    train_y = labels[:train_size, :]
    test_x = features[train_size:, :]
    test_y = labels[train_size:, :]
    clf = OneVsRestClassifier(
        LogisticRegression(class_weight='balanced', solver='liblinear', max_iter=3000))

    clf.fit(train_x, train_y)
    y_pred = clf.predict_proba(test_x)
    y_pred = lb.transform(np.argmax(y_pred, 1))
    acc = np.sum(np.argmax(y_pred, 1) == np.argmax(test_y, 1)) / len(y_pred)
    eval_dict = {
        'acc': acc,
        'f1-micro': metrics.f1_score(np.argmax(test_y, 1), np.argmax(y_pred, 1),
                                     average='micro'),
        'f1-macro': metrics.f1_score(np.argmax(test_y, 1), np.argmax(y_pred, 1),
                                     average='macro'),
    }
    return list(eval_dict.values())

def classification(embedding, labels_np, split_ratio=0.7, loop=100):
    eval_dict = {
        'acc': 0.0,
        'f1-micro': 0.0,
        'f1-macro': 0.0,
        'std-micro': 0.0,
        'std-macro': 0.0
    }
    temp = []
    for _ in range(loop):
        temp.append(_classification(embedding, labels_np, split_ratio))
    temp = np.array(temp)
    eval_dict['acc'] = np.round(np.mean(temp[:, 0]), 4)
    eval_dict['f1-micro'] = np.round(np.mean(temp[:, 1]), 4)
    eval_dict['f1-macro'] = np.round(np.mean(temp[:, 2]), 4)
    eval_dict['std-micro'] = np.round(np.std(temp[:, 1]), 4)
    eval_dict['std-macro'] = np.round(np.std(temp[:, 2]), 4)
    print(eval_dict)
    return eval_dict

def _clustering(embedding, labels_np):
    labels_np = shuffle(labels_np)
    nodes = labels_np[:, 0]
    labels = labels_np[:, 1]
    features = embedding[nodes]

    model = KMeans(n_clusters=max(labels) + 1)
    # model = BayesianGaussianMixture(n_components=max(labels) + 1)

    lb = LabelBinarizer()
    labels = lb.fit_transform(labels)

    model.fit(features)

    # c_pred = model.labels_
    c_pred = model.predict(features)

    nmi = metrics.normalized_mutual_info_score(np.argmax(labels, 1), c_pred)

    eval_dict = {
        'homogeneity': metrics.homogeneity_score(np.argmax(labels, 1), c_pred),
        'completeness': metrics.completeness_score(np.argmax(labels, 1), c_pred),
        'silhouette': metrics.silhouette_score(features, c_pred),
        'NMI': nmi
    }
    # print(eval_dict)
    return eval_dict

def clustering(embedding, labels_np, loop=10):
    eval_dict = {
        'homogeneity': 0.0,
        'completeness': 0.0,
        'silhouette': 0.0,
        'NMI': 0
    }
    for _ in range(loop):
        tmp_dict = _clustering(embedding, labels_np)
        for key in tmp_dict.keys():
            eval_dict[key] += tmp_dict[key]
    for key in tmp_dict.keys():
        eval_dict[key] = round((1.0 * eval_dict[key]) / loop, 4)
    print(eval_dict)
    return eval_dict

max_stars_repo_name: cswzhang/DRSR

max_issues_repo_name: cswzhang/DRSR

max_forks_repo_name: cswzhang/DRSR


text:
from galaxy_analysis.plot.plot_styles import *
import numpy
import matplotlib.pyplot as plt

rc('font', size = 22)

names = ['Fiducial','Shortrad','No RT']

dirs = {'Fiducial' : './sn_H2atten_H2sh', 'Shortrad' : './sn_H2atten_H2_shortrad',
        'No RT' : './sn_H2atten_H2_noion'}

data = {}
for n in names:
    data[n] = np.genfromtxt(dirs[n] + '/number_density_cut_mass.dat',names=True)

fig, ax = plt.subplots()
fig.set_size_inches(8,8)

colors = {}
colors['Fiducial'] = 'C0'
colors['Shortrad'] = 'C1'
colors['No RT'] = 'C3'

for i,n in enumerate(names):
    ax.plot(data[n]['Time'] - 119.0, data[n]['Mass']/1.0E4, color = colors[n],
             lw = 3, ls = '-', label = n)

ax.set_xlabel(r'Time (Myr)')
ax.set_ylabel(r'Mass Above SF Threshold (10$^4$ M$_{\odot}$)')
#ax.semilogy()
#ax.set_ylim(1.0E2,1.0E5)
ax.set_ylim(0.0, 6E4 / 1.0E4)
ax.set_xlim(0.0, 100.0)
ax.legend(loc='best')
plt.minorticks_on()
plt.tight_layout()
fig.savefig('mass_density_cut.png')
plt.close()


text:
from queryparser.mysql import MySQLQueryProcessor
import numpy as np

def f3():
    query = """
            SELECT COUNT(*) AS n, id, mra, mlem AS qqq, blem
            FROM (
                SELECT inner1.id, mra, mlem,
                       inner2.col3 + inner2.parallax AS blem
                FROM (
                    SELECT qwerty.id, MAX(ra) AS mra, inner1.parallax,
                           qwerty.mlem mlem
                    FROM db.tab dbt
                    JOIN (
                        SELECT rekt AS parallax, id, mlem
                        FROM db.bar
                    ) AS qwerty USING (id)
                ) AS inner1
                JOIN (
                    SELECT qqq, col2 AS ra2, parallax, subsub.col3
                    FROM (
                        SELECT ra AS qqq, col2, col3, parallax
                        FROM db.gaia AS gaia
                        WHERE col5 > 5
                    ) AS subsub
                ) AS inner2
                ON inner1.parallax = inner2.parallax
            ) AS subq
            GROUP BY id;
            """
    query = """
        SELECT t1.a, t2.b, t3.c, t4.z
          FROM d.tab t1, `db2`.`tab` t2, foo.tab t3, x.y t4
    """
    qp = MySQLQueryProcessor()
    qp.set_query(query)
    qp.process_query(replace_schema_name={'d': 'foo', 'db2': 'bar', 'foo': 'bas'})

    print(qp.query)
    for i in qp.columns:
        print(i)
    print(qp.keywords)
    print(qp.functions)
    print(qp.display_columns)
    print(qp.tables)

f3()

max_stars_repo_name: aipescience/queryparser

max_issues_repo_name: django-daiquiri/queryparser

max_forks_repo_name: django-daiquiri/queryparser


text:
"""The DAS response.

The DAS response describes the attributes associated with a dataset and its
variables. Together with the DDS the DAS response completely describes the
metadata of a dataset, allowing it to be introspected and data to be
downloaded.

"""

try:
    from functools import singledispatch
except ImportError:
    from singledispatch import singledispatch
from collections import Iterable

from six import string_types, integer_types
from six.moves import map

import numpy as np

from ..model import (DatasetType, BaseType,
                     StructureType, SequenceType,
                     GridType)
from ..lib import encode, quote, __version__, NUMPY_TO_DAP2_TYPEMAP
from .lib import BaseResponse

INDENT = ' ' * 4

class DASResponse(BaseResponse):

    """The DAS response."""

    __version__ = __version__

    def __init__(self, dataset):
        BaseResponse.__init__(self, dataset)
        self.headers.extend([
            ('Content-description', 'dods_das'),
            ('Content-type', 'text/plain; charset=ascii'),
        ])

    def __iter__(self):
        for line in das(self.dataset):
            yield line.encode('ascii')

@singledispatch
def das(var, level=0):
    """Single dispatcher that generates the DAS response."""
    raise StopIteration

@das.register(DatasetType)
def _datasettype(var, level=0):
    yield '{indent}Attributes {{\n'.format(indent=level*INDENT)

    for attr in sorted(var.attributes.keys()):
        values = var.attributes[attr]
        for line in build_attributes(attr, values, level+1):
            yield line

    for child in var.children():
        for line in das(child, level=level+1):
            yield line
    yield '{indent}}}\n'.format(indent=level*INDENT)

@das.register(StructureType)
@das.register(SequenceType)
def _structuretype(var, level=0):
    yield '{indent}{name} {{\n'.format(indent=level*INDENT, name=var.name)

    for attr in sorted(var.attributes.keys()):
        values = var.attributes[attr]
        for line in build_attributes(attr, values, level+1):
            yield line

    for child in var.children():
        for line in das(child, level=level+1):
            yield line
    yield '{indent}}}\n'.format(indent=level*INDENT)

@das.register(BaseType)
@das.register(GridType)
def _basetypegridtype(var, level=0):
    yield '{indent}{name} {{\n'.format(indent=level*INDENT, name=var.name)

    for attr in sorted(var.attributes.keys()):
        values = var.attributes[attr]
        if np.asarray(values).size > 0:
            for line in build_attributes(attr, values, level+1):
                yield line
    yield '{indent}}}\n'.format(indent=level*INDENT)

def build_attributes(attr, values, level=0):
    """Recursive function to build the DAS."""
    # check for metadata
    if isinstance(values, dict):
        yield '{indent}{attr} {{\n'.format(indent=(level)*INDENT, attr=attr)
        for k, v in values.items():
            for line in build_attributes(k, v, level+1):
                yield line
        yield '{indent}}}\n'.format(indent=(level)*INDENT)
    else:
        # get type
        type = get_type(values)

        # encode values
        if (isinstance(values, string_types) or
           not isinstance(values, Iterable) or
           getattr(values, 'shape', None) == ()):
            values = [encode(values)]
        else:
            values = map(encode, values)

        yield '{indent}{type} {attr} {values};\n'.format(
            indent=(level)*INDENT,
            type=type,
            attr=quote(attr),
            values=', '.join(values))

def get_type(values):
    """Extract the type of a variable.

    This function tries to determine the DAP type of a Python variable using
    several methods. Returns the DAP type as a string.

    """
    if hasattr(values, 'dtype'):
        return NUMPY_TO_DAP2_TYPEMAP[values.dtype.char]
    elif isinstance(values, string_types) or not isinstance(values, Iterable):
        return type_convert(values)
    else:
        # if there are several values, they may have different types, so we
        # need to convert all of them and use a precedence table
        types = [type_convert(val) for val in values]
        precedence = ['String', 'Float64', 'Int32']
        types.sort(key=precedence.index)
        return types[0]

def type_convert(obj):
    """Map Python objects to the corresponding Opendap types.

    Returns the DAP representation of the type as a string.

    """
    if isinstance(obj, float):
        return 'Float64'
    elif isinstance(obj, integer_types):
        return 'Int32'
    else:
        return 'String'

max_stars_repo_name: nathanlcarlson/pydap

max_issues_repo_name: nathanlcarlson/pydap

max_forks_repo_name: nathanlcarlson/pydap


text:
import numpy as np

from astropy import units as u

from .. import Spectrum1D, SpectralRegion
from astropy.nddata.nduncertainty import StdDevUncertainty, VarianceUncertainty, InverseVariance
from .extract_spectral_region import extract_region

__all__ = ['noise_region_uncertainty']

def noise_region_uncertainty(spectrum, spectral_region, noise_func=np.std):
    """
    Generates a new spectrum with an uncertainty from the noise in a particular
    region of the spectrum.

    Parameters
    ----------

    spectrum : `~specutils.Spectrum1D`
        The spectrum to which we want to set the uncertainty.

    spectral_region : `~specutils.SpectralRegion`
        The region to use to calculate the standard deviation.

    noise_func : callable
        A function which takes the (1D) flux in the ``spectral_region`` and
        yields a *single* value for the noise to use in the result spectrum.

    Returns
    -------
    spectrum_uncertainty : `~specutils.Spectrum1D`
        The ``spectrum``, but with a constant uncertainty set by the result of
        the noise region calculation
    """

    # Extract the sub spectrum based on the region
    sub_spectra = extract_region(spectrum, spectral_region)

    # TODO: make this work right for multi-dimensional spectrum1D's?
    if not isinstance(sub_spectra, list):
        sub_spectra = [sub_spectra]
    sub_flux = u.Quantity(np.concatenate([s.flux.value for s in sub_spectra]),
                          spectrum.flux.unit)

    # Compute the standard deviation of the flux.
    noise = noise_func(sub_flux)

    # Uncertainty type will be selected based on the unit coming from the
    # noise function compared to the original spectral flux units.
    if noise.unit == spectrum.flux.unit:
        uncertainty = StdDevUncertainty(noise*np.ones(spectrum.flux.shape))
    elif noise.unit == spectrum.flux.unit**2:
        uncertainty = VarianceUncertainty(noise*np.ones(spectrum.flux.shape))
    elif noise.unit == 1/(spectrum.flux.unit**2):
        uncertainty = InverseVariance(noise*np.ones(spectrum.flux.shape))
    else:
        raise ValueError('Can not determine correct NDData Uncertainty based on units {} relative to the flux units {}'.format(noise.unit, spectrum.flux.unit))

    # Return new specturm with uncertainty set.
    return Spectrum1D(flux=spectrum.flux, spectral_axis=spectrum.spectral_axis,
                      uncertainty=uncertainty,
                      wcs=spectrum.wcs,
                      velocity_convention=spectrum.velocity_convention,
                      rest_value=spectrum.rest_value)

reason: import numpy,from astropy


text:
#!/usr/bin/python

# modify image stack by a min and max pixel value threshold

import Image
import sys
import numpy

def unique(a):
    return list(set(a))

def main(infile,mxint,mnint,outfile):
    img = Image.open(infile,'r')
    #pix = img.load()

    print "Mode = ", img.mode
    assert(len(img.getbands()) == 1)

    xsize = img.size[0]
    ysize = img.size[1]
    imarray = numpy.array(img)

    rawData = []

    for i in range(ysize):
        colors = imarray[i,:]
        #colors = unique(colors)
        for color in colors:
            #print color
            #modify color: allow if between values, throw out otherwise
            if (color < numpy.uint8(mnint) or color > numpy.uint8(mxint)):
                color = numpy.uint8(0)
            rawData.append(color)

    #print "Raw data = ", rawData
    a = numpy.array(rawData)
    a = numpy.reshape(a,(-1,xsize))
    fh = Image.fromarray(a)
    fh.save(outfile)
    fh.show()

if (len(sys.argv) != 5):
	print '***wrong number of input arguments'
	print str(len(sys.argv)-1) + '!= 4. Need <input.tiff> <minint> <maxint> <output.tiff>'
else:
    infile = sys.argv[1]
    mnint = int(sys.argv[2])
    mxint = int(sys.argv[3])
    outfile= sys.argv[4]
    main(infile,mxint,mnint,outfile)


text:
from neatflapper.utils import Resources
import pygame
import numpy as np
from pythonneat.nn.FeedForwardNetwork import FeedForwardNetwork
import pythonneat.utils.Activations as Activations

def blitRotateCenter(surf, image, topleft, angle):
    """
    Rotate a surface and blit it to the window
    :param surf: the surface to blit to
    :param image: the image surface to rotate
    :param topLeft: the top left position of the image
    :param angle: a float value for angle
    :return: None
    """
    rotated_image = pygame.transform.rotate(image, angle)
    new_rect = rotated_image.get_rect(center=image.get_rect(topleft=topleft).center)

    surf.blit(rotated_image, new_rect.topleft)

class Bird:
    IMGS = Resources.BIRD_IMGS
    MAX_ROTATION = 25
    ROT_VEL = 20
    ANIMATION_TIME = 5

    def __init__(self, x, y, network):
        self.x = x
        self.y = y
        self.tilt = 0
        self.tick_count = 0
        self.vel = 0
        self.height = self.y
        self.img_count = 0
        self.img = self.IMGS[0]
        self.network = network
        self.score = 0
        self.next_pipe = None
        self.alive = True

    def act(self, pipes):
        self.next_pipe = pipes[0]
        if self.next_pipe.passed:
            self.next_pipe = pipes[1]

        self.network.activate([Resources.get_bird_mid(self)[0],
                               Resources.get_bird_mid(self)[1],
                               Resources.get_pipe_top_mid(self.next_pipe)[0],
                               Resources.get_pipe_top_mid(self.next_pipe)[1],
                               Resources.get_pipe_bottom_mid(self.next_pipe)[0],
                               Resources.get_pipe_bottom_mid(self.next_pipe)[1]])
        out = self.network.neurons[1][0].value
        if out >= 0.5:
            self.vel = -10.5
            self.tick_count = 0
            self.height = self.y

    def move(self):
        self.tick_count += 1

        d = self.vel * self.tick_count + 1.5*self.tick_count**2

        if d >= 16:
            d = 16
        elif d < 0:
            d -= 2

        self.y = self.y + d

        if d < 0 or self.y < self.height + 50:
            if self.tilt < self.MAX_ROTATION:
                self.tilt = self.MAX_ROTATION
        else:
            if self.tilt > -90:
                self.tilt -= self.ROT_VEL

    def draw(self, window):
        self.img_count += 1

        if self.img_count < self.ANIMATION_TIME:
            self.img = self.IMGS[0]
        elif self.img_count < self.ANIMATION_TIME*2:
            self.img = self.IMGS[1]
        elif self.img_count < self.ANIMATION_TIME*3:
            self.img = self.IMGS[2]
        elif self.img_count < self.ANIMATION_TIME*4:
            self.img = self.IMGS[1]
        elif self.img_count == self.ANIMATION_TIME*4 + 1:
            self.img = self.IMGS[0]
            self.img_count = 0

        if self.tilt <= -80:
            self.img = self.IMGS[1]
            self.img_count = self.ANIMATION_TIME * 2

        blitRotateCenter(window, self.img, (self.x, self.y), self.tilt)

    def get_mask(self):
        return pygame.mask.from_surface(self.img)

max_stars_repo_name: SananR/NEATFlapper

max_issues_repo_name: SananR/NEATFlapper

max_forks_repo_name: SananR/NEATFlapper


text:
# -*- coding: utf-8 -*-
#!/usr/bin/env python3
"""
Created on Sat Jan  5 16:17:53 2019

@author: yansl
"""

import os
import numpy as np
import cv2
import gc

import scipy.spatial
from PIL import Image

def file_operation(file_name):
    #creating a folder to save the results
    pwd = os.getcwd()
    pwd = pwd.strip()
    path = pwd.rstrip('\\')
    file_path = path.replace('\\', '/') + '/' + file_name

    #make sure the save path is existed
    #note that the existed folder will be deleted
    if (os.path.exists(file_path)):
        os.remove(file_name)
    else:
        print('The results text do not exist!')
    return None

def result_saving(text_name, image_name, folder_name, results):
    """
    file processing part
    """
    #save estimation values
    file = open(folder_name + text_name, 'a')

    file.write('name' + '  ' + 'cos' + '  ' + 'dice' + '  '  + 'jaccard' + '  ' + 'pearson' + '  ' + 'tanimoto' + '\n')
    file.write(image_name + ':' + '  ')
    file.write(str(results[0]) + '  ')

    file.write(str(results[1]) + '  ')
    file.write(str(results[2]) + '  ')
    file.write(str(results[3]) + '  ')
    file.write(str(results[4]) + '\n')
    file.write('\n')
    file.close()
    return None

def files(folder_name, image_name, postfix_name):
    #creating a folder to save the results
    pwd = os.getcwd()
    pwd = pwd.strip()
    path = pwd.rstrip('\\')
    result = path.replace('\\', '/') + '/' + folder_name + '/' + image_name + postfix_name

    return result

def files_iteration():
    #getting files
    current_path = os.getcwd()
    image_path = current_path.replace('\\', '/') + '/' + 'benchmark'

    #the folder of results
    saving_evaluation = current_path.replace('\\', '/') + '/'

    #creating the new text file
    #!Note: we will delete the existed results text file.
    file_operation('fitted_mask_evaluation.txt')

    for images in os.listdir(image_path):
        full_path = os.path.join(image_path, images)
        if os.path.isdir(full_path):
            continue
        file = os.path.normcase(full_path)

        temp = os.path.splitext(images)[0]
        image_name = temp.split('_mask')[0]
        target_image = files('recovering_mask', image_name, '_recovering_mask.png')

        (image_name, outputs) = main(file, target_image)
        result_saving('fitted_mask_evaluation.txt', image_name, saving_evaluation, outputs)
    return None

"""
secondary functions
"""
def cos_similarity(vector1, vector2):
    result_temp = 1 - scipy.spatial.distance.cosine(vector1, vector2)
    result_cos = round(np.float64(result_temp), 5)
    return result_cos

def dice_similarity(vector1, vector2):
    vector1[vector1 > 0] = 1
    vector1 = vector1.copy().astype(np.bool)
    vector2[vector2 > 0] = 1
    vector2 = vector2.copy().astype(np.bool)
    result_temp = 1 - scipy.spatial.distance.dice(vector1, vector2)
    result_dice = round(np.float64(result_temp), 5)
    return result_dice

def jaccard_similarity(vector1, vector2):
    result_temp = 1 - scipy.spatial.distance.jaccard(vector1, vector2)
    result_jaccard = round(np.float64(result_temp), 5)
    return result_jaccard

def pearson_similarity(vector1, vector2):
    result_temp = 1 - scipy.spatial.distance.correlation(vector1, vector2)
    result_pearson = round(np.float64(result_temp), 5)
    return result_pearson

def tanimoto_similarity(vector1, vector2):
    vector1[vector1 > 0] = 1
    vector1 = vector1.copy().astype(np.bool)
    vector2[vector2 > 0] = 1
    vector2 = vector2.copy().astype(np.bool)
    result_temp = 1 - scipy.spatial.distance.rogerstanimoto(vector1, vector2)
    result_tanimoto = round(np.float64(result_temp), 5)
    return result_tanimoto

def boundingbox(image):
    temp = image.copy()
    #find profile
    (_, contours, _) = cv2.findContours(temp, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
    #find bounding box coordinates
    #to get rectangular area which encloses the profile area, but it may be not the minimum.
    index = 0
    max_size = 0
    for i in range(len(contours)):
        size = contours[i].shape[0]
        if (size >= max_size):
            max_size = size
            index = i
        else:
            pass
    cnt = contours[index]
    #(x, y)minimal up-right bounding rectangle
    (x, y, width, height) = cv2.boundingRect(np.array(cnt))
    return (x, y, width, height)

def border_addition(image, top, bottom, left, right, value):
    temp = image.copy()
    #adding border
    temp = cv2.copyMakeBorder(temp, top, bottom, left, right, cv2.BORDER_CONSTANT, value)
    return temp

def border_cropping(mask):
    """
    mask coordinate processing & processing part
    """
    #get bounding box
    (x, y, width, height) = boundingbox(mask)
    """
    mask & image cropping part
    """
    #image coordinate (y, x)
    #numpy array range: [a, b), note: include a, but not include b!
    #numpy array order: [row, column]
    #cropping
    cropped_mask = mask[y : (y + height), x : (x + width)]

    return (cropped_mask, x, width, y, height)

def border_adding(mask, border_length=20):
    """
    mask processing part
    """
    #adding a border
    #default: border_addition(img, 20, 0)
    mask_adding = border_addition(mask, border_length, 0)
    """
    coordinate processing part
    """
    #getting coordinate parameters (x, y, width, height)
    coordinate = boundingbox(mask_adding)

    return (mask_adding, coordinate)

"""
main function
"""
def main(official_mask_fullname, generated_mask_name):
    #main
    #read input images with PIL
    official_temp = Image.open(official_mask_fullname).convert('L')
    generated_temp = Image.open(generated_mask_name).convert('L')

    #pixels comparison
    official_mask = np.array(official_temp)
    official_mask[official_mask > 0] = 255
    generated_mask = np.array(generated_temp)
    generated_mask[generated_mask > 0] = 255

    #scatch around for image's dimensions
    #note: shape[0] denotes row (y), shape[1] denotes column (x)
    official_size = official_mask.shape[0: 2]
    generated_size = generated_mask.shape[0: 2]

    if ((official_size[0] != generated_size[0]) or (official_size[1] != generated_size[1])):
        #get the core size of offcial mask
        (cropped_official, official_x, official_width, official_y, official_height) = border_cropping(official_mask)
        (cropped_generated, generated_x, generated_width, generated_y, generated_height) = border_cropping(generated_mask)
        #estimate the difference between the two core sizes
        cropped_official_size = cropped_official.shape[0: 2]
        cropped_generated_size = cropped_generated.shape[0: 2]

        diff_y = (cropped_official_size[0] - cropped_generated_size[0]) / 2
        diff_x = (cropped_official_size[1] - cropped_generated_size[1]) / 2

        if (diff_y < 0):
            constant_y = -1
        else:
            constant_y = 1

        if (diff_x < 0):
            constant_x = -1
        else:
            constant_x = 1

        #scan the size of cropped official mask
        start_x = official_x
        start_y = official_y
        end_x = start_x + official_width
        end_y = start_y + official_height

        if (np.fabs(diff_x) > int(np.fabs(diff_x))):
            left_x = start_x + int(diff_x)
            right_x = official_size[1] - end_x + int(diff_x) + constant_x
        else:
            left_x = start_x + int(diff_x)
            right_x = official_size[1] - end_x + int(diff_x)

        if (np.fabs(diff_y) > int(np.fabs(diff_y))):
            up_y = start_y + int(diff_y)
            down_y = official_size[0] - end_y + int(diff_y) + constant_y
        else:
            up_y = start_y + int(diff_y)
            down_y = official_size[0] - end_y + int(diff_y)

        resized_mask = border_addition(cropped_generated, up_y, down_y, left_x, right_x, 0)
    else:
        resized_mask = generated_mask

    #cosine similarity
    official_vector = official_mask.flatten().reshape(1, -1)
    resized_vector = resized_mask.flatten().reshape(1, -1)
    #release memory
    del generated_temp, official_temp, generated_mask, official_mask
    gc.collect()

    #scipy method
    result_cos = cos_similarity(official_vector, resized_vector)
    result_dice = dice_similarity(official_vector, resized_vector)
    result_jaccard = jaccard_similarity(official_vector, resized_vector)
    result_pearson = pearson_similarity(official_vector, resized_vector)
    result_tanimoto = tanimoto_similarity(official_vector, resized_vector)

    #save
    core_name = os.path.split(official_mask_fullname)[1]
    name = os.path.splitext(core_name)[0]
    image_name = name.split('_mask')[0]

    outputs = (result_cos, result_dice, result_jaccard, result_pearson, result_tanimoto)

    return (image_name, outputs)

#entrance
if __name__ == '__main__':
    files_iteration()

reason: import numpy,import scipy


text:
import numpy as np
from scipy import sparse

from diffusion import Diffusion

class ConsistencyMethod(Diffusion):
    r"""
    Consistency Method Label Propagation Algorithm

    Parameters
    ----------
    graph : scipy.sparse
        Graph in which the labels should be diffused (before the kernel
        is built)
    proteins : pandas.DataFrame
        Indices of the proteins that conform the graph.
        This DataFrame can be built using the stand-alone 'utils' command
    terms : pandas.DataFrame
        Indices of the GO terms that will be mapped to the diffused seed.
        This DataFrame can be built using the stand-alone 'utils' command
    """
    def __init__(self, graph, proteins, terms):
        super(ConsistencyMethod, self).__init__()
        self.graph = graph
        self.proteins = proteins
        self.terms = terms
        self.kernel_params = {}
        self.latest_diffusion = None
        self.kernel = None
        self.beta = None

    def write_results(self, filename):
        r"""
        Write the results of the diffusion to the path pointed by `filename`
        the format will be TSV with the following columns:
            * protein
            * goterm
            * score

        Parameters
        ----------
        filename : str
            Path to write the results
        """
        Diffusion._write_results(self.latest_diffusion, self.proteins,
                                 self.terms, filename)

    def diffuse(self, initial_guess, **kwargs):
        r"""
        Diffuses the initial labelling `initial_guess` into
        the built kernel, if the kernel hasn't been built, it will
        build it using `compute_kernel`
        Parameters
        ----------
        initial_guess : scipy.sparse matrix
            The initial labelling matrix that will be diffused on the graph,
            shapes must be consistent to the given graph.

        Returns
        -------
        scipy.sparse.coo_matrix
            the new labelling after performing the label propagation

        Notes
        -----
        The final labelling is kept in `self.latest_diffusion`,
        for access convenience. This enables a subsequent
        call to `write_results` that does not require a re-calculation
        of the final labelling.
        """
        self.tell('Starting diffusion...')
        self.latest_diffusion = self.beta * self.kernel *\
            initial_guess.todense()
        self.latest_diffusion = sparse.coo_matrix(self.latest_diffusion)
        self.tell('done')
        return self.latest_diffusion

    def compute_kernel(self, **kwargs):
        r"""

        .. math:: (I - \alpha S)^{-1}

        Parameters
        ----------
        kwargs
            Parameters to compute the kernel, the following
            entries will be handled:
            * 'mu' : float
                To be used in the transformation described above.
                Defaults to 1.0
            * 'pinv' : boolean
                If True, the pseudo-inverse function will be used instead
                of the inverse, by default False

        Notes
        -----
        The kernel is available in `self.kernel`
        """

        if self.set_kernel_params(**kwargs):

            self.tell('Diffusion Kernel computation started...')
            # build D
            # n = self.graph.shape[0]
            sums = self.graph.sum(1)  # sum every column per row
            # in case there are unconnected parts in the matrix
            indNonZeros = np.where(sums != 0)
            diagonalValues = np.zeros(sums.shape)

            # degree = 1/np.sqrt(self.graph.sum(axis=1))
            # D = sparse.spdiags(degree.T, 0, n, n)

            diagonalValues[indNonZeros] = 1.0 / np.sqrt(sums[indNonZeros])
            D = sparse.spdiags(diagonalValues.T, 0,
                               diagonalValues.shape[0],
                               diagonalValues.shape[0])
            # build S
            S = D * self.graph * D

            mu = self.kernel_params['mu']
            alpha = 1 / (1 + mu)
            self.beta = mu / (1 + mu)

            IalphaS = sparse.eye(S.shape[0]) - alpha * S

            self.tell(r'Inverting (I - \alpha S)...')
            if self.kernel_params['pinv']:
                self.kernel = np.linalg.pinv(IalphaS.todense())
            else:
                self.kernel = np.linalg.inv(IalphaS.todense())
            self.tell('Kernel built')
        else:
            self.warning('Wrong parameters in Compute Kernel')
            raise ValueError('Wrong parameters in Compute Kernel')

    def set_kernel_params(self, **kwargs):
        self.tell('reading kernel parameters...')
        self.kernel_params['mu'] = kwargs.get('mu', 1.0)
        self.kernel_params['pinv'] = kwargs.get('pinv', False)
        self.tell(self.kernel_params)
        return True

reason: import numpy,from scipy


text:
import bisect
import bounded_kde as bk
import bz2
import gzip
import log_kde as lk
import numpy as np
import matplotlib.pyplot as pp
import os.path
import scipy.interpolate as si
import scipy.stats as ss
import scipy.stats.mstats as sm

def load_header_data(file, header_commented=False):
    """Load data from the file, using header for column names.

    :param file: A file object or filename.

    :param header_commented: If ``True``, discard the first character
      of header as comment marker."""

    def do_read(file):
        header=file.readline()

        if header_commented:
            header=header[1:].split()
        else:
            header=header.split()

        return np.loadtxt(file, dtype=[(h, np.float) for h in header])

    if isinstance(file, str):
        f,ext = os.path.splitext(file)

        if ext == '.gz':
            with gzip.open(file, 'r') as inp:
                return do_read(inp)
        elif ext == '.bz2':
            with bz2.BZ2File(file, 'r') as inp:
                return do_read(inp)
        else:
            with open(file, 'r') as inp:
                return do_read(inp)
    else:
        return do_read(inp)

def plot_emcee_chains(chain, truths=None, mean=True, fburnin=0):
    """Produces a chain plot of the mean values of each variable at each
    step.  The chain should have the shape ``(nwalkers, nsteps,
    nvars)``, and the resulting grid of plots will be as close to
    square as possible, showing the mean walker position at each step.

    :param chain: An array of shape ``(nwalkers, nsteps, nvars)``
      giving the history of the chain.

    :param truths: Either ``None`` or an iterable giving the truth
      values for each of the parameters, which will be plotted as a
      horizontal line.

    :param mean: If ``True`` (default) plot only the mean of the
      walker ensemble.  Otherwise, plot the evolution of each walker
      in the chain.

    :param fburnin: The fraction of points to discard at the beginning
      of the chain.

    """
    nk = chain.shape[2]
    n = int(np.ceil(np.sqrt(nk)))

    istart = int(round(fburnin*chain.shape[1]))

    for k in range(nk):
        pp.subplot(n,n,k+1)

        if mean:
            pp.plot(np.mean(chain[:,istart:,k], axis=0))
        else:
            pp.plot(chain[:,istart:,k].T)

        if truths is not None:
            pp.axhline(truths[k], color='k')

def plot_emcee_chains_one_fig(chain, fburnin=None):
    """Plots a single-figure representation of the chain evolution of the
    given chain.  The figure shows the evolution of the mean of each
    coordinate of the ensemble, normalised to zero-mean, unit-standard
    deviation.

    :param chain: The sampler chain, of shape ``(nwalkers, niter,
      nparams)``

    :param fburnin: If not ``None``, refers to the fraction of samples
      to discard at the beginning of the chain.

    """

    nk = chain.shape[2]
    if fburnin is None:
        istart = 0
    else:
        istart = int(round(fburnin*chain.shape[1]))

    for k in range(nk):
        mus = np.mean(chain[:,istart:,k], axis=0)
        mu = np.mean(mus)
        sigma = np.std(mus)

        pp.plot((mus - mu)/sigma)

def decorrelated_2d_histogram_pdf(pts, xmin=None, xmax=None, ymin=None, ymax=None):
    """Returns ``(XS, YS, ZS)``, with ``ZS`` of shape ``(Nx,Ny)`` and
    ``XS`` and ``YS`` of shape ``(Nx+1,Ny+1)`` giving the height and
    box corners, respectively, of the histogram estimate of the PDF
    from which ``pts`` are drawn.  The bin widths and orientations are
    chosen optimally for the convergence of the histogram to the true
    PDF under a squared-error metric; the automatic binning is tuned
    to work particularly well for multivariate Gaussians.

    Note: the first index of ZS varies with the X coordinate, while
    the second varies with the y coordinate.  This is consistent with
    :func:`pp.pcolor`, but inconsistent with :func:`pp.imshow` and
    :func:`pp.contour`.

    :param pts: The sample points, of shape ``(Npts,2)``.

    :param xmin: Minimum value in x.  If ``None``, use minimum data
      value.

    :param xmax: Maximum value in x.  If ``None``, use minimum data
      value.

    :param ymin: Minimum value in y.  If ``None``, use minimum data
      value.

    :param ymax: Maximum value in y.  If ``None``, use minimum data
      value."""

    if xmin is None:
        xmin = np.min(pts[:,0])
    if xmax is None:
        xmax = np.max(pts[:,0])
    if ymin is None:
        ymin = np.min(pts[:,1])
    if ymax is None:
        ymax = np.max(pts[:,1])

    cov=np.cov(pts, rowvar=0)
    mu=np.mean(pts, axis=0)

    # cov = L*L^T
    d, L = np.linalg.eig(cov)

    rescaled_pts = np.dot(pts-mu, L)
    rescaled_pts = rescaled_pts / np.reshape(np.sqrt(d), (1, 2))

    h = (42.5/pts.shape[0])**0.25

    Nx=int((np.max(rescaled_pts[:,0])-np.min(rescaled_pts[:,0]))/h + 0.5)
    Ny=int((np.max(rescaled_pts[:,1])-np.min(rescaled_pts[:,1]))/h + 0.5)

    H,xs,ys = np.histogram2d(rescaled_pts[:,0], rescaled_pts[:,1], bins=(Nx,Ny))

    # Backwards to account for the ordering in histogram2d.
    YS_RESCALED,XS_RESCALED=np.meshgrid(ys, xs)

    HPTS_RESCALED=np.column_stack((XS_RESCALED.flatten(),
                                   YS_RESCALED.flatten()))

    HPTS=np.dot(HPTS_RESCALED*np.reshape(np.sqrt(d), (1,2)), L.T) + mu

    XS=np.reshape(HPTS[:,0], (Nx+1,Ny+1))
    YS=np.reshape(HPTS[:,1], (Nx+1,Ny+1))

    return XS,YS,H

def interpolated_quantile(sorted_pts, quantile):
    """Returns a linearly interpolated quantile value.

    :param sorted_pts: A sorted array of points.

    :param quantile: The quantile desired."""

    N=sorted_pts.shape[0]

    idx=N*quantile
    lidx=int(np.floor(idx))
    hidx=int(np.ceil(idx))

    return (idx-lidx)*sorted_pts[lidx] + (hidx-idx)*sorted_pts[hidx]

def plot_interval(pts, levels, *args, **kwargs):
    """Plot probability intervals corresponding to ``levels`` in 1D.
    Additional args are passed to :func:`pp.axvline`.  The chosen
    levels are symmetric, in that they have equal probability mass
    outside the interval on each side.

    :param pts: Shape ``(Npts,)`` array of samples.

    :param levels: Sequence of levels to plot."""

    for level in levels:
        low,high = sm.mquantiles(pts, [0.5*(1.0-level), 0.5+0.5*level])
        pp.axvline(low, *args, **kwargs)
        pp.axvline(high, *args, **kwargs)

def plot_greedy_kde_interval_2d(pts, levels, xmin=None, xmax=None, ymin=None, ymax=None, Nx=100, Ny=100, cmap=None, colors=None, *args, **kwargs):
    """Plots the given probability interval contours, using a greedy
    selection algorithm.  Additional arguments passed to
    :func:`pp.contour`.

    The algorithm uses a two-step process (see `this document
    <https://dcc.ligo.org/LIGO-P1400054/public>`_) so that the
    resulting credible areas will be unbiased.

    :param pts: Array of shape ``(Npts, 2)`` that contains the points
      in question.

    :param levels: Sequence of levels (between 0 and 1) of probability
      intervals to plot.

    :param xmin: Minimum value in x.  If ``None``, use minimum data
      value.

    :param xmax: Maximum value in x.  If ``None``, use minimum data
      value.

    :param ymin: Minimum value in y.  If ``None``, use minimum data
      value.

    :param ymax: Maximum value in y.  If ``None``, use minimum data
      value.

    :param Nx: Number of subdivisions in x for contour plot.  (Default
      100.)

    :param Ny: Number of subdivisions in y for contour plot.  (Default
      100.)

    :param cmap: See :func:`pp.contour`.

    :param colors: See :func:`pp.contour`.

    """

    Npts=pts.shape[0]

    kde_pts = pts[:Npts/2,:]
    den_pts = pts[Npts/2:,:]

    Nkde = kde_pts.shape[0]
    Nden = den_pts.shape[0]

    kde=ss.gaussian_kde(kde_pts.T)
    den=kde(den_pts.T)
    densort=np.sort(den)[::-1]

    if xmin is None:
        xmin = np.min(pts[:,0])
    if xmax is None:
        xmax = np.max(pts[:,0])
    if ymin is None:
        ymin = np.min(pts[:,1])
    if ymax is None:
        ymax = np.max(pts[:,1])

    xs = np.linspace(xmin, xmax, Nx)
    ys = np.linspace(ymin, ymax, Ny)

    XS,YS=np.meshgrid(xs,ys)
    ZS=np.reshape(kde(np.row_stack((XS.flatten(), YS.flatten()))), (Nx, Ny))

    zvalues=[]
    for level in levels:
        ilevel = int(Nden*level + 0.5)
        if ilevel >= Nden:
            ilevel = Nden-1
        zvalues.append(densort[ilevel])

    pp.contour(XS, YS, ZS, zvalues, colors=colors, cmap=cmap, *args, **kwargs)

def greedy_kde_areas_2d(pts, levels, Nx=100, Ny=100, truth=None):
    """Returns an estimate of the area within the given credible levels
    for the posterior represented by ``pts``.

    The algorithm uses a two-step process (see `this document
    <https://dcc.ligo.org/LIGO-P1400054/public>`_) so that the
    resulting credible areas will be unbiased.

    :param pts: An ``(Npts, 2)`` array giving samples from the
      posterior.  The algorithm assumes that each point is an
      independent draw from the posterior.

    :param levels: The credible levels for which the areas are
      desired.

    :param Nx: The number of subdivisions along the first parameter to
      be used for the credible area integral.

    :param Ny: The number of subdivisions along the second parameter
      to be used for the credible area integral.

    :param truth: If given, then the area contained within the
      posterior contours that are more probable than the posterior
      evaluated at ``truth`` will be returned.  Also, the credible
      level that corresponds to truth is returned.  The area quantity
      is sometimes called the 'searched area', since it is the area a
      greedy search algorithm will cover before finding the true
      values.

    :return: If ``truth`` is None, ``areas``, an array of the same
      shape as ``levels`` giving the credible areas; if ``truth`` is
      not ``None`` then ``(areas, searched_area, p_value)``.

    """

    pts = np.random.permutation(pts)

    mu = np.mean(pts, axis=0)
    cov = np.cov(pts, rowvar=0)

    L = np.linalg.cholesky(cov)
    detL = L[0,0]*L[1,1]

    pts = np.linalg.solve(L, (pts - mu).T).T

    if truth is not None:
        truth = np.linalg.solve(L, truth-mu)

    Npts = pts.shape[0]
    kde_pts = pts[:Npts/2, :]
    den_pts = pts[Npts/2:, :]

    kde = ss.gaussian_kde(kde_pts.T)
    den = kde(den_pts.T)
    densort = np.sort(den)[::-1]

    xs = np.linspace(np.min(pts[:,0]), np.max(pts[:,0]), Nx)
    ys = np.linspace(np.min(pts[:,1]), np.max(pts[:,1]), Ny)

    dx = xs[1]-xs[0]
    dy = ys[1]-ys[0]

    xmids = 0.5*(xs[:-1] + xs[1:])
    ymids = 0.5*(ys[:-1] + ys[1:])

    XMIDS, YMIDS = np.meshgrid(xmids, ymids)
    ZS = kde(np.row_stack((XMIDS.flatten(), YMIDS.flatten()))).reshape(XMIDS.shape)

    areas = []
    for l in levels:
        d = densort[int(round(l*den_pts.shape[0]))]
        count = np.sum(ZS > d)
        areas.append(dx*dy*count*detL)

    if truth is not None:
        td = kde(truth)
        count = np.sum(ZS > td)
        tarea = dx*dy*count*detL
        index = bisect.bisect(densort[::-1], td)
        p_value = 1-float(index)/densort.shape[0]
        return areas, tarea, p_value
    else:
        return areas

def plot_greedy_histogram_interval_2d(pts, levels, xmin=None, xmax=None, ymin=None, ymax=None, Nx=100, Ny=100,
                                      cmap=None, colors=None, *args, **kwargs):
    """Plot probability interval contours estimated from a histogram
    PDF of the given points.  The number of bins in each dimension is
    chosen optimally for minimizing the squared error with a Gaussian
    PDF.  Additional arguments passed to :func:`pp.contour`.

    :param pts: Shape ``(Npts, 2)`` array of samples.

    :param levels: The probability interval levels to plot.

    :param xmin: Minimum value in x.  If ``None``, use minimum data
      value.

    :param xmax: Maximum value in x.  If ``None``, use minimum data
      value.

    :param ymin: Minimum value in y.  If ``None``, use minimum data
      value.

    :param ymax: Maximum value in y.  If ``None``, use minimum data
      value.

    :param Nx: Number of divisions in x for contour resolution.

    :param Ny: Number of divisions in y for contour resolution.

    :param cmap: See :func:`pp.contour`.

    :param colors: See :func:`pp.contour`."""

    if xmin is None:
        xmin = np.min(pts[:,0])
    if xmax is None:
        xmax = np.max(pts[:,0])
    if ymin is None:
        ymin = np.min(pts[:,1])
    if ymax is None:
        ymax = np.max(pts[:,1])

    Npts = pts.shape[0]

    XS,YS,H=decorrelated_2d_histogram_pdf(pts, xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax)

    XS_CENTER=0.25*(XS[:-1,:-1]+XS[:-1,1:]+XS[1:,:-1]+XS[1:,1:])
    YS_CENTER=0.25*(YS[:-1,:-1]+YS[:-1,1:]+YS[1:,:-1]+YS[1:,1:])

    pts_post=si.griddata(np.column_stack((XS_CENTER.flatten(),
                                          YS_CENTER.flatten())),
                         H.flatten(),
                         pts,
                         method='linear',
                         fill_value=0.0)

    ipost=np.argsort(pts_post)[::-1]

    post_levels = [pts_post[ipost[int(l*Npts+0.5)]] for l in levels]

    pxs=np.linspace(xmin, xmax, Nx)
    pys=np.linspace(ymin, ymax, Ny)

    PXS,PYS=np.meshgrid(pxs, pys)

    posts=si.griddata(np.column_stack((XS_CENTER.flatten(),
                                       YS_CENTER.flatten())),
                      H.flatten(),
                      np.column_stack((PXS.flatten(), PYS.flatten())),
                      method='linear',
                      fill_value=0.0)
    posts=np.reshape(posts, (Nx,Ny))

    pp.contour(PXS, PYS, posts, post_levels, colors=colors, cmap=cmap, origin='lower', extent=(xmin,xmax,ymin,ymax), *args, **kwargs)

def plot_kde_posterior(pts, xmin=None, xmax=None, N=100, periodic=False, low=None, high=None, log=False, *args, **kwargs):
    """Plots the a KDE estimate of the posterior from which ``pts``
    are drawn.  Extra arguments are passed to :func:`pp.plot`.

    :param pts: Shape ``(Npts,)`` array of samples.

    :param xmin: Minimum x value.  If ``None``, will be derived from
      ``pts``.

    :param xmax: Maximum x value.  If ``None``, will be derived from
      ``pts``.

    :param N: Number of intervals across ``(xmin, xmax)`` in plot.

    :param periodic: If true, then the function is periodic on the
      interval.

    :param low: If not ``None``, indicates a lower boundary for the
      domain of the PDF.

    :param high: If not ``None``, indicates an upper boundary for the
      domain of the PDF."""

    sigma = np.std(pts)

    if xmin is None and not log:
        xmin = np.min(pts)-0.5*sigma
        if low is not None:
            xmin = max(low, xmin)
    if xmax is None and not log:
        xmax = np.max(pts)+0.5*sigma
        if high is not None:
            xmax = min(high, xmax)

    if not log:
        xs=np.linspace(xmin, xmax, N)

    if periodic:
        period=xmax-xmin
        kde=ss.gaussian_kde(pts)
        pp.plot(xs, kde(xs)+kde(xs+period)+kde(xs-period), *args, **kwargs)
    elif low is not None or high is not None:
        kde=bk.Bounded_kde(pts, low=low, high=high)
        pp.plot(xs, kde(xs), *args, **kwargs)
    elif log:
        assert low is None, 'cannot impose low boundary in log-space'
        assert high is None, 'cannot impose high boundary in log-space'

        lpts = np.log(pts)
        sigma = np.std(lpts)

        if xmin is None or xmin < 0:
            xmin = np.min(lpts) - 0.5*sigma
        else:
            xmin = np.log(xmin)

        if xmax is None:
            xmax = np.max(lpts) + 0.5*sigma
        else:
            xmax = np.log(xmax)

        kde=lk.Log_kde(pts)

        xs = np.exp(np.linspace(xmin, xmax, N))

        pp.plot(xs, kde(xs), *args, **kwargs)
        pp.xscale('log')
    else:
        kde=ss.gaussian_kde(pts)
        pp.plot(xs, kde(xs), *args, **kwargs)

def plot_histogram_posterior(pts, xmin=None, xmax=None, log=False, **args):
    """Plots a histogram estimate of the posterior from which ``pts``
    are drawn.  Extra arguments are passed to :func:`pp.hist`.

    :param pts: Shape ``(Npts,)`` array of samples.

    :param xmin: Minimum x value.  If ``None``, will be derived from
      ``pts``.

    :param xmax: Maximum x value.  If ``None``, will be derived from
      ``pts``.

    :param fmt: Line format; see :func:`pp.plot`.

    :param log: If ``True`` compute and plot histogram in log-space.

    """

    if log:
        pts = np.log(pts)

    if xmin is None:
        xmin=np.min(pts)
    if xmax is None:
        xmax=np.max(pts)

    Npts=pts.shape[0]
    spts=np.sort(pts)

    iqr = spts[int(Npts*0.75+0.5)] - spts[int(Npts*0.25 + 0.5)]

    # Optimal for Gaussian PDF in 1D; minimizes the squared error
    # between true and histogram PDF.
    h = iqr/1.35*(42.5/Npts)**(1.0/3.0)

    Nbins = int((xmax-xmin)/h + 0.5)

    if log:
        pp.hist(np.exp(pts), bins=np.exp(np.linspace(xmin, xmax, Nbins+1)), **args)
    else:
        pp.hist(pts, bins=Nbins, **args)

    # Just because matplotlib sometimes puts the y-axis above 0:
    pp.axis(ymin=0)

    if log:
        pp.xscale('log')

def plot_kde_posterior_2d(pts, xmin=None, xmax=None, ymin=None, ymax=None, Nx=100, Ny=100, cmap=None, log=False):
    """Plot a 2D KDE estimated posterior.

    :param pts: A ``(Npts, 2)`` array of points.

    :param xmin: Minimum x-coordinate.  If ``None``, use the minimum
      value from ``pts``.

    :param xmax: Maximum x-coordinate.  If ``None``, use the maximum
      value from ``pts``.

    :param ymin: Minimum y-coordinate.  If ``None``, use the minimum
      value from ``pts``.

    :param ymax: Maximum y-coordinate.  If ``None``, use the maximum
      value from ``pts``.

    :param Nx: The number of pixels in the x direction.

    :param Ny: The number of pixels in the y direction.

    :param cmap: The colormap, passed to :func:`pp.imshow`.

    :param log: If ``True`` compute and plot the density in log-space.

    """

    if log:
        pts = np.log(pts)

    if xmin is None:
        xmin = np.min(pts[:,0])
    if xmax is None:
        xmax = np.max(pts[:,0])
    if ymin is None:
        ymin = np.min(pts[:,1])
    if ymax is None:
        ymax = np.max(pts[:,1])

    kde=ss.gaussian_kde(pts.T)
    XS,YS=np.meshgrid(np.linspace(xmin, xmax, Nx),
                      np.linspace(ymin, ymax, Ny))
    XCENTERS = 0.25*(XS[:-1, :-1] + XS[:-1, 1:] + XS[1:,:-1] + XS[1:, 1:])
    YCENTERS = 0.25*(YS[:-1, :-1] + YS[:-1, 1:] + YS[1:,:-1] + YS[1:, 1:])
    ZS=np.reshape(kde(np.row_stack((XCENTERS.flatten(), YCENTERS.flatten()))), (Nx-1, Ny-1))

    if log:
        XS = np.exp(XS)
        YS = np.exp(YS)

        xmin = np.exp(xmin)
        xmax = np.exp(xmax)
        ymin = np.exp(ymin)
        ymax = np.exp(ymax)

    pp.pcolormesh(XS, YS, ZS, cmap=cmap)
    pp.axis(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax)

    if log:
        pp.xscale('log')
        pp.yscale('log')

def plot_histogram_posterior_2d(pts, log=False, cmap=None):
    """Plots a 2D histogram density of the given points.

    :param pts: An ``(Npts, 2)`` array of points.

    :param log: If ``True``, then compute and plot the histogram in
      log-space.

    :param cmap: Passed to :func:`pp.imshow` as colormap.

    """

    if log:
        XS,YS,HS=decorrelated_2d_histogram_pdf(np.log(pts))
        XS = np.exp(XS)
        YS = np.exp(YS)
    else:
        XS,YS,HS=decorrelated_2d_histogram_pdf(pts)

    xmin=np.min(XS.flatten())
    xmax=np.max(XS.flatten())
    ymin=np.min(YS.flatten())
    ymax=np.max(YS.flatten())

    # Plot a zero-level background...
    pp.pcolormesh(np.array([[xmin, xmax], [xmin, xmax]]),
                  np.array([[ymin, ymin], [ymax, ymax]]),
                  np.array([[0.0]]), cmap=cmap)

    pp.pcolormesh(XS,YS,HS, cmap=cmap)

    pp.axis(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax)

    if log:
        pp.xscale('log')
        pp.yscale('log')

def plot_cumulative_distribution(pts, *args, **kwargs):
    """Plots the 1D normalized empirical CDF for the given points.
    Additional arguments are passed to matplotlib's ``plot``.

    """
    pts = np.atleast_1d(pts)
    pts = np.sort(np.concatenate(([0], pts)))

    pp.plot(pts, np.linspace(0, 1, pts.shape[0]), *args, **kwargs)


text:
import numpy
import logging
import theano
import theano.tensor as TT
import theano.tensor.nnet
from sparnn.utils import *
from sparnn.layers import Layer

logger = logging.getLogger(__name__)

class AggregatePoolingLayer(Layer):
    def __init__(self, layer_param):
        super(AggregatePoolingLayer, self).__init__(layer_param)
        self.pooling_func = layer_param["pooling_func"]
        self.fprop()

    def set_name(self):
        self.name = "AggregatePoolingLayer-" + str(self.id)

    def step_fprop(self, input, mask=None):
        output = quick_aggregate_pooling(input, self.pooling_func, mask)
        return output

    def fprop(self):
        self.output = self.step_fprop(self.input, self.mask)

max_stars_repo_name: JoeinChina/DeepWeather

max_issues_repo_name: JoeBuzh/DeepWeather

max_forks_repo_name: JoeBuzh/DeepWeather

reason: import numpy,import theano


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

# 通过rcParams设置全局横纵轴字体大小
mpl.rcParams['xtick.labelsize'] = 24
mpl.rcParams['ytick.labelsize'] = 24

np.random.seed(42)

# x轴的采样点
x = np.linspace(0, 5, 100)
print(x)
print(type(x))

# 通过下面曲线加上噪声生成数据，所以拟合模型就用y了……
y = 2*np.sin(x) + 0.3*x**2
y_data = y + np.random.normal(scale=0.3, size=100)
print(y_data)
print(type(y_data))

# figure()指定图表名称
plt.figure('data')

# '.'标明画散点图，每个散点的形状是个圆
plt.plot(x, y_data, '.')

# 画模型的图，plot函数默认画连线图
plt.figure('model')
plt.plot(x, y)

# 两个图画一起
plt.figure('data & model')

# 通过'k'指定线的颜色，lw指定线的宽度
# 第三个参数除了颜色也可以指定线形，比如'r--'表示红色虚线
# 更多属性可以参考官网：http://matplotlib.org/api/pyplot_api.html
plt.plot(x, y, 'k', lw=3)

# scatter可以更容易地生成散点图
plt.scatter(x, y_data)

# 将当前figure的图保存到文件result.png
plt.savefig('result.png')

# 一定要加上这句才能让画好的图显示在屏幕上
plt.show()


text:
import json
import os
import matplotlib.pyplot as plt
import numpy as np
import re
import datetime as dt
from datetime import timedelta
import timestring

def reactions():
    loc = raw_input('Enter facebook archive extracted location: ')
    if not os.path.isdir(loc):
        print("The provided location doesn't seem to be right")
        exit(1)

    fname = loc+'/likes_and_reactions/posts_and_comments.json'
    if not os.path.isfile(fname):
        print("The file posts_and_commments.json is not present at the entered location.")
        exit(1)

    with open(fname) as f:
        base_data = json.load(f)

    data = base_data['reactions']
    reactions = []
    count = []

    # Counting the occurence of each reaction

    for ele in data:
        reaction = ele['data'][0]['reaction']['reaction']
        reactions.append(reaction)

    count.append(reactions.count("LIKE"))
    count.append(reactions.count("HAHA"))
    count.append(reactions.count("WOW"))
    count.append(reactions.count("LOVE"))
    count.append(reactions.count("ANGER"))
    count.append(reactions.count("SORRY"))

    # Plotting the counts

    x = np.array([0,1,2,3,4,5])
    y = np.array(count)
    x_ticks = ['LIKE', 'HAHA', 'WOW', 'LOVE', 'ANGER', 'SORRY']
    plt.xticks(x,x_ticks)
    plt.plot(x,y,linestyle='--',marker='o')
    plt.ylabel('Frequency')
    plt.show()

    # Top10 friends whom you are most likely to react to

    pattern1 = r"(?:likes ).+\b"
    pattern2 = r"(?:to ).+\b"
    names = []
    for ele in data:
        title = ele["title"]
        first_names = re.findall(pattern1, title)
        if len(first_names)>0:
            if len(first_names[0].split())>1:
                names.append(first_names[0].split()[1] + " " + first_names[0].split()[2])
        else:
            first_names = re.findall(pattern2, title)
            if len(first_names)>0:
                if len(first_names[0].split())>1:
                    names.append(first_names[0].split()[1] + " " + first_names[0].split()[2])

    name_counter = {}
    totalCnt=0
    for name in names:
        if name in name_counter:
            name_counter[name]+=1
        else:
            name_counter[name]=1
        totalCnt+=1
    #print(name_counter)

    popular_names = sorted(name_counter,key = name_counter.get, reverse = True)
    top_10 = popular_names[:10]
    top_10per = []
    x_ticks = []
    for friend in top_10:
        per = (name_counter[friend] / totalCnt) * 100
        friend = re.sub('\'s', '', friend)
        top_10per.append(per)
        x_ticks.append(friend)

    x = np.array([0,1,2,3,4,5,6,7,8,9])
    y = np.array(top_10per)
    #x_ticks = ['LIKE', 'HAHA', 'WOW', 'LOVE', 'ANGER', 'SORRY']
    plt.xticks(x,x_ticks,rotation = 45)
    plt.plot(x,y,linestyle='--',marker='o')
    plt.ylabel('Percentage of Reactions')
    plt.tight_layout()
    plt.show()

    # Month Wise Distribution of Reactions

    count_month = [0]*12
    for ele in data:
        timestamp = ele['timestamp']
        month = dt.datetime.fromtimestamp(timestamp).month
        count_month[month-1]+=1
    plt.plot(count_month,linestyle="--", marker="^", color="g")
    plt.ylabel("Frequency")
    plt.xlabel("Month Number")
    plt.show()

    # Line plot for each reaction

    rxnList = ['LIKE', 'HAHA', 'WOW', 'LOVE', 'ANGER', 'SORRY']
    for rxn in rxnList:
        dataTemp = [item for item in data if item["data"][0]["reaction"]["reaction"]==rxn]
        dates = [timestring.Date(i["timestamp"]).date for i in dataTemp]
        dates.reverse()

        firstdate = dates[0]
        maxdays = int((dates[-1] - firstdate).total_seconds() / 86400) + 1
        reactionCount = [0]*int(maxdays)

        for i in range(len(dates)):
            days_diff = (dates[i] - firstdate).total_seconds() / 86400
            reactionCount[int(days_diff)] += 1

        xaxis = [ dt.datetime.now() - timedelta(days=maxdays-i) for i in range(maxdays) ]
        cumulative_reactions = np.cumsum(reactionCount).tolist()

        plt.plot(xaxis,cumulative_reactions,linewidth=3.0, label=rxn)
    plt.legend(loc='upper left')
    plt.title("Reactions on posts", fontsize=16, fontweight='bold')
    plt.xlabel("Time")
    plt.ylabel("Cumulative Sum of Reactions")
    plt.show()

if __name__ == '__main__':
    reactions()

max_stars_repo_name: purvaudai/facebook-archive

max_issues_repo_name: purvaudai/facebook-archive

max_forks_repo_name: purvaudai/facebook-archive


text:
import cv2
import numpy as np

# Load image, convert to HSV, color threshold to get mask
image = cv2.imread('1.png')
original = image.copy()
blank = np.zeros(image.shape[:2], dtype=np.uint8)
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
lower = np.array([0, 0, 0])
upper = np.array([179, 255, 165])
mask = cv2.inRange(hsv, lower, upper)

# Merge text into a single contour
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
close = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel, iterations=3)

# Find contours
cnts = cv2.findContours(close, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    # Filter using contour area and aspect ratio
    x,y,w,h = cv2.boundingRect(c)
    area = cv2.contourArea(c)
    ar = w / float(h)
    if (ar > 1.4 and ar < 4) or ar < .85 and area > 100:
        # Find rotated bounding box
        rect = cv2.minAreaRect(c)
        box = cv2.boxPoints(rect)
        box = np.int0(box)
        cv2.drawContours(image,[box],0,(36,255,12),2)
        cv2.drawContours(blank,[box],0,(255,255,255),-1)

# Bitwise operations to isolate text
extract = cv2.bitwise_and(mask, blank)
extract = cv2.bitwise_and(original, original, mask=extract)
extract[extract==0] = 255

cv2.imshow('mask', mask)
cv2.imshow('image', image)
cv2.imshow('close', close)
cv2.imshow('extract', extract)
cv2.waitKey()

max_stars_repo_name: nathancy/stackoverflow

max_issues_repo_name: nathancy/stackoverflow

max_forks_repo_name: nathancy/stackoverflow


text:
from __future__ import absolute_import

import numpy as np
import tensornets as nets
from tensornets.utils import load_img
import os
import pytest

pytestmark = pytest.mark.skipif(
    os.environ.get('CORE_CHANGED', 'True') == 'False',
    reason='Runs only when the relevant files have been modified.')

def test_load_img():
    x = load_img('cat.png')
    assert x.shape == (1, 733, 490, 3)

    x = load_img(['cat.png', 'cat.png'], target_size=(100, 200))
    assert x.shape == (2, 100, 200, 3)

    x = load_img(['cat.png'] * 3, target_size=(100, 200), crop_size=50)
    assert x.shape == (3, 50, 50, 3)

    with pytest.raises(ValueError):
        x = load_img(['cat.png', 'cat.png'])

    with pytest.raises(ValueError):
        x = load_img(['cat.png'] * 3, target_size=100)

max_stars_repo_name: oliver-pola/tensornets

max_issues_repo_name: oliver-pola/tensornets

max_forks_repo_name: oliver-pola/tensornets


text:
from parallelEnv import parallelEnv
import matplotlib
import matplotlib.pyplot as plt
import torch
import numpy as np
from JSAnimation.IPython_display import display_animation
from matplotlib import animation
from IPython.display import display
import random as rand

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

# collect trajectories for a parallelized parallelEnv object
def collect_trajectories(envs, policy, tmax=200, nrand=5):

    # number of parallel instances
    n=len(envs.ps)

    #initialize returning lists and start the game!
    state_list=[]
    reward_list=[]
    action_list=[]

    states = envs.reset()

    for t in range(tmax):

        # probs will only be used as the pi_old
        # no gradient propagation is needed
        # so we move it to the cpu
        actions = policy(states).squeeze().cpu().detach().numpy()

        next_states,rewards,dones,_ = envs.step(actions)

        # store the result
        state_list.append(states)
        reward_list.append(rewards)
        action_list.append(action)

        # stop if any of the trajectories is done
        # we want all the lists to be retangular
        if np.any(dones):
            break

    # return pi_theta, states, actions, rewards, probability
    return state_list, action_list, reward_list

def clipped_surrogate(policy, old_actions, states, actions, rewards,
                      discount=0.995,
                      epsilon=0.1, beta=0.01):

    discount = discount**np.arange(len(rewards))
    rewards = np.asarray(rewards)*discount[:,np.newaxis]

    # convert rewards to future rewards
    rewards_future = rewards[::-1].cumsum(axis=0)[::-1]

    mean = np.mean(rewards_future, axis=1)
    std = np.std(rewards_future, axis=1) + 1.0e-10

    rewards_normalized = (rewards_future - mean[:,np.newaxis])/std[:,np.newaxis]

    # convert everything into pytorch tensors and move to gpu if available
    actions = torch.tensor(actions, dtype=torch.int8, device=device)
    old_actions = torch.tensor(old_actions, dtype=torch.float, device=device)
    rewards = torch.tensor(rewards_normalized, dtype=torch.float, device=device)

    # ratio for clipping
    ratio = new_actions/old_actions

    # clipped function
    clip = torch.clamp(ratio, 1-epsilon, 1+epsilon)
    clipped_surrogate = torch.min(ratio*rewards, clip*rewards)

    # include a regularization term
    # this steers new_policy towards 0.5
    # add in 1.e-10 to avoid log(0) which gives nan
    entropy = -(new_probs*torch.log(old_probs+1.e-10)+ \
        (1.0-new_probs)*torch.log(1.0-old_probs+1.e-10))

    # this returns an average of all the entries of the tensor
    # effective computing L_sur^clip / T
    # averaged over time-step and number of trajectories
    # this is desirable because we have normalized our rewards
    return torch.mean(clipped_surrogate + beta*entropy)