Exemple #1
0
    def test_pairs_to_array(self):
        """pairs_to_array should match hand-calculated results"""
        p2a = pairs_to_array
        p1 = [0, 1, 0.5]
        p2 = [2, 3, 0.9]
        p3 = [1, 2, 0.6]
        pairs = [p1, p2, p3]
        self.assertEqual(p2a(pairs), \
            array([[0,.5,0,0],[0,0,.6,0],[0,0,0,.9],[0,0,0,0]]))
        #try it without weights -- should assign 1
        new_pairs = [[0,1],[2,3],[1,2]]
        self.assertEqual(p2a(new_pairs), \
            array([[0,1,0,0],[0,0,1,0],[0,0,0,1],[0,0,0,0]]))
        #try it with explicit array size
        self.assertEqual(p2a(pairs, 5), \
            array([[0,.5,0,0,0],[0,0,.6,0,0],[0,0,0,.9,0],[0,0,0,0,0],\
            [0,0,0,0,0]]))
        #try it when we want to map the indices into gapped coords
        #we're effectively doing ABCD -> -A--BC-D-
        transform = array([1,4,5,7])
        result = p2a(pairs, transform=transform)
        self.assertEqual(result.shape, (8,8))
        exp = zeros((8,8), Float64)
        exp[1,4] = 0.5
        exp[4,5] = 0.6
        exp[5,7] = 0.9
        self.assertEqual(result, exp)

        result = p2a(pairs, num_items=9, transform=transform)
        self.assertEqual(result.shape, (9,9))
        exp = zeros((9,9), Float64)
        exp[1,4] = 0.5
        exp[4,5] = 0.6
        exp[5,7] = 0.9
        self.assertEqual(result, exp)
Exemple #2
0
def pacbporflist2similarityarray(pacbps, queryorsbjct, length):
    """ """
    bea = zeros(length)
    for pacbporf in pacbps:
        spos = pacbporf._get_original_alignment_pos_start()
        epos = pacbporf._get_original_alignment_pos_end()
        q, m, s = pacbporf.get_unextended_aligned_protein_sequences()
        if queryorsbjct == 'query':
            start = spos.query_pos
            end = epos.query_pos + 1
            seqa = list(q)
            ma = list(m)
        else:
            start = spos.sbjct_pos
            end = epos.sbjct_pos + 1
            seqa = list(s)
            ma = list(m)

        for pos in range(len(seqa) - 1, -1, -1):
            if seqa[pos] == '-':
                seqa.pop(pos)
                ma.pop(pos)
        # prepare replacement of match string into match score list
        matcharray = zeros(end - start)
        for pos in range(0, len(ma)):
            symbol = ma[pos]
            if symbol != ' ':
                matcharray[pos] = 1
        # update (binary) array
        bea[spos.query_pos:epos.query_pos + 1] += matcharray

    # correct bea for values > 1
    bea = where(greater_equal(bea, 2), 1, bea)
    return bea
Exemple #3
0
    def analyze(self,observations):
        """use Viterbi algorithm to
        find the states corresponding to the observations"""
        B = self.B
        A = self.A
        T = len(observations)
        N = self.N
        Omega_X = self.omega_X
        obs = self._getObservationIndices(observations)
        # initialisation
        delta = []
        delta.append(B[obs[0]] * self.pi) # (32a)
        phi = [array([0]*N)]              # (32b)
        # recursion
        for O_t in obs[1:]:
            delta_t = zeros(N,Float)
            phi_t = zeros(N)
            for j in range(N):
                delta_t[j] = max(delta[-1]*A[:,j]*B[O_t][j]) # (33a)
                phi_t[j] = argmax(delta[-1]*A[:,j])          # (33b)
            delta.append(delta_t)
            phi.append(phi_t)

        # reconstruction
        i_star = [argmax(delta[-1])]                         # (34b)
        phi.reverse() # we start from the end
        for phi_t in phi[:-1]:
            i_star.append(phi_t[i_star[-1]])                 # (35)
        trajectory = [Omega_X[i] for i in i_star]
        trajectory.reverse() # put time back in the right direction
        return trajectory
Exemple #4
0
    def test_vanishing_moments(self):
        """Test that coefficients in lp satisfy the
           vanishing moments condition
        """ 

        from daubfilt import daubfilt, number_of_filters

        for i in range(number_of_filters):
            D = 2*(i+1)

            P = D/2  # Number of vanishing moments
            N = P-1  # Dimension of nullspace of the matrix A
            R = P+1  # Rank of A, R = D-N = P+1 equations
        
            lp, hp = daubfilt(D)


            # Condition number of A grows with P, so we test only
            # the first 6 (and eps is slightly larger than machine precision)

            A    = zeros((R,D), Float)  # D unknowns, D-N equations
            b    = zeros((R,1), Float)  # Right hand side
            b[0] = sqrt(2)                
  
            A[0,:] = ones(D, Float)   # Coefficients must sum to sqrt(2)
            for p in range(min(P,6)): # the p'th vanishing moment (Cond Ap)
                for k in range(D):            
                    m=D-k;
                    A[p+1,k] = (-1)**m * k**p;

            assert allclose(b, mvmul(A,lp))         
    def __init__(self, **params):
        from plastk.rand import uniform
        from Numeric import zeros
        super(GNG, self).__init__(**params)

        N = self.initial_num_units

        self.weights = uniform(self.rmin, self.rmax, (N, self.dim))
        self.dists = zeros((N, 1)) * 0.0
        self.error = zeros((N, 1)) * 0.0

        self.connections = [{} for i in range(N)]

        self.last_input = zeros(self.dim)

        self.count = 0

        if self.initial_connections_per_unit > 0:
            for w in self.weights:
                self.present_input(w)
                ww = self.winners(self.initial_connections_per_unit + 1)
                i = ww[0]
                for j in ww[1:]:
                    self.add_connection(i, j)

        self.nopickle += ['_activation_fn']
        self.unpickle()
def analyse_transitions(T):
    from Numeric import zeros
    states, actions, results = T.shape

    entropies = zeros((states, actions)) * 0.0
    counts = zeros((states, actions)) * 0.0
    max_prob = zeros((states, actions)) * 0.0
    max_act = zeros((states, actions)) * 0.0

    for s in range(states):
        for a in range(actions):
            entropies[s, a] = entropy(normalize_sum(T[s, a]))
            counts[s, a] = sum(T[s, a])
            max_prob[s, a] = max(normalize_sum(T[s, a]))
            max_act[s, a] = argmax(normalize_sum(T[s, a]))

    print '       : ',
    for c in 'FBLR':
        print '%10s' % c,
    print
    print '-------------------------------------------------------------'

    for r in range(states):
        print '%6d' % r, ': ',
        for c in range(actions):
            print '%6.2f (%2d)' % (max_prob[r, c], max_act[r, c]),
        print
