예제 #1
0
파일: hmm.py 프로젝트: pruan/TestDepot
    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
예제 #2
0
    def computeEndPointsFromChunk(self, chunk, update = True):
        """
        Derives and returns the endpoints and radius of a Peptide chunk.
        @param chunk: a Peptide chunk
        @type  chunk: Chunk
        @return: endPoint1, endPoint2 and radius
        @rtype: Point, Point and float

        @note: computing the endpoints works fine when n=m or m=0. Otherwise,
               the endpoints can be slightly off the central axis, especially
               if the Peptide is short.
        @attention: endPoint1 and endPoint2 may not be the original endpoints,
                    and they may be flipped (opposites of) the original
                    endpoints.
        """
        # Since chunk.axis is not always one of the vectors chunk.evecs
        # (actually chunk.poly_evals_evecs_axis[2]), it's best to just use
        # the axis and center, then recompute a bounding cylinder.
        if not chunk.atoms:
            return None

        axis = chunk.axis
        axis = norm(axis) # needed
        center = chunk._get_center()
        points = chunk.atpos - center # not sure if basepos points are already centered
        # compare following Numeric Python code to findAtomUnderMouse and its caller
        matrix = matrix_putting_axis_at_z(axis)
        v = dot( points, matrix)
        # compute xy distances-squared between axis line and atom centers
        r_xy_2 = v[:,0]**2 + v[:,1]**2

        # to get radius, take maximum -- not sure if max(r_xy_2) would use Numeric code, but this will for sure:
        i = argmax(r_xy_2)
        max_xy_2 = r_xy_2[i]
        radius = sqrt(max_xy_2)
        # to get limits along axis (since we won't assume center is centered between them), use min/max z:
        z = v[:,2]
        min_z = z[argmin(z)]
        max_z = z[argmax(z)]

        # Adjust the endpoints such that the ladder rungs (rings) will fall
        # on the ring segments.
        # TO DO: Fix drawPeptideLadder() to offset the first ring, then I can
        # remove this adjustment. --Mark 2008-04-12
        z_adjust = self.getEndPointZOffset()
        min_z += z_adjust
        max_z -= z_adjust

        endpoint1 = center + min_z * axis
        endpoint2 = center + max_z * axis

        if update:
            #print "Original endpoints:", self.getEndPoints()
            self.setEndPoints(endpoint1, endpoint2)
            #print "New endpoints:", self.getEndPoints()

        return (endpoint1, endpoint2, radius)
예제 #3
0
    def computeEndPointsFromChunk(self, chunk, update=True):
        """
        Derives and returns the endpoints and radius of a Peptide chunk.
        @param chunk: a Peptide chunk
        @type  chunk: Chunk
        @return: endPoint1, endPoint2 and radius
        @rtype: Point, Point and float

        @note: computing the endpoints works fine when n=m or m=0. Otherwise,
               the endpoints can be slightly off the central axis, especially
               if the Peptide is short.
        @attention: endPoint1 and endPoint2 may not be the original endpoints,
                    and they may be flipped (opposites of) the original
                    endpoints.
        """
        # Since chunk.axis is not always one of the vectors chunk.evecs
        # (actually chunk.poly_evals_evecs_axis[2]), it's best to just use
        # the axis and center, then recompute a bounding cylinder.
        if not chunk.atoms:
            return None

        axis = chunk.axis
        axis = norm(axis)  # needed
        center = chunk._get_center()
        points = chunk.atpos - center  # not sure if basepos points are already centered
        # compare following Numeric Python code to findAtomUnderMouse and its caller
        matrix = matrix_putting_axis_at_z(axis)
        v = dot(points, matrix)
        # compute xy distances-squared between axis line and atom centers
        r_xy_2 = v[:, 0] ** 2 + v[:, 1] ** 2

        # to get radius, take maximum -- not sure if max(r_xy_2) would use Numeric code, but this will for sure:
        i = argmax(r_xy_2)
        max_xy_2 = r_xy_2[i]
        radius = sqrt(max_xy_2)
        # to get limits along axis (since we won't assume center is centered between them), use min/max z:
        z = v[:, 2]
        min_z = z[argmin(z)]
        max_z = z[argmax(z)]

        # Adjust the endpoints such that the ladder rungs (rings) will fall
        # on the ring segments.
        # TO DO: Fix drawPeptideLadder() to offset the first ring, then I can
        # remove this adjustment. --Mark 2008-04-12
        z_adjust = self.getEndPointZOffset()
        min_z += z_adjust
        max_z -= z_adjust

        endpoint1 = center + min_z * axis
        endpoint2 = center + max_z * axis

        if update:
            # print "Original endpoints:", self.getEndPoints()
            self.setEndPoints(endpoint1, endpoint2)
            # print "New endpoints:", self.getEndPoints()

        return (endpoint1, endpoint2, radius)
