text:
# pylint: disable=bad-indentation

"""
Plot distributions and error distributions comparing Retro to Pegleg (and truth).
"""

from __future__ import absolute_import, division, print_function

from os.path import expanduser, expandvars, isdir, join
from os import makedirs

import matplotlib.pyplot as plt
import numpy as np

def interquartile_range(x, lower_percentile=25, upper_percentile=75):
    """Compute the inter-quartile range for values.

    Parameters
    ----------
    x : array-like

    Returns
    -------
    iq_range : float
        Interquartile range

    """
    return np.diff(np.percentile(x, [lower_percentile, upper_percentile]))[0]

def plot_comparison(
    pegleg,
    retro,
    xlim,
    truth=None,
    nbins=102,
    logx=False,
    logy=False,
    xlab=None,
    pl_label='Pegleg',
    retro_label='Retro',
    include_stats=True,
):
    """Plot comparison between pegleg, retro, and possibly truth distributions."""
    fig, ax = plt.subplots(figsize=(8, 3), dpi=120)
    if logx:
        b = np.logspace(np.log10(xlim[0]), np.log10(xlim[1]), nbins)
    else:
        b = np.linspace(xlim[0], xlim[1], nbins)

    if truth is None and include_stats:
        pl_label = (
            '    {}\nmean  {:7.3f}\nmed   {:7.3f}\nIQ50% {:7.3f}'
            .format(pl_label, np.mean(pegleg), np.median(pegleg), interquartile_range(pegleg))
        )
        retro_label = (
            '    {}\nmean  {:7.3f}\nmed   {:7.3f}\nIQ50% {:7.3f}'
            .format(retro_label, np.mean(retro), np.median(retro), interquartile_range(retro))
        )

    if truth is not None:
        _, b, _ = ax.hist(
            truth,
            bins=b,
            histtype='stepfilled',
            lw=2,
            color=(0.7,)*3,
            label='Truth',
        )
    _, b, _ = ax.hist(pegleg, bins=b, histtype='step', lw=2, color='C1', label=pl_label)
    _, b, _ = ax.hist(retro, bins=b, histtype='step', lw=2, color='C0', label=retro_label)

    ylim = ax.get_ylim()
    if truth is None:
        ax.plot([0, 0], ylim, 'k-', lw=0.5)

    ax.set_xlim(xlim)
    ax.set_ylim(ylim)
    if logx:
        ax.set_xscale('log')
    if logy:
        ax.set_yscale('log')
    ax.legend(loc='best', prop={'family': 'monospace'})
    if xlab is not None:
        ax.set_xlabel(xlab)
    fig.tight_layout()
    return fig, ax

