text:
# coding: utf-8

"""
The Queue function mainly deals with reading and preparing dataset in a multi-processing manner.
We didnot use the built-in tensorflow function Dataset because it lacks of flexibility.
The function defined below is mainly inspired by the Keras.
"""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import six
import sys
import threading
import numpy as np
import multiprocessing as mp
from multiprocessing.pool import ThreadPool
from contextlib import closing

try:
    import queue
except ImportError:
    import Queue as queue

# Global variables to be shared across processes.
_SHARED_GENERATORS = {}
# Value counter to provide unique id to different processes.
_GENERATOR_COUNTER = None

def init_pool_generator(gens, random_seed=None):
    global _SHARED_GENERATORS
    _SHARED_GENERATORS = gens

    if random_seed is not None:
        ident = mp.current_process().ident
        np.random.seed(random_seed + ident)

def next_sample(uid):
    return six.next(_SHARED_GENERATORS[uid])

class EnQueuer(object):
    def __init__(self, generator,
                 multiprocessing=True,  # whether use multi-processing, default True
                 random_seed=1234,      # control the randomness
                 ):
        """
        The queue that stores data using multiprocessing or multithreading.
        :param generator: A iterable batched-data generator, define the __next__ function
        """
        self.generator = generator
        self.multiprocessing = multiprocessing
        self.random_seed = random_seed

        global _GENERATOR_COUNTER
        if _GENERATOR_COUNTER is None:
            try:
                _GENERATOR_COUNTER = mp.Value("i", 0)
            except OSError:
                # OS system does not support multiprocessing
                _GENERATOR_COUNTER = 0

        if isinstance(_GENERATOR_COUNTER, int):
            self.uid = _GENERATOR_COUNTER
            _GENERATOR_COUNTER += 1
        else:

            with _GENERATOR_COUNTER.get_lock():
                self.uid = _GENERATOR_COUNTER.value
                _GENERATOR_COUNTER.value += 1

        self.workers = 0
        self.executor_fn = None
        self.queue = None
        self.run_thread = None
        self.stop_signal = None

    def is_running(self):
        # whether the queue is working
        return self.stop_signal is not None and not self.stop_signal.is_set()

    def _send_generator(self):
        # Send current Iterable to all workers
        _SHARED_GENERATORS[self.uid] = self.generator

    def _get_executor_init(self, workers):
        # Get the pool initializer for multiprocessing
        return lambda seqs: mp.Pool(workers,
                                    initializer=init_pool_generator,
                                    initargs=(seqs, self.random_seed))

    def start(self, workers=1, max_queue_size=10):
        """
        Start the handler's worker
        :param workers: number of worker threads
        :param max_queue_size: queue size,
            when full, workers could block on `put()`
        :return:
        """
        self.workers = workers if not self.multiprocessing else 1

        if self.multiprocessing:
            self.executor_fn = self._get_executor_init(self.workers)
        else:
            self.executor_fn = lambda _: ThreadPool(self.workers)

        self.queue = queue.Queue(max_queue_size)
        self.stop_signal = threading.Event()
        self.run_thread = threading.Thread(target=self._run)
        self.run_thread.daemon = True
        self.run_thread.start()

    def _run(self):
        # Submit request to the executor and queue the future data
        self._send_generator()  # share the initial generator
        with closing(self.executor_fn(_SHARED_GENERATORS)) as executor:
            while True:
                if self.stop_signal.is_set():
                    return
                self.queue.put(executor.apply_async(next_sample, (self.uid,)), block=True)

    def stop(self, timeout=None):
        # stop the running threads and wait for them to exist
        # should be called by the same thread which called `start()`
        self.stop_signal.set()
        with self.queue.mutex:
            self.queue.queue.clear()
            self.queue.unfinished_tasks = 0
            self.queue.not_full.notify()
        self.run_thread.join(timeout)
        _SHARED_GENERATORS[self.uid] = None

    def get(self):
        # Data fetcher
        try:
            while self.is_running():
                inputs = self.queue.get(block=True).get()
                self.queue.task_done()
                if inputs is not None:
                    yield inputs
        except StopIteration:
            # Special case for finite generators
            last_ones = []
            while self.queue.qsize() > 0:
                last_ones.append(self.queue.get(block=True))
            # wait for them to complete
            list(map(lambda f: f.wait(), last_ones))
            # keep the good ones
            last_ones = [future.get() for future in last_ones if future.successful()]
            for inputs in last_ones:
                if inputs is not None:
                    yield inputs
        except KeyboardInterrupt:
            self.stop()
            six.reraise(*sys.exc_info())
        except Exception as e:
            self.stop()
            if 'generator already executing' in str(e):
                raise RuntimeError('your generator is NOT thread-safe')
            six.reraise(*sys.exc_info())

max_stars_repo_name: Avmb/lrn

max_issues_repo_name: Avmb/lrn

max_forks_repo_name: Avmb/lrn


text:
#
#   UNITTESTS
#
import unittest

# Check if object is a generator
import types

# Import class that shall be tested and create an instance of it
import utilities as util

#
#   EXTERNAL LIBARIES
#

# Handling files
import pathlib

# math stuff
import numpy as np

class TestUtilities_ReadFileAsText(unittest.TestCase):
    """
    Test utilities.readFileAsText()
    """

    def test_types(self):
        """
        Make sure type errors are raised if necessary
        """
        self.assertRaises(TypeError, util.readFileAsText, "string")
        self.assertRaises(TypeError, util.readFileAsText, 1)
        self.assertRaises(TypeError, util.readFileAsText, True)
        self.assertRaises(TypeError, util.readFileAsText, False)
        self.assertRaises(TypeError, util.readFileAsText, 1.0)
        self.assertRaises(TypeError, util.readFileAsText, 1+1j)

class TestUtilities_ConvertTextToMatrices(unittest.TestCase):
    """
    Test utilities.readFileAsText()
    """

    def test_types(self):
        """
        Make sure type errors are raised if necessary
        """
        # Test string
        self.assertRaises(TypeError, util.convertTextToMatrices, pathlib.Path("test_utilities.py"))
        self.assertRaises(TypeError, util.convertTextToMatrices, 1)
        self.assertRaises(TypeError, util.convertTextToMatrices, True)
        self.assertRaises(TypeError, util.convertTextToMatrices, False)
        self.assertRaises(TypeError, util.convertTextToMatrices, 1.0)
        self.assertRaises(TypeError, util.convertTextToMatrices, 1+1j)

        # Test shape
        self.assertRaises(TypeError, util.convertTextToMatrices, "!matrix\n  0 1\n 0 1", shape = 1)
        self.assertRaises(TypeError, util.convertTextToMatrices, "!matrix\n  0 1\n 0 1", shape = "string")
        self.assertRaises(TypeError, util.convertTextToMatrices, "!matrix\n  0 1\n 0 1", shape = True)
        self.assertRaises(TypeError, util.convertTextToMatrices, "!matrix\n  0 1\n 0 1", shape = False)
        self.assertRaises(TypeError, util.convertTextToMatrices, "!matrix\n  0 1\n 0 1", shape = 1.0)
        self.assertRaises(TypeError, util.convertTextToMatrices, "!matrix\n  0 1\n 0 1", shape = 1+1j)
        self.assertRaises(TypeError, util.convertTextToMatrices, "!matrix\n  0 1\n 0 1", shape = (1, 1, 1) )
        self.assertRaises(TypeError, util.convertTextToMatrices, "!matrix\n  0 1\n 0 1", shape = (1) )
        self.assertRaises(TypeError, util.convertTextToMatrices, "!matrix\n  0 1\n 0 1", shape = ("string", "string") )
        self.assertRaises(TypeError, util.convertTextToMatrices, "!matrix\n  0 1\n 0 1", shape = (None, None) )
        self.assertRaises(TypeError, util.convertTextToMatrices, "!matrix\n  0 1\n 0 1", shape = (True, False) )
        self.assertRaises(TypeError, util.convertTextToMatrices, "!matrix\n  0 1\n 0 1", shape = (1.0, 1.0) )
        self.assertRaises(TypeError, util.convertTextToMatrices, "!matrix\n  0 1\n 0 1", shape = (1+1j, 1+1j) )

    def test_index(self):
        """
        Make sure index errors are raised if necessary
        """
        self.assertRaises(IndexError, util.convertTextToMatrices, "!matrix\n  0 1\n 0 1", shape = (1,1))

    def test_output(self):
        """
        Make sure the output is correct
        """

        # String to convert
        teststring = """# Comment

                        ! first matrix
                        1 0 0
                        0 1 0
                        0 0 1
                        ! second matrix
                        10 3 0
                        1 -1 0
                        0  0 0
                     """

        # Correct result
        correctoutput = [ {"head": "first matrix",
                           "matrix": np.array([ [1, 0, 0], [0, 1, 0], [0, 0, 1] ]) },
                          {"head": "second matrix",
                           "matrix": np.array([ [10, 3, 0], [1, -1, 0], [0, 0, 0] ]) }]

        # Function output
        output = util.convertTextToMatrices(teststring)

        # Check type and identitiy
        self.assertIsInstance(output, list)
        for index, matrix in enumerate(output):
            self.assertIsInstance(matrix, dict)
            self.assertEqual(matrix["head"], correctoutput[index]["head"])
            self.assertEqual(matrix["matrix"].any(), correctoutput[index]["matrix"].any())

class TestUtilities_FindEntries(unittest.TestCase):
    """
    Test utilities.findEntries()
    """
    def test_types(self):
        """
        Make sure type errors are raised
        """
        with self.assertRaises(TypeError):
            list(util.findEntries("string", False))
            list(util.findEntries("string", ["string"]))
            list(util.findEntries("string", 1))
            list(util.findEntries("string", 1.0))
            list(util.findEntries("string", 1+1j))
            list(util.findEntries("string", ("string")))

            list(util.findEntries(False, "string"))
            list(util.findEntries(["string"], "string"))
            list(util.findEntries(1, "string"))
            list(util.findEntries(1.0, "string"))
            list(util.findEntries(1+1j, "string"))
            list(util.findEntries(("string"), "string"))

            list(util.findEntries("string", "string", lines = "string"))
            list(util.findEntries("string", "string", lines = 1.0))
            list(util.findEntries("string", "string", lines = 1+1j))
            list(util.findEntries("string", "string", lines = [1]))
            list(util.findEntries("string", "string", lines = (1)))
            list(util.findEntries("string", "string", lines = True))

            list(util.findEntries("string", "string", returnKeyword = "string"))
            list(util.findEntries("string", "string", returnKeyword = 1.0))
            list(util.findEntries("string", "string", returnKeyword =  1+1j))
            list(util.findEntries("string", "string", returnKeyword =  [True]))
            list(util.findEntries("string", "string", returnKeyword =  (True)))

    def test_values(self):
        """
        Make sure value errors are raised
        """
        with self.assertRaises(ValueError):
            list(util.findEntries("string", "string", lines = 0))
            list(util.findEntries("string", "string", lines = -1))

    def test_output(self):
        """
        Make sure output is correct
        """

        # Define test input parameter
        test_string = """Lorem ipsum
                         Lorem ipsum Polarizability derivatives wrt mode          2
                                    1             2             3
                         1   0.215860D+00  0.000000D+00  0.000000D+00
                        Lorem ipsum"""
        test_keyword = "Polarizability derivatives wrt mode"
        test_lines = 3
        test_returnKeyword = False

        # Define correct output (correct output will be type generator not list!)
        correct_output = [ ["2", "1             2             3", "1   0.215860D+00  0.000000D+00  0.000000D+00"] ]
        # Compute output
        test_output = util.findEntries(test_string, test_keyword, lines = test_lines, returnKeyword = test_returnKeyword)

        # Test output
        self.assertIsInstance(test_output, types.GeneratorType)
        self.assertListEqual(list(test_output), correct_output)

