text:
import logging
import numpy as np
import pandas as pd
from scipy.stats import t, beta
from scipy.optimize import fmin

from bab.mcmc import get_mcmc

logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger()
logger.setLevel('INFO')

def get_power(stan_model, y1, y2, rope_m, rope_sd, max_hdi_width_m, max_hdi_width_sd,
              cred_mass=0.95, n_sim=200, precision=2, rand_seed=None):
    """
    :param stan_model: StanModel instance
    :param y1: iterable, prospective samples of group one
    :param y2: iterable, prospective samples of group two
    :param rope_m: iterable, of length two, such as (-1, 1),
        specifying the limit of the ROPE on the difference of means.
    :param rope_sd: iterable, of length two, such as (-1, 1),
        specifying the limit of the ROPE on the difference of standard deviations.
    :param max_hdi_width_m: float, maximum desired width of the 95% HDI on the difference of means.
    :param max_hdi_width_sd: float, maximum desired width of the 95% HDI on the difference of standard deviations.
    :param cred_mass: (optional) float, fraction of credible mass.
    :param n_sim: (optional) int, number of simulated experiments used to estimate the power.
    :param precision: (optional) int, number of decimals to round the power statistics to.
    :param rand_seed: int (optional), random seed
    :return: power, dict
    """
    if rand_seed is not None:
        np.random.seed(int(rand_seed))

    sample_size = len(y1)

    mcmc_chain = get_mcmc(stan_model, y1, y2, rand_seed=rand_seed).extract()

    step_idx = _get_step_indices(mcmc_chain, n_sim)

    goal_tally = {
        'HDIm > ROPE': 0,
        'HDIm < ROPE': 0,
        'HDIm in ROPE': 0,
        'HDIm width < max': 0,
        'HDIsd > ROPE': 0,
        'HDIsd < ROPE': 0,
        'HDIsd in ROPE': 0,
        'HDIsd width < max': 0
    }

    power = {
        'HDIm > ROPE': [0, 0, 0],
        'HDIm < ROPE': [0, 0, 0],
        'HDIm in ROPE': [0, 0, 0],
        'HDIm width < max': [0, 0, 0],
        'HDIsd > ROPE': [0, 0, 0],
        'HDIsd < ROPE': [0, 0, 0],
        'HDIsd in ROPE': [0, 0, 0],
        'HDIsd width < max': [0, 0, 0]
    }

    n_sim = 0
    for step in step_idx:
        n_sim += 1

        y1_sim, y2_sim = _generate_simulated_data(mcmc_chain, sample_size, step)

        sim_chain = get_mcmc(stan_model, y1_sim, y2_sim, rand_seed=rand_seed).extract()

        goal_tally = _update_goal_tally(goal_tally, max_hdi_width_m, max_hdi_width_sd, rope_m, rope_sd, sim_chain)

        _assess_and_tally_goals(cred_mass, goal_tally, n_sim, power)

        _log_progress(n_sim, power, step_idx)

    for k, v in power.items():
        power[k] = [round(e, precision) for e in v]

    return power

def _get_step_indices(mcmc_chain, n_sim):
    chain_length = len(mcmc_chain['mu'][:, 0])  # same as len(mcmc_chain[:, 1])
    # Select thinned steps in chain for posterior predictions:
    step_idx = list(range(1, chain_length, int(chain_length / n_sim)))
    return step_idx

def _assess_and_tally_goals(cred_mass, goal_tally, n_sim, power):
    # Assess which goals were achieved and tally them:
    for k, v in goal_tally.items():
        a = 1 + v
        b = 1 + (n_sim - v)
        power[k][0] = a / (a + b)
        power[k][1:] = get_hdi_of_lcdf(beta, cred_mass=cred_mass, a=a, b=b)

def _generate_simulated_data(mcmc_chain, sample_size, step):
    # Get parameter values for this simulation:
    y1_sim = _generate_data_for_group(mcmc_chain, sample_size, step, 0)
    y2_sim = _generate_data_for_group(mcmc_chain, sample_size, step, 1)
    return y1_sim, y2_sim

def _generate_data_for_group(mcmc_chain, sample_size, step, group):
    return t.rvs(df=mcmc_chain['nu'][step],
                 loc=mcmc_chain['mu'][step, group],
                 scale=mcmc_chain['sigma'][step, group],
                 size=sample_size)

def _log_progress(n_sim, power, step_idx):
    if n_sim % 100 == 0:
        logging.info('Power after {} of {} simulations: '.format(n_sim, len(step_idx)))
        logging.info(pd.DataFrame(power, index=['mean', 'CrIlo', 'CrIhi']).T)

def get_hdi_of_lcdf(dist_name, cred_mass=0.95, **args):
    """
    Returns the HDI of a probability density function (form scipy.stats)
    """
    # freeze distribution with given arguments
    distri = dist_name(**args)
    # initial guess for hdi_low_tail_pr
    incred_mass = 1.0 - cred_mass

    def interval_width(low_tail_pr):
        return distri.ppf(cred_mass + low_tail_pr) - distri.ppf(low_tail_pr)

    # find low_tail_pr that minimizes interval width
    hdi_low_tail_pr = fmin(interval_width, incred_mass, ftol=1e-8, disp=False)[0]
    # return interval as array([low, high])
    return distri.ppf([hdi_low_tail_pr, cred_mass + hdi_low_tail_pr])

def _update_goal_tally(goal_tally, max_hdi_width_m, max_hdi_width_sd, rope_m, rope_sd, sim_chain):
    for variable, v, max_hdi_width, rope in [
        ('mu', 'm', max_hdi_width_m, rope_m),
        ('sigma', 'sd', max_hdi_width_sd, rope_sd)
    ]:
        hdim_l, hdim_r = get_hdi(sim_chain[variable][:, 0] - sim_chain[variable][:, 1])

        if hdim_l > rope[1]:
            goal_tally['HDI{} > ROPE'.format(v)] += 1
        elif hdim_r < rope[0]:
            goal_tally['HDI{} < ROPE'.format(v)] += 1
        elif rope[0] < hdim_l and hdim_r < rope[1]:
            goal_tally['HDI{} in ROPE'.format(v)] += 1
        else:
            pass
        if hdim_r - hdim_l < max_hdi_width:
            goal_tally['HDI{} width < max'.format(v)] += 1
    return goal_tally

def get_hdi(samples, cred_mass=0.95):
    sorted_samples = sorted(samples)
    n_samples = len(sorted_samples)

    ci_idx_inc = int(cred_mass * n_samples)
    n_cis = n_samples - ci_idx_inc

    ci_width = n_cis * [0]
    for i in range(n_cis):
        ci_width[i] = sorted_samples[i + ci_idx_inc] - sorted_samples[i]

    hdi_min = sorted_samples[np.argmin(ci_width)]
    hdi_max = sorted_samples[np.argmin(ci_width) + ci_idx_inc]

    return hdi_min, hdi_max

max_stars_repo_name: volkale/bab

max_issues_repo_name: volkale/bab

max_forks_repo_name: volkale/bab


text:
# MIT License
#
# Copyright (c) 2018 Markus Semmler
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import numpy as np
from misc.disc_sample import sample_cat

class StickyHDPSLDSHMMModelInitMixin:

    def slds_init_z(self):

        num_traces, num_steps, _ = self.y.shape
        beta = self.hdp_params[3]
        pi = self.hdp_params[4]

        # now we sample the mode sequence
        max_state = len(beta)
        mode_seq = np.empty([num_traces, num_steps], np.int64)
        mode_seq[:, 0] = np.random.choice(max_state, p=beta, size=[num_traces])

        # iterate over
        for trace in range(num_traces):
            for step in range(num_steps - 1):
                z = mode_seq[trace, step]
                mode_seq[trace, step + 1] = np.random.choice(max_state, p=pi[z])

        self.z = mode_seq

ext: py

max_stars_repo_name: kosmitive/sticky-hdp-slds-hmm

max_issues_repo_name: kosmitive/sticky-hdp-slds-hmm

max_forks_repo_name: kosmitive/sticky-hdp-slds-hmm

reason: import numpy


text:
import numpy as np

class Node:
    to = ""
    cost = ""
    nxt = None

class Graph:
    edges = {}
    num_nodes = 0
    num_edges = 0
    directed = False

def insert_edge(objGraph, u, v, cost, isDirected):
    item = Node()

    item.cost = cost
    item.to = v;
    if u in objGraph.edges:
        item.nxt = objGraph.edges[u]
    else:
        item.nxt = None

    objGraph.edges[u] = item
    #objGraph.grade[u] += 1
    if isDirected == False and v != u:
        insert_edge(objGraph, v, u, cost, True)
    #else:
    #    objGraph.num_edges += 1

def create_graph (objGraph, hasCost):
    i = 0;
    u = v = cost = 0
    number_edges = objGraph.num_edges

    while i < objGraph.num_edges:
        print("Insert edge (u, v)")
        u = input('u: ')
        v = input('v: ')

        if hasCost == True :
            print("Insert cost or weight from edge:")
            cost = input('Cost / weight: ')
        else:
            cost = 1

        # insert edge on the adjacent list
        insert_edge(objGraph, u, v, cost, objGraph.directed);
        i += 1

def print_graph(objGraph):
    i = 1
    item = None
    print("\nADJACENT LIST:")
    string = ""
    for key in objGraph.edges.keys():
        string += (key + " -> ")
        item = objGraph.edges[key]
        while item != None:
            string += item.to + ": " + item.cost +"\t"
            item = item.nxt
        string += "\n"
    print(string)
    print()

def main():
    op = directed = cost = i = j = 0
    print("\n####### GRAPH STRUCTURE #######\n")
    print("\nWhat kind of graph is it?\n")
    print("\t1) Directed.")
    print("\t2) No directed.\n")
    directed = int(input())

    print("\nDo the edges have cost / weight?\n")
    print("\t1) YES.")
    print("\t2) NO.\n")
    cost = int(input())
    hasCost = True if cost == 1 else False

    print("Number of nodes.\n")
    num_nodes = int(input())

    obj_graph.num_nodes = num_nodes
    obj_graph.directed = True if directed == 1 else False

    print("Number of edges.\n")
    obj_graph.num_edges = int(input())

    create_graph(obj_graph, hasCost)
    print_graph(obj_graph)

obj_graph = Graph()
main()

max_stars_repo_name: jrg-sln/academy

max_issues_repo_name: jrg-sln/academy

max_forks_repo_name: jrg-sln/academy


text:
import numpy
from scipy import fftpack

from audionmf.transforms.window_func import mdct_window_mp3
from audionmf.util.matrix_util import array_pad

def mdct_fast(mdct_ary):
    """ Turns 2N inputs into N outputs using MDCT via a DCT-IV. """

    # split into fourths
    half_block_size = mdct_ary.size // 4

    # create blocks for MDCT
    # the MDCT of 2N inputs (a, b, c, d) is exactly equivalent to a DCT-IV of the N inputs: (−cR−d, a−bR)
    a = mdct_ary[:half_block_size]
    bR = mdct_ary[half_block_size:2 * half_block_size][::-1]
    cR = mdct_ary[2 * half_block_size:3 * half_block_size][::-1]
    d = mdct_ary[3 * half_block_size:4 * half_block_size]

    # fill input array
    dct_input = numpy.concatenate((-cR - d, a - bR))

    # run DCT-IV on this array of size N, producing effectively MDCT of size 2N
    dct4 = fftpack.dct(dct_input, type=4)

    return dct4 * 0.5

def imdct_fast(mdct_ary):
    """ Turns N inputs into 2N outputs using MDCT via a DCT-IV. """

    # get a size of fourths
    half_block_size = mdct_ary.size // 2

    # allocate output array
    output_ary = numpy.ndarray((half_block_size * 4,))

    # inverse DCT-IV, obtaining back (−cR−d, a−bR)
    idct4 = fftpack.idct(mdct_ary, type=4)

    # divide by implicit scaling factor 2N
    idct4 /= 4 * half_block_size

    # extend and shift to gain the (almost) original array (still need overlap-and-add with the next block)
    # IMDCT(MDCT(a, b, c, d)) = (a−bR, b−aR, c+dR, d+cR) / 2
    abR = idct4[half_block_size:]
    output_ary[:half_block_size] = abR
    output_ary[half_block_size:2 * half_block_size] = -abR[::-1]

    cRd = idct4[:half_block_size]
    output_ary[2 * half_block_size:3 * half_block_size] = -cRd[::-1]
    output_ary[3 * half_block_size:] = -cRd

    return output_ary

