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

# **Exercise: Animate the timeseries plot**

# In[1]:

# Imports
from threading import Thread
import datetime
import logging
import time

import numpy as np
import netCDF4
import pandas as pd

from bokeh.plotting import vplot, hplot, cursession, curdoc, output_server, show
from bokeh.models.widgets import Button, Icon

from viz import climate_map, timeseries, legend, get_slice

# In[2]:

# Data
data = netCDF4.Dataset('data/Land_and_Ocean_LatLong1.nc')
t = data.variables['temperature']
df = pd.read_csv('data/Land_Ocean_Monthly_Anomaly_Average.csv', parse_dates=[0])

# In[3]:

# Output option
output_server("climate")

# In[4]:

from bokeh.plotting import figure

# Data
year = 1850
month = 1

years = [str(x) for x in np.arange(1850, 2015, 1)]

months = [str(x) for x in np.arange(1, 13, 1)]
months_str = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']

month_str = months_str[month-1]

date = datetime.date(year, month, 1)
df['moving_average'] = pd.rolling_mean(df['anomaly'], 12)
df = df.fillna(0)

# New text Plot
title = figure(width=1200, height=100, x_range=(0, 1200), y_range=(0, 100), toolbar_location=None,
        x_axis_type=None, y_axis_type=None, outline_line_color="#FFFFFF", tools="", min_border=0)

title.text(x=500, y=5, text=[month_str], text_font_size='36pt', text_color='black', name="month", text_font="Georgia")
title.text(x=350, y=5, text=[str(year)], text_font_size='36pt', text_color='black', name="year",text_font="Georgia")

# In[5]:

# Plots
climate_map = climate_map()
timeseries = timeseries()
legend = legend()

# ADD WIDGETS
play = True

def play_handler():
    print("button_handler: start click")
    global play
    play = True

def stop_handler():
    print("button_handler: stop click")
    global play
    play = False

button_start = Button(label="Start", type="success")
button_start.on_click(play_handler)

button_stop = Button(label="Stop", type="danger")
button_stop.on_click(stop_handler)

controls = hplot(button_start, button_stop)

# In[6]:

# New circle in timeseries plot
timeseries.circle(x=[date], y=[df[df.datetime == date].moving_average], size=8, name="circle")

# In[7]:

# Create layout
map_legend = hplot(climate_map, legend)
layout = vplot(controls, title, map_legend, timeseries)

# In[8]:

# Show
show(layout)

# In[9]:

# Select data source for climate_map and month and year
renderer = climate_map.select(dict(name="image"))
ds = renderer[0].data_source

month_renderer = title.select(dict(name="month"))
month_ds = month_renderer[0].data_source

year_renderer = title.select(dict(name="year"))
year_ds = year_renderer[0].data_source

# Select data source for timeseries data
timeseries_renderer = timeseries.select(dict(name="circle"))
timeseries_ds = timeseries_renderer[0].data_source

def should_play():
    """
    Return true if we should play animation, otherwise block
    """
    global play
    while True:
        if play:
            return True
        else:
            time.sleep(0.05)

def background_thread(ds, year_ds, month_ds, timeseries_ds):
    """Plot animation, update data if play is True, otherwise stop"""
    try:
        while True:
            for year_index in np.arange(2000, 2015, 1):
                    year_ds.data["text"] = [str(year_index)]
                    for month_index in np.arange(1, 13, 1):
                        if should_play():
                            month_ds.data["text"] = [months_str[month_index-1]]
                            image = get_slice(t, year_index, month_index)
                            date = datetime.date(year_index, month_index, 1)
                            timeseries_ds.data["x"] = [date]
                            timeseries_ds.data["y"] = [df[df.datetime == date].moving_average]
                            ds.data["image"] = [image]
                            cursession().store_objects(ds, year_ds, month_ds, timeseries_ds)
                            time.sleep(0.5)
                        time.sleep(0.5)
    except:
        logger.exception("An error occurred")
        raise

# spin up a background thread
Thread(target=background_thread, args=(ds, year_ds, month_ds, timeseries_ds)).start()

# endlessly poll
cursession().poll_document(curdoc(), 0.04)


text:
# vim: fdm=marker
'''
author:     Fabio Zanini
date:       23/10/13
content:    Description module for HIV samples from patients.

            Note: it is possible to read the Excel XLSX table with xlrd. It also
            works ok, but it's painful and error-prone code, and it assumes that
            table is up-to-date. Better write down the machine-relevant info
            manually down here (until otherwise specified).
'''
# Modules
import numpy as np
import pandas as pd

from hivwholeseq.sequencing.filenames import table_filename

# Globals
_sequencing_runs = None

# Classes
class SampleSeq(pd.Series):
    '''A sequenced sample (if something has been sequenced twice, they are separate)'''

    _sequencing_run = None
    _sample_pat = None

    def __init__(self, *args, **kwargs):
        '''Initialize a sequenced sample'''
        super(SampleSeq, self).__init__(*args, **kwargs)

        from hivwholeseq.sequencing.filenames import get_seqrun_foldername
        from hivwholeseq.sequencing.adapter_info import foldername_adapter
        seq_run = self.loc['seq run']
        adaID = self.loc['adapter']
        self['folder'] = str(get_seqrun_foldername(seq_run)+foldername_adapter(adaID))
        self['seqrun_folder'] = str(get_seqrun_foldername(seq_run))

    @property
    def _constructor(self):
        return SampleSeq

    @property
    def sequencing_run(self):
        '''The sequencing run object of this sample'''
        if self._sequencing_run is None:
            self._sequencing_run = load_sequencing_run(self.loc['seq run'])
        return self._sequencing_run

    @property
    def sample_pat(self):
        '''Patient sample of this sequencing sample'''
        if self._sample_pat is None:
            from hivwholeseq.patients.patients import load_samples_sequenced as lssp
            from hivwholeseq.patients.patients import SamplePat
            self._sample_pat = SamplePat(lssp(include_wrong=True).loc[self['patient sample']])
        return self._sample_pat

    @property
    def patientname(self):
        '''Name of the patient this sample belongs to'''
        return self.sample_pat.patient

    @property
    def regions_complete(self):
        '''Get the complete regions, e.g. F5ao'''
        if self.PCR == 1:
            PCR_suffix = 'o'
        elif self.PCR == 2:
            PCR_suffix = 'i'
        else:
            PCR_suffix = ''

        regions = self.regions.split(' ')
        if len(regions):
            return ['F'+fr+PCR_suffix for fr in regions]
        else:
            return []

    @property
    def regions_generic(self):
        '''Get the complete regions, e.g. F5ao'''
        regions = self.regions.split(' ')
        if len(regions):
            return ['F'+fr[0] for fr in regions]
        else:
            return []

    def convert_region(self, fragment):
        '''Get the complete region of a generic or vice versa'''
        if fragment in self.regions_generic:
            return self.regions_complete[self.regions_generic.index(fragment)]
        elif fragment in self.regions_complete:
            return self.regions_generic[self.regions_complete.index(fragment)]
        else:
            raise IndexError('Region not in complete nor in generic regions!')

    def get_read_filenames(self, **kwargs):
        '''Get the filenames of the demultiplexed reads'''
        from hivwholeseq.sequencing.filenames import get_read_filenames as gfn
        return gfn(self.folder, **kwargs)

    def get_reference_premap_filename(self, **kwargs):
        '''Get the filename of the premapping refernce'''
        from hivwholeseq.sequencing.filenames import get_reference_premap_filename as gfn
        return gfn(self.seqrun_folder, self.adapter, **kwargs)

    def get_premapped_filename(self, **kwargs):
        '''Get the filename of the readed premapped to reference'''
        from hivwholeseq.sequencing.filenames import get_premapped_filename as gfn
        return gfn(self.folder, **kwargs)

    def get_divided_filename(self, fragment, **kwargs):
        '''Get the filename of the divided and trimmed reads'''
        from hivwholeseq.sequencing.filenames import get_divided_filename as gfn
        return gfn(self.folder, adaID=None, fragment=fragment, **kwargs)

    def get_consensus_filename(self, fragment, **kwargs):
        '''Get the filename of the consensus'''
        from hivwholeseq.sequencing.filenames import get_consensus_filename as gfn
        from hivwholeseq.sequencing.filenames import get_merged_consensus_filename as gfn2
        if fragment != 'genomewide':
            return gfn(self.folder, adaID=None, fragment=fragment, **kwargs)
        else:
            return gfn2(self.folder, adaID=None, **kwargs)

    def get_mapped_filename(self, fragment, **kwargs):
        '''Get the filename of the mapped reads'''
        from hivwholeseq.sequencing.filenames import get_mapped_filename as gfn
        return gfn(self.folder, adaID=None, fragment=fragment, **kwargs)

    def get_mapped_to_initial_filename(self, fragment, **kwargs):
        '''Get the filename of the reads mapped to patient initial reference'''
        from hivwholeseq.patients.filenames import get_mapped_to_initial_filename as gfn
        pname = self.patientname
        samplename_pat = self['patient sample']
        PCR = int(self.PCR)
        return gfn(pname, samplename_pat, self.name, fragment, PCR=PCR, **kwargs)

    def get_mapped_filtered_filename(self, fragment, **kwargs):
        '''Get the filename of the reads mapped to patient initial reference'''
        from hivwholeseq.patients.filenames import get_mapped_filtered_filename as gfn
        pname = self.patientname
        samplename_pat = self['patient sample']
        PCR = int(self.PCR)
        return gfn(pname, samplename_pat, fragment, PCR=PCR, **kwargs)

    def get_consensus(self, fragment):
        '''Get consensus sequence for mapping'''
        from Bio import SeqIO
        return SeqIO.read(self.get_consensus_filename(fragment), 'fasta')

    def get_allele_counts_filename(self, fragment):
        '''Get the filename with the allele counts'''
        from .filenames import get_allele_counts_filename
        return get_allele_counts_filename(self.seqrun_folder, self.adapter, fragment)

    def get_allele_counts(self, fragment, merge_read_types=False):
        '''Get the allele counts'''
        import numpy as np
        counts = np.load(self.get_allele_counts_filename(fragment))
        if merge_read_types:
            counts = counts.sum(axis=0)
        return counts

    def get_insert_size_distribution(self, fragment, VERBOSE=0, **kwargs):
        '''Get insert size distribution for a fragment'''
        from .check_insert_distribution import get_insert_size_distribution as gisd
        return gisd(self.seqrun_folder, self.adapter, fragment, VERBOSE=VERBOSE,
                    **kwargs)

class SamplesSeq(pd.DataFrame):
    '''Table of sequenced samples'''

    @property
    def _constructor(self):
        return SamplesSeq

    def filter_seq_run(self, run):
        '''Get only the samples from one sequencing run'''
        return self.loc[self.run == run]

    def filter_patient(self, patient_id, exclude_reps=True):
        '''Get only the samples from a specific patient'''
        samples = self.loc[self.patient == patient_id]
        if exclude_reps:
            ind = [(len(sn) < 2) or (sn[-2:] not in ('-2', '-3')) for sn in samples.name.tolist()]
            samples = samples[ind]
        return samples

