예제 #1
0
def make_incidence_matrix(individuals, effect_name):
    if effect_name.lower() == 'residual':
        incidence_matrix = sparseeye(len(individuals))

    elif is_genetic_effect(effect_name):
        incidence_matrix = sparseeye(len(individuals))

    else:
        levels = sorted({ind.phenotypes[effect_name] for ind in individuals})

        # Missing values are not a valid level
        levels = [x for x in levels if x is not None]

        nlevels = len(levels)

        # Calculate which individual has which level
        gen = (ind.phenotypes[effect_name] == level for ind, level in
               product(individuals, levels))
        Z = np.fromiter(gen, dtype=np.uint8)

        # Shout out to scipy for both not documenting reshape on any of their
        # sparse matrix objects and also not making them take the same number
        # of arguments
        Z = Z.reshape(-1, nlevels)

        # Check for missing values and complain about them!
        # Kind of hard to read but heres how it goes:
        # Check if any of the rows are all zero.
        if (Z == 0).all(axis=1).any():
            raise LinAlgError('Missing values in random effect')

        incidence_matrix = csc_matrix(Z)

    return incidence_matrix
예제 #2
0
def make_incidence_matrix(individuals, effect_name):
    if effect_name.lower() == 'residual':
        incidence_matrix = sparseeye(len(individuals))

    elif is_genetic_effect(effect_name):
        incidence_matrix = sparseeye(len(individuals))

    else:
        levels = sorted({ind.phenotypes[effect_name] for ind in individuals})

        # Missing values are not a valid level
        levels = [x for x in levels if x is not None]

        nlevels = len(levels)

        # Calculate which individual has which level
        gen = (ind.phenotypes[effect_name] == level
               for ind, level in product(individuals, levels))
        Z = np.fromiter(gen, dtype=np.uint8)

        # Shout out to scipy for both not documenting reshape on any of their
        # sparse matrix objects and also not making them take the same number
        # of arguments
        Z = Z.reshape(-1, nlevels)

        # Check for missing values and complain about them!
        # Kind of hard to read but heres how it goes:
        # Check if any of the rows are all zero.
        if (Z == 0).all(axis=1).any():
            raise LinAlgError('Missing values in random effect')

        incidence_matrix = csc_matrix(Z)

    return incidence_matrix
예제 #3
0
    def __init__(self,
                 individuals,
                 label,
                 variance=None,
                 incidence_matrix=None,
                 covariance_matrix=None,
                 levels=None):
        """
        Create the random effect.

        :param individuals: Individuals included
        :param label: name of the effect
        :param variance: variance associated with the effect
        :param incidence_matrix: incidence matrix for random effect
        :param covariance_matrix: covariance matrix for random effect
        :param levels: levels of random effect
        :type individuals: iterable
        :type label: string
        :type variance: float
        :type incidence_matrix: matrix
        :type covariance_matrix: matrix 
        """
        nobs = len(individuals)

        self.label = label
        self.variance_component = variance

        if isinstance(incidence_matrix, str) and incidence_matrix == 'eye':
            self.incidence_matrix = sparseeye(nobs, nobs)
        elif incidence_matrix is None:
            self.incidence_matrix = make_incidence_matrix(
                individuals, self.label)
        else:
            self.incidence_matrix = incidence_matrix

        if covariance_matrix is None:
            # Number of levels of random effects is the number of
            # columns in the incidence matrix
            nlevel = self.incidence_matrix.shape[1]
            self.covariance_matrix = sparseeye(nlevel, nlevel)
        else:
            # Covariance matrices are square
            if covariance_matrix.shape[0] != covariance_matrix.shape[1]:
                raise LinAlgError('Covariance matrix not square')
            if covariance_matrix.shape[0] != self.incidence_matrix.shape[1]:
                raise LinAlgError('Incidence and covariance matrix '
                                  'not conformable')
            self.covariance_matrix = covariance_matrix

        if not levels:
            self.levels = [
                'L{}'.format(i) for i in range(self.incidence_matrix.shape[1])
            ]
        else:
            if len(levels) != incidence_matrix.shape[1]:
                raise ValueError('Number of levels not correct')
            self.levels = levels

        self.V_i = self.Z * self.G * self.Z.T
예제 #4
0
    def __init__(self, individuals, label, variance=None,
                 incidence_matrix=None, covariance_matrix=None, levels=None):
        """
        Create the random effect.

        :param individuals: Individuals included
        :param label: name of the effect
        :param variance: variance associated with the effect
        :param incidence_matrix: incidence matrix for random effect
        :param covariance_matrix: covariance matrix for random effect
        :param levels: levels of random effect
        :type individuals: iterable
        :type label: string
        :type variance: float
        :type incidence_matrix: matrix
        :type covariance_matrix: matrix 
        """
        nobs = len(individuals)

        self.label = label
        self.variance_component = variance
        
        if isinstance(incidence_matrix, str) and incidence_matrix == 'eye':
            self.incidence_matrix = sparseeye(nobs, nobs)
        elif incidence_matrix is None:
            self.incidence_matrix = make_incidence_matrix(individuals,
                                                          self.label)
        else:
            self.incidence_matrix = incidence_matrix

        if covariance_matrix is None:
            # Number of levels of random effects is the number of
            # columns in the incidence matrix
            nlevel = self.incidence_matrix.shape[1]
            self.covariance_matrix = sparseeye(nlevel, nlevel)
        else:
            # Covariance matrices are square
            if covariance_matrix.shape[0] != covariance_matrix.shape[1]:
                raise LinAlgError('Covariance matrix not square')
            if covariance_matrix.shape[0] != self.incidence_matrix.shape[1]:
                raise LinAlgError('Incidence and covariance matrix '
                                  'not conformable')
            self.covariance_matrix = covariance_matrix

        if not levels:
            self.levels = ['L{}'.format(i) for i in 
                            range(self.incidence_matrix.shape[1])]
        else:
            if len(levels) != incidence_matrix.shape[1]:
                raise ValueError('Number of levels not correct')
            self.levels = levels

        self.V_i = self.Z * self.G * self.Z.T
예제 #5
0
    def __init__(self, individuals, label, variance=None,
                 incidence_matrix=None, covariance_matrix=None, levels=None):
        nobs = len(individuals)

        self.label = label
        self.variance_component = variance
        
        if isinstance(incidence_matrix, str) and incidence_matrix == 'eye':
            self.incidence_matrix = sparseeye(nobs, nobs)
        elif incidence_matrix is None:
            m = make_incidence_matrix(individuals,
                                      self.label)
            self.incidence_matrix = m
            
        else:
            self.incidence_matrix = incidence_matrix

        if covariance_matrix is None:
            # Number of levels of random effects is the number of
            # columns in the incidence matrix
            nlevel = self.incidence_matrix.shape[1]
            self.covariance_matrix = sparseeye(nlevel, nlevel)
        else:
            # Covariance matrices are square
            if covariance_matrix.shape[0] != covariance_matrix.shape[1]:
                raise LinAlgError('Covariance matrix not square')
            if covariance_matrix.shape[0] != self.incidence_matrix.shape[1]:
                raise LinAlgError('Incidence and covariance matrix '
                                  'not conformable')
            self.covariance_matrix = covariance_matrix

        if not levels:
            self.levels = ['L{}'.format(i) for i in range(self.incidence_matrix.shape[1])]
        else:
            if len(levels) != incidence_matrix.shape[1]:
                raise ValueError('Number of levels not correct')
            self.levels = levels

        self.V_i = self.Z * self.G * self.Z.T