how to use SaveState()?

Moderator: wwlytton

ubartsch
Posts: 34
Joined: Thu May 19, 2005 11:02 am
Location: CTCN, University of Plymouth
Contact:

how to use SaveState()?

Post by ubartsch »

Hi,

the network I am simulating has a very long transient before it settles into the dynamic regime I am interested in.
My idea is now to use SaveState() to save the network state after the transient and then run a sweep of simulations starting of at that point.

My questions are:
How can you set the time point the network state is saved?
Can you use continuetil() to run simulations from the restore point on?

Well, any example of how to use SaveState would be very helpful!
Many thanks in advance!

Ulli

update:
tried to save state, load similar hoc file and read state file, error was:

/usr/local/nrn/x86_64/bin/nrniv: Bad SaveState binary file Not version 5.6 in transrun2.hoc near line 58
IBnetstate.fread(fo)
^
SaveState[0].fread(File[1])



code used to save state:

Code: Select all

objref IBnetstate[Ncol]
k=0
strdef fn, fn0
sprint(fn0,"%s%s",outstr,"Netstate_%d.dat")
for i=0,Ncol-1 {
	sprint(fn,fn0,i)
	print fn
	fp.wopen(fn)
	if ( t>=500  && k<1){
		IBnet[i].soma IBnetstate[i] = new SaveState()
		IBnetstate[i].save()
	        IBnetstate[i].fwrite(fp)
	        k=k+1
	        fp.close()
        }
}
ted
Site Admin
Posts: 5814
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: how to use SaveState()?

Post by ted »

Excellent questions, and they pertain to all kinds of models, not just network models.

This

Code: Select all

objref svstate
svstate = new SaveState()
svstate.save()
creates a SaveState object that contains the present values of "all" states, regardless
of the size and complexity of (and number of cells in) your model. The only exception
is that the event queue is not saved (see Caveats below).

To use the state data that are contained in a SaveState object, it is necessary to make
a small change to the standard run system's proc init() (I assume that you are using the
standard run system, not your own simulation control code). To do this, insert the
following code into your program, anywhere after the point at which you put the stuff
that creates svstate--

Code: Select all

proc init() {
  finitialize(v_init)
  svstate.restore()
  t = 0 // t is one of the "states"
  if (cvode.active()) {
    cvode.re_init()
  } else {
    fcurrent()
  }
  frecord_init()
}
Now every simulation will start from the state that you saved earlier.

State information can be saved for use in later sessions with

Code: Select all

objref f
f = new File("states.dat")
svstate.fwrite(f)
Here's how to read such a file into SaveState object:

Code: Select all

objref svstate, f
svstate = new SaveState()
f = new File("states.dat")
svstate.fread(f)
To use these state data, just include the new proc init() shown above.

Caveats:
1. The biggest one is that the "state" of the event queue is not saved. This means that
undelivered events are lost. This could have important consequences for models that
use the event delivery system, i.e. models that use NetCons or which use
cvode.event() or an FInitializeHandler to launch events that have not yet been
delivered by the time SaveState.save() is called. Bill Lytton does a lot of network
modeling and may have worked out a strategy for dealing with this problem; I'll
ask him to comment on this.
2. States are saved in a binary format using data structures that are likely to be
incompatible across different versions of NEURON. Consequently, if you update
NEURON, you will have to generate a new states.dat file.
How can you set the time point the network state is saved?
If this means what I think it does, the simplest answer is: use tstop.

Code: Select all

Can you use continuetil() to run simulations from the restore point on?
You could, but first you'd have to execute init(). Why not just execute run(), which
takes care of everything?
ubartsch
Posts: 34
Joined: Thu May 19, 2005 11:02 am
Location: CTCN, University of Plymouth
Contact:

Post by ubartsch »

Thanks again!
tried to save state, load similar hoc file and read state file, error was:

/usr/local/nrn/x86_64/bin/nrniv: Bad SaveState binary file Not version 5.6 in transrun2.hoc near line 58
IBnetstate.fread(fo)
^
SaveState[0].fread(File[1])
2. States are saved in a binary format using data structures that are likely to be
incompatible across different versions of NEURON. Consequently, if you update
NEURON, you will have to generate a new states.dat file.
Sorry, but the binary file was saved by the same version of nrn that opens it!
The current version I am using is:
NEURON -- Version 5.7 2005-4-21 11:7:7 Main (159)

Thanks for a hint!

Ulli
wwlytton
Posts: 66
Joined: Wed May 18, 2005 10:37 pm
Contact:

Post by wwlytton »

SaveState is supposed to save the NetCon queue as well:
(from NEURON ref manual)
"The outstanding event delivery times are absolute. When restored, all outstanding events will be cleared and the retored event times and NetCon info will take their place. Note that it is not in general possible to change the value of t in a network simulation since most NET_RECEIVE blocks keep t0 (the last event time) as part of their state."

I have used this successfully in the past.

Bill
ted
Site Admin
Posts: 5814
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Post by ted »

Sorry, but the binary file was saved by the same version of nrn that opens it!
Ah, (1) the ambiguities of language and intent, and (2) the imperfections of
human knowledge (or at least my knowledge).