def plot_all_distributions(
    evts,
    p_reco="Pegleg_Fit_MN",
    r_reco="retro",
    outdir=None,
    n_xe=50,
    n_ye=50,
    n_ze=50,
    n_te=50,
    n_zenerr=50,
    n_azerr=50,
    n_cscdenerr=50,
    n_trckenerr=50,
    n_enerr=50,
    n_x=50,
    n_y=50,
    n_z=50,
    n_t=50,
    n_zen=50,
    n_az=50,
    n_cscden=50,
    n_trcken=50,
    n_en=50,
    n_ang=50,
):
    """Plot all distributions comparing Pegleg, Retro, and, where appropriate, truth."""
    if outdir is not None:
        outdir = expanduser(expandvars(outdir))
        if not isdir(outdir):
            makedirs(outdir, mode=0o750)

    truth = evts.x
    pegleg = evts['{}_x'.format(p_reco)]
    retro = evts['{}_x'.format(r_reco)]

    fig, ax = plot_comparison(
        pegleg=pegleg - truth,
        retro=retro - truth,
        xlim=(-20, 20),
        nbins=n_xe,
        xlab='reco x - true x (m)',
    )
    if outdir is not None:
        fbp = join(outdir, 'dist_x_error')
        fig.savefig(fbp + '.png', dpi=120)
        fig.savefig(fbp + '.pdf')
    fig, ax = plot_comparison(
        pegleg=pegleg,
        retro=retro,
        truth=truth,
        xlim=(-150, 250),
        nbins=n_x,
        xlab='x (m)',
    )
    if outdir is not None:
        fbp = join(outdir, 'dist_x')
        fig.savefig(fbp + '.png', dpi=120)
        fig.savefig(fbp + '.pdf')

    truth = evts.y
    pegleg = evts['{}_y'.format(p_reco)]
    retro = evts['{}_y'.format(r_reco)]

    fig, ax = plot_comparison(
        pegleg=pegleg - truth,
        retro=retro - truth,
        xlim=(-20, 20),
        nbins=n_ye,
        xlab='reco y - true y (m)',
    )
    if outdir is not None:
        fbp = join(outdir, 'dist_y_error')
        fig.savefig(fbp + '.png', dpi=120)
        fig.savefig(fbp + '.pdf')
    fig, ax = plot_comparison(
        pegleg=pegleg,
        retro=retro,
        truth=truth,
        xlim=(-250, 150),
        nbins=n_y,
        xlab='y (m)',
    )
    if outdir is not None:
        fbp = join(outdir, 'dist_y')
        fig.savefig(fbp + '.png', dpi=120)
        fig.savefig(fbp + '.pdf')

    truth = evts.z
    pegleg = evts['{}_z'.format(p_reco)]
    retro = evts['{}_z'.format(r_reco)]

    fig, ax = plot_comparison(
        pegleg=pegleg - truth,
        retro=retro - truth,
        xlim=(-20, 20),
        nbins=n_ze,
        xlab='reco z - true z (m)',
    )
    if outdir is not None:
        fbp = join(outdir, 'dist_z_error')
        fig.savefig(fbp + '.png', dpi=120)
        fig.savefig(fbp + '.pdf')
    fig, ax = plot_comparison(
        pegleg=pegleg,
        retro=retro,
        truth=truth,
        xlim=(-650, -150),
        nbins=n_z,
        xlab='z (m)',
    )
    if outdir is not None:
        fbp = join(outdir, 'dist_z')
        fig.savefig(fbp + '.png', dpi=120)
        fig.savefig(fbp + '.pdf')

    truth = evts.time
    pegleg = evts['{}_time'.format(p_reco)]
    retro = evts['{}_time'.format(r_reco)]

    fig, ax = plot_comparison(
        pegleg=pegleg - truth,
        retro=retro - truth,
        xlim=(-100, 200),
        nbins=n_te,
        xlab='reco time - true time (ns)',
    )
    if outdir is not None:
        fbp = join(outdir, 'dist_time_error')
        fig.savefig(fbp + '.png', dpi=120)
        fig.savefig(fbp + '.pdf')
    fig, ax = plot_comparison(
        pegleg=pegleg,
        retro=retro,
        truth=truth,
        xlim=(9.4e3, 9.925e3),
        nbins=n_t,
        xlab='time (ns)',
    )
    if outdir is not None:
        fbp = join(outdir, 'dist_time')
        fig.savefig(fbp + '.png', dpi=120)
        fig.savefig(fbp + '.pdf')

    truth = np.arccos(evts.coszen)
    pegleg = evts['{}_track_zenith'.format(p_reco)]
    retro = evts['{}_track_zenith'.format(r_reco)]

    fig, ax = plot_comparison(
        pegleg=pegleg - truth,
        retro=retro - truth,
        xlim=(-.75, .75),
        nbins=n_zenerr,
        xlab='reco zenith - true zenith (rad)',
    )
    if outdir is not None:
        fbp = join(outdir, 'dist_track_zenith_error')
        fig.savefig(fbp + '.png', dpi=120)
        fig.savefig(fbp + '.pdf')
    fig, ax = plot_comparison(
        pegleg=pegleg,
        retro=retro,
        truth=truth,
        xlim=(0, np.pi),
        nbins=n_zen,
        xlab='track zenith (rad)',
    )
    if outdir is not None:
        fbp = join(outdir, 'dist_zenith')
        fig.savefig(fbp + '.png', dpi=120)
        fig.savefig(fbp + '.pdf')

    truth = evts.track_azimuth
    pegleg = evts['{}_track_azimuth'.format(p_reco)]
    retro = evts['{}_track_azimuth'.format(r_reco)]

    fig, ax = plot_comparison(
        pegleg=(pegleg - truth + np.pi) % (2*np.pi) - np.pi,
        retro=(retro - truth + np.pi) % (2*np.pi) - np.pi,
        xlim=(-np.pi/8, np.pi/8),
        nbins=n_azerr,
        xlab='reco track azimuth - true track azimuth (rad)',
    )
    if outdir is not None:
        fbp = join(outdir, 'dist_track_azimuth_error')
        fig.savefig(fbp + '.png', dpi=120)
        fig.savefig(fbp + '.pdf')
    fig, ax = plot_comparison(
        pegleg=pegleg,
        retro=retro,
        truth=truth,
        xlim=(0, 2*np.pi),
        nbins=n_az,
        xlab='track azimuth (rad)',
    )
    #y0, y1 = ax.get_ylim()
    #ax.set_ylim(y1*0.7, y1*.98)
    ax.legend(loc='lower right')
    if outdir is not None:
        fbp = join(outdir, 'dist_track_azimuth')
        fig.savefig(fbp + '.png', dpi=120)
        fig.savefig(fbp + '.pdf')

    p_err = evts['{}_track_angle_error'.format(p_reco)]
    r_err = evts['{}_track_angle_error'.format(r_reco)]

    fig, ax = plot_comparison(
        pegleg=p_err,
        retro=r_err,
        xlim=(0, np.pi),
        nbins=n_ang,
        xlab=r'track angle error (rad)',
    )
    if outdir is not None:
        fbp = join(outdir, 'dist_track_angle_error')
        fig.savefig(fbp + '.png', dpi=120)
        fig.savefig(fbp + '.pdf')

    truth = evts.cascade_energy
    pegleg = evts['{}_cascade_energy'.format(p_reco)]
    retro = evts['{}_cascade_energy'.format(r_reco)]

    fig, ax = plot_comparison(
        pegleg=pegleg - truth,
        retro=retro - truth,
        xlim=(-20, 20),
        nbins=n_cscdenerr,
        xlab='reco cascade energy - true cascade energy (GeV)',
    )
    if outdir is not None:
        fbp = join(outdir, 'dist_cascade_energy_error')
        fig.savefig(fbp + '.png', dpi=120)
        fig.savefig(fbp + '.pdf')
    fig, ax = plot_comparison(
        pegleg=pegleg,
        retro=retro,
        truth=truth,
        xlim=(0.0, 20),
        nbins=n_cscden,
        logx=False,
        xlab='cascade energy (GeV)',
    )
    if outdir is not None:
        fbp = join(outdir, 'dist_cascade_energy')
        fig.savefig(fbp + '.png', dpi=120)
        fig.savefig(fbp + '.pdf')

    truth = evts.track_energy
    pegleg = evts['{}_track_energy'.format(p_reco)]
    retro = evts['{}_track_energy'.format(r_reco)]

    fig, ax = plot_comparison(
        pegleg=pegleg - truth,
        retro=retro - truth,
        xlim=(-30, 40),
        nbins=n_trckenerr,
        xlab='reco track energy - true track energy (GeV)',
    )
    if outdir is not None:
        fbp = join(outdir, 'dist_track_energy_error')
        fig.savefig(fbp + '.png', dpi=120)
        fig.savefig(fbp + '.pdf')
    fig, ax = plot_comparison(
        pegleg=pegleg,
        retro=retro,
        truth=truth,
        xlim=(.5, 500),
        nbins=n_trcken,
        logx=True,
        xlab='track energy (GeV)',
    )
    if outdir is not None:
        fbp = join(outdir, 'dist_track_energy')
        fig.savefig(fbp + '.png', dpi=120)
        fig.savefig(fbp + '.pdf')

    truth = evts.energy
    pegleg = evts['{}_cascade_energy'.format(p_reco)] + evts['{}_track_energy'.format(p_reco)]
    retro = evts['{}_cascade_energy'.format(r_reco)]*2 + evts['{}_track_energy'.format(r_reco)]

    fig, ax = plot_comparison(
        pegleg=pegleg - truth,
        retro=retro - truth,
        xlim=(-40, 40),
        nbins=n_enerr,
        xlab=r'reco $\nu$ energy - true $\nu$ energy (GeV)',
    )
    if outdir is not None:
        fbp = join(outdir, 'dist_neutrino_energy_error')
        fig.savefig(fbp + '.png', dpi=120)
        fig.savefig(fbp + '.pdf')
    fig, ax = plot_comparison(
        pegleg=pegleg,
        retro=retro,
        truth=truth,
        xlim=(1, 500),
        nbins=n_en,
        logx=True,
        xlab=r'$\nu$ energy (GeV)',
    )
    if outdir is not None:
        fbp = join(outdir, 'dist_neutrino_energy')
        fig.savefig(fbp + '.png', dpi=120)
        fig.savefig(fbp + '.pdf')