class SequencingRun(pd.Series):
    '''Sequencing run'''

    def __init__(self, *args, **kwargs):
        '''Initialize a sequencing run'''
        super(SequencingRun, self).__init__(*args, **kwargs)

        from hivwholeseq.sequencing.filenames import get_seqrun_foldername
        self['folder'] = str(get_seqrun_foldername(self.name))

        self['samples'] = load_samples_sequenced(seq_runs=[self.name])

    @property
    def _constructor(self):
        return SequencingRun

    def itersamples(self):
        '''Generator for samples in this run, each with extended attributes'''
        for samplename, sample in self.samples.iterrows():
            yield SampleSeq(sample)

    @property
    def adapters(self):
        '''The adapters used in the sequencing run'''
        return self.samples.adapter

    def discard_nondivided_samples(self):
        '''Discard samples that have no divided reads (e.g. SA, random hexamers)'''
        import os
        from hivwholeseq.sequencing.filenames import get_divided_filename
        ind = []
        for sample in self.itersamples():
            frag = sample.regions_complete[0]
            div = os.path.isfile(get_divided_filename(self.folder, sample.adapter, frag))
            ind.append(div)
        self.samples = self.samples.loc[ind]

# Functions
def load_samples_sequenced(seq_runs=None):
    '''Load samples sequenced from general table'''
    sample_table = pd.read_excel(table_filename, 'Samples sequenced',
                                 index_col=0)
    sample_table.index = pd.Index(map(str, sample_table.index))
    sample_table.loc[:, 'patient sample'] = map(str, sample_table.loc[:, 'patient sample'])
    sample_table.loc[:, 'regions'] = map(str, sample_table.loc[:, 'regions'])
    sample_table.loc[sample_table.loc[:, 'regions'] == 'nan', 'regions'] = ''

    if seq_runs is not None:
        sample_table = sample_table.loc[sample_table.loc[:, 'seq run'].isin(seq_runs)]

    return SamplesSeq(sample_table)

def load_sample_sequenced(samplename):
    '''Load a sequenced sample from the general table'''
    return SampleSeq(load_samples_sequenced().loc[samplename])

def load_sequencing_runs(seq_runs=None):
    '''Load sequencing runs from general table'''
    global _sequencing_runs
    if _sequencing_runs is None:
        seq_runs_in = pd.read_excel(table_filename, 'Sequencing runs',
                                 index_col=0)
        _sequencing_runs = seq_runs_in

    if seq_runs is not None:
        seq_runs = _sequencing_runs.loc[_sequencing_runs.index.isin(seq_runs)]
        return seq_runs
    else:
        return _sequencing_runs

def load_sequencing_run(seq_run):
    '''Load a single sequencing run (with extended attributes)'''
    seq_runs = load_sequencing_runs()
    seq_run_obj = SequencingRun(seq_runs.loc[seq_run])
    return seq_run_obj

ext: py

max_stars_repo_name: iosonofabio/hivwholeseq

max_issues_repo_name: iosonofabio/hivwholeseq

max_forks_repo_name: iosonofabio/hivwholeseq

reason: import numpy


text:
# -*- coding: utf-8 -*-
"""
.. module:: hindex
    :synopsis: Calculate the hindex.

.. moduleauthor:: Alex Gates <ajgates42@gmail.com>
 """

import sys

import pandas as pd
import numpy as np

# determine if we are loading from a jupyter notebook (to make pretty progress bars)
if 'ipykernel' in sys.modules:
    from tqdm.notebook import tqdm
else:
    from tqdm import tqdm

from pyscisci.utils import zip2dict

## Q-factor
def qfactor(a):
    """
    This function calculates the Q-factor for an author.  See :cite:`Sinatra2016individual` for details.

    """

    return np.exp(np.mean(np.log(a[a>0])))

def compute_qfactor(df, colgroupby, colcountby, show_progress=False):
    """
    Calculate the q factor for each group in the DataFrame.  See :cite:`Sinatra2016individual` for the definition.

    The algorithmic implementation for each author can be found in :py:func:`citationanalysis.author_qfactor`.

    Parameters
    ----------
    :param df : DataFrame
        A DataFrame with the citation information for each Author.

    :param colgroupby : str
        The DataFrame column with Author Ids.

    :param colcountby : str
        The DataFrame column with Citation counts for each publication.

    Returns
    -------
    DataFrame
        DataFrame with 2 columns: colgroupby, 'Hindex'

        """
    # register our pandas apply with tqdm for a progress bar
    tqdm.pandas(desc='Qfactor', disable= not show_progress)

    newname_dict = zip2dict([str(colcountby), '0'], [str(colgroupby)+'Qfactor']*2)
    return df.groupby(colgroupby, sort=False)[colcountby].progress_apply(qfactor).to_frame().reset_index().rename(columns=newname_dict)

max_stars_repo_name: kishorevasan/pyscisci

max_issues_repo_name: kishorevasan/pyscisci

max_forks_repo_name: kishorevasan/pyscisci


text:
# Example file that finds the groundstate of a condensate in a rotating frame.
# Takes quite some time to run so you can just stop it when you run out of
# patience and run the plotting script.

# Run with 'mpirun -n <N CPUs> python run.py'

from __future__ import division, print_function
import sys
sys.path.insert(0, '../..') # The location of the modules we need to import

import numpy as np

from parPDE import Simulator2D, LAPLACIAN, GRADX, GRADY
from BEC2D import BEC2D

def get_number_and_trap(rhomax, R):
    """Gives the 2D normalisation constant and trap frequency required for the
    specified maximum density and radius of a single-component condensate in
    the Thomas-Fermi approximation"""
    N = pi * rhomax * R**2 / 2
    omega = np.sqrt(2 * g * rhomax / (m * R**2))
    return N, omega

# Constants:
pi = np.pi
hbar = 1.054571726e-34                        # Reduced Planck's constant
a_0  = 5.29177209e-11                         # Bohr radius
u    = 1.660539e-27                           # unified atomic mass unit
m  = 86.909180*u                              # 87Rb atomic mass
a  = 98.98*a_0                                # 87Rb |2,2> scattering length
g  = 4*pi*hbar**2*a/m                         # 87Rb self interaction constant

rhomax = 2.5e14 * 1e6                         # Desired peak condensate density
R = 9e-6                                      # Desired condensate radius
mu = g* rhomax                                # Approximate chemical potential for desired max density
                                              # (assuming all population is in in mF=+1 or mF=-1)
N_2D, omega = get_number_and_trap(rhomax, R)  # 2D normalisation constant and trap frequency
                                              # required for specified radius and peak density

# Rotation rate:
Omega = 2*omega

# Space:
nx_global = ny_global = 256
x_max_global = y_max_global = 10e-6

simulator = Simulator2D(-x_max_global, x_max_global, -y_max_global, y_max_global, nx_global, ny_global,
                        periodic_x=False, periodic_y=False, operator_order=6)
bec2d = BEC2D(simulator, natural_units=False, use_ffts=False)

x = simulator.x
y = simulator.y
dx = simulator.dx
dy = simulator.dy

r2 = x**2.0 + y**2.0
r  = np.sqrt(r2)

# A harmonic trap to exactly cancel out the centrifugal force:
alpha = 2
V = 0.5 * m * Omega**2 * R**2.0 * (r/R)**alpha

# A high order polynomial trap as a hard wall potential:
alpha = 4
V += 0.5 * m * omega**2 * R**2.0 * (r/R)**alpha

import IPython
IPython.embed()

# The kinetic and rotation terms of the Hamiltonian:
K = -hbar**2/(2*m)*LAPLACIAN - 1j*hbar*Omega*(y * GRADX - x * GRADY)

def H(t, psi):
    """The Hamiltonian for single-component wavefunction psi. Returns the
    kinetic term as an OperatorSum instance, and the local terms separately."""
    H_local_lin = V
    H_local_nonlin = g * abs(psi)**2
    return K, H_local_lin, H_local_nonlin

if __name__ == '__main__':
    # The initial Thomas-Fermi guess:
    # psi = rhomax * (1 - (x**2 + y**2) / R**2)
    # psi[psi < 0] = 0
    # psi = np.sqrt(psi)

    # psi = np.array(psi, dtype=complex)

    import h5py
    with h5py.File('initial_16.h5', 'r') as f:
        psi = f['psi'][:]

    # # Some random vortices to give the relaxation something to start with:
    # np.random.seed(42) # must be seeded so that all MPI processes agree
    # for i in range(100):
    #     x_vortex = np.random.normal(0, scale=R)
    #     y_vortex = np.random.normal(0, scale=R)
    #     psi[:] *= np.exp(1j*np.arctan2(x - y_vortex, y - x_vortex))

    # Find the groundstate:
    psi = bec2d.find_groundstate(H, mu, psi, relaxation_parameter=1.9, convergence=1e-6,
                                 output_interval=1000, output_directory='groundstate_4', convergence_check_interval=10)

max_stars_repo_name: chrisjbillington/parpde

max_issues_repo_name: chrisjbillington/parpde

max_forks_repo_name: chrisjbillington/parpde


text:
# !/usr/bin/python
# coding: utf-8

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(100)

# 生成一组随机数，数量为1000
data = np.random.normal(size = (1000, ), loc = 0, scale= 1)

# sym 调整好异常值的点的形状
# whis 默认是1.5， 通过调整它的竖直来设置异常值显示的数量，
# 如果想显示尽可能多的异常值，whis设置很小，否则很大
plt.boxplot(data, sym ="o", whis = 1.5)
# plt.boxplot(data, sym ="o", whis = 0.01)
# plt.boxplot(data, sym ="o", whis = 999)
plt.show()


text:
import matplotlib.pyplot as pyplot
import matplotlib.image as mpimg
import numpy as np
img = mpimg.imread(r'src\matplotlib\stinkbug.png')

print(len(img), type(img))

imgplot = pyplot.imshow(img)
pyplot.show()

ar = [np.random.randn(1000), np.random.randn(10000), np.random.randn(10000)]
lum_img =  img[:, :, 0]
pyplot.imshow(lum_img)

pyplot.show()
pyplot.imshow(lum_img, cmap="hot")
pyplot.colorbar()

pyplot.show()

pyplot.hist(lum_img.ravel(), bins=256, range=(0.0, 1.0), fc='k', ec='k')
pyplot.show()

from PIL import Image

img = Image.open(r'src\matplotlib\stinkbug.png')
img.thumbnail((64, 64), Image.ANTIALIAS)  # resizes image in-place
imgplot = pyplot.imshow(img)
pyplot.show()

imgplot = pyplot.imshow(img, interpolation="nearest")


text:
import numpy as np
from gym.envs.registration import register
from .cartpole import CartPoleEnv

register(
    id='CartPole-v2',
    entry_point='envs:CartPoleEnv',
)


text:
"""Classes of networks

Author: Xiaodong Nian
Time: November 2019
"""

import numpy as np
import tensorflow as tf
from collections import deque

from networks import Actor, Critic, Critic0, ActorCritic, ActorCriticER
# NN configs.
from config_agent import IM_SIZE, BLACK_AND_WHITE
# from diverse_mem import DiverseMemory
from diverse_mem_attentive import AttentiveMemoryBuffer, MemoryBuffer
from history import History
from utils import *

