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)))
def dotMA(a, b): """Returns dot-product for MA arrays; fixed masked values. """ a = MA.asarray(a) b = MA.asarray(b) ab = MA.dot(a,b) # fix masked values in ab (MA.dot returns 0 instead of MA.masked) nonMasked = Numeric.dot(1-MA.getmaskarray(a).astype(Numeric.Int), 1-MA.getmaskarray(b).astype(Numeric.Int)) return MA.where(Numeric.equal(nonMasked,0), MA.masked, ab)
def orng2ma(aExampleTable): """Converts orange.ExampleTable to MA.array based on the attribute values. rows correspond to examples, columns correspond to attributes, class values are left out missing values and attributes of types other than orange.FloatVariable are masked """ vals = aExampleTable.native(0, substituteDK="?", substituteDC="?", substituteOther="?") ma = MA.array(vals, Numeric.PyObject) if aExampleTable.domain.classVar != None: ma = ma[:,:-1] mask = MA.where(MA.equal(ma, "?"), 1, 0) for varIdx, var in enumerate(aExampleTable.domain.attributes): if type(var) != orange.FloatVariable: mask[:,varIdx] = Numeric.ones(len(aExampleTable)) return MA.array(MA.array(ma, Numeric.PyObject, mask=mask).filled(1e20), Numeric.Float, mask=mask)
def orng2ma(aExampleTable): """Converts orange.ExampleTable to MA.array based on the attribute values. rows correspond to examples, columns correspond to attributes, class values are left out missing values and attributes of types other than orange.FloatVariable are masked """ vals = aExampleTable.native(0, substituteDK="?", substituteDC="?", substituteOther="?") ma = MA.array(vals, Numeric.PyObject) if aExampleTable.domain.classVar != None: ma = ma[:, :-1] mask = MA.where(MA.equal(ma, "?"), 1, 0) for varIdx, var in enumerate(aExampleTable.domain.attributes): if type(var) != orange.FloatVariable: mask[:, varIdx] = Numeric.ones(len(aExampleTable)) return MA.array(MA.array(ma, Numeric.PyObject, mask=mask).filled(1e20), Numeric.Float, mask=mask)