Skip to content

Commit 7e2ee23

Browse files
committed
getmodels: download from GitHub
1 parent b789d6f commit 7e2ee23

File tree

5 files changed

+32
-80
lines changed

5 files changed

+32
-80
lines changed

modeldb/config.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,9 @@
22

33
import os
44

5-
# MDB_NEURON_MODELS_URL = "https://senselab.med.yale.edu/_site/webapi/object.json/?cl=19&oid=1882"
65
MDB_NEURON_MODELS_URL = (
76
"http://modeldb.science/api/v1/models?modeling_application=NEURON"
87
)
9-
MDB_MODEL_DOWNLOAD_URL = (
10-
"https://senselab.med.yale.edu/_site/webapi/object.json/{model_id}"
11-
)
128

139
ROOT_DIR = os.path.abspath(__file__ + "/../../")
1410

modeldb/data.py

Lines changed: 0 additions & 14 deletions
This file was deleted.

modeldb/modeldb.py

Lines changed: 27 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -7,70 +7,36 @@
77
import time
88
from .progressbar import ProgressBar
99
import yaml
10-
from .data import Model
1110
from .config import *
1211
import traceback
1312
from pprint import pformat
1413

1514
def download_model(arg_tuple):
1615
model_id, model_run_info = arg_tuple
1716
try:
18-
model_json = requests.get(MDB_MODEL_DOWNLOAD_URL.format(model_id=model_id)).json()
19-
model = Model(
20-
*(
21-
model_json[key]
22-
for key in ("object_id", "object_name", "object_created", "object_ver_date")
23-
)
24-
)
25-
url = None
26-
for att in model_json["object_attribute_values"]:
27-
if att["attribute_id"] == 23:
28-
url = att["value"]
29-
break
30-
# print(model.id)
3117
model_zip_uri = os.path.join(
32-
MODELS_ZIP_DIR, "{model_id}.zip".format(model_id=model.id)
18+
MODELS_ZIP_DIR, "{model_id}.zip".format(model_id=model_id))
19+
20+
suffix = model_run_info["github"] if "github" in model_run_info else "master"
21+
github_url = "https://github.com/ModelDBRepository/{model_id}/archive/refs/heads/{suffix}.zip".format(
22+
model_id=model_id, suffix=suffix
3323
)
34-
with open(model_zip_uri, "wb+") as zipfile:
35-
zipfile.write(base64.standard_b64decode(url["file_content"]))
36-
37-
if "github" in model_run_info:
38-
# This means we should try to replace the version of the model that
39-
# we downloaded from the ModelDB API just above with a version from
40-
# GitHub
41-
github = model_run_info["github"]
42-
if github == "default":
43-
suffix = ""
44-
elif github.startswith("pull/"):
45-
pr_number = int(github[5:])
46-
suffix = "/pull/{}/head".format(pr_number)
47-
else:
48-
raise Exception("Invalid value for github key: {}".format(github))
49-
github_url = "https://api.github.com/repos/ModelDBRepository/{model_id}/zipball{suffix}".format(
50-
model_id=model_id, suffix=suffix
51-
)
52-
# Replace the local file `model_zip_uri` with the zip file we
53-
# downloaded from `github_url`
54-
num_attempts = 3
55-
status_codes = []
56-
for _ in range(num_attempts):
57-
github_response = requests.get(github_url)
58-
status_codes.append(github_response.status_code)
59-
if github_response.status_code == requests.codes.ok:
60-
break
61-
time.sleep(5)
62-
else:
63-
raise Exception(
64-
"Failed to download {} with status codes {}".format(
65-
github_url, status_codes
66-
)
67-
)
68-
with open(model_zip_uri, "wb+") as zipfile:
69-
zipfile.write(github_response.content)
24+
25+
# download github_url to model_zip_uri
26+
logging.info("Downloading model {} from {}".format(model_id, github_url))
27+
response = requests.get(github_url, stream=True)
28+
if response.status_code != 200:
29+
raise Exception("Failed to download model: {}".format(response.text))
30+
with open(model_zip_uri, "wb") as f:
31+
for chunk in response.iter_content(chunk_size=1024):
32+
if chunk:
33+
f.write(chunk)
34+
f.flush()
35+
logging.info("Downloaded model {} to {}".format(model_id, model_zip_uri))
7036
except Exception as e: # noqa
71-
model = e
37+
github_url = e
7238

73-
return model_id, model
39+
return model_id, github_url
7440

7541

7642
class ModelDB(object):
@@ -105,11 +71,15 @@ def _download_models(self, model_list=None):
10571
[(model_id, self._run_instr.get(model_id, {})) for model_id in models],
10672
)
10773
download_err = {}
108-
for model_id, model in ProgressBar.iter(processed_models, len(models)):
109-
if not isinstance(model, Exception):
110-
self._metadata[model_id] = model
74+
for model_id, model_url in ProgressBar.iter(processed_models, len(models)):
75+
76+
if not isinstance(model_url, Exception):
77+
model_meta = {}
78+
model_meta["id"] = model_id
79+
model_meta["url"] = model_url
80+
self._metadata[model_id] = model_meta
11181
else:
112-
download_err[model_id] = model
82+
download_err[model_id] = model_url
11383

11484
if download_err:
11585
logging.error("Error downloading models:")

modeldb/modelrun.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import platform
55
import sys
66
from .progressbar import ProgressBar
7-
from .data import Model
87
from . import modeldb
98
from .config import *
109
from .hocscripts import *
@@ -35,7 +34,7 @@ def is_dir_non_empty(directory):
3534
class ModelRun(dict):
3635
def __init__(self, model, working_dir, clean=False, norun=False, inplace=False):
3736
super().__init__()
38-
self._model = model
37+
super().update(model)
3938
self._working_dir = os.path.abspath(working_dir)
4039
self._logs = []
4140
self._gout = []
@@ -88,7 +87,7 @@ def _fetch_model(self):
8887
run_time = property(lambda self: self._run_time)
8988
run_times = property(lambda self: self._run_times)
9089

91-
id = property(lambda self: self._model.id)
90+
id = property(lambda self: self["id"])
9291

9392

9493
def curate_log_string(model, logstr):

modeldb/report.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,9 @@ def _speedup(a, b):
9898
runtime_dict[k][runkey] = _speedup(data_a[k]["run_times"][runkey], data_b[k]["run_times"][runkey])
9999

100100
# compare gout
101-
gout_a_file = os.path.join(data_a[k]["run_info"]["start_dir"], "gout")
102-
gout_b_file = os.path.join(data_b[k]["run_info"]["start_dir"], "gout")
101+
gout_a_file = os.path.join(start_dir_a, "gout")
102+
gout_b_file = os.path.join(start_dir_b, "gout")
103+
103104
# gout may be missing in one of the paths. `diff -N` treats non-existent files as empty.
104105
if os.path.isfile(gout_a_file) or os.path.isfile(gout_b_file):
105106
diff_out = subprocess.getoutput(

0 commit comments

Comments
 (0)