def fill_in_parameters(self, group_type, random=False, ones=False, universal=False): """Separate all possible types of twobodys, threebodys, manybody. One type per group. And fill in either universal ls and sigma from pre-defined parameters from set_parameters("sigma", ..) and set_parameters("ls", ..) or random parameters if random is True. Args: group_type (str): "specie", "twobody", "threebody", "cut3b", "manybody" definition_list (list, dict): list of elements """ nspec = len(self.all_group_names["specie"]) if nspec < 1: raise RuntimeError("the specie group has to be defined in advance") if random: for group_name in self.all_group_names[group_type]: self.set_parameters(group_name, parameters=nprandom(2)) elif ones: for group_name in self.all_group_names[group_type]: self.set_parameters(group_name, parameters=np.ones(2)) elif universal: for group_name in self.all_group_names[group_type]: self.set_parameters( group_name, parameters=[self.universal["sigma"], self.universal["lengthscale"]], )
def prepare_samples(self, horizon): if horizon <= 0: raise Exception("the input horizon is invalid") else: self.horizon = horizon self.prepared_samples = self.lower + (nprandom(self.horizon) * self.amplitude)
def draw_sample(self, context, t=None): """ Draw one random sample.""" if self.context != context: raise Exception( "the arm corresponding to a different context is called") if t is None: # The parameter t is ignored in this Arm. Do sampling right away. return self.lower + (nprandom() * self.amplitude) else: if t >= self.horizon: raise Exception("the time instance is beyond the horizon") else: return self.prepared_samples[t]
'''Test for generic utils ''' import numpy as np from chemlab.utils.celllinkedlist import CellLinkedList from chemlab.libs.ckdtree import cKDTree from chemlab.utils import distance_matrix import time from nose_parameterized import parameterized from numpy.random import random as nprandom @parameterized([ (nprandom((5, 3)) * 2, nprandom((5, 3)) * 2, 0.5), (nprandom((4,3)) - 0.5, nprandom((4,3)) - 0.5, 0.5) # negative nums ]) def test_distances(coords, coords_b, cutoff): # Consistency checks print "Simple" t = time.time() dist_simple = distance_matrix(coords, coords_b, cutoff, method="simple") print -t + time.time() print "Cell-lists" t = time.time() dist_clist = distance_matrix(coords, coords_b, cutoff, method="cell-lists") print -t + time.time() print dist_simple print dist_clist.todense() assert np.allclose(dist_simple, dist_clist.todense())
def draw_nparray(self, shape=(1,)): """ Draw one random sample. The parameter t is ignored in this Arm.""" return np.minimum((-1. / self.p) * np.log(nprandom(shape)), self.trunc)
from numpy import array, rec from numpy.random import normal as nprandom from rpy2.robjects import numpy2ri, r foo = array(range(10)) bar = foo + nprandom(0,1,10) d = rec.fromarrays([foo, bar], names=('foo','bar')) print d fit = r.lm('bar ~ foo', data=d) print fit.rx2('coefficients')
def draw_nparray(self, shape=(1, )): """ Draw a numpy array of random samples, of a certain shape.""" return self.lower + (nprandom(shape) * self.amplitude)
'''Test for generic utils ''' import numpy as np from chemlab.utils.celllinkedlist import CellLinkedList from chemlab.libs.ckdtree import cKDTree from chemlab.utils import distance_matrix import time from nose_parameterized import parameterized from numpy.random import random as nprandom @parameterized([ (nprandom((5, 3)) * 2, nprandom((5, 3)) * 2, 0.5), (nprandom((4, 3)) - 0.5, nprandom((4, 3)) - 0.5, 0.5) # negative nums ]) def test_distances(coords, coords_b, cutoff): # Consistency checks print "Simple" t = time.time() dist_simple = distance_matrix(coords, coords_b, cutoff, method="simple") print -t + time.time() print "Cell-lists" t = time.time() dist_clist = distance_matrix(coords, coords_b, cutoff, method="cell-lists") print -t + time.time() print dist_simple print dist_clist.todense()