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

Download NY Head model files automatically if missing #192

Merged
merged 8 commits into from Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/coveralls.yml
Expand Up @@ -35,7 +35,7 @@ jobs:
pip install neuron
- name: Test with pytest and coveralls
env:
COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
py.test -v --cov=lfpykit/tests/
coveralls
coveralls --service=github
23 changes: 16 additions & 7 deletions lfpykit/eegmegcalc.py
Expand Up @@ -1406,22 +1406,31 @@ def _load_head_model(self, nyhead_file):
nyhead_file = os.path.join(os.getcwd(), "sa_nyhead.mat")
self.head_file = os.path.abspath(nyhead_file)
if not os.path.isfile(self.head_file):

print(f"New York head model file not found: {self.head_file}")
print(f"Now downloading as {self.head_file} (710 MB). "
+ "This might take a while ...")
import urllib
from urllib.request import urlopen
import ssl
print("New York head-model file not found: %s" % self.head_file)
yn = input(f"Download as {self.head_file} (710 MB)? [y/n]: ")
if yn == 'y':
print("Now downloading. This might take a while ...")
try:
nyhead_url = 'https://www.parralab.org/nyhead/sa_nyhead.mat'
u = urlopen(nyhead_url,
context=ssl._create_unverified_context())
localFile = open(self.head_file, 'wb')
localFile.write(u.read())
localFile.close()
print("Download done!")
else:
print("Exiting program ...")
sys.exit()
except urllib.error.URLError:
print("URLError: Is the internet connection working?")
raise
except PermissionError:
print("PermissionError: Write access is needed "
+ "for downloading head model.")
raise
except Exception:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should capture URLError (as in URLError: <urlopen error [Errno -2] Name or service not known> or URLError: <urlopen error [Errno 8] nodename nor servname provided, or not known>), otherwise this looks good!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think URLError is a built-in error in python though, are you sure we should do this? I considered catching both PermissionError and URLErrors, but originally decided against it, since in the end I did not supply any more information than what was already effectively given in the error name.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

URLError is raised by urllib which is part of the standard distribution. I think it is fine to capture it as well as PermissionError then. Having consecutive excepts should be ok, according to the docs:

import sys
try:
    f = open('myfile.txt')
    s = f.readline()
    i = int(s.strip())
except OSError as err:
    print("OS error:", err)
except ValueError:
    print("Could not convert data to an integer.")
except Exception as err:
    print(f"Unexpected {err=}, {type(err)=}")
    raise

print("Unable to find or download New York head model file")
raise

self.head_data = h5py.File(self.head_file, 'r')["sa"]
self.cortex = np.array(self.head_data["cortex75K"]["vc"])
Expand Down
2 changes: 1 addition & 1 deletion lfpykit/version.py
@@ -1 +1 @@
version = "0.5"
version = "0.5.1"