예제 #4
0
    def compute_memo(self, chunk):
        """
        If drawing chunk in this display mode can be optimized by precomputing some info from chunk's appearance,
        compute that info and return it.
           If this computation requires preference values, access them as env.prefs[key],
        and that will cause the memo to be removed (invalidated) when that preference value is changed by the user.
           This computation is assumed to also depend on, and only on, chunk's appearance in ordinary display modes
        (i.e. it's invalidated whenever havelist is). There is not yet any way to change that,
        so bugs will occur if any ordinarily invisible chunk info affects this rendering,
        and potential optimizations will not be done if any ordinarily visible info is not visible in this rendering.
        These can be fixed if necessary by having the real work done within class Chunk's _recompute_ rules,
        with this function or drawchunk just accessing the result of that (and sometimes causing its recomputation),
        and with whatever invalidation is needed being added to appropriate setter methods of class Chunk.
        If the real work can depend on more than chunk's ordinary appearance can, the access would need to be in drawchunk;
        otherwise it could be in drawchunk or in this method compute_memo.
        """
        # for this example, we'll turn the chunk axes into a cylinder.
        # Since chunk.axis is not always one of the vectors chunk.evecs (actually chunk.poly_evals_evecs_axis[2]),
        # it's best to just use the axis and center, then recompute a bounding cylinder.
        if not chunk.atoms:
            return None
        axis = chunk.axis
        axis = norm(
            axis
        )  # needed (unless we're sure it's already unit length, which is likely)
        center = chunk.center
        points = chunk.atpos - center  # not sure if basepos points are already centered
        # compare following Numeric Python code to findAtomUnderMouse and its caller
        matrix = matrix_putting_axis_at_z(axis)
        v = dot(points, matrix)
        # compute xy distances-squared between axis line and atom centers
        r_xy_2 = v[:, 0]**2 + v[:, 1]**2
        ## r_xy = sqrt(r_xy_2) # not needed

        # to get radius, take maximum -- not sure if max(r_xy_2) would use Numeric code, but this will for sure:
        i = argmax(r_xy_2)
        max_xy_2 = r_xy_2[i]
        radius = sqrt(max_xy_2)
        # to get limits along axis (since we won't assume center is centered between them), use min/max z:
        z = v[:, 2]
        min_z = z[argmin(z)]
        max_z = z[argmax(z)]
        bcenter = chunk.abs_to_base(center)
        # return, in chunk-relative coords, end1, end2, and radius of the cylinder, and color.
        color = chunk.color
        if color is None:
            color = V(0.5, 0.5, 0.5)
        # make sure it's longer than zero (in case of a single-atom chunk); in fact, add a small margin all around
        # (note: this is not sufficient to enclose all atoms entirely; that's intentional)
        margin = 0.2
        min_z -= margin
        max_z += margin
        radius += margin
        return (bcenter + min_z * axis, bcenter + max_z * axis, radius, color)
예제 #5
0
    def compute_memo(self, chunk):
        """
        If drawing chunk in this display mode can be optimized by precomputing some info from chunk's appearance,
        compute that info and return it.
           If this computation requires preference values, access them as env.prefs[key],
        and that will cause the memo to be removed (invalidated) when that preference value is changed by the user.
           This computation is assumed to also depend on, and only on, chunk's appearance in ordinary display modes
        (i.e. it's invalidated whenever havelist is). There is not yet any way to change that,
        so bugs will occur if any ordinarily invisible chunk info affects this rendering,
        and potential optimizations will not be done if any ordinarily visible info is not visible in this rendering.
        These can be fixed if necessary by having the real work done within class Chunk's _recompute_ rules,
        with this function or drawchunk just accessing the result of that (and sometimes causing its recomputation),
        and with whatever invalidation is needed being added to appropriate setter methods of class Chunk.
        If the real work can depend on more than chunk's ordinary appearance can, the access would need to be in drawchunk;
        otherwise it could be in drawchunk or in this method compute_memo.
        """
        # for this example, we'll turn the chunk axes into a cylinder.
        # Since chunk.axis is not always one of the vectors chunk.evecs (actually chunk.poly_evals_evecs_axis[2]),
        # it's best to just use the axis and center, then recompute a bounding cylinder.
        if not chunk.atoms:
            return None
        axis = chunk.axis
        axis = norm(axis) # needed (unless we're sure it's already unit length, which is likely)
        center = chunk.center
        points = chunk.atpos - center # not sure if basepos points are already centered
        # compare following Numeric Python code to findAtomUnderMouse and its caller
        matrix = matrix_putting_axis_at_z(axis)
        v = dot( points, matrix)
        # compute xy distances-squared between axis line and atom centers
        r_xy_2 = v[:,0]**2 + v[:,1]**2
        ## r_xy = sqrt(r_xy_2) # not needed

        # to get radius, take maximum -- not sure if max(r_xy_2) would use Numeric code, but this will for sure:
        i = argmax(r_xy_2)
        max_xy_2 = r_xy_2[i]
        radius = sqrt(max_xy_2)
        # to get limits along axis (since we won't assume center is centered between them), use min/max z:
        z = v[:,2]
        min_z = z[argmin(z)]
        max_z = z[argmax(z)]
        bcenter = chunk.abs_to_base(center)
        # return, in chunk-relative coords, end1, end2, and radius of the cylinder, and color.
        color = chunk.color
        if color is None:
            color = V(0.5,0.5,0.5)
        # make sure it's longer than zero (in case of a single-atom chunk); in fact, add a small margin all around
        # (note: this is not sufficient to enclose all atoms entirely; that's intentional)
        margin = 0.2
        min_z -= margin
        max_z += margin
        radius += margin
        return (bcenter + min_z * axis, bcenter + max_z * axis, radius, color)
