Exemple #1
0
def load_data(primal=True, fold_index=0):
    X1 = np.random.rand(20, 300)
    X2 = np.random.rand(10, 200)
    dfold = [1, 2, 4, 5]
    tfold = [0, 6, 7]
    Y = np.random.randn(X1.shape[0], X2.shape[0])
    Y = np.where(Y >= 0, 1., -1.)
    dtraininds = list(set(range(Y.shape[0])).difference(dfold))
    ttraininds = list(set(range(Y.shape[1])).difference(tfold))
    X1_train = X1[dtraininds, :]
    X2_train = X2[ttraininds, :]
    X1_test = X1[dfold, :]
    X2_test = X2[tfold, :]
    KT = np.mat(X2)
    KT = KT * KT.T
    KD = np.mat(X1)
    KD = KD * KD.T
    K1_train = KD[np.ix_(dtraininds, dtraininds)]
    K2_train = KT[np.ix_(ttraininds, ttraininds)]
    Y_train = Y[np.ix_(dtraininds, ttraininds)]
    K1_test = KD[np.ix_(dfold, dtraininds)]
    K2_test = KT[np.ix_(tfold, ttraininds)]
    Y_test = Y[np.ix_(dfold, tfold)]
    ssize = int(Y_train.shape[0] * Y_train.shape[1] * 0.25)
    rows = numpyrandom.random_integers(0, K1_train.shape[0] - 1, ssize)
    cols = numpyrandom.random_integers(0, K2_train.shape[0] - 1, ssize)
    ind = np.ravel_multi_index([rows, cols],
                               (K1_train.shape[0], K2_train.shape[0]))
    Y_train = Y_train.ravel()[ind]
    Y_test = Y_test.ravel(order='F')
    if primal:
        return X1_train, X2_train, Y_train, rows, cols, X1_test, X2_test, Y_test
    else:
        return K1_train, K2_train, Y_train, rows, cols, K1_test, K2_test, Y_test
Exemple #2
0
def load_data(primal=True, fold_index=0):
    X1 = np.random.rand(20, 300)
    X2 = np.random.rand(10, 200)
    dfold = [1,2,4,5]
    tfold = [0,6,7]
    Y = np.random.randn(X1.shape[0], X2.shape[0])
    Y = np.where(Y>=0, 1., -1.)
    dtraininds = list(set(range(Y.shape[0])).difference(dfold))
    ttraininds = list(set(range(Y.shape[1])).difference(tfold))    
    X1_train = X1[dtraininds, :]
    X2_train = X2[ttraininds, :]
    X1_test = X1[dfold,:]
    X2_test = X2[tfold,:]
    KT = np.mat(X2)
    KT = KT * KT.T
    KD = np.mat(X1)
    KD = KD * KD.T
    K1_train = KD[np.ix_(dtraininds, dtraininds)]
    K2_train = KT[np.ix_(ttraininds, ttraininds)]
    Y_train = Y[np.ix_(dtraininds, ttraininds)]
    K1_test = KD[np.ix_(dfold,dtraininds)]
    K2_test = KT[np.ix_(tfold,ttraininds)]
    Y_test = Y[np.ix_(dfold, tfold)]
    ssize = int(Y_train.shape[0]*Y_train.shape[1]*0.25)
    rows = numpyrandom.random_integers(0, K1_train.shape[0]-1, ssize)
    cols = numpyrandom.random_integers(0, K2_train.shape[0]-1, ssize)
    ind = np.ravel_multi_index([rows, cols], (K1_train.shape[0], K2_train.shape[0]))
    Y_train = Y_train.ravel()[ind]
    Y_test = Y_test.ravel(order='F')
    if primal:
        return X1_train, X2_train, Y_train, rows, cols, X1_test, X2_test, Y_test
    else:
        return K1_train, K2_train, Y_train, rows, cols, K1_test, K2_test, Y_test
Exemple #3
0
def shuffle(bedtool, background,organism):
	""" Accepts a file path to a bed file and a background file
	Random intervals are generate from the intervals in the background
	file.  An attempt is made to make the new random intervals the same size
	as the original intervals. One new interval is generated per interval in the
	bed file.

	If no background is provided, The pybedtool internal shuffle function is called
	and the entire genome is used as background
	"""
	A, B =  bedtool,background
	if B is not None:
		rand_a = ""
		for a in A:
			a_len = a.length
			r = rand.random_integers(0,len(B)-1)
			# if cur A length is greater than random background inteval
			# the new randA is set to be same size as background inteval
			if  a_len > B[r].length:
				rand_a += "\t".join(B[r][:4]) +"\t" + "\t".join(a[4:])+"\n"
			else:
				randstart = rand.random_integers(B[r].start,B[r].end - a_len)
				rand_a += "\t".join([B[r].chrom,str(randstart),str(randstart+a_len),
					B[r].name,
					"\t".join(a[4:])]) + "\n"	
		rand_A = BedTool(rand_a,from_string=True)
		return rand_A
	else:
		return A.shuffle(genome=organism,chrom=True)
