Skip to content

Commit 2cc1c9d

Browse files
committed
fix: pytest for evaluating model performance dont read stderr anymore
1 parent bfeca31 commit 2cc1c9d

File tree

5 files changed

+40
-27
lines changed

5 files changed

+40
-27
lines changed

.github/workflows/pytest.yml

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,19 @@ jobs:
1111
- "3.8"
1212
- "3.9"
1313
- "3.10"
14+
- "3.11"
15+
- "3.12"
1416

1517
steps:
1618
- uses: actions/checkout@v4
17-
- name: Set up Python ${{ matrix.python-version }}
18-
uses: actions/setup-python@v4
19+
20+
- name: Install uv
21+
uses: astral-sh/setup-uv@v5
1922
with:
2023
python-version: ${{ matrix.python-version }}
21-
- name: Install Python dependencies
22-
run: |
23-
python -m pip install -U pip
24-
pip install -e .[test]
24+
25+
- name: Install the project
26+
run: uv sync --locked --all-extras --dev
27+
2528
- name: Run unit tests
26-
run: python -m pytest -m "not slow" --cov=compressai -s tests/
29+
run: uv run pytest -m "not slow" --cov=compressai -s tests/

.github/workflows/static-analysis.yml

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,10 @@ jobs:
1010
python-version:
1111
- "3.8"
1212
- "3.10"
13+
- "3.12"
14+
1315
include:
1416
- os: "ubuntu-latest"
1517
steps:
16-
- uses: actions/checkout@v4
17-
- name: Set up Python ${{ matrix.python-version }}
18-
uses: actions/setup-python@v4
19-
with:
20-
python-version: ${{ matrix.python-version }}
21-
- name: Install Python dependencies
22-
run: |
23-
python3 -m pip install -U pip
24-
python3 -m pip install .[dev]
25-
- name: Run static analysis checks
18+
- uses: astral-sh/ruff-action@v3
2619
run: make static-analysis

compressai/utils/video/eval_model/__main__.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,13 @@ def create_parser() -> argparse.ArgumentParser:
467467
default="mse",
468468
help="metric trained against (default: %(default)s)",
469469
)
470+
parent_parser.add_argument(
471+
"-d",
472+
"--output_directory",
473+
type=str,
474+
default="",
475+
help="path of output directory. Optional, required for output json file, results per video.",
476+
)
470477
parent_parser.add_argument(
471478
"-o",
472479
"--output-file",
@@ -525,8 +532,8 @@ def main(args: Any = None) -> None:
525532
raise SystemExit(1)
526533

527534
# create output directory
528-
outputdir = args.output
529-
Path(outputdir).mkdir(parents=True, exist_ok=True)
535+
if args.output_directory:
536+
Path(args.output_directory).mkdir(parents=True, exist_ok=True)
530537

531538
if args.source == "pretrained":
532539
args.qualities = [int(q) for q in args.qualities.split(",") if q]
@@ -561,7 +568,7 @@ def main(args: Any = None) -> None:
561568
filepaths,
562569
args.dataset,
563570
model,
564-
outputdir,
571+
args.output_directory,
565572
trained_net=trained_net,
566573
description=description,
567574
**args_dict,
@@ -581,7 +588,7 @@ def main(args: Any = None) -> None:
581588
else:
582589
output_file = args.output_file
583590

584-
with (Path(f"{outputdir}/{output_file}").with_suffix(".json")).open("wb") as f:
591+
with (Path(f"{args.output_directory}/{output_file}").with_suffix(".json")).open("wb") as f:
585592
f.write(json.dumps(output, indent=2).encode())
586593
print(json.dumps(output, indent=2))
587594

tests/test_eval_model.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def test_eval_model():
7878
@pytest.mark.parametrize("metric", ("mse", "ms-ssim"))
7979
@pytest.mark.parametrize("entropy_estimation", (False, True))
8080
def test_eval_model_pretrained(
81-
capsys, model, quality, metric, entropy_estimation, tmpdir
81+
model, quality, metric, entropy_estimation, tmpdir
8282
):
8383
here = os.path.dirname(__file__)
8484
dirpath = os.path.join(here, "assets/dataset/image")
@@ -92,13 +92,18 @@ def test_eval_model_pretrained(
9292
metric,
9393
"-q",
9494
quality,
95+
"-o",
96+
f"{model}-{metric}-{quality}",
97+
"-d",
98+
str(tmpdir),
9599
]
96100
if entropy_estimation:
97101
cmd += ["--entropy-estimation"]
98102
eval_model.main(cmd)
99103

100-
output = capsys.readouterr().out
101-
output = json.loads(output)
104+
with open(f"{tmpdir}/{model}-{metric}-{quality}.json") as f:
105+
output = json.load(f)
106+
102107
expected = os.path.join(
103108
here,
104109
"expected",

tests/test_eval_model_video.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def test_eval_model_video():
8080
@pytest.mark.parametrize("metric", ("mse",))
8181
@pytest.mark.parametrize("entropy_estimation", (True, False))
8282
def test_eval_model_pretrained(
83-
capsys, model, quality, metric, entropy_estimation, tmpdir
83+
model, quality, metric, entropy_estimation, tmpdir
8484
):
8585
here = os.path.dirname(__file__)
8686
dirpath = os.path.join(here, "assets/dataset/video")
@@ -95,13 +95,18 @@ def test_eval_model_pretrained(
9595
metric,
9696
"-q",
9797
quality,
98+
"-d",
99+
str(tmpdir),
100+
"-o",
101+
f"{model}-{metric}-{quality}",
98102
]
99103
if entropy_estimation:
100104
cmd += ["--entropy-estimation"]
101105
eval_model.main(cmd)
102106

103-
output = capsys.readouterr().out
104-
output = json.loads(output)
107+
with open(f"{tmpdir}/{model}-{metric}-{quality}.json") as f:
108+
output = json.load(f)
109+
105110
expected = os.path.join(
106111
here,
107112
"expected",

0 commit comments

Comments
 (0)