# Using PyCall to call iminuit

**URL:** <https://discourse.julialang.org/t/using-pycall-to-call-iminuit/32819>\
**Category:** New to Julia\
**Tags:** pycall\
**Created:** [December 31, 2019, 8:11am UTC](https://discourse.julialang.org/t/using-pycall-to-call-iminuit/32819 "2019-12-31T08:11:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![zwwi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zwwi/32/11932_2.png) [@zwwi](https://discourse.julialang.org/u/zwwi)\
**Post date:** [December 31, 2019, 8:11am UTC](https://discourse.julialang.org/t/using-pycall-to-call-iminuit/32819/1 "2019-12-31T08:11:14Z")

</div>

Hello, I’m working in high energy physics, and there is a package called MINUIT, [https://seal.web.cern.ch/seal/snapshot/work-packages/mathlibs/minuit/](https://seal.web.cern.ch/seal/snapshot/work-packages/mathlibs/minuit/), that is commonly used in the field for Function Minimization and Error Analysis. It is in cernlib, and both fortran and c++ versions. Wrapping either of them in Julia does not exist yet. But there is a python wrapping of the c++ version, called iminuit, [https://iminuit.readthedocs.io/en/latest/](https://iminuit.readthedocs.io/en/latest/). It can be installed by `conda install iminuit`.

I then tried PyCall to use iminuit inside Julia.

```julia
minuit = pyimport(:iminuit)

datax = LinRange(0,10, 10)
σ = 1; μ = 0;
off = σ .*randn(10) .+ μ
line(x, a, b) = a + sin(x) * b
datay = line.(datax, 1, 2) .+ 0.2*off

function χsq(a, b)
    err = 0.01
    sum(@. (datay- line(datax, a, b))^2/err )
end

```

I then tried to get the best fit to the data `datay` by minimizing `χsq` with the following line

```julia
minuit.Minuit(χsq, a=5, b=5, error_a = 0.1, error_b = 0.1, errordef = 1)

```

Then I got an error starting with

```julia
PyError ($(Expr(:escape, :(ccall(#= /homepath/.julia/packages/PyCall/ttONZ/src/pyfncall.jl:44 =# @pysym(:PyObject_Call), PyPtr, (PyPtr, PyPtr, PyPtr), o, pyargsptr, kw))))) <class 'TypeError'>
TypeError('Unable to obtain function signature')
  File "iminuit/_libiminuit.pyx", line 475, in iminuit._libiminuit.Minuit. __init__
File "/homepath/anaconda3/lib/python3.7/site-packages/iminuit/util.py", line 350, in describe
    return better_arg_spec(f, verbose)
  File "/homepath/anaconda3/lib/python3.7/site-packages/iminuit/util.py", line 340, in better_arg_spec
    raise TypeError("Unable to obtain function signature")

```

Any help will be very grateful.  
If there is some day a Julia wrapping of the MINUIT package, there would be great!

Thank you in advance

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [December 31, 2019, 6:08pm UTC](https://discourse.julialang.org/t/using-pycall-to-call-iminuit/32819/2 "2019-12-31T18:08:04Z")

</div>

Not too be dismissive, but Julia already has some state of the art told for these problems. Why not use those?

---

<div class="post-metadata">

**Author:** ![zwwi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zwwi/32/11932_2.png) [@zwwi](https://discourse.julialang.org/u/zwwi)\
**Post date:** [January 1, 2020, 8:15am UTC](https://discourse.julialang.org/t/using-pycall-to-call-iminuit/32819/3 "2020-01-01T08:15:37Z")

</div>

MINUIT has lots of subroutines that are very useful and easy-to-use for the data analysis at least in high energy physics. Just copy one sentence from the manual:

> The Minuit processor MINOS was probably the first, and may still be the only, generally available  
> program to calculate parameter errors taking into account both parameter correlations and non-linearities.  
> The MINOS error intervals are in general assymmetric, and may be expensive to calculate, especially if there are a lot of free parameters and the problem is very non-linear.

If this lib can be ported to julia, I’m sure there will be many more julia users from this community.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 1, 2020, 2:18pm UTC](https://discourse.julialang.org/t/using-pycall-to-call-iminuit/32819/4 "2020-01-01T14:18:57Z")

</div>

> [@zwwi](#):
>
> `Unable to obtain function signature`

This is [not a Python function · Issue #268 · JuliaPy/PyCall.jl · GitHub](https://github.com/JuliaPy/PyCall.jl/issues/268)

The workaround is to wrap the Julia function in a Python `lambda`:

```julia
pyfun = py"lambda fun: lambda x, y: fun(x,y)"

minuit.Minuit(pyfun(χsq), a=5, b=5, error_a = 0.1, error_b = 0.1, errordef = 1)

```

---

<div class="post-metadata">

**Author:** ![zwwi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zwwi/32/11932_2.png) [@zwwi](https://discourse.julialang.org/u/zwwi)\
**Post date:** [January 2, 2020, 12:57am UTC](https://discourse.julialang.org/t/using-pycall-to-call-iminuit/32819/5 "2020-01-02T00:57:12Z")

</div>

Thanks a lot! I tested, to make it work, I need to use the same letters for the parameters as those appear in the lambda definition, i.e.,

```julia
pyfun = py"lambda fun: lambda a, b: fun(a, b)"

minuit.Minuit(pyfun(χsq), a=5, b=5, error_a = 0.1, error_b = 0.1, errordef = 1)

```

Then `a`, `b` and `error_a`, `error_b` can be correctly recognized.
