-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathingester.py
152 lines (122 loc) · 3.83 KB
/
ingester.py
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import csv
import uuid
import pdb
from collections import OrderedDict
# ----- Objects ----->
class Event:
def __init__(self, name):
self.name = name
self.color = None # will be changed by the coloring algorithm
self.students = 0
self.valence = 0
self.conflicts = []
self.eventConflicts = []
def __str__(self):
ans = self.name
for c in self.conflicts:
ans = ans + " " + c
return ans
class User:
def __init__(self, name):
self.name = name
self.conflicts = []
def __str__(self):
ans = self.name
for c in self.conflicts:
ans = ans + " " + c
return ans
class Graph:
def __init__(self, firstNode):
self.nodes = [firstNode]
def addEvent(self, newEvent):
'''
Takes an event and adds all conflicts to the graph if they haven't already been accounted for.
'''
for event in self.nodes:
for person in newEvent.conflicts:
if person in event.conflicts: # check if a person (conflict) appears in both
# add conflict to both
if event not in newEvent.eventConflicts:
newEvent.eventConflicts.append(event)
newEvent.valence += 1
if newEvent not in event.eventConflicts:
event.eventConflicts.append(newEvent)
event.valence += 1
self.nodes.append(newEvent)
# ----- Methods ----->
def importData(name):
'''
importData takes a CSV filename and parses it into a list of rows.
'''
csv_file = open(name, mode='r')
csv_reader = csv.reader(csv_file)
ans = []
for line in csv_reader:
ans.append(line)
return ans
def buildUsers(usersRaw):
'''
Generates user objects based off the dataset given.
'''
users = OrderedDict()
for u in usersRaw:
users[u] = User(u)
del users[""]
return users
def buildEvents(data):
'''
Generates events objects based off the dataset given.
'''
events = OrderedDict()
for line in data:
events[line[0]] = Event(line[0])
return events
def buildConflicts(data, events, users):
'''
Reads the conflict matrix and populates each event with conflicts in the form of a list of Users that conflict.
Also appends information about how many students are required for the event.
'''
userKeys = list(users.keys())
for row in data:
event = events[row[0]]
for i,col in enumerate(row[1:]):
if i == 0:
event.students = col
if col == '1':
event.conflicts.append(users[userKeys[i]])
return
def buildGraph(events):
'''
Builds an undirected graph datastructure with events as nodes and edges as conflicts.
'''
keys = list(events.keys())
g = Graph(events[keys[0]])
del events[keys[0]] # remove item that's already been added to graph
for event in events:
if event not in g.nodes:
g.addEvent(events[event])
return g
def printGraph(graph):
'''
Pretty printer function to display graph.
'''
for node in graph.nodes:
print("CONFLICT --------->", node.name)
for c in node.eventConflicts:
print(c.name, c.color, c.valence)
def prepareDataStructure(file):
'''
Builds the data structure.
'''
data = importData(file)
users = buildUsers(data[0])
events = buildEvents(data[1:])
buildConflicts(data[1:], events, users)
graph = buildGraph(events)
for node in graph.nodes:
print("NODE: ", node.name)
return graph
if __name__ == "__main__":
# graph = prepareDataStructure('Data/allConflicts.csv')
graph = prepareDataStructure('Data/dataset.csv')
# printGraph(graph)