Skip to content

Commit 067a372

Browse files
committed
fix: download space
1 parent e2b0181 commit 067a372

File tree

1 file changed

+37
-8
lines changed

1 file changed

+37
-8
lines changed

api/common/data_handlers/compounds/download.py

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from datetime import datetime
88
import json
99
import math
10+
import requests
1011

1112
VOLUME_SIZE_LIMIT = 2 * 1024 * 1024
1213

@@ -127,26 +128,54 @@ def write_desc(filename, obj, **kwargs):
127128
injected_content=f"space_id={space_id}, parcellation_id={parcellation_id}, region_id={region_id}, bbox={bbox}"
128129

129130
readme_txt = README.format(siibra_api_version=__version__,
130-
timestamp=str(datetime.now()),
131-
injected_content=injected_content)
131+
timestamp=str(datetime.now()),
132+
injected_content=injected_content)
132133
zipfile.writestr("README.md", readme_txt)
133134
zipfile.writestr("LICENCE.txt", LICENSE)
134135
try:
135136
space_filename = None
136137

137138
space: _space.Space = siibra.spaces[space_id]
138-
space_filename = f"{space.key}.nii.gz"
139+
space_filename = space.key
139140

140-
if bbox:
141+
template = space.get_template()
142+
143+
if "neuroglancer/precomputed" in template.formats:
144+
space_filename += ".nii.gz"
145+
if bbox is None:
146+
bbox = template.boundingbox
141147
bounding_box = space.get_bounding_box(*json.loads(bbox))
142148
value = VOLUME_SIZE_LIMIT
143149
for dim in bounding_box.maxpoint - bounding_box.minpoint:
144150
value /= dim
145151
cube_rooted = math.pow(value, 1/3)
146-
space_vol = space.get_template().fetch(voi=bounding_box, resolution_mm=1/cube_rooted)
147-
else:
148-
space_vol = space.get_template().fetch()
149-
zipfile.writestr(space_filename, gzip.compress(space_vol.to_bytes()))
152+
space_vol = template.fetch(voi=bounding_box,
153+
format="neuroglancer/precomputed",
154+
resolution_mm=1/cube_rooted)
155+
zipfile.writestr(space_filename, gzip.compress(space_vol.to_bytes()))
156+
if "gii-mesh" in template.formats:
157+
for idx, vol in enumerate(space.volumes):
158+
variant = vol.variant or f'unknown-variant-{idx}'
159+
root = f"{space_filename}/{variant}"
160+
if "gii-mesh" in vol.providers:
161+
url = vol.providers["gii-mesh"]
162+
def write_url(base_path: str, url: str):
163+
filename = "unknownfile"
164+
try:
165+
filepath = Path(url)
166+
filename = filepath.name
167+
resp = requests.get(url)
168+
resp.raise_for_status()
169+
170+
zipfile.writestr(f"{base_path}/{filename}", resp.content)
171+
except Exception as e:
172+
zipfile.writestr(f"{base_path}/{filename}.error.txt", str(e))
173+
if isinstance(url, str):
174+
write_url(root, url)
175+
if isinstance(url, dict):
176+
for key, _url in url.items():
177+
write_url(f"{root}/{key}", _url)
178+
150179
write_desc(f'{space_filename}.info.md', space)
151180
except Exception as e:
152181
zipfile.writestr(f"{space_filename or 'UNKNOWN_SPACE'}.error.txt", str(e))

0 commit comments

Comments
 (0)