class TestUtilities_BuildRamanMuellerMatrix(unittest.TestCase):
    """
    Test utilities.buildRamanMuellerMatrix()
    """

    def test_values(self):
        """
        Make sure value errors are raised if necessary
        """
        pass

    def test_types(self):
        """
        Make sure type errors are raised if necessary
        """
        self.assertRaises(TypeError, util.buildRamanMuellerMatrix, [ [1,1,1], [1,1,1], [1,1,1] ])
        self.assertRaises(TypeError, util.buildRamanMuellerMatrix, 1)
        self.assertRaises(TypeError, util.buildRamanMuellerMatrix, "string")
        self.assertRaises(TypeError, util.buildRamanMuellerMatrix, True)
        self.assertRaises(TypeError, util.buildRamanMuellerMatrix, False)
        self.assertRaises(TypeError, util.buildRamanMuellerMatrix, 1+1j)
        self.assertRaises(TypeError, util.buildRamanMuellerMatrix, np.diag([1,1,1,1]) )
        self.assertRaises(TypeError, util.buildRamanMuellerMatrix, np.diag([1,1]) )

    def test_output(self):
        """
        Make sure the returned matrix is correct
        """
        # lambda funtion that converts raman tensor to mueller matrix for testing purposes
        correct_matrix = lambda xx, xy, yx, yy : np.array([ [ (xx**2 + yx**2 + xy**2 + yy**2)/2, (xx**2 + yx**2 - xy**2 - yy**2)/2, xy*xx + yx*yy, 0 ],
                                                            [ (xx**2 - yx**2 + xy**2 - yy**2)/2, (xx**2 - yx**2 - xy**2 + yy**2)/2, xy*xx - yx*yy, 0 ],
                                                            [  xx*yx + xy*yy,                     xx*yx - xy*yy,                    xx*yy + xy*yx, 0 ],
                                                            [  0,                                 0,                                0,             0 ]  ])

        # Check type
        self.assertTrue( type( util.buildRamanMuellerMatrix(np.diag([1,1,1])) ) is np.ndarray )

        # Make sure output is correct
        for xx in [-2, -1.2, -1, -0.5, 0, 0.7, 1, 1.3, 2]:
            for xy in [-2, -1.2, -1, -0.5, 0, 0.7, 1, 1.3, 2]:
                for yx in [-2, -1.2, -1, -0.5, 0, 0.7, 1, 1.3, 2]:
                    for yy in [-2, -1.2, -1, -0.5, 0, 0.7, 1, 1.3, 2]:

                        test_output = util.buildRamanMuellerMatrix( np.array([ [xx, xy, 0],
                                                                               [yx, yy, 0],
                                                                               [0,  0,  0] ]) )
                        correct_output = correct_matrix(xx, xy, yx, yy)

                        np.testing.assert_array_almost_equal(test_output, correct_output)

ext: py

max_stars_repo_name: AlreadyTakenJonas/PolaRam

max_issues_repo_name: AlreadyTakenJonas/PolaRam

max_forks_repo_name: AlreadyTakenJonas/PolaRam

reason: import numpy


text:
import torch
from torch.optim import Adam
import torch.nn.functional as F
import numpy as np

from utils.buffer import Buffer
from utils.noise import OUActionNoise
from utils.networks import Actor, Critic

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

class DDPG:
    def __init__(self, num_actions, num_states, min_action, max_action, lr_actor=0.001,
                 lr_critic=0.002, gamma=0.99, buffer_capacity=100000, tau=0.005,
                 hidden_size=(512, 512), batch_size=64, noise_stddev=0.1):
        self.gamma = gamma
        self.tau = tau
        self.memory = Buffer(num_actions, num_states, buffer_capacity, batch_size)
        self.batch_size = batch_size
        self.num_actions = num_actions
        self.num_states = num_states
        self.min_action = min_action
        self.max_action = max_action
        self.noise = OUActionNoise(mean=np.zeros(1), std_deviation=float(noise_stddev) * np.ones(1))

        self.actor = Actor(num_actions, num_states, hidden_size).to(device)
        self.critic = Critic(num_actions+num_states, hidden_size).to(device)
        self.target_actor = Actor(num_actions, num_states, hidden_size, name='target_actor').to(device)
        self.target_critic = Critic(num_actions+num_states,hidden_size, name='target_critic').to(device)

        self.actor_optimizer = Adam(self.actor.parameters(), lr_actor)
        self.critic_optimizer = Adam(self.critic.parameters(), lr_critic)

        self.update_network_parameters(tau=1)

    def update_network_parameters(self, tau=None):
        if tau is None:
            tau = self.tau

        for param, target_param in zip(self.actor.parameters(), self.target_actor.parameters()):
            target_param.data.copy_(param.data * tau + target_param.data * (1.0-tau))

        for param, target_param in zip(self.critic.parameters(), self.target_critic.parameters()):
            target_param.data.copy_(param.data * tau + target_param.data * (1.0 - tau))

    def save_models(self):
        torch.save(self.actor.state_dict(), self.actor.checkpoint_file)
        torch.save(self.critic.state_dict(), self.critic.checkpoint_file)
        torch.save(self.target_actor.state_dict(), self.target_actor.checkpoint_file)
        torch.save(self.target_critic.state_dict(), self.target_critic.checkpoint_file)

    def load_models(self):
        self.actor.load_state_dict(torch.load(self.actor.checkpoint_file))
        self.critic.load_state_dict(torch.load(self.critic.checkpoint_file))
        self.target_actor.load_state_dict(torch.load(self.target_actor.checkpoint_file))
        self.target_critic.load_state_dict(torch.load(self.target_critic.checkpoint_file))

    def choose_action(self, state, evaluate=False):
        mu = self.actor(state.to(device), self.max_action)
        mu = mu.data

        if not evaluate:
            noise = torch.Tensor(self.noise()).to(device)
            mu += noise

        mu = mu.clamp(self.min_action, self.max_action)

        return mu.cpu().detach().numpy()

    def update(self, state_batch, action_batch, reward_batch, next_state_batch, done_batch):
        # Training and updating Actor & Critic networks.
        target_actions = self.target_actor.forward(next_state_batch, self.max_action)
        y = reward_batch + self.gamma * self.target_critic.forward(next_state_batch, target_actions) * (1 - done_batch)
        critic_value = self.critic.forward(state_batch, action_batch)
        critic_loss = F.mse_loss(y, critic_value)

        self.critic_optimizer.zero_grad()
        critic_loss.backward()
        self.critic_optimizer.step()

        actions = self.actor.forward(state_batch, self.max_action)
        critic_value = self.critic.forward(state_batch, actions)
        actor_loss = -torch.mean(critic_value)

        self.actor_optimizer.zero_grad()
        actor_loss.backward()
        self.actor_optimizer.step()

    def learn(self):
        state_batch, action_batch, reward_batch, next_state_batch, done_batch = self.memory.sample()

        self.update(state_batch, action_batch, reward_batch, next_state_batch, done_batch)

        self.update_network_parameters()

max_stars_repo_name: jspieler/reinforcement-learning

max_issues_repo_name: jspieler/reinforcement-learning

max_forks_repo_name: jspieler/reinforcement-learning


text:
import cv2
import numpy as np
from numpy.linalg import norm

import helper as hp

class StatModel(object):
    def load(self, fn):
        self.model.load(fn)  # Known bug: https://github.com/Itseez/opencv/issues/4969

    def save(self, fn):
        self.model.save(fn)

class KNearest(StatModel):
    def __init__(self, k=3):
        self.k = k
        self.model = cv2.ml.KNearest_create()

    def train(self, samples, responses):
        self.model.train(samples, cv2.ml.ROW_SAMPLE, responses)

    def predict(self, samples):
        retval, results, neigh_resp, dists = self.model.findNearest(samples, self.k)
        return results.ravel()

knn_num_input_data_file = "knn/num.data"
knn_sym_input_data_file = "knn/sym.data"

class KNN():
    name = "KNN"
    def __init__(self):
        self.knn_num = hp.train_num
        self.knn_sym = hp.train_sym
        self.samples_num_file = "knn/samples-num.npy"
        self.labels_num_file = "knn/labels-num.npy"
        self.samples_sym_file = "knn/samples-sym.npy"
        self.labels_sym_file = "knn/labels-sym.npy"

        self.num_model = self.__make_knn_from_npy("num")
        self.sym_model = self.__make_knn_from_npy("sym")

        print('loading KNN...')

    def __buddy_hog(self, img):
        gx = cv2.Sobel(img, cv2.CV_32F, 1, 0)
        gy = cv2.Sobel(img, cv2.CV_32F, 0, 1)
        mag, ang = cv2.cartToPolar(gx, gy)
        bin_n = 16
        bin = np.int32(bin_n * ang / (2 * np.pi))
        bin_cells = bin[:10, :10], bin[10:, :10], bin[:10, 10:], bin[10:, 10:]
        mag_cells = mag[:10, :10], mag[10:, :10], mag[:10, 10:], mag[10:, 10:]
        hists = [np.bincount(b.ravel(), m.ravel(), bin_n) for b, m in zip(bin_cells, mag_cells)]
        hist = np.hstack(hists)

        # transform to Hellinger kernel
        eps = 1e-7
        hist /= hist.sum() + eps
        hist = np.sqrt(hist)
        hist /= norm(hist) + eps
        # print hist
        return np.float32(hist)

    def make_npy(self):
        base = self.knn_num
        samples, labels = [], []
        paths = hp.get_paths(base)
        for path in paths:
            img = hp.get_image(base, path)
            sample = self.__buddy_hog(img)
            label = hp.get_name(path)
            labels.append(label)
            samples.append(sample)

        samples = np.float32(samples)
        labels = np.array(labels)
        np.save(self.samples_num_file, samples)
        np.save(self.labels_num_file, labels)

        base = self.knn_sym
        samples, labels = [], []
        paths = hp.get_paths(base)
        for path in paths:
            img = hp.get_image(base, path)
            sample = self.__buddy_hog(img)
            label = hp.get_name(path) - 10
            labels.append(label)
            samples.append(sample)

        samples = np.float32(samples)
        labels = np.array(labels)
        np.save(self.samples_sym_file, samples)
        np.save(self.labels_sym_file, labels)

    def __make_knn_from_images(self, mode):
        base = ""
        modifier = 0
        samples, labels = [], []
        if mode == "num":
            base = self.knn_num
            modifier = 0
        elif mode == "sym":
            base = self.knn_sym
            modifier = 10
        paths = hp.get_paths(base)

        for path in paths:
            img = hp.get_image(base, path)
            sample = self.__buddy_hog(img)
            label = hp.get_name(path) - modifier
            labels.append(label)
            samples.append(sample)

        samples = np.float32(samples)
        labels = np.array(labels)

        model = KNearest(k=5)
        model.train(samples, labels)

        return model

    def __make_knn_from_npy(self, mode):
        samples, labels = [], []
        if mode == "num":
            samples = np.load(self.samples_num_file)
            labels = np.load(self.labels_num_file)
        elif mode == "sym":
            samples = np.load(self.samples_sym_file)
            labels = np.load(self.labels_sym_file)

        model = KNearest(k=5)
        model.train(samples, labels)
        model.save("knn/knn-{0}.dat".format(mode))

        return model

    def rec(self, img, mode):
        # data = hp.get_int_array_from_image(img)
        data = self.__buddy_hog(img)
        data2 = np.float32([np.float32(data)])

        rec = -1
        if mode == "sym":
            rec = np.int32(self.sym_model.predict(data2))
        elif mode == "num":
            rec = np.int32(self.num_model.predict(data2))
        return hp.ann_get_lit(rec, mode)

def testing_knn_from_image_base(input_dir, set_name, mode):
    # init block
    knn = KNN()
    success, error = "", ""
    counter, err = 1, 0
    paths = hp.get_paths(input_dir)
    for path in paths:
        # recognition
        img = hp.get_image(input_dir, path)
        lit, i = knn.rec(img, mode)

        # test
        tlit, ti = hp.get_test(path, mode)
        if tlit != lit:
            err += 1
            error += "{0}\n".format(path)
            hp.write_image("try/error", path, img)
        else:
            success += "{0}\n".format(path)
        counter += 1

    hp.print_result(counter, err, set_name, mode)
    #hp.write_report("test/knn", set_name, mode, success)
    #hp.write_error("test/knn", set_name, mode, error)

if __name__ == "__main__":
    N = 1

    if N == 1:
        testing_knn_from_image_base(hp.small_sym, "small", hp.mode_sym)

    if N == 1:
        testing_knn_from_image_base(hp.small_num, "small", hp.mode_num)

    if N == 1:
        testing_knn_from_image_base(hp.big_sym, "big", hp.mode_sym)

    if N == 1:
        testing_knn_from_image_base(hp.big_num, "big", hp.mode_num)

max_stars_repo_name: artem-astafyev/plate-recognition

max_issues_repo_name: artem-astafyev/plate-recognition

max_forks_repo_name: artem-astafyev/plate-recognition

reason: import numpy,from numpy


text:
__author__ = 'kjoseph'

import numpy as np
import signal

def epa_to_index(option):
    if option == 'e':
        return 0
    if option == 'p':
        return 1
    if option == 'a':
        return 2

def abo_to_index(option):
    if option == 'a':
        return 0
    if option == 'b':
        return 1
    if option == 'o':
        return 2
    return -1

def epa_abo_to_index(option):
    return epa_to_index(option[1])+3 * abo_to_index(option[0])

def draw_from_inverse_chi_square(degrees_of_freedom,scale, n=1):
    return (degrees_of_freedom * scale)/np.random.chisquare(degrees_of_freedom)

