scikit-tda / kepler-mapper

Kepler Mapper: A flexible Python implementation of the Mapper algorithm.
https://kepler-mapper.scikit-tda.org
MIT License
623 stars 180 forks source link

Issue with generating visuals in mapper #252

Open wingenium-nagesh opened 5 months ago

wingenium-nagesh commented 5 months ago

Describe the bug During execution of the mapper.visualize() function, it errors out in Visuals.py (line 573 - np.asscalar(object) "Numpy does not support attribute asscalar".

To Reproduce Steps to reproduce the behavior:

  1. brew install numpy (1.26.3)
  2. Open Visual Studio Code
  3. Follow the instructions in https://kepler-mapper.scikit-tda.org/en/latest/generated/gallery/plot_breast_cancer.html#sphx-glr-generated-gallery-plot-breast-cancer-py
  4. Download the notebook https://kepler-mapper.scikit-tda.org/en/latest/_downloads/a94b1f1598c3cfb97a9077a22fcc2de4/plot_breast_cancer.ipynb
  5. Install kmapper using the following notebook directive %pip install --upgrade kmapper
  6. Execute the notebook
  7. It errors out on the below mentioned function:

mapper.visualize( graph, path_html="breast-cancer.html", title="Wisconsin Breast Cancer Dataset", custom_tooltips=y, )

Expected behavior The expectation is that the visualize function should create a visualization map and stores it as an html file with the given filename

Screenshots

Desktop (please complete the following information):

Smartphone (please complete the following information): Not applicable

Additional context I believe the issue happens because of a bug in the code in Visuals.py on line 573. The code (between lines 569-573) should be replaced as follows:

# Jinja default json serializer can't handle np arrays; provide custom encoding
def my_dumper(obj, **kwargs):
    def np_encoder(object, **kwargs):
        if isinstance(object, np.generic):
            #return np.asscalar(object)  # <-- ERROR and a BUG on line 573
            return object.item()   # <-- The correct code should be this
catanzaromj commented 3 months ago

@wingenium-nagesh Thanks for catching this! If you would like to implement the change yourself, you can fork the repo, make the change on your fork and then open a PR. If you need help with this, let me know.

linusgun commented 2 months ago

I got the same error, but luckily I created another venv using Python 3.12.2, numpy 1.26.4 and plotly 5.21.0 and the error disappeared. The original venv (where I got the error) was Python 3.11.6, numpy 1.26.4 and plotly 5.20.0. Maybe this can help. Btw I used the same version of kmapper 2.0.1 in both cases

blue-j commented 1 week ago

@wingenium-nagesh Thanks for catching this! If you would like to implement the change yourself, you can fork the repo, make the change on your fork and then open a PR. If you need help with this, let me know.

Several people have already submitted this PR.

wingenium-nagesh commented 1 week ago

@blue-j @catanzaromj : apologies for the delay in responding. I will raise the PR shortly.

blue-j commented 1 week ago

you are kind to respond. the PRs are there to approve already. do you have write access? - J

catanzaromj commented 1 week ago

@wingenium-nagesh @blue-j Thank you both for the PRs. The build and deploy process for kepler-mapper is deprecated (notice the tests are only checking with python 3.6 - 3.8 here--and python 3.6 is EOL with no runners available). Give me a week to update the build and release process, and then we can get one of your solutions merged.

wingenium-nagesh commented 1 week ago

@catanzaromj @blue-j - thanks. I understand. I've raised the PR [https://github.com/Wingenium/kepler-mapper/tree/bugfix/issue-252-np-asscalar-error]. Thanks!