analyze.py: Set `changed = True` for tweaked vibration variance
[calibcant.git] / bin / calibcant-plot.py
1 #!/usr/bin/env python
2 # calibcant - tools for thermally calibrating AFM cantilevers
3 #
4 # Copyright (C) 2011-2013 W. Trevor King <wking@tremily.us>
5 #
6 # This file is part of calibcant.
7 #
8 # calibcant is free software: you can redistribute it and/or modify it under
9 # the terms of the GNU General Public License as published by the Free Software
10 # Foundation, either version 3 of the License, or (at your option) any later
11 # version.
12 #
13 # calibcant is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along with
18 # calibcant.  If not, see <http://www.gnu.org/licenses/>.
19
20 """Load a calibration file and plot everything interesting.
21 """
22
23 from optparse import OptionParser
24
25 import h5py as _h5py
26 from matplotlib.pyplot import close, get_fignums, figure, show
27
28 from calibcant.calibrate import load_all
29 from calibcant.analyze import analyze_all
30
31
32 def main(args):
33     usage = '%prog [options] filename'
34     p = OptionParser(usage)
35
36     p.add_option('-g', '--group', dest='group', default='/',
37                  help='HDF5 group containing calibration data (%default).')
38     p.add_option('-b', '--no-bumps', dest='bumps', default=True,
39                  action='store_false',
40                  help="Don't display bump details.")
41     p.add_option('-v', '--no-vibrations', dest='vibrations', default=True,
42                  action='store_false',
43                  help="Don't display vibration details.")
44     p.add_option('-s', '--save-figures', dest='save', action='store_true',
45                  help="Save plots (instead of displaying them.")
46
47     options,args = p.parse_args(args)
48     filename = args[0]
49
50     with _h5py.File(filename) as f:
51         if options.group in f:
52             g = f[options.group]
53             if 'approach' in g:
54                 position = g['approach/position'][...]
55                 deflection = g['approach/deflection'][...]
56                 fig = figure()
57                 axes = fig.add_subplot(1, 1, 1)
58                 axes.plot(position, deflection)
59                 axes.set_title('Stepper approach')
60                 axes.set_xlabel('position (steps)')
61                 axes.set_ylabel('deflection (bits)')
62     calibrator,data,raw_data = load_all(filename=filename, group=options.group)
63     if not options.bumps:
64         raw_data['bump'] = []
65     if not options.vibrations:
66         raw_data['vibration'] = []
67     analyze_all(config=calibrator.config, data=data, raw_data=raw_data,
68                 plot=True, dry_run=True)
69     if options.save:
70         for i in get_fignums():
71             fig = figure(i)
72             fig.savefig('%i.png' % i, dpi=300)
73             close(i)
74     else:
75         show()
76
77
78 if __name__ == '__main__':
79     import sys
80
81     sys.exit(main(sys.argv[1:]))