annotate numerics/bar.py @ 138:488cb433576c

add d3 from http://d3js.org/d3.v3.min.js
author Jeff Hammel <k0scist@gmail.com>
date Sat, 21 Mar 2015 14:54:38 -0700
parents a4e6b6ad6907
children 9acea302899a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
a4e6b6ad6907 cleanup + notes
Jeff Hammel <k0scist@gmail.com>
parents: 118
diff changeset
9 - http://bokeh.pydata.org/en/latest/tutorial/topical.html
a4e6b6ad6907 cleanup + notes
Jeff Hammel <k0scist@gmail.com>
parents: 118
diff changeset
10 -
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
067aa27050a3 limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15 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
16 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
17 from .data import transpose
112
7578313b9fbf hook up basic plumbing
Jeff Hammel <k0scist@gmail.com>
parents: 107
diff changeset
18 from .manipulate import ManipulationParser
117
0adf95bdda00 what a waste of time and i still dont have a plot
Jeff Hammel <k0scist@gmail.com>
parents: 116
diff changeset
19 from bokeh.plotting import figure, output_file, show, VBox
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
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
24
117
0adf95bdda00 what a waste of time and i still dont have a plot
Jeff Hammel <k0scist@gmail.com>
parents: 116
diff changeset
25 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
26 """
fe820a3afa48 wip but we do need that new dependency
Jeff Hammel <k0scist@gmail.com>
parents: 115
diff changeset
27 create a bar chart
fe820a3afa48 wip but we do need that new dependency
Jeff Hammel <k0scist@gmail.com>
parents: 115
diff changeset
28
fe820a3afa48 wip but we do need that new dependency
Jeff Hammel <k0scist@gmail.com>
parents: 115
diff changeset
29 See:
fe820a3afa48 wip but we do need that new dependency
Jeff Hammel <k0scist@gmail.com>
parents: 115
diff changeset
30 - 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
31 """
fe820a3afa48 wip but we do need that new dependency
Jeff Hammel <k0scist@gmail.com>
parents: 115
diff changeset
32 # 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
33
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 figure
117
0adf95bdda00 what a waste of time and i still dont have a plot
Jeff Hammel <k0scist@gmail.com>
parents: 116
diff changeset
36 p1 = figure(title=title,
0adf95bdda00 what a waste of time and i still dont have a plot
Jeff Hammel <k0scist@gmail.com>
parents: 116
diff changeset
37 tools="pan,wheel_zoom,box_zoom,reset,resize",
0adf95bdda00 what a waste of time and i still dont have a plot
Jeff Hammel <k0scist@gmail.com>
parents: 116
diff changeset
38 x_range=data[0]
0adf95bdda00 what a waste of time and i still dont have a plot
Jeff Hammel <k0scist@gmail.com>
parents: 116
diff changeset
39 )
137
a4e6b6ad6907 cleanup + notes
Jeff Hammel <k0scist@gmail.com>
parents: 118
diff changeset
40 # see https://github.com/bokeh/bokeh/blob/master/bokeh/plotting_helpers.py#L277
a4e6b6ad6907 cleanup + notes
Jeff Hammel <k0scist@gmail.com>
parents: 118
diff changeset
41 # for _known_tools
a4e6b6ad6907 cleanup + notes
Jeff Hammel <k0scist@gmail.com>
parents: 118
diff changeset
42
a4e6b6ad6907 cleanup + notes
Jeff Hammel <k0scist@gmail.com>
parents: 118
diff changeset
43
117
0adf95bdda00 what a waste of time and i still dont have a plot
Jeff Hammel <k0scist@gmail.com>
parents: 116
diff changeset
44 if not len(data) == 2:
0adf95bdda00 what a waste of time and i still dont have a plot
Jeff Hammel <k0scist@gmail.com>
parents: 116
diff changeset
45 raise NotImplementedError('TODO') # -> record TODO items
116
fe820a3afa48 wip but we do need that new dependency
Jeff Hammel <k0scist@gmail.com>
parents: 115
diff changeset
46
117
0adf95bdda00 what a waste of time and i still dont have a plot
Jeff Hammel <k0scist@gmail.com>
parents: 116
diff changeset
47 p1.rect(x=data[0], y=data[1], height=data[1], width=0.2)
118
f98db1657dfa no another guess didnt do it oh well
Jeff Hammel <k0scist@gmail.com>
parents: 117
diff changeset
48 show(VBox(p1))
103
067aa27050a3 limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
49
117
0adf95bdda00 what a waste of time and i still dont have a plot
Jeff Hammel <k0scist@gmail.com>
parents: 116
diff changeset
50 # bar = Bar(data, tools="pan,wheel_zoom,box_zoom,reset,resize")
116
fe820a3afa48 wip but we do need that new dependency
Jeff Hammel <k0scist@gmail.com>
parents: 115
diff changeset
51 # bar.filename(output)
117
0adf95bdda00 what a waste of time and i still dont have a plot
Jeff Hammel <k0scist@gmail.com>
parents: 116
diff changeset
52 # bar.width(len(data)*50)
0adf95bdda00 what a waste of time and i still dont have a plot
Jeff Hammel <k0scist@gmail.com>
parents: 116
diff changeset
53 # bar.show()
103
067aa27050a3 limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
54
107
19a5c2fb52bb add transpose functionality
Jeff Hammel <k0scist@gmail.com>
parents: 104
diff changeset
55
112
7578313b9fbf hook up basic plumbing
Jeff Hammel <k0scist@gmail.com>
parents: 107
diff changeset
56 class BarChartParser(ManipulationParser):
104
889728b8d359 minor cleanup
Jeff Hammel <k0scist@gmail.com>
parents: 103
diff changeset
57 """command line options parser for bar charts"""
103
067aa27050a3 limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
58 # TODO: upstream to PlotParser
067aa27050a3 limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
59
067aa27050a3 limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
60 def __init__(self, **kwargs):
067aa27050a3 limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
61 kwargs.setdefault('description', __doc__)
112
7578313b9fbf hook up basic plumbing
Jeff Hammel <k0scist@gmail.com>
parents: 107
diff changeset
62 ManipulationParser.__init__(self, **kwargs)
104
889728b8d359 minor cleanup
Jeff Hammel <k0scist@gmail.com>
parents: 103
diff changeset
63 self.add_argument('-t', '--title', dest='title',
889728b8d359 minor cleanup
Jeff Hammel <k0scist@gmail.com>
parents: 103
diff changeset
64 help="plot title")
103
067aa27050a3 limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
65
117
0adf95bdda00 what a waste of time and i still dont have a plot
Jeff Hammel <k0scist@gmail.com>
parents: 116
diff changeset
66 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
67 """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
68 # 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
69 # 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
70 # 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
71 # 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
72 # '20150315203424.html'
0adf95bdda00 what a waste of time and i still dont have a plot
Jeff Hammel <k0scist@gmail.com>
parents: 116
diff changeset
73 # 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
74 # 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
75 # 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
76 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
77
103
067aa27050a3 limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
78
067aa27050a3 limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
79 def main(args=sys.argv[1:]):
067aa27050a3 limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
80 """CLI"""
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 # parse command line
067aa27050a3 limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
83 parser = BarChartParser()
067aa27050a3 limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
84 options = parser.parse_args(args)
067aa27050a3 limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
85
112
7578313b9fbf hook up basic plumbing
Jeff Hammel <k0scist@gmail.com>
parents: 107
diff changeset
86 # process data
7578313b9fbf hook up basic plumbing
Jeff Hammel <k0scist@gmail.com>
parents: 107
diff changeset
87 data = parser.typed_data()
7578313b9fbf hook up basic plumbing
Jeff Hammel <k0scist@gmail.com>
parents: 107
diff changeset
88
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
89 # ensure a mapping is given
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
90 if len(data) == 1:
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
91 data.insert(0, range(len(data[-1])))
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
92 if len(data) != 2:
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
93 parser.error("A mapping is required")
116
fe820a3afa48 wip but we do need that new dependency
Jeff Hammel <k0scist@gmail.com>
parents: 115
diff changeset
94 # mapping = OrderedDict(transpose(data))
fe820a3afa48 wip but we do need that new dependency
Jeff Hammel <k0scist@gmail.com>
parents: 115
diff changeset
95
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
96
113
f9900883db2e hook it up and break it down
Jeff Hammel <k0scist@gmail.com>
parents: 112
diff changeset
97 # generate bar chart
117
0adf95bdda00 what a waste of time and i still dont have a plot
Jeff Hammel <k0scist@gmail.com>
parents: 116
diff changeset
98 bar_chart(data, parser.plot_filename(), title=options.title)
0adf95bdda00 what a waste of time and i still dont have a plot
Jeff Hammel <k0scist@gmail.com>
parents: 116
diff changeset
99 # bar_chart(data, options.output, title=options.title)
103
067aa27050a3 limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
100
113
f9900883db2e hook it up and break it down
Jeff Hammel <k0scist@gmail.com>
parents: 112
diff changeset
101 # BBB keeping this around for reference;
f9900883db2e hook it up and break it down
Jeff Hammel <k0scist@gmail.com>
parents: 112
diff changeset
102 # we should probably move to a better parsing system at some point
f9900883db2e hook it up and break it down
Jeff Hammel <k0scist@gmail.com>
parents: 112
diff changeset
103 # parse file
f9900883db2e hook it up and break it down
Jeff Hammel <k0scist@gmail.com>
parents: 112
diff changeset
104 # data = pd.read_csv(options.input, header=None, names=options.columns, index_col=0)
103
067aa27050a3 limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
105
067aa27050a3 limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
106 if __name__ == '__main__':
067aa27050a3 limping along towards bar charts
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
107 main()