def stringify(data):
    return [str(x) for x in data]

def tab_stringify_newline(data,newline=True):
    to_return = "\t".join(stringify(data))
    if newline:
         return to_return + "\n"
    return to_return

#handle SIGINT from SyncManager object
def mgr_sig_handler(signal, frame):
    print 'not closing the mgr'

#initilizer for SyncManager
def mgr_init():
    signal.signal(signal.SIGINT, mgr_sig_handler)
    print 'initialized mananger'

max_stars_repo_licenses: MIT Unlicense

max_issues_repo_licenses: MIT Unlicense

max_forks_repo_licenses: MIT Unlicense


text:
#
# Copyright 2020 Spotify AB
# 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.
#
""" Utility Functions used in evaluation metric"""

from __future__ import absolute_import, division, print_function, unicode_literals

import numpy as np

def utility_DCG(grade):  # pylint: disable=invalid-name
    """
    Discounted Cumulative gain based utility function.
    Args:
      grade (float): grade value.
    Returns:
      float: utility score for the grade
    """
    return np.power(2, grade) - 1

def utility_Prec(grade):  # pylint: disable=invalid-name
    """
    Simple precision based or identity utility function.
    Args:
      grade (float): grade value.
    Returns:
      float: utility score for the grade
    """
    return grade

def utility_Binary(grade):  # pylint: disable=invalid-name
    """
    Binary utility function, that converts any grades into binary utility.
    Args:
      grade (float): grade value.
    Returns:
      float: utility score for the grade
    """
    return 1 if grade > 0 else 0


text:
# Copyright 2017 Google Inc. All Rights Reserved.
#
# 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.
"""Trains Graph Embeddings (node embeddings + Edge function).

This trainer runs on the output of `create_dataset_arrays.py`.
"""

import edge_nn
import collections
import pickle
import os
import random

import numpy
from sklearn import metrics

import tensorflow as tf
# from tensorflow import flags
from tensorflow.python.platform import flags
import argparse
# from tensorflow import app
import tensorflow.compat.v1.gfile as gfile2
from tensorflow.io.gfile import GFile as gfile
# from tensorflow import gfile
# from tensorflow import logging

def check_args():
  parser = argparse.ArgumentParser()
  parser.add_argument("--dataset_dir", type=str, help='Directory where all dataset files live. All data files ' +
                    'must be located here. Including {train,test}.txt.npy, ' +
                    'train.pairs.*.txt.npy, {train,test}.neg.txt.npy.',
                      nargs='?', default="output/", const="output/")
  parser.add_argument("--restore", type=int, help='If set to 1 and dump was previously found, it will be restored.',
                      nargs='?', default=1, const=1)
  parser.add_argument("--num_nodes", type=int, help='Number of nodes in graph. Node IDs in all dataset files must range from 0 to (num_nodes - 1).',
                      nargs='?', default=0, const=0)
  parser.add_argument("--embed_dim",type=int, help='Size of node embedding.',
                      nargs='?', default=100, const=100)
  parser.add_argument("--dnn_dims", type=str, help='Comma-separated layer dimensions for deep embedding transformation.',
                      nargs='?', default='100,100', const='100,100')
  parser.add_argument("--projection_dim", type=int, help='Inner dimensions of L and R projection matrices. If set ' +
                     'to <= 0, then no Asymetric Projection is applied, and ' +
                     'edge function becomes a hadamard product of node ' +
                     'embeddings, followed by dot-product with a trainable ' +
                     'vector',
                      nargs='?', default=32, const=32)
  parser.add_argument("--num_projections", type=int, help='Number of asymmetric matrices (L and R) to use. If >1, ' +
                     'then output of DNN(left) x L_i x R_i x DNN(right) for i ' +
                     'in [0, this flag) will be concatenated, and a fully-'     +
                     'connected layer is added to give a scalar (edge score). ' +
                     'If set to 1, then no fully connected layer is added. We ' +
                     'found that setting to >1 gives only slight improvement.',
                      nargs='?', default=1, const=1)
  parser.add_argument("--description_prefix", type=str, help="You may write the prefix for the description",
                      nargs='?', default='test', const='test')
  parser.add_argument("--num_iterations", type=int, help="Number of iterations or epochs",
                      nargs='?', default=10, const=10)
  parser.add_argument("--save_every", type=int, help="Saves every this many iterations.",
                      nargs='?', default=500, const=500)
  parser.add_argument("--verbose", type=bool, help="If set, prints info during training.",
                      nargs='?', default=False, const=False)

  args = parser.parse_args()
  global dataset_dir, restore, embed_dim, dnn_dims, projection_dim, num_projections, description_prefix, num_iterations, save_every, verbose, num_nodes
  dataset_dir = args.dataset_dir
  num_nodes = args.num_nodes
  restore = args.restore
  embed_dim = args.embed_dim
  dnn_dims = args.dnn_dims
  projection_dim = args.projection_dim
  num_projections = args.num_projections
  description_prefix = args.description_prefix
  num_iterations = args.num_iterations
  save_every = args.save_every
  verbose = args.verbose

# flags.DEFINE_string('dataset_dir', '',
#                     'Directory where all dataset files live. All data files '
#                     'must be located here. Including {train,test}.txt.npy, '
#                     'train.pairs.*.txt.npy, {train,test}.neg.txt.npy.')
# flags.DEFINE_integer('restore', 1,
#                      'If set to 1 and dump was previously found, it will be '
#                      'restored.')
# flags.DEFINE_integer('embed_dim', 100, 'Size of node embedding.')
# flags.DEFINE_string('dnn_dims', '100,100',
#                     'Comma-separated layer dimensions for deep embedding '
#                     'transformation.')
# flags.DEFINE_integer('projection_dim', 32,
#                      'Inner dimensions of L and R projection matrices. If set '
#                      'to <= 0, then no Asymetric Projection is applied, and '
#                      'edge function becomes a hadamard product of node '
#                      'embeddings, followed by dot-product with a trainable '
#                      'vector')
# flags.DEFINE_integer('num_projections', 1,
#                      'Number of asymmetric matrices (L and R) to use. If >1, '
#                      'then output of DNN(left) x L_i x R_i x DNN(right) for i '
#                      'in [0, this flag) will be concatenated, and a fully-'
#                      'connected layer is added to give a scalar (edge score). '
#                      'If set to 1, then no fully connected layer is added. We '
#                      'found that setting to >1 gives only slight improvement.')
# flags.DEFINE_integer('num_nodes', 0,
#                      'Number of nodes in graph. Node IDs in all dataset files '
#                      'must range from 0 to (num_nodes - 1).')
# flags.DEFINE_string('description_prefix', 'test', '')
# flags.DEFINE_integer('num_iterations', 10, '')
# flags.DEFINE_integer('save_every', 500, 'Saves every this many iterations.')
# flags.DEFINE_boolean('verbose', False, 'If set, prints info during training.')

# FLAGS = flags.FLAGS

BATCH_SIZE = 1000  # Batch size during training.
NCE_K = 5  # Number of noise samples per anchor.

def Description():
  """Returns a string that encodes model hyper-parameters.

  String is used to create a sub-directory for storing the model files.
  """
  description = '%s.d%i_f%s_g%i' % (
      description_prefix, embed_dim,
      dnn_dims.replace(',', 'x'), projection_dim)
  return description

def DatasetFileName(filename):
  """Returns location of `filename` within --dataset_dir`."""
  return os.path.join(dataset_dir, filename)

def ModelFileName(suffix):
  """Location of filename `Description()`_`suffix` in `dataset_dir/dumps`."""
  return os.path.join(dataset_dir, 'dumps',
                      '%s_%s' % (Description(), suffix))

def InFile(suffix):
  """Opens file `ModelFileName(suffix)` for reading."""
  # return gfile.Open(ModelFileName(suffix))
  # changed
  return gfile(ModelFileName(suffix))

def OutFile(suffix):
  """Opens file `ModelFileName(suffix)` for writing."""
  # return gfile.Open(ModelFileName(suffix), 'w')
  # changed
  return gfile(ModelFileName(suffix), 'w')

def estimate_AUC(sess, nn, embeddings, positives, negatives):
  """Measures the AUC-ROC given positive and negative edges.

  Args:
    sess: TensorFlow session that contains parameters of edge neural net `nn`.
    nn: Edge Neural Network. It is expected that it contains members:
      embeddings_a: (B, D) float tensor for inputting embedding vectors, where
        `B` is batch size and `D` is the input node embedding dimensionality.
      embeddings_b: (B, D) float tensor for inputting embedding vectors. Every
        row corresponds to `embeddings_a`
      batch_size: int tensor, which will be fed `B`.
      output: Output float tensor with shape (B). Every entry `i` contains the
        edge score between `embeddings_a[i]` and `embeddings_b[i]`.
    embeddings: (|V|, D) float32 numpy.array containing all node embeddings.
    positives: (P, 2) int32 numpy.array containing positive edges, where
      `positives[j]` contains (node ID 1, node ID 2) with both IDs in range
      [0, |V|-1].
    negatives: (N, 2) int32 numpy.array containing negative edges.

  Returns:
    roc-auc score for ranking `positives` above `negatives`.
  """
  all_pairs = numpy.concatenate([positives, negatives], 0)
  all_scores = sess.run(
      nn.output,
      feed_dict={
          nn.embeddings_a: embeddings[all_pairs[:, 0]],
          nn.embeddings_b: embeddings[all_pairs[:, 1]],
          nn.batch_size: len(all_pairs),
      })
  pos_scores = all_scores[:len(positives)]
  neg_scores = all_scores[-len(negatives):]
  return metrics.roc_auc_score(
      [1] * len(pos_scores) + [0] * len(neg_scores),
      numpy.concatenate([pos_scores, neg_scores], 0))

class Evaluator(object):
  """Evaluates the accuracy of the model on the data.

  If you have a custom way to calculate evals, you can override this class.
  """

  def __init__(self, positive_pairs_file, negative_pairs_file):
    # self.pos_data = numpy.load(gfile2.Open(positive_pairs_file))
    # self.neg_data = numpy.load(gfile2.Open(negative_pairs_file))
    # changed
    self.pos_data = numpy.load(positive_pairs_file)
    self.neg_data = numpy.load(negative_pairs_file)

  def calculate_accuracy(self, session, nn, embeddings):
    return estimate_AUC(session, nn, embeddings, self.pos_data, self.neg_data)

class TrainNegatives(object):
  """Reads negatives file and stores in-memory all negatives per node."""

  def __init__(self, train_negatives_file):
    #train_negatives_arr = numpy.load(gfile.Open(train_negatives_file))
    # changed
    train_negatives_arr = numpy.load(train_negatives_file)
    self.negatives_dict = collections.defaultdict(list)
    for n1, n2 in train_negatives_arr:
      self.negatives_dict[n1].append(n2)
      # self.negatives_dict[n2].append(n1)

  def sample(self, nodes, num_negatives):
    """Returns `num_negatives` arrays each of size len(nodes)"""
    negatives = numpy.zeros(shape=(len(nodes), num_negatives), dtype='int32')
    for i, n in enumerate(nodes):
      node_negatives = self.negatives_dict[n]
      for j in range(num_negatives):
        selected_random_id = random.randint(0, len(node_negatives) - 1)
        negatives[i, j] = node_negatives[selected_random_id]

    return negatives

class TrainPairsReader(object):
  """Reads all train.pairs.*.npy files.

  Each time calling `next_pairs_array()` will return the next train.pairs.*.npy
  file. It round-robins across the files."""

  def __init__(self):
    files = gfile2.Glob(DatasetFileName('train.pairs.*.npy'))
    random.shuffle(files)
    self.train_npy_files = files
    self.next_idx = 0

  def next_pairs_array(self):
    #arr = numpy.load(gfile.Open(self.train_npy_files[self.next_idx]))
    # changged
    arr = numpy.load(self.train_npy_files[self.next_idx])
    # indices = range(len(arr))
    # changed
    indices = list(range(len(arr)))
    random.shuffle(indices)
    arr = arr[indices]
    self.next_idx = (self.next_idx + 1) % len(self.train_npy_files)
    return arr

