Exemplo n.º 1
0
def triangularPut(m1d, upper=1, lower=0):
    """Returns 2D masked array with elements of the given 1D array in the strictly upper (lower) triangle.
    Elements of the 1D array should be ordered according to the upper triangular part of the 2D matrix.
    The lower triangular part (if requested) equals to the transposed upper triangular part.
    If upper == lower == 1 a symetric matrix is returned.
    """
    assert upper in [0,1] and lower in [0,1], "[0|1] expected for upper / lower"
    m1d = MA.asarray(m1d)
    assert MA.rank(m1d) == 1, "1D masked array expected"
    m2dShape0 = math.ceil(math.sqrt(2*m1d.shape[0]))
    assert m1d.shape[0] == m2dShape0*(m2dShape0-1)/2, "the length of m1d does not correspond to n(n-1)/2"
    if upper:
        if lower:
            mask = Numeric.fromfunction(lambda i,j: i==j, (m2dShape0, m2dShape0))
        else:
            mask = Numeric.fromfunction(lambda i,j: i>=j, (m2dShape0, m2dShape0))
    else:
        if lower:
            mask = Numeric.fromfunction(lambda i,j: i<=j, (m2dShape0, m2dShape0))
        else:
            mask = Numeric.ones((m2dShape0, m2dShape0))

    m2d = MA.ravel(MA.zeros((m2dShape0, m2dShape0), m1d.dtype.char))
    condUpperTriang = Numeric.fromfunction(lambda i,j: i<j, (m2dShape0, m2dShape0))
    putIndices = Numeric.compress(Numeric.ravel(condUpperTriang), Numeric.arange(0, m2dShape0**2, typecode=Numeric.Int))
    MA.put(m2d, putIndices, m1d)
    m2d = MA.reshape(m2d, (m2dShape0, m2dShape0))
    m2d = MA.where(condUpperTriang, m2d, MA.transpose(m2d))
    return MA.array(m2d, mask=Numeric.logical_or(mask, MA.getmaskarray(m2d)))
Exemplo n.º 2
0
def diagonalPut(m1d, m2d):
    """Puts the given 1D masked array into the diagonal of the given 2D masked array and returns a new copy of the 2D array.
    """
    m1d = MA.asarray(m1d)
    m2d = MA.asarray(m2d)
    assert MA.rank(m1d) == 1 and MA.rank(m2d) == 2, "1D and 2D masked array expected"
    assert m1d.shape[0] == m2d.shape[0] == m2d.shape[1], "the shape of the given arrays does not match"
    putIndices = Numeric.compress(Numeric.ravel(Numeric.fromfunction(lambda i,j: i==j, m2d.shape)), Numeric.arange(0, Numeric.multiply.reduce(m2d.shape), typecode=Numeric.Int))
    m2dShape = m2d.shape
    m2d = MA.ravel(m2d)
    MA.put(m2d, putIndices, m1d)
    return MA.reshape(m2d, m2dShape)
Exemplo n.º 3
0
def triangularGet(m2d, upper=1):
    """Returns 1D masked array with elements from the upper (lower) triangular part of the given matrix.
    For a symetric matrix triangularGet(m2d, 0) and triangularGet(m2d, 1) return elements in different order.
    """
    assert upper in [0,1], "upper: [0|1] expected"
    m2d = MA.asarray(m2d)
    assert MA.rank(m2d) == 2, "2D (masked) array expected"
    if upper:
        takeInd = Numeric.compress(Numeric.ravel(Numeric.fromfunction(lambda i,j: i<j, m2d.shape)), Numeric.arange(0, Numeric.multiply.reduce(m2d.shape), typecode=Numeric.Int))
    else:
        takeInd = Numeric.compress(Numeric.ravel(Numeric.fromfunction(lambda i,j: i>j, m2d.shape)), Numeric.arange(0, Numeric.multiply.reduce(m2d.shape), typecode=Numeric.Int))
    return MA.ravel(m2d).take(takeInd)
Exemplo n.º 4
0
    def computeMatrix(self):
        if not self.data:
            self.send("Distance Matrix", None)
            return
##        if self.Metrics == 0: # bug in orange, correct (remove normalize) once it is fixed
##            dist = self.metrics[self.Metrics][1](self.data[0], normalize=0)
##        else:
##            dist = self.metrics[self.Metrics][1](self.data[0])
        matrix = orange.SymMatrix(len(self.data))
        matrix.setattr('items', self.data)
        self.progressBarInit()
        pbStep = 100. / (len(self.data)**2 / 2. - len(self.data) / 2.)
        for i in range(len(self.data) - 1):
            for j in range(i + 1, len(self.data)):
                ##                matrix[i, j] = self.computeDistance(self.data[i], self.data[j], dist)
                matrix[i, j] = self.metrics[self.Metrics][1](
                    MA.ravel(self.data[i].toNumpyMA("a")[0]),
                    MA.ravel(self.data[j].toNumpyMA("a")[0]))
                self.progressBarAdvance(pbStep)
        self.progressBarFinished()
        self.send("Distance Matrix", matrix)
Exemplo n.º 5
0
 def anova1B(self, ma3d, groupLens, repMeasures, callback):
     """conducts one-way ANOVA on individual examples wrt factor B (data sets);
     ma3d axis 2 also contains replicas according to groupLens;
     returns Numeric array of p-values in shape (1, numExamples).
     WARNING: works slower than anova1A because it requires to copy 1D array to 2D array
              although we could use Anova1wayLR instead of Anova1wayLR_2D, but not for repeated measures
              additionaly, Anova1wayLR_2D handles missing factor levels correctly, which is not the case for Anova1wayLR
     """
     ps = -1*Numeric.ones((ma3d.shape[0],), Numeric.Float)
     # groupLens [2,3,4] -> groupInd [[0,1],[2,3,4],[5,6,7,8]]
     if repMeasures:
         fAnova = Anova.AnovaRM12LR
     else:
         fAnova = Anova.Anova1wayLR_2D
     grpLensAcc = Numeric.concatenate([[0],Numeric.add.accumulate(groupLens)])
     grpInd = map(lambda i,j: range(i, j), grpLensAcc[:-1], grpLensAcc[1:])
     for eIdx in range(ma3d.shape[0]):
         m2 = MA.zeros((max(groupLens)*ma3d.shape[1], len(groupLens)), Numeric.Float) * MA.masked # axis0: replicas, axis1: factor B levels
         for groupIdx,takeInd in enumerate(grpInd):
             m2[:groupLens[groupIdx]*ma3d.shape[1], groupIdx] = MA.ravel(ma3d[eIdx].take(takeInd, 1))
         an = fAnova(m2)
         ps[eIdx] = an.Fprob
         callback()
     return ps
Exemplo n.º 6
0
def getPositions(m, val):
    """Input: arbitrary (masked) array and a value from that array;
    Output: array of positions of the given value in a flat m;
    """
    m = MA.asarray(m)
    return Numeric.compress(MA.equal(MA.ravel(m),val), Numeric.arange(Numeric.multiply.reduce(m.shape)))