Description of problem
One cannot use store() or restore() in the standalone modes. I want to restore the saved states from standalone mode in a cython initiation of network, for quick runs/ testing and not necessarily longer runs for which I used standalone initially. I just want to restore the network, and use the plotting codes I wrote for Cython implementation.
Minimal code to reproduce problem
#in cpp_standalone/ cuda_standalone
state = {}
for obj in self.network.sorted_objects:
print(f"Saving state for {obj.name}")
if hasattr(obj, 'get_states'):
state[obj.name] = obj.get_states()
with open(filename, 'wb') as f:
pickle.dump(state, f)
with open(filename.replace('.pkl', '_monitors.pkl'), 'wb') as f:
pickle.dump(monitor_states, f)
#in non standalone (cython) implementation:
#assuming restoring the exact network structure (say, from a stored file having the initial states and weights)
with open(filename, 'rb') as f:
state = pickle.load(f)
for obj in self.network.sorted_objects:
if obj.name in state:
print(f"Restoring state for {obj.name}")
if hasattr(obj, '_restore_from_full_state'):
new_state = {}
for i in state[obj.name]:
temp_entry = state[obj.name][i]
temp_entry = (temp_entry, temp_entry.shape) #because this is how the input to _restore_from_full_state is needed.
new_state[i] = temp_entry
obj._restore_from_full_state(new_state)
What you have aready tried
The above code.
I tried changing the groups.py _restore_from_full_state() function to:
def _restore_from_full_state(self, state):
for var_name, (values, size) in state.items():
print(f"Restoring variable {var_name} with size {size}")
var = self.variables[var_name]
if isinstance(var, DynamicArrayVariable):
var.resize(size)
try:
var.set_value(values)
except Exception as e:
print(f"Error restoring variable {var_name}: {e}")
continue
To see what error arise when doing it manually.
Expected output (if relevant)
Actual output (if relevant)
Restoring variable N_incoming with size (3600,)
Error restoring state for synapses: '>' not supported between instances of 'tuple' and 'int'
Full traceback of error (if relevant)
Traceback when not catching exceptions:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[3], line 1
----> 1 net.load_state(filename='./sim_data/final_network_state.pkl')
File ~/DHC/Simulator/NetworkInit2.py:1367, in NetworkInit.load_state(self, filename, monitor_filename)
1365 temp_entry = (temp_entry, temp_entry.shape)
1366 new_state[i] = temp_entry
-> 1367 obj._restore_from_full_state(new_state)
1368 else:
1369 for var, values in state[obj.name].items():
File ~/miniconda3/envs/myenv/lib/python3.9/site-packages/brian2/groups/group.py:653, in VariableOwner._restore_from_full_state(self, state)
651 var = self.variables[var_name]
652 if isinstance(var, DynamicArrayVariable):
--> 653 var.resize(size)
654 var.set_value(values)
File ~/miniconda3/envs/myenv/lib/python3.9/site-packages/brian2/core/variables.py:660, in DynamicArrayVariable.resize(self, new_size)
658 self.device.resize_along_first(self, new_size)
659 else:
--> 660 self.device.resize(self, new_size)
662 self.size = new_size
File ~/miniconda3/envs/myenv/lib/python3.9/site-packages/brian2/devices/device.py:535, in RuntimeDevice.resize(self, var, new_size)
534 def resize(self, var, new_size):
--> 535 self.arrays[var].resize(new_size)
File ~/miniconda3/envs/myenv/lib/python3.9/site-packages/brian2/memory/dynamicarray.py:199, in DynamicArray1D.resize(self, newshape)
197 def resize(self, newshape):
198 (datashape,) = self._data.shape
--> 199 if newshape > datashape:
200 (shape,) = self.shape # we work with int shapes only
201 newdatashape = max(newshape, int(shape * self.factor) + 1)
TypeError: '>' not supported between instances of 'tuple' and 'int'
Please don’t mind the methods called, before _restore_from_full_state()