Beispiel #1
0
 def __random_gen(self):
     if self.lsup == None and self.linf == None:
         return norm.rvs()
     elif self.lsup == None:
         return uniform.rvs(self.linf, 2.0*(self.linf+1))
     elif self.linf == None:
         return uniform.rvs(self.lsup - self.lsup, self.lsup)
     return uniform.rvs(self.linf, self.lsup)
Beispiel #2
0
 def random_initialization(self):
     self.genes = []
     for i in range(self.size):
         self.genes.append(self.__random_gen())
     self.sigma = []
     for i in range(self.size):
         self.sigma.append(uniform.rvs())
Beispiel #3
0
    def learn(self):
        """Learns about the world after receiving an award."""

        # After an award, we choose some adjacencies and increase the
        # likelihood of their being chosen for a message next time
        # around.

        # The probability of choosing an adjacency to increase its
        # likelihood is proportional to the value of the award received.

        # Since at every step we choose at random from our list of
        # adjacencies, to increase the likelihood of one of them being
        # chosen, we need only add them again to this list, i.e., if my
        # adjacencies are [1,2,3] and sending a message to 2 was
        # successful, our new extended adjacency list is [1,2,3,2], and
        # when we choose uniformly from this list, we will be more
        # likely to choose 2 again.

        # self.log("learning on %d" % self.index)

        for adj in self.sent:
            assert adj in self.adjacencies

            u_val = uniform.rvs(0, 100, 1)
            # self.log("u_val is %d, learn? %r" % (u_val, u_val < self.value))
            if u_val < self.value:
                self.adjacencies.append(adj)
                # import ipdb ; ipdb.set_trace() #z()  # breakpoint f35d9e23 //

        self.log("Adj after:  " + str(self.adjacencies))
Beispiel #4
0
    def learn(self):
        """Learns about the world after receiving an award."""

        # After an award, we choose some adjacencies and increase the
        # likelihood of their being chosen for a message next time
        # around.

        # The probability of choosing an adjacency to increase its
        # likelihood is proportional to the value of the award received.

        # Since at every step we choose at random from our list of
        # adjacencies, to increase the likelihood of one of them being
        # chosen, we need only add them again to this list, i.e., if my
        # adjacencies are [1,2,3] and sending a message to 2 was
        # successful, our new extended adjacency list is [1,2,3,2], and
        # when we choose uniformly from this list, we will be more
        # likely to choose 2 again.

        # self.log("learning on %d" % self.index)

        for adj in self.sent:
            assert adj in self.adjacencies

            u_val = uniform.rvs(0, 100, 1)
            # self.log("u_val is %d, learn? %r" % (u_val, u_val < self.value))
            if u_val < self.value:
                self.adjacencies.append(adj)
                # import ipdb ; ipdb.set_trace() #z()  # breakpoint f35d9e23 //

        self.log("Adj after:  " + str(self.adjacencies))
Beispiel #5
0
 def resize(self, size):
     curr_size = len(self.genes)
     if size > curr_size:
         for i in range(size-curr_size):
             self.genes.append(self.__random_gen())
             self.sigma.append(uniform.rvs())
     elif size < curr_size:
         self.genes = self.genes[:size]
         self.sigma = self.sigma[:size]
 def mutate(self):
     # Mutate as a binary.
     sq = self.individual.genoma[0]
     q = sq.genes[0]
     data_set = self.individual.data_set
     pm = 1.0 / float(len(self.genes))
     mut = 0.0
     for i in range(len(self.genes)):
         x = uniform.rvs()
         if x < pm:
             mut += 1.0
             self.genes[i] = data_set.get_nearest(self.get_gen(i), q, \
             exclusion=self.genes)
     self.mutation_factor = mut/float(len(self.genes))
Beispiel #7
0
 def test_add_noise(self):
     '''Test that noise is added predictably.'''
     # test uniform noise, seed for reproducibility
     inp_data = \
         array([-1.875, -1.5  , -1.125, -0.75 , -0.375,  0.,  0.375,  0.75 ,
             1.125,  1.5  ,  1.875,  2.25 ,  2.625, -3.   , -2.625, -2.25 ,
            -1.875, -1.5  , -1.125, -0.75 , -0.375,  0.   ,  0.375,  0.75 ,
             1.125])
     nfap = [uniform, -.1, .2]
     seed(0)
     exp = uniform.rvs(-.1, .2, size=25) + inp_data
     seed(0)
     obs = add_noise(nfap, inp_data)
     assert_array_almost_equal(exp, obs)