Exemple #4
0
def displacement(parent):

    # generate two key positions; second position should be different than first
    p1 = random.random_integers(len(parent) - 2)

    while True:
        p2 = random.random_integers(len(parent) - 2)
        if p2 != p1:
            break

    keyPos = [p1, p2]
    keyPos.sort()

    # generate third key position different than the min keyPos
    while True:
        p3 = random.random_integers(len(parent)) - 1
        if p3 != keyPos[0]:
            break

    offspring = [None for i in range(len(parent))]

    slicedP = parent[:keyPos[0]] + parent[keyPos[1] + 1:]

    # starting from p3 report the alleles between p1 and p2
    for i in range(keyPos[1] - keyPos[0] + 1):
        offspring[(p3 + i) % len(parent)] = parent[keyPos[0] + i]

    startPos = (p3 + keyPos[1] - keyPos[0] + 1) % len(parent)

    # complete the offspring with alleles from the slicedP
    for i in range(len(slicedP)):
        offspring[(startPos + i) % len(parent)] = slicedP[i]

    return offspring
 def Evolve_DE(self):
     for i in scipy.arange(self.npop1):
         # para cada individuo da populacao
         # gera trial vector usado para perturbar individuo atual (indice i)
         # a partir de 3 individuos escolhidos aleatoriamente na populacao e
         # cujos indices sejam distintos e diferentes de i
         invalido = True
         while invalido:
             j = random_integers(0, self.npop1 - 1, 3)
             invalido = (i in j)
             invalido = invalido or (j[0] == j[1])
             invalido = invalido or (j[1] == j[2])
             invalido = invalido or (j[2] == j[0])
         # trial vector a partir da mutacao de um alvo
         u = self.pop1[j[0]] + self.beta * (self.pop1[j[1]] -
                                            self.pop1[j[2]])
         # gera por crossover solucao candidata
         c = self.pop1[i].copy()
         # seleciona indices para crossover
         # garantindo que ocorra crossover em
         # pelo menos uma vez
         j = random_integers(0, self.pop1.shape[1] - 1)
         for k in scipy.arange(self.pop1.shape[1]):
             if (scipy.rand() < self.pr) or (k == j):
                 c[k] = u[k]
         ans, c = self.resolve_desafio(c)
         c_fit = self.avalia_aptidao1(ans)
         # leva para proxima geracao quem tiver melhor fitness
         if (c_fit > self.fit1[i]):
             self.pop1[i] = c
             self.fit1[i] = c_fit
             self.ans1[i] = ans
Exemple #6
0
def uniform_crossover(parents, len_items):
    parents_crossover = []

    while len(parents) > 0:
        first_person = parents.pop(random.random_integers(0, len(parents) - 1))
        second_person = parents.pop(random.random_integers(0, len(parents) - 1))

        if random.rand() > 0.6:
            pattern = random.random_integers(0, 1, len_items)
            for i, cross in enumerate(pattern):
                if cross == 1:
                    r = first_person.backpack[i]
                    first_person.backpack[i] = second_person.backpack[i]
                    second_person.backpack[i] = r

            alfa = random.rand()

            new_first_deviation = alfa * np.array(first_person.standard_deviation) + (1 - alfa) * np.array(second_person.standard_deviation)
            new_second_deviation = alfa * np.array(second_person.standard_deviation) + (1 - alfa) * np.array(first_person.standard_deviation)
            first_person.standard_deviation = new_first_deviation
            second_person.standard_deviation = new_second_deviation

        parents_crossover.append(first_person)
        parents_crossover.append(second_person)

    return parents_crossover
    def test_002(self):
        vlen = 200
        N = 100000
        _n = N / vlen

        refdata = random_integers(0, 1, N)
        data = random_integers(0, 1, N)

        ref = numpy.array([0] * (vlen / 2))
        for x in range(N / 2, N):
            ref[x % (vlen / 2)] += 1 if refdata[x] != data[x] else 0

        vlen = concatenate([[vlen] * (_n / 2), [vlen / 2] * (_n)])
        src0 = gr.vector_source_b(refdata.tolist())
        src1 = gr.vector_source_b(data.tolist())
        src2 = gr.vector_source_i(vlen.tolist())

        uut = ofdm.bit_position_dependent_BER("test")

        self.tb.connect(src0, (uut, 0))
        self.tb.connect(src1, (uut, 1))
        self.tb.connect(src2, (uut, 2))

        if os.path.exists("test_000.uint"):
            os.remove("test_000.uint")

        self.assert_(not os.path.exists("test_000.uint"))

        self.tb.run()

        ret = numpy.array(uut.get_cntr_vec())

        self.assert_((ret == ref).all())

        self.assert_(os.path.exists("test_000.uint"))
    def test_001(self):
        vlen = 256
        N = 100000

        refdata = random_integers(0, 1, N)
        data = random_integers(0, 1, N)

        ref = numpy.array([0] * vlen)
        for x in range(len(data)):
            ref[x % vlen] += 1 if refdata[x] != data[x] else 0

        src0 = gr.vector_source_b(refdata.tolist())
        src1 = gr.vector_source_b(data.tolist())
        src2 = gr.vector_source_i([vlen] * N)

        uut = ofdm.bit_position_dependent_BER("test")

        self.tb.connect(src0, (uut, 0))
        self.tb.connect(src1, (uut, 1))
        self.tb.connect(src2, (uut, 2))

        self.tb.run()

        ret = numpy.array(uut.get_cntr_vec())

        self.assert_((ret == ref).all())
Exemple #9
0
def order2x(parent1, parent2):

    # verify that the parents have same length
    assert len(parent1) == len(parent2)
    numGenes = len(parent1)

    # generate number of key positions
    numKP = random.random_integers(numGenes - 1)

    # generate key positions
    keyPositions = []
    key = 0
    while key < numKP:
        possibleKey = random.random_integers(numGenes - 1)
        if possibleKey not in keyPositions:
            keyPositions.append(possibleKey)
            key += 1

    # sort key positions
    keyPositions.sort()
    # generate first offspring
    offspring1 = offGeneration(keyPositions, parent1, parent2)

    # generate second offspring
    offspring2 = offGeneration(keyPositions, parent2, parent1)

    return offspring1, offspring2
