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

Exception when using lm. #18

Open
JocuperDARY opened this issue Feb 1, 2024 · 7 comments
Open

Exception when using lm. #18

JocuperDARY opened this issue Feb 1, 2024 · 7 comments

Comments

@JocuperDARY
Copy link

JocuperDARY commented Feb 1, 2024

Some coords might cause a problem like the below:

  File C:\ProgramData\anaconda3\Lib\site-packages\circle_fit\circle_fit.py:233 in lm
    DelPar = mldivide(np.vstack((J, lambda_sqrt * np.identity(3))), np.hstack([g, np.zeros(3)]))

  File C:\ProgramData\anaconda3\Lib\site-packages\circle_fit\circle_fit.py:82 in mldivide
    sol[nz, :] = np.asarray(np.linalg.solve(A[:, nz], b))

  File C:\ProgramData\anaconda3\Lib\site-packages\numpy\linalg\linalg.py:396 in solve
    _assert_stacked_square(a)

  File C:\ProgramData\anaconda3\Lib\site-packages\numpy\linalg\linalg.py:213 in _assert_stacked_square
    raise LinAlgError('Last 2 dimensions of the array must be square')

LinAlgError: Last 2 dimensions of the array must be square

The Exception appears when the varible "rank" is not equal to "num_vars" in the function "mldivide", but I have no idea how it happens.
According to the Stack Overflow (https://stackoverflow.com/questions/33614378/how-can-i-obtain-the-same-special-solutions-to-underdetermined-linear-systems), repleacing the function with the following might solve the problem:

def mldivide(A: npt.NDArray, b: npt.NDArray) -> npt.NDArray:
    num_vars = A.shape[1]
    x1, res, rank, s = np.linalg.lstsq(A, b, rcond=None)
    if rank == num_vars:
        sol: npt.NDArray = x1  # not under-determined
    else:
        try:
            sol = np.zeros((num_vars, 1))
            for nz in combinations(range(num_vars), rank):  # the variables not set to zero
                sol[nz, :] = np.asarray(np.linalg.solve(A[:, nz], b))
        except:
            Q, R, P = qr(A.T, mode='full', pivoting=True)
            Z = Q[:, rank:].conj()
            C = np.linalg.solve(Z[rank :], -x1[rank :])
            sol = x1 + Z.dot(C)
    return sol
@nup002
Copy link
Collaborator

nup002 commented Feb 2, 2024

Hi @JocuperDARY , thank you for the bug report. Can you post the coords you provide to lm that causes this error? Please try to reduce the amount of data to the minimum amount that still causes the error.

I took the liberty of formatting your post with triple quotes so that the code snippets are formatted correctly.

@JocuperDARY
Copy link
Author

JocuperDARY commented Feb 2, 2024

import numpy as np
import circle_fit
coords = np.array([[-1.56078000e+03, 6.92745426e-01]
, [-1.56074000e+03, 7.07348320e-01]
, [-1.56070000e+03, 7.21708276e-01]
, [-1.56066000e+03, 7.36362813e-01]
, [-1.56062000e+03, 7.50619567e-01]
, [-1.56058000e+03, 7.64246289e-01]
, [-1.56054000e+03, 7.78310843e-01]
, [-1.56050000e+03, 7.91871212e-01]
, [-1.56046000e+03, 8.04925490e-01]
, [-1.56042000e+03, 8.18261888e-01]
, [-1.56038000e+03, 8.31818728e-01]
, [-1.56034000e+03, 8.44824445e-01]
, [-1.56030000e+03, 8.57567586e-01]
, [-1.56026000e+03, 8.70146809e-01]
, [-1.56022000e+03, 8.82460881e-01]
, [-1.56018000e+03, 8.95108674e-01]
, [-1.56014000e+03, 9.07059170e-01]
, [-1.56010000e+03, 9.18921453e-01]
, [-1.56006000e+03, 9.30614709e-01]
, [-1.56002000e+03, 9.41408226e-01]
, [-1.55998000e+03, 9.52591391e-01]
, [-1.55994000e+03, 9.63923685e-01]
, [-1.55990000e+03, 9.74634684e-01]
, [-1.55986000e+03, 9.85424057e-01]
, [-1.55982000e+03, 9.95871559e-01]
, [-1.55978000e+03, 1.00612703e+00]
, [-1.55974000e+03, 1.01594041e+00]
, [-1.55970000e+03, 1.02522169e+00]
, [-1.55966000e+03, 1.03503096e+00]
, [-1.55962000e+03, 1.04436837e+00]
, [-1.55958000e+03, 1.05371418e+00]
, [-1.55954000e+03, 1.06187866e+00]
, [-1.55950000e+03, 1.07051218e+00]
, [-1.55946000e+03, 1.07887518e+00]
, [-1.55942000e+03, 1.08720814e+00]
, [-1.55938000e+03, 1.09534160e+00]
, [-1.55934000e+03, 1.10321616e+00]
, [-1.55930000e+03, 1.11050244e+00]
, [-1.55926000e+03, 1.11756115e+00]
, [-1.55922000e+03, 1.12469301e+00]
, [-1.55918000e+03, 1.13179879e+00]
, [-1.55914000e+03, 1.13824928e+00]
, [-1.55910000e+03, 1.14477532e+00]
, [-1.55906000e+03, 1.15113777e+00]
, [-1.55902000e+03, 1.15724751e+00]
, [-1.55898000e+03, 1.16314544e+00]
, [-1.55894000e+03, 1.16866249e+00]
, [-1.55890000e+03, 1.17430960e+00]
, [-1.55886000e+03, 1.17971769e+00]
, [-1.55882000e+03, 1.18482773e+00]])
par_ini = [-1557.66, 8, 9.375]
b = circle_fit.lm(coords, par_ini)
print(b)

This should work

@nup002
Copy link
Collaborator

nup002 commented Feb 10, 2024

I cannot replicate this bug. Using the data you posted above executes with no problems.
Can you post the version number of all the packages in your environment, as well as which python version you are on? circle-fit requires python>=3.7.

@JocuperDARY
Copy link
Author

Python 3.11.7 (tags/v3.11.7:fa7a6f2, Dec  4 2023, 19:24:49) [MSC v.1937 64 bit (AMD64)] on win32
Package                       Version
----------------------------- ---------------
aiobotocore                   2.7.0
aiohttp                       3.9.0
aioitertools                  0.7.1
aiosignal                     1.2.0
alabaster                     0.7.12
anaconda-anon-usage           0.4.2
anaconda-catalogs             0.2.0
anaconda-client               1.12.1
anaconda-cloud-auth           0.1.4
anaconda-navigator            2.5.0
anaconda-project              0.11.1
anyio                         3.5.0
appdirs                       1.4.4
archspec                      0.2.1
argon2-cffi                   21.3.0
argon2-cffi-bindings          21.2.0
arrow                         1.2.3
astroid                       2.14.2
astropy                       5.3.4
asttokens                     2.0.5
async-lru                     2.0.4
atomicwrites                  1.4.0
attrs                         23.1.0
Automat                       20.2.0
autopep8                      1.6.0
Babel                         2.11.0
backports.functools-lru-cache 1.6.4
backports.tempfile            1.0
backports.weakref             1.0.post1
bcrypt                        3.2.0
beautifulsoup4                4.12.2
binaryornot                   0.4.4
black                         23.11.0
bleach                        4.1.0
blosc2                        2.0.0
bokeh                         3.3.0
boltons                       23.0.0
botocore                      1.31.64
Bottleneck                    1.3.5
Brotli                        1.0.9
certifi                       2023.11.17
cffi                          1.16.0
chardet                       4.0.0
charset-normalizer            2.0.4
circle-fit                    0.2.1
click                         8.1.7
cloudpickle                   2.2.1
clyent                        1.2.2
colorama                      0.4.6
colorcet                      3.0.1
comm                          0.1.2
conda                         23.11.0
conda-build                   3.28.4
conda-content-trust           0.2.0
conda_index                   0.3.0
conda-libmamba-solver         23.12.0
conda-pack                    0.6.0
conda-package-handling        2.2.0
conda_package_streaming       0.9.0
conda-repo-cli                1.0.75
conda-token                   0.4.0
conda-verify                  3.4.2
constantly                    23.10.4
contourpy                     1.2.0
cookiecutter                  2.5.0
cryptography                  41.0.7
cssselect                     1.1.0
cycler                        0.11.0
Cython                        3.0.8
cytoolz                       0.12.2
daal4py                       2023.1.1
dask                          2023.11.0
datasets                      2.12.0
datashader                    0.16.0
debugpy                       1.6.7
decorator                     5.1.1
defusedxml                    0.7.1
diff-match-patch              20200713
dill                          0.3.6
distributed                   2023.11.0
distro                        1.8.0
docstring-to-markdown         0.11
docutils                      0.18.1
entrypoints                   0.4
et-xmlfile                    1.1.0
executing                     0.8.3
fastjsonschema                2.16.2
filelock                      3.13.1
flake8                        6.0.0
Flask                         2.2.5
fonttools                     4.25.0
frozenlist                    1.4.0
fsspec                        2023.10.0
future                        0.18.3
gensim                        4.3.0
gmpy2                         2.1.2
greenlet                      3.0.1
h5py                          3.9.0
HeapDict                      1.0.1
holoviews                     1.18.1
huggingface-hub               0.17.3
hvplot                        0.9.1
hyperlink                     21.0.0
idna                          3.4
imagecodecs                   2023.1.23
imageio                       2.31.4
imagesize                     1.4.1
imbalanced-learn              0.11.0
importlib-metadata            7.0.1
incremental                   21.3.0
inflection                    0.5.1
iniconfig                     1.1.1
intake                        0.6.8
intervaltree                  3.1.0
ipykernel                     6.28.0
ipython                       8.20.0
ipython-genutils              0.2.0
ipywidgets                    8.0.4
isort                         5.9.3
itemadapter                   0.3.0
itemloaders                   1.0.4
itsdangerous                  2.0.1
jaraco.classes                3.2.1
jedi                          0.18.1
jellyfish                     1.0.1
Jinja2                        3.1.2
jmespath                      1.0.1
joblib                        1.2.0
json5                         0.9.6
jsonpatch                     1.32
jsonpointer                   2.1
jsonschema                    4.19.2
jsonschema-specifications     2023.7.1
jupyter                       1.0.0
jupyter_client                8.6.0
jupyter-console               6.6.3
jupyter_core                  5.5.0
jupyter-events                0.8.0
jupyter-lsp                   2.2.0
jupyter_server                2.10.0
jupyter_server_terminals      0.4.4
jupyterlab                    4.0.8
jupyterlab-pygments           0.1.2
jupyterlab_server             2.25.1
jupyterlab-widgets            3.0.9
kaleido                       0.2.1
keyring                       23.13.1
kiwisolver                    1.4.4
lazy_loader                   0.3
lazy-object-proxy             1.6.0
libarchive-c                  2.9
libmambapy                    1.5.6
linkify-it-py                 2.0.0
llvmlite                      0.41.0
lmdb                          1.4.1
locket                        1.0.0
lxml                          4.9.3
lz4                           4.3.2
Markdown                      3.4.1
markdown-it-py                2.2.0
MarkupSafe                    2.1.3
matplotlib                    3.8.0
matplotlib-inline             0.1.6
mccabe                        0.7.0
mdit-py-plugins               0.3.0
mdurl                         0.1.0
menuinst                      1.4.19
mistune                       2.0.4
mkl-fft                       1.3.8
mkl-random                    1.2.4
mkl-service                   2.4.0
more-itertools                10.1.0
mpmath                        1.3.0
msgpack                       1.0.3
multidict                     6.0.4
multipledispatch              0.6.0
multiprocess                  0.70.14
munkres                       1.1.4
mypy-extensions               1.0.0
navigator-updater             0.4.0
nbclient                      0.8.0
nbconvert                     7.10.0
nbformat                      5.9.2
nest-asyncio                  1.5.6
networkx                      3.1
nltk                          3.8.1
notebook                      7.0.6
notebook_shim                 0.2.3
numba                         0.58.1
numexpr                       2.8.7
numpy                         1.26.3
numpydoc                      1.5.0
openpyxl                      3.0.10
overrides                     7.4.0
packaging                     23.1
pandas                        2.1.4
pandocfilters                 1.5.0
panel                         1.3.1
param                         2.0.2
paramiko                      2.8.1
parsel                        1.6.0
parso                         0.8.3
partd                         1.4.1
pathlib                       1.0.1
pathspec                      0.10.3
patsy                         0.5.3
pep8                          1.7.1
pexpect                       4.8.0
pickleshare                   0.7.5
Pillow                        10.0.1
pip                           23.3.2
pkce                          1.0.3
pkginfo                       1.9.6
platformdirs                  3.10.0
plotly                        5.9.0
pluggy                        1.0.0
ply                           3.11
prometheus-client             0.14.1
prompt-toolkit                3.0.43
Protego                       0.1.16
psutil                        5.9.0
ptyprocess                    0.7.0
pure-eval                     0.2.2
py-cpuinfo                    9.0.0
pyarrow                       11.0.0
pyasn1                        0.4.8
pyasn1-modules                0.2.8
pycodestyle                   2.10.0
pycosat                       0.6.6
pycparser                     2.21
pyct                          0.5.0
pycurl                        7.45.2
pydantic                      1.10.12
PyDispatcher                  2.0.5
pydocstyle                    6.3.0
pyerfa                        2.0.0
pyflakes                      3.0.1
Pygments                      2.15.1
PyJWT                         2.4.0
pylint                        2.16.2
pylint-venv                   2.3.0
pyls-spyder                   0.4.0
PyNaCl                        1.5.0
pyodbc                        5.0.1
pyOpenSSL                     23.2.0
pyparsing                     3.0.9
PyQt5                         5.15.10
PyQt5-sip                     12.13.0
PyQtWebEngine                 5.15.6
PySocks                       1.7.1
pytest                        7.4.0
python-dateutil               2.8.2
python-dotenv                 0.21.0
python-json-logger            2.0.7
python-lsp-black              1.2.1
python-lsp-jsonrpc            1.0.0
python-lsp-server             1.7.2
python-slugify                5.0.2
python-snappy                 0.6.1
pytoolconfig                  1.2.6
pytz                          2023.3.post1
pyviz_comms                   3.0.0
pywavelets                    1.5.0
pywin32                       305.1
pywin32-ctypes                0.2.0
pywinpty                      2.0.10
PyYAML                        6.0.1
pyzmq                         25.1.2
QDarkStyle                    3.0.2
qstylizer                     0.2.2
QtAwesome                     1.2.2
qtconsole                     5.4.2
QtPy                          2.4.1
queuelib                      1.6.2
referencing                   0.30.2
regex                         2023.10.3
requests                      2.31.0
requests-file                 1.5.1
requests-toolbelt             1.0.0
responses                     0.13.3
rfc3339-validator             0.1.4
rfc3986-validator             0.1.1
rich                          13.3.5
rope                          1.7.0
rpds-py                       0.10.6
Rtree                         1.0.1
ruamel.yaml                   0.17.21
ruamel-yaml-conda             0.17.21
s3fs                          2023.10.0
safetensors                   0.4.0
scikit-image                  0.20.0
scikit-learn                  1.2.2
scikit-learn-intelex          20230426.121932
scipy                         1.11.4
Scrapy                        2.8.0
seaborn                       0.12.2
semver                        2.13.0
Send2Trash                    1.8.2
service-identity              18.1.0
setuptools                    68.0.0
sip                           6.7.12
six                           1.16.0
smart-open                    5.2.1
sniffio                       1.3.0
snowballstemmer               2.2.0
sortedcontainers              2.4.0
soupsieve                     2.5
Sphinx                        5.0.2
sphinxcontrib-applehelp       1.0.2
sphinxcontrib-devhelp         1.0.2
sphinxcontrib-htmlhelp        2.0.0
sphinxcontrib-jsmath          1.0.1
sphinxcontrib-qthelp          1.0.3
sphinxcontrib-serializinghtml 1.1.5
spyder                        5.4.3
spyder-kernels                2.4.4
SQLAlchemy                    2.0.25
stack-data                    0.2.0
statsmodels                   0.14.0
sympy                         1.12
tables                        3.9.2
tabulate                      0.9.0
TBB                           0.2
tblib                         1.7.0
tenacity                      8.2.2
terminado                     0.17.1
text-unidecode                1.3
textdistance                  4.2.1
threadpoolctl                 2.2.0
three-merge                   0.1.1
tifffile                      2023.4.12
tinycss2                      1.2.1
tldextract                    3.2.0
tokenizers                    0.13.3
toml                          0.10.2
tomlkit                       0.11.1
toolz                         0.12.0
torch                         2.1.1
torchaudio                    2.1.1
torchvision                   0.16.1
tornado                       6.3.3
tqdm                          4.65.0
traitlets                     5.7.1
transformers                  4.32.1
truststore                    0.8.0
Twisted                       22.10.0
twisted-iocpsupport           1.0.2
typing_extensions             4.9.0
tzdata                        2023.3
uc-micro-py                   1.0.1
ujson                         5.4.0
Unidecode                     1.2.0
urllib3                       1.26.18
w3lib                         1.21.0
watchdog                      2.1.6
wcwidth                       0.2.5
webencodings                  0.5.1
websocket-client              0.58.0
Werkzeug                      2.2.3
whatthepatch                  1.0.2
wheel                         0.38.4
widgetsnbextension            4.0.5
win-inet-pton                 1.1.0
wrapt                         1.14.1
xarray                        2023.6.0
xlwings                       0.29.1
xxhash                        2.0.2
xyzservices                   2022.9.0
yapf                          0.31.0
yarl                          1.9.3
zict                          3.0.0
zipp                          3.17.0
zope.interface                5.4.0
zstandard                     0.19.0

@nup002
Copy link
Collaborator

nup002 commented Feb 12, 2024

Please start a fresh new environment and only install circle-fit and see if you still have the same problem.

@JocuperDARY
Copy link
Author

I created a new environment with python 3.11.7 and the problem still exists.

Python 3.11.7 | packaged by Anaconda, Inc. | (main, Dec 15 2023, 18:05:47) [MSC v.1916 64 bit (AMD64)] on win32
Package    Version
---------- -------
circle-fit 0.2.1
numpy      1.26.4
pip        23.3.1
scipy      1.12.0
setuptools 68.2.2
wheel      0.41.2

@nup002
Copy link
Collaborator

nup002 commented Feb 15, 2024

Okey, I can replicate it now. I will investigate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants