Skip to content
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
59 changes: 31 additions & 28 deletions PAMI/coveragePattern/basic/CMine.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@

from PAMI.coveragePattern.basic import abstract as _ab
from typing import List, Dict, Tuple, Set, Union, Any, Generator
from deprecation import deprecated
from deprecated import deprecated


class CMine(_ab._coveragePatterns):
Expand Down Expand Up @@ -241,58 +241,62 @@ def creatingCoverageItems(self) -> Dict[str, List[str]]:
tidData = {}
self._lno = 0
for transaction in self._Database:
self._lno = self._lno + 1
for item in transaction[1:]:
for item in transaction:
if item not in tidData:
tidData[item] = [self._lno]
else:
tidData[item].append(self._lno)
self._lno = self._lno + 1
coverageTidData = {k: v for k, v in tidData.items() if len(v) / len(self._Database) >= self._minRF}
coverageTidData = dict(sorted(coverageTidData.items(), reverse=True, key=lambda x: len(x[1])))
return coverageTidData

def tidToBitset(self,item_set: Dict[str, int]) -> Dict[str, int]:
def tidToBitset(self,item_set: Dict[str, List[int]]) -> Dict[str, int]:
"""
This function converts tid list to bitset.
This function converts tid list to bitset. Each item's transactions become one
integer with one bit per transaction of the database.

:param item_set:
:param item_set: coverage items and their tid lists
:return: Dictionary
:rtype: dict[str,int]
"""
bitset = {}
size = (self._lno + 7) >> 3

for k,v in item_set.items():
bitset[k] = 0b1
bitset[k] = (bitset[k] << int(v[0])) | 0b1
for i in range(1,len(v)):
diff = int(v[i]) - int(v[i-1])
bitset[k] = (bitset[k] << diff) | 0b1
bitset[k] = (bitset[k] << (self._lno - int(v[-1])))
buffer = bytearray(size)
for tid in v:
buffer[tid >> 3] |= 128 >> (tid & 7)
bitset[k] = int.from_bytes(bytes(buffer), 'big')
return bitset

def genPatterns(self,prefix: Tuple[str, int],tidData: List[Tuple[str, int]]) -> None:
def genPatterns(self,prefix: Tuple[str, int],tidData: List[Tuple[str, int]],start: int=0) -> None:
"""
This function generate coverage pattern about prefix.

:param prefix: String
:param tidData: list
This function generate coverage pattern about prefix. The prefix carries the union
of the transactions covered by its items, and it is extended with every later item
whose overlap ratio stays within maxOR (sorted closure property). Coverage support
only grows with more items, so it gates the output but never the growth.

:param prefix: the pattern and the bitset of the transactions it covers
:param tidData: list of coverage items and their bitsets
:param start: index of the first item eligible to extend the prefix
:return: None
"""
# variables to store coverage item set and
item_set = prefix[0]

# Get the length of tidData
length = len(tidData)
for i in range(length):
for i in range(start, length):
tid = prefix[1] & tidData[i][1]
tid1 = prefix[1] | tidData[i][1]
andCount = bin(tid).count("1") - 1
orCount = bin(tid1).count("1") - 1
if orCount/len(self._Database) >= self._minCS and andCount / len(str(prefix[1])) <= self._maxOR:
andCount = tid.bit_count()
if andCount <= self._maxOR * self._mapSupport[tidData[i][0]]:
tid1 = prefix[1] | tidData[i][1]
orCount = tid1.bit_count()
coverageItem_set = item_set + '\t' + tidData[i][0]
if orCount / len(self._Database) >= self._minRF:
self._finalPatterns[coverageItem_set] = andCount
self.genPatterns((coverageItem_set,tid),tidData[i+1:length])
if orCount / len(self._Database) >= self._minCS:
self._finalPatterns[coverageItem_set] = orCount
self.genPatterns((coverageItem_set,tid1),tidData,i+1)