def main(num_nodes):
  """Trains embeddings and edge function, recording eval metrics."""
  assert len(description_prefix) > 0
  assert len(dataset_dir) > 0
  if num_nodes == 0:
    index_pkl_file = DatasetFileName('index.pkl')
    # added
    with open(index_pkl_file, 'rb') as pickle_file:
      content = pickle.load(pickle_file)
    # num_nodes = len(pickle.load(gfile.Open(index_pkl_file))['index'])
    # changed
    num_nodes = len(content['index'])
  assert num_nodes > 0

  dumps_dir = DatasetFileName('dumps')
  if not gfile2.Exists(dumps_dir):
    gfile2.MakeDirs(dumps_dir)

  # Used for model selection. Note: Positive training data are positive node
  # pairs, generated via Random Walks on the train graph (train.txt.npy).
  TRAIN_EVALUATOR = Evaluator(DatasetFileName('train.txt.npy'),
                              DatasetFileName('train.neg.txt.npy'))

  directed_negs_file = DatasetFileName('test.directed.neg.txt.npy')
  if gfile2.Exists(directed_negs_file):
    print( 'evaluating on directed edges')
    test_negative_file = directed_negs_file
  else:
    test_negative_file = DatasetFileName('test.neg.txt.npy')

  TEST_EVALUATOR = Evaluator(DatasetFileName('test.txt.npy'),
                             test_negative_file)

  # Create the edge Neural Network.
  NN = edge_nn.EdgeNN()
  NN.build_net(
      embedding_dim=embed_dim, dnn_dims=dnn_dims,
      projection_dim=projection_dim,
      num_projections=num_projections)

  # Embeddings matrix
  EMBEDDINGS = numpy.array(
      numpy.random.uniform(
          low=-0.1, high=0.1, size=(num_nodes, embed_dim)),
      dtype='float32')

  # Initialize to be unit norm.
  EMBEDDINGS = (EMBEDDINGS.T / numpy.sqrt((EMBEDDINGS ** 2).sum(axis=1))).T

  # Positive training data.
  TRAIN_PAIRS = TrainPairsReader()

  # Initialize TensorFlow session.
  SESS = tf.compat.v1.Session()
  SESS.run(tf.compat.v1.global_variables_initializer())

  # Negative training data.
  TRAIN_NEGATIVES = TrainNegatives(
      DatasetFileName('train.neg_per_node.txt.npy'))

  ALL_VARS = tf.compat.v1.global_variables()

  TRAILS = {
      'train_auc': [],
      'test_auc': [],
      'embedding_lrs': [],
      'best_train_auc': 0,  # Best train AUC
      'best_test_auc': 0,  # Test AUC at Best train AUC
      'j': 0,
      'j_list': [],
      'i_list': [],
  }

  def restore(suffix=''):
    if not gfile.Exists(ModelFileName('embeddings.npy' + suffix)):
      print("Starting with fresh model...")
      return
    print("Restoring ...")

    EMBEDDINGS[:, :] = numpy.load(InFile('embeddings.npy' + suffix))

    trails = pickle.load(InFile('trails'))
    for k, v in trails.iteritems():
      TRAILS[k] = v

    net_values = dict(pickle.load(InFile('net.pkl' + suffix)))
    for v in ALL_VARS:
      if v.name in net_values:
        SESS.run(v.assign(net_values[v.name]))
    #if len(net_values) != len(ALL_VARS):
    #  import IPython; IPython.embed()
    #assert len(net_values) == len(ALL_VARS)
    #for (name, val), var in zip(net_values, ALL_VARS):
    #  assert name == var.name
    #  SESS.run(var.assign(val))

  def evaluate(j, ii):
    test_auc = TEST_EVALUATOR.calculate_accuracy(SESS, NN, EMBEDDINGS)
    train_auc = TRAIN_EVALUATOR.calculate_accuracy(SESS, NN, EMBEDDINGS)
    TRAILS['test_auc'].append(test_auc)
    TRAILS['train_auc'].append(train_auc)
    TRAILS['i_list'].append(ii)
    TRAILS['j_list'].append(j)

    if train_auc > TRAILS['best_train_auc']:
      TRAILS['best_train_auc'] = train_auc
      TRAILS['best_test_auc'] = test_auc
      save('.best')

    msg = '@%i (%i) test/train Best=%f/%f cur=%f/%f. - %s' % (
        j, ii, TRAILS['best_test_auc'], TRAILS['best_train_auc'], test_auc,
        train_auc, Description())

    print( msg)

  def save(suffix=''):
    if verbose:
      print ('Saving %s ...' % suffix)
    numpy.save(OutFile('embeddings.npy' + suffix), EMBEDDINGS)
    all_vals = SESS.run(ALL_VARS)
    pickle.dump(zip([v.name for v in ALL_VARS], all_vals),
                 OutFile('net.pkl' + suffix))
    pickle.dump(TRAILS, OutFile('trails' + suffix))
    if verbose:
      print ('Saved!')

  def train_on_pairs(j, pairs, lr_embedding=-1):
    batch_size = BATCH_SIZE
    sum_percent_change = 0
    num_percent_change = 0
    for i in range(0, len(pairs), batch_size):
      epoch = 2 * (j + float(i) / len(pairs))  # Approximation of the epoch.
      ii = i / batch_size
      end_i = min(i + batch_size, len(pairs))
      if end_i - i < 10:
        continue
      left_e = EMBEDDINGS[pairs[i:end_i, 0]]
      right_e = EMBEDDINGS[pairs[i:end_i, 1]]

      labels = numpy.ones(shape=(len(left_e)), dtype='float32')

      negatives = TRAIN_NEGATIVES.sample(pairs[i:end_i, 0], NCE_K)
      for k in range(NCE_K):
        left_e = numpy.concatenate(
            [left_e, EMBEDDINGS[pairs[i:end_i, 0]]], 0)
        right_e = numpy.concatenate(
            [right_e, EMBEDDINGS[negatives[:, k]]], 0)
        labels = numpy.concatenate(
            [labels, numpy.zeros(shape=len(negatives), dtype='float32')], 0)

      # Calculate gradients. grads[0] and grads[1] correspond to gradients for
      # the anchors and (positives,negatives), respectively. The remaining
      # grads[2:] correspond to gradients of Neural Network (NN)
      grads, loss = NN.get_gradients(SESS, left_e, right_e, labels)

      # Step on NN.
      nn_deltas = NN.apply_gradients(SESS, grads[2:], epoch)

      # Step on Embeddings. First, sum-up anchor (i.e. "left" gradients).
      embedding_l_grads = grads[0]
      sum_embedding_l_grad = embedding_l_grads[:len(negatives)]
      for k in range(NCE_K):
        s_j = (k + 1) * len(negatives)
        e_j = s_j + len(negatives)
        sum_embedding_l_grad += embedding_l_grads[s_j:e_j]

      # Determine Learning rate using PercentDelta (Abu-El-Haija, 2017).
      mean_percent_grad = numpy.mean(numpy.abs(
          sum_embedding_l_grad / (edge_nn.PlusEpsilon(
              EMBEDDINGS[pairs[i:end_i, 0], :]))))
      lr_embedding = edge_nn.PickLearnRate(
          mean_percent_grad, 300 * (j + float(i) / len(pairs)))

      delta_embeddings = lr_embedding * sum_embedding_l_grad
      if verbose:
        mean_percent_change = numpy.mean(
            abs(delta_embeddings /
                edge_nn.PlusEpsilon(EMBEDDINGS[pairs[i:end_i, 0], :])))
        print (f'Mean percent grad={mean_percent_grad}, percent change = {mean_percent_change}'  )

      # Update embeddings.
      EMBEDDINGS[pairs[i:end_i, 0], :] -= delta_embeddings

      if ii % save_every == 0:
        evaluate(j, ii)  # Populates TRAILS.
        TRAILS['embedding_lrs'].append(lr_embedding)
        save()

  if restore == 1:
    restore()

  while TRAILS['j'] < num_iterations:
    train_on_pairs(TRAILS['j'], TRAIN_PAIRS.next_pairs_array())
    TRAILS['j'] += 1
    save()

if __name__ == '__main__':
  check_args()
  main(num_nodes)
  # app.run()


text:
"""
// Converter Script [Mesh Points from Langley Research Center -> GMSH Format]
// Distributed by: Notre Dame CICS (MIT Liscense)
// - Associated publication:
// doi: https://doi.org/10.1016/j.jcp.2019.01.021
// github: https://github.com/cics-nd/rans-uncertainty
// - Original Flow Reference:
// doi: http://www.doi.org/10.1016/j.ijheatfluidflow.2015.07.006
// url: https://turbmodels.larc.nasa.gov/Other_LES_Data/conv-div-channel20580les.html
"""

import numpy as np

if __name__ == '__main__':

    # Read in surface points from nasa database
    print('Reading data file.')
    surfacePoints = np.genfromtxt('surfacePoints.dat', delimiter=' ', skip_header=3,
                     skip_footer=0, names=["X", "Y"])

    # Seperate out the points on the bump
    bumpPoints = []
    for p0 in surfacePoints:
        if( not p0['Y'] <= 10**(-8)):
            bumpPoints.append([p0['X'],p0['Y']])
    bumpPoints = np.array(bumpPoints)

    print('Writing gmsh points to file.')
    # Write to file to copy into gmsh script
    fh = open('surfacePointsGmsh.dat', 'w')
    for p0 in bumpPoints:
        fh.write('Point(newp) = {%.9f*s, %.9f*s, 0, 1.0};\n' % (p0[0], p0[1]))
    fh.close()

    print('Done')

max_issues_repo_name: zabaras/rans-uncertainty

max_forks_repo_name: zabaras/rans-uncertainty


text:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# (c) Camille Scott, 2019
# File   : test_signatures.py
# License: MIT
# Author : Camille Scott <camille.scott.w@gmail.com>
# Date   : 16.10.2019

import random

from goetia.hashing import Canonical, StrandAware
from goetia.parsing import read_fastx
from goetia.sketches import SourmashSketch, UnikmerSketch
from goetia.storage import SparseppSetStorage
from goetia.storage import HLLStorage

from .utils import *

import cppyy
import numpy as np
import pandas as pd
import pytest

def test_sourmash_signature(datadir, ksize):
    import sourmash

    rfile = datadir('random-20-a.fa')

    goetia_sig = SourmashSketch.Sketch.build(10000, 31, False, False, False, 42, 0)
    sourmash_sig = sourmash.MinHash(10000, 31)

    processor = SourmashSketch.Processor.build(goetia_sig)
    processor.process(rfile)

    for record in read_fastx(rfile):
        sourmash_sig.add_sequence(record.sequence)

    goetia_mh = goetia_sig.to_sourmash()

    assert goetia_mh.similarity(sourmash_sig) == 1.0

def test_sourmash_scaled(datadir, ksize):
    import sourmash

    rfile = datadir('random-20-a.fa')
    goetia_sig = SourmashSketch.Sketch.build(0, 31, False, False, False, 42, 1000)
    sourmash_sig = sourmash.MinHash(0, 31, scaled=1000)

    processor = SourmashSketch.Processor.build(goetia_sig)
    processor.process(rfile)

    for record in read_fastx(rfile):
        sourmash_sig.add_sequence(record.sequence)

    goetia_mh = goetia_sig.to_sourmash()

    assert goetia_mh.similarity(sourmash_sig) == 1.0

def test_draff_to_numpy(datadir):
    rfile = datadir('random-20-a.fa')

    sketch_t = UnikmerSketch[SparseppSetStorage, StrandAware]
    sketch = sketch_t.Sketch.build(31, 7)
    processor = sketch_t.Processor.build(sketch)
    processor.process(rfile)

    np_sig = sketch.to_numpy()
    py_sig = list(sketch.get_sketch_as_vector())

    assert len(np_sig) == len(py_sig)
    assert list(np_sig) == list(py_sig)

    for np_val, py_val in zip(list(np_sig), list(py_sig)):
        assert np_val == py_val

def test_hllcounter():
    ints = set(np.random.randint(0, 100000, 100000))
    e = 0.01
    hll = HLLStorage(e)

    for i in ints:
        hll.insert(cppyy.gbl.uint64_t(i))

    est = hll.n_unique_kmers()
    act = len(ints)

    margin = (2 * e) * act

    print(act)
    print(est)

    assert (act - margin) < est < (act + margin)

def test_sourmash_stream(tmpdir, datadir):
    with tmpdir.as_cwd():
        rfile = datadir('sacPom.pombase.fa.gz')

        goetia_sig = 'goetia.sig'
        sourmash_sig = 'sourmash.sig'
        comp_file = 'comp.csv'

        goetia_cmd = ['goetia', 'sketch', 'sourmash',
                      '--interval', '50000',
                      '-K', '31', '-N', '100000', '--save-sig', goetia_sig,
                      '-i', rfile]
        sourmash_cmd = ['sourmash', 'compute', '-k', '31', '-n', '100000',
                        '-o', sourmash_sig, rfile]
        compare_cmd = ['sourmash', 'compare', goetia_sig, sourmash_sig, '--csv', comp_file]

        run_shell_cmd(goetia_cmd)
        run_shell_cmd(sourmash_cmd)
        run_shell_cmd(compare_cmd)

        comp = pd.read_csv(comp_file)
        assert comp.all().all()

max_stars_repo_name: camillescott/boink

max_issues_repo_name: camillescott/boink

