Mercurial > hg > numerics
annotate numerics/bar.py @ 140:9acea302899a
preparing for d3
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Mon, 30 Mar 2015 10:45:47 -0700 |
parents | a4e6b6ad6907 |
children | 310290f95787 |
rev | line source |
---|---|
103
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
1 #!/usr/bin/env python |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
3 |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
4 """ |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
5 bar charts using bokeh |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
6 |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
7 See: |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
8 - http://bokeh.pydata.org/tutorial/solutions/gallery/olympics.html |
137 | 9 - http://bokeh.pydata.org/en/latest/tutorial/topical.html |
140 | 10 - https://gist.github.com/mbostock/7322386 |
103
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
11 """ |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
12 |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
13 # imports |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
14 import argparse |
140 | 15 import os |
103
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
16 import sys |
117
0adf95bdda00
what a waste of time and i still dont have a plot
Jeff Hammel <k0scist@gmail.com>
parents:
116
diff
changeset
|
17 import tempfile |
115
7fac47bb648e
mysteries and more mysteries: bokeh should really be a plugin for this, not the way to do it, but we are porting so lets ignore that and boldly walk forward
Jeff Hammel <k0scist@gmail.com>
parents:
113
diff
changeset
|
18 from .data import transpose |
112 | 19 from .manipulate import ManipulationParser |
115
7fac47bb648e
mysteries and more mysteries: bokeh should really be a plugin for this, not the way to do it, but we are porting so lets ignore that and boldly walk forward
Jeff Hammel <k0scist@gmail.com>
parents:
113
diff
changeset
|
20 from collections import OrderedDict |
103
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
21 |
107
19a5c2fb52bb
add transpose functionality
Jeff Hammel <k0scist@gmail.com>
parents:
104
diff
changeset
|
22 __all__ = ['bar_chart', 'BarChartParser', 'main'] |
103
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
23 |
140 | 24 # template info |
25 # TODO: own module | |
26 here = os.path.dirname(os.path.abspath(__file__)) | |
27 templates = os.path.join(here, 'templates') | |
28 template = os.path.join(here, 'bar.d3.html') | |
29 d3 = os.path.join(here, 'javascript', 'd3.v3.min.js') | |
30 | |
115
7fac47bb648e
mysteries and more mysteries: bokeh should really be a plugin for this, not the way to do it, but we are porting so lets ignore that and boldly walk forward
Jeff Hammel <k0scist@gmail.com>
parents:
113
diff
changeset
|
31 |
117
0adf95bdda00
what a waste of time and i still dont have a plot
Jeff Hammel <k0scist@gmail.com>
parents:
116
diff
changeset
|
32 def bar_chart(data, output, title=None): |
116
fe820a3afa48
wip but we do need that new dependency
Jeff Hammel <k0scist@gmail.com>
parents:
115
diff
changeset
|
33 """ |
fe820a3afa48
wip but we do need that new dependency
Jeff Hammel <k0scist@gmail.com>
parents:
115
diff
changeset
|
34 create a bar chart |
fe820a3afa48
wip but we do need that new dependency
Jeff Hammel <k0scist@gmail.com>
parents:
115
diff
changeset
|
35 |
fe820a3afa48
wip but we do need that new dependency
Jeff Hammel <k0scist@gmail.com>
parents:
115
diff
changeset
|
36 See: |
fe820a3afa48
wip but we do need that new dependency
Jeff Hammel <k0scist@gmail.com>
parents:
115
diff
changeset
|
37 - http://bokeh.pydata.org/en/latest/tutorial/solutions/gallery/olympics.html |
fe820a3afa48
wip but we do need that new dependency
Jeff Hammel <k0scist@gmail.com>
parents:
115
diff
changeset
|
38 """ |
fe820a3afa48
wip but we do need that new dependency
Jeff Hammel <k0scist@gmail.com>
parents:
115
diff
changeset
|
39 # TODO: abstract this to a plot class |
fe820a3afa48
wip but we do need that new dependency
Jeff Hammel <k0scist@gmail.com>
parents:
115
diff
changeset
|
40 |
fe820a3afa48
wip but we do need that new dependency
Jeff Hammel <k0scist@gmail.com>
parents:
115
diff
changeset
|
41 |
112 | 42 class BarChartParser(ManipulationParser): |
104 | 43 """command line options parser for bar charts""" |
103
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
44 # TODO: upstream to PlotParser |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
45 |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
46 def __init__(self, **kwargs): |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
47 kwargs.setdefault('description', __doc__) |
112 | 48 ManipulationParser.__init__(self, **kwargs) |
104 | 49 self.add_argument('-t', '--title', dest='title', |
50 help="plot title") | |
103
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
51 |
117
0adf95bdda00
what a waste of time and i still dont have a plot
Jeff Hammel <k0scist@gmail.com>
parents:
116
diff
changeset
|
52 def plot_filename(self): |
0adf95bdda00
what a waste of time and i still dont have a plot
Jeff Hammel <k0scist@gmail.com>
parents:
116
diff
changeset
|
53 """determine the plot filename""" |
0adf95bdda00
what a waste of time and i still dont have a plot
Jeff Hammel <k0scist@gmail.com>
parents:
116
diff
changeset
|
54 # this is a STUB |
0adf95bdda00
what a waste of time and i still dont have a plot
Jeff Hammel <k0scist@gmail.com>
parents:
116
diff
changeset
|
55 # in reality, this should come from -o, --output |
0adf95bdda00
what a waste of time and i still dont have a plot
Jeff Hammel <k0scist@gmail.com>
parents:
116
diff
changeset
|
56 # if applicable, or, should be determined from |
0adf95bdda00
what a waste of time and i still dont have a plot
Jeff Hammel <k0scist@gmail.com>
parents:
116
diff
changeset
|
57 # the plot --title, or should be eg |
0adf95bdda00
what a waste of time and i still dont have a plot
Jeff Hammel <k0scist@gmail.com>
parents:
116
diff
changeset
|
58 # '20150315203424.html' |
0adf95bdda00
what a waste of time and i still dont have a plot
Jeff Hammel <k0scist@gmail.com>
parents:
116
diff
changeset
|
59 # we are doing this right nowe to work around the fact |
0adf95bdda00
what a waste of time and i still dont have a plot
Jeff Hammel <k0scist@gmail.com>
parents:
116
diff
changeset
|
60 # that bokeh, in particular, will just cry if you |
0adf95bdda00
what a waste of time and i still dont have a plot
Jeff Hammel <k0scist@gmail.com>
parents:
116
diff
changeset
|
61 # don't set this |
0adf95bdda00
what a waste of time and i still dont have a plot
Jeff Hammel <k0scist@gmail.com>
parents:
116
diff
changeset
|
62 return 'foo.html' |
0adf95bdda00
what a waste of time and i still dont have a plot
Jeff Hammel <k0scist@gmail.com>
parents:
116
diff
changeset
|
63 |
103
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
64 |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
65 def main(args=sys.argv[1:]): |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
66 """CLI""" |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
67 |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
68 # parse command line |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
69 parser = BarChartParser() |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
70 options = parser.parse_args(args) |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
71 |
112 | 72 # process data |
73 data = parser.typed_data() | |
74 | |
115
7fac47bb648e
mysteries and more mysteries: bokeh should really be a plugin for this, not the way to do it, but we are porting so lets ignore that and boldly walk forward
Jeff Hammel <k0scist@gmail.com>
parents:
113
diff
changeset
|
75 # ensure a mapping is given |
140 | 76 if len(data) != 1: |
77 raise NotImplementedError("TODO") | |
115
7fac47bb648e
mysteries and more mysteries: bokeh should really be a plugin for this, not the way to do it, but we are porting so lets ignore that and boldly walk forward
Jeff Hammel <k0scist@gmail.com>
parents:
113
diff
changeset
|
78 |
113
f9900883db2e
hook it up and break it down
Jeff Hammel <k0scist@gmail.com>
parents:
112
diff
changeset
|
79 # generate bar chart |
140 | 80 # bar_chart(data, parser.plot_filename(), title=options.title) |
103
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
81 |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
82 |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
83 if __name__ == '__main__': |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
84 main() |