Skip to content

Commit

Permalink
Add option to cast MozaikExtendedParameterSet to dict
Browse files Browse the repository at this point in the history
  • Loading branch information
rozsatib committed Jan 11, 2024
1 parent 35185da commit 10f82f9
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions mozaik/tools/distribution_parametrization.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from collections import OrderedDict
import mozaik
import sys
import json

def load_parameters(parameter_url,modified_parameters=ParameterSet({})):
"""
Expand Down Expand Up @@ -201,3 +202,26 @@ def instanciate_objects(self, classes):
else:
v.instanciate_objects(classes = classes)

def to_dict(self):
"""
Cast to json serializable dictionary while dealing with PyNNDistributions
and MozaikExtendedParameterSets. Use a stack to avoid explicit recursion.
"""
import copy
d = copy.deepcopy(dict(self))
stack = [(d, k, v) for k, v in d.items()]

while stack:
parent, key, value = stack.pop()
if isinstance(value, dict):
stack.extend((value, k, v) for k, v in value.items())
elif isinstance(value, PyNNDistribution):
parent[key] = {
'class_name': 'PyNNDistribution',
'params': {
'name': value.__dict__['name'],
},
}
for pk, pv in value.__dict__['parameters'].items():
parent[key]['params'][pk] = pv
return d

0 comments on commit 10f82f9

Please sign in to comment.