text:
import numpy as np
import pandas as pd
import os
import os.path as osp

# import pytorch packages
import torch
from torch.autograd import Variable
import torch.nn as nn
from torch.utils import data

from tqdm.auto import tqdm
import emoji

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

def to_emoji(emoji_dict, label):
    """ Cast a numerical label to the emoji
    """

    emoji_new = emoji.emojize(emoji_dict[label],use_aliases=True)

    return emoji_new

emoji_dictionary = {0: "\u2764\uFE0F",    # :heart: prints a black instead of red heart depending on the font
                    1: ":baseball:",
                    2: ":smile:",
                    3: ":disappointed:",
                    4: ":fork_and_knife:"}

class emoji_dataset(data.Dataset):

    def __init__(self, data_root, data_name):
        """
        """

        self.dataset = pd.read_csv(osp.join(data_root, data_name), header = None)
        self.length = len(self.dataset)
        self.data = self.dataset[0]
        self.labels = self.dataset[1]

    def __len__(self):
        return self.length

    def data(self,index):
        """return the data.
        """
        return self.data[index]

    def label(self,index):
        """return the labels of dataset.
        """
        return self.labels[index]

    def __getitem__(self, index):

        X = self.data[index]
        y = self.labels[index]

        return X, y

def read_glove(name):
    """Given the path/name of the glove file, return the words(set) and word2vec_map(a python dict)
    """
    file = open(name, 'r')
    # Create set for words and a dictionary for words and their corresponding
    words = set()
    word2vec_map = {}

    data = file.readlines()
    for line in data:
        # add word to the words set.
        word = line.split()[0]
        words.add(word)

        word2vec_map[word] = np.array(line.split()[1:], dtype = np.float64)

    i = 1
    word2index = {}
    index2word = {}
    for word in words:
        word2index[word] = i
        index2word[i] = word
        i = i+1

    return words, word2vec_map, word2index, index2word

def to_index(sentences, word2index, max_length):
    """ Given the word2index dict, maximum length, and inputs, return the numerical inputs
    """
    num = len(sentences)
    out = torch.zeros(num, max_length).long()

    for idx, sen in enumerate(sentences):

        sen = sen.lower().split()

        j = 0

        for word in sen:
            word_idx = word2index[word]
            out[idx, j] = word_idx
            j += 1

            if j >= max_length:
                break

    return out

class Emoji_Net(nn.Module):
    """ The emoji net uses embedding layer, lstm layer and fully-connected layer.
    """
    def __init__(self,layer_num,input_dim, hidden_dim, output_dim, weights):
        super(Emoji_Net, self).__init__()
        self.input_dim = input_dim
        self.layer_num = layer_num
        self.output_dim = output_dim
        self.hidden_dim = hidden_dim

        # the embedding layer
        weights = weights.to(device)
        self.embedding = nn.Embedding.from_pretrained(weights)

        # the lstm layer
        self.lstm = nn.LSTM(input_size = self.input_dim, hidden_size = self.hidden_dim,
                            num_layers = self.layer_num, batch_first = True, dropout = 0.8,bidirectional = True)
        self.dropout = nn.Dropout(0.6)

        # the output layer
        self.fc = nn.Linear(hidden_dim*2, output_dim)

    def forward(self, x):

        # h0
        h0 = Variable(torch.zeros(2*self.layer_num, x.size(0), self.hidden_dim)).to(device)

        # c0
        c0 = Variable(torch.zeros(2*self.layer_num, x.size(0), self.hidden_dim)).to(device)

        # embedding
        x = self.embedding(x)
        # lstm
        x, (hn ,cn) = self.lstm(x, (h0, c0))
        x = self.dropout(x)

        # output layer
        x = self.fc(x[:, -1, :])

        return x

def save_best(is_best, best_accuracy, model, epoch, path):
    filename = path + 'best_model.pth'

    if is_best:
        if not osp.exists(path):
            os.makedirs(path)
        torch.save({'epoch':epoch,
                   'model_state_dict':model.state_dict(),
                    'best_accuracy':best_accuracy
                   }, filename)

        print(best_accuracy)

ext: py

reason: import numpy


text:
import sys
import json
from os.path import join, isdir, isfile
from os import makedirs
from diffco import DiffCo, MultiDiffCo, CollisionChecker
from diffco import kernel
from matplotlib import pyplot as plt
import numpy as np
import torch
from diffco.model import RevolutePlanarRobot
import fcl
from scipy import ndimage
from matplotlib import animation
from matplotlib.patches import Rectangle, FancyBboxPatch, Circle
import seaborn as sns
sns.set()
import matplotlib.patheffects as path_effects
from diffco import utils
from diffco.Obstacles import FCLObstacle
from diffco.FCLChecker import FCLChecker
from speed_compare import gradient_free_traj_optimize, givengrad_traj_optimize

def traj_optimize(robot, dist_est, start_cfg, target_cfg, history=False):
    N_WAYPOINTS = 20
    NUM_RE_TRIALS = 5
    UPDATE_STEPS = 200
    dif_weight = 1
    max_move_weight = 10
    collision_weight = 10
    safety_margin = torch.FloatTensor([0]) #-3,  #-1 (shown)
    lr = 5e-1

    lowest_cost_solution = None
    lowest_cost = np.inf
    lowest_cost_trial = None
    lowest_cost_step = None
    best_valid_solution = None
    best_valid_cost = np.inf
    best_valid_step = None
    best_valid_trial = None

    trial_histories = []

    found = False
    for trial_time in range(NUM_RE_TRIALS):
        path_history = []
        if trial_time == 0 and False: # Temp
            init_path = torch.from_numpy(np.linspace(start_cfg, target_cfg, num=N_WAYPOINTS))
        else:
            init_path = (torch.rand(N_WAYPOINTS, robot.dof))*np.pi*2-np.pi
        init_path[0] = start_cfg
        init_path[-1] = target_cfg
        p = init_path.requires_grad_(True)
        opt = torch.optim.Adam([p], lr=lr)

        for step in range(UPDATE_STEPS):
            opt.zero_grad()
            collision_score = torch.clamp(dist_est(p)-safety_margin, min=0).sum()
            control_points = robot.fkine(p)
            max_move_cost = torch.clamp((control_points[1:, -1:]-control_points[:-1, -1:]).pow(2).sum(dim=2)-1**2, min=0).sum() \
                + torch.clamp((utils.wrap2pi(p[1:]-p[:-1])).pow(2).sum(dim=1)-(5*np.pi/180)**2, min=0).sum()
            diff = (control_points[1:]-control_points[:-1]).pow(2).sum()
            constraint_loss = collision_weight * collision_score + max_move_weight * max_move_cost
            objective_loss = dif_weight * diff
            loss = objective_loss + constraint_loss
            loss.backward()
            p.grad[[0, -1]] = 0.0
            opt.step()
            p.data = utils.wrap2pi(p.data)
            if history:
                path_history.append(p.data.clone())
            if constraint_loss.data.numpy() < lowest_cost:
                lowest_cost = loss.data.numpy()
                lowest_cost_solution = p.data.clone()
                lowest_cost_step = step
                lowest_cost_trial = trial_time
            if constraint_loss <= 1e-2:
                if objective_loss.data.numpy() < best_valid_cost:
                    best_valid_cost = objective_loss.data.numpy()
                    best_valid_solution = p.data.clone()
                    best_valid_step = step
                    best_valid_trial = trial_time
            if constraint_loss <= 1e-2 or step % (UPDATE_STEPS/5) == 0 or step == UPDATE_STEPS-1:
                print('Trial {}: Step {}, collision={:.3f}*{:.1f}, max_move={:.3f}*{:.1f}, diff={:.3f}*{:.1f}, Loss={:.3f}'.format(
                    trial_time, step,
                    collision_score.item(), collision_weight,
                    max_move_cost.item(), max_move_weight,
                    diff.item(), dif_weight,
                    loss.item()))
        trial_histories.append(path_history)

        if best_valid_solution is not None:
            found = True
            break
    if not found:
        print('Did not find a valid solution after {} trials!\
            Giving the lowest cost solution'.format(NUM_RE_TRIALS))
        solution = lowest_cost_solution
        solution_step = lowest_cost_step
        solution_trial = lowest_cost_trial
    else:
        solution = best_valid_solution
        solution_step = best_valid_step
        solution_trial = best_valid_trial
    path_history = trial_histories[solution_trial] # Could be empty when history = false
    if not path_history:
        path_history.append(solution)
    else:
        path_history = path_history[:(solution_step+1)]
    return solution, path_history, solution_trial, solution_step # sum(trial_histories, []),

def create_plots(robot, obstacles, dist_est, checker):
    from matplotlib.cm import get_cmap
    cmaps = [get_cmap('Reds'), get_cmap('Blues')]

    if robot.dof > 2:
        fig = plt.figure(figsize=(3, 3))
        ax = fig.add_subplot(111)
    elif robot.dof == 2:
        # Show C-space at the same time
        num_class = getattr(checker, 'num_class', 1)

        fig = plt.figure(figsize=(3*(num_class), 3*(num_class+1)))
        plt.rcParams.update({
            "text.usetex": True,
            "font.family": "sans-serif",
            "font.sans-serif": ["Helvetica"]})
        gs = fig.add_gridspec(num_class+1, num_class)
        ax = fig.add_subplot(gs[:-1, :])
        cfg_path_plots = []

        size = [400, 400]
        yy, xx = torch.meshgrid(torch.linspace(-np.pi, np.pi, size[0]), torch.linspace(-np.pi, np.pi, size[1]))
        grid_points = torch.stack([xx, yy], dim=2).reshape((-1, 2))
        grid_points = grid_points.double() if checker.support_points.dtype == torch.float64 else grid_points
        score_spline = dist_est(grid_points).reshape(size+[num_class])
        c_axes = []

        # 여기여기여기여기여기여기
        with sns.axes_style('ticks'): # 그리드가 없음
            for cat in range(num_class):
                c_ax = fig.add_subplot(gs[-1, cat])
                score = score_spline[:, :, cat] # estimated score grid
                color_mesh = c_ax.pcolormesh(xx, yy, score, cmap=cmaps[cat], vmin=-torch.abs(score).max(), vmax=torch.abs(score).max())
                if checker.gains.dim() == 1:
                    c_support_points = checker.support_points[checker.gains != 0]
                else:
                    c_support_points = checker.support_points[checker.gains[:, cat] != 0]
                c_ax.scatter(c_support_points[:, 0], c_support_points[:, 1], marker='.', c='black', s=1.5)
                c_ax.contour(xx, yy, score, levels=10, linewidths=1, alpha=0.4, ) #-1.5, -0.75, 0, 0.3
                for _ in range(4):
                    cfg_path, = c_ax.plot([], [], '-o', c='olivedrab', markersize=3)
                    cfg_path_plots.append(cfg_path)

                c_ax.set_aspect('equal', adjustable='box')
                c_ax.set_xlim(-np.pi, np.pi)
                c_ax.set_ylim(-np.pi, np.pi)
                c_ax.set_xticks([-np.pi, 0, np.pi])
                c_ax.set_xticklabels(['$-\pi$', '$0$', '$\pi$'])
                c_ax.set_yticks([-np.pi, 0, np.pi])
                c_ax.set_yticklabels(['$-\pi$', '$0$', '$\pi$'])

    # Plot ostacles
    ax.set_xlim(-8, 8)
    ax.set_ylim(-8, 8)
    ax.set_aspect('equal', adjustable='box')
    ax.set_xticks([-4, 0, 4])
    ax.set_yticks([-4, 0, 4])
    for obs in obstacles:
        cat = obs[3] if len(obs) >= 4 else 1
        if obs[0] == 'circle':
            ax.add_patch(Circle(obs[1], obs[2], path_effects=[path_effects.withSimplePatchShadow()], color=cmaps[cat](0.5)))
        elif obs[0] == 'rect':
            ax.add_patch(Rectangle((obs[1][0]-float(obs[2][0])/2, obs[1][1]-float(obs[2][1])/2), obs[2][0], obs[2][1], path_effects=[path_effects.withSimplePatchShadow()],
            color=cmaps[cat](0.5)))

    # Placeholder of the robot plot
    trans = ax.transData.transform
    lw = ((trans((1, robot.link_width))-trans((0,0)))*72/ax.figure.dpi)[1]
    link_plot, = ax.plot([], [], color='silver', alpha=0.1, lw=lw, solid_capstyle='round', path_effects=[path_effects.SimpleLineShadow(), path_effects.Normal()])
    joint_plot, = ax.plot([], [], 'o', color='tab:red', markersize=lw)
    eff_plot, = ax.plot([], [], 'o', color='black', markersize=lw)

    if robot.dof > 2:
        return fig, ax, link_plot, joint_plot, eff_plot
    elif robot.dof == 2:
        return fig, ax, link_plot, joint_plot, eff_plot, cfg_path_plots

