text:
"""
PODPAC-DROUGHT-MONITOR SMAP-DATA-UPDATER AWS Handler
"""

import sys
import os
import subprocess

import boto3
import botocore
from multiprocessing.pool import ThreadPool

from six import string_types

def handler(event, context, source=None):
    """Lambda function handler

    Parameters
    ----------
    event : dict
        Description
    context : TYPE
        Description
    """
    print(event)

    sys.path.append("/tmp/")
    bucket = 'podpac-drought-monitor-s3'

    # get dependencies path
    if "FUNCTION_DEPENDENCIES_KEY" in os.environ:
        dependencies = os.environ["FUNCTION_DEPENDENCIES_KEY"]
    else:
        dependencies = "podpac_deps_{}.zip".format(
            os.environ.get("PODPAC_VERSION", '2.0.0')
        )  # this should be equivalent to version.semver()

    # Download dependencies from specific bucket/object
    if os.path.exists("/tmp/scipy"):
        print (
            "Scipy has been detected in the /tmp/ directory. Assuming this function is hot, dependencies will"
            " not be downloaded."
        )
    elif event:
        print ("Downloading and extracting dependencies")
        s3 = boto3.client("s3")
        s3.download_file(bucket, dependencies, "/tmp/" + dependencies)
        subprocess.call(["unzip", "/tmp/" + dependencies, "-d", "/tmp"])
        sys.path.append("/tmp/")
        subprocess.call(["rm", "/tmp/" + dependencies])

    import matplotlib

    matplotlib.use("agg")
    import datetime
    import podpac
    import xarray as xr
    import zarr
    import numpy as np

    from podpac import settings
    import podpac.datalib
    from podpac.datalib import smap_egi

    bucket = "podpac-drought-monitor-s3"
    if source is None:
        source = 's3://%s/%s' % (bucket, 'SMAP_CHUNKED32.zarr')

    tmpzarr = '/tmp/UPD.zarr'

    # AM/PM Data
    print("Opening L3 Zarr dataset.")
    smap_zarr = podpac.data.Zarr(
        source=source,
        time_key='time',
        data_key='Soil_Moisture_Retrieval_Data_AM/soil_moisture',
        lat_key='lat',
        lon_key='lon',
        crs='EPSG:4326',
        nan_vals=[-9999],
        skip_validation=True,
        file_mode='a'
    )
    smap_ds = smap_zarr.dataset

    print("Creating L3 Coordinates")
    coords = smap_zarr.coordinates.drop('time')
    time = podpac.crange(smap_zarr.coordinates['time'].bounds[-1], str(datetime.date.today()), '1,D', 'time')
    coords = podpac.coordinates.merge_dims([coords, podpac.Coordinates([time], crs=coords.crs)]).transpose(*smap_zarr.dims)

    # Create the local zarr file and download the relevant chunks
    zf = zarr.open(tmpzarr, 'a')

    available_data_keys = [
        'Soil_Moisture_Retrieval_Data_AM/soil_moisture',
        'Soil_Moisture_Retrieval_Data_AM/retrieval_qual_flag',
        'Soil_Moisture_Retrieval_Data_PM/soil_moisture_pm',
        'Soil_Moisture_Retrieval_Data_PM/retrieval_qual_flag_pm',
            ]

    for k in available_data_keys:
        zf.create_dataset(k, shape=smap_ds[k].shape, chunks=smap_ds[k].chunks, dtype=smap_ds[k].dtype, overwrite=True)
    zf['time'] = smap_ds['time']
    chunks = smap_ds[k].chunks
    shape = smap_ds[k].shape
    shape = shape[:2] + (smap_ds['time'].shape[0] + 1, )
    parts = [[i for i in range(int(np.ceil(shape[ii] / chunks[ii])))] for ii in range(3)]
    files = ['{}.{}.{}'.format(i, j, parts[2][-1]) for i in parts[0] for j in parts[1]]
    files.append('.zarray')

    print ('Downloading chunks that need updating locally in a separate thread')
    def download_files(src_root, dst_root, up=False):
        for f in files:
            for k in available_data_keys:
                src = '{}/{}/{}'.format(src_root, k, f)
                dst = '{}/{}/{}'.format(dst_root, k, f)
                if up:
                    smap_zarr.s3.put(src, dst)
                else:
                    try:
                        smap_zarr.s3.download(src, dst)
                    except FileNotFoundError:
                        pass

    up_pool = ThreadPool(1)
    r_up = up_pool.apply_async(download_files, (smap_zarr.source, tmpzarr))

    print("Downloading the new L3 Data")
    count = 0
    for c in coords.iterchunks(coords.shape[:-1] + (1, )):
        count += 1
        if count == 1: continue  # Start is already in the dataset
        print("Download L3 Data for:", str(c['time']), '...', end='')
        try:
            # AM Data
            print("AM", end=' ')
            smap = smap_egi.SMAP(product='SPL3SMP_E_AM', check_quality_flags=False)
            l3_am = smap.eval(c)
            smap.set_trait('data_key', smap.quality_flag_key)
            smap.update_cache = True
            print("AM_QF", end=' ')
            l3_am_qf = smap.eval(c)

            # PM Data
            print("PM", end=' ')
            smap = smap_egi.SMAP(product='SPL3SMP_E_PM', check_quality_flags=False)
            l3_pm = smap.eval(c)
            smap.set_trait('data_key', smap.quality_flag_key)
            smap.update_cache = True
            print("PM_QF", end=' ')
            l3_pm_qf = smap.eval(c)
        except ValueError as e:
            print("No Granules available:", e)
            continue

        # replace_nans
        l3_am.set(-9999, podpac.UnitsDataArray(np.isnan(l3_am)))
        l3_am_qf.set(-9999, podpac.UnitsDataArray(np.isnan(l3_am_qf)))
        l3_pm.set(-9999, podpac.UnitsDataArray(np.isnan(l3_pm)))
        l3_pm_qf.set(-9999, podpac.UnitsDataArray(np.isnan(l3_pm_qf)))

        print(" ... Done.")
        new_times = c.coords['time']
        print("Waiting for local chunks to download...", end='')
        up_pool.close()
        up_pool.join()
        r_up.get()
        print(" ... Done.")

        for i, nt in enumerate(new_times):
            if np.any((smap_ds['time'][:] - nt).astype(int) >= 0) or np.all(l3_am[..., i] == -9999) or np.all(l3_pm[..., i] == -9999):
                print('Time already exists, or is all nan -- skipping.')
                continue

            print("Updating S3 Zarr file for L3 Data.")
            old_time_shape = zf['time'].shape
            old_data_shape = zf[available_data_keys[0]].shape
            expected_data_shape = old_data_shape[:2] + old_time_shape

            # Make sure the starting sizes are all the same
            print ('Ensuring starting sizes are the same')
            zf['time'].resize(*old_time_shape)
            zf['Soil_Moisture_Retrieval_Data_AM/soil_moisture'].resize(*expected_data_shape)
            zf['Soil_Moisture_Retrieval_Data_AM/retrieval_qual_flag'].resize(*expected_data_shape)
            zf['Soil_Moisture_Retrieval_Data_PM/soil_moisture_pm'].resize(*expected_data_shape)
            zf['Soil_Moisture_Retrieval_Data_PM/retrieval_qual_flag_pm'].resize(*expected_data_shape)

            print ('Old Shape:', expected_data_shape)
            try:
                def f(key, data):
                    zf[key].append(data, axis=2)
                    return True

                # Update the zarr file
                ## IMPORTANT ONLY EXECUTE THIS CELL ONCE!!!! ANY ERRORS? DO NOT EXECUTE THE SAME APPEND AGAIN!
                f('Soil_Moisture_Retrieval_Data_AM/soil_moisture', l3_am[..., i:i+1])
                f('Soil_Moisture_Retrieval_Data_AM/retrieval_qual_flag', l3_am_qf[..., i:i+1])
                f('Soil_Moisture_Retrieval_Data_PM/soil_moisture_pm', l3_pm[..., i:i+1])
                f('Soil_Moisture_Retrieval_Data_PM/retrieval_qual_flag_pm', l3_pm_qf[..., i:i+1])
                print("Uploading local chunks")
                #files = os.path.listdir(tmpzarr)
                download_files(tmpzarr, smap_zarr.source, True)
                print("Updating time as final step")
                smap_ds['time'].append(np.atleast_1d(nt))
                zf['time'].append(np.atleast_1d(nt))
            except Exception as e:
                print ('Updating L3 data failed:', e)
                smap_ds['time'].resize(*old_time_shape)
                smap_ds['Soil_Moisture_Retrieval_Data_AM/soil_moisture'].resize(*expected_data_shape)
                smap_ds['Soil_Moisture_Retrieval_Data_AM/retrieval_qual_flag'].resize(*expected_data_shape)
                smap_ds['Soil_Moisture_Retrieval_Data_PM/soil_moisture_pm'].resize(*expected_data_shape)
                smap_ds['Soil_Moisture_Retrieval_Data_PM/retrieval_qual_flag_pm'].resize(*expected_data_shape)
            print ('New Shape:', smap_ds['Soil_Moisture_Retrieval_Data_AM/soil_moisture'].shape)
        break  # only do 1 loop
        zarr.consolidate_metadata(smap_zarr._get_store())
        print("Done!")
    return

if __name__ == '__main__':
    handler(None, None)#, source=r'C:\SMAP2.zarr')
    print('Done')

ext: py

max_stars_repo_name: creare-com/podpac-drought-monitor

max_issues_repo_name: creare-com/podpac-drought-monitor

max_forks_repo_name: creare-com/podpac-drought-monitor

reason: import numpy


text:
from GW151226_reader.data_reader import load_res

from bilby.gw.prior import PriorDict, Uniform
from deep_gw_pe_followup.sample_cacher.kde2d import get_kde
from deep_gw_pe_followup.sample_cacher import cacher
from effective_spins.priors_conditional_on_xeff import p_param_and_xeff
import numpy as np

SAMPLES_FILE = "qxeff_samples.h5"
ASTRO_SAMPLES_FILE = "astro_qxeff_samples.h5"

NUMERICAL_QXEFF_NORM_FACTOR = 5280.323631278067

def get_max_l_data():
    return dict(
        IAS={'q': 0.10537432884262372, 'xeff': 0.5899239152977372},
        LVK={'q': 0.29103432408863716, 'xeff': 0.2959265313232419},
        LOW_Q = {'q': 0.15, 'xeff': 0.5},
        HIGH_Q = {'q': 0.68, 'xeff': 0.15}
    )

def get_max_l_data_raw():
    res = load_res()
    for l, r in res.items():
        max_param = r.get_max_log_likelihood_param()
        print(l, dict(q=max_param["mass_ratio"], xeff=max_param["chi_eff"]))

    keys = ["chi_eff", "mass_ratio"]
    lvk_max_param = {
        k: v for k, v in res["lvk_data"].get_max_log_likelihood_param().items() if k in keys
    }
    ias_max_param = {
        k: v for k, v in res["ias_data"].get_max_log_likelihood_param().items() if k in keys
    }
    return dict(LVK=lvk_max_param, IAS=ias_max_param)

