diff --git a/.github/workflows/typescript-tests.yml b/.github/workflows/typescript-tests.yml index b536105a..e9c25829 100644 --- a/.github/workflows/typescript-tests.yml +++ b/.github/workflows/typescript-tests.yml @@ -62,3 +62,35 @@ jobs: run: | npm install npm run test + + test-tslib: + name: Run tests + runs-on: ubuntu-latest + + steps: + - name: Check out code + uses: actions/checkout@master + + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: '3.11' + architecture: x64 + + - name: Generate test asset + working-directory: tslib/storage/test/ + run: | + python -m pip install --progress-bar off --upgrade pip setuptools + pip install --progress-bar off optuna + python generate_assets.py + + - name: Setup Node + uses: actions/setup-node@v2 + with: + node-version: '16' + - name: Build test + run: make tslib + - name: Run tslib test + working-directory: tslib/storage + run: | + npm run test diff --git a/.gitignore b/.gitignore index 7f5f6761..98571a6a 100644 --- a/.gitignore +++ b/.gitignore @@ -32,8 +32,7 @@ rustlib/pkg/ .coverage .coverage.* coverage.xml -tslib/storage/test/asset/*.log -tslib/storage/test/asset/*.db +tslib/storage/test/asset/ # Others .envrc diff --git a/biome.json b/biome.json index fc74ae38..fa75000f 100644 --- a/biome.json +++ b/biome.json @@ -9,7 +9,8 @@ "vscode/src/**/*.ts", "vscode/src/**/*.tsx", "tslib/**/*.ts", - "tslib/**/*.tsx" + "tslib/**/*.tsx", + "tslib/**/*.mjs" ], "ignore": [ "optuna_dashboard/ts/components/PlotlyColorTemplates.ts", diff --git a/package.json b/package.json index 909dce6c..9e243cb4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "scripts": { - "fmt": "biome format --write . && biome check standalone_app vscode --apply", + "fmt": "biome format --write . && biome check standalone_app vscode tslib --apply", "lint": "npm run lint:eslint && npm run lint:biome", "lint:eslint": "eslint . --ext .ts,.tsx --max-warnings 0", "lint:biome": "biome format . && biome ci standalone_app vscode tslib" diff --git a/setup.cfg b/setup.cfg index 1eabbdce..a3fdddd0 100644 --- a/setup.cfg +++ b/setup.cfg @@ -4,4 +4,4 @@ ignore = W503 max-line-length = 99 statistics = True -exclude = venv,build +exclude = venv,build,node_modules diff --git a/tslib/storage/test/asset/generate_test_asset.py b/tslib/storage/test/asset/generate_test_asset.py deleted file mode 100644 index e17d92fe..00000000 --- a/tslib/storage/test/asset/generate_test_asset.py +++ /dev/null @@ -1,17 +0,0 @@ -import os.path -import optuna - - -BASE_DIR = os.path.dirname(os.path.abspath(__file__)) - - -def objective(trial): - x = trial.suggest_uniform('x', -10, 10) - return (x - 2) ** 2 - - -if __name__ == '__main__': - journal_backend = optuna.storages.JournalFileStorage(os.path.join(BASE_DIR, "journal.log")) - storage = optuna.storages.JournalStorage(journal_backend) - study = optuna.create_study(storage=storage) - study.optimize(objective, n_trials=100) diff --git a/tslib/storage/test/generate_assets.py b/tslib/storage/test/generate_assets.py new file mode 100644 index 00000000..0674a711 --- /dev/null +++ b/tslib/storage/test/generate_assets.py @@ -0,0 +1,55 @@ +import os.path +import shutil + +import optuna +from optuna.storages import BaseStorage, JournalStorage, JournalFileStorage, RDBStorage + + +optuna.logging.set_verbosity(optuna.logging.WARNING) +BASE_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "asset") + + +def remove_assets() -> None: + if os.path.exists(BASE_DIR): + shutil.rmtree(BASE_DIR) + os.mkdir(BASE_DIR) + + +def create_optuna_storage(storage: BaseStorage) -> None: + # Single-objective study + study = optuna.create_study( + study_name="single-objective", storage=storage, sampler=optuna.samplers.RandomSampler() + ) + print(f"Generating {study.study_name} for {type(storage).__name__}...") + + def objective_single(trial: optuna.Trial) -> float: + x1 = trial.suggest_float("x1", 0, 10) + x2 = trial.suggest_float("x2", 0, 10) + trial.suggest_categorical("x3", ["foo", "bar"]) + return (x1 - 2) ** 2 + (x2 - 5) ** 2 + + study.optimize(objective_single, n_trials=50) + + # Single-objective study with dynamic search space + study = optuna.create_study( + study_name="single-objective-dynamic", storage=storage, direction="maximize" + ) + print(f"Generating {study.study_name} for {type(storage).__name__}...") + + def objective_single_dynamic(trial: optuna.Trial) -> float: + category = trial.suggest_categorical("category", ["foo", "bar"]) + if category == "foo": + return (trial.suggest_float("x1", 0, 10) - 2) ** 2 + else: + return -((trial.suggest_float("x2", -10, 0) + 5) ** 2) + + study.optimize(objective_single_dynamic, n_trials=50) + + +if __name__ == "__main__": + remove_assets() + for storage in [ + JournalStorage(JournalFileStorage(os.path.join(BASE_DIR, "journal.log"))), + RDBStorage("sqlite:///" + os.path.join(BASE_DIR, "db.sqlite3")), + ]: + create_optuna_storage(storage) diff --git a/tslib/storage/test/journal.test.mjs b/tslib/storage/test/journal.test.mjs index 9f140b9f..1e8bd274 100644 --- a/tslib/storage/test/journal.test.mjs +++ b/tslib/storage/test/journal.test.mjs @@ -1,15 +1,19 @@ -import test from "node:test"; -import assert from "node:assert"; -import path from "node:path"; -import { openAsBlob } from "node:fs"; +import assert from "node:assert" +import { openAsBlob } from "node:fs" +import path from "node:path" +import test from "node:test" -import * as mut from "../pkg/journal.js"; +import * as mut from "../pkg/journal.js" + +const n_studies = 2 test("Test Journal File Storage", async () => { - const blob = await openAsBlob(path.resolve(".", "test", "asset", "journal.log")); - const buf = await blob.arrayBuffer(); - const storage = new mut.JournalFileStorage(buf); - const studies = await storage.getStudies(); + const blob = await openAsBlob( + path.resolve(".", "test", "asset", "journal.log") + ) + const buf = await blob.arrayBuffer() + const storage = new mut.JournalFileStorage(buf) + const studies = await storage.getStudies() - assert.strictEqual(studies.length, 1); -}); + assert.strictEqual(studies.length, n_studies) +})