-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsettings.py
212 lines (187 loc) · 7.43 KB
/
settings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import codecs
from xml.etree.ElementTree import Element
import yaml
from utils.py_utils import get_class, get_class_name_from_type
from collections import Callable
import logging
from multipledispatch import dispatch
import soundcard as sc
class Settings:
"""
System-wide settings for OpenDial.
"""
# logger
log = logging.getLogger('PyOpenDial')
discretization_buckets = 50
eps = 1e-6
nr_samples = 3000
max_sampling_time = 250 # in milliseconds
_functions = dict()
def __init__(self, arg1=None):
if arg1 is None:
"""
Creates new settings with the default values
"""
self.show_gui = False
self.floor = ''
self.user_speech = ''
self.system_speech = ''
self.user_input = ''
self.system_output = ''
self.vars_to_monitor = list()
self.horizon = 0
self.discount_factor = 0.
self.inverted_role = False
self.modules = []
self.mcts_simulation_count = 1
self.mcts_exploration_constant = 1.
self.planner = 'forward'
self.GOOGLE_APPLICATION_CREDENTIALS = None
self._input_mixer = None
self._output_mixer = None
self._params = dict()
self._explicit_settings = set()
self.fill_settings(yaml.load(codecs.open('settings.yml', 'r')))
self._explicit_settings.clear()
elif isinstance(arg1, dict):
mapping = arg1
"""
Creates a new settings with the values provided as argument. Values that are
not explicitly specified in the mapping are set to their default values.
:param mapping: the properties
"""
self.floor = ''
self.user_speech = ''
self.system_speech = ''
self.user_input = ''
self.system_output = ''
self.vars_to_monitor = list()
self.horizon = 0
self.discount_factor = 0.
self.inverted_role = False
self.modules = []
self.GOOGLE_APPLICATION_CREDENTIALS = None
self._input_mixer = None
self._output_mixer = None
self._params = dict()
self._explicit_settings = set()
self.fill_settings(yaml.load(codecs.open('settings.yml', 'r')))
self._explicit_settings.clear()
if mapping is not None:
self.fill_settings(mapping)
else:
raise NotImplementedError()
def select_audio_mixers(self):
self._input_mixer = sc.all_speakers()[0] if len(sc.all_speakers()) > 0 else None
self._output_mixer = sc.all_microphones()[0] if len(sc.all_microphones()) > 0 else None
@dispatch(dict)
def fill_settings(self, settings):
"""
Fills the current settings with the values provided as argument. Existing
values are overridden.
:param settings: the properties
"""
for key, value in settings.items():
if key.lower() == 'horizon':
self.horizon = value
elif key.lower() == 'discount':
self.discount_factor = value
elif key.lower() == 'gui':
self.show_gui = value
elif key.lower() == 'user':
self.user_input = value
elif key.lower() == 'speech_user':
self.user_speech = value
elif key.lower() == 'speech_system':
self.system_speech = value
elif key.lower() == 'floor':
self.floor = value
elif key.lower() == 'system':
self.system_output = value
elif key.lower() == 'monitor':
split = value.split(",")
for i in range(len(split)):
if len(split[i].strip()) > 0:
self.vars_to_monitor.append(split[i].strip())
elif key.lower() == 'samples':
Settings.nr_samples = value
elif key.lower() == 'timeout':
Settings.max_sampling_time = value
elif key.lower() == 'discretisation':
Settings.discretization_buckets = value
elif key.lower() == 'modules' or key.lower() == 'module':
for module in value.split(','):
self.modules.append(get_class(module))
elif key.lower() == 'mcts_simulation_count':
self.mcts_simulation_count = value
elif key.lower() == 'mcts_exploration_constant':
self.mcts_exploration_constant = float(value)
elif key.lower() == 'planner':
if value.lower() in ['forward', 'mcts']:
self.planner = value.lower()
else:
raise ValueError("Not supported planner: %s" % value)
elif key.upper() == 'GOOGLE_APPLICATION_CREDENTIALS':
self.GOOGLE_APPLICATION_CREDENTIALS = value
else:
self._params[key.lower()] = value
self._explicit_settings.add(key.lower())
@dispatch()
def get_full_mapping(self):
"""
Returns a representation of the settings in terms of a mapping between
property labels and values
:return: the corresponding mapping
"""
mapping = dict()
mapping.update(self._params)
mapping["horizon"] = self.horizon
mapping["discount"] = self.discount_factor
mapping["speech_user"] = self.user_speech
mapping["speech_system"] = self.system_speech
mapping["floor"] = self.floor
mapping["user"] = self.user_input
mapping["system"] = self.system_output
mapping['input_mixer'] = self._input_mixer.id if self._input_mixer else ''
mapping['output_mixer'] = self._output_mixer.id if self._output_mixer else ''
# mapping.setProperty("monitor", StringUtils.join(varsToMonitor, ","));
mapping["monitor"] = ",".join(self.vars_to_monitor)
mapping["samples"] = Settings.nr_samples
mapping["timeout"] = Settings.max_sampling_time
mapping["discretisation"] = Settings.discretization_buckets
mapping['modules'] = ','.join([get_class_name_from_type(module_type) for module_type in self.modules])
return mapping
@dispatch()
def get_specified_mapping(self):
full_mapping = self.get_full_mapping()
mapping = dict()
for key in full_mapping.keys():
if key in self._explicit_settings:
mapping[key] = full_mapping[key]
return mapping
@staticmethod
@dispatch(str, Callable)
def add_function(name, func):
Settings._functions[name] = func
@staticmethod
@dispatch(str)
def is_function(str_val):
for name in Settings._functions.keys():
name = name.strip()
if str_val.startswith(name) and str_val[len(name)] == '(' and str_val[-1] == ')':
return True
return False
@staticmethod
@dispatch(str)
def get_function(name):
if name in Settings._functions:
return Settings._functions[name]
else:
raise ValueError()
def generateXML(self):
root = Element('settings')
for key, value in self.get_full_mapping():
param_element = Element(key)
param_element.text(str(value))
root.append(param_element)
return root