def single_plot(robot, p, fig, link_plot, joint_plot, eff_plot, cfg_path_plots=None, path_history=None, save_dir=None, ax=None):
    points_traj = torch.cat([torch.zeros(len(p), 1, 2), robot.fkine(p)], dim=1)
    traj_alpha = 0.3
    ends_alpha = 0.5

    lw = link_plot.get_lw()
    link_traj = [ax.plot(points[:, 0], points[:, 1], color='gray', alpha=traj_alpha, lw=lw, solid_capstyle='round')[0] for points in points_traj]
    joint_traj = [ax.plot(points[:-1, 0], points[:-1, 1], 'o', color='tab:red', alpha=traj_alpha, markersize=lw)[0] for points in points_traj]
    eff_traj = [ax.plot(points[-1:, 0], points[-1:, 1], 'o', color='black', alpha=traj_alpha, markersize=lw)[0] for points in points_traj]
    for i in [0, -1]:
        link_traj[i].set_alpha(ends_alpha)
        link_traj[i].set_path_effects([path_effects.SimpleLineShadow(), path_effects.Normal()])
        joint_traj[i].set_alpha(ends_alpha)
        eff_traj[i].set_alpha(ends_alpha)
    link_traj[0].set_color('green')
    link_traj[-1].set_color('orange')

    offset = torch.FloatTensor([[0, 0], [0, 1], [-1, 1], [-1, 0]]) * np.pi*2
    for i, cfg_path in enumerate(cfg_path_plots):
        cfg_path.set_data(p[:, 0]+offset[i%4, 0], p[:, 1]+offset[i%4, 1])

def escape(robot, dist_est, start_cfg):
    N_WAYPOINTS = 20
    UPDATE_STEPS = 200

    safety_margin = -0.3
    lr = 5e-2
    path_history = []
    init_path = start_cfg
    p = init_path.requires_grad_(True)
    opt = torch.optim.Adam([p], lr=lr)

    for step in range(N_WAYPOINTS):
        if step % 1 == 0:
            path_history.append(p.data.clone())
        opt.zero_grad()
        collision_score = dist_est(p)-safety_margin
        loss = collision_score
        loss.backward()
        opt.step()
        p.data = utils.wrap2pi(p.data)
        if collision_score <= 1e-4:
            break

    return torch.stack(path_history, dim=0)

def main(checking_method='diffco'):
    DOF = 2
    env_name = '1rect_active'

    dataset = torch.load('data/2d_{}dof_{}.pt'.format(DOF, env_name))
    cfgs = dataset['data'].double()
    labels = dataset['label'].reshape(-1, 1).double()
    dists = dataset['dist'].reshape(-1, 1).double()
    obstacles = dataset['obs']
    obstacles = [list(o) for o in obstacles]
    robot = dataset['robot'](*dataset['rparam'])
    width = robot.link_width

    #=================================================================================================================================
    fcl_obs = [FCLObstacle(*param) for param in obstacles]
    fcl_collision_obj = [fobs.cobj for fobs in fcl_obs]

    label_type = 'binary'
    num_class = 1

    T = 11
    nu = 5 #5
    kai = 1500
    sigma = 0.3
    seed = 1918
    torch.manual_seed(seed)
    np.random.seed(seed)

    num_init_points = 8000
    if label_type == 'binary':
        obs_managers = [fcl.DynamicAABBTreeCollisionManager()]
        obs_managers[0].registerObjects(fcl_collision_obj)
        obs_managers[0].setup()
    elif label_type == 'instance':
        obs_managers = [fcl.DynamicAABBTreeCollisionManager() for _ in fcl_obs]
        for mng, cobj in zip(obs_managers, fcl_collision_obj):
            mng.registerObjects([cobj])
    elif label_type == 'class':
        obs_managers = [fcl.DynamicAABBTreeCollisionManager() for _ in range(num_class)]
        obj_by_cls = [[] for _ in range(num_class)]
        for obj in fcl_obs:
            obj_by_cls[obj.category].append(obj.cobj)
        for mng, obj_group in zip(obs_managers, obj_by_cls):
            mng.registerObjects(obj_group)

    robot_links = robot.update_polygons(cfgs[0])
    robot_manager = fcl.DynamicAABBTreeCollisionManager()
    robot_manager.registerObjects(robot_links)
    robot_manager.setup()
    for mng in obs_managers:
        mng.setup()
    gt_checker = FCLChecker(obstacles, robot, robot_manager, obs_managers)

    train_num = 6000
    fkine = robot.fkine
    from time import time
    init_train_t = time()
    checker = MultiDiffCo(obstacles, kernel_func=kernel.FKKernel(fkine, kernel.RQKernel(10)), beta=1.0)
    labels, dists = gt_checker.predict(cfgs[:train_num], distance=True)
    labels = labels.double()
    dists = dists.double()
    checker.train(cfgs[:train_num], labels[:train_num], max_iteration=len(cfgs[:train_num]), distance=dists[:train_num])

    fitting_target = 'dist'
    Epsilon = 0.01
    checker.fit_poly(kernel_func=kernel.Polyharmonic(1, Epsilon), target=fitting_target, fkine=fkine)
    dist_est = checker.rbf_score # 위의 polyharmonic 함수
    init_train_t = time() - init_train_t
    print('MIN_SCORE = {:.6f}'.format(dist_est(cfgs[train_num:]).min()))

    positions = torch.FloatTensor(np.linspace(obstacles[0][1], [4, 3], T))
    start_cfg = torch.zeros(robot.dof, dtype=cfgs.dtype)
    target_cfg = torch.zeros(robot.dof, dtype=cfgs.dtype)
    start_cfg[0] = np.pi/2
    start_cfg[1] = -np.pi/6
    target_cfg[0] = 0
    target_cfg[1] = np.pi/7

    update_ts = []
    plan_ts = []
    for t, trans in zip(range(T), positions):
        ut = time()
        fcl_collision_obj[0].setTransform(fcl.Transform(
                    [trans[0], trans[1], 0]))
        for obs_mng in obs_managers:
            obs_mng.update()

        exploit_samples = torch.randn(nu, len(checker.gains), robot.dof, dtype=checker.support_points.dtype) * sigma + checker.support_points
        exploit_samples = utils.wrap2pi(exploit_samples).reshape(-1, robot.dof)

        explore_samples = torch.rand(kai, robot.dof, dtype=checker.support_points.dtype) * 2*np.pi - np.pi

        cfgs = torch.cat([exploit_samples, explore_samples, checker.support_points])
        labels, dists = gt_checker.predict(cfgs, distance=True)
        dists = dists.double()
        print('Collision {}, Free {}\n'.format((labels == 1).sum(), (labels==-1).sum()))

        gains = torch.cat([torch.zeros(len(exploit_samples)+len(explore_samples), checker.num_class, dtype=checker.gains.dtype), checker.gains])
        added_hypothesis = checker.score(cfgs[:-len(checker.support_points)])
        hypothesis = torch.cat([added_hypothesis, checker.hypothesis])

        checker.train(cfgs, labels, gains=gains, hypothesis=hypothesis, distance=dists)
        print('Num of support points {}'.format(len(checker.support_points)))
        checker.fit_poly(kernel_func=kernel.Polyharmonic(1, Epsilon), target=fitting_target, fkine=fkine, reg=0.1)
        update_ts.append(time()-ut)

        if checking_method == 'fcl':
            fcl_options = {
                'N_WAYPOINTS': 20,
                'NUM_RE_TRIALS': 5,
                'MAXITER': 200,
                'seed': seed,
                'history': False
            }
        elif checking_method == 'diffco':
            diffco_options = {
                'N_WAYPOINTS': 20,
                'NUM_RE_TRIALS': 5,
                'MAXITER': 200,
                'safety_margin': -0.5,
                'seed': seed,
                'history': False
            }

        print('t = {}'.format(t))
        if t % 1 == 0 and not torch.any(checker.predict(torch.stack([start_cfg, target_cfg], dim=0)) == 1):

            obstacles[0][1] = (trans[0], trans[1])
            cfg_path_plots = []
            if robot.dof > 2:
                fig, ax, link_plot, joint_plot, eff_plot = create_plots(robot, obstacles, dist_est, checker) # 플롯 생성
            elif robot.dof == 2:
                fig, ax, link_plot, joint_plot, eff_plot, cfg_path_plots = create_plots(robot, obstacles, dist_est, checker)

            ot = time()
            # Begin optimization==========
            if checking_method == 'diffco':
                solution_rec = givengrad_traj_optimize(
                    robot, dist_est, start_cfg, target_cfg, options=diffco_options)
                p = torch.FloatTensor(solution_rec['solution'])
            elif checking_method == 'fcl':
                solution_rec = gradient_free_traj_optimize(
                    robot, lambda cfg: gt_checker.predict(cfg, distance=False), start_cfg, target_cfg, options=fcl_options)
                p = torch.FloatTensor(solution_rec['solution'])
            # ============================
            plan_ts.append(time()-ot)

            path_dir = 'results/active_learning/path_2d_{}dof_{}_checker={}_seed{}_step{:02d}.json'.format(robot.dof, env_name, checking_method, seed, t)
            with open(path_dir, 'w+t') as f:
                json.dump(
                    {
                        'path': p.data.numpy().tolist(),
                    },
                    f, indent=1)
                print('Plan recorded in {}'.format(f.name))

            p = utils.make_continue(p)

            # single shot
            single_plot(robot, p, fig, link_plot, joint_plot, eff_plot, cfg_path_plots=cfg_path_plots, ax=ax)
            # plt.show()
            fig_dir = 'figs/active/{random_seed}/{checking_method}'.format(random_seed=seed, checking_method=checking_method)
            if not isdir(fig_dir):
                makedirs(fig_dir)
            plt.savefig(join(fig_dir, '2d_{DOF}dof_{ename}_{checking_method}_{step:02d}'.format(
                DOF=robot.dof, ename=env_name, checking_method=checking_method, step=t)), dpi=300)

    print('{} summary'.format(checking_method))
    print('Initial training {} sec.'.format(init_train_t))
    print('Update {} sec.'.format(update_ts))
    print('Planning {} sec.'.format(plan_ts))

if __name__ == "__main__":
    main(checking_method='diffco')
    main(checking_method='fcl')

reason: import numpy,from scipy


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

import numpy as np
from deepspeech_pytorch.decoder import GreedyDecoder

class MSGreedyDecoder(GreedyDecoder):
    """
    GreedyDecoder used for MindSpore
    """

    def process_string(self, sequence, size, remove_repetitions=False):
        """
        process string
        """
        string = ''
        offsets = []
        for i in range(size):
            char = self.int_to_char[sequence[i].item()]
            if char != self.int_to_char[self.blank_index]:
                if remove_repetitions and i != 0 and char == self.int_to_char[sequence[i - 1].item()]:
                    pass
                elif char == self.labels[self.space_index]:
                    string += ' '
                    offsets.append(i)
                else:
                    string = string + char
                    offsets.append(i)
        return string, offsets

    def decode(self, probs, sizes=None):
        probs = probs.asnumpy()
        sizes = sizes.asnumpy()

        max_probs = np.argmax(probs, axis=-1)
        strings, offsets = self.convert_to_strings(max_probs, sizes, remove_repetitions=True, return_offsets=True)
        return strings, offsets

