forked from EDA2021-1-SEC05-G5N/Reto3-G05N
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.py
278 lines (209 loc) · 6.93 KB
/
controller.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
"""
* Copyright 2020, Departamento de sistemas y Computación,
* Universidad de Los Andes
*
*
* Desarrolado para el curso ISIS1225 - Estructuras de Datos y Algoritmos
*
*
* 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 withthis program. If not, see <http://www.gnu.org/licenses/>.
"""
import config as cf
import model
import csv
import time
import tracemalloc
"""
El controlador se encarga de mediar entre la vista y el modelo.
"""
def init():
"""
Llama la funcion de inicializacion del modelo.
"""
# catalog es utilizado para interactuar con el modelo
analyzer = model.newAnalyzer()
return analyzer
# Inicialización del Catálogo
def loadData(analyzer, file1, file2, file3):
"""
Carga los datos de los archivos CSV en el modelo
"""
delta_time = -1.0
delta_memory = -1.0
tracemalloc.start()
start_time = getTime()
start_memory = getMemory()
loadEvents(analyzer, file1)
loadHashtags(analyzer, file2)
loadVader(analyzer, file3)
stop_memory = getMemory()
stop_time = getTime()
tracemalloc.stop()
delta_time = stop_time - start_time
delta_memory = deltaMemory(start_memory, stop_memory)
return delta_time, delta_memory
# Funciones para la carga de datos
def loadEvents(analyzer, file):
"""
Itera cada elemento del archivo csv
"""
analysis_file = cf.data_dir + file
input_file = csv.DictReader(open(analysis_file, encoding="utf-8"),
delimiter=",")
for event in input_file:
model.addEvent(analyzer, event)
def loadHashtags(analyzer, file):
"""
Itera cada elemento del archivo csv
"""
analysis_file = cf.data_dir + file
input_file = csv.DictReader(open(analysis_file, encoding="utf-8"),
delimiter=",")
for event in input_file:
model.addHashtagsToTracks(analyzer, event)
def loadVader(analyzer, file):
"""
Itera cada elemento del archivo csv
"""
analysis_file = cf.data_dir + file
input_file = csv.DictReader(open(analysis_file, encoding="utf-8"),
delimiter=",")
for vader in input_file:
model.addOnMap(
analyzer, vader['vader_avg'], vader['hashtag'], 'hashtag_vader')
# Funciones de ordenamiento
# Funciones de consulta sobre el analyzer
def getEventsByRange(analyzer, criteria, initial, final):
'''
Función puente entre las funciones homónimas entre el model y view
'''
delta_time = -1.0
delta_memory = -1.0
tracemalloc.start()
start_time = getTime()
start_memory = getMemory()
result = model.getEventsByRange(analyzer, criteria, initial, final)
stop_memory = getMemory()
stop_time = getTime()
tracemalloc.stop()
delta_time = stop_time - start_time
delta_memory = deltaMemory(start_memory, stop_memory)
return result, delta_time, delta_memory
def getMusicToParty(analyzer, energyrange, danceabilityrange):
'''
Función puente entre las funciones homónimas entre el model y view
'''
delta_time = -1.0
delta_memory = -1.0
tracemalloc.start()
start_time = getTime()
start_memory = getMemory()
result = model.getTrcForTwoCriteria(
analyzer, energyrange, 'energy', danceabilityrange, 'danceability')
stop_memory = getMemory()
stop_time = getTime()
tracemalloc.stop()
delta_time = stop_time - start_time
delta_memory = deltaMemory(start_memory, stop_memory)
return result, delta_time, delta_memory
def getMusicToStudy(analyzer, instrumentalnessrange, temporange):
'''
Función puente entre las funciones homónimas entre el model y view
'''
delta_time = -1.0
delta_memory = -1.0
tracemalloc.start()
start_time = getTime()
start_memory = getMemory()
result = model.getTrcForTwoCriteria(
analyzer, instrumentalnessrange, 'instrumentalness', temporange, 'tempo')
stop_memory = getMemory()
stop_time = getTime()
tracemalloc.stop()
delta_time = stop_time - start_time
delta_memory = deltaMemory(start_memory, stop_memory)
return result, delta_time, delta_memory
def getEventsByTimeRangeGenre(analyzer, temporange, timerange):
'''
Función puente entre las funciones homónimas entre el model y view
'''
# delta_time = -1.0
# delta_memory = -1.0
# tracemalloc.start()
# start_time = getTime()
# start_memory = getMemory()
result = model.getEventsByTimeRangeGenre(
analyzer, timerange[0], timerange[1], temporange[0], temporange[1])
# stop_memory = getMemory()
# stop_time = getTime()
# tracemalloc.stop()
# delta_time = stop_time - start_time
# delta_memory = deltaMemory(start_memory, stop_memory)
return result
def doSentimentAnalysis(analyzer, temporange):
'''
Función puente entre las funciones homónimas entre el model y view
'''
delta_time = -1.0
delta_memory = -1.0
tracemalloc.start()
start_time = getTime()
start_memory = getMemory()
result = model.doSentimentAnalysis(
analyzer, temporange[0], temporange[1])
stop_memory = getMemory()
stop_time = getTime()
tracemalloc.stop()
delta_time = stop_time - start_time
delta_memory = deltaMemory(start_memory, stop_memory)
return result, delta_time, delta_memory
def eventsSize(analyzer):
"""
Número de eventos cargados
"""
return model.eventsSize(analyzer)
def artistsSize(analyzer):
"""
Número de artistas únicos
"""
return model.artistsSize(analyzer)
def tracksSize(analyzer):
"""
Número de pistas únicas
"""
return model.tracksSize(analyzer)
# Medir tiempo y memoria
def getTime():
"""
devuelve el instante tiempo de procesamiento en milisegundos
"""
return float(time.perf_counter()*1000)
def getMemory():
"""
toma una muestra de la memoria alocada en instante de tiempo
"""
return tracemalloc.take_snapshot()
def deltaMemory(start_memory, stop_memory):
"""
calcula la diferencia en memoria alocada del programa entre dos
instantes de tiempo y devuelve el resultado en bytes (ej.: 2100.0 B)
"""
memory_diff = stop_memory.compare_to(start_memory, "filename")
delta_memory = 0.0
# suma de las diferencias en uso de memoria
for stat in memory_diff:
delta_memory = delta_memory + stat.size_diff
# de Byte -> kByte
delta_memory = delta_memory/1024.0
return delta_memory