@SuppressWarnings("unchecked") public void addNode(int id, String label, Map attributes) { Node node = this.networkInfo.findNodeByOriginalID(id); Map annotatedAttributes = new HashMap(); annotatedAttributes.putAll(attributes); if (node != null) { ArrayList nodeCommunities = node.getCommunities(); for (int ii = 0; ii < nodeCommunities.size(); ii++) { String communityLevelAttributeName = BASE_COMMUNITY_LEVEL_ATTRIBUTE_NAME + ii; String communityName = BASE_COMMUNITY_LABEL + nodeCommunities.get(ii); annotatedAttributes.put(communityLevelAttributeName, communityName); } } else { // Isolate nodes would not have been added to our nodes list. for (int ii = 0; ii < this.networkInfo.getMaxCommunityLevel(); ii++) { String communityLevelAttributeName = BASE_COMMUNITY_LEVEL_ATTRIBUTE_NAME + ii; String communityName = BASE_ISOLATE_LABEL + this.isolateCount; annotatedAttributes.put(communityLevelAttributeName, communityName); this.isolateCount++; } } super.addNode(id, label, annotatedAttributes); }
@SuppressWarnings("unchecked") // Raw LinkedHashMap public void setNodeSchema(LinkedHashMap schema) { for (int ii = 0; ii < this.networkInfo.getMaxCommunityLevel(); ii++) { String communityLevelAttributeName = BASE_COMMUNITY_LEVEL_ATTRIBUTE_NAME + ii; schema.put(communityLevelAttributeName, NWBFileProperty.TYPE_STRING); } super.setNodeSchema(schema); }
public static File writeCernColtMatrixToNWBFile(DoubleMatrix2D matrix) throws IOException { File nwbFile = NWBFileUtilities.createTemporaryNWBFile(); NWBFileWriter nwbFileWriter = new NWBFileWriter(nwbFile); double[][] doubleMatrix = matrix.toArray(); // Output node schema. nwbFileWriter.setNodeSchema(NWBFileWriter.getDefaultNodeSchema()); // Output nodes and determine whether or not the matrix is directed. Map emptyAttributes = new HashMap(); boolean hasDeterminedIfDirected = false; boolean isDirected = false; for (int ii = 0; ii < doubleMatrix.length; ii++) { int nodeID = ii + 1; nwbFileWriter.addNode(nodeID, "node" + nodeID, emptyAttributes); if (!hasDeterminedIfDirected) { for (int jj = 0; jj < doubleMatrix[ii].length; jj++) { if (doubleMatrix[ii][jj] != doubleMatrix[jj][ii]) { hasDeterminedIfDirected = true; isDirected = true; break; } } } } // Set the edge schema and output the edges. LinkedHashMap edgeSchema = NWBFileWriter.getDefaultEdgeSchema(); if (isDirected) { nwbFileWriter.setDirectedEdgeSchema(edgeSchema); // This loop will iterate over every cell in the matrix. for (int ii = 0; ii < doubleMatrix.length; ii++) { for (int jj = 0; jj < doubleMatrix[ii].length; jj++) { double weight = doubleMatrix[ii][jj]; if (weight != 0.0) { // Add a new edge. int sourceNodeID = ii + 1; int targetNodeID = jj + 1; HashMap attributes = new HashMap(); attributes.put("weight", new Double(weight)); nwbFileWriter.addDirectedEdge(sourceNodeID, targetNodeID, attributes); } } } } else { nwbFileWriter.setUndirectedEdgeSchema(edgeSchema); // This loop will only iterate over the upper half of the matrix. for (int ii = 0; ii < doubleMatrix.length; ii++) { for (int jj = ii; jj < doubleMatrix[ii].length; jj++) { double weight = doubleMatrix[ii][jj]; if (weight != 0.0) { // Add a new edge. int sourceNodeID = ii + 1; int targetNodeID = jj + 1; HashMap attributes = new HashMap(); attributes.put("weight", new Double(weight)); nwbFileWriter.addUndirectedEdge(sourceNodeID, targetNodeID, attributes); } } } } nwbFileWriter.finishedParsing(); return nwbFile; }