From 9fbe09c3a9d626071243c525ad5bf8561cb75205 Mon Sep 17 00:00:00 2001 From: Likhitha Date: Sat, 22 Jun 2024 13:47:29 +0900 Subject: [PATCH] Automated Testing --- .../basic/automated_test_Apriori.py | 19 +++++++ .../basic/automated_test_ECLAT.py | 19 +++++++ .../basic/automated_test_ECLATDiffset.py | 19 +++++++ .../basic/automated_test_FPGrowth.py | 19 +++++++ .../basic/automated_test_case_ECLAT.py | 56 +++++++++++++++++++ .../basic/automated_test_case_ECLATDiffset.py | 56 +++++++++++++++++++ .../basic/automated_test_case_apriori.py | 56 +++++++++++++++++++ .../basic/automated_test_case_fpgrowth.py | 56 +++++++++++++++++++ tests/frequentPattern/basic/gen.py | 20 +++++++ ..._cases.ipynb => RPGrowth_test_cases.ipynb} | 0 10 files changed, 320 insertions(+) create mode 100644 tests/frequentPattern/basic/automated_test_Apriori.py create mode 100644 tests/frequentPattern/basic/automated_test_ECLAT.py create mode 100644 tests/frequentPattern/basic/automated_test_ECLATDiffset.py create mode 100644 tests/frequentPattern/basic/automated_test_FPGrowth.py create mode 100644 tests/frequentPattern/basic/automated_test_case_ECLAT.py create mode 100644 tests/frequentPattern/basic/automated_test_case_ECLATDiffset.py create mode 100644 tests/frequentPattern/basic/automated_test_case_apriori.py create mode 100644 tests/frequentPattern/basic/automated_test_case_fpgrowth.py create mode 100644 tests/frequentPattern/basic/gen.py rename tests/recurringPattern/basic/{test_RPGrowth_cases.ipynb => RPGrowth_test_cases.ipynb} (100%) diff --git a/tests/frequentPattern/basic/automated_test_Apriori.py b/tests/frequentPattern/basic/automated_test_Apriori.py new file mode 100644 index 00000000..64e4a1de --- /dev/null +++ b/tests/frequentPattern/basic/automated_test_Apriori.py @@ -0,0 +1,19 @@ +import pandas as pd +from gen import generate_transactional_dataset +from PAMI.frequentPattern.basic.Apriori import Apriori as alg +import warnings + +warnings.filterwarnings("ignore") + +# Apriori algorithm from PAMI +def test_pami(dataset, min_sup=0.2): + dataset = [",".join(i) for i in dataset] + with open("sample.csv", "w+") as f: + f.write("\n".join(dataset)) + obj = alg(iFile="sample.csv", minSup=min_sup, sep=',') + obj.mine() + res = obj.getPatternsAsDataFrame() + res["Patterns"] = res["Patterns"].apply(lambda x: x.split()) + res["Support"] = res["Support"].apply(lambda x: x / len(dataset)) + pami = res + return pami diff --git a/tests/frequentPattern/basic/automated_test_ECLAT.py b/tests/frequentPattern/basic/automated_test_ECLAT.py new file mode 100644 index 00000000..896207da --- /dev/null +++ b/tests/frequentPattern/basic/automated_test_ECLAT.py @@ -0,0 +1,19 @@ +import pandas as pd +from gen import generate_transactional_dataset +from PAMI.frequentPattern.basic.ECLAT import ECLAT as alg +import warnings + +warnings.filterwarnings("ignore") + +# Apriori algorithm from PAMI +def test_pami(dataset, min_sup=0.2): + dataset = [",".join(i) for i in dataset] + with open("sample.csv", "w+") as f: + f.write("\n".join(dataset)) + obj = alg(iFile="sample.csv", minSup=min_sup, sep=',') + obj.mine() + res = obj.getPatternsAsDataFrame() + res["Patterns"] = res["Patterns"].apply(lambda x: x.split()) + res["Support"] = res["Support"].apply(lambda x: x / len(dataset)) + pami = res + return pami diff --git a/tests/frequentPattern/basic/automated_test_ECLATDiffset.py b/tests/frequentPattern/basic/automated_test_ECLATDiffset.py new file mode 100644 index 00000000..dae07f45 --- /dev/null +++ b/tests/frequentPattern/basic/automated_test_ECLATDiffset.py @@ -0,0 +1,19 @@ +import pandas as pd +from gen import generate_transactional_dataset +from PAMI.frequentPattern.basic.ECLATDiffset import ECLATDiffset as alg +import warnings + +warnings.filterwarnings("ignore") + +# Apriori algorithm from PAMI +def test_pami(dataset, min_sup=0.2): + dataset = [",".join(i) for i in dataset] + with open("sample.csv", "w+") as f: + f.write("\n".join(dataset)) + obj = alg(iFile="sample.csv", minSup=min_sup, sep=',') + obj.mine() + res = obj.getPatternsAsDataFrame() + res["Patterns"] = res["Patterns"].apply(lambda x: x.split()) + res["Support"] = res["Support"].apply(lambda x: x / len(dataset)) + pami = res + return pami diff --git a/tests/frequentPattern/basic/automated_test_FPGrowth.py b/tests/frequentPattern/basic/automated_test_FPGrowth.py new file mode 100644 index 00000000..d24b84f4 --- /dev/null +++ b/tests/frequentPattern/basic/automated_test_FPGrowth.py @@ -0,0 +1,19 @@ +import pandas as pd +from gen import generate_transactional_dataset +from PAMI.frequentPattern.basic.FPGrowth import FPGrowth as alg +import warnings + +warnings.filterwarnings("ignore") + +# Apriori algorithm from PAMI +def test_pami(dataset, min_sup=0.2): + dataset = [",".join(i) for i in dataset] + with open("sample.csv", "w+") as f: + f.write("\n".join(dataset)) + obj = alg(iFile="sample.csv", minSup=min_sup, sep=',') + obj.mine() + res = obj.getPatternsAsDataFrame() + res["Patterns"] = res["Patterns"].apply(lambda x: x.split()) + res["Support"] = res["Support"].apply(lambda x: x / len(dataset)) + pami = res + return pami diff --git a/tests/frequentPattern/basic/automated_test_case_ECLAT.py b/tests/frequentPattern/basic/automated_test_case_ECLAT.py new file mode 100644 index 00000000..a70248d3 --- /dev/null +++ b/tests/frequentPattern/basic/automated_test_case_ECLAT.py @@ -0,0 +1,56 @@ +import unittest +from gen import generate_transactional_dataset +from automated_test_ECLAT import test_pami +import warnings + +warnings.filterwarnings("ignore") + +class TestExample(unittest.TestCase): + def test_num_patterns(self): + for _ in range(3): + num_distinct_items = 20 + num_transactions = 1000 + max_items_per_transaction = 20 + items = ["item-{}".format(i) for i in range(1, num_distinct_items + 1)] + dataset = generate_transactional_dataset(num_transactions, items, max_items_per_transaction) + + pami = test_pami(dataset) + # As we don't have a second method to compare, we just verify the length of pami + self.assertGreater(len(pami), 0, "No patterns were generated by PAMI") + + print("3 test cases for number of patterns have been passed") + + def test_equality(self): + for _ in range(3): + num_distinct_items = 20 + num_transactions = 1000 + max_items_per_transaction = 20 + items = ["item-{}".format(i) for i in range(1, num_distinct_items + 1)] + dataset = generate_transactional_dataset(num_transactions, items, max_items_per_transaction) + + pami = test_pami(dataset) + # Since we have no second method to compare, we just verify the patterns are generated + pami_patterns = sorted(list(pami["Patterns"])) + self.assertTrue(len(pami_patterns) > 0, "No patterns were generated by PAMI") + + print("2 test cases for Patterns equality are passed") + + def test_support(self): + for _ in range(3): + num_distinct_items = 20 + num_transactions = 1000 + max_items_per_transaction = 20 + items = ["item-{}".format(i) for i in range(1, num_distinct_items + 1)] + dataset = generate_transactional_dataset(num_transactions, items, max_items_per_transaction) + + pami = test_pami(dataset) + # Since we have no second method to compare, we just verify the support values are generated + pami.sort_values(by="Support", inplace=True) + ps = list(pami["Support"]) + for support in ps: + self.assertTrue(support > 0, "Support value should be greater than 0") + + print("3 test cases for support equality are passed") + +if __name__ == '__main__': + unittest.main() diff --git a/tests/frequentPattern/basic/automated_test_case_ECLATDiffset.py b/tests/frequentPattern/basic/automated_test_case_ECLATDiffset.py new file mode 100644 index 00000000..92014d00 --- /dev/null +++ b/tests/frequentPattern/basic/automated_test_case_ECLATDiffset.py @@ -0,0 +1,56 @@ +import unittest +from gen import generate_transactional_dataset +from automated_test_ECLATDiffset import test_pami +import warnings + +warnings.filterwarnings("ignore") + +class TestExample(unittest.TestCase): + def test_num_patterns(self): + for _ in range(3): + num_distinct_items = 20 + num_transactions = 1000 + max_items_per_transaction = 20 + items = ["item-{}".format(i) for i in range(1, num_distinct_items + 1)] + dataset = generate_transactional_dataset(num_transactions, items, max_items_per_transaction) + + pami = test_pami(dataset) + # As we don't have a second method to compare, we just verify the length of pami + self.assertGreater(len(pami), 0, "No patterns were generated by PAMI") + + print("3 test cases for number of patterns have been passed") + + def test_equality(self): + for _ in range(3): + num_distinct_items = 20 + num_transactions = 1000 + max_items_per_transaction = 20 + items = ["item-{}".format(i) for i in range(1, num_distinct_items + 1)] + dataset = generate_transactional_dataset(num_transactions, items, max_items_per_transaction) + + pami = test_pami(dataset) + # Since we have no second method to compare, we just verify the patterns are generated + pami_patterns = sorted(list(pami["Patterns"])) + self.assertTrue(len(pami_patterns) > 0, "No patterns were generated by PAMI") + + print("2 test cases for Patterns equality are passed") + + def test_support(self): + for _ in range(3): + num_distinct_items = 20 + num_transactions = 1000 + max_items_per_transaction = 20 + items = ["item-{}".format(i) for i in range(1, num_distinct_items + 1)] + dataset = generate_transactional_dataset(num_transactions, items, max_items_per_transaction) + + pami = test_pami(dataset) + # Since we have no second method to compare, we just verify the support values are generated + pami.sort_values(by="Support", inplace=True) + ps = list(pami["Support"]) + for support in ps: + self.assertTrue(support > 0, "Support value should be greater than 0") + + print("3 test cases for support equality are passed") + +if __name__ == '__main__': + unittest.main() diff --git a/tests/frequentPattern/basic/automated_test_case_apriori.py b/tests/frequentPattern/basic/automated_test_case_apriori.py new file mode 100644 index 00000000..808666db --- /dev/null +++ b/tests/frequentPattern/basic/automated_test_case_apriori.py @@ -0,0 +1,56 @@ +import unittest +from gen import generate_transactional_dataset +from automated_test_Apriori import test_pami +import warnings + +warnings.filterwarnings("ignore") + +class TestExample(unittest.TestCase): + def test_num_patterns(self): + for _ in range(3): + num_distinct_items = 20 + num_transactions = 1000 + max_items_per_transaction = 20 + items = ["item-{}".format(i) for i in range(1, num_distinct_items + 1)] + dataset = generate_transactional_dataset(num_transactions, items, max_items_per_transaction) + + pami = test_pami(dataset) + # As we don't have a second method to compare, we just verify the length of pami + self.assertGreater(len(pami), 0, "No patterns were generated by PAMI") + + print("3 test cases for number of patterns have been passed") + + def test_equality(self): + for _ in range(3): + num_distinct_items = 20 + num_transactions = 1000 + max_items_per_transaction = 20 + items = ["item-{}".format(i) for i in range(1, num_distinct_items + 1)] + dataset = generate_transactional_dataset(num_transactions, items, max_items_per_transaction) + + pami = test_pami(dataset) + # Since we have no second method to compare, we just verify the patterns are generated + pami_patterns = sorted(list(pami["Patterns"])) + self.assertTrue(len(pami_patterns) > 0, "No patterns were generated by PAMI") + + print("2 test cases for Patterns equality are passed") + + def test_support(self): + for _ in range(3): + num_distinct_items = 20 + num_transactions = 1000 + max_items_per_transaction = 20 + items = ["item-{}".format(i) for i in range(1, num_distinct_items + 1)] + dataset = generate_transactional_dataset(num_transactions, items, max_items_per_transaction) + + pami = test_pami(dataset) + # Since we have no second method to compare, we just verify the support values are generated + pami.sort_values(by="Support", inplace=True) + ps = list(pami["Support"]) + for support in ps: + self.assertTrue(support > 0, "Support value should be greater than 0") + + print("3 test cases for support equality are passed") + +if __name__ == '__main__': + unittest.main() diff --git a/tests/frequentPattern/basic/automated_test_case_fpgrowth.py b/tests/frequentPattern/basic/automated_test_case_fpgrowth.py new file mode 100644 index 00000000..6e115bc0 --- /dev/null +++ b/tests/frequentPattern/basic/automated_test_case_fpgrowth.py @@ -0,0 +1,56 @@ +import unittest +from gen import generate_transactional_dataset +from automated_test_FPGrowth import test_pami +import warnings + +warnings.filterwarnings("ignore") + +class TestExample(unittest.TestCase): + def test_num_patterns(self): + for _ in range(3): + num_distinct_items = 20 + num_transactions = 1000 + max_items_per_transaction = 20 + items = ["item-{}".format(i) for i in range(1, num_distinct_items + 1)] + dataset = generate_transactional_dataset(num_transactions, items, max_items_per_transaction) + + pami = test_pami(dataset) + # As we don't have a second method to compare, we just verify the length of pami + self.assertGreater(len(pami), 0, "No patterns were generated by PAMI") + + print("3 test cases for number of patterns have been passed") + + def test_equality(self): + for _ in range(3): + num_distinct_items = 20 + num_transactions = 1000 + max_items_per_transaction = 20 + items = ["item-{}".format(i) for i in range(1, num_distinct_items + 1)] + dataset = generate_transactional_dataset(num_transactions, items, max_items_per_transaction) + + pami = test_pami(dataset) + # Since we have no second method to compare, we just verify the patterns are generated + pami_patterns = sorted(list(pami["Patterns"])) + self.assertTrue(len(pami_patterns) > 0, "No patterns were generated by PAMI") + + print("2 test cases for Patterns equality are passed") + + def test_support(self): + for _ in range(3): + num_distinct_items = 20 + num_transactions = 1000 + max_items_per_transaction = 20 + items = ["item-{}".format(i) for i in range(1, num_distinct_items + 1)] + dataset = generate_transactional_dataset(num_transactions, items, max_items_per_transaction) + + pami = test_pami(dataset) + # Since we have no second method to compare, we just verify the support values are generated + pami.sort_values(by="Support", inplace=True) + ps = list(pami["Support"]) + for support in ps: + self.assertTrue(support > 0, "Support value should be greater than 0") + + print("3 test cases for support equality are passed") + +if __name__ == '__main__': + unittest.main() diff --git a/tests/frequentPattern/basic/gen.py b/tests/frequentPattern/basic/gen.py new file mode 100644 index 00000000..12215ba6 --- /dev/null +++ b/tests/frequentPattern/basic/gen.py @@ -0,0 +1,20 @@ +import random +import warnings +warnings.filterwarnings("ignore") + +def generate_transactional_dataset(num_transactions, items, max_items_per_transaction): + dataset = [] + for _ in range(num_transactions): + num_items = random.randint(1, max_items_per_transaction) + transaction = random.sample(items, num_items) + dataset.append(transaction) + return dataset + + +# num_distinct_items=20 +# num_transactions = 1000 +# max_items_per_transaction = 20 +# items=["item-{}".format(i) for i in range(1,num_distinct_items+1)] + +# dataset = generate_transactional_dataset(num_transactions, items, max_items_per_transaction) +# print(dataset) \ No newline at end of file diff --git a/tests/recurringPattern/basic/test_RPGrowth_cases.ipynb b/tests/recurringPattern/basic/RPGrowth_test_cases.ipynb similarity index 100% rename from tests/recurringPattern/basic/test_RPGrowth_cases.ipynb rename to tests/recurringPattern/basic/RPGrowth_test_cases.ipynb