def get_kde_object(samp_file):
    samp_df = cacher.load_probabilities(samp_file)
    kde = get_kde(samp_df.q.values, samp_df.xeff.values)
    return kde

def calculate_p_with_kde(kde, params):
    print("Using KDE")
    for label, param in params.items():
        prob = kde([param['q'], param['xeff']])
        print(f"ln pi_qxeff({label} pt, {param})--> {np.log(prob/NUMERICAL_QXEFF_NORM_FACTOR)}")

def calculate_p_with_numerical(params):
    a1a2qcos2_prior = PriorDict(dict(
        a1=Uniform(0, 1),
        a2=Uniform(0, 1),
        q=Uniform(0, 1),
        cos2=Uniform(-1, 1)
    ))
    print("Using numerical")
    for label, param in params.items():
        prob = p_param_and_xeff(
            param=param['q'], xeff=param['xeff'],
            init_a1a2qcos2_prior=a1a2qcos2_prior, param_key='q'
        )
        print(f"ln pi_qxeff({label} pt, {param})--> {np.log(prob/NUMERICAL_QXEFF_NORM_FACTOR)}")

def main():
    params = get_max_l_data()
    calculate_p_with_kde(get_kde_object(SAMPLES_FILE), params)
    calculate_p_with_numerical(params)
    print("ASTRO KDE")
    calculate_p_with_kde(get_kde_object(ASTRO_SAMPLES_FILE), params)

if __name__ == '__main__':
    main()


text:
import sys
import os
import numpy as np
# from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
import utility
# import time

import preprocessing

if sys.platform != "win32":
    embedding_file = "./vec_all"
    func_embedding_file = "./func_vec_all"
    node2addr_file = "./data/DeepBD/nodeIndexToCode"
    func2addr_file = "./data/DeepBD/functionIndexToCode"
    bin_edgelist_file = "./data/DeepBD/edgelist"
    bin_features_file = "./data/DeepBD/features"
    func_features_file = "./data/DeepBD/func.features"
    ground_truth_file = "./data/DeepBD/addrMapping"
else:
    embedding_file = ".\\vec_all"
    func_embedding_file = ".\\func_vec_all"
    node2addr_file = ".\\data\\DeepBD\\nodeIndexToCode"
    func2addr_file = ".\\data\\DeepBD\\functionIndexToCode"
    bin_edgelist_file = ".\\data\\DeepBD\\edgelist"
    bin_features_file = ".\\data\\DeepBD\\features"
    func_features_file = ".\\data\\DeepBD\\func.features"
    ground_truth_file = ".\\data\\DeepBD\\addrMapping"

# whether use deepwalk to create embeddings for each function or not
# Set to false as default, which can get better result for now.
EBD_CALL_GRAPH = False

def pre_matching(bin1_name, bin2_name, toBeMergedBlocks={}):
    # if sys.platform != "win32":

    tadw_command = "python3 ./src/performTADW.py --method tadw --input " + bin_edgelist_file + " --graph-format edgelist --feature-file " + bin_features_file + " --output vec_all"
    os.system(tadw_command)

    ebd_dic, _ = utility.ebd_file_to_dic(embedding_file)

    node_in_bin1, _node_in_bin2 = utility.readNodeInfo(node2addr_file)

    bin1_mat = []
    bin2_mat = []
    node_map = {}
    for idx, line in ebd_dic.items():
        if idx < node_in_bin1:
            bin1_mat.append(line)
            node_map[str(idx)] = len(bin1_mat) - 1
        else:
            bin2_mat.append(line)
            node_map[str(idx)] = len(bin2_mat) - 1

    bin1_mat = np.array(bin1_mat)
    bin2_mat = np.array(bin2_mat)
    sim_result = utility.similarity_gpu(bin1_mat, bin2_mat)

    print("Perform matching...")
    matched_pairs, inserted, deleted = utility.matching(node_in_bin1, ebd_dic, sim_result, node_map, toBeMergedBlocks)

    print("matched pairs: ")
    print(matched_pairs)

    # print("Inserted blocks: ")
    # print(inserted)

    # print("Deleted blocks: ")
    # print(deleted)

# if __name__ == '__main__' :
#     # here is cross-platform configurations.
#     # actually I can do this in more elegant way, but it is enough for testing.

#     # np.set_printoptions(threshold=np.inf, suppress=True)  # set numpy options
#     sys.exit(two_level_matching('yes_830_o1', 'yes_830_o3'))

max_stars_repo_name: river-li/DeepBinDiff

max_issues_repo_name: river-li/DeepBinDiff

max_forks_repo_name: river-li/DeepBinDiff


text:
from collections import OrderedDict
from random import sample

import numpy as np

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

import rlkit.torch.pytorch_util as ptu
from rlkit.torch.torch_meta_irl_algorithm import np_to_pytorch_batch
from rlkit.core.eval_util import create_stats_ordered_dict
from rlkit.torch.torch_meta_irl_algorithm import TorchMetaIRLAlgorithm
from rlkit.torch.sac.policies import MakeDeterministic
from rlkit.samplers.util import rollout
from rlkit.torch.sac.policies import PostCondMLPPolicyWrapper
from rlkit.data_management.path_builder import PathBuilder

from gym.spaces import Dict

def concat_trajs(trajs):
    new_dict = {}
    for k in trajs[0].keys():
        if isinstance(trajs[0][k], dict):
            new_dict[k] = concat_trajs([t[k] for t in trajs])
        else:
            new_dict[k] = np.concatenate([t[k] for t in trajs], axis=0)
    return new_dict

