-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathnetpyne_geppetto.py
1261 lines (1058 loc) · 52.3 KB
/
netpyne_geppetto.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
netpyne_geppetto.py
Initialise NetPyNE Geppetto, this class contains methods to connect NetPyNE with the Geppetto based UI
"""
import json
import os
from os.path import join, dirname
from glob import glob
import copy
import dataclasses
import importlib
import json
import logging
import os
from pathlib import Path
import pprint
import re
import sys
from shutil import copyfile
from dacite import from_dict
import base64
import jsonpickle
import neuron
import numpy as np
from netpyne import specs, sim, analysis
from netpyne.conversion.neuronPyHoc import mechVarList
from netpyne.metadata import metadata
from netpyne.specs.utils import validateFunction
from netpyne.sim import utils as netpyne_utils
from netpyne_ui import experiments
from netpyne_ui import constants
from netpyne_ui import model
from netpyne_ui import simulations
from netpyne_ui import utils as netpyne_ui_utils
from netpyne_ui.netpyne_model_interpreter import NetPyNEModelInterpreter
from netpyne_ui.simulations import InvalidConfigError
from pygeppetto.model.model_serializer import GeppettoModelSerializer
from pygeppetto import ui
from jupyter_geppetto import jupyter_geppetto, synchronization, utils
from contextlib import redirect_stdout
from netpyne_ui.constants import NETPYNE_WORKDIR_PATH, NUM_CONN_LIMIT
from netpyne_ui.mod_utils import loadModMechFiles
os.chdir(constants.NETPYNE_WORKDIR_PATH)
class NetpyneValidationError(Exception):
...
def deepcopy_wout_empty(d, memo=None):
def is_empty(x):
return x == '' or x == [] or x == () or x == set() or x == {} or x is None
# if is_empty(d):
# return None
memo = {} if memo is None else memo
if id(d) in memo:
return memo[id(d)]
if isinstance(d, dict):
cpy = {}
for k, v in d.items():
v_cpy = deepcopy_wout_empty(v, memo=memo)
if is_empty(v_cpy):
continue
cpy[k] = memo.setdefault(id(v), v_cpy)
return cpy
elif isinstance(d, (list, set, tuple)):
return d.__class__(memo.setdefault(id(v), deepcopy_wout_empty(v, memo=memo)) for v in d if not is_empty(v))
elif hasattr(d, '__dict__'):
cpy = d.__new__(d.__class__) # We skip the initializer, in case it needs some arguments
for k, v in d.__dict__.items():
v_cpy = deepcopy_wout_empty(v, memo=memo)
# if is_empty(v_cpy): #
# continue
cpy.__dict__[k] = memo.setdefault(id(v), v_cpy)
return cpy
return d
class NetPyNEGeppetto:
def __init__(self):
self.model_interpreter = NetPyNEModelInterpreter()
# Geppetto model of a created network
self.geppetto_model = None
self.netParams = specs.NetParams()
self.simConfig = specs.SimConfig()
self.run_config = model.RunConfig()
self.simConfig.recordTraces = {'V_soma': {'sec':'soma', 'loc':0.5, 'var':'v'}}
self.experiments = experiments
model.register(metadata)
synchronization.startSynchronization(self.__dict__)
logging.debug("Initializing the original model")
jupyter_geppetto.context = {'netpyne_geppetto': self}
# Set running experiments without any subprocess to ERRROR
experiments.get_experiments()
running_exps = experiments.get_by_states([
model.ExperimentState.PENDING,
model.ExperimentState.SIMULATING,
model.ExperimentState.INSTANTIATING
])
if not simulations.local.is_running():
[experiments.set_to_error(e) for e in running_exps]
def getData(self):
return {
"metadata": metadata,
"netParams": self.netParams.todict(),
"simConfig": self.simConfig.todict(),
"isDocker": os.path.isfile('/.dockerenv'),
"currentFolder": os.getcwd(),
"tuts": self.find_tutorials(),
"cores": simulations.local.cpus
}
def getModelAsJson(self):
# TODO: netpyne should offer a method asJSON (#240)
# that returns the JSON model without dumping to to disk.
obj = netpyne_utils.replaceFuncObj({"netParams": self.netParams.__dict__, "simConfig": self.simConfig.__dict__})
obj = netpyne_utils.replaceDictODict(obj)
return obj
def get_run_configuration(self):
return dataclasses.asdict(self.run_config)
def edit_run_configuration(self, configDictionary: dict):
self.run_config = from_dict(model.RunConfig, configDictionary)
def cloneExperiment(self, payload: dict):
""" Loads experiment from disk and replaces experiment in design with it.
1. Replaces current experiment in design with copy of stored experiment.
2. Replace current model specification with spec of stored experiment.
:param payload: { name: str, replaceModelSpec: bool, replaceExperiment: bool }
"""
name = payload.get('name')
# Creates new Experiment in design based on `name` experiment.
if payload.get('replaceExperiment', True):
experiments.replace_current_with(name)
# Replaces model specification
if payload.get('replaceModelSpec', True):
path = os.path.join(constants.EXPERIMENTS_FOLDER_PATH, name)
if self.doIhaveInstOrSimData()['haveInstance']:
sim.clearAll()
sim.initialize()
sim.loadNetParams(os.path.join(path, experiments.NET_PARAMS_FILE))
sim.loadSimCfg(os.path.join(path, experiments.SIM_CONFIG_FILE))
self.netParams = sim.net.params
self.simConfig = sim.cfg
netpyne_ui_utils.remove(self.simConfig.todict())
netpyne_ui_utils.remove(self.netParams.todict())
def viewExperimentResult(self, payload: dict):
""" Loads the output file of a simulated experiment trial.
:param payload: {name: str, trial: str, onlyModelSpecification: bool}
:return: geppetto model
"""
name = payload.get("name", None)
trial = payload.get("trial", None)
only_model_spec = payload.get("onlyModelSpecification", False)
file = experiments.get_trial_output_path(name, trial)
if file is None or not os.path.exists(file):
return utils.getJSONError(f"Couldn't find output file of condition. Please take a look at the simulation log.", "")
if self.doIhaveInstOrSimData()['haveInstance']:
sim.clearAll()
sim.initialize()
if only_model_spec:
# Load only model specification
sim.loadNetParams(file)
sim.loadSimCfg(file)
self.netParams = sim.net.params
self.simConfig = sim.cfg
netpyne_ui_utils.remove(self.simConfig.todict())
netpyne_ui_utils.remove(self.netParams.todict())
return
else:
# Load the complete simulation
sim.loadAll(file)
self._create3D_shapes(file)
self.geppetto_model = self.model_interpreter.getGeppettoModel(sim)
return json.loads(GeppettoModelSerializer.serialize(self.geppetto_model))
def stopExperiment(self, experiment_name):
simulations.local.stop()
return {
"message": f"Stopped simulation of {experiment_name}"
}
def find_tutorials(self):
return glob(join(NETPYNE_WORKDIR_PATH, "**/gui_tut*.py"), recursive=True)
def instantiateNetPyNEModelInGeppetto(self, args):
try:
with redirect_stdout(sys.__stdout__):
if not args.get("usePrevInst", False):
if self.doIhaveInstOrSimData()['haveInstance']:
sim.clearAll()
netpyne_model = self.instantiateNetPyNEModel()
self.geppetto_model = self.model_interpreter.getGeppettoModel(netpyne_model)
return json.loads(GeppettoModelSerializer.serialize(self.geppetto_model))
except NetpyneValidationError as e:
message = ("Error while validating the NetPyNE model before instantiation.\n"
"One or more components in your model have issues, see details below:")
logging.exception(message)
return utils.getJSONError(message, '\n'.join(e.args))
except Exception as e:
message = "Error while instantiating the NetPyNE model"
logging.exception(message)
return utils.getJSONError(message, sys.exc_info())
def simulate_experiment_trials(self, experiment: model.Experiment):
try:
working_directory = self._prepare_batch_files(experiment)
except OSError:
experiment.state = model.ExperimentState.ERROR
return utils.getJSONError("The specified folder already exists", "")
logging.info("Running experiment %s combinations", experiment.name)
try:
simulations.run(
platform="local",
parallel=self.run_config.parallel,
cores=self.run_config.cores,
method=self.run_config.type,
batch=True,
asynchronous=self.run_config.asynchronous,
working_directory=working_directory
)
except InvalidConfigError as e:
experiment.state = model.ExperimentState.ERROR
logging.error("Error running experiment %s: %s", experiment.name, str(e))
return utils.getJSONError(str(e), "")
if self.run_config.asynchronous:
message = f"Experiment {experiment.name} started. " \
f"You can view the Experiment status in the Experiment Manager."
else:
message = f"Experiment {experiment.name} finished, you can view the results in the Experiment Manager."
return dict(message=message)
def simulate_single_model(self, experiment: model.Experiment = None, use_prev_inst: bool = False):
if experiment:
working_directory = self._prepare_simulation_files(experiment, use_prev_inst)
simulations.run(
parallel=self.run_config.parallel,
cores=self.run_config.cores,
asynchronous=self.run_config.asynchronous,
method=simulations.MPI_BULLETIN,
working_directory=working_directory,
)
if self.run_config.asynchronous:
message = "Experiment is pending! " \
f"Results will be stored in your workspace at ./{os.path.join(constants.EXPERIMENTS_FOLDER, experiment.name)}"
return dict(message=message)
else:
sim.load(f'{constants.MODEL_OUTPUT_FILENAME}.json')
self.geppetto_model = self.model_interpreter.getGeppettoModel(sim)
response = json.loads(GeppettoModelSerializer.serialize(self.geppetto_model))
return response
else:
# Run in same process
if not use_prev_inst:
logging.debug('Instantiating single thread simulation')
netpyne_model = self.instantiateNetPyNEModel()
self.geppetto_model = self.model_interpreter.getGeppettoModel(netpyne_model)
simulations.run()
if self.geppetto_model:
response = json.loads(GeppettoModelSerializer.serialize(self.geppetto_model))
return response
def validate_netParams(self):
cpy = deepcopy_wout_empty(self.netParams)
_, failed = sim.validator.validateNetParams(cpy)
if failed:
message = ""
components_error = {}
for entry in failed:
components_error.setdefault(entry.component, []).append((entry.keyPath, entry.summary))
for component, details in components_error.items():
message = message + f"* Error validating {component}\n"
for (keyPath, summary) in details:
path = ' -> '.join(f"{key}" for key in keyPath)
message = message + f" Error in {path}\n"
for line in summary:
message = message + f" {line}\n"
message = message + "\n"
raise NetpyneValidationError(message)
def simulateNetPyNEModelInGeppetto(self, args):
""" Starts simulation of the currently loaded NetPyNe model.
* runConfiguration is used to determine asynch/synch & other parameters.
* complete flag in args decides if we simulate single model as Experiment or complete Experiment.
* if Experiment in design does not exist, we create a new one & start single sim.
* All Simulations run in different process.
:param args: { allTrials: bool, usePrevInst: bool }
:return: geppetto model.
"""
allTrials = args.get('allTrials', True)
use_prev_inst = args.get('usePrevInst', False)
sim_id = args.get('simId', 0)
try:
# self.validate_netParams()
experiment = experiments.get_current()
if experiment:
if self.experiments.any_in_state([model.ExperimentState.PENDING, model.ExperimentState.SIMULATING]):
return utils.getJSONError("Experiment is already simulating or pending", "")
if simulations.local.is_running():
simulations.local.stop()
experiment.state = model.ExperimentState.PENDING
try:
if allTrials:
if len(experiment.trials) == 1 and experiment.trials[0].id == experiments.BASE_TRIAL_ID:
# special case where we don't want to run a batch simulation
return self.simulate_single_model(experiment, use_prev_inst)
else:
return self.simulate_experiment_trials(experiment)
else:
return self.simulate_single_model(experiment, use_prev_inst)
except Exception:
experiment.state = model.ExperimentState.ERROR
message = f"Unknown error during simulation of Experiment. SimulationId {sim_id}"
logging.exception(message)
# return utils.getJSONError("Unknown error during simulation of Experiment", sys.exc_info(), { "sim_id": sim_id})
return utils.getJSONError("Unknown error during simulation of Experiment", sys.exc_info())
else:
return self.simulate_single_model(use_prev_inst=use_prev_inst)
except NetpyneValidationError as e:
message = (f"Error while validating the NetPyNE model before simulation {sim_id}.\n"
"One or more components in your model have issues, see details below:")
logging.exception(message)
return utils.getJSONError(message, '\n'.join(e.args))
except Exception as e :
message = f"Error while simulating the NetPyNE model: {e}. SimulationId {sim_id}"
logging.exception(message)
return utils.getJSONError(message, sys.exc_info())
def _prepare_simulation_files(self, experiment: model.Experiment = None, use_prev_inst: bool = False) -> str:
"""Prepares template files and netpyne model files for a single simulation """
exp = copy.deepcopy(experiment)
# Remove parameter & trials for single run
exp.params = []
exp.trials = []
save_folder_path = os.path.join(constants.NETPYNE_WORKDIR_PATH, constants.EXPERIMENTS_FOLDER, exp.name)
try:
os.makedirs(save_folder_path)
except OSError:
raise
if use_prev_inst:
sim.cfg.saveJson = True
oldName = sim.cfg.filename
sim.cfg.filename = constants.MODEL_OUTPUT_FILENAME
# workaround for issue with empty LFP dict when calling saveData()
if 'LFP' in sim.allSimData:
del sim.allSimData['LFP']
# TODO: store in experiments folder!
sim.saveData()
sim.cfg.filename = oldName
template_name = constants.TEMPLATE_FILENAME_SINGLE_RUN_INSTANTIATED
else:
# Create netParams and SimConfig
self.netParams.save(os.path.join(save_folder_path, experiments.NET_PARAMS_FILE))
simCfg = copy.copy(self.simConfig)
# filename and simLabel must be set to define the output filename
simCfg.saveJson = True
simCfg.filename = 'model_output'
simCfg.simLabel = 'model_output'
simCfg.saveDataInclude = [
"simData",
"simConfig",
"netParams",
"net"
]
simCfg.save(os.path.join(save_folder_path, experiments.SIM_CONFIG_FILE))
template_name = constants.TEMPLATE_FILENAME_SINGLE_RUN
# Create Experiment Config
config_dict = dataclasses.asdict(exp)
config_dict["runCfg"] = dataclasses.asdict(self.run_config)
experiment_config = os.path.join(save_folder_path, experiments.EXPERIMENT_FILE)
json.dump(config_dict, open(experiment_config, 'w'), default=str, sort_keys=True, indent=4)
# Copy Template
template = os.path.join(os.path.dirname(__file__), "templates", template_name)
copyfile(template, os.path.join(save_folder_path, constants.SIMULATION_SCRIPT_NAME))
return save_folder_path
def _prepare_batch_files(self, experiment: model.Experiment) -> str:
"""Creates template files and netpyne model files in the experiment folder.
Only for an experiment consisting of many trials.
:param experiment: given experiment
:return: working directory path
"""
exp = copy.deepcopy(experiment)
exp.params = self.experiments.process_params(exp.params)
netParams = copy.deepcopy(self.netParams)
netParams.mapping = {p.mapsTo.replace('netParams.', ''): p.mapsTo.split('.')[1::] for p in exp.params if 'netParams' in p.mapsTo}
simCfg = copy.copy(self.simConfig)
simCfg.saveJson = True
simCfg.saveDataInclude = [
"simData",
"simConfig",
"netParams",
"net"
]
config_dict = dataclasses.asdict(exp)
config_dict["runCfg"] = dataclasses.asdict(self.run_config)
save_folder_path = os.path.join(constants.NETPYNE_WORKDIR_PATH, constants.EXPERIMENTS_FOLDER, exp.name)
try:
os.makedirs(save_folder_path)
except OSError:
raise
simCfg.save(os.path.join(save_folder_path, experiments.SIM_CONFIG_FILE))
netParams.save(os.path.join(save_folder_path, experiments.NET_PARAMS_FILE))
experiment_json = os.path.join(save_folder_path, experiments.EXPERIMENT_FILE)
json.dump(config_dict, open(experiment_json, 'w'), default=str, sort_keys=True, indent=4)
template_single_run = os.path.join(os.path.dirname(__file__), "templates", constants.TEMPLATE_FILENAME_BATCH_RUN)
template_batch = os.path.join(os.path.dirname(__file__), "templates", constants.TEMPLATE_FILENAME_BATCH)
copyfile(template_single_run, os.path.join(save_folder_path, 'run.py'))
# TODO: name should be batch.py not init.py
copyfile(template_batch, os.path.join(save_folder_path, constants.SIMULATION_SCRIPT_NAME))
return save_folder_path
def loadModel(self, args):
""" Imports a model stored as file in json format.
:param args:
:return:
"""
if not any([args[option] for option in ['loadNetParams', 'loadSimCfg', 'loadSimData', 'loadNet']]):
return utils.getJSONError("Error while loading data", 'You have to select at least one option')
try:
owd = os.getcwd()
loadModMechFiles(args['compileMod'], args['modFolder'])
except Exception:
message = "Error while importing/compiling mods"
logging.exception(message)
return utils.getJSONError(message, sys.exc_info())
finally:
os.chdir(owd)
try:
with redirect_stdout(sys.__stdout__):
sim.initialize()
wake_up_geppetto = False
if all([args[option] for option in ['loadNetParams', 'loadSimCfg', 'loadSimData', 'loadNet']]):
wake_up_geppetto = True
if self.doIhaveInstOrSimData()['haveInstance']:
sim.clearAll()
sim.initialize()
sim.loadAll(args['jsonModelFolder'], instantiate=False)
self.netParams = sim.net.params
self.simConfig = sim.cfg
self.simConfig.saveCellSecs = True
netpyne_ui_utils.remove(self.netParams.todict())
netpyne_ui_utils.remove(self.simConfig.todict())
else:
if args['loadNet']:
wake_up_geppetto = True
if self.doIhaveInstOrSimData()['haveInstance']:
sim.clearAll()
sim.initialize()
sim.loadNet(args['jsonModelFolder'])
# TODO (https://github.com/Neurosim-lab/netpyne/issues/360)
if args['loadSimData']:
wake_up_geppetto = True
if not self.doIhaveInstOrSimData()['haveInstance']:
sim.create(specs.NetParams(), specs.SimConfig())
sim.net.defineCellShapes()
sim.gatherData(gatherLFP=False)
sim.loadSimData(args['jsonModelFolder'])
if args['loadSimCfg']:
sim.loadSimCfg(args['jsonModelFolder'])
self.simConfig = sim.cfg
netpyne_ui_utils.remove(self.simConfig.todict())
if args['loadNetParams']:
if self.doIhaveInstOrSimData()['haveInstance']:
sim.clearAll()
sim.loadNetParams(args['jsonModelFolder'])
self.netParams = sim.net.params
netpyne_ui_utils.remove(self.netParams.todict())
if wake_up_geppetto:
self._create3D_shapes(args['jsonModelFolder'])
# TODO: Fix me - gatherData will remove allSimData!
sim.gatherData()
self.geppetto_model = self.model_interpreter.getGeppettoModel(sim)
return json.loads(GeppettoModelSerializer.serialize(self.geppetto_model))
else:
return utils.getJSONReply()
except Exception:
message = "Error while loading the NetPyNE model"
logging.exception(message)
return utils.getJSONError(message, sys.exc_info())
def _create3D_shapes(self, json_path: str):
""" Creates cellShapes for 3D viewer.
Performed as final step after `json_path` was loaded.
"""
if len(sim.net.cells) > 0:
section = list(sim.net.cells[0].secs.keys())[0]
if 'pt3d' not in list(sim.net.cells[0].secs[section].geom.keys()):
sim.net.defineCellShapes()
sim.gatherData()
# Load again because gatherData removed simData
sim.loadSimData(json_path)
def loadFromIndexFile(self, json_path: str):
cfg, netParams = sim.loadModel(json_path, loadMechs=True, ignoreMechAlreadyExistsError=True)
self.simConfig = cfg
self.netParams = netParams
if isinstance(self.netParams, dict):
self.netParams = specs.NetParams(self.netParams)
if isinstance(self.simConfig, dict):
self.simConfig = specs.SimConfig(self.simConfig)
for key, value in self.netParams.cellParams.items():
if hasattr(value, 'todict'):
self.netParams.cellParams[key] = value.todict()
# TODO: when should sim.initialize be called?
# Only on import or better before every simulation or network instantiation?
sim.initialize()
def saveToIndexFile(self, srcPath, dstPath, exportNetParamsAsPython, exportSimConfigAsPython):
sim.saveModel(netParams=self.netParams,
simConfig=self.simConfig,
srcPath=srcPath,
dstPath=dstPath,
exportNetParamsAsPython=exportNetParamsAsPython,
exportSimConfigAsPython=exportSimConfigAsPython)
def importModel(self, modelParameters):
""" Imports a model stored in form of Python files.
:param modelParameters:
:return:
"""
if self.doIhaveInstOrSimData()['haveInstance']:
# TODO: this must be integrated into the general lifecycle of "model change -> simulate"
# Shouldn't be specific to Import
sim.clearAll()
try:
loadModMechFiles(modelParameters['compileMod'], modelParameters['modFolder'], modelParameters.get("forceRecompile", True))
except Exception:
message = "Error while importing/compiling mods"
logging.exception(message)
return utils.getJSONError(message, sys.exc_info())
try:
# Get Current dir
owd = os.getcwd()
with redirect_stdout(sys.__stdout__):
# NetParams
net_params_path = str(modelParameters["netParamsPath"])
sys.path.append(net_params_path)
os.chdir(net_params_path)
# Import Module
net_params_module_name = importlib.import_module(str(modelParameters["netParamsModuleName"]))
# Import Model attributes
self.netParams = getattr(net_params_module_name, str(modelParameters["netParamsVariable"]))
if isinstance(self.netParams, dict):
self.netParams = specs.NetParams(self.netParams)
if isinstance(self.simConfig, dict):
self.simConfig = specs.SimConfig(self.simConfig)
for key, value in self.netParams.cellParams.items():
if hasattr(value, 'todict'):
self.netParams.cellParams[key] = value.todict()
# SimConfig
sim_config_path = str(modelParameters["simConfigPath"])
sys.path.append(sim_config_path)
os.chdir(sim_config_path)
# Import Module
sim_config_module_name = importlib.import_module(str(modelParameters["simConfigModuleName"]))
# Import Model attributes
self.simConfig = getattr(sim_config_module_name, str(modelParameters["simConfigVariable"]))
# TODO: when should sim.initialize be called?
# Only on import or better before every simulation or network instantiation?
sim.initialize()
return utils.getJSONReply()
except:
message = "Error while importing the NetPyNE model"
logging.exception(message)
return utils.getJSONError(message, sys.exc_info())
finally:
os.chdir(owd)
def importNeuroML(self, modelParameters):
from netpyne_ui.helpers import neuroml
try:
# Get Current dir
owd = os.getcwd()
with redirect_stdout(sys.__stdout__):
# NetParams
filename = str(modelParameters["fileName"])
json_fname = neuroml.convertNeuroML2(filename, compileMod=modelParameters["compileMod"])
return self.loadModel(args=dict(
compileMod=True,
modFolder=os.path.dirname(json_fname),
jsonModelFolder=json_fname,
loadNet=True,
loadSimData=True,
loadSimCfg=True,
loadNetParams=True
))
except:
message = "Error while importing the NetPyNE model"
logging.exception(message)
return utils.getJSONError(message, sys.exc_info())
finally:
os.chdir(owd)
def importLEMS(self, modelParameters):
from netpyne_ui.helpers import neuroml
try:
# Get Current dir
owd = os.getcwd()
with redirect_stdout(sys.__stdout__):
# NetParams
filename = str(modelParameters["fileName"])
json_fname = neuroml.convertLEMSSimulation(filename)
return self.loadModel(args=dict(
compileMod=True,
modFolder=os.path.dirname(json_fname),
jsonModelFolder=json_fname,
loadNet=True,
loadSimData=True,
loadSimCfg=True,
loadNetParams=True
))
except Exception:
message = "Error while importing the NetPyNE model"
logging.exception(message)
return utils.getJSONError(message, sys.exc_info())
finally:
os.chdir(owd)
def importCellTemplate(self, modelParameters):
try:
with redirect_stdout(sys.__stdout__):
rule = modelParameters["label"]
# Get Current dir
owd = os.getcwd()
conds = {} if rule not in self.netParams.cellParams else self.netParams.cellParams[rule]['conds']
loadModMechFiles(modelParameters["compileMod"], modelParameters["modFolder"])
del modelParameters["modFolder"]
del modelParameters["compileMod"]
# import cell template
self.netParams.importCellParams(**modelParameters, conds=conds)
# convert fron netpyne.specs.dict to dict
self.netParams.cellParams[rule] = self.netParams.cellParams[rule].todict()
return utils.getJSONReply()
except Exception:
message = "Error while importing the NetPyNE cell template"
logging.exception(message)
return utils.getJSONError(message, sys.exc_info())
finally:
os.chdir(owd)
def exportModel(self, args):
try:
with redirect_stdout(sys.__stdout__):
if not args['netCells']:
sim.initialize(netParams=self.netParams, simConfig=self.simConfig)
sim.cfg.filename = args['fileName']
sim_config_data_include = specs.SimConfig().saveDataInclude
include = [el for el in sim_config_data_include if args.get(el, False)]
if args['netCells']: include += ['netPops']
sim.cfg.saveJson = True
sim.saveData(include)
sim.cfg.saveJson = False
with open(f"{sim.cfg.filename}_data.json") as json_file:
data = json.load(json_file)
return data
except Exception:
message = "Error while exporting the NetPyNE model"
logging.exception(message)
return utils.getJSONError(message, sys.exc_info())
def exportNeuroML(self, modelParams):
try:
with redirect_stdout(sys.__stdout__):
sim.exportNeuroML2(modelParams['fileName'], specs.SimConfig())
return utils.getJSONReply()
except Exception:
message = "Error while exporting the NetPyNE model"
logging.exception(message)
return utils.getJSONError(message, sys.exc_info())
def importNeuroMLOLD(self, modelParams):
try:
with redirect_stdout(sys.__stdout__):
sim.initialize()
sim.importNeuroML2(modelParams['neuroMLFolder'], simConfig=specs.SimConfig(), simulate=False,
analyze=False)
self.geppetto_model = self.model_interpreter.getGeppettoModel(sim)
return json.loads(GeppettoModelSerializer.serialize(self.geppetto_model))
except Exception:
message = "Error while exporting the NetPyNE model"
logging.exception(message)
return utils.getJSONError(message, sys.exc_info())
def deleteModel(self, modelParams):
try:
with redirect_stdout(sys.__stdout__):
self.netParams = specs.NetParams()
self.simConfig = specs.SimConfig()
sim.initialize(specs.NetParams(), specs.SimConfig())
self.geppetto_model = None
except Exception:
message = "Error while exporting the NetPyNE model"
logging.exception(message)
return utils.getJSONError(message, sys.exc_info())
try:
sim.clearAll()
except:
logging.exception("Failed to clear simulation")
return utils.getJSONReply()
def instantiateNetPyNEModel(self):
# self.validate_netParams()
with redirect_stdout(sys.__stdout__):
saveData = sim.allSimData if hasattr(sim, 'allSimData') and 'spkt' in sim.allSimData.keys() and len(
sim.allSimData['spkt']) > 0 else False
#netcoded = jsonpickle.encode(self.netParams, unpicklable=False)
#simcoded = jsonpickle.encode(self.simConfig, unpicklable=False)
sim.create(self.netParams, self.simConfig)
sim.net.defineCellShapes() # creates 3d pt for cells with stylized geometries
sim.gatherData(gatherLFP=False)
if saveData:
sim.allSimData = saveData # preserve data from previous simulation
return sim
def doIhaveInstOrSimData(self):
""" Telling if we have an instance or simulated data.
return [bool, bool]
"""
with redirect_stdout(sys.__stdout__):
out = [False, False]
if hasattr(sim, 'net'):
if hasattr(sim.net, 'cells') and hasattr(sim.net, 'pops'):
if len(sim.net.cells) > 0 and len(sim.net.pops.keys()) > 0:
out[0] = True
if hasattr(sim, 'allSimData'):
if 'spkt' in sim.allSimData.keys() and 'spkid' in sim.allSimData.keys():
if len(sim.allSimData['spkt']) > 0 and len(sim.allSimData['spkid']) > 0:
out[1] = True
return {'haveInstance': out[0], 'haveSimData': out[1]}
def rename(self, path, oldValue, newValue):
# command = 'sim.rename(self.' + path + ',"' + oldValue + '","' + newValue + '")'
command = f'sim.rename(self.{path}, {oldValue!r}, {newValue!r})'
logging.debug('renaming ' + command)
eval(command)
for model, synched_component in list(jupyter_geppetto.synched_models.items()):
if model != '' and oldValue in model and path in model: #
jupyter_geppetto.synched_models.pop(model)
new_model = re.sub("(['])(?:(?=(\\?))\2.)*?\1",
lambda x: x.group(0).replace(oldValue, newValue, 1),
model)
logging.debug("Rename funct - Model is " + model + " newModel is " + new_model)
jupyter_geppetto.synched_models[new_model] = synched_component
with redirect_stdout(sys.__stdout__):
if "popParams" in path:
self.propagate_field_rename("pop", newValue, oldValue)
elif "stimSourceParams" in path:
self.propagate_field_rename("source", newValue, oldValue)
elif "synMechParams" in path:
self.propagate_field_rename("synMech", newValue, oldValue)
return 1
def getPlotSettings(self, plot_name):
if self.simConfig.analysis and plot_name in self.simConfig.analysis:
return self.simConfig.analysis[plot_name]
return {}
def checkFileExists(self, path):
path = Path(path or '')
return path.exists()
def getFullPath(self, dir, subDir):
if dir is None or dir == '':
base = constants.NETPYNE_WORKDIR_PATH
if subDir:
base = os.path.join(base, subDir)
dir = os.path.join(os.getcwd(), base)
return dir
def getDirList(self, dir=None, onlyDirs=False, filterFiles=False, subDir=None):
# Get Current dir
dir = self.getFullPath(dir, subDir)
dir_list = []
file_list = []
for f in sorted(os.listdir(str(dir)), key=str.lower):
ff = os.path.join(dir, f)
if os.path.isdir(ff):
dir_list.append({'title': f, 'path': ff, 'load': False, 'children': [{'title': 'Loading...'}]})
elif not onlyDirs:
if not filterFiles or os.path.isfile(ff) and ff.endswith(filterFiles):
file_list.append({'title': f, 'path': ff})
return dir_list + file_list
def checkAvailablePlots(self):
return analysis.checkAvailablePlots()
def getPlot(self, plotName, LFPflavour, theme='gui'):
try:
with redirect_stdout(sys.__stdout__):
availablePlots = self.checkAvailablePlots()
checkCondition = availablePlots.get(plotName.replace('iplot', 'plot'), False)
if checkCondition is False:
logging.info("Plot " + plotName + " not available")
return -1
args = self.getPlotSettings(plotName)
if LFPflavour:
args['plots'] = [LFPflavour]
args['showFig'] = False
if plotName.startswith('iplot'):
# This arg brings dark theme. But some plots are broken by it
args['theme'] = theme
if plotName in ("iplotConn", "iplot2Dnet") and hasattr(sim, 'net') and sim.net.allCells:
def conns_length(cell) -> int:
if type(cell) is dict:
return len(cell.get('conns', []))
else:
return len(getattr(cell, 'conns', []))
# To prevent unresponsive kernel, we don't show conns if they become too many
num_conn = sum([conns_length(cell) for cell in sim.net.allCells])
if num_conn > NUM_CONN_LIMIT:
args["showConns"] = False
html = getattr(analysis, plotName)(**args)
if not html or html == -1:
return ""
# some plots return "fig", some return "(fig, data)"
if plotName in ('iplotRaster', 'iplotRxDConcentration', 'iplot2Dnet'):
html = html[0]
return html
else:
fig_data = getattr(analysis, plotName)(**args)
if isinstance(fig_data, tuple):
fig = fig_data[0]
if fig == -1:
return fig
elif isinstance(fig, list):
return [ui.getSVG(fig[0])]
elif isinstance(fig, dict):
svgs = []
for key, value in fig.items():
svgs.append(ui.getSVG(value))
return svgs
else:
return [ui.getSVG(fig)]
else:
if plotName == 'plotEEG':
return self.simConfig.filename + '_EEG.png'
elif plotName == 'plotDipole':
return self.simConfig.filename + '_dipole.png'
else:
return fig_data
except Exception as e:
err = "There was an exception in %s():" % (e.plotName)
logging.exception(("%s \n %s \n%s" % (err, e, sys.exc_info())))
def getAvailablePops(self):
return list(self.netParams.popParams.keys())
def getAvailableCellModels(self):
cell_models = set([])
for p in self.netParams.popParams:
if 'cellModel' in self.netParams.popParams[p]:
cm = self.netParams.popParams[p]['cellModel']
if cm not in cell_models:
cell_models.add(cm)
return list(cell_models)
def getAvailableCellModels(self):
return ["", "VecStim", "NetStim", "IntFire1"]
def getAvailableStimulationDistribution(self):
return ["normal", "uniform"]
def getAvailableStimulationPattern(self):
# self.netParams.popParams[name]['spikePattern'] = {}
return ["", "rhythmic", "evoked", "poisson", "gauss"]
def getAvailableSections(self):
sections = {}
for cellRule in self.netParams.cellParams:
sections[cellRule] = list(self.netParams.cellParams[cellRule]['secs'].keys())
return sections
def getAvailableCellTypes(self):
cell_types = set([])
cell_types.add('all')