|
1 | | -from neuron import h, gui |
| 1 | +from neuron import h, gui, coreneuron |
| 2 | +from pathlib import Path |
| 3 | +from typing import List |
| 4 | + |
2 | 5 |
|
3 | 6 | def inspect(v): |
4 | 7 | print(v, type(v), id(v)) |
@@ -31,79 +34,139 @@ def __init__(self, gid): |
31 | 34 |
|
32 | 35 |
|
33 | 36 | # function to register section-segment mapping with bbcore write |
34 | | -def setup_nrnbbcore_register_mapping(rings): |
| 37 | +def setup_nrnbbcore_register_mapping(gid): |
35 | 38 |
|
36 | 39 | #for recording |
37 | 40 | recordlist = [] |
38 | 41 |
|
39 | 42 | pc = h.ParallelContext() |
40 | 43 |
|
41 | | - #all rings in the simulation |
42 | | - for ring in rings: |
| 44 | + #vector for soma sections and segment |
| 45 | + somasec = h.Vector() |
| 46 | + somaseg = h.Vector() |
43 | 47 |
|
44 | | - #every gid in the ring |
45 | | - for gid in ring.gids: |
| 48 | + #vector for dendrite sections and segment |
| 49 | + densec = h.Vector() |
| 50 | + denseg = h.Vector() |
46 | 51 |
|
47 | | - #vector for soma sections and segment |
48 | | - somasec = h.Vector() |
49 | | - somaseg = h.Vector() |
| 52 | + #if gid exist on rank |
| 53 | + if (pc.gid_exists(gid)): |
50 | 54 |
|
51 | | - #vector for dendrite sections and segment |
52 | | - densec = h.Vector() |
53 | | - denseg = h.Vector() |
| 55 | + #get cell instance |
| 56 | + cell = pc.gid2cell(gid) |
| 57 | + isec = 0 |
54 | 58 |
|
55 | | - #if gid exist on rank |
56 | | - if (pc.gid_exists(gid)): |
| 59 | + #soma section, only pne |
| 60 | + for sec in [cell.soma]: |
| 61 | + for seg in sec: |
| 62 | + #get section and segment index |
| 63 | + somasec.append(isec) |
| 64 | + somaseg.append(seg.node_index()) |
57 | 65 |
|
58 | | - #get cell instance |
59 | | - cell = pc.gid2cell(gid) |
60 | | - isec = 0 |
| 66 | + #vector for recording |
| 67 | + v = h.Vector() |
| 68 | + v.record(seg._ref_v) |
| 69 | + v.label("soma %d %d" % (isec, seg.node_index())) |
| 70 | + recordlist.append(v) |
| 71 | + isec += 1 |
61 | 72 |
|
62 | | - #soma section, only pne |
63 | | - for sec in [cell.soma]: |
64 | | - for seg in sec: |
65 | | - #get section and segment index |
66 | | - somasec.append(isec) |
67 | | - somaseg.append(seg.node_index()) |
| 73 | + #register soma section list |
| 74 | + pc.nrnbbcore_register_mapping(gid, "soma", somasec, somaseg) |
68 | 75 |
|
69 | | - #vector for recording |
70 | | - v = h.Vector() |
71 | | - v.record(seg._ref_v) |
72 | | - v.label("soma %d %d" % (isec, seg.node_index())) |
73 | | - recordlist.append(v) |
74 | | - isec += 1 |
| 76 | + return recordlist |
75 | 77 |
|
76 | | - #for sections in dendrite |
77 | | - for sec in cell.den: |
78 | | - for seg in sec: |
79 | | - densec.append(isec) |
80 | | - denseg.append(seg.node_index()) |
| 78 | +def write_report_config(output_file, report_name, target_name, report_type, report_variable, |
| 79 | + unit, report_format, target_type, dt, start_time, end_time, gids, |
| 80 | + buffer_size=8): |
| 81 | + import struct |
| 82 | + num_gids = len(gids) |
| 83 | + report_conf = Path(output_file) |
| 84 | + report_conf.parent.mkdir(parents=True, exist_ok=True) |
| 85 | + with report_conf.open("wb") as fp: |
| 86 | + # Write the formatted string to the file |
| 87 | + fp.write(b"1\n") |
| 88 | + fp.write(("%s %s %s %s %s %s %d %lf %lf %lf %d %d\n" % ( |
| 89 | + report_name, |
| 90 | + target_name, |
| 91 | + report_type, |
| 92 | + report_variable, |
| 93 | + unit, |
| 94 | + report_format, |
| 95 | + target_type, |
| 96 | + dt, |
| 97 | + start_time, |
| 98 | + end_time, |
| 99 | + num_gids, |
| 100 | + buffer_size |
| 101 | + )).encode()) |
| 102 | + # Write the array of integers to the file in binary format |
| 103 | + fp.write(struct.pack(f'{num_gids}i', *gids)) |
| 104 | + fp.write(b'\n') |
| 105 | + |
| 106 | +def write_spike_config(output_file: str, spike_filename: str, |
| 107 | + population_names: List[str], population_offsets: List[int]): |
| 108 | + report_conf = Path(output_file) |
| 109 | + num_population = len(population_names) |
| 110 | + with report_conf.open("a") as fp: |
| 111 | + fp.write(f"{num_population}\n") |
| 112 | + for pop_name, offset in zip(population_names, population_offsets): |
| 113 | + fp.write(f"{pop_name} {offset}\n") |
| 114 | + fp.write(f"{spike_filename}\n") |
| 115 | + |
| 116 | +def write_sim_config(output_file, coredata_dir, report_conf, tstop): |
| 117 | + sim_conf = Path(output_file) |
| 118 | + sim_conf.parent.mkdir(parents=True, exist_ok=True) |
| 119 | + Path(coredata_dir).mkdir(parents=True, exist_ok=True) |
| 120 | + with sim_conf.open("w") as fp: |
| 121 | + fp.write("outpath=./\n") |
| 122 | + fp.write(f"datpath=./{coredata_dir}\n") |
| 123 | + fp.write(f"tstop={tstop}\n") |
| 124 | + fp.write(f"report-conf='{report_conf}'\n") |
| 125 | + fp.write("mpi=true\n") |
| 126 | + |
| 127 | +def spike_record(): |
| 128 | + global tvec, idvec |
| 129 | + pc = h.ParallelContext() |
| 130 | + tvec = h.Vector(1000000) |
| 131 | + idvec = h.Vector(1000000) |
| 132 | + pc.spike_record(-1, tvec, idvec) |
81 | 133 |
|
82 | | - #for recordings |
83 | | - v = h.Vector() |
84 | | - v.record(seg._ref_v) |
85 | | - v.label("dend %d %d" % (isec, seg.node_index())) |
86 | | - recordlist.append(v) |
87 | | - isec += 1 |
| 134 | +def test_coreneuron_report(): |
| 135 | + # model setup |
| 136 | + pc = h.ParallelContext() |
88 | 137 |
|
89 | | - #register soma section list |
90 | | - pc.nrnbbcore_register_mapping(gid, "soma", somasec, somaseg) |
| 138 | + c = Cell(0) |
| 139 | + h.cvode.use_fast_imem(1) |
91 | 140 |
|
92 | | - #register dend section list |
93 | | - pc.nrnbbcore_register_mapping(gid, "dend", densec, denseg) |
| 141 | + # activate coreneuron |
| 142 | + coreneuron.enable = True |
| 143 | + coreneuron.file_mode = False |
| 144 | + coreneuron.gpu = False |
| 145 | + coreneuron.cell_permute = False |
94 | 146 |
|
95 | | - return recordlist |
| 147 | + # register reports |
| 148 | + if pc.id() == 0: |
| 149 | + setup_nrnbbcore_register_mapping(c.gid) |
| 150 | + report_conf_file = "report.conf" |
| 151 | + sim_conf_file = "sim.conf" |
| 152 | + write_report_config(report_conf_file, "soma_v.h5", "Mosaic", "compartment", "v", |
| 153 | + "mV", "SONATA", 2, 1, 0, h.tstop, [c.gid]) |
| 154 | + write_spike_config(report_conf_file, "spikes.h5", ["default"], [0]) |
| 155 | + write_sim_config(sim_conf_file, "corenrn_data", report_conf_file, h.tstop) |
| 156 | + coreneuron.sim_config=sim_conf_file |
96 | 157 |
|
| 158 | + spike_record() |
97 | 159 |
|
98 | | -def test_coreneuron_report(): |
99 | | - gid = 0 |
100 | | - pc = h.ParallelContext() |
| 160 | + # run |
| 161 | + pc.set_maxstep(10) |
| 162 | + h.stdinit() |
| 163 | + pc.psolve(h.tstop) |
| 164 | + |
| 165 | + |
| 166 | + |
| 167 | + # assert False |
101 | 168 |
|
102 | | - c = Cell(gid) |
103 | | - h.cvode.use_fast_imem(1) |
104 | 169 |
|
105 | | - inspect(pc.gid2cell(0)) |
106 | | - inspect(c) |
107 | 170 |
|
108 | 171 |
|
109 | 172 |
|
|
0 commit comments