Exemple #10
0
def createHeteroAPD(tissueHeight, APDrefernce, PatchDiam, PatchAPDref,
                    APDfile):
    """
    Write a set of tissueHeight^2 APD values to APDfile with a patch of
    different APD in the middle
    """
    # Create a square array to represent the tissue
    setAPD = np.zeros((tissueHeight, tissueHeight))
    # Identify where the patch will start
    patchStart = np.floor(tissueHeight / 2) - 1
    # Set the values in the patch to NaN to make them easy to find
    for i in np.arange(patchStart, patchStart + PatchDiam, 1):
        for j in np.arange(patchStart, patchStart + PatchDiam, 1):
            setAPD[int(i), int(j)] = np.NaN
    # Flatten the array so the indices match up to ResList's (a flat array)
    setAPD = np.reshape(setAPD, (-1, 1))
    for i in range(len(setAPD)):
        if (setAPD[i, 0] == np.NaN):  # in the patch
            setAPD[i] = npr.random_integers(PatchAPDref - 5, PatchAPDref + 5)
        else:
            setAPD[i] = npr.random_integers(APDrefernce - 5, APDrefernce + 5)
    with open(APDfile, 'w') as file:
        for apd in setAPD:
            theString = "{!s}\n"
            file.write(theString.format(int(apd[0])))
    return
Exemple #11
0
def displacement(parent):
    
    # generate two key positions; second position should be different than first
    p1 = random.random_integers(len(parent)-2)

    while True:
        p2 = random.random_integers(len(parent)-2)
        if p2 != p1:
            break
    
    keyPos = [p1,p2]
    keyPos.sort()
    
    # generate third key position different than the min keyPos
    while True:
        p3 = random.random_integers(len(parent))-1
        if p3 != keyPos[0]:
            break

    offspring = [None for i in range(len(parent))]    
    
    slicedP = parent[:keyPos[0]]+parent[keyPos[1]+1:]
    
    # starting from p3 report the alleles between p1 and p2
    for i in range(keyPos[1]-keyPos[0]+1): 
        offspring[(p3+i)%len(parent)] = parent[keyPos[0]+i]
        
    startPos = (p3+keyPos[1]-keyPos[0]+1)%len(parent)
    
    # complete the offspring with alleles from the slicedP
    for i in range(len(slicedP)):
        offspring[(startPos+i)%len(parent)] = slicedP[i]
    
    return offspring
Exemple #12
0
 def gera_individuo(self):
  l = []
  l.append(random_integers(self.arg_lim[0][0],self.arg_lim[0][1]))
  l.append(self.arg_lim[1][0]+ (self.arg_lim[1][1] - self.arg_lim[1][0])*rand())
  l.append(self.arg_lim[2][0]+ (self.arg_lim[2][1] - self.arg_lim[2][0])*rand())
  l.append(random_integers(self.arg_lim[3][0],self.arg_lim[3][1]))
  return np.array(l)	
 def test_001( self ):
   vlen = 256
   N = 100000
   
   refdata = random_integers(0, 1, N)
   data = random_integers(0, 1, N)
   
   ref = numpy.array([0]*vlen)
   for x in range(len(data)):
     ref[x%vlen] += 1 if refdata[x] != data[x] else 0
     
   src0 = gr.vector_source_b( refdata.tolist() )
   src1 = gr.vector_source_b( data.tolist() )
   src2 = gr.vector_source_i( [vlen]*N )
   
   uut = ofdm.bit_position_dependent_BER( "test" )
   
   self.tb.connect( src0, ( uut, 0 ) )
   self.tb.connect( src1, ( uut, 1 ) )
   self.tb.connect( src2, ( uut, 2 ) )
   
   self.tb.run()
   
   ret = numpy.array( uut.get_cntr_vec() )
   
   self.assert_( (ret==ref).all() )
 def test_003( self ):
   vlen = 200
   N = 100000
   _n = N / vlen
   
   refdata = random_integers(0, 1, N)
   data = random_integers(0, 1, N)
   
   vlen = concatenate( [ [vlen]*(_n/2),[vlen/2]*(_n/2),[vlen/4]*(_n)] ) 
   src0 = gr.vector_source_b( refdata.tolist() )
   src1 = gr.vector_source_b( data.tolist() )
   src2 = gr.vector_source_i( vlen.tolist() )
   
   uut = ofdm.bit_position_dependent_BER( "test" )
   
   self.tb.connect( src0, ( uut, 0 ) )
   self.tb.connect( src1, ( uut, 1 ) )
   self.tb.connect( src2, ( uut, 2 ) )
   
   if os.path.exists("test_000.uint"):
     os.remove("test_000.uint")
   if os.path.exists("test_001.uint"):
     os.remove("test_001.uint")
     
   self.assert_( not os.path.exists("test_000.uint") )
   self.assert_( not os.path.exists("test_001.uint") )
   
   self.tb.run()
   
   self.assert_( os.path.exists("test_000.uint"))
   self.assert_( os.path.exists("test_001.uint"))
def el_distor(im,  kernel_dim, Sigma, alpha):
    
    out = np.zeros(im.shape)
    
    displace_x = np.array([[random_integers(-1, 1) for x in xrange(im.shape[0])] \
                            for y in xrange(im.shape[1])]) * alpha
    displace_y = np.array([[random_integers(-1, 1) for x in xrange(im.shape[0])] \
                            for y in xrange(im.shape[1])]) * alpha
                   
    kernel = create_gaussian_kernel(kernel_dim, Sigma)
    #kernel = cv2.getGaussianKernel(kernel_dim,Sigma)
    displace_x = convolve2d(displace_x, kernel)
    displace_y = convolve2d(displace_y, kernel)
    
    for row in xrange(im.shape[1]):
        for col in xrange(im.shape[0]):
            low_x = row + int(math.floor(displace_x[row, col]))
            high_x = row + int(math.ceil(displace_x[row, col]))

            low_y = col + int(math.floor(displace_y[row, col]))
            high_y = col + int(math.ceil(displace_y[row, col]))

            if  high_x >= im.shape[1] -1 or high_y >= im.shape[0] - 1:
                continue

            res = im[low_x, low_y]/4 + im[low_x, high_y]/4 + \
                    im[high_x, low_y]/4 + im[high_x, high_y]/4

            out[row, col] = res   
    n,m = out.shape
    out = out.reshape(n*m,)    
    return out