class DCRACAgent:
    def __init__(self,
                 env,
                 gamma=0.98,
                 weights=None,
                 timesteps=5,
                 batch_size=32,
                 replay_type='STD',
                 buffer_size=10000,
                 buffer_a=2,
                 buffer_e=0.01,
                 memnn_size=9,
                 start_annealing=0.05,
                 max_episode_length=500,
                 start_e=1.,
                 end_e=0.05,
                 im_size=(IM_SIZE, IM_SIZE),
                 grayscale=BLACK_AND_WHITE,
                 net_type='R',
                 obj_func='a',
                 lr=1e-3,
                 lr_2=1e-3,
                 frame_skip=4,
                 update_interval=4,
                 clipnorm=1,
                 clipvalue=1,
                 nesterov=True,
                 momentum=0.9,
                 dup=False,
                 min_buf_size=0.1,
                 action_conc=True,
                 feature_embd=True,
                 extra=None,
                 gpu_setting='1',
                 attentive=True):

        self.env = env
        self.nb_action = self.env.action_space.shape[0]
        self.observation_shape = self.env.observation_space.shape
        self.nb_objective = self.env.obj_cnt
        self.discount = gamma
        self.qvalue_dim = self.nb_objective

        self.timesteps = timesteps
        self.batch_size = batch_size
        self.replay_type = replay_type
        self.buffer_size = buffer_size
        self.buffer_a = buffer_a
        self.buffer_e = buffer_e
        self.max_episode_length = max_episode_length

        self.fill_history = False
        self.stoch_policy = True
        self.use_prob = False

        self.memnn_size = memnn_size

        self.weight_history = []
        if weights is not None:
            self.set_weights(np.array(weights))

        self.start_e = start_e
        self.end_e = end_e
        self.start_annealing = start_annealing

        self.im_size = im_size
        channel = 1 if grayscale else 3
        self.im_shape = im_size + (channel, )

        self.lr_2 = lr_2
        self.lr = lr
        self.frame_skip = frame_skip

        self.net_type = net_type
        self.action_conc = action_conc
        self.feature_embd = feature_embd

        # self.mask_empty = True if self.action_conc and self.net_type in ('R', 'M') else False
        self.mask_empty = True

        self.dup = dup
        self.obj_func = obj_func.lower()
        self.min_buf_size = min_buf_size
        self.update_interval = update_interval
        self.actor_update_interval = 1

        self.nesterov = nesterov
        self.clipnorm = clipnorm
        self.clipvalue = clipvalue
        self.momentum = momentum

        self.direct_update = True
        self.is_first_update = True
        self.max_error = .0
        self.trace_values={}
        self.recent_experiences = []

        self.start_lambda = 3
        self.end_lambda = 1
        self.alpha = 1
        self.attentive = attentive

        self.extra = extra
        self.gpu_setting = gpu_setting

        # Tensorflow GPU optimization.
        config = tf.compat.v1.ConfigProto()
        config.gpu_options.allow_growth = True
        sess = tf.compat.v1.Session(config=config)
        tf.compat.v1.keras.backend.set_session(sess)

        # config = tf.ConfigProto()
        # config.gpu_options.allow_growth = True
        # sess = tf.Session(config=config)
        # from keras import backend as K
        # K.set_session(sess)

        # Initialize the history
        self.history = History(self.timesteps, self.im_shape, self.nb_action)

        # Make neural networks
        self.build_models()

        # Initialize replay buffer.
        self.initialize_memory()

    def build_models(self):
        # Make actor network (creates target model internally).
        self.actor = Actor(self.nb_action, self.im_shape, self.nb_objective, self.qvalue_dim,
            timesteps=self.timesteps, net_type=self.net_type, memnn_size=self.memnn_size,
            lr=self.lr_2, action_conc=self.action_conc, action_embd=self.feature_embd,
            weight_embd=self.feature_embd, backendtype=self.gpu_setting)

        # Make critic network (creates target model internally).
        self.critic = Critic(self.nb_action, self.im_shape, self.nb_objective, self.qvalue_dim,
            timesteps=self.timesteps, net_type=self.net_type, memnn_size=self.memnn_size,
            lr=self.lr, action_conc=self.action_conc, action_embd=self.feature_embd,
            weight_embd=self.feature_embd, backendtype=self.gpu_setting)

        # log the settings
        with open("output/logs/info_"+self.extra+".log", "a") as f:
            f.write(time.asctime()+'\n\n')
            f.write('ACTORRRRRRRRRRRRRRRRRRRRRRR\n')
            f.write(str(self.actor.model.get_config())+'\n\n')
            f.write('CRITICCCCCCCCCCCCCCCCCCCCCC\n')
            f.write(str(self.critic.model.get_config())+'\n\n')
            f.write(str(vars(self))+'\n\n')
        print(vars(self))

    # Train or run for some number of episodes.
    def train(self, log_file, learning_steps, weights,
              per_weight_steps, total_steps, log_game_step=False):

        self.learning_steps = learning_steps
        self.epsilon = self.start_e
        per_weight_steps = per_weight_steps

        weight_index = 0
        self.steps = 0
        self.total_steps = int(total_steps)
        half_total = int(total_steps/2)

        self.log = Log(log_file)
        self.log_game_step = log_game_step

        self.set_weights(weights[weight_index])

        episodes = 1
        episode_steps = 0
        pred_idx = None

        current_state_raw = self.env.reset()
        self.current_state, last_action = self.history.reset_with_raw_frame(current_state_raw, fill=self.fill_history)

        for step in range(self.total_steps):

            self.steps = step
            episode_steps += 1

            # pick an action following an epsilon-greedy strategy
            action, acts_prob = self.pick_action(self.current_state, last_action)

            # perform the action
            next_state_raw, reward, terminal, info = self.env.step(action, self.frame_skip)
            next_state, next_last_action = self.history.add_raw_frame(next_state_raw, action)

            if self.log_game_step:
                print("Taking action", action, "under prob", acts_prob, "at", info["position"], "with reward", reward)

            # memorize the experienced transition
            pred_idx = self.memorize(
                self.current_state,
                action,
                reward,
                next_state,
                terminal,
                last_action,
                acts_prob,
                trace_id=episodes if self.replay_type == "DER" else step,
                pred_idx=pred_idx)

            # update the networks and exploration rate
            self.update_lambda(step)
            loss = self.perform_updates(step)
            self.update_epsilon(step)

            self.log.log_step(episodes, step, loss, reward,
                              terminal or episode_steps > self.max_episode_length,
                              self.weights, self.discount, episode_steps,
                              self.epsilon, self.frame_skip, action)

            self.current_state = next_state
            current_state_raw = next_state_raw
            last_action = next_last_action

            if terminal or episode_steps > self.max_episode_length:
                current_state_raw = self.env.reset()
                self.current_state, last_action = self.history.reset_with_raw_frame(current_state_raw, fill=self.fill_history)
                pred_idx = None

                is_weight_change = int(
                    (step + 1) / per_weight_steps) != weight_index

                if per_weight_steps == 1:
                    weight_index += 1
                else:
                    weight_index = int((step + 1) / per_weight_steps)

                if per_weight_steps == 1 or is_weight_change:
                    self.set_weights(weights[weight_index])

                episodes += 1
                episode_steps = 0

            if (step + 1) % 10000 == 0:
                # self.save_weights()
                self.save_model()

            if step == half_total:
                self.use_prob = True

    def set_weights(self, weights):
        """Set current weight vector

        Arguments:
            weights {np.array} -- Weight vector of size N
        """

        self.weights = np.array(weights)
        self.weight_history.append(self.weights)

    def pick_action(self, trace_o, trace_a, weights=None):
        """Given a observation trace and weights, compute the next action, following an
            epsilon-greedy strategy

        Arguments:
            trace_o {np.array} -- The trace of observations o_t in which to act
            trace_a {np.array} -- The trace of actions a_{t-1}, in the same order as trace_o

        Keyword Arguments:
            weights {np.array} -- The weights on which to act (default: self.weights)

        Returns:
            int -- The selected action's index
        """
        # np.random.seed(self.steps)
        if np.random.random() < self.epsilon:
            return np.random.choice(self.env.action_space), (np.ones(self.nb_action)/self.nb_action)

        trace_a = self._get_onehot_action_trace(trace_a)
        weights = self.weights if weights is None else weights

        action, acts_prob = self.actor.choose_action(
            weights[np.newaxis, ], trace_o[np.newaxis, ], trace_a[np.newaxis, ],
            stochastic=self.stoch_policy, return_probs=True)

        return action, acts_prob

    def _get_onehot_action_trace(self, actions):
        """Translate the numerical actions array as one-hot array.

        Args:
            actions ([int]): integer action list

        Returns:
            np.array: one-hot action list
        """
        length = len(actions)
        onehot_trace = np.zeros((length, self.nb_action))
        for i, a in enumerate(actions):
            if a >= 0:
                onehot_trace[i][a] = 1
            # if mask_empty, set all action slots as -1 to be masked by the network.
            if self.mask_empty and a == -1:
                onehot_trace[i] = -1
        return onehot_trace

    # Given a bunch of experienced histories, update our models.
    def perform_updates(self, steps):
        loss = 0
        if steps> self.min_buf_size * self.learning_steps:

            if steps % self.update_interval == 0:

                if self.is_first_update:
                    # Compute buffer's priorities all at once before the first update
                    self.recent_experiences = []
                    self.update_all_priorities()
                    self.is_first_update = False

                loss = self.policy_update(update_actor=(steps%(self.actor_update_interval*self.update_interval) == 0))
                self.update_target(soft_update=True)

        return loss

    def update_target(self, **kwargs):
        self.actor.update_target(**kwargs)
        self.critic.update_target(**kwargs)

    def policy_update(self, update_actor=True):
        # np.random.seed(self.steps)
        # ids, batch, _ = self.buffer.sample(self.batch_size)
        if self.attentive:
            ids, batch, _ = self.buffer.sample(self.batch_size, self.k, self.steps, self.weights, self.current_state)
        else:
            ids, batch, _ = self.buffer.sample(self.batch_size)

        if self.direct_update:
            # Add recent experiences to the priority update batch
            batch = np.concatenate((batch, self.buffer.get(self.recent_experiences)), axis=0)
            ids = np.concatenate((ids, self.recent_experiences)).astype(int)

        # if self.dup is True, we train each sample in the batch on two
        # weight vectors, hence we duplicate the batch data
        if self.dup:
            batch = np.repeat(batch, 2, axis=0)
            ids = np.repeat(ids, 2, axis=0)

        cur_batch_size = len(batch)

        # q_predict, q_target, p_predict, p_target, w_batch, o_batch, a_last_batch
        q_predict, q_target, p_predict, p_target, w_batch, o_batch, a_last_batch = self._get_training_data(batch)

        q_true = np.copy(q_predict)
        q_mask = np.zeros((cur_batch_size, self.nb_action, self.qvalue_dim), dtype=float)
        action_objective = np.zeros((cur_batch_size, self.nb_action), dtype=float)

        for i, (_, action, reward, _, done, _, _, _) in enumerate(batch):
            q_true[i][action] = np.copy(reward)
            q_mask[i][action] = 1

            if not done:
                if self.stoch_policy:
                    # # stochastic policy (sample an action 'a_next' with target action_probs)
                    # a_next = np.random.choice(range(self.nb_action), p=p_target[i])
                    # q_true[i][action] += self.discount * q_target[i][a_next]

                    # Expectation for stochastic policy
                    q_true[i][action] += self.discount * np.dot(p_target[i], q_target[i])
                else:
                    # deterministic policy (choose the 'a_next' with largest target action_probs)
                    a_next = np.argmax(p_target[i])
                    q_true[i][action] += self.discount * q_target[i][a_next]

            if update_actor:
                action_objective[i][action] = self._get_action_objective(q_true[i], q_predict[i], p_predict[i], action, w_batch[i])

        a_batch = self._get_onehot_action_trace([b[1] for b in batch])
        p_old_batch = np.array([b[6] for b in batch])

        self.train_on_batch(w_batch, o_batch, a_last_batch, q_true, action_objective, a_batch, p_old_batch, q_mask)

        loss = self.update_priorities(batch, ids)
        self.recent_experiences = []

        return loss

    def train_on_batch(self, w_batch, o_batch, a_last_batch, q_true, action_objective, a_batch, p_old_batch, q_mask=None):
        self.critic.model.train_on_batch([w_batch, o_batch, a_last_batch], q_true)
        self.actor.model.train_on_batch([w_batch, o_batch, a_last_batch], action_objective)

    def _get_training_data(self, batch):

        o_batch, o_next_batch, a_last_batch, a_last_next_batch = self._get_full_states(batch)

        w_batch = self.get_training_weights(batch)

        # Predict both the model and target q-values
        predict_input = [w_batch, o_batch, a_last_batch]
        target_input = [w_batch, o_next_batch, a_last_next_batch]

        # Predict both the model and target q-values
        q_predict = self.critic.model.predict(predict_input)
        q_target = self.critic.target_model.predict(target_input)

        # Predict both the model and target action probs
        p_predict = self.actor.model.predict(predict_input)
        p_target = self.actor.target_model.predict(target_input)

        return q_predict, q_target, p_predict, p_target, w_batch, o_batch, a_last_batch

    def _get_full_states(self, batch):

        o_batch = np.zeros((len(batch),) + self.history.history_o.shape, dtype=np.uint8)
        o_next_batch = np.zeros((len(batch),) + self.history.history_o.shape, dtype=np.uint8)

        # one-hot for predict
        a_last_batch = np.zeros((len(batch), self.history.length, self.nb_action), dtype=float)
        a_last_next_batch = np.zeros((len(batch), self.history.length, self.nb_action), dtype=float)

        for i, b in enumerate(batch):
            o_batch[i] = b[0]
            o_next_batch[i][:-1] = b[0][1:]
            o_next_batch[i][-1] = b[3]

            a_last_batch[i] = self._get_onehot_action_trace(b[5])
            a_last_next_batch[i] = np.roll(a_last_batch[i], -1, axis=0)
            a_last_next_batch[i][-1][b[1]] = 1

        return o_batch, o_next_batch, a_last_batch, a_last_next_batch

    def _get_action_objective(self, q_true, q_predict, p_predict, action, weight):
        if self.obj_func == 'a':
            objective = q_true[action] - q_predict.mean(axis=0)
        elif self.obj_func == 'ap':
            # based on advantages
            objective = q_true[action] - np.average(q_predict, weights=p_predict, axis=0)
        elif self.obj_func == 'td':
            # based on td error
            objective = q_true[action] - q_predict[action]
        elif self.obj_func == 'y':
            # based on y values
            objective = q_true[action]
        else:
            # based on q values
            objective = q_predict[action]

        return np.dot(objective, weight)

    def get_training_weights(self, batch):
        """Given a batch of transitions, this method generates a batch of
        weights to train on

        Arguments:
            batch {list} -- batch of transitions

        Returns:
            list -- batch of weights
        """
        w_batch = np.repeat([self.weights], len(batch), axis=0)
        if self.dup and len(self.weight_history) > 1:
            idx = np.random.randint(len(self.weight_history)-1 , size=int(len(batch)))
            w_batch[::] = np.array(self.weight_history)[idx]
            w_batch[::2] = self.weights
        return w_batch

    def update_priorities(self, batch, ids, ignore_dup=False, pr=False):
        """Given a batch of transitions, this method computes each transition's
        error and uses that error to update its priority in the replay buffer

        Arguments:
            batch {list} -- list of transitions
            ids {list} -- list of identifiers of each transition in the replay
                        buffer

        Returns:
            float -- The batch's mean loss
        """
        # q_predict, q_target, p_predict, p_target, w_batch, o_batch, a_last_batch
        q_predict, q_target, _, p_target, _, _, _ = self._get_training_data(batch)

        errors = np.zeros(len(batch))

        for i, (_, action, reward, _, terminal, _, _, _) in enumerate(batch):

            target = np.copy(reward)

            if not terminal:
                if self.stoch_policy:
                    # # stochastic policy (sample an action 'a_next' with target action_probs)
                    # a_next = np.random.choice(range(self.nb_action), p=p_target[i])
                    # target += self.discount * q_target[i][a_next]

                    # Expectation for stochastic policy
                    target += self.discount * np.dot(p_target[i], q_target[i])
                else:
                    # deterministic policy (choose the 'a_next' with largest target action_probs)
                    a_next = np.argmax(p_target[i])
                    target += self.discount * q_target[i][a_next]

            error = mae(q_predict[i][action], target)
            errors[i] = error

            # When dup is True, we train each sample on two weight vectors
            # Hence, there are two TD-errors per sample, we use the mean of
            # both errors to update the priorities
            if self.dup:
                if i % 2 == 0:
                    continue
                error = (error + errors[i - 1]) / 2
            self.buffer.update(ids[i], error)
            self.max_error = max(error, self.max_error)

        return np.mean(errors)

    def update_all_priorities(self):
        """Updates all priorities of the replay buffer
        """
        data = self.buffer.get_data(True)

        chunk_size = 100
        for i in range(0, len(data[0]), chunk_size):
            chunk_data = np.array(data[1][i:i + chunk_size])
            chunk_ids = data[0][i:i + chunk_size]

            if self.dup:
                chunk_data = np.repeat(chunk_data, 2, axis=0)
                chunk_ids = np.repeat(chunk_ids, 2, axis=0)

    def update_epsilon(self, steps):
        """Update exploration rate

        Arguments:
            steps {int} -- Elapsed number of steps
        """

        start_steps = self.learning_steps * self.start_annealing
        annealing_steps = self.learning_steps * (1 - self.start_annealing)

        self.epsilon = linear_anneal(steps, annealing_steps, self.start_e, self.end_e, start_steps)

    def update_lambda(self, steps):
        start_steps = self.learning_steps * self.start_annealing
        annealing_steps = self.total_steps * self.alpha

        self.k = self.linear_anneal_lambda(steps, annealing_steps, self.start_lambda, self.end_lambda, start_steps)

    def linear_anneal_lambda(self, steps, annealing_steps, start_lambda, end_lambda, start_steps):
        t = max(0, steps - start_steps)
        return max(end_lambda, (annealing_steps-t) * (start_lambda - end_lambda) / annealing_steps + end_lambda)

    def initialize_memory(self):
        """Initialize the replay buffer, with a secondary diverse buffer and/or
            a secondary tree to store prediction errors
        """
        if self.replay_type == "DER":
            main_capacity = sec_capacity = self.buffer_size // 2
        else:
            main_capacity, sec_capacity = self.buffer_size, 0

        def der_trace_value(trace, trace_id, memory_indices):
            """Computes a trace's value as its return

            Arguments:
                trace {list} -- list of transitions
                trace_id {object} -- the trace's id
                memory_indices {list} -- list of the trace's indexes in memory

            Returns:
                np.array -- the trace's value
            """

            if trace_id in self.trace_values:
                return self.trace_values[trace_id]
            I_REWARD = 2
            value = np.copy(trace[0][I_REWARD])
            for i, v in enumerate(trace[1:]):
                value += v[I_REWARD] * self.discount**(i + 1)
            if type(value) == float:
                value = np.array([value])
            self.trace_values[trace_id] = value
            return value

        if self.attentive:
            self.buffer = AttentiveMemoryBuffer(main_capacity=main_capacity, sec_capacity=sec_capacity,
                value_function=der_trace_value, trace_diversity=True, a=self.buffer_a, e=self.buffer_e)
        else:
            self.buffer = MemoryBuffer(main_capacity=main_capacity, sec_capacity=sec_capacity,
                value_function=der_trace_value, trace_diversity=True, a=self.buffer_a, e=self.buffer_e)

    def memorize(self, state, action, reward, next_state, terminal, action_prev, acts_prob,
        initial_error=0, trace_id=None, pred_idx=None):
        """Memorizes a transition into the replay, if no error is provided, the
        transition is saved with the lowest priority possible, and should be
        updated accordingly later on.

        Arguments:
            state {object} -- s_t
            action {int} -- a_t
            reward {np.array} -- r_t
            next_state {object} -- s_{t+1}
            terminal {bool} -- wether s_{t+1} is terminal
            action_prev {object} -- a_{t-1}
            acts_prob {np.array} -- sample probability

        Keyword Arguments:
            initial_error {float} -- The initial error of the transition (default: {0})
            trace_id {object} -- The trace's identifier, if None, the transition is treated as
                                an individual trace. (default: {None})
        """
        if initial_error == 0 and not self.direct_update:
            initial_error = self.max_error

        extra = np.array([self.weights])
        transition = np.array((state, action, reward, next_state[-1], terminal, action_prev, acts_prob, extra))

        # Add transition to replay buffer
        idx = self.buffer.add(initial_error, transition, pred_idx=pred_idx, trace_id=trace_id)
        self.recent_experiences.append(idx)

        return idx

    @property
    def name(self):
        return self.extra

    def save_weights(self):
        """Saves the networks' weights to files identified by the agent's name
        and the current weight vector
        """
        self.critic.model.save_weights("output/networks/{}_critic.weights".format(self.name))
        self.actor.model.save_weights("output/networks/{}_actor.weights".format(self.name))

    def save_model(self):
        """Saves the networks to files identified by the agent's name
        and the current weight vector
        """
        self.critic.model.save("output/networks/{}_critic_model.h5".format(self.name))
        self.actor.model.save("output/networks/{}_actor_model.h5".format(self.name))

