how to save a segment list in a pkl file

When Python is the interpreter, what is a good
design for the interface to the basic NEURON
concepts.

Moderator: hines

Post Reply
tine
Posts: 10
Joined: Thu Jan 13, 2011 2:18 pm

how to save a segment list in a pkl file

Post by tine »

Dear all,

I am running a simulation, where among others the voltage of different dendrite segments is calculated and saved in a pkl file. This is all done in the main.py file. Afterwards the analysis.py script opens this pkl script, and analyses the data and makes figures.

This all worked fine, but I now want to know to which dendrite my data corresponds to. Within the main.py function I store the links to all segments recorded in a variable model.dend containing

Code: Select all

In [2]: model.dends
Out[2]: 
[<nrn.Section at 0x10be4b4b0>,
 <nrn.Section at 0x10be4b4e0>,
 <nrn.Section at 0x10be4b510>,
 <nrn.Section at 0x10be4b540>,
Within the main.py script I simply ask for model.dends[10].name() when I want to know the name of the 10th dendritic recording. However when I add this data to the data stored in the pkl file (such that the analysis script can access this later on) by:

Code: Select all

data.recordSec = False
data.SectionsRecorded = model.dends

modelData = sc.emptyObject()
lb.props(modelData)

tstamp = time.strftime("sim%Y%b%d_%H%M%S")
tstamp = 'temp'

dataList = [data, modelData]
fname = './'+tstamp+'.pkl'
f = open(fname, 'wb')
pickle.dump(dataList, f)
f.close()
The script gives an error at

Code: Select all

ModellingL2_3/L23_model/main_MRG.py in <module>()
    277 fname = './'+tstamp+'.pkl'
    278 f = open(fname, 'wb')
--> 279 pickle.dump(dataList, f)
    280 f.close()
    281 
Can someone explain to me why I can't save the segment list and/or give me suggestion how to save this information in another way. Thanks in advance,
aaronmil
Posts: 35
Joined: Fri Apr 25, 2014 10:54 am

Re: how to save a segment list in a pkl file

Post by aaronmil »

"Pickling" a data structure performs a deep copy of all the objects comprising the data structure. This means that if you have a Python class that is a wrapper around your hoc sections, then information about section names or lengths or distances that is contained in Python class variables will be preserved. However, the hoc section itself that is referred to by a class variable will not exist, so you won't be able to call the section's methods to extract parameters like nseg or diam or name. The hoc object will be of the right type from the perspective of the Python interpreter, but it has never been "created" from the perspective of the hoc interpreter. The best practice is to divorce simulation output to files from the live simulation environment by "extracting" information about hoc sections before "pickling". For example, pickle a data structure that contains the output of calling .diam or .nseg on existing hoc sections. Then later you can "unpickle" the data structure, and get information about the hoc sections without having to use any hoc objects or the hoc interpreter at all.

Note: exporting to an HDF5 database file has advantages over pickle for large arrays like voltage time series...
hines
Site Admin
Posts: 1691
Joined: Wed May 18, 2005 3:32 pm

Re: how to save a segment list in a pkl file

Post by hines »

h.Vector is now pickleable which is particularly useful for the ParallelContext Bulletin Board. But direct pickling of any other hoc objects or internal
NEURON objects in not on the agenda.
Post Reply