def test_sample_box():
    """Test that `sample_box` generates the proper number of samples within the
    correct bounds.

    """
    # Generate some random bounds
    bounds = []
    bounds_count = 0
    num_dimensions = random_integers(2, 10)
    while bounds_count < num_dimensions:
        candidate_bound = random_sample(2)
        low, high = candidate_bound
        if low > high:
            candidate_bound = [high, low]
        bounds.append(candidate_bound)
        bounds_count += 1

    num_samples = random_integers(100)
    points = sample_box(bounds, num_samples)

    assert len(points) == num_samples

    results = np.empty([num_samples, num_dimensions])
    for counter, point in enumerate(points):
        for axis, value in enumerate(point):
            low = bounds[axis][0]
            high = bounds[axis][1]
            results[counter, axis] = (low <= value <= high)
    assert results.all()
Exemple #17
0
def corrupt_image(img,
                  MAR_prob=0,
                  min_rects=0,
                  max_rects=0,
                  min_width=0,
                  max_width=0):
    new_img = img.copy()
    mask = np.zeros(img.shape[0:2], dtype=np.bool)
    if MAR_prob > 0:
        mask[(random_sample(mask.shape) < MAR_prob)] = True
    if max_rects > 0 and max_width > 0:
        h, w = mask.shape
        num_rects = random_integers(min_rects, max_rects)
        for i in range(num_rects):
            px1 = random_integers(0, w - min(max(min_width, 1), w))
            py1 = random_integers(0, h - min(max(min_width, 1), h))
            px2 = px1 + (min_width - 1) + random_integers(
                0, max(min(w - px1 - min_width, max_width - min_width), 0))
            py2 = py1 + (min_width - 1) + random_integers(
                0, max(min(h - py1 - min_width, max_width - min_width), 0))
            if px1 <= px2 and py1 <= py2:
                mask[py1:py2, px1:px2] = True
            else:
                # One of the sides has length 0, so we should remove any pixels4
                pass
    if len(new_img.shape) == 2:
        new_img[mask] = 0
    else:
        new_img[mask, :] = 0
    return (new_img, 1.0 * mask)
def generate_data(start_index=START_INDEX, num_users=NUM_USERS,
                  num_rows=NUM_ROWS):
    users = random_integers(start_index, start_index+num_users, num_rows)
    activity = random_integers(0, len(MESSAGE_TYPES) - 1, num_rows)
    activity_name = [MESSAGE_TYPES[i] for i in activity]
    user_activity = zip(users, activity_name)
    return user_activity
Exemple #19
0
def order2x(parent1, parent2):
    
    # verify that the parents have same length
    assert(len(parent1)==len(parent2))
    numGenes = len(parent1)
    
    # generate number of key positions
    numKP = random.random_integers(numGenes-1)
    
    # generate key positions
    keyPositions = []
    key = 0
    while key < numKP:
        possibleKey = random.random_integers(numGenes-1)
        if possibleKey not in keyPositions:
            keyPositions.append(possibleKey)
            key += 1
    
    # sort key positions
    keyPositions.sort()
    # generate first offspring
    offspring1 = offGeneration(keyPositions, parent1, parent2)
    
    # generate second offspring
    offspring2 = offGeneration(keyPositions, parent2, parent1)
    
    return offspring1, offspring2
def random_crop(image, normal, size):
    decal = [image.shape[0]-size[0],image.shape[1]-size[1]];
    #tirer decalage aleatoire
    decal_alea = [random_integers(0,decal[0]),random_integers(0,decal[1])]
    crop_img = image[decal_alea[0]:decal_alea[0]+size[0], decal_alea[1]:decal_alea[1]+size[1]]
    crop_normal = normal[decal_alea[0]:decal_alea[0]+size[0], decal_alea[1]:decal_alea[1]+size[1]]
    return crop_img,crop_normal
Exemple #21
0
def getPoly(order, sym=sympy.symbols('x')):
    roots = random_integers(-5,5, order)
    leadCoeff = random_integers(1,5)
    poly = sympy.S(leadCoeff)
    for root in roots:
        poly *= sym-root
    return sympy.Poly(poly.expand())
def getPoly(order, sym=sympy.symbols('x')):
    roots = random_integers(-5, 5, order)
    leadCoeff = random_integers(1, 5)
    poly = sympy.S(leadCoeff)
    for root in roots:
        poly *= sym - root
    return sympy.Poly(poly.expand())
def MangleByte(packet):
  index = random_integers(0,packet.size-1)
  new_byte = random_integers(0,255)
  while new_byte == packet[index]:
    new_byte = random_integers(0,255)
  packet[index] = new_byte
  return packet
