private void printTree(FeatureTree tree) { minEntropy = Double.POSITIVE_INFINITY; maxEntropy = Double.NEGATIVE_INFINITY; for (TreeNode node : tree.getNodes()) { // if node matrix contains only zeros, don't print if (node.getCost() != 0) { node.setNodeIdentityNumber(nodeCounter); nodeCounter += 1; double nodeEntropy = computeEntropy(node.getCountMatrix()); if (nodeEntropy > maxEntropy) { maxEntropy = nodeEntropy; } if (nodeEntropy < minEntropy) { minEntropy = nodeEntropy; } } } for (TreeNode node : tree.getNodes()) { try { if (node.getCost() != 0) { fw.write(getEdgeString(tree, node)); fw.write(getNodeString(tree, node)); } } catch (IOException ex) { Logger.getLogger(TreeGraphPrinter.class.getName()).log(Level.SEVERE, null, ex); } } }
private String getMatrixString(FeatureTree tree, TreeNode node) { StringBuilder sb = new StringBuilder(); int[][] matrix = node.getCountMatrix(); if (tree.getClass() != JointlyCodingFeatureTree.class) { return Arrays.toString(matrix[0]); } else { int counter = 0; sb.append(" "); for (char c : tree.getColumnLabels()) { sb.append(c).append(" "); } sb.append("\\n"); for (int[] row : matrix) { sb.append(tree.getRowLabels().get(counter)).append(" "); sb.append(Arrays.toString(row)).append("\\n"); counter++; } // sb.deleteCharAt(sb.length()-1); } return sb.toString(); }
private String getNodeString(FeatureTree tree, TreeNode parentNode) { StringBuilder sb = new StringBuilder(); // each node line begins with this sb.append("# _attributes "); String language = tree.getLanguage(); // the default name of the node; here the name of the feature sb.append(getNodeTypeAndDatabaseIdString(tree, parentNode)); if (parentNode.isRootNode()) { // add some extra attributes for root node sb.append(getAttributeString(NodeLabels.TOTAL_COST_OF_TREE, tree.getTotalTreeCost())); sb.append(getAttributeString(NodeLabels.MODEL_COST_OF_TREE, tree.getModelCost())); sb.append(getAttributeString(NodeLabels.DATA_COST_OF_TREE, tree.getDataCost())); sb.append(getAttributeString(NodeLabels.FEATURE_NAME, tree.getFeatureName())); sb.append( getAttributeString( NodeLabels.TREE_LEVEL, tree.getBabyTreeType().toString() + ":" + language)); sb.append(getAttributeString(NodeLabels.TREE_TYPE, tree.getTreeType())); sb.append(getAttributeString(NodeLabels.COLUMN_LABELS, tree.getColumnLabels())); if (tree.getClass() == JointlyCodingFeatureTree.class) { sb.append(getAttributeString(NodeLabels.ROW_LABELS, tree.getRowLabels())); sb.append(getAttributeString(NodeLabels.ALIGNMENT_TYPE, tree.getAlignmentType())); } if (tree.getBabyTreeType() != BabyTreeType.JOINT) { sb.append(getAttributeString(NodeLabels.POS, getRootNodePositionString(tree, parentNode))); } sb.append("queryset=root "); } else { // not root node sb.append( getAttributeString( NodeLabels.FILL, getColor(minEntropy, maxEntropy, computeEntropy(parentNode.getCountMatrix())))); } // default label, if not defined, the getNodeDatabaseIdString is shown in nodes instead sb.append(getAttributeString(NodeLabels.LABEL, getMatrixString(tree, parentNode))); // attributes that all nodes share sb.append(getAttributeString(NodeLabels.MATRIX_COST, parentNode.getCost())); sb.append(getAttributeString(NodeLabels.MATRIX, getMatrixString(tree, parentNode))); sb.append( getAttributeString( NodeLabels.FEATURE_VALUE_IN_PREVIOUS_SPLIT, parentNode.getValueOfCandidateFeature())); sb.append( getAttributeString(NodeLabels.NUMBER_OF_CANDIDATES, parentNode.getCandidates().size())); sb.append(getAttributeString(NodeLabels.ENTROPY, computeEntropy(parentNode.getCountMatrix()))); // inner nodes have been split if (parentNode.getAppliedCandidate() != null) { sb.append(getAttributeString(NodeLabels.BEST_CANDIDATE, parentNode.getAppliedCandidate())); sb.append( getAttributeString( NodeLabels.BEST_CANDIDATE_COST, parentNode.getAppliedCandidate().getCost())); } sb.append("\n"); return sb.toString(); }