Exemplo n.º 1
0
  public void build() {
    JMenu fileMenu = new JMenu("File");
    fileMenu.setMnemonic('F');

    saveEdgelistAction.setParent(this);
    JMenuItem saveEdgelist = new JMenuItem(saveEdgelistAction);
    fileMenu.add(saveEdgelist);

    saveAdjAction.setParent(this);
    fileMenu.add(new JMenuItem(saveAdjAction));

    saveAlterAttributesCSVAction.setParent(this);
    fileMenu.add(new JMenuItem(saveAlterAttributesCSVAction));

    saveConsensusAction.setParent(this);
    fileMenu.add(new JMenuItem(saveConsensusAction));

    JMenuBar mb = new JMenuBar();
    mb.add(fileMenu);
    setJMenuBar(mb);

    SparseGraph<WholeNetworkAlter, WholeNetworkTie> graph =
        new SparseGraph<WholeNetworkAlter, WholeNetworkTie>();

    for (WholeNetworkAlter alter : net.getWholeNetworkAlters().values()) {
      graph.addVertex(alter);
      logger.info("Adding vertex " + alter);
    }

    for (WholeNetworkTie tie : net.getWholeNetworkTies()) {
      graph.addEdge(tie, tie.getA(), tie.getB());
      logger.info("Adding edge " + tie);
    }

    Layout<WholeNetworkAlter, WholeNetworkTie> layout =
        new ELSFRLayout2<WholeNetworkAlter, WholeNetworkTie>(graph);
    VisualizationViewer<WholeNetworkAlter, WholeNetworkTie> vv =
        new VisualizationViewer<WholeNetworkAlter, WholeNetworkTie>(layout);
    vv.setGraphMouse(new DefaultModalGraphMouse());
    vv.setPickSupport(new ShapePickSupport<WholeNetworkAlter, WholeNetworkTie>(vv));
    vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<WholeNetworkAlter>());
    vv.getRenderContext()
        .setEdgeShapeTransformer(new EdgeShape.Line<WholeNetworkAlter, WholeNetworkTie>());

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel = new JPanel(new BorderLayout());
    panel.add(vv, BorderLayout.CENTER);

    setContentPane(panel);
    pack();
  }
Exemplo n.º 2
0
  public void doCombineInterviews() throws Exception {
    /* Read new study */
    EgoStore store = new EgoStore(null);
    File studyFile = store.selectStudy(new File("."));

    StudyReader sr = new StudyReader(studyFile);
    Study study = sr.getStudy();

    // Find the interview files associated with this study
    File parentFile = studyFile.getParentFile();
    File interviewFile = new File(parentFile, "/Interviews/");

    File guessLocation = new File(".");
    if (parentFile.exists() && parentFile.isDirectory() && parentFile.canRead())
      guessLocation = parentFile;

    if (interviewFile.exists() && interviewFile.isDirectory() && interviewFile.canRead())
      guessLocation = interviewFile;

    final File currentDirectory = guessLocation;

    String[] fileList = currentDirectory.list();
    InterviewFileFilter filter = new InterviewFileFilter(study, "Interview Files", "int");
    ArrayList<String> alterList = new ArrayList<String>();
    int[][] adj = null;

    Set<Edge> allPairs = new HashSet<Edge>();
    Set<Vertex> pairedAlters = new HashSet<Vertex>();

    for (String s : fileList) {

      File f = new File(currentDirectory.toString() + "/" + s);
      if (!filter.accept(f) || !f.canRead())
        throw new IOException("Couldn't read file or file not associated with selected study.");

      InterviewReader interviewReader = new InterviewReader(study, f);
      Interview interview = interviewReader.getInterview();
      if (!interview.isComplete()) {
        logger.info("*** SKIPPED because interview isn't complete: " + f.getName());
        continue;
      }

      logger.info("** Reading next file " + f.getName());

      String[] thisInterviewAlterlist = interview.getAlterList();
      alterList.addAll(Arrays.asList(interview.getAlterList()));

      Iterator<Long> questions = study.getQuestionOrder(AlterPairQuestion.class).iterator();
      while (questions.hasNext()) {
        Question q = study.getQuestion((Long) questions.next());
        adj = interview.generateAdjacencyMatrix(q, false);

        // loop through adj
        // if adj[i][j] == 1, thisInterviewAlters[i] && thisInterviewAlters[j] are adjacent in final
        // matrix

        for (int i = 0; i < adj.length; i++) {
          for (int j = 0; j < adj[i].length; j++) {
            if (adj[i][j] == 1 && i != j) {

              String alter1 = thisInterviewAlterlist[i];
              String alter2 = thisInterviewAlterlist[j];

              allPairs.add(new Edge(alter1, alter2));

              pairedAlters.add(new Vertex(alter1));
              pairedAlters.add(new Vertex(alter2));

              // mark those as adjacent in the new big matrix
              // logger.info(p +  " are adjacent");
            }
          }
        }
      }
    }

    Set<Vertex> vertices = new HashSet<Vertex>();
    for (Edge pair : allPairs) {
      Vertex v1 = new Vertex(pair.pair.getFirst());
      Vertex v2 = new Vertex(pair.pair.getSecond());
      vertices.add(v1);
      vertices.add(v2);
    }

    for (String isolate : alterList) {
      Vertex v = new Vertex(isolate);
      vertices.add(v);
    }

    SparseGraph<Vertex, Edge> graph = new SparseGraph<Vertex, Edge>();
    for (Edge pair : allPairs) {
      Vertex v1 = new Vertex(pair.pair.getFirst());
      Vertex v2 = new Vertex(pair.pair.getSecond());

      if (!graph.getVertices().contains(v1)) graph.addVertex(v1);
      if (!graph.getVertices().contains(v2)) graph.addVertex(v2);

      graph.addEdge(pair, Arrays.asList(v1, v2));
    }

    for (String isolate : alterList) {
      Vertex v = new Vertex(isolate);
      if (!graph.getVertices().contains(v)) graph.addVertex(v);
    }

    Layout<Vertex, Edge> layout = new ELSFRLayout2<Vertex, Edge>(graph);
    VisualizationViewer<Vertex, Edge> vv = new VisualizationViewer<Vertex, Edge>(layout);

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel = new JPanel(new BorderLayout());
    panel.add(vv, BorderLayout.CENTER);

    frame.setContentPane(panel);
    frame.pack();

    frame.setVisible(true);

    // TODO: how do isolates exist from combining personal networks into a whole network?
    logger.info("Pairs: " + allPairs);
    alterList.removeAll(pairedAlters);
    logger.info("Single alters: " + alterList);

    // TODO: write to file using save dialog
  }