f8f06c32d068fe9e6a604d76e2e97b99180b0fb4
[hooke.git] / doc / tutorial.txt
1 ********
2 Tutorial
3 ********
4
5 `A short video showing Hooke in action`_! (courtesy of Fabrizio
6 Benedetti, EPFL, Lausanne)
7
8 .. _A short video showing Hooke in action:
9   https://documents.epfl.ch/users/f/fb/fbenedet/www/hooke_short_demostration.ogv
10
11 .. toctree::
12    :maxdepth: 2
13
14    gui
15
16 Introduction
17 ============
18
19 This tutorial will focus on the command-line interface as the most
20 powerful, and leave the :doc:`gui` interface to another document.
21
22 .. _command-line: `Command-line interface`_
23
24 Installation
25 ============
26
27 See :doc:`install` for details on downloading and installing Hooke.
28
29
30 Command-line interface
31 ======================
32
33 Running the hooke shell
34 -----------------------
35
36 Hooke has a set of commands that depend on the loaded
37 :class:`hooke.plugin.Plugin`\s.  To access these commands, you'll need
38 to run the Hooke shell.::
39
40     $ hooke
41
42 If you are running hooke from the source directory (see
43 :doc:`install`), the equivalent command is::
44
45     $ python bin/hk.py
46
47 You may need to give the full path for Python on Windows systems, and
48 also check that the current working directory (`.`) is in your
49 `PYTHONPATH`.  See :manpage:`python(1)` for details.
50
51 As Hooke launches, you should see something like the following in your
52 terminal::
53
54     Hooke version 0.9.0.devel (Kenzo)
55
56     Copyright (C) 2006-2010 A. Seeholzer, Alberto Gomez-Casado, Allen
57     Chen, Fabrizio Benedetti, Francesco Musiani, Marco Brucale, Massimo
58     Sandal, Pancaldi Paolo, Richard Naud, Rolf Schmidt, W. Trevor King
59
60     Hooke comes with ABSOLUTELY NO WARRANTY and is licensed under the GNU
61     Lesser General Public License.  For details, run `license`.
62     ----
63     hooke>
64
65 The final line, ``hooke>``, is the Hooke prompt.  It allows you to
66 enter commands to interact with the interpreter.
67
68 Help
69 ----
70
71 All commands have help text explaining their purpose and usage.  The
72 text is stored in the code itself, and therefore more likely to be up
73 to date than this tutorial.  You can get a list of commands and topics
74 with::
75
76     hooke> help
77
78 Or see specific help on ``TOPIC`` with::
79
80     hooke> help TOPIC
81
82 for example::
83
84     hooke> help load_playlist
85
86 will give help on the ``load_playlist`` command.
87
88 Leaving Hooke
89 -------------
90
91 When you're done with an interactive Hooke session, you can close the
92 session with ``exit`` or its aliases ``quit`` and ``EOF`` (``EOF`` is
93 the end of the stdin stream, which is Ctrl-d in many shells).
94
95 Creating a playlist
96 -------------------
97
98 To start analyzing your curves, you first have to build a playlist.
99 The playlist is just an index of the force curve files you want to
100 analyze.  Imagine it as a music playlist (that’s why it is called a
101 playlist), but with data files instead of audio files.
102
103 Suppose you have 100 PicoForce curve files in your curves directory,
104 starting from :file:`mycurve.000` and ending in :file:`mycurve.100`
105 and you want to analyze them all.
106
107 You then can ``cd`` (change directory) to the directory::
108
109     hooke> cd --path c:\curves
110
111 Type ``pwd`` (print working directory) to check the directory is correct.::
112
113     hooke> pwd
114     c:\curves
115
116 You can list the files in the directory using ``ls`` or ``dir``
117 (they’re synonyms).::
118
119     hooke> ls
120     mycurve.000
121     mycurve.001
122     ...
123
124 Now you are ready to generate the playlist.  First, create a blank playlist::
125
126     hooke> new_playlist --output_playlist mylist
127
128 Ensure that the new playlist is active::
129
130     hooke> jump_to_playlist -- -1
131     hooke> get_playlist
132     <FilePlaylist mylist>
133
134 The ``--`` in the ``jump_to_playlist`` command lets
135 ``jump_to_playlist`` know that ``-1`` is an argument and not an
136 option.  Using the bare ``--`` is a POSIX specification [#POSIX]_
137 supported by the `optparse module`_.  You don't need to jump if
138 the new playlist is your only loaded playlist.
139
140 .. _optparse module:
141   http://docs.python.org/library/optparse.html#callback-example-6-variable-arguments
142
143 .. [#POSIX] `Guideline 10 of POSIX:2008's section 12.2 <http://www.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html#tag_12_02>`_ states:
144
145     "The first ``--`` argument that is not an option-argument should be
146     accepted as a delimiter indicating the end of options. Any
147     following arguments should be treated as operands, even if they
148     begin with the ``-`` character."
149
150 Then glob your curves onto the new list::
151
152     hooke> glob_curves_to_playlist mycurve.*
153
154 You can also be more specific with wildcards.  For example::
155
156     hooke> glob_curve_to_playlist mycurve.05*
157
158 will take only curves from :file:`mycurve.050` to :file:`mycurve.059`.
159
160 Note that by using ``glob_curves_to_playlist`` you just generate the
161 playlist in the local session. To save your playlist to a file for
162 future reuse, type::
163
164     hooke> save_playlist --output mylist
165
166 In this example, the list will be saved in the file
167 :file:`mylist.hkp`.  Hooke will add the extension ``.hkp`` (Hooke
168 playlist) to the playlist if you forget to.  The ``.hkp`` file is an
169 XML file you can read and edit with any text editor (i.e. Wordpad), if
170 needed.  If you want to load it, simply issue ``load_playlist
171 mylist.hkp`` or ``load_playlist mylist``, Hooke will add ``.hkp`` if
172 necessary.
173
174 If, generating the playlist, you are including by chance a non-force
175 curve file that Hooke cannot open, Hooke will log a warning and
176 continue on.
177
178 Navigating the playlist
179 -----------------------
180
181 Now you can navigate through your playlist using the commands
182 ``next_curve`` and ``previous_curve``. You don’t need to type
183 ``next_curve`` every time to run along a list of curves.  You can
184 navigate through the command history by using the up and down arrows,
185 or auto-complete partial commands with TAB.  From the last curve of
186 your playlist, ``next_curve`` will wrap around to the first curve.
187 Analogously, issuing ``previous_curve`` at the first curve will jump
188 to the last.
189
190 You can also jump to a given curve::
191
192     hooke> jump_to_curve 14
193
194 will jump to the 14th curve in the zero-indexed playlist.
195
196 .. todo:: ``jump_to_curve <PATH>``, where the path can be either an
197   absolute path or a path relative to the directory holding the
198   playlist file.
199
200 Replace ``curve`` with ``playlist`` in the above commands to navigate
201 around through the list of loaded playlists.
202
203 Because the playlist name is usually saved in the playlist file
204 itself, there is a ``name_playlist`` command that allows you to rename
205 playlists on the fly.
206
207     hooke> name_playlist 'my old playlist'
208
209 Taking notes
210 ------------
211
212 You can take notes about the curves you are looking at.  Just type
213 ``set_note`` followed by the text you want to attach to that curve.
214 Hooke will save the text in your current playlist and in an external
215 log file.
216
217 .. todo:: No external file yet.  Is this important?
218
219 The output will look like this::
220
221     Notes taken at Sun Sep 17 20:42:07 2006
222     /home/cyclopia/work/tris/20060620a.041 | This is a note
223     /home/cyclopia/work/tris/20060620a.207 | This is another note
224     /home/cyclopia/work/tris/20060620a.286 | This is a third one
225
226 The log file name can be configured (:doc:`config`), but it defaults
227 to :file:`hooke.log`.
228
229 Usually curves you annotated are useful later.  You can create a
230 playlist for only annotated curves with
231
232     hooke> note_filter_playlist --output_playlist nice_list
233
234 will create sub-playlist `nice_list`.  Remember to save the new list
235 if you like it.
236
237 If you change your mind about a note, you can remove it by setting a
238 blank note string with ``set_note ''``.
239
240 Exporting curves
241 ----------------
242
243 You can export Hooke curves as images and as text columns.  To export
244 as images or text, use the ``export_block`` command.  Supported
245 formats are PNG (Portable Network Graphic, raster) and EPS
246 (Encapsulated Postscript, vector).  The export format is determined by
247 the filename extension, so ``export_block --output foo.png``,
248 ``export_block --output foo.eps``, and ``export_block --output
249 foo.txt`` will save PNG, EPS, and TAB-delimited text files
250 respectively.
251
252 .. todo:: Currently no PNG or EPS output, use the GUI and the plot
253   panel's toolbar for non-text exports.
254
255 .. todo:: Multiple cycles in exported data?  Solution: blank lines for
256   "breaks", add option to extract specific sections using Python's
257   slice notation.
258
259 If you don't want the entire block, try the ``cut`` command.
260
261 Analysis
262 --------
263
264 The commands we have covered so far allow basic bookkeeping.  The
265 point of Hooke, though, is to allow you to easily analyze force
266 spectroscopy data.  We cover those analysis commands in this section.
267
268 Measuring distances and forces
269 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
270
271 To measure the distance between points, use the ``delta`` command.
272 For example,::
273
274     hooke> delta 300 500
275
276 will measure the distance between the 300th point and the 500th point.
277 One difficulty with the command line interface is that is difficult
278 to know which points you're interested without seeing the plot.  The
279 two ways around this are:
280
281 1) Export the block (with ``export_block``), and graph the exported
282    file with a program of your choice (e.g. Gnuplot_).  Use the
283    resulting graph to determine the indices of the points you are
284    interested in.
285 2) Run Hooke's GUI instead of the command line when you need to make
286    manual measurements.  See :doc:`gui` for details.
287
288 .. _Gnuplot: http://gnuplot.sourceforge.net/
289
290 Worm like chain and freely jointed chain fitting
291 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
292
293 Polymer model fitting is a complicated beast.  To correctly fit your
294 chosen model (WLC, FJC, etc.), you need to execute a multi-step
295 analysis.  Hooke provides a flexible chain of curve analyisis commands
296 that create new data columns (e.g. `deflection (N)`) or store
297 information in a curve's `info` dictionary (e.g. `flat filter peaks`).
298 You can, if necessary, adjust the names of input and output columns
299 and `info` values to combine the available commands in new and useful
300 ways.::
301
302     hooke> zero_surface_contact_point --block retract
303     hooke> flat_filter_peaks --block retract --min_points 1
304     hooke> zero_surface_contact_point --block retract
305     ...        --ignore_after_last_peak_info_name 'flat filter peaks'
306     hooke> convert_distance_to_force --block retract
307     ...        --deflection_column 'surface deflection (m)'
308     hooke> remove_cantilever_from_extension --block retract
309     hooke> flat_peaks_to_polymer_peaks --block retract
310     hooke> polymer_fit_peaks --block retract
311
312 This stores the fit parameters in the block's
313 :attr:`~hooke.curve.Data.info` dictionary (see each command's `Help`_
314 for details).  To access the parameters, save the playlist,::
315
316     hooke> save_playlist --output mylist.hkp
317
318 load it with PyYAML_::
319
320     $ python
321     >>> import yaml
322     >>> import hooke.util.yaml
323     >>> mylist = yaml.load(open('mylist.hkp', 'r'))
324
325 navigate to your curve,::
326
327     >>> mylist.jump(1234)
328
329 and extract your parameter.::
330
331     >>> mylist.current().data[1].info['polymer peak 0']['contour length (m)']
332     2.124...e-08
333
334 You can also get a list of all available parameters::
335
336     >>> mylist.current().data[1].info['polymer peak 0'].keys()
337     ['model', 'contour length (m)', 'temperature (K)', 'fit',
338      'persistence length (m)']
339
340 or just pprint_ the whole thing::
341
342     >>> from pprint import pprint
343     >>> pprint(mylist.current().data[1].info['polymer peak 0'])
344     {'contour length (m)': 2.1248220207858103e-08,
345      'fit': {'active fitted parameters': 3.7024307200788127,
346              'active parameters': 3.7024307200788127,
347              'convergence flag': 1,
348              'covariance matrix': None,
349              'data scale factor': 5.2716380732198972e-10,
350              'fitted parameters': -2.5663294148411571,
351              'info': {'fjac': None,
352                       'fvec': None,
353                       'ipvt': None,
354                       'nfev': 16,
355                       'qtf': None},
356              'initial parameters': [-0.69314718055994529],
357              'message': 'Both actual and predicted relative reductions in the sum of squares\n  are at most 0.000000',
358              'rescaled': True,
359              'scale': None},
360      'model': u'WLC',
361      'persistence length (m)': 4.0000000000000001e-10,
362      'temperature (K)': 301.0}
363
364 .. _PyYAML: http://pyyaml.org/
365 .. _pprint: http://docs.python.org/library/pprint.html
366
367 .. todo:: UI access to block (and curve?) info dicts.  Someone should
368    make a tree-based object browser ;).
369
370 Command stacks
371 ~~~~~~~~~~~~~~
372
373 Since you are likely to apply similar analysis to several curves,
374 Hooke provides :mod:`command stacks <hooke.command_stack>` for
375 bundling groups of commands.::
376
377     hooke> start_command_capture
378     hooke> zero_surface_contact_point --block retract
379     hooke> flat_filter_peaks --block retract --min_points 1
380     ...
381     hooke> stop_command_capture
382
383 You can check the state of the command stack with
384 ``get_command_stack`` and the state of capture with
385 ``get_command_capture_state``.  If you make mistakes, you can pop
386 commands from the stack with ``pop_command_from_stack``.  If you stop
387 capturing a command stack (e.g. to test a complicated command before
388 continuing), you can continue adding to the same stack with
389 ``restart_command_capture.``
390
391 To execute a command stack, run::
392
393     hooke> execute_command_stack
394
395 To execute a command stack on every curve in a playlist, run::
396
397     hooke> apply_command_stack_to_playlist
398
399 If you decide that there are commands in a curve's stack that you
400 don't want, you can clear the stack with::
401
402     hooke> clear_curve_command_stack
403
404 You can also save command stacks to disk (and reload them later,
405 potentially in a different Hooke session).::
406
407     hooke> save_command_stack --output my_stack
408     hooke> load_command_stack --input my_stack
409
410 Multiple curve analysis
411 ~~~~~~~~~~~~~~~~~~~~~~~
412
413 You can analyze multiple curves by combining `Worm like chain and
414 freely jointed chain fitting`_ and `Command stacks`_.::
415
416     hooke> start_command_capture
417     hooke> zero_surface_contact_point --block retract
418     hooke> flat_filter_peaks --block retract --min_points 1
419     hooke> zero_surface_contact_point --block retract
420     ...        --ignore_after_last_peak_info_name 'flat filter peaks'
421     hooke> convert_distance_to_force --block retract
422     ...        --deflection_column 'surface deflection (m)'
423     hooke> remove_cantilever_from_extension --block retract
424     hooke> flat_peaks_to_polymer_peaks --block retract
425     hooke> polymer_fit_peaks --block retract
426     hooke> export_block --block retract --output myblock.dat
427     hooke> stop_command_capture
428     hooke> apply_command_stack_to_playlist
429
430 Configuring Hooke
431 -----------------
432
433 You can set environment variables to influence the behaviour of Hooke.
434 The command to use is ``set_config``.  Use ``get_config`` to read a
435 particular option and ``print_config`` to display the entire
436 configuration file.  To save changes, either run ``save_config`` or
437 start Hooke with the ``--save-config`` option.  See :doc:`config` for
438 details.