Search in sources :

Example 86 with ChoiceGenerator

use of edu.cmu.tetrad.util.ChoiceGenerator in project tetrad by cmu-phil.

the class GraphUtils method getAmbiguousTriplesFromGraph.

/**
 * @return A list of triples of the form <X, Y, Z>, where <X, Y, Z> is a
 * definite noncollider in the given graph.
 */
public static List<Triple> getAmbiguousTriplesFromGraph(Node node, Graph graph) {
    List<Triple> ambiguousTriples = new ArrayList<>();
    List<Node> adj = graph.getAdjacentNodes(node);
    if (adj.size() < 2) {
        return new LinkedList<>();
    }
    ChoiceGenerator gen = new ChoiceGenerator(adj.size(), 2);
    int[] choice;
    while ((choice = gen.next()) != null) {
        Node x = adj.get(choice[0]);
        Node z = adj.get(choice[1]);
        if (graph.isAmbiguousTriple(x, node, z)) {
            ambiguousTriples.add(new Triple(x, node, z));
        }
    }
    return ambiguousTriples;
}
Also used : ArrayList(java.util.ArrayList) ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator) LinkedList(java.util.LinkedList)

Example 87 with ChoiceGenerator

use of edu.cmu.tetrad.util.ChoiceGenerator in project tetrad by cmu-phil.

the class GraphUtils method getCollidersFromGraph.

/**
 * @return A list of triples of the form X*->Y<-*Z.
 */
public static List<Triple> getCollidersFromGraph(Node node, Graph graph) {
    List<Triple> colliders = new ArrayList<>();
    List<Node> adj = graph.getAdjacentNodes(node);
    if (adj.size() < 2) {
        return new LinkedList<>();
    }
    ChoiceGenerator gen = new ChoiceGenerator(adj.size(), 2);
    int[] choice;
    while ((choice = gen.next()) != null) {
        Node x = adj.get(choice[0]);
        Node z = adj.get(choice[1]);
        Endpoint endpt1 = graph.getEdge(x, node).getProximalEndpoint(node);
        Endpoint endpt2 = graph.getEdge(z, node).getProximalEndpoint(node);
        if (endpt1 == Endpoint.ARROW && endpt2 == Endpoint.ARROW) {
            colliders.add(new Triple(x, node, z));
        }
    }
    return colliders;
}
Also used : ArrayList(java.util.ArrayList) ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator) LinkedList(java.util.LinkedList)

Example 88 with ChoiceGenerator

use of edu.cmu.tetrad.util.ChoiceGenerator in project tetrad by cmu-phil.

the class CcdMax method maxPSepset.

