From 591dc2446da1a8147e4bc40255e3e0f7570825d1 Mon Sep 17 00:00:00 2001 From: Jan Feitsma Date: Sun, 6 Aug 2023 10:03:16 +0200 Subject: [PATCH] falcons/localization_vision: tune.py: apply composition instead of inheritance at parameters.py; for now disable tick call as long as threading and parameter handling is not sorted out --- .../localization_vision/test/parameters.py | 54 +++++++++++-------- .../falcons/localization_vision/test/tune.py | 4 +- 2 files changed, 36 insertions(+), 22 deletions(-) diff --git a/components/falcons/localization_vision/test/parameters.py b/components/falcons/localization_vision/test/parameters.py index 7fa841b..ad33118 100755 --- a/components/falcons/localization_vision/test/parameters.py +++ b/components/falcons/localization_vision/test/parameters.py @@ -1,54 +1,66 @@ # python modules -import types from google.protobuf.message import Message # own modules import gui -class ParametersProxy(gui.Parameters): +class ParametersProxy: """ Connect sliders with protobuf Params object. + This class behaves as gui.Parameters, such that the GUI (sliders etc) remains clean. """ + # choose composition over inheritance + def __init__(self, params_proto, range_hints_proto): - super().__init__() self.params_proto = params_proto self.range_hints_proto = range_hints_proto - gui.create_standard_gui_params(self) # default zoom etc - self._init_params(params_proto.solver) # only make solver configurable, not the field dimension model A,B,C etc + self.gui_params = gui.create_standard_gui_params() # system parameters from GUI (zoom, frequency etc) + self.pb_params = gui.Parameters() # component-specific parameters from protobuf + self._init_params(params_proto) def _init_params(self, proto, parent_name=''): for field, value in proto.ListFields(): name = f'{parent_name}.{field.name}' if parent_name else field.name if isinstance(value, str): # Handling string fields raise NotImplementedError('strings not yet supported in tuning') - self.add(name, str, None, None, value) + # self.add(name, str, None, None, value) elif isinstance(value, int): # Handling integer fields min_val, max_val = self.range_hints_proto[name] - self.add(name, int, min_val, max_val, value) + self.pb_params.add(name, int, min_val, max_val, value) elif isinstance(value, float): # Handling float fields min_val, max_val = self.range_hints_proto[name] - self.add(name, float, min_val, max_val, value) + self.pb_params.add(name, float, min_val, max_val, value) elif isinstance(value, bool): # Handling boolean fields - self.add(name, bool, False, True, value) + self.pb_params.add(name, bool, False, True, value) elif isinstance(value, Message): # Handling nested protobuf messages self._init_params(value, name) - def _construct(self, *args, **kwargs): - result = gui.Param(*args, **kwargs) - result.notify = self._update_protobuf - return result + def get(self, name): + if name in self.gui_params.params: + return self.gui_params.get(name) + return self.pb_params.get(name) + + def set(self, name, value): + if name in self.gui_params.params: + self.gui_params.set(name, value) + else: + self.pb_params.set(name, value) + self._update_protobuf(name, value) + + def __iter__(self): + yield from self.gui_params + yield from self.pb_params + + def __repr__(self): + gui_repr = '\n'.join([repr(p) for p in self.gui_params]) + pb_repr = '\n'.join([repr(p) for p in self.pb_params]) + return f"{gui_repr}\n{pb_repr}" def _update_protobuf(self, name, value): names = name.split('.') - current_proto = self.params_proto.solver # only make solver configurable, not the field dimension model A,B,C etc + current_proto = self.params_proto for n in names[:-1]: current_proto = getattr(current_proto, n) - try: - setattr(current_proto, names[-1], value) - except: - pass # TODO - - def __repr__(self): - return '\n'.join([repr(p) for p in self.params.values()]) + setattr(current_proto, names[-1], value) diff --git a/components/falcons/localization_vision/test/tune.py b/components/falcons/localization_vision/test/tune.py index 0a60726..ae5dc19 100755 --- a/components/falcons/localization_vision/test/tune.py +++ b/components/falcons/localization_vision/test/tune.py @@ -55,7 +55,7 @@ class TuningTool(): def __init__(self, filename): self.image = None self.data = common.Data(filename) - self.params = parameters.ParametersProxy(self.data.params, RANGE_HINTS) + self.params = parameters.ParametersProxy(self.data.params.solver, RANGE_HINTS) self.gui = gui.WindowManager(self.params, callback=self.get_image) self.thread = threading.Thread(target=self.runner) self.thread.start() @@ -69,6 +69,8 @@ def runner(self): time.sleep(1) def tick(self): + print(self.data.params.solver) + return print('py before tick') t = self.data.t ans = pybind_ext.tick(