Exemple #24
0
def mutate_packet(pkt, state, lan):
    radiohead = pkt.getlayer(RadioTap2)

    mutateDot11Body = pkt.getlayer(Dot11)
    mutateDot11Body.proto = np.random_integers(0, 256**2 - 1, 1)[0]
    mutateDot11Body.FCfield = np.random_integers(0, 256 - 1, 1)[0]
    mutateDot11Body.ID = np.random_integers(0, 256**2 - 1, 1)[0]
    mutateDot11Body.SC = np.random_integers(0, 256**2 - 1, 1)[0]

    returnPkt = None
    if (state == 0):
        mutatePrbElt = np.bytes(np.random_integers(0, 1514 - 424, 1)[0])
        returnPkt = radiohead / mutateDot11Body / mutatePrbElt

    if (state == 1):

        mutateAuthBody = pkt.getlayer(Dot11Auth)
        mutateAuthBody.algo = np.random_integers(0, 256**2 - 1, 1)[0]
        mutateAuthBody.sequm = np.random_integers(0, 256**2 - 1, 1)[0]
        mutateAuthBody.status = np.random_integers(0, 256**2 - 1, 1)[0]
        # mutateAuthElt = np.bytes(1514 - 54) #not necessary

        returnPkt = radiohead / mutateDot11Body / mutateAuthBody

    if (state == 2):
        mutateAssoBody = pkt.getlayer(Dot11AssoReq)
        mutateAssoBody.cap = np.random_integers(0, 256**2 - 1, 1)[0]
        mutateAssoElt = np.bytes(np.random_integers(0, 1514 - 424, 1)[0])

        returnPkt = radiohead / mutateDot11Body / mutateAssoBody / mutateAssoElt

    sendp(returnPkt, iface=lan, verbose=3)
    mutateList.append(returnPkt)
    return mutateList
def Fragment(packet):
  index = random_integers(1,packet.size-1)
  if 1 == random_integers(0,1):
    packet = packet[:index]
  else:
    packet = packet[index:]
  return packet
Exemple #26
0
 def Evolve_DE(self):
  for i in scipy.arange(self.npop1):
   # para cada individuo da populacao 
   # gera trial vector usado para perturbar individuo atual (indice i)
   # a partir de 3 individuos escolhidos aleatoriamente na populacao e
   # cujos indices sejam distintos e diferentes de i
   invalido = True
   while invalido:
    j = random_integers(0,self.npop1-1,3)
    invalido = (i in j)
    invalido = invalido or (j[0] == j[1]) 
    invalido = invalido or (j[1] == j[2]) 
    invalido = invalido or (j[2] == j[0])    
   # trial vector a partir da mutacao de um alvo 
   u = self.pop1[j[0]] + self.beta*(self.pop1[j[1]] - self.pop1[j[2]]) 
   # gera por crossover solucao candidata
   c = self.pop1[i].copy()  
   # seleciona indices para crossover
   # garantindo que ocorra crossover em
   # pelo menos uma vez                 
   j = random_integers(0,self.pop1.shape[1]-1)
   for k in scipy.arange(self.pop1.shape[1]):
    if (scipy.rand() < self.pr) or (k == j):
     c[k] = u[k]  
   ans,c = self.resolve_desafio(c)
   c_fit = self.avalia_aptidao1(ans)    
   # leva para proxima geracao quem tiver melhor fitness
   if (c_fit > self.fit1[i]):
    self.pop1[i] = c
    self.fit1[i] = c_fit
    self.ans1[i] = ans
    def create_test_set(self):
        '''
        randomly selects test sentences from positive and negative bags and making a uniform distribution of test sentences
        '''
        from numpy import random as np_random
        count = self.counts["test set"] // 2
        while (count != 0):
            index = np_random.random_integers(low=0,
                                              high=len(self.positive_bag) - 1)
            self.positve_test_bag.append(self.positive_bag.pop(index))
            index = np_random.random_integers(low=0,
                                              high=len(self.negative_bag) - 1)
            self.negative_test_bag.append(self.negative_bag.pop(index))
            count -= 1

        self.logger.info("test sentences selected")
        self.logger.info(
            "Total sentences for testing : " +
            str(len(self.positve_test_bag) + len(self.negative_test_bag)))
        self.logger.info("positive sentences for testing : " +
                         str(len(self.positve_test_bag)))
        self.logger.info("negative sentences for testing : " +
                         str(len(self.negative_test_bag)))

        self.counts["positive sentences"] = len(self.positive_bag)
        self.counts["negative sentences"] = len(self.negative_bag)
        self.counts["total sentences"] = len(self.positive_bag) + len(
            self.negative_bag)
Exemple #28
0
 def __call__(self, X, n = 10, x0 = None):
   '''
   Parameters
   ----------
   n : required number of start points, if  None, defaults to 10 start points
   
   x0 : 2-dimensional numpy.array containing #rows equal to number of explicitly defined start points and #columns equal to 
   dimension of the feature space points
   
   X : 2-dimensional numpy.array containing #rows equal to number of data points and #columns equal to dimension 
   of the data points
   '''
   self._Xi = X
   if n is None or n == 0:
     n = 10
   if x0 is not None:
     num_x0_pts = x0.shape[0]
   else:
     return self._Xi[random_integers(0, self._Xi.shape[0] - 1, n),:]
   if num_x0_pts == n:
     return x0
   elif num_x0_pts < n:
     return vstack((x0,self._Xi[random_integers(0, self._Xi.shape[0] - 1, n - num_x0_pts),:]))
   else: #num_x0_pts > n
     return x0[sample(xrange(0,num_x0_pts), n),:]
