1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 """
20 Routines for dive profile data plotting.
21
22 The R scripts are used for plotting, see stats/pplot-*.R files.
23
24 Before R script execution, the kz.dives.ui data frame is injected into R
25 space to have preformatted dive data like dive title, dive legend label or
26 dive information ready for a dive graph (and formatted with Python as it is
27 more convenient).
28 """
29
30 import itertools
31 from itertools import zip_longest as lzip
32 import logging
33
34 import rpy2.robjects as ro
35 R = ro.r
36
37 from kenozooid.util import min2str, FMT_DIVETIME
38 from kenozooid.units import K2C
39 import kenozooid.analyze as ka
40 import kenozooid.rglue as kr
41
42 log = logging.getLogger('kenozooid.plot')
43
46
47 """
48 Inject ``kz.dives.ui`` data frame into R global space.
49
50 The data frame has the following columns
51
52 info
53 Dive information string with dive duration, maximum depth and
54 dive temperature.
55 title
56 Dive title.
57 avg_depth
58 Dive average depth label.
59 label
60 Dive label put on legend.
61
62 See ``plot`` for parameters description.
63 """
64 labels = [] if labels is None else labels
65
66
67 tfmt = lambda d, l: '{}'.format(d.datetime)
68
69
70 _ifmt = '{:.1f}m \u00b7 {}min \u00b7 {:.1f}\u00b0C'.format
71 ifmt = lambda d, l: _ifmt(d.depth, min2str(d.duration / 60.0), K2C(d.temp))
72
73
74 dfmt = lambda d, l: None if d.avg_depth is None \
75 else '{:.1f}m'.format(d.avg_depth)
76
77
78 lfmt = lambda d, l: l if l else tfmt(d)
79
80 cols = []
81 fmts = []
82 if title:
83 cols.append('title')
84 fmts.append(tfmt)
85 if info:
86 cols.append('info')
87 fmts.append(ifmt)
88 if avg_depth:
89 cols.append('avg_depth')
90 fmts.append(dfmt)
91 if legend:
92 cols.append('label')
93 fmts.append(lfmt)
94 vt = (ro.StrVector, ) * len(cols)
95 data = (tuple(f(d, l) for f in fmts) for d, l in lzip(dives, labels))
96 ui_df = kr.df(cols, vt, data)
97
98 ro.globalenv['kz.dives.ui'] = ui_df
99
100
101 -def plot(dives, fout, ptype='details', title=False, info=False, temp=False,
102 avg_depth=False, mod=False, sig=True, legend=False, labels=None,
103 format='pdf'):
104 """
105 Plot graphs of dive profiles.
106
107 :Parameters:
108 dives
109 Dives to be plotted.
110 fout
111 Name of output file.
112 ptype
113 Plot type converted to R script name ``stats/pplot-*.R``.
114 title
115 Set plot title.
116 info
117 Display dive information (time, depth, temperature).
118 temp
119 Plot temperature graph.
120 avg_depth
121 Plot dive average depth.
122 mod
123 Plot MOD of current gas.
124 sig
125 Display Kenozooid signature.
126 legend
127 Display graph legend.
128 labels
129 Alternative legend labels.
130 format
131 Format of output file (i.e. pdf, png, svg).
132 """
133 dives, dt = itertools.tee(dives, 2)
134 _inject_dives_ui(dt, title=title, info=info, temp=temp,
135 avg_depth=avg_depth, legend=legend, labels=labels)
136
137 v = lambda s, t: '--{}'.format(s) if t else '--no-{}'
138 args = (fout, format, v('sig', sig), v('mod', mod))
139 ka.analyze('pplot-{}.R'.format(ptype), args, dives)
140
141
142
143