-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMarkov.py
More file actions
100 lines (75 loc) · 3.66 KB
/
Markov.py
File metadata and controls
100 lines (75 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import numpy as np
import csv
from utils import configFilePath, responseFileReadingDecorator
class MarkovChain:
def __init__(self, txtFile):
self.txtFile = txtFile
self.lengthTXTFile = len(txtFile)
self.MarkovDict = None
self.MarkovMatrix = None
self.MarkovMatrixWithWeighting = None
self.averageObjectiveDistance = None
self.createMarkovDict()
self.generateMarkovMatrix()
##the following two are only applicable to number conditions
self.generateMarkovMatrixWithWeighting()
self.calculateAverageObjectiveDistance()
def createMarkovDict(self):
MarkovDict = {}
for order, current in enumerate(self.txtFile):
if order:
MarkovDict.setdefault((lastOne, current), 0)
MarkovDict[(lastOne, current)] += 1
lastOne = current
self.MarkovDict = MarkovDict
def generateMarkovMatrix(self):
self.MarkovMatrix = np.zeros((6, 6))
for pair, occurrences in self.MarkovDict.items():
I = int(pair[0]) - 1
J = int(pair[1]) - 1
self.MarkovMatrix[I, J] = occurrences
def generateMarkovMatrixWithWeighting(self):
objectiveWeighting = np.array([[i for i in range(6)], [1, 0, 1, 2, 3, 4], [2, 1, 0, 1, 2, 3], [3, 2, 1, 0, 1, 2], [4, 3, 2, 1, 0, 1], [5, 4, 3, 2, 1, 0]])
self.MarkovMatrixWithWeighting = self.MarkovMatrix * objectiveWeighting
def calculateAverageObjectiveDistance(self):
self.averageObjectiveDistance = np.sum(self.MarkovMatrixWithWeighting) / (self.lengthTXTFile - 1)
@responseFileReadingDecorator
def createMarkovChain(txtFile):
return MarkovChain(txtFile)
class Participant:
pass
class MarkovChainAll:
def __init__(self, totalParticipant, txtFileFolder):
self.totalParticipant = totalParticipant
self.txtFileFolder = txtFileFolder
for participant in range(1, self.totalParticipant + 1):
# print(participant)
setattr(self, "p" + str(participant), Participant()) #set the attribute of the class MarkovChainAll to be MarkovChain, with the name being p1, p2, p3, etc.
for condition in ["snum", "fnum", "sact", "fact"]:
setattr(getattr(self, "p" + str(participant)),
str(condition),
createMarkovChain(participantNumber = participant,
condition = condition,
txtFileFolder = self.txtFileFolder))
# print(getattr(getattr(getattr(self, "p" + str(participant)), str(condition)),"txtFile"))
if __name__ == "__main__":
totalParticipant = 24
txtFileFolder = configFilePath("FOLDER","responseFileFolder")
theAnswer = MarkovChainAll(totalParticipant, txtFileFolder)
# theAnswer.p1 contains two attributes that are MarkovChain objects for participant 1:
# theAnswer.p1.snum and theAnswer.p1.fnum
print(1, theAnswer.p1.snum.MarkovMatrix)
print(1, theAnswer.p1.fnum.averageObjectiveDistance)
# print(theAnswer.p1.snum.txtFile == theAnswer.p2.snum.txtFile)
# print(theAnswer.p1 == theAnswer.p2)
# print(theAnswer.p17.snum.averageObjectiveDistance)
# print(theAnswer.p20.fnum.averageObjectiveDistance)
# print(theAnswer.p20.snum.MarkovMatrixWithWeighting)
# participant = 17
# condition = "snum"
# pp17 = createMarkovChain(participantNumber = participant,
# condition = condition,
# txtFileFolder = txtFileFolder)
# print(pp17.txtFile)
# a = MarkovChain
# print(a.txtFile)