text:
# Import packages
import os
import argparse
import cv2
import numpy as np
import sys
import importlib.util
import tensorflow as tf
import InputManager
import Object_detection_image
import Object_detection_video
import Object_detection_webcam

# Define and parse input arguments
parser = argparse.ArgumentParser()
parser.add_argument('--source', help='Is image, video or webcam',
                    default='webcam')
parser.add_argument('--modeldir', help='Folder the .tflite file is located in',
                    default='Sample_TFLite_model')
parser.add_argument('--graph', help='Name of the .tflite file, if different than detect.tflite',
                    default='detect.tflite')
parser.add_argument('--labels', help='Name of the labelmap file, if different than labelmap.txt',
                    default='labelmap.txt')
parser.add_argument('--threshold', help='Minimum confidence threshold for displaying detected objects',
                    default=0.5)
parser.add_argument('--video', help='Name of the video file',
                    default=0)
parser.add_argument('--image', help='Name of the image file',
                    default=0)
parser.add_argument('--edgetpu', help='Use Coral Edge TPU Accelerator to speed up detection',
                    action='store_true')

args = parser.parse_args()

MODEL_NAME = args.modeldir
GRAPH_NAME = args.graph
LABELMAP_NAME = args.labels
source = args.source
VIDEO_NAME = args.video
IMAGE_NAME = args.image
min_conf_threshold = float(args.threshold)
use_TPU = args.edgetpu

# 1. Input
# testing data
VIDEO_NAME = "video/test1.mp4"
IMAGE_NAME = "pictures/test1.jpg"

if (source == "video"):
    Object_detection_video.detect(VIDEO_NAME)

elif (source == "image"):
    Object_detection_image.detect(IMAGE_NAME)

elif (source == "webcam"):
    Object_detection_webcam.detect()

ext: py

max_stars_repo_name: PauliusGrigaliunas/PeopleCounter

max_issues_repo_name: PauliusGrigaliunas/PeopleCounter

max_forks_repo_name: PauliusGrigaliunas/PeopleCounter

reason: import numpy


text:
import io
from pathlib import Path

import flask
import numpy as np
import pandas as pd

from .utils import list_dir
from .model import define_model

class ServeConfig:
    OPT_ML_DIR = Path("/opt/ml")
    MODELS_DIR = OPT_ML_DIR / "models"

    ASSETS_PATH = Path("./assets")
    ASSETS_PATH.mkdir(parents=True, exist_ok=True)
    AUDIO_PATH = ASSETS_PATH / "audio"
    AUDIO_PATH.mkdir(parents=True, exist_ok=True)
    META_PATH = ASSETS_PATH / "metadata"
    META_PATH.mkdir(parents=True, exist_ok=True)

class ScoringService(object):
    """
    A singleton for holding the model. This simply loads the model and holds it.
    It has a predict function that does a prediction based on the model and the input data.
    """

    models = None  # Where we keep the model when it's loaded

    @classmethod
    def get_model(cls):
        """Get the model object for this instance, loading it if it's not already loaded."""
        if cls.models == None:
            models = []
            model_list = list_dir(ServeConfig.MODELS_DIR)
            for name in model_list:
                cnn = define_model()
                cnn.load_weights(str(ServeConfig.IN_MODELS_DIR / name))
                models.append(cnn)
            cls.models = models
        return cls.models

    @classmethod
    def predict(cls, input: pd.DataFrame):
        """For the input, do the predictions and return them.

        Args:
            input (a pandas dataframe): The data on which to do the predictions. There will be
                one prediction per row in the dataframe"""
        models = cls.get_model()
        res = np.zeros(input.shape[0], dtype=float)
        input_features = input.values
        for model in models:
            res += model.predict(input_features)

        output = res / len(models)
        return output

app = flask.Flask(__name__)

@app.route("/ping", methods=["GET"])
def ping():
    """Determine if the container is working and healthy. In this sample container, we declare
    it healthy if we can load the model successfully."""
    health = ScoringService.get_model() is not None

    status = 200 if health else 404
    return flask.Response(response="\n", status=status, mimetype="application/json")

@app.route("/invocations", methods=["POST"])
def invocations():
    data = None

    # Convert from CSV to pandas
    if flask.request.content_type == "text/csv":
        data = flask.request.data.decode("utf-8")
        s = io.StringIO(data)
        data = pd.read_csv(s, header=None)
    else:
        return flask.Response(
            response="This predictor only supports CSV data",
            status=415,
            mimetype="text/plain",
        )

    print("Invoked with {} records".format(data.shape[0]))

    # Do the prediction
    predictions = ScoringService.predict(data)

    # Convert from numpy back to CSV
    out = io.StringIO()
    pd.DataFrame({"results": predictions}).to_csv(
        out, header=False, index=False)
    result = out.getvalue()

    return flask.Response(response=result, status=200, mimetype="text/csv")

reason: import numpy,from numpy


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

from virocon import (
    AndContour,
    WeibullDistribution,
    LogNormalDistribution,
    DependenceFunction,
    GlobalHierarchicalModel,
    plot_2D_contour
)

def _power3(x, a=0.1000, b=1.489, c=0.1901):
        return a + b * x ** c

# A 3-parameter exponential function (a dependence function).
def _exp3(x, a=0.0400, b=0.1748, c=-0.2243):
    return a + b * np.exp(c * x)

