Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Digraph Graphics #224

Merged
merged 2 commits into from
Dec 31, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions dtreeviz/trees.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ def class_leaf_node(node, label_fontsize: int = 12):
return f'leaf{node.id} [margin="0" shape=box penwidth="0" color="{colors["text"]}" label=<{html}>]'

def node_label(node):
return f'<tr><td CELLPADDING="0" CELLSPACING="0"><font face="Helvetica" color="{colors["node_label"]}" point-size="14"><i>Node {node.id}</i></font></td></tr>'
return f'<tr><td CELLPADDING="0" CELLSPACING="0"><font face="{fontname}" color="{colors["node_label"]}" point-size="14"><i>Node {node.id}</i></font></td></tr>'

def class_legend_html():
return f"""
Expand Down Expand Up @@ -389,7 +389,7 @@ def instance_html(path, instance_fontsize: int = 11):
else:
color = colors['text']
headers.append(f'<td cellpadding="1" align="right" bgcolor="white">'
f'<font face="Helvetica" color="{color}" point-size="{instance_fontsize}">'
f'<font face="{fontname}" color="{color}" point-size="{instance_fontsize}">'
f'{name}'
'</font>'
'</td>')
Expand All @@ -405,7 +405,7 @@ def instance_html(path, instance_fontsize: int = 11):
else:
disp_v = myround(v, precision)
values.append(f'<td cellpadding="1" align="right" bgcolor="white">'
f'<font face="Helvetica" color="{color}" point-size="{instance_fontsize}">{disp_v}</font>'
f'<font face="{fontname}" color="{color}" point-size="{instance_fontsize}">{disp_v}</font>'
'</td>')

if instance_orientation == "TD":
Expand All @@ -432,17 +432,17 @@ def instance_gr():
path = self.shadow_tree.predict_path(x)
leaf = f"leaf{path[-1].id}"
if self.shadow_tree.is_classifier():
edge_label = f" &#160;Prediction<br/> {path[-1].prediction_name()}"
edge_label = f" Prediction<br/> {path[-1].prediction_name()}"
else:
edge_label = f" &#160;Prediction<br/> {myround(path[-1].prediction(), precision)}"
edge_label = f" Prediction<br/> {myround(path[-1].prediction(), precision)}"
return f"""
subgraph cluster_instance {{
style=invis;
X_y [penwidth="0.3" margin="0" shape=box margin="0.03" width=.1, height=.1 label=<
{instance_html(path)}
>]
}}
{leaf} -> X_y [dir=back; penwidth="1.2" color="{colors['highlight']}" label=<<font face="Helvetica" color="{colors['leaf_label']}" point-size="{11}">{edge_label}</font>>]
{leaf} -> X_y [dir=back; penwidth="1.2" color="{colors['highlight']}" label=<<font face="{fontname}" color="{colors['leaf_label']}" point-size="{11}">{edge_label}</font>>]
"""

def get_internal_nodes():
Expand Down Expand Up @@ -585,10 +585,10 @@ def get_leaves():

# TODO do we need show_edge_labels ?
show_edge_labels = False
all_llabel = '&lt;' if show_edge_labels else ''
all_rlabel = '&ge;' if show_edge_labels else ''
root_llabel = self.shadow_tree.get_root_edge_labels()[0] if show_root_edge_labels else ''
root_rlabel = self.shadow_tree.get_root_edge_labels()[1] if show_root_edge_labels else ''
all_llabel = ' &lt;' if show_edge_labels else ''
parrt marked this conversation as resolved.
Show resolved Hide resolved
all_rlabel = ' &ge;' if show_edge_labels else ''
root_llabel = f' {self.shadow_tree.get_root_edge_labels()[0]}' if show_root_edge_labels else ''
root_rlabel = f' {self.shadow_tree.get_root_edge_labels()[1]}' if show_root_edge_labels else ''

edges = []
# non leaf edges with > and <=
Expand Down Expand Up @@ -629,12 +629,12 @@ def get_leaves():

if show_just_path:
if node.left.id in highlight_path:
edges.append(f'{nname} -> {left_node_name} [penwidth={lpw} color="{lcolor}" label=<{llabel}> fontcolor="{colors["text"]}"]')
edges.append(f'{nname} -> {left_node_name} [penwidth={lpw} fontname="{fontname}" color="{lcolor}" label=<{llabel}> fontcolor="{colors["text"]}"]')
if node.right.id in highlight_path:
edges.append(f'{nname} -> {right_node_name} [penwidth={rpw} color="{rcolor}" label=<{rlabel}> fontcolor="{colors["text"]}"]')
edges.append(f'{nname} -> {right_node_name} [penwidth={rpw} fontname="{fontname}" color="{rcolor}" label=<{rlabel}> fontcolor="{colors["text"]}"]')
else:
edges.append(f'{nname} -> {left_node_name} [penwidth={lpw} color="{lcolor}" label=<{llabel}> fontcolor="{colors["text"]}"]')
edges.append(f'{nname} -> {right_node_name} [penwidth={rpw} color="{rcolor}" label=<{rlabel}> fontcolor="{colors["text"]}"]')
edges.append(f'{nname} -> {left_node_name} [penwidth={lpw} fontname="{fontname}" color="{lcolor}" label=<{llabel}> fontcolor="{colors["text"]}"]')
parrt marked this conversation as resolved.
Show resolved Hide resolved
edges.append(f'{nname} -> {right_node_name} [penwidth={rpw} fontname="{fontname}" color="{rcolor}" label=<{rlabel}> fontcolor="{colors["text"]}"]')
edges.append(f"""
{{
rank=same;
Expand Down