max_forks_repo_name: camillescott/goetia


text:
import pandas as pd
import json
from database import *
import numpy as np

#open json file
alec_json = "/Users/eugeniagiraudy/Dropbox/DSSG/policy_diffusion/scripts/model_legislation_alignments.json"

def create_bill_to_bill_matrix(jsonfile):
	'''
	Converts a json file with matching text between model legislation and bills into a
	dataframe.

	'''
	alignments = [json.loads(x.strip()) for x in open(jsonfile)]
	df_list = []
	for i in range(len(alignments)):
		left_id = alignments[i]['query_document_id']
		interest_group = left_id.split('_')
		interest_group = "_".join(interest_group[0:2])
		try:
			for result in alignments[i]['alignment_results']:
				right_id = result['document_id']
				score_list = []
				for j in range(len(result['alignments'])):
					score = result['alignments'][j]['score']
					score_list.append(score)
					#Need to decide whehter we want the sum, average, max
					score_max = max(score_list)
				df_list.append([interest_group, left_id,right_id,score_max,right_id[0:2],left_id+"_"+right_id,'undirected'])
		except KeyError:
				print left_id, 'failed'
				continue
	df = pd.DataFrame(df_list)
	df.columns = ['interst_group_id','model_legislation_id', 'unique_id','score_max','state','bill_ml_id','undirected']
	return df

def grab_ids_for_data_frame(df):
	'''
	Grabs bill ids from ElasticSearch and adds it to a dataframe.
	Outputs csv file with data frame containing model legislation to bills matches and
	information on date introduced and date signed

	Arguments:
		dataframe = data frame containing model legislation to bill analysis

	'''
	bill_id_list = df['unique_id']
	bill_id_list = bill_id_list.tolist()

	ec = ElasticConnection(host = '54.203.12.145', port = 9200)

	bill_dates = []
	bill_signed = []
	for bill in bill_id_list:
	    bill_all = ec.get_bill_by_id(bill)
	    date_introduced = bill_all['date_introduced']
	    date_signed = bill_all['date_signed']
	    bill_dates.append(date_introduced)
	    bill_signed.append(date_signed)
	    print bill
	bills_introd_signed = zip(bill_id_list, bill_dates, bill_signed)
	df_dates = pd.DataFrame(bills_introd_signed)
	df_dates.columns = ['unique_id', 'date_introduced', 'date_signed']
	df2 = pd.merge(df, df_dates, on='unique_id')
	#Drop duplicates from the merge
	df3 = df2.drop_duplicates('bill_ml_id')
	return df3.to_csv('./model_legislation_to_bills_max_score.csv')

#Analysis of ALEC

df_alec = df3[(df3.interst_group_id =='alec_bills')|(df3.interst_group_id=='alec_old')]
#eliminate cases where two model legislations influence the same bill
df_alec = df_alec.groupby(['unique_id']).max()
date = df_alec['date_introduced']
df_alec['year_introduced']=date.apply(lambda x:x.year)
#eliminate cases wher states may have two identical bills for a given year
df_grouped = df_alec.groupby(['state', 'year_introduced', 'model_legislation_id']).max()
df_grouped.to_csv('./alec_model_legislation_to_bills_max_score_unique.csv')


text:
import pandas
import pandas as pd
import numpy as np

data = pd.read_csv('2018_Central_Park_Squirrel_Census_-_Squirrel_Data.csv')

gray_colour_count = len(data[data["Primary Fur Color"] == "Gray"])
cinnamon_colour_count = len(data[data["Primary Fur Color"] == "Cinnamon"])
black_colour_count = len(data[data["Primary Fur Color"] == "Black"])

fur_colour_dict = {"Fur Color": ["Gray", "Cinnamon", "Black"],
                   "Count": [gray_colour_count, cinnamon_colour_count,black_colour_count]
                   }

new_dframe = pandas.DataFrame(fur_colour_dict)

new_dframe.to_csv("fur_colour.csv")

# print(color_count)
# print(type(color_count))
# # fur_color.pd.DataFrame.to_dict()
# print(color_count[2])

# print(color_count)

# df = pd.DataFrame(columns=['Colour', 'Count'], data=color_count)
# print(color_count)


text:
from __future__ import annotations
from abc import ABC, abstractmethod
from enum import IntEnum, unique
from typing import List, Optional, Union

import numpy as np

from ..common.config import Config
from ..math.matrix import Matrix
from .geom_algo import GeomAlgo2D

class Shape(ABC):
    @unique
    class Type(IntEnum):
        BASE: int = -1
        Point: int = 0
        Polygon: int = 1
        Circle: int = 2
        Ellipse: int = 3
        Capsule: int = 4
        Edge: int = 5
        Curve: int = 6
        Sector: int = 7

    def __init__(self):
        self._type: Shape.Type = Shape.Type.BASE

    @property
    def type(self) -> Shape.Type:
        return self._type

    @type.setter
    def type(self, val: Shape.Type) -> None:
        self._type = val

    @abstractmethod
    def scale(self, factor: float) -> None:
        raise NotImplementedError

    @abstractmethod
    def contains(self, point: Matrix) -> bool:
        raise NotImplementedError

    @abstractmethod
    def center(self) -> Matrix:
        raise NotImplementedError

class ShapePrimitive():
    '''Basic Shape Description Primitive.
       Including vertices/position/angle of shape
    '''
    def __init__(self):
        self._shape: Optional[Union[Point, Polygon, Rectangle, Circle, Ellipse,
                                    Edge, Curve, Capsule, Sector]] = None
        self._xform: Matrix = Matrix([0.0, 0.0], 'vec')
        self._rot: float = 0.0

    def translate(self, src: Matrix) -> Matrix:
        assert src.shape == (2, 1)
        return Matrix.rotate_mat(self._rot) * src + self._xform

class Point(Shape):
    def __init__(self):
        super().__init__()
        self.type = self.Type.Point
        self._pos: Matrix = Matrix([0.0, 0.0], 'vec')

    @property
    def pos(self) -> Matrix:
        return self._pos

    @pos.setter
    def pos(self, pos: Matrix) -> None:
        self._pos = pos

    def scale(self, factor: float) -> None:
        self._pos *= factor

    def contains(self, point: Matrix) -> bool:
        return self._pos == point

    def center(self) -> Matrix:
        return self._pos

class Polygon(Shape):
    '''Convex polygon, not concve
    '''
    def __init__(self):
        super().__init__()
        self.type = self.Type.Polygon
        self._vertices: List[Matrix] = []

    @property
    def vertices(self) -> List[Matrix]:
        return self._vertices

    @vertices.setter
    def vertices(self, vertices: List[Matrix]) -> None:
        self._vertices = vertices
        self.update_vertices()

    def append(self, vertice: Matrix) -> None:
        raise AssertionError('dont use, otherwise make gjk algo fail! ')
        self._vertices.append(vertice)
        self.update_vertices()

    def scale(self, factor: float) -> None:
        assert len(self._vertices) > 0
        self._vertices = [v * factor for v in self._vertices]

    def contains(self, point: Matrix) -> bool:
        assert len(self._vertices) > 2

        vert_len: int = len(self._vertices)
        for i in range(vert_len - 1):
            p1: Matrix = self._vertices[i]
            p2: Matrix = self._vertices[i + 1]
            ref: Matrix = self._vertices[
                1] if i + 2 == vert_len else self._vertices[i + 2]
            # NOTE: why [1]? according to the vertices list type,
            # shape need to be closed
            if not GeomAlgo2D.is_point_on_same_side(p1, p2, ref, point):
                return False

        return True

    def center(self) -> Matrix:
        return GeomAlgo2D.calc_mass_center(self._vertices)

    def update_vertices(self) -> None:
        center_point: Matrix = self.center()
        self._vertices = [v - center_point for v in self._vertices]

class Rectangle(Polygon):
    def __init__(self, width: float = 0.0, height: float = 0.0):
        super().__init__()
        self.set_value(width, height)

    @property
    def width(self) -> float:
        return self._width

    @width.setter
    def width(self, width: float) -> None:
        self._width: float = width
        self.calc_vertices()

    @property
    def height(self) -> float:
        return self._height

    @height.setter
    def height(self, height: float) -> None:
        self._height: float = height
        self.calc_vertices()

    @property
    def vertices(self) -> List[Matrix]:
        return self._vertices

    @vertices.setter
    def vertices(self, vert: List[Matrix]) -> None:
        self._vertices = vert

    # NOTE: use _var to set val, because property 'width'
    # and 'height' call 'calc_vertices' will use _var in init
    def set_value(self, width: float, height: float) -> None:
        self._width = width
        self._height = height
        self.calc_vertices()

    def scale(self, factor: float) -> None:
        self._width *= factor
        self._height *= factor
        self.calc_vertices()

    @staticmethod
    def contain_helper(val: float, ref: float) -> bool:
        return -ref * 0.5 < val < ref * 0.5

    def contains(self, point: Matrix) -> bool:
        return self.contain_helper(point.x,
                                   self._width) and self.contain_helper(
                                       point.y, self._height)

    def calc_vertices(self) -> None:
        self._vertices: List[Matrix] = []
        self._vertices.append(
            Matrix([-self._width * 0.5, self._height * 0.5], 'vec'))
        self._vertices.append(
            Matrix([-self._width * 0.5, -self._height * 0.5], 'vec'))
        self._vertices.append(
            Matrix([self._width * 0.5, -self._height * 0.5], 'vec'))
        self._vertices.append(
            Matrix([self._width * 0.5, self._height * 0.5], 'vec'))
        self._vertices.append(
            Matrix([-self._width * 0.5, self._height * 0.5], 'vec'))

class Circle(Shape):
    def __init__(self, radius: float = 0.0):
        super().__init__()
        self.type = self.Type.Circle
        self.radius = radius

    @property
    def radius(self) -> float:
        return self._radius

    @radius.setter
    def radius(self, radius: float) -> None:
        self._radius: float = radius

    def scale(self, factor: float) -> None:
        self._radius *= factor

    def contains(self, point: Matrix) -> bool:
        return point.len_square() < self._radius * self._radius

    def center(self) -> Matrix:
        return Matrix([0.0, 0.0], 'vec')

class Ellipse(Shape):
    def __init__(self, width: float = 0.0, height: float = 0.0):
        super().__init__()
        self.type = self.Type.Ellipse
        self.set_value(width, height)

    @property
    def width(self) -> float:
        return self._width

    @width.setter
    def width(self, width: float) -> None:
        self._width: float = width

    @property
    def height(self) -> float:
        return self._height

    @height.setter
    def height(self, height: float) -> None:
        self._height: float = height

    def set_value(self, width: float, height: float) -> None:
        self._width = width
        self._height = height

    def scale(self, factor: float) -> None:
        self._width *= factor
        self._height *= factor

    def contains(self, point: Matrix) -> bool:
        return False

    def center(self) -> Matrix:
        return Matrix([0.0, 0.0], 'vec')

    def A(self) -> float:
        return self._width / 2.0

    def B(self) -> float:
        return self._height / 2.0

    def C(self) -> float:
        va = self.A()
        vb = self.B()
        return np.sqrt(va * va - vb * vb)

class Edge(Shape):
    def __init__(self):
        super().__init__()
        self.type = self.Type.Edge
        # set (1.0, 1.0) just for init calc the normal vec correctly
        # if both are (0.0, 0.0), can lead div zero error
        self.set_value(Matrix([1.0, 1.0], 'vec'), Matrix([0.0, 0.0], 'vec'))

    @property
    def start(self) -> Matrix:
        return self._start

    @start.setter
    def start(self, point: Matrix) -> None:
        self._start: Matrix = point
        self.update_normal()

    @property
    def end(self) -> Matrix:
        return self._end

    @end.setter
    def end(self, point: Matrix) -> None:
        self._end: Matrix = point
        self.update_normal()

    # the only method to init the edge value
    def set_value(self, start: Matrix, end: Matrix) -> None:
        self._start = start
        self._end = end
        self.update_normal()

    def update_normal(self) -> None:
        self._normal: Matrix = (self._end -
                                self._start).perpendicular().normal().negate()

    def scale(self, factor: float) -> None:
        self._start *= factor
        self._end *= factor

    def contains(self, point: Matrix) -> bool:
        return GeomAlgo2D.is_point_on_segment(self._start, self._end, point)

    def center(self) -> Matrix:
        return (self._start + self._end) / 2.0

    @property
    def normal(self) -> Matrix:
        return self._normal

    @normal.setter
    def normal(self, normal: Matrix) -> None:
        self._normal = normal

