Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Plotting figures after individual sim runs in a batch simulation

69 views
Skip to first unread message

Maliha A

unread,
Jan 23, 2024, 7:01:53 PM1/23/24
to NetPyNE Q&A forum
Is there no way of plotting figures after each run in a batch simulation? 
Or is the only option to read batch data from individual simulation files at the very end of the full batch run?

Let's say for example I would like to plot the average membrane potential of a population after each simulation. I've been playing around with the model in Tutorial 8. I felt optimistic and tried adding lines of code to the tut8_init.py file after 

sim.create(netParams, simConfig)
sim.simulate()
sim.analyze()

like so:

stimtimevec = sim.allSimData.t
VavgM=np.zeros(len(sim.allSimData.t))
for i in range(20,40):
    arrayname = f'cell_{i}'
    VavgM = np.add(VavgM,sim.allSimData.V_soma[arrayname])
    VavgM = (1/20)*VavgM

plt.figure(figsize=(10,6))
plt.plot(stimtimevec,VavgM)
plt.xlabel("Time (ms)")
plt.ylabel("Voltage (mV)")
plt.rcParams['font.size'] = 16

But that didn't work... any suggestions would be greatly appreciated.

Thank you
Maliha

J C

unread,
Feb 26, 2024, 8:43:03 PM2/26/24
to NetPyNE Q&A forum
I'm not certain how to do this during a batch run using the current NetPyNE batch tools.
if this code works on a single run, then it could possibly be modified to work in a batch simulation.
First, any display is usually disabled during batch runs, so doing something to save the figure would be more appropriate.
`plt.savefig()`
the second issue is that we would need to provide a unique name for the figure as opposed to something like "results.png" or else all of the simulations would clobber each other.
to provide a string to the `plt.savefig()` that would be unique for each run, you could generate a string based on some `cfg` variable that changes between simulations in the batch...
if you look at the `batchCell` example, the generated `_cfg.json` files might show some useful unique identifiers that can be referenced.
The `simConfig` dictionary is essentially what the `cfg` that is being imported in the `init.py` file is...
At the bottom, one string includes `"simLabel"` so you could theoretically do
`plt.savefig("{}.png".format(cfg.simLabel))`
to generate a name unique to each run (and therefore a unique png)

J C

unread,
Feb 26, 2024, 8:50:26 PM2/26/24
to NetPyNE Q&A forum
```
(base) ➜  batchNa git:(development) ✗ cat batchNa_0_1_cfg.json | grep "simLabel" -B 5 -A 5
        "seeds": {
            "conn": 4321,
            "loc": 4321,
            "stim": 1234
        },
        "simLabel": "batchNa_0_1", <---- this value
        "tau1NMDA": 15,
        "timestampFilename": false,
        "timing": true,
        "tstop": 1000.0,
        "use_fast_imem": false,
```

what I don't know is if there are issues with the matplotlib library that would cause the figures from the analysis scripts to conflict with the any custom plot function in your init.py

Maliha A

unread,
Mar 15, 2024, 2:09:27 PM3/15/24
to NetPyNE Q&A forum
Thank you for the suggestion! I gave it a try and unfortunately it still didn't work.. From my understanding it seems to not execute any lines written after sim.analyze(). I tested this using the following lines:

randomname = 3
randomname2 = 4
with open('tut8_output.txt', "a") as tut8_output_txt:
    l1 = '\n [%.3f,' % randomname
    l2 = '%.3f],' % randomname2
    tut8_output_txt.writelines([l1,l2])

I expected that each time the init.py is called for each run, these lines would also get executed but that's not the case. Hmm..


J C

unread,
Mar 19, 2024, 12:45:02 AM3/19/24
to NetPyNE Q&A forum
Can you clarify, nothing executes after `sim.analyze()` when running it as a batch job or as a single job?
Can you link me a repository of your code? 

Thanks,

James 
Message has been deleted

Maliha A

unread,
Apr 2, 2024, 2:12:06 PM4/2/24
to NetPyNE Q&A forum
Hi James, 
I realized I had errors in my code! So i am indeed able to execute lines after sim.analyze() as a batch job :) 

Thanks
Maliha
Reply all
Reply to author
Forward
0 new messages