programatically controlling the MRF

Using the Multiple Run Fitter, praxis, etc..
Post Reply

programatically controlling the MRF

Post by Brad »

I am interested in using the Multiple Run Fitter for a task nearly identical to the one shown in the 2nd tutorial ( ... tline.html). However, I'd like to do the fitting task several hundred (or thousand) times, and no matter how clever I set up my session files, there was always a certain amount of mouse clicking (such as retrieve-vector-from-file and selecting variable-to-fit) that required user intervention that would challenge my sanity.

I therefore wanted to figure out how to programatically control the MRF so I could dump it into a FOR loop and come back tomorrow to get the results. Since what I want to do is so similar to the tutorial, I made it an exercise to do replicate the tutorial from a script. I am posting this in case anyone else would find it useful. There is only one (inconsequential) thing I wasn't able to do (change the display name of the generator; has no effect on the actual behavior). Further, there are some steps that I include in order to explicitly replicate the tutorial, but are not strictly necessary for batch mode, such as refreshing a window to make sure the contents are updated. I noticed small differences in the error depending on whether the fixed time step or adaptive time step integrator is used (using the later generates results much faster, of course); set to replicate exactly the error values through Step 6A. From that point on, the results don't match up quite exactly because it's unclear to me from the prose exactly how many times the tutorial intends one to do an optimization run and if we're starting over again with the parameters at their initial values, but I feel the general form of the script is okay.

