Search code examples
pythonzfit

Error when running zfit on Ubuntu 22.04 with Python 3.10.12 and TensorFlow 2.18.0


I am trying to run zfit on my Ubuntu 22.04 laptop. I installed it like this:

python -m venv venv
source venv/bin/activate
pip install zfit

I've run this code (taken from https://github.com/zfit/zfit):

import zfit
import numpy as np

obs = zfit.Space('x', -10, 10)

# create the model
mu    = zfit.Parameter("mu"   , 2.4, -1, 5)
sigma = zfit.Parameter("sigma", 1.3,  0, 5)
gauss = zfit.pdf.Gauss(obs=obs, mu=mu, sigma=sigma)

# load the data
data_np = np.random.normal(size=10000)
data = zfit.Data(obs=obs, data=data_np)
# or sample from model
data = gauss.sample()

and I've got this error:

Traceback (most recent call last):
  File "/home/pleskot/ftft/test.py", line 15, in <module>
    data = gauss.sample()
  File "/home/pleskot/ftft/venv/lib/python3.10/site-packages/zfit/core/basemodel.py", line 1196, in sample
    tensor = run_tf(n=n, limits=limits, x=xclean)
  File "/home/pleskot/ftft/venv/lib/python3.10/site-packages/zfit/core/basemodel.py", line 1192, in run_tf
    return self._single_hook_sample(n=n, limits=limits, x=x)  # todo: make data a composite object
  File "/home/pleskot/ftft/venv/lib/python3.10/site-packages/zfit/z/zextension.py", line 332, in concrete_func
    result = func_to_run(*args, **kwargs)
  File "/home/pleskot/ftft/venv/lib/python3.10/site-packages/tensorflow/python/util/traceback_utils.py", line 153, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "/home/pleskot/ftft/venv/lib/python3.10/site-packages/zfit/core/basemodel.py", line 1202, in _single_hook_sample
    return self._hook_sample(n=n, limits=limits)
  File "/home/pleskot/ftft/venv/lib/python3.10/site-packages/zfit/core/basepdf.py", line 785, in _hook_sample
    raise tf.errors.InvalidArgumentError(msg)
TypeError: InvalidArgumentError.__init__() missing 2 required positional arguments: 'op' and 'message'

I've experienced the same thing with Python 3.11 on the same system.


Solution

  • That is actually a typo in the documentation paired with a small bug in zfit: the sample method takes a mandatory argument (the number of points to sample) if it is not extended.

    Due to a small API call bug in zfit, this error message has not been displayed correctly.

    The README has been updated and the errors are also fixed and should now be raised correctly.

    P.S: these kind of errors, if you cannot run a standard example, are best brought up by opening an issue on zfit github