Mercurial > hg > numerics
annotate numerics/bar.py @ 150:8a1fe454c98a
STUB
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Mon, 13 Apr 2015 10:17:47 -0700 |
parents | dcedbe63d2c6 |
children | 134bdb520040 |
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 |
148
279f5ae33564
this now generates something
Jeff Hammel <k0scist@gmail.com>
parents:
147
diff
changeset
|
15 import json |
140 | 16 import os |
103
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
17 import sys |
141
310290f95787
cleanup; now it really doesnt do anything
Jeff Hammel <k0scist@gmail.com>
parents:
140
diff
changeset
|
18 import tempita |
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') | |
147 | 28 bar_template = os.path.join(templates, 'bar.d3.html') |
146 | 29 with open(os.path.join(here, 'js', 'd3.v3.min.js')) as f: |
141
310290f95787
cleanup; now it really doesnt do anything
Jeff Hammel <k0scist@gmail.com>
parents:
140
diff
changeset
|
30 d3 = f.read() |
140 | 31 |
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
|
32 |
117
0adf95bdda00
what a waste of time and i still dont have a plot
Jeff Hammel <k0scist@gmail.com>
parents:
116
diff
changeset
|
33 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
|
34 """ |
fe820a3afa48
wip but we do need that new dependency
Jeff Hammel <k0scist@gmail.com>
parents:
115
diff
changeset
|
35 create a bar chart |
fe820a3afa48
wip but we do need that new dependency
Jeff Hammel <k0scist@gmail.com>
parents:
115
diff
changeset
|
36 |
fe820a3afa48
wip but we do need that new dependency
Jeff Hammel <k0scist@gmail.com>
parents:
115
diff
changeset
|
37 See: |
fe820a3afa48
wip but we do need that new dependency
Jeff Hammel <k0scist@gmail.com>
parents:
115
diff
changeset
|
38 - 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
|
39 """ |
fe820a3afa48
wip but we do need that new dependency
Jeff Hammel <k0scist@gmail.com>
parents:
115
diff
changeset
|
40 # 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
|
41 |
142
6747ce2bb090
more stubbing; almost there
Jeff Hammel <k0scist@gmail.com>
parents:
141
diff
changeset
|
42 template = tempita.Template.from_filename(bar_template) |
6747ce2bb090
more stubbing; almost there
Jeff Hammel <k0scist@gmail.com>
parents:
141
diff
changeset
|
43 bar_chart = template.substitute(title=title or '', |
6747ce2bb090
more stubbing; almost there
Jeff Hammel <k0scist@gmail.com>
parents:
141
diff
changeset
|
44 d3=d3, |
148
279f5ae33564
this now generates something
Jeff Hammel <k0scist@gmail.com>
parents:
147
diff
changeset
|
45 data=json.dumps(data)) |
279f5ae33564
this now generates something
Jeff Hammel <k0scist@gmail.com>
parents:
147
diff
changeset
|
46 return bar_chart |
116
fe820a3afa48
wip but we do need that new dependency
Jeff Hammel <k0scist@gmail.com>
parents:
115
diff
changeset
|
47 |
112 | 48 class BarChartParser(ManipulationParser): |
104 | 49 """command line options parser for bar charts""" |
103
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
50 # TODO: upstream to PlotParser |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
51 |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
52 def __init__(self, **kwargs): |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
53 kwargs.setdefault('description', __doc__) |
112 | 54 ManipulationParser.__init__(self, **kwargs) |
104 | 55 self.add_argument('-t', '--title', dest='title', |
56 help="plot title") | |
103
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
57 |
117
0adf95bdda00
what a waste of time and i still dont have a plot
Jeff Hammel <k0scist@gmail.com>
parents:
116
diff
changeset
|
58 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
|
59 """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
|
60 # 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
|
61 # 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
|
62 # 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
|
63 # 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
|
64 # '20150315203424.html' |
0adf95bdda00
what a waste of time and i still dont have a plot
Jeff Hammel <k0scist@gmail.com>
parents:
116
diff
changeset
|
65 # 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
|
66 # 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
|
67 # 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
|
68 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
|
69 |
103
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
70 |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
71 def main(args=sys.argv[1:]): |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
72 """CLI""" |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
73 |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
74 # parse command line |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
75 parser = BarChartParser() |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
76 options = parser.parse_args(args) |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
77 |
112 | 78 # process data |
79 data = parser.typed_data() | |
80 | |
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
|
81 # ensure a mapping is given |
140 | 82 if len(data) != 1: |
83 raise NotImplementedError("TODO") | |
141
310290f95787
cleanup; now it really doesnt do anything
Jeff Hammel <k0scist@gmail.com>
parents:
140
diff
changeset
|
84 else: |
310290f95787
cleanup; now it really doesnt do anything
Jeff Hammel <k0scist@gmail.com>
parents:
140
diff
changeset
|
85 data = data[0] |
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
|
86 |
113
f9900883db2e
hook it up and break it down
Jeff Hammel <k0scist@gmail.com>
parents:
112
diff
changeset
|
87 # generate bar chart |
148
279f5ae33564
this now generates something
Jeff Hammel <k0scist@gmail.com>
parents:
147
diff
changeset
|
88 options.output.write(bar_chart(data, parser.plot_filename(), title=options.title)) |
103
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
89 |
149 | 90 if options.output != sys.stdout: |
91 # print URL of file | |
92 print ('file://{}'.format(os.path.abspath(options.output.name))) | |
93 | |
103
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
94 |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
95 if __name__ == '__main__': |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
96 main() |