class DCRACSAgent(DCRACAgent):
    def build_models(self):
        # Make only critic network (creates target model internally).
        self.actorcritic = ActorCritic(self.nb_action, self.im_shape, self.nb_objective, self.qvalue_dim,
            timesteps=self.timesteps, net_type=self.net_type, memnn_size=self.memnn_size,
            lr=self.lr, action_conc=self.action_conc, action_embd=self.feature_embd,
            weight_embd=self.feature_embd, actor_lr=self.lr_2, backendtype=self.gpu_setting)

        with open("output/logs/info_"+self.extra+".log", "a") as f:
            f.write(time.asctime()+'\n\n')
            f.write(str(self.actorcritic.model.get_config())+'\n\n')
            f.write(str(vars(self))+'\n\n')
        print(vars(self))

    def pick_action(self, trace_o, trace_a, weights=None):
        # np.random.seed(self.steps)
        if np.random.random() < self.epsilon:
            return np.random.choice(self.env.action_space), (np.ones(self.nb_action)/self.nb_action)

        trace_a = self._get_onehot_action_trace(trace_a)
        weights = self.weights if weights is None else weights

        action, acts_prob = self.actorcritic.choose_action(
            weights[np.newaxis, ], trace_o[np.newaxis, ], trace_a[np.newaxis, ],
            stochastic=self.stoch_policy, use_prob=self.use_prob, return_probs=True)

        return action, acts_prob

    def update_target(self, **kwargs):
        self.actorcritic.update_target(**kwargs)

    def train_on_batch(self, w_batch, o_batch, a_last_batch, q_true, action_objective, a_batch, p_old_batch, q_mask=None):
        self.actorcritic.model.train_on_batch([w_batch, o_batch, a_last_batch], [q_true, action_objective])

    def _get_training_data(self, batch):

        o_batch, o_next_batch, a_last_batch, a_last_next_batch = self._get_full_states(batch)

        w_batch = self.get_training_weights(batch)

        # Predict both the model and target q-values
        predict_input = [w_batch, o_batch, a_last_batch]
        target_input = [w_batch, o_next_batch, a_last_next_batch]

        # Predict both the model and target q-values
        q_predict, p_predict = self.actorcritic.model.predict(predict_input)
        q_target, p_target = self.actorcritic.target_model.predict(target_input)

        return q_predict, q_target, p_predict, p_target, w_batch, o_batch, a_last_batch

    def save_weights(self):
        """Saves the networks' weights to files identified by the agent's name
        and the current weight vector
        """
        self.actorcritic.model.save_weights("output/networks/{}_actorcritic_share.weights".format(self.name))

    def save_model(self):
        """Saves the networks to files identified by the agent's name
        and the current weight vector
        """
        self.actorcritic.model.save("output/networks/{}_critic_model.h5".format(self.name))

