|
7 | 7 | from datetime import datetime
|
8 | 8 | import json
|
9 | 9 | import math
|
| 10 | +import requests |
10 | 11 |
|
11 | 12 | VOLUME_SIZE_LIMIT = 2 * 1024 * 1024
|
12 | 13 |
|
@@ -127,26 +128,54 @@ def write_desc(filename, obj, **kwargs):
|
127 | 128 | injected_content=f"space_id={space_id}, parcellation_id={parcellation_id}, region_id={region_id}, bbox={bbox}"
|
128 | 129 |
|
129 | 130 | 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) |
132 | 133 | zipfile.writestr("README.md", readme_txt)
|
133 | 134 | zipfile.writestr("LICENCE.txt", LICENSE)
|
134 | 135 | try:
|
135 | 136 | space_filename = None
|
136 | 137 |
|
137 | 138 | space: _space.Space = siibra.spaces[space_id]
|
138 |
| - space_filename = f"{space.key}.nii.gz" |
| 139 | + space_filename = space.key |
139 | 140 |
|
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 |
141 | 147 | bounding_box = space.get_bounding_box(*json.loads(bbox))
|
142 | 148 | value = VOLUME_SIZE_LIMIT
|
143 | 149 | for dim in bounding_box.maxpoint - bounding_box.minpoint:
|
144 | 150 | value /= dim
|
145 | 151 | 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 | + |
150 | 179 | write_desc(f'{space_filename}.info.md', space)
|
151 | 180 | except Exception as e:
|
152 | 181 | zipfile.writestr(f"{space_filename or 'UNKNOWN_SPACE'}.error.txt", str(e))
|
|
0 commit comments