예제 #6
0
    def toConsensus(self, cutoff=None, fully_degenerate=False,\
        include_all=False):
        """Returns the consensus sequence from a profile.

        cutoff: cutoff value, determines how much should be covered in a
        position (row) of the profile. Example: pos 0 [.2,.1,.3,.4]
        (CharOrder: TCAG). To cover .65 (=cutoff) we need two characters:
        A and G, which results in the degenerate character R.
        
        fully_degenerate: determines whether the fully degenerate character
        is returned at a position. For the example above an 'N' would
        be returned.
       
        inlcude_all: all possibilities are included in the degenerate 
        character. Example: row = UCAG = [.1,.3,.3,.3] cutoff = .4, 
        consensus = 'V' (even though only 2 chars would be enough to 
        reach the cutoff value).

        The Alphabet of the Profile should implement degenerateFromSequence.
        
        Note that cutoff has priority over fully_degenerate. In other words,
        if you specify a cutoff value and set fully_degenerate to true, 
        the calculation will be done with the cutoff value. If nothing 
        gets passed in, the maximum argument is chosen. In the first example
        above G will be returned.
        """
        #set up some local variables
        co = array(self.CharOrder)
        alpha = self.Alphabet
        data = self.Data

        #determine the action. Cutoff takes priority over fully_degenerate
        if cutoff:
            result = []
            degen = self.rowDegeneracy(cutoff)
            sorted = argsort(data)
            if include_all:
                #if include_all include all possiblilities in the degen char 
                for row_idx, (num_to_keep, row) in enumerate(zip(degen,sorted)):
                    to_take = [item for item in row[-num_to_keep:]\
                    if item in nonzero(data[row_idx])] +\
                    [item for item in nonzero(data[row_idx] ==\
                        data[row_idx,row[-num_to_keep]]) if item in\
                        nonzero(data[row_idx])]
                    result.append(alpha.degenerateFromSequence(\
                    map(str,take(co, to_take))))
            else:
                for row_idx, (num_to_keep, row) in enumerate(zip(degen,sorted)):
                    result.append(alpha.degenerateFromSequence(\
                        map(str,take(co, [item for item in row[-num_to_keep:]\
                        if item in nonzero(data[row_idx])]))))
                                    
        elif not fully_degenerate: 
            result = take(co, argmax(self.Data))
        else:
            result = []
            for row in self.Data:
                result.append(alpha.degenerateFromSequence(\
                map(str,take(co, nonzero(row)))))
        return ''.join(map(str,result))
예제 #7
0
파일: cv.py 프로젝트: bxlab/esperr
 def run_fold(self, train_sets, test_sets):
     """Run one fold of the cross validation"""
     # Build predictor from training sets
     predictors = []
     for train in train_sets:
         predictors.append(self.model_class(train))
     # Score each sequence
     for i, train in enumerate(test_sets):
         for s in train:
             scores = [p.score(s) for p in predictors]
             c = argmax(scores)
             if c == i:
                 self.cls[i].pos += 1
             else:
                 self.cls[i].neg += 1
             if self.keep_results:
                 self.scores.append(scores)
                 self.classes.append((i, c))
예제 #8
0
 def nanargmax(x, axis=-1):
     """Find the maximum over the given axis ignoring nans.
     """
     x = _asarray1d(x).copy()
     _nx.putmask(x, isnan(x), -inf)
     return argmax(x, axis)