class DCRACSEAgent(DCRACSAgent):
    def build_models(self):
        # Make only critic network (creates target model internally).
        self.actorcritic = ActorCriticER(self.nb_action, self.im_shape, self.nb_objective, self.qvalue_dim,
            timesteps=self.timesteps, net_type=self.net_type, memnn_size=self.memnn_size,
            lr=self.lr, action_conc=self.action_conc, action_embd=self.feature_embd,
            weight_embd=self.feature_embd, backendtype=self.gpu_setting)

        with open("output/logs/info_"+self.extra+".log", "a") as f:
            f.write(time.asctime()+'\n\n')
            f.write(str(self.actorcritic.model.get_config())+'\n\n')
            f.write(str(vars(self))+'\n\n')
        print(vars(self))

    def train_on_batch(self, w_batch, o_batch, a_last_batch, q_true, action_objective, a_batch, p_old_batch, q_mask=None):
        action_objective = np.sum(action_objective, axis=1)
        self.actorcritic.trainable_model.train_on_batch([w_batch, o_batch, a_last_batch, action_objective, p_old_batch], [q_true, a_batch])

class DCRAC0Agent(DCRACAgent):
    def build_models(self):
        # Make actor network (creates target model internally).
        self.actor = Actor(self.nb_action, self.im_shape, self.nb_objective, self.qvalue_dim,
            timesteps=self.timesteps, net_type=self.net_type, memnn_size=self.memnn_size,
            lr=self.lr_2, action_conc=self.action_conc, action_embd=self.feature_embd,
            weight_embd=self.feature_embd, backendtype=self.gpu_setting)

        # Make critic network (creates target model internally).
        self.critic = Critic0(self.nb_action, self.im_shape, self.nb_objective, self.qvalue_dim,
            timesteps=self.timesteps, net_type=self.net_type, memnn_size=self.memnn_size,
            lr=self.lr, action_conc=self.action_conc, action_embd=self.feature_embd,
            weight_embd=self.feature_embd, backendtype=self.gpu_setting)

        # log the settings
        with open("output/logs/info_"+self.extra+".log", "a") as f:
            f.write(time.asctime()+'\n\n')
            f.write('ACTORRRRRRRRRRRRRRRRRRRRRRR\n')
            f.write(str(self.actor.model.get_config())+'\n\n')
            f.write('CRITICCCCCCCCCCCCCCCCCCCCCC\n')
            f.write(str(self.critic.model.get_config())+'\n\n')
            f.write(str(vars(self))+'\n\n')
        print(vars(self))

    def train_on_batch(self, w_batch, o_batch, a_last_batch, q_true, action_objective, a_batch, p_old_batch, q_mask=None):
        dummy = q_true[:, 0, :]
        self.critic.trainable_model.train_on_batch([w_batch, o_batch, a_last_batch, q_true, q_mask], [dummy, q_true])
        self.actor.model.train_on_batch([w_batch, o_batch, a_last_batch], action_objective)

class CNAgent(DCRACAgent):
    def build_models(self):
        # Make only critic network (creates target model internally).
        self.critic = Critic(self.nb_action, self.im_shape, self.nb_objective, self.qvalue_dim,
            timesteps=self.timesteps, net_type=self.net_type, memnn_size=self.memnn_size,
            lr=self.lr, action_conc=self.action_conc, action_embd=self.feature_embd,
            weight_embd=self.feature_embd, backendtype=self.gpu_setting)

        with open("output/logs/info_"+self.extra+".log", "a") as f:
            f.write(time.asctime()+'\n\n')
            f.write(str(self.critic.model.get_config())+'\n\n')
            f.write(str(vars(self))+'\n\n')
        print(vars(self))

    def pick_action(self, trace_o, trace_a, weights=None):
        """Given a observation trace and weights, compute the next action, following an
            epsilon-greedy strategy

        Arguments:
            trace_o {np.array} -- The trace of observations o_t in which to act
            trace_a {np.array} -- The trace of actions a_{t-1}, in the same order as trace_o

        Keyword Arguments:
            weights {np.array} -- The weights on which to act (default: self.weights)

        Returns:
            int -- The selected action's index
        """
        # np.random.seed(self.steps)
        if np.random.random() < self.epsilon:
            return np.random.choice(self.env.action_space), (np.ones(self.nb_action)/self.nb_action)

        trace_a = self._get_onehot_action_trace(trace_a)
        weights = self.weights if weights is None else weights

        q_value = self.critic.model.predict([weights[np.newaxis, ], trace_o[np.newaxis, ], trace_a[np.newaxis, ]])

        scal_q = np.dot(q_value[0], weights)
        action = np.argmax(scal_q)
        return action, scal_q

    # Given a bunch of experienced histories, update our models.
    def update_target(self, **kwargs):
        self.critic.update_target(**kwargs)

    def policy_update(self, update_actor=False):
        # np.random.seed(self.steps)
        ids, batch, _ = self.buffer.sample(self.batch_size)

        if self.direct_update:
            # Add recent experiences to the priority update batch
            batch = np.concatenate((batch, self.buffer.get(self.recent_experiences)), axis=0)
            ids = np.concatenate((ids, self.recent_experiences)).astype(int)

        # if self.dup is True, we train each sample in the batch on two
        # weight vectors, hence we duplicate the batch data
        if self.dup:
            batch = np.repeat(batch, 2, axis=0)
            ids = np.repeat(ids, 2, axis=0)

        # q_predict, q_target, w_batch, o_batch, a_last_batch
        q_predict, q_target, w_batch, o_batch, a_last_batch = self._get_training_data(batch)

        q_true = np.copy(q_predict)
        masks = np.zeros((len(batch), self.nb_action, self.qvalue_dim), dtype=float)

        for i, (_, action, reward, _, done, _, _) in enumerate(batch):
            q_true[i][action] = np.copy(reward)
            masks[i][action] = 1

            if not done:
                a_next = np.argmax(np.dot(q_target[i], w_batch[i]))
                q_true[i][action] += self.discount * q_target[i][a_next]

        dummy = q_true[:, 0, :]
        self.train_on_batch(w_batch, o_batch, a_last_batch, q_true, dummy, masks)

        loss = self.update_priorities(batch, ids)
        self.recent_experiences = []

        return loss

    def train_on_batch(self, w_batch, o_batch, a_last_batch, q_true, dummy=None, q_mask=None):
        self.critic.model.train_on_batch([w_batch, o_batch, a_last_batch], q_true)

    def _get_training_data(self, batch):

        o_batch, o_next_batch, a_last_batch, a_last_next_batch = self._get_full_states(batch)

        w_batch = self.get_training_weights(batch)

        # Predict both the model and target q-values
        predict_input = [w_batch, o_batch, a_last_batch]
        target_input = [w_batch, o_next_batch, a_last_next_batch]

        # Predict both the model and target q-values
        q_predict = self.critic.model.predict(predict_input)
        q_target = self.critic.target_model.predict(target_input)

        return q_predict, q_target, w_batch, o_batch, a_last_batch

    def update_priorities(self, batch, ids, ignore_dup=False, pr=False):
        """Given a batch of transitions, this method computes each transition's
        error and uses that error to update its priority in the replay buffer

        Arguments:
            batch {list} -- list of transitions
            ids {list} -- list of identifiers of each transition in the replay
                        buffer

        Returns:
            float -- The batch's mean loss
        """
        q_predict, q_target, w_batch, _, _ = self._get_training_data(batch)

        errors = np.zeros(len(batch))

        for i, (_, action, reward, _, terminal, _, _) in enumerate(batch):
            target = np.copy(reward)

            if not terminal:
                a_next = np.argmax(np.dot(q_target[i], w_batch[i]))
                target += self.discount * q_target[i][a_next]

            error = mae(q_predict[i][action], target)
            errors[i] = error

            # When dup is True, we train each sample on two weight vectors
            # Hence, there are two TD-errors per sample, we use the mean of
            # both errors to update the priorities
            if self.dup:
                if i % 2 == 0:
                    continue
                error = (error + errors[i - 1]) / 2
            self.buffer.update(ids[i], error)
            self.max_error = max(error, self.max_error)

        return np.mean(errors)

    def save_weights(self):
        """Saves the networks' weights to files identified by the agent's name
        and the current weight vector
        """
        self.critic.model.save_weights("output/networks/{}_dqn.weights".format(self.name))

    def save_model(self):
        """Saves the networks to files identified by the agent's name
        and the current weight vector
        """
        self.critic.model.save("output/networks/{}_dqn_model.h5".format(self.name))

class CN0Agent(CNAgent):
    def build_models(self):
        # Make only critic network (creates target model internally).
        self.critic = Critic0(self.nb_action, self.im_shape, self.nb_objective, self.qvalue_dim,
            timesteps=self.timesteps, net_type=self.net_type, memnn_size=self.memnn_size,
            lr=self.lr, action_conc=self.action_conc, action_embd=self.feature_embd,
            weight_embd=self.feature_embd, backendtype=self.gpu_setting)

        with open("output/logs/info_"+self.extra+".log", "a") as f:
            f.write(time.asctime()+'\n\n')
            f.write(str(self.critic.model.get_config())+'\n\n')
            f.write(str(vars(self))+'\n\n')
        print(vars(self))

    def train_on_batch(self, w_batch, o_batch, a_last_batch, q_true, dummy=None, q_mask=None):
        self.critic.trainable_model.train_on_batch([w_batch, o_batch, a_last_batch, q_true, q_mask], [dummy, q_true])


text:
#!/usr/bin/env python

from __future__ import print_function

import itertools

from sympy import symbols, simplify_logic

TMPL_ADD = '''def {name}({args}):
    if {body}
    else:
        raise Exception('{name}: Unhandled case "{{}}"'.format({args}))'''

def _sym_to_py(expr):
    try:
        if expr.is_Symbol:
            return expr.name
        elif expr.is_Function:
            name = str(expr.func)
            if name == 'And':
                return '(' + ' & '.join(sym_to_py(a) for a in expr.args) + ')'
            elif name == 'Xor':
                return '(' + ' ^ '.join(sym_to_py(a) for a in expr.args) + ')'
            elif name == 'Or':
                return '(' + ' | '.join(sym_to_py(a) for a in expr.args) + ')'
            elif name == 'Not':
                assert len(expr.args) == 1
                return '(~{})'.format(sym_to_py(expr.args[0]))
            else:
                raise Exception('Operator "{}" missing'.format(name))
        else:
            return str(bool(expr))
    except Exception as e:
        print(e)
        import IPython; IPython.embed()

def sym_to_py(expr):
    expr_simp = simplify_logic(expr)
    # Stupid heuristics
    if expr.count_ops() > expr_simp.count_ops():
        expr = expr_simp
    return _sym_to_py(expr).replace('True', '_one').replace('False', '_zero')

def bool_to_cond(b):
    if b is None:
        return 'X'
    elif b:
        return 'I'
    else:
        return 'O'

TMPL_COND = '''{cond}:
        return {stmts}'''

def mk_funk(funk, vars, exprs_tmpls):
    nn = ['ss[{}]'.format(i) for i in range(vars)]
    ii = ['b{}'.format(i) for i in range(vars)]
    ss = symbols(' '.join(ii))

    conds = []
    body = []

    exprs = []
    for e in exprs_tmpls:
        exprs.append(eval(e.format(*nn)))

    for vv in itertools.product((False, True, None), repeat=3):
        s = dict((si, vi if vi is not None else si) for si, vi in zip(ss, vv))
        cond = ' and '.join('{}.{}'.format(n, bool_to_cond(v)) for n, v in zip(ii, vv))
        conds.append(cond)
        body.append(tuple(sym_to_py(e.subs(s)) for e in exprs))

    stmts = [TMPL_COND.format(cond=cond, stmts=', '.join(stmts)) for cond, stmts in zip(conds, body)]
    stmts = '\n    elif '.join(stmts)

    return TMPL_ADD.format(
        name=funk,
        args=', '.join(ii),
        body=stmts,
    )