Exemple #7
0
    def __init__(self,**params):
        from plastk.rand import uniform
        from Numeric import zeros
        super(GNG,self).__init__(**params)

        N = self.initial_num_units
        
        self.weights = uniform(self.rmin,self.rmax,(N,self.dim))
        self.dists = zeros((N,1)) * 0.0
        self.error = zeros((N,1)) * 0.0

        self.connections = [{} for i in range(N)]
        
        self.last_input = zeros(self.dim)
        
        self.count = 0

        if self.initial_connections_per_unit > 0:
            for w in self.weights:
                self.present_input(w)
                ww = self.winners(self.initial_connections_per_unit+1)
                i = ww[0]
                for j in ww[1:]:
                    self.add_connection(i,j)

        self.nopickle += ['_activation_fn']
        self.unpickle()
Exemple #8
0
def pacbporflist2similarityarray(pacbps,queryorsbjct,length):
    """ """
    bea = zeros(length)
    for pacbporf in pacbps:
        spos  = pacbporf._get_original_alignment_pos_start()
        epos  = pacbporf._get_original_alignment_pos_end()
        q,m,s = pacbporf.get_unextended_aligned_protein_sequences()
        if queryorsbjct == 'query':
            start = spos.query_pos
            end   = epos.query_pos + 1
            seqa  = list(q)
            ma    = list(m)
        else:
            start = spos.sbjct_pos
            end   = epos.sbjct_pos + 1
            seqa  = list(s)
            ma    = list(m)

        for pos in range(len(seqa)-1,-1,-1):
            if seqa[pos] == '-':
                seqa.pop(pos)
                ma.pop(pos)
        # prepare replacement of match string into match score list
        matcharray = zeros(end-start)
        for pos in range(0,len(ma)):
            symbol = ma[pos]
            if symbol != ' ':
                matcharray[pos] = 1
        # update (binary) array
        bea[spos.query_pos:epos.query_pos+1] += matcharray

    # correct bea for values > 1
    bea = where(greater_equal(bea, 2), 1, bea)
    return bea
    def __init__(self, win):
        self.coords = zeros([30, 3], Float)
        self.prev_coords = zeros([3, 3], Float)

        self.peptide_mol = None
        self.length = 0
        self.prev_psi = 0

        PeptideGeneratorPropertyManager.__init__(self)
        GeneratorBaseClass.__init__(self, win)
    def __init__(self, win):
        self.coords = zeros([30,3], Float)
        self.prev_coords = zeros([3,3], Float)

        self.peptide_mol = None
        self.length = 0
        self.prev_psi = 0

        PeptideGeneratorPropertyManager.__init__(self)
        GeneratorBaseClass.__init__(self, win)
    def accept(self):

        m = self.getPickledMesh()

        # Create some Numeric arrays for the mesh (much faster)
        triangleNumArray = m.getTriangulation()
        verticeNumArray = m.getMeshVertices()
        #QMessageBox.information(None, "DEBUG", 'Tri 0 has verts ' + str(self.triangleNumArray[0]) )
        #QMessageBox.information(None, "DEBUG", 'Vert 942 is at ' + str(self.verticeNumArray[942]) )
        triCnt = len(triangleNumArray)
        verCnt = len(verticeNumArray)
        triangleNumArray = array(triangleNumArray, Numeric.Int32)
        #verticeNumArray = array( verticeNumArray, Numeric.Int32 )

        # Blank list of neighbors
        neighbors = zeros((triCnt, 3), Numeric.Int32)
        linkedNeighborCnt = zeros(triCnt, Numeric.Int32)

        self.progressBar.setMinimum(0)
        self.progressBar.setMaximum(triCnt)

        startTime = time.time()
        for T in xrange(triCnt):
            self.progressBar.setValue(T)
            if linkedNeighborCnt[T] < 3:
                me = triangleNumArray[T]
                for t in xrange(triCnt):
                    if linkedNeighborCnt[t] < 3 and t != T:
                        them = triangleNumArray[t]
                        commonVerts = 0
                        for i in xrange(3):
                            if them[i] == me[0] or them[i] == me[1] or them[
                                    i] == me[2]:
                                commonVerts += 1
                        if commonVerts == 2:
                            add = True
                            for i in xrange(linkedNeighborCnt[T]):
                                if neighbors[T][i] == t:
                                    add = False
                            if add:
                                neighbors[T][linkedNeighborCnt[T]] = t
                                linkedNeighborCnt[T] += 1
                                """except:
                  QMessageBox.information(None, 'DEBUG', 'T is ' + str(T) )
                  QMessageBox.information(None, 'DEBUG', 'its neighbors are ' + str(neighbors[T]) )
                  QMessageBox.information(None, 'DEBUG', 'we tried to add ' + str(t) )"""
                                neighbors[t][linkedNeighborCnt[t]] = T
                                linkedNeighborCnt[t] += 1

        QMessageBox.information(
            None, 'DEBUG',
            'Done in ' + str(time.time() - startTime) + ' seconds')
        """badTriAreaCnt = 0
 def accept(self):
   
   m = self.getPickledMesh()
   
   # Create some Numeric arrays for the mesh (much faster)
   triangleNumArray = m.getTriangulation()
   verticeNumArray = m.getMeshVertices()
   #QMessageBox.information(None, "DEBUG", 'Tri 0 has verts ' + str(self.triangleNumArray[0]) )
   #QMessageBox.information(None, "DEBUG", 'Vert 942 is at ' + str(self.verticeNumArray[942]) )
   triCnt = len(triangleNumArray)
   verCnt = len(verticeNumArray)
   triangleNumArray = array( triangleNumArray, Numeric.Int32 )
   #verticeNumArray = array( verticeNumArray, Numeric.Int32 )
   
   # Blank list of neighbors
   neighbors         = zeros( (triCnt, 3), Numeric.Int32 )
   linkedNeighborCnt = zeros( triCnt, Numeric.Int32 )
   
   self.progressBar.setMinimum(0)
   self.progressBar.setMaximum(triCnt)
   
   startTime = time.time()
   for T in xrange(triCnt):
     self.progressBar.setValue(T)
     if linkedNeighborCnt[T] < 3:
       me = triangleNumArray[T]
       for t in xrange(triCnt):
         if linkedNeighborCnt[t] < 3 and t != T:
           them = triangleNumArray[t]
           commonVerts = 0
           for i in xrange(3):
             if them[i] == me[0] or them[i] == me[1] or them[i] == me[2]:
               commonVerts += 1
           if commonVerts == 2:
             add = True
             for i in xrange(linkedNeighborCnt[T]):
               if neighbors[T][i] == t:
                 add = False
             if add:
               neighbors[T][linkedNeighborCnt[T]] = t
               linkedNeighborCnt[T] += 1
               """except:
                 QMessageBox.information(None, 'DEBUG', 'T is ' + str(T) )
                 QMessageBox.information(None, 'DEBUG', 'its neighbors are ' + str(neighbors[T]) )
                 QMessageBox.information(None, 'DEBUG', 'we tried to add ' + str(t) )"""
               neighbors[t][linkedNeighborCnt[t]] = T
               linkedNeighborCnt[t] += 1
   
   QMessageBox.information(None, 'DEBUG', 'Done in ' + str(time.time() - startTime) + ' seconds' )
   
   """badTriAreaCnt = 0
