Skip to content

Commit f8fd4cf

Browse files
committed
NETPYNE-193 Add validation on intantiation
1 parent 7b95723 commit f8fd4cf

1 file changed

Lines changed: 51 additions & 8 deletions

File tree

netpyne_ui/netpyne_geppetto.py

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,40 @@
4747
os.chdir(constants.NETPYNE_WORKDIR_PATH)
4848

4949

50-
class NepyneValidationError(Exception):
50+
class NetpyneValidationError(Exception):
5151
...
5252

5353

54+
def deepcopy_wout_empty(d, memo=None):
55+
def is_empty(x):
56+
return x == '' or x == [] or x == () or x == set() or x == {} or x is None
57+
58+
# if is_empty(d):
59+
# return None
60+
memo = {} if memo is None else memo
61+
if id(d) in memo:
62+
return memo[id(d)]
63+
if isinstance(d, dict):
64+
cpy = {}
65+
for k, v in d.items():
66+
v_cpy = deepcopy_wout_empty(v, memo=memo)
67+
if is_empty(v_cpy):
68+
continue
69+
cpy[k] = memo.setdefault(id(v), v_cpy)
70+
return cpy
71+
elif isinstance(d, (list, set, tuple)):
72+
return d.__class__(memo.setdefault(id(v), deepcopy_wout_empty(v, memo=memo)) for v in d if not is_empty(v))
73+
elif hasattr(d, '__dict__'):
74+
cpy = d.__new__(d.__class__) # We skip the initializer, in case it needs some arguments
75+
for k, v in d.__dict__.items():
76+
v_cpy = deepcopy_wout_empty(v, memo=memo)
77+
# if is_empty(v_cpy): #
78+
# continue
79+
cpy.__dict__[k] = memo.setdefault(id(v), v_cpy)
80+
return cpy
81+
return d
82+
83+
5484
class NetPyNEGeppetto:
5585

5686
def __init__(self):
@@ -189,6 +219,11 @@ def instantiateNetPyNEModelInGeppetto(self, args):
189219
self.geppetto_model = self.model_interpreter.getGeppettoModel(netpyne_model)
190220

191221
return json.loads(GeppettoModelSerializer.serialize(self.geppetto_model))
222+
except NetpyneValidationError as e:
223+
message = ("Error while validating the NetPyNE model before instantiation.\n"
224+
"One or more components in your model have issues, see details below:")
225+
logging.exception(message)
226+
return utils.getJSONError(message, '\n'.join(e.args))
192227
except Exception as e:
193228
message = "Error while instantiating the NetPyNE model"
194229
logging.exception(message)
@@ -260,22 +295,24 @@ def simulate_single_model(self, experiment: model.Experiment = None, use_prev_in
260295
response = json.loads(GeppettoModelSerializer.serialize(self.geppetto_model))
261296
return response
262297

298+
263299
def validate_netParams(self):
264-
_, failed = sim.validator.validateNetParams(self.netParams)
300+
cpy = deepcopy_wout_empty(self.netParams)
301+
_, failed = sim.validator.validateNetParams(cpy)
265302
if failed:
266-
message = "One or more components in your model have issues, see details below:\n"
303+
message = ""
267304
components_error = {}
268305
for entry in failed:
269306
components_error.setdefault(entry.component, []).append((entry.keyPath, entry.summary))
270307
for component, details in components_error.items():
271-
message = message + f" * Error validating {component}\n"
308+
message = message + f"* Error validating {component}\n"
272309
for (keyPath, summary) in details:
273-
path = ' -> '.join(f"{key!r}" for key in keyPath)
274-
message = message + f" Error in {path}\n"
310+
path = ' -> '.join(f"{key}" for key in keyPath)
311+
message = message + f" Error in {path}\n"
275312
for line in summary:
276-
message = message + f" {line}\n"
313+
message = message + f" {line}\n"
277314
message = message + "\n"
278-
raise NepyneValidationError(message)
315+
raise NetpyneValidationError(message)
279316

280317

281318
def simulateNetPyNEModelInGeppetto(self, args):
@@ -323,6 +360,11 @@ def simulateNetPyNEModelInGeppetto(self, args):
323360

324361
else:
325362
return self.simulate_single_model(use_prev_inst=use_prev_inst)
363+
except NetpyneValidationError as e:
364+
message = (f"Error while validating the NetPyNE model before simualtion {sim_id}.\n"
365+
"One or more components in your model have issues, see details below:")
366+
logging.exception(message)
367+
return utils.getJSONError(message, '\n'.join(e.args))
326368

327369
except Exception as e :
328370
message = f"Error while simulating the NetPyNE model: {e}. SimulationId {sim_id}"
@@ -763,6 +805,7 @@ def deleteModel(self, modelParams):
763805
return utils.getJSONReply()
764806

765807
def instantiateNetPyNEModel(self):
808+
self.validate_netParams()
766809
with redirect_stdout(sys.__stdout__):
767810
saveData = sim.allSimData if hasattr(sim, 'allSimData') and 'spkt' in sim.allSimData.keys() and len(
768811
sim.allSimData['spkt']) > 0 else False

0 commit comments

Comments
 (0)