|
1 | | -# pattern-analyzer |
| 1 | +# PatternLab |
2 | 2 |
|
| 3 | +Binary pattern analysis framework for statistical testing and transformation of binary data. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **Plugin Architecture**: Extensible system for transforms and statistical tests |
| 8 | +- **XOR Transform**: Apply constant XOR transformation to binary data |
| 9 | +- **Monobit Test**: Statistical frequency test for binary sequences |
| 10 | +- **CLI Interface**: Command-line tool for easy analysis |
| 11 | +- **Python API**: Programmatic access to all functionality |
| 12 | + |
| 13 | +## Installation |
| 14 | + |
| 15 | +### From Source |
| 16 | + |
| 17 | +```bash |
| 18 | +# Clone the repository |
| 19 | +git clone <repository-url> |
| 20 | +cd patternlab |
| 21 | + |
| 22 | +# Install in development mode |
| 23 | +pip install -e . |
| 24 | +``` |
| 25 | + |
| 26 | +### Dependencies |
| 27 | + |
| 28 | +- Python 3.10+ |
| 29 | +- click (for CLI) |
| 30 | +- pytest (for testing) |
| 31 | + |
| 32 | +## Usage |
| 33 | + |
| 34 | +### Command Line Interface |
| 35 | + |
| 36 | +Analyze a binary file: |
| 37 | + |
| 38 | +```bash |
| 39 | +# Basic analysis (writes JSON with top-level keys "results" and "scorecard") |
| 40 | +patternlab analyze input.bin --out results.json |
| 41 | + |
| 42 | +# With XOR constant transformation |
| 43 | +patternlab analyze input.bin --out results.json --xor-value 255 |
| 44 | +``` |
| 45 | + |
| 46 | +Options: |
| 47 | +- `input-file`: Path to binary file to analyze |
| 48 | +- `--out`: Output JSON file (default: report.json) |
| 49 | +- `--xor-value`: XOR value for transformation (0-255, default: 0) |
| 50 | + |
| 51 | +CLI example — updated JSON output (file written to `results.json`; top-level schema shown below): |
| 52 | + |
| 53 | +Top-level schema: |
| 54 | + |
| 55 | +```json |
| 56 | +{ |
| 57 | + "results": [...], |
| 58 | + "scorecard": {...} |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +Full example output (file written to `results.json`): |
| 63 | + |
| 64 | +```json |
| 65 | +{ |
| 66 | + "results": [ |
| 67 | + { |
| 68 | + "test_name": "monobit", |
| 69 | + "passed": true, |
| 70 | + "p_value": 0.05, |
| 71 | + "p_values": {"overall": 0.05}, |
| 72 | + "effect_sizes": {"overall": 0.8}, |
| 73 | + "flags": [], |
| 74 | + "metrics": {"bit_count": 1000}, |
| 75 | + "z_score": 1.96, |
| 76 | + "evidence": null, |
| 77 | + "fdr_rejected": false, |
| 78 | + "fdr_q": 0.05 |
| 79 | + } |
| 80 | + ], |
| 81 | + "scorecard": { |
| 82 | + "failed_tests": 0, |
| 83 | + "mean_effect_size": 0.8, |
| 84 | + "p_value_distribution": { |
| 85 | + "count": 1, |
| 86 | + "mean": 0.05, |
| 87 | + "median": 0.05, |
| 88 | + "stdev": 0.0, |
| 89 | + "histogram": {"0-0.01": 0, "0.01-0.05": 0, "0.05-0.1": 1, "0.1-1.0": 0} |
| 90 | + }, |
| 91 | + "total_tests": 1, |
| 92 | + "fdr_q": 0.05 |
| 93 | + } |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +### Python API |
| 98 | + |
| 99 | +```python |
| 100 | +from patternlab.engine import Engine |
| 101 | +from patternlab.plugins.xor_const import XOPlugin |
| 102 | +from patternlab.plugins.monobit import MonobitTest |
| 103 | +from patternlab.plugin_api import BytesView, serialize_testresult |
| 104 | +import json |
| 105 | + |
| 106 | +# Engine ve plugin kayıtları |
| 107 | +engine = Engine() |
| 108 | +engine.register_transform('xor_const', XOPlugin()) |
| 109 | +engine.register_test('monobit', MonobitTest()) |
| 110 | + |
| 111 | +# Veri analizi |
| 112 | +with open('input.bin', 'rb') as f: |
| 113 | + data = f.read() |
| 114 | + |
| 115 | +config = {'transforms': [{'name': 'xor_const', 'params': {'xor_value': 255}}], 'tests': [{'name': 'monobit', 'params': {}}]} |
| 116 | +output = engine.analyze(data, config) |
| 117 | + |
| 118 | +# `engine.analyze` fonksiyonu top-level bir dict döner: |
| 119 | +# { "results": [...], "scorecard": {...} } |
| 120 | +# TestResult'ları canonical JSON şemasına çevirmek için serialize_testresult kullanılmaktadır. |
| 121 | +print(json.dumps(output, indent=2)) |
| 122 | +``` |
| 123 | + |
| 124 | +Örnek serializasyon (yeni TestResult şeması — CLI ve engine tarafından üretilen her bir sonuç bu şemaya uyar): |
| 125 | +```json |
| 126 | +{ |
| 127 | + "test_name": "monobit", |
| 128 | + "passed": true, |
| 129 | + "p_value": 0.05, |
| 130 | + "p_values": {"overall": 0.05}, |
| 131 | + "effect_sizes": {"overall": 0.8}, |
| 132 | + "flags": ["flag1", "flag2"], |
| 133 | + "metrics": {"metric1": 123, "metric2": 456}, |
| 134 | + "z_score": 1.96, |
| 135 | + "evidence": "Some evidence string", |
| 136 | + "fdr_rejected": false, |
| 137 | + "fdr_q": 0.05 |
| 138 | +} |
| 139 | +``` |
| 140 | + |
| 141 | +Not: Eğer eski `details` alanı varsa, `serialize_testresult` fonksiyonu onu `metrics` içine birleştirir. |
| 142 | + |
| 143 | +## Available Plugins |
| 144 | + |
| 145 | +### Transform Plugins |
| 146 | + |
| 147 | +- **xor_const**: Applies XOR transformation with constant value |
| 148 | + |
| 149 | +### Test Plugins |
| 150 | + |
| 151 | +- **monobit**: Monobit frequency test for random number generators |
| 152 | + |
| 153 | +## Testing |
| 154 | + |
| 155 | +Run the test suite: |
| 156 | + |
| 157 | +```bash |
| 158 | +# Run all tests |
| 159 | +pytest |
| 160 | + |
| 161 | +# Run with verbose output |
| 162 | +pytest -v |
| 163 | + |
| 164 | +# Run specific test file |
| 165 | +pytest tests/test_xor.py |
| 166 | +``` |
| 167 | + |
| 168 | +## Project Structure |
| 169 | + |
| 170 | +``` |
| 171 | +patternlab/ |
| 172 | +├── __init__.py # Package initialization |
| 173 | +├── plugin_api.py # Plugin base classes and data structures |
| 174 | +├── engine.py # Main analysis engine |
| 175 | +├── cli.py # Command line interface |
| 176 | +└── plugins/ |
| 177 | + ├── __init__.py |
| 178 | + ├── xor_const.py # XOR transform plugin |
| 179 | + └── monobit.py # Monobit test plugin |
| 180 | +tests/ |
| 181 | +├── test_xor.py |
| 182 | +└── test_monobit.py |
| 183 | +``` |
| 184 | + |
| 185 | +## License |
| 186 | + |
| 187 | +MIT License - see LICENSE file for details. |
| 188 | + |
| 189 | +## Contributing |
| 190 | + |
| 191 | +1. Fork the repository |
| 192 | +2. Create a feature branch |
| 193 | +3. Add tests for new functionality |
| 194 | +4. Ensure all tests pass: `pytest` |
| 195 | +5. Submit a pull request |
| 196 | + |
| 197 | +## Requirements |
| 198 | + |
| 199 | +- Python 3.10 or higher |
| 200 | +- No external dependencies beyond standard library (except for development) |
0 commit comments