private Pair maxPSepset(Node i, Node k, Graph graph) {
    double _p = Double.POSITIVE_INFINITY;
    List<Node> _v = null;
    List<Node> adji = graph.getAdjacentNodes(i);
    List<Node> adjk = graph.getAdjacentNodes(k);
    adji.remove(k);
    adjk.remove(i);
    for (int d = 0; d <= Math.min((depth == -1 ? 1000 : depth), Math.max(adji.size(), adjk.size())); d++) {
        if (d <= adji.size()) {
            ChoiceGenerator gen = new ChoiceGenerator(adji.size(), d);
            int[] choice;
            while ((choice = gen.next()) != null) {
                List<Node> v2 = GraphUtils.asList(choice, adji);
                if (isForbidden(i, k, v2))
                    continue;
                try {
                    getIndependenceTest().isIndependent(i, k, v2);
                    double p2 = getIndependenceTest().getScore();
                    if (p2 < _p) {
                        _p = p2;
                        _v = v2;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    return new Pair(null, Double.POSITIVE_INFINITY);
                }
            }
        }
        if (d <= adjk.size()) {
            ChoiceGenerator gen = new ChoiceGenerator(adjk.size(), d);
            int[] choice;
            while ((choice = gen.next()) != null) {
                List<Node> v2 = GraphUtils.asList(choice, adjk);
                try {
                    getIndependenceTest().isIndependent(i, k, v2);
                    double p2 = getIndependenceTest().getScore();
                    if (p2 < _p) {
                        _p = p2;
                        _v = v2;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    return new Pair(null, Double.POSITIVE_INFINITY);
                }
            }
        }
    }
    return new Pair(_v, _p);
}
Also used : ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator)

Example 89 with ChoiceGenerator

use of edu.cmu.tetrad.util.ChoiceGenerator in project tetrad by cmu-phil.

the class LTestQnet3 method main.

public static void main(String[] argv) {
    String fileName = argv[0];
    InputStream s;
    StringTokenizer st;
    int ngenes = 5;
    int ntimes = 400;
    int nrecords = 5;
    int nchips = 4;
    // double[] chisq = {3.84, 5.99, 7.81, 9.49, 11.1, 12.6, 14.1, 15.5,
    // 16.9, 18.3, 19.7, 21.0, 22.4, 23.7, 25.0, 26.3,
    // 27.6, 28.9, 30.1, 31.4, 32.7, 33.9, 35.2, 36.4,
    // 37.7, 38.9, 40.1, 41.3, 42.6, 43.8};
    double[][] cases = new double[4][2004];
    try {
        s = new FileInputStream(fileName);
    } catch (IOException e) {
        System.out.println("Cannot open file " + fileName);
        return;
    }
    // DataInputStream in = new DataInputStream(s);
    BufferedReader in = new BufferedReader(new InputStreamReader(s));
    for (int k = 0; k < nrecords; k++) {
        try {
            st = new StringTokenizer(in.readLine());
            if (k == 0) {
                continue;
            }
            // int ichip = Integer.parseInt(st.nextToken("\t"));
            for (int j = 0; j < ntimes * ngenes; j++) {
                cases[k - 1][j] = Double.parseDouble(st.nextToken("\t"));
            }
        } catch (IOException e) {
            System.out.println("Read error in " + fileName);
            return;
        }
    }
    // System.out.println("Read " + cases[0][0] + " " + cases[1][0] + " " +
    // cases[2][0] + " " + cases[3][0]);
    double[][] gene = new double[ntimes][ngenes];
    double[][] deriv = new double[ntimes][ngenes];
    double[] sum = new double[ngenes];
    for (int j = 0; j < ntimes; j++) {
        for (int g = 0; g < ngenes; g++) {
            int icol = j * ngenes + g;
            sum[g] = 0.0;
            for (int c = 0; c < nchips; c++) // sum[g] += cases[c][icol]*cases[c][icol];
            {
                sum[g] += cases[c][icol];
            }
            gene[j][g] = sum[g];
            if (j != 0) {
                deriv[j][g] = (gene[j][g] - gene[j - 1][g]) / 10.0;
            }
        }
    /*
            if(j != 0) {
              System.out.println();
              for(int k = 0; k < ngenes; k++)
                System.out.print(deriv[j][k] + " ");
              System.out.println();
            }
            */
    }
    NumberFormat nf = NumberFormatUtil.getInstance().getNumberFormat();
    for (int g = 0; g < ngenes; g++) {
        System.out.println("For gene " + g);
        int k = 5;
        ChoiceGenerator cg = new ChoiceGenerator(ngenes, k);
        int[] regs = new int[k];
        while ((regs = cg.next()) != null) {
            System.out.println("Sets of " + k + " regulators are:");
            System.out.println(regs[0] + " " + regs[1] + " " + regs[2] + " " + regs[3] + " " + regs[4]);
            for (int t = 1; t < ntimes; t++) {
                String g0 = nf.format(gene[t][regs[0]]);
                String g1 = nf.format(gene[t][regs[1]]);
                String g2 = nf.format(gene[t][regs[2]]);
                String g3 = nf.format(gene[t][regs[3]]);
                String g4 = nf.format(gene[t][regs[4]]);
                if (deriv[t][g] > 0.3) {
                    System.out.println(g0 + "a0+" + g1 + "a1+" + g2 + "a2+" + g3 + "a3+" + g4 + "a4+b > 0");
                } else if (deriv[t][g] < -0.3) {
                    System.out.println(g0 + "a0+" + g1 + "a1+" + g2 + "a2+" + g3 + "a3+" + g4 + "a4+b< 0");
                }
            }
        }
    }
/*
        double[] p = new double[ngenes];
        for(int g = 0; g < ngenes; g++) {
          for(int j = 0; j < ntimes; j++)
            if(gene[j][g] > 0) p[g]++;
          p[g] /= ntimes;
          //System.out.println(" gene " + g + " p = " + p[g]);
        }
        */
}
Also used : StringTokenizer(java.util.StringTokenizer) ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator) NumberFormat(java.text.NumberFormat)

