A Python library for comparing program versions using metamorphic testing
Project description
Metamorphic Guard
A Python library that compares two program versions—baseline and candidate—by running property and metamorphic tests, computing confidence intervals on pass-rate differences, and deciding whether to adopt the candidate.
Overview
Metamorphic Guard evaluates candidate implementations against baseline versions by:
- Property Testing: Verifying that outputs satisfy required properties
- Metamorphic Testing: Checking that input transformations produce equivalent outputs
- Statistical Analysis: Computing bootstrap confidence intervals on pass-rate differences
- Adoption Gating: Making data-driven decisions about whether to adopt candidates
Installation
pip install -e .
Quick Start
Basic Usage
metamorphic-guard --task top_k \
--baseline examples/top_k_baseline.py \
--candidate examples/top_k_improved.py
Tip: If the shorter
metamorphic-guardalias collides with a system binary, usepython -m metamorphic_guard.clior the alternative console scriptmetaguard.
Command Line Options
metamorphic-guard --help
Required Options:
--task: Task name to evaluate (e.g., "top_k")--baseline: Path to baseline implementation--candidate: Path to candidate implementation
Optional Options:
--n: Number of test cases (default: 400)--seed: Random seed for reproducibility (default: 42)--timeout-s: Timeout per test in seconds (default: 2.0)--mem-mb: Memory limit in MB (default: 512)--alpha: Significance level for confidence intervals (default: 0.05)--improve-delta: Minimum improvement threshold (default: 0.02)--violation-cap: Maximum violations to report (default: 25)--parallel: Number of worker processes used to drive the sandbox (default: 1)--bootstrap-samples: Resamples used for percentile bootstrap CI (default: 1000)
Example Implementations
The examples/ directory contains sample implementations for the top_k task:
top_k_baseline.py: Correct baseline implementationtop_k_bad.py: Buggy implementation (should be rejected)top_k_improved.py: Improved implementation (should be accepted)
Task Specification
Top-K Task
The top_k task finds the k largest elements from a list:
Input: (L: List[int], k: int)
Output: List[int] - k largest elements, sorted in descending order
Properties:
- Output length equals
min(k, len(L)) - Output is sorted in descending order
- All output elements are from the input list
Metamorphic Relations:
- Permute Input: Shuffling the input list should produce equivalent results
- Add Noise Below Min: Adding small values below the minimum should not affect results
Implementation Requirements
Candidate Function Contract
Each candidate file must export a callable function:
def solve(*args):
"""
Your implementation here.
Must handle the same input format as the task specification.
"""
return result
Sandbox Execution
- All candidate code runs in isolated subprocesses
- Resource limits: CPU time, memory usage
- Network access is disabled by stubbing socket primitives and import hooks
- Subprocess creation (
os.system,subprocess.Popen, etc.) is denied inside the sandbox - Timeout enforcement per test case
- Deterministic execution with fixed seeds
Output Format
The system generates JSON reports in reports/report_<timestamp>.json:
{
"task": "top_k",
"n": 400,
"seed": 42,
"config": {
"timeout_s": 2.0,
"mem_mb": 512,
"alpha": 0.05,
"improve_delta": 0.02,
"violation_cap": 25,
"parallel": 1,
"bootstrap_samples": 1000
},
"hashes": {
"baseline": "sha256...",
"candidate": "sha256..."
},
"baseline": {
"passes": 388,
"total": 400,
"pass_rate": 0.97
},
"candidate": {
"passes": 396,
"total": 400,
"pass_rate": 0.99,
"prop_violations": [],
"mr_violations": []
},
"delta_pass_rate": 0.02,
"delta_ci": [0.015, 0.035],
"decision": {
"adopt": true,
"reason": "meets_gate"
}
}
Adoption Policy
A candidate is adopted if all conditions are met:
- No Property Violations: All hard properties must pass
- No Metamorphic Relation Violations: All relations must be satisfied
- Sufficient Improvement: Lower bound of 95% CI > improvement threshold
- Minimum Pass Rate: Candidate pass rate ≥ minimum threshold
Testing
Install development dependencies first:
pip install -e .[dev]
# or
pip install -r requirements-dev.txt
Run the test suite:
pytest tests/
Run specific test categories:
pytest tests/test_sandbox.py # Sandbox isolation tests
pytest tests/test_harness.py # Evaluation tests
pytest tests/test_gate.py # Adoption logic tests
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file metamorphic_guard-1.0.0-py3-none-any.whl.
File metadata
- Download URL: metamorphic_guard-1.0.0-py3-none-any.whl
- Upload date:
- Size: 21.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
647f7360a99be4a00bba2fe3be49f397add6144cc4a133e3382331879a2f907f
|
|
| MD5 |
4f62b74b982b8b0d8a08de348eaf93c2
|
|
| BLAKE2b-256 |
44d7bd542c0a722535ba9f46a0fcc2a6813955d26a4a26f5effc8c5543da9b10
|