Beispiel #8
0
def model2_otu(inducer_arr, rules):
    """Creates an OTU vector according to model 2 and given params.
    Inputs:
     inducer_arr - 2d array or OTU vectors which are inducing vectors for the 
     created otu.
     rules - list of len(2) lists with l[0] float (amount to multiply by when 
     creating new vector) and l[1] str ('self' or 'sum' where self means 
     multiply l[0] times inducing OTU abundance and 'sum' means multiply l[0] by
     induced OTU abundance after all 'self' additions have been made).
    """
    # create rule function which assigns to each inducer array input an output
    raw_otu_vals = array([model2_eval_rules(rules, i) for i in inducer_arr.T])
    # add noise
    return uniform.rvs(0.9, 0.2, size=raw_otu_vals.shape[0]) * raw_otu_vals
Beispiel #9
0
 def test_add_noise(self):
     '''Test that noise is added predictably.'''
     # test uniform noise, seed for reproducibility
     inp_data = \
         array([-1.875, -1.5  , -1.125, -0.75 , -0.375,  0.,  0.375,  0.75 ,
             1.125,  1.5  ,  1.875,  2.25 ,  2.625, -3.   , -2.625, -2.25 ,
            -1.875, -1.5  , -1.125, -0.75 , -0.375,  0.   ,  0.375,  0.75 ,
             1.125])
     nfap = [uniform, -.1, .2]
     seed(0)
     exp = uniform.rvs(-.1, .2, size=25)+inp_data
     seed(0)
     obs = add_noise(nfap, inp_data)
     assert_array_almost_equal(exp, obs)
Beispiel #10
0
def model2_otu(inducer_arr, rules):
    """Creates an OTU vector according to model 2 and given params.
    Inputs:
     inducer_arr - 2d array or OTU vectors which are inducing vectors for the 
     created otu.
     rules - list of len(2) lists with l[0] float (amount to multiply by when 
     creating new vector) and l[1] str ('self' or 'sum' where self means 
     multiply l[0] times inducing OTU abundance and 'sum' means multiply l[0] by
     induced OTU abundance after all 'self' additions have been made).
    """
    # create rule function which assigns to each inducer array input an output
    raw_otu_vals = array([model2_eval_rules(rules, i) for i in inducer_arr.T])
    # add noise
    return uniform.rvs(.9, .2, size=raw_otu_vals.shape[0]) * raw_otu_vals
Beispiel #11
0
 def mutate(self):
     # Mutate as a binary.
     sq = self.individual.genoma[0]
     q = sq.genes[0]
     data_set = self.individual.data_set
     pm = 1.0 / float(len(self.genes))
     mut = 0.0
     for i in range(len(self.genes)):
         x = uniform.rvs()
         if x < pm:
             mut += 1.0
             self.genes[i] = data_set.get_nearest(self.get_gen(i), q, \
             exclusion=self.genes)
     self.mutation_factor = mut / float(len(self.genes))
Beispiel #12
0
def generate_otu_from_pt_in_R5(pt, wave_f, y_shift=None):
    """Generate an OTU sequence from a pt in R5."""
    freq, amp, phase_offset, noise, sampling_params = pt
    # noise is from a uniform distribution where amp*noise controls noise level
    noise_func_and_params = [uniform, -noise * amp, 2 * noise * amp]
    # if y_pos is none we will randomly select the y_shift for the signal
    # between 50 percent of amplitude and 150 percent of amplitude
    if y_shift == None:
        y_shift = uniform.rvs(0.5 * amp, 1.5 * amp)
    # make the base otu + y_shift
    base_otu = y_shift + signal(amp, freq, phase_offset, wave_f, sampling_freq=100, lb=0, ub=2 * pi)
    # add noise
    noisy_otu = add_noise(noise_func_and_params, base_otu)
    # subsample the otu according to the sampling_f and params
    sampling_f = sampling_params[0]
    return sampling_f(noisy_otu, *sampling_params[1:])