Exemple #13
0
    def __init__(self, shp, ptlist, origin, selSense, **opts):
        """
        ptlist is a list of 3d points describing a selection.
        origin is the center of view, and normal gives the direction
        of the line of light. Form a structure for telling whether
        arbitrary points fall inside the curve from the point of view.
        """
        # bruce 041214 rewrote some of this method
        simple_shape_2d.__init__(self, shp, ptlist, origin, selSense, opts)

        # bounding rectangle, in integers (scaled 8 to the angstrom)
        ibbhi = array(map(int, ceil(8 * self.bboxhi) + 2))
        ibblo = array(map(int, floor(8 * self.bboxlo) - 2))
        bboxlo = self.bboxlo

        # draw the curve in these matrices and fill it
        # [bruce 041214 adds this comment: this might be correct but it's very
        # inefficient -- we should do it geometrically someday. #e]
        mat = zeros(ibbhi - ibblo)
        mat1 = zeros(ibbhi - ibblo)
        mat1[0, :] = 1
        mat1[-1, :] = 1
        mat1[:, 0] = 1
        mat1[:, -1] = 1
        pt2d = self.pt2d
        pt0 = pt2d[0]
        for pt in pt2d[1:]:
            l = ceil(vlen(pt - pt0) * 8)
            if l < 0.01:
                continue
            v = (pt - pt0) / l
            for i in range(1 + int(l)):
                ij = 2 + array(map(int, floor((pt0 + v * i - bboxlo) * 8)))
                mat[ij] = 1
            pt0 = pt
        mat1 += mat

        fill(mat1, array([1, 1]), 1)
        mat1 -= mat  # Which means boundary line is counted as inside the shape.
        # boolean raster of filled-in shape
        self.matrix = mat1  ## For any element inside the matrix, if it is 0, then it's inside.
        # where matrix[0, 0] is in x, y space
        self.matbase = ibblo

        # axes of the plane; only used for debugging
        self.x = self.right
        self.y = self.up
        self.z = self.normal
 def absoluteProfile(alignment,char_order):
     f = a.columnFrequencies()
     res = zeros([len(f),len(char_order)])
     for row, freq in enumerate(f):
         for i in freq:
             res[row, i] = freq[i]
     return res
Exemple #15
0
def getSegyTraceHeader(SH,THN='cdp',data='none'):
        """
        getSegyTraceHeader(SH,TraceHeaderName)
        """

        bps=getBytePerSample(SH)

        if (data=='none'):
                data = open(SH["filename"]).read()
                

        # MAKE SOME LOOKUP TABLE THAT HOLDS THE LOCATION OF HEADERS
#       THpos=TraceHeaderPos[THN]
        THpos=STH_def[THN]["pos"]
        THformat=STH_def[THN]["type"]
        ntraces=SH["ntraces"]
        thv = zeros(ntraces)
        for itrace in range(1,ntraces+1,1):
                i=itrace

                pos=THpos+3600+(SH["ns"]*bps+240)*(itrace-1);

                txt="getSegyTraceHeader : Reading trace header " + THN + " " + str(itrace)  + " of " + str(ntraces) + " " +str(pos)

                printverbose(txt,20);
                thv[itrace-1],index = getValue(data,pos,THformat,endian,1)
                txt="getSegyTraceHeader : " + THN + "=" + str(thv[itrace-1])
                printverbose(txt,30);
        
Exemple #16
0
def getSegyTraceHeader(SH,THN='cdp',data='none'):
	"""
	getSegyTraceHeader(SH,TraceHeaderName)
	"""

	bps=getBytePerSample(SH)

	if (data=='none'):
		data = open(SH["filename"]).read()
		

	# MAKE SOME LOOKUP TABLE THAT HOLDS THE LOCATION OF HEADERS
#	THpos=TraceHeaderPos[THN]
	THpos=STH_def[THN]["pos"]
	THformat=STH_def[THN]["type"]
	ntraces=SH["ntraces"]
	thv = zeros(ntraces)
	for itrace in range(1,ntraces+1,1):
		i=itrace

		pos=THpos+3600+(SH["ns"]*bps+240)*(itrace-1);

		txt="getSegyTraceHeader : Reading trace header " + THN + " " + str(itrace)  + " of " + str(ntraces) + " " +str(pos)

		printverbose(txt,20);
		thv[itrace-1],index = getValue(data,pos,THformat,endian,1)
		txt="getSegyTraceHeader : " + THN + "=" + str(thv[itrace-1])
		printverbose(txt,30);
	
	return thv
Exemple #17
0
def pos_char_weights(alignment, order=DNA_ORDER):
    """Returns the contribution of each character at each position.

    alignment: Alignemnt object
    order: the order of characters in the profile (all observed chars
        in the alignment
    
    This function is used by the function position_based
    
    For example: 
    GYVGS
    GFDGF
    GYDGF
    GYQGG
    
        0       1       2       3       4       5   
    G   1/1*4                           1/1*4   1/3*1
    Y           1/2*3
    F           1/2*1                           1/3*2
    V                   1/3*1
    D                   1/3*2
    Q                   1/3*1
    S                                           1/3*1
    """
    counts = alignment.columnFrequencies()
    a = zeros([len(order), alignment.SeqLen],Float64)
    for col, c in enumerate(counts):
        for char in c:
            a[order.index(char),col] = 1/(len(c)*c[char])
    return Profile(a,Alphabet=order)
 def __call__(self, input, error=False):
     X = self.scale_input(input)
     if not error:
         Y = dot(self.w, X)
     else:
         Y = dot(self.w, X), zeros(self.num_outputs)
     return self.scale_output(Y)
def inertia_eigenvectors(basepos, already_centered=False):
    """
    Given basepos (an array of positions),
    compute and return (as a 2-tuple) the lists of eigenvalues and
    eigenvectors of the inertia tensor (computed as if all points had the same
    mass). These lists are always length 3, even for len(basepos) of 0,1, or 2,
    overlapping or colinear points, etc, though some evals will be 0 in these cases.
       Optional small speedup: if caller knows basepos is centered at the origin, it can say so.
    """
    #bruce 060119 split this out of shakedown_poly_evals_evecs_axis() in chunk.py
    basepos = A(basepos)  # make sure it's a Numeric array
    if not already_centered and len(basepos):
        center = add.reduce(basepos) / len(basepos)
        basepos = basepos - center
    # compute inertia tensor
    tensor = zeros((3, 3), Float)
    for p in basepos:
        rsq = dot(p, p)
        m = -multiply.outer(p, p)
        m[0, 0] += rsq
        m[1, 1] += rsq
        m[2, 2] += rsq
        tensor += m
    evals, evecs = eigenvectors(tensor)
    assert len(evals) == len(evecs) == 3
    return evals, evecs