max_stars_repo_name: GuoSuiming/mindspore


text:
import numpy as np

from multiagent.core import World, Agent

from multiagent.scenario import BaseScenario

class Scenario(BaseScenario):

    def make_world(self):
        world = World()

        # add agents
        world.agents = [Agent() for i in range(2)]
        for i, agent in enumerate(world.agents):
            agent.name = 'agent %d' % i
            agent.collide = False
            agent.silent = False

        # make initial conditions
        self.reset_world(world)
        return world

    def reset_world(self, world):
        for i, agent in enumerate(world.agents):
            agent.color = np.array([0.45, 0.95, 0.45])

        # set random initial states
        for agent in world.agents:
            agent.state.p_pos = np.random.uniform(-1, +1, world.dim_p)
            agent.state.p_vel = np.zeros(world.dim_p)
            agent.state.c = np.zeros(world.dim_c)

    def reward(self, agent, world):
        for other in world.agents:
            if other is not agent:
                return -np.sum(np.square(agent.state.p_pos - other.state.p_pos))

    def observation(self, agent, world):
        other_pos = []
        for other in world.landmarks:
            if other is not agent:
                other_pos.append(other.state.p_pos - agent.state.p_pos)
        return np.concatenate([agent.state.p_vel] + other_pos)

max_stars_repo_name: Night-Voyager/BoBoChu-AI

max_issues_repo_name: Night-Voyager/BoBoChu-AI

max_forks_repo_name: Night-Voyager/BoBoChu-AI


text:
from mindware.components.feature_engineering.transformations.base_transformer import *

class DiscreteCategorizer(Transformer):
    type = 25

    def __init__(self, max_unique=10):
        super().__init__("discrete_categorizer")
        self.input_type = [DISCRETE]
        self.output_type = CATEGORICAL
        self.max_unique = max_unique
        self.target_fields = None

    def operate(self, input_datanode, target_fields=None):
        import numpy as np
        from sklearn.preprocessing import OneHotEncoder

        if target_fields is None:
            target_fields = collect_fields(input_datanode.feature_types, self.input_type)
            if len(target_fields) == 0:
                return input_datanode.copy_()

        X, y = input_datanode.data
        if self.target_fields is None:
            target_fields = [idx for idx in target_fields if len(set(X[:, idx])) <= self.max_unique]
            # Fetch the fields to transform.
            self.target_fields = target_fields

        if len(self.target_fields) == 0:
            return input_datanode.copy_()

        X_input = X[:, self.target_fields]
        if self.model is None:
            self.model = OneHotEncoder(handle_unknown='ignore')  # Ignore values out of range
            self.model.fit(X_input)

        new_X = self.model.transform(X_input).toarray()
        X_output = X.copy()

        # Delete the original columns.
        X_output = np.delete(X_output, np.s_[self.target_fields], axis=1)
        X_output = np.hstack((X_output, new_X))
        feature_types = input_datanode.feature_types.copy()
        feature_types = list(np.delete(feature_types, self.target_fields))
        feature_types.extend([CATEGORICAL] * new_X.shape[1])
        output_datanode = DataNode((X_output, y), feature_types, input_datanode.task_type)
        output_datanode.trans_hist = input_datanode.trans_hist.copy()
        output_datanode.enable_balance = input_datanode.enable_balance
        output_datanode.data_balance = input_datanode.data_balance
        output_datanode.trans_hist.append(self.type)
        return output_datanode


text:
import torch
from torch import nn
import torch.nn.functional as F
from torch.utils.data import DataLoader, Dataset
import os

from PIL import Image
from torchvision.transforms import Resize, Compose, ToTensor, Normalize
import numpy as np
# import skimage
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
import wandb
import time
import tqdm
import cvgutils.Viz as viz
from cvgutils.nn.Siren import Siren
from cvgutils.nn.SirenUtils import laplace, gradient, get_mgrid

if __name__ == "__main__":
    wandb.init(project='specular_relighting',name='Transmittance 1D')

    visstep = 100

    img_siren = Siren(in_features=1, out_features=1, hidden_features=20,
                    hidden_layers=2, outermost_linear=True)
    img_siren.cuda()

    total_steps = 50000 # Since the whole image is our dataset, this just means 500 gradient descent steps.
    steps_til_summary = 10

    optim = torch.optim.Adam(lr=1e-4, params=img_siren.parameters())
    logger = viz.logger('','wandb','specular_relighting','sirenRangeTest')

    x0 = np.linspace(0,1,1000)[:,None]
    y0 = ((0.25 < x0) & (x0 < 0.75)) * 1000
    # y0 = x0 * 2
    x0 = torch.Tensor(x0).cuda()
    y0 = torch.Tensor(y0).cuda()
    batchsize = 10
    for step in tqdm.trange(total_steps):
        idx = (np.random.rand(batchsize) * x0.shape[0])
        x = x0[torch.Tensor(idx).long(),:]
        y = y0[torch.Tensor(idx).long(),:]
        model_output, coords = img_siren(x)
        loss = ((model_output - y) ** 2).mean()
        if(step % visstep == 0):
            yout,_ = img_siren(x0)
            plt1 = viz.plot(x0.detach().cpu().numpy(),y0.detach().cpu().numpy(),ptype='plot')
            plt2 = viz.plot(x0.detach().cpu().numpy(),yout.detach().cpu().numpy(),ptype='plot')
            pltmain = np.concatenate((plt1,plt2),axis=1)
            logger.addImage(pltmain,'comparison')
            logger.addLoss(loss.detach().cpu().numpy(),'loss')

        logger.takeStep()
        optim.zero_grad()
        loss.backward()
        optim.step()

max_stars_repo_name: mshafiei/cvutils

max_issues_repo_name: mshafiei/cvgutils

max_forks_repo_name: mshafiei/cvgutils


text:
import unittest
from mlmo.eval.metrics import PrRecF1
from collections import OrderedDict
from numpy import isclose

class TestPrRecF1(unittest.TestCase):

    def test_full_results(self):

        pred_labels = [1, 1, 2, 2, 100, 105, 3]
        true_labels = [1, 1, 1, 2, 3, 100, 100]

        exp_res = {1: {'p': 1., 'r': 2./3, 'f1': 0.8},
                   2: {'p': 0.5, 'r': 1., 'f1': 2./3},
                   3: {'p': 0., 'r': 0., 'f1': 0.},
                   100: {'p': 0., 'r': 0., 'f1': 0.},
                   105: {'p': 0., 'r': 0., 'f1': 0.}}

        metr = PrRecF1()
        metr.accum(pred_labels, true_labels)
        res = metr.aggr()

        self.assertTrue(equal_dicts(res, exp_res))

    def test_macro_results(self):
        pred_labels = [1, 1, 2, 2, 100, 105, 3]
        true_labels = [1, 1, 1, 2, 3, 100, 100]

        exp_res = {"macro_p": 0.3, "macro_r": (1. + 2./3)/5.,
                   'macro_f1': (0.8 + 2./3.)/5.}

        metr = PrRecF1()
        metr.accum(pred_labels, true_labels)
        res = metr.aggr(macro=True)

        self.assertTrue(equal_dicts(res, exp_res))

def equal_dicts(dict1, dict2):
    if len(dict1.keys()) != len(dict2.keys()):
        return False
    for k in dict1:
        if k not in dict2:
            return False
        v1 = dict1[k]
        v2 = dict2[k]
        if isinstance(v1, (OrderedDict, dict)) and \
            isinstance(v2, (OrderedDict, dict)):
            return equal_dicts(v1, v2)
        if type(v1) == type(v2):
            return isclose(v1, v2)
        else:
            return False
    return True

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

max_stars_repo_name: prashantlv/mltoolkit

max_issues_repo_name: prashantlv/mltoolkit

max_forks_repo_name: prashantlv/mltoolkit

reason: from numpy


text:
"""
A set of useful tools for handing the text segmentation tasks.
"""
import numpy as np
from itertools import chain, tee
# from nltk.corpus import stopwords
from nltk.stem import PorterStemmer

stopword_set = set()

stopword_path = "/home/pltrdy/autoalign/graphseg/source/res/stopwords-fr.txt"
with open(stopword_path) as f:
    for line in f:
        stopword_set.add(line.strip())
# add punctuation
stopword_set.update(["''", ",", ".", "``", "'", "!", '"', "#", "$", "%", "&", "(", ")", "*", "+", "-", "/",
                     ":", ";", "<", "=", ">", "?", "@", "[", "\\", "]", "^", "_", "`", "{", "|", "}", "~"])

stemmer = PorterStemmer()

CHOI_TEMPLATE = "/home/aaa244/storage/segmentation/data/choi/{}/{}/{}.ref"

def choi_loader(doc, tp, ref, word_cut=0, remove_stop=False, stem=False):
    """ Load a choi document from the dataset,
        returns a list of parts
        each part is a list of sentences
        each sentence is a list of words,
        the only preprocessing is to lowercase everything """

    # Open the corresponding file
    with open(CHOI_TEMPLATE.format(doc, tp, ref)) as f:
        doc = f.read()

    def is_valid(word):
        if stem:
            word = stemmer.stem(word)
        if remove_stop:
            if word in stopword_set:
                return False
        return True

    # Split into parts
    parts = [x.splitlines() for x in doc.split("==========\n") if x]

    doc = [[[x.lower() for x in sent.split() if is_valid(x)]
            for sent in doc] for doc in parts]

    filtered = [[sent for sent in part if len(
        sent) > word_cut] for part in doc]

    return filtered

ARX_TEMPLATE = "/home/aaa244/storage/segmentation/data/arxiv/{:03d}.ref"

def arx_loader(num):
    """ Load an arxiv document from the dataset,
        returns a list of parts
        each part is a list of words"""

    # Open the corresponding file
    with open(ARX_TEMPLATE.format(num)) as f:
        doc = f.read()

    # Split into parts
    return [[[x] for x in x.split()] for x in doc.split("BR")]

def allchoi(set, *args, **kwargs):
    if set == "3-5":
        for a in [1, 2]:
            for i in range(50):
                yield choi_loader(a, "3-5", i, *args, **kwargs)
    if set == "6-8":
        for a in [1, 2]:
            for i in range(50):
                yield choi_loader(a, "6-8", i, *args, **kwargs)
    if set == "9-11":
        for a in [1, 2]:
            for i in range(50):
                yield choi_loader(a, "9-11", i, *args, **kwargs)
    if set == "3-11":
        for a in [1, 2]:
            for i in range(50):
                yield choi_loader(a, "3-11", i, *args, **kwargs)
        for i in range(300):
            yield choi_loader(3, "3-11", i, *args, **kwargs)

def collapse(doc):
    """ Turn a document into a single string """
    return " ".join(" ".join(" ".join(sent) for sent in part) for part in doc)

def collapse_sents(doc):
    """ Collapse a doc to a list of sentences """
    return [sent for part in doc for sent in part]

def collapse_words(doc):
    """ Collapse a doc to a list of words """
    return [word for part in doc for sent in part for word in sent]

def word_iter(doc):
    """ Iterate over the words in a document """
    words = (word for part in doc for sent in part for word in sent)
    for word in words:
        yield word

def sent_iter(doc):
    """ Iterate over the sentences in a document """
    sents = (sent for part in doc for sent in part)
    for sent in sents:
        yield sent

def refsplit(doc):
    """ Get the reference splitting for the document """
    middle = np.cumsum(
        [1] + [sum(1 for sent in part for word in sent) for part in doc])
    return (middle[1:-1] - 1).tolist() + [middle[-1] - 1]

