private Graph changeLatentNames(Graph full, Clusters measurements, List<String> latentVarList) { Graph g2 = null; try { g2 = (Graph) new MarshalledObject(full).get(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } for (int i = 0; i < measurements.getNumClusters(); i++) { List<String> d = measurements.getCluster(i); String latentName = latentVarList.get(i); for (Node node : full.getNodes()) { if (!(node.getNodeType() == NodeType.LATENT)) { continue; } List<Node> _children = full.getChildren(node); _children.removeAll(ReidentifyVariables.getLatents(full)); List<String> childNames = getNames(_children); if (new HashSet<String>(childNames).equals(new HashSet<String>(d))) { g2.getNode(node.getName()).setName(latentName); } } } return g2; }
private void clusterDiscovery() throws URISyntaxException, ParserConfigurationException, SAXException, IOException { determineUri(); readClustersXml(); // Now try to join or create cluster if (clusters != null) { Clusters.Cluster clusterConfig = clusters.getCluster(config.get(ClusterSettings.cluster_name)); if (clusterConfig != null) { for (Clusters.Member member : clusterConfig.getMembers()) { URI joinUri = new URI("cluster://" + member.getHost()); if (!joinUri.equals(serverId)) { Future<ClusterConfiguration> config = cluster.join(joinUri); try { logger.logMessage("Joined cluster:" + config.get()); try { updateMyInfo(); } catch (TransformerException e) { throw new RuntimeException(e); } return; } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { logger.logMessage("Could not join cluster member " + member.getHost()); } } } } // Could not find cluster or not join nodes in cluster - create it! if (clusterConfig == null) { clusterConfig = new Clusters.Cluster(config.get(ClusterSettings.cluster_name)); clusters.getClusters().add(clusterConfig); } cluster.create(clusterConfig.getName()); if (clusterConfig.getByUri(serverId) == null) { clusterConfig.getMembers().add(new Clusters.Member(serverId.toString())); try { updateMyInfo(); } catch (TransformerException e) { logger.logMessage("Could not update cluster discovery file:" + clustersUri, e); } } } }
/** @param args the command line arguments */ public static void main(String[] args) throws IOException { String fileName = "icon_"; String ext = ".jpg"; for (int p = 1; p < 10; p++) { for (int q = 3; q < 8; q += 2) { HClust hc = new HClust(new ManhatanDistance()); BufferedImage image = ImageIO.read(new File("./images/" + fileName + p + ext)); int pix, r, g, b, k = 0; Color[] colors = new Color[image.getHeight() * image.getWidth()]; for (int i = 0; i < image.getWidth(); i++) { for (int j = 0; j < image.getHeight(); j++) { pix = image.getRGB(i, j); r = (pix >> 16) & 0xFF; g = (pix >> 8) & 0xFF; b = pix & 0xFF; colors[k++] = new Color(r, g, b); } } Clusters clust = new Clusters(colors, hc.dist, new CompleteDistance()); int nClust = q, imgSize = image.getHeight() * image.getWidth(); clust.compute(nClust); Color reps[] = new Color[nClust]; k = 0; for (int i = 0; i < clust.clusts.length; i++) { if (!clust.clusts[i].isEmpty()) { reps[k++] = hc.findRep(clust.clusts[i]); } } imgSize /= k; int c = 0, rep = 0; for (int i = 0; i < image.getWidth(); i++) { for (int j = 0; j < image.getHeight(); j++) { while (clust.clusts[c].isEmpty()) { c++; } pix = reps[rep / (imgSize + 1)].toInt(); // clust.clusts[c].get(0).toInt(); clust.clusts[c].remove(0); image.setRGB(i, j, pix); rep++; } } ImageIO.write( image, "bmp", new File("./images/out/hclust/" + fileName + p + "_hclust_m_" + q + ".bmp")); } } }
public static Clusters getClustersDetails() { Cluster cluster = new Cluster(); cluster.setDataCenter(DataCenter.DFW); cluster.setDescription("aDesc"); cluster.setName("aClusterName"); Cluster cluster2 = new Cluster(); cluster2.setDataCenter((DataCenter) rndChoice(DataCenter.values())); cluster2.setDescription("aDesc"); cluster2.setName("aSecondCluster"); Clusters clusters = new Clusters(); clusters.getClusters().add(cluster); clusters.getClusters().add(cluster2); return clusters; }
private void readClustersXml() throws SAXException, IOException { if (clustersUri.getScheme().equals("file")) { File file = new File(clustersUri); if (file.exists()) { Document doc = builder.parse(file); clusters = new ClustersXMLSerializer(builder).read(doc); clusters.setTimestamp(file.lastModified()); } } }
private void updateMyInfo() throws TransformerException, IOException, SAXException { Clusters.Cluster cluster = clusters.getCluster(config.get(ClusterSettings.cluster_name)); if (cluster == null) { clusters .getClusters() .add(cluster = new Clusters.Cluster(config.get(ClusterSettings.cluster_name))); } if (cluster.contains(serverId)) { // Do nothing } else { // Add myself to list cluster .getMembers() .add( new Clusters.Member( serverId.getHost() + (serverId.getPort() == -1 ? "" : ":" + serverId.getPort()))); Document document = new ClustersXMLSerializer(builder).write(clusters); // Save new version if (clustersUri.getScheme().equals("file")) { File clustersFile = new File(clustersUri); if (clustersFile.lastModified() != clusters.getTimestamp()) { readClustersXml(); // Re-read XML file updateMyInfo(); // Try again return; } // Save new version transformer.transform(new DOMSource(document), new StreamResult(clustersFile)); clusters.setTimestamp(clustersFile.lastModified()); } else { // TODO Implement HTTP version } } }