class NeuralProcessAIRL(TorchMetaIRLAlgorithm):
    '''
        Meta-AIRL using a neural process

        assuming the context trajectories all have the same length and flat and everything nice
    '''
    def __init__(
            self,
            env,

            # this is the main policy network that we wrap with
            # PostCondWrapperPolicy for get_exploration policy
            main_policy,
            disc,

            train_context_expert_replay_buffer,
            train_test_expert_replay_buffer,
            test_context_expert_replay_buffer,
            test_test_expert_replay_buffer,

            np_encoder,

            policy_optimizer, # the RL algorith that updates the policy

            num_disc_updates_per_epoch=160,
            num_policy_updates_per_epoch=80,
            num_tasks_used_per_update=5,
            num_context_trajs_for_training=3,
            num_test_trajs_for_training=3,
            policy_batch_size_per_task=256,

            # for each task, for each context, infer post, for each post sample, generate some eval trajs
            num_tasks_per_eval=10,
            num_diff_context_per_eval_task=2,
            num_context_trajs_for_eval=3,
            num_eval_trajs_per_post_sample=2,

            num_context_trajs_for_exploration=3,

            encoder_lr=1e-3,
            encoder_optimizer_class=optim.Adam,

            disc_lr=1e-3,
            disc_optimizer_class=optim.Adam,

            use_grad_pen=True,
            grad_pen_weight=10,

            plotter=None,
            render_eval_paths=False,
            **kwargs
    ):
        super().__init__(
            env=env,
            train_context_expert_replay_buffer=train_context_expert_replay_buffer,
            train_test_expert_replay_buffer=train_test_expert_replay_buffer,
            test_context_expert_replay_buffer=test_context_expert_replay_buffer,
            test_test_expert_replay_buffer=test_test_expert_replay_buffer,
            **kwargs
        )

        self.main_policy = main_policy
        self.encoder = np_encoder
        self.disc = disc
        self.rewardf_eval_statistics = None

        self.policy_optimizer = policy_optimizer
        self.encoder_optimizer = encoder_optimizer_class(
            self.encoder.parameters(),
            lr=encoder_lr,
        )
        self.disc_optimizer = disc_optimizer_class(
            self.disc.parameters(),
            lr=disc_lr,
        )

        self.num_policy_updates_per_epoch = num_policy_updates_per_epoch
        self.num_disc_updates_per_epoch = num_disc_updates_per_epoch
        self.num_tasks_used_per_update = num_tasks_used_per_update
        self.num_context_trajs_for_training = num_context_trajs_for_training
        self.num_test_trajs_for_training = num_test_trajs_for_training
        self.policy_batch_size_per_task = policy_batch_size_per_task

        self.num_tasks_per_eval = num_tasks_per_eval
        self.num_diff_context_per_eval_task = num_diff_context_per_eval_task
        self.num_context_trajs_for_eval = num_context_trajs_for_eval
        self.num_eval_trajs_per_post_sample = num_eval_trajs_per_post_sample

        self.num_context_trajs_for_exploration = num_context_trajs_for_exploration

        # things we need for computing the discriminator objective
        self.bce = nn.BCEWithLogitsLoss()
        total_samples = self.max_path_length * self.num_tasks_used_per_update * (self.num_context_trajs_for_training + self.num_test_trajs_for_training)
        self.bce_targets = torch.cat(
            [
                torch.ones(total_samples, 1),
                torch.zeros(total_samples, 1)
            ],
            dim=0
        )
        self.bce_targets = Variable(self.bce_targets)
        if ptu.gpu_enabled():
            self.bce.cuda()
            self.bce_targets = self.bce_targets.cuda()

        self.use_grad_pen = use_grad_pen
        self.grad_pen_weight = grad_pen_weight

    def get_exploration_policy(self, task_identifier):
        list_of_trajs = self.train_context_expert_replay_buffer.sample_trajs_from_task(
            task_identifier,
            self.num_context_trajs_for_exploration,
        )
        post_dist = self.encoder([list_of_trajs])
        # z = post_dist.sample()
        z = post_dist.mean
        z = z.cpu().data.numpy()[0]
        return PostCondMLPPolicyWrapper(self.main_policy, z)

    def get_eval_policy(self, task_identifier, mode='meta_test'):
        if mode == 'meta_train':
            rb = self.train_context_expert_replay_buffer
        else:
            rb = self.test_context_expert_replay_buffer
        list_of_trajs = rb.sample_trajs_from_task(
            task_identifier,
            self.num_context_trajs_for_eval,
        )
        post_dist = self.encoder([list_of_trajs])
        # z = post_dist.sample()
        z = post_dist.mean
        z = z.cpu().data.numpy()[0]
        return PostCondMLPPolicyWrapper(self.main_policy, z)

    def _get_disc_training_batch(self):
        # k = list(self.replay_buffer.task_replay_buffers.keys())[0]
        # print('\nReplay Buffer')
        # print(len(self.replay_buffer.task_replay_buffers[k]._traj_endpoints))
        # print('\nTrain Context')
        # print(len(self.train_context_expert_replay_buffer.task_replay_buffers[k]._traj_endpoints))
        # print('\nTest Context')
        # print(len(self.train_test_expert_replay_buffer.task_replay_buffers[k]._traj_endpoints))

        # context batch is a list of list of dicts
        context_batch, task_identifiers_list = self.train_context_expert_replay_buffer.sample_trajs(
            self.num_context_trajs_for_training,
            num_tasks=self.num_tasks_used_per_update,
            keys=['observations', 'actions']
        )

        flat_context_batch = [traj for task_trajs in context_batch for traj in task_trajs]
        context_pred_batch = concat_trajs(flat_context_batch)

        test_batch, _ = self.train_test_expert_replay_buffer.sample_trajs(
            self.num_test_trajs_for_training,
            task_identifiers=task_identifiers_list,
            keys=['observations', 'actions']
        )
        flat_test_batch = [traj for task_trajs in test_batch for traj in task_trajs]
        test_pred_batch = concat_trajs(flat_test_batch)

        # get the test batch for the tasks from policy buffer
        policy_test_batch_0, _ = self.replay_buffer.sample_trajs(
            self.num_context_trajs_for_training,
            task_identifiers=task_identifiers_list,
            keys=['observations', 'actions']
        )
        flat_policy_batch_0 = [traj for task_trajs in policy_test_batch_0 for traj in task_trajs]
        policy_test_pred_batch_0 = concat_trajs(flat_policy_batch_0)

        policy_test_batch_1, _ = self.replay_buffer.sample_trajs(
            self.num_test_trajs_for_training,
            task_identifiers=task_identifiers_list,
            keys=['observations', 'actions']
        )
        flat_policy_batch_1 = [traj for task_trajs in policy_test_batch_1 for traj in task_trajs]
        policy_test_pred_batch_1 = concat_trajs(flat_policy_batch_1)

        policy_test_pred_batch = {
            'observations': np.concatenate((policy_test_pred_batch_0['observations'], policy_test_pred_batch_1['observations']), axis=0),
            'actions': np.concatenate((policy_test_pred_batch_0['actions'], policy_test_pred_batch_1['actions']), axis=0)
        }

        # if we want to handle envs with different traj lengths we need to do
        # something smarter with how we repeat z
        traj_len = flat_context_batch[0]['observations'].shape[0]
        assert all(t['observations'].shape[0] == traj_len for t in flat_context_batch), "Not handling different traj lens"
        assert all(t['observations'].shape[0] == traj_len for t in flat_test_batch), "Not handling different traj lens"
        assert all(t['observations'].shape[0] == traj_len for t in flat_policy_batch_0), "Not handling different traj lens"
        assert all(t['observations'].shape[0] == traj_len for t in flat_policy_batch_1), "Not handling different traj lens"

        return context_batch, context_pred_batch, test_pred_batch, policy_test_pred_batch, traj_len

    def _get_policy_training_batch(self):
        # context batch is a list of list of dicts
        context_batch, task_identifiers_list = self.train_context_expert_replay_buffer.sample_trajs(
            self.num_context_trajs_for_training,
            num_tasks=self.num_tasks_used_per_update,
            keys=['observations', 'actions']
        )

        # get the test batch for the tasks from policy buffer
        policy_batch, _ = self.replay_buffer.sample_random_batch(
            self.policy_batch_size_per_task,
            task_identifiers_list=task_identifiers_list
        )
        policy_obs = np.concatenate([d['observations'] for d in policy_batch], axis=0) # (N_tasks * batch_size) x Dim
        policy_acts = np.concatenate([d['actions'] for d in policy_batch], axis=0) # (N_tasks * batch_size) x Dim
        policy_terminals = np.concatenate([d['terminals'] for d in policy_batch], axis=0) # (N_tasks * batch_size) x Dim
        policy_next_obs = np.concatenate([d['next_observations'] for d in policy_batch], axis=0) # (N_tasks * batch_size) x Dim
        policy_batch = dict(
            observations=policy_obs,
            actions=policy_acts,
            terminals=policy_terminals,
            next_observations=policy_next_obs
        )

        return context_batch, policy_batch

    def _do_training(self):
        '''
        '''
        # train the discriminator (and the encoder)
        # print('$$$$$$$$$')
        # print(self.num_disc_updates_per_epoch)
        for i in range(self.num_disc_updates_per_epoch):
            self.encoder_optimizer.zero_grad()
            self.disc_optimizer.zero_grad()

            context_batch, context_pred_batch, test_pred_batch, policy_test_pred_batch, traj_len = self._get_disc_training_batch()

            # convert it to a pytorch tensor
            # note that our objective says we should maximize likelihood of
            # BOTH the context_batch and the test_batch
            exp_obs_batch = np.concatenate((context_pred_batch['observations'], test_pred_batch['observations']), axis=0)
            exp_obs_batch = Variable(ptu.from_numpy(exp_obs_batch), requires_grad=False)
            exp_acts_batch = np.concatenate((context_pred_batch['actions'], test_pred_batch['actions']), axis=0)
            exp_acts_batch = Variable(ptu.from_numpy(exp_acts_batch), requires_grad=False)

            policy_obs_batch = Variable(ptu.from_numpy(policy_test_pred_batch['observations']), requires_grad=False)
            policy_acts_batch = Variable(ptu.from_numpy(policy_test_pred_batch['actions']), requires_grad=False)

            post_dist = self.encoder(context_batch)
            # z = post_dist.sample() # N_tasks x Dim
            z = post_dist.mean

            # z_reg_loss = 0.0001 * z.norm(2, dim=1).mean()
            z_reg_loss = 0.0

            # make z's for expert samples
            context_pred_z = z.repeat(1, traj_len * self.num_context_trajs_for_training).view(
                -1,
                z.size(1)
            )
            test_pred_z = z.repeat(1, traj_len * self.num_test_trajs_for_training).view(
                -1,
                z.size(1)
            )
            z_batch = torch.cat([context_pred_z, test_pred_z], dim=0)
            positive_obs_batch = torch.cat([exp_obs_batch, z_batch], dim=1)
            positive_acts_batch = exp_acts_batch

            # make z's for policy samples
            z_policy = z_batch
            negative_obs_batch = torch.cat([policy_obs_batch, z_policy], dim=1)
            negative_acts_batch = policy_acts_batch

            # compute the loss for the discriminator
            obs_batch = torch.cat([positive_obs_batch, negative_obs_batch], dim=0)
            acts_batch = torch.cat([positive_acts_batch, negative_acts_batch], dim=0)
            disc_logits = self.disc(obs_batch, acts_batch)
            disc_preds = (disc_logits > 0).type(torch.FloatTensor)
            # disc_percent_policy_preds_one = disc_preds[z.size(0):].mean()
            disc_loss = self.bce(disc_logits, self.bce_targets)
            accuracy = (disc_preds == self.bce_targets).type(torch.FloatTensor).mean()

            if self.use_grad_pen:
                eps = Variable(torch.rand(positive_obs_batch.size(0), 1), requires_grad=True)
                if ptu.gpu_enabled(): eps = eps.cuda()

                # old and probably has a bad weird effect on the encoder
                # difference is that before I was also taking into account norm of grad of disc
                # wrt the z
                # interp_obs = eps*positive_obs_batch + (1-eps)*negative_obs_batch

                # permute the exp_obs_batch (not just within a single traj, but overall)
                # This is actually a really tricky question how to permute the batches
                # 1) permute within each of trajectories
                # z's will be matched, colors won't be matched anyways
                # 2) permute within trajectories corresponding to a single context set
                # z's will be matched, colors will be "more unmatched"
                # 3) just shuffle everything up
                # Also, the z's need to be handled appropriately

                interp_obs = eps*exp_obs_batch + (1-eps)*policy_obs_batch
                # interp_z = z_batch.detach()
                # interp_obs = torch.cat([interp_obs, interp_z], dim=1)
                interp_obs.detach()
                # interp_obs.requires_grad = True
                interp_actions = eps*positive_acts_batch + (1-eps)*negative_acts_batch
                interp_actions.detach()
                # interp_actions.requires_grad = True
                gradients = autograd.grad(
                    outputs=self.disc(torch.cat([interp_obs, z_batch.detach()], dim=1), interp_actions).sum(),
                    inputs=[interp_obs, interp_actions],
                    # grad_outputs=torch.ones(exp_specs['batch_size'], 1).cuda(),
                    create_graph=True, retain_graph=True, only_inputs=True
                )
                # print(gradients[0].size())
                # z_norm = gradients[0][:,-50:].norm(2, dim=1)
                # print('Z grad norm: %.4f +/- %.4f' % (torch.mean(z_norm), torch.std(z_norm)))
                # print(gradients[0][:,-50:].size())
                # o_norm = gradients[0][:,:-50].norm(2, dim=1)

                # o_norm = gradients[0].norm(2, dim=1)
                # print('Obs grad norm: %.4f +/- %.4f' % (torch.mean(o_norm), torch.std(o_norm)))
                # print(gradients[0].size())

                # print(gradients[0][:,:50].norm(2, dim=1))

                total_grad = torch.cat([gradients[0], gradients[1]], dim=1)
                # print(total_grad.size())
                gradient_penalty = ((total_grad.norm(2, dim=1) - 1) ** 2).mean()

                # another form of grad pen
                # gradient_penalty = (total_grad.norm(2, dim=1) ** 2).mean()

                disc_loss = disc_loss + gradient_penalty * self.grad_pen_weight

            total_reward_loss = z_reg_loss + disc_loss

            total_reward_loss.backward()
            self.disc_optimizer.step()
            self.encoder_optimizer.step()

            # print(self.disc.fc0.bias[0])
            # print(self.encoder.traj_encoder.traj_enc_mlp.fc0.bias[0])

        # train the policy
        # print('--------')
        # print(self.num_policy_updates_per_epoch)
        for i in range(self.num_policy_updates_per_epoch):
            context_batch, policy_batch = self._get_policy_training_batch()
            policy_batch = np_to_pytorch_batch(policy_batch)

            post_dist = self.encoder(context_batch)
            # z = post_dist.sample() # N_tasks x Dim
            z = post_dist.mean
            z = z.detach()

            # repeat z to have the right size
            z = z.repeat(1, self.policy_batch_size_per_task).view(
                self.num_tasks_used_per_update * self.policy_batch_size_per_task,
                -1
            ).detach()

            # now augment the obs with the latent sample z
            policy_batch['observations'] = torch.cat([policy_batch['observations'], z], dim=1)
            policy_batch['next_observations'] = torch.cat([policy_batch['next_observations'], z], dim=1)

            # compute the rewards
            # If you compute log(D) - log(1-D) then you just get the logits
            policy_rewards = self.disc(policy_batch['observations'], policy_batch['actions']).detach()
            policy_batch['rewards'] = policy_rewards
            # rew_more_than_zero = (rewards > 0).type(torch.FloatTensor).mean()
            # print(rew_more_than_zero.data[0])

            # do a policy update (the zeroing of grads etc. should be handled internally)
            # print(policy_rewards.size())
            self.policy_optimizer.train_step(policy_batch)
            # print(self.main_policy.fc0.bias[0])

        if self.eval_statistics is None:
            """
            Eval should set this to None.
            This way, these statistics are only computed for one batch.
            """
            self.eval_statistics = OrderedDict()
            self.eval_statistics['Disc Loss'] = np.mean(ptu.get_numpy(disc_loss))
            self.eval_statistics['Disc Acc'] = np.mean(ptu.get_numpy(accuracy))
            # self.eval_statistics['Disc Percent Policy Preds 1'] = np.mean(ptu.get_numpy(disc_percent_policy_preds_one))
            self.eval_statistics['Disc Rewards Mean'] = np.mean(ptu.get_numpy(policy_rewards))
            self.eval_statistics['Disc Rewards Std'] = np.std(ptu.get_numpy(policy_rewards))
            self.eval_statistics['Disc Rewards Max'] = np.max(ptu.get_numpy(policy_rewards))
            self.eval_statistics['Disc Rewards Min'] = np.min(ptu.get_numpy(policy_rewards))
            # self.eval_statistics['Disc Rewards GT Zero'] = np.mean(ptu.get_numpy(rew_more_than_zero))

            z_norm = z.norm(2, dim=1).mean()
            self.eval_statistics['Z Norm'] = np.mean(ptu.get_numpy(z_norm))

            if self.policy_optimizer.eval_statistics is not None:
                self.eval_statistics.update(self.policy_optimizer.eval_statistics)

    def evaluate(self, epoch):
        super().evaluate(epoch)
        self.policy_optimizer.eval_statistics = None

    def obtain_eval_samples(self, epoch, mode='meta_train'):
        self.training_mode(False)

        if mode == 'meta_train':
            params_samples = self.train_task_params_sampler.sample_unique(self.num_tasks_per_eval)
        else:
            params_samples = self.test_task_params_sampler.sample_unique(self.num_tasks_per_eval)
        all_eval_tasks_paths = []
        for task_params, obs_task_params in params_samples:
            cur_eval_task_paths = []
            self.env.reset(task_params=task_params, obs_task_params=obs_task_params)
            task_identifier = self.env.task_identifier

            for _ in range(self.num_diff_context_per_eval_task):
                eval_policy = self.get_eval_policy(task_identifier, mode=mode)

                for _ in range(self.num_eval_trajs_per_post_sample):
                    cur_eval_path_builder = PathBuilder()
                    observation = self.env.reset(task_params=task_params, obs_task_params=obs_task_params)
                    terminal = False

                    while (not terminal) and len(cur_eval_path_builder) < self.max_path_length:
                        if isinstance(self.obs_space, Dict):
                            if self.policy_uses_pixels:
                                agent_obs = observation['pixels']
                            else:
                                agent_obs = observation['obs']
                        else:
                            agent_obs = observation
                        action, agent_info = eval_policy.get_action(agent_obs)

                        next_ob, raw_reward, terminal, env_info = (self.env.step(action))
                        if self.no_terminal:
                            terminal = False

                        reward = raw_reward
                        terminal = np.array([terminal])
                        reward = np.array([reward])
                        cur_eval_path_builder.add_all(
                            observations=observation,
                            actions=action,
                            rewards=reward,
                            next_observations=next_ob,
                            terminals=terminal,
                            agent_infos=agent_info,
                            env_infos=env_info,
                            task_identifiers=task_identifier
                        )
                        observation = next_ob

                    if terminal and self.wrap_absorbing:
                        raise NotImplementedError("I think they used 0 actions for this")
                        cur_eval_path_builder.add_all(
                            observations=next_ob,
                            actions=action,
                            rewards=reward,
                            next_observations=next_ob,
                            terminals=terminal,
                            agent_infos=agent_info,
                            env_infos=env_info,
                            task_identifiers=task_identifier
                        )

                    if len(cur_eval_path_builder) > 0:
                        cur_eval_task_paths.append(
                            cur_eval_path_builder.get_all_stacked()
                        )
            all_eval_tasks_paths.extend(cur_eval_task_paths)

        # flatten the list of lists
        return all_eval_tasks_paths

    @property
    def networks(self):
        return [
            self.encoder,
            self.main_policy
        ] + self.policy_optimizer.networks

    def get_epoch_snapshot(self, epoch):
        snapshot = super().get_epoch_snapshot(epoch)
        snapshot.update(
            encoder=self.encoder,
            main_policy=self.main_policy
        )
        snapshot.update(self.policy_optimizer.get_snapshot())
        return snapshot


