Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update comp_function() to take in separate inputs #222

Merged
merged 7 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion grama/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
from toolz import curry



#######
from collections import Iterable
MayaSimone marked this conversation as resolved.
Show resolved Hide resolved


## Package settings
RUNTIME_LOWER = 1 # Cutoff threshold for runtime messages

Expand Down Expand Up @@ -106,8 +111,9 @@ def eval(self, df):
## Set up output
n_rows = df.shape[0]
results = zeros((n_rows, len(self.out)))

for ind in range(n_rows):
results[ind] = self.func(df.loc[ind, self.var])
results[ind] = self.func(*df.loc[ind, self.var])

## Package output as DataFrame
return DataFrame(data=results, columns=self.out)
Expand Down
1 change: 1 addition & 0 deletions grama/eval_defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,7 @@ def eval_sample(model, n=None, df_det=None, seed=None, append=True, skip=False,

return df_samp


df_res = eval_df(model, df=df_samp, append=append)
## Attach metadata
with catch_warnings():
Expand Down
1 change: 1 addition & 0 deletions grama/fit/fit_scikitlearn.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ def __init__(self, regressor, var, out, name, runtime):

def eval(self, df):
## Check invariant; model inputs must be subset of df columns

if not set(self.var).issubset(set(df.columns)):
raise ValueError(
"Model function `{}` var not a subset of given columns".format(
Expand Down
2 changes: 1 addition & 1 deletion grama/models/circuit_RLC.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def make_prlc():
var=["L", "C"],
out=["omega0"],
)
>> cp_function(
>> cp_vec_function(
MayaSimone marked this conversation as resolved.
Show resolved Hide resolved
fun=lambda df: df_make(Q=df.omega0 * df.R * df.C),
name="parallel RLC",
var=["omega0", "R", "C"],
Expand Down
3 changes: 1 addition & 2 deletions grama/models/ishigami.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
from numpy import sin, pi


def fun(x):
a, b, x1, x2, x3 = x
def fun(a, b, x1, x2, x3):
return sin(x1) + a * sin(x2)**2 + b * x3**4 * sin(x1)

def make_ishigami():
Expand Down
4 changes: 1 addition & 3 deletions grama/models/linear_normal.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
from grama import cp_copula_independence, cp_function, cp_marginals, Model


def limit_state(x):
x1, x2 = x

def limit_state(x1, x2):
return 1 - x1 - x2

def make_linear_normal():
Expand Down
26 changes: 13 additions & 13 deletions grama/models/pipe_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,29 @@
from math import sqrt, log10, pow, log
from scipy.optimize import bisect

def re_fcn(q):
def re_fcn(rho,u,d,mu,eps):
# {rho,u,d,mu,eps}
return q[0]*q[1]*q[2]/q[3]
return rho*u*d/mu

def f_lam(q):
def f_lam(rho,u,d,mu,eps):
# {rho,u,d,mu,eps}
return 64. / re_fcn(q)
return 64. / re_fcn(rho,u,d,mu,eps)

def colebrook(q,f):
def colebrook(rho,u,d,mu,eps,f):
# {rho,u,d,mu,eps}
fs = sqrt(f); Re = re_fcn(q)
return 1 + 2.*fs*log10(q[4]/3.6/q[2] + 2.51/Re/fs)
fs = sqrt(f); Re = re_fcn(rho,u,d,mu,eps)
return 1 + 2.*fs*log10(eps/3.6/d + 2.51/Re/fs)

def f_tur(q):
return bisect(lambda f: colebrook(q,f), 1e-5, 10)
def f_tur(rho,u,d,mu,eps):
return bisect(lambda f: colebrook(rho,u,d,mu,eps,f), 1e-5, 10)

Re_c = 3e3
def fcn_pipe(q):
Re = re_fcn(q)
def fcn_pipe(rho,u,d,mu,eps):
Re = re_fcn(rho,u,d,mu,eps)
if Re < Re_c:
return f_lam(q)
return f_lam(rho,u,d,mu,eps)
else:
return f_tur(q)
return f_tur(rho,u,d,mu,eps)

def make_pipe_friction():
r"""Pipe Friction Factor
Expand Down
2 changes: 1 addition & 1 deletion grama/models/plane_laminate.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ def mapSign(x):
name=name,
functions=[
Function(
lambda X: uniaxial_stress_limit(X),
lambda *X: uniaxial_stress_limit(X),
make_names(Theta_nom),
list(itertools.chain.from_iterable([
["g_11_tension_{}".format(i),
Expand Down
3 changes: 1 addition & 2 deletions grama/models/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
df_test_input = DataFrame(data={"x0": [0], "x1": [0], "x2": [0]})

## Define a test model
def fun(x):
x1, x2, x3 = x
def fun(x1, x2, x3):
return x1 + x2 + x3

def make_test():
Expand Down
8 changes: 4 additions & 4 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def setUp(self):
self.model_2d = gr.Model(
functions=[
gr.Function(
lambda x: [x[0], x[1]], ["x0", "x1"], ["y0", "y1"], "test", 0
lambda x0, x1: [x0, x1], ["x0", "x1"], ["y0", "y1"], "test", 0
),
],
domain=domain_2d,
Expand All @@ -49,7 +49,7 @@ def setUp(self):
self.model_3d = gr.Model(
functions=[
gr.Function(
lambda x: x[0] + x[1] + x[2], ["x", "y", "z"], ["f"], "test", 0
lambda x, y, z: x + y + z, ["x", "y", "z"], ["f"], "test", 0
)
],
density=gr.Density(marginals=marginals),
Expand Down Expand Up @@ -111,7 +111,7 @@ def test_var_outer(self):

def test_drop_out(self):
"""Checks that output column names are properly dropped"""
md = gr.Model() >> gr.cp_function(lambda x: x[0] + 1, var=1, out=1)
md = gr.Model() >> gr.cp_function(lambda x0: x0 + 1, var=1, out=1)
df_in = gr.df_make(x0=[0, 1, 2], y0=[0, 1, 2])
df_true = gr.df_make(x0=[0, 1, 2], y0=[1, 2, 3])

Expand Down Expand Up @@ -194,7 +194,7 @@ def test_dag(self):
md = (
gr.Model("model")
>> gr.cp_function(lambda x: x, var=1, out=1)
>> gr.cp_function(lambda x: x[0] + x[1], var=["x0", "y0"], out=1)
>> gr.cp_function(lambda x0, y0: x0 + y0, var=["x0", "y0"], out=1)
)

G_true = nx.DiGraph()
Expand Down
20 changes: 10 additions & 10 deletions tests/test_evals.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def setUp(self):

self.model_2d = gr.Model(
functions=[
gr.Function(lambda x: [x[0], x[1]], ["x", "y"], ["f", "g"], "test", 0)
gr.Function(lambda x, y: [x, y], ["x", "y"], ["f", "g"], "test", 0)
],
domain=domain_2d,
density=gr.Density(
Expand Down Expand Up @@ -310,7 +310,7 @@ def test_grad_fd(self):
## Flags
md_test = (
gr.Model()
>> gr.cp_function(fun=lambda x: x[0] + x[1] ** 2, var=2, out=1)
>> gr.cp_function(fun=lambda x0, x1: x0 + x1 ** 2, var=2, out=1)
>> gr.cp_marginals(x0={"dist": "norm", "loc": 0, "scale": 1})
)
df_base = pd.DataFrame(dict(x0=[0, 1], x1=[0, 1]))
Expand Down Expand Up @@ -368,7 +368,7 @@ def setUp(self):

self.md_2d = (
gr.Model()
>> gr.cp_function(fun=lambda x: x[0], var=2, out=1)
>> gr.cp_function(fun=lambda x0, x1: x0, var=2, out=1)
>> gr.cp_marginals(
x0={"dist": "uniform", "loc": 0, "scale": 1},
x1={"dist": "uniform", "loc": 0, "scale": 1},
Expand All @@ -378,7 +378,7 @@ def setUp(self):

self.md_mixed = (
gr.Model()
>> gr.cp_function(fun=lambda x: x[0] + x[1], var=2, out=1)
>> gr.cp_function(fun=lambda x0, x1: x0 + x1, var=2, out=1)
>> gr.cp_bounds(x0=(-1, +1))
>> gr.cp_marginals(
x1={"dist": "uniform", "loc": 0, "scale": 1},
Expand Down Expand Up @@ -466,7 +466,7 @@ def setUp(self):

self.md_mixed = (
gr.Model()
>> gr.cp_function(fun=lambda x: x[0], var=3, out=1)
>> gr.cp_function(fun=lambda x0, x1, x2: x0, var=3, out=1)
>> gr.cp_bounds(x2=(0, 1))
>> gr.cp_marginals(
x0={"dist": "uniform", "loc": 0, "scale": 1},
Expand Down Expand Up @@ -582,14 +582,14 @@ def test_nls(self):
## Setup
md_feat = (
gr.Model()
>> gr.cp_function(fun=lambda x: x[0] * x[1] + x[2], var=3, out=1,)
>> gr.cp_function(fun=lambda x0, x1, x2: x0 * x1 + x2, var=3, out=1,)
>> gr.cp_bounds(x0=[-1, +1], x2=[0, 0])
>> gr.cp_marginals(x1=dict(dist="norm", loc=0, scale=1))
)

md_const = (
gr.Model()
>> gr.cp_function(fun=lambda x: x[0], var=1, out=1)
>> gr.cp_function(fun=lambda x0: x0, var=1, out=1)
>> gr.cp_bounds(x0=(-1, +1))
)

Expand Down Expand Up @@ -653,13 +653,13 @@ def test_opt(self):
md_bowl = (
gr.Model("Constrained bowl")
>> gr.cp_function(
fun=lambda x: x[0] ** 2 + x[1] ** 2, var=["x", "y"], out=["f"],
fun=lambda x, y: x ** 2 + y ** 2, var=["x", "y"], out=["f"],
)
>> gr.cp_function(
fun=lambda x: (x[0] + x[1] + 1), var=["x", "y"], out=["g1"],
fun=lambda x, y: (x + y + 1), var=["x", "y"], out=["g1"],
)
>> gr.cp_function(
fun=lambda x: -(-x[0] + x[1] - np.sqrt(2 / 10)),
fun=lambda x, y: -(-x + y - np.sqrt(2 / 10)),
var=["x", "y"],
out=["g2"],
)
Expand Down
20 changes: 11 additions & 9 deletions tests/test_fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@ def setUp(self):
## Smooth model
self.md_smooth = (
gr.Model()
# >> gr.cp_function(fun=lambda x: [x, x + 1], var=["x"], out=["y", "z"])
>> gr.cp_vec_function(
fun=lambda df: gr.df_make(y=df.x, z=df.x + 1), var=["x"], out=["y", "z"]
)
>> gr.cp_function(fun=lambda x: [x, x + 1], var=["x"], out=["y", "z"])
# >> gr.cp_vec_function(fun=lambda df: gr.df_make(y=df.x, z=df.x + 1), var=["x"], out=["y", "z"])
>> gr.cp_marginals(x={"dist": "uniform", "loc": 0, "scale": 2})
>> gr.cp_copula_independence()
)

print(self.md_smooth)
MayaSimone marked this conversation as resolved.
Show resolved Hide resolved
self.df_smooth = self.md_smooth >> gr.ev_df(df=pd.DataFrame(dict(x=[0, 1, 2])))

## Tree model
Expand Down Expand Up @@ -159,7 +157,7 @@ def test_nls(self):
md_true = (
gr.Model()
>> gr.cp_function(
fun=lambda x: a_true * np.exp(x[0] * c_true) + x[1],
fun=lambda x, epsilon: a_true * np.exp(x * c_true) + epsilon,
var=["x", "epsilon"],
out=["y"],
)
Expand All @@ -174,7 +172,9 @@ def test_nls(self):
md_param = (
gr.Model()
>> gr.cp_function(
fun=lambda x: x[2] * np.exp(x[0] * x[1]), var=["x", "c", "a"], out=["y"]
fun=lambda x, c, a: a * np.exp(x * c),
var=["x", "c", "a"],
out=["y"]
)
>> gr.cp_bounds(c=[0, 4], a=[0.1, 2.0])
)
Expand All @@ -191,7 +191,7 @@ def test_nls(self):
md_unidet = (
gr.Model()
>> gr.cp_function(
fun=lambda x: x[2] / x[3] * np.exp(x[0] * x[1]),
fun=lambda x, c, a, z: a / z * np.exp(x * c),
var=["x", "c", "a", "z"],
out=["y"],
)
Expand Down Expand Up @@ -222,7 +222,9 @@ def test_nls(self):
md_fixed = (
gr.Model()
>> gr.cp_function(
fun=lambda x: x[2] * np.exp(x[0] * x[1]), var=["x", "c", "a"], out=["y"]
fun=lambda x, c, a: a * np.exp(x * c),
var=["x", "c", "a"],
out=["y"]
)
>> gr.cp_bounds(c=[0, 4], a=[1, 1])
)
Expand Down
10 changes: 5 additions & 5 deletions tests/test_mbi.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,13 @@ def test_comp_function(self):
with self.assertRaises(ValueError):
# Cycle by input
self.md >> gr.cp_function(
fun=lambda x: x[0], var=["y0"], out=1
) >> gr.cp_function(fun=lambda x: x[0], var=1, out=["y0"])
fun=lambda x0: x0, var=["y0"], out=1
) >> gr.cp_function(fun=lambda x0: x0, var=1, out=["y0"])
with self.assertRaises(ValueError):
# Non-unique output
self.md >> gr.cp_function(
fun=lambda x: x[0], var=1, out=["y0"]
) >> gr.cp_function(fun=lambda x: x[0], var=1, out=["y0"])
fun=lambda x0: x0, var=1, out=["y0"]
) >> gr.cp_function(fun=lambda x0: x0, var=1, out=["y0"])

## Check vectorized builder
md_vec = gr.comp_vec_function(
Expand All @@ -148,7 +148,7 @@ def test_comp_model(self):
"""Test model composition"""
md_inner = (
gr.Model("inner")
>> gr.cp_function(fun=lambda x: x[0] + x[1], var=2, out=1)
>> gr.cp_function(fun=lambda x0, x1: x0 + x1, var=2, out=1)
>> gr.cp_marginals(x0=dict(dist="norm", loc=0, scale=1))
>> gr.cp_copula_independence()
)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_tail.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def setUp(self):
self.md = (
gr.Model()
>> gr.cp_function(
fun=lambda x: self.beta_true * 2 - x[0] - np.sqrt(3) * x[1],
fun=lambda x0, x1: self.beta_true * 2 - x0 - np.sqrt(3) * x1,
var=2,
out=["g"],
)
Expand Down