File: hoc_exercises.rst

package info (click to toggle)
neuron 8.2.6-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 34,760 kB
  • sloc: cpp: 149,571; python: 58,465; ansic: 50,329; sh: 3,510; xml: 213; pascal: 51; makefile: 35; sed: 5
file content (673 lines) | stat: -rw-r--r-- 17,122 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
.. _hoc_exercises:


HOC Exercises
=============

Executable lines below are shown with the hoc prompt ``oc>``.

Typing these, although trivial, can be a valuable way to get familiar with the language.

.. code-block:: c++

    oc> // A comment

    oc> /* ditto */


Anything not explicitly declared is assumed to be a number
-------------
.. code::
    c++

    oc> x=5300 // no previous declaration as to what 'x' is

- numbers are all doubles (high precision numbers)

    there is no integer type in Hoc

- Scientific notation use e or E

    .. code::
        c++

        oc> print 5.3e3,5.3E3 // e preferred (see next)

- there are some useful built-in values
    
    .. code::
        c++

        oc> print PI, E, FARADAY, R

Do you have anything to declare?: objects and strings
----------

- Must declare an object reference (=object variable) before making an object

- Objref: manipulate references to objects, not the objects themselves

    often names are chosen that make it easy to remember what an object reference is to be used for (eg g for a Graph or vec for a Vector) but it's important to remember that these are just for convenience and that any object reference can be used to point to any kind of object

- Objects include vectors, graphs, lists, ...

    .. code::
        c++

        oc> objref XO,YO // capital 'oh' not zero

        oc> print XO,YO // these are object references

        oc> XO = new List() // 'new' creates a new instance of the List class

        oc> print XO,YO // XO now points to something, YO does not

        oc> objref XO // redeclaring an objref breaks the link; if this is the only reference to that object the object is destroyed

        oc> XO = new List() // a new new List

        oc> print XO // notice the List[#] -- this is a different List, the old one is gone

- After creating object reference, can use it to point a new or old object

    .. code::
        c++

        oc> objref vec,foo // two object refs

        oc> vec = new Vector() // use 'new' to create something

        oc> foo = vec // foo is now just another reference to the same thing

        oc> print vec, foo // same thing

        oc> vec=XO

        oc> print vec, foo // vec no longer points to a vector

        oc> objectvar vec // objref and objectvar are the same; redeclaring an objref breaks the link between it and the object it had pointed to

        oc> print vec, foo // vec had no special status, foo still points equally well

- Can create an array of objrefs

    .. code::
        c++

        oc> objref objarr[10]

        oc> objarr[0]=XO

        oc> print objarr, objarr[0] // two ways of saying same thing

        oc> objarr[1]=foo

        oc> objarr[2]=objarr[0] // piling up more references to the same thing

        oc> print objarr[0],objarr[1],objarr[2]