def refsplit_sent(doc):
    """ Get the reference splitting for the sentence representation """
    middle = np.cumsum([1] + [sum(1 for sent in part) for part in doc])
    return (middle[1:-1] - 1).tolist() + [middle[-1] - 1]

# A testing document in the same structure as a choi doc
testdoc = [["this is the first sentence".split()] * 5,
           ["second sentence same as the first".split()] * 3,
           ["the blue fish went to the market".split()] * 4,
           ["once upon a midnight dreary with my pack".split()] * 5,
           ["while i pondered weak and weary".split()] * 3,
           ["one fish two fish three fish blue fish".split()] * 5,
           ["pack it up pack it in let me begin".split()] * 3,
           ["i came to win battle me that is a sin to begin".split()] * 5,
           ["and think about how ravens and writing desks".split()] * 4,
           ["other people are people too not ravens or fish".split()] * 3]

# Utility

def pairwise(iterable):
    "s -> (s0,s1), (s1,s2), (s2, s3), ..."
    a, b = tee(iterable)
    next(b, None)
    return zip(a, b)

def seg_iter(splits):
    return pairwise([0] + splits)

def length_iter(splits):
    return ((b - a) for (a, b) in seg_iter(splits))

# Scoring functions

def xnor(a, b): return (a and b) or (not a and not b)

def score(hyp, ref, k=None):
    """ The Pk metric from Beeferman """
    # if k is undefined, use half the mean segment size
    k = k or int(round(0.5 * ref[-1] / len(ref))) - 1

    length = ref[-1]
    probeinds = np.arange(length - k)
    dref = np.digitize(probeinds, ref) == np.digitize(probeinds + k, ref)
    dhyp = np.digitize(probeinds, hyp) == np.digitize(probeinds + k, hyp)

    return (dref ^ dhyp).mean()

def score_wd(hyp, ref, k=None):
    """ The window diff metric of Pevzner """
    k = k or int(round(0.5 * ref[-1] / len(ref))) - 1

    length = ref[-1]
    hyp = np.asarray(hyp)
    ref = np.asarray(ref)

    score = 0.0
    tot = 0.0
    for i in range(length - k):
        bref = ((ref > i) & (ref <= i + k)).sum()
        bhyp = ((hyp > i) & (hyp <= i + k)).sum()
        score += 1.0 * (np.abs(bref - bhyp) > 0)
        tot += 1.0
    return score / tot


text:
from __future__ import absolute_import
from __future__ import print_function
from __future__ import division

import _init_paths
import time
import torch
import torch.nn.functional as F
import numpy as np
from dataset.data_feeder import DataFeeder
from utils.config import cfg_from_yaml
from utils.create_edge import create_1toN_adj, create_fc_adj
from model.gat import GAT
import math
import random
from eval import evaluation

def load_checkpoint(model, path):
    print("loading model from",path)
    checkpoint = torch.load(path)
    model_state_dict = checkpoint['model_state_dict']
    model.load_state_dict(model_state_dict)

def test(model_cfg, dataset_cfg,
        batch_size, gpus=1, workers = 0,
        resume_from=None, load_from=None, checkpoint_path=None):
    dataset = DataFeeder(**dataset_cfg)
    data_loader = torch.utils.data.DataLoader(dataset,batch_size=batch_size,
                    shuffle=False, num_workers=workers)

    model = GAT(**model_cfg)
    model = model.cuda()
    load_checkpoint(model, checkpoint_path)
    # print(model)
    # model = model.cuda()
    model.eval()
    try:
        # set redution='none' to cal pos and neg loss
        loss_fn = torch.nn.CrossEntropyLoss(reduction='none')
    except TypeError:
        loss_fn = torch.nn.CrossEntropyLoss(reduce=False)
    all_pred = []
    all_label = []
    test_loss = 0.0
    fps = 20.0
    for name, num_det, det, feat, ffeat, label in data_loader:
        # print(name[0])
        batch_tic = time.time()
        feat = feat.cuda()
        ffeat = ffeat.cuda()
        all_label.append(label.float().numpy())
        label = label.cuda()
        # N, T, V, C
        batch_sz = feat.size()[0]
        T = feat.size()[1]
        h_prev = []
        batch_pred = []
        batch_loss = 0.0
        with torch.no_grad():
            for i, n in enumerate(num_det[0,:]):
                if n <= 0:
                    # print('no,obj')
                    output = np.zeros(1)
                    batch_pred.append(output)
                else:
                    # feat: N T V C
                    feat_in = feat[:,i,:n,:]
                    ffeat_in = ffeat[:,i,:]
                    N = feat_in.size()[1]
                    adj = create_fc_adj(N).cuda()
                    x = model.extract_feature(feat_in, adj)
                    adj_att = create_1toN_adj(N + 1).cuda()
                    x = model.fusion_feature(x, ffeat_in, adj_att)
                    output, h_prev = model.classifer(x, h_prev)

                    pos_loss = -math.exp(-(T-i-1)/fps)*(-1 * loss_fn(output,label))
                    neg_loss = loss_fn(output,label)
                    loss = pos_loss*label.float() + neg_loss*(1-label).float()
                    batch_loss = batch_loss + loss

                    output = F.softmax(output,dim=-1)
                    output = output[:,1].cpu().numpy()
                    batch_pred.append(output)
                # print(output)
            batch_pred = np.stack(batch_pred,axis = 1)
            all_pred.append(batch_pred)
            test_loss = test_loss + batch_loss
        batch_toc = time.time()
        # print('time:',batch_toc - batch_tic)
    all_label = np.hstack(all_label)
    all_pred = np.vstack(all_pred)
    all_label = np.expand_dims(all_label,-1)
    all_pred = all_pred[:,:90]
    print(all_label.shape)
    print(all_pred.shape)
    print('loss', test_loss)
    evaluation(all_pred,all_label)

if __name__ == "__main__":
    config = cfg_from_yaml('cfgs/test.yaml')
    test(**config)

max_stars_repo_name: CGNAM/CGNAM

max_issues_repo_name: CGNAM/CGNAM

max_forks_repo_name: CGNAM/CGNAM


text:
from __future__ import print_function

import io
import gzip

import numpy as np

import os
from os.path import expanduser, join
import sys
import numpy as np
import io

if len(sys.argv) > 1 and sys.argv[1] == '-p':
    import mfem.par as mfem
    use_parallel = True
    from mfem.common.mpi_debug import nicePrint as print
    from mpi4py import MPI
    myid  = MPI.COMM_WORLD.rank

else:
    import mfem.ser as mfem
    use_parallel = False
    myid = 0

def check(a, b, msg):
    assert len(a) == len(b), msg
    assert np.sum(np.abs(a-b)) == 0, msg

def check_mesh(m1, m2, msg):
    assert m1.GetNE() == m2.GetNE(), msg
    assert m1.GetNBE() == m2.GetNBE(), msg
    assert m1.GetNV() == m2.GetNV(), msg

def run_test():
    mesh = mfem.Mesh(10, 10, "TRIANGLE")

    fec = mfem.H1_FECollection(1, mesh.Dimension())
    fespace = mfem.FiniteElementSpace(mesh, fec)

    gf = mfem.GridFunction(fespace)
    gf.Assign(1.0)
    odata = gf.GetDataArray().copy()

    visit_dc = mfem.VisItDataCollection("test_gf", mesh)
    visit_dc.RegisterField("gf", gf)
    visit_dc.Save()

    mesh2 = mfem.Mesh("test_gf_000000/mesh.000000")
    check_mesh(mesh, mesh2, ".mesh2 does not agree with original")

    gf2 = mfem.GridFunction(mesh2, "test_gf_000000/gf.000000")
    odata2 = gf2.GetDataArray().copy()
    check(odata, odata2, "odata2 file does not agree with original")

    visit_dc = mfem.VisItDataCollection("test_gf_gz", mesh)
    visit_dc.SetCompression(True)
    visit_dc.RegisterField("gf", gf)
    visit_dc.Save()

    with gzip.open("test_gf_gz_000000/mesh.000000", 'rt') as f:
       sio = io.StringIO(f.read())
    mesh3 = mfem.Mesh(sio)
    check_mesh(mesh, mesh3, ".mesh3 does not agree with original")

    with gzip.open("test_gf_gz_000000/gf.000000", 'rt') as f:
       sio = io.StringIO(f.read())

    # This is where the error is:
    gf3 = mfem.GridFunction(mesh3, sio)
    odata3 = gf3.GetDataArray()
    check(odata, odata3, "gf3 file does not agree with original")

    mesh4 = mfem.Mesh("test_gf_gz_000000/mesh.000000")
    check_mesh(mesh, mesh4, ".mesh4 does not agree with original")

    gf4 = mfem.GridFunction(mesh3, "test_gf_gz_000000/gf.000000")
    odata4 = gf4.GetDataArray()

    check(odata, odata4, "gf3 file does not agree with original")

    print("PASSED")

if __name__=='__main__':
    run_test()

max_stars_repo_name: GabrielJie/PyMFEM

max_issues_repo_name: GabrielJie/PyMFEM

max_forks_repo_name: GabrielJie/PyMFEM


text:
# getting and importing modules
myConnection = psycopg2.connect( host=host, user=user, password=password, dbname=dbname )

import pandas as pd
import numpy as np
from sklearn import preprocessing
import matplotlib.pyplot as plt

from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
import seaborn as sns
from sklearn import linear_model

data = pd.read_sql("Select * FROM final_data_thayn;", con=myConnection)
data_unclean = pd.read_sql("Select * FROM food_inspection_predict;", con=myConnection)

#visualization of pass and fail
sns.countplot(x='results',data=data, palette='hls')
plt.show()

#averages
data.groupby('results').mean()

#ratings and pass an fail vizualisation
pd.crosstab(data.rating, data.results).plot(kind='bar')
plt.title('Rating Frequency for Pass Fail')
plt.xlabel('Rating')
plt.ylabel('Frequency of Pass or Fail')

plt.rcParams["figure.figsize"] =(12,9)
plt.show()

#price and pass and fail vizualisation
pd.crosstab(data.price, data.results).plot(kind='bar')
plt.title('Rating Frequency for Pass Fail')
plt.xlabel('Price')
plt.ylabel('Frequency of Pass or Fail')

plt.rcParams["figure.figsize"] =(12,9)
plt.show()

#risk and pass and fail vizualization
table = pd.crosstab(data_unclean.risk, data.results)
table.div(table.sum(1), axis=0).plot(kind='bar', stacked=True)

plt.title('Stacked Bar Chart of Risk vs Results')
plt.xlabel('Risk')
plt.ylabel('Proportion of Results')
plt.show()

#stacked bar chart for price and pass and fail
table = pd.crosstab(data.price, data.results)
table.div(table.sum(1), axis=0).plot(kind='bar', stacked=True)

plt.title('Stacked Bar Chart of Price vs Results')
plt.xlabel('Price')
plt.ylabel('Proportion of Results')
plt.show()

#stacked bar chart for rating and pass and fail
table = pd.crosstab(data.rating, data.results)
table.div(table.sum(1), axis=0).plot(kind='bar', stacked=True)

plt.title('Stacked Bar Chart of Rating vs Results')
plt.xlabel('Rating')
plt.ylabel('Proportion of Results')
plt.show()

#getting rid of NA
data.dropna(inplace=True)