bounds = [(0, None), (0, None), (None, None)]
power3 = DependenceFunction(_power3, bounds)
exp3 = DependenceFunction(_exp3, bounds)

dist_description_0 = {
    "distribution": WeibullDistribution(alpha=2.776, beta=1.471, gamma=0.8888),
}
dist_description_1 = {
    "distribution": LogNormalDistribution(),
    "conditional_on": 0,
    "parameters": {"mu": power3, "sigma": exp3},
}
model = GlobalHierarchicalModel([dist_description_0, dist_description_1])

alpha = 0.01
n = 10000
sample = model.draw_sample(n)
contour = AndContour(model, alpha, deg_step=1, sample=sample, allowed_error=0.01)
plot_2D_contour(contour, sample=sample, swap_axis=True)
plt.show()

max_stars_repo_name: ahaselsteiner/viroconcom

max_issues_repo_name: virocon-organization/viroconcom

max_forks_repo_name: virocon-organization/viroconcom


text:
import numpy as np
import modin.pandas as pd
from modin.config import Engine
Engine.put("ray")

"""
- https://github.com/fmilthaler/FinQuant/blob/master/finquant/moving_average.py
- https://github.com/modin-project/modin
"""

def sma(data:pd.Series, span:int) -> pd.Series:
    return data.rolling(window=span, center=False).mean()

def ema(data:pd.Series, span:int, alpha:int) -> pd.Series:
    return data.ewm(span=span, adjust=False, min_periods=span, alpha = alpha).mean()

def sma_std(data:pd.Series, span:int) -> pd.Series:
    return data.rolling(window=span, center=False).std()

def ema_std(data:pd.Series, span:int, alpha:int) -> pd.Series:
    return data.ewm(span=span, adjust=False, min_periods=span, alpha = alpha).std()

def macd(data:pd.Series, alpha:int, dif_span1:int = 12, dif_span2:int = 26, dea_span:int = 9) -> pd.Series:
    dif = (ema(data, span = dif_span1, alpha = alpha) - ema(data, span = dif_span2, alpha = alpha))/ data
    dea = ema(dif, span = dea_span, alpha = alpha) / data
    return 2 * (dif - dea)

max_stars_repo_name: LiamSeon/SLM-Lab

max_issues_repo_name: LiamSeon/SLM-Lab

max_forks_repo_name: LiamSeon/SLM-Lab


text:
import gym
import gym_snake
import numpy as np

from tensorflow.keras import backend as K
from tensorflow.keras.utils import to_categorical
from tensorflow.keras import optimizers
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense

class Agent:
    def __init__(self, inp_shape):
        self.inp_shape = inp_shape
        self.model = Sequential()
        self.model.add(Dense(128, activation='relu', input_shape=inp_shape, kernel_initializer='he_normal'))
        self.model.add(Dense(3, activation='softmax'))

        action_prob_placeholder = self.model.output
        action_onehot_placeholder = K.placeholder(shape=(None, 3), name="action_onehot_placeholder")
        discount_reward_placeholder = K.placeholder(shape=(None,), name="discount_reward")

        action_prob = K.sum(action_prob_placeholder * action_onehot_placeholder, axis=1)
        log_action_prob = K.log(action_prob)

        loss = - log_action_prob * discount_reward_placeholder
        loss = K.mean(loss)

        adam = optimizers.Adam(lr=1e-4)

        updates = adam.get_updates(params=self.model.trainable_weights,
                                   loss=loss)

        self.train_fn = K.function(
            inputs=[self.model.input,
                    action_onehot_placeholder,
                    discount_reward_placeholder],
            outputs=[],
            updates=updates
        )

    def action(self, state):
        action_prob = np.squeeze(self.model.predict(state))
        return np.random.choice(np.arange(3), p=action_prob), action_prob

    def discounted_reward(self, R, discount_rate=0.99):
        disc_R = np.zeros_like(R, dtype=np.float32)
        running_add = 0

        for t in reversed(range(len(R))):
            running_add = running_add * discount_rate + R[t]
            disc_R[t] = running_add

        disc_R -= disc_R.mean()
        disc_R /= disc_R.std()

        return disc_R

    def fit(self, S, A, R):
        action_onehot = to_categorical(A, num_classes=3)
        self.train_fn([S, action_onehot, self.discounted_reward(R)])

    def run_episode(self, env, verbose=False, render=False):
        S = []
        A = []
        R = []

        done = False
        s = env.reset()

        reward_total = 0

        while not done:
            if render:
                env.render()
            a, a_prob = self.action(s.reshape((1,) + self.inp_shape))
            if verbose:
                print(f"ACTION PROB: {a_prob} Action: {env.ACTIONS[a]}")
            s2, r, done, _ = env.step(a)

            reward_total += r

            S.append(s.reshape(self.inp_shape))
            A.append(a)
            R.append(r)

            s = s2

            if done:
                self.fit(np.array(S), np.array(A), np.array(R))

        if verbose:
            print(f"TOTAL REWARD: {reward_total} SNAKE LENGTH: {env.snake_size}")
            print("=================================================================")

        return env.snake_size, reward_total

if __name__ == "__main__":
    env = gym.make('snake-v0')
    agent = Agent((484,))

    max_length = 0
    episode = 1

    try:
        while True:
            print(f"EPISODE {episode}:")
            print("==================")
            length, reward = agent.run_episode(env, verbose=True, render=True)
            if length > max_length: max_length = length
            episode += 1
    except KeyboardInterrupt:
        print(f"\nBest score: {max_length}")
        agent.model.save("model.h5")

max_stars_repo_name: AleksaC/gym-snake

max_issues_repo_name: AleksaC/gym-snake

max_forks_repo_name: AleksaC/gym-snake


text:
import numpy as np
import pytest

from prigen.generators import PrimersGenerator
from prigen.utils import (
    filter_primers_by_blast,
    gc_content,
    parse_blast_result,
    remove_keys_from_dict
)

@pytest.fixture
def blast_result():
    return "gene1\tchr7\t100.000\t96\t0\t0\t1\t96\t5529033\t5528938\t4.22e-43\t178\n" + \
           "gene2\tchr17\t100.000\t111\t0\t0\t1\t111\t7686764\t7686654\t2.36e-51\t206\n"

