project/log_analyzer.py

104 lines
2.9 KiB
Python

import json
import sys
from load import LOADERS
import analyzer
from typing import List
class LogSettings:
log_format = None
type_field = None
spatials = None
actions = None
analyzers = []
boards = None
sequences = None
def __init__(self, json_dict):
self.log_format = json_dict['logFormat']
self.type_field = json_dict['entryType']
self.spatials = json_dict['spatials']
self.actions = json_dict['actions']
self.boards = json_dict['boards']
for mod in json_dict['analyzers']:
for name in json_dict['analyzers'][mod]:
self.analyzers.append(getattr(sys.modules[mod], name))
self.sequences = json_dict['sequences']
def __repr__(self):
return str({
"logFormat": self.log_format,
"entryType": self.type_field,
"spatials": self.spatials,
"actions": self.actions,
"analyzers": self.analyzers,
"boards": self.boards,
"sequences": self.sequences,
})
def load_settings(file: str) -> LogSettings:
return LogSettings(json.load(open(file)))
def process_log(log_id: str, settings: LogSettings) -> List:
logfile = "data/inst_{id}/instance_log.sqlite".format(id=log_id)
loader = LOADERS[settings.log_format]()
try:
loader.load(logfile)
except BaseException as e:
raise RuntimeError(e)
analyzers = []
for analyzer in settings.analyzers:
analyzers.append(analyzer(settings))
for entry in loader.get_entry():
for analyzer in analyzers:
if analyzer.process(entry):
break
return analyzers
if __name__ == '__main__':
settings = load_settings("biogames2.json")
#print(settings)
log_id = "56d9b64144ab44e7b90bf766f3be32e3"
log_ids = ["56d9b64144ab44e7b90bf766f3be32e3","85a9ad58951e4fbda26f860c9b66f567"]
results = []
for log_id in log_ids:
for analysis in process_log(log_id, settings):
print("* Result for " + analysis.name())
#print(analysis.result())
if analysis.name() in ("LocomotionAction"):
results.append(analysis.render())
import numpy as np
import matplotlib.pyplot as plt
data = list(zip(*results))
ind = np.arange(len(results))
loc = plt.bar(ind, data[2], width=0.35, color="red")
act = plt.bar(ind, data[3], width=0.35, bottom=data[2], color="green")
# ratio = plt.plot([1,2,3],[raw['locomotion_action_ratio'],raw['locomotion_relative'],raw['action_relative']], label="ratio", marker=".")
#ratio = plt.plot(ind, data[4], label="ratio", marker=".")
plt.ylabel("time")
plt.title("abs locomotion/action")
plt.xlabel("sessions")
#plt.xticks(ind, log_ids)
plt.xticks(ind, [""]*len(results))
#plt.yticks(np.arange(0,1.1,0.10))
plt.legend((loc[0], act[0]), ("loc", "act"))
plt.show()
# for analyzer in analyzers:
# if analyzer.name() in ["LogEntryCount", "ActionSequenceAnalyzer"]:
# print(json.dumps(analyzer.result(), indent=2))
# for analyzer in analyzers:
# if analyzer.name() in ["BoardDuration"]:
# print(json.dumps(analyzer.result(), indent=2))
# print(analyzer.render())
# coords = analyzers[1].render()
# with open("test.js", "w") as out:
# out.write("coords = "+coords)