(1) My message was almost purely generic, especially the statement that SaveState data are
likely to be incompatible across different NEURON versions. I wasn't commenting on the
code in your original msg--the complexity of which (compared to that which is required to
save and retrieve states) suggests an indended purpose which eludes me--or on the
error message you received.

(2) As Bill Lytton has already noted, outstanding events ARE saved, with their absolute
delivery times. So my first Caveat is incorrect. Furthermore, with network models, it is
a bad idea to change t in init(), because
"most NET_RECEIVE blocks keep t0 (the last event time) as part of their state"
So, for use with network models, the t = 0 statement in that custom proc init() should
be commented out.

And I must remember to always read the latest documentation. As Mark Twain said,
"It ain't what you don't know that gets you into trouble. It's what you know for sure
that just ain't so."
ubartsch
Posts: 34
Joined: Thu May 19, 2005 11:02 am
Location: CTCN, University of Plymouth
Contact:

Post by ubartsch »

I do admit the code above is not very elegant...
Anyhow, an altered version of that code seems to save the state but the problem remains that this state saved by neuron is not readable to that same version of neuron?!

It seems like neuron wants to have a 5.6 binary file but it is 5.7. So maybe it has multiple personalities, a 5.7 for saving and a 5.6 for reading these binaries...?

An amateurish speculation.
ted
Site Admin
Posts: 5814
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Post by ted »

ubartsch wrote:I do admit the code above is not very elegant
It's not a question of elegance that puzzles me, it's why you need to save states in the
context of a particular section of a particular cell. I thought that SaveState.save() saved
all states throughout an entire model, regardless of how many cells existed. Your
code suggests that it doesn't. The documentation for 5.7 says nothing that would lead me
to believe otherwise (although it notes that SaveState doesn't work with local variable
timesteps).

So why does your code execute SaveState.save() the way it does?
You already tested a single invocation of SaveState.save() for your entire model and
found that it didn't work?
It seems like neuron wants to have a 5.6 binary file but it is 5.7.
Maybe you discovered a bug. I haven't run into any problems with SaveState myself,
but it has been a while since I last used 5.7. Does SaveState.save(), invoked simply,
work properly with a simple model that contains only one cell? two cells?
ubartsch
Posts: 34
Joined: Thu May 19, 2005 11:02 am
Location: CTCN, University of Plymouth
Contact:

Post by ubartsch »

Sorry, I guess I have to clarify this:

If I use savestate in the sugessted way (and using the modified init proc from above, t=0 commented out, in the hoc that opens the state):

Code: Select all

...
IBnetstate = new SaveState()
IBnetstate.save()
...
I do get this error during saving the state:

/usr/local/nrn/x86_64/bin/nrniv: Section access unspecified in transrun1.hoc near line 97
IBnetstate = new SaveState()
^
SaveState()

There are two ways to fix that:
(1)

Code: Select all

access IBnet[0].soma  // somewhere after building the network, before running


This results in another error during reading that binary file:

Assertion failed: file ../nrncvode/netcvode.cpp, line 3443
/usr/local/nrn/x86_64/bin/nrniv: i < prl_->count() in transrun2.hoc near line 60
IBnetstate.fread(fo)
^
SaveState[0].fread(File[1])

As learned from an earlier post this is not recommended in order not to abuse the keyword access. (Please, correct me if I am wrong.)

Another error occurs when restoring the savestate of a single cell modell
(save state after running, write to file, open in a modified copy hoc to read state):
Assertion failed: file ../nrncvode/netcvode.cpp, line 3594
/usr/local/nrn/x86_64/bin/nrniv: cursize_ < vrd->t_->capacity() in pfc_pc.hoc near line 246
run()
^
SaveState[0].restore()
init()
stdinit()
run()



(2)

Code: Select all

...
IBnet[0].soma IBnetstate = new SaveState()
...
That results in the error I was posting before:
/usr/local/nrn/x86_64/bin/nrniv: Bad SaveState binary file Not version 5.6 in transrun2.hoc near line 60
IBnetstate.fread(fo)
^
SaveState[0].fread(File[1])


Many thanks (again!!) for some help.
Cheers
Ulli
ted
Site Admin
Posts: 5814
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Post by ted »

This is the first time I ever heard of SaveState needing a currently accessed section.
Probably the best way to deal with this is to pick one section that is "conceptually
privileged" (e.g, if you were to plot v(0.5) vs. t for a single section, which section
would you prefer to see?) and then make that the default section with an
access secname
statement. In addition to satisfying SaveState's requirement for a currently accessed
section, this will make all of the GUI's tools work (many of them require the existence
of a default section), which is good because the GUI can be very useful for debugging
programs and attaching "instrumentation" to models.
(1)

Code: Select all

access IBnet[0].soma  // somewhere after building the network, before running


This results in another error during reading that binary file:

Assertion failed: file ../nrncvode/netcvode.cpp, line 3443
/usr/local/nrn/x86_64/bin/nrniv: i < prl_->count() in transrun2.hoc near line 60
IBnetstate.fread(fo)
^
SaveState[0].fread(File[1])