text:
from networkx import Graph, connected_components

def intervene(graph,opts):
	# make a temporary copy of the giant component
	temp = Graph(graph)
	cc = list(max(connected_components(graph), key=len))
	ncc = [n for n in list(graph.nodes()) if n not in cc]
	temp.remove_nodes_from(ncc)

	# get a dictionary of "is a node extremist"
	eh = opts.initial.max * 0.9
	el = opts.initial.min * 0.9
	extremists = {n: not el < graph.values["opinion"][n] < eh for n in temp.nodes()}

	# assign weights to edges
	target_edges = {}
	for node in temp.nodes():
		# we don't trim edges from the extremist agents, but rather from their targets
		if extremists[node]:
			continue
		neighbors = list(temp[node])
		myop = graph.values["opinion"][node]
		extreme_neighbors = [n for n in neighbors if extremists[n]]
		# weight is 1/(distance to closest extremist neighbour) * (proportion of extreme neighbors)
		if len(extreme_neighbors):
			weight = max(abs(1/(myop-graph.values["opinion"][n])) for n in extreme_neighbors)
			weight *= len(extreme_neighbors)/len(neighbors)
			for ex_n in extreme_neighbors:
				target_edges[(node,ex_n)] = weight

	# get the edge with the highest weight and remove it
	targets = sorted(target_edges.items(), key=lambda e: e[1])
	if len(targets):
		edge = targets[-1][0]
		graph.remove_edge(*edge)
	return

max_stars_repo_name: Kanavoy/UODS

max_issues_repo_name: Kanavoy/UODS

max_forks_repo_name: Kanavoy/UODS

reason: from networkx


text:
import logging
import numpy as np
from abc import ABC, abstractmethod

from ..utils.time import timeit

logger = logging.getLogger(__name__)

class PoseDetector(ABC):

    def __init__(self,
                stride=8,
                hthreshold=0.1,
                pthreshold=0.05,
                pretrain_model=None,
                device="cpu",
                **kwargs):
        self.stride = stride
        self.hthreshold = hthreshold
        self.pthreshold = pthreshold
        self.pretrain_model = pretrain_model
        self.device = device
        self.model = None

    @timeit(logger)
    def __call__(self, imgs):
        self._check_input(imgs)
        data = self.preprocessing(imgs)

        pafs, heatmaps = self.model(data)

        peoples = self.postprocessing(imgs, heatmaps, pafs)
        self._check_output(peoples)

        return peoples

    def _check_input(self, imgs):
        assert type(imgs) == list
        # img is numpy array
        for img in imgs:
            assert type(img) == np.ndarray

        # all imgs with same size
        size = tuple(imgs[0].shape)
        for img in imgs:
            assert tuple(img.shape) == size

    def _check_output(self, peoples):
        assert type(peoples) == list
        for people in peoples:
            assert type(people) == list
            for person in people:
                assert 'conf' in person
                assert 'bbox' in person
                assert 'n_parts' in person
                assert 'keypoints' in person

    @abstractmethod
    def preprocessing(self, imgs):
        pass

    @abstractmethod
    def postprocessing(self, imgs, heatmaps, pafs):
        pass

max_stars_repo_name: johnnylord/mtmc-testbed

max_issues_repo_name: johnnylord/mtmc-testbed

max_forks_repo_name: johnnylord/mtmc-testbed


text:
#! /usr/bin/python

# Copyright (c) 2019 Filippo Ranza <filipporanza@gmail.com>

import unittest
from tempfile import TemporaryDirectory
from os.path import join
import numpy as np
from fp_engine.fp_parser.json_parser import parse_data, load_json_instance

class TestJsonLoader(unittest.TestCase):
    def test_parse_data(self):
        instance = {
            "min": [1, 1],
            "constrs": [
                {"eq": [1, 1], "ge": 1},
                {"eq": [5, 10], "ge": 5},
                {"eq": [11, 5], "le": 11},
                {"eq": [11, 2], "le": 11},
            ],
        }

        c, A, b = parse_data(instance)

        valid_c = np.array([1, 1])
        valid_A = np.array([[-1, -1], [-5, -10], [11, 5], [11, 2]])
        valid_b = np.array([-1, -5, 11, 11])

        self.assertTrue(np.array_equal(c, valid_c))
        self.assertTrue(np.array_equal(A, valid_A))
        self.assertTrue(np.array_equal(b, valid_b))

    def test_json_loader(self):
        instance = """
        {
            "min" : [1, 1],
            "constrs":[
                {"eq": [1, 1], "ge": 1},
                {"eq": [5, 10], "ge": 5},
                {"eq": [11, 5], "le": 11},
                {"eq": [11, 2], "le": 11}
            ]
        }
        """

        with TemporaryDirectory() as temp_dir:
            path = join(temp_dir, "instance.json")
            with open(path, "w") as out:
                out.write(instance)

            c, A, b = load_json_instance(path)

            valid_c = np.array([1, 1])
            valid_A = np.array([[-1, -1], [-5, -10], [11, 5], [11, 2]])
            valid_b = np.array([-1, -5, 11, 11])

            self.assertTrue(np.array_equal(c, valid_c))
            self.assertTrue(np.array_equal(A, valid_A))
            self.assertTrue(np.array_equal(b, valid_b))

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

max_stars_repo_name: FilippoRanza/Feasibility-Pump

max_issues_repo_name: FilippoRanza/Feasibility-Pump

max_forks_repo_name: FilippoRanza/Feasibility-Pump


text:
from __future__ import absolute_import, division, print_function

__doc__ = """
.. autofunction:: generate_list1_gallery
"""

__copyright__ = "Copyright (C) 2017 - 2018 Xiaoyu Wei"

