-
Notifications
You must be signed in to change notification settings - Fork 23
/
bg_atlas.py
119 lines (86 loc) · 3.17 KB
/
bg_atlas.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
from pathlib import Path
import urllib
import shutil
import tarfile
import warnings
from brainatlas_api import DEFAULT_PATH
from brainatlas_api.core import Atlas
from brainatlas_api.utils import check_internet_connection
COMPRESSED_FILENAME = "atlas.tar.gz"
class BrainGlobeAtlas(Atlas):
"""
Add download functionalities to Atlas class.
Parameters
----------
brainglobe_path : str or Path object
default folder for brainglobe downloads
interm_dir_path : str or Path object
folder to download the compressed file for extraction
"""
atlas_name = None
version = None
_remote_url_base = (
"https://gin.g-node.org/brainglobe/atlases/raw/master/{}.tar.gz"
)
def __init__(self, brainglobe_path=None, interm_dir_path=None):
if brainglobe_path is None:
brainglobe_path = DEFAULT_PATH
self.brainglobe_path = Path(brainglobe_path)
self.interm_dir_path = (
Path(interm_dir_path)
if interm_dir_path is not None
else self.brainglobe_path
)
self.atlas_name = self.atlas_name + f"_v{self.version}"
try:
super().__init__(self.brainglobe_path / self.atlas_name)
except FileNotFoundError:
warnings.warn(f"{self.atlas_name} not found. Downloading...")
self.download_extract_file()
super().__init__(self.brainglobe_path / self.atlas_name)
@property
def remote_url(self):
""" Format complete url for download.
"""
return self._remote_url_base.format(self.atlas_name)
def download_extract_file(self):
""" Download and extract atlas from remote url.
"""
check_internet_connection()
# Get path to folder where data will be saved
destination_path = self.interm_dir_path / COMPRESSED_FILENAME
# Try to download atlas data
try:
with urllib.request.urlopen(self.remote_url) as response:
with open(destination_path, "wb") as outfile:
shutil.copyfileobj(response, outfile)
except urllib.error.HTTPError as e:
raise FileNotFoundError(
f"Could not download data from {self.remote_url}. error code: {e.code}"
)
except urllib.error.URLError as e:
raise FileNotFoundError(
f"Could not download data from {self.remote_url}: {e.args}"
)
# Uncompress
tar = tarfile.open(destination_path)
# Ensure brainglobe path exists and extract there:
self.brainglobe_path.mkdir(exist_ok=True)
tar.extractall(path=self.brainglobe_path)
tar.close()
destination_path.unlink()
class TestAtlas(BrainGlobeAtlas):
atlas_name = "test_allen_100um"
version = "0.1"
class FishAtlas(BrainGlobeAtlas):
atlas_name = "fishatlas"
version = "0.1"
class RatAtlas(BrainGlobeAtlas):
atlas_name = "ratatlas"
version = "0.1"
class AllenBrain25Um(BrainGlobeAtlas):
atlas_name = "allenbrain25um"
version = "0.2"
class AllenHumanBrain500Um(BrainGlobeAtlas):
atlas_name = "allen_human_500um"
version = "0.1"