def main():
    print('Generating the functions...')
    defs = (
        ('_bit_add', 3, ['{0} ^ {1} ^ {2}', '({0} & {1}) | ({2} & ({0} ^ {1}))']),
    )
    funks = []
    for d in defs:
        print('[+] Making "{}"'.format(d[0]))
        funks.append(mk_funk(*d))
    src = 'from ._tbits import _zero, _one\n\n\n' + '\n\n'.join(funks) + '\n'

    print('Writing to file...')
    with open('./tbits/_gen_tables.py', 'w') as f:
        f.write(src)

    print('End of this giant hack :D')

if __name__ == '__main__':
    main()

max_stars_repo_name: thebabush/tbits

max_issues_repo_name: thebabush/tbits

max_forks_repo_name: thebabush/tbits

reason: from sympy


text:
myTestRule {
#Input parameters are:
#  Session variable
#    Session variable without the $ sign
#    all      - output all of the defined variables
#  Output mode flag:
#    server   - log the output to the server log
#    client   - send the output to the client in rError
#    all      - send the output to both client and server
#Output from running the example is:
#  Variables are written to the log file
#Output in irods/server/log/rodsLog.2011.6.1 log file is:
#  msiGetSessionVarValue: userNameClient=rods
   msiGetSessionVarValue(*A,"server");
   writeLine("stdout","Variables are written to the log file");
}
INPUT *A="userNameClient"
OUTPUT ruleExecOut

ext: r

max_stars_repo_name: PlantandFoodResearch/irods

max_issues_repo_name: PlantandFoodResearch/irods

max_forks_repo_name: PlantandFoodResearch/irods


text:
### Add to history() and avoid repeatedly appending suffix.
addhistory <- function(read)
{
  tmp <- as.character(read)
  suffix <- paste0(" # ", getval(prompt))
  if (!grepl(x = tmp, pattern = paste0(suffix, "$"), perl = TRUE))
    tmp <- paste0(tmp, suffix)

  if (isWindows() && isRStudio())
  {
    ### This adds history to native R but is insufficient for Rtudio IDE.
    # .rs.registerReplaceHook("timestamp", "utils", function(original, ...)
    # {
    #   invisible(.Call("rs_timestamp", "bb"))
    # })
    # timestamp()
  }
  else
    utils::timestamp(stamp = tmp, prefix = "", suffix = "", quiet = TRUE)

  invisible()
}

max_stars_repo_name: cran/remoter

max_issues_repo_name: cran/remoter

max_forks_repo_name: cran/remoter


text:
## -------->>  [[file:../nstandr.src.org::*inset_formals][inset_formals:1]]
inset_formals <- function(formals_args, new_args) {
    checkmate::assert_subset(names(new_args)
                           , choices = names(formals_args)
                           , empty.ok = FALSE)
    for (i in 1:length(new_args)) {
        formals_args[names(new_args)[i]] <- new_args[i]
    }
    return(formals_args)
}

make_alias <- function(fun, ...) {
    .fun <- fun
    ## remove 1st and 2nd elemnts ('call' name and 'fun' arg)
    args <- as.list(sys.call())[-(1:2)]
    checkmate::assert_subset(names(args)
                           , choices = names(formals(.fun))
                           , empty.ok = FALSE)
    formals(.fun)[names(args)] <- args
    return(.fun)
}
## --------<<  inset_formals:1 ends here

max_stars_repo_name: stasvlasov/nstandr

max_issues_repo_name: stasvlasov/nstandr

max_forks_repo_name: stasvlasov/nstandr


text:
rm(list = ls())

gene_posSite<- function(input_file, output_file){
  bed_file = read.table(input_file, sep = '\t')
  posSite = bed_file[,-c(3,5)]
  posSite[,2] = posSite[,2] + 10
  colnames(posSite) = c('chr', 'coordinate', 'strand', 'IPD_ratio', 'methy_level', 'low_95', 'up_95')
  write.csv(file = output_file, posSite, row.names = F, quote = F)
}

# for D. melanogaster
gene_posSite(input_file = '825_m6A_25x_novar_dev03.bed', output_file = 'm6A_dm_pos.csv')

# for A. thaliana
gene_posSite(input_file = 'm6A_TAIR10.bed', output_file = 'm6A_ara_pos.csv')

# for A. thaliana
gene_posSite(input_file = 'm6A_ecoli.bed', output_file = 'm6A_ecoli_pos.csv')


text:
# Assignment: ASSIGNMENT 7
# Name: Angel, Daniel
# Date: 2021-08-01

## Set the working directory to the root of your DSC 520 directory
setwd("C:/Users/Danny/Documents")

## Load the `data/r4ds/heights.csv` to
heights_df <- read.csv("data/r4ds/heights.csv")

# Fit a linear model
earn_lm <-  lm(earn ~ ed + race + height + age + sex, data = heights_df)

# View the summary of your model
summary(earn_lm)

predicted_df <- data.frame(
  earn = predict(earn_lm, heights_df),
  ed=heights_df$ed, race=heights_df$race, height=heights_df$height,
  age=heights_df$age, sex=heights_df$sex
)

## Compute deviation (i.e. residuals)
mean_earn <- mean(heights_df$earn)
mean_earn
## Corrected Sum of Squares Total
sst <- sum((mean_earn - heights_df$earn)^2)
sst
## Corrected Sum of Squares for Model
ssm <- sum((mean_earn - predicted_df$earn)^2)
ssm
## Residuals
residuals <- heights_df$earn - predicted_df$earn
## Sum of Squares for Error
sse <- sum(residuals^2)
sse
## R Squared
r_squared <- ssm / sst
r_squared
## Number of observations
n <- nrow(heights_df)
n
## Number of regression parameters
p <- 8
p
## Corrected Degrees of Freedom for Model
dfm <- (p - 1)
dfm
## Degrees of Freedom for Error
dfe <- (n - p)
dfe
## Corrected Degrees of Freedom Total:   DFT = n - 1
dft <- (n - 1)
dft
## Mean of Squares for Model:   MSM = SSM / DFM
msm <- ssm / dfm
msm
## Mean of Squares for Error:   MSE = SSE / DFE
mse <- sse / dfe
mse
## Mean of Squares Total:   MST = SST / DFT
mst <- sst / dft
mst
## F Statistic
f_score <- msm / mse
f_score
## Adjusted R Squared R2 = 1 - (1 - R2)(n - 1) / (n - p)
adjusted_r_squared <- 1 - ((1-r_squared)*(n-1)/(n - p))
adjusted_r_squared


text:
## Example

## install.packages(c("moments"))
setwd("~/GitHub/SpatialStress")
source("R/functions.r")

# creating an initial landscape object

initial <- init_landscape(states = c("+","0","-"), cover = c(0.4,0.1,0.5))

plot(initial)
plot(initial, cols = c("darkgreen", "grey70", "white"))

summary(initial)

# change size of lattice

initial <- init_landscape(states = c("+","0","-"), cover = c(0.4,0.1,0.5), width = 25, height = 25)

plot(initial, cols = c("darkgreen", "grey70", "white"))

summary(initial)

# run simulation

parms_grazing <- list(
  del = 0.9,
  b = 0.3,
  c_ = 0.2,
  m0 = 0.05,
  g = 0.1,
  r = 0.01,
  f = 0.9,
  d = 0.2,
  protect = 0.9
)

grazingrun <- ca(initial, parms_grazing, model = grazing)

plot(grazingrun)

## specify what to plot
plot(grazingrun, plotstates = c(TRUE, TRUE, TRUE), cols = c("darkgreen", "grey50", "grey80"), lwd = 2)

## get plots of the snapshots
par(mfrow = c(2,5), mar = c(0,0,0,0)+0.4)
for(i in 1:9) {
  plot(grazingrun$timeseries[[i]])
}

dev.off()
# analyse output

## a simple summary
summary(grazingrun)

## a function that returns  indicators / early warning signs
indicators(grazingrun)

# select model

parms_mussel  <- list(
  r = 0.4, # recolonisation of empty sites dependent on local density
  d = 0.9, # probability of disturbance of occupied sites if at least one disturbed site
  delta = 0.01 # intrinsic disturbance rate
)

musselrun <- ca(initial, parms_mussel, model = musselbed)

## specify what to plot
plot(musselrun, plotstates = c(TRUE, FALSE, TRUE), cols = c("black", "grey50", "grey90"), lwd = 2)

## get plots of the snapshots
par(mfrow = c(2,5), mar = c(0,0,0,0)+0.4)
for(i in 1:9) {
  plot(musselrun$timeseries[[i]])
  mtext(musselrun$timeseries[[i]], outer = TRUE)
}
dev.off()

# simulate gradients

parms_mussel_gradient  <- list(
  r = 0.4, # recolonisation of empty sites dependent on local density
  d = seq(0,1,0.1), # probability of disturbance of occupied sites if at least one disturbed site
  delta = 0.01 # intrinsic disturbance rate
)

# provides parallel backend
library(foreach)
library(doSNOW)

workerlist <- c(rep("localhost", times = 7))

cl <- makeSOCKcluster(workerlist, outfile='out_messages.txt')

registerDoSNOW(cl)

musselgradient <- ca_array(initial, parms_mussel_gradient, model = musselbed)

stopCluster(cl)

musselgradient

plot(`+`~ d, data = musselgradient )

max_stars_repo_name: skefi/SpatialStress

max_issues_repo_name: skefi/SpatialStress

max_forks_repo_name: skefi/SpatialStress