__license__ = """
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""

import numpy as np

# {{{ TreeBox class
class TreeBox:
    """
    A minimalistic tree class.
    Courtesy of: Andreas Klockner
    """

    def __init__(self, center, radius, child_nlevels):
        self.center = center
        self.radius = radius

        self.children = []

        if child_nlevels:
            child_radius = radius // 2
            assert child_radius

            dimensions = len(center)

            for i in range(2 ** dimensions):
                child_center = center.copy()
                for idim in range(dimensions):
                    # 1 if that dimension bit is set, 0 if not
                    dim_indicator = int(bool(i & 1 << idim))
                    child_center[idim] += (2 * dim_indicator - 1) * child_radius

                self.children.append(
                    TreeBox(child_center, child_radius, child_nlevels - 1)
                )

    def draw(self):
        lx, ly = self.center - self.radius * 0.95
        hx, hy = self.center + self.radius * 0.95

        import matplotlib.pyplot as plt

        plt.plot([lx, lx, hx, hx, lx], [ly, hy, hy, ly, ly])

# }}} End TreeBox class

def build_tree(dimensions):
    """
    Courtesy of: Andreas Klockner
    """
    # four levels deep
    # -> centers on a 2**4 x 2**4 grid

    nlevels = 4
    root_radius = 2 ** (nlevels - 1)
    root = TreeBox(
        center=np.array(dimensions * [root_radius], np.int),
        radius=root_radius,
        child_nlevels=nlevels - 1,
    )

    return root

def generate_boxes_on_level(box, ilevel):
    """
    Courtesy of: Andreas Klockner
    """
    if ilevel:
        for child in box.children:
            for result in generate_boxes_on_level(child, ilevel - 1):
                yield result
    else:
        yield box

def generate_boxes(box):
    """
    Courtesy of: Andreas Klockner
    """
    yield box

    for child in box.children:
        for result in generate_boxes(child):
            yield result

def linf_dist(box1, box2):
    """
    Courtesy of: Andreas Klockner
    """
    return np.max(np.abs(box1.center - box2.center) - (box1.radius + box2.radius))

def generate_interactions(dimensions):
    """
    Courtesy of: Andreas Klockner
    """
    root = build_tree(dimensions)
    root_radius = root.radius

    min_cutoff = root_radius >> 2
    max_cutoff = 2 * root_radius - min_cutoff

    target_boxes = [
        box
        for box in generate_boxes_on_level(root, 2)
        if np.min(box.center) > min_cutoff and np.max(box.center) < max_cutoff
    ]

    near_neighbor_interactions = [
        (tbox, sbox)
        for tbox in target_boxes
        for sbox in generate_boxes(root)
        if linf_dist(tbox, sbox) == 0
    ]

    if 0:
        import matplotlib.pyplot as plt

        for tbox, sbox in near_neighbor_interactions:
            plt.figure()
            plt.gca().set_aspect("equal")
            tbox.draw()
            sbox.draw()
    return near_neighbor_interactions

def postprocess_interactions(near_neighbor_interactions):
    unique_interaction_vectors = set()

    for (tbox, sbox) in near_neighbor_interactions:
        unique_interaction_vectors.add(tuple(tbox.center - sbox.center))

    # Add interactions within the same box
    tb0, wb0 = near_neighbor_interactions[0]
    unique_interaction_vectors.add(tuple(tb0.center - tb0.center))

    list1_interactions = sorted(list(unique_interaction_vectors))

    return list1_interactions

def generate_list1_gallery(dim):
    """Generate a *list1* that servers as the gallery for all possible *list1*
    interactions with given dimension and order.
    """
    # contains each sourcebox.center-to-targetbox.center vector
    # source box is 4x4 to make all involved lengths to be integers
    vec_list = postprocess_interactions(generate_interactions(dim))

    distinct_numbers = set()
    for vec in vec_list:
        for cvc in vec:
            distinct_numbers.add(cvc)

    # contains a lookup table for case indices
    base = len(range(min(distinct_numbers), max(distinct_numbers) + 1))
    case_indices = -np.ones(base ** dim, dtype=int)
    shift = -min(distinct_numbers)

    def case_encode(case_vec):
        table_id = 0
        for cvc in case_vec:
            table_id = table_id * base + (cvc + shift)
        return int(table_id)

    case_id = 0
    for vec in vec_list:
        case_indices[case_encode(vec)] = case_id
        case_id += 1

    assert len(vec_list) == case_id

    return (vec_list, case_encode, case_indices)

# vim: ft=pyopencl:fdm=marker

max_stars_repo_name: xywei/volumential

max_issues_repo_name: inducer/volumential

max_forks_repo_name: inducer/volumential


text:
""" This module contains the main functions for visualising synthetic data """
import functools
import numpy as np
import matplotlib.pyplot as plt

def plot_data(plotting_function, *plotargs, **plotkwargs):
    """ This function is a decorator generator function

    The output of this function is a function based on the arguments provided
    The outputting function actually wraps, the function that was decorated in order to:
     plot the data generated for easy visualisation

    """

    # wrap the decorator, to be able to pass, plotting functions
    def _plot(func):

        # wrap the original function
        @functools.wraps(func)
        def _wrapper(*args, **kwargs):

            # check if plot was invoked
            if kwargs.get("plot", None):

                # remove the plot argument from kwargs, since its not used in the wrapped function
                del kwargs["plot"]

                # run the function that generates the data and pass any remaining arguments to it
                returned_data = func(*args, **kwargs)

                # check if there was a plot_name specified
                if plotkwargs.get("plot_name", None):
                    plot_name = plotkwargs["plot_name"]
                else:
                    plot_name = func.__name__

                # now plot the gotten data, and pass any arguments originally defined for plotting
                plotting_function(returned_data, plot_name, *plotargs, **plotkwargs)

                # now return the data once the plotting is closed
                return returned_data

            # if not just return the data
            else:
                return func(*args, **kwargs)

        # unroll
        return _wrapper
    return _plot

def plot_1D(timeseries_df, name):
    """ This is the function that corresponds to plotting 1D data """

    # create figure
    fig = plt.figure(figsize=(10, 6))

    # give it a name
    fig.suptitle(name, fontsize="20")

    plt.plot(timeseries_df.values)
    plt.show()

def plot_multidim(timeseries_df, name):
    """ This function corresponds to plotting multidimensional data """
    # create figure
    fig = plt.figure(figsize=(10, 6))

    # give it a name
    fig.suptitle(name, fontsize="20")

    # figure out the needed grid
    num_vals, num_dim = timeseries_df.shape
    grid_size = num_dim, 1

    x_coords = np.arange(0, num_vals)

    # plot individual dimensions
    for dim_n, dim_name in enumerate(timeseries_df.columns):
        current_ax = plt.subplot2grid(grid_size, (dim_n, 0))
        current_ax.plot(x_coords, dim_name, data=timeseries_df)
        current_ax.set_title(f"Signal of {dim_name}", fontsize="16")
        current_ax.set_xlabel("time", fontsize="14")
        current_ax.set_ylabel(dim_name, fontsize="14")

    plt.subplots_adjust(hspace=1.2, top=0.8)

    plt.show()

if __name__ == "__main__":
    raise RuntimeError("This module should not be run directly!")

max_stars_repo_name: HySynth/HySynth

max_issues_repo_name: HySynth/HySynth

max_forks_repo_name: HySynth/HySynth


text:
#!/usr/bin/env python
import rospy
from geometry_msgs.msg import PoseStamped
from geometry_msgs.msg import TwistStamped
import numpy as np

max_len = 1000

x_list = []
y_list = []
z_list = []
xn_list = []
yn_list = []

def cb(msg):
  entry_x = [msg.header.seq, msg.pose.position.x]
  entry_y = [msg.header.seq, msg.pose.position.y]
  x_list.append(entry_x)
  y_list.append(entry_y)

def cb(msg2):
  entry_nx = [msg.header.seq, msg.pose.position.x]
  entry_ny = [msg.header.seq, msg.pose.position.y]
  nx_list.append(entry_nx)
  ny_list.append(entry_ny)
  if(len(ny_list) == max_len):
    cal_dev()
    exit()

def cal_dev():

def element_wise_multiplication(data1, data2):
  output = []
  for i in range(len(data1)):
    output.append(data1[i]*data2[i])
  return output

def element_wise_multiplication(data1, data2):
  output = []
  for i in range(len(data1)):
    output.append(data1[i]*data2[i])
  return output

def sum_element(data):
  sum = 0
  for i in range(len(data)):
    sum += data[i]
  return sum

def main():
  rospy.init_node('imu_cm_republisher')
  sub = rospy.Subscriber('gnss_pose', PoseStamped, cb)
  sub2 = rospy.Subscriber('gnss_pose_noise', PoseStamped, cb2)

  r = rospy.Rate(100)

  rospy.spin()

if __name__=="__main__":
  main()

max_stars_repo_name: rubis-lab/RUBIS-SelfDriving

max_issues_repo_name: rubis-lab/RUBIS-SelfDriving

max_forks_repo_name: rubis-lab/RUBIS-SelfDriving


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

import numpy as np
import numba

from africanus.util.numba import generated_jit, njit

@njit(nogil=True, cache=True)
def _unique_internal(data):
    if len(data.shape) != 1:
        raise ValueError("_unique_internal currently "
                         "only supports 1D arrays")

    # Handle the empty array case
    if data.shape[0] == 0:
        return (data,
                np.empty((0,), dtype=np.intp),
                np.empty((0,), dtype=np.intp),
                np.empty((0,), dtype=np.intp))

    # See numpy's unique1d
    perm = np.argsort(data, kind='mergesort')

    # Combine these arrays to save on allocations?
    aux = np.empty_like(data)
    mask = np.empty(aux.shape, dtype=np.bool_)
    inv_idx = np.empty(mask.shape, dtype=np.intp)

    # Hard code first iteration
    p = perm[0]
    aux[0] = data[p]
    mask[0] = True
    cumsum = 1
    inv_idx[p] = cumsum - 1
    counts = [np.intp(0)]

    for i in range(1, aux.shape[0]):
        p = perm[i]
        aux[i] = data[p]
        d = aux[i] != aux[i - 1]
        mask[i] = d
        cumsum += d
        inv_idx[p] = cumsum - 1

        if d:
            counts.append(np.intp(i))

    counts.append(aux.shape[0])

    # (uniques, indices, inverse index, counts)
    return aux[mask], perm[mask], inv_idx, np.diff(np.array(counts))

@generated_jit(nopython=True, nogil=True, cache=True)
def unique_time(time):
    """ Return unique time, inverse index and counts """
    if time.dtype not in (numba.float32, numba.float64):
        raise ValueError("time must be floating point but is %s" % time.dtype)

    def impl(time):
        return _unique_internal(time)

    return impl

@generated_jit(nopython=True, nogil=True, cache=True)
def unique_baselines(ant1, ant2):
    """ Return unique baselines, inverse index and counts """
    if not ant1.dtype == numba.int32 or not ant2.dtype == numba.int32:
        # Need these to be int32 for the bl_32bit.view(np.int64) trick
        raise ValueError("ant1 and ant2 must be np.int32 "
                         "but received %s and %s" %
                         (ant1.dtype, ant2.dtype))

    def impl(ant1, ant2):
        # Trickery, stack the two int32 antenna pairs in an array
        # and cast to int64
        bl_32bit = np.empty((ant1.shape[0], 2), dtype=np.int32)

        # Copy data
        for r in range(ant1.shape[0]):
            bl_32bit[r, 0] = ant1[r]
            bl_32bit[r, 1] = ant2[r]

        # Cast to int64 for the unique operation
        bl = bl_32bit.view(np.int64).reshape(ant1.shape[0])

        ret, idx, inv, counts = _unique_internal(bl)

        # Recast to int32 and reshape
        ubl = ret.view(np.int32).reshape(ret.shape[0], 2)

        return ubl, idx, inv, counts

    return impl

max_stars_repo_name: JoshVStaden/codex-africanus

max_issues_repo_name: JoshVStaden/codex-africanus

max_forks_repo_name: JoshVStaden/codex-africanus

reason: import numpy,import numba


text:
from baselines.common.vec_env.subproc_vec_env import SubprocVecEnv
from baselines.common.vec_env import VecEnvWrapper
import numpy as np
import pickle
import gym
import argparse
import os
from scipy import stats
import visdom

import torch
import utils
import TD3
import OurDDPG
import DDPG

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

def make_env(env_id, seed, rank):
    def _thunk():
        env = gym.make(env_id)

        env.seed(seed + rank)
        return env

    return _thunk

class VecPyTorch(VecEnvWrapper):
    def __init__(self, venv, device):
        """Return only every `skip`-th frame"""
        super(VecPyTorch, self).__init__(venv)
        self.device = device

    def reset(self):
        obs = self.venv.reset()
        obs = torch.from_numpy(obs).float().to(self.device)
        return obs

    def step_async(self, actions):
        if isinstance(actions, torch.LongTensor):
            # Squeeze the dimension for discrete actions
            actions = actions.squeeze(1)
        actions = actions.cpu().numpy()
        self.venv.step_async(actions)

    def step_wait(self):
        obs, reward, done, info = self.venv.step_wait()
        obs = torch.from_numpy(obs).float().to(self.device)
        reward = reward
        done = 1 * done
        return obs, reward, done, info

def make_vec_envs(env_name, num_processes):
    seed = np.random.randint(0, 10000000)
    envs = [
        make_env(env_name, seed, i)
        for i in range(num_processes)
    ]

    envs = SubprocVecEnv(envs)
    envs = VecPyTorch(envs, device)

    return envs

def moving_average(a, n=3):
    plot_data = np.zeros_like(a)
    for idx in range(len(a)):
        length = min(idx, n)
        plot_data[idx] = a[idx - length:idx + 1].mean()
    return plot_data

def vis_plot(viz, log_dict):
    ma_length = 0
    if viz is not None:
        for field in log_dict:
            if len(log_dict[field]) > 0:
                _, values = zip(*log_dict[field])

                plot_data = np.array(log_dict[field])
                viz.line(X=plot_data[:, 0], Y=moving_average(plot_data[:, 1], ma_length), win=field,
                         opts=dict(title=field, legend=[field]))

def swap_policies_if_better(policy, r, std, target_r, target_std, force_swap, criterion='prob', significance=0.05,
                            n=10, cpg=False):
    if target_std is not None and std is not None:
        if target_std > 0 or std > 0:
            DF_1 = (std ** 2 / n + target_std ** 2 / n) ** 2
            DF_2 = ((std ** 2 / n) ** 2) / (n - 1)
            DF_3 = ((target_std ** 2 / n) ** 2) / (n - 1)
            DF = int(DF_1 / (DF_2 + DF_3))
            t = (r - target_r) / np.sqrt((std ** 2 / n) + (target_std ** 2 / n))
            p = stats.t.cdf(t, df=DF)
        else:
            t = 0
            if r > target_r:
                p = 1
            else:
                p = 0
    else:
        t = 0
        if target_r is None or r > target_r:
            p = 1
        else:
            p = 0

    swapped = False
    if criterion == 'prob':
        if significance > 1 - p or force_swap:
            swapped = True
    elif criterion == 'max':
        if target_r is None or r > target_r or force_swap:
            swapped = True
    elif criterion == 'margin':
        if r > target_r * 1.05 or force_swap:
            swapped = True
    elif criterion == 'always':
        swapped = True
    else:
        raise NotImplementedError

    if swapped:
        if cpg:
            policy.actor_target.load_state_dict(policy.actor.state_dict())
        policy.actor_static.load_state_dict(policy.actor.state_dict())
    if target_r is None:
        target_r = 0
        target_std = 0
    print("R: %f, STD: %f, Target R: %f, Target STD: %f, t-statistic: %f, p: %f, swapped: %s" % (
        r, std, target_r, target_std, t, p, str(swapped)))
    return 1 if swapped else 0

# Runs policy for X episodes and returns average reward
def evaluate_policy(policy, memory, eval_episodes=1, force_swap=False, cpg=False, criterion='eval_prob',
                    significance=0.05, prev_r=None, prev_std=None, reevaluate=True, swap=True):
    additional_samples = 0

    if len(memory.storage) == 0:
        train = False
    else:
        train = True

    num_evals = max(eval_envs, eval_episodes)
    if criterion is not None and ('eval' in criterion or 'replay' in criterion):
        use_memory = 'replay' in criterion
        criterion = criterion.split('_')[1]
    else:
        use_memory = False

    with torch.no_grad():
        rewards = np.zeros(num_evals)
        target_rewards = np.zeros(num_evals)
        total_steps = np.zeros(num_evals)

        for iter in range(int(num_evals / eval_envs)):
            not_done_vec = np.ones(eval_envs)
            valid_idx = np.ones(eval_envs)
            obs = eval_env.reset()
            while np.sum(not_done_vec) > 0:
                action = policy.actor(obs) * max_action
                new_obs, reward, done, _ = eval_env.step(action)

                if swap and criterion is not None:
                    # Store data in replay buffer
                    for idx in range(eval_episodes):
                        if valid_idx[idx] == 1:
                            additional_samples += 1
                            done_bool = 0 if total_steps[idx] + 1 == env._max_episode_steps else float(done[idx])
                            memory.add((obs[idx].cpu().data.numpy().flatten(), new_obs[idx].cpu().data.numpy().flatten(), action[idx].cpu().data.numpy().flatten(), reward[idx].flatten()[0], done_bool, 1))

                            if done[idx]:
                                valid_idx[idx] = 0
                obs = new_obs

                rewards[iter * eval_envs:(iter + 1) * eval_envs] += reward * not_done_vec
                total_steps[iter * eval_envs:(iter + 1) * eval_envs] += not_done_vec

                not_done_vec = not_done_vec * (1 - done)
            avg_reward = np.mean(rewards[:eval_episodes])
            std = np.std(rewards[:eval_episodes])

        if not use_memory:
            total_target_steps = np.zeros(num_evals)
            for iter in range(int(num_evals / eval_envs)):
                not_done_vec = np.ones(eval_envs)
                valid_idx = np.ones(eval_envs)
                obs = eval_env.reset()
                while np.sum(not_done_vec) > 0:
                    additional_samples += 1
                    action = policy.actor_static(obs) * max_action

                    new_obs, reward, done, _ = eval_env.step(action)

                    if swap and reevaluate and criterion is not None:
                        # Store data in replay buffer
                        for idx in range(eval_episodes):
                            if valid_idx[idx] == 1:
                                additional_samples += 1
                                done_bool = 0 if total_target_steps[idx] + 1 == env._max_episode_steps else float(done[idx])
                                memory.add((obs[idx].cpu().data.numpy().flatten(), new_obs[idx].cpu().data.numpy().flatten(), action[idx].cpu().data.numpy().flatten(), reward[idx].flatten()[0], done_bool, 1))

                                if done[idx]:
                                    valid_idx[idx] = 0
                    obs = new_obs

                    target_rewards[iter * eval_envs:(iter + 1) * eval_envs] += reward * not_done_vec
                    total_target_steps[iter * eval_envs:(iter + 1) * eval_envs] += not_done_vec

                    not_done_vec = not_done_vec * (1 - done)
            if reevaluate:
                avg_target_reward = np.mean(target_rewards[:eval_episodes])
                target_std = np.std(target_rewards[:eval_episodes])
            else:
                avg_target_reward = prev_r
                target_std = prev_std
        else:
            distribution = stats.norm(0, 0.1)
            ind = np.ones(len(memory.storage))
            ind = np.cumsum(ind) - 1
            x, y, u, r, d, p = [], [], [], [], [], []

            for i in ind:
                X, Y, U, R, D, P = memory.storage[int(i)]
                x.append(np.array(X, copy=False))
                y.append(np.array(Y, copy=False))
                u.append(np.array(U, copy=False))
                r.append(np.array(R, copy=False))
                d.append(np.array(D, copy=False))
                p.append(np.array(P, copy=False))

            x, y, u, r, d, p = np.array(x), np.array(y), np.array(u), np.array(r).reshape(-1, 1), np.array(d).reshape(
                -1, 1), np.array(p).reshape(len(memory.storage), -1)
            state = torch.FloatTensor(x).to(device)
            action = torch.FloatTensor(u).to(device)

            distances = (action - policy.actor(state) * max_action).cpu().numpy()
            target_distances = (action - policy.actor_static(state) * max_action).cpu().numpy()
            probs = distribution.pdf(-np.abs(distances))
            target_probs = distribution.pdf(-np.abs(target_distances))

            is_rewards = []
            is_target_rewards = []
            idx = 0

            is_reward = 0
            is_target_reward = 0
            is_prev_prob = 1
            is_prob = 1
            is_target_prob = 1
            while idx < len(memory.storage):
                is_prev_prob *= np.prod(p[idx])
                is_prob *= np.prod(probs[idx])
                is_target_prob *= np.prod(target_probs[idx])

                if is_prev_prob == 0:
                    is_reward = 0
                    is_target_reward = 0
                    is_prob = 0
                    is_target_prob = 0
                else:
                    is_reward += (is_prob / is_prev_prob) * r[idx]
                    is_target_reward += (is_target_prob / is_prev_prob) * r[idx]

                if d[idx] == 1:
                    print('Probs: ' + str(is_prev_prob) + ' ' + str(is_prob) + ' ' + str(is_target_prob))
                    if is_prev_prob > 0.5:
                        if is_prob > 0.5:
                            is_rewards.append(is_reward)
                        if is_target_prob > 0.5:
                            is_target_rewards.append(is_target_reward)

                    is_reward = 0
                    is_target_reward = 0
                    is_prev_prob = 1
                    is_prob = 1
                    is_target_prob = 1
                idx += 1

            avg_reward = np.mean(is_rewards)
            std = np.std(is_rewards)
            avg_target_reward = np.mean(is_target_rewards)
            target_std = np.std(is_target_rewards)

    print("---------------------------------------")
    print("Evaluation over %d episodes: %f" % (eval_episodes, avg_reward))
    if criterion is not None:
        swapped = swap_policies_if_better(policy, avg_reward, std, avg_target_reward, target_std, force_swap, criterion,
                                          significance, eval_episodes, cpg)
        if swapped or prev_r is None:
            prev_r = avg_reward
            prev_std = std
        else:
            prev_r = avg_target_reward
            prev_std = target_std
    else:
        swapped = False
    print("---------------------------------------")
    if train:
        policy.train(memory, additional_samples,
                     args.batch_size,
                     args.discount, args.tau, args.policy_noise,
                     args.noise_clip,
                     args.policy_freq,
                     update_target_actor=criterion is None or not cpg,
                     update_target_q=not args.swap_q)
    return np.mean(rewards[eval_episodes:]), np.mean(target_rewards[eval_episodes:]), swapped, additional_samples, prev_r, prev_std

if __name__ == "__main__":

    parser = argparse.ArgumentParser()
    parser.add_argument("--policy_name", default="TD3-swap", choices=['TD3-swap', 'TD3'])  # Policy name
    parser.add_argument("--env_name", default="HalfCheetah-v2")  # OpenAI gym environment name
    parser.add_argument("--start_timesteps", default=1e4,
                        type=int)  # How many time steps purely random policy is run for
    parser.add_argument("--swap_start", default=0, type=int)
    parser.add_argument('--swap_q', default=False, action='store_true')
    parser.add_argument("--swap_freq", default=1e3, type=float)  # How often (time steps) we evaluate and swap
    parser.add_argument('--swap_criterion', default='replay_prob',
                        choices=['replay_max', 'replay_prob', 'eval_max', 'eval_prob', 'always'])
    parser.add_argument('--no_reevaluation', default=False, action='store_true')
    parser.add_argument("--eval_freq", default=1e3, type=float)  # How often (time steps) we evaluate
    parser.add_argument("--max_timesteps", default=1e6, type=float)  # Max time steps to run environment for
    parser.add_argument("--expl_noise", default=0.1, type=float)  # Std of Gaussian exploration noise
    parser.add_argument("--batch_size", default=100, type=int)  # Batch size for both actor and critic
    parser.add_argument("--discount", default=0.99, type=float)  # Discount factor
    parser.add_argument("--tau", default=0.005, type=float)  # Target network update rate
    parser.add_argument("--policy_noise", default=0.2, type=float)  # Noise added to target policy during critic update
    parser.add_argument("--noise_clip", default=0.5, type=float)  # Range to clip target policy noise
    parser.add_argument("--policy_freq", default=2, type=int)  # Frequency of delayed policy updates
    parser.add_argument('--visualize', default=False, action='store_true')
    parser.add_argument('--no_target_q', default=False, action='store_true')
    parser.add_argument('--cpg', default=False, action='store_true')
    parser.add_argument('--experiment_name', default=None, type=str,
                        help='For multiple different experiments, provide an informative experiment name')
    parser.add_argument("--eval_episodes", default=10, type=int)  # Number of episodes for policy evaluation
    parser.add_argument("--target_distance_weight", default=0, type=float)  # Frequency of delayed policy updates
    parser.add_argument("--significance", default=0.1, type=float)  # Significance for the probability based approach
    args = parser.parse_args()
    args.target_q = not args.no_target_q
    args.reevaluate = not args.no_reevaluation

    base_dir = os.getcwd() + '/models/' + args.policy_name
    if args.swap_q:
        base_dir = base_dir + '_swap_q'
    if args.experiment_name is not None:
        base_dir += '/' + args.experiment_name
    base_dir = base_dir + '/' + args.swap_criterion + '/' + args.env_name + '/'

    run_number = 0
    while os.path.exists(base_dir + str(run_number)):
        run_number += 1
    base_dir = base_dir + str(run_number)
    os.makedirs(base_dir)

    results_dict = {'eval_rewards': [],
                    'target_eval_rewards': [],
                    'value_losses': [],
                    'policy_losses': [],
                    'train_rewards': [],
                    'swapped': [],
                    'actor_loss': [],
                    'critic_loss': [],
                    'additional_samples': []
                    }

    if args.visualize:
        vis = visdom.Visdom(env=base_dir)
    else:
        vis = None

    env = gym.make(args.env_name)

    eval_envs = 100 + args.eval_episodes
    eval_env = make_vec_envs(args.env_name, eval_envs)

    state_dim = env.observation_space.shape[0]
    action_dim = env.action_space.shape[0]
    max_action = float(env.action_space.high[0])

    if args.policy_name == 'TD3':
        args.swap_criterion = None

    # Initialize policy
    if args.policy_name == "TD3" or args.policy_name == 'TD3-swap':
        policy = TD3.TD3(state_dim, action_dim, 1, args.target_q, args.target_distance_weight)
    elif args.policy_name == "OurDDPG":
        policy = OurDDPG.DDPG(state_dim, action_dim, 1)
    elif args.policy_name == "DDPG":
        policy = DDPG.DDPG(state_dim, action_dim, 1)
    else:
        raise NotImplementedError

    replay_buffer = utils.ReplayBuffer()

    total_timesteps = 0
    total_timesteps_with_eval = 0
    timesteps_since_eval = 0
    timesteps_since_swapped = 0
    episode_num = 0
    done = True
    episode_reward = 0
    episode_timesteps = 0
    prev_r = None
    prev_std = None

    # Evaluate untrained policy
    eval_r, target_eval_r, swapped, additional_samples, prev_r, prev_std = evaluate_policy(policy,
                                                                                memory=replay_buffer,
                                                                                force_swap=False,
                                                                                criterion=None,
                                                                                cpg=args.cpg,
                                                                                eval_episodes=args.eval_episodes,
                                                                                significance=args.significance,
                                                                                prev_r=prev_r,
                                                                                prev_std=prev_std,
                                                                                reevaluate=args.reevaluate,
                                                                                swap='swap' in args.policy_name)
    results_dict['eval_rewards'].append((total_timesteps, eval_r))
    results_dict['target_eval_rewards'].append((total_timesteps, target_eval_r))
    results_dict['swapped'].append((total_timesteps, swapped))
    results_dict['additional_samples'].append((total_timesteps, 0))

    play_with_target = False
    distribution = stats.norm(0, 0.1)
    last_swapped = 0
    max_swap_diff = 20000

    while total_timesteps_with_eval < args.max_timesteps:
        if total_timesteps < args.swap_start or not args.cpg:
            swap_criterion = None
        else:
            swap_criterion = args.swap_criterion

        if done:
            play_with_target = not play_with_target if args.policy_name == 'TD3-swap' else False

            if total_timesteps != 0:
                print("%s - Run %d - Total T: %d Episode Num: %d Episode T: %d Reward: %f" % (
                    args.env_name, run_number, total_timesteps, episode_num, episode_timesteps, episode_reward))
                results_dict['train_rewards'].append((total_timesteps, episode_reward))
                if args.policy_name == "TD3" or args.policy_name == 'TD3-swap':
                    critic_loss, actor_loss = policy.train(replay_buffer, episode_timesteps, args.batch_size,
                                                           args.discount, args.tau, args.policy_noise, args.noise_clip,
                                                           args.policy_freq,
                                                           update_target_actor=swap_criterion is None or not args.cpg,
                                                           update_target_q=not args.swap_q)
                else:
                    policy.train(replay_buffer, episode_timesteps, args.batch_size, args.discount, args.tau)
                    critic_loss = actor_loss = 0

                results_dict['actor_loss'].append((total_timesteps, actor_loss))
                results_dict['critic_loss'].append((total_timesteps, critic_loss))

            # Evaluate episode
            if timesteps_since_eval >= args.eval_freq:
                timesteps_since_eval %= args.eval_freq
                if timesteps_since_swapped >= args.swap_freq and total_timesteps > args.swap_start:
                    criterion = args.swap_criterion
                    timesteps_since_swapped %= args.swap_freq
                else:
                    criterion = None
                eval_r, target_eval_r, swapped, additional_samples, prev_r, prev_std = evaluate_policy(policy,
                                                                                memory=replay_buffer,
                                                                                force_swap=False,
                                                                                cpg=args.cpg,
                                                                                criterion=criterion,
                                                                                eval_episodes=args.eval_episodes,
                                                                                significance=args.significance,
                                                                                prev_r=prev_r,
                                                                                prev_std=prev_std,
                                                                                reevaluate=args.reevaluate,
                                                                                swap='swap' in args.policy_name)
                total_timesteps_with_eval += additional_samples
                if args.swap_q:
                    policy.critic_target.load_state_dict(policy.critic.state_dict())
                results_dict['eval_rewards'].append((total_timesteps, eval_r))
                results_dict['target_eval_rewards'].append((total_timesteps, target_eval_r))
                results_dict['swapped'].append((total_timesteps, swapped))
                results_dict['additional_samples'].append((total_timesteps, additional_samples))

                if swapped or swap_criterion is None:
                    last_swapped = total_timesteps
                elif last_swapped + max_swap_diff <= total_timesteps and args.cpg:
                    policy.actor.load_state_dict(policy.actor_static.state_dict())
                    params = policy.actor.state_dict()
                    for name in params:
                        if 'ln' in name:
                            pass
                        param = params[name]
                        param += torch.randn(param.shape).to(device) * 0.01

                    last_swapped = total_timesteps

                with open(base_dir + '/results', 'wb') as f:
                    pickle.dump(results_dict, f)

            # Reset environment
            obs = env.reset()
            done = False
            episode_reward = 0
            episode_timesteps = 0
            episode_num += 1

            vis_plot(vis, results_dict)

        # Select action randomly or according to policy
        if total_timesteps < args.start_timesteps:
            action = env.action_space.sample()
            prob = [0 for _ in range(action_dim)]
        else:
            if play_with_target:
                action = policy.select_action_target(np.array(obs))
            else:
                action = policy.select_action(np.array(obs))
            if args.expl_noise != 0:
                # Gaussian noise
                noise = np.random.normal(0, args.expl_noise, size=env.action_space.shape[0])
                action = (action + noise).clip(-1, 1) * max_action
                prob = distribution.pdf(-np.abs(noise))
            else:
                prob = 0

        # Perform action
        new_obs, reward, done, _ = env.step(action)
        done_bool = 0 if episode_timesteps + 1 == env._max_episode_steps else float(done)
        episode_reward += reward

        # Store data in replay buffer
        replay_buffer.add((obs, new_obs, action, reward, done_bool, prob))

        obs = new_obs

        episode_timesteps += 1
        total_timesteps += 1
        total_timesteps_with_eval += 1
        timesteps_since_eval += 1
        timesteps_since_swapped += 1

    # Final evaluation
    if timesteps_since_swapped >= args.swap_freq and total_timesteps > args.swap_start:
        criterion = args.swap_criterion
    else:
        criterion = None
    eval_r, target_eval_r, swapped, additional_samples, prev_r, prev_std = evaluate_policy(policy,
                                                                                memory=replay_buffer,
                                                                                force_swap=False,
                                                                                cpg=args.cpg,
                                                                                criterion=criterion,
                                                                                eval_episodes=args.eval_episodes,
                                                                                significance=args.significance,
                                                                                prev_r=prev_r,
                                                                                prev_std=prev_std,
                                                                                reevaluate=args.reevaluate,
                                                                                swap='swap' in args.policy_name)
    total_timesteps_with_eval += additional_samples
    results_dict['eval_rewards'].append((total_timesteps, eval_r))
    results_dict['target_eval_rewards'].append((total_timesteps, target_eval_r))
    results_dict['additional_samples'].append((total_timesteps, additional_samples))

    policy.save('model', directory=base_dir)
    with open(base_dir + '/results', 'wb') as f:
        pickle.dump(results_dict, f)
    env.close()
    eval_env.close()

max_stars_repo_name: tesslerc/ConservativePolicyGradient

max_issues_repo_name: tesslerc/ConservativePolicyGradient

max_forks_repo_name: tesslerc/ConservativePolicyGradient

reason: import numpy,from scipy


text:
import numpy as np
import random
import math
from keras.models import Sequential, load_model
from keras.layers import Conv1D, MaxPooling1D, GlobalAveragePooling1D, Embedding, LSTM, Dropout, Dense, Flatten
from keras.constraints import max_norm

epochs = 100
batch_size = 128

model_types = ['dense_1', 'dense_2', 'dense_3', 'dense_4', 'dense_5', 'conv1d_1', 'conv1d_2', 'conv1d_3', 'lstm_1']

active_model = 'conv1d_3'

source_csv = 'hki_liikennemaarat.csv'
source_csv_delimiter = ';'
source_csv_start_year = 2011
source_csv_end_year = 2017

stops = ['A01', 'A02', 'A03', 'A04', 'A05', 'A06', 'A07', 'A08', 'A09', 'B01', 'B02', 'B03', 'B04', 'B05', 'B06', 'B07', 'B08', 'B09', 'B10', 'B11', 'B12', 'C01', 'C02', 'C03', 'C04', 'C05', 'C06', 'C07', 'C08', 'C09', 'C10', 'C11', 'C12', 'C13', 'C14', 'C15', 'C16', 'C17', 'C18', 'D01', 'D02', 'D03', 'D04', 'D05', 'D06', 'D07', 'D08', 'D09', 'D10', 'D11', 'D12', 'D13', 'F01', 'F02', 'F03', 'F04', 'F05', 'F06']

# fix random seed for reproducibility
np.random.seed(7)

source_data = np.genfromtxt(source_csv, delimiter=source_csv_delimiter, skip_header=1, usecols=[0,4,5,6,7,8,9,10,11,12,13], encoding='iso-8859-1', unpack=True, dtype=['U3', 'U32', 'uint32', 'uint32', 'uint8', 'uint16', 'uint16', 'uint16', 'uint16', 'uint16', 'uint16', 'uint16', 'uint16', 'uint16', 'uint16'])

print(source_data[25000])

traffic_data = []

for i in range(len(source_data)):
  hours, minutes = divmod(source_data[i][2], 100)
  traffic_data.append([
    stops.index(source_data[i][0]) / len(stops),
    source_data[i][1] - 1,
    (hours + (minutes / 60)) / 24,
    (source_data[i][3] - source_csv_start_year) / (source_csv_end_year - source_csv_start_year),
    source_data[i][4] / 5000,
    source_data[i][5] / 1000,
    source_data[i][6] / 100,
    source_data[i][7] / 250,
    source_data[i][8] / 250,
    source_data[i][9] / 250,
    source_data[i][10] / 50
  ])

print(traffic_data[25000])

# Get training and test data

trainX = []
trainY = []
testX = []
testY = []

for i in range(len(traffic_data)):
  # latest year data is used for testing
  if(traffic_data[i][3] == 1):
    testX.append(traffic_data[i][0:4])
    testY.append(traffic_data[i][4:])
  else:
    trainX.append(traffic_data[i][0:4])
    trainY.append(traffic_data[i][4:])

trainX = np.array(trainX)
trainY = np.array(trainY)
testX = np.array(testX)
testY = np.array(testY)

# make it divisable by batch size
remainder = len(trainX) % batch_size
if remainder > 0:
  trainX = trainX[:-remainder]
  trainY = trainY[:-remainder]

# make it divisable by batch size
remainder = len(testX) % batch_size
if remainder > 0:
  testX = testX[:-remainder]
  testY = testY[:-remainder]

print(trainX.shape)
print(trainY.shape)
print(testX.shape)
print(testY.shape)

print("Active model is: " + active_model)

# create and fit model
model = Sequential()

if(active_model == 'dense_1'):

  model.add(Dense(4, input_shape=(4, ), activation='relu'))
  model.add(Dropout(0.5))
  model.add(Dense(12, activation='relu'))
  model.add(Dropout(0.5))
  model.add(Dense(7, activation='sigmoid'))

elif(active_model == 'dense_2'):

  model.add(Dense(4, input_shape=(4, ), activation='relu'))
  model.add(Dropout(0.5))
  model.add(Dense(20, activation='relu'))
  model.add(Dropout(0.5))
  model.add(Dense(80, activation='relu'))
  model.add(Dropout(0.5))
  model.add(Dense(7, activation='tanh'))

elif(active_model == 'dense_3'):

  model.add(Dense(4, input_shape=(4, ), activation='relu'))
  model.add(Dropout(0.5))
  model.add(Dense(16, activation='relu'))
  model.add(Dropout(0.5))
  model.add(Dense(7, activation='sigmoid'))

elif(active_model == 'dense_4'):

  model.add(Dense(20, input_shape=(4, ), activation='relu'))
  model.add(Dropout(0.5))
  model.add(Dense(60, activation='relu'))
  model.add(Dropout(0.5))
  model.add(Dense(7, activation='sigmoid'))

elif(active_model == 'dense_5'):

  model.add(Dense(40, input_shape=(4, ), activation='relu'))
  model.add(Dropout(0.5))
  model.add(Dense(40, activation='relu'))
  model.add(Dropout(0.5))
  model.add(Dense(7, activation='sigmoid'))

elif(active_model == 'conv1d_1'):

  model.add(Embedding(2000, 50, input_length=4))
  model.add(Dropout(0.3))
  model.add(Conv1D(200, kernel_size=4, padding='valid', activation='relu', strides=1))
  model.add(GlobalMaxPooling1D())
  model.add(Dense(28, activation='relu'))
  model.add(Dropout(0.3))
  model.add(Dense(7, activation='sigmoid'))

elif(active_model == 'conv1d_2'):

  trainX = np.expand_dims(trainX, axis=2)
  testX = np.expand_dims(testX, axis=2)

  model.add(Conv1D(input_shape=(4, 1), filters=200, kernel_size=4, activation='relu'))
  model.add(MaxPooling1D(1))
  model.add(Dropout(0.50))
  model.add(Flatten())
  model.add(Dense(7, activation='sigmoid'))

elif(active_model == 'conv1d_3'):

  trainX = np.expand_dims(trainX, axis=2)
  testX = np.expand_dims(testX, axis=2)

  model.add(Conv1D(input_shape=(4, 1), filters=100, kernel_size=4, activation='relu'))
  model.add(MaxPooling1D(1))
  model.add(Dropout(0.50))
  model.add(Conv1D(filters=400, kernel_size=1, activation='relu'))
  model.add(Dropout(0.50))
  model.add(MaxPooling1D(1))
  model.add(Flatten())
  model.add(Dense(7, activation='sigmoid'))

elif(active_model == 'lstm_1'):

  model.add(Embedding(1000, 64, input_length=4))
  model.add(LSTM(20, return_sequences=True, kernel_constraint=max_norm(3), activation='relu'))
  model.add(Dropout(0.5))
  model.add(LSTM(60, return_sequences=True, kernel_constraint=max_norm(3), activation='relu'))
  model.add(Dropout(0.5))
  model.add(Flatten())
  model.add(Dense(20, activation='relu'))
  model.add(Dropout(0.5))
  model.add(Dense(7, activation='sigmoid'))

elif(active_model == 'lstm_2'):

  model.add(Embedding(100, 4, input_length=4))
  model.add(LSTM(10, return_sequences=True, kernel_constraint=max_norm(3), activation='relu'))
  model.add(Dropout(0.5))
  model.add(LSTM(20, return_sequences=True, kernel_constraint=max_norm(3), activation='relu'))
  model.add(Dropout(0.5))
  model.add(Flatten())
  model.add(Dense(7, activation='sigmoid'))

model.compile(loss='mae', optimizer='adam')

while True:
  model.fit(trainX, trainY, epochs=epochs, batch_size=batch_size, validation_data=(testX, testY), verbose=2)
  model.save(active_model+'.h5')

max_stars_repo_name: Krisseck/hki-traffic-predict

max_issues_repo_name: Krisseck/hki-traffic-predict

max_forks_repo_name: Krisseck/hki-traffic-predict


text:
"""
A library of custom weighted profiles
to fit to the NIRISS orders to complete
the optimal extraction of the data.
"""

import numpy as np
from scipy.special import gamma
from astropy.modeling.models import Gaussian1D, Moffat1D

__all__ = ['moffat_2poly_piecewise', 'moffat_1poly_piecewise',
           'gaussian_1poly_piecewise', 'gaussian_2poly_piecewise',
           'generalized_normal']

def generalized_normal(x, mu,alpha,beta,scale):
    """
    Generalized normal distribution.

    Parameters
    ----------
    x : np.ndarray
       X values to evaluate the distribution.
       over.
    mu : float
       Mean/center value of the distribution.
    alpha : float
       Sets the scale/standard deviation of the
       distribution.
    beta : float
       Sets the shape of the distribution.
       Beta > 2 becomes boxy. Beta < 2 becomes
       peaky. Beta = 2 is a normal Gaussian.
    scale : float
       A value to scale the distribution by.
    """
    gfunc = gamma(1.0/beta)
    frac  = beta / (2 * alpha * gfunc)
    exp   = (np.abs(x-mu)/alpha)**beta
    return frac * np.exp(-exp)*scale

def moffat_2poly_piecewise(args, x):
    """
    A piece-wise function consisting of 2 Moffat profiles
    connected with a 2D polynomial to mimic the bat-shaped
    profile of NIRISS.

    Parameters
    ----------
    args : np.ndarray
       A list or array of parameters for the fits.
    x : np.ndarray
       X values to evaluate the shape over.
    """
    x0,x1,mu1,alpha1,gamma1,f1,a,b,c,mu2,alpha2,gamma2,f2 = args
    model = np.piecewise(x, [((x<x0)),
                             ((x>=x0) & (x<x1))],
                         [lambda x: Moffat1D(x, x_0=mu1, alpha=alpha1,
                                             gamma=gamma1, amplitude=f1),
                          lambda x: a*x**2+b*x+c,
                          lambda x: Moffat1D(x, x_0=mu2, alpha=alpha2,
                                             gamma=gamma2,
                                             amplitude=f2)]
                        )
    return model

def moffat_1poly_piecewise(args, x):
    """
    A piece-wise function consisting of 2 Moffat profiles
    connected with a 1D polynomial to mimic the bat-shaped
    profile of NIRISS.

    Parameters
    ----------
    args : np.ndarray
       A list or array of parameters for the fits.
    x : np.ndarray
       X values to evaluate the shape over.
    """
    x0,x1,mu1,alpha1,gamma1,f1,m,b,mu2,alpha2,gamma2,f2 = args
    model = np.piecewise(x, [((x<x0)),
                             ((x>=x0) & (x<x1))],
                         [lambda x: Moffat1D(x, x_0=mu1, alpha=alpha1,
                                             gamma=gamma1, amplitude=f1),
                          lambda x: m*x+b,
                          lambda x: Moffat1D(x, x_0=mu2, alpha=alpha2,
                                             gamma=gamma2,
                                             amplitude=f2)]
                        )
    return model

def gaussian_1poly_piecewise(args, x):
    """
    A piece-wise function consisting of 2 generalized
    normal distribution profiles
    connected with a 1D polynomial to mimic the bat-shaped
    profile of NIRISS.

    Parameters
    ----------
    args : np.ndarray
       A list or array of parameters for the fits.
    x : np.ndarray
       X values to evaluate the shape over.
    """
    x0,x1,mu1,std1,beta1,scale1,m,b,mu2,std2,beta2,scale2 = args
    model = np.piecewise(x, [((x<x0)),
                             ((x>=x0) & (x<x1))],
                         [lambda x: generalized_normal(x, mu1,std1,beta1,scale1),
                          lambda x: m*x+b,
                          lambda x: generalized_normal(x, mu2,std2,beta2,scale2)]
                        )
    return model

def gaussian_2poly_piecewise(args, x):
    """
    A piece-wise function consisting of 2 generalized
    normal distribution profiles
    connected with a 2D polynomial to mimic the bat-shaped
    profile of NIRISS.

    Parameters
    ----------
    args : np.ndarray
       A list or array of parameters for the fits.
    x : np.ndarray
       X values to evaluate the shape over.
    """
    x0,x1,mu1,std1,beta1,scale1,a,b,c,mu2,std2,beta2,scale2 = args
    model = np.piecewise(x, [((x<x0)),
                             ((x>=x0) & (x<x1))],
                         [lambda x: generalized_normal(x, mu1,std1,beta1,scale1),
                          lambda x: a*x**2+b*x+c,
                          lambda x: generalized_normal(x, mu2,std2,beta2,scale2)]
                        )
    return model

max_stars_repo_name: iancrossfield/Eureka

max_issues_repo_name: iancrossfield/Eureka

max_forks_repo_name: iancrossfield/Eureka

reason: import numpy,from scipy,from astropy