Exemple #20
0
def writemb(index, data, dxsize, dysize, bands, mb_db):
    """
    Write raster 'data' (of the size 'dataxsize' x 'dataysize') read from
    'dataset' into the mbtiles document 'mb_db' with size 'tilesize' pixels.
    Later this should be replaced by new <TMS Tile Raster Driver> from GDAL.
    """
    if bands == 3 and tileformat == 'png':
        tmp = tempdriver.Create('', tilesize, tilesize, bands=4)
        alpha = tmp.GetRasterBand(4)
        #from Numeric import zeros
        alphaarray = (zeros((dysize, dxsize)) + 255).astype('b')
        alpha.WriteArray(alphaarray, 0, tilesize - dysize)
    else:
        tmp = tempdriver.Create('', tilesize, tilesize, bands=bands)

    tmp.WriteRaster(0,
                    tilesize - dysize,
                    dxsize,
                    dysize,
                    data,
                    band_list=range(1, bands + 1))
    tiledriver.CreateCopy('tmp.png', tmp, strict=0)
    query = """insert into tiles 
        (zoom_level, tile_column, tile_row, tile_data) 
        values (%d, %d, %d, ?)""" % (index[0], index[1], index[2])
    cur = mb_db.cursor()
    d = open('tmp.png', 'rb').read()
    cur.execute(query, (sqlite3.Binary(d), ))
    mb_db.commit()
    cur.close()
    return 0
Exemple #21
0
def writemb(index, data, dxsize, dysize, bands, mb_db):
    """
    Write raster 'data' (of the size 'dataxsize' x 'dataysize') read from
    'dataset' into the mbtiles document 'mb_db' with size 'tilesize' pixels.
    Later this should be replaced by new <TMS Tile Raster Driver> from GDAL.
    """
    if bands == 3 and tileformat == 'png':
        tmp = tempdriver.Create('', tilesize, tilesize, bands=4)
        alpha = tmp.GetRasterBand(4)
        #from Numeric import zeros
        alphaarray = (zeros((dysize, dxsize)) + 255).astype('b')
        alpha.WriteArray( alphaarray, 0, tilesize-dysize )
    else:
        tmp = tempdriver.Create('', tilesize, tilesize, bands=bands)

    tmp.WriteRaster( 0, tilesize-dysize, dxsize, dysize, data, band_list=range(1, bands+1))
    tiledriver.CreateCopy('tmp.png', tmp, strict=0)
    query = """insert into tiles 
        (zoom_level, tile_column, tile_row, tile_data) 
        values (%d, %d, %d, ?)""" % (index[0], index[1], index[2])
    cur = mb_db.cursor()
    d = open('tmp.png', 'rb').read()
    cur.execute(query, (sqlite3.Binary(d),))
    mb_db.commit()
    cur.close()
    return 0
 def __call__(self,input,error=False):
     X = self.scale_input(input)
     if not error:
         Y = dot(self.w,X)
     else:
         Y = dot(self.w,X), zeros(self.num_outputs)
     return self.scale_output(Y)
Exemple #23
0
def fitsin(x,y,tau=1,delta=0,A=1,C=1,sig=1):
    """Fit data in x and y to a sin function"""
    data=zeros((len(x),3),typecode='d')
    data[:,2]=sig
    data[:,0]=x
    data[:,1]=y
    return LS(sinus,(tau,delta,A,C),data)
def inertia_eigenvectors(basepos, already_centered = False):
    """
    Given basepos (an array of positions),
    compute and return (as a 2-tuple) the lists of eigenvalues and
    eigenvectors of the inertia tensor (computed as if all points had the same
    mass). These lists are always length 3, even for len(basepos) of 0,1, or 2,
    overlapping or colinear points, etc, though some evals will be 0 in these cases.
       Optional small speedup: if caller knows basepos is centered at the origin, it can say so.
    """
    #bruce 060119 split this out of shakedown_poly_evals_evecs_axis() in chunk.py
    basepos = A(basepos) # make sure it's a Numeric array
    if not already_centered and len(basepos):
        center = add.reduce(basepos)/len(basepos)
        basepos = basepos - center
    # compute inertia tensor
    tensor = zeros((3,3),Float)
    for p in basepos:
        rsq = dot(p, p)
        m= - multiply.outer(p, p)
        m[0,0] += rsq
        m[1,1] += rsq
        m[2,2] += rsq
        tensor += m
    evals, evecs = eigenvectors(tensor)
    assert len(evals) == len(evecs) == 3
    return evals, evecs
Exemple #25
0
    def epsilon_greedy(self,sensation,applicable_actions):
        """
        Given self.epsilon() and self.Q(), return a distribution over
        applicable_actions as an array where each element contains the
        a probability mass for the corresponding action.  I.e.  The
        action with the highest Q gets p = self.epsilon() and the
        others get the remainder of the mass, uniformly distributed.
        """
        Q = array([self.Q(sensation,action) for action in applicable_actions])

        # simple epsilon-greedy policy
        # get a vector with a 1 where each max element is, zero elsewhere
        mask = (Q == mmax(Q))

        num_maxes = len(nonzero(mask))
        num_others = len(mask) - num_maxes

        if num_others == 0: return mask
        
        e0 = self.epsilon()/num_maxes
        e1 = self.epsilon()/num_others

        result = zeros(len(mask))+0.0
        putmask(result,mask,1-e0)
        putmask(result,mask==0,e1)
        return result
def FindValueInterpolate(array, value, axis, delta=1.0e-10):
    """Returns an with the position of value in array using linear interpolation
	This method can be used for finding the position of value along a given
	axis in 'array' . The values are found by using linear interpolation
	between neighboring points in the array.
	"""
    from Numeric import zeros, Float
    shape = array.shape
    otheraxes = range(3)
    del otheraxes[axis]
    contoursurface = zeros((shape[otheraxes[0]], shape[otheraxes[1]]), Float)
    for axis0 in range(shape[otheraxes[0]]):
        for axis1 in range(shape[otheraxes[1]]):
            slice = [None, None, None]
            slice[otheraxes[0]] = axis0
            slice[otheraxes[1]] = axis1
            arrayslice = SliceArray(array, slice).flat
            #for normalaxis in range(interval[1],interval-1,-1):
            # Counting down from above.
            # 2 is subtracted since the surface cannot be
            # outside the searchinterval
            for normalaxis in range(shape[axis] - 2, -1, -1):
                rel_deviation = (value - arrayslice[normalaxis]) / (
                    arrayslice[normalaxis + 1] - arrayslice[normalaxis] +
                    delta)
                if (rel_deviation >= 0.0 and rel_deviation < 1.0):
                    contoursurface[axis0, axis1] = normalaxis + rel_deviation
                    break
    return contoursurface
    def epsilon_greedy(self, sensation, applicable_actions):
        """
        Given self.epsilon() and self.Q(), return a distribution over
        applicable_actions as an array where each element contains the
        a probability mass for the corresponding action.  I.e.  The
        action with the highest Q gets p = self.epsilon() and the
        others get the remainder of the mass, uniformly distributed.
        """
        Q = array([self.Q(sensation, action) for action in applicable_actions])

        # simple epsilon-greedy policy
        # get a vector with a 1 where each max element is, zero elsewhere
        mask = (Q == mmax(Q))

        num_maxes = len(nonzero(mask))
        num_others = len(mask) - num_maxes

        if num_others == 0: return mask

        e0 = self.epsilon() / num_maxes
        e1 = self.epsilon() / num_others

        result = zeros(len(mask)) + 0.0
        putmask(result, mask, 1 - e0)
        putmask(result, mask == 0, e1)
        return result
 def insert_n_rows(self, i, n=1):
     " Insert `n` rows into each column at row `i`. "
     rows = list()
     for tc in self.typecodes:
         rows.append( zeros((n,),typecode=tc) )
     self.insert_rows(i, rows)
     self.update_rows()