text:
; ******** Source: suite-a.asm
     1                          !cpu 65816
     2
     3                          ; 2017-12-13 J.E. Klasek j+816 AT klasek DOT at
     4
     5
     6
     7                          videoram = $0400
     8                          colorram = $d800
     9
    10                          ;-------------------------------------------------------------------------------
    11                          	*=$07ff
    12  07ff 0108               	!word $0801
    13  0801 0b08               	!word bend
    14  0803 0a00               	!word 10
    15  0805 9e                 	!byte $9e
    16  0806 3230363100         	!text "2061", 0
    17  080b 0000               bend:       !word 0
    18                          ;-------------------------------------------------------------------------------
    19
    20  080d 78                 	sei
    21  080e a917               	lda #$17
    22  0810 8d18d0             	sta $d018
    23  0813 a935               	lda #$35
    24  0815 8501               	sta $01
    25  0817 a97f               	lda #$7f
    26  0819 8d0ddc             	sta $dc0d
    27  081c 8d0ddd             	sta $dd0d
    28  081f ad0ddc             	lda $dc0d
    29  0822 ad0ddd             	lda $dd0d
    30  0825 a200               	ldx #0
    31                          -
    32  0827 a920               	lda #$20
    33  0829 9d0004             	sta videoram,x
    34  082c 9d0005             	sta videoram+$0100,x
    35  082f 9d0006             	sta videoram+$0200,x
    36  0832 9d0007             	sta videoram+$0300,x
    37  0835 a901               	lda #1
    38  0837 9d00d8             	sta colorram,x
    39  083a 9d00d9             	sta colorram+$0100,x
    40  083d 9d00da             	sta colorram+$0200,x
    41  0840 9d00db             	sta colorram+$0300,x
    42  0843 e8                 	inx
    43  0844 d0e1               	bne -
    44
    45  0846 4c0010             	jmp start
    46                          theend:
    47  0849 e230               	sep #$30                        ; 8-bit for X/Y and A/M
    48                          	!as
    49                          	!rs
    50  084b af100204           	lda $040210
    51  084f c9ff               	cmp #$ff
    52  0851 d00d               	bne error
    53
    54  0853 a905                      	lda #5
    55  0855 8d20d0             	sta $d020
    56  0858 a200                      	ldx #0 ; success
    57  085a 8effd7             	stx $d7ff
    58  085d 4c5d08             	jmp *
    59                          error
    60  0860 8d0004             	sta $0400
    61  0863 af110204           	lda $040211			; failure map (which test failed)
    62  0867 8d0104             	sta $0401
    63  086a a90a               	lda #10
    64  086c 8d20d0             	sta $d020
    65  086f a2ff               	ldx #$ff ; failure
    66  0871 8effd7             	stx $d7ff
    67  0874 4c7408             	jmp *
    68
    69                          ;-------------------------------------------------------------------------------
    70
    71                          	* = $1000
    72                          start:
    73                          ; EXPECTED FINAL RESULTS: $0210 = FF
    74                          ; (any other number will be the
    75                          ;  test that failed)
    76
    77                          ; initialize:
    78  1000 a900               	lda #$00
    79  1002 8f100204           	sta $040210
    80  1006 8f110204           	sta $040211
    81
    82
    83                          test00:
    84
    85                          ; setup cpu
    86  100a 18                 	clc
    87  100b fb                 	xce                             ; native mode
    88  100c c230               	rep #$30                        ; 16-bit for X/Y and A/M
    89                          	!al
    90                          	!rl
    91
    92                          ; setup registers
    93  100e a90404             	lda #$0404                      ; Data Bank Register register
    94  1011 48                 	pha                             ; akku in 16 bit
    95  1012 ab                 	plb                             ; pull DBR twice
    96  1013 ab                 	plb
    97  1014 a08888             	ldy #$8888                      ; change marker
    98  1017 bb                 	tyx
    99  1018 98                 	tya
   100
   101                          ; setup memory
   102  1019 a95555             	lda #$5555                      ; wrap marker
   103  101c 8f878804           	sta $048887                     ; into bank 4, for LDX/LDY
   104  1020 a97777             	lda #$7777                      ; no-wrap marker
   105  1023 8f878805           	sta $058887			; into bank 5, for LDX/LDY
   106
   107                          ;---------------------------------------------------------------------
   108
   109  1027 9c0000             	stz $0000			; init wrap marker
   110  102a a97777             	lda #$7777                      ; no-wrap marker
   111  102d 8f000005           	sta $050000                     ; to start of bank 5
   112
   113  1031 8cffff             	sty $ffff                       ; high byte of Y is where?
   114  1034 ad0000             	lda $0000
   115  1037 d011               	bne +
   116  1039 adffff             	lda $ffff			; fetch, does not wrap
   117  103c c98888             	cmp #$8888
   118  103f d009               	bne +
   119  1041 af000005           	lda $050000
   120  1045 c98877             	cmp #$7788			; write to bank 5
   121  1048 f004               	beq ++
   122  104a ee1002             +	inc $0210			; fail counter
   123  104d 18                 	clc
   124                          ++
   125  104e 2e1102             	rol $0211			; update failure map
   126                          ;---------------------------------------------------------------------
   127
   128  1051 9c0000             	stz $0000			; init wrap marker
   129  1054 a97777             	lda #$7777                      ; no-wrap marker
   130  1057 8f000005           	sta $050000                     ; to start of bank 5
   131
   132  105b bb                 	tyx                             ; change marker
   133  105c 8effff             	stx $ffff                       ; high byte of Y is where?
   134  105f ad0000             	lda $0000
   135  1062 d011               	bne +
   136  1064 adffff             	lda $ffff			; fetch, does not wrap
   137  1067 c98888             	cmp #$8888
   138  106a d009               	bne +
   139  106c af000005           	lda $050000
   140  1070 c98877             	cmp #$7788			; write to bank 5
   141  1073 f004               	beq ++
   142  1075 ee1002             +	inc $0210			; fail counter
   143  1078 18                 	clc
   144                          ++
   145  1079 2e1102             	rol $0211			; update failure map
   146                          ;---------------------------------------------------------------------
   147
   148  107c bcffff             	ldy $ffff,x     ; Y=5555  Y=7777  value for Y comes from which bank?
   149  107f c07777             	cpy #$7777
   150  1082 f004               	beq +
   151  1084 ee1002             	inc $0210			; fail counter
   152  1087 18                 	clc
   153                          +
   154  1088 2e1102             	rol $0211			; update failure map
   155                          ;---------------------------------------------------------------------
   156
   157  108b 9b                 	txy				; reinitialize y
   158  108c beffff             	ldx $ffff,y     ; X=5555  X=7777  value for X comes from which bank?
   159  108f e07777             	cpx #$7777
   160  1092 f004               	beq +
   161  1094 ee1002             	inc $0210			; fail counter
   162  1097 18                 	clc
   163                          +
   164  1098 2e1102             	rol $0211			; update failure map
   165                          ;---------------------------------------------------------------------
   166
   167  109b 9c0000             	stz $0000			; init wrap marker
   168  109e a97777             	lda #$7777                      ; no-wrap marker
   169  10a1 8f000005           	sta $050000                     ; to start of bank 5
   170
   171  10a5 a98877             	lda #$7788
   172  10a8 ee0000             	inc $0000			; $0000 = 1
   173  10ab 1cffff             	trb $ffff                       ; 88 77 & ^(88 77) -> 00 00
   174  10ae ad0000             	lda $0000
   175  10b1 c90100             	cmp #$0001			; $0000 not reset by trb (does not wrap)
   176  10b4 d009               	bne +
   177  10b6 af000005           	lda $050000
   178  10ba c90077             	cmp #$7700			; $050001 reset by trb
   179  10bd f004               	beq ++
   180  10bf ee1002             +	inc $0210			; fail counter
   181  10c2 18                 	clc
   182                          ++
   183  10c3 2e1102             	rol $0211			; update failure map
   184                          ;---------------------------------------------------------------------
   185
   186  10c6 a98877             	lda #$7788
   187  10c9 8f000005           	sta $050000			; 00 88 | 88 77 -> 88 ff
   188  10cd 0cffff             	tsb $ffff                       ; set bits (which are already cleared)
   189  10d0 ad0000             	lda $0000
   190  10d3 c90100             	cmp #$0001			; $0000 not set by tsb (does not wrap!)
   191  10d6 d009               	bne +
   192  10d8 af000005           	lda $050000
   193  10dc c9ff77             	cmp #$77ff			; $050001 all bits set by tsb
   194  10df f004               	beq ++
   195  10e1 ee1002             +	inc $0210			; fail counter
   196  10e4 18                 	clc
   197                          ++
   198  10e5 2e1102             	rol $0211			; update failure map
   199                          ;---------------------------------------------------------------------
   200
   201
   202                          test00pass:
   203  10e8 ad1002             	lda $0210
   204  10eb 49003f             	eor #%0011111100000000		; invert failure map
   205  10ee 8d1002             	sta $0210
   206  10f1 d003               	bne +
   207  10f3 ce1002             	dec $0210			; 0 -> FF
   208                          +
   209
   210  10f6 a90000             	lda #$0000
   211  10f9 48                 	pha
   212  10fa ab                 	plb
   213  10fb ab                 	plb				; program bank = 0
   214  10fc 38                 	sec
   215  10fd fb                 	xce				; emulation mode
   216  10fe e230               	sep #$30			; a/m, x/y 8 bit
   217
   218  1100 4c4908                     jmp theend


text:
	maximize_wrench <- function(H_matrix_input, bounds_tuple_of_numeric, direction) {
		positive_fx_direction_constraint <- a_matrix_lhs_direction(H_matrix_input, direction = direction, bounds_tuple_of_numeric)
	    indices_for_muscles <- 1:ncol(H_matrix_input)
	    fmax_info <- lpsolve_force_in_dir("max", positive_fx_direction_constraint, indices_for_muscles, nrow(H_matrix_input))
	}

max_stars_repo_name: bc/stfeasibility

max_issues_repo_name: bc/stfeasibility

max_forks_repo_name: bc/stfeasibility


text:
#' Convert spatial data files to GeoJSON from various formats.
#'
#' You can use a web interface called Ogre, or do conversions locally using the
#' rgdal package.
#'
#' @export
#' @param input The file being uploaded, path to the file on your machine.
#' @param output Destination for output geojson file. Defaults to current
#' working directory, and gives a random alphanumeric file name
#' @param encoding (character) The encoding passed to
#' \code{\link[rgdal]{readOGR}}.  Default: CP1250
#' @param verbose (logical) Printing of \code{\link[rgdal]{readOGR}} progress.
#' Default: \code{FALSE}
#' @template read
#' @examples \dontrun{
#' file <- system.file("examples", "norway_maple.kml", package = "geojsonio")
#'
#' # KML type file - using the web method
#' file_to_geojson(input=file, method='web', output='kml_web')
#' ## read into memory
#' file_to_geojson(input=file, method='web', output = ":memory:")
#' file_to_geojson(input=file, method='local', output = ":memory:")
#'
#' # KML type file - using the local method
#' file_to_geojson(input=file, method='local', output='kml_local')
#'
#' # Shp type file - using the web method - input is a zipped shp bundle
#' file <- system.file("examples", "bison.zip", package = "geojsonio")
#' file_to_geojson(file, method='web', output='shp_web')
#'
#' # Shp type file - using the local method - input is the actual .shp file
#' file <- system.file("examples", "bison.zip", package = "geojsonio")
#' dir <- tempdir()
#' unzip(file, exdir = dir)
#' list.files(dir)
#' shpfile <- file.path(dir, "bison-Bison_bison-20130704-120856.shp")
#' file_to_geojson(shpfile, method='local', output='shp_local')
#'
#' # Neighborhoods in the US
#' ## beware, this is a long running example
#' # url <- 'http://www.nws.noaa.gov/geodata/catalog/national/data/ci08au12.zip'
#' # out <- file_to_geojson(input=url, method='web', output='cities')
#'
#' # geojson with .json extension
#' ## this doesn't work anymore, hmmm
#' # x <- gsub("\n", "", paste0('https://gist.githubusercontent.com/hunterowens/
#' # 25ea24e198c80c9fbcc7/raw/7fd3efda9009f902b5a991a506cea52db19ba143/
#' # wards2014.json', collapse = ""))
#' # res <- file_to_geojson(x)
#' # jsonlite::fromJSON(res)
#' # res <- file_to_geojson(x, method = "local")
#' # jsonlite::fromJSON(res)
#' }