def GIBBSSAMPLER(DNA, K, T, N):
      MOTIF = []
      randn = random.random_integers(0, len(DNA[0]), T)
      countmatrix = array([[1.0]*K]*4)
      for i in range(T):
            index = randn[i]
            MOTIF.append(DNA[i][index:index+K])
            countmatrix = UPDATEPROFILE(countmatrix, MOTIF[-1])
      
      bestscore = 1000
      BESTGROUP = []
      for i in range(N):
            randdna = random.random_integers(0, T-1)
            countmatrix = UPDATEPROFILE2(countmatrix, MOTIF[randdna])  
            profile = countmatrix/(T-1)
            probs = FINDPROBS(DNA[randdna], K, profile)
            probs = probs/sum(probs)
            randi = random.choice(range(len(probs)), p = probs)
            motifi = DNA[randdna][randi:randi+K]
            MOTIF[randdna] = motifi

            countmatrix = UPDATEPROFILE(countmatrix, motifi)
            profile = countmatrix/T
            concensus = FINDCONCENSUS(profile)
            curscore = DISTANCEPATTERNSTRING(concensus, MOTIF)
            if curscore < bestscore:
                  BESTGROUP = deepcopy(MOTIF)
                  bestscore = curscore
      return BESTGROUP, bestscore
    def test_003(self):
        vlen = 200
        N = 100000
        _n = N / vlen

        refdata = random_integers(0, 1, N)
        data = random_integers(0, 1, N)

        vlen = concatenate([[vlen] * (_n / 2), [vlen / 2] * (_n / 2),
                            [vlen / 4] * (_n)])
        src0 = gr.vector_source_b(refdata.tolist())
        src1 = gr.vector_source_b(data.tolist())
        src2 = gr.vector_source_i(vlen.tolist())

        uut = ofdm.bit_position_dependent_BER("test")

        self.tb.connect(src0, (uut, 0))
        self.tb.connect(src1, (uut, 1))
        self.tb.connect(src2, (uut, 2))

        if os.path.exists("test_000.uint"):
            os.remove("test_000.uint")
        if os.path.exists("test_001.uint"):
            os.remove("test_001.uint")

        self.assert_(not os.path.exists("test_000.uint"))
        self.assert_(not os.path.exists("test_001.uint"))

        self.tb.run()

        self.assert_(os.path.exists("test_000.uint"))
        self.assert_(os.path.exists("test_001.uint"))
Exemple #31
0
def mutate_simple(ind):
    "just blast one spot"
    mutated = ind.copy()
    row = nprand.random_integers(0, ind.data.shape[0] - 1)
    col = nprand.random_integers(0, ind.data.shape[1] - 1)
    mutated.data[row, col] = nprand.random()
    mutated.cost = -1
    return mutated
Exemple #32
0
 def __gera_s0(self):
  l = []
  l.append(random_integers(self.arg_lim[0][0],self.arg_lim[0][1]))
  l.append(random_integers(self.arg_lim[1][0],self.arg_lim[1][1]))
  l.append(self.arg_lim[2][0]+ (self.arg_lim[2][1] - self.arg_lim[2][0])*rand())
  l.append(random_integers(self.arg_lim[3][0],self.arg_lim[3][1]))
  
  return l
Exemple #33
0
 def N(self):
     if random.sample() > .5:
         return [self.nouns[random.random_integers(0, len(self.nouns) - 1)]]
     else:
         return [self.THE] + [
             self.nouns[random.random_integers(0,
                                               len(self.nouns) - 1)]
         ]
Exemple #34
0
 def random_cl_key(self):
     """Returns a random classifier key.
     """
     a, b = random_integers(0, 100), random_integers(0, 100)
     # make sure that b > a
     if a > b:
         return (b, a)
     return (a, b)
Exemple #35
0
def random_terrain():
    """Return a randomized terrain"""
    answer = np.ones((I_MAX+1, J_MAX+1))*100
    for i, j in zip(nprand.random_integers(0, I_MAX, 8),
                    nprand.random_integers(0, J_MAX, 8)):
        answer[i, j] = nprand.random_integers(2, 4)*100
    answer[0, 0] += 1
    return answer
Exemple #36
0
def _rand_sparse(m, n, density):
    # check parameters here
    nnz = max( min( int(m*n*density), m*n), 0)
    row  = random_integers(low=0, high=m-1, size=nnz)
    col  = random_integers(low=0, high=n-1, size=nnz)
    data = ones(nnz, dtype='int8')
    # duplicate (i,j) entries will be summed together
    return csr_matrix( (data,(row,col)),shape=(m,n))
def generate_data(start_index=START_INDEX,
                  num_users=NUM_USERS,
                  num_rows=NUM_ROWS):
    users = random_integers(start_index, start_index + num_users, num_rows)
    activity = random_integers(0, len(MESSAGE_TYPES) - 1, num_rows)
    activity_name = [MESSAGE_TYPES[i] for i in activity]
    user_activity = zip(users, activity_name)
    return user_activity
 def new_food(terminal, body_locs, edge_x, edge_y):
     valid = False
     while not valid:
         food_loc = (random_integers(edge_y[0] + 1, edge_y[1] - 1),
                     random_integers(
                         random_integers(edge_x[0] + 1, edge_x[1] - 1)))
         valid = food_loc not in body_locs
     return food_loc
Exemple #39
0
def mutate_simple(ind):
    "just blast one spot"
    mutated = ind.copy()
    row = nprand.random_integers(0, ind.data.shape[0] - 1)
    col = nprand.random_integers(0, ind.data.shape[1] - 1)
    mutated.data[row, col] = nprand.random()
    mutated.cost = -1
    return mutated
Exemple #40
0
def _rand_sparse(m, n, density):
    # check parameters here
    nnz = max(min(int(m * n * density), m * n), 0)
    row = random_integers(low=0, high=m - 1, size=nnz)
    col = random_integers(low=0, high=n - 1, size=nnz)
    data = ones(nnz, dtype='int8')
    # duplicate (i,j) entries will be summed together
    return csr_matrix((data, (row, col)), shape=(m, n))
Exemple #41
0
    def successors3(self):
        successors = []
        nc = copy.deepcopy(self.path)
        si = random.random_integers(1, len(self.path)-1)
        ti = random.random_integers(1, len(self.path)-1)
        nc[si], nc[ti] = nc[ti], nc[si]
        successors.append(TravelingSalesmanProblem(nc))

        return successors
 def gera_individuo(self):
     l = []
     l.append(random_integers(self.arg_lim[0][0], self.arg_lim[0][1]))
     l.append(self.arg_lim[1][0] +
              (self.arg_lim[1][1] - self.arg_lim[1][0]) * rand())
     l.append(self.arg_lim[2][0] +
              (self.arg_lim[2][1] - self.arg_lim[2][0]) * rand())
     l.append(random_integers(self.arg_lim[3][0], self.arg_lim[3][1]))
     return np.array(l)