def test_primers_generator_with_wrong_length():
    with pytest.raises(ValueError):
        PrimersGenerator(length=-1, gc_percentage=.5)

    with pytest.raises(ValueError):
        PrimersGenerator(length=0, gc_percentage=.5)

def test_primers_generator_with_wrong_number():
    with pytest.raises(ValueError):
        PrimersGenerator(length=1, gc_percentage=.5, number_of_primers=0)

    with pytest.raises(ValueError):
        PrimersGenerator(length=1, gc_percentage=.5, number_of_primers=-999)

def test_primers_generator_with_wrong_temperature_bounds():
    with pytest.raises(ValueError):
        PrimersGenerator(length=1, gc_percentage=.5, min_temperature=5, max_temperature=3)

    with pytest.raises(ValueError):
        PrimersGenerator(length=1, gc_percentage=.5, min_temperature=5, max_temperature=5)

def test_primers_generator_with_wrong_gc_percentage():
    for percent in np.linspace(-2, -0.001, 30):
        with pytest.raises(ValueError):
            PrimersGenerator(length=1, gc_percentage=percent)

    for percent in np.linspace(1.001, 10, 30):
        with pytest.raises(ValueError):
            PrimersGenerator(length=1, gc_percentage=percent)

def test_number_of_generated_primers():
    for n_primers in range(1, 20):
        generator = PrimersGenerator(
            length=20,
            number_of_primers=n_primers,
            gc_percentage=.5
        )
        primers = generator.generate_primers()

        assert len(primers) <= n_primers

def test_length_of_generated_primers():
    for length in range(4, 20):
        generator = PrimersGenerator(
            length=length,
            number_of_primers=20,
            gc_percentage=.5
        )
        primers = generator.generate_primers()

        assert all(len(primer) == length for primer in primers)

def test_primers_generator_with_any_temperature():
    for length in range(2, 50):
        for gc_percentage in np.linspace(0, 1, 30):
            generator = PrimersGenerator(
                length=length,
                number_of_primers=20,
                gc_percentage=gc_percentage
            )
            primers = generator.generate_primers()

            for primer in primers:
                assert abs(gc_content(primer) - gc_percentage) < 1/length

def test_primers_generator_with_temperature_bounds():
    min_temperature, max_temperature = 30, 80

    for gc_percentage in np.linspace(0, 1, 20):
        generator = PrimersGenerator(
            length=20,
            number_of_primers=20,
            gc_percentage=gc_percentage,
            min_temperature=min_temperature,
            max_temperature=max_temperature
        )
        primers = generator.generate_primers()

        for temperature in primers.values():
            assert min_temperature <= temperature <= max_temperature

def test_gc_content():
    assert gc_content("A") == 0

    length = 20

    for n_g in range(10):
        for n_c in range(10):
            primer = "G" * n_g + "C" * n_c + "A" * (length - n_c - n_g)

            assert gc_content(primer) == (n_c + n_g) / length

    primer = "GCGCGCATGCAGTACGA"

    assert gc_content(primer) == gc_content(primer.lower())

def test_parse_blast_result(blast_result):
    assert parse_blast_result(blast_result) == {"gene1", "gene2"}

def test_parse_empty_blast_result():
    assert parse_blast_result("") == set()

def test_remove_keys_from_dict():
    d = {"a": 1, "b": 2, "c": 3}

    assert remove_keys_from_dict(d, ["b"]) == {"a": 1, "c": 3}
    assert remove_keys_from_dict(d, ["not_existing"]) == d
    assert remove_keys_from_dict(d, ["a", "not_existing"]) == {"b": 2, "c": 3}
    assert remove_keys_from_dict(d, d.keys()) == {}
    assert remove_keys_from_dict({}, ["a", "b", "c"]) == {}

def test_filter_primers_by_blast():
    # Nucleotide sequences from human genome
    primers = {
        "TTTGGAACTCTGCAGGTTCTATTTGCTTTTTCCCAGATGA": 63.99,
        "GGTTTGCCCGCCAGGCCGAGGAGGACCGTCGCAATCTGAG": 74.74
    }

    assert filter_primers_by_blast(primers, remote=True) == {}

max_stars_repo_name: VladimirShitov/prigen

max_issues_repo_name: VladimirShitov/prigen

max_forks_repo_name: VladimirShitov/prigen


text:
# This file serves creates an example netCDF file to demonstrate the tools
# for reading and writing parameter scan data.

from exceptions import *
from dakota_file import DakotaFile
import numpy as np
import xarray as xr
import main
import os

# First create an instance of the dakota_netcdf class!
my_netcdf = DakotaFile()

# Unlike the Monte-Carlo sampling case we do not need any run settings
# so the add_settings function should not be called

# We need to add some parameter scan variables

# ------------------------------------------------------------------------
# WRITE EXAMPLE 1
# ------------------------------------------------------------------------

# The simplest way is to build a dictionary of needed variables and call
# the add_variable_from_dict function.

# Performing a parameter scan requires 3 variables. Upper and lower bounds
# and the number of partitions.

# Give our variable a name!
name = 'test_scan1'

# Specify that the variable should be scanned
var_type = 'scan'

# Make a dictionary of the needed data
lower_bounds  = np.random.rand(4,3)
upper_bounds  = np.random.rand(4,3)
partitions    = np.random.randint(2,10,size=(4,3))

variable_dict = { 'lower_bounds' : lower_bounds, 'upper_bounds' : upper_bounds,
                  'partitions' : partitions }

my_netcdf.add_variable_from_dict( name, var_type, variable_dict )

# -----------------------------------------------------------------------
# WRITE EXAMPLE 2
# -----------------------------------------------------------------------

# We can also add a variable by constructing an Xarray dataset
# To do this the data must be created as data arrays and then added to the dataset

# Create a name for this new variable
name = 'test_scan2'

var_type = 'scan'

lower_bounds1 = xr.DataArray(lower_bounds)
upper_bounds1 = xr.DataArray(upper_bounds)
partitions1   = xr.DataArray(partitions)

dataset = xr.Dataset( {'lower_bounds' : lower_bounds1, 'upper_bounds' : upper_bounds1,
                       'partitions' : partitions1 } )

