diff --git a/analyzer/__init__.py b/analyzer/__init__.py index 9bec4e6..71f8bde 100644 --- a/analyzer/__init__.py +++ b/analyzer/__init__.py @@ -1,3 +1,4 @@ from .analyzer import * from .biogames import * from .locomotion_action import * +from .mask import * diff --git a/analyzer/analyzer.py b/analyzer/analyzer.py index dab3ad8..5f66b1e 100644 --- a/analyzer/analyzer.py +++ b/analyzer/analyzer.py @@ -40,7 +40,7 @@ class LocationAnalyzer(Analyzer): raise NotImplementedError() def process(self, entry: dict) -> bool: - if entry[self.settings.entry_type] in self.settings.spatials: + if entry[self.settings.type_field] in self.settings.spatials: self.entries.append(entry) return False @@ -51,11 +51,11 @@ class LogEntryCountAnalyzer(Analyzer): """ __name__ = "LogEntryCount" - def result(self) -> defaultdict: - return self.store + def result(self) -> dict: + return dict(self.store) def process(self, entry: dict) -> bool: - self.store[entry[self.settings.entry_type]] += 1 + self.store[entry[self.settings.type_field]] += 1 return False def __init__(self, settings: LogSettings): @@ -73,7 +73,7 @@ class LogEntrySequenceAnalyzer(Analyzer): return self.store def process(self, entry: dict) -> bool: - entry_type = entry[self.settings.entry_type] + entry_type = entry[self.settings.type_field] self.store.append(entry_type) return False @@ -89,7 +89,7 @@ class ActionSequenceAnalyzer(LogEntrySequenceAnalyzer): __name__ = "ActionSequenceAnalyzer" def process(self, entry: dict) -> bool: - entry_type = entry[self.settings.entry_type] + entry_type = entry[self.settings.type_field] if entry_type in self.settings.spatials: return False self.store.append(entry_type) diff --git a/analyzer/biogames.py b/analyzer/biogames.py index e97a4e8..7c6d213 100644 --- a/analyzer/biogames.py +++ b/analyzer/biogames.py @@ -29,7 +29,7 @@ class BoardDurationAnalyzer(Analyzer): return result def process(self, entry: dict) -> bool: - entry_type = entry[self.settings.entry_type] + entry_type = entry[self.settings.type_field] if entry_type in self.settings.boards: self.store.append(self.save_entry(entry["board_id"], entry["timestamp"])) # TODO: make configurable return False diff --git a/analyzer/locomotion_action.py b/analyzer/locomotion_action.py index b2e3fab..f2dba97 100644 --- a/analyzer/locomotion_action.py +++ b/analyzer/locomotion_action.py @@ -9,14 +9,14 @@ from util import combinate def init_filter(settings: LogSettings, state: str) -> callable: # this implies OR for lists; AND for dicts if type(settings.sequences[state]) in (str, list): - return lambda entry: entry[settings.entry_type] in settings.sequences[state] + return lambda entry: entry[settings.type_field] in settings.sequences[state] else: return lambda entry: combinate(settings.sequences[state], entry) -class LocomotionActionAnalyzer(Analyzer): # TODO +class LocomotionActionAnalyzer(Analyzer): """ - calculate locomation/action times and ratio + calculate locomotion/action times and ratio Anything between LogEntryCache and CacheEnableAction is counted as ActionTime, the rest as LocomotionTime. @@ -74,15 +74,16 @@ class LocomotionActionAnalyzer(Analyzer): # TODO self.last = None -class CacheSequenceAnalyzer(Analyzer): # TODO +class CacheSequenceAnalyzer(Analyzer): __name__ = "CacheSequence" def process(self, entry: dict) -> bool: if self.filter(entry): if entry['cache']: - self.store.append((entry['timestamp'],entry['cache']['@id'])) + self.store.append((entry['timestamp'], entry['cache']['@id'])) else: - self.store.append((entry['timestamp'],entry['cache'])) + self.store.append((entry['timestamp'], entry['cache'])) + return False def result(self) -> list: return self.store diff --git a/analyzer/mask.py b/analyzer/mask.py new file mode 100644 index 0000000..66038f1 --- /dev/null +++ b/analyzer/mask.py @@ -0,0 +1,15 @@ +from .analyzer import Analyzer + + +class MaskSpatials(Analyzer): + __name__ = "MaskSpatials" + masked = 0 + + def process(self, entry: dict) -> bool: + if entry[self.settings.type_field] in self.settings.spatials: + self.masked += 1 + return True + return False + + def result(self) -> int: + return self.masked \ No newline at end of file diff --git a/log_analyzer.py b/log_analyzer.py index cb26911..128af98 100644 --- a/log_analyzer.py +++ b/log_analyzer.py @@ -6,7 +6,7 @@ import analyzer class LogSettings: log_format = None - entry_type = None + type_field = None spatials = None actions = None analyzers = [] @@ -15,7 +15,7 @@ class LogSettings: def __init__(self, json_dict): self.log_format = json_dict['logFormat'] - self.entry_type = json_dict['entryType'] + self.type_field = json_dict['entryType'] self.spatials = json_dict['spatials'] self.actions = json_dict['actions'] self.boards = json_dict['boards'] @@ -27,7 +27,7 @@ class LogSettings: def __repr__(self): return str({ "logFormat": self.log_format, - "entryType": self.entry_type, + "entryType": self.type_field, "spatials": self.spatials, "actions": self.actions, "analyzers": self.analyzers, diff --git a/util/iter.py b/util/iter.py index 9ad787a..3409b85 100644 --- a/util/iter.py +++ b/util/iter.py @@ -3,6 +3,8 @@ def json_path(obj: dict, key: str): if not type(obj) is dict: return None if "." not in key: + if key not in obj: + return None return obj[key] child_key = key.split(".") if child_key[0] not in obj: @@ -10,9 +12,9 @@ def json_path(obj: dict, key: str): return json_path(obj[child_key[0]], ".".join(child_key[1:])) -def combinate(settings: dict, entry:dict)-> bool: +def combinate(settings: dict, entry: dict) -> bool: """combine all settings {: } with entry using AND""" result = True for key, value in settings.items(): result = result and json_path(entry, key) == value - return result \ No newline at end of file + return result