def random_crop(image, normal, size):
    decal = [image.shape[0] - size[0], image.shape[1] - size[1]]
    #tirer decalage aleatoire
    decal_alea = [random_integers(0, decal[0]), random_integers(0, decal[1])]
    crop_img = image[decal_alea[0]:decal_alea[0] + size[0],
                     decal_alea[1]:decal_alea[1] + size[1]]
    crop_normal = normal[decal_alea[0]:decal_alea[0] + size[0],
                         decal_alea[1]:decal_alea[1] + size[1]]
    return crop_img, crop_normal
Exemple #44
0
def myNew():
    global n, an
    q = random.random_integers(10)
    if random.random() > 0.2:
        an += q
        print('mtn {} -A {}'.format(q, random.random_integers(100)), file=f)
    else:
        n += q
        print('mtn {}'.format(q), file=f)
    def __gera_s0(self):
        l = []
        l.append(random_integers(self.arg_lim[0][0], self.arg_lim[0][1]))
        l.append(random_integers(self.arg_lim[1][0], self.arg_lim[1][1]))
        l.append(self.arg_lim[2][0] +
                 (self.arg_lim[2][1] - self.arg_lim[2][0]) * rand())
        l.append(random_integers(self.arg_lim[3][0], self.arg_lim[3][1]))

        return l
Exemple #46
0
def elastic_distortion(image, kernel_dim=21, sigma=6, alpha=30, negated=True):
    """
    This method performs elastic transformations on an image by convolving
    with a gaussian kernel.
    :param image: a numpy nd array
    :kernel_dim: dimension(1-D) of the gaussian kernel
    :param sigma: standard deviation of the kernel
    :param alpha: a multiplicative factor for image after convolution
    :param negated: a flag indicating whether the image is negated or not
    :returns: a nd array transformed image
    """
    # check if the image is a negated one
    if not negated:
        image = 255 - image

    # check if kernel dimesnion is odd
    if kernel_dim % 2 == 0:
        raise ValueError("Kernel dimension should be odd")

    # create an empty image
    result = np.zeros(image.shape)

    # create random displacement fields
    displacement_field_x = np.array([[random_integers(-1, 1) for x in xrange(image.shape[0])] \
                            for y in xrange(image.shape[1])]) * alpha
    displacement_field_y = np.array([[random_integers(-1, 1) for x in xrange(image.shape[0])] \
                            for y in xrange(image.shape[1])]) * alpha

    # create the gaussian kernel
    kernel = create_2d_gaussian(kernel_dim, sigma)

    # convolve the fields with the gaussian kernel
    displacement_field_x = convolve2d(displacement_field_x, kernel)
    displacement_field_y = convolve2d(displacement_field_y, kernel)

    # make the distortrd image by averaging each pixel value to the neighbouring
    # four pixels based on displacement fields

    for row in xrange(image.shape[1]):
        for col in xrange(image.shape[0]):
            low_ii = row + int(math.floor(displacement_field_x[row, col]))
            high_ii = row + int(math.ceil(displacement_field_x[row, col]))

            low_jj = col + int(math.floor(displacement_field_y[row, col]))
            high_jj = col + int(math.ceil(displacement_field_y[row, col]))

            if low_ii < 0 or low_jj < 0 or high_ii >= image.shape[1] -1 \
               or high_jj >= image.shape[0] - 1:
                continue

            res = image[low_ii, low_jj]/4 + image[low_ii, high_jj]/4 + \
                    image[high_ii, low_jj]/4 + image[high_ii, high_jj]/4

            result[row, col] = res

    return result
Exemple #47
0
    def test_reference_3d(self):

        random.seed(42)
        im0 = random.random_integers(0, high=127,
                                     size=(25, 25, 3)).astype('uint16')
        im1 = random.random_integers(0, high=127,
                                     size=(25, 25, 3)).astype('uint16')
        imgIn = ImagesLoader(self.sc).fromArrays([im0, im1])
        ref = Register.reference(imgIn)
        assert (allclose(ref, (im0 + im1) / 2))
Exemple #48
0
def mutate_reorder(ind):
    "reorder rectangles "
    mutated = ind.copy()
    for i in xrange(3):
        row1 = nprand.random_integers(0, ind.data.shape[0] - 1)
        row2 = nprand.random_integers(0, ind.data.shape[0] - 1)
        mutated.data[row1], mutated.data[row2] =\
                mutated.data[row2].copy(), mutated.data[row1].copy()
    mutated.cost = -1
    return mutated
def random_pick_attr_splits(node, config, attr_index):
    samples = node.samples
    ind_middle = random.random_integers(0, len(samples)-2 )
    ind_a = random.random_integers(0, ind_middle )
    ind_b = random.random_integers(ind_middle+1, len(samples)-1 )
    # print "a:",ind_a, samples[ind_a][attr_index]
    # print "b:",ind_b, samples[ind_b][attr_index]

    spliter = (samples[ind_a][attr_index] + samples[ind_b][attr_index])*0.5
    return spliter