my_netcdf.add_variable_from_dataset( name, var_type, dataset )

# ----------------------------------------------------------------------
# WRITE EXAMPLE 3
# ----------------------------------------------------------------------

# One of the advantages of using a dataset is that one can also specify the dimensions
# and coordinates of variables. It may be convenient to have these available in the
# interface script.

# Create a name for this new variable
name = 'test_scan3'

var_type = 'scan'

# Coordinates
x = np.array( [0.2,0.3,0.4,0.5,0.6] )
y = np.array( [2.5,3.5,4.5] )
z = np.array( [5.2,5.3] )

# Convert coordinates to data arrays
x = xr.DataArray(x,dims=['x'])
y = xr.DataArray(y,dims=['y'])
z = xr.DataArray(z,dims=['z'])

# Construct variable data arrays which include dimensions and coordinates
lower_bounds = np.random.rand(5,3,2)
lower_bounds = xr.DataArray(lower_bounds, coords=[x,y,z], dims=['x','y','z'])

upper_bounds = np.random.rand(5,3,2) + 2.0
upper_bounds = xr.DataArray(upper_bounds, coords=[x,y,z], dims=['x','y','z'])

partitions   = np.random.randint(2,10,size=(5,3,2))
partitions   = xr.DataArray(partitions,   coords=[x,y,z], dims=['x','y','z'])

dataset = xr.Dataset( {'lower_bounds':lower_bounds, 'upper_bounds':upper_bounds,
                       'partitions':partitions}, attrs={'type':'scan'} )

my_netcdf.add_variable_from_dataset( name, var_type, dataset )

# -----------------------------------------------------------------------
# WRITE EXAMPLE 4
# -----------------------------------------------------------------------

# We can also add a variable by constructing an Xarray dataset directly from numpy
# arrays if coordinate information is not required

# Create a name for this new variable
name = 'test_scan4'

var_type = 'scan'

lower_bounds = 0.0
upper_bounds = 1.0
partitions   = 7

dataset = xr.Dataset( {'lower_bounds' : lower_bounds, 'upper_bounds' : upper_bounds,
                       'partitions' : partitions } )

my_netcdf.add_variable_from_dataset( name, var_type, dataset )

# ----------------------------------------------------------------------
# WRITE THE FILE
# ----------------------------------------------------------------------

filename = 'DAKOTA.nc'
my_netcdf.write(filename)

#=======================================================================
# READING EXAMPLES
#=======================================================================

# To build an interface between DAKOTA and your code you will need to write something
# to read a netcdf file like the one just created and pass the new data to your code.
# The following examples demonstrate reading a netcdf.

# Create a new instance of the
my_netcdf2 = DakotaFile()

filename = 'DAKOTA.nc'
my_netcdf2.read(filename)

# ----------------------------------------------------------------------
# READ EXAMPLE 1
# ----------------------------------------------------------------------

# We can read one of the variables in the netcdf file back into a dictionary of data
# We shall read test_scan2 which was originally written as a dataset

variable_dict = my_netcdf2.get_variable_as_dict('test_scan2')

print('Read Example 1:')
print()

for key, var in variable_dict.items():

    print( key+':' )
    print( var )
    print()

# After running DAKOTA the varied data for each variable should be available as the
# 'values' entry of that variable's dictionary: variable_dict['values']

# ----------------------------------------------------------------------
# READ EXAMPLE 2
# ----------------------------------------------------------------------

# We can also read a variable in the netcdf file back into a dataset
# If the coordinates were also written to the data arrays or the dataset itself
# these will also be available

dataset = my_netcdf2.get_variable_as_dataset('test_scan3')

print('Read Example 2:')
print()

for key in dataset:

    print( key+':' )
    print( dataset[key].dims )
    print( dataset[key].coords )
    print( dataset[key].data )
    print()

# After running DAKOTA the varied data for each variables should be available within the
# 'values' data array of the variables dataset: dataset['values'].data


text:
import numpy as np

from dnnv.verifiers.common.base import Parameter, Verifier
from dnnv.verifiers.common.reductions import IOPolytopeReduction, HalfspacePolytope
from dnnv.verifiers.common.results import SAT, UNSAT, UNKNOWN
from dnnv.verifiers.common.utils import as_layers
from functools import partial

from .errors import PlanetError, PlanetTranslatorError
from .layers import PLANET_LAYER_TYPES
from .utils import to_rlv_file

class Planet(Verifier):
    reduction = partial(IOPolytopeReduction, HalfspacePolytope, HalfspacePolytope)
    translator_error = PlanetTranslatorError
    verifier_error = PlanetError

    def build_inputs(self, prop):
        if prop.input_constraint.num_variables > 1:
            raise ReluplexTranslatorError(
                "Unsupported network: More than 1 input variable"
            )
        layers = as_layers(
            prop.suffixed_op_graph(),
            extra_layer_types=PLANET_LAYER_TYPES,
            translator_error=self.translator_error,
        )
        rlv_file_name = to_rlv_file(
            prop.input_constraint,
            layers,
            # dirname=dirname,
            translator_error=self.translator_error,
        )
        return "planet", rlv_file_name

    def parse_results(self, prop, results):
        stdout, stderr = results
        if len(stdout) == 0:
            raise self.verifier_error(f"Running planet produced no output.")
        if stdout[-1] == "SAT":
            shape, dtype = prop.op_graph.input_details[0]
            cex = np.zeros((1,) + shape[1:], dtype)
            found = False
            for line in stdout:
                if line.startswith("SAT"):
                    found = True
                if found and line.startswith("- input"):
                    position = tuple(int(i) for i in line.split(":")[1:-1])
                    value = float(line.split()[-1])
                    cex[position] = value
            return SAT, cex.reshape(shape)
        elif stdout[-1] == "UNSAT":
            return UNSAT, None
        raise self.verifier_error(f"Unexpected verification result: {stdout[-1]}")

    def validate_counter_example(self, prop, cex):
        # planet output has a precision of 5 decimal places
        is_valid, err_msg = prop.validate_counter_example(cex, threshold=1e-5)
        if not is_valid:
            raise self.verifier_error(err_msg)
        return is_valid