def mdct_slow(signal):
    """ Slow implementation from definition. Do not use in production. """

    N = len(signal) // 2

    output_ary = numpy.ndarray((N,))

    for k in range(N):
        val = 0
        for n in range(2 * N):
            val += signal[n] * numpy.cos((numpy.pi / N) * (n + 0.5 + N / 2) * (k + 0.5))
        output_ary[k] = val

    return output_ary

def imdct_slow(mdct_ary):
    """ Slow implementation from definition. Do not use in production. """

    N = len(mdct_ary)

    output_ary = numpy.ndarray((2 * N,))

    for n in range(2 * N):
        val = 0
        for k in range(N):
            val += mdct_ary[k] * numpy.cos((numpy.pi / N) * (n + 0.5 + N / 2) * (k + 0.5))
        output_ary[n] = (1 / N) * val

    return output_ary

def mdct(full_signal, block_size, slow=False):
    """ Runs overlapping MDCT on a full signal.

     Block size must be an even value.
     """

    # pad samples properly to block size
    samples, padding = array_pad(full_signal, block_size)

    # add an extra block to the start and end to fix the first and last block
    samples = numpy.pad(samples, (block_size, block_size), mode='constant', constant_values=0)

    # allocate MDCT output matrix
    mdct_matrix = numpy.ndarray(((len(samples) // block_size) - 1, block_size))

    # pick the transform function R^2N -> R^N
    if not slow:
        f = mdct_fast
    else:
        f = mdct_slow

    # get the window function
    window = mdct_window_mp3(block_size)

    # run MDCT for each block except the last one
    for i in range((len(samples) // block_size) - 1):
        signal = samples[i * block_size:i * block_size + block_size * 2]

        # window the signal
        signal = window * signal

        mdct_ary = f(signal)
        mdct_matrix[i] = mdct_ary

    return mdct_matrix, padding

def imdct(mdct_matrix, padding, slow=False):
    """ Reverses MDCT, returning the original signal after overlap-and-add. """

    # find block size
    block_size = mdct_matrix.shape[1]

    # allocate IMDCT output array along with an extra block filler at the end
    imdct_ary = numpy.zeros(mdct_matrix.size + block_size)

    # pick the transform function R^N -> R^2N
    if not slow:
        f = imdct_fast
    else:
        f = imdct_slow

    # get the window function
    window = mdct_window_mp3(block_size)

    # run IMDCT for each block
    for i in range(mdct_matrix.shape[0]):
        signal = f(mdct_matrix[i])

        # window the signal and multiply by two to gain original amplitudes
        signal = window * signal * 2

        imdct_ary[i * block_size:i * block_size + block_size * 2] += signal

    # remove the padding from the array and return it
    return imdct_ary[block_size:-padding - block_size]

max_stars_repo_name: argoneuscze/AudioNMF

max_issues_repo_name: argoneuscze/AudioNMF

max_forks_repo_name: argoneuscze/AudioNMF

reason: import numpy,from scipy


text:
# ==============================================================================
#  Copyright 2019 Intel Corporation
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
# ==============================================================================
"""nGraph TensorFlow bridge floor operation test
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import pytest
import platform

import tensorflow as tf
import numpy as np
from common import NgraphTest

class TestFlibOperations(NgraphTest):

    @pytest.mark.skipif(platform.system() == 'Darwin', reason='Only for Linux')
    def test_flib_1(self):
        graph = self.import_pbtxt('flib_graph_1.pbtxt')
        with graph.as_default() as g:

            x = self.get_tensor(g, "Placeholder:0", True)
            y = self.get_tensor(g, "Placeholder_1:0", True)
            z = self.get_tensor(g, "Placeholder_2:0", True)

            a = self.get_tensor(g, "add_1:0", True)
            b = self.get_tensor(g, "Sigmoid:0", True)

            sess_fn = lambda sess: sess.run(
                [a, b], feed_dict={i: np.full((2, 3), 1.0) for i in [x, y, z]})

            res1 = self.with_ngraph(sess_fn)
            res2 = self.without_ngraph(sess_fn)
            exp = [np.full((2, 3), 3.0), np.full((2, 3), 0.95257413)]
            # Note both run on Host (because NgraphEncapsulate can only run on host)
            assert np.isclose(res1, res2).all()
            # Comparing with expected value
            assert np.isclose(res1, exp).all()

    @pytest.mark.skip(reason="Not passing through grappler")
    def test_flib_2(self):
        graph = self.import_pbtxt('flib_graph_2.pbtxt')
        with graph.as_default() as g:

            x = self.get_tensor(g, "Variable_2/peek/_2:0", True)
            y = self.get_tensor(g, "Variable_1/peek/_3:0", True)
            z = self.get_tensor(g, "Variable/peek/_4:0", True)

            a = self.get_tensor(g, "add_1:0", True)
            b = self.get_tensor(g, "Sigmoid:0", True)

            def sess_fn(sess):
                #sess.run(tf.global_variables_initializer())
                return sess.run(
                    [a, b],
                    feed_dict={i: np.full((2, 3), 1.0) for i in [x, y, z]})

            res1 = self.with_ngraph(sess_fn)
            res2 = self.without_ngraph(sess_fn)
            exp = [np.full((2, 3), 3.0), np.full((2, 3), 0.95257413)]
            # Note both run on Host (because NgraphEncapsulate can only run on host)
            assert np.isclose(res1, res2).all()
            # Comparing with expected value
            assert np.isclose(res1, exp).all()  #fails

max_stars_repo_name: indie/ngraph-bridge

max_issues_repo_name: indie/ngraph-bridge

max_forks_repo_name: indie/ngraph-bridge


text:
#!/usr/bin/env python3

from flask import Blueprint, Response, render_template, request, session
from ezprobs.geometry import area_circle
from ezprobs.hydraulics import pipe_loss, local_loss
from ezprobs.problems import Parameter, Plot
from ezprobs.units import M, CM, MM, M3PS, KINEMATIC_VISCOSITY, GRAVITY
from ezprobs.dict import DICT_GER, DICT_ENG
from io import BytesIO
from math import sqrt
from scipy.optimize import fsolve

import numpy as np
import matplotlib as mpl

mpl.use("Agg")

import matplotlib.pyplot as plt

__author__ = "Manuel Pirker"
__copyright__ = "Copyright (c) 2021 Manuel Pirker"
__license__ = "MIT"
__email__ = "manuel.pirker@tugraz.at"

bp = Blueprint("pressure_pipe_02", __name__)

def compute_solution():
    d = 5 * CM
    ha = 150 * CM
    hb = 20 * CM
    hout = 30 * CM
    l = 2 * M
    k = 0.3 * MM
    nu_entry = 0.5

    scale = 1 # over scaling velocity head for better display

    q_initial = 3 * 10 ** -3 * M3PS

    if request.method == "POST":
        d = float(request.form["d"]) * MM
        hb = float(request.form["hb"]) * CM

    a = area_circle(d / 2)

    if hb == ha:
        q = 0
    elif hb < hout:
        q = fsolve(
            lambda q: hout
            + (q / a) ** 2 / (2 * GRAVITY)
            + local_loss(nu_entry, a, q)
            + pipe_loss(l, a, k, d, q)
            - ha,
            1,
        )[0]
    else:
        q = fsolve(
            lambda q: hb
            + (q / a) ** 2 / (2 * GRAVITY)
            + local_loss(nu_entry, a, q)
            + pipe_loss(l, a, k, d, q)
            - ha,
            1,
        )[0]

    v = q / a

    distances = np.array([0, l])

    x = np.cumsum(distances)
    pipe = np.array([ha-0.85, hout])
    energy_horizon = np.full((len(x)), ha)

    if hb == ha:
        losses = np.array([0, 0])
    else:
        losses = np.array(
            [
                local_loss(nu_entry, a, q),
                pipe_loss(l, a, k, d, q),
            ]
        )
    cum_losses = np.cumsum(losses)
    energy_line = energy_horizon - cum_losses
    energy_line[0] = energy_horizon[0]-scale*cum_losses[0] # for display
    kinetic_energy = np.array([v, v]) ** 2 / (2 * GRAVITY)
    pressure_line = energy_line - kinetic_energy
    #pressure_line = energy_line - scale*kinetic_energy # for display

    return {
        "d": d,
        "hb": hb,
        "discharge": q,
        "x": x.tolist(),
        "pipe": pipe.tolist(),
        "energy_horizon": energy_horizon.tolist(),
        "energy_line": energy_line.tolist(),
        "pressure_line": pressure_line.tolist(),
    }

@bp.route("/", methods=["POST", "GET"])
def index():
    lang = DICT_GER

    solution = compute_solution()
    session["solution"] = solution

    parameters = [
        Parameter(
            "d",
            "d",
            10,
            120,
            5,
            solution["d"] / MM,
            unit="mm",
            description=lang["dia_pipe"],
        ),
        Parameter(
            "hb",
            "hb",
            10,
            160,
            5,
            solution["hb"] / CM,
            unit="cm",
            description=lang["wlvl_basin"]+" B",
        ),
    ]

    plot = Plot("plot", alt="plot", caption="Energy- and pressure lines")

    return render_template(
        "problems/pressure_pipe_02.html",
        plot=plot,
        parameters=parameters,
        solution=solution,
    )

@bp.route("/plot")
def plot_function():
    ha = 1.5 * M
    hout = 0.5 * M
    lang = DICT_GER

    x = session["solution"]["x"]
    d = session["solution"]["d"]
    hb = session["solution"]["hb"]
    pipe = session["solution"]["pipe"]
    energy_horizon = session["solution"]["energy_horizon"]
    energy_line = session["solution"]["energy_line"]
    pressure_line = session["solution"]["pressure_line"]

    #xticks = np.array([0, l1, l1+l2, l1+l2+l3])
    #xticks = np.array([])
    xticks = np.array([-.50, 0, x[-1], x[-1]+.50])
    #yticks = np.sort(np.array([ha, hout, hb, 175]))

    fig, ax = plt.subplots(figsize=(9,5))
    ax.set_frame_on(False)
    ax.set_xticks(xticks)
    ax.set_xticklabels([' ','A','B',' '])
    #ax.set_yticks(yticks)

    ax.plot(x, energy_horizon, label=lang["ehorizont_l"], color="red", linestyle="dashdot", lw=1)
    ax.plot(x, energy_line, label=lang["eline_l"], color="red", lw=1.5)
    ax.plot(x, pressure_line, label=lang["pline_l"], color="blue", linestyle="dashed", lw=1.5)
    ax.plot(x, pipe, label=lang["paxis"], color="black", linestyle="dashdot", lw=1)
    ax.plot(x, np.array(pipe)+d/2, color="grey", linestyle="-", lw=0.5)
    ax.plot(x,  np.array(pipe)-d/2, color="grey", linestyle="-", lw=0.5)

	# plot reservoirs
    ax.plot(np.array([-.50, 0]), np.array([ha, ha]), color="blue", lw=1.5)
    ax.plot(np.array([x[-1], x[-1]+.50]), np.array([hb, hb]), color="blue", lw=1.5)
    ax.fill_between(np.array([-.50, 0]), np.array([ha, ha]), np.array([0, 0]), color="b", alpha=0.1)
    ax.fill_between(np.array([x[-1], x[-1]+.50]), np.array([hb, hb]), np.array([0, 0]), color="b", alpha=0.1)

    ax.plot(np.array([-.50, -.50, 0, 0]), np.array([ha+.3, 0, 0, ha+.3]), color="k", lw=1.5)
    ax.plot(np.array([x[-1], x[-1], x[-1]+.50, x[-1]+.50]), np.array([ha+.3, 0, 0, ha+.3]), color="k", lw=1.5)

    ax.text(x[-1], ha, lang["ehorizont_s"], ha='right', va="bottom")
    ax.text(x[-1]/2, np.mean(energy_line), lang["eline_s"], ha='left', va="bottom", color="red")
    ax.text(x[-1]/2, np.mean(pressure_line), lang["pline_s"], ha='left', va="bottom", color="blue")
    ax.text(0, ha-0.85, f"DN{int(d*1000)} ", ha='right', va="center")

	# add labels
    #ax.text(x[0],ha-10,'I ',ha="right")
    #ax.text(x[1],h2,'II', va='top', ha='center')
    #ax.text(x[3],h3,'III', va='bottom', ha='center')
    #ax.text(x[5],h4,' IV')

    ax.grid(axis='x')
    #ax.legend()
    ax.set_xlim((-.51,x[-1]+.51))
    ax.set_ylim((-0.01,1.6))
    ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.05),
          fancybox=True, shadow=True, ncol=4)
    #ax.set_xlabel("Distance [m]")
    ax.set_ylabel(lang["height"]+" [m]")
    #ax.set_title("Pressure- and Energyline")

    secax = ax.secondary_yaxis("right")
    secax.spines["right"].set_visible(False)

    buffer = BytesIO()
    fig.savefig(buffer, format="png")
    plt.close(fig)
    return Response(buffer.getvalue(), mimetype="image/png")

@bp.route("/ajax", methods=["POST", "GET"])
def ajax():
    solution = compute_solution()
    session["solution"] = solution

    return render_template(
        "problems/pressure_pipe_02_solution.html",
        solution=solution,
    )


text:
import xml.etree.ElementTree as ET
import numpy as np
import torch

def load_mesh_data(mesh_path):
    """Loads mesh vertices, bone assignments, and triangle IDs.

    Args:
        mesh_path - string: Path to the OGRE XML mesh data.

    Returns:
        mesh_vertices - array (N_v x 3): Mesh vertices, where N_v is the
            number of vertices.
        bone_weights - array (N_b x N_v): Bone weights, where N_b is the bone
            count and N_v is the number of vertices.
        triangles - array (N_f x 3): Triangle IDs, where N_f is the number of
            triangle faces in the mesh.
    """

    tree = ET.parse(mesh_path)
    root = tree.getroot()

    # Store all bone assignments
    bone_assignment_dict = {}
    bone_weight_dict = {}
    num_bones = 0
    for child in root[4]:
        key = 'vertex_' + str(child.attrib['vertexindex'])
        bone_index = int(child.attrib['boneindex'])
        if bone_index > num_bones:
            num_bones = bone_index

        if key in bone_assignment_dict:
            bone_weight_dict[key] = np.append(bone_weight_dict[key], np.array([float(child.attrib['weight'])]))
            bone_assignment_dict[key] = np.append(bone_assignment_dict[key], np.array([bone_index]))
        else:
            bone_weight_dict[key] = np.array([float(child.attrib['weight'])])
            bone_assignment_dict[key] = np.array([bone_index])

    num_bones += 1 # because num_bones is only as large as the biggest index.

    # Store the vertices
    mesh_vertices = np.zeros((int(root[0].attrib['vertexcount']), 3))
    normals = np.zeros((int(root[0].attrib['vertexcount']), 3))
    i = 0
    for child in root[0][0]:
        mesh_vertices[i, 0] = child[0].attrib['x']
        mesh_vertices[i, 1] = child[0].attrib['y']
        mesh_vertices[i, 2] = child[0].attrib['z']
        normals[i, 0] = child[1].attrib['x']
        normals[i, 1] = child[1].attrib['y']
        normals[i, 2] = child[1].attrib['z']
        i += 1

    # Build the bone_weights matrix
    bone_weights = np.zeros((num_bones, len(mesh_vertices)))
    i = 0
    for key, value in bone_assignment_dict.items():
        bone_assignments = value
        bone_weight = bone_weight_dict[key]
        bone_weights[bone_assignments, i] = bone_weight
        i += 1

    triangles_idxs = None
    vertex_map = [1, 2, 0]
    i = 0

    for submesh in root[1]:
        for faces in submesh:
            num_faces = int(faces.attrib['count'])
            if triangles_idxs is None:
                triangles_idxs = np.zeros((num_faces, 3), dtype=int)
            else:
                triangles_idxs = np.append(triangles_idxs, np.zeros((num_faces, 3), dtype=int), axis=0)

            for face in faces:
                j = 0
                for _, value in face.attrib.items():
                    triangles_idxs[i, vertex_map[j]] = int(value)
                    j += 1
                i += 1

    triangles = torch.from_numpy(triangles_idxs.astype(np.int32))

    return mesh_vertices, normals, bone_weights, triangles

max_stars_repo_name: ajdillhoff/pytorch-mesh-mask

max_issues_repo_name: ajdillhoff/pytorch-mesh-mask

max_forks_repo_name: ajdillhoff/pytorch-mesh-mask


text:
"""
flowfield pathfinding
author: Sarim Mehdi (muhammadsarim.mehdi@studio.unibo.it)
Source: https://leifnode.com/2013/12/flow-field-pathfinding/
"""

import numpy as np
import matplotlib.pyplot as plt

show_animation = True

def draw_horizontal_line(start_x, start_y, length, o_x, o_y, o_dict, path):
    for i in range(start_x, start_x + length):
        for j in range(start_y, start_y + 2):
            o_x.append(i)
            o_y.append(j)
            o_dict[(i, j)] = path

def draw_vertical_line(start_x, start_y, length, o_x, o_y, o_dict, path):
    for i in range(start_x, start_x + 2):
        for j in range(start_y, start_y + length):
            o_x.append(i)
            o_y.append(j)
            o_dict[(i, j)] = path

class FlowField:
    def __init__(self, obs_grid, goal_x, goal_y, start_x, start_y,
                 limit_x, limit_y):
        self.start_pt = [start_x, start_y]
        self.goal_pt = [goal_x, goal_y]
        self.obs_grid = obs_grid
        self.limit_x, self.limit_y = limit_x, limit_y
        self.cost_field = {}
        self.integration_field = {}
        self.vector_field = {}

    def find_path(self):
        self.create_cost_field()
        self.create_integration_field()
        self.assign_vectors()
        self.follow_vectors()

    def create_cost_field(self):
        """Assign cost to each grid which defines the energy
        it would take to get there."""
        for i in range(self.limit_x):
            for j in range(self.limit_y):
                if self.obs_grid[(i, j)] == 'free':
                    self.cost_field[(i, j)] = 1
                elif self.obs_grid[(i, j)] == 'medium':
                    self.cost_field[(i, j)] = 7
                elif self.obs_grid[(i, j)] == 'hard':
                    self.cost_field[(i, j)] = 20
                elif self.obs_grid[(i, j)] == 'obs':
                    continue

                if [i, j] == self.goal_pt:
                    self.cost_field[(i, j)] = 0

    def create_integration_field(self):
        """Start from the goal node and calculate the value
        of the integration field at each node. Start by
        assigning a value of infinity to every node except
        the goal node which is assigned a value of 0. Put the
        goal node in the open list and then get its neighbors
        (must not be obstacles). For each neighbor, the new
        cost is equal to the cost of the current node in the
        integration field (in the beginning, this will simply
        be the goal node) + the cost of the neighbor in the
        cost field + the extra cost (optional). The new cost
        is only assigned if it is less than the previously
        assigned cost of the node in the integration field and,
        when that happens, the neighbor is put on the open list.
        This process continues until the open list is empty."""
        for i in range(self.limit_x):
            for j in range(self.limit_y):
                if self.obs_grid[(i, j)] == 'obs':
                    continue
                self.integration_field[(i, j)] = np.inf
                if [i, j] == self.goal_pt:
                    self.integration_field[(i, j)] = 0

        open_list = [(self.goal_pt, 0)]
        while open_list:
            curr_pos, curr_cost = open_list[0]
            curr_x, curr_y = curr_pos
            for i in range(-1, 2):
                for j in range(-1, 2):
                    x, y = curr_x + i, curr_y + j
                    if self.obs_grid[(x, y)] == 'obs':
                        continue
                    if (i, j) in [(1, 0), (0, 1), (-1, 0), (0, -1)]:
                        e_cost = 10
                    else:
                        e_cost = 14
                    neighbor_energy = self.cost_field[(x, y)]
                    neighbor_old_cost = self.integration_field[(x, y)]
                    neighbor_new_cost = curr_cost + neighbor_energy + e_cost
                    if neighbor_new_cost < neighbor_old_cost:
                        self.integration_field[(x, y)] = neighbor_new_cost
                        open_list.append(([x, y], neighbor_new_cost))
            del open_list[0]

    def assign_vectors(self):
        """For each node, assign a vector from itself to the node with
        the lowest cost in the integration field. An agent will simply
        follow this vector field to the goal"""
        for i in range(self.limit_x):
            for j in range(self.limit_y):
                if self.obs_grid[(i, j)] == 'obs':
                    continue
                if [i, j] == self.goal_pt:
                    self.vector_field[(i, j)] = (None, None)
                    continue
                offset_list = [(i + a, j + b)
                               for a in range(-1, 2)
                               for b in range(-1, 2)]
                neighbor_list = [{'loc': pt,
                                  'cost': self.integration_field[pt]}
                                 for pt in offset_list
                                 if self.obs_grid[pt] != 'obs']
                neighbor_list = sorted(neighbor_list, key=lambda x: x['cost'])
                best_neighbor = neighbor_list[0]['loc']
                self.vector_field[(i, j)] = best_neighbor

    def follow_vectors(self):
        curr_x, curr_y = self.start_pt
        while curr_x is not None and curr_y is not None:
            plt.plot(curr_x, curr_y, "b*")
            curr_x, curr_y = self.vector_field[(curr_x, curr_y)]
            plt.pause(0.001)
        if show_animation:
            plt.show()

def main():
    # set obstacle positions
    obs_dict = {}
    for i in range(51):
        for j in range(51):
            obs_dict[(i, j)] = 'free'
    o_x, o_y, m_x, m_y, h_x, h_y = [], [], [], [], [], []

    s_x = 5.0
    s_y = 5.0
    g_x = 35.0
    g_y = 45.0

    # draw outer border of maze
    draw_vertical_line(0, 0, 50, o_x, o_y, obs_dict, 'obs')
    draw_vertical_line(48, 0, 50, o_x, o_y, obs_dict, 'obs')
    draw_horizontal_line(0, 0, 50, o_x, o_y, obs_dict, 'obs')
    draw_horizontal_line(0, 48, 50, o_x, o_y, obs_dict, 'obs')

    # draw inner walls
    all_x = [10, 10, 10, 15, 20, 20, 30, 30, 35, 30, 40, 45]
    all_y = [10, 30, 45, 20, 5, 40, 10, 40, 5, 40, 10, 25]
    all_len = [10, 10, 5, 10, 10, 5, 20, 10, 25, 10, 35, 15]
    for x, y, l in zip(all_x, all_y, all_len):
        draw_vertical_line(x, y, l, o_x, o_y, obs_dict, 'obs')

    all_x[:], all_y[:], all_len[:] = [], [], []
    all_x = [35, 40, 15, 10, 45, 20, 10, 15, 25, 45, 10, 30, 10, 40]
    all_y = [5, 10, 15, 20, 20, 25, 30, 35, 35, 35, 40, 40, 45, 45]
    all_len = [10, 5, 10, 10, 5, 5, 10, 5, 10, 5, 10, 5, 5, 5]
    for x, y, l in zip(all_x, all_y, all_len):
        draw_horizontal_line(x, y, l, o_x, o_y, obs_dict, 'obs')

    # Some points are assigned a slightly higher energy value in the cost
    # field. For example, if an agent wishes to go to a point, it might
    # encounter different kind of terrain like grass and dirt. Grass is
    # assigned medium difficulty of passage (color coded as green on the
    # map here). Dirt is assigned hard difficulty of passage (color coded
    # as brown here). Hence, this algorithm will take into account how
    # difficult it is to go through certain areas of a map when deciding
    # the shortest path.

    # draw paths that have medium difficulty (in terms of going through them)
    all_x[:], all_y[:], all_len[:] = [], [], []
    all_x = [10, 45]
    all_y = [22, 20]
    all_len = [8, 5]
    for x, y, l in zip(all_x, all_y, all_len):
        draw_vertical_line(x, y, l, m_x, m_y, obs_dict, 'medium')

    all_x[:], all_y[:], all_len[:] = [], [], []
    all_x = [20, 30, 42] + [47] * 5
    all_y = [35, 30, 38] + [37 + i for i in range(2)]
    all_len = [5, 7, 3] + [1] * 3
    for x, y, l in zip(all_x, all_y, all_len):
        draw_horizontal_line(x, y, l, m_x, m_y, obs_dict, 'medium')

    # draw paths that have hard difficulty (in terms of going through them)
    all_x[:], all_y[:], all_len[:] = [], [], []
    all_x = [15, 20, 35]
    all_y = [45, 20, 35]
    all_len = [3, 5, 7]
    for x, y, l in zip(all_x, all_y, all_len):
        draw_vertical_line(x, y, l, h_x, h_y, obs_dict, 'hard')

    all_x[:], all_y[:], all_len[:] = [], [], []
    all_x = [30] + [47] * 5
    all_y = [10] + [37 + i for i in range(2)]
    all_len = [5] + [1] * 3
    for x, y, l in zip(all_x, all_y, all_len):
        draw_horizontal_line(x, y, l, h_x, h_y, obs_dict, 'hard')

    plt.plot(o_x, o_y, "sr")
    plt.plot(m_x, m_y, "sg")
    plt.plot(h_x, h_y, "sy")
    plt.plot(s_x, s_y, "og")
    plt.plot(g_x, g_y, "o")
    plt.grid(True)

    flow_obj = FlowField(obs_dict, g_x, g_y, s_x, s_y, 50, 50)
    flow_obj.find_path()

if __name__ == '__main__':
    main()

max_stars_repo_name: csalinasonline/PythonRobotics

max_issues_repo_name: csalinasonline/PythonRobotics

max_forks_repo_name: csalinasonline/PythonRobotics


text:
import numpy as np

def array_conv2d(array, kernel, stride):
    if kernel.shape[0] > array.shape[0] or kernel.shape[1] > array.shape[1]:
        return np.nan
    if stride[0] > array.shape[0] - kernel.shape[0] or stride[1] > array.shape[1] - kernel.shape[1]:
        return np.nan

    step_x, step_y = stride
    array = np.pad(array,
                   ((0, (array.shape[0] - kernel.shape[0]) % step_x), (0, (array.shape[1] - kernel.shape[1]) % step_y)),
                   'constant', constant_values=(0, 0))
    result = []

    for i in range(0, array.shape[0] - kernel.shape[0] + 1, step_x):
        temp_res = []
        for j in range(0, array.shape[1] - kernel.shape[1] + 1, step_y):
            temp_res.append(np.sum(array[i:kernel.shape[0] + i, j:kernel.shape[1] + j] \
                                                                   * kernel[:, :]))
        result.append(np.array(temp_res))
    result = np.array(result)
    return result

def array_pooling(array, kernel_size, stride, mode="max_pooling"):
    if kernel_size[0] > array.shape[0] or kernel_size[1] > array.shape[1]:
        return np.nan
    if stride[0] > array.shape[0] - kernel_size[0] or stride[1] > array.shape[1] - kernel_size[1]:
        return np.nan

    step_x, step_y = stride
    if step_x != 0 and step_y != 0:
        array = np.pad(array,
                   ((0, (array.shape[0] - kernel_size[0]) % step_x), (0, (array.shape[1] - kernel_size[1]) % step_y)),
                   'constant', constant_values=(0, 0))
    else:
        step_x = 1
        step_y = 1
    result = []

    for i in range(0, array.shape[0] - kernel_size[0] + 1, step_x):
        temp_res = []
        for j in range(0, array.shape[1] - kernel_size[1] + 1, step_y):
            if mode == "max_pooling":
                temp_res.append(np.max(array[i:kernel_size[0] + i, j:kernel_size[1] + j]))
            elif mode == "avg_pooling":
                temp_res.append(np.mean(array[i:kernel_size[0] + i, j:kernel_size[1] + j]))
            else:
                raise NameError("Selected pooling method is not supported")
        result.append(np.array(temp_res))
    result = np.array(result)
    return result

if __name__ == "__main__":
    test_arr = np.array([range(0, 5), range(5, 10), range(10, 15), range(15, 20), range(20, 25)])
    kernel = np.array([(6, 2), (3, 5)])
    print(array_conv2d(test_arr, kernel, (2, 2)))
    print(array_pooling(array_conv2d(test_arr, kernel, (2, 2)), (2, 2), (1, 1), "avg_pooling"))


text:
import unittest

import numpy as np
from scipy.stats import hmean

from automix import config
from automix.featureExtraction.lowLevel import (CoreFinder, PeakPicking, PeakSelection, Windowing)
from automix.featureExtraction.structure.eval import evalCuesMutliple
from automix.model.classes import (DenseSignal, Signal, SparseSegmentSignal, SparseSignal, Track)
from automix.model.inputOutput.serializer import JamsSerializer

class TestFeatureExtraction(unittest.TestCase):
    def testPeakPicking(self):
        # Basic test
        myInput = Signal([1.5, 1, 0, 0, 0, 2, 0], sampleRate=1)
        pp = PeakPicking(parameterMinDistance=2, parameterRelativeThreshold=0.2)
        result = pp.predictOne(myInput)[0]
        self.assertEqual(list(result.values), [1.5, 2.])
        self.assertEqual(list(result.times), [0, 5])

    def testPeakSelection(self):
        # Basic test
        myInput = [Signal(1, times=[0, 5, 10]), Signal(0.5, times=[0, 10])]
        ps = PeakSelection()
        result = ps.predictOne(myInput, None, None)[0]
        self.assertEqual(list(result.values), [1.5, 1, 1.5])
        self.assertEqual(list(result.times), [0, 5, 10])

        # Test with mean
        ps = PeakSelection(parameterMergeFunction=np.mean)
        result = ps.predictOne(myInput, None, None)[0]
        self.assertEqual(list(result.values), [0.75, 1, 0.75])
        self.assertEqual(list(result.times), [0, 5, 10])

    def testStructureEval(self):
        # basic tests
        result = evalCuesMutliple([Signal(1, times=[1, 3, 2]), Signal(1, times=[1, 2, 3])],
                                  [Signal(1, times=[1, 2, 3]), Signal(1, times=[1, 2])])
        self.assertEqual(result["recall"], 1)
        self.assertEqual(result["precision"], 5 / 6)
        self.assertEqual(result["fMeasure"], hmean([1, 5 / 6]))

        result = evalCuesMutliple([Signal(1, times=[1, 2, 4, 5, 6])], [Signal(1, times=[1, 2, 3, 4])], limitSearchSpace=True)
        self.assertEqual(result["recall"], 3 / 4)
        self.assertEqual(result["precision"], 3 / 3)

        result = evalCuesMutliple([Signal(1, times=[1, 2, 5, 6]), Signal(1, times=[1, 2, 3, 4])],
                                  [Signal(1, times=[1, 2, 3, 4]), Signal(1, times=[1, 2, 5, 6])],
                                  averagePerDocument=True,
                                  returnDetails=True,
                                  limitSearchSpace=True)
        self.assertEqual(result["recall"], [0.5, 0.5])
        self.assertEqual(result["precision"], [1, 0.5])
        self.assertEqual(result["fMeasure"], list(hmean([[0.5, 0.5], [1, 0.5]], axis=0)))

        # test duration
        result = evalCuesMutliple([Signal(1, times=[1, 2, 3])], [SparseSegmentSignal(1, [[1, 2], [1.6, 2.2]])])
        self.assertEqual(result["recall"], 1)
        self.assertEqual(result["precision"], 2 / 3)

    def testSignal(self):
        # test sort of the values
        signal = Signal([5, 4, 3, 2, 1], times=[5, 4, 3, 2, 1])
        self.assertEqual(list(signal.values), [1, 2, 3, 4, 5])

        # test assertion of duplicate values
        # with self.assertRaises(AssertionError):
        signal = Signal([5, 4, 3, 2, 1], times=[5, 4, 3, 3, 1])

        # test Qantization: remove doubles
        signal = Signal(1, times=[0, 1.1, 2.1, 2.5, 2.6, 2.7, 3.1])
        grid = Signal(-1, times=list(range(5)))
        signal.quantizeTo(grid)
        # In case 1 value is exactly between 2 grid ticks, use the smallest one
        self.assertEqual(list(signal.values), [1, 1, 2, 3])

        # test Quantization: maxThreshold for out of bound
        signal = Signal(1, times=[0, 1.1, 2.0, 2.1, 2.6, 2.7, 3.1])
        signal.quantizeTo(grid, maxThreshold=0.2)
        self.assertEqual(list(signal.times), [0, 1, 2, 3])
        self.assertEqual(list(signal.values), [1, 1, 2, 1])

        # test quantization: don't remove duplicates
        signal = Signal(1, times=[0, 1.1, 2.0, 2.1, 2.6, 2.7, 3.1])
        signal.quantizeTo(grid, maxThreshold=0.2, removeDuplicatedValues=False)
        self.assertEqual(list(signal.times), [0, 1, 2, 2, 3])
        self.assertEqual(list(signal.values), [1, 1, 1, 1, 1])

        # test quantization: don't remove out of bounds
        signal = Signal(1, times=[0, 1.1, 2.0, 2.1, 2.6, 2.7, 3.1])
        signal.quantizeTo(grid, maxThreshold=0.2, removeDuplicatedValues=False, removeOutOfBound=False)
        self.assertEqual(list(signal.times), [0, 1, 2, 2, 2.6, 2.7, 3])
        self.assertEqual(list(signal.values), [1, 1, 1, 1, 1, 1, 1])

        # test getIndex
        signal = Signal(1, times=[0, 1.1, 2.0, 2.1, 2.6, 2.7, 3.1])
        idx = signal.getIndex(2.2, toleranceWindow=0.5)
        self.assertEqual(idx, 3)

        # test clusterSignals
        result = Signal.clusterSignals([SparseSignal(1, [0, 1, 2, 3]), SparseSignal(0, [1, 3, 3.1])], minDistance=0, mergeValue=np.mean)
        self.assertEqual(list(result.times), [0, 1, 2, 3, 3.1])
        self.assertEqual(list(result.values), [1, 0.5, 1, .5, 0])

    def testSparseSegmentSignal(self):
        signal = SparseSegmentSignal(1, [[0, 1], [1, 2]])
        idx = signal.getIndex(2.2, toleranceWindow=0.5)
        self.assertEqual(idx, 1)

        idx = signal.getIndex(1.2, toleranceWindow=0.5)
        self.assertEqual(idx, 1)

    def testWindow(self):
        # RMS value normalize by sample number
        w = Windowing()
        input = Signal(2, times=list(range(20)))
        result, = w.predictOne(input, Signal(0, times=[-1, 2.1, 3.1, 4.1, 20]))
        self.assertEqual(result[0], result[2])

    def testJamsSerialization(self):
        aggregatedSignal = JamsSerializer.aggregateAnnotations([[{
            "time": 0,
            "duration": 0,
            "confidence": 0
        }], [{
            "time": 0,
            "duration": 5,
            "confidence": 0
        }]])
        self.assertEqual(len(aggregatedSignal), 1)

    def testCoreFinder(self):
        cf = CoreFinder(parameterIncludeBorders=False)
        values = DenseSignal(range(10), 1)
        grid = SparseSignal(1, [0, 5, 10])
        result = cf.predictOne(values, grid)[0]
        self.assertEqual(result.values.tolist(), [False, True])
        self.assertEqual(result.times.tolist(), [[0, 5], [5, 10]])

        values = SparseSignal(range(10), list(range(10)))
        grid = SparseSignal(1, [0, 5, 10])
        result = cf.predictOne(values, grid)[0]
        self.assertEqual(result.values.tolist(), [False, True])
        self.assertEqual(result.times.tolist(), [[0, 5], [5, 10]])

    def testCheckerboard(self):
        """Check the addZerosStart which is not implemented yet. Needs to be updated."""
        from automix.featureExtraction.novelty import Checkerboard
        c = Checkerboard()

        values = DenseSignal(np.random.rand(100, 5), 1)
        novelty1 = Checkerboard(parameterAddZerosStart=True).predictOne(values)[0]
        novelty2 = Checkerboard(parameterAddZerosStart=0).predictOne(values)[0]
        novelty3 = Checkerboard(parameterAddZerosStart=None).predictOne(values)[0]
        self.assertGreater(novelty2[0], novelty1[0])
        self.assertEqual(novelty3[0], 0)

    def testResults(self):
        """Check that the precision is not going down with update of the code (takes time to execute)"""
        tp, gttp = config.GET_PAOLO_FULL(checkCompletude=True)
        tracks = [Track(path=path) for path in tp]
        gttracks = [JamsSerializer.deserializeTrack(track, agreement=0.5) for track in gttp]
        result = evalCuesMutliple([track.features["selectedPeaks"] for track in tracks],
                         [track.features["switchIn"] for track in gttracks],
                         limitSearchSpace=True)
        self.assertGreater(result["fMeasure"], 0.6538)

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

max_stars_repo_name: MZehren/Automix

max_issues_repo_name: MZehren/Automix

max_forks_repo_name: MZehren/Automix


text:
import os
import shutil

import azcam_testers
import numpy

import azcam
from azcam.tools.image import Image
from azcam_testers.basetester import Tester

class Defects(Tester):
    """
    Find and quantify image azcam.db.defects.
    """

    def __init__(self):

        super().__init__("defects")

        # edge mask
        self.use_edge_mask = 1  # use edge mask to ignore some pixels
        self.edge_mask = 0  # edge mask buffer
        self.edge_size = 9  # number of pixels to exclude if use_edge_mask

        # other masks
        self.dark_mask = 0
        self.bright_mask = 0
        self.defects_mask = 0

        # bright defects
        self.brightdefects_datafile = "brightdefects.txt"
        self.brightdefectsreport_rst = "brightdefects"
        self.dark_filename = "dark.fits"  # dark image for bright defects
        self.BrightPixelRejectionMask = "BrightPixelRejectionMask.png"
        self.bright_pixel_reject = -1  # reject dark image pixels above this e/pix/sec
        self.bright_defects_grade = "FAIL"
        self.bright_rejected_pixels = -1
        self.allowable_bright_pixels = -1
        self.allowable_bright_defects_per_column = -1  # allowable defects per column
        self.total_bright_pixels = -1  # total pixels with mask

        # dark defects
        self.darkdefects_datafile = "darkdefects.txt"
        self.darkdefectsreport_file = "darkdefects"
        self.DarkPixelRejectionMask = "DarkPixelRejectionMask.png"
        self.flat_filename = "superflat.fits"  # flat image for dark pixels
        self.dark_pixel_reject = -1  # reject dark pixels below this value from mean
        self.dark_defects_grade = "FAIL"
        self.dark_rejected_pixels = -1
        self.allowable_dark_pixels = -1
        self.allowable_dark_defects_per_column = -1  # allowable defects per column
        self.total_dark_pixels = -1  # total pixels with mask

        # total defects
        self.data_file = "defects.txt"
        self.report_file = "defects"
        self.defects_mask_filename = "DefectsMask.fits"
        self.PixelRejectionMask = "PixelRejectionMask.png"
        self.total_rejected_pixels = -1
        self.allowable_rejected_pixels = -1  # allowable total number of defective pixels
        self.allowable_bad_fraction = -1  # allowable total fraction of defective pixels

        self.report_include_plots = 1  # include plots in report file

    def analyze(self):
        """
        Find dark defects in a flat/superflat image.
        """

        azcam.log("Combining data from Bright and Dark analysis")

        # allowed number of rejected pixels
        # totalpixels = self.total_dark_pixels + self.total_bright_pixels

        self.allowable_rejected_pixels = int(self.totalpixels * self.allowable_bad_fraction)

        self.total_rejected_pixels = self.dark_rejected_pixels + self.bright_rejected_pixels

        if self.total_rejected_pixels <= self.allowable_rejected_pixels:
            self.grade = "PASS"
        else:
            self.grade = "FAIL"

        # make total defects mask
        self.make_defects_mask()

        # define dataset
        self.dataset = {
            "brightdefects_datafile": self.brightdefects_datafile,
            "bright_defects_grade": self.bright_defects_grade,
            "allowable_bright_pixels": self.allowable_bright_pixels,
            "bright_pixel_reject": float(self.bright_pixel_reject),
            "bright_rejected_pixels": float(self.bright_rejected_pixels),
            "darkdefects_datafile": self.darkdefects_datafile,
            "dark_defects_grade": self.dark_defects_grade,
            "allowable_dark_pixels": self.allowable_dark_pixels,
            "dark_pixel_reject": float(self.dark_pixel_reject),
            "dark_rejected_pixels": float(self.dark_rejected_pixels),
            "data_file": self.data_file,
            "grade": self.grade,
            "allowable_rejected_pixels": self.allowable_rejected_pixels,
            "total_rejected_pixels": float(self.total_rejected_pixels),
        }

        # write output files
        self.write_datafile()
        if self.create_reports:
            self.report()

        return

    def analyze_dark_defects(self):
        """
        Find dark defects in a flat/superflat image.
        """

        azcam.log("Analyzing illuminated image for dark defects")

        self.dark_defects_grade = "UNKNOWN"
        self.dark_rejected_pixels = 0

        CurrentFolder = azcam.utils.curdir()

        # reject dark pixels from superflat
        # superflat.analyze() should have been run already
        s = "Rejecting illuminated pixels below %.3f of mean" % self.dark_pixel_reject
        azcam.log(s)

        superflatimagename = azcam.utils.make_image_filename(self.flat_filename)
        self.template = superflatimagename
        NumExt, _, _ = azcam.fits.get_extensions(superflatimagename)

        superflatimage = Image(superflatimagename)

        # scale by gain
        superflatimage.set_scaling(azcam.db.gain.get_system_gain(), None)  # no offsets as ColBiased
        superflatimage.assemble(1)  # now in electrons

        # optionally mask edges
        if self.use_edge_mask:
            self.make_edge_mask(superflatimage.buffer)
        else:
            self.MaskedImage = numpy.ma.masked_invalid(superflatimage.buffer)

        # calculate stats
        totalpixels = numpy.ma.count(self.MaskedImage)  # all pixels except edge mask
        self.totalpixels = totalpixels
        self.total_dark_pixels = totalpixels
        mean = numpy.ma.mean(self.MaskedImage)

        s = "SuperflatMean= %.1f" % mean
        azcam.log(s)

        darklimit = mean * self.dark_pixel_reject
        self.MaskedImage = numpy.ma.masked_where(
            self.MaskedImage < darklimit, self.MaskedImage, copy=False
        )
        rejectedpixels = totalpixels - numpy.ma.count(self.MaskedImage)
        self.dark_rejected_pixels = rejectedpixels

        # account for allowable defects per column
        if self.allowable_bright_defects_per_column != -1:
            pass

        # self.allowable_dark_pixels = int(totalpixels * self.allowable_bad_fraction)
        s = "RejectedDarkPixels= %d (%.2f%%)" % (
            self.dark_rejected_pixels,
            (float(self.dark_rejected_pixels) / totalpixels) * 100.0,
        )
        azcam.log(s)

        if self.grade_sensor:
            if self.allowable_dark_pixels != -1:
                if rejectedpixels >= self.allowable_dark_pixels:
                    self.dark_defects_grade = "FAIL"
                else:
                    self.dark_defects_grade = "PASS"
                s = "Grade = %s" % self.dark_defects_grade
                azcam.log(s)

        # save dark mask
        fig = azcam.plot.plt.figure()
        fignum = fig.number
        azcam.plot.move_window(fignum)
        azcam.plot.plt.title("Dark Pixel Rejection Mask")
        self.dark_mask = numpy.ma.getmask(self.MaskedImage).astype("uint8")
        implot = azcam.plot.plt.imshow(self.dark_mask)
        implot.set_cmap("gray")
        azcam.plot.plt.show()
        azcam.plot.save_figure(fignum, "DarkPixelRejectionMask")

        # write mask as FITS
        maskfile = Image(superflatimagename)
        maskfile.hdulist[0].header["OBJECT"] = "dark pixel mask"
        maskfile.assemble(1)  # for parameters
        maskfile.buffer = self.dark_mask
        maskfile.save_data_format = 8
        maskfile.overwrite = 1
        maskfile.write_file("DarkPixelRejectionMask.fits", 6)

        # copy data files to initial folder
        try:
            shutil.copy("DarkPixelRejectionMask.png", CurrentFolder)
            shutil.copy("DarkPixelRejectionMask.fits", CurrentFolder)
        except Exception:
            pass

        # write output files
        azcam.utils.curdir(CurrentFolder)
        if self.create_reports:
            self.make_dark_defects_report()

        return

    def analyze_bright_defects(self):
        """
        Find bright defects in dark signal image.
        """

        azcam.log("Analyzing dark image for bright defects")

        self.bright_defects_grade = "UNKNOWN"
        self.bright_rejected_pixels = 0

        # reject bright pixels from dark median combined and colbiased image
        s = "Rejecting bright pixels above %.3f e/pix/sec" % self.bright_pixel_reject
        azcam.log(s)

        darkfilename = azcam.utils.make_image_filename(self.dark_filename)
        self.template = darkfilename
        NumExt, _, _ = azcam.fits.get_extensions(darkfilename)

        darkimage = Image(darkfilename)

        # Assemble and scale by gain
        darkimage.set_scaling(azcam.db.gain.get_system_gain(), None)
        darkimage.assemble(1)

        # scale darkimage data by exposure time and binning to get per pixel per second
        bin1 = azcam.fits.get_keyword(darkfilename, "CCDBIN1")
        bin2 = azcam.fits.get_keyword(darkfilename, "CCDBIN2")
        binned = int(bin1) * int(bin2)
        exptime = float(azcam.fits.get_keyword(darkfilename, "EXPTIME"))
        darkimage.buffer = darkimage.buffer * binned / exptime

        # optionally mask edges
        if self.use_edge_mask:
            self.make_edge_mask(darkimage.buffer)
        else:
            self.MaskedImage = numpy.ma.masked_invalid(darkimage.buffer)

        # clip to 16-bit real data
        self.MaskedImage = numpy.ma.MaskedArray.clip(self.MaskedImage, 0, 65535)
        totalpixels = numpy.ma.count(
            self.MaskedImage
        )  # total non-masked (not including masked edges)
        self.total_bright_pixels = totalpixels

        # allowed number of rejected pixels
        # self.allowable_bright_pixels = int(totalpixels * self.allowable_bad_fraction)

        self.MaskedImage = numpy.ma.masked_where(
            self.MaskedImage > self.bright_pixel_reject, self.MaskedImage, copy=False
        )

        # save dark mask
        fig = azcam.plot.plt.figure()
        fignum = fig.number
        azcam.plot.move_window(fignum)
        azcam.plot.plt.title("Bright Pixel Rejection Mask")
        self.bright_mask = numpy.ma.getmask(self.MaskedImage).astype("uint8")
        implot = azcam.plot.plt.imshow(self.bright_mask)
        implot.set_cmap("gray")
        azcam.plot.plt.show()
        azcam.plot.save_figure(fignum, "BrightPixelRejectionMask")

        # write mask as FITS
        maskfile = Image(darkfilename)
        maskfile.hdulist[0].header["OBJECT"] = "bright pixel mask"
        maskfile.assemble(1)  # for parameters
        maskfile.buffer = self.bright_mask
        maskfile.save_data_format = 8
        maskfile.overwrite = 1
        maskfile.write_file("BrightPixelRejectionMask.fits", 6)

        # count bright rejected pixels
        self.bright_rejected_pixels = totalpixels - numpy.ma.count(self.MaskedImage)

        if self.allowable_bright_defects_per_column != -1:
            pass

        # this is correct count, but mask not yet correct
        s = "BrightRejectedPixels=%d" % self.bright_rejected_pixels
        azcam.log(s)

        if self.grade_sensor:
            if self.allowable_bright_pixels != -1:
                if self.bright_rejected_pixels >= self.allowable_bright_pixels:
                    self.bright_defects_grade = "FAIL"
                else:
                    self.bright_defects_grade = "PASS"
                s = "Grade = %s" % self.bright_defects_grade
                azcam.log(s)

        # write output files
        if self.create_reports:
            self.make_bright_defects_report()

        return

    # ********************************************************************************************
    # contiguous pixels
    # ********************************************************************************************

    def find_section(
        self,
        x_coord,
        y_coord,
        num_sections_vertical,
        num_sections_horizontal,
        vertical_pixels,
        horizontal_pixels,
    ):
        """
        Find the section the group of bad pixels resides in based off a coordinate in the group. Can be used for any
        number of horizontal and vertical sections.
        """

        section_width = horizontal_pixels / num_sections_horizontal
        section_height = vertical_pixels / num_sections_vertical
        section_num = 0

        i = 1
        j = 1

        # Make sure values are 1 or larger
        if num_sections_vertical < 1 & num_sections_horizontal < 1:
            pass

        else:
            # Determine section number
            # Go through horizontal sections first
            while (i <= num_sections_horizontal) & (section_num == 0):
                if x_coord < (section_width * i):
                    # When horizontal section has been found, go through vertical sections
                    while j <= num_sections_vertical:
                        if y_coord < (section_height * j):
                            if j == 1:
                                section_num = i
                                return section_num
                            else:
                                section_num = i + (num_sections_horizontal * (j - 1))
                                return section_num

                        else:
                            j = j + 1

                else:
                    i = i + 1

        # Return statements in while loop

    def make_defects_mask(self):
        """
        Create a single mask from Edge Mask, Bright mask, and Dark mask.
        Execute AFTER dark and bright defects are found.
        """

        # combine masks, values may be > 1
        self.defects_mask = self.bright_mask + self.dark_mask  # + self.EdgeMask

        fig = azcam.plot.plt.figure()
        fignum = fig.number
        azcam.plot.move_window(fignum)
        azcam.plot.plt.title("Pixel Rejection Mask")
        implot = azcam.plot.plt.imshow(self.defects_mask.astype("uint8"))
        implot.set_cmap("gray")
        azcam.plot.plt.show()
        azcam.plot.save_figure(fignum, "PixelRejectionMask")

        # write mask as FITS
        defectsmask = Image(self.template)  # just a template
        defectsmask.assemble(1)
        # defectsmask.buffer=numpy.ma.getmask(self.DefectsMask).astype('uint8')
        defectsmask.buffer = self.defects_mask.astype("uint8")
        defectsmask.save_data_format = 8
        defectsmask.overwrite = 1
        defectsmask.write_file(self.defects_mask_filename, 6)
        self.defects_mask_filename = os.path.abspath(self.defects_mask_filename)

        self.valid = True  # defects mask in now valid

        return

    def make_edge_mask(self, ImageBuffer, EdgeSize=-1):
        """
        Creates a masked image from ImageBuffer which has its edges masked.
        Inp: EdgeSize (or self.EdgeSize) is the integer number of pixels closest to edge to mask.
        Out: self.MaskedImage is a numpy masked image array with edges masked set to True.
        Out: self.EdgeMask is the numpy mask array associated with self. MaskedImage.
        """

        if not self.use_edge_mask:
            return

        # create new masked array (True in mask is INVALID data which is the edges)
        self.MaskedImage = numpy.ma.masked_invalid(ImageBuffer)  # masked array
        self.edge_mask = numpy.ma.getmask(self.MaskedImage)  # mask bool data only

        # edge size
        if EdgeSize != -1:
            es = EdgeSize
        else:
            es = self.edge_size

        # set edge mask
        nrows, ncols = ImageBuffer.shape
        for y in range(nrows):
            for x in range(ncols):
                if x - es < 0:
                    self.edge_mask[y, x] = True
                elif x + es + 1 > ncols:
                    self.edge_mask[y, x] = True
                elif y - es < 0:
                    self.edge_mask[y, x] = True
                elif y + es + 1 > nrows:
                    self.edge_mask[y, x] = True

        return

    def read_defects_mask(self, filename=""):
        """
        Read a defects mask (FITS format).
        """

        if filename == "":
            filename = self.defects_mask_filename

        defectsimage = Image(filename)
        defectsimage.assemble(1)
        self.DefectsImage = defectsimage

        # self.DefectsMask=numpy.ma.masked_invalid(defectsimage.buffer)
        self.defects_mask = numpy.ma.masked_where(
            defectsimage.buffer > 0, defectsimage.buffer
        )  # 0 or 1, not >1

        self.valid = True  # defects mask in now valid

        del defectsimage

        return

    def get_defect_coords(self, mask_in):
        """
        Get the coordinates [col,row] of defects, not including edge mask.
        """

        nrows = len(mask_in)
        ncols = len(mask_in[0])

        # mask=numpy.ma.getmask(mask_in)
        coords = mask_in.nonzero()
        lc = len(coords[0])

        coords1 = []
        for i in range(lc):
            row = coords[0][i]
            col = coords[1][i]

            if self.use_edge_mask:
                if row < self.edge_size:
                    pass
                elif row >= (nrows - self.edge_size):
                    pass
                elif col < self.edge_size:
                    pass
                elif col >= (ncols - self.edge_size):
                    pass
                else:
                    coords1.append([col, row])
            else:
                coords1.append([col, row])

        # return [col,row] pairs
        coords2 = sorted(coords1, key=lambda k: [k[0], k[1]])
        return coords2

    def plot_defects_mask(self):
        """
        Plot the defective pixel mask.
        """

        fig = azcam.plot.plt.figure()
        fignum = fig.number
        azcam.plot.move_window(fignum)
        azcam.plot.plt.title("Pixel Rejection Mask")
        implot = azcam.plot.plt.imshow(self.defects_mask.astype("uint8"))
        implot.set_cmap("gray")
        azcam.plot.plt.show()
        azcam.plot.save_figure(fignum, "PixelRejectionMask")

        return

    def make_bright_defects_report(self):
        """
        Write bright defects report file.
        """

        lines = []

        lines.append("# Bright Defects Analysis")
        lines.append("")
        lines.append(
            f"Rejecting bright pixels above {self.bright_pixel_reject:.01f} electrons/pix/sec"
        )
        lines.append("")
        lines.append(f"Number of bright rejected: {self.bright_rejected_pixels}")
        lines.append("")
        lines.append(f"Allowable bad pixels: {self.allowable_bright_pixels}")
        lines.append("")

        # add plots
        if self.report_include_plots:
            lines.append(
                f"![Bright pixel rejection mask]({os.path.abspath(self.BrightPixelRejectionMask)}) "
            )
            lines.append("")
            lines.append("*Bright pixel rejection mask.*")
            lines.append("")

        # Make report files
        self.write_report(self.brightdefectsreport_rst, lines)

        return

    def make_dark_defects_report(self):
        """
        Write dark defects report file.
        """

        lines = []

        lines.append("# Dark Defects Analysis")
        lines.append("")
        lines.append(f"Rejecting dark pixels below {self.dark_pixel_reject:.03f} mean")
        lines.append("")
        lines.append(f"Number of dark rejected: {self.dark_rejected_pixels}")
        lines.append("")
        lines.append(f"Allowable bad pixels: {self.allowable_dark_pixels}")
        lines.append("")

        # add plots
        if self.report_include_plots:
            lines.append(
                f"![Dark pixel rejection mask]({os.path.abspath(self.DarkPixelRejectionMask)}) "
            )
            lines.append("")
            lines.append("*Dark pixel rejection mask.*")
            lines.append("")

        # Make report files
        self.write_report(self.darkdefectsreport_file, lines)

        return

    def report(self):
        """
        Write defects report file for Bright and Dark.
        Run only after all analysis has been completed.
        """

        lines = []

        lines.append("# Defects Analysis")
        lines.append("")
        if self.grade != "UNDEFINED":
            lines.append(f"Total defects grade: {self.grade}")
            lines.append("")
        lines.append(f"Number of pixels rejected: {self.total_rejected_pixels}")
        lines.append("")
        lines.append(f"Allowable total bad pixels: {self.allowable_rejected_pixels}")
        lines.append("")

        lines.append("# Bright Defects Analysis")
        lines.append("")
        lines.append(
            f"Rejecting bright pixels above {self.bright_pixel_reject:.01f} electrons/pix/sec"
        )
        lines.append("")
        lines.append(f"Number of bright rejected pixels: {self.bright_rejected_pixels}")
        lines.append("")
        lines.append(f"Allowable bright pixels: {self.allowable_bright_pixels}")
        lines.append("")

        lines.append("# Dark Defects Analysis")
        lines.append("")
        lines.append(f"Rejecting dark pixels below {self.dark_pixel_reject:.03f} mean")
        lines.append("")
        lines.append(f"Number of dark rejected pixels: {self.dark_rejected_pixels}")
        lines.append("")
        lines.append(f"Allowable dark pixels: {self.allowable_dark_pixels}")
        lines.append("")

        if self.report_include_plots:
            lines.append('<div style="page-break-after: always;"></div>')

            lines.append(
                f"![Bright pixel rejection mask]({os.path.abspath(self.BrightPixelRejectionMask)}) "
            )
            lines.append("")
            lines.append("*Bright pixel rejection mask.*")
            lines.append("")

            lines.append(
                f"![Dark pixel rejection mask]({os.path.abspath(self.DarkPixelRejectionMask)}) "
            )
            lines.append("")
            lines.append("*Dark pixel rejection mask.*")
            lines.append("")

            lines.append(
                f"![Total pixel rejection mask]({os.path.abspath(self.PixelRejectionMask)}) "
            )
            lines.append("")
            lines.append("*Total pixel rejection mask.*")
            lines.append("")

        # Make report files
        self.write_report(self.report_file, lines)

        return

    def copy_data_files(self, folder=""):
        """
        Copy data files to proper report folder.
        """

        files = [
            "brightdefects.pdf",
            "brightdefects.md",
            "brightdefects.txt",
            "BrightPixelRejectionMask.png",
            "BrightPixelRejectionMask.fits",
            "darkdefects.pdf",
            "darkdefects.md",
            "darkdefects.txt",
            "DarkPixelRejectionMask.png",
            "DarkPixelRejectionMask.fits",
        ]

        # destination folder
        if folder == "":
            fldr = os.path.abspath("../defects")
        else:
            fldr = os.path.abspath(folder)

        if os.path.exists(fldr):
            azcam.log("Existing defects folder: %s" % fldr)
        else:
            azcam.log("Creating new defects folder: %s" % fldr)
            os.mkdir(fldr)

        for f in files:
            azcam.log("Copying: %s" % f)
            # allow errors as some files may not exist
            try:
                shutil.copy(f, fldr)
            except Exception:
                pass

        return

max_stars_repo_name: mplesser/azcam-testers

max_issues_repo_name: mplesser/azcam-testers

max_forks_repo_name: mplesser/azcam-testers


text:
import gym.spaces as spaces
import numpy as np
import matplotlib.pyplot as plt
from rlberry.agents.adaptiveql.utils import bounds_contains, split_bounds

class TreeNode:
    """
    Node representing an l-infinity ball in R^d, that points
    to sub-balls (node children).
    Stores a value, a number of visits, and (possibly) rewards and transition probability
    to a list of other nodes.

    This class is used to represent (and store data about)
    a tuple (state, action, stage) = (x, a, h).

    Parameters
    ----------
    bounds : numpy.ndarray
        Bounds of each dimension [ [x0, y0], [x1, y1], ..., [xd, yd] ],
        representing the cartesian product in R^d:
        [x0, y0] X [x1, y1] X ... X [xd, yd]
    depth: int
        Node depth, root is at depth 0.
    qvalue : double, default: 0
        Initial node Q value
    n_visits : int, default = 0
        Number of visits to the node.

    """

    def __init__(self, bounds, depth, qvalue=0.0, n_visits=0):
        self.dim = len(bounds)

        self.radius = (bounds[:, 1] - bounds[:, 0]).max() / 2.0
        assert self.radius > 0.0

        self.bounds = bounds
        self.depth = depth
        self.qvalue = qvalue
        self.n_visits = n_visits
        self.children = []

        #
        # For AdaMB
        #

        # Value V, initialized as Q
        self.vvalue = qvalue
        # Reward estimate
        self.reward_est = 0.0
        # Dictionary node_id -> transition_prob
        # node_id = id(node), where id() is a built-in python function
        self.transition_probs = {}
        # Dictionary node_id -> node
        self.transition_nodes = {}

    def is_leaf(self):
        return len(self.children) == 0

    def contains(self, x):
        """Check if `x` is contained in the node/ball."""
        return bounds_contains(self.bounds, x)

    def split(self):
        """Spawn children nodes by splitting the ball."""
        child_bounds = split_bounds(self.bounds)
        for bounds in child_bounds:
            self.children.append(
                TreeNode(bounds, self.depth + 1, self.qvalue, self.n_visits)
            )

class TreePartition:
    """
    Tree-based partition of an l-infinity ball in R^d.

    Each node is of type TreeNode.

    Parameters
    ----------
    space: gym.spaces.Box
        Domain of the function.
    initial_value: double
        Value to initialize the root node.
    """

    def __init__(self, space, initial_value=0.0):
        assert isinstance(space, spaces.Box)
        assert space.is_bounded()

        bounds = np.vstack((space.low, space.high)).T
        self.root = TreeNode(bounds, depth=0, qvalue=initial_value)
        self.dim = bounds.shape[0]
        self.dmax = self.root.radius

    def traverse(self, x, update=False):
        """
        Returns leaf node containing x.

        If `update=true`, increments number of visits of each
        node in the path.

        Parameters
        ----------
        x : numpy.ndarray
            Array of shape (d,)
        """
        node = self.root

        # traverse the tree until leaf
        while True:
            if update:
                node.n_visits += 1
            if node.is_leaf():
                break
            for cc in node.children:
                if cc.contains(x):
                    node = cc
                    break

        # return value at leaf
        return node

    def plot(self,
             fignum="tree plot",
             colormap_name='cool',
             max_value=10,
             node=None,
             root=True, ):
        """
        Visualize the function (2d domain only).
        Shows the hierarchical partition.
        """
        if root:
            assert self.dim == 2, "TreePartition plot only available for 2-dimensional spaces."
            node = self.root
            plt.figure(fignum)

        # draw region corresponding to the leaf
        if node.is_leaf():
            x0, x1 = node.bounds[0, :]
            y0, y1 = node.bounds[1, :]

            colormap_fn = plt.get_cmap(colormap_name)
            color = colormap_fn(node.qvalue / max_value)
            rectangle = plt.Rectangle((x0, y0), x1 - x0, y1 - y0, ec="black", color=color)
            plt.gca().add_patch(rectangle)
            plt.axis('scaled')

        else:
            for cc in node.children:
                self.plot(max_value=max_value, colormap_name=colormap_name, node=cc, root=False)

class MDPTreePartition:
    """
    Set of H x A TreePartition instances.

    Used to store/manipulate a Q function, a reward function and a transition model.
    """

    def __init__(self, observation_space, action_space, horizon):
        self.horizon = horizon
        self.n_actions = action_space.n
        self.trees = []
        for hh in range(horizon):
            self.trees.append({})
            for aa in range(self.n_actions):
                self.trees[hh][aa] = TreePartition(observation_space,
                                                   initial_value=horizon - hh)

        self.dmax = self.trees[0][0].dmax

    def get_argmax_and_node(self, x, hh):
        """
        Returns a* = argmax_a Q_h(x, a) and the node corresponding to (x, a*).
        """
        # trees for each action at hh
        trees_hh = self.trees[hh]

        best_action = 0
        best_node = trees_hh[0].traverse(x, update=False)
        best_val = best_node.qvalue
        for aa in range(1, self.n_actions):
            node = trees_hh[aa].traverse(x, update=False)
            val = node.qvalue
            if val > best_val:
                best_val = val
                best_action = aa
                best_node = node

        return best_action, best_node

    def update_counts(self, x, aa, hh):
        """
        Increment counters associated to (x, aa, hh) and returns the node.
        """
        tree = self.trees[hh][aa]
        node = tree.traverse(x, update=True)
        return node

    def plot(self, a, h):
        """
        Visualize Q_h(x, a)
        """
        self.trees[h][a].plot(max_value=self.horizon - h)

max_stars_repo_name: rlberry-py/rlberry

max_issues_repo_name: rlberry-py/rlberry

max_forks_repo_name: rlberry-py/rlberry


text:
# myls.py
# Import the argparse library
from compute_M import compute_M
from plot_cheaters import plot_cheaters
from plot_population import plot_population
from plot_initial import plot_initial
from compute_cheaters import compute_cheaters
import argparse
from random import randrange

import numpy as np
from collections import Counter

import os
import sys

# Create the parser
my_parser = argparse.ArgumentParser(description='List the content of a folder')

# Add the arguments
my_parser.add_argument('-P',
                       action='store',
                       default=10000,
                       type=int,
                       help="Number of town's inhabitants.")
my_parser.add_argument('-I',
                       action='store',
                       # dest=I,
                       default=100,
                       type=int,
                       help="Number of initial positive cases")
my_parser.add_argument('-N',
                       action='store',
                       type=int,
                       help="Number of 'cheaters' or else people that ignore lockdown measures and visit other houses")
my_parser.add_argument('-S',
                       action='store',
                       default=1,
                       type=float,
                       help="Number of initial positive cases")
my_parser.add_argument('-D1',
                       action='store',
                       default=3,
                       type=int,
                       help="Days after on which an infected person becomes infectious")
my_parser.add_argument('-D2',
                       action='store',
                       default=10,
                       type=int,
                       help=" Days after on which an infected person stops being infectious")

my_parser.add_argument('--compute_M',
                       action='store_true',
                       help="Prints M for the given set of parameters")
my_parser.add_argument('--plot_population',
                       nargs=3,
                       type=int,
                       action='store',
                       help=" shows or saves (whatever you want) a plot. On X axis are the values of the population (parameter P) from A to B with C intermediate steps (use np.linspace(A, B, C)). On Y axis there are the corresponding M values.")
my_parser.add_argument('--plot_initial',
                       nargs=3,
                       type=int,
                       action='store',
                       help="shows or saves (whatever you want) a plot. On X axis are the initial cases (parameter I) from A to B with C steps (use np.linspace(A, B, C)). On Y axis there are the corresponding M values.")
my_parser.add_argument('--plot_cheaters',
                       nargs=3,
                       type=int,
                       action='store',
                       help="shows or saves (whatever you want) a plot. On X axis are the cheaters (parameter N) from A to B with C steps (use np.linspace(A, B, C)). On Y axis there are the corresponding M values")
my_parser.add_argument('--compute_cheaters',
                       nargs=1,
                       type=int,
                       action='store',
                       help="prints the maximum number of cheaters (N) that can be if don't want Μ to go over the given value.")

# Execute parse_args()
args = my_parser.parse_args()
# print("You passed "+(str)(len(sys.argv)-1)+" arguments");

print(vars(args))

if vars(args)['N'] is None and vars(args)['compute_cheaters'] is None and vars(args)['plot_cheaters'] is None:
    raise Exception('You have to specify N.')

activeElem = 0
list = [args.compute_M, args.plot_population, args.plot_initial, args.compute_cheaters, args.plot_cheaters]
# print(list)
for argument in list:
    # print(argument)
    if argument:
        if not argument is None:
            activeElem = activeElem + 1
            # print(activeElem)

if activeElem > 1:
    raise Exception('You have specified more than one operation.')

P = vars(args)["P"]
I = vars(args)["I"]
N = vars(args)["N"]
D1 = vars(args)["D1"]
D2 = vars(args)["D2"]
S = vars(args)["S"]

if args.compute_M:
    compute_M(P, I, N, D1, D2, S)
elif args.plot_population:
    temp_list = (vars(args)["plot_population"])
    A = temp_list[0]
    B = temp_list[1]
    C = temp_list[2]
    plot_population(A, B, C, I, N, D1, D2, S)

elif args.plot_initial:
    temp_list = (vars(args)["plot_initial"])
    A = temp_list[0]
    B = temp_list[1]
    C = temp_list[2]
    plot_initial(A, B, C, P, N, D1, D2, S)

elif args.compute_cheaters:
    M = vars(args)["compute_cheaters"][0]
    N = compute_cheaters(M, P, I, D1, D2, S)
    print(N)
elif args.plot_cheaters:
    temp_list = (vars(args)["plot_cheaters"])
    print(temp_list)
    A = temp_list[0]
    B = temp_list[1]
    C = temp_list[2]
    print(A,B,C)
    N = plot_cheaters(A, B, C, P, I, D1, D2, S)


text:
import cv2
import numpy as np

image_size = 128
num_channels = 3

def get_images_formatted(path):
    images = []
    image = cv2.imread(path)
    image = cv2.resize(image, (image_size, image_size), 0, 0, cv2.INTER_LINEAR)
    images.append(image)
    images = np.array(images, dtype=np.uint8)
    images = images.astype('float32')
    images = np.multiply(images, 1.0 / 255.0)
    x_batch_temp = images.reshape(1, image_size, image_size, num_channels)
    return x_batch_temp

max_stars_repo_name: eshanMewantha/image-processing

max_issues_repo_name: eshanMewantha/image-processing

max_forks_repo_name: eshanMewantha/image-processing


text:
import torch.utils.data as data
import sys

sys.path.append('/home/benkesheng/BMI_DETECT/')

from sklearn.metrics import mean_absolute_error

from sklearn.svm import SVR

from Detected import Image_Processor
import torch
import torch.nn as nn
import torch.utils.data as data
from torchvision import models, transforms
import numpy as np
import os
import pandas as pd
import cv2
import re
import csv
from PIL import Image
from Data import Img_info
import random

def setup_seed(seed):
    torch.manual_seed(seed)
    torch.cuda.manual_seed(seed)
    np.random.seed(seed)
    random.seed(seed)
    torch.backends.cudnn.deterministic = True

setup_seed(20)

END_EPOCH = 0

mask_model = "/home/benkesheng/BMI_DETECT/pose2seg_release.pkl"
keypoints_model = "COCO-Keypoints/keypoint_rcnn_R_101_FPN_3x.yaml"
# P = Image_Processor(mask_model,keypoints_model)
IMG_MEAN = [0.485, 0.456, 0.406]
IMG_STD = [0.229, 0.224, 0.225]
DEVICE = torch.device("cuda:3")
IMG_SIZE = 224
BATCH_SIZE = 64

def _get_image_size(img):
    if transforms.functional._is_pil_image(img):
        return img.size
    elif isinstance(img, torch.Tensor) and img.dim() > 2:
        return img.shape[-2:][::-1]
    else:
        raise TypeError("Unexpected type {}".format(type(img)))

class Resize(transforms.Resize):

    def __call__(self, img):
        h, w = _get_image_size(img)
        scale = max(w, h) / float(self.size)
        new_w, new_h = int(w / scale), int(h / scale)
        return transforms.functional.resize(img, (new_w, new_h), self.interpolation)

class Dataset(data.Dataset):
    def __init__(self, file, transfrom):
        self.Pic_Names = os.listdir(file)
        self.file = file
        self.transfrom = transfrom

    def __len__(self):
        return len(self.Pic_Names)

    def __getitem__(self, idx):
        img_name = self.Pic_Names[idx]
        Pic = Image.open(os.path.join(self.file, self.Pic_Names[idx]))
        Pic = self.transfrom(Pic)
        try:
            ret = re.match(r"\d+?_([FMfm])_(\d+?)_(\d+?)_(\d+).+", img_name)
            BMI = (int(ret.group(4)) / 100000) / (int(ret.group(3)) / 100000) ** 2
            Pic_name = os.path.join(self.file, self.Pic_Names[idx])
            return (Pic, Pic_name), BMI
        except:
            return (Pic, ''), 10000

transform = transforms.Compose([
    Resize(IMG_SIZE),
    transforms.Pad(IMG_SIZE),
    transforms.CenterCrop(IMG_SIZE),
    transforms.ToTensor(),
    transforms.Normalize(IMG_MEAN, IMG_STD)
])

dataset = Dataset('/home/benkesheng/BMI_DETECT/datasets/Image_train', transform)
# val_dataset = Dataset('/home/benkesheng/BMI_DETECT/datasets/Image_val', transform)
test_dataset = Dataset('/home/benkesheng/BMI_DETECT/datasets/Image_test', transform)

train_size = int(0.8 * len(dataset))
val_size = len(dataset) - train_size
train_dataset, val_dataset = torch.utils.data.random_split(dataset, [train_size, val_size])

train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=BATCH_SIZE, shuffle=True)
val_loader = torch.utils.data.DataLoader(val_dataset, batch_size=1, shuffle=True)
test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=1, shuffle=True)

# Vgg16
# Pred_Net = torchvision.models.vgg16(pretrained=True)
# for param in Pred_Net.parameters():
#     param.requires_grad = True
#
# Pred_Net.classifier = nn.Sequential(
#     nn.Linear(25088, 1024),
#     nn.ReLU(True),
#     nn.Linear(1024, 512),
#     nn.ReLU(True),
#     nn.Linear(512, 256),
#     nn.ReLU(True),
#     nn.Linear(256, 20),
#     nn.ReLU(True),
#     nn.Linear(20, 1)
# )

# Resnet101

Pred_Net = models.resnet101(pretrained=True,num_classes= 1)
print(Pred_Net)
for param in Pred_Net.parameters():
    param.requires_grad = True
# Pred_Net.fc = nn.Sequential(
#     nn.Linear(2048, 1024),
#     nn.ReLU(True),
#     nn.Linear(1024, 512),
#     nn.ReLU(True),
#     nn.Linear(512, 256),
#     nn.ReLU(True),
#     nn.Linear(256, 20),
#     nn.ReLU(True),
#     nn.Linear(20, 1)
# )

Pred_Net = Pred_Net.to(DEVICE)

criterion = nn.MSELoss()
optimizer = torch.optim.Adam([
    {'params': Pred_Net.parameters()}
], lr=0.0001)

def train(model, device, train_loader, epoch):
    model.train()
    runing_loss = 0.0
    for idx, ((x, n), y) in enumerate(train_loader, 0):
        x, y = x.to(device), y.to(device)
        optimizer.zero_grad()
        y_pred = model(x)
        #         print(y_pred.shape)
        y = torch.unsqueeze(y, 1)
        loss = criterion(y_pred.double(), y.double())
        loss.backward()
        optimizer.step()

        runing_loss += loss.item()
    print('loss:', loss.item())
    print('Train Epoch:{}\t RealLoss:{:.6f}'.format(epoch, runing_loss / len(train_loader)))

def mean_absolute_percentage_error(y_true, y_pred):
    y_true, y_pred = np.array(y_true), np.array(y_pred)
    return np.mean(np.abs((y_true - y_pred) / y_true)) * 100

def test(model, device, test_loader):
    model.eval()
    pred = []
    targ = []
    with torch.no_grad():
        for i, ((x, n), y) in enumerate(test_loader):
            x, y = x.to(device), y.to(device)
            # optimizer.zero_grad()
            y_pred = model(x)
            pred.append(y_pred.item())
            targ.append(y.item())
            y = torch.unsqueeze(y, 1)
    MAE = mean_absolute_error(targ, pred)
    MAPE = mean_absolute_percentage_error(targ, pred)
    print('\nTest MAE:{}\t Test MAPE:{} '.format(MAE, MAPE))
    return MAE, MAPE

MIN_MAE, MAPE = test(Pred_Net, DEVICE, val_loader)
for epoch in range(100):
    print('*' * 50)
    train(Pred_Net, DEVICE, train_loader, epoch)
    val_MAE, val_MAPE = test(Pred_Net, DEVICE, val_loader)
    if val_MAE < MIN_MAE:
        MIN_MAE = val_MAE
        torch.save(Pred_Net.state_dict(), '/home/benkesheng/BMI_DETECT/MODEL/param/MIN_RESNET101_BMI_Cache_test.pkl')
        END_EPOCH = epoch
Net = Pred_Net
Net.load_state_dict(torch.load('/home/benkesheng/BMI_DETECT/MODEL/param/MIN_RESNET101_BMI_Cache.pkl'))
Net = Net.to(DEVICE)

print('=' * 50)
Net.eval()
test(Net, DEVICE, test_loader)
# Net.train()
# train(Net, DEVICE, train_loader, 1)

print('END_EPOCH:', END_EPOCH)
print('=' * 50)

# class Dataset(data.Dataset):
#     def __init__(self, file, transfrom):
#         self.Pic_Names = os.listdir(file)
#         self.file = file
#         self.transfrom = transfrom
#
#     def __len__(self):
#         return len(self.Pic_Names)
#
#     def __getitem__(self, idx):
#         img_name = self.Pic_Names[idx]
#         Pic = Image.open(os.path.join(self.file, self.Pic_Names[idx]))
#         Pic = self.transfrom(Pic)
#
#         ret = re.match(r"\d+?_([FMfm])_(\d+?)_(\d+?)_(\d+).+", img_name)
#         sex = 0 if (ret.group(1) == 'F' or ret.group(1) == 'f') else 1
#         age = int(ret.group(2))
#         height = int(ret.group(3)) / 100000
#         weight = int(ret.group(4)) / 100000
#         BMI = weight / (height ** 2)
#         #         BMI = (int(ret.group(4))/100000) / (int(ret.group(3))/100000)**2
#         Pic_name = os.path.join(self.file, self.Pic_Names[idx])
#         return (Pic, Pic_name, img_name, sex, age, height, weight), BMI
#
#
# dataset_train = Dataset('/home/benkesheng/BMI_DETECT/datasets/Image_train', transform)
# # dataset_val = Dataset('/home/benkesheng/BMI_DETECT/datasets/Image_val',transform)
# dataset_test = Dataset('/home/benkesheng/BMI_DETECT/datasets/Image_test', transform)
# loader_train = torch.utils.data.DataLoader(dataset_train, batch_size=1, shuffle=True)
# loader_test = torch.utils.data.DataLoader(dataset_test, batch_size=1, shuffle=True)
#
#
# # loader_val = torch.utils.data.DataLoader(dataset_val,batch_size=1,shuffle=True)
#
# class LayerActivations:
#     features = None
#
#     def __init__(self, model, layer_num):
#         self.hook = model[layer_num].register_forward_hook(self.hook_fn)
#
#     def hook_fn(self, module, input, output):
#         self.features = output.cpu()
#
#     def remove(self):
#         self.hook.remove()
#
#
# import time
#
#
# # with open('/home/benkesheng/BMI_DETECT/ReDone_CSV/HaveArms/Image_train1.csv', 'w', newline='') as fp:
# #     writer = csv.writer(fp)
# #     cnt = 1
# #     loaders = [loader_train]
# #     for loader in loaders:
# #         for (data, name, img_name, sex, age, height, weight), target in loader:
# #             try:
# #             # if(1):
# #                 if target.numpy()[0] <= 10 or target.numpy()[0] > 100:
# #                     continue
# #                 values = []
# #                 data = data.to(DEVICE)
# #
# #                 values.append(img_name[0])
# #                 values.append(target.numpy()[0])
# #                 values.append(sex.numpy()[0])
# #
# #                 cnt += 1
# #                 t0 = time.time()
# #                 img_e = cv2.imread(name[0])
# #                 print('Handling the pic %s' % img_name[0])
# #                 F = P.Process(img_e)
# #                 print('The body features detected cost %.3f s' % (time.time() - t0))
# #                 values.append(F.WSR)
# #                 values.append(F.WTR)
# #                 values.append(F.WHpR)
# #                 values.append(F.WHdR)
# #                 values.append(F.HpHdR)
# #                 values.append(F.Area)
# #                 values.append(F.H2W)
# #                 conv_out = LayerActivations(Net.fc, 6)
# #                 t1 = time.time()
# #                 out = Net(data.to(DEVICE))
# #                 conv_out.remove()
# #                 xs = torch.squeeze(conv_out.features.detach()).numpy()
# #                 print('The deep features detected cost %.3f s' % (time.time() - t1))
# #                 for x in xs:
# #                     values.append(x)
# #
# #                 values.append(age.numpy()[0])
# #                 values.append(height.numpy()[0])
# #                 values.append(weight.numpy()[0])
# #                 writer.writerow(values)
# #                 print('The %d pic %s cost %.3f s' % (cnt, img_name[0], time.time() - t0))
# #                 print('The shape of the pic is %d * %d' % (img_e.shape[0], img_e.shape[1]))
# #                 print('*' * 40)
# #             except:
# #                 print('error')
# #                 continue
# #
# # with open('/home/benkesheng/BMI_DETECT/ReDone_CSV/HaveArms/Image_test1.csv', 'w', newline='') as fp:
# #     writer = csv.writer(fp)
# #     cnt = 0
# #     for (data, name, img_name, sex, age, height, weight), target in loader_test:
# #         try:
# #             if target.numpy()[0] <= 10 or target.numpy()[0] > 100:
# #                 continue
# #             values = []
# #             data = data.to(DEVICE)
# #
# #             values.append(img_name[0])
# #             values.append(target.numpy()[0])
# #             values.append(sex.numpy()[0])
# #
# #             print(img_name[0], '\t', str(cnt))
# #             cnt += 1
# #             img_e = cv2.imread(name[0])
# #             F = P.Process(img_e)
# #             values.append(F.WSR)
# #             values.append(F.WTR)
# #             values.append(F.WHpR)
# #             values.append(F.WHdR)
# #             values.append(F.HpHdR)
# #             values.append(F.Area)
# #             values.append(F.H2W)
# #             conv_out = LayerActivations(Net.fc, 6)
# #             out = Net(data.to(DEVICE))
# #             conv_out.remove()
# #             xs = torch.squeeze(conv_out.features.detach()).numpy()
# #             #             print(xs)
# #             for x in xs:
# #                 values.append(x)
# #
# #             values.append(age.numpy()[0])
# #             values.append(height.numpy()[0])
# #             values.append(weight.numpy()[0])
# #             writer.writerow(values)
# #         except:
# #             print('error')
# #             continue
#
#
# def Pre(raw_data):
#     raw_data = raw_data.iloc[:, 1:]
#     raw_data = raw_data.replace([np.inf, -np.inf], np.nan)
#     raw_data = raw_data.replace(np.nan, 0)
#     raw_data = raw_data.values
#     return raw_data
#
#
# def Data(raw_data):
#     x_5f = raw_data[:, 3:8]
#     x_7f = raw_data[:, 2:9]
#     x_20f = raw_data[:, 9:29]
#     y = raw_data[:, 0]
#     return x_5f, x_7f, x_20f, y
#
#
# raw_data = pd.read_csv('/home/benkesheng/BMI_DETECT/ReDone_CSV/HaveArms/Image_train.csv')
# raw_data = Pre(raw_data)
#
# x_5f_tr, x_7f_tr, x_20f_tr, y_train = Data(raw_data)
#
# x_7f_sm = raw_data[:, 2:9]
# x_5f_sm = raw_data[:, 3:8]
# Mean_7f = np.mean(x_7f_sm, axis=0)
# Std_7f = np.std(x_7f_sm, axis=0)
# Mean_5f = np.mean(x_5f_sm, axis=0)
# Std_5f = np.std(x_5f_sm, axis=0)
#
# # x_7f_tr = (x_7f_tr - Mean_7f)/Std_7f
# # x_5f_tr = (x_5f_tr - Mean_5f)/Std_5f
# x_train = np.append(x_7f_tr, x_20f_tr, axis=1)
# y_train = y_train
#
# raw_data_test = pd.read_csv('/home/benkesheng/BMI_DETECT/ReDone_CSV/HaveArms/Image_test.csv')
# raw_data_test = Pre(raw_data_test)
#
# x_5f, x_7f, x_20f, y_test = Data(raw_data_test)
#
# # x_7f = (x_7f - Mean_7f) / Std_7f
# # x_5f = (x_5f - Mean_5f) / Std_5f
#
# x_test = np.append(x_7f, x_20f, axis=1)
# y_test = y_test
#
# from sklearn.kernel_ridge import KernelRidge
# from sklearn.gaussian_process.kernels import DotProduct, WhiteKernel
# import sklearn.gaussian_process
#
# svr1 = SVR()
# svr2 = SVR()
# svr3 = SVR()
# kr1 = KernelRidge()
# kr2 = KernelRidge()
# kr3 = KernelRidge()
# kernel = DotProduct() + WhiteKernel()
# gpr1 = sklearn.gaussian_process.GaussianProcessRegressor()
# gpr2 = sklearn.gaussian_process.GaussianProcessRegressor()
# gpr3 = sklearn.gaussian_process.GaussianProcessRegressor()
#
# svr1.fit(x_train, y_train)
# svr2.fit(x_20f_tr, y_train)
# svr3.fit(np.append(x_5f_tr, x_20f_tr, axis=1), y_train)
# kr1.fit(x_train, y_train)
# kr2.fit(x_20f_tr, y_train)
# kr3.fit(np.append(x_5f_tr, x_20f_tr, axis=1), y_train)
# gpr1.fit(x_train, y_train)
# gpr2.fit(x_20f_tr, y_train)
# gpr3.fit(np.append(x_5f_tr, x_20f_tr, axis=1), y_train)
#
# y_svr1 = svr1.predict(x_test)
# y_svr2 = svr2.predict(x_20f)
# y_svr3 = svr3.predict(np.append(x_5f, x_20f, axis=1))
# y_kr1 = kr1.predict(x_test)
# y_kr2 = kr2.predict(x_20f)
# y_kr3 = kr3.predict(np.append(x_5f, x_20f, axis=1))
# y_gpr1 = gpr1.predict(x_test)
# y_gpr2 = gpr2.predict(x_20f)
# y_gpr3 = gpr3.predict(np.append(x_5f, x_20f, axis=1))
#
#
# def mean_absolute_percentage_error(y_true, y_pred):
#     y_true, y_pred = np.array(y_true), np.array(y_pred)
#     return np.mean(np.abs((y_true - y_pred) / y_true)) * 100
#
#
# print('SVR1 7+20: MAE: ', mean_absolute_error(y_test, y_svr1), ' MAPE: ',
#       mean_absolute_percentage_error(y_test, y_svr1))
# print('SVR2 20: MAE: ', mean_absolute_error(y_test, y_svr2), ' MAPE: ', mean_absolute_percentage_error(y_test, y_svr2))
# print('SVR3 5+20: MAE: ', mean_absolute_error(y_test, y_svr3), ' MAPE: ',
#       mean_absolute_percentage_error(y_test, y_svr3))
#
# print('KRR1 7+20: MAE: ', mean_absolute_error(y_test, y_kr1), ' MAPE: ', mean_absolute_percentage_error(y_test, y_kr1))
# print('KRR2 20: MAE: ', mean_absolute_error(y_test, y_kr2), ' MAPE: ', mean_absolute_percentage_error(y_test, y_kr2))
# print('KRR3 5+20: MAE: ', mean_absolute_error(y_test, y_kr3), ' MAPE: ', mean_absolute_percentage_error(y_test, y_kr3))
#
# print('GPR1 7+20: MAE: ', mean_absolute_error(y_test, y_gpr1), ' MAPE: ',
#       mean_absolute_percentage_error(y_test, y_gpr1))
# print('GPR2 20: MAE: ', mean_absolute_error(y_test, y_gpr2), ' MAPE: ', mean_absolute_percentage_error(y_test, y_gpr2))
# print('GPR3 5+20: MAE: ', mean_absolute_error(y_test, y_gpr3), ' MAPE: ',
#       mean_absolute_percentage_error(y_test, y_gpr3))


text:
import os

import PyWiseRF
import cloud
import numpy as np

if cloud.running_on_cloud():
    os.environ['WISERF_ROOT'] = '/home/picloud/WiseRF-1.5.9-linux-x86_64-rc2'

def test():
    x = np.random.random((5, 5))
    y = np.array([1, 1, 1, 0, 0])
    clf = WiseRF().fit(x, y)
    return clf

class WiseRF(PyWiseRF.WiseRF):
    def decision_function(self, x):
        p = self.predict_proba(x)
        return p[:, 1] - p[:, 0]

max_stars_repo_name: ChrisBeaumont/brut

max_issues_repo_name: ChrisBeaumont/brut

max_forks_repo_name: ChrisBeaumont/brut