1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 """
21 Dive analytics via R statistical package.
22 """
23
24 import rpy2.robjects as ro
25 import rpy2.rinterface
26 import logging
27 import pkg_resources
28 import os.path
29
30 import kenozooid.uddf as ku
31 import kenozooid.rglue as kr
32
33 log = logging.getLogger('kenozooid.analyze')
34 R = ro.r
35
36
38 """
39 Analyze dives with specified R script.
40
41 The dive data is converted into R data frames and script is executed in
42 the context of the converted data.
43
44 :Parameters:
45 script
46 R script to run in the context of dive data.
47 args
48 R script arguments.
49 dives
50 Dive data.
51 """
52 if not os.path.exists(script):
53 log.debug('loading {} script as resource'.format(script))
54 script = pkg_resources.resource_filename('kenozooid',
55 'stats/{}'.format(script))
56 else:
57 log.debug('loading {} script as file'.format(script))
58
59 kr.inject_dive_data(dives)
60
61 if args:
62 ro.globalenv['kz.args'] = ro.StrVector(args)
63 else:
64 R('kz.args = list()')
65
66 R('source("{}")'.format(script))
67
68
69
70