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

Make TreeAdapters for Pythagorean trees more general #1651

Merged
merged 2 commits into from
Oct 14, 2016
Merged
Show file tree
Hide file tree
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
10 changes: 6 additions & 4 deletions Orange/widgets/visualize/owpythagorastree.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ def commit(self):
self.scene.selectedItems())

data = self.tree_adapter.get_instances_in_nodes(
self.clf_dataset, [item.tree_node for item in items])
self.clf_dataset, [item.tree_node.label for item in items])
self.send('Selected Data', data)

def send_report(self):
Expand Down Expand Up @@ -532,7 +532,8 @@ def _color_class_mean(self, adapter, tree_node):
# calculate node colors relative to the mean of the node samples
min_mean = np.min(self.clf_dataset.Y)
max_mean = np.max(self.clf_dataset.Y)
instances = adapter.get_instances_in_nodes(self.clf_dataset, tree_node)
instances = adapter.get_instances_in_nodes(self.clf_dataset,
tree_node.label)
mean = np.mean(instances.Y)

return self.color_palette[(mean - min_mean) / (max_mean - min_mean)]
Expand All @@ -541,7 +542,8 @@ def _color_stddev(self, adapter, tree_node):
# calculate node colors relative to the standard deviation in the node
# samples
min_mean, max_mean = 0, np.std(self.clf_dataset.Y)
instances = adapter.get_instances_in_nodes(self.clf_dataset, tree_node)
instances = adapter.get_instances_in_nodes(self.clf_dataset,
tree_node.label)
std = np.std(instances.Y)

return self.color_palette[(std - min_mean) / (max_mean - min_mean)]
Expand All @@ -553,7 +555,7 @@ def _regression_get_tooltip(self, node):
ratio = samples / total

instances = self.tree_adapter.get_instances_in_nodes(
self.clf_dataset, node)
self.clf_dataset, node.label)
mean = np.mean(instances.Y)
std = np.std(instances.Y)

Expand Down
6 changes: 4 additions & 2 deletions Orange/widgets/visualize/owpythagoreanforest.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,8 @@ def _color_class_mean(self, adapter, tree_node):
# calculate node colors relative to the mean of the node samples
min_mean = np.min(self.clf_dataset.Y)
max_mean = np.max(self.clf_dataset.Y)
instances = adapter.get_instances_in_nodes(self.clf_dataset, tree_node)
instances = adapter.get_instances_in_nodes(self.clf_dataset,
tree_node.label)
mean = np.mean(instances.Y)

return self.color_palette[(mean - min_mean) / (max_mean - min_mean)]
Expand All @@ -384,7 +385,8 @@ def _color_stddev(self, adapter, tree_node):
# calculate node colors relative to the standard deviation in the node
# samples
min_mean, max_mean = 0, np.std(self.clf_dataset.Y)
instances = adapter.get_instances_in_nodes(self.clf_dataset, tree_node)
instances = adapter.get_instances_in_nodes(self.clf_dataset,
tree_node.label)
std = np.std(instances.Y)

return self.color_palette[(std - min_mean) / (max_mean - min_mean)]
Expand Down
6 changes: 4 additions & 2 deletions Orange/widgets/visualize/pythagorastreeviewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import Qt

from Orange.widgets.visualize.utils.tree.treeadapter import TreeAdapter

# z index range, increase if needed
Z_STEP = 5000000

Expand Down Expand Up @@ -384,7 +386,7 @@ def __init__(self, tree_node, parent=None, **kwargs):
self.z_step = Z_STEP

# calculate the correct z values based on the parent
if self.tree_node.parent != -1:
if self.tree_node.parent != TreeAdapter.ROOT_PARENT:
p = self.tree_node.parent
# override root z step
num_children = len(p.children)
Expand Down Expand Up @@ -503,7 +505,7 @@ def _propagate_to_children(self, graphics_item, fnc):

def _propagate_to_parents(self, graphics_item, fnc, other_fnc):
# propagate function that handles graphics item to appropriate parents
if graphics_item.tree_node.parent != -1:
if graphics_item.tree_node.parent != TreeAdapter.ROOT_PARENT:
parent = graphics_item.tree_node.parent.graphics_item
# handle the non relevant children nodes
for c in parent.tree_node.children:
Expand Down
3 changes: 1 addition & 2 deletions Orange/widgets/visualize/utils/tree/skltreeadapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class SklTreeAdapter(TreeAdapter):

"""

ROOT_PARENT = -1
NO_CHILD = -1
FEATURE_UNDEFINED = -2

Expand Down Expand Up @@ -279,7 +278,7 @@ def get_instances_in_nodes(self, dataset, nodes):
if not isinstance(nodes, (list, tuple)):
nodes = [nodes]

node_leaves = [self.leaves(n.label) for n in nodes]
node_leaves = [self.leaves(n) for n in nodes]
if len(node_leaves) > 0:
# get the leaves of the selected tree node
node_leaves = np.unique(np.hstack(node_leaves))
Expand Down
6 changes: 3 additions & 3 deletions Orange/widgets/visualize/utils/tree/treeadapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class TreeAdapter(metaclass=ABCMeta):

"""

ROOT_PARENT = -1
ROOT_PARENT = None
NO_CHILD = -1
FEATURE_UNDEFINED = -2

Expand Down Expand Up @@ -52,7 +52,7 @@ def num_samples(self, node):

@abstractmethod
def parent(self, node):
"""Get the parent of a given node. Return -1 if the node is the root.
"""Get the parent of a given node or ROOT_PARENT if the node is the root.

Parameters
----------
Expand Down Expand Up @@ -212,7 +212,7 @@ def get_instances_in_nodes(self, dataset, nodes):
----------
dataset : Table
A Orange Table dataset.
nodes : iterable[TreeNode]
nodes : iterable[node]
A list of tree nodes for which we want the instances.

Returns
Expand Down