class Curve(Shape):
    def __init__(self):
        super().__init__()
        self.type = self.Type.Curve
        self.set_value(Matrix([0.0, 0.0], 'vec'), Matrix([0.0, 0.0], 'vec'),
                       Matrix([0.0, 0.0], 'vec'), Matrix([0.0, 0.0], 'vec'))

    @property
    def start(self) -> Matrix:
        return self._start

    @start.setter
    def start(self, point: Matrix) -> None:
        self._start: Matrix = point

    @property
    def ctrl1(self) -> Matrix:
        return self._ctrl1

    @ctrl1.setter
    def ctrl1(self, point: Matrix) -> None:
        self._ctrl1: Matrix = point

    @property
    def ctrl2(self) -> Matrix:
        return self._ctrl2

    @ctrl2.setter
    def ctrl2(self, point: Matrix) -> None:
        self._ctrl2: Matrix = point

    @property
    def end(self) -> Matrix:
        return self._end

    @end.setter
    def end(self, point: Matrix) -> None:
        self._end: Matrix = point

    def set_value(self, start: Matrix, ctrl1: Matrix, ctrl2: Matrix,
                  end: Matrix) -> None:
        self._start = start
        self._ctrl1 = ctrl1
        self._ctrl2 = ctrl2
        self._end = end

    def scale(self, factor: float) -> None:
        self._start *= factor
        self._ctrl1 *= factor
        self._ctrl2 *= factor
        self._end *= factor

    def contains(self, point: Matrix) -> bool:
        return False

    def center(self) -> Matrix:
        return Matrix([0.0, 0.0], 'vec')

class Capsule(Shape):
    def __init__(self, width: float = 0.0, height: float = 0.0):
        super().__init__()
        self.type = self.Type.Capsule
        self.set_value(width, height)

    @property
    def width(self) -> float:
        return self._width

    @width.setter
    def width(self, width: float) -> None:
        self._width: float = width

    @property
    def height(self) -> float:
        return self._height

    @height.setter
    def height(self, height: float) -> None:
        self._height: float = height

    def set_value(self, width: float, height: float) -> None:
        self._width = width
        self._height = height

    def calc_pos(self, sign: int = 1) -> List[float]:
        res: List[float] = []
        tmp: float = 0.0
        if self._width > self._height:
            tmp = self._height / 2.0
            res.append(-self._width / 2.0 + tmp)
            res.append(sign * tmp)
        else:
            tmp = self._width / 2.0
            res.append(-tmp)
            res.append(sign * (self._height / 2.0 - tmp))

        return res

    # NOTE: the capsule is made by 2 circle and 1 rectangle with
    # the radius go through the rectangle's width
    # so the top left is the circle's inscribe rectangle's top left point
    def top_left(self) -> Matrix:
        return Matrix(self.calc_pos(), 'vec')

    def bottom_left(self) -> Matrix:
        return Matrix(self.calc_pos(-1), 'vec')

    def top_right(self) -> Matrix:
        return -self.bottom_left()

    def bottom_right(self) -> Matrix:
        return -self.top_left()

    def box_vertices(self) -> List[Matrix]:
        vertices: List[Matrix] = []
        vertices.append(self.top_left())
        vertices.append(self.bottom_left())
        vertices.append(self.bottom_right())
        vertices.append(self.top_right())
        vertices.append(self.top_left())
        return vertices

    def scale(self, factor: float) -> None:
        self._width *= factor
        self._height *= factor

    @staticmethod
    def range_helper_x(p: Matrix, ref1: Matrix, ref2: Matrix,
                       idx: int) -> bool:
        if p._val[idx] - ref1._val[idx] <= Config.Epsilon and p._val[
                idx] - ref2._val[idx] >= Config.Epsilon:
            return True

        return False

    @staticmethod
    def range_helper_y(p: Matrix, idx: int, dis: float) -> bool:
        if p._val[idx] - dis <= Config.Epsilon and p._val[
                idx] + dis >= Config.Epsilon:
            return True

        return False

    @staticmethod
    def range_helper_val(p: Matrix, ref: Matrix, dis: float) -> bool:
        return (p - ref).len_square() - dis * dis <= Config.Epsilon

    def contains(self, point: Matrix) -> bool:
        anchor1: Matrix = Matrix([0.0, 0.0], 'vec')
        anchor2: Matrix = Matrix([0.0, 0.0], 'vec')
        x_len: float = 0.0
        y_len: float = 0.0

        if self._width >= self._height:
            y_len = self._height / 2.0
            x_len = self._width - self._height
            anchor1.set_value([x_len / 2.0, 0.0])
            anchor2.set_value([-x_len / 2.0, 0.0])
            if Capsule.range_helper_x(point, anchor1, anchor2,
                                      0) and Capsule.range_helper_y(
                                          point, 1, y_len):
                return True
        else:
            y_len = self._width / 2.0
            x_len = self._height - self._width
            anchor1.set_value([0.0, x_len / 2.0])
            anchor2.set_value([0.0, -x_len / 2.0])
            if Capsule.range_helper_x(point, anchor1, anchor2,
                                      1) and Capsule.range_helper_y(
                                          point, 0, y_len):
                return True

        # judge if the point in the arc part of the capsule
        if Capsule.range_helper_val(anchor1, point,
                                    y_len) or Capsule.range_helper_val(
                                        anchor2, point, y_len):
            return True

        return False

    def center(self):
        return Matrix([0.0, 0.0], 'vec')

class Sector(Shape):
    def __init__(self):
        super().__init__()
        self.type = self.Type.Sector
        self.set_value()

    def vertices(self) -> List[Matrix]:
        res: List[Matrix] = []
        res.append(Matrix([0.0, 0.0], 'vec'))
        res.append(
            Matrix.rotate_mat(self._start) * Matrix([self._radius, 0], 'vec'))
        res.append(
            Matrix.rotate_mat(self._start + self._span) *
            Matrix([self._radius, 0], 'vec'))
        res.append(Matrix([0.0, 0.0], 'vec'))
        return res

    @property
    def start(self) -> float:
        return self._start

    @start.setter
    def start(self, radian: float) -> None:
        self._start: float = radian

    @property
    def span(self) -> float:
        return self._span

    @span.setter
    def span(self, radian: float) -> None:
        self._span: float = radian

    @property
    def radius(self) -> float:
        return self._radius

    @radius.setter
    def radius(self, radius: float) -> None:
        self._radius: float = radius

    def set_value(self,
                  start: float = 0.0,
                  span: float = 0.0,
                  radius: float = 0.0) -> None:
        self._start = start
        self._span = span
        self._radius = radius

    def area(self) -> float:
        return self._span * self._radius * self._radius / 2.0

    def scale(self, factor: float) -> None:
        self._radius *= factor

    def contains(self, point: Matrix) -> bool:
        if np.isclose(point.x, 0):
            return 0 <= point.y <= self._radius
        else:
            theta: float = point.theta()
            ang_check1: bool = theta >= self._start
            ang_check2: bool = theta <= self._start + self._span
            len_check: bool = point.len_square() <= self._radius * self._radius
            return ang_check1 and ang_check2 and len_check

    def center(self) -> Matrix:
        vertices: List[Matrix] = self.vertices()
        point1: Matrix = vertices[1]
        point2: Matrix = vertices[2]
        normal: Matrix = (point1 + point2) / 2
        normal.normalize()

        point_len: float = (point1 - point2).len()
        rad_len: float = self._radius * self._span
        res: Matrix = normal * (2.0 * self._radius * point_len /
                                (3.0 * rad_len))

        return res

max_stars_repo_name: erizmr/TaichiGAME

max_issues_repo_name: maksyuki/TaichiGame

max_forks_repo_name: maksyuki/TaichiGame


text:
import pytest
import numpy as np
import pyarrow as pa
import pandas as pd
import pybnesian as pbn
from scipy.stats import gaussian_kde
from scipy.stats import norm
from scipy.stats import multivariate_normal as mvn
from scipy.special import logsumexp

import util_test

SIZE = 10000
SMALL_SIZE = 10
TEST_SIZE = 50
df = util_test.generate_normal_data(SIZE, seed=0)
df_small = util_test.generate_normal_data(SMALL_SIZE, seed=0)
df_float = df.astype('float32')
df_small_float = df_small.astype('float32')

def test_variable():
    for variable, evidence in [('a', []), ('b', ['a']), ('c', ['a', 'b']), ('d', ['a', 'b', 'c'])]:
        cpd = pbn.CKDE(variable, evidence)
        assert cpd.variable() == variable

def test_evidence():
    for variable, evidence in [('a', []), ('b', ['a']), ('c', ['a', 'b']), ('d', ['a', 'b', 'c'])]:
        cpd = pbn.CKDE(variable, evidence)
        assert cpd.evidence() == evidence

def test_kde_data_type():
    k = pbn.CKDE("a", [])

    with pytest.raises(ValueError) as ex:
        k.data_type()
    "CKDE factor not fitted" in str(ex.value)

    k.fit(df)
    assert k.data_type() == pa.float64()
    k.fit(df_float)
    assert k.data_type() == pa.float32()

def test_ckde_kde_joint():
    def _test_ckde_kde_joint_iter(variable, evidence, _df):
        cpd = pbn.CKDE(variable, evidence)
        cpd.fit(_df)
        kde_joint = cpd.kde_joint
        kde_joint().bandwidth = np.eye(len(evidence) + 1)
        assert np.all(cpd.kde_joint().bandwidth == np.eye(len(evidence) + 1)), "kde_joint do not return a reference to the KDE joint, but a copy."

    for variable, evidence in [('a', []), ('b', ['a']), ('c', ['a', 'b']), ('d', ['a', 'b', 'c'])]:
        _test_ckde_kde_joint_iter(variable, evidence, df)
        _test_ckde_kde_joint_iter(variable, evidence, df_float)

def test_ckde_kde_marg():
    def _test_ckde_kde_marg_iter(variable, evidence, _df):
        cpd = pbn.CKDE(variable, evidence)
        cpd.fit(_df)
        kde_marg = cpd.kde_marg

        if evidence:
            assert kde_marg().fitted()
            kde_marg().bandwidth = np.eye(len(evidence))
            assert np.all(cpd.kde_marg().bandwidth == np.eye(len(evidence))), "kde_marg do not return a reference to the KDE joint, but a copy."
        else:
            # kde_marg contains garbage if there is no evidence
            pass

    for variable, evidence in [('a', []), ('b', ['a']), ('c', ['a', 'b']), ('d', ['a', 'b', 'c'])]:
        _test_ckde_kde_marg_iter(variable, evidence, df)
        _test_ckde_kde_marg_iter(variable, evidence, df_float)

def test_ckde_fit():
    def _test_ckde_fit(variables, _df, instances):
        npdata = _df.loc[:, variables].to_numpy()
        scipy_kde = gaussian_kde(npdata[:instances, :].T,
                        bw_method=lambda s : np.power(4 / (s.d + 2), 1 / (s.d + 4)) * s.scotts_factor())

        cpd = pbn.CKDE(variable, evidence)
        assert not cpd.fitted()
        cpd.fit(_df.iloc[:instances])
        assert cpd.fitted()

        kde_joint = cpd.kde_joint
        assert np.all(np.isclose(kde_joint().bandwidth, scipy_kde.covariance))

        if evidence:
            kde_marg = cpd.kde_marg
            assert np.all(np.isclose(kde_marg().bandwidth, scipy_kde.covariance[1:,1:]))

        assert cpd.num_instances() == instances

    for variable, evidence in [('a', []), ('b', ['a']), ('c', ['a', 'b']), ('d', ['a', 'b', 'c'])]:
        variables = [variable] + evidence
        for instances in [50, 1000, 10000]:
            _test_ckde_fit(variables, df, instances)
            _test_ckde_fit(variables, df_float, instances)

