Skip to content

Commit 09477a9

Browse files
author
Zan Vrabic
committed
Two-Key plot color map fixes and tests
1 parent c9a5184 commit 09477a9

File tree

2 files changed

+57
-6
lines changed

2 files changed

+57
-6
lines changed

niaarm/visualize.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ def prepare_data(rules, metrics):
493493
return data_frame
494494

495495
# Check if one or more rules
496-
if not hasattr(rules, "data"):
496+
if not hasattr(rules, "data") and not isinstance(rules, list):
497497
rules = [rules]
498498

499499
# Prepare the data
@@ -528,16 +528,21 @@ def prepare_data(rules, metrics):
528528

529529
# Map each order to a unique color
530530
unique_orders = sorted(df["order"].unique())
531-
color_map = plt.cm.get_cmap("tab10", len(unique_orders))
532-
color_mapping = {order: color_map(i) for i, order in enumerate(unique_orders)}
531+
color_map = plt.colormaps.get_cmap("Set1")
532+
color_indices = np.linspace(0, 1, len(unique_orders))
533+
colors = [color_map(i) for i in color_indices]
534+
color_mapping = {order: colors[i] for i, order in enumerate(unique_orders)}
533535

534536
# Plot each order separately for discrete colors
535537
for order in unique_orders:
536538
subset = df[df["order"] == order]
539+
x_data = np.array(subset[metrics[0]].tolist())
540+
y_data = np.array(subset[metrics[1]].tolist())
541+
537542
plt.scatter(
538-
subset[metrics[0]],
539-
subset[metrics[1]],
540-
label=f"Order {order}",
543+
x_data,
544+
y_data,
545+
label=order,
541546
color=color_mapping[order],
542547
alpha=0.7
543548
)

tests/test_two_key_plot.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from unittest import TestCase
2+
import matplotlib.pyplot as plt
3+
import pandas as pd
4+
from niaarm.visualize import two_key_plot
5+
6+
class Rule:
7+
def __init__(self, antecedent, consequent, support, confidence):
8+
self.antecedent = antecedent
9+
self.consequent = consequent
10+
self.support = support
11+
self.confidence = confidence
12+
13+
def __repr__(self):
14+
return f"Rule({self.antecedent} -> {self.consequent})"
15+
16+
class TestTwoKeyPlot(TestCase):
17+
18+
@classmethod
19+
def setUpClass(cls):
20+
cls.rule1 = Rule(antecedent=["A", "B"], consequent=["C"], support=0.3, confidence=0.8)
21+
cls.rule2 = Rule(antecedent=["D"], consequent=["E", "F"], support=0.5, confidence=0.7)
22+
cls.rule3 = Rule(antecedent=["G", "H"], consequent=["I"], support=0.2, confidence=0.9)
23+
24+
cls.rules = [cls.rule1, cls.rule2, cls.rule3] # Ensure rules are available to all tests
25+
26+
def test_two_key_plot(self):
27+
metrics = ("support", "confidence")
28+
29+
plot = two_key_plot(self.rules, metrics, interactive=False)
30+
31+
# Verify that the return type is Matplotlib's pyplot
32+
self.assertIs(plot, plt)
33+
34+
# Ensure a figure is created
35+
self.assertTrue(plt.gcf().axes, "No axes found in the generated plot.")
36+
37+
def test_invalid_metrics(self):
38+
with self.assertRaises(ValueError):
39+
two_key_plot(self.rules, ("support",), interactive=False)
40+
41+
def test_interactive_plot(self):
42+
metrics = ("support", "confidence")
43+
fig = two_key_plot(self.rules, metrics, interactive=True)
44+
45+
# Verify that a Plotly figure is returned
46+
self.assertEqual(fig.__class__.__name__, "Figure", "Expected a Plotly figure but got a different type.")

0 commit comments

Comments
 (0)