Search in sources :

Example 56 with DoubleMatrix2D

use of cern.colt.matrix.DoubleMatrix2D in project Gemma by PavlidisLab.

the class ComBatTest method test5NonParametric.

@Test
public void test5NonParametric() throws Exception {
    DoubleMatrixReader f = new DoubleMatrixReader();
    DoubleMatrix<String, String> testMatrix = f.read(this.getClass().getResourceAsStream("/data/analysis/preprocess/batcheffects/GSE492.test.dat.txt"));
    StringMatrixReader of = new StringMatrixReader();
    StringMatrix<String, String> sampleInfo = of.read(this.getClass().getResourceAsStream("/data/analysis/preprocess/batcheffects/100_GSE492_expdesign.data.txt"));
    @SuppressWarnings({ "unchecked", "rawtypes" }) ComBat<String, String> comBat = new ComBat(testMatrix, sampleInfo);
    DoubleMatrix2D X = comBat.getDesignMatrix();
    // log.info( X );
    assertEquals(1, X.get(0, 0), 0.001);
    assertEquals(0, X.get(1, 0), 0.001);
    assertEquals(0, X.get(4, 3), 0.001);
    DoubleMatrix2D y = new DenseDoubleMatrix2D(testMatrix.asArray());
    DoubleMatrix2D sdata = comBat.standardize(y, X);
    // log.info( sdata.viewPart( 0, 0, 10, 12 ) );
    assertEquals(-1.85175902, sdata.get(7, 1), 0.0001);
    assertEquals(0.2479669, sdata.get(8, 2), 0.001);
    assertEquals(-0.56259384, sdata.get(0, 8), 0.001);
    assertEquals(1.07168246, sdata.get(3, 11), 0.001);
    DoubleMatrix2D finalResult = comBat.run(false);
    assertEquals(12.026930, finalResult.get(7, 0), 0.001);
    assertEquals(11.635157, finalResult.get(7, 7), 0.01);
    assertEquals(12.930425, finalResult.get(9, 7), 0.01);
}
Also used : StringMatrixReader(ubic.basecode.io.reader.StringMatrixReader) DenseDoubleMatrix2D(cern.colt.matrix.impl.DenseDoubleMatrix2D) DoubleMatrix2D(cern.colt.matrix.DoubleMatrix2D) DenseDoubleMatrix2D(cern.colt.matrix.impl.DenseDoubleMatrix2D) DoubleMatrixReader(ubic.basecode.io.reader.DoubleMatrixReader) Test(org.junit.Test)

Example 57 with DoubleMatrix2D

use of cern.colt.matrix.DoubleMatrix2D in project Gemma by PavlidisLab.

the class ComBatTest method test3.

/**
 * Based on GSE13712
 */
@Test
public final void test3() throws Exception {
    DoubleMatrixReader f = new DoubleMatrixReader();
    DoubleMatrix<String, String> testMatrix = f.read(this.getClass().getResourceAsStream("/data/analysis/preprocess/batcheffects/comat.test.data.txt"));
    StringMatrixReader of = new StringMatrixReader();
    StringMatrix<String, String> sampleInfo = of.read(this.getClass().getResourceAsStream("/data/analysis/preprocess/batcheffects/combat.test.design.txt"));
    @SuppressWarnings({ "unchecked", "rawtypes" }) ComBat<String, String> comBat = new ComBat(testMatrix, sampleInfo);
    DoubleMatrix2D result = comBat.run();
    assertNotNull(result);
}
Also used : StringMatrixReader(ubic.basecode.io.reader.StringMatrixReader) DenseDoubleMatrix2D(cern.colt.matrix.impl.DenseDoubleMatrix2D) DoubleMatrix2D(cern.colt.matrix.DoubleMatrix2D) DoubleMatrixReader(ubic.basecode.io.reader.DoubleMatrixReader) Test(org.junit.Test)

Example 58 with DoubleMatrix2D

use of cern.colt.matrix.DoubleMatrix2D in project Gemma by PavlidisLab.

the class ComBatTest method test4.