Beispiel #13
0
def generate_otu_from_pt_in_R5(pt, wave_f, y_shift=None):
    '''Generate an OTU sequence from a pt in R5.'''
    freq, amp, phase_offset, noise, sampling_params = pt
    # noise is from a uniform distribution where amp*noise controls noise level
    noise_func_and_params = [uniform, -noise * amp, 2 * noise * amp]
    # if y_pos is none we will randomly select the y_shift for the signal
    # between 50 percent of amplitude and 150 percent of amplitude
    if y_shift == None:
        y_shift = uniform.rvs(.5 * amp, 1.5 * amp)
    # make the base otu + y_shift
    base_otu = y_shift + signal(
        amp, freq, phase_offset, wave_f, sampling_freq=100, lb=0, ub=2 * pi)
    # add noise
    noisy_otu = add_noise(noise_func_and_params, base_otu)
    # subsample the otu according to the sampling_f and params
    sampling_f = sampling_params[0]
    return sampling_f(noisy_otu, *sampling_params[1:])
Beispiel #14
0
def model1_otu(inducer_arr, df_and_params, weights, rules):
    """Creates an OTU vector according to model 1 and given params.
    Inputs:
     inducer_arr - 2d array or OTU vectors which are inducing vectors for the 
     created otu.
     df_and_params - list with [0]=scipy.stats.distribution func, and following 
     entries params for the df in proper order. 
     weight_arr - arr, prob(out_vector[i]!=0 | x rules satisfied). weight arr is 
     len(rules)+1 for model 1. The reason for the +1 is that if no rules are 
     specified, a base probability for occurrence of the OTU is still needed.
     rules - list of len(2) lists with l[0]=lower bound, l[1] upper bound for
     rule evaluation.
     """
    num_samples = inducer_arr.shape[1]
    # random draw from distribution according to parameters
    unweighted_draw = df_and_params[0].rvs(*df_and_params[1:], size=num_samples)
    # find out number of conditions satisfied and create uniform draw matrix.
    cs = [model1_eval_rules(rules, otu_vals) for otu_vals in inducer_arr.T]
    # draw from uniform distribution and weight according to weights matrix.
    res = where(uniform.rvs(0, 1, size=num_samples) > 1 - weights.take(cs), 1.0, 0)
    # multiply element wise to produce output vector
    return (res * unweighted_draw).astype(int)
Beispiel #15
0
def model1_otu(inducer_arr, df_and_params, weights, rules):
    """Creates an OTU vector according to model 1 and given params.
    Inputs:
     inducer_arr - 2d array or OTU vectors which are inducing vectors for the 
     created otu.
     df_and_params - list with [0]=scipy.stats.distribution func, and following 
     entries params for the df in proper order. 
     weight_arr - arr, prob(out_vector[i]!=0 | x rules satisfied). weight arr is 
     len(rules)+1 for model 1. The reason for the +1 is that if no rules are 
     specified, a base probability for occurrence of the OTU is still needed.
     rules - list of len(2) lists with l[0]=lower bound, l[1] upper bound for
     rule evaluation.
     """
    num_samples = inducer_arr.shape[1]
    # random draw from distribution according to parameters
    unweighted_draw = df_and_params[0].rvs(*df_and_params[1:],
                                           size=num_samples)
    # find out number of conditions satisfied and create uniform draw matrix.
    cs = [model1_eval_rules(rules, otu_vals) for otu_vals in inducer_arr.T]
    # draw from uniform distribution and weight according to weights matrix.
    res = where(
        uniform.rvs(0, 1, size=num_samples) > 1 - weights.take(cs), 1., 0)
    # multiply element wise to produce output vector
    return (res * unweighted_draw).astype(int)
 def _rvs(self, c, a, b):
     probs = self._delta * uniform.rvs(size=self._size) + self._Fa
     return loggamma.ppf(probs, c)
Beispiel #17
0
 def rvs(self, n, size):
     out = []
     rand_list = uniform.rvs(size = size)
     for rand_num in rand_list:
         out.append(self.ppf(n, rand_num))
     return out
Beispiel #18
0
 def random_initialization(self):
     self.genes = []
     for i in range(self.size):
         self.genes.append(randint.rvs(1, 100))
     self.sigma = [uniform.rvs()]
Beispiel #19
0
 def random_initialization(self):
     self.genes = random.sample(range(self.lsup), self.size)
     self.sigma = []
     for i in range(self.size):
         self.sigma.append(uniform.rvs())
Beispiel #20
0
null_table2 = model2_table(otu_sums, samples, seq_depth, tpk)

print 'null done'

##################################################
#                 ga table                       #
##################################################

