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

nk.download_zip throws AttributeError when invoked #1061

Open
ignatious-p opened this issue Jan 22, 2025 · 3 comments
Open

nk.download_zip throws AttributeError when invoked #1061

ignatious-p opened this issue Jan 22, 2025 · 3 comments

Comments

@ignatious-p
Copy link

ignatious-p commented Jan 22, 2025

Describe the bug

I ran the following code on Google Colab

import pandas as pd
import numpy as np
import wfdb
import os
import neurokit2 as nk

url = "https://physionet.org/static/published-projects/mitdb/mit-bih-arrhythmia-database-1.0.0.zip"
download_successful = nk.download_zip(url, ".")

this returns

---------------------------------------------------------------------------

AttributeError                            Traceback (most recent call last)

[<ipython-input-33-bf0018a1f393>](https://localhost:8080/#) in <cell line: 0>()
      1 url = "https://physionet.org/static/published-projects/mitdb/mit-bih-arrhythmia-database-1.0.0.zip"
----> 2 download_successful = nk.download_zip(url, ".")

1 frames

[/usr/local/lib/python3.11/dist-packages/neurokit2/data/database.py](https://localhost:8080/#) in download_zip(url, destination_path, unzip)
     64 
     65     # Download the ZIP file
---> 66     download_successful = download_from_url(url, zip_filepath)
     67     if download_successful:
     68         if unzip:

[/usr/local/lib/python3.11/dist-packages/neurokit2/data/database.py](https://localhost:8080/#) in download_from_url(url, destination_path)
     27     response = urllib.request.urlopen(url)
     28 
---> 29     if response.status_code == 200:
     30         with destination_path.open("wb") as file:
     31             file.write(response.content)

AttributeError: 'HTTPResponse' object has no attribute 'status_code'

To Reproduce
Run the above code on Google Colab or any Python environment

Expected behaviour
Download the files into the directory

Screenshots
Image

System Specifications

  • OS: Linux (ELF 64bit)

  • Python: 3.11.11

  • NeuroKit2: 0.2.10

  • NumPy: 1.26.4

  • Pandas: 2.2.3

  • SciPy: 1.13.1

  • sklearn: 1.6.0

  • matplotlib: 3.10.0

Copy link

welcome bot commented Jan 22, 2025

Hi 👋 Thanks for reaching out and opening your first issue here! We'll try to come back to you as soon as possible. ❤️ kenobi

@ignatious-p
Copy link
Author

ignatious-p commented Jan 22, 2025

I did some debugging and fixed the error by making the following changes to the download_from_url function in neurokit2/data/database.py.

def download_from_url(url, destination_path=None):

--- SNIP ---

    # if response.status_code == 200:
    if response.status == 200:
        with destination_path.open("wb") as file:
            # file.write(response.content)
            file.write(response.read())
        return True
    else:
        return False

I have commented out the earlier code and replaced it with what I did to fix the issue.

I believe the earlier error messages are due to changes to the HTTPResponse class in Python's HTTP library.

I have checked that the changes show the expected behaviour of calling the aforementioned function.

If you'd like to, I can create a PR for this.

@DerAndereJohannes
Copy link
Collaborator

It looks like the issue occurred when removing the requests dependency in favour for the builtin urllib solution here.

Good catch! Please do make a PR.

DominiqueMakowski added a commit that referenced this issue Feb 11, 2025
[Fix] update data/database.py to use urllib methods. fixes #1061
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