Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions app/nullius/catalogue/sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

A benchmark that runs with Harbor and the Nullius standard reads its data two
ways: Harbor from its registry or hub, the standard from the upstream it pins
(a repository at a commit, or a file at a revision) or from the Hugging Face
dataset its driver downloads (``data-source.json``: ``repository`` and
``revision``). Each row names the source, the pin and who fetches it and the files a definition
(a repository at a commit, a file at a revision, or a file at an address of
its own) or from the Hugging Face dataset or GitHub repository its driver
fetches (``data-source.json``: ``repository`` and ``revision``). Each row names the source, the pin and who fetches it and the files a definition
checks by their sha256. Nothing here reads the network: it reads the
catalogue entry and the definitions beside it.
"""
Expand All @@ -18,6 +18,7 @@
_GITHUB = re.compile(r"^https://github\.com/([^/]+/[^/]+?)(?:\.git)?/?$")
_HF = re.compile(r"^https://huggingface\.co/datasets/([^/]+/[^/]+)/resolve/([^/]+)/")
_HF_REPO = re.compile(r"^[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+$")
_URL = re.compile(r"^https?://([^/\s]+)/\S*$")
_REVISION = re.compile(r"^[^/\s]+$")


Expand Down Expand Up @@ -52,7 +53,8 @@ def _files(defined: List[dict]) -> List[dict]:


def _data_source(here, key: str = "") -> Optional[tuple]:
"""The Hugging Face dataset a driver downloads itself, ``data-source.json``
"""The Hugging Face dataset a driver downloads itself, or the GitHub
repository it fetches its data from (a URL), ``data-source.json``
beside its definition: (repository, revision), or None. A dataset's own
entry there (``datasets.<key>``) may name its repository and revision,
for a driver that reads several."""
Expand All @@ -66,19 +68,28 @@ def _data_source(here, key: str = "") -> Optional[tuple]:
repo, rev = own.get("repository", src.get("repository")), own.get("revision", src.get("revision"))
except (OSError, ValueError, TypeError, AttributeError):
return None
ok = isinstance(repo, str) and _HF_REPO.match(repo) and isinstance(rev, str) and _REVISION.match(rev)
ok = isinstance(repo, str) and (_HF_REPO.match(repo) or _GITHUB.match(repo)) and isinstance(rev, str) and _REVISION.match(rev)
return (repo, rev) if ok else None


def _own(upstream: dict, defined: List[dict], who: str, source: Optional[tuple] = None) -> dict:
"""A row for a harness that reads the upstream itself: from Hugging Face when
its driver names a dataset there (``source``) or every file it names is
there, else from the repository it clones."""
"""A row for a harness that reads the upstream itself: from the GitHub
repository its driver fetches the data from (``source``); from Hugging Face
when its driver names a dataset there or every file it names is there; from
the one host of the files it names by a URL elsewhere; else from the
repository it clones."""
hf = [_HF.match(d.get("url") or "") for d in defined]
if source or (defined and all(hf)):
urls = [_URL.match(d.get("url") or "") for d in defined]
if source and _GITHUB.match(source[0]):
row = dict(_repo({"repository": source[0], "tested": source[1]}),
by=f"{who} fetches the repository at this commit on the first run and keeps it")
elif source or (defined and all(hf)):
name, rev = source or (hf[0].group(1), hf[0].group(2))
row = {"from": "Hugging Face", "name": name, "href": f"https://huggingface.co/datasets/{name}/tree/{rev}",
"pin": rev[:7], "by": f"{who} downloads it on the first run and keeps it"}
elif defined and all(urls) and len({u.group(1) for u in urls}) == 1:
row = {"from": "Download", "name": urls[0].group(1), "href": urls[0].group(0), "pin": "",
"by": f"{who} downloads it on the first run and keeps it"}
else:
row = dict(_repo(upstream), by=f"{who} fetches the repository at this commit on the first run and keeps it")
row["files"] = _files(defined)
Expand Down
23 changes: 23 additions & 0 deletions app/tests/test_catalogue_sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,29 @@ def test_a_dataset_the_driver_downloads_from_hugging_face_names_it_not_the_upstr
self.assertEqual((row["from"], row["name"], row["pin"]), ("Hugging Face", "org/data", "0123456"))
self.assertEqual(row["href"], f"https://huggingface.co/datasets/org/data/tree/{COMMIT}")

def test_a_dataset_the_driver_fetches_from_another_github_repository_names_it_not_the_upstream(self):
d = catalog.Dataset(key="main", title="Main")
with tempfile.TemporaryDirectory() as here:
std = {"_here": here, "upstream": {"repository": "https://github.com/org/harness", "tested": COMMIT},
"datasets": [{"id": "main", "units": 3}]}
with open(os.path.join(here, "data-source.json"), "w") as f:
json.dump({"repository": "https://github.com/org/tasks", "revision": "f" * 40}, f)
(row,) = sources.of(d, ["standard"], std)
self.assertEqual((row["from"], row["name"], row["pin"]), ("GitHub", "org/tasks", "fffffff"))
self.assertEqual(row["href"], "https://github.com/org/tasks/tree/" + "f" * 40)

def test_a_file_at_an_address_outside_hugging_face_names_that_address_not_the_upstream(self):
d = catalog.Dataset(key="main", title="Main")
url = "https://files.example.org/evals/test_set.csv"
std = {"upstream": {"repository": "https://github.com/org/evals", "tested": COMMIT},
"datasets": [{"id": "main", "url": url, "sha256": "c" * 64, "bytes": 2_000_000}]}
(row,) = sources.of(d, ["standard"], std)
self.assertEqual((row["from"], row["name"], row["href"], row["pin"]), ("Download", "files.example.org", url, ""))
self.assertEqual(row["files"], [{"name": "test_set.csv", "size": "2 MB", "checked": True}])
# files on two hosts have no one address to name: the upstream's row, as before
std["datasets"].append({"id": "main", "url": "https://other.example.net/more.csv", "sha256": "d" * 64})
self.assertEqual(sources.of(d, ["standard"], std)[0]["from"], "GitHub")

def test_a_driver_that_downloads_from_two_repositories_names_each_datasets_own(self):
lite, full = catalog.Dataset(key="qa-lite", title="Lite"), catalog.Dataset(key="qa-full", title="Full")
other = "f" * 40
Expand Down