# run the genetic algorithms method on a known
# otu sequence and maximize graphic dissimilarity
# pearson distance is about .007
from numpy.random import seed
seed(2039203920392039)
from generators.ga import *
from scipy.stats.distributions import uniform
ref_gene = uniform.rvs(100, 1000, size=(50, 2))
igp = [
    ref_gene[:] + uniform.rvs(100, 1000, size=ref_gene.shape)
    for i in range(400)
]

gc, fmeans, fmaxes = evolve(igp, ref_gene, 1000)
tmp_ga_table = vstack([i.T for i in gc])

# remove negative entries
ga_table = []
for i in range(400):
    if (tmp_ga_table[2 * i:2 * (i + 1)] < 0).sum() < 1:
        ga_table.append(tmp_ga_table[2 * i:2 * (i + 1)])
ga_table = vstack(ga_table)
 def random_initialization(self):
     self.genes = []
     for i in range(self.size):
         self.genes.append(randint.rvs(1, 100))
     self.sigma = [uniform.rvs()]
#     partial_obligate_syntrophic_related_1d]).astype(int).astype(float)

# def make_ids(data):
#     sids = ['s%i' % i for i in range(data.shape[1])]
#     oids = ['o%i' % i for i in range(data.shape[0])]
#     return sids, oids

# sids, oids = make_ids(otu_data)

# bt = table_factory(otu_data, sids, oids)


################ GA Table #######################

seed(300)
ref_a = uniform.rvs(100,1000,size=50)
ref_b = ref_a + uniform.rvs(0,20,size=50)
ref_gene = array([ref_a,ref_b]).T

igp = [ref_gene[:]+uniform.rvs(100,1000,size=ref_gene.shape) for i in range(500)]
gc, fmeans, fmaxes = evolve(igp, ref_gene, 1000)
tmp_ga_table = vstack([i.T for i in gc])

ga_table = []
for i in range(500):
    if (tmp_ga_table[2*i:2*(i+1)]>0).all():
        ga_table.append(tmp_ga_table[2*i:2*(i+1)])
ga_table = vstack(ga_table)


# this removes all otus (rows) from the table for which the correlation between
 def random_initialization(self):
     self.genes = random.sample(range(self.lsup), self.size)
     self.sigma = []
     for i in range(self.size):
         self.sigma.append(uniform.rvs())
Beispiel #24
0






# test 2/25/2013
# create 200 otu X 50 sample table. use 4 otu's per rule 

# generate background otus which will be used to induce 
w = lognorm.rvs(2,1,size=10000).astype(int).reshape(200,50)
# generate model 1 rules
model1_rules = []
for i in range(10):
    rule_vals = uniform.rvs(-100,400,size=8)
    # sort rules so that left is lb <= ub
    rule_vals = where(rule_vals > 0, rule_vals, 0).reshape(4,2)
    rule_vals.sort()
    model1_rules.append(map(list, rule_vals))
# define weights and new otu distribution function
weights = array([1.0, 1.0, 1.0, 1.0, .2])
df_and_params = [lognorm, 2, 1]
# create new otus
table_index = 0
new_otus = []
table_map = []
for rules in model1_rules:
    tmp_table_map = [table_index]
    inducer_arr = w[table_index:table_index+4]
    notu = model1_otu(inducer_arr, df_and_params, weights, rules)
Beispiel #25
0
 def rvs(self, n, size):
     out = []
     rand_list = uniform.rvs(size=size)
     for rand_num in rand_list:
         out.append(self.ppf(n, rand_num))
     return out
print 'null done'


##################################################
#                 ga table                       #
##################################################

# run the genetic algorithms method on a known
# otu sequence and maximize graphic dissimilarity
# pearson distance is about .007
from numpy.random import seed
seed(2039203920392039)
from generators.ga import *
from scipy.stats.distributions import uniform
ref_gene = uniform.rvs(100,1000,size=(50,2))
igp = [ref_gene[:]+uniform.rvs(100,1000,size=ref_gene.shape) for i in range(400)]

gc, fmeans, fmaxes = evolve(igp, ref_gene, 1000)
tmp_ga_table = vstack([i.T for i in gc])

# remove negative entries
ga_table = []
for i in range(400):
    if (tmp_ga_table[2*i:2*(i+1)]<0).sum() < 1:
        ga_table.append(tmp_ga_table[2*i:2*(i+1)])
ga_table = vstack(ga_table)

print 'ga done'

##################################################
Beispiel #27
0
 def rvs(self, n):
     return self.orv.ppf(self.F_ab * unif.rvs(size=n) + self.F_a)