Mercurial > hg > numerics
annotate numerics/bar.py @ 142:6747ce2bb090
more stubbing; almost there
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Mon, 30 Mar 2015 12:43:36 -0700 |
parents | 310290f95787 |
children | 3ca7289b09d9 |
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 |
141
310290f95787
cleanup; now it really doesnt do anything
Jeff Hammel <k0scist@gmail.com>
parents:
140
diff
changeset
|
17 import tempita |
112 | 18 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
|
19 from collections import OrderedDict |
103
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
20 |
107
19a5c2fb52bb
add transpose functionality
Jeff Hammel <k0scist@gmail.com>
parents:
104
diff
changeset
|
21 __all__ = ['bar_chart', 'BarChartParser', 'main'] |
103
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
22 |
140 | 23 # template info |
24 # TODO: own module | |
25 here = os.path.dirname(os.path.abspath(__file__)) | |
26 templates = os.path.join(here, 'templates') | |
141
310290f95787
cleanup; now it really doesnt do anything
Jeff Hammel <k0scist@gmail.com>
parents:
140
diff
changeset
|
27 bar_template = os.path.join(here, 'bar.d3.html') |
310290f95787
cleanup; now it really doesnt do anything
Jeff Hammel <k0scist@gmail.com>
parents:
140
diff
changeset
|
28 with open(os.path.join(here, 'javascript', 'd3.v3.min.js')) as f: |
310290f95787
cleanup; now it really doesnt do anything
Jeff Hammel <k0scist@gmail.com>
parents:
140
diff
changeset
|
29 d3 = f.read() |
140 | 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 |
142
6747ce2bb090
more stubbing; almost there
Jeff Hammel <k0scist@gmail.com>
parents:
141
diff
changeset
|
41 template = tempita.Template.from_filename(bar_template) |
6747ce2bb090
more stubbing; almost there
Jeff Hammel <k0scist@gmail.com>
parents:
141
diff
changeset
|
42 bar_chart = template.substitute(title=title or '', |
6747ce2bb090
more stubbing; almost there
Jeff Hammel <k0scist@gmail.com>
parents:
141
diff
changeset
|
43 d3=d3, |
6747ce2bb090
more stubbing; almost there
Jeff Hammel <k0scist@gmail.com>
parents:
141
diff
changeset
|
44 data=data) |
116
fe820a3afa48
wip but we do need that new dependency
Jeff Hammel <k0scist@gmail.com>
parents:
115
diff
changeset
|
45 |
112 | 46 class BarChartParser(ManipulationParser): |
104 | 47 """command line options parser for bar charts""" |
103
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
48 # TODO: upstream to PlotParser |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
49 |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
50 def __init__(self, **kwargs): |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
51 kwargs.setdefault('description', __doc__) |
112 | 52 ManipulationParser.__init__(self, **kwargs) |
104 | 53 self.add_argument('-t', '--title', dest='title', |
54 help="plot title") | |
103
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
55 |
117
0adf95bdda00
what a waste of time and i still dont have a plot
Jeff Hammel <k0scist@gmail.com>
parents:
116
diff
changeset
|
56 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
|
57 """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
|
58 # 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
|
59 # 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
|
60 # 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
|
61 # 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
|
62 # '20150315203424.html' |
0adf95bdda00
what a waste of time and i still dont have a plot
Jeff Hammel <k0scist@gmail.com>
parents:
116
diff
changeset
|
63 # 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
|
64 # 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
|
65 # 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
|
66 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
|
67 |
103
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
68 |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
69 def main(args=sys.argv[1:]): |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
70 """CLI""" |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
71 |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
72 # parse command line |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
73 parser = BarChartParser() |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
74 options = parser.parse_args(args) |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
75 |
112 | 76 # process data |
77 data = parser.typed_data() | |
78 | |
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
|
79 # ensure a mapping is given |
140 | 80 if len(data) != 1: |
81 raise NotImplementedError("TODO") | |
141
310290f95787
cleanup; now it really doesnt do anything
Jeff Hammel <k0scist@gmail.com>
parents:
140
diff
changeset
|
82 else: |
310290f95787
cleanup; now it really doesnt do anything
Jeff Hammel <k0scist@gmail.com>
parents:
140
diff
changeset
|
83 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
|
84 |
113
f9900883db2e
hook it up and break it down
Jeff Hammel <k0scist@gmail.com>
parents:
112
diff
changeset
|
85 # generate bar chart |
141
310290f95787
cleanup; now it really doesnt do anything
Jeff Hammel <k0scist@gmail.com>
parents:
140
diff
changeset
|
86 bar_chart(data, parser.plot_filename(), title=options.title) |
103
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
87 |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
88 |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
89 if __name__ == '__main__': |
067aa27050a3
limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
90 main() |