@Test
public void test4() throws Exception {
    DoubleMatrixReader f = new DoubleMatrixReader();
    DoubleMatrix<String, String> testMatrix = f.read(this.getClass().getResourceAsStream("/data/analysis/preprocess/batcheffects/GSE492.test.dat.txt"));
    StringMatrixReader of = new StringMatrixReader();
    ObjectMatrix<String, String, String> sampleInfo = of.read(this.getClass().getResourceAsStream("/data/analysis/preprocess/batcheffects/100_GSE492_expdesign.data.txt"));
    @SuppressWarnings({ "unchecked", "rawtypes" }) ComBat<String, String> comBat = new ComBat(testMatrix, sampleInfo);
    DoubleMatrix2D X = comBat.getDesignMatrix();
    // log.info( X );
    assertEquals(1, X.get(0, 0), 0.001);
    assertEquals(0, X.get(1, 0), 0.001);
    assertEquals(0, X.get(4, 3), 0.001);
    DoubleMatrix2D y = new DenseDoubleMatrix2D(testMatrix.asArray());
    DoubleMatrix2D sdata = comBat.standardize(y, X);
    // log.info( sdata.viewPart( 0, 0, 10, 12 ) );
    assertEquals(-1.85175902, sdata.get(7, 1), 0.0001);
    assertEquals(0.2479669, sdata.get(8, 2), 0.001);
    assertEquals(-0.56259384, sdata.get(0, 8), 0.001);
    assertEquals(1.07168246, sdata.get(3, 11), 0.001);
    DoubleMatrix2D finalResult = comBat.run();
    assertEquals(12.026468, finalResult.get(7, 0), 0.0001);
    assertEquals(11.640057, finalResult.get(7, 7), 0.0001);
    assertEquals(12.932352, finalResult.get(9, 7), 0.0001);
}
Also used : StringMatrixReader(ubic.basecode.io.reader.StringMatrixReader) DenseDoubleMatrix2D(cern.colt.matrix.impl.DenseDoubleMatrix2D) DoubleMatrix2D(cern.colt.matrix.DoubleMatrix2D) DenseDoubleMatrix2D(cern.colt.matrix.impl.DenseDoubleMatrix2D) DoubleMatrixReader(ubic.basecode.io.reader.DoubleMatrixReader) Test(org.junit.Test)

Example 59 with DoubleMatrix2D

use of cern.colt.matrix.DoubleMatrix2D in project midpoint by Evolveum.

the class AbstractOrgClosureTest method checkClosureMatrix.

protected boolean checkClosureMatrix() throws SchemaException {
    try (Session session = openSession()) {
        // we compute the closure table "by hand" as 1 + A + A^2 + A^3 + ... + A^n where n is the greatest expected path length
        int vertices = getVertices().size();
        long start = System.currentTimeMillis();
        // used to give indices to vertices
        List<String> vertexList = new ArrayList<>(getVertices());
        if (DUMP_TC_MATRIX_DETAILS) {
            logger.info("Vertex list = {}", vertexList);
        }
        DoubleMatrix2D a = new SparseDoubleMatrix2D(vertices, vertices);
        // }
        for (DefaultEdge edge : orgGraph.edgeSet()) {
            a.set(vertexList.indexOf(orgGraph.getEdgeSource(edge)), vertexList.indexOf(orgGraph.getEdgeTarget(edge)), 1.0);
        }
        DoubleMatrix2D result = new SparseDoubleMatrix2D(vertices, vertices);
        for (int i = 0; i < vertices; i++) {
            result.setQuick(i, i, 1.0);
        }
        DoubleMatrix2D power = result.copy();
        Algebra alg = new Algebra();
        for (int level = 1; level <= maxLevel; level++) {
            power = alg.mult(power, a);
            result.assign(power, Functions.plus);
        // System.out.println("a=" + a);
        // System.out.println("a^"+level+"="+power);
        }
        logger.info("TC matrix computed in {} ms", System.currentTimeMillis() - start);
        if (DUMP_TC_MATRIX_DETAILS) {
            logger.info("TC matrix expected = {}", result);
        }
        Query q = session.createNativeQuery("select descendant_oid, ancestor_oid, val from m_org_closure").addScalar("descendant_oid", StringType.INSTANCE).addScalar("ancestor_oid", StringType.INSTANCE).addScalar("val", LongType.INSTANCE);
        List<Object[]> list = q.list();
        logger.info("OrgClosure has {} rows", list.size());
        DoubleMatrix2D closureInDatabase = new SparseDoubleMatrix2D(vertices, vertices);
        for (Object[] item : list) {
            int val = Integer.parseInt(item[2].toString());
            if (val == 0) {
                throw new IllegalStateException("Row with val == 0 in closure table: " + list);
            }
            closureInDatabase.set(vertexList.indexOf(item[0]), vertexList.indexOf(item[1]), val);
        }
        if (DUMP_TC_MATRIX_DETAILS) {
            logger.info("TC matrix fetched from db = {}", closureInDatabase);
        }
        double zSumResultBefore = result.zSum();
        double zSumClosureInDb = closureInDatabase.zSum();
        result.assign(closureInDatabase, Functions.minus);
        double zSumResultAfter = result.zSum();
        logger.info("Summary of items in closure computed: {}, in DB-stored closure: {}, delta: {}", zSumResultBefore, zSumClosureInDb, zSumResultAfter);
        if (DUMP_TC_MATRIX_DETAILS) {
            logger.info("Difference matrix = {}", result);
        }
        boolean problem = false;
        for (int i = 0; i < vertices; i++) {
            for (int j = 0; j < vertices; j++) {
                double delta = result.get(i, j);
                if (Math.round(delta) != 0) {
                    System.err.println("delta(" + vertexList.get(i) + "," + vertexList.get(j) + ") = " + delta + " (closureInDB=" + closureInDatabase.get(i, j) + ", expected=" + (result.get(i, j) + closureInDatabase.get(i, j)) + ")");
                    logger.error("delta(" + vertexList.get(i) + "," + vertexList.get(j) + ") = " + delta);
                    problem = true;
                }
            }
        }
        if (problem) {
            checkOrgGraph();
        }
        return problem;
    }
}
Also used : Query(org.hibernate.query.Query) ObjectQuery(com.evolveum.midpoint.prism.query.ObjectQuery) DefaultEdge(org.jgrapht.graph.DefaultEdge) Algebra(cern.colt.matrix.linalg.Algebra) DoubleMatrix2D(cern.colt.matrix.DoubleMatrix2D) SparseDoubleMatrix2D(cern.colt.matrix.impl.SparseDoubleMatrix2D) PrismObject(com.evolveum.midpoint.prism.PrismObject) SparseDoubleMatrix2D(cern.colt.matrix.impl.SparseDoubleMatrix2D) Session(org.hibernate.Session)

