add SequenceSimuRetries

laptop_master
agp8x 2017-11-01 17:05:05 +01:00
parent 6bbc66d55e
commit b4c52ac655
10 changed files with 121 additions and 35 deletions

View File

@ -2,14 +2,14 @@ from typing import List
from .analyzer import Analyzer, Result from .analyzer import Analyzer, Result
from .analyzer.biogames import BoardDurationAnalyzer, SimulationRoundsAnalyzer, ActivationSequenceAnalyzer, \ from .analyzer.biogames import BoardDurationAnalyzer, SimulationRoundsAnalyzer, ActivationSequenceAnalyzer, \
BiogamesCategorizer, ActivityMapper, BiogamesStore BiogamesCategorizer, ActivityMapper, BiogamesStore, InstanceConfig, SimulationOrderAnalyzer
from .analyzer.default import LogEntryCountAnalyzer, LocationAnalyzer, LogEntrySequenceAnalyzer, ActionSequenceAnalyzer, \ from .analyzer.default import LogEntryCountAnalyzer, LocationAnalyzer, LogEntrySequenceAnalyzer, ActionSequenceAnalyzer, \
CategorizerStub, Store, ProgressAnalyzer CategorizerStub, Store, ProgressAnalyzer
from .analyzer.locomotion import LocomotionActionAnalyzer, CacheSequenceAnalyzer from .analyzer.locomotion import LocomotionActionAnalyzer, CacheSequenceAnalyzer
from .analyzer.mask import MaskSpatials from .analyzer.mask import MaskSpatials
from .render import Render from .render import Render
from .render.biogames import SimulationRoundsRender, BoardDurationHistRender, BoardDurationBoxRender, \ from .render.biogames import SimulationRoundsRender, BoardDurationHistRender, BoardDurationBoxRender, \
ActivityMapperRender, StoreRender ActivityMapperRender, StoreRender, SimulationOrderRender
from .render.default import PrintRender, JSONRender, TrackRender, HeatMapRender from .render.default import PrintRender, JSONRender, TrackRender, HeatMapRender
from .render.locomotion import LocomotionActionRelativeRender, LocomotionActionAbsoluteRender, \ from .render.locomotion import LocomotionActionRelativeRender, LocomotionActionAbsoluteRender, \
LocomotionActionRatioRender LocomotionActionRatioRender
@ -46,6 +46,10 @@ __MAPPING__ = {
], ],
ProgressAnalyzer: [ ProgressAnalyzer: [
StoreRender StoreRender
],
SimulationOrderAnalyzer: [
JSONRender,
SimulationOrderRender
] ]
} }

View File

@ -44,7 +44,7 @@ class ResultStore:
def get_store(self) -> dict: def get_store(self) -> dict:
return dict(self.store) return dict(self.store)
def get_all(self) -> list: def get_all(self) -> Collection[Result]:
""" """
Throw all categories together Throw all categories together
:return: :return:

View File

