Skip to content

Commit 981f655

Browse files
local periodic patterns
1 parent 40be522 commit 981f655

File tree

3 files changed

+226
-87
lines changed

3 files changed

+226
-87
lines changed

PAMI/localPeriodicPattern/basic/LPPGrowth.py

+84-30
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,39 @@
88
# **Importing this algorithm into a python program**
99
# --------------------------------------------------------
1010
#
11-
# from PAMI.localPeriodicPattern.basic import LPPGrowth as alg
11+
# from PAMI.localPeriodicPattern.basic import LPPGrowth as alg
1212
#
13-
# obj = alg.LPPGrowth(iFile, maxPer, maxSoPer, minDur)
13+
# obj = alg.LPPGrowth(iFile, maxPer, maxSoPer, minDur)
1414
#
15-
# obj.startMine()
15+
# obj.mine()
1616
#
17-
# localPeriodicPatterns = obj.getPatterns()
17+
# localPeriodicPatterns = obj.getPatterns()
1818
#
19-
# print(f'Total number of local periodic patterns: {len(localPeriodicPatterns)}')
19+
# print(f'Total number of local periodic patterns: {len(localPeriodicPatterns)}')
2020
#
21-
# obj.save(oFile)
21+
# obj.save(oFile)
2222
#
23-
# Df = obj.getPatternsAsDataFrame()
23+
# Df = obj.getPatternsAsDataFrame()
2424
#
25-
# memUSS = obj.getMemoryUSS()
25+
# memUSS = obj.getMemoryUSS()
2626
#
27-
# print(f'Total memory in USS: {memUSS}')
27+
# print(f'Total memory in USS: {memUSS}')
2828
#
29-
# memRSS = obj.getMemoryRSS()
29+
# memRSS = obj.getMemoryRSS()
3030
#
31-
# print(f'Total memory in RSS: {memRSS}')
31+
# print(f'Total memory in RSS: {memRSS}')
3232
#
33-
# runtime = obj.getRuntime()
34-
#
35-
# print(f'Total execution time in seconds: {runtime})
33+
# runtime = obj.getRuntime()
3634
#
35+
# print(f'Total execution time in seconds: {runtime})
3736
#
3837

3938

39+
40+
41+
4042
__copyright__ = """
41-
Copyright (C) 2021 Rage Uday Kiran
43+
Copyright (C) 2021 Rage Uday Kiran
4244
4345
This program is free software: you can redistribute it and/or modify
4446
it under the terms of the GNU General Public License as published by
@@ -60,6 +62,7 @@
6062

6163
from PAMI.localPeriodicPattern.basic import abstract as _ab
6264
from typing import List, Dict, Tuple, Set, Union, Any, Generator
65+
from deprecated import deprecated
6366

6467
class Node:
6568
"""
@@ -94,8 +97,14 @@ def __init__(self) -> None:
9497
def getChild(self, item: int) -> 'Node':
9598
"""
9699
This function is used to get child node from the parent node
97-
:param item:
100+
101+
:param item: item of the parent node
102+
103+
:type item: int
104+
98105
:return: if node have node of item, then return it. if node don't have return []
106+
107+
:rtype: Node
99108
"""
100109
for child in self.child:
101110
if child.item == item:
@@ -141,6 +150,7 @@ def addTransaction(self, transaction: List[int], tid: int) -> None:
141150
:type transaction: list
142151
:param tid: represents the timestamp of transaction
143152
:type tid: list or int
153+
:return: None
144154
"""
145155
current = self.root
146156
for item in transaction:
@@ -164,6 +174,7 @@ def fixNodeLinks(self, item: int, newNode: 'Node') -> None:
164174
:type item: string
165175
:param newNode: it represents node which is added
166176
:type newNode: Node
177+
:return: None
167178
"""
168179
if item in self.nodeLinks:
169180
lastNode = self.nodeLinks[item]
@@ -178,6 +189,7 @@ def deleteNode(self, item: int) -> None:
178189
179190
:param item: it represents the item name of node
180191
:type item: str
192+
:return: None
181193
"""
182194
deleteNode = self.firstNodeLink[item]
183195
parentNode = deleteNode.parent
@@ -203,6 +215,7 @@ def createPrefixTree(self, path: List[int], tidList: List[int]) -> None:
203215
:type path: list
204216
:param tidList: it represents tid of each item
205217
:type tidList: list
218+
:return: None
206219
"""
207220
currentNode = self.root
208221
for item in path:
@@ -235,9 +248,9 @@ class LPPGrowth(_ab._localPeriodicPatterns):
235248
a Discrete Sequence. Information Sciences, Elsevier, to appear. [ppt] DOI: 10.1016/j.ins.2020.09.044
236249
237250
:param iFile: str :
238-
Name of the Input file to mine complete set of frequent pattern's
251+
Name of the Input file to mine complete set of local periodic pattern's
239252
:param oFile: str :
240-
Name of the output file to store complete set of frequent patterns
253+
Name of the output file to store complete set of local periodic patterns
241254
:param minDur: str:
242255
Minimal duration in seconds between consecutive periods of time-intervals where a pattern is continuously periodic.
243256
:param maxPer: float:
@@ -299,7 +312,7 @@ class LPPGrowth(_ab._localPeriodicPatterns):
299312
Calculate PTL from input tsList as integer list.
300313
calculatePTLbit(tsList)
301314
Calculate PTL from input tsList as bit vector.
302-
startMine()
315+
mine()
303316
Mining process will start from here.
304317
getMemoryUSS()
305318
Total amount of USS memory consumed by the mining process will be retrieved from this function.
@@ -315,12 +328,19 @@ class LPPGrowth(_ab._localPeriodicPatterns):
315328
Complete set of local periodic patterns will be loaded in to a dataframe.
316329
317330
**Executing the code on terminal:**
318-
-------------------------------------
319-
Format:
320-
>>> python3 LPPMGrowth.py <inputFile> <outputFile> <maxPer> <minSoPer> <minDur>
331+
---------------------------------------
332+
333+
.. code-block:: console
334+
335+
Format:
321336
322-
Examples:
323-
>>> python3 LPPMGrowth.py sampleDB.txt patterns.txt 0.3 0.4 0.5
337+
(.venv) $ python3 LPPMGrowth.py <inputFile> <outputFile> <maxPer> <minSoPer> <minDur>
338+
339+
Example Usage:
340+
341+
(.venv) $ python3 LPPMGrowth.py sampleDB.txt patterns.txt 0.3 0.4 0.5
342+
343+
.. note: minDur will be considered as time interval between two consecutive periods
324344
325345
326346
**Sample run of importing the code:**
@@ -331,7 +351,7 @@ class LPPGrowth(_ab._localPeriodicPatterns):
331351
332352
obj = alg.LPPGrowth(iFile, maxPer, maxSoPer, minDur)
333353
334-
obj.startMine()
354+
obj.mine()
335355
336356
localPeriodicPatterns = obj.getPatterns()
337357
@@ -585,6 +605,7 @@ def __patternGrowth(self, tree: 'Tree', prefix: List[int], prefixPFList: Dict[An
585605
:type prefix: list
586606
:param prefixPFList: tsList of prefix patterns.
587607
:type prefixPFList: dict or list
608+
:return: None
588609
"""
589610
items = list(prefixPFList)
590611
if not prefix:
@@ -641,6 +662,7 @@ def __calculatePTL(self, tsList: List[int]) -> set:
641662
:param tsList: It is tsList which store time stamp as integer.
642663
:type tsList: list
643664
:return: PTL
665+
:rtype: set
644666
"""
645667
start = -1
646668
PTL = set()
@@ -674,6 +696,7 @@ def __calculatePTLbit(self, tsList: List[int]) -> set:
674696
:param tsList: It is tsList which store time stamp as bit vector.
675697
:type tsList: list
676698
:return: PTL
699+
:rtype: set
677700
"""
678701
tsList = list(bin(tsList))
679702
tsList = tsList[2:]
@@ -720,7 +743,9 @@ def __convert(self, value: Any) -> float:
720743
to convert the type of user specified minSup value
721744
722745
:param value: user specified minSup value
746+
:type value: int or float or str
723747
:return: converted type
748+
:rtype: float
724749
"""
725750
if type(value) is int:
726751
value = int(value)
@@ -734,6 +759,7 @@ def __convert(self, value: Any) -> float:
734759
value = int(value)
735760
return value
736761

762+
@deprecated("It is recommended to use 'mine()' instead of 'startMine()' for mining process. Starting from January 2025, 'startMine()' will be completely terminated.")
737763
def startMine(self) -> None:
738764
"""
739765
Mining process start from here.
@@ -755,8 +781,30 @@ def startMine(self) -> None:
755781
self._localPeriodicPatterns__memoryUSS = process.memory_full_info().uss
756782
self._localPeriodicPatterns__memoryRSS = process.memory_info().rss
757783

784+
def mine(self) -> None:
785+
"""
786+
Mining process start from here.
787+
"""
788+
self._localPeriodicPatterns__startTime = _ab._time.time()
789+
self._localPeriodicPatterns__finalPatterns = {}
790+
self.__creatingItemSets()
791+
self._localPeriodicPatterns__maxPer = self.__convert(self._localPeriodicPatterns__maxPer)
792+
self._localPeriodicPatterns__maxSoPer = self.__convert(self._localPeriodicPatterns__maxSoPer)
793+
self._localPeriodicPatterns__minDur = self.__convert(self._localPeriodicPatterns__minDur)
794+
self.__createTSList()
795+
self.__generateLPP()
796+
self.__createLPPTree()
797+
self.__patternGrowth(self.__root, [], self.__items)
798+
self._localPeriodicPatterns__endTime = _ab._time.time()
799+
process = _ab._psutil.Process(_ab._os.getpid())
800+
self._localPeriodicPatterns__memoryUSS = float()
801+
self._localPeriodicPatterns__memoryRSS = float()
802+
self._localPeriodicPatterns__memoryUSS = process.memory_full_info().uss
803+
self._localPeriodicPatterns__memoryRSS = process.memory_info().rss
804+
758805
def getMemoryUSS(self) -> float:
759-
"""Total amount of USS memory consumed by the mining process will be retrieved from this function
806+
"""
807+
Total amount of USS memory consumed by the mining process will be retrieved from this function
760808
761809
:return: returning USS memory consumed by the mining process
762810
:rtype: float
@@ -765,7 +813,8 @@ def getMemoryUSS(self) -> float:
765813
return self._localPeriodicPatterns__memoryUSS
766814

767815
def getMemoryRSS(self) -> float:
768-
"""Total amount of RSS memory consumed by the mining process will be retrieved from this function
816+
"""
817+
Total amount of RSS memory consumed by the mining process will be retrieved from this function
769818
770819
:return: returning RSS memory consumed by the mining process
771820
:rtype: float
@@ -774,7 +823,8 @@ def getMemoryRSS(self) -> float:
774823
return self._localPeriodicPatterns__memoryRSS
775824

776825
def getRuntime(self) -> float:
777-
"""Calculating the total amount of runtime taken by the mining process
826+
"""
827+
Calculating the total amount of runtime taken by the mining process
778828
779829
:return: returning total amount of runtime taken by the mining process
780830
:rtype: float
@@ -783,7 +833,8 @@ def getRuntime(self) -> float:
783833
return self._localPeriodicPatterns__endTime - self._localPeriodicPatterns__startTime
784834

785835
def getPatternsAsDataFrame(self) -> '_ab._pd.DataFrame':
786-
"""Storing final local periodic patterns in a dataframe
836+
"""
837+
Storing final local periodic patterns in a dataframe
787838
788839
:return: returning local periodic patterns in a dataframe
789840
:rtype: pd.DataFrame
@@ -805,6 +856,7 @@ def save(self, outFile: str) -> None:
805856
806857
:param outFile: name of the output file
807858
:type outFile: csv file
859+
:return: None
808860
"""
809861
self._localPeriodicPatterns__oFile = outFile
810862
writer = open(self._localPeriodicPatterns__oFile, 'w+')
@@ -819,7 +871,8 @@ def save(self, outFile: str) -> None:
819871
writer.write("%s \n" % patternsAndPTL)
820872

821873
def getPatterns(self) -> Dict:
822-
""" Function to send the set of local periodic patterns after completion of the mining process
874+
"""
875+
Function to send the set of local periodic patterns after completion of the mining process
823876
824877
:return: returning frequent patterns
825878
:rtype: dict
@@ -844,6 +897,7 @@ def printResults(self) -> None:
844897
if len(_ab._sys.argv) == 5:
845898
_ap = LPPGrowth(_ab._sys.argv[1], _ab._sys.argv[3], float(_ab._sys.argv[4]))
846899
_ap.startMine()
900+
_ap.mine()
847901
print("Total number of Local Periodic Patterns:", len(_ap.getPatterns()))
848902
_ap.save(_ab._sys.argv[2])
849903
print("Total Memory in USS:", _ap.getMemoryUSS())

0 commit comments

Comments
 (0)