def test_ckde_fit_null():
    def _test_ckde_fit_null(variable, evidence, variables, _df, instances):
        cpd = pbn.CKDE(variable, evidence)
        assert not cpd.fitted()
        cpd.fit(_df.iloc[:instances])
        assert cpd.fitted()

        npdata = _df.loc[:, variables].to_numpy()
        npdata_instances = npdata[:instances,:]
        nan_rows = np.any(np.isnan(npdata_instances), axis=1)
        npdata_no_null = npdata_instances[~nan_rows,:]
        scipy_kde = gaussian_kde(npdata_no_null.T,
                        bw_method=lambda s : np.power(4 / (s.d + 2), 1 / (s.d + 4)) * s.scotts_factor())

        kde_joint = cpd.kde_joint
        assert np.all(np.isclose(kde_joint().bandwidth, scipy_kde.covariance))

        if evidence:
            kde_marg = cpd.kde_marg
            assert np.all(np.isclose(kde_marg().bandwidth, scipy_kde.covariance[1:,1:]))

        assert cpd.num_instances() == scipy_kde.n

    np.random.seed(0)
    a_null = np.random.randint(0, SIZE, size=100)
    b_null = np.random.randint(0, SIZE, size=100)
    c_null = np.random.randint(0, SIZE, size=100)
    d_null = np.random.randint(0, SIZE, size=100)

    df_null = df.copy()
    df_null.loc[df_null.index[a_null], 'a'] = np.nan
    df_null.loc[df_null.index[b_null], 'b'] = np.nan
    df_null.loc[df_null.index[c_null], 'c'] = np.nan
    df_null.loc[df_null.index[d_null], 'd'] = np.nan

    df_null_float = df_float.copy()
    df_null_float.loc[df_null_float.index[a_null], 'a'] = np.nan
    df_null_float.loc[df_null_float.index[b_null], 'b'] = np.nan
    df_null_float.loc[df_null_float.index[c_null], 'c'] = np.nan
    df_null_float.loc[df_null_float.index[d_null], 'd'] = np.nan

    for variable, evidence in [('a', []), ('b', ['a']), ('c', ['a', 'b']), ('d', ['a', 'b', 'c'])]:
        variables = [variable] + evidence
        for instances in [50, 1000, 10000]:
            _test_ckde_fit_null(variable, evidence, variables, df, instances)
            _test_ckde_fit_null(variable, evidence, variables, df_float, instances)

def train_scipy_ckde(data, variable, evidence):
    variables = [variable] + evidence
    npdata_joint = data.loc[:, variables].to_numpy()
    npdata_marg = data.loc[:, evidence].to_numpy()

    nan_rows = np.any(np.isnan(npdata_joint), axis=1)

    scipy_kde_joint = gaussian_kde(npdata_joint[~nan_rows,:].T,
                        bw_method=lambda s : np.power(4 / (s.d + 2), 1 / (s.d + 4)) * s.scotts_factor())
    if evidence:
        scipy_kde_marg = gaussian_kde(npdata_marg[~nan_rows,:].T, bw_method=scipy_kde_joint.covariance_factor())
    else:
        scipy_kde_marg = None

    return scipy_kde_joint, scipy_kde_marg

def scipy_ckde_logpdf(test_data, joint_kde, marg_kde, variable, evidence):
    variables = [variable] + evidence
    test_data_joint = test_data.loc[:, variables].to_numpy()
    test_data_marg = test_data.loc[:, evidence].to_numpy()

    nan_rows = np.any(np.isnan(test_data_joint), axis=1)

    if np.all(test_data.dtypes == "float32"):
        result = np.full(test_data.shape[0], np.nan, dtype=np.float32)
    else:
        result = np.full(test_data.shape[0], np.nan, dtype=np.float64)

    if evidence:
        result[~nan_rows] = joint_kde.logpdf(test_data_joint[~nan_rows,:].T) - marg_kde.logpdf(test_data_marg[~nan_rows,:].T)
    else:
        result[~nan_rows] = joint_kde.logpdf(test_data_joint[~nan_rows,:].T)

    return result

def scipy_ckde_cdf(test_data, joint_kde, marg_kde, variable, evidence):
    variables = [variable] + evidence
    test_data_joint = test_data.loc[:, variables].to_numpy()
    test_data_marg = test_data.loc[:, evidence].to_numpy()

    nan_rows = np.any(np.isnan(test_data_joint), axis=1)

    if np.all(test_data.dtypes == "float32"):
        result = np.full(test_data.shape[0], np.nan, dtype=np.float32)
    else:
        result = np.full(test_data.shape[0], np.nan, dtype=np.float64)

    total_w = np.empty((joint_kde.n, test_data_joint.shape[0]))
    conditional_mean = np.empty((joint_kde.n, test_data_joint.shape[0]))
    total_cdf = np.empty((joint_kde.n, test_data_joint.shape[0]))

    if evidence:
        bandwidth = joint_kde.covariance
        cond_var = bandwidth[0,0] - bandwidth[0, 1:].dot(np.linalg.inv(bandwidth[1:, 1:])).dot(bandwidth[1:, 0])
        for test_index in np.where(~np.any(np.isnan(test_data_joint), axis=1))[0]:
            w = mvn.logpdf(marg_kde.dataset.T, mean=test_data_marg[test_index,:], cov=marg_kde.covariance)
            w = np.exp(w)
            total_w[:, test_index] = w

            evidence_diff = test_data_marg[test_index,:] - joint_kde.dataset[1:,:].T
            cond_mean = joint_kde.dataset[0,:] + bandwidth[0,1:].dot(np.linalg.inv(bandwidth[1:,1:])).dot(evidence_diff.T)

            conditional_mean[:, test_index] = cond_mean
            total_cdf[:, test_index] = norm.cdf(test_data_joint[test_index,0], cond_mean, np.sqrt(cond_var))

            result[test_index] = np.dot(w, norm.cdf(test_data_joint[test_index,0], cond_mean, np.sqrt(cond_var)))

        result /= np.sum(total_w, axis=0)

    else:
        cdf = norm.cdf(test_data_joint[~nan_rows], joint_kde.dataset, np.sqrt(joint_kde.covariance[0,0]))
        result[~nan_rows] = np.sum((1 / joint_kde.n) * cdf, axis=1)

    return result

def test_ckde_logl():
    def _test_ckde_logl(variable, evidence, _df, _test_df):
        cpd = pbn.CKDE(variable, evidence)
        cpd.fit(_df)
        scipy_kde_joint, scipy_kde_marg = train_scipy_ckde(_df, variable, evidence)

        logl = cpd.logl(_test_df)
        scipy = scipy_ckde_logpdf(_test_df, scipy_kde_joint, scipy_kde_marg, variable, evidence)

        if np.all(_df.dtypes == 'float32'):
            assert np.all(np.isclose(logl, scipy, atol=0.0005))
        else:
            assert np.all(np.isclose(logl, scipy))

    test_df = util_test.generate_normal_data(TEST_SIZE, seed=1)
    test_df_float = test_df.astype('float32')

    for variable, evidence in [('a', []), ('b', ['a']), ('c', ['a', 'b']), ('d', ['a', 'b', 'c'])]:
        _test_ckde_logl(variable, evidence, df, test_df)
        _test_ckde_logl(variable, evidence, df_small, test_df)
        _test_ckde_logl(variable, evidence, df_float, test_df_float)
        _test_ckde_logl(variable, evidence, df_small_float, test_df_float)

    cpd = pbn.CKDE('d', ['a', 'b', 'c'])
    cpd.fit(df)
    cpd2 = pbn.CKDE('d', ['c', 'b', 'a'])
    cpd2.fit(df)
    assert np.all(np.isclose(cpd.logl(test_df), cpd2.logl(test_df))), "Order of evidence changes logl() result."

    cpd = pbn.CKDE('d', ['a', 'b', 'c'])
    cpd.fit(df_float)
    cpd2 = pbn.CKDE('d', ['c', 'b', 'a'])
    cpd2.fit(df_float)
    assert np.all(np.isclose(cpd.logl(test_df_float), cpd2.logl(test_df_float), atol=0.0005)), "Order of evidence changes logl() result."

def test_ckde_logl_null():
    def _test_ckde_logl_null(variable, evidence, _df, _test_df):
        cpd = pbn.CKDE(variable, evidence)
        cpd.fit(_df)

        scipy_kde_joint, scipy_kde_marg = train_scipy_ckde(_df, variable, evidence)

        logl = cpd.logl(_test_df)
        scipy = scipy_ckde_logpdf(_test_df, scipy_kde_joint, scipy_kde_marg, variable, evidence)

        if np.all(_test_df.dtypes == "float32"):
            assert np.all(np.isclose(logl, scipy, atol=0.0005, equal_nan=True))
        else:
            assert np.all(np.isclose(logl, scipy, equal_nan=True))

    test_df = util_test.generate_normal_data(TEST_SIZE, seed=1)
    test_df_float = test_df.astype('float32')

    np.random.seed(0)
    a_null = np.random.randint(0, TEST_SIZE, size=10)
    b_null = np.random.randint(0, TEST_SIZE, size=10)
    c_null = np.random.randint(0, TEST_SIZE, size=10)
    d_null = np.random.randint(0, TEST_SIZE, size=10)

    df_null = test_df.copy()
    df_null.loc[df_null.index[a_null], 'a'] = np.nan
    df_null.loc[df_null.index[b_null], 'b'] = np.nan
    df_null.loc[df_null.index[c_null], 'c'] = np.nan
    df_null.loc[df_null.index[d_null], 'd'] = np.nan

    df_null_float = test_df_float.copy()
    df_null_float.loc[df_null_float.index[a_null], 'a'] = np.nan
    df_null_float.loc[df_null_float.index[b_null], 'b'] = np.nan
    df_null_float.loc[df_null_float.index[c_null], 'c'] = np.nan
    df_null_float.loc[df_null_float.index[d_null], 'd'] = np.nan

    for variable, evidence in [('a', []), ('b', ['a']), ('c', ['a', 'b']), ('d', ['a', 'b', 'c'])]:
        _test_ckde_logl_null(variable, evidence, df, df_null)
        _test_ckde_logl_null(variable, evidence, df_small, df_null)
        _test_ckde_logl_null(variable, evidence, df_float, df_null_float)
        _test_ckde_logl_null(variable, evidence, df_small_float, df_null_float)

    cpd = pbn.CKDE('d', ['a', 'b', 'c'])
    cpd.fit(df)
    cpd2 = pbn.CKDE('d', ['c', 'b', 'a'])
    cpd2.fit(df)

    ll = cpd.logl(df_null)
    ll2 = cpd2.logl(df_null)
    assert np.all(np.isclose(ll, ll2, equal_nan=True)), "Order of evidence changes the position of nan values."

    cpd = pbn.CKDE('d', ['a', 'b', 'c'])
    cpd.fit(df_float)
    cpd2 = pbn.CKDE('d', ['c', 'b', 'a'])
    cpd2.fit(df_float)

    ll = cpd.logl(df_null_float)
    ll2 = cpd2.logl(df_null_float)
    assert np.all(np.isclose(ll, ll2, equal_nan=True)), "Order of evidence changes the position of nan values."

def test_ckde_slogl():
    def _test_ckde_slogl(variable, evidence, _df, _test_df):
        cpd = pbn.CKDE(variable, evidence)
        cpd.fit(_df)

        scipy_kde_joint, scipy_kde_marg = train_scipy_ckde(_df, variable, evidence)
        scipy_logl = scipy_ckde_logpdf(_test_df, scipy_kde_joint, scipy_kde_marg, variable, evidence)

        if np.all(_test_df.dtypes == "float32"):
            # Allow an error of 0.0005 for each training instance.
            assert np.isclose(cpd.slogl(_test_df), scipy_logl.sum(), atol=0.0005*_df.shape[0])
        else:
            assert np.isclose(cpd.slogl(_test_df), scipy_logl.sum())

    test_df = util_test.generate_normal_data(TEST_SIZE, seed=1)
    test_df_float = test_df.astype('float32')

    for variable, evidence in [('a', []), ('b', ['a']), ('c', ['a', 'b']), ('d', ['a', 'b', 'c'])]:
        _test_ckde_slogl(variable, evidence, df, test_df)
        _test_ckde_slogl(variable, evidence, df_small, test_df)
        _test_ckde_slogl(variable, evidence, df_float, test_df_float)
        _test_ckde_slogl(variable, evidence, df_small_float, test_df_float)

    cpd = pbn.CKDE('d', ['a', 'b', 'c'])
    cpd.fit(df)
    cpd2 = pbn.CKDE('d', ['c', 'b', 'a'])
    cpd2.fit(df)
    assert np.all(np.isclose(cpd.slogl(test_df), cpd2.slogl(test_df))), "Order of evidence changes slogl() result."

    cpd = pbn.CKDE('d', ['a', 'b', 'c'])
    cpd.fit(df_float)
    cpd2 = pbn.CKDE('d', ['c', 'b', 'a'])
    cpd2.fit(df_float)
    assert np.all(np.isclose(cpd.slogl(test_df_float), cpd2.slogl(test_df_float))), "Order of evidence changes slogl() result."

