Commits

Marko Toplak committed 0f81153

Merge orngAssoc to Orange.associate. References #733.

Comments (0)

Files changed (2)

orange/Orange/associate/__init__.py

     >>> set(rule.match_left) - set(rule.match_both)
     set([0, 2, 8, 10, 16, 17, 18])
 
+===============
+Utilities
+===============
+
+.. autofunction:: print_rules
+
+.. autofunction:: sort
+
 """
 
-
-
-
 from orange import \
     AssociationRule, \
     AssociationRules, \
     ItemsetNodeProxy, \
     ItemsetsSparseInducer
 
+def print_rules(rules, ms = []):
+    """
+    Print the rules. If ms is left empty, only the rules are printed. If ms
+    contains rules' attributes, e.g. ``["support", "confidence"]``, these are printed out as well.
+    """
+    if ms:
+        print "\t".join([m[:4] for m in ms]) + "\trule"
+        for rule in rules:
+            print "\t".join(["%5.3f" % getattr(rule, m) for m in ms]) + "\t" + str(rule)
+    else:
+        for rule in rules:
+            print rule
 
+class __Cmp:
+    def __init__(self, ms):
+        self.ms = ms
+
+    def __call__(self, r1, r2):        
+        for m in self.ms:
+            c = -cmp(getattr(r1, m), getattr(r2, m))
+            if c:
+                return c
+        return 0
+
+def sort(rules, ms = ["support"]):
+    """
+    Sort the rules according to the given criteria. The default key is "support"; list multiple keys in a list.
+    """
+    rules.sort(__Cmp(ms))

orange/orngAssoc.py

 # Provided only for backward compatibility.
-# Don't use
 
 import orange
 
 
 AssociationRulesInduces = orange.AssociationRulesInducer
 
-def printRules(rules, ms = []):
-    if ms:
-        print "\t".join([m[:4] for m in ms]) + "\trule"
-        for rule in rules:
-            print "\t".join(["%5.3f" % getattr(rule, m) for m in ms]) + "\t" + str(rule)
-    else:
-        for rule in rules:
-            print rule
+import Orange.associate
 
-class Cmp:
-    def __init__(self, ms):
-        self.ms = ms
-
-    def __call__(self, r1, r2):        
-        for m in self.ms:
-            c = -cmp(getattr(r1, m), getattr(r2, m))
-            if c:
-                return c
-        return 0
-
-def sort(rules, ms = ["support"]):
-    rules.sort(Cmp(ms))
+printRules = Orange.associate.print_rules
+sort = Orange.associate.sort