Exemple #50
0
 def resolve_desafio(self,x):
    if not self.arg_lim[0][0] <= x[0] <= self.arg_lim[0][1]:
      x[0] = random_integers(self.arg_lim[0][0],self.arg_lim[0][1])
    if not self.arg_lim[1][0] <= x[1] <= self.arg_lim[1][1]:
      x[1] = self.arg_lim[1][0]+ (self.arg_lim[1][1] - self.arg_lim[1][0])*rand()
    if not self.arg_lim[2][0] <= x[2] <= self.arg_lim[2][1]:
      x[2] = self.arg_lim[2][0]+ (self.arg_lim[2][1] - self.arg_lim[2][0])*rand()
    if not self.arg_lim[3][0] <= x[3] <= self.arg_lim[3][1]:
      x[0] = random_integers(self.arg_lim[3][0],self.arg_lim[3][1])
    return (self.fc(x),x)
Exemple #51
0
def mutate_reorder(ind):
    "reorder rectangles "
    mutated = ind.copy()
    for i in xrange(3):
        row1 = nprand.random_integers(0, ind.data.shape[0] - 1)
        row2 = nprand.random_integers(0, ind.data.shape[0] - 1)
        mutated.data[row1], mutated.data[row2] =\
                mutated.data[row2].copy(), mutated.data[row1].copy()
    mutated.cost = -1
    return mutated
 def new_config(self,batch):
     """
     Randomly generate a new configuration
     """
     x_index,y_index=random.random_integers(0,self.Nx-1,size=batch),random.random_integers(0,self.Ny-1,size=batch)
     x=(2 * random.rand(batch))
     thetaprime,phiprime=np.arccos(1-x),self.delta_phi*(2*random.rand(batch))
     layer=np.round(np.random.rand(batch))
     # thetaprime,phiprime=2*random.rand(batch),self.delta_phi*(2*random.rand(batch)-1)
     return np.vstack([x_index,y_index]).transpose(), np.vstack([thetaprime,phiprime]).transpose(),layer.transpose()
Exemple #53
0
def add_noise(imagename,changed_pixels=10000):
	# Read the image file imagename.
	# Multiply by 1. / 255 to change the values so that they are floating point
	# numbers ranging from 0 to 1.
	IM = imread(imagename, flatten=True) * (1. / 255)
	IM_x, IM_y = IM.shape
	
	for lost in xrange(changed_pixels):
		x_,y_ = random_integers(1,IM_x-2), random_integers(1,IM_y-2)
		val =  .25*randn() + .5
		IM[x_,y_] = max( min(val+ IM[x_,y_],1.), 0.)
Exemple #54
0
def randomPix(im):
    """Switch the value of two random pixels"""
    x1 = random.random_integers(0, im.size[0] - 1)
    x2 = random.random_integers(0, im.size[0] - 1)
    y1 = random.random_integers(0, im.size[1] - 1)
    y2 = random.random_integers(0, im.size[1] - 1)

    aux = im.getpixel((x1, y1))
    im.putpixel((x1, y1), im.getpixel((x2, y2)))
    im.putpixel((x2, y2), aux)
    return 1
Exemple #55
0
def seq1(L):
    a = random_integers(20)
    # d = random_integers(15) * (-1+np.random.binomial(1,0.5)*2)
    
    d = random_integers(15)
    seq = [str(a)]
    
    for i in range(1, L):
        seq.append(str(a + i * d))
        
    return seq
Exemple #56
0
 def avalia_aptidao(self,x):
    if not self.arg_lim[0][0] <= x[0] <= self.arg_lim[0][1]:
      x[0] = random_integers(self.arg_lim[0][0],self.arg_lim[0][1])
    if not self.arg_lim[1][0] <= x[1] <= self.arg_lim[1][1]:
      x[1] = self.arg_lim[1][0]+ (self.arg_lim[1][1] - self.arg_lim[1][0])*rand()
    if not self.arg_lim[2][0] <= x[2] <= self.arg_lim[2][1]:
      x[2] = self.arg_lim[2][0]+ (self.arg_lim[2][1] - self.arg_lim[2][0])*rand()
    if not self.arg_lim[3][0] <= x[3] <= self.arg_lim[3][1]:
      x[3] = random_integers(self.arg_lim[3][0],self.arg_lim[3][1])
   
    return (self.fitness_func(x),x)
Exemple #57
0
def SaltAndPepper(src, percetage):
    NoiseImg = src
    NoiseNum = int(percetage * src.shape[0] * src.shape[1])
    for i in range(NoiseNum):
        randX = random.random_integers(0, src.shape[0] - 1)
        randY = random.random_integers(0, src.shape[1] - 1)
        if random.random_integers(0, 1) == 0:
            NoiseImg[randX, randY] = 0
        else:
            NoiseImg[randX, randY] = 255
    return NoiseImg
def getcartaddinfo(inventoryInfo,k = 500):
    cartList = []
    n = len(set(inventoryInfo["beerId"]))
    #get quantities for each beers when added to cart
    for i in range(k):
        cartDict = {}
        userId = random.random_integers(0,k)
        beerId = inventoryInfo.loc[random.random_integers(0,n),"beerId"]
        quantity = [1,2,3,4,5,6][random.choice(6,1,p=[0.7,0.2,0.01,0.02,0.01,0.06])]
        cartDict["userId"] = userId
        cartDict["beerId"] = beerId
        cartDict["quantity"] = quantity
        cartList.append(cartDict)
    return(DataFrame(cartList))
def getcheckoutinfo(inventoryInfo,k = 100):
    checkoutList = []
    n = len(set(inventoryInfo["beerId"]))
    #get checkout quantities for each beers
    for i in range(k):
        checkoutDict = {}
        userId = random.random_integers(0,k)
        beerId = inventoryInfo.loc[random.random_integers(0,n),"beerId"]
        quantity = [1,2,3,4,5,6][random.choice(6,1,p=[0.5,0.3,0.02,0.04,0.04,0.1])]
        checkoutDict["userId"] = userId
        checkoutDict["beerId"] = beerId
        checkoutDict["quantity"] = quantity
        checkoutList.append(checkoutDict)
    return(DataFrame(checkoutList))