# Radial basis function (RBF2D) from ADCME.jl returns MethodError

**URL:** <https://discourse.julialang.org/t/radial-basis-function-rbf2d-from-adcme-jl-returns-methoderror/55598>\
**Category:** General Usage\
**Tags:** question, error\
**Created:** [February 19, 2021, 3:17am UTC](https://discourse.julialang.org/t/radial-basis-function-rbf2d-from-adcme-jl-returns-methoderror/55598 "2021-02-19T03:17:05Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![mdsa3d](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mdsa3d/32/20389_2.png) [@mdsa3d](https://discourse.julialang.org/u/mdsa3d)\
**Post date:** [February 19, 2021, 3:17am UTC](https://discourse.julialang.org/t/radial-basis-function-rbf2d-from-adcme-jl-returns-methoderror/55598/1 "2021-02-19T03:17:05Z")

</div>

I am trying this tutorial for `raidal basis function` using `ADCME.jl` package.  
When I execute the code, I get a method error:

```julia
using ADCME

# use centers on a uniform grid 
n = 5
h = 1/n
xc = Float64[] 
yc = Float64[]
for i = 1:n+1
    for j = 1:n+1
        push!(xc, (i-1)*h)
        push!(yc, (j-1)*h)
    end
end

# by default, c is initialized to Variable(ones(...))
# eps is initialized to ones(...) and no linear terms are used
rbf = RBF2D(xc, yc)

x = rand(100) 
y = rand(100)
f = @. 1+y^2/(1+x^2)

fv = rbf(x, y)
loss = sum((f-fv)^2)

sess = Session(); init(sess)
BFGS!(sess, loss)

```

Error observed for `fv = rbf(x, y)` line:

```julia
MethodError: no method matching load_op_and_grad(::Missing, ::String)
Closest candidates are:
  load_op_and_grad(!Matched::Union{String, PyCall.PyObject}, ::String; multiple, verbose) at C:\Users\user\.julia\packages\ADCME\7qIYe\src\extra.jl:160
(::RBF2D)(::Array{Float64,1}, ::Array{Float64,1}) at rbf.jl:64
top-level scope at test-functions.jl:23

```

Please suggest on the possible cause of this error and how may i resolve this issue?

Thanks in advance!!

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [February 19, 2021, 1:54pm UTC](https://discourse.julialang.org/t/radial-basis-function-rbf2d-from-adcme-jl-returns-methoderror/55598/2 "2021-02-19T13:54:49Z")

</div>

Have you tried Surrogates.jl? That should be rather straighforward.

---

<div class="post-metadata">

**Author:** ![mdsa3d](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mdsa3d/32/20389_2.png) [@mdsa3d](https://discourse.julialang.org/u/mdsa3d)\
**Post date:** [February 22, 2021, 6:30am UTC](https://discourse.julialang.org/t/radial-basis-function-rbf2d-from-adcme-jl-returns-methoderror/55598/3 "2021-02-22T06:30:05Z")

</div>

@ChrisRackauckas , Thanks for reminding, highly appreciate it!!

---

<div class="post-metadata">

**Author:** ![mdsa3d](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mdsa3d/32/20389_2.png) [@mdsa3d](https://discourse.julialang.org/u/mdsa3d)\
**Post date:** [February 22, 2021, 11:28am UTC](https://discourse.julialang.org/t/radial-basis-function-rbf2d-from-adcme-jl-returns-methoderror/55598/4 "2021-02-22T11:28:08Z")

</div>

I tried a tutorial using `surrogates.jl`, however my code is throwing an error, saying that in `sample()` sample not defined.

```julia
using Surrogates
using Plots
default()
g(x) = log(x)*x^2 + x^3
n_samples = 30
lower_bound = 5
upper_bound = 25
x = sample(n_samples, lower_bound, upper_bound, SobolSample())
y = g.(x)
scatter(x, y, label="Sampled Points", xlims=(lower_bound, upper_bound), legend=:top)
plot!(f, label="True function", scatter(x, y, label="Sampled Points", xlims=(lower_bound, upper_bound), legend=:top)

radial_surrogate = RadialBasis(x, y, lower_bound, upper_bound)
val = radial_surrogate(5.4)

```

# Error

```julia
UndefVarError: sample not defined
top-level scope at test-functions.jl:39

```

May i know what is the cause of this error?

Thanks !!!

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [February 22, 2021, 1:16pm UTC](https://discourse.julialang.org/t/radial-basis-function-rbf2d-from-adcme-jl-returns-methoderror/55598/5 "2021-02-22T13:16:05Z")

</div>

Are you on Julia v1.5 with Surrogates v1.6?

---

<div class="post-metadata">

**Author:** ![mdsa3d](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mdsa3d/32/20389_2.png) [@mdsa3d](https://discourse.julialang.org/u/mdsa3d)\
**Post date:** [February 22, 2021, 1:30pm UTC](https://discourse.julialang.org/t/radial-basis-function-rbf2d-from-adcme-jl-returns-methoderror/55598/6 "2021-02-22T13:30:32Z")

</div>

Yes, I am using Julia 1.5.3. and Surrogates v1.6!

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [February 22, 2021, 1:32pm UTC](https://discourse.julialang.org/t/radial-basis-function-rbf2d-from-adcme-jl-returns-methoderror/55598/7 "2021-02-22T13:32:26Z")

</div>

Odd… Surrogates.sample?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [February 22, 2021, 1:32pm UTC](https://discourse.julialang.org/t/radial-basis-function-rbf2d-from-adcme-jl-returns-methoderror/55598/8 "2021-02-22T13:32:55Z")

</div>

> [@mdsa3d](#):
>
> `using Surrogates, Sample`

what’s this Sample.jl?

---

<div class="post-metadata">

**Author:** ![mdsa3d](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mdsa3d/32/20389_2.png) [@mdsa3d](https://discourse.julialang.org/u/mdsa3d)\
**Post date:** [February 22, 2021, 1:36pm UTC](https://discourse.julialang.org/t/radial-basis-function-rbf2d-from-adcme-jl-returns-methoderror/55598/9 "2021-02-22T13:36:03Z")

</div>

Apologies, that is my mistake. I will fix that now.

Btw `Surrogates.sample` works!!!

@ChrisRackauckas Thanks !

I will run the test and post update on my implementation.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [February 22, 2021, 1:38pm UTC](https://discourse.julialang.org/t/radial-basis-function-rbf2d-from-adcme-jl-returns-methoderror/55598/10 "2021-02-22T13:38:34Z")

</div>

Is something throwing a warning saying that it’s shadowing `sample` when you do `using`?

---

<div class="post-metadata">

**Author:** ![mdsa3d](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mdsa3d/32/20389_2.png) [@mdsa3d](https://discourse.julialang.org/u/mdsa3d)\
**Post date:** [February 22, 2021, 1:41pm UTC](https://discourse.julialang.org/t/radial-basis-function-rbf2d-from-adcme-jl-returns-methoderror/55598/11 "2021-02-22T13:41:25Z")

</div>

It does not throw any warnings just an error saying that it is not define. The error result is presented in highlight:

```julia
UndefVarError: sample not defined
top-level scope at test-functions.jl:39

```

---

<div class="post-metadata">

**Author:** ![mdsa3d](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mdsa3d/32/20389_2.png) [@mdsa3d](https://discourse.julialang.org/u/mdsa3d)\
**Post date:** [February 22, 2021, 1:52pm UTC](https://discourse.julialang.org/t/radial-basis-function-rbf2d-from-adcme-jl-returns-methoderror/55598/12 "2021-02-22T13:52:33Z")

</div>

@ChrisRackauckas I figured out the issue, why was i getting the error for `sample`, it is due to the `ADCME.jl` package.  
The `ADCME.sample` is creating a conflict with `Surrogates.sample` identifier. This I resolved it by removing the precompilation of the package.

---

<div class="post-metadata">

**Author:** ![mdsa3d](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mdsa3d/32/20389_2.png) [@mdsa3d](https://discourse.julialang.org/u/mdsa3d)\
**Post date:** [February 22, 2021, 2:08pm UTC](https://discourse.julialang.org/t/radial-basis-function-rbf2d-from-adcme-jl-returns-methoderror/55598/13 "2021-02-22T14:08:42Z")

</div>

I ran the tutorial for `Surrogates` and below is the example code:

```julia
using Surrogates
using Plots
default()
g(x) = log(x)*x^2 + x^3
n_samples = 30
lower_bound = 5
upper_bound = 25
x = sample(n_samples, lower_bound, upper_bound, SobolSample())
y = g.(x)

radial_surrogate = RadialBasis(x, y, lower_bound, upper_bound)

plot(x, y, seriestype=:scatter, label="Sampled points", xlims=(lower_bound, upper_bound), legend=:top)
plot!(g, label="True function", xlims=(lower_bound, upper_bound), legend=:top)
plot!(radial_surrogate, label="Surrogate function", xlims=(lower_bound, upper_bound), legend=:top)

```

I would like to know, how may I implement various functions for `rbf` like `multiquadratic`, `gaussian` etc?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [February 22, 2021, 2:12pm UTC](https://discourse.julialang.org/t/radial-basis-function-rbf2d-from-adcme-jl-returns-methoderror/55598/14 "2021-02-22T14:12:34Z")

</div>

> [@mdsa3d](#):
>
> I would like to know, how may I implement various functions for `rbf` like `multiquadratic` , `gaussian` etc?

What do you mean?

---

<div class="post-metadata">

**Author:** ![mdsa3d](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mdsa3d/32/20389_2.png) [@mdsa3d](https://discourse.julialang.org/u/mdsa3d)\
**Post date:** [February 22, 2021, 2:18pm UTC](https://discourse.julialang.org/t/radial-basis-function-rbf2d-from-adcme-jl-returns-methoderror/55598/15 "2021-02-22T14:18:21Z")

</div>

Apologies, for the pixelated snippet,

 ![image](https://global.discourse-cdn.com/julialang/original/3X/0/a/0a13fccc7b63d716ee052f0602c3486dcdbdc631.png)

Reference for above: [scipy.interpolate.Rbf — SciPy v1.6.1 Reference Guide](https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.Rbf.html)

There are various `functions` which are used for operating `rbf` as listed in the documentation. however, this documentation is for `scipy` python. Do we have something like this which we can implement in `Surrogates`? I couldn’t find something similar for julia.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [February 23, 2021, 4:24am UTC](https://discourse.julialang.org/t/radial-basis-function-rbf2d-from-adcme-jl-returns-methoderror/55598/16 "2021-02-23T04:24:22Z")

</div>

Yes, look at the tutorial and it shows how to interpolate the RBF: [Basics · Surrogates.jl](https://surrogates.sciml.ai/latest/tutorials/)

---

<div class="post-metadata">

**Author:** ![mdsa3d](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mdsa3d/32/20389_2.png) [@mdsa3d](https://discourse.julialang.org/u/mdsa3d)\
**Post date:** [February 25, 2021, 5:36am UTC](https://discourse.julialang.org/t/radial-basis-function-rbf2d-from-adcme-jl-returns-methoderror/55598/17 "2021-02-25T05:36:15Z")

</div>

Thanks, I went through everything in documentation again and it is now much clearer now.  
I highly appreciate your input and time.

---

<div class="post-metadata">

**Author:** ![mdsa3d](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mdsa3d/32/20389_2.png) [@mdsa3d](https://discourse.julialang.org/u/mdsa3d)\
**Post date:** [February 25, 2021, 8:27am UTC](https://discourse.julialang.org/t/radial-basis-function-rbf2d-from-adcme-jl-returns-methoderror/55598/18 "2021-02-25T08:27:43Z")

</div>

I build the `surrogate model` for `kriging` function, here is my code:

# Surrogate

```julia
using Surrogates
using Plots, DataFrames
default()

f(x) = sin(x)
n_samples = 10
lower_bound = 0.0
upper_bound = 9

x = sample(n_samples, lower_bound, upper_bound, SobolSample())
y = f.(x)

scatter(x, y, label="Sampled points")

kriging_surrogate = Surrogates.Kriging(x, y, lower_bound, upper_bound, p=1.9);

@show surrogate_optimize(f, SRBF(), lower_bound, upper_bound, kriging_surrogate, SobolSample())

result = []
arr = 0:1:10 

for i in arr
    interpolated = kriging_surrogate(i)
    push!(result, interpolated)
end

plot!(result, label="kriging", legend=:bottomright) #, title="Percentage Error", xlabel="Values", ylabel="Percentage")

```

Plot for the code:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/4/f/4f93b095675f701806377ac87d9a78cbb495854e.png)

I am quite confused why the julia model is not accurate as compared to the python?

# Python Example

```python
import numpy as np
from scipy.interpolate import Rbf, InterpolatedUnivariateSpline

import matplotlib
#matplotlib.use('Agg')
import matplotlib.pyplot as plt

# setup data
x = np.linspace(0, 10, 9)
y = np.sin(x)
xi = np.linspace(0, 10, 101)

# use RBF method
rbf = Rbf(x, y)
fi = rbf(xi)

plt.plot(x, y, 'bo')
plt.plot(xi, fi, 'g')
plt.plot(xi, np.sin(xi), 'r')
plt.title('Interpolation using RBF (scipy) - multiquadrics')
plt.tight_layout()
plt.show()

```

Plot for the python:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/5/9/59c752a3d6dcb45f3c925b77f5c6a89ef58e871b.png)

May i know why is there offset in the datapoints?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [February 25, 2021, 12:18pm UTC](https://discourse.julialang.org/t/radial-basis-function-rbf2d-from-adcme-jl-returns-methoderror/55598/19 "2021-02-25T12:18:47Z")

</div>

that looks good. Can you open an issue?

---

<div class="post-metadata">

**Author:** ![mdsa3d](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mdsa3d/32/20389_2.png) [@mdsa3d](https://discourse.julialang.org/u/mdsa3d)\
**Post date:** [February 25, 2021, 4:16pm UTC](https://discourse.julialang.org/t/radial-basis-function-rbf2d-from-adcme-jl-returns-methoderror/55598/20 "2021-02-25T16:16:58Z")

</div>

Thanks for the suggestion,  
I have open an issue : [Incorrect predicted values for kriging function · Issue #250 · SciML/Surrogates.jl (github.com)](https://github.com/SciML/Surrogates.jl/issues/250)

Looking forward to positive response. 🙂

[Next page](https://discourse.julialang.org/t/radial-basis-function-rbf2d-from-adcme-jl-returns-methoderror/55598.md?page=2)