#preping X a y
cols=['price', 'rating', 'review_count', 'is_african', 'is_asian_fusion', 'is_bakeries', 'is_bars',
       'is_breakfast_brunch', 'is_buffets', 'is_cafes', 'is_caribbean',
       'is_chinese', 'is_deli', 'is_eastern_european', 'is_european',
       'is_fast_food', 'is_hawaiian', 'is_health_food', 'is_icecream',
       'is_indian', 'is_italian', 'is_japanese', 'is_korean', 'is_latin',
       'is_mediterranean', 'is_mexican', 'is_middleasten', 'is_new_american',
       'is_piza', 'is_seafood', 'is_south_east_asian', 'is_southern',
       'is_street_food', 'is_sweets', 'is_thai', 'is_other_category',
       'is_pickup', 'is_delivery', 'is_restaurant_reservation', 'Canvass',
       'Complaint', 'reinspection', 'License', 'FoodPoison', 'high_risk_1',
       'medium_risk_2', 'low_risk_2', 'grocery', 'Bakery', 'Mobile']

X = data[cols]
y = data['pass']

import statsmodels.api as sm
from scipy import stats
stats.chisqprob = lambda chisq, df: stats.chi2.sf(chisq, df)

#logit model
logit_model = sm.Logit(y, X)

result = logit_model.fit()

print(result.summary())

max_stars_repo_name: georgetown-analytics/Trade-Imbalances

reason: import numpy,from scipy,import statsmodels


text:
"""
General helper functions for the examples.
"""

import numpy as np

def gaussian_peak_1d(x, p):
    """
    Generates a 1D Gaussian peak.
    See http://gpufit.readthedocs.io/en/latest/fit_model_functions.html#d-gaussian-function

    :param x: x grid position values
    :param p: parameters (amplitude, center position, width, offset)
    :return: Gaussian peak
    """
    p = p.flatten()
    return p[0] * np.exp(-(x - p[1])**2 / (2 * p[2]**2)) + p[3]

def gaussian_peak_2d(x, y, p):
    """
    Generates a 2D Gaussian peak.
    http://gpufit.readthedocs.io/en/latest/fit_model_functions.html#d-gaussian-function-cylindrical-symmetry

    x,y - x and y grid position values
    p - parameters (amplitude, x,y center position, width, offset)

    :param x:
    :param y:
    :param p:
    :return:
    """
    p = p.flatten()
    return p[0] * np.exp(-((x - p[1])**2 + (y - p[2])**2) / (2 * p[3]**2)) + p[4]

max_stars_repo_name: BigBSB/Gpufit

max_issues_repo_name: BigBSB/Gpufit

max_forks_repo_name: BigBSB/Gpufit


text:
from pathlib import Path
import torch.utils.data
import random
import torch
import tqdm
import soundfile as sf
import yaml
import itertools
import numpy as np
import re
import copy

class SlakhDataset(torch.utils.data.Dataset):

    dataset_name = "Slakh"

    def __init__(
        self,
        root,
        sources=["guitar1", "guitar2", "bass", "piano"],
        means=[-35,15,0,-20],
        stds=[0,0,0,0],
        targets=["guitar1", "guitar2", "bass", "piano"],
        suffix=".wav",
        split="train",
        subset=None,
        segment=None,
        samples_per_track=1,
        random_segments=False,
        random_track_mix=False,
        source_augmentations=lambda audio: audio,
        sample_rate=44100,
        valid_tracks=15,
        test_tracks=50,
        seed=None,
    ):
        if seed:
            torch.manual_seed(seed)
            random.seed(seed)
        self.validation = True if split=='validation' else False
        self.valid_tracks = valid_tracks
        self.test_tracks = test_tracks
        self.means = means if type(means) == list else means[0]
        self.stds = stds if type(stds) == list else stds[0]
        self.root = Path(root).expanduser()
        self.split = split
        self.sample_rate = sample_rate
        self.segment = segment
        self.random_track_mix = random_track_mix
        self.random_segments = random_segments
        self.source_augmentations = source_augmentations
        self.sources = sources
        self.targets = targets
        self.suffix = suffix
        self.subset = subset
        self.samples_per_track = samples_per_track
        self.tracks = list(self.get_tracks())

        if not self.tracks:
            raise RuntimeError("No tracks found.")

    def _panner(self, x, angle):
        """

        pan a mono audio source into stereo
        x: <frames, 2>
        angle: the angle in degrees
        """

        angle_r = np.radians(angle)
        left = np.sqrt(2)/2.0 * (np.cos(angle_r) - np.sin(angle_r)) * x[...,0]
        right = np.sqrt(2)/2.0 * (np.cos(angle_r) + np.sin(angle_r)) * x[...,1]
        return np.stack((left,right),-1)

    def __getitem__(self, index):
        # assemble the mixture of target and interferers
        audio_sources = []
        source_angles = []

        # get track_id
        track_id = index // self.samples_per_track
        min_dur = self.tracks[track_id]["min_duration"]
        if self.random_segments:
            start = random.uniform(0, min_dur - self.segment)
        else:
            start = 0

        # load sources
        paths = copy.deepcopy(self.tracks[track_id]["paths"])
        for i, source in enumerate(self.sources):

            # If multiple of the same inst exists, remove the leading number (i.e. "guitar1" -> "guitar")
            source = re.sub(r'[0-9]+', '', source)

            # loads the full track duration
            start_sample = int(start * self.sample_rate)
            # check if dur is none
            if self.segment:
                # stop in soundfile is calc in samples, not seconds
                stop_sample = start_sample + int(self.segment * self.sample_rate)
                seg_stop = int(self.segment * self.sample_rate)
            else:
                # set to None for reading complete file
                stop_sample = None
                seg_stop = int(min_dur*self.sample_rate)

            # load actual audio
            random.shuffle(paths[source])
            if source!='_dummy':
                p = random.choice(paths[source])
            else:
                p = paths[source].pop(0)

            audio, _ = sf.read(p,always_2d=True, start=start_sample, stop=stop_sample)
            audio = audio.repeat(2, -1)
            angle = np.clip(np.random.uniform(low=-45.0,high=45.0), a_min=-45.0, a_max=45.0)
            noise = np.random.normal(0.0,4.0)
            source_angles.append(np.clip(angle, a_min=-45.0, a_max=45.0))
            audio = self._panner(audio[:seg_stop,:], angle)
            # convert to torch tensor
            audio = torch.tensor(audio.T, dtype=torch.float)
            # apply source-wise augmentations
            #audio = self.source_augmentations(audio)
            audio_sources.append(audio)

        # apply linear mix over source index=0
        audio_mix = torch.stack(audio_sources).sum(0)
        if self.targets:
            audio_sources = torch.stack(audio_sources, dim=0)
        return audio_mix, audio_sources, torch.FloatTensor(source_angles)

    def __len__(self):
        return len(self.tracks) * self.samples_per_track

    def get_tracks(self):
        """Loads input and output tracks"""
        p = list(Path(self.root, self.split).iterdir())

        if self.validation or self.split=='test':
            tracks = random.sample(p, len(p))
        else:
            tracks = p

        sources = list(set([re.sub(r'[0-9]+', '', source) for source in self.sources]))
        include_tracks = 0

        for track_path in tqdm.tqdm(tracks):
            if track_path.is_dir():

                # Open yaml and check for matching stems
                with open(track_path / "metadata.yaml", 'r') as stream:
                    stems_conf = yaml.safe_load(stream)['stems']

                source_paths = {src:[str(track_path / 'stems' / (s + self.suffix)) for s in stems_conf.keys() if (stems_conf[s]['inst_class'].lower()==src and stems_conf[s]['audio_rendered'])] for src in set(sources)}
                if any(len(x)==0 for x in source_paths.values()):# or len(source_paths['guitar'])<2:
                    print("Exclude track due to non-existing source", track_path)
                    continue

                # get metadata
                infos = list(map(sf.info, list(itertools.chain(*source_paths.values()))))
                if not all(i.samplerate == self.sample_rate for i in infos):
                    print("Exclude track due to different sample rate ", track_path)
                    continue

                # If we go beyond num. tracks for the split, just stop
                if self.validation and self.valid_tracks <= include_tracks:
                    continue
                elif self.split=='test' and self.test_tracks <= include_tracks:
                    continue

                include_tracks += 1
                min_duration = min(i.duration for i in infos)
                if self.segment is not None:
                    # get minimum duration of track
                    if min_duration > self.segment:
                        yield ({"paths": source_paths, "min_duration": min_duration})
                else:
                    yield ({"paths": source_paths, "min_duration": min_duration})

    def get_infos(self):
        """Get dataset infos (for publishing models).
        Returns:
            dict, dataset infos with keys `dataset`, `task` and `licences`.
        """
        props = vars(self).copy()
        del props['tracks']
        del props['source_augmentations']
        return props


text:
import argparse
import time
import random
import wandb
import numpy as np
import os
import signal
import sys

parser = argparse.ArgumentParser()
parser.add_argument("--epochs", type=int, default=2)
parser.add_argument("--heavy", action="store_true", default=False)
parser.add_argument("--sleep_every", type=int, default=0)
args = parser.parse_args()
print("Calling init with args: {}", format(args))
print("Environ: {}".format({k: v for k, v in os.environ.items() if k.startswith("WANDB")}))
wandb.init(config=args)
print("Init called with config {}".format(wandb.config))

# raise ValueError()
# os.kill(os.getpid(), signal.SIGINT)
for i in range(0, wandb.config.epochs):
    loss = random.uniform(0, wandb.config.epochs - i)
    print("loss: %s" % loss)
    wandb.log({"loss": loss}, commit=False)
    if wandb.config.heavy:
        for x in range(50):
            wandb.log(
                {
                    "hist_{}".format(x): wandb.Histogram(
                        np.random.randint(255, size=(1000))
                    )
                },
                commit=False,
            )
    wandb.log({"cool": True})
    if wandb.config.sleep_every > 0 and i % wandb.config.sleep_every == 0:
        print("sleeping")
        time.sleep(random.random() + 1)
    sys.stdout.flush()
print("Finished")

max_stars_repo_name: nbardy/client

max_issues_repo_name: jiaqianjing/client

max_forks_repo_name: jiaqianjing/client


text:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on Thurs Oct 24th 9:54am 2019

Calculates the Z^2 statistic given a list of ObsIDs, where the event files
must already have 'PULSE_PHASE' in them! The input will be a par file. Will also
have a way to iterate through a grid of orbital parameters.