Exemple #29
0
    def setIds(self, id_fun=lambda x: x.Data.split("_")[-1]):
        """
        Sets "LeafLabel", "LeafCts", and "ContainsAll" attributes

        id_fun: function that takes node and generate a unique id (label)
            for each node. By default will create a label consisting of 
            the string to the right of the last underscore in the data
            attribute. E.g. if the node has data label of 1234_HSA, the
            function will return a unique lable of "HSA". the idea being
            that if your tree has multiple human (HSA) sequences, the
            result of the function will be multiple nodes w/the same
            label. 

        The LeafLabel attribute is the the result of the id_fun function.

        The LeafCts attribute is an array with counts of the leaves with the 
            same label.

        The ContainsAll attribute is True when it contains every instance 
            of the LeafLabels of its terminal descendants. E.g. the set
            of LeafLabels of its terminal descendants occur nowhere else
            in the tree. 

        This is used by the uniqueIds function to remove duplicate species
        from the tree but can be used for any label you choose.
        """
        labels =  [id_fun(x)  for x in self.TerminalDescendants]
        u_labels = list(set(labels))
        len_u_labels = len(u_labels)
        labels_dict =  dict(zip(u_labels, range(len_u_labels)))
        all_cts = zeros(len(u_labels))

        for label in labels: 
            all_cts[labels_dict[label]] += 1
      
        for n in self.traverse(self_before=False, self_after=True):
            if not n.Children:
                setattr(n, "LeafLabel", id_fun(n))
                setattr(n, "LeafCts", zeros(len_u_labels))
                n.LeafCts[labels_dict[n.LeafLabel]] = 1
            else:
                n.LeafCts = zeros(len_u_labels)
                for c in n.Children:
                    n.LeafCts += c.LeafCts 
            nzero = nonzero(n.LeafCts)
            total = sum(take(all_cts, nzero)- take(n.LeafCts, nzero))
            setattr(n, "ContainsAll", (total == 0))
Exemple #30
0
    def __init__(self, shp, ptlist, origin, selSense, **opts):
        """
        ptlist is a list of 3d points describing a selection.
        origin is the center of view, and normal gives the direction
        of the line of light. Form a structure for telling whether
        arbitrary points fall inside the curve from the point of view.
        """
        # bruce 041214 rewrote some of this method
        simple_shape_2d.__init__( self, shp, ptlist, origin, selSense, opts)
        
        # bounding rectangle, in integers (scaled 8 to the angstrom)
        ibbhi = array(map(int, ceil(8 * self.bboxhi)+2))
        ibblo = array(map(int, floor(8 * self.bboxlo)-2))
        bboxlo = self.bboxlo
        
        # draw the curve in these matrices and fill it
        # [bruce 041214 adds this comment: this might be correct but it's very
        # inefficient -- we should do it geometrically someday. #e]
        mat = zeros(ibbhi - ibblo)
        mat1 = zeros(ibbhi - ibblo)
        mat1[0,:] = 1
        mat1[-1,:] = 1
        mat1[:,0] = 1
        mat1[:,-1] = 1
        pt2d = self.pt2d
        pt0 = pt2d[0]
        for pt in pt2d[1:]:
            l = ceil(vlen(pt - pt0)*8)
            if l<0.01: continue
            v=(pt - pt0)/l
            for i in range(1 + int(l)):
                ij = 2 + array(map(int, floor((pt0 + v * i - bboxlo)*8)))
                mat[ij]=1
            pt0 = pt
        mat1 += mat
        
        fill(mat1, array([1, 1]),1)
        mat1 -= mat #Which means boundary line is counted as inside the shape.
        # boolean raster of filled-in shape
        self.matrix = mat1  ## For any element inside the matrix, if it is 0, then it's inside.
        # where matrix[0, 0] is in x, y space
        self.matbase = ibblo

        # axes of the plane; only used for debugging
        self.x = self.right
        self.y = self.up
        self.z = self.normal
 def __call__(self, sensation, reward=None):
     if not is_terminal(sensation):
         assert (type(sensation) == int)
         s = zeros(self.num_features)
         s[sensation] = 1.0
     else:
         s = sensation
     return super(LinearTabularTDAgent, self).__call__(s, reward)
Exemple #32
0
 def __call__(self,sensation,reward=None):
     if is_terminal(sensation):
         new_sensation = sensation
     else:
         new_sensation = zeros(self.num_features,'f')
         for f in sensation:
             new_sensation[f] = 1
     return super(LinearListAgent,self).__call__(new_sensation,reward)
def table_to_array(tbl, typecode='f'):
    shape = (tbl.ncols, tbl.nrows)
    
    a = zeros( (tbl.ncols, tbl.nrows), typecode)
    for j in range(tbl.ncols):
        a[j] = tbl[j].astype(typecode)

    return transpose(a)
 def __call__(self, sensation, reward=None):
     if is_terminal(sensation):
         new_sensation = sensation
     else:
         new_sensation = zeros(self.num_features, 'f')
         for f in sensation:
             new_sensation[f] = 1
     return super(LinearListAgent, self).__call__(new_sensation, reward)
Exemple #35
0
 def __call__(self,sensation,reward=None):
     if not is_terminal(sensation):
         assert(type(sensation) == int)
         s = zeros(self.num_features)
         s[sensation] = 1.0
     else:
         s = sensation
     return super(LinearTabularTDAgent,self).__call__(s,reward)
