-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsupports_beyond_esep.py
343 lines (278 loc) · 17.9 KB
/
supports_beyond_esep.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
from __future__ import absolute_import
import numpy as np
import itertools
from radix import to_digits
from supports import SupportTesting
import methodtools
from sys import hexversion
if hexversion >= 0x3080000:
from functools import cached_property
elif hexversion >= 0x3060000:
# with io.capture_output() as captured:
# !pip
# install
# backports.cached - property
from backports.cached_property import cached_property
else:
cached_property = property
"""
Suppose that we see a d-sep relation of the form (a,b,C).
Then we take a support and partition into into values on C.
We then take every subset of partitions of cardinality strictly less than 2^|C|
Suppose that we see an e-sep relation of the form (a,b,C,D)
"""
def product_support_test(two_column_mat:np. ndarray) -> bool:
# exploits the fact that np.unique also sorts
return np.array_equiv(
list(itertools.product(*map(np.unique, two_column_mat.T))),
# np.fromiter(itertools.product(*map(np.unique, two_column_mat.T)),int).reshape((-1,2)),
np.unique(two_column_mat, axis=0)
)
def does_this_dsep_rule_out_this_support(ab: np.ndarray, C: np.ndarray, s: np.ndarray) -> bool:
if len(C):
s_resorted = s[np.lexsort(np.asarray(s)[:, list(C)].T)]
partitioned = [np.vstack(tuple(g)) for k, g in itertools.groupby(s_resorted, lambda e: tuple(e.take(C)))]
for submat in partitioned:
if not product_support_test(submat[:, list(ab)]):
return True
return False
else:
return not product_support_test(np.asarray(s)[:, list(ab)])
def does_this_esep_rule_out_this_support(ab: np.ndarray, C: np.ndarray, D: np.ndarray, s: np.ndarray) -> bool:
if len(D) == 0:
return does_this_dsep_rule_out_this_support(ab, C, s)
else:
columns_D = np.asarray(s)[:, np.asarray(D)]
if np.array_equiv(columns_D[0], columns_D):
return does_this_dsep_rule_out_this_support(ab, C, s)
else:
return False
class SmartSupportTesting(SupportTesting):
def __init__(self, parents_of, observed_cardinalities, nof_events, esep_relations, **kwargs):
super().__init__(parents_of, observed_cardinalities, nof_events, **kwargs)
self.esep_relations = tuple(esep_relations)
self.dsep_relations = tuple(((ab, C) for (ab, C, D) in self.esep_relations if len(D)==0))
def infeasible_support_Q_due_to_esep_from_matrix(self, candidate_s: np.ndarray) -> bool:
return any(does_this_esep_rule_out_this_support(*map(list, e_sep), candidate_s) for e_sep in self.esep_relations)
def infeasible_support_Q_due_to_dsep_from_matrix(self, candidate_s: np.ndarray)-> bool:
return any(does_this_dsep_rule_out_this_support(*map(list,d_sep), candidate_s) for d_sep in self.dsep_relations)
#REDEFINITION!
def feasibleQ_from_matrix(self, occurring_events: np.ndarray, **kwargs) -> bool:
if not self.infeasible_support_Q_due_to_esep_from_matrix(occurring_events):
return super().feasibleQ_from_matrix(occurring_events, **kwargs)
else:
# return (False, 0) #Timing of zero.
return False
@methodtools.lru_cache(maxsize=None, typed=False)
def subSupportTester(self, n: int):
if n != self.nof_events:
return self.__class__(self.parents_of, self.observed_cardinalities, n, self.esep_relations)
else:
return self
@cached_property
def _infeasible_compressed_supports_due_to_esep_picklist(self) -> np.ndarray:
to_filter = self.from_list_to_matrix(self.unique_candidate_supports_as_compressed_lists)
return np.fromiter(map(self.infeasible_support_Q_due_to_esep_from_matrix, to_filter), bool)
@cached_property
def infeasible_supports_due_to_esep_as_compressed_integers(self) -> np.ndarray:
return np.asarray(self.unique_candidate_supports_as_compressed_integers, dtype=self.int_dtype)[self._infeasible_compressed_supports_due_to_esep_picklist]
@cached_property
def infeasible_supports_due_to_esep_as_expanded_integers(self) -> np.ndarray:
return self.expand_integers_from_canonical_via_internal_party_relabelling(
self.infeasible_supports_due_to_esep_as_compressed_integers)
@cached_property
def _infeasible_compressed_support_due_to_dsep_picklist(self) -> np.ndarray:
to_filter = self.from_list_to_matrix(self.unique_candidate_supports_as_compressed_lists)
return np.fromiter(map(self.infeasible_support_Q_due_to_dsep_from_matrix, to_filter), bool)
@cached_property
def infeasible_supports_due_to_dsep_as_compressed_integers(self) -> np.ndarray:
return np.asarray(self.unique_candidate_supports_as_compressed_integers, dtype=self.int_dtype)[self._infeasible_compressed_support_due_to_dsep_picklist]
@cached_property
def infeasible_supports_due_to_dsep_as_expanded_integers(self) -> np.ndarray:
return self.expand_integers_from_canonical_via_internal_party_relabelling(
self.infeasible_supports_due_to_dsep_as_compressed_integers)
@cached_property
def infeasible_supports_due_to_esep_as_expanded_matrices(self) -> np.ndarray:
return self.from_integer_to_matrix(self.infeasible_supports_due_to_esep_as_expanded_integers)
@cached_property
def infeasible_supports_due_to_esep_as_compressed_matrices(self) -> np.ndarray:
return self.from_integer_to_matrix(self.infeasible_supports_due_to_esep_as_compressed_integers)
@cached_property
def infeasible_supports_due_to_dsep_as_expanded_matrices(self) -> np.ndarray:
return self.from_integer_to_matrix(self.infeasible_supports_due_to_dsep_as_expanded_integers)
@cached_property
def infeasible_supports_due_to_dsep_as_compressed_matrices(self) -> np.ndarray:
return self.from_integer_to_matrix(self.infeasible_supports_due_to_dsep_as_compressed_integers)
@cached_property
def unique_candidate_supports_not_infeasible_due_to_esep_as_compressed_integers(self) -> np.ndarray:
return np.asarray(self.unique_candidate_supports_as_compressed_integers, dtype=self.int_dtype)[
np.logical_not(self._infeasible_compressed_supports_due_to_esep_picklist)]
@cached_property
def unique_candidate_supports_not_infeasible_due_to_dsep_as_compressed_integers(self) -> np.ndarray:
return np.asarray(self.unique_candidate_supports_as_compressed_integers, dtype=self.int_dtype)[
np.logical_not(self._infeasible_compressed_support_due_to_dsep_picklist)]
@cached_property
def unique_candidate_supports_not_infeasible_due_to_esep_as_compressed_lists(self) -> np.ndarray:
return np.asarray(self.unique_candidate_supports_as_compressed_lists, dtype=self.int_dtype)[
np.logical_not(self._infeasible_compressed_supports_due_to_esep_picklist)]
@cached_property
def unique_candidate_supports_not_infeasible_due_to_dsep_as_compressed_lists(self) -> np.ndarray:
return np.asarray(self.unique_candidate_supports_as_compressed_lists, dtype=np.intp)[
np.logical_not(self._infeasible_compressed_support_due_to_dsep_picklist)]
@methodtools.lru_cache(maxsize=None, typed=False)
def unique_infeasible_supports_beyond_esep_as_compressed_integers(self,
**kwargs):
"""
Return infeasible support UP TO INTERNAL SYMMETRY for a given parents_of, observed_cardinalities, and nof_events
"""
return self.unique_infeasible_supports_as_integers_among(
self.unique_candidate_supports_not_infeasible_due_to_esep_as_compressed_integers,
**kwargs)
@methodtools.lru_cache(maxsize=None, typed=False)
def unique_infeasible_supports_beyond_esep_as_expanded_integers(self, **kwargs):
"""
Return a signature of infeasible support for a given parents_of, observed_cardinalities, and nof_events
"""
return self.expand_integers_from_canonical_via_internal_party_relabelling(self.unique_infeasible_supports_beyond_esep_as_compressed_integers(**kwargs))
@methodtools.lru_cache(maxsize=None, typed=False)
def unique_infeasible_supports_beyond_dsep_as_compressed_integers(self,
**kwargs):
return self.unique_infeasible_supports_as_integers_among(
self.unique_candidate_supports_not_infeasible_due_to_dsep_as_compressed_integers,
**kwargs)
@methodtools.lru_cache(maxsize=None, typed=False)
def unique_infeasible_supports_beyond_dsep_as_expanded_integers(self, **kwargs):
return self.expand_integers_from_canonical_via_internal_party_relabelling(self.unique_infeasible_supports_beyond_dsep_as_compressed_integers(**kwargs))
@methodtools.lru_cache(maxsize=None, typed=False)
def unique_infeasible_supports_beyond_esep_as_expanded_matrices(self, **kwargs):
return self.from_integer_to_matrix(self.unique_infeasible_supports_beyond_esep_as_expanded_integers(**kwargs))
@methodtools.lru_cache(maxsize=None, typed=False)
def unique_infeasible_supports_beyond_esep_as_compressed_matrices(self, **kwargs):
return self.from_integer_to_matrix(self.unique_infeasible_supports_beyond_esep_as_compressed_integers(**kwargs))
@methodtools.lru_cache(maxsize=None, typed=False)
def unique_infeasible_supports_beyond_dsep_as_expanded_matrices(self, **kwargs):
return self.from_integer_to_matrix(self.unique_infeasible_supports_beyond_dsep_as_expanded_integers(**kwargs))
@methodtools.lru_cache(maxsize=None, typed=False)
def unique_infeasible_supports_beyond_dsep_as_compressed_matrices(self, **kwargs):
return self.from_integer_to_matrix(self.unique_infeasible_supports_beyond_dsep_as_compressed_integers(**kwargs))
@methodtools.lru_cache(maxsize=None, typed=False)
def unique_infeasible_supports_beyond_esep_as_integers_unlabelled(self, **kwargs):
return self.convert_integers_into_canonical_under_coherent_relabelling(
self.unique_infeasible_supports_beyond_esep_as_expanded_integers(**kwargs))
#REDEFINITION
@methodtools.lru_cache(maxsize=None, typed=False)
def unique_infeasible_supports_as_expanded_integers(self, **kwargs):
"""
Return a signature of infeasible support for a given parents_of, observed_cardinalities, and nof_events
:param kwargs: optional arguments to pysat.Solver
CHANGED: Now returns each infeasible support as a single integer.
"""
return np.sort(np.hstack((self.unique_infeasible_supports_beyond_esep_as_expanded_integers(**kwargs),
self.infeasible_supports_due_to_esep_as_expanded_integers)).astype(self.int_dtype)).astype(self.int_dtype)
@methodtools.lru_cache(maxsize=None, typed=False)
def unique_infeasible_supports_as_compressed_integers(self, **kwargs):
return np.sort(np.hstack((self.unique_infeasible_supports_beyond_esep_as_compressed_integers(**kwargs),
self.infeasible_supports_due_to_esep_as_compressed_integers)).astype(self.int_dtype)).astype(self.int_dtype)
@methodtools.lru_cache(maxsize=None, typed=False)
def unique_infeasible_supports_as_expanded_matrices(self, **kwargs):
return self.from_integer_to_matrix(self.unique_infeasible_supports_as_expanded_integers(**kwargs))
@methodtools.lru_cache(maxsize=None, typed=False)
def unique_infeasible_supports_as_compressed_matrices(self, **kwargs):
return self.from_integer_to_matrix(self.unique_infeasible_supports_as_compressed_integers(**kwargs))
def attempt_to_find_one_infeasible_support_beyond_dsep(self, **kwargs):
return self.attempt_to_find_one_infeasible_support_among(self.unique_candidate_supports_not_infeasible_due_to_dsep_as_compressed_lists, **kwargs)
def attempt_to_find_one_infeasible_support_beyond_esep(self, **kwargs):
return self.attempt_to_find_one_infeasible_support_among(self.unique_candidate_supports_not_infeasible_due_to_esep_as_compressed_lists, **kwargs)
@methodtools.lru_cache(maxsize=None, typed=False)
def no_infeasible_supports_beyond_esep(self, **kwargs):
return self.no_infeasible_supports_among(self.unique_candidate_supports_not_infeasible_due_to_esep_as_compressed_lists, **kwargs)
@methodtools.lru_cache(maxsize=None, typed=False)
def no_infeasible_supports_beyond_dsep(self, **kwargs):
return self.no_infeasible_supports_among(self.unique_candidate_supports_not_infeasible_due_to_dsep_as_compressed_lists,
**kwargs)
@cached_property
def interesting_due_to_esep(self):
return not set(self.unique_candidate_supports_not_infeasible_due_to_dsep_as_compressed_integers).issubset(
self.unique_candidate_supports_not_infeasible_due_to_esep_as_compressed_integers)
if __name__ == '__main__':
from mDAG_advanced import mDAG
from hypergraphs import LabelledHypergraph, Hypergraph
from directed_structures import LabelledDirectedStructure, DirectedStructure
MarinaTest = mDAG(directed_structure_instance=DirectedStructure([], 4),
simplicial_complex_instance=Hypergraph([], 4))
st_instance = MarinaTest.support_testing_instance_binary(2)
result = st_instance.feasibleQ_from_tuple((0, 14))
# print("Result:", result)
# print("Result:", result[0])
print(st_instance.feasibleQ_from_tuple((0, 14)))
print(st_instance.feasibleQ_from_integer(14))
print(st_instance.feasibleQ_from_matrix(np.array([[0,0,0,0],[1,1,1,1]])))
# print(st_instance.no_infeasible_supports())
#
print(does_this_dsep_rule_out_this_support([1,2], [0], np.asarray([[0,0,0,0],[0,1,1,0]])))
print(does_this_dsep_rule_out_this_support([0,3], [1,2], np.asarray([[0, 0, 0, 0], [1, 1, 1, 1]])))
# latent_free1 = mDAG(DirectedStructure([(0, 1), (0, 2), (1, 3), (2, 3)], 4), Hypergraph([], 4))
# print(latent_free1.all_CI)
# print("From dsep:",
# latent_free1.support_testing_instance_binary(2).infeasible_supports_due_to_dsep_as_matrices)
# print("From esep:",
# latent_free1.support_testing_instance_binary(2).infeasible_supports_due_to_esep_as_matrices)
# print(latent_free1.no_esep_beyond_dsep_up_to(2))
#
# latent_free2 = mDAG(DirectedStructure([(0, 3), (1, 3), (2, 3)], 4), Hypergraph([], 4))
# latent_free2.no_infeasible_binary_supports_beyond_dsep_up_to(4)
# from radix import to_bits
# test_two_col_mat = np.asarray([[1, 0], [0, 1], [0, 1], [1, 1]])
# instrumental = mDAG(
# LabelledDirectedStructure([0,1,2],[(0,1),(1,2)]),
# LabelledHypergraph([0,1,2], [(0,),(1,2)]))
# UC = mDAG(LabelledDirectedStructure([0,1,2],[(1,0),(1,2)]),
# LabelledHypergraph([0,1,2],[(0,1),(1,2)]))
#
# print(instrumental.infeasible_binary_supports_n_events(3, verbose=False))
# print(UC.infeasible_binary_supports_n_events(3, verbose=False))
# print(instrumental.infeasible_binary_supports_n_events_beyond_esep(3, verbose=False))
# print(UC.infeasible_binary_supports_n_events_beyond_esep(3, verbose=False))
# print(instrumental.infeasible_binary_supports_n_events(4, verbose=False))
# print(UC.infeasible_binary_supports_n_events(4, verbose=False))
# print(instrumental.infeasible_binary_supports_n_events_beyond_esep(4, verbose=False))
# print(UC.infeasible_binary_supports_n_events_beyond_esep(4, verbose=False))
# print(to_bits(instrumental.infeasible_binary_supports_n_events(3, verbose=False), mantissa=3))
# print(to_bits(UC.infeasible_binary_supports_n_events(3, verbose=False), mantissa=3))
# print(to_bits(instrumental.infeasible_binary_supports_n_events_beyond_esep(3, verbose=False), mantissa=3))
# print(to_bits(UC.infeasible_binary_supports_n_events_beyond_esep(3, verbose=False), mantissa=3))
# print(to_bits(instrumental.infeasible_binary_supports_n_events(4, verbose=False), mantissa=3))
# print(to_bits(UC.infeasible_binary_supports_n_events(4, verbose=False), mantissa=3))
# print(to_bits(instrumental.infeasible_binary_supports_n_events_beyond_esep(4, verbose=False), mantissa=3))
# print(to_bits(UC.infeasible_binary_supports_n_events_beyond_esep(4, verbose=False), mantissa=3))
# print(set(instrumental.infeasible_binary_supports_n_events(4, verbose=False)).difference(
# UC.infeasible_binary_supports_n_events(4, verbose=False)))
# print(set(instrumental.infeasible_binary_supports_n_events_beyond_esep(4, verbose=False)).difference(
# UC.infeasible_binary_supports_n_events_beyond_esep(4, verbose=False)))
s = np.asarray(
[[0, 0, 0, 0, 0],
[1, 1, 0, 1, 0],
[0, 0, 1, 1, 1],
[1, 1, 0, 0, 0],
[1, 1, 1, 1, 1]])
C = [1, 2]
ab = [0, 4]
print(does_this_dsep_rule_out_this_support(ab, C, s))
import networkx as nx
#
# # Testing example for Ilya conjecture proof.
directed_dict_of_lists = {"C": ["A", "B"], "X": ["C"], "D": ["A", "B"]}
ds = LabelledDirectedStructure(["X", "A", "B", "C", "D"], nx.from_dict_of_lists(directed_dict_of_lists, create_using=nx.DiGraph).edges())
simplicial_complex1 = LabelledHypergraph(["X", "A", "B", "C", "D"], [("A", "X", "D"), ("B", "X", "D"), ("A", "C")])
simplicial_complex2 = LabelledHypergraph(["X", "A", "B", "C", "D"], [("A", "X", "D"), ("B", "X", "D")])
md1 = mDAG(ds, simplicial_complex1)
md2 = mDAG(ds, simplicial_complex2)
print("Hashing test: ", hash(simplicial_complex1), hash(ds), hash(md1))
test = set([simplicial_complex1, simplicial_complex2])
print(md2.all_esep)
print(set(md2.infeasible_binary_supports_n_events_beyond_esep(4, verbose=True)).difference(md1.infeasible_binary_supports_n_events_beyond_esep(4, verbose=True)))
print(to_digits(to_digits(9786, np.broadcast_to(2 ** 5, 4)), np.broadcast_to(2, 5)))
# #Cool, it works great.
#Now testing memoization
print(set(md2.infeasible_binary_supports_n_events_beyond_esep_unlabelled(4, verbose=True)))
print(set(md2.infeasible_binary_supports_n_events_unlabelled(4, verbose=True)))