text:
import copy
import numpy as np
from pyflowline.formats.convert_coordinates import convert_gcs_coordinates_to_flowline
from pyflowline.algorithms.auxiliary.find_index_in_list import check_if_duplicates

def remove_returning_flowline(iMesh_type_in, aCell_intersect_in, pVertex_outlet_in):
    aFlowline_out=list()
    nCell =  len(aCell_intersect_in)
    def simplify_list(aCell_flowline_in):
        aCell_flowline_out = copy.deepcopy(aCell_flowline_in)
        nCell2 = len(aCell_flowline_out)
        iFlag_unique = check_if_duplicates(aCell_flowline_out)
        if iFlag_unique == 1:
            return aCell_flowline_out
        else:
            for i in range(nCell2):
                elem=aCell_flowline_out[i]
                if aCell_flowline_out.count(elem) > 1:
                    dummy = [i for i, x in enumerate(aCell_flowline_out) if x == elem]
                    start = dummy[0]
                    end = dummy[-1]
                    del aCell_flowline_out[start: end]
                    break
                else:
                    pass

                pass

            aCell_flowline_out = simplify_list(aCell_flowline_out)
        return   aCell_flowline_out

    def retrieve_flowline_intersect_index(iSegment_in, iStream_order_in, pVertex_end_in):
        iFlag_found = 1
        pVertex_end_current = pVertex_end_in
        aCell_flowline=list()
        aSegment_upstream = list()
        aVertex_end_upstream = list()
        aStream_order = list()
        iFlag_skip = 0
        iFlag_previous_overlap=0
        while iFlag_found == 1:
            iFlag_found = 0
            for j in range(nCell):
                pCell = aCell_intersect_in[j]
                lCellID = pCell.lCellID
                aFlowline= pCell.aFlowline
                nFlowline = len(aFlowline)
                for i in range(nFlowline):
                    pFlowline = aFlowline[i]
                    pVertex_start = pFlowline.pVertex_start
                    pVertex_end = pFlowline.pVertex_end
                    iStream_segment = pFlowline.iStream_segment
                    iStream_order = pFlowline.iStream_order
                    if pVertex_end == pVertex_end_current:
                        if iStream_segment == iSegment_in:
                            dLength = pFlowline.dLength
                            iFlag_found2, dummy2 = pCell.which_edge_cross_this_vertex(pVertex_start)
                            iFlag_found3, dummy3 = pCell.which_edge_cross_this_vertex(pVertex_end)

                            if iFlag_found2 == 1 and iFlag_found3 == 1: #on the edge
                                #same edge
                                if dummy2.is_overlap(dummy3) ==1:
                                    pVertex_end_current = pVertex_start
                                    aCell_flowline.append(lCellID)
                                    #print(lCellID)
                                    iFlag_found = 1
                                    iFlag_previous_overlap =1

                                    pass
                                else:
                                    if dLength < (0.1*pCell.dLength) : #because it taks a short cut
                                        pVertex_end_current = pVertex_start
                                        iFlag_found = 1
                                        if iFlag_previous_overlap ==1:
                                            aCell_flowline.append(lCellID)
                                            #print(lCellID)
                                            iFlag_previous_overlap=0
                                            pass
                                        else:
                                            iFlag_previous_overlap=1
                                            pass
                                        pass
                                    else:
                                        pVertex_end_current = pVertex_start
                                        aCell_flowline.append(lCellID)
                                        #print(lCellID)
                                        iFlag_found = 1
                                        pass

                                    pass

                                pass

                            else:
                                pVertex_end_current = pVertex_start
                                aCell_flowline.append(lCellID)
                                iFlag_found = 1
                                iFlag_previous_overlap=0
                                pass

                            break
                            pass
                        else:
                            iFlag_found2, dummy2 = pCell.which_edge_cross_this_vertex(pVertex_start)
                            iFlag_found3, dummy3 = pCell.which_edge_cross_this_vertex(pVertex_end)
                            if pVertex_end == pVertex_end_in:
                                iFlag_found = 1
                                pass
                            else:
                                #this is a new upstream segment location
                                iFlag_found = 0
                                #save the upstream information for new step
                                aSegment_upstream.append(iStream_segment)
                                aVertex_end_upstream.append(pVertex_end)
                                aStream_order.append(iStream_order)

                            pass
                    pass

                if iFlag_found == 1:
                    break

        #should have finished search
        if iFlag_found == 0:
            #reverse
            aCell_flowline = aCell_flowline[::-1]
            #simplify list
            aCell_simple = simplify_list(aCell_flowline)
            #save the output
            nCell3 = len(aCell_simple)
            aCoordinates = list()
            if nCell3 >1:
                for i in range(nCell3):
                    lCellID = aCell_simple[i]
                    for j in range( nCell):
                        if aCell_intersect_in[j].lCellID == lCellID:
                            x = aCell_intersect_in[j].dLongitude_center_degree
                            y = aCell_intersect_in[j].dLatitude_center_degree
                            aCoordinates.append([x,y])
                    pass
                pFlowline = convert_gcs_coordinates_to_flowline(aCoordinates)
                pFlowline.iStream_segment = iSegment_in
                pFlowline.iStream_order=iStream_order_in
                aFlowline_out.append(pFlowline)

            sort_index = np.argsort(aStream_order)
            sort_index = sort_index[::-1]
            nUpstream = len(aSegment_upstream)
            for i in sort_index:
                retrieve_flowline_intersect_index(aSegment_upstream[i], aStream_order[i], aVertex_end_upstream[i])
                pass
            pass

        return

    #starting from the outlet
    dDiatance_min=0.0
    iFlag_first=1
    for j in range(nCell):
        pCell = aCell_intersect_in[j]
        aFlowline= pCell.aFlowline
        nFlowline = len(aFlowline)
        for i in range(nFlowline):
            pFlowline = aFlowline[i]
            pVertex_start = pFlowline.pVertex_start
            pVertex_end = pFlowline.pVertex_end
            dDiatance = pVertex_end.calculate_distance( pVertex_outlet_in)
            if iFlag_first ==1:
                dDiatance_min = dDiatance
                lCellID_outlet = pCell.lCellID
                lCellID_outlet2 = j
                lCellID_outlet3 = i
                iFlag_first=0
            else:
                if  dDiatance < dDiatance_min:
                    dDiatance_min = dDiatance
                    lCellID_outlet = pCell.lCellID
                    lCellID_outlet2 = j
                    lCellID_outlet3 = i
                    pass
                else:
                    pass

            pass

    #loop through
    pFlowline = aCell_intersect_in[lCellID_outlet2].aFlowline[lCellID_outlet3]
    iStream_segment =pFlowline.iStream_segment
    iStream_order=pFlowline.iStream_order
    pVertex_outlet=pFlowline.pVertex_end

    retrieve_flowline_intersect_index(iStream_segment, iStream_order, pVertex_outlet)

    return aFlowline_out, lCellID_outlet, pVertex_outlet


