Skip to content

Commit 2908fdf

Browse files
committed
merge all /api/* routes into single page /api
start adding comments to module globals
1 parent bf7f53a commit 2908fdf

File tree

7 files changed

+57
-36
lines changed

7 files changed

+57
-36
lines changed

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,4 @@ models/voronoi/*.zip
2929
site/static/figures
3030

3131
# generated docs
32-
site/src/routes/api/.*
3332
site/src/routes/api/*.md

matbench_discovery/__init__.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
import sys
55
from datetime import datetime
66

7-
ROOT = os.path.dirname(os.path.dirname(__file__))
7+
ROOT = os.path.dirname(os.path.dirname(__file__)) # repository root
8+
# whether a currently running slurm job is in debug mode
89
DEBUG = "slurm-submit" not in sys.argv and "SLURM_JOB_ID" not in os.environ
10+
# directory to store model checkpoints downloaded from wandb cloud storage
911
CHECKPOINT_DIR = f"{ROOT}/wandb/checkpoints"
12+
# wandb <entity>/<project name> to record new runs to
1013
WANDB_PATH = "materialsproject/matbench-discovery"
1114

1215
timestamp = f"{datetime.now():%Y-%m-%d@%H-%M-%S}"

matbench_discovery/data.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import os
44
import urllib.error
5-
from collections.abc import Generator, Sequence
5+
from collections.abc import Sequence
66
from glob import glob
77
from pathlib import Path
88
from typing import Any, Callable
@@ -17,7 +17,9 @@
1717
df_wbm = pd.read_csv(f"{ROOT}/data/wbm/2022-10-19-wbm-summary.csv")
1818
df_wbm.index = df_wbm.material_id
1919

20+
# repo URL to raw files on GitHub
2021
RAW_REPO_URL = "https://raw.githubusercontent.com/janosh/matbench-discovery"
22+
# directory to cache downloaded data files
2123
default_cache_dir = os.path.expanduser("~/.cache/matbench-discovery")
2224

2325
DATA_FILENAMES = {
@@ -31,10 +33,6 @@
3133
}
3234

3335

34-
def chunks(xs: Sequence[Any], n: int) -> Generator[Sequence[Any], None, None]:
35-
return (xs[i : i + n] for i in range(0, len(xs), n))
36-
37-
3836
def as_dict_handler(obj: Any) -> dict[str, Any] | None:
3937
"""Pass this to json.dump(default=) or as pandas.to_json(default_handler=) to
4038
convert Python classes with a as_dict() method to dictionaries on serialization.
@@ -50,7 +48,7 @@ def as_dict_handler(obj: Any) -> dict[str, Any] | None:
5048
def load_train_test(
5149
data_names: str | Sequence[str] = ("summary",),
5250
version: str = "1.0.0",
53-
cache_dir: str | Path | None = default_cache_dir,
51+
cache_dir: str | Path = default_cache_dir,
5452
hydrate: bool = False,
5553
**kwargs: Any,
5654
) -> pd.DataFrame:

site/src/app.css

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
:root {
2-
--night: #102030;
2+
--night: #061e25;
33
--blue: cornflowerblue;
44
--text-color: rgb(208, 208, 208);
55

site/src/routes/+layout.svelte

+5-5
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919
const routes = Object.keys(import.meta.glob(`./*/+page.{svx,svelte,md}`)).map(
2020
(filename) => `/` + filename.split(`/`)[1]
2121
)
22+
23+
$: headingSelector = `main > :is(${
24+
$page.url.pathname === `/api` ? `h1, ` : ``
25+
}h2, h3, h4):not(.toc-exclude)`
2226
</script>
2327

24-
<Toc
25-
headingSelector="main > :is(h2, h3, h4):not(.toc-exclude)"
26-
breakpoint={1250}
27-
warnOnEmpty={false}
28-
/>
28+
<Toc {headingSelector} breakpoint={1250} warnOnEmpty={false} />
2929

3030
{#if $page.url.pathname !== `/`}
3131
<a href="/" aria-label="Back to index page">&laquo; home</a>

site/src/routes/api/+page.svelte

+28-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,28 @@
1-
<!-- needed for /api page to show up -->
1+
<script lang="ts">
2+
import { onMount } from 'svelte'
3+
4+
onMount(() => {
5+
for (const img of [
6+
...document.querySelectorAll(
7+
`img[src='https://img.shields.io/badge/-source-cccccc?style=flat-square']`
8+
),
9+
] as HTMLAnchorElement[]) {
10+
img.src = `https://img.shields.io/badge/source-blue?style=flat`
11+
}
12+
})
13+
</script>
14+
15+
{#each Object.values(import.meta.glob(`./*.md`, { eager: true })) as file}
16+
<svelte:component this={file?.default} />
17+
{/each}
18+
19+
<style>
20+
:global(hr) {
21+
border: none;
22+
margin: 3em;
23+
}
24+
:global(code) {
25+
line-height: 1em;
26+
border-radius: 4pt;
27+
}
28+
</style>

tests/test_data.py

+15-21
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
import urllib.request
55
from pathlib import Path
6+
from random import random
67
from tempfile import TemporaryDirectory
78
from typing import Any
89
from unittest.mock import patch
@@ -18,7 +19,6 @@
1819
PRED_FILENAMES,
1920
RAW_REPO_URL,
2021
as_dict_handler,
21-
chunks,
2222
df_wbm,
2323
glob_to_df,
2424
load_df_wbm_with_preds,
@@ -38,29 +38,34 @@
3838

3939

4040
@pytest.mark.parametrize(
41-
"data_names, cache_dir, hydrate",
41+
"data_names, hydrate",
4242
[
43-
(["wbm-summary"], None, True),
44-
(["wbm-initial-structures"], TemporaryDirectory().name, True),
45-
(["wbm-computed-structure-entries"], None, False),
46-
(["wbm-summary", "wbm-initial-structures"], TemporaryDirectory().name, True),
47-
(["mp-elemental-ref-energies"], None, True),
48-
(["mp-energies"], None, True),
43+
(["wbm-summary"], True),
44+
(["wbm-initial-structures"], True),
45+
(["wbm-computed-structure-entries"], False),
46+
(["wbm-summary", "wbm-initial-structures"], True),
47+
(["mp-elemental-ref-energies"], True),
48+
(["mp-energies"], True),
4949
],
5050
)
5151
def test_load_train_test(
5252
data_names: list[str],
53-
cache_dir: str | None,
5453
hydrate: bool,
5554
dummy_df_with_structures: pd.DataFrame,
5655
capsys: CaptureFixture[str],
56+
tmp_path: Path,
5757
) -> None:
5858
# intercept HTTP requests to GitHub raw user content and return dummy df instead
5959
with patch("matbench_discovery.data.pd.read_csv") as read_csv, patch(
6060
"matbench_discovery.data.pd.read_json"
6161
) as read_json:
6262
read_csv.return_value = read_json.return_value = dummy_df_with_structures
63-
out = load_train_test(data_names, cache_dir=cache_dir, hydrate=hydrate)
63+
out = load_train_test(
64+
data_names,
65+
hydrate=hydrate,
66+
# test both str and Path cache_dir
67+
cache_dir=TemporaryDirectory().name if random() < 0.5 else tmp_path,
68+
)
6469

6570
stdout, stderr = capsys.readouterr()
6671

@@ -152,17 +157,6 @@ def test_load_train_test_no_mock(
152157
)
153158

154159

155-
def test_chunks() -> None:
156-
assert list(chunks([], 1)) == []
157-
assert list(chunks([1], 1)) == [[1]]
158-
assert list(chunks([1, 2], 1)) == [[1], [2]]
159-
assert list(chunks([1, 2, 3], 1)) == [[1], [2], [3]]
160-
assert list(chunks([1, 2, 3], 2)) == [[1, 2], [3]]
161-
assert list(chunks(range(1, 4), 2)) == [range(1, 3), range(3, 4)]
162-
assert list(chunks(range(1, 5), 2)) == [range(1, 3), range(3, 5)]
163-
assert list(chunks(range(1, 5), 3)) == [range(1, 4), range(4, 5)]
164-
165-
166160
def test_as_dict_handler() -> None:
167161
class C:
168162
def as_dict(self) -> dict[str, Any]:

0 commit comments

Comments
 (0)