file_to_geojson <- function(input, method = "web", output = ".", parse = FALSE,
                            encoding = "CP1250", verbose = FALSE, ...) {

  method <- match.arg(method, choices = c("web", "local"))
  if (!inherits(parse, "logical")) stop("parse must be logical", call. = FALSE)

  input <- handle_remote(input)
  mem <- ifelse(output == ":memory:", TRUE, FALSE)

  if (method == "web") {
    url <- "http://ogre.adc4gis.com/convert"
    tt <- httr::POST(url, body = list(upload = httr::upload_file(input)))
    if (tt$status_code > 201) {
      res <- tryCatch(
        httr::content(tt, as = "text", encoding = "UTF-8"),
        error = function(e) e
      )
      if (inherits(res, "error")) httr::stop_for_status(tt)
      res2 <- tryCatch(
        jsonlite::fromJSON(res),
        error = function(e) e
      )
      if (inherits(res2, "error")) httr::stop_for_status(tt)
      stop(paste0(res2[[1]], collapse = "\n"), call. = FALSE)
    }
    out <- httr::content(tt, as = "text", encoding = "UTF-8")
    if (mem) {
      jsonlite::fromJSON(out, parse)
    } else {
      if (output == ".") {
        temp <- tempfile()
        on.exit(unlink(temp))
        output <- basename(temp)
      }
      fileConn <- file(paste0(output, ".geojson"))
      writeLines(out, fileConn)
      close(fileConn)
      file_at(output)
      file_ret(path.expand(output))
    }
  } else {
    fileext <- ftype(input)
    if (output == ":memory:") {
      temp <- tempfile()
      output <- temp
      on.exit(unlink(temp))
      on.exit(unlink(output), add = TRUE)
    } else if (output == ".") {
      temp <- tempfile()
      output <- basename(temp)
      on.exit(unlink(temp))
      on.exit(unlink(output), add = TRUE)
    }

    output <- path.expand(output)
    if (fileext == "kml") {
      my_layer <- rgdal::ogrListLayers(input)
      x <- rgdal::readOGR(input, layer = my_layer[1],
                          drop_unsupported_fields = TRUE,
                          verbose = FALSE, stringsAsFactors = FALSE,
                          encoding = encoding, ...)
      write_ogr2(x, output)
      if (mem) {
        from_json(output, parse)
      } else {
        file_at(output)
        file_ret(output)
      }
    } else if (fileext == "shp") {
      x <- rgdal::readOGR(input, rgdal::ogrListLayers(input),
                          verbose = FALSE, stringsAsFactors = FALSE,
                          encoding = encoding, ...)
      write_ogr2(x, output)
      if (mem) {
        from_json(output, parse)
      } else {
        file_at(output)
        file_ret(output)
      }
    } else if (fileext %in% c("geojson", "json")) {
      unlink(paste0(output, ".geojson"))
      x <- rgdal::readOGR(input, rgdal::ogrListLayers(input),
                          verbose = FALSE, stringsAsFactors = FALSE,
                          encoding = encoding, ...)

      write_ogr2(x, output)
      if (mem) {
        from_json(output, parse)
      } else {
        file_at(output)
        file_ret(output)
      }
    } else {
      stop("only .shp, .kml, .topojson, and url's are supported",
           call. = FALSE)
    }
  }
}

write_ogr2 <- function(x, y) {
  unlink(paste0(y, ".geojson"))
  rgdal::writeOGR(x, paste0(y, ".geojson"), basename(y), driver = "GeoJSON",
                  check_exists = FALSE)
}

file_at <- function(x) {
  message(paste0("Success! File is at ", x, ".geojson"))
}

file_ret <- function(x) {
  invisible(paste0(x, ".geojson"))
}

from_json <- function(x, parse) {
  on.exit(unlink(paste0(x, ".geojson")))
  jsonlite::fromJSON(paste0(x, ".geojson"), parse)
}

ftype <- function(z) {
  if (is.url(z)) {
    z <- httr::parse_url(z)$path
  }
  fileext <- strsplit(z, "\\.")[[1]]
  fileext[length(fileext)]
}

# If given a url for a zip file, download it give back a path to the temporary file
handle_remote <- function(x){
  if (!is.url(x)) {
    return(x)
  } else {
    tfile <- tempfile(fileext = paste0(".", ftype(x)))
    res <- httr::GET(x, httr::write_disk(tfile))
    httr::stop_for_status(res)
    res$request$output$path
  }
}

max_stars_repo_name: pvictor/geojsonio

max_issues_repo_name: pvictor/geojsonio

max_forks_repo_name: pvictor/geojsonio


text:
#' @export
plotArrows = function (Y,X,add=F,colfun=rainbow,startend=T,...) {

  #//trace arrows around x,y points from start to end
  xl = c(min(X),max(X))
  yl = c(min(Y),max(Y))
  if(Y[length(Y)]==0) {Y=Y[-length(Y)]}
  if(length(Y) !=length(X)) {X=X[-length(X)]}
  n = length(Y)
  cl = colfun(n)
  if(add==F) plot(1,1,ylim=yl,xlim=xl,type='n')

  for(i in 1:n) {
    arrows(x0=X[i],x1=X[i+1],y0=Y[i],y1=Y[i+1],angle=45,col=cl[i],...)
  }
  points(X,Y,pch=16,cex=1,col='black')
  if(startend){
    points(X[1],Y[1],pch=17,cex=2,col='black')
    points(X[n],Y[n],pch=17,cex=2,col='black')
  }
}


text:
ver_crassy     = Sys.getenv("CRASSY_VER")
ver_cqlconnect = Sys.getenv("CQLCONNECT_VER")
ver_subprocess = Sys.getenv("SUBPROCESS_VER")

install_github = function(pkg, ver, repo, sep) {
    cat("Installing", pkg, ver, "\n")
    remotes::install_github(file.path(repo, ver, fsep = sep))
}

install_github("crassy", ver_crassy, "AkhilNairAmey/crassy", "@v")
install_github("CQLConnect", ver_cqlconnect, "AkhilNairAmey/CQLConnect", "@v")
install_github("subprocess", ver_subprocess, "lbartnik/subprocess", "@")

max_stars_repo_name: CipherAPJNP/cipher-base

max_issues_repo_name: CipherAPJNP/cipher-base

max_forks_repo_name: CipherAPJNP/cipher-base


text:
context("readFile")

test_that("file separator", {
  expect_equal(readFile("../../tests/shinytest/test-data/passes_everything.csv")$fileSeparator, ",")
})

max_stars_repo_name: cjrace/data-screening-shinyapp

max_issues_repo_name: cjrace/data-screening-shinyapp

max_forks_repo_name: cjrace/data-screening-shinyapp


text:
source("_common.r")
x = readLines("../runs/summit/dgemm/openblas.txt")
df = process(x, gpu=FALSE)

plotter(df, color=nthreads) +
  labs(color="Number of Threads") +
  ggtitle("Square Matrix Product", subtitle="OpenBLAS from R")

ggsave(last_plot(), file="openblas.pdf")

max_stars_repo_name: wrathematics/matprodbench

max_issues_repo_name: wrathematics/matprodbench

max_forks_repo_name: wrathematics/matprodbench


text:
<!DOCTYPE html>

  <meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Make the UpSet plot — UpSet • ComplexHeatmap</title>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha256-916EbMg70RQy9LHiGkXzG8hSg9EdNy97GazNG/aiY1w=" crossorigin="anonymous" />

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.7.1/css/all.min.css" integrity="sha256-nAmazAk6vS34Xqo0BSrTb+abbtFlgsFK7NKSi6o7Y78=" crossorigin="anonymous" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.7.1/css/v4-shims.min.css" integrity="sha256-6qHlizsOWFskGlwVOKuns+D1nB6ssZrHQrNj1wGplHc=" crossorigin="anonymous" />

<link href="../pkgdown.css" rel="stylesheet">

<meta property="og:title" content="Make the UpSet plot — UpSet" />

<meta property="og:description" content="Make the UpSet plot" />
<meta name="twitter:card" content="summary" />

      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false">
        Toggle navigation

      </button>

        ComplexHeatmap
        2.1.0

  Reference

    Articles

      UNKNOWN TITLE

      UNKNOWN TITLE

Make the UpSet plot

UpSet.rd

Make the UpSet plot

UpSet(m,
    comb_col = "black",
    pt_size = unit(3, "mm"), lwd = 2,
    bg_col = "#F0F0F0", bg_pt_col = "#CCCCCC",
    set_order = order(set_size(m), decreasing = TRUE),
    comb_order = if(attr(m, "set_on_rows")) {
    order.comb_mat(m[set_order, ], decreasing = TRUE)
    } else {
    order.comb_mat(m[, set_order], decreasing = TRUE)
    },
    top_annotation = upset_top_annotation(m),
    right_annotation = upset_right_annotation(m),
    row_names_side = "left",
    ...)

Arguments

    <colgroup><col class="name" /><col class="desc" /></colgroup>

      m

A combination matrix returned by make_comb_mat. The matrix can be transposed to switch the position of sets and combination sets.

      comb_col

The color for the dots representing combination sets.

      pt_size

The point size for the dots representing combination sets.

      lwd

The line width for the combination sets.

      bg_col

Color for the background rectangles.

      bg_pt_col

Color for the dots representing the set is not selected.

      set_order

The order of sets.

      comb_order

The order of combination sets.

      top_annotation

A HeatmapAnnotation object on top of the combination matrix.

      right_annotation

A HeatmapAnnotation object on the right of the combination matrix.

      row_names_side

The side of row names.

      ...

Other arguments passed to Heatmap.

Details

By default, the sets are on rows and combination sets are on columns. The positions of the
two types of sets can be switched by transposing the matrix.

When sets are on rows, the default top annotation is the barplot showing the size of each
combination sets and the default right annotation is the barplot showing the size of the sets.
The annotations are simply constructed by HeatmapAnnotation and anno_barplot with some
parameters pre-set. Users can check the source code of upset_top_annotation and
upset_right_annotation to find out how the annotations are defined.

To change or to add annotations, users just need to define a new HeatmapAnnotation object.
E.g. if we want to change the side of the axis and name on top annotation:

    Upset(..., top_annotation =
        HeatmapAnnotation(
           "Intersection size" = anno_barplot(
               comb_size(m),
               border = FALSE,
               gp = gpar(fill = "black"),
               height = unit(2, "cm"),
               axis_param = list(side = "right")
           ),
           annotation_name_side = "right",
           annotation_name_rot = 0)
    )

To add more annotations on top, users just add it in HeatmapAnnotation:

    Upset(..., top_annotation =
        HeatmapAnnotation(
           "Intersection size" = anno_barplot(
               comb_size(m),
               border = FALSE,
               gp = gpar(fill = "black"),
               height = unit(2, "cm"),
               axis_param = list(side = "right")
           ),
           "anno1" = anno_points(...),
           "anno2" = some_vector,
           annotation_name_side = "right",
           annotation_name_rot = 0)
    )

And so is for the right annotations.

UpSet returns a Heatmap-class object, which means, you can add it with other heatmaps and annotations
by + or %v%.

Examples

set.seed(123)
lt = list(a = sample(letters, 10),
          b = sample(letters, 15),
          c = sample(letters, 20))
m = make_comb_mat(lt)
UpSet(m)

UpSet(t(m))

m = make_comb_mat(lt, mode = "union")
UpSet(m)

UpSet(m, comb_col = c(rep(2, 3), rep(3, 3), 1))

# compare two UpSet plots
set.seed(123)
lt1 = list(a = sample(letters, 10),
          b = sample(letters, 15),
          c = sample(letters, 20))
m1 = make_comb_mat(lt1)
set.seed(456)
lt2 = list(a = sample(letters, 10),
          b = sample(letters, 15),
          c = sample(letters, 20))
m2 = make_comb_mat(lt2)

max1 = max(c(set_size(m1), set_size(m2)))
max2 = max(c(comb_size(m1), comb_size(m2)))

UpSet(m1, top_annotation = upset_top_annotation(m1, ylim = c(0, max2)),
    right_annotation = upset_right_annotation(m1, ylim = c(0, max1)),
    column_title = "UpSet1") +
UpSet(m2, top_annotation = upset_top_annotation(m2, ylim = c(0, max2)),
    right_annotation = upset_right_annotation(m2, ylim = c(0, max1)),
    column_title = "UpSet2")

Contents

Arguments

Details

Examples

Developed by Zuguang Gu.

Site built with pkgdown 1.4.1.

ext: rd