Skip to content

Commit

Permalink
falcons/localization_vision: tune.py: apply composition instead of in…
Browse files Browse the repository at this point in the history
…heritance at parameters.py; for now disable tick call as long as threading and parameter handling is not sorted out
  • Loading branch information
janfeitsma committed Aug 6, 2023
1 parent 6e1ebdb commit 591dc24
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 22 deletions.
54 changes: 33 additions & 21 deletions components/falcons/localization_vision/test/parameters.py
Original file line number Diff line number Diff line change
@@ -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)

4 changes: 3 additions & 1 deletion components/falcons/localization_vision/test/tune.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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(
Expand Down

0 comments on commit 591dc24

Please sign in to comment.