Exemple #36
0
def ortho(n,v,w=None):
    """ortho -  Test orthogonality condition of filter coefficients.

    s=ortho(n,v,w) 
    Computes s = \sum_i v(i)*w(i+2n)  
    
    s=ortho(n,v) 
    Computes s = \sum_i v(i)*v(i+2n)  


    Ex: ortho(1,v):   s = v(1)*v(3) + v(2)*v(4) + ...

        ortho(1,v,w): s = v(1)*w(3) + v(2)*w(4) + ... +
                          w(1)*v(3) + w(2)*v(4) + ...

    See also daubfilt, test_daubfilt

    Ole Moller Nielsen, NYU, 03/18/96.
    Translated from Matlab to Python by OMN, GA, May 2003
    """

    k = 2*n

    Dv = len(v);
    if w is None:
        Dw = Dv
        w = v
    else:    
        Dw = len(w)

    D = max(Dv,Dw)
    dif = abs(Dw-Dv)

    if dif > 0:
        #Pad smallest vector with zeros
        x = zeros(D, Float)
        if Dw > Dv:
            x[:Dv] = v
            v = x
            #v[Dv+1:Dw]
        else:
            print x
            print w
            print x[:Dw]
            x[:Dw] = w
            w = x
            #w[Dw+1:Dv]
  
    s = 0
    if Dw > 0:   #
        for i in range(D-k):
            s = s + v[i]*w[i+k] + w[i]*v[i+k];
        s = s/2;
    else:        
        for i in range(1, D-k+1):
            s = s + v[i]*v[i+k]

    return s
def getT(n):
    "Create an nxn KE Hamiltonian for the problem"
    T = zeros((n, n), Float)  # get a zero matrix
    delta = 1. / (n - 1)
    for i in range(n):
        T[i, i] = 1 / (delta * delta)
        if i: T[i - 1, i] = T[i, i - 1] = -0.5 / (delta * delta)
    #for i in range(n): print i,T[i,i]
    return T
Exemple #38
0
 def __init__(self, steppingParams):
     self.ndim = len(steppingParams)
     self.info = []
     self.shape = []
     for param in steppingParams:
         self.info.append( (param.name, param.slo, param.shi, \
                            param.steps, param.stype) )
         self.shape.append(param.steps)
     self.values = zeros(self.shape,Float)
Exemple #39
0
def lr(x,y,y0guess=0,gradguess=0,sig=1):
    """ (x,y) points deffine line to fit
    sig is sigma of each point, if doing chi^2 fitting.
    lr[0][0]-> y0, lr[0][1]->grad, lr[1]-> chi"""
    data=zeros((len(x),3),typecode='d')
    data[:,2]=sig
    data[:,0]=x
    data[:,1]=y
    return LS(line,(y0guess,gradguess),data)
Exemple #40
0
    def gaussian_activation(self):
        x = self.dists
        radii = zeros(self.dists.shape) * 0.0

        for u,conn_dict in enumerate(self.connections):
            neighbors = take(self.weights,conn_dict.keys())
            radii[u] = average(matrixnorm(neighbors-self.weights[u]))

        self.__activation = gaussian(x,radii/2)
Exemple #41
0
 def __init__(self, steppingParams):
     self.ndim = len(steppingParams)
     self.info = []
     self.shape = []
     for param in steppingParams:
         self.info.append( (param.name, param.slo, param.shi, \
                            param.steps, param.stype) )
         self.shape.append(param.steps)
     self.values = zeros(self.shape, Float)
    def gaussian_activation(self):
        x = self.dists
        radii = zeros(self.dists.shape) * 0.0

        for u, conn_dict in enumerate(self.connections):
            neighbors = take(self.weights, conn_dict.keys())
            radii[u] = average(matrixnorm(neighbors - self.weights[u]))

        self.__activation = gaussian(x, radii / 2)
Exemple #43
0
def nipals_c(X, PCs, threshold, E_matrices):
    """
    
    @param X: 2-dimensional matrix of number data. 
    @type X: Numeric array
    
    @param PCs: Number of Principal Components.
    @type PCs: int
    
    @param threshold: Convergence check value. For checking on convergence to zero (e.g. 0.000001). 
    @type threshold: float
    
    @param E_matrices: If E-matrices should be retrieved or not. E-matrices (for each PC) or explained_var (explained variance for each PC).
    @type E_matrices: bool
    
    @return: (Scores, Loadings, E)

    """

    if not import_ok:
        raise ImportError, "could not import c_nipals python extension"
    else:

        (rows, cols) = shape(X)

        maxPCs = min(rows,
                     cols)  # max number of PCs is min(objects, variables)
        if maxPCs < PCs: PCs = maxPCs  # change to maxPCs if PCs > maxPCs

        Scores = zeros((rows, PCs), Float)  # all Scores (T)
        Loadings = zeros((PCs, cols), Float)  # all Loadings (P)

        E = X.copy()  #E[0]  (should already be mean centered)

        if E_matrices:
            Error_matrices = zeros((PCs, rows, cols),
                                   Float)  # all Error matrices (E)
            c_nipals.nipals2(Scores, Loadings, E, Error_matrices, PCs,
                             threshold)
            return Scores, Loadings, Error_matrices
        else:
            explained_var = c_nipals.nipals(Scores, Loadings, E, PCs,
                                            threshold)
            return Scores, Loadings, explained_var
Exemple #44
0
    def test_SeqToProfile(self):
        """SequenceToProfile: should work with different parameter settings
        """
        seq = DnaSequence("ATCGRYN-")

        #Only non-degenerate bases in the char order, all other
        #characters are ignored. In a sequence this means that 
        #several positions will contain only zeros in the profile.
        exp = zeros([len(seq),4],Float64)
        for x,y in zip(range(len(seq)),[2,0,1,3]):
            exp[x,y] = 1
        self.assertEqual(SeqToProfile(seq,char_order="TCAG",\
            split_degenerates=False).Data.tolist(),exp.tolist()) 
       
        #Same thing should work as well when the char order is not passed in
        exp = zeros([len(seq),4],Float64)
        for x,y in zip(range(len(seq)),[2,0,1,3]):
            exp[x,y] = 1
        self.assertEqual(SeqToProfile(seq, split_degenerates=False)\
            .Data.tolist(),exp.tolist()) 

       
        #All symbols in the sequence are in the char order, no row
        #should contain only zeros. Degenerate symbols are not split.
        exp = zeros([len(seq),8],Float64)
        for x,y in zip(range(len(seq)),[2,0,1,3,4,5,6,7]):
            exp[x,y] = 1
        self.assertEqual(SeqToProfile(seq,char_order="TCAGRYN-",\
            split_degenerates=False).Data.tolist(), exp.tolist())
        
        #splitting all degenerate symbols, having only non-degenerate symbols
        #in the character order (and -)
        exp = array([[0,0,1,0,0],[1,0,0,0,0],[0,1,0,0,0],[0,0,0,1,0],
            [0,0,.5,.5,0],[.5,.5,0,0,0],[.25,.25,.25,.25,0],[0,0,0,0,1]])
        self.assertEqual(SeqToProfile(seq,char_order="TCAG-",\
            split_degenerates=True).Data.tolist(),exp.tolist())
        
        #splitting degenerates, but having one of the degenerate
        #symbols in the character order. In that case the degenerate symbol
        #is not split. 
        exp = array([[0,0,1,0,0,0],[1,0,0,0,0,0],[0,1,0,0,0,0],[0,0,0,1,0,0],
            [0,0,.5,.5,0,0],[.5,.5,0,0,0,0],[0,0,0,0,1,0],[0,0,0,0,0,1]])
        self.assertEqual(SeqToProfile(seq,char_order="TCAGN-",\
            split_degenerates=True).Data.tolist(),exp.tolist())
