pyfunctionhood 0.2.1__tar.gz → 0.3.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pyfunctionhood-0.3.0/.github/workflows/python-publish.yml +47 -0
- pyfunctionhood-0.3.0/.gitignore +8 -0
- {pyfunctionhood-0.2.1/src/pyfunctionhood.egg-info → pyfunctionhood-0.3.0}/PKG-INFO +17 -1
- {pyfunctionhood-0.2.1 → pyfunctionhood-0.3.0}/README.md +16 -0
- pyfunctionhood-0.3.0/img/pyfunctionhood.png +0 -0
- {pyfunctionhood-0.2.1 → pyfunctionhood-0.3.0}/pyproject.toml +6 -2
- pyfunctionhood-0.3.0/requirements.txt +2 -0
- {pyfunctionhood-0.2.1 → pyfunctionhood-0.3.0}/src/pyfunctionhood/hassediagram.py +7 -6
- {pyfunctionhood-0.2.1 → pyfunctionhood-0.3.0/src/pyfunctionhood.egg-info}/PKG-INFO +17 -1
- {pyfunctionhood-0.2.1 → pyfunctionhood-0.3.0}/src/pyfunctionhood.egg-info/SOURCES.txt +10 -1
- pyfunctionhood-0.3.0/src/pyfunctionhood.egg-info/scm_file_list.json +20 -0
- pyfunctionhood-0.3.0/src/pyfunctionhood.egg-info/scm_version.json +8 -0
- pyfunctionhood-0.3.0/tests/test_check_rules.py +110 -0
- pyfunctionhood-0.3.0/tests/test_check_sign_changes.py +130 -0
- pyfunctionhood-0.3.0/tests/test_rule_categorization.py +117 -0
- {pyfunctionhood-0.2.1 → pyfunctionhood-0.3.0}/LICENSE +0 -0
- {pyfunctionhood-0.2.1 → pyfunctionhood-0.3.0}/setup.cfg +0 -0
- {pyfunctionhood-0.2.1 → pyfunctionhood-0.3.0}/src/pyfunctionhood/__init__.py +0 -0
- {pyfunctionhood-0.2.1 → pyfunctionhood-0.3.0}/src/pyfunctionhood/clause.py +0 -0
- {pyfunctionhood-0.2.1 → pyfunctionhood-0.3.0}/src/pyfunctionhood/function.py +0 -0
- {pyfunctionhood-0.2.1 → pyfunctionhood-0.3.0}/src/pyfunctionhood/main.py +0 -0
- {pyfunctionhood-0.2.1 → pyfunctionhood-0.3.0}/src/pyfunctionhood/powerset.py +0 -0
- {pyfunctionhood-0.2.1 → pyfunctionhood-0.3.0}/src/pyfunctionhood.egg-info/dependency_links.txt +0 -0
- {pyfunctionhood-0.2.1 → pyfunctionhood-0.3.0}/src/pyfunctionhood.egg-info/entry_points.txt +0 -0
- {pyfunctionhood-0.2.1 → pyfunctionhood-0.3.0}/src/pyfunctionhood.egg-info/requires.txt +0 -0
- {pyfunctionhood-0.2.1 → pyfunctionhood-0.3.0}/src/pyfunctionhood.egg-info/top_level.txt +0 -0
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
name: Publish Python Package
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
tags:
|
|
6
|
+
- 'v*' # Trigger workflow on tags that start with "v" (e.g., v1.0.0)
|
|
7
|
+
workflow_dispatch: # Allow manual runs from the Actions tab
|
|
8
|
+
|
|
9
|
+
jobs:
|
|
10
|
+
build-and-publish:
|
|
11
|
+
name: Build and publish to PyPI
|
|
12
|
+
runs-on: ubuntu-latest
|
|
13
|
+
|
|
14
|
+
# Required for trusted publishing to PyPI
|
|
15
|
+
permissions:
|
|
16
|
+
id-token: write
|
|
17
|
+
contents: write # Elevated from 'read' to allow Release creation
|
|
18
|
+
|
|
19
|
+
steps:
|
|
20
|
+
- name: Checkout Code
|
|
21
|
+
uses: actions/checkout@v6
|
|
22
|
+
with:
|
|
23
|
+
fetch-depth: 0
|
|
24
|
+
|
|
25
|
+
- name: Set up Python
|
|
26
|
+
uses: actions/setup-python@v6
|
|
27
|
+
with:
|
|
28
|
+
python-version: "3.x"
|
|
29
|
+
|
|
30
|
+
- name: Install build dependencies
|
|
31
|
+
run: python -m pip install --upgrade pip build setuptools_scm[toml]
|
|
32
|
+
|
|
33
|
+
- name: Build sdist and wheel
|
|
34
|
+
run: python -m build
|
|
35
|
+
|
|
36
|
+
- name: Publish to PyPI
|
|
37
|
+
uses: pypa/gh-action-pypi-publish@release/v1
|
|
38
|
+
|
|
39
|
+
- name: Create GitHub Release
|
|
40
|
+
if: startsWith(github.ref, 'refs/tags/')
|
|
41
|
+
uses: softprops/action-gh-release@v2
|
|
42
|
+
with:
|
|
43
|
+
# This will include the .tar.gz and .whl files in the GitHub Release assets
|
|
44
|
+
files: dist/*
|
|
45
|
+
generate_release_notes: true
|
|
46
|
+
env:
|
|
47
|
+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: pyfunctionhood
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.3.0
|
|
4
4
|
Summary: Local computation of immediate neighbours of a given monotone non-degenerate Boolean function
|
|
5
5
|
Author-email: "Pedro T. Monteiro" <Pedro.Tiago.Monteiro@tecnico.ulisboa.pt>
|
|
6
6
|
License: AGPL-3.0
|
|
@@ -146,6 +146,22 @@ s1, s2, s3 = hd.get_f_parents_with_sign_changes(f, changed_signs)
|
|
|
146
146
|
|
|
147
147
|
---
|
|
148
148
|
|
|
149
|
+
Testing
|
|
150
|
+
-------
|
|
151
|
+
|
|
152
|
+
The library includes a comprehensive pytest suite to validate correctness of the core algorithms:
|
|
153
|
+
|
|
154
|
+
```bash
|
|
155
|
+
pytest tests/ -v
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
This runs 22 tests covering:
|
|
159
|
+
- **Sign changes computation** (test_check_sign_changes.py): Validates `get_f_parents/children_with_sign_changes()` by exhaustive checking for small dimensions and random sampling for larger ones
|
|
160
|
+
- **Random walk consistency** (test_check_rules.py): Performs random walks on the Hasse diagram to verify parent-child relationships are consistent
|
|
161
|
+
- **Rule categorization** (test_rule_categorization.py): Validates that parent and children are correctly partitioned into S1, S2, and S3 rules
|
|
162
|
+
|
|
163
|
+
---
|
|
164
|
+
|
|
149
165
|
License
|
|
150
166
|
-------
|
|
151
167
|
|
|
@@ -120,6 +120,22 @@ s1, s2, s3 = hd.get_f_parents_with_sign_changes(f, changed_signs)
|
|
|
120
120
|
|
|
121
121
|
---
|
|
122
122
|
|
|
123
|
+
Testing
|
|
124
|
+
-------
|
|
125
|
+
|
|
126
|
+
The library includes a comprehensive pytest suite to validate correctness of the core algorithms:
|
|
127
|
+
|
|
128
|
+
```bash
|
|
129
|
+
pytest tests/ -v
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
This runs 22 tests covering:
|
|
133
|
+
- **Sign changes computation** (test_check_sign_changes.py): Validates `get_f_parents/children_with_sign_changes()` by exhaustive checking for small dimensions and random sampling for larger ones
|
|
134
|
+
- **Random walk consistency** (test_check_rules.py): Performs random walks on the Hasse diagram to verify parent-child relationships are consistent
|
|
135
|
+
- **Rule categorization** (test_rule_categorization.py): Validates that parent and children are correctly partitioned into S1, S2, and S3 rules
|
|
136
|
+
|
|
137
|
+
---
|
|
138
|
+
|
|
123
139
|
License
|
|
124
140
|
-------
|
|
125
141
|
|
|
Binary file
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
[build-system]
|
|
2
|
-
requires = ["setuptools>=61.0", "wheel"]
|
|
2
|
+
requires = ["setuptools>=61.0", "wheel", "setuptools_scm[toml]>=8"]
|
|
3
3
|
build-backend = "setuptools.build_meta"
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "pyfunctionhood"
|
|
7
|
-
|
|
7
|
+
dynamic = ["version"]
|
|
8
8
|
description = "Local computation of immediate neighbours of a given monotone non-degenerate Boolean function"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
requires-python = ">=3.9"
|
|
@@ -36,3 +36,7 @@ Repository = "https://github.com/ptgm/pyfunctionhood"
|
|
|
36
36
|
|
|
37
37
|
[project.scripts]
|
|
38
38
|
pyfunctionhood = "pyfunctionhood.main:main"
|
|
39
|
+
|
|
40
|
+
[tool.setuptools_scm]
|
|
41
|
+
tag_regex = "^v(?P<version>\\d+\\.\\d+\\.\\d+)$"
|
|
42
|
+
local_scheme = "no-local-version"
|
|
@@ -164,16 +164,17 @@ class HasseDiagram:
|
|
|
164
164
|
if not invariants:
|
|
165
165
|
return set(), set(), set()
|
|
166
166
|
|
|
167
|
-
# One f' per clause s in invariants: f' =
|
|
167
|
+
# One f' per clause s in invariants: f' = (Inv \ {s}) U { s U {l} : l in ext(s) },
|
|
168
|
+
# where ext(s) is every literal l not in s such that s U {l} is independent of Inv \ {s}.
|
|
169
|
+
# (ext(s) always contains every changed variable, so f' is a cover.)
|
|
168
170
|
sChildren = set()
|
|
169
171
|
for s in invariants:
|
|
170
172
|
sRest = invariants - { s }
|
|
171
|
-
|
|
173
|
+
sExt = set()
|
|
172
174
|
for l in s.get_off_literals():
|
|
173
|
-
candidate =
|
|
175
|
+
candidate = s.clone_add(l)
|
|
174
176
|
if all(candidate.is_independent(c) for c in sRest):
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
sChildren.add(Function(self.nvars, sRest | { s_ext }))
|
|
177
|
+
sExt.add(candidate)
|
|
178
|
+
sChildren.add(Function(self.nvars, sRest | sExt))
|
|
178
179
|
|
|
179
180
|
return sChildren, set(), set() # between signature changes only R1 children are possible
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: pyfunctionhood
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.3.0
|
|
4
4
|
Summary: Local computation of immediate neighbours of a given monotone non-degenerate Boolean function
|
|
5
5
|
Author-email: "Pedro T. Monteiro" <Pedro.Tiago.Monteiro@tecnico.ulisboa.pt>
|
|
6
6
|
License: AGPL-3.0
|
|
@@ -146,6 +146,22 @@ s1, s2, s3 = hd.get_f_parents_with_sign_changes(f, changed_signs)
|
|
|
146
146
|
|
|
147
147
|
---
|
|
148
148
|
|
|
149
|
+
Testing
|
|
150
|
+
-------
|
|
151
|
+
|
|
152
|
+
The library includes a comprehensive pytest suite to validate correctness of the core algorithms:
|
|
153
|
+
|
|
154
|
+
```bash
|
|
155
|
+
pytest tests/ -v
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
This runs 22 tests covering:
|
|
159
|
+
- **Sign changes computation** (test_check_sign_changes.py): Validates `get_f_parents/children_with_sign_changes()` by exhaustive checking for small dimensions and random sampling for larger ones
|
|
160
|
+
- **Random walk consistency** (test_check_rules.py): Performs random walks on the Hasse diagram to verify parent-child relationships are consistent
|
|
161
|
+
- **Rule categorization** (test_rule_categorization.py): Validates that parent and children are correctly partitioned into S1, S2, and S3 rules
|
|
162
|
+
|
|
163
|
+
---
|
|
164
|
+
|
|
149
165
|
License
|
|
150
166
|
-------
|
|
151
167
|
|
|
@@ -1,6 +1,10 @@
|
|
|
1
|
+
.gitignore
|
|
1
2
|
LICENSE
|
|
2
3
|
README.md
|
|
3
4
|
pyproject.toml
|
|
5
|
+
requirements.txt
|
|
6
|
+
.github/workflows/python-publish.yml
|
|
7
|
+
img/pyfunctionhood.png
|
|
4
8
|
src/pyfunctionhood/__init__.py
|
|
5
9
|
src/pyfunctionhood/clause.py
|
|
6
10
|
src/pyfunctionhood/function.py
|
|
@@ -12,4 +16,9 @@ src/pyfunctionhood.egg-info/SOURCES.txt
|
|
|
12
16
|
src/pyfunctionhood.egg-info/dependency_links.txt
|
|
13
17
|
src/pyfunctionhood.egg-info/entry_points.txt
|
|
14
18
|
src/pyfunctionhood.egg-info/requires.txt
|
|
15
|
-
src/pyfunctionhood.egg-info/
|
|
19
|
+
src/pyfunctionhood.egg-info/scm_file_list.json
|
|
20
|
+
src/pyfunctionhood.egg-info/scm_version.json
|
|
21
|
+
src/pyfunctionhood.egg-info/top_level.txt
|
|
22
|
+
tests/test_check_rules.py
|
|
23
|
+
tests/test_check_sign_changes.py
|
|
24
|
+
tests/test_rule_categorization.py
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"files": [
|
|
3
|
+
".github/workflows/python-publish.yml",
|
|
4
|
+
".gitignore",
|
|
5
|
+
"LICENSE",
|
|
6
|
+
"README.md",
|
|
7
|
+
"img/pyfunctionhood.png",
|
|
8
|
+
"pyproject.toml",
|
|
9
|
+
"requirements.txt",
|
|
10
|
+
"src/pyfunctionhood/__init__.py",
|
|
11
|
+
"src/pyfunctionhood/clause.py",
|
|
12
|
+
"src/pyfunctionhood/function.py",
|
|
13
|
+
"src/pyfunctionhood/hassediagram.py",
|
|
14
|
+
"src/pyfunctionhood/main.py",
|
|
15
|
+
"src/pyfunctionhood/powerset.py",
|
|
16
|
+
"tests/test_check_rules.py",
|
|
17
|
+
"tests/test_check_sign_changes.py",
|
|
18
|
+
"tests/test_rule_categorization.py"
|
|
19
|
+
]
|
|
20
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"""Pytest for consistency of parent-child relationships in Hasse diagrams.
|
|
2
|
+
|
|
3
|
+
This module contains tests that verify parent-child relationships are
|
|
4
|
+
consistent by performing random walks on the Hasse diagram, both from
|
|
5
|
+
infimum to supremum and from supremum to infimum.
|
|
6
|
+
"""
|
|
7
|
+
import pytest
|
|
8
|
+
import random
|
|
9
|
+
from pyfunctionhood import HasseDiagram
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def random_walk_infimum_to_supremum(hd, num_walks=10):
|
|
13
|
+
"""Perform random walks from infimum to supremum.
|
|
14
|
+
|
|
15
|
+
For each walk, verify that at every step, the previous function
|
|
16
|
+
is indeed a child of the randomly chosen parent.
|
|
17
|
+
|
|
18
|
+
Args:
|
|
19
|
+
hd: HasseDiagram instance
|
|
20
|
+
num_walks: number of independent random walks to perform
|
|
21
|
+
|
|
22
|
+
Returns:
|
|
23
|
+
Tuple of (failed, total_steps) where failed is True if any walk failed
|
|
24
|
+
"""
|
|
25
|
+
finf = hd.get_infimum()
|
|
26
|
+
fsup = hd.get_supremum()
|
|
27
|
+
|
|
28
|
+
for walk in range(num_walks):
|
|
29
|
+
f = finf
|
|
30
|
+
while f != fsup:
|
|
31
|
+
s1Parents, s2Parents, s3Parents = hd.get_f_parents(f)
|
|
32
|
+
lParents = list(s1Parents) + list(s2Parents) + list(s3Parents)
|
|
33
|
+
|
|
34
|
+
# Randomly choose a parent
|
|
35
|
+
parent = lParents[random.randint(0, len(lParents) - 1)]
|
|
36
|
+
|
|
37
|
+
# Verify that f is indeed a child of parent
|
|
38
|
+
s1Children, s2Children, s3Children = hd.get_f_children(parent)
|
|
39
|
+
if not (f in s1Children or f in s2Children or f in s3Children):
|
|
40
|
+
return True, walk, f, parent # Failed
|
|
41
|
+
|
|
42
|
+
f = parent
|
|
43
|
+
|
|
44
|
+
return False, num_walks, None, None # Success
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def random_walk_supremum_to_infimum(hd, num_walks=10):
|
|
48
|
+
"""Perform random walks from supremum to infimum.
|
|
49
|
+
|
|
50
|
+
For each walk, verify that at every step, the previous function
|
|
51
|
+
is indeed a parent of the randomly chosen child.
|
|
52
|
+
|
|
53
|
+
Args:
|
|
54
|
+
hd: HasseDiagram instance
|
|
55
|
+
num_walks: number of independent random walks to perform
|
|
56
|
+
|
|
57
|
+
Returns:
|
|
58
|
+
Tuple of (failed, total_steps) where failed is True if any walk failed
|
|
59
|
+
"""
|
|
60
|
+
finf = hd.get_infimum()
|
|
61
|
+
fsup = hd.get_supremum()
|
|
62
|
+
|
|
63
|
+
for walk in range(num_walks):
|
|
64
|
+
f = fsup
|
|
65
|
+
while f != finf:
|
|
66
|
+
s1Children, s2Children, s3Children = hd.get_f_children(f)
|
|
67
|
+
lChildren = list(s1Children) + list(s2Children) + list(s3Children)
|
|
68
|
+
|
|
69
|
+
# Randomly choose a child
|
|
70
|
+
child = lChildren[random.randint(0, len(lChildren) - 1)]
|
|
71
|
+
|
|
72
|
+
# Verify that f is indeed a parent of child
|
|
73
|
+
s1Parents, s2Parents, s3Parents = hd.get_f_parents(child)
|
|
74
|
+
if not (f in s1Parents or f in s2Parents or f in s3Parents):
|
|
75
|
+
return True, walk, f, child # Failed
|
|
76
|
+
|
|
77
|
+
f = child
|
|
78
|
+
|
|
79
|
+
return False, num_walks, None, None # Success
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
@pytest.mark.parametrize("n", [2, 3, 4, 5, 6])
|
|
83
|
+
def test_hasse_diagram_rules_infimum_to_supremum(n):
|
|
84
|
+
"""Test parent-child consistency with random walks from infimum to supremum.
|
|
85
|
+
|
|
86
|
+
Performs 10 independent random walks from the infimum (0) to supremum (1)
|
|
87
|
+
on the Hasse diagram, verifying that at each step, the previous function
|
|
88
|
+
is indeed a child of the randomly chosen parent.
|
|
89
|
+
"""
|
|
90
|
+
hd = HasseDiagram(n)
|
|
91
|
+
failed, walk, f, parent = random_walk_infimum_to_supremum(hd, num_walks=10)
|
|
92
|
+
assert not failed, f"Walk failed at walk {walk} for n={n}: f={f} is not a child of parent={parent}"
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
@pytest.mark.parametrize("n", [2, 3, 4, 5, 6])
|
|
96
|
+
def test_hasse_diagram_rules_supremum_to_infimum(n):
|
|
97
|
+
"""Test parent-child consistency with random walks from supremum to infimum.
|
|
98
|
+
|
|
99
|
+
Performs 10 independent random walks from the supremum (1) to infimum (0)
|
|
100
|
+
on the Hasse diagram, verifying that at each step, the previous function
|
|
101
|
+
is indeed a parent of the randomly chosen child.
|
|
102
|
+
"""
|
|
103
|
+
hd = HasseDiagram(n)
|
|
104
|
+
failed, walk, f, child = random_walk_supremum_to_infimum(hd, num_walks=10)
|
|
105
|
+
assert not failed, f"Walk failed at walk {walk} for n={n}: f={f} is not a parent of child={child}"
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
if __name__ == "__main__":
|
|
109
|
+
# Allow running directly with: python -m pytest tests/test_check_rules.py
|
|
110
|
+
pytest.main([__file__, "-v"])
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"""Pytest for sign changes validation using checkSignChanges.
|
|
2
|
+
|
|
3
|
+
This module contains parameterized tests that validate the correctness of
|
|
4
|
+
get_f_parents/children_with_sign_changes by checking against brute-force
|
|
5
|
+
exhaustive or sampled (f, K) pairs.
|
|
6
|
+
"""
|
|
7
|
+
#A test case is a pair (f, K): f a non-degenerate monotone function
|
|
8
|
+
#over n variables and K a non-empty set of variables whose sign changes.
|
|
9
|
+
#Number of pairs = #functions * (2^n - 1):
|
|
10
|
+
#
|
|
11
|
+
# n #functions #K = 2^n - 1 #pairs (f, K)
|
|
12
|
+
# 2 2 3 6
|
|
13
|
+
# 3 9 7 63
|
|
14
|
+
# 4 114 15 1,710
|
|
15
|
+
# 5 6,894 31 213,714 - tested below for 100 random pairs
|
|
16
|
+
# 6 7,785,062 63 ~4.9 * 10^8
|
|
17
|
+
# 7 ~2.4 * 10^12 127 ~3.1 * 10^14
|
|
18
|
+
# 8 ~5.6 * 10^22 255 ~1.4 * 10^25
|
|
19
|
+
|
|
20
|
+
import pytest
|
|
21
|
+
import sys
|
|
22
|
+
import random
|
|
23
|
+
from bitarray import bitarray
|
|
24
|
+
from pyfunctionhood import HasseDiagram, Function, Clause
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def all_functions(n):
|
|
28
|
+
"""Generate all non-degenerate monotone functions over n variables."""
|
|
29
|
+
full = (1 << n) - 1
|
|
30
|
+
subsets = sorted(range(1, full + 1), key=lambda s: (bin(s).count('1'), s))
|
|
31
|
+
res = []
|
|
32
|
+
def rec(i, cur):
|
|
33
|
+
if i == len(subsets):
|
|
34
|
+
u = 0
|
|
35
|
+
for c in cur: u |= c
|
|
36
|
+
if u == full: res.append(frozenset(cur))
|
|
37
|
+
return
|
|
38
|
+
rec(i + 1, cur)
|
|
39
|
+
s = subsets[i]
|
|
40
|
+
if all((s & c) != c and (s & c) != s for c in cur):
|
|
41
|
+
cur.append(s); rec(i + 1, cur); cur.pop()
|
|
42
|
+
rec(0, [])
|
|
43
|
+
return res
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def true_set(fn, n, K):
|
|
47
|
+
"""Compute the true set of a function with sign changes in variables K.
|
|
48
|
+
|
|
49
|
+
Variables in K are negative: a literal is true iff x_i == 0.
|
|
50
|
+
"""
|
|
51
|
+
return frozenset(x for x in range(1 << n) if any(((x ^ K) & s) == s for s in fn))
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def to_fn(n, masks):
|
|
55
|
+
"""Convert from mask representation to Function object."""
|
|
56
|
+
return Function(n, {Clause(bitarray(''.join('1' if (m >> i) & 1 else '0' for i in range(n)))) for m in masks})
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def from_fn(f):
|
|
60
|
+
"""Convert from Function object to mask representation."""
|
|
61
|
+
return frozenset(sum(1 << i for i in c.get_on_literals()) for c in f.get_clauses())
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def check_dimension(n, sample=None):
|
|
65
|
+
"""Check all (f, K) pairs for dimension n.
|
|
66
|
+
|
|
67
|
+
Args:
|
|
68
|
+
n: number of variables
|
|
69
|
+
sample: if None, do exhaustive check; otherwise randomly sample this many pairs
|
|
70
|
+
|
|
71
|
+
Returns:
|
|
72
|
+
Tuple of (mismatches, total_pairs_checked)
|
|
73
|
+
"""
|
|
74
|
+
hd = HasseDiagram(n)
|
|
75
|
+
bad = tot = 0
|
|
76
|
+
|
|
77
|
+
if sample is None: # exhaustive: need all functions
|
|
78
|
+
funcs = all_functions(n)
|
|
79
|
+
Tpos = {f: true_set(f, n, 0) for f in funcs}
|
|
80
|
+
pairs = {K: funcs for K in range(1, 1 << n)}
|
|
81
|
+
else: # random (f,K) pairs: only generate functions as needed
|
|
82
|
+
funcs = all_functions(n) # Still need all for comparison, but cache results
|
|
83
|
+
Tpos = {f: true_set(f, n, 0) for f in funcs}
|
|
84
|
+
pairs = {}
|
|
85
|
+
for _ in range(sample):
|
|
86
|
+
pairs.setdefault(random.randrange(1, 1 << n), []).append(random.choice(funcs))
|
|
87
|
+
|
|
88
|
+
for K, fs in pairs.items():
|
|
89
|
+
Tk = {f: true_set(f, n, K) for f in funcs}
|
|
90
|
+
Kb = bitarray(''.join('1' if (K >> i) & 1 else '0' for i in range(n)))
|
|
91
|
+
for f in fs:
|
|
92
|
+
up = [g for g in funcs if Tpos[f] <= Tk[g]]
|
|
93
|
+
down = [g for g in funcs if Tk[g] <= Tpos[f]]
|
|
94
|
+
mn = {g for g in up if not any(Tk[h] < Tk[g] for h in up)}
|
|
95
|
+
mx = {g for g in down if not any(Tk[g] < Tk[h] for h in down)}
|
|
96
|
+
P, _, _ = hd.get_f_parents_with_sign_changes(to_fn(n, f), Kb)
|
|
97
|
+
C, _, _ = hd.get_f_children_with_sign_changes(to_fn(n, f), Kb)
|
|
98
|
+
tot += 1
|
|
99
|
+
if {from_fn(g) for g in P} != mn or {from_fn(g) for g in C} != mx:
|
|
100
|
+
bad += 1
|
|
101
|
+
|
|
102
|
+
return bad, tot
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
@pytest.mark.parametrize("n", [2, 3, 4])
|
|
106
|
+
def test_sign_changes_exhaustive(n):
|
|
107
|
+
"""Test sign changes with exhaustive checking for n=2,3,4.
|
|
108
|
+
|
|
109
|
+
For small dimensions (n <= 4), all (f, K) pairs are checked.
|
|
110
|
+
"""
|
|
111
|
+
mismatches, total = check_dimension(n, sample=None)
|
|
112
|
+
assert mismatches == 0, f"Found {mismatches} mismatches in {total} (f, K) pairs for n={n}"
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
@pytest.mark.parametrize("n,sample", [(5, 100)])
|
|
116
|
+
def test_sign_changes_sampled(n, sample):
|
|
117
|
+
"""Test sign changes with random sampling for larger dimensions.
|
|
118
|
+
|
|
119
|
+
For n=5 with 100 random (f, K) pairs sampled.
|
|
120
|
+
Note: n=6 would require generating 7.7M functions just to check 1 pair,
|
|
121
|
+
so it's not practical for routine testing.
|
|
122
|
+
"""
|
|
123
|
+
mismatches, total = check_dimension(n, sample=sample)
|
|
124
|
+
assert mismatches == 0, f"Found {mismatches} mismatches in {total} sampled (f, K) pairs for n={n} (sample={sample})"
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
if __name__ == "__main__":
|
|
129
|
+
# Allow running directly with: python -m pytest tests/test_check_sign_changes.py
|
|
130
|
+
pytest.main([__file__, "-v"])
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
"""Pytest for rule categorization (S1, S2, S3) in parent-child relationships.
|
|
2
|
+
|
|
3
|
+
This module validates that parents and children are correctly categorized
|
|
4
|
+
into S1, S2, and S3 rules, and that the categorization is exhaustive and
|
|
5
|
+
consistent.
|
|
6
|
+
"""
|
|
7
|
+
import pytest
|
|
8
|
+
from pyfunctionhood import HasseDiagram
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def test_rule_categorization_completeness(n=4):
|
|
12
|
+
"""Verify that all parents/children are categorized into exactly one rule.
|
|
13
|
+
|
|
14
|
+
Every parent must belong to exactly one of S1, S2, or S3.
|
|
15
|
+
Every child must belong to exactly one of S1, S2, or S3.
|
|
16
|
+
"""
|
|
17
|
+
hd = HasseDiagram(n)
|
|
18
|
+
finf = hd.get_infimum()
|
|
19
|
+
|
|
20
|
+
# Sample some functions at different levels
|
|
21
|
+
s1Parents, s2Parents, s3Parents = hd.get_f_parents(finf)
|
|
22
|
+
|
|
23
|
+
# Check partition properties: sets should be disjoint
|
|
24
|
+
assert len(s1Parents & s2Parents) == 0, "S1 and S2 parents overlap"
|
|
25
|
+
assert len(s2Parents & s3Parents) == 0, "S2 and S3 parents overlap"
|
|
26
|
+
assert len(s1Parents & s3Parents) == 0, "S1 and S3 parents overlap"
|
|
27
|
+
|
|
28
|
+
# Check that union equals total (optional, but good for exhaustiveness)
|
|
29
|
+
all_parents = s1Parents | s2Parents | s3Parents
|
|
30
|
+
assert len(all_parents) > 0, "No parents found"
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
@pytest.mark.parametrize("n", [2, 3, 4])
|
|
34
|
+
def test_rule_categorization_consistency(n):
|
|
35
|
+
"""Test that rule categorization is consistent across multiple queries.
|
|
36
|
+
|
|
37
|
+
For several functions, verify that:
|
|
38
|
+
1. Each parent/child belongs to exactly one rule category
|
|
39
|
+
2. Getting the same parent/child again yields the same categorization
|
|
40
|
+
"""
|
|
41
|
+
hd = HasseDiagram(n)
|
|
42
|
+
finf = hd.get_infimum()
|
|
43
|
+
fsup = hd.get_supremum()
|
|
44
|
+
|
|
45
|
+
# Test parents of infimum
|
|
46
|
+
s1P, s2P, s3P = hd.get_f_parents(finf)
|
|
47
|
+
|
|
48
|
+
# Verify partition
|
|
49
|
+
assert len(s1P & s2P) == 0 and len(s2P & s3P) == 0 and len(s1P & s3P) == 0, \
|
|
50
|
+
f"n={n}: Parents not properly partitioned into disjoint sets"
|
|
51
|
+
|
|
52
|
+
# Verify at least one rule is non-empty
|
|
53
|
+
assert len(s1P) + len(s2P) + len(s3P) > 0, \
|
|
54
|
+
f"n={n}: No parents found for infimum"
|
|
55
|
+
|
|
56
|
+
# Test children of supremum
|
|
57
|
+
s1C, s2C, s3C = hd.get_f_children(fsup)
|
|
58
|
+
|
|
59
|
+
# Verify partition
|
|
60
|
+
assert len(s1C & s2C) == 0 and len(s2C & s3C) == 0 and len(s1C & s3C) == 0, \
|
|
61
|
+
f"n={n}: Children not properly partitioned into disjoint sets"
|
|
62
|
+
|
|
63
|
+
# Verify at least one rule is non-empty
|
|
64
|
+
assert len(s1C) + len(s2C) + len(s3C) > 0, \
|
|
65
|
+
f"n={n}: No children found for supremum"
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
@pytest.mark.parametrize("n", [2, 3, 4])
|
|
69
|
+
def test_rule_coverage_across_dimensions(n):
|
|
70
|
+
"""Verify that rules are non-empty in the Hasse diagram.
|
|
71
|
+
|
|
72
|
+
For small to medium dimensions, at least S3 should appear
|
|
73
|
+
(S1 and S2 may require intermediate functions).
|
|
74
|
+
"""
|
|
75
|
+
hd = HasseDiagram(n)
|
|
76
|
+
finf = hd.get_infimum()
|
|
77
|
+
fsup = hd.get_supremum()
|
|
78
|
+
|
|
79
|
+
# Infimum should have S3 parents
|
|
80
|
+
s1P, s2P, s3P = hd.get_f_parents(finf)
|
|
81
|
+
assert len(s3P) > 0, f"n={n}: Infimum should have S3 parents"
|
|
82
|
+
|
|
83
|
+
# Supremum should have S3 children
|
|
84
|
+
s1C, s2C, s3C = hd.get_f_children(fsup)
|
|
85
|
+
assert len(s3C) > 0, f"n={n}: Supremum should have S3 children"
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def test_infimum_supremum_duality(n=4):
|
|
89
|
+
"""Test duality: infimum and supremum should have complementary properties.
|
|
90
|
+
|
|
91
|
+
The infimum has only parents (no parents of parents...).
|
|
92
|
+
The supremum has only children (no children of children...).
|
|
93
|
+
"""
|
|
94
|
+
hd = HasseDiagram(n)
|
|
95
|
+
finf = hd.get_infimum()
|
|
96
|
+
fsup = hd.get_supremum()
|
|
97
|
+
|
|
98
|
+
# Infimum has parents but no children
|
|
99
|
+
s1P, s2P, s3P = hd.get_f_parents(finf)
|
|
100
|
+
s1C, s2C, s3C = hd.get_f_children(finf)
|
|
101
|
+
|
|
102
|
+
assert len(s1P) + len(s2P) + len(s3P) > 0, f"Infimum has no parents (n={n})"
|
|
103
|
+
assert len(s1C) == 0 and len(s2C) == 0 and len(s3C) == 0, \
|
|
104
|
+
f"Infimum should have no children (n={n})"
|
|
105
|
+
|
|
106
|
+
# Supremum has children but no parents
|
|
107
|
+
s1P, s2P, s3P = hd.get_f_parents(fsup)
|
|
108
|
+
s1C, s2C, s3C = hd.get_f_children(fsup)
|
|
109
|
+
|
|
110
|
+
assert len(s1C) + len(s2C) + len(s3C) > 0, f"Supremum has no children (n={n})"
|
|
111
|
+
assert len(s1P) == 0 and len(s2P) == 0 and len(s3P) == 0, \
|
|
112
|
+
f"Supremum should have no parents (n={n})"
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
if __name__ == "__main__":
|
|
116
|
+
# Allow running directly with: python -m pytest tests/test_rule_categorization.py
|
|
117
|
+
pytest.main([__file__, "-v"])
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{pyfunctionhood-0.2.1 → pyfunctionhood-0.3.0}/src/pyfunctionhood.egg-info/dependency_links.txt
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|