forked from Project-Platypus/Rhodium
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rbridge.py
62 lines (52 loc) · 2.24 KB
/
rbridge.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# Copyright 2015-2016 David Hadka
#
# This file is part of Rhodium, a Python module for robust decision making and
# exploratory modeling.
#
# Rhodium is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Rhodium is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Rhodium. If not, see <http://www.gnu.org/licenses/>.
from __future__ import division, print_function, absolute_import
import six
from pyper import *
from .model import *
import numpy as np
class RModel(Model):
def __init__(self, file, function, **kwargs):
super(RModel, self).__init__(self._evaluate)
self.file = file
self.r_function = function
self.r = R(**kwargs)
with open(file) as f:
self.r(f.read())
def _evaluate(self, **kwargs):
prefix = "...rhodium."
assigned_parameters = []
for parameter in self.parameters:
if parameter.name in kwargs:
self.r.assign(prefix + parameter.name, kwargs[parameter.name])
assigned_parameters.append(parameter)
self.r(prefix + "..result = " + self.r_function + "(" + ",".join([prefix + p.name for p in assigned_parameters]) + ")")
r_result = self.r[prefix + "..result"]
result = {}
if isinstance(r_result, (list, tuple, np.ndarray)):
for i, response in enumerate(self.responses):
result[response.name] = r_result[i]
elif isinstance(r_result, dict):
for response in self.responses:
result[response.name] = r_result[response.name]
else:
if len(self.responses) > 1:
raise ValueError("received more than one response from R")
else:
result[self.responses[0].name] = r_result
return result