예제 #9
0
 def getrotation(self): #bruce 050518 new feature for showing rotation of rmotor in its cap-arrow
     """
     Return a rotation angle for the motor. This is arbitrary, but rotates smoothly
     with the atoms, averaging out their individual thermal motion.
     It is not history-dependent -- e.g. it will be consistent regardless of how you jump around
     among the frames of a movie. But if we ever implement remaking or revising the motor position,
     or if you delete some of the motor's atoms, this angle is forgotten and essentially resets to 0.
     (That could be fixed, and the angle even saved in the mmp file, if desired. See code comments
     for other possible improvements.)
     """
     # possible future enhancements:
     # - might need to preserve rotation when we forget old posns, by setting an arb offset then;
     # - might need to preserve it in mmp file??
     # - might need to draw it into PovRay file??
     # - might need to preserve it when we translate or rotate entire jig with its atoms (doing which is NIM for now)
     # - could improve and generalize alg, and/or have sim do it (see comments below for details).
     #
     posns = A(map( lambda a: a.posn(), self.atoms ))
     posns -= self.center
     if self._initial_posns is None:
         # (we did this after -= center, so no need to forget posns if we translate the entire jig)
         self._initial_posns = posns # note, we're storing *relative* positions, in spite of the name!
         self._initial_quats = None # compute these the first time they're needed (since maybe never needed)
         return 0.0 # returning this now (rather than computing it below) is just an optim, in theory
     assert len(self._initial_posns) == len(posns), "bug in invalidating self._initial_posns when rmotor atoms change"
     if not (self._initial_posns != posns): # have to use not (x != y) rather than (x == y) due to Numeric semantics!
         # no (noticable) change in positions - return quickly
         # (but don't change stored posns, in case this misses tiny changes which could accumulate over time)
         # (we do this before the subsequent stuff, to not waste redraw time when posns don't change;
         #  just re correctness, we could do it at a later stage)
         return 0.0
     # now we know the posns are different, and we have the old ones to compare them to.
     posns = self.norm_project_posns( posns) # this might modify posns object, and might return same or different object
     quats = self._initial_quats
     if quats is None:
         # precompute a quat to rotate new posns into a standard coord system for comparison to old ones
         # (Q args must be orthonormal and right-handed)
         oldposns = + self._initial_posns # don't modify those stored initial posns
             # (though it probably wouldn't matter if we did -- from now on,
             #  they are only compared to None and checked for length, as of 050518)
         oldposns = self.norm_project_posns( oldposns)
         axis = self.axis
         quats = self._initial_quats = [ Q(axis,pos1,cross(axis,pos1)) for pos1 in oldposns ]
     angs = []
     for qq, pos2 in zip( self._initial_quats, posns):
         npos2 = qq.unrot(pos2)
         # now npos2 is in yz plane, and pos1 (if transformed) would just be the y axis in that plane;
         # just get its angle in that plane (defined so that if pos2 = pos1, ie npos2 = (0,1,0), then angle is 0)
         ang = angle(npos2[1], npos2[2]) # in degrees
         angs.append(ang)
     # now average these angles, paying attention to their being on a circle
     # (which means the average of 1 and 359 is 0, not 180!)
     angs.sort()
         # Warning: this sort is only correct since we know they're in the range [0,360] (inclusive range is ok).
         # It might be correct for any range that covers the circle exactly once, e.g. [-180,180]
         # (not fully analyzed for that), but it would definitely be wrong for e.g. [-0.001, 360.001]!
         # So be careful if you change how angle() works.
     angs = A(angs)
     gaps = angs[1:] - angs[:-1]
     gaps = [angs[0] - angs[-1] + 360] + list(gaps)
     i = argmax(gaps)
     ##e Someday we should check whether this largest gap is large enough for this to make sense (>>180);
     # we are treating the angles as "clustered together in the part of the circle other than this gap"
     # and averaging them within that cluster. It would also make sense to discard outliers,
     # but doing this without jittering the rotation angle (as individual points become closer
     # to being outliers) would be challenging. Maybe better to just give up unless gap is, say, >>340.
     ##e Before any of that, just get the sim to do this in a better way -- interpret the complete set of
     # atom motions as approximating some overall translation and rotation, and tell us this, so we can show
     # not only rotation, but axis wobble and misalignment, and so these can be plotted.
     angs = list(angs)
     angs = angs[i:] + angs[:i] # start with the one just after the largest gap
     relang0 = angs[0]
     angs = A(angs) - relang0 # be relative to that, when we average them
     # but let them all be in the range [0,360)!
     angs = (angs + 720) % 360
         # We need to add 720 since Numeric's mod produces negative outputs
         # for negative inputs (unlike Python's native mod, which is correct)!
         # How amazingly ridiculous.
     ang = (sum(angs) / len(angs)) + relang0
     ang = ang % 360 # this is Python mod, so it's safe
     return ang
예제 #10
0
 def nanargmax(x,axis=-1):
     """Find the maximum over the given axis ignoring nans.
     """
     x = _asarray1d(x).copy()
     _nx.putmask(x,isnan(x),-inf)
     return argmax(x,axis)