- Exercises: Lists are useful for maintaining pointers to objects so that they are maintained when explicit object references are removed

    1. 
        Make vec point to a new vector. Print out and record its identity (*print vec*). Now print using the object name (ie *print Vector[#] with the right #*). This confirms that the object exists. Destroy the object by reinitializing the vec reference. Now try to print using the object name. What does it say.

    2. 
        As in Exercise 1: make vec point to a new vector and use print to find the vector name. Make XO a reference to a new list. Append the vector to the list: {XO.append(vec). Now dereference vec as in Exercise 1. Print out the object by name and confirm that it still exists. Even though the original objref is gone, it is still point to by the list.

    3.
        Identify the vector on the list: (*print XO.object(0)*). Remove the vector from the list (*print XO.remove(0)*). Confirm that this vector no longer exists.

- Strings 

- Must declare a string before assigning it

    .. code::
        c++

        oc> mystr = "hello" // ERROR: needed to be declared

        oc> strdef mystr // declaration

        oc> mystr = "hello" // can't declare and set together

        oc> print mystr

        oc> printf("-%s-", mystr) // tab-string-newline; printf=print formatted; see documentation

- There are no string arrays; get around this using arrays of String objects

- Can also declare number arrays, but vectors are often more useful

    .. code::
        c++

        oc> x=5

        oc> double x[10]

        oc> print x // overwrote prior value

        oc> x[0]=7

        oc> print x, x[0] // these are the same

Operators and numerical functions
==========

.. code::
    c++ 
=======

.. seealso::

    :ref:`hoc_language_guide` and the :ref:`hoc_prog_ref`

Data types: numbers, strings, and objects
-----------------------------------------

Anything not explicitly declared is assumed to be a number
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. code-block:: c++

    oc> x=5300 // no previous declaration as to what 'x' is

.. note::

    Numbers are all doubles (high-precision floating point numbers). In particular, there is no integer type in HOC.

For scientific notation use ``e`` or ``E``.

.. code-block:: c++

    oc> print 5.3e3,5.3E3 // e preferred (see next)

There are some useful built-in values:

.. code-block:: c++

    oc> print PI, E, FARADAY, R

Do you have anything to declare?: objects and strings
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Must declare an object reference (=object variable) before making an object

Objref: manipulate references to objects, not the objects themselves

- often names are chosen that make it easy to remember what an object reference is to be used for (eg g for a :hoc:class:`Graph` or vec for a :hoc:class:`Vector`) but it's important to remember that these are just for convenience and that any object reference can be used to point to any kind of object

Objects include vectors, graphs, lists, ...
###########################################

.. code-block:: c++

    oc> objref XO,YO // capital 'oh' not zero

    oc> print XO,YO // these are object references

    oc> XO = new List() // 'new' creates a new instance of the List class

    oc> print XO,YO // XO now points to something, YO does not

    oc> objref XO // redeclaring an objref breaks the link; if this is the only reference to that object the object is destroyed

    oc> XO = new List() // a new new List

    oc> print XO // notice the List[#] -- this is a different List, the old one is gone

After creating object reference, can use it to point a new or old object
########################################################################

.. code-block:: c++

    oc> objref vec,foo // two object refs

    oc> vec = new Vector() // use 'new' to create something

    oc> foo = vec // foo is now just another reference to the same thing

    oc> print vec, foo // same thing

    oc> vec=XO

    oc> print vec, foo // vec no longer points to a vector

    oc> objectvar vec // objref and objectvar are the same; redeclaring an objref breaks the link between it and the object it had pointed to

    oc> print vec, foo // vec had no special status, foo still points equally well

Can create an array of objrefs
##############################

.. code-block:: c++

    oc> objref objarr[10]

    oc> objarr[0]=XO

    oc> print objarr, objarr[0] // two ways of saying same thing

    oc> objarr[1]=foo

    oc> objarr[2]=objarr[0] // piling up more references to the same thing

    oc> print objarr[0],objarr[1],objarr[2]

Exercises
#########

Lists are useful for maintaining pointers to objects so that they are maintained when explicit object references are removed.

1. Make vec point to a new vector. Print out and record its identity (``print vec``). Now print using the object name (ie print Vector[#] with the right #). This confirms that the object exists. Destroy the object by reinitializing the vec reference. Now try to print using the object name. What does it say.

2. As in Exercise 1: make vec point to a new :hoc:class:`Vector` and use print to find the vector name. Make XO a reference to a new list. Append the vector to the list: {XO.append(vec). Now dereference vec as in Exercise 1. Print out the object by name and confirm that it still exists. Even though the original objref is gone, it is still pointed to by the list.

3. Identify the vector on the list: (``print XO.object(0)``). Remove the vector from the list (``print XO.remove(0)``). Confirm that this vector no longer exists.

Strings
#######

Must declare a string before assigning it

.. code-block:: c++

    oc> mystr = "hello" // ERROR: needed to be declared

    oc> strdef mystr // declaration

    oc> mystr = "hello" // can't declare and set together

    oc> print mystr

    oc> printf("-%s-", mystr) // tab-string-newline; printf=print formatted; see documentation

There are no string arrays; get around this using arrays of String objects
 
Can also declare number arrays, but vectors are often more useful

.. code-block:: c++

    oc> x=5

    oc> double x[10]

    oc> print x // overwrote prior value

    oc> x[0]=7

    oc> print x, x[0] // these are the same

Operators and numerical functions
---------------------------------

.. code-block:: c++

    oc> x=8 // assignment

    oc> print x+7, x*7, x/7, x%7, x-7, x^7 // doesn't change x

    oc> x==8 // comparison

    oc> x==8 && 5==3 // logical AND, 0 is False; 1 is True

    oc> x==8 \\ 5==3 // logical OR

    oc> !(x==8) // logical NOT, need parens here

    oc> print 18%5, 18/5, 5^3, 3*7, sin(3.1), cos(3.1), log(10), log10(10), exp(1)

    oc> print x, x+=5, x*=2, x-=1, x/=5, x // each changes value of x; no x++

Blocks of code
--------------

.. code-block:: c++

    oc> { x=7

    print x

    x = 12

    print x

    }

Conditionals
=========

.. code::
    c++

    oc> x=8

    oc> if (x==8) print "T" else print "F" // brackets optional for single statements

    oc> if (x==8) {print "T"} else {print "F"} // usually better for clarity

    oc> {x=1 while (x<=7) {print x x+=1}} // nested blocks, statements separate by space

    oc> {x=1 while (x<=7) {print x, x+=1}} // notice difference: comma makes 2 args of print

    oc> for x=1, 7 print x // simplest for loop

    oc> for (x=1;x<=7;x+=2) print x // (init;until;change)

Procedures and functions
===========

.. code::
    c++

    oc> proc hello () { print "hello" }

    oc> hello()

    oc> func hello () { print "hello" return 1.7 } // functions return a number

    oc> hello()

Numerical arguments to procedures and functions
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. code-block:: c++

    oc> proc add () { print $1 + $2 } // first and second argument, then $3, $4...

    oc> add(5, 3)

    oc> func add () { return $1 + $2 }

    oc> print 7*add(5, 3) // can use the returned value

    oc> print add(add(2, 4), add(5, 3)) // nest as much as you want

String (``$s1``, ``$s2``, ...) and object arguments (``$o1``, ``$o2``, ...)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. code-block:: c++

    oc> proc prstuff () { print $1, "::", $s2, "::", $o3 }

    oc> prstuff(5.3, "hello", vec)

Exercises
~~~~~~~~~

1. Use printf in a procedure to print out a formatted table of powers of 2

2. Write a function that returns the average of 4 numbers

3. Write a procedure that creates a section called soma and sets diam and L to 2 args

Built-in object types: graphs, vectors, lists, files
----------------------------------------------------

Graph
~~~~~

.. code-block:: c++

    oc> objref g[10]

    oc> g = new Graph()

    oc> g.size(5, 10, 2, 30) // set x and y axes

    oc> g.beginline("line", 2, 3) // start a red (2), thick (3) line

    oc> {g.line(6, 3) g.line(9, 25)} // draw a line (x, y) to (x, y)

    oc> g.flush() // show the line

.. seealso:: 

    :hoc:class:`Graph`

Exercises
#########

1. write ``proc`` that draws a colored line ($1) from (0, 0) to given coordinate ($2, $3) assume g is a :hoc:class:`Graph` object

2. write a ``proc`` that puts up two new graphs

3. bring up a graph using GUI, on graph use right-button right pull-down to "Object Name"; set 'g' objectvar to point to this graph and use ``g.size()`` to resize it

Vector
~~~~~~

.. code-block:: c++

    oc> objref vec[10]

    oc> for ii=0, 9 vec[ii]=new Vector()

    oc> vec.append(3, 12, 8, 7) // put 4 values in the vector

    oc> vec.append(4) // put on one more

    oc> vec.printf // look at them

    oc> vec.size // how many are there?

    oc> print vec.sum/vec.size, vec.mean // check average two ways

    oc> {vec.add(7) vec.mul(3) vec.div(4) vec.sub(2) vec.printf}

    oc> vec.resize(vec.size-1) // get rid of last value

    oc> for ii=0, vec.size-1 print vec.x[ii] // print values

    oc> vec[1].copy(vec[0]) // copy vec into vec[1]

    oc> vec[1].add(3)

    oc> vec.mul(vec[1]) // element by element; must be same size

.. seealso::

    :hoc:class:`Vector`

Exercises
#########

1. 
    Write a ``proc`` to make ``$o1`` vec elements the product of $o2*$o3 elements

    (Use :hoc:meth:`Vector.resize` to get ``$o1`` to right size; generate error if sizes wrong e.g. ``if ($o2.size!=$o3.size) { print "ERROR: wrong sizes" return }``)

2.
    Graph vector values: ``vec.line(g, 1)`` or ``vec.mark(g, 1)``

    Play with colors and mark shapes (see documentation for details).

3. Graph one vec against another: ``vec.line(g, vec[1])``; ``vec.mark(g, vec[1])``

4.
    Write a ``proc`` to multiply the elements of a vector by sequential values from ``1`` to ``size-1``

    Hint: use :hoc:meth:`vec.resize <Vector.resize>`, :hoc:meth:`vec.indgen <Vector.indgen>`, :hoc:meth:`vec.mul <Vector.mul>`

File
~~~~

.. code-block:: c++

    oc> objref file

    oc> mystr = "AA.dat" // use as file name

    oc> file = new File()

    oc> file.wopen(mystr) // 'w' means write, arg is file name

    oc> vec.vwrite(file) // binary format

    oc> file.close()

    oc> vec[1].fill(0) // set all elements to 0

    oc> file.ropen(mystr) // 'r' means read

    oc> vec[1].vread(file)

    oc> if (vec.eq(vec[1])) print "SAME" // should be the same


.. seealso::

    :hoc:class:`File`

Exercises
#########

1. ``proc`` to write a vector (``$o1``) to file with name ``$s2``

2. ``proc`` to read a vector (``$o1``) from file with name ``$s2``

3. proc to append a number to end of a file: ``tmpfile.aopen()``, ``tmpfile.printf``

List
~~~~

.. code-block:: c++

    oc> objref list

    oc> list = new List()

    oc> list.append(vec) // put an object on the list

    oc> list.append(g) // can put different kind of object on

    oc> list.append(list) // pointless

    oc> print list.count() // how many things on the list

    oc> print list.object(2) // count from zero as with arrays

    oc> list.remove(2) // remove this object

    oc> for ii=0, list.count-1 print list.object(ii) // remember list.count, vec.size


.. seealso::

    :hoc:class:`List`

Exercises
#########

1. write ``proc`` that takes a list ``$o1`` with a graph (.object(0)) followed by a vector (.object(1)) and shows the vector on the graph

2. modify this ``proc`` to read the vector out of file given in ``$s2``


Simulation
----------

.. code-block:: c++

    oc> create soma

    oc> access soma

    oc> insert hh

    oc> ismembrane("hh") // make sure it's set

    oc> print v, v(0.5), soma.v, soma.v(0.5) // only have 1 seg in section

    oc> tstop=50

    oc> run()

    oc> print t, v

    oc> print gnabar_hh

    oc> gnabar_hh *= 10

    oc> run()

    oc> print t, v // what happened?

    oc> gnabar_hh /= 10 // put it back

Recording the simulation
~~~~~~~~~~~~~~~~~~~~~~~~

.. code-block:: 
    c++

    oc> cvode_active(0) // this turns off variable time step

    oc> dt = 0.025

    oc> vec.record(&soma.v(0.5)) // '&' gives a pointer to the voltage

    oc> objref stim

    oc> soma stim = new IClamp(0.5) // current clamp at location 0.5 in soma

    oc> stim.amp = 20 // need high amp since cell is big

    oc> stim.dur = 1e10 // forever

    oc> run()

    oc> print vec.size()*dt, tstop // make sure stored the right amount of data

.. seealso::

    :hoc:meth:`Vector.record`

Graphing and analyzing data
~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. code-block:: c++

    oc> g=new Graph()

    oc> vec.line(g, dt, 2, 2)

    oc> g.size(0, tstop, -80, 50)

    oc> print vec.min, vec.max, vec.min_ind*dt, vec.max_ind*dt

    oc> vec[1].deriv(vec, dt)

    oc> print vec[1].max, vec[1].max_ind*dt // steepest AP

Exercises
#########

1. change params (``stim.amp``, ``gnabar_hh``, ``gkbar_hh``), regraph and reanalyze

2. bring up the GUI and demonstrate that the GUI and command line control same parameters

3. write ``proc`` to count spikes and determine spike frequency (use ``vec.where``)

Roll your own GUI
~~~~~~~~~~~~~~~~~

.. code-block:: c++

    oc> proc sety () { y=x print x }

    oc> xpanel("test panel")

    oc> xvalue("Set x", "x")

    oc> xvalue("Set y", "y")

    oc> xbutton("Set y to x", "sety()")

    oc> xpanel()

Exercise
########

1. put up panel to run sim and display (in an :hoc:func:`xvalue`) the average frequency