Exemple #45
0
def spline(x, y, yp1=None, ypn=None):
	"""y2 = spline(x_vals,y_vals, yp1=None, ypn=None) 
	returns the y2 table for the spline as needed by splint()"""

	n=len(x)
	u=zeros(n,Float)
	y2=zeros(n,Float)
	
	x=asarray(x, Float)
	y=asarray(y, Float)
	
	dx=x[1:]-x[:-1]
	dxi=1.0/dx
	dx2i=1.0/(x[2:]-x[:-2])
	dy=(y[1:]-y[:-1])
	siga=dx[:-1]*dx2i
	dydx=dy*dxi
	
	# u[i]=(y[i+1]-y[i])/float(x[i+1]-x[i]) - (y[i]-y[i-1])/float(x[i]-x[i-1])
	u[1:-1]=dydx[1:]-dydx[:-1] #this is an incomplete rendering of u... the rest requires recursion in the loop
	
	if yp1 is None:
		y2[0]=u[0]=0.0
	else:
		y2[0]= -0.5
		u[0]=(3.0*dxi[0])*(dy[0]*dxi[0] -yp1)

	for i in range(1,n-1):
		sig=siga[i-1]
		p=sig*y2[i-1]+2.0
		y2[i]=(sig-1.0)/p
		u[i]=(6.0*u[i]*dx2i[i-1] - sig*u[i-1])/p

	if ypn is None:
		qn=un=0.0
	else:
		qn= 0.5
		un=(3.0*dxi[-1])*(ypn- dy[-1]*dxi[-1] )
		
	y2[-1]=(un-qn*u[-2])/(qn*y2[-2]+1.0)
	for k in range(n-2,-1,-1):
		y2[k]=y2[k]*y2[k+1]+u[k]

	return y2
def median_filter(data, N=5):
    """
    Median filter sequence data with window of width N.
    """
    results = zeros(len(data - N))
    for i in xrange(N, len(data)):
        x = data[i - N:i]
        s = argsort(x)
        results[i] = x[s[(N / 2) + 1]]
    return results
Exemple #47
0
 def __init__(self, steppingParams, storedParams):
     self.ndim = len(steppingParams)
     self.info = []
     self.shape = []
     for param in steppingParams:
         self.info.append( (param.name, param.slo, param.shi, \
                            param.steps, param.stype) )
         self.shape.append(param.steps)
     self.values = zeros(self.shape,Float)
     raise NotImplementedError, 'Oops, not finished yet!'
Exemple #48
0
 def __init__(self, steppingParams, storedParams):
     self.ndim = len(steppingParams)
     self.info = []
     self.shape = []
     for param in steppingParams:
         self.info.append( (param.name, param.slo, param.shi, \
                            param.steps, param.stype) )
         self.shape.append(param.steps)
     self.values = zeros(self.shape, Float)
     raise NotImplementedError, 'Oops, not finished yet!'
Exemple #49
0
def pacbporflist2codingarray(pacbps, queryorsbjct, length):
    """ """
    bea = zeros(length)
    for pacbporf in pacbps:
        spos = pacbporf._get_original_alignment_pos_start()
        epos = pacbporf._get_original_alignment_pos_end()
        if queryorsbjct == 'query':
            bea[spos.query_pos:epos.query_pos + 1] = 1
        else:
            bea[spos.sbjct_pos:epos.sbjct_pos + 1] = 1
    return bea
Exemple #50
0
def mVOR(alignment,n=1000,order=DNA_ORDER):
    """Returns sequence weights according to the modified Voronoi method.
    
    alignment: Alignment object
    n: sample size (=number of random profiles to be generated)
    order: specifies the order of the characters found in the alignment,
        used to build the sequence and random profiles.
    
    mVOR is a modification of the VOR method. Instead of generating discrete
    random sequences, it generates random profiles, to sample more equally from
    the sequence space and to prevent random sequences to be equidistant to 
    multiple sequences in the alignment. 

    See the Implementation notes to see how the random profiles are generated
    and compared to the 'sequence profiles' from the alignment.

    Random generalized sequences (or a profile filled with random numbers):
    Sequences that are equidistant to multiple sequences in the alignment
    can form a problem in small datasets. For longer sequences the likelihood
    of this event is negligable. Generating 'random generalized sequences' is 
    a solution, because we're then sampling from continuous sequence space. 
    Each column of a random profile is generated by normalizing a set of 
    independent, exponentially distributed random numbers. In other words, a 
    random profile is a two-dimensional array (rows are chars in the alphabet, 
    columns are positions in the alignment) filled with a random numbers, 
    sampled from the standard exponential distribution (lambda=1, and thus 
    the mean=1), where each column is normalized to one. These random profiles 
    are compared to the special profiles of just one sequence (ones for the 
    single character observed at that position). The distance between the 
    two profiles is simply the Euclidean distance.

    """
    
    weights = zeros(len(alignment),Float64)

    #get seq profiles
    seq_profiles = {}
    for k,v in alignment.items():
        #seq_profiles[k] = ProfileFromSeq(v,order=order)
        seq_profiles[k] = SeqToProfile(v,alphabet=order)

    for count in range(n):
        #generate a random profile
        exp = exponential(1,[alignment.SeqLen,len(order)])
        r = Profile(Data=exp,Alphabet=order)
        r.normalizePositions()
        #append the distance between the random profile and the sequence
        #profile to temp
        temp = [seq_profiles[key].distance(r) for key in alignment.RowOrder]
        votes = row_to_vote(array(temp))
        weights += votes
    weight_dict = Weights(dict(zip(alignment.RowOrder,weights)))
    weight_dict.normalize()
    return weight_dict
def makeArray(shape, typecode):
    if typecode == 'D':
        res = zeros(shape, typecode)
        res.imag = (rand(*shape) - 0.5) * 100
        res.real = (rand(*shape) - 0.5) * 100
    # XXX cheat for the small integer types to avoid overflows and hassles
    elif typecode in '1bw':
        res = array((rand(2,2)) * 10)
    else:
        res = array((rand(*shape) - 0.5) * 100).astype(typecode)
    return res