To get this to work, I had to modify some of the classes in $NEURONHOME/lib/hoc/mulfit/ to make some members public (they're noted in the code comments). I also did a lot of snooping on the session files and liberal use of allobjectvars(). I have a map of how all the various mulfit classes fit together and if someone asks for it, I'll digitize it and post it as well (but I won't otherwise as I am nearly done with it).

Code: Select all

// try to do run fitter tutorial programatically

// Step 1. Create an "unoptimized" model

	v_init = -70
	tstop = 200
	dt = 0.025

//  Step 2. Set up a current clamp experiment on this model

	objref Istim
	soma Istim = new IClamp(0.5)
	Istim.dur = 0.5
	Istim.del = 1
	Istim.amp = 1
	objref VB, g[2], tvec[2], ivec, vvec
	tvec[0] = new Vector()
	tvec[1] = new Vector()
	ivec = new Vector()
	vvec = new Vector()		// using the adaptive time step integrator will result in slightly 
				// different error values from the tutorial, but the computation is 
				// much faster; to get the indentical results as the tutoiral, 
				// set
	soma cvode.record(&v(0.5),vvec,tvec[0])
	VB = new VBox()
	g[0] = new Graph()
	g[1]= new Graph()

// "What about input resistance?"

	load_file("rn.hoc")	// from tutorial
	print "DC input resistance of cell is ", rn(), "MOhms"

// Step 3A. Create a Multiple Run Fitter

	/* makemulrunfitter()
	Calling this proc also clears the object name "tobj" so that is only 
	referenced by the GUI, so it is not possible to access it programmatically.
	Therefore, create it manually.

	objref MRF
	MRF = new MulRunFitter()

// Step 3B. Add generator; requires "addgen" to be declared a public function of ParmFitnessGUI (eparmlst.hoc)

// Step 3C. change title; required only for replicating tutorial = "iclamp"
	MRF.p.mulfit.title = "iclamp"

// Step 3D. Diplay generator; requires that these be made public also
	MRF.p.gmode = 1
	MRF.p.gensel(0,1)	// the selected generator is the 0th index (for this exercise only when we have one generator!)
				// in the GUI, the browser.accept_action() dumps the list index into hoc_ac_

// Step 3D3.  Select variable to fit"soma.v(0.5)", new RegionFitness()) // update GUI box to reflect fit variable; 
							// req fitdeck() be declared a public member of FitnessGenerator in "eonerun.hoc"
							// necessary only for demo purposes

	// to refresh Run Fitness Generator window, close and re-open it; not strictly necessary for mere programmatic control
	MRF.p.dgen()		// close window; dgen() declared a public member of ParmFitnessGui in "eparmlst.hoc"
	MRF.p.gensel(0,1)	// re-open it

// Step4A. read data ("iclamp.dat") from file
	objref f1, orig_data
	orig_data = new Matrix()
	f1 = new File()
	// orig_data.printf()	// uncomment to test data import success

	"iclamp.dat" originally had a label:str for its first line, thus preventing us from using the simpler 
	file-IO idiom above. We could use the built-in clipboard_retrieve() function (include 
	"stdlib.hoc" if not already) which takes care of this for us at the cost of one bit of 
	user-intervention.  To use the above approach, I manually deleted the first line of "iclamp.dat"
	and added nrow/tncol as the first line (i.e. "8001	2")

// put data on clipboard
	for i = 0,1 {hoc_obj_[i] = new Vector()}
	hoc_obj_[0].copy(orig_data.getcol(1))	// y data
	hoc_obj_[1].copy(orig_data.getcol(0))	// x data

// pull data into RegionFitness from clipboard

// Step 4B.  Test the run fitness generator

// Step 5A.  Create proxy parameters and custom init() procedure

// test proxy parameters
	//forall print secname(), " ", g_pas
	Rm *= 2
	//forall print secname(), " ", g_pas

	// undo doubling to get in sync with tutorial
	Rm /= 2

// use the proxy parameters
	objref xobj, proxies
	strdef xstr
	proxies = new List()
	proxies.append(new String("Ri"))
	proxies.append(new String("Cm"))
	proxies.append(new String("Rm"))

	for i = 0,proxies.count()-1 {	// the gist of ParmFitnessGUI.addarg(), without displaying the SymChooser to interactively query the user
		xstr = proxies.object(i).s
		xobj = new RunFitParm(xstr)	// this might cause problems later, the object is declared at the top-level, not within the ParmFitnessGui object
		sprint(xstr, "%s.val = %s", xobj, xstr)
		execute1(xstr)			// not sure why this doesn't have to be executed in the context of the ParmFitnessGui object		

// Step 5B.  Display parameter panel, only for replicating tutorial

// increase Rm to 10000 = 10000

// check error value

// return Rm to 1000 to stay in synch with tutorial = 1000

// check error value

// Step 5C.  Constrain parameters to use positive definite limits and log scaling
	MRF.p.uselog(1)	// add uselog() to public members of ParmFitnessGui
	MRF.p.limits(1e-9,1e9)	// add uselog() to public members of ParmFitnessGui

// toggle to use generator
	MRF.p.gmode = 2

// Step 6A.  run again
// Step 6B.  Choose and use optimizer
	sprint(xstr, "mulfit.opt = new %s(pf)", mulfit_optimizers_.object(0).s)
	execute(xstr, MRF.p)

// change "# of quad forms before return"
	MRF.opt.nstep = 1

// run optimizer 3X as in tutorial

// check rn(); the results are slightly different due to using the variable time step [change to exactly replicate tutorial results, but it will take longer to compute]
	print "somatic input resistance [ie. output of rn()]: ", rn(), " MOhms"

// "A minimal principled strategy"
	// reset parameters to their initial values
		Ri = 80    // ohm cm
		Cm = 1     // uf/cm2
		Rm = 1000  // ohm cm2

	// optimize Rm only; turn off Ri and Cm... = 0 = 0

	// ...and run a few times
		for i = 0,8 {MRF.prun()}

	// Now turn off Rm... = 0

	// ...turn Ri and Cm back on... = 1 = 1

	// ...and run a few more times
		for i = 0,2 {MRF.prun()}

	// Finally, optimize all at once...
		for i = 0,2 { = 1}

	// ...and run a few more times
		for i = 0,4 {MRF.prun()}

	print "somatic input resistance [ie. output of rn()]: ", rn(), " MOhms"

// adjust regions
	// reset parameters to their initial values
		Ri = 80    // ohm cm
		Cm = 1     // uf/cm2
		Rm = 1000  // ohm cm2

	// zoom-in,25,-70,-65.5)

	// set region to optimize from 1 to 10 msec (only one region with weight of 1)

		proc get_region_info() {
			print "bounds: ",
			print "weights: ",
		//get_region_info()[0] = 1[1] = 10

	// try a few runs
		for i = 0,9 {MRF.prun()}

Posts: 5
Joined: Thu Apr 17, 2008 3:42 pm
Location: University of Alberta

Post by Karl »

Thank you so much for posting this! I'd be very interested in seeing your class list posted.

There was just one member that needed to be made public that wasn't mentioned in the comments: clipboard_data() needs to be added to "e_norm.hoc". There's probably a better spot to put it so that you can access the clipboard for other forms of run fitting.
Post Reply