@ -119,9 +119,7 @@ class ActivityMapper(Analyzer):
def result(self, store: ResultStore) -> None: def result(self, store: ResultStore) -> None:
instance_config_id = self.instance_config_id instance_config_id = self.instance_config_id
for active_segment in self.store: # active_segment → sequence or None (None → map active) for active_segment in self.store: # active_segment → sequence or None (None → map active)
host = self.settings.custom["host"] seq_data_url = "/game2/editor/config/{config_id}/sequence/{sequence_id}/".format(
seq_data_url = "{host}/game2/editor/config/{config_id}/sequence/{sequence_id}/".format(
host=host,
config_id=instance_config_id, config_id=instance_config_id,
sequence_id=active_segment.sequence, sequence_id=active_segment.sequence,
) )
@ -133,7 +131,7 @@ class ActivityMapper(Analyzer):
if event[self.settings.type_field] in self.settings.boards: if event[self.settings.type_field] in self.settings.boards:
sequence_id = active_segment.sequence sequence_id = active_segment.sequence
board_id = event["board_id"] board_id = event["board_id"]
local_file = download_board(board_id, host, instance_config_id, sequence_id, source) local_file = download_board(board_id, instance_config_id, sequence_id, source)
if local_file is not None: if local_file is not None:
event["image"] = local_file[16:] event["image"] = local_file[16:]
store.add(Result(type(self), {"instance": instance_config_id, "store": [x._asdict() for x in self.store]})) store.add(Result(type(self), {"instance": instance_config_id, "store": [x._asdict() for x in self.store]}))
@ -176,7 +174,6 @@ class BiogamesStore(Store):
board_id = event["board_id"] board_id = event["board_id"]
local_file = download_board( local_file = download_board(
board_id=board_id, board_id=board_id,
host=self.settings.custom["host"],
instance_config_id=json_path(self.store[0], self.settings.custom["instance_config_id"]), instance_config_id=json_path(self.store[0], self.settings.custom["instance_config_id"]),
sequence_id=sequence_id, sequence_id=sequence_id,
source=self.settings.source) source=self.settings.source)
@ -188,4 +185,42 @@ class BiogamesStore(Store):
def process(self, entry: dict) -> bool: def process(self, entry: dict) -> bool:
self.store.append(entry) self.store.append(entry)
return False
class InstanceConfig(Analyzer):
__name__ = "InstanceConfig"
def __init__(self, settings: LogSettings):
super().__init__(settings)
self.store = {}
def process(self, entry: dict):
if entry[self.settings.type_field] in self.settings.custom["instance_start"]:
print(entry)
self.store["instance_id"] = json_path(entry, self.settings.custom["instance_config_id"])
def result(self, store: ResultStore):
store.add(Result(type(self), dict(self.store)))
class SimulationOrderAnalyzer(Analyzer):
__name__ = "SimuOrder"
def __init__(self, settings: LogSettings):
super().__init__(settings)
self.store = defaultdict(lambda: -1) # TODO verify
self.order = []
def result(self, store: ResultStore) -> None:
store.add(Result(type(self), [self.store[sim] for sim in self.order]))
def process(self, entry: dict) -> bool:
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"]:
simu_id = entry['answers']["@id"]
self.store[simu_id] += 1
if not simu_id in self.order:
self.order.append(simu_id)
return False return False

View File

@ -5,18 +5,19 @@ from typing import List, Tuple
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import os import os
from analyzers import Store, BiogamesStore from analyzers import Store, BiogamesStore, SimulationOrderAnalyzer
from . import Render from . import Render
from .. import Result, SimulationRoundsAnalyzer, BoardDurationAnalyzer, ActivityMapper from .. import Result, SimulationRoundsAnalyzer, BoardDurationAnalyzer, ActivityMapper
def plot(src_data: List[Tuple[str, List[int]]]): def plot(src_data: List[Tuple[str, List[int]]], ylabel="simulation rounds", title="simulation retries",
rotation='vertical'):
names, datas = list(zip(*src_data)) names, datas = list(zip(*src_data))
plt.boxplot(datas, labels=names) plt.boxplot(datas, labels=names)
plt.xticks(rotation='vertical') plt.xticks(rotation=rotation)
# plt.margins() # plt.margins()
plt.ylabel("simulation rounds") plt.ylabel(ylabel)
plt.title("simulation retries") plt.title(title)
plt.show() plt.show()
@ -86,3 +87,18 @@ class StoreRender(Render):
for result in self.filter(results): for result in self.filter(results):
with open(os.path.join("static","progress","data","fooo"), "w") as out: with open(os.path.join("static","progress","data","fooo"), "w") as out:
json.dump(result.get(), out, indent=1) json.dump(result.get(), out, indent=1)
class SimulationOrderRender(Render):
def render(self, results: List[Result]):
data = defaultdict(list)
for result in self.filter(results):
get = result.get()
for i,value in enumerate(get):
data[i].append(value)
#data_tuples = [(key, data[key]) for key in sorted(data)]
#data_tuples = sorted(data_tuples, key=lambda x: sum(x[1]))
#plot(enumerate([r.get() for r in self.filter(results)]))
plot(list(data.items()), ylabel="simulation retries", title="sequential simulation retries", rotation=None)
result_types = [SimulationOrderAnalyzer]

View File

