pyfunctionhood 0.0.0__py3-none-any.whl

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.
@@ -0,0 +1,82 @@
1
+ from .clause import Clause
2
+ from typing import Set
3
+ from collections import defaultdict
4
+
5
+ class PowerSet:
6
+ def __init__(self, nvars: int) -> None:
7
+ self.nvars = nvars
8
+ self.subset_clauses = defaultdict(set)
9
+ self.superset_clauses = defaultdict(set)
10
+ # useful to compute the independent clauses
11
+ self.all = set()
12
+ # initialize the graph with the most specific clause
13
+ c = Clause('1'*nvars)
14
+ self.superset_clauses[c] = set()
15
+ self.build_graph(c)
16
+
17
+ def build_graph(self, c_superset: 'Clause') -> None:
18
+ if c_superset not in self.subset_clauses:
19
+ self.subset_clauses[c_superset] = set()
20
+ if c_superset in self.all:
21
+ return
22
+ self.all.add(c_superset)
23
+ if c_superset.get_order() <= 1:
24
+ return
25
+ for c_subset in c_superset.get_subsets():
26
+ if c_subset not in self.superset_clauses:
27
+ self.superset_clauses[c_subset] = set()
28
+ s_tmp = self.subset_clauses[c_superset]
29
+ s_tmp.add(c_subset)
30
+ self.subset_clauses[c_superset] = s_tmp
31
+ s_tmp = self.superset_clauses[c_subset]
32
+ s_tmp.add(c_superset)
33
+ self.superset_clauses[c_subset] = s_tmp
34
+ self.build_graph(c_subset)
35
+
36
+ def get_dominated_directly(self, s_clauses: Set['Clause']) -> Set['Clause']:
37
+ s_dominated = set()
38
+ for c in s_clauses:
39
+ s_dominated.update(self.subset_clauses.get(c, set()))
40
+ return s_dominated
41
+
42
+ def get_dominant_directly(self, s_clauses: Set['Clause']) -> Set['Clause']:
43
+ s_dominant = set()
44
+ for c in s_clauses:
45
+ s_dominant.update(self.superset_clauses.get(c, set()))
46
+ return s_dominant
47
+
48
+ def get_dominated_recursively(self, c: 'Clause', s_seen: Set['Clause']) -> Set['Clause']:
49
+ for c_subset in self.subset_clauses.get(c, set()):
50
+ if c_subset not in s_seen:
51
+ s_seen.add(c_subset)
52
+ s_seen.update(self.get_dominated_recursively(c_subset, s_seen))
53
+ return s_seen
54
+
55
+ def get_dominant_recursively(self, c: 'Clause', s_seen: Set['Clause']) -> Set['Clause']:
56
+ for c_superset in self.superset_clauses.get(c, set()):
57
+ if c_superset not in s_seen:
58
+ s_seen.add(c_superset)
59
+ s_seen.update(self.get_dominant_recursively(c_superset, s_seen))
60
+ return s_seen
61
+
62
+ def get_independent(self, clauses: Set['Clause']) -> Set['Clause']:
63
+ if not clauses: # If the set is empty
64
+ return { Clause('1'*self.nvars) } # return the maximal clause
65
+ dep_set = set()
66
+ for c in clauses:
67
+ dep_set.update(self.get_dominated_recursively(c, dep_set))
68
+ dep_set.update(self.get_dominant_recursively(c, dep_set))
69
+ s_diff = self.all - clauses - dep_set
70
+ return s_diff
71
+
72
+ def get_minimal(self, s_clauses: Set['Clause']) -> Set['Clause']:
73
+ return { s1 for s1 in s_clauses if not any(s2 < s1 for s2 in s_clauses) }
74
+
75
+ def get_maximal(self, s_clauses: Set['Clause']) -> Set['Clause']:
76
+ return { s1 for s1 in s_clauses if not any(s2 > s1 for s2 in s_clauses) }
77
+
78
+ def __str__(self) -> str:
79
+ s = ""
80
+ for c in self.all:
81
+ s += f"{c}\tSuper: {self.superset_clauses.get(c, set())}\tSub: {self.subset_clauses.get(c, set())}\n"
82
+ return s
@@ -0,0 +1,182 @@
1
+ Metadata-Version: 2.4
2
+ Name: pyfunctionhood
3
+ Version: 0.0.0
4
+ Summary: Local computation of immediate neighbours of a given monotone non-degenerate Boolean function
5
+ Author-email: "Pedro T. Monteiro" <Pedro.Tiago.Monteiro@tecnico.ulisboa.pt>
6
+ License: AGPL-3.0
7
+ Project-URL: Homepage, https://github.com/ptgm/pyfunctionhood
8
+ Project-URL: Repository, https://github.com/ptgm/pyfunctionhood
9
+ Keywords: boolean,monotone,hasse-diagram,functions,bitarray
10
+ Classifier: Programming Language :: Python
11
+ Classifier: Programming Language :: Python :: 3
12
+ Classifier: Programming Language :: Python :: 3.9
13
+ Classifier: Programming Language :: Python :: 3.10
14
+ Classifier: Programming Language :: Python :: 3.11
15
+ Classifier: Programming Language :: Python :: 3.12
16
+ Classifier: Programming Language :: Python :: 3.13
17
+ Classifier: Operating System :: OS Independent
18
+ Classifier: License :: OSI Approved :: GNU Affero General Public License v3
19
+ Classifier: Intended Audience :: Science/Research
20
+ Classifier: Topic :: Scientific/Engineering :: Mathematics
21
+ Requires-Python: >=3.9
22
+ Description-Content-Type: text/markdown
23
+ License-File: LICENSE
24
+ Requires-Dist: bitarray
25
+ Dynamic: license-file
26
+
27
+ pyFunctionHood: local computation of immediate neighbours of a given monotone non-degenerate Boolean function
28
+ =================================================================================
29
+
30
+ This Python library implements the set of rules described in [arxiv:2407.01337](https://arxiv.org/abs/2407.01337), to compute the immediate neighbours of a given monotone non-degenerate Boolean function, without the need to generate the whole function space.
31
+
32
+ Here, immediate neighbours, are the monotone Boolean functions that are immediately above (or below) with respect to the partial order, in other words, are the functions that add/remove the minimum number of entries in the truth table.
33
+
34
+ Monotone non-degenerate Boolean functions are represented as sets of clauses, where each clause is represented by the bitarray data structure, and each bit represents the presence/absence of a given variable/regulator.
35
+
36
+ This work is developed in Python in [https://github.com/ptgm/pyfunctionhood/](https://github.com/ptgm/pyfunctionhood/), correcting and extending our previous work developed in Java in [https://github.com/ptgm/functionhood/](https://github.com/ptgm/functionhood/) and described in [arxiv:1901.07623](https://arxiv.org/abs/1901.07623).
37
+
38
+ Given a reference monotone Boolean function, this library can be used in three distinct manners:
39
+ - in the command line passing the reference function as an argument
40
+ - using a graphical interface developed with Tkinter.
41
+ - as a library integrated in other tools
42
+
43
+ ---
44
+
45
+ Usage - command line
46
+ --------------------
47
+ ```
48
+ Usage example: python main.py <type> <dimension> <function> [<changed_signs>]
49
+ <type>: [p]arents or [c]hildren
50
+ <dimension>: 4
51
+ <function>: "{{1,2,3},{1,3,4},{2,3,4}}"
52
+ <changed_signs>: optional, e.g. "0100" (bit i=1 means variable i switched sign)
53
+ ```
54
+
55
+ **Parents**
56
+
57
+ To compute the parents (the functions immediately above) of a given monotone Boolean function, on can call:
58
+ ```
59
+ python main.py p 4 "{{1,2,3},{1,3,4},{2,3,4}}"
60
+ R1 {{1,2,3},{1,2,4},{1,3,4},{2,3,4}}
61
+ R2 {{1,3},{2,3,4}}
62
+ R2 {{1,2,3},{3,4}}
63
+ R2 {{1,3,4},{2,3}}
64
+ ```
65
+ which yields one parent function generated by Rule 1 (adding a maximal independent clause), and three parent functions generated by Rule 2 (collapsing pairs of clauses).
66
+
67
+ **Children**
68
+
69
+ To compute the children (the functions immediately below) of a given monotone Boolean function, one just change the first parameter:
70
+ ```
71
+ python main.py c 4 "{{1,2,3},{1,3,4},{2,3,4}}"
72
+ R1 {{1,3,4},{2,3,4}}
73
+ R1 {{1,2,3},{1,3,4}}
74
+ R1 {{1,2,3},{2,3,4}}
75
+ ```
76
+
77
+ **Sign changes**
78
+
79
+ Commands accept an optional trailing `<changed_signs>` argument: a bitstring of the same length as `<dimension>`, where bit `i=1` means variable/regulator `i` switched sign (0->1 or 1->0) with respect to the reference topology. When provided (and not all zeros), parents/children are computed considering that change of signs instead of the default fixed-topology rules:
80
+ ```
81
+ python main.py p 4 "{{1,2,3},{1,3,4},{2,3,4}}" 0100
82
+ python main.py c 4 "{{1,2,3},{1,3,4},{2,3,4}}" 0100
83
+ ```
84
+ When omitted (or all zeros), the behaviour is unchanged from above, i.e. fixed-topology.
85
+
86
+ ---
87
+
88
+ Usage - Graphical interface
89
+ ---------------------------
90
+
91
+ There is also a graphical interface using Tkinter where the user can define the function dimension and write the desired monotone Boolean function.
92
+
93
+ ![Screenshot of main window](img/pyfunctionhood.png)
94
+
95
+ The interface has a button to automatically write the Infimum function of the chosen dimension, and another for the Supremum function.
96
+
97
+ A **Changed signs** text box is also available to optionally specify a bitstring of the same length as the dimension, where bit `i=1` means variable/regulator `i` switched sign. Leaving it empty (the default) keeps the fixed-topology behaviour.
98
+
99
+ The user has two buttons to generate the immediate neighbouring functions: **Generate Parents** to generate the list of parent functions, and **Generate Children** to generate the list of children functions.
100
+
101
+ In the text area the immediate neighbouring functions are shown, as well as any error if an invalid function is provided.
102
+
103
+ The button **Default values** cleans the text area and resets the input text boxes to their default values.
104
+
105
+ The button **Close** closes the application.
106
+
107
+ ---
108
+
109
+ Usage - programmatically
110
+ ------------------------
111
+
112
+ If you are using this library directly you should first initialise the Hasse Diagram with a given dimension as a parameter (example below considers monotone Boolean functions of **4** variables/regulators):
113
+ ```
114
+ from hassediagram import *
115
+ hd = HasseDiagram(4)
116
+ ```
117
+
118
+ Then to initialise a function one can use a set of Clauses, where each Clause is a String representing the regulators that are present ("1") or absent ("0"), of dimension **4**:
119
+ ```
120
+ f = Function(4, {Clause('1110'), Clause('1011'), Clause('0111')})
121
+ ```
122
+ or equivalently constructing the Function from its String representation:
123
+ ```
124
+ f = Function.fromString(4, "{{1,2,3},{1,3,4},{2,3,4}}")
125
+ ```
126
+
127
+ Once the Hasse Diagram and the function are defined, the method `hd.get_f_parents(f)` or `hd.get_f_children(f)` can be called, each returning a tuple of three sets of functions.
128
+ The functions generated by Rule 1, by Rule 2 and by Rule 3, respectively.
129
+ Below an example for parent functions generation:
130
+ ```
131
+ s1, s2, s3 = hd.get_f_parents(f)
132
+ print(f'# parents from Rule1:{len(s1)} Rule2:{len(s2)} Rule3:{len(s3)}')
133
+ print('\n'.join(['R1 ' + str(f) for f in s1]\
134
+ + ['R2 ' + str(f) for f in s2]\
135
+ + ['R3 ' + str(f) for f in s3]))
136
+ ```
137
+
138
+ **Sign changes**
139
+
140
+ To compute parents/children considering that some variables/regulators switched sign, use `hd.get_f_parents_with_sign_changes(f, changed_signs)` or `hd.get_f_children_with_sign_changes(f, changed_signs)`, where `changed_signs` is a `bitarray` of the same size as the dimension, with bit `i` set to `True` if variable `i` switched sign:
141
+ ```
142
+ from bitarray import bitarray
143
+ changed_signs = bitarray('0100')
144
+ s1, s2, s3 = hd.get_f_parents_with_sign_changes(f, changed_signs)
145
+ ```
146
+
147
+ ---
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
+
165
+ License
166
+ -------
167
+
168
+ This code is available under GPL-3.0.
169
+
170
+ Cite
171
+ ----
172
+
173
+ This work is available on [arxiv:2407.01337](https://arxiv.org/abs/2407.01337).
174
+
175
+ Authors
176
+ -------
177
+
178
+ - Patrícia Roxo
179
+ - José E. R. Cury
180
+ - Vasco Manquinho
181
+ - Claudine Chaouiya
182
+ - Pedro T. Monteiro
@@ -0,0 +1,12 @@
1
+ pyfunctionhood/__init__.py,sha256=OE92UysyneS395E4FgW6-Ptef5UCz0krMzhLaeCYGdE,231
2
+ pyfunctionhood/clause.py,sha256=6ZJZBRsdnv-V1lx3-PTptUbYVuT1r_4oK9orSAxnB9k,4296
3
+ pyfunctionhood/function.py,sha256=WE3Q7S8fyrPkv1nFEs2V9FITEytqe0hKizpufekE3Ok,3557
4
+ pyfunctionhood/hassediagram.py,sha256=r0272GUafMDK_mN-wnXfb63em5GVKbiJSSxgjXMrueI,8344
5
+ pyfunctionhood/main.py,sha256=KeNLZQHlr8xjvQl4oniYf3B5OPXq5wQ_SfbvFQ7LG_U,8702
6
+ pyfunctionhood/powerset.py,sha256=MeFR7emOpo6La8sND7x9NsA9y1FP9cq034l_InwVIIk,3380
7
+ pyfunctionhood-0.0.0.dist-info/licenses/LICENSE,sha256=hIahDEOTzuHCU5J2nd07LWwkLW7Hko4UFO__ffsvB-8,34523
8
+ pyfunctionhood-0.0.0.dist-info/METADATA,sha256=2i2TelFX9vCjXVp9CPZiTOxppHLD-iyrZ4OgbuRw8H0,8105
9
+ pyfunctionhood-0.0.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
10
+ pyfunctionhood-0.0.0.dist-info/entry_points.txt,sha256=mxn9BOjwtlL0Dix8-t6gNtlZf6QM4oDyQkVTTFS_xc4,60
11
+ pyfunctionhood-0.0.0.dist-info/top_level.txt,sha256=_QWqurgqCz1zIh4kzR7yJfbIBQKnnBlKkx4axyVqrOA,15
12
+ pyfunctionhood-0.0.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (84.2.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ pyfunctionhood = pyfunctionhood.main:main