"""
from __future__ import division, print_function
from astropy.io import fits
import numpy as np
import matplotlib.pyplot as plt
from pint.eventstats import sf_z2m,z2m,sig2sigma
import subprocess
from tqdm import tqdm
import glob
import os,shutil
import Lv0_dirs,Lv2_mkdir

Lv0_dirs.global_par()

def get_Z2(obsid,model,par_file,m):
    """
    Calculates the Z^2 statistic given the ObsID, binary model name (if there's one),
    and the parameter file.

    obsid - Observation ID of the object of interest (10-digit str)
    model - binary model being used (ELL1, BT, DD, or DDK for now)
    par_file - orbital parameter file for input into PINT's photonphase
    m - number of harmonics
    """
    filename = Lv0_dirs.NICERSOFT_DATADIR + obsid + '_pipe/cleanfilt_phase'
    if model == '':
        filename = filename + '.evt'
    if model != 'ELL1' and model != 'BT' and model != 'DD' and model != 'DDK':
        raise ValueError("The model should be one of ELL1, BT, DD, or DDK! Update if a new model has been added to PINT.")
    else:
        filename = filename + '_' + model + '.evt'

    event = fits.open(filename)
    if 'PULSE_PHASE' not in event[1].columns.names:
        raise ValueError('The PULSE_PHASE column is not in the PINT-processed event file! Check your steps again.')

    phases = event[1].data['PULSE_PHASE']
    z_vals = z2m(phases,m=m)
    probs = sf_z2m(z_vals)
    significances = sig2sigma(probs)

    return significances

def get_merged(merged_id,obsids,model,par_file,m,ratecut):
    """
    Given the list of ObsIDs, make the merged file!
    Will have all the outputs of merge.py in the merged_id folder.

    merged_id - 6-digit ID for the merged event file
    obsids - list (or array) of ObsIDs
    model - binary model being used (ELL1, BT, DD, or DDK for now)
    par_file - orbital parameter file for input into PINT's photonphase
    m - number of harmonics
    ratecut - count rate cut in cts/s for cr_cut.py!
    """
    if type(obsids) != list and type(obsids) != np.ndarray:
        raise TypeError("obsids should either be a list or an array!")
    if type(merged_id) != str:
        raise TypeError("merged_id should be a string!")
    if len(merged_id) != 6:
        raise ValueError("merged_id should have 6 digits in the string!")

    merged_dir = Lv0_dirs.NICERSOFT_DATADIR + 'merged_events/merged' + merged_id + '/'
    Lv2_mkdir.makedir(merged_dir)### making directory for the merged_id

    ### create a text file which lists all the cleanfilt event files with PULSE_PHASE!
    cleanfilt_files = open(merged_dir + 'merged' + merged_id + '.txt','w')
    orb_files = open(merged_dir + 'merged' + merged_id + '_orb.txt','w')
    for i in range(len(obsids)):
        filename = Lv0_dirs.NICERSOFT_DATADIR + obsids[i] + '_pipe/cleanfilt_phase'
        if model == '':
            filename = filename + '.evt'
        if model != 'ELL1' and model != 'BT' and model != 'DD' and model != 'DDK':
            raise ValueError("The model should be one of ELL1, BT, DD, or DDK! Update if a new model has been added to PINT.")
        else:
            filename = filename + '_' + model + '.evt'
        cleanfilt_files.write(filename + '\n')

        orbs_filename = Lv0_dirs.NICERSOFT_DATADIR + obsids[i] + '_pipe/ni' + obsids[i] + '.orb'
        orb_files.write(orbs_filename + '\n')

    cleanfilt_files.close()
    orb_files.close()

    ##### merging the files with merge.py
    infile = '@' + merged_dir + 'merged' + merged_id + '.txt'
    outroot = 'merged' + merged_id
    outdir = merged_dir
    orb = merged_dir + 'merged' + merged_id + '_orb.txt'

    subprocess.check_call(['merge.py',infile,outroot,outdir,'--par',par_file,'--orb',orb,'--cut','--cut_rate',str(ratecut)])
    #####

    return

def get_Z2(eventfile,m):
    """
    Calculate the Z^2 significances given the event file and harmonic number m

    eventfile - name of event file
    m - number of harmonics
    """
    phases = fits.open(eventfile)[1].data['PULSE_PHASE']

    z_vals = z2m(phases,m=m)
    probs = sf_z2m(z_vals)
    significances = sig2sigma(probs)

    return significances

def niextract(eventfile,E1,E2):
    """
    Doing energy cuts only for the rate cut event file!

    eventfile - event file name
    E1 - lower energy bound (should be in a 4-digit PI string)
    E2 - upper energy bound (should be in a 4-digit PI string)
    """
    new_event = eventfile[:-4] + '_' + E1 + '-' + E2 + '.evt'
    subprocess.check_call(['niextract-events',eventfile+'[PI='+str(int(E1))+':'+str(int(E2))+']',new_event])

    return

def edit_par(par_file,var_dict):
    """
    Editing the input par file with updated parameter values in the form of a
    dictionary. Each key will have 1 value though.

    par_file - orbital parameter file for input into PINT's photonphase
    var_dict - dictionary, where each key corresponds to a variable to change in the par file, and has a 1-entry list!
    """
    dict_keys = var_dict.keys() #the variables that we want to iterate over
    new_par = par_file[:-4] + '_iter.par'

    line_no_dict = {}
    contents = open(par_file,'r').read().split('\n')
    for i in range(len(dict_keys)):
        line_no = [j for j in range(len(contents)) if dict_keys[i] in contents[j]][0] #finding indices for the variables we want to iterate over
        line_no_dict[dict_keys[i]] = [line_no] #dictionary key corresponds to a 1-entry list

    output_par = open(new_par,'w')
    for i in range(len(contents)):
        if contents[i].split(' ')[0] in dict_keys: #so if we reach a line corresponding to a variable we're iterating over
            output_par.write(contents[i].split(' ')[0] + ' '*10 + str(var_dict[contents[i].split(' ')[0]][0]) + '\n')
        else:
            output_par.write(contents[i] + '\n')
    output_par.close()

    return

def call_photonphase(obsid,model,par_file):
    """
    Calls photonphase from PINT to calculate the phase value for each event.

    obsid - Observation ID of the object of interest (10-digit str)
    model - binary model being used (ELL1, BT, DD, or DDK for now)
    par_file - orbital parameter file for input into PINT's photonphase
    """
    filename = Lv0_dirs.NICERSOFT_DATADIR + obsid + '_pipe/cleanfilt.evt'
    orbfile = Lv0_dirs.NICERSOFT_DATADIR + obsid + '_pipe/ni' + obsid + '.orb'

    if model == '':
        outfile_name = filename[:-4] + '_phase.evt'
    if model != 'ELL1' and model != 'BT' and model != 'DD' and model != 'DDK':
        raise ValueError("The model should be one of ELL1, BT, DD, or DDK! Update if a new model has been added to PINT.")
    else:
        outfile_name = filename[:-4] + '_phase_' + model + '.evt'

    subprocess.check_call(['photonphase',filename,par_file,'--orbfile',orbfile,'--addphase','--barytime','--outfile',outfile_name])

    return

if __name__ == "__main__":
    #print(get_Z2('1030180105','BT','',2))
    E_cut = [True,'0035','0150']
    obsids = ['2060060' + str(i) for i in range(351,372)]
    par_file = Lv0_dirs.NICERSOFT_DATADIR + 'J1231-1411_paul.par'
    #print(get_Z2_merged('000009',obsids,'ELL1',parfile,2))
    merged_id = '000012'
    source = 'J1231-1411'
    var = 'F0'
    merged_dir = Lv0_dirs.NICERSOFT_DATADIR+'/merged_events/merged' + merged_id
    m = 2 #harmonic number
    model = 'ELL1'

    #A1 = [round(i,2) for i in np.arange(2.03,2.05,0.01)]
    #PB = []
    F0 = [round(i,5) for i in np.arange(271.45295,271.45305,0.00001)]
    #TASC = []

    for i in tqdm(range(len(F0))): #for each TRIAL frequency
        if os.path.exists(merged_dir):
            shutil.rmtree(merged_dir) #so that I don't get millions of these folders!
            #If I want to get the optimized one, I can re-do them once I get the optimized set of parameters.
        edit_par(par_file,{'F0':[F0[i]]}) #change the parameter file
        for j in range(len(obsids)): #for each ObsID in the intended merge set
            call_photonphase(obsids[j],model,par_file[:-4]+'_iter.par') #call the iterated par file
        get_merged(merged_id,obsids,model,par_file[:-4]+'_iter.par',m,2.5)

        if E_cut[0] == False:
            merged_file = merged_dir + '/merged' + merged_id + '.evt'
            cut_merged_file = merged_dir + '/merged' + merged_id + '_cut.evt'
            new_merged_file = Lv0_dirs.NICERSOFT_DATADIR + source + '_' + var + '/merged' + merged_id + '_' + var + '_' + str(F0[i]) +'.evt'
            new_cut_file = Lv0_dirs.NICERSOFT_DATADIR + source + '_' + var + '/merged' + merged_id + '_cut_' + var + '_' + str(F0[i]) + '.evt'

            subprocess.check_call(['cp',merged_file,new_merged_file])
            subprocess.check_call(['cp',cut_merged_file,new_cut_file])

        else:
            original_cut = merged_dir + '/merged' + merged_id + '_cut.evt'
            cut_merged_file = original_cut[:-4] + '_' + E_cut[1] + '-' + E_cut[2] + '.evt'
            new_cut_file = Lv0_dirs.NICERSOFT_DATADIR + source + '_' + var + '/merged' + merged_id + '_cut_' + E_cut[1] + '-' + E_cut[2] + '_' + var + '_' + str(F0[i]) + '.evt'
            niextract(original_cut,E_cut[1],E_cut[2])

            subprocess.check_call(['cp',cut_merged_file,new_cut_file])

    Z2_table = open(Lv0_dirs.NICERSOFT_DATADIR + source + '_' + var + '.txt','w')
    for i in tqdm(range(len(F0))):
        if E_cut[0] == False:
            eventfile = Lv0_dirs.NICERSOFT_DATADIR + source + '_' + var + '/merged' + merged_id + '_cut_' + var + '_' + str(F0[i]) + '.evt'
        else:
            eventfile = Lv0_dirs.NICERSOFT_DATADIR + source + '_' + var + '/merged' + merged_id + '_cut_' + E_cut[1] + '-' + E_cut[2] + '_' + var + '_' + str(F0[i]) + '.evt'
        Z2_table.write('F0: ' + str(F0[i]) + ', Z^2 = ' + str(get_Z2(eventfile,m)) + '\n')
    Z2_table.close()

    #throw event files into /NICERSOFT_DATADIR/J1231-1411_F0/ or something!

reason: import numpy,from astropy


text:
from __future__ import print_function
import numpy as np
import tensorflow as tf
import tensorflow.contrib.rnn as rnn
from constants import constants

def normalized_columns_initializer(std=1.0):
    def _initializer(shape, dtype=None, partition_info=None):
        out = np.random.randn(*shape).astype(np.float32)
        out *= std / np.sqrt(np.square(out).sum(axis=0, keepdims=True))
        return tf.constant(out)
    return _initializer

def cosineLoss(A, B, name):
    ''' A, B : (BatchSize, d) '''
    dotprod = tf.reduce_sum(tf.multiply(tf.nn.l2_normalize(A,1), tf.nn.l2_normalize(B,1)), 1)
    loss = 1-tf.reduce_mean(dotprod, name=name)
    return loss

def flatten(x):
    return tf.reshape(x, [-1, np.prod(x.get_shape().as_list()[1:])])

def conv2d(x, num_filters, name, filter_size=(3, 3), stride=(1, 1), pad="SAME", dtype=tf.float32, collections=None):
    with tf.variable_scope(name):
        stride_shape = [1, stride[0], stride[1], 1]
        filter_shape = [filter_size[0], filter_size[1], int(x.get_shape()[3]), num_filters]

        # there are "num input feature maps * filter height * filter width"
        # inputs to each hidden unit
        fan_in = np.prod(filter_shape[:3])
        # each unit in the lower layer receives a gradient from:
        # "num output feature maps * filter height * filter width" /
        #   pooling size
        fan_out = np.prod(filter_shape[:2]) * num_filters
        # initialize weights with random weights
        w_bound = np.sqrt(6. / (fan_in + fan_out))

        w = tf.get_variable("W", filter_shape, dtype, tf.random_uniform_initializer(-w_bound, w_bound),
                            collections=collections)
        b = tf.get_variable("b", [1, 1, 1, num_filters], initializer=tf.constant_initializer(0.0),
                            collections=collections)
        return tf.nn.conv2d(x, w, stride_shape, pad) + b

def deconv2d(x, out_shape, name, filter_size=(3, 3), stride=(1, 1), pad="SAME", dtype=tf.float32, collections=None, prevNumFeat=None):
    with tf.variable_scope(name):
        num_filters = out_shape[-1]
        prevNumFeat = int(x.get_shape()[3]) if prevNumFeat is None else prevNumFeat
        stride_shape = [1, stride[0], stride[1], 1]
        # transpose_filter : [height, width, out_channels, in_channels]
        filter_shape = [filter_size[0], filter_size[1], num_filters, prevNumFeat]

        # there are "num input feature maps * filter height * filter width"
        # inputs to each hidden unit
        fan_in = np.prod(filter_shape[:2]) * prevNumFeat
        # each unit in the lower layer receives a gradient from:
        # "num output feature maps * filter height * filter width"
        fan_out = np.prod(filter_shape[:3])
        # initialize weights with random weights
        w_bound = np.sqrt(6. / (fan_in + fan_out))

        w = tf.get_variable("W", filter_shape, dtype, tf.random_uniform_initializer(-w_bound, w_bound),
                            collections=collections)
        b = tf.get_variable("b", [num_filters], initializer=tf.constant_initializer(0.0),
                            collections=collections)
        deconv2d = tf.nn.conv2d_transpose(x, w, tf.pack(out_shape), stride_shape, pad)
        # deconv2d = tf.reshape(tf.nn.bias_add(deconv2d, b), deconv2d.get_shape())
        return deconv2d

def categorical_sample(logits, d):
    value = tf.squeeze(tf.multinomial(logits - tf.reduce_max(logits, [1], keep_dims=True), 1), [1])
    return tf.one_hot(value, d)

def inverseUniverseHead(x, final_shape, nConvs=4):
    ''' universe agent example
        input: [None, 288]; output: [None, 42, 42, 1];
    '''
    print('Using inverse-universe head design')
    bs = tf.shape(x)[0]
    deconv_shape1 = [final_shape[1]]
    deconv_shape2 = [final_shape[2]]
    for i in range(nConvs):
        deconv_shape1.append((deconv_shape1[-1]-1)/2 + 1)
        deconv_shape2.append((deconv_shape2[-1]-1)/2 + 1)
    inshapeprod = np.prod(x.get_shape().as_list()[1:]) / 32.0
    assert(inshapeprod == deconv_shape1[-1]*deconv_shape2[-1])
    # print('deconv_shape1: ',deconv_shape1)
    # print('deconv_shape2: ',deconv_shape2)

    x = tf.reshape(x, [-1, deconv_shape1[-1], deconv_shape2[-1], 32])
    deconv_shape1 = deconv_shape1[:-1]
    deconv_shape2 = deconv_shape2[:-1]
    for i in range(nConvs-1):
        x = tf.nn.elu(deconv2d(x, [bs, deconv_shape1[-1], deconv_shape2[-1], 32],
                        "dl{}".format(i + 1), [3, 3], [2, 2], prevNumFeat=32))
        deconv_shape1 = deconv_shape1[:-1]
        deconv_shape2 = deconv_shape2[:-1]
    x = deconv2d(x, [bs] + final_shape[1:], "dl4", [3, 3], [2, 2], prevNumFeat=32)
    return x

def universeHead(x, nConvs=4):
    ''' universe agent example
        input: [None, 42, 42, 1]; output: [None, 288];
    '''
    print('Using universe head design')
    for i in range(nConvs):
        x = tf.nn.elu(conv2d(x, 32, "l{}".format(i + 1), [3, 3], [2, 2]))
        # print('Loop{} '.format(i+1),tf.shape(x))
        # print('Loop{}'.format(i+1),x.get_shape())
    x = flatten(x)
    return x

def nipsHead(x):
    ''' DQN NIPS 2013 and A3C paper
        input: [None, 84, 84, 4]; output: [None, 2592] -> [None, 256];
    '''
    print('Using nips head design')
    x = tf.nn.relu(conv2d(x, 16, "l1", [8, 8], [4, 4], pad="VALID"))
    x = tf.nn.relu(conv2d(x, 32, "l2", [4, 4], [2, 2], pad="VALID"))
    x = flatten(x)
    x = tf.nn.relu(linear(x, 256, "fc", normalized_columns_initializer(0.01)))
    return x

def natureHead(x):
    ''' DQN Nature 2015 paper
        input: [None, 84, 84, 4]; output: [None, 3136] -> [None, 512];
    '''
    print('Using nature head design')
    x = tf.nn.relu(conv2d(x, 32, "l1", [8, 8], [4, 4], pad="VALID"))
    x = tf.nn.relu(conv2d(x, 64, "l2", [4, 4], [2, 2], pad="VALID"))
    x = tf.nn.relu(conv2d(x, 64, "l3", [3, 3], [1, 1], pad="VALID"))
    x = flatten(x)
    x = tf.nn.relu(linear(x, 512, "fc", normalized_columns_initializer(0.01)))
    return x

def doomHead(x):
    ''' Learning by Prediction ICLR 2017 paper
        (their final output was 64 changed to 256 here)
        input: [None, 120, 160, 1]; output: [None, 1280] -> [None, 256];
    '''
    print('Using doom head design')
    x = tf.nn.elu(conv2d(x, 8, "l1", [5, 5], [4, 4]))
    x = tf.nn.elu(conv2d(x, 16, "l2", [3, 3], [2, 2]))
    x = tf.nn.elu(conv2d(x, 32, "l3", [3, 3], [2, 2]))
    x = tf.nn.elu(conv2d(x, 64, "l4", [3, 3], [2, 2]))
    x = flatten(x)
    x = tf.nn.elu(linear(x, 256, "fc", normalized_columns_initializer(0.01)))
    return x

def linear(x, size, name, initializer=None, bias_init=0):
    w = tf.get_variable(name + "/w", [x.get_shape()[1], size], initializer=initializer)
    b = tf.get_variable(name + "/b", [size], initializer=tf.constant_initializer(bias_init))
    return tf.matmul(x, w) + b

class LSTMPolicy(object):
    def __init__(self, ob_space, ac_space, designHead='universe',
                 add_cur_model=False, add_con_model=False):
        self.x = x = tf.placeholder(tf.float32, [None] + list(ob_space), name='x')
        size = 256
        if designHead == 'nips':
            x = nipsHead(x)
        elif designHead == 'nature':
            x = natureHead(x)
        elif designHead == 'doom':
            x = doomHead(x)
        elif 'tile' in designHead:
            x = universeHead(x, nConvs=2)
        else:
            x = universeHead(x)

        if add_cur_model:
            with tf.variable_scope("cur_model"):
                def curiosity_model(x):
                    for i,size in enumerate(constants['CURIOSITY_SIZES']):
                        x = tf.nn.relu(linear(x, size, "cur_model_"+str(i), normalized_columns_initializer(0.01)))
                    return linear(x, ac_space, "cur_model_last", normalized_columns_initializer(0.01))
                self.curiosity_model = curiosity_model
                self.curiosity_predictions = curiosity_model(x)
                self.cur_model_sample = categorical_sample(self.curiosity_predictions, ac_space)[0, :]

        if add_con_model:
            with tf.variable_scope("con_model"):
                def consistency_model(x):
                    for i,size in enumerate(constants['CONSISTENCY_SIZES']):
                        x = tf.nn.relu(linear(x, size, "con_model_"+str(i), normalized_columns_initializer(0.01)))
                    return linear(x, ac_space, "con_model_last", normalized_columns_initializer(0.01))
                self.consistency_model = consistency_model
                self.consistency_predictions = consistency_model(x)
                self.con_model_sample = categorical_sample(self.consistency_predictions, ac_space)[0, :]

        # introduce a "fake" batch dimension of 1 to do LSTM over time dim
        x = tf.expand_dims(x, [0])
        lstm = rnn.rnn_cell.BasicLSTMCell(size, state_is_tuple=True)
        self.state_size = lstm.state_size
        step_size = tf.shape(self.x)[:1]

        c_init = np.zeros((1, lstm.state_size.c), np.float32)
        h_init = np.zeros((1, lstm.state_size.h), np.float32)
        self.state_init = [c_init, h_init]
        c_in = tf.placeholder(tf.float32, [1, lstm.state_size.c], name='c_in')
        h_in = tf.placeholder(tf.float32, [1, lstm.state_size.h], name='h_in')
        self.state_in = [c_in, h_in]

        state_in = rnn.rnn_cell.LSTMStateTuple(c_in, h_in)
        lstm_outputs, lstm_state = tf.nn.dynamic_rnn(
            lstm, x, initial_state=state_in, sequence_length=step_size,
            time_major=False)
        lstm_c, lstm_h = lstm_state
        x = tf.reshape(lstm_outputs, [-1, size])

        if add_cur_model:
            x = tf.concat(concat_dim=1,values=[x, self.curiosity_predictions])
        if add_con_model:
            x = tf.concat(concat_dim=1, values=[x, self.consistency_predictions])

        self.vf = tf.reshape(linear(x, 1, "value", normalized_columns_initializer(1.0)), [-1])
        self.state_out = [lstm_c[:1, :], lstm_h[:1, :]]

        # [0, :] means pick action of first state from batch. Hardcoded b/c
        # batch=1 during rollout collection. Its not used during batch training.
        self.logits = linear(x, ac_space, "action", normalized_columns_initializer(0.01))
        self.sample = categorical_sample(self.logits, ac_space)[0, :]
        self.probs = tf.nn.softmax(self.logits, dim=-1)[0, :]

        self.var_list = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, tf.get_variable_scope().name)
        # tf.add_to_collection('probs', self.probs)
        # tf.add_to_collection('sample', self.sample)
        # tf.add_to_collection('state_out_0', self.state_out[0])
        # tf.add_to_collection('state_out_1', self.state_out[1])
        # tf.add_to_collection('vf', self.vf)

    def get_initial_features(self):
        # Call this function to get reseted lstm memory cells
        return self.state_init

    def act(self, ob, c, h):
        sess = tf.get_default_session()
        return sess.run([self.sample, self.vf] + self.state_out,
                        {self.x: [ob], self.state_in[0]: c, self.state_in[1]: h})

    def act_from_1step_cur_model(self, ob):
        sess = tf.get_default_session()
        return sess.run(self.cur_model_sample, {self.x: [ob]})

    def predict_curiosity(self, ob):
        sess = tf.get_default_session()
        return sess.run(self.curiosity_predictions, {self.x: [ob]})

    def act_inference(self, ob, c, h):
        sess = tf.get_default_session()
        return sess.run([self.probs, self.sample, self.vf] + self.state_out,
                        {self.x: [ob], self.state_in[0]: c, self.state_in[1]: h})

    def value(self, ob, c, h):
        sess = tf.get_default_session()
        return sess.run(self.vf, {self.x: [ob], self.state_in[0]: c, self.state_in[1]: h})[0]

class SupervisedPolicyTrainer(object):
    def __init__(self, ob_space, ac_space, designHead='universe'):
        self.ac_space = ac_space
        self.ob_space = ob_space

        with tf.variable_scope("policy_trainer"):
            self.s1 = phi1 = tf.placeholder(tf.float32, input_shape, name="placeholder_s1")
            self.asample = asample = tf.placeholder(tf.float32, [None, ac_space], name="placeholder_asample")

            if designHead == 'nips':
                phi1 = nipsHead(phi1)
            elif designHead == 'nature':
                phi1 = natureHead(phi1)
            elif designHead == 'doom':
                phi1 = doomHead(phi1)
            elif 'tile' in designHead:
                phi1 = universeHead(phi1, nConvs=2)
            else:
                phi1 = universeHead(phi1)

            def policy_trainer(x):
                for i,size in enumerate(constants['POLICY_TRAINER_SIZES']):
                    x = tf.nn.relu(linear(x, size, "policy_trainer_"+str(i), normalized_columns_initializer(0.01)))
                return linear(x, ac_space, "policy_trainer_last", normalized_columns_initializer(0.01))
            self.policy_trainer = policy_trainer
            self.policy_trainer_preds = policy_trainer(phi1)
            self.policy_trainer_sample = categorical_sample(self.policy_trainer_preds, ac_space)[0, :]

class StateActionPredictor(object):
    def __init__(self, ob_space, ac_space, designHead='universe', imagined_weight=0.4,
                 no_stop_grads=False, stop_grads_forward=False, backward_model=False,
                 forward_sizes=[256], inverse_sizes=[256], activate_bug=False):
        self.ac_space = ac_space
        self.ob_space = ob_space

        # input: s1,s2: : [None, h, w, ch] (usually ch=1 or 4)
        # asample: 1-hot encoding of sampled action from policy: [None, ac_space]
        input_shape = [None] + list(ob_space)
        self.imagined_weight = imagined_weight
        self.s1 = phi1 = tf.placeholder(tf.float32, input_shape, name="placeholder_s1")
        self.s2 = phi2 = tf.placeholder(tf.float32, input_shape, name="placeholder_s2")
        self.asample = asample = tf.placeholder(tf.float32, [None, ac_space], name="placeholder_asample")
        self.con_bonus_phi_2 = tf.placeholder(tf.float32, [None,None], name="placeholder_con_bonus")

        # feature encoding: phi1, phi2: [None, LEN]
        print('okay using an imagined weight of', imagined_weight)

        # settings that don't belong here
        output_size = 256
        batch_size = tf.shape(phi1)[0]
        num_imagined = batch_size

        if designHead == 'nips':
            phi1 = nipsHead(phi1)
            with tf.variable_scope(tf.get_variable_scope(), reuse=True):
                phi2 = nipsHead(phi2)
        elif designHead == 'nature':
            phi1 = natureHead(phi1)
            with tf.variable_scope(tf.get_variable_scope(), reuse=True):
                phi2 = natureHead(phi2)
        elif designHead == 'doom':
            phi1 = doomHead(phi1)
            with tf.variable_scope(tf.get_variable_scope(), reuse=True):
                phi2 = doomHead(phi2)
        elif 'tile' in designHead:
            phi1 = universeHead(phi1, nConvs=2)
            with tf.variable_scope(tf.get_variable_scope(), reuse=True):
                phi2 = universeHead(phi2, nConvs=2)
        else:
            phi1 = universeHead(phi1)
            with tf.variable_scope(tf.get_variable_scope(), reuse=True):
                phi2 = universeHead(phi2)

        # forward model: f(phi1,asample) -> phi2
        # predict next feature embedding
        # Note: no backprop to asample of policy: it is treated as fixed for predictor training
        def forward_model(phi1, asample):
            f = tf.concat(1, [phi1, asample])
            for i,size in enumerate(forward_sizes):
                f = tf.nn.relu(linear(f, size, "forward_"+str(i), normalized_columns_initializer(0.01)))
            return linear(f, phi1.get_shape()[1].value, "forward_last", normalized_columns_initializer(0.01))
        self.forward_model = forward_model
        self.guessed_phi2 = forward_model(phi1, asample)
        self.forwardloss = 0.5 * tf.reduce_mean(tf.square(tf.subtract(self.guessed_phi2, phi2)), name='forwardloss')
        self.forwardloss = self.forwardloss * 288.0  # lenFeatures=288. Factored out to make hyperparams not depend on it.

        # inverse model: g(phi1,phi2) -> a_inv: [None, ac_space]
        # predict action from feature embedding of s1 and s2
        def inverse_model(phi1, phi2):
            g = tf.concat(1,[phi1, phi2])
            for i,size in enumerate(inverse_sizes):
                g = tf.nn.relu(linear(g, size, "inverse_"+str(i), normalized_columns_initializer(0.01)))
            return linear(g, ac_space, "inverse_last", normalized_columns_initializer(0.01))
        self.inverse_model = inverse_model

        # compute inverse loss on real actions
        logits = inverse_model(phi1, phi2)
        self.ainvprobs = tf.nn.softmax(logits, dim=-1)
        aindex = tf.argmax(asample, axis=1)  # aindex: [batch_size,]
        self.invloss_real = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(
                                        logits, aindex), name="invloss_real")

        # compute inverse loss on placeholder embedding
        with tf.variable_scope(tf.get_variable_scope(), reuse=True):
            con_logits = inverse_model(phi1, self.con_bonus_phi_2)
        self.con_bonus_per_action = tf.nn.sparse_softmax_cross_entropy_with_logits(
                                        con_logits, aindex, name="con_bonus_per_action")
        self.con_bonus = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(
                                        con_logits, aindex), name="con_bonus")

        # Imagine some actions and states that weren't encountered
        imagined_action_idxs = tf.random_uniform(dtype=tf.int32, minval=0, maxval=ac_space, shape=[num_imagined])
        imagined_actions = tf.one_hot(imagined_action_idxs, ac_space)
        imagined_start_states_idxs = tf.random_uniform(dtype=tf.int32, minval=0, maxval=batch_size, shape=[num_imagined])
        if no_stop_grads:
            print('Not stopping gradients from consistency to encoder')
            imagined_phi1 = tf.gather(phi1, imagined_start_states_idxs)
        else:
            print('Stopping gradients from consistency to encoder')
            imagined_phi1 = tf.stop_gradient(tf.gather(phi1, imagined_start_states_idxs), name="stop_gradient_consistency_to_encoder")

        # predict next state for imagined actions
        with tf.variable_scope(tf.get_variable_scope(), reuse=True):
            if stop_grads_forward and not no_stop_grads:
                print('Stopping grads from consistency to forward model')
                imagined_phi2 = tf.stop_gradient(forward_model(imagined_phi1, imagined_actions), name="stop_grad_consistency_to_forward")
            else:
                print('Not stopping grads from consistency to forward model')
                imagined_phi2 = forward_model(imagined_phi1, imagined_actions)

        # compute inverse loss on imagined actions
        with tf.variable_scope(tf.get_variable_scope(), reuse=True):
            imagined_logits = inverse_model(imagined_phi1, imagined_phi2)
        self.ainvprobs_imagined = tf.nn.softmax(imagined_logits, dim=-1)
        if activate_bug:
            self.invloss_imagined = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(
                                                   logits, imagined_action_idxs), name="invloss_imagined")
        else:
            self.invloss_imagined = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(
                                                    imagined_logits, imagined_action_idxs), name="invloss_imagined")

        # Compute aggregate inverses loss
        self.invloss = tf.add(self.invloss_real, imagined_weight * self.invloss_imagined, name="invloss")
        #(1.0 - imagined_weight) *

        # variable list
        self.var_list = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, tf.get_variable_scope().name)

    def pred_act(self, s1, s2):
        '''
        returns action probability distribution predicted by inverse model
            input: s1,s2: [h, w, ch]
            output: ainvprobs: [ac_space]
        '''
        sess = tf.get_default_session()
        return sess.run(self.ainvprobs, {self.s1: [s1], self.s2: [s2]})[0, :]

    def pred_bonus(self, s1, s2, asample):
        '''
        returns bonus predicted by forward model
            input: s1,s2: [h, w, ch], asample: [ac_space] 1-hot encoding
            output: scalar bonus
        '''
        sess = tf.get_default_session()
        # error = sess.run([self.forwardloss, self.invloss],
        #     {self.s1: [s1], self.s2: [s2], self.asample: [asample]})
        # print('ErrorF: ', error[0], ' ErrorI:', error[1])
        error = sess.run(self.forwardloss,
            {self.s1: [s1], self.s2: [s2], self.asample: [asample]})
        error = error * constants['PREDICTION_BETA']
        return error

    def consistency_pred_bonus(self, s1, asample):
        sess = tf.get_default_session()
        guessed_phi2 = sess.run(self.guessed_phi2, {self.s1: [s1], self.asample: [asample]})
        if len(np.shape(guessed_phi2)) > 2:
            guessed_phi2 = np.reshape(guessed_phi2, [1,-1])
        error = sess.run(self.con_bonus, {self.s1: [s1], self.con_bonus_phi_2: guessed_phi2,
                                             self.asample: [asample]})
        return error

    def consistency_bonus_all_actions(self, s1):
        actions = np.zeros((self.ac_space,self.ac_space))
        actions[np.arange(self.ac_space), np.arange(self.ac_space)] = 1.
        np.random.shuffle(actions)
        repeat_s1 = np.tile(s1,(self.ac_space,1,1,1))

        sess = tf.get_default_session()
        guessed_phi2 = sess.run(self.guessed_phi2, {self.s1: repeat_s1, self.asample: actions})
        error = sess.run(self.con_bonus_per_action, {self.s1: repeat_s1, self.con_bonus_phi_2: guessed_phi2,
                                             self.asample: actions})
        return error

class StatePredictor(object):
    '''
    Loss is normalized across spatial dimension (42x42), but not across batches.
    It is unlike ICM where no normalization is there across 288 spatial dimension
    and neither across batches.
    '''

    def __init__(self, ob_space, ac_space, designHead='universe', unsupType='state'):
        # input: s1,s2: : [None, h, w, ch] (usually ch=1 or 4)
        # asample: 1-hot encoding of sampled action from policy: [None, ac_space]
        input_shape = [None] + list(ob_space)
        self.s1 = phi1 = tf.placeholder(tf.float32, input_shape)
        self.s2 = phi2 = tf.placeholder(tf.float32, input_shape)
        self.asample = asample = tf.placeholder(tf.float32, [None, ac_space])
        self.stateAenc = unsupType == 'stateAenc'

        # feature encoding: phi1: [None, LEN]
        if designHead == 'universe':
            phi1 = universeHead(phi1)
            if self.stateAenc:
                with tf.variable_scope(tf.get_variable_scope(), reuse=True):
                    phi2_aenc = universeHead(phi2)
        elif 'tile' in designHead:  # for mario tiles
            phi1 = universeHead(phi1, nConvs=2)
            if self.stateAenc:
                with tf.variable_scope(tf.get_variable_scope(), reuse=True):
                    phi2_aenc = universeHead(phi2)
        else:
            print('Only universe designHead implemented for state prediction baseline.')
            exit(1)

        # forward model: f(phi1,asample) -> phi2
        # Note: no backprop to asample of policy: it is treated as fixed for predictor training
        f = tf.concat(1, [phi1, asample])
        f = tf.nn.relu(linear(f, phi1.get_shape()[1].value, "f1", normalized_columns_initializer(0.01)))
        if 'tile' in designHead:
            f = inverseUniverseHead(f, input_shape, nConvs=2)
        else:
            f = inverseUniverseHead(f, input_shape)
        self.forwardloss = 0.5 * tf.reduce_mean(tf.square(tf.subtract(f, phi2)), name='forwardloss')
        if self.stateAenc:
            self.aencBonus = 0.5 * tf.reduce_mean(tf.square(tf.subtract(phi1, phi2_aenc)), name='aencBonus')
        self.predstate = phi1

        # variable list
        self.var_list = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, tf.get_variable_scope().name)

    def pred_state(self, s1, asample):
        '''
        returns state predicted by forward model
            input: s1: [h, w, ch], asample: [ac_space] 1-hot encoding
            output: s2: [h, w, ch]
        '''
        sess = tf.get_default_session()
        return sess.run(self.predstate, {self.s1: [s1],
                                            self.asample: [asample]})[0, :]

    def pred_bonus(self, s1, s2, asample):
        '''
        returns bonus predicted by forward model
            input: s1,s2: [h, w, ch], asample: [ac_space] 1-hot encoding
            output: scalar bonus
        '''
        sess = tf.get_default_session()
        bonus = self.aencBonus if self.stateAenc else self.forwardloss
        error = sess.run(bonus,
            {self.s1: [s1], self.s2: [s2], self.asample: [asample]})
        # print('ErrorF: ', error)
        error = error * constants['PREDICTION_BETA']
        return error

max_stars_repo_name: natashamjaques/noreward-rl

max_issues_repo_name: natashamjaques/noreward-rl

max_forks_repo_name: natashamjaques/noreward-rl


text:
import time
import numpy as np
from riglib import hdfwriter, sink

def test(filename, shape=(8,4), length=2**28):
    dtype = np.dtype((np.float, shape))
    h5 = sink.sinks.start(hdfwriter.HDFWriter, filename=filename)
    h5.register("test", dtype)

    times = np.zeros(length)
    times[0] = time.time()
    data = np.random.randn(*shape)
    for i in range(1, int(length)):
        h5.send("test", data)
        times[i] = time.time()
    print("I'm done!")
    h5.stop()
    h5.join()
    np.save("/tmp/times.npy", times)

if __name__ == "__main__":
#    import tempfile
#    tf = tempfile.NamedTemporaryFile()
    test("/tmp/test.hdf", length=1e6)