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 bugs #271

Merged
merged 2 commits into from
Nov 17, 2023
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
5 changes: 3 additions & 2 deletions PAMI/highUtilitySpatialPattern/basic/SHUIM.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"""

from PAMI.highUtilitySpatialPattern.basic import abstract as _ab
from typing import List, Dict, Tuple, Set, Union, Any, Generator, Optional
from typing import List, Dict, Tuple, Set, Union, Any, Generator, Optional, TypeVar
from functools import cmp_to_key as _cmpToKey
import pandas as pd

Expand Down Expand Up @@ -92,6 +92,7 @@ class _Transaction:
"""
offset = 0
prefixUtility = 0
Self = TypeVar("Self", bound="_Transaction")

def __init__(self, items: List[int], utilities: List[int], transactionUtility: int, pmus: Optional[List[int]]=None) -> None:
self.items = items
Expand All @@ -100,7 +101,7 @@ def __init__(self, items: List[int], utilities: List[int], transactionUtility: i
if pmus is not None:
self.pmus = pmus

def projectTransaction(self, offsetE: int) -> _Transaction:
def projectTransaction(self, offsetE: int) -> Self:
"""
A method to create new Transaction from existing till offsetE
Expand Down
75 changes: 49 additions & 26 deletions PAMI/highUtilitySpatialPattern/topk/TKSHUIM.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@ def createTransaction(self, line):
transactionUtility = int(trans_list[1])
itemsString = trans_list[0].strip().split(self.sep)
utilityString = trans_list[2].strip().split(self.sep)
pmuString = trans_list[3].strip().split(self.sep)
if (len(trans_list) == 4):
pmuString = trans_list[3].strip().split(self.sep)
items = []
utilities = []
pmus = []
Expand All @@ -243,7 +244,8 @@ def createTransaction(self, line):
self.maxItem = item_int
items.append(item_int)
utilities.append(int(utilityString[idx]))
pmus.append(int(pmuString[idx]))
if (len(trans_list) == 4):
pmus.append(int(pmuString[idx]))
return Transaction(items, utilities, transactionUtility, pmus)

def getMaxItem(self):
Expand Down Expand Up @@ -495,6 +497,8 @@ def backtrackingEFIM(self, transactionsOfP, itemsToKeep, itemsToExplore, prefixL
initialMemory = psutil.virtual_memory()[3]
transactionsPe = []
utilityPe = 0
if len(transactionsOfP) == 0:
break
previousTransaction = transactionsOfP[0]
consecutiveMergeCount = 0
for transaction in transactionsOfP:
Expand Down Expand Up @@ -774,10 +778,18 @@ def useUtilityBinArrayToCalculateLocalUtilityFirstTime(self, dataset):
utilityMatrix = defaultdict(lambda: defaultdict(int))
for transaction in dataset.getTransactions():
for idx, item in enumerate(transaction.getItems()):
pmu = transaction.getUtilities()[idx]
if item in self.Neighbours:
neighbors = self.Neighbours[item]
for idx, item in enumerate(transaction.getItems()):
if item in neighbors:
pmu += transaction.getUtilities()[idx]
if item in self.utilityBinArrayLU:
self.utilityBinArrayLU[item] += transaction.getPmus()[idx]
# self.utilityBinArrayLU[item] += transaction.getPmus()[idx]
self.utilityBinArrayLU[item] += pmu
else:
self.utilityBinArrayLU[item] = transaction.getPmus()[idx]
# self.utilityBinArrayLU[item] = transaction.getPmus()[idx]
self.utilityBinArrayLU[item] = pmu
utilityMatrix[item][item] += transaction.getUtilities()[idx]
if item in self.Neighbours:
neighbors = self.Neighbours[item]
Expand Down Expand Up @@ -877,26 +889,37 @@ def printResults(self):
print("Total Memory in RSS", self.getMemoryRSS())
print("Total ExecutionTime in seconds:", self.getRuntime())

def main():
inputFile = 'mushroom_utility_spmf.txt'
neighborFile = 'mushroom_neighbourhood.txt' #Users can also specify this constraint between 0 to 1.
k = 1000
seperator = ' '
obj = TKSHUIM(iFile=inputFile, nFile=neighborFile, k=k, sep=seperator) #initialize
obj.startMine()
obj.printResults()
print(obj.getPatterns())

if __name__ == '__main__':
_ap = str()
if len(sys.argv) == 5 or len(sys.argv) == 6:
if len(sys.argv) == 6:
_ap = TKSHUIM(sys.argv[1], sys.argv[3], int(sys.argv[4]), sys.argv[5])
if len(sys.argv) == 5:
_ap = TKSHUIM(sys.argv[1], sys.argv[3], int(sys.argv[4]))
_ap.startMine()
print("Top K Spatial High Utility Patterns:", len(_ap.getPatterns()))
_ap.save(sys.argv[2])
print("Total Memory in USS:", _ap.getMemoryUSS())
print("Total Memory in RSS", _ap.getMemoryRSS())
print("Total ExecutionTime in seconds:", _ap.getRuntime())
else:
for i in [1000, 5000]:
_ap = TKSHUIM('/Users/Likhitha/Downloads/mushroom_main_2000.txt',
'/Users/Likhitha/Downloads/mushroom_neighbors_2000.txt', i, ' ')
_ap.startMine()
print("Total number of Spatial High Utility Patterns:", len(_ap.getPatterns()))
print("Total Memory in USS:", _ap.getMemoryUSS())
print("Total Memory in RSS", _ap.getMemoryRSS())
print("Total ExecutionTime in seconds:", _ap.getRuntime())
print("Error! The number of input parameters do not match the total number of parameters provided")
main()
# _ap = str()
# if len(sys.argv) == 5 or len(sys.argv) == 6:
# if len(sys.argv) == 6:
# _ap = TKSHUIM(sys.argv[1], sys.argv[3], int(sys.argv[4]), sys.argv[5])
# if len(sys.argv) == 5:
# _ap = TKSHUIM(sys.argv[1], sys.argv[3], int(sys.argv[4]))
# _ap.startMine()
# print("Top K Spatial High Utility Patterns:", len(_ap.getPatterns()))
# _ap.save(sys.argv[2])
# print("Total Memory in USS:", _ap.getMemoryUSS())
# print("Total Memory in RSS", _ap.getMemoryRSS())
# print("Total ExecutionTime in seconds:", _ap.getRuntime())
# else:
# for i in [1000, 5000]:
# _ap = TKSHUIM('/Users/Likhitha/Downloads/mushroom_main_2000.txt',
# '/Users/Likhitha/Downloads/mushroom_neighbors_2000.txt', i, ' ')
# _ap.startMine()
# print("Total number of Spatial High Utility Patterns:", len(_ap.getPatterns()))
# print("Total Memory in USS:", _ap.getMemoryUSS())
# print("Total Memory in RSS", _ap.getMemoryRSS())
# print("Total ExecutionTime in seconds:", _ap.getRuntime())
# print("Error! The number of input parameters do not match the total number of parameters provided")