Example 90 with ChoiceGenerator

use of edu.cmu.tetrad.util.ChoiceGenerator in project tetrad by cmu-phil.

the class ItkPredictorSearch method predictor.

public void predictor(int gene) {
    SortedSet[][] S = new TreeSet[nrows][nrows];
    Gene[] G = new Gene[ngenes];
    System.out.println("For gene " + names[gene] + ":");
    for (int i = 0; i < nrows; i++) {
        for (int j = 0; j < nrows; j++) {
            S[i][j] = new TreeSet();
        }
    }
    for (int k = 0; k < ngenes; k++) {
        G[k] = new Gene(k);
    }
    // Consider all pairs of rows of the expression matrix.
    ChoiceGenerator cg = new ChoiceGenerator(ngenes, 2);
    int[] rows;
    while ((rows = cg.next()) != null) {
        // Exclude row pairs in which the given gene was perturbed.
        if (expression[rows[0]][gene] == -1 || expression[rows[0]][gene] == 2 || expression[rows[1]][gene] == -1 || expression[rows[1]][gene] == 2) {
            continue;
        }
        // differ between the two rows (perturbations).
        if (!differByPerturbation(gene, rows[0], rows[1])) {
            continue;
        }
        // between the two perturbations.
        for (int gother = 0; gother < ngenes; gother++) {
            if (gother == gene) {
                // Don't test this gene
                continue;
            }
            // Exclude genes which do not differ between the two rows
            if (!differByPerturbation(gother, rows[0], rows[1])) {
                continue;
            }
            S[rows[0]][rows[1]].add(G[gother]);
        }
        System.out.print("sem" + rows[0] + rows[1] + " = ");
        for (Iterator it = S[rows[0]][rows[1]].iterator(); it.hasNext(); ) {
            System.out.print(((Gene) it.next()).getIndex());
        }
        System.out.println();
    }
    int sum = 0;
    for (int i = 0; i < nrows; i++) {
        for (int j = 0; j < nrows; j++) {
            sum += S[i][j].size();
        }
    }
    if (sum == 0) {
        System.out.println("Insufficient perturbations for gene " + names[gene]);
        System.out.println();
        return;
    }
    System.out.println("Smin:");
    SortedSet[] minCover;
    minCover = minCoveringSet(S);
    for (SortedSet<Gene> aMinCover : minCover) {
        display(aMinCover);
        inferFunction(gene, aMinCover);
    }
    System.out.println();
}
Also used : TreeSet(java.util.TreeSet) ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator) Iterator(java.util.Iterator) SortedSet(java.util.SortedSet)

Aggregations

ChoiceGenerator (edu.cmu.tetrad.util.ChoiceGenerator)161 Node (edu.cmu.tetrad.graph.Node)37 ArrayList (java.util.ArrayList)20 DepthChoiceGenerator (edu.cmu.tetrad.util.DepthChoiceGenerator)15 List (java.util.List)11 LinkedList (java.util.LinkedList)10 ExecutorService (java.util.concurrent.ExecutorService)5 NumberFormat (java.text.NumberFormat)2 HashSet (java.util.HashSet)2 StringTokenizer (java.util.StringTokenizer)2 Test (org.junit.Test)2 DataSet (edu.cmu.tetrad.data.DataSet)1 Triple (edu.cmu.tetrad.graph.Triple)1 DeltaSextadTest (edu.cmu.tetrad.search.DeltaSextadTest)1 IndependenceTest (edu.cmu.tetrad.search.IndependenceTest)1 IntSextad (edu.cmu.tetrad.search.IntSextad)1 SemIm (edu.cmu.tetrad.sem.SemIm)1 IndependenceResult (edu.cmu.tetradapp.model.IndependenceResult)1 Iterator (java.util.Iterator)1 SortedSet (java.util.SortedSet)1