text:
from keras.layers.core import TimeDistributedDense, Merge
from keras.layers.recurrent import LSTM, GRU
from keras.layers.advanced_activations import PReLU, LeakyReLU
from keras.preprocessing import sequence
from keras.optimizers import RMSprop
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten, Reshape, Permute, AutoEncoder
from keras.layers.embeddings import Embedding
from keras.layers.convolutional import Convolution1D, MaxPooling1D
from keras.layers import containers
from utils import *
import wave, struct, time
import numpy as np
import os
import scipy.io.wavfile as wav
import scipy.fftpack as fft

audioData = openWavFile('4-samples-sapo.wav')
phaseData, meanPhiWav, stdPhiWav = extractWaveData(audioData, 8, blkCount=800, returnObj="phase", olapf=4)
magntData, maxMagWav, minMagWav = extractWaveData(audioData, 8, blkCount=800, returnObj="magnitude", olapf=4)
#mfccData, meanMfcc, stdMfcc = extractWaveData(audioData, 2, blkCount=200, returnObj="mfcc")

px = phaseData.shape[0]
py = phaseData.shape[1]
pz = phaseData.shape[2]

mx = magntData.shape[0]
my = magntData.shape[1]
mz = magntData.shape[2]

print phaseData.shape
print magntData.shape

kMagnData = np.copy(magntData)
for encInd in range(0, 5):
    encMag = Sequential()
    midlay = encInd * 200
    outlay = (encInd + 1) * 200
    encoder = containers.Sequential([TimeDistributedDense(input_dim=pz - midlay, output_dim=pz - outlay, activation='tanh')])
    decoder = containers.Sequential([TimeDistributedDense(input_dim=pz - outlay, output_dim=pz - midlay, activation='linear')])
    encMag.add(Dropout(p=0.1, input_shape=(py, pz - midlay)))
    encMag.add(AutoEncoder(encoder=encoder, decoder=decoder, output_reconstruction=False))
    #encMag.load_weights('magnitude-layer-' + str(encInd))
    encMag.compile(loss='mean_squared_error', optimizer='rmsprop')
    startLoss = 999
    losses = []
    times = []
    while len(losses) < 20:
        start = time.time()
        rnum = np.random.randint(0,4)
        myBatch = 1
        encMag.fit(kMagnData, kMagnData, batch_size=myBatch, nb_epoch=1000, verbose=0, validation_split=0.0, shuffle=False)
        startLoss = encMag.train_on_batch(kMagnData, kMagnData)
        losses.append(startLoss)
        end = time.time()
        timeDiff = end - start
        times.append(timeDiff)
        print 'magnitude autoencoder-layer ' + str(encInd) + ', epoch: ' + str(len(losses)) + ', loss: ' + str(startLoss)
        print 'time: ' + str(times)
        times = []
    encMag.save_weights('magnitude-layer-' + str(encInd), overwrite=True)
    np.save('./olap-aeparams/drop-magnitude-encoder-layer-' + str(encInd), encMag.layers[1].encoder.get_weights())
    np.save('./olap-aeparams/drop-magnitude-decoder-layer-' + str(encInd), encMag.layers[1].decoder.get_weights())
    kMagnData = encMag.predict(kMagnData)

kPhaseData = np.copy(phaseData)
for encInd in range(0, 5):
    encPhi = Sequential()
    midlay = encInd * 200
    outlay = (encInd + 1) * 200
    encoder = containers.Sequential([TimeDistributedDense(input_dim=pz - midlay, output_dim=pz - outlay, activation='tanh')])
    decoder = containers.Sequential([TimeDistributedDense(input_dim=pz - outlay, output_dim=pz - midlay, activation='linear')])
    encPhi.add(Dropout(p=0.1, input_shape=(py, pz - midlay)))
    encPhi.add(AutoEncoder(encoder=encoder, decoder=decoder, output_reconstruction=False))
    #encPhi.load_weights('phase-layer-' + str(encInd))
    encPhi.compile(loss='mean_squared_error', optimizer='rmsprop')
    startLoss = 999
    losses = []
    times = []
    while len(losses) < 20:
        start = time.time()
        rnum = np.random.randint(0,4)
        myBatch = 1
        encPhi.fit(kPhaseData, kPhaseData, batch_size=myBatch, nb_epoch=1000, verbose=0, validation_split=0.0, shuffle=False)
        startLoss = encPhi.train_on_batch(kPhaseData, kPhaseData)
        losses.append(startLoss)
        end = time.time()
        timeDiff = end - start
        times.append(timeDiff)
        print 'phase autoencoder-layer ' + str(encInd) + ', epoch: ' + str(len(losses)) + ', loss: ' + str(startLoss)
        print 'time: ' + str(times)
        times = []
    encPhi.save_weights('phase-layer-' + str(encInd), overwrite=True)
    np.save('./olap-aeparams/drop-phase-encoder-layer-' + str(encInd), encPhi.layers[1].encoder.get_weights())
    np.save('./olap-aeparams/drop-phase-decoder-layer-' + str(encInd), encPhi.layers[1].decoder.get_weights())
    kPhaseData = encPhi.predict(kPhaseData)

max_stars_repo_name: ppramesi/JOANNA

max_issues_repo_name: ppramesi/JOANNA

max_forks_repo_name: ppramesi/JOANNA

reason: import numpy,import scipy