lucasmaystre / choix

Inference algorithms for models based on Luce's choice axiom
MIT License
158 stars 27 forks source link

Using Python sets to represent top-1 data leads to TypeError #21

Open Feuermurmel opened 2 years ago

Feuermurmel commented 2 years ago

The documentation states that to represent a top-1 list, a Python list with an integer and a Python set should be used. This leads to a TypeError:

% python3 -m venv venv
% . venv/bin/activate
% pip install choix   
Collecting choix
  Using cached choix-0.3.5.tar.gz (63 kB)
  Preparing metadata (setup.py) ... done
Collecting numpy
  Using cached numpy-1.22.1-cp39-cp39-macosx_11_0_arm64.whl (12.8 MB)
Collecting scipy
  Using cached scipy-1.7.3-1-cp39-cp39-macosx_12_0_arm64.whl (27.0 MB)
Using legacy 'setup.py install' for choix, since package 'wheel' is not installed.
Installing collected packages: numpy, scipy, choix
    Running setup.py install for choix ... done
Successfully installed choix-0.3.5 numpy-1.22.1 scipy-1.7.3
(venv) michi@lappy ~ % python
Python 3.9.10 (main, Jan 20 2022, 11:41:00) 
[Clang 13.0.0 (clang-1300.0.29.30)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import choix
>>> choix.ilsr_top1(3, [[0, {1, 2}]], alpha=0.1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/michi/venv/lib/python3.9/site-packages/choix/lsr.py", line 391, in ilsr_top1
    return _ilsr(fun, initial_params, max_iter, tol)
  File "/Users/michi/venv/lib/python3.9/site-packages/choix/lsr.py", line 30, in _ilsr
    params = fun(initial_params=params)
  File "/Users/michi/venv/lib/python3.9/site-packages/choix/lsr.py", line 350, in lsr_top1
    val = 1 / (weights.take(losers).sum() + weights[winner])
TypeError: int() argument must be a string, a bytes-like object or a number, not 'set'

Using another list instead works:

>>> choix.ilsr_top1(3, [[0, [1, 2]]], alpha=0.1)
array([ 0.97755805, -0.48877902, -0.48877902])

Also, IMHO a tuple is a better fit to represent a top-1 list, e.g. [(0, {1, 2})]. This can be accurately represented using the types from the typing module, i.e. List[Tuple[int, Set[int]]] where the currently documented convention can't.

lucasmaystre commented 2 years ago

Hi @Feuermurmel thanks for the report. I will look into this as soon as I get a chance.