def test_ckde_slogl_null():
    def _test_ckde_slogl_null(variable, evidence, _df, _test_df):
        cpd = pbn.CKDE(variable, evidence)
        cpd.fit(_df)

        scipy_kde_joint, scipy_kde_marg = train_scipy_ckde(_df, variable, evidence)
        scipy_logl = scipy_ckde_logpdf(_test_df, scipy_kde_joint, scipy_kde_marg, variable, evidence)

        if np.all(_test_df.dtypes == "float32"):
            # Allow an error of 0.0005 for each training instance.
            assert np.isclose(cpd.slogl(_test_df), np.nansum(scipy_logl), atol=0.0005*_df.shape[0])
        else:
            assert np.isclose(cpd.slogl(_test_df), np.nansum(scipy_logl))

    test_df = util_test.generate_normal_data(TEST_SIZE, seed=1)
    test_df_float = test_df.astype('float32')

    np.random.seed(0)
    a_null = np.random.randint(0, TEST_SIZE, size=10)
    b_null = np.random.randint(0, TEST_SIZE, size=10)
    c_null = np.random.randint(0, TEST_SIZE, size=10)
    d_null = np.random.randint(0, TEST_SIZE, size=10)

    df_null = test_df.copy()
    df_null.loc[df_null.index[a_null], 'a'] = np.nan
    df_null.loc[df_null.index[b_null], 'b'] = np.nan
    df_null.loc[df_null.index[c_null], 'c'] = np.nan
    df_null.loc[df_null.index[d_null], 'd'] = np.nan

    df_null_float = test_df_float.copy()
    df_null_float.loc[df_null_float.index[a_null], 'a'] = np.nan
    df_null_float.loc[df_null_float.index[b_null], 'b'] = np.nan
    df_null_float.loc[df_null_float.index[c_null], 'c'] = np.nan
    df_null_float.loc[df_null_float.index[d_null], 'd'] = np.nan

    for variable, evidence in [('a', []), ('b', ['a']), ('c', ['a', 'b']), ('d', ['a', 'b', 'c'])]:
        _test_ckde_slogl_null(variable, evidence, df, df_null)
        _test_ckde_slogl_null(variable, evidence, df_small, df_null)
        _test_ckde_slogl_null(variable, evidence, df_float, df_null_float)
        _test_ckde_slogl_null(variable, evidence, df_small_float, df_null_float)

    cpd = pbn.CKDE('d', ['a', 'b', 'c'])
    cpd.fit(df)
    cpd2 = pbn.CKDE('d', ['c', 'b', 'a'])
    cpd2.fit(df)
    assert np.all(np.isclose(cpd.slogl(df_null), cpd2.slogl(df_null))), "Order of evidence changes slogl() result."

    cpd = pbn.CKDE('d', ['a', 'b', 'c'])
    cpd.fit(df_float)
    cpd2 = pbn.CKDE('d', ['c', 'b', 'a'])
    cpd2.fit(df_float)
    assert np.all(np.isclose(cpd.slogl(df_null_float), cpd2.slogl(df_null_float))), "Order of evidence changes slogl() result."

def test_ckde_cdf():
    def _test_ckde_cdf(variable, evidence, _df, _test_df):
        cpd = pbn.CKDE(variable, evidence)
        cpd.fit(_df)
        scipy_kde_joint, scipy_kde_marg = train_scipy_ckde(_df, variable, evidence)

        cdf = cpd.cdf(_test_df)
        scipy = scipy_ckde_cdf(_test_df, scipy_kde_joint, scipy_kde_marg, variable, evidence)

        if np.all(_df.dtypes == 'float32'):
            assert np.all(np.isclose(cdf, scipy, atol=0.0005))
        else:
            assert np.all(np.isclose(cdf, scipy))

    test_df = util_test.generate_normal_data(TEST_SIZE, seed=1)
    test_df_float = test_df.astype('float32')

    for variable, evidence in [('a', []), ('b', ['a']), ('c', ['a', 'b']), ('d', ['a', 'b', 'c'])]:
        _test_ckde_cdf(variable, evidence, df, test_df)
        _test_ckde_cdf(variable, evidence, df_small, test_df)
        _test_ckde_cdf(variable, evidence, df_float, test_df_float)
        _test_ckde_cdf(variable, evidence, df_small_float, test_df_float)

    cpd = pbn.CKDE('d', ['a', 'b', 'c'])
    cpd.fit(df)
    cpd2 = pbn.CKDE('d', ['c', 'b', 'a'])
    cpd2.fit(df)
    assert np.all(np.isclose(cpd.cdf(test_df), cpd2.cdf(test_df))), "Order of evidence changes logl() result."

    cpd = pbn.CKDE('d', ['a', 'b', 'c'])
    cpd.fit(df_float)
    cpd2 = pbn.CKDE('d', ['c', 'b', 'a'])
    cpd2.fit(df_float)
    assert np.all(np.isclose(cpd.cdf(test_df_float), cpd2.cdf(test_df_float), atol=0.0005)), "Order of evidence changes logl() result."

def test_ckde_cdf_null():
    def _test_ckde_cdf_null(variable, evidence, _df, _test_df):
        cpd = pbn.CKDE(variable, evidence)
        cpd.fit(_df)

        scipy_kde_joint, scipy_kde_marg = train_scipy_ckde(_df, variable, evidence)

        cdf = cpd.cdf(_test_df)
        scipy = scipy_ckde_cdf(_test_df, scipy_kde_joint, scipy_kde_marg, variable, evidence)

        if np.all(_df.dtypes == 'float32'):
            assert np.all(np.isclose(cdf, scipy, atol=0.0005, equal_nan=True))
        else:
            assert np.all(np.isclose(cdf, scipy, equal_nan=True))

    test_df = util_test.generate_normal_data(TEST_SIZE, seed=1)
    test_df_float = test_df.astype('float32')

    np.random.seed(0)
    a_null = np.random.randint(0, TEST_SIZE, size=10)
    b_null = np.random.randint(0, TEST_SIZE, size=10)
    c_null = np.random.randint(0, TEST_SIZE, size=10)
    d_null = np.random.randint(0, TEST_SIZE, size=10)

    df_null = test_df.copy()
    df_null.loc[df_null.index[a_null], 'a'] = np.nan
    df_null.loc[df_null.index[b_null], 'b'] = np.nan
    df_null.loc[df_null.index[c_null], 'c'] = np.nan
    df_null.loc[df_null.index[d_null], 'd'] = np.nan

    df_null_float = test_df_float.copy()
    df_null_float.loc[df_null_float.index[a_null], 'a'] = np.nan
    df_null_float.loc[df_null_float.index[b_null], 'b'] = np.nan
    df_null_float.loc[df_null_float.index[c_null], 'c'] = np.nan
    df_null_float.loc[df_null_float.index[d_null], 'd'] = np.nan

    for variable, evidence in [('a', []), ('b', ['a']), ('c', ['a', 'b']), ('d', ['a', 'b', 'c'])]:
        _test_ckde_cdf_null(variable, evidence, df, df_null)
        _test_ckde_cdf_null(variable, evidence, df_small, df_null)
        _test_ckde_cdf_null(variable, evidence, df_float, df_null_float)
        _test_ckde_cdf_null(variable, evidence, df_small_float, df_null_float)

    cpd = pbn.CKDE('d', ['a', 'b', 'c'])
    cpd.fit(df)
    cpd2 = pbn.CKDE('d', ['c', 'b', 'a'])
    cpd2.fit(df)
    assert np.all(np.isclose(cpd.cdf(df_null), cpd2.cdf(df_null), equal_nan=True)), "Order of evidence changes cdf() result."

    cpd = pbn.CKDE('d', ['a', 'b', 'c'])
    cpd.fit(df_float)
    cpd2 = pbn.CKDE('d', ['c', 'b', 'a'])
    cpd2.fit(df_float)
    assert np.all(np.isclose(cpd.cdf(df_null_float),
                             cpd2.cdf(df_null_float),
                             atol=0.0005, equal_nan=True)), "Order of evidence changes cdf() result."

def test_ckde_sample():
    SAMPLE_SIZE = 1000

    cpd = pbn.CKDE('a', [])
    cpd.fit(df)

    sampled = cpd.sample(SAMPLE_SIZE, None, 0)

    assert sampled.type == pa.float64()
    assert int(sampled.nbytes / (sampled.type.bit_width / 8)) == SAMPLE_SIZE

    cpd = pbn.CKDE('b', ['a'])
    cpd.fit(df)

    sampling_df = pd.DataFrame({'a': np.full((SAMPLE_SIZE,), 3.0)})
    sampled = cpd.sample(SAMPLE_SIZE, sampling_df, 0)

    assert sampled.type == pa.float64()
    assert int(sampled.nbytes / (sampled.type.bit_width / 8)) == SAMPLE_SIZE

    cpd = pbn.CKDE('c', ['a', 'b'])
    cpd.fit(df)

    sampling_df = pd.DataFrame({'a': np.full((SAMPLE_SIZE,), 3.0),
                                'b': np.full((SAMPLE_SIZE,), 7.45)})
    sampled = cpd.sample(SAMPLE_SIZE, sampling_df, 0)

    assert sampled.type == pa.float64()
    assert int(sampled.nbytes / (sampled.type.bit_width / 8)) == SAMPLE_SIZE

    cpd = pbn.CKDE('a', [])
    cpd.fit(df_float)

    sampled = cpd.sample(SAMPLE_SIZE, None, 0)

    assert sampled.type == pa.float32()
    assert int(sampled.nbytes / (sampled.type.bit_width / 8)) == SAMPLE_SIZE

    cpd = pbn.CKDE('b', ['a'])
    cpd.fit(df_float)

    sampling_df = pd.DataFrame({'a': np.full((SAMPLE_SIZE,), 3.0, dtype=np.float32)})
    sampled = cpd.sample(SAMPLE_SIZE, sampling_df, 0)

    assert sampled.type == pa.float32()
    assert int(sampled.nbytes / (sampled.type.bit_width / 8)) == SAMPLE_SIZE

    cpd = pbn.CKDE('c', ['a', 'b'])
    cpd.fit(df_float)

    sampling_df = pd.DataFrame({'a': np.full((SAMPLE_SIZE,), 3.0, dtype=np.float32),
                                'b': np.full((SAMPLE_SIZE,), 7.45, dtype=np.float32)})
    sampled = cpd.sample(SAMPLE_SIZE, sampling_df, 0)

    assert sampled.type == pa.float32()
    assert int(sampled.nbytes / (sampled.type.bit_width / 8)) == SAMPLE_SIZE

max_stars_repo_name: vishalbelsare/PyBNesian

max_issues_repo_name: vishalbelsare/PyBNesian

max_forks_repo_name: vishalbelsare/PyBNesian

reason: import numpy,from scipy


text:
import numpy as np

class KernelModel:
        def __init__(self, kernel=None, informations=True, preprocessing=None):
            """Abstract Model Class"""

            # Definition of the name of the model
            self.name = "KernelModel"

            # Instantiation of the kernel
            self.kernel = kernel

            # Function to preprocess the data
            self.preprocessing = preprocessing

            # Parameter of the gradient Descent
            self.informations = informations

        def score(self, data_test, labels_test):
            """Compute the accuracy."""

            return np.mean(self.predict(data_test) == labels_test.reshape((-1, 1)))

        def fit(self, data_train, labels, alpha_init=[]):
            """Fitting of the model."""
            pass

        def predict(self, data_test, average_size=3):
            """Prediction of the label for the given data."""
            pass

        def predict_proba(self, data_test, average_size=3):
            """Predict the probability to belong to the class 1."""
            pass

        def loss(self):
            """Compute the loss."""
            pass

        def grad_loss(self):
            """Compute the gradient of the loss."""
            pass

max_stars_repo_name: PierreOreistein/MVA-Kernels

max_issues_repo_name: PierreOreistein/MVA-Kernels

max_forks_repo_name: PierreOreistein/MVA-Kernels


text:
'''
Created on Dec 24, 2011

@author: sean
'''
import clyther as cly
from clyther.array import CLArrayContext
import opencl as cl
import unittest
import math
import os
import numpy as np

ca = None

def setUpModule():
    global ca

    DEVICE_TYPE_ATTR = os.environ.get('DEVICE_TYPE', 'DEFAULT')
    DEVICE_TYPE = getattr(cl.Device, DEVICE_TYPE_ATTR)

    ca = CLArrayContext(device_type=DEVICE_TYPE)

    print ca.devices

class Test(unittest.TestCase):

    def test_cos(self):

        @cly.task
        def do_sin(ret, value):
            ret[0] = math.sin(value)

        cy = ca.empty([1], 'f')
        nu = np.empty([1], 'f')

        do_sin(cy.queue, cy, math.pi / 2)
        do_sin.emulate(cy.queue, nu, math.pi / 2)

        with cy.map() as arr:
            self.assertTrue(np.allclose(arr, nu))

    def test_sin(self):

        @cly.task
        def do_sin(ret, value):
            ret[0] = math.sin(value)

        cy = ca.empty([1], 'f')
        nu = np.empty([1], 'f')

        do_sin(cy.queue, cy, math.pi / 2)
        do_sin.emulate(cy.queue, nu, math.pi / 2)

        with cy.map() as arr:
            self.assertTrue(np.allclose(arr, nu))

if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.test_sim']
    unittest.main()

max_stars_repo_name: srossross/Clyther

max_issues_repo_name: srossross/Clyther

max_forks_repo_name: srossross/Clyther