예제 #1
0
파일: matrix.py 프로젝트: cviner/weblogo
    def _ordkey(self, key):
        """Convert string indices into integers. Handles characters, strings
        slices with strings, and tuples of the same. Anything else is
        unchanged.
        """
        def norm(key, alpha):
            if key is None:
                return None
            elif isinstance(key, str) or isinstance(key, Alphabet):
                key = str(key)
                if len(key) == 1:
                    return alpha.ord(key)
                if len(key) == 0:
                    return None
                return na.asarray(alpha.ords(key))
            elif isinstance(key, slice):
                start = norm(key.start, alpha)
                stop = norm(key.stop, alpha)
                step = key.step
                return slice(start, stop, step)
            else:
                return key

        if isinstance(key, tuple):
            return tuple([norm(k, a) for k, a in zip(key, self.alphabets)])
        else:
            return norm(key, self.alphabets[0])
예제 #2
0
파일: matrix.py 프로젝트: ghuls/weblogo
    def _ordkey(self, key):
        """Convert string indices into integers. Handles characters, strings
        slices with strings, and tuples of the same. Anything else is 
        unchanged.
        """

        def norm(key, alpha):
            if key is None:
                return None
            elif isinstance(key, str) or isinstance(key, Alphabet):
                key = str(key)
                if len(key) == 1:
                    return alpha.ord(key)
                if len(key) == 0:
                    return None
                return na.asarray(alpha.ords(key))
            elif isinstance(key, slice):
                start = norm(key.start, alpha)
                stop = norm(key.stop, alpha)
                step = key.step
                return slice(start, stop, step)
            else:
                return key

        if isinstance(key, tuple):
            return tuple([norm(k, a) for k, a in zip(key, self.alphabets)])
        else:
            return norm(key, self.alphabets[0])
예제 #3
0
파일: matrix.py 프로젝트: cviner/weblogo
    def __init__(self, alphabets, values=None, dtype=None):
        """
        Args:
        - alphabets -- a list of alphabets (as string or Alphabet objects) to
                    be used to convert strings into indices. The lengths of
                    the alphabets match the shape of the indexed array.
                    Alternatively, an integer or None in the list indicate a
                    non-alphabetic dimension. If None the dimension length is
                    taken from values argument.
        - values -- An array of values to be indexed. If None a new
                 array is created. If this argument is not a numpy array
                 then the alphabet list must be explicit (cannot contain
                 None.)
        - dtype -- An optional numpy type code.
        """

        # A dummy object to be used in place of None in the alphabets list
        # so that we get meaningful error messages if we try to index a
        # nonalphabetic dimension with a string.
        class NullAlphabet(object):
            def ord(self, key):
                raise IndexError('This dimension does not have an alphabet')

            def ords(self, key):
                raise IndexError('This dimension does not have an alphabet')

        alpha = []
        shape = []
        for a in alphabets:
            if isinstance(a, str):
                a = Alphabet(a)

            if a is None:
                shape.append(None)
                alpha.append(NullAlphabet())
            elif isinstance(a, Alphabet):
                shape.append(len(a))
                alpha.append(a)
            else:
                shape.append(int(a))
                alpha.append(None)

        shape = tuple(shape)
        if values is None:
            values = na.zeros(shape=shape, dtype=dtype)
        else:
            values = na.asarray(values, dtype=dtype)
            vshape = values.shape
            if len(shape) != len(vshape):
                raise ValueError("The values array is the wrong shape.")
            for s1, s2 in zip(shape, vshape):
                if s1 is not None and s1 != s2:
                    raise ValueError("The values array is the wrong shape.")
        self.array = values
        self.alphabets = tuple(alpha)
예제 #4
0
파일: matrix.py 프로젝트: ghuls/weblogo
    def __init__(self, alphabets, values=None, dtype=None):
        """
        Args:
        - alphabets -- a list of alphabets (as string or Alphabet objects) to
                    be used to convert strings into indices. The lengths of 
                    the alphabets match the shape of the indexed array. 
                    Alternatively, an integer or None in the list indicate a 
                    non-alphabetic dimension. If None the dimension length is 
                    taken from values argument.
        - values -- An array of values to be indexed. If None a new  
                 array is created. If this argument is not a numpy array
                 then the alphabet list must be explicit (cannot contain 
                 None.)
        - dtype -- An optional numpy type code.
        """

        # A dummy object to be used in place of None in the alphabets list
        # so that we get meaningful error messages if we try to index a 
        # nonalphabetic dimension with a string.
        class NullAlphabet(object):
            def ord(self, key):
                raise IndexError('This dimension does not have an alphabet')

            def ords(self, key):
                raise IndexError('This dimension does not have an alphabet')

        alpha = []
        shape = []
        for a in alphabets:
            if isinstance(a, str):
                a = Alphabet(a)

            if a is None:
                shape.append(None)
                alpha.append(NullAlphabet())
            elif isinstance(a, Alphabet):
                shape.append(len(a))
                alpha.append(a)
            else:
                shape.append(int(a))
                alpha.append(None)

        shape = tuple(shape)
        if values is None:
            values = na.zeros(shape=shape, dtype=dtype)
        else:
            values = na.asarray(values, dtype=dtype)
            vshape = values.shape
            if len(shape) != len(vshape):
                raise ValueError("The values array is the wrong shape.")
            for s1, s2 in zip(shape, vshape):
                if s1 is not None and s1 != s2:
                    raise ValueError("The values array is the wrong shape.")
        self.array = values
        self.alphabets = tuple(alpha)