Skip to content

Commit

Permalink
Merge pull request #16 from n-takumasa/dev
Browse files Browse the repository at this point in the history
dev
  • Loading branch information
n-takumasa authored Jan 6, 2025
2 parents 9295876 + ee41758 commit 2b31c04
Show file tree
Hide file tree
Showing 8 changed files with 212 additions and 86 deletions.
11 changes: 4 additions & 7 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,13 @@ jobs:

steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version-file: "pyproject.toml"

- name: Update pip
run: python -m pip install --upgrade pip
persist-credentials: false

- name: Install uv
uses: astral-sh/setup-uv@v3
uses: astral-sh/setup-uv@v5
with:
enable-cache: false

- name: Build a binary wheel and a source tarball
run: uv build
Expand Down
62 changes: 29 additions & 33 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,63 +1,59 @@
name: test

on:
workflow_dispatch:
push:
branches: [main, master]
pull_request:
branches: [main, master]

concurrency:
group: test-${{ github.head_ref }}
group: test-${{ github.ref }}
cancel-in-progress: true

env:
PYTHONUNBUFFERED: "1"
FORCE_COLOR: "1"

jobs:
check:
name: Static analysis
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version-file: "pyproject.toml"
- name: Install uv
uses: astral-sh/setup-uv@v3
- run: uv venv
- name: Install tox
run: uv pip install tox tox-uv
- name: Run static analysis
run: uv run tox run -e lint
- name: Run mypy
run: uv run tox run -e type

run:
needs: check
name: Python ${{ matrix.python-version }} on ${{ startsWith(matrix.os, 'macos-') && 'macOS' || startsWith(matrix.os, 'windows-') && 'Windows' || 'Linux' }}
name: test with ${{ matrix.env }} on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest]
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
env: ["3.13", "3.12", "3.11", "3.10", "3.9", "3.8"]
include:
- os: windows-latest
python-version: 3.12
env: "3.13"
- os: macos-latest
python-version: 3.12
env: "3.13"

steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install uv
uses: astral-sh/setup-uv@v3
- run: uv venv
persist-credentials: false
- uses: astral-sh/setup-uv@v5
- name: Install tox
run: uv pip install tox tox-uv tox-gh-actions
- name: Run tests
run: uv run tox
run: >-
uv tool install
--python-preference only-managed
--python 3.13
tox
--with tox-uv
--with tox-gh
- name: Install Python
if: matrix.env != '3.13'
run: >-
uv python install
--python-preference only-managed
${{ matrix.env }}
- name: Setup test suite
run: tox run -vv --notest --skip-missing-interpreters false
env:
TOX_GH_MAJOR_MINOR: ${{ matrix.env }}
- name: Run test suite
run: tox run --skip-pkg-install
env:
TOX_GH_MAJOR_MINOR: ${{ matrix.env }}
38 changes: 38 additions & 0 deletions .github/workflows/zizmor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: GitHub Actions Security Analysis with zizmor 🌈

on:
push:
branches: ["main"]
pull_request:
branches: ["**"]
schedule:
- cron: "0 23 * * 2"

jobs:
zizmor:
name: zizmor latest via PyPI
runs-on: ubuntu-latest
permissions:
security-events: write
# required for workflows in private repositories
contents: read
actions: read
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
persist-credentials: false

- name: Install the latest version of uv
uses: astral-sh/setup-uv@v5

- name: Run zizmor 🌈
run: uvx zizmor --format sarif . > results.sarif
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Upload SARIF file
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: results.sarif
category: zizmor
6 changes: 2 additions & 4 deletions example/water.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
# ]
# ///

import os

import matplotlib.pyplot as plt
import polars as pl
from matplotlib.axes import Axes
Expand Down Expand Up @@ -39,5 +37,5 @@

fig.suptitle(f"{t.name} {t.formula}")
fig.tight_layout()
if not os.getenv("CI"):
plt.show()

plt.show()
25 changes: 20 additions & 5 deletions tests/test_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,18 @@
import subprocess
import sys
from pathlib import Path
from tempfile import NamedTemporaryFile

import pytest

_patch = """\
import matplotlib.pyplot as plt
def _show(*args, **kwargs):
...
plt.show = _show
"""


def get_example_scripts():
return [
Expand All @@ -19,9 +28,15 @@ def get_example_scripts():
)
def test_example(fp: Path):
env = os.environ.copy()
env["CI"] = "true"
ret = subprocess.run(
[sys.executable, fp.absolute()],
env=env,
)
env["MPLBACKEND"] = "agg"
with NamedTemporaryFile("w", suffix=".py", delete=False) as tmp:
# python >= 3.12
# with TemporaryFile("w", suffix=".py", delete_on_close=False) as tmp:
tmp.write(_patch + fp.read_text())
tmp.close()
ret = subprocess.run(
[sys.executable, tmp.name],
env=env,
)
os.unlink(tmp.name)
assert ret.returncode == 0
79 changes: 78 additions & 1 deletion tests/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ def get_index() -> list[str]:


