From b81719aba31ef37aaf9765b6d88e35c6f7bd2d24 Mon Sep 17 00:00:00 2001 From: agp8x Date: Thu, 10 Aug 2017 16:08:51 +0200 Subject: [PATCH] add boxplot for simu retries --- analyzers/__init__.py | 11 +++++++++++ analyzers/analyzer/biogames.py | 7 ++++--- analyzers/render/biogames.py | 31 +++++++++++++++++++++++++++++++ log_analyzer.py | 6 ++---- 4 files changed, 48 insertions(+), 7 deletions(-) create mode 100644 analyzers/render/biogames.py diff --git a/analyzers/__init__.py b/analyzers/__init__.py index 4d7f5b3..1bcc8ca 100644 --- a/analyzers/__init__.py +++ b/analyzers/__init__.py @@ -1,3 +1,5 @@ +from typing import List + from .analyzer import Analyzer, Result from .analyzer.biogames import BoardDurationAnalyzer, SimulationRoundsAnalyzer from .analyzer.default import LogEntryCountAnalyzer, LocationAnalyzer, LogEntrySequenceAnalyzer, ActionSequenceAnalyzer @@ -7,6 +9,7 @@ from .render import Render from .render.default import PrintRender, JSONRender from .render.locomotion import LocomotionActionRelativeRender, LocomotionActionAbsoluteRender, \ LocomotionActionRatioRender +from .render.biogames import SimulationRoundsRender __FALLBACK__ = PrintRender __MAPPING__ = { @@ -19,6 +22,7 @@ __MAPPING__ = { ], SimulationRoundsAnalyzer: [ JSONRender, + SimulationRoundsRender, ] } @@ -27,3 +31,10 @@ def get_renderer(cls: type) -> [type]: if cls not in __MAPPING__: return [__FALLBACK__] return __MAPPING__[cls] + + +def render(cls: type, results: List[Result]): + for r in get_renderer(cls): + p = r() + p.result_types.append(cls) + p.render(results) diff --git a/analyzers/analyzer/biogames.py b/analyzers/analyzer/biogames.py index 85cfd06..8d89e83 100644 --- a/analyzers/analyzer/biogames.py +++ b/analyzers/analyzer/biogames.py @@ -49,7 +49,7 @@ class SimulationRoundsAnalyzer(Analyzer): def __init__(self, settings: LogSettings): super().__init__(settings) - self.store = defaultdict(lambda: 0) + self.store = defaultdict(lambda: -1) # TODO verify def result(self) -> Result: return Result(type(self), dict(self.store)) @@ -58,5 +58,6 @@ class SimulationRoundsAnalyzer(Analyzer): entry_type = entry[self.settings.type_field] if entry_type in self.settings.custom['simulation_rounds']: if entry["answers"][self.settings.type_field] in self.settings.custom["simu_data"]: - self.store[entry['answers']["@id"]] += 1 - return False \ No newline at end of file + simu_id = entry['answers']["@id"] + self.store[simu_id] += 1 + return False diff --git a/analyzers/render/biogames.py b/analyzers/render/biogames.py new file mode 100644 index 0000000..dd9545a --- /dev/null +++ b/analyzers/render/biogames.py @@ -0,0 +1,31 @@ +from collections import defaultdict +from typing import List, Tuple + +import matplotlib.pyplot as plt + +from . import Render +from .. import Result, SimulationRoundsAnalyzer + + +def plot(src_data: List[Tuple[str, List[int]]]): + names, datas = list(zip(*src_data)) + plt.boxplot(datas, labels=names) + plt.xticks(rotation='vertical') + # plt.margins() + plt.ylabel("simulation rounds") + plt.title("simulation retries") + plt.show() + + +class SimulationRoundsRender(Render): + def render(self, results: List[Result]): + data = defaultdict(list) + for result in self.filter(results): + get = result.get() + for key in get: + data[key].append(get[key]) + data_tuples = [(key, data[key]) for key in sorted(data)] + data_tuples = sorted(data_tuples, key=lambda x: sum(x[1])) + plot(data_tuples) + + result_types = [SimulationRoundsAnalyzer] diff --git a/log_analyzer.py b/log_analyzer.py index a4e3abd..4d137c4 100644 --- a/log_analyzer.py +++ b/log_analyzer.py @@ -1,6 +1,6 @@ from load import LOADERS from typing import List -from analyzers import get_renderer, Analyzer +from analyzers import get_renderer, Analyzer, render from analyzers.settings import LogSettings, load_settings import analyzers @@ -50,9 +50,7 @@ if __name__ == '__main__': if False: for r in get_renderer(analyzers.LocomotionActionAnalyzer): r().render(results) - jr = analyzers.JSONRender() - jr.result_types = [analyzers.SimulationRoundsAnalyzer] - jr.render(results) + render(analyzers.SimulationRoundsAnalyzer, results) # for analyzers in analyzers: # if analyzers.name() in ["LogEntryCount", "ActionSequenceAnalyzer"]: