Skip to content

Commit 6bb493b

Browse files
committed
Added auto resolving for monitors
1 parent 19bf502 commit 6bb493b

File tree

3 files changed

+67
-33
lines changed

3 files changed

+67
-33
lines changed

ngclearn/components/base_monitor.py

+47-33
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ class Base_Monitor(Component):
4242
4343
default_window_length: The default window length.
4444
"""
45+
auto_resolve = False
4546

46-
_singleton = None # Only one Monitor
4747

4848
@staticmethod
4949
def build_advance(compartments):
@@ -62,14 +62,19 @@ def build_advance(compartments):
6262
"ngclearn.components.lava (If using lava)")
6363

6464
@staticmethod
65-
def build_reset(compartments):
65+
def build_reset(component):
6666
"""
6767
A method to build the method to reset the stored values.
6868
Args:
69-
compartments: A list of compartments to reset
69+
component: The component to resolve
7070
71-
Returns: The method to reset the stored values.
71+
Returns: the reset resolver
7272
"""
73+
output_compartments = []
74+
compartments = []
75+
for comp in component.compartments:
76+
output_compartments.append(comp.split("/")[-1] + "*store")
77+
compartments.append(comp.split("/")[-1])
7378

7479
@staticmethod
7580
def _reset(**kwargs):
@@ -79,13 +84,22 @@ def _reset(**kwargs):
7984
return_vals.append(np.zeros(current_store.shape))
8085
return return_vals if len(compartments) > 1 else return_vals[0]
8186

82-
return _reset
87+
# pure func, output compartments, args, params, input compartments
88+
return _reset, output_compartments, [], [], output_compartments
89+
90+
@staticmethod
91+
def build_advance_state(component):
92+
output_compartments = []
93+
compartments = []
94+
for comp in component.compartments:
95+
output_compartments.append(comp.split("/")[-1] + "*store")
96+
compartments.append(comp.split("/")[-1])
97+
98+
_advance = component.build_advance(compartments)
99+
100+
return _advance, output_compartments, [], [], compartments + output_compartments
83101

84102
def __init__(self, name, default_window_length=100, **kwargs):
85-
if Base_Monitor._singleton is not None:
86-
critical("Only one monitor can be built")
87-
else:
88-
Base_Monitor._singleton = True
89103
super().__init__(name, **kwargs)
90104
self.store = {}
91105
self.compartments = []
@@ -127,7 +141,7 @@ def watch(self, compartment, window_length):
127141
setattr(self, store_comp_key, new_comp_store)
128142
self.compartments.append(new_comp.path)
129143
self._sources.append(compartment)
130-
self._update_resolver()
144+
# self._update_resolver()
131145

132146
def halt(self, compartment):
133147
"""
@@ -157,29 +171,29 @@ def halt_all(self):
157171
for compartment in self._sources:
158172
self.halt(compartment)
159173

160-
def _update_resolver(self):
161-
output_compartments = []
162-
compartments = []
163-
for comp in self.compartments:
164-
output_compartments.append(comp.split("/")[-1] + "*store")
165-
compartments.append(comp.split("/")[-1])
166-
167-
args = []
168-
parameters = []
169-
170-
add_component_resolver(self.__class__.__name__, "advance_state",
171-
(self.build_advance(compartments),
172-
output_compartments))
173-
add_resolver_meta(self.__class__.__name__, "advance_state",
174-
(args, parameters,
175-
compartments + [o for o in output_compartments],
176-
False))
177-
178-
add_component_resolver(self.__class__.__name__, "reset", (
179-
self.build_reset(compartments), output_compartments))
180-
add_resolver_meta(self.__class__.__name__, "reset",
181-
(args, parameters, [o for o in output_compartments],
182-
False))
174+
# def _update_resolver(self):
175+
# output_compartments = []
176+
# compartments = []
177+
# for comp in self.compartments:
178+
# output_compartments.append(comp.split("/")[-1] + "*store")
179+
# compartments.append(comp.split("/")[-1])
180+
#
181+
# args = []
182+
# parameters = []
183+
#
184+
# add_component_resolver(self.__class__.__name__, "advance_state",
185+
# (self.build_advance(compartments),
186+
# output_compartments))
187+
# add_resolver_meta(self.__class__.__name__, "advance_state",
188+
# (args, parameters,
189+
# compartments + [o for o in output_compartments],
190+
# False))
191+
192+
# add_component_resolver(self.__class__.__name__, "reset", (
193+
# self.build_reset(compartments), output_compartments))
194+
# add_resolver_meta(self.__class__.__name__, "reset",
195+
# (args, parameters, [o for o in output_compartments],
196+
# False))
183197

184198
def _add_path(self, path):
185199
_path = path.split("/")[1:]

ngclearn/components/lava/monitor.py

+10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ class Monitor(Base_Monitor):
55
"""
66
A numpy implementation of `Base_Monitor`. Designed to be used with all lava compatible ngclearn components
77
"""
8+
auto_resolve = False
9+
810

911
@staticmethod
1012
def build_advance(compartments):
@@ -20,3 +22,11 @@ def _advance(**kwargs):
2022
return return_vals if len(compartments) > 1 else return_vals[0]
2123

2224
return _advance
25+
26+
@staticmethod
27+
def build_advance_state(component):
28+
return super().build_advance_state(component)
29+
30+
@staticmethod
31+
def build_reset(component):
32+
return super().build_reset(component)

ngclearn/components/monitor.py

+10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ class Monitor(Base_Monitor):
55
A jax implementation of `Base_Monitor`. Designed to be used with all
66
non-lava ngclearn components
77
"""
8+
auto_resolve = False
9+
810
@staticmethod
911
def build_advance(compartments):
1012
@staticmethod
@@ -18,3 +20,11 @@ def _advance(**kwargs):
1820
return_vals.append(current_store)
1921
return return_vals if len(compartments) > 1 else return_vals[0]
2022
return _advance
23+
24+
@staticmethod
25+
def build_advance_state(component):
26+
return super().build_advance_state(component)
27+
28+
@staticmethod
29+
def build_reset(component):
30+
return super().build_reset(component)

0 commit comments

Comments
 (0)