As learned from an earlier post this is not recommended in order not to abuse the keyword access. (Please, correct me if I am wrong.)
It isn't "access abuse" if
access secname
is asserted once, and only once, in a session. And even if the code does abuse access,
that seems unlikely to have anything to do with the the "Assertion failed" error msg.

One last item: SaveState saves and restores the states of all cells in the net. It
is not necessary to iterate through them to save or restore each, one at a time.

Is there a bug in your hoc code, or in SaveState, or maybe in both? Quite possible.
The key to diagnosing and fixing the problem is to isolate its source, which means to
pare down the model to the smallest bit of code that will reproduce the symptom.
So can you make a toy model with just one or two of your cells, that produces an
error msg when you try to save and/or restore states?
ubartsch
Posts: 34
Joined: Thu May 19, 2005 11:02 am
Location: CTCN, University of Plymouth
Contact:

Post by ubartsch »

Another error occurs when restoring the savestate of a single cell modell
(save state after running, write to file, open in a modified copy hoc to read state):

Assertion failed: file ../nrncvode/netcvode.cpp, line 3594
/usr/local/nrn/x86_64/bin/nrniv: cursize_ < vrd->t_->capacity() in pfc_pc.hoc near line 246
run()
^
SaveState[0].restore()
init()
stdinit()
run()

?
ted
Site Admin
Posts: 5814
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Post by ted »

It's looking more and more like you have uncovered a bug in SaveState. The next
step is to send us your minimal model so we can run further tests--zip up the necessary
files and send them as an attachment to
ted (dot) carnevale (at) yale (dot) edu
ted
Site Admin
Posts: 5814
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Post by ted »

The problem was caused by trying to restore states and continue a simulation past the
end of a vector that was being used in a cvode.record() statement. The specific code that
led to the problem is

Code: Select all

objref T, V
n=int(tstop/deltat)
T = new Vector (n)
V = new Vector(n)
for j=0, n-1{T.x[j]=j*deltat}
cvode.record(&v(.5),V,T,1)
Michael Hines has incorporated a fix into the main development branch of NEURON,
which should eliminate this problem from future releases of NEURON. A possible
workaround for NEURON 5.7 is to make n larger
n=int(tstop/deltat) + 1
so that the new simulation starts _before_ the end of the time vector.
fred

Post by fred »

I'm trying to use savestate to begin simulations at steady state. I understand that sections cannot be deleted or moved between a save and a restore of states (I can't even add a Vclamp between save and restore). I've been able to run simulations from saved states only so long as I don't change any section properties.

I'm having trouble restoring in a new session, and I think it's because the only way I know to "restore" the sections of my neuron is to recreate them in hoc. Is there a way to save my sections as well as my states, so that I can restore both of them successfully? Can I do all of this from hoc?

Thanks,
Fred

Here's the error message:
6119 Segmentation fault
ted
Site Admin
Posts: 5814
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Post by ted »

Separate model specification from state restoration. The code that recreates
the model must be executed before the code that restores states. One way
to ensure that this happens is to use an init.hoc file that contains a sequence
of load_file() statements that first recreate the model, and then as its final
action loads the file that contains the proc that restores states.
fred

Post by fred »

I'm still having trouble with restoring states using savestate() in a new session. I'm including all of my simplified code (most of it is actually ted's).

First, I run the model, and use savestate() to write to a file:

Code: Select all

//use savestate to store steadystate to a file

load_file("nrngui.hoc")
load_file("make_cell.hoc")

cell {        
        cassi = .000115001 //mM
        cao = 1.8
        nao = 140
        ko = 5.4
        nai = 14.2371
        ki = 143.72
        cai = 0.115001e-3
}
    
// Set temperature and v_init
    {celsius=37}
    vrest = -82.4202
    v_init = vrest
    dt = .0001

// SAVESTATE STUFF ------------------------------------------------

    tstop = 10
    run()
    objref svstate
    svstate = new SaveState()
    svstate.save()

    objref f
    f = new File("states.dat")
    svstate.fwrite(f)

// /savestate stuff -----------------------------------------------
Then I exit NEURON and come back later with this code:

Code: Select all

load_file("make_cell.hoc")

// Set temperature and v_init
    {celsius=37}
    vrest = -82.4202
    v_init = vrest
    dt = .0001


// restore from savestate()

// restore sections
//    INCLUDE "make_cell.hoc"

// restore states
    objref svstate, f
    svstate = new SaveState()
    f = new File("states.dat")
    svstate.fread(f)
Am I missing something obvious here? To be thorough- what do you mean by "recreate the model"? To me, this means (assuming this is exactly the same code used when the states were saved):

Code: Select all

// init cell

    create cell
    access cell
    cell { 
        insert Channel
            
        nseg = 1
        L = 724.7 //um
        diam = 6.738 //um

    }
And state restoration looks like this:

Code: Select all

// restore states
    objref svstate, f
    svstate = new SaveState()
    f = new File("states.dat")
    svstate.fread(f)
So, as long as the former code is above the latter code in one hoc file, the states should be retrieved. Right?
Post Reply