@ -14,10 +14,13 @@
"analyzers": { "analyzers": {
"analyzers": [ "analyzers": [
"BiogamesCategorizer", "BiogamesCategorizer",
"ActivityMapper", "SimulationOrderAnalyzer"
"ProgressAnalyzer"
] ]
}, },
"dis":[
"ActivityMapper",
"ProgressAnalyzer",
"InstanceConfig"],
"disabled_analyzers": [ "disabled_analyzers": [
"LocomotionActionAnalyzer", "LocomotionActionAnalyzer",
"LogEntryCountAnalyzer", "LogEntryCountAnalyzer",
@ -59,7 +62,6 @@
"action":"PAUSE" "action":"PAUSE"
} }
}, },
"host":"http://0.0.0.0:5000",
"coordinates": "location.coordinates" "coordinates": "location.coordinates"
}, },
"source":{ "source":{
@ -67,6 +69,7 @@
"url": "http://0.0.0.0:5000/game2/instance/log/list/", "url": "http://0.0.0.0:5000/game2/instance/log/list/",
"login_url": "http://localhost:5000/game2/auth/json-login", "login_url": "http://localhost:5000/game2/auth/json-login",
"username": "dev", "username": "dev",
"password": "dev" "password": "dev",
"host":"http://0.0.0.0:5000"
} }
} }

View File

@ -48,9 +48,9 @@ if __name__ == '__main__':
# "57c444470dbf88605433ca935c", # "57c444470dbf88605433ca935c",
# "78e0c545b594e82edfad55bd7f", # "78e0c545b594e82edfad55bd7f",
# "91abfd4b31a5562b1c66be37d9", # "91abfd4b31a5562b1c66be37d9",
# "597b704fe9ace475316c345903", "597b704fe9ace475316c345903",
# "e01a684aa29dff9ddd9705edf8", "e01a684aa29dff9ddd9705edf8",
# "fbf9d64ae0bdad0de7efa3eec6", "fbf9d64ae0bdad0de7efa3eec6",
# "fe1331481f85560681f86827ec", # "fe1331481f85560681f86827ec",
"fe1331481f85560681f86827ec"] "fe1331481f85560681f86827ec"]
#"fec57041458e6cef98652df625", ] #"fec57041458e6cef98652df625", ]
@ -67,10 +67,17 @@ if __name__ == '__main__':
if False: if False:
render(analyzers.LocationAnalyzer, store.get_all()) render(analyzers.LocationAnalyzer, store.get_all())
#print(json.dumps(store.serializable(), indent=1)) #print(json.dumps(store.serializable(), indent=1))
if True: if False:
render(analyzers.ActivityMapper, store.get_all()) render(analyzers.ActivityMapper, store.get_all())
render(analyzers.ProgressAnalyzer, store.get_all()) render(analyzers.ProgressAnalyzer, store.get_all())
if False:
from analyzers.postprocessing import graph
g = graph.Cache(settings)
g.run(store)
if True:
render(analyzers.SimulationOrderAnalyzer, store.get_all())
# for analyzers in analyzers: # for analyzers in analyzers:
# if analyzers.name() in ["LogEntryCount", "ActionSequenceAnalyzer"]: # if analyzers.name() in ["LogEntryCount", "ActionSequenceAnalyzer"]:
# print(json.dumps(analyzers.result(), indent=2)) # print(json.dumps(analyzers.result(), indent=2))

View File

@ -1,3 +1,6 @@
requests requests==2.18.4
numpy numpy==1.13.1
matplotlib matplotlib==2.1.0
osmnx==0.6
networkx==2.0
pydot==1.2.3

View File