Example 60 with DoubleMatrix2D

use of cern.colt.matrix.DoubleMatrix2D in project beast-mcmc by beast-dev.

the class MarkovModulatedFrequencyModel method computeStationaryDistribution.

private void computeStationaryDistribution(double[] statDistr) {
    if (allRatesAreZero(switchingRates)) {
        return;
    }
    // Uses an LU decomposition to solve Q^t \pi = 0 and \sum \pi_i = 1
    DoubleMatrix2D mat2 = new DenseDoubleMatrix2D(numBaseModel + 1, numBaseModel);
    int index2 = 0;
    for (int i = 0; i < numBaseModel; ++i) {
        for (int j = i + 1; j < numBaseModel; ++j) {
            // Transposed
            mat2.set(j, i, switchingRates.getParameterValue(index2));
            index2++;
        }
    }
    for (int j = 0; j < numBaseModel; ++j) {
        for (int i = j + 1; i < numBaseModel; ++i) {
            // Transposed
            mat2.set(j, i, switchingRates.getParameterValue(index2));
            index2++;
        }
    }
    for (int i = 0; i < numBaseModel; ++i) {
        double rowTotal = 0.0;
        for (int j = 0; j < numBaseModel; ++j) {
            if (i != j) {
                // Transposed
                rowTotal += mat2.get(j, i);
            }
        }
        mat2.set(i, i, -rowTotal);
    }
    // Add row for sum-to-one constraint
    for (int i = 0; i < numBaseModel; ++i) {
        mat2.set(numBaseModel, i, 1.0);
    }
    LUDecomposition decomp = new LUDecomposition(mat2);
    DoubleMatrix2D x = new DenseDoubleMatrix2D(numBaseModel + 1, 1);
    x.set(numBaseModel, 0, 1.0);
    DoubleMatrix2D y = decomp.solve(x);
    for (int i = 0; i < numBaseModel; ++i) {
        statDistr[i] = y.get(i, 0);
    }
//System.err.println(new Vector(statDistr));              
}
Also used : DenseDoubleMatrix2D(cern.colt.matrix.impl.DenseDoubleMatrix2D) DoubleMatrix2D(cern.colt.matrix.DoubleMatrix2D) LUDecomposition(cern.colt.matrix.linalg.LUDecomposition) DenseDoubleMatrix2D(cern.colt.matrix.impl.DenseDoubleMatrix2D)

Aggregations

DoubleMatrix2D (cern.colt.matrix.DoubleMatrix2D)136 DenseDoubleMatrix2D (cern.colt.matrix.impl.DenseDoubleMatrix2D)38 DoubleMatrix1D (cern.colt.matrix.DoubleMatrix1D)36 Algebra (cern.colt.matrix.linalg.Algebra)16 DoubleFactory2D (cern.colt.matrix.DoubleFactory2D)13 DenseDoubleMatrix1D (cern.colt.matrix.impl.DenseDoubleMatrix1D)13 Node (edu.cmu.tetrad.graph.Node)11 Graph (edu.cmu.tetrad.graph.Graph)8 Test (org.junit.Test)6 DoubleMatrixReader (ubic.basecode.io.reader.DoubleMatrixReader)6 StringMatrixReader (ubic.basecode.io.reader.StringMatrixReader)6 DataSet (edu.cmu.tetrad.data.DataSet)5 DoubleArrayList (cern.colt.list.DoubleArrayList)4 TetradMatrix (edu.cmu.tetrad.util.TetradMatrix)4 DenseDoubleMatrix (ubic.basecode.dataStructure.matrix.DenseDoubleMatrix)4 AbstractFormatter (cern.colt.matrix.impl.AbstractFormatter)3 Endpoint (edu.cmu.tetrad.graph.Endpoint)3 ExpressionDataDoubleMatrix (ubic.gemma.core.datastructure.matrix.ExpressionDataDoubleMatrix)3 BioMaterial (ubic.gemma.model.expression.biomaterial.BioMaterial)3 CompositeSequence (ubic.gemma.model.expression.designElement.CompositeSequence)3