def generateAllPatterns(self,coverageItems: Dict[str, int]) -> None:
"""
Expand All @@ -305,7 +309,7 @@ def generateAllPatterns(self,coverageItems: Dict[str, int]) -> None:
length = len(tidData)
for i in range(length):
#print(i,tidData[i][0])
self.genPatterns(tidData[i],tidData[i+1:length])
self.genPatterns(tidData[i],tidData,i+1)

@deprecated("It is recommended to use 'mine()' instead of 'mine()' for mining process. Starting from January 2025, 'mine()' will be completely terminated.")
def startMine(self) -> None:
Expand All @@ -324,10 +328,10 @@ def mine(self) -> None:
self._minRF = self._convert(self._minRF)
self._maxOR = self._convert(self._maxOR)
coverageItems = self.creatingCoverageItems()
self._mapSupport = {k: len(v) for k, v in coverageItems.items()}
self._finalPatterns = {k: len(v) for k, v in coverageItems.items()}
coverageItemsBitset = self.tidToBitset(coverageItems)
self.generateAllPatterns(coverageItemsBitset)
self.save('output.txt')
self._endTime = _ab._time.time()
process = _ab._psutil.Process(_ab._os.getpid())
self._memoryUSS = float()
Expand Down Expand Up @@ -422,7 +426,6 @@ def printResults(self) -> None:
if len(_ab._sys.argv) == 6:
_ap = CMine(_ab._sys.argv[1], _ab._sys.argv[3], _ab._sys.argv[4], _ab._sys.argv[5])
_ap.mine()
_ap.mine()
print("Total number of coverage Patterns:", len(_ap.getPatterns()))
_ap.save(_ab._sys.argv[2])
print("Total Memory in USS:", _ap.getMemoryUSS())
Expand Down
Empty file.
167 changes: 167 additions & 0 deletions PAMI/fuzzyFrequentPattern/cuda/abstract.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
# Copyright (C) 2021 Rage Uday Kiran
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

from abc import ABC as _ABC, abstractmethod as _abstractmethod
import time as _time
import math as _math
import csv as _csv
import pandas as _pd
from collections import defaultdict as _defaultdict
from itertools import combinations as _c
import os as _os
import os.path as _ospath
import psutil as _psutil
import sys as _sys
import validators as _validators
import cupy as _cp
import numpy as _np
from urllib.request import urlopen as _urlopen


class _fuzzyFrequentPatterns(_ABC):
"""
:Description: This abstract base class defines the variables and methods that every fuzzy frequent pattern mining algorithm must
employ in PAMI

:Attributes:

iFile : str
Input file name or path of the input file
minSup: int or float or str
The user can specify minSup either in count or proportion of database size.
If the program detects the data type of minSup is integer, then it treats minSup is expressed in count.
Otherwise, it will be treated as float.
Example: minSup=10 will be treated as integer, while minSup=10.0 will be treated as float
sep : str
This variable is used to distinguish items from one another in a transaction. The default seperator is tab space or \t.
However, the users can override their default separator.
startTime:float
To record the start time of the algorithm
endTime:float
To record the completion time of the algorithm
finalPatterns: dict
Storing the complete set of patterns in a dictionary variable
oFile : str
Name of the output file to store complete set of fuzzy frequent patterns
memoryUSS : float
To store the total amount of USS memory consumed by the program
memoryRSS : float
To store the total amount of RSS memory consumed by the program

:Methods:

mine()
Mining process will start from here
getPatterns()
Complete set of patterns will be retrieved with this function
save(oFile)
Complete set of fuzzy frequent patterns will be loaded in to a output file
getPatternsAsDataFrame()
Complete set of fuzzy frequent patterns will be loaded in to data frame
getMemoryUSS()
Total amount of USS memory consumed by the program will be retrieved from this function
getMemoryRSS()
Total amount of RSS memory consumed by the program will be retrieved from this function
getRuntime()
Total amount of runtime taken by the program will be retrieved from this function
"""

def __init__(self, iFile, minSup, sep = '\t'):
"""
:param iFile: Input file name or path of the input file
:type iFile: str
:param minSup: The user can specify minSup either in count or proportion of database size.
If the program detects the data type of minSup is integer, then it treats minSup is expressed in count.
Otherwise, it will be treated as float.
Example: minSup=10 will be treated as integer, while minSup=10.0 will be treated as float
:type minSup: int or float or str
:param sep: separator used in user specified input file
:type sep: str
"""

self._iFile = iFile
self._minSup = minSup
self._sep = sep
self._finalPatterns = {}
self._startTime = float()
self._endTime = float()
self._memoryRSS = float()
self._memoryUSS = float()
self._oFile = " "

@_abstractmethod
def startMine(self):
"""
Code for the mining process will start from this function
"""

pass

@_abstractmethod
def getPatterns(self):
"""
Complete set of fuzzy frequent patterns generated will be retrieved from this function
"""

pass

@_abstractmethod
def save(self, oFile):
"""
Complete set of fuzzy frequent patterns will be saved in to an output file from this function
:param oFile: Name of the output file
:type oFile: csvfile
"""

pass

@_abstractmethod
def getPatternsAsDataFrame(self):
"""
Complete set of fuzzy frequent patterns will be loaded in to data frame from this function
"""

pass

@_abstractmethod
def getMemoryUSS(self):
"""
Total amount of USS memory consumed by the program will be retrieved from this function
"""

pass

@_abstractmethod
def getMemoryRSS(self):
"""
Total amount of RSS memory consumed by the program will be retrieved from this function
"""
pass

@_abstractmethod
def getRuntime(self):
"""
Total amount of runtime taken by the program will be retrieved from this function
"""

pass

@_abstractmethod
def printResults(self):
"""
To print results of the execution.
"""

pass
Loading