Exemple #52
0
    def initsolver(self,
                   tol=1e-8,
                   hbsize=10000,
                   dt=0.1,
                   statescale=array([0.0])):
        '''
        Sets the tolerance, buffer size, initial step size and a scaling
        parameter to help deal with states close to 0.
        
        Arguments:
           
           tol                       = 0.000005,
           hbsize                    = 1000,
           OBSOLETE: datasize        = 1000,
           OBSOLETE: dout            = 0.01,
           dt                        = 1.0,
           OBSOLETE: mindtx          = 10.0**(-9),
           OBSOLETE: maxdtx          = 100,
           statescale                = array([0]).
        '''

        # some simple checks
        if (hbsize == None): hbsize = 10000
        if (hbsize <= 0): hbsize = 1  #if 0 or less, C code crashes
        if (dt == None): dt = 0.1

        # check that the state scale is of the appropriate length
        if (self.PROB != None):
            if (len(statescale) < self.PROB[0]):
                tempstsc = zeros((self.PROB[0]), 'd')
                for i in range(len(statescale)):
                    tempstsc[i] = statescale[i]
                statescale = tempstsc
            else:
                statescale = statescale[:self.PROB[0]]

        try:
            self.SOLVER = (
                float(tol),
                int(hbsize),
                float(dt),  # output and integration timesteps
                array(list(map(float, statescale))))
        except:
            print(
                "DDE Error: Something is wrong: perhaps one of the supplied variables has the wrong type?"
            )
            print("DDE Error: Solver initialisation failed!")
            return 0

        # all went well
        self._set[1] = 1
        return 1
Exemple #53
0
def PCG2codingarray(PCG, organism, aalength, omit_unigenes=True):
    """ """
    array_algpresence = zeros(aalength)
    for orgS in PCG.organism_set():
        if organism == orgS: continue
        pacbporfs = order_pacbporf_list(
            PCG.get_pacbps_by_organisms(organism, orgS))
        if pacbporfs and omit_unigenes and hasattr(pacbporfs[0].orfS,
                                                   ORF_IS_UNIGENE_LABEL):
            continue
        orgPresArray = pacbporflist2codingarray(pacbporfs, "query", aalength)
        array_algpresence += orgPresArray

    # return coding/presence array
    return array_algpresence
Exemple #54
0
def mean_center(X):
    """
    
    @param X: 2-dimensional matrix of number data 
    @type X: Numeric array
    
    
    @return: Mean centered X (always has same dimensions as X)
    
    """
    (rows, cols) = shape(X)
    new_X = zeros((rows, cols), Float)
    _averages = average(X, 0)
    #print _averages

    for row in range(rows):
        for col in range(cols):
            new_X[row, col] = X[row, col] - _averages[col]
    return new_X
Exemple #55
0
    def omsr_entropy_of_position(self,site,omsraacoord,window=15):
        """
        """
        # get the binary entropy array
        bea = zeros((window*2))
        try:    coord = site.pos / 3
        except: return 0.0
        omsrinbeacoord = max([ 0, window+(omsraacoord-coord) ])
        # set omsr area to 1 in bea array
        bea[omsrinbeacoord:window*2] = 1
        # calculate maximum entropy score
        max_entropy = float(sum(range(1,window+1)))

        # now calculate entropy of position around omsr
        score_left  = sum(range(1,sum(bea[0:window])+1))
        score_rigth = sum(range(1,sum(bea[window:window*2])+1))
        entropy     = score_rigth - score_left
        entropy     = float(entropy) / max_entropy
        # return entropy value
        return entropy
Exemple #56
0
def get2d_2():
    from math import pi
    from Numeric import arange, zeros
    from enthought.util.numerix import Float, zeros
    from sasModeling.file2array import readfile2array
    from sasModeling.pointsmodelpy import pointsmodelpy
    from sasModeling.geoshapespy import geoshapespy

    lm = pointsmodelpy.new_loresmodel(0.1)
    cyn = geoshapespy.new_cylinder(5, 20)
    geoshapespy.set_orientation(cyn, 0, 0, 90)
    pointsmodelpy.lores_add(lm, cyn, 1.0)

    vp = pointsmodelpy.new_point3dvec()
    pointsmodelpy.get_lorespoints(lm, vp)

    pointsmodelpy.distdistribution_xy(lm, vp)

    value_grid = zeros((100, 100), Float)
    width, height = value_grid.shape
    print(width, height)

    I = pointsmodelpy.calculateI_Qxy(lm, 0.00001, 0.000002)
    print(I)

    Imax = 0
    for i in range(width):
        for j in range(height):
            qx = float(i - 50) / 200.0
            qy = float(j - 50) / 200.0
            value_grid[i, j] = pointsmodelpy.calculateI_Qxy(lm, qx, qy)
            if value_grid[i][j] > Imax:
                Imax = value_grid[i][j]

    for i in range(width):
        for j in range(height):
            value_grid[i][j] = value_grid[i][j] / Imax

    value_grid[50, 50] = 1
    return value_grid
Exemple #57
0
    def __init__(self, length, initialCoordinates):
        """
        @param length: How many points will be converted, including
                       the 3 dummy points.

        @param initialCoordinates: Either None, or an array of three
                                   sets of x, y, z cartesian
                                   coordinates.  These are the
                                   coordinates of the three dummy
                                   points, indices 1, 2, and 3, which
                                   are defined before any actual data
                                   points are added.
        """
        self._coords = zeros([length+1, 3], Float)
        if (initialCoordinates):
            self._coords[1][0] = initialCoordinates[0][0]
            self._coords[1][1] = initialCoordinates[0][1]
            self._coords[1][2] = initialCoordinates[0][2]

            self._coords[2][0] = initialCoordinates[1][0]
            self._coords[2][1] = initialCoordinates[1][1]
            self._coords[2][2] = initialCoordinates[1][2]

            self._coords[3][0] = initialCoordinates[2][0]
            self._coords[3][1] = initialCoordinates[2][1]
            self._coords[3][2] = initialCoordinates[2][2]
        else:
            self._coords[1][0] = -1.0
            self._coords[1][1] = -1.0
            self._coords[1][2] =  0.0

            self._coords[2][0] = -1.0
            self._coords[2][1] =  0.0
            self._coords[2][2] =  0.0

            self._coords[3][0] =  0.0
            self._coords[3][1] =  0.0
            self._coords[3][2] =  0.0

        self._nextIndex = 4
Exemple #58
0
def _ReadMX(sh,r,c, n,m, NoneValue=0, typecode='d'):
	from Numeric import array, zeros
	A,B = Letter(c),Letter(c+m-1)
	try:
		cell1="%s%d" % (A,r)
		cell2="%s%d" % (B,r+n-1)
		def filt(x,d=0):
 			if x is None: return d
 			return x
		MX=array(list(map(lambda x,filt=filt: list(map(filt,x)),sh.Range(cell1,cell2).Value)),typecode)
	except:
		MX = zeros((n,m),typecode)
		fmt="%s%%d:%s%%d"%(A,B)
		for i in range(n):
			R=sh.Range(fmt%(r+i,r+i)).Value
			if m>1:
				R = R[0]
			try:
				MX[i,:]=array(R,typecode)
			except:
				MX[i,:]=array(_NoneToDefault(R,NoneValue),typecode)
	return MX,(r+n,c+m)