@pytest.mark.parametrize("index", get_index())
def test_columns(index: str):
def test_table(index: str):
table = janaf.Table(index)

assert table.df.columns == [
"T(K)",
"Cp",
Expand All @@ -27,6 +28,7 @@ def test_columns(index: str):
"log Kf",
"Note",
]

assert table.df.dtypes == [
pl.Float64,
pl.Float64,
Expand All @@ -38,3 +40,78 @@ def test_columns(index: str):
pl.Float64,
pl.String,
]

# S(Tr) = -[G(Tr) - H(Tr)] / Tr
if index not in {"S-016", "S-017", "S-018", "S-019", "S-020"}:
assert (
table.df.filter(pl.col("T(K)") == 298.15)
.select(pl.col("S") - pl.col("-[G-H(Tr)]/T"))
.item()
== 0
)

# At constant pressure, enthalpy is a monotonically increasing function of temperature
# TODO: https://github.com/n-takumasa/py-janaf/issues/13
if index not in {"C-066", "Cl-121"}:
assert table.df.select((pl.col("H-H(Tr)").diff() >= 0).all()).item()

R = 8.31446261815324
atol = 1e-3
rtol = 1e-3
# ΔG(T) = ΔfG°(T) + R T ln(Kf) = 0
# ΔfG°(T) = - R T ln(Kf)
# log10(Kf) = - ΔfG°(T) / (ln(10) * R * T)
# TODO: https://github.com/n-takumasa/py-janaf/issues/13
if index not in {"B-123", "B-133"}:
assert (
table.df.select(
a="log Kf",
b=(
(-1e3 * pl.col("delta-f G"))
/ (pl.lit(10).log() * R * pl.col("T(K)"))
).fill_nan(0),
)
.with_columns(
eq=(pl.col("a") == pl.col("b")),
er_a=(pl.col("a") - pl.col("b")).abs(),
er_r=(pl.col("a") - pl.col("b")).abs() / pl.col("b").abs(),
)
.with_columns(
is_close=(pl.col("er_a") <= (atol + rtol * pl.col("b").abs())),
)
.select(
q=(pl.col("eq") | pl.col("is_close")).all(),
)
.item()
)

# G-H-S relation
# G°(T) = H°(T) - T S°(T)
# S°(T) = [H°(T) - H°(Tr)]/T - [G°(T) - H°(Tr)]/T
# TODO: https://github.com/n-takumasa/py-janaf/issues/13
# fmt: off
if index not in {
"B-065", "B-116", "B-133", "Be-044", "Be-047", "C-002", "C-008", "C-010",
"H-014", "H-016", "Hf-001", "Hf-002", "Hf-004", "Hf-005", "Li-014", "Li-016",
"Mg-008", "Mg-010", "S-016", "S-017", "S-018", "S-019", "S-020",
}:
# fmt: on
assert (
table.df.select(
"T(K)",
a="S",
b=(pl.col("H-H(Tr)") * 1e3 / pl.col("T(K)") + pl.col("-[G-H(Tr)]/T")),
)
.with_columns(
eq=(pl.col("a") == pl.col("b")),
er_a=(pl.col("a") - pl.col("b")).abs(),
er_r=(pl.col("a") - pl.col("b")).abs() / pl.col("b").abs(),
)
.with_columns(
is_close=(pl.col("er_a") <= (atol + rtol * pl.col("b").abs())),
)
.select(
q=(pl.col("eq") | pl.col("is_close")).all(),
)
.item()
)
36 changes: 0 additions & 36 deletions tox.ini

This file was deleted.

41 changes: 41 additions & 0 deletions tox.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
requires = [
"tox>=4.21",
"tox-uv",
]
env_list = [
"lint", "type",
"3.13", "3.12", "3.11", "3.10", "3.9", "3.8",
]

[gh.python]
"3.13" = ["3.13", "lint", "type"]
"3.12" = ["3.12"]
"3.11" = ["3.11"]
"3.10" = ["3.10"]
"3.9" = ["3.9"]
"3.8" = ["3.8"]

[env_run_base]
package = "wheel"
wheel_build_env = ".pkg"
extras = ["test"]
commands = [
["pytest", "-p", "no:faulthandler", { replace = "posargs", default = ["tests"], extend = true }],
]

[env.lint]
package = "skip"
deps = [
"ruff",
]
commands = [
["ruff", "format", "--check", { replace = "posargs", default = ["."], extend = true }],
["ruff", "check", "--no-fix", { replace = "posargs", default = ["."], extend = true }],
]

[env.type]
package = "wheel"
extras = ["type"]
commands = [
["mypy", "--install-types", "--non-interactive", { replace = "posargs", default = [], extend = true }],
]

0 comments on commit 2b31c04

Please sign in to comment.