@ -18,9 +18,10 @@ class Biogames(Source):
self.headers: typing.Dict[str, str] = {'Accept': 'application/json'} self.headers: typing.Dict[str, str] = {'Accept': 'application/json'}
self.cookies: typing.Dict[str, str] = {} self.cookies: typing.Dict[str, str] = {}
self.id2link: typing.Dict[str, str] = {} self.id2link: typing.Dict[str, str] = {}
self.host: str = None
def connect(self, **kwargs): def connect(self, **kwargs):
for i in ['username', 'password', 'url', 'login_url']: for i in ['username', 'password', 'url', 'login_url', 'host']:
if not i in kwargs: if not i in kwargs:
raise ValueError("missing value " + i) raise ValueError("missing value " + i)
csrf_request = requests.get(kwargs['url']) csrf_request = requests.get(kwargs['url'])
@ -40,12 +41,11 @@ class Biogames(Source):
self.cookies['sessionid'] = login.cookies['sessionid'] self.cookies['sessionid'] = login.cookies['sessionid']
log.info("obtained sessionid (" + self.cookies['sessionid'] + ")") log.info("obtained sessionid (" + self.cookies['sessionid'] + ")")
self.url = kwargs['url'] self.url = kwargs['url']
self.host = kwargs['host']
log.info("stored url (" + self.url + ")") log.info("stored url (" + self.url + ")")
def list(self): def list(self):
logs_query = requests.get(self.url, cookies=self.cookies, headers=self.headers) logs = self.get_json(self.url)
log.info(logs_query.status_code)
logs = logs_query.json()
log.info(len(logs)) log.info(len(logs))
for i in logs: for i in logs:
self.id2link[i["id"]] = i["link"] # TODO self.id2link[i["id"]] = i["link"] # TODO
@ -65,15 +65,18 @@ class Biogames(Source):
def download_file(self, url, filename): def download_file(self, url, filename):
with open(filename, "wb") as out: with open(filename, "wb") as out:
try: try:
download = requests.get(url, cookies=self.cookies, stream=True) download = self._get(url)
shutil.copyfileobj(download.raw, out) shutil.copyfileobj(download.raw, out)
return filename return filename
except Exception as e: except Exception as e:
log.exception(e) log.exception(e)
os.remove(filename) os.remove(filename)
def get_json(self, url):
return self._get(url, stream=False).json()
def close(self): def close(self):
pass pass
def _get(self, url): def _get(self, url, stream=True):
return requests.get(url, cookies=self.cookies, headers=self.headers, stream=True) return requests.get(self.host + url, cookies=self.cookies, headers=self.headers, stream=stream)

View File

@ -11,5 +11,8 @@ class Source:
def get(self, ids: typing.Collection): def get(self, ids: typing.Collection):
raise NotImplementedError raise NotImplementedError
def get_json(self, url:str) -> dict:
raise NotImplementedError
def close(self): def close(self):
raise NotImplementedError raise NotImplementedError

View File

@ -6,15 +6,14 @@ from util import json_path
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
def download_board(board_id, host, instance_config_id, sequence_id, source): def download_board(board_id, instance_config_id, sequence_id, source):
local_file = "static/progress/images/{config_id}/{sequence_id}/{board_id}".format( local_file = "static/progress/images/{config_id}/{sequence_id}/{board_id}".format(
config_id=instance_config_id, config_id=instance_config_id,
sequence_id=sequence_id, sequence_id=sequence_id,
board_id=board_id) board_id=board_id)
if os.path.exists(local_file): if os.path.exists(local_file):
return local_file return local_file
url = "{host}/game2/editor/config/{config_id}/sequence/{sequence_id}/board/{board_id}/".format( url = "/game2/editor/config/{config_id}/sequence/{sequence_id}/board/{board_id}/".format(
host=host,
config_id=instance_config_id, config_id=instance_config_id,
sequence_id=sequence_id, sequence_id=sequence_id,
board_id=board_id board_id=board_id
@ -26,5 +25,18 @@ def download_board(board_id, host, instance_config_id, sequence_id, source):
preview_url = json_path(data, "preview_url.medium") preview_url = json_path(data, "preview_url.medium")
logger.debug(preview_url) logger.debug(preview_url)
os.makedirs(local_file[:-len(board_id)], exist_ok=True) os.makedirs(local_file[:-len(board_id)], exist_ok=True)
source.download_file(host + preview_url, local_file) source.download_file(preview_url, local_file)
return local_file return local_file
def get_config(source, instance_id):
url = "/game2/editor/config/{config_id}/".format(config_id=instance_id)
instance_data = source.get_json(url)
caches = url + "cache/"
cache_data = source.get_json(caches)
return {
"name": instance_data["name"],
"id": instance_data["@id"],
"caches": cache_data
}