compiled-knowledge 4.0.0a20__cp313-cp313-macosx_11_0_arm64.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.
Potentially problematic release.
This version of compiled-knowledge might be problematic. Click here for more details.
- ck/__init__.py +0 -0
- ck/circuit/__init__.py +17 -0
- ck/circuit/_circuit_cy.c +37525 -0
- ck/circuit/_circuit_cy.cpython-313-darwin.so +0 -0
- ck/circuit/_circuit_cy.pxd +32 -0
- ck/circuit/_circuit_cy.pyx +768 -0
- ck/circuit/_circuit_py.py +836 -0
- ck/circuit/tmp_const.py +74 -0
- ck/circuit_compiler/__init__.py +2 -0
- ck/circuit_compiler/circuit_compiler.py +26 -0
- ck/circuit_compiler/cython_vm_compiler/__init__.py +1 -0
- ck/circuit_compiler/cython_vm_compiler/_compiler.c +19826 -0
- ck/circuit_compiler/cython_vm_compiler/_compiler.cpython-313-darwin.so +0 -0
- ck/circuit_compiler/cython_vm_compiler/_compiler.pyx +380 -0
- ck/circuit_compiler/cython_vm_compiler/cython_vm_compiler.py +121 -0
- ck/circuit_compiler/interpret_compiler.py +223 -0
- ck/circuit_compiler/llvm_compiler.py +388 -0
- ck/circuit_compiler/llvm_vm_compiler.py +546 -0
- ck/circuit_compiler/named_circuit_compilers.py +57 -0
- ck/circuit_compiler/support/__init__.py +0 -0
- ck/circuit_compiler/support/circuit_analyser/__init__.py +13 -0
- ck/circuit_compiler/support/circuit_analyser/_circuit_analyser_cy.c +10620 -0
- ck/circuit_compiler/support/circuit_analyser/_circuit_analyser_cy.cpython-313-darwin.so +0 -0
- ck/circuit_compiler/support/circuit_analyser/_circuit_analyser_cy.pyx +98 -0
- ck/circuit_compiler/support/circuit_analyser/_circuit_analyser_py.py +93 -0
- ck/circuit_compiler/support/input_vars.py +148 -0
- ck/circuit_compiler/support/llvm_ir_function.py +234 -0
- ck/example/__init__.py +53 -0
- ck/example/alarm.py +366 -0
- ck/example/asia.py +28 -0
- ck/example/binary_clique.py +32 -0
- ck/example/bow_tie.py +33 -0
- ck/example/cancer.py +37 -0
- ck/example/chain.py +38 -0
- ck/example/child.py +199 -0
- ck/example/clique.py +33 -0
- ck/example/cnf_pgm.py +39 -0
- ck/example/diamond_square.py +68 -0
- ck/example/earthquake.py +36 -0
- ck/example/empty.py +10 -0
- ck/example/hailfinder.py +539 -0
- ck/example/hepar2.py +628 -0
- ck/example/insurance.py +504 -0
- ck/example/loop.py +40 -0
- ck/example/mildew.py +38161 -0
- ck/example/munin.py +22982 -0
- ck/example/pathfinder.py +53747 -0
- ck/example/rain.py +39 -0
- ck/example/rectangle.py +161 -0
- ck/example/run.py +30 -0
- ck/example/sachs.py +129 -0
- ck/example/sprinkler.py +30 -0
- ck/example/star.py +44 -0
- ck/example/stress.py +64 -0
- ck/example/student.py +43 -0
- ck/example/survey.py +46 -0
- ck/example/triangle_square.py +54 -0
- ck/example/truss.py +49 -0
- ck/in_out/__init__.py +3 -0
- ck/in_out/parse_ace_lmap.py +216 -0
- ck/in_out/parse_ace_nnf.py +322 -0
- ck/in_out/parse_net.py +480 -0
- ck/in_out/parser_utils.py +185 -0
- ck/in_out/pgm_pickle.py +42 -0
- ck/in_out/pgm_python.py +268 -0
- ck/in_out/render_bugs.py +111 -0
- ck/in_out/render_net.py +177 -0
- ck/in_out/render_pomegranate.py +184 -0
- ck/pgm.py +3475 -0
- ck/pgm_circuit/__init__.py +1 -0
- ck/pgm_circuit/marginals_program.py +352 -0
- ck/pgm_circuit/mpe_program.py +237 -0
- ck/pgm_circuit/pgm_circuit.py +79 -0
- ck/pgm_circuit/program_with_slotmap.py +236 -0
- ck/pgm_circuit/slot_map.py +35 -0
- ck/pgm_circuit/support/__init__.py +0 -0
- ck/pgm_circuit/support/compile_circuit.py +83 -0
- ck/pgm_circuit/target_marginals_program.py +103 -0
- ck/pgm_circuit/wmc_program.py +323 -0
- ck/pgm_compiler/__init__.py +2 -0
- ck/pgm_compiler/ace/__init__.py +1 -0
- ck/pgm_compiler/ace/ace.py +299 -0
- ck/pgm_compiler/factor_elimination.py +395 -0
- ck/pgm_compiler/named_pgm_compilers.py +63 -0
- ck/pgm_compiler/pgm_compiler.py +19 -0
- ck/pgm_compiler/recursive_conditioning.py +231 -0
- ck/pgm_compiler/support/__init__.py +0 -0
- ck/pgm_compiler/support/circuit_table/__init__.py +17 -0
- ck/pgm_compiler/support/circuit_table/_circuit_table_cy.c +16398 -0
- ck/pgm_compiler/support/circuit_table/_circuit_table_cy.cpython-313-darwin.so +0 -0
- ck/pgm_compiler/support/circuit_table/_circuit_table_cy.pyx +332 -0
- ck/pgm_compiler/support/circuit_table/_circuit_table_py.py +304 -0
- ck/pgm_compiler/support/clusters.py +568 -0
- ck/pgm_compiler/support/factor_tables.py +406 -0
- ck/pgm_compiler/support/join_tree.py +332 -0
- ck/pgm_compiler/support/named_compiler_maker.py +43 -0
- ck/pgm_compiler/variable_elimination.py +91 -0
- ck/probability/__init__.py +0 -0
- ck/probability/empirical_probability_space.py +50 -0
- ck/probability/pgm_probability_space.py +32 -0
- ck/probability/probability_space.py +622 -0
- ck/program/__init__.py +3 -0
- ck/program/program.py +137 -0
- ck/program/program_buffer.py +180 -0
- ck/program/raw_program.py +67 -0
- ck/sampling/__init__.py +0 -0
- ck/sampling/forward_sampler.py +211 -0
- ck/sampling/marginals_direct_sampler.py +113 -0
- ck/sampling/sampler.py +62 -0
- ck/sampling/sampler_support.py +232 -0
- ck/sampling/uniform_sampler.py +72 -0
- ck/sampling/wmc_direct_sampler.py +171 -0
- ck/sampling/wmc_gibbs_sampler.py +153 -0
- ck/sampling/wmc_metropolis_sampler.py +165 -0
- ck/sampling/wmc_rejection_sampler.py +115 -0
- ck/utils/__init__.py +0 -0
- ck/utils/iter_extras.py +163 -0
- ck/utils/local_config.py +270 -0
- ck/utils/map_list.py +128 -0
- ck/utils/map_set.py +128 -0
- ck/utils/np_extras.py +51 -0
- ck/utils/random_extras.py +64 -0
- ck/utils/tmp_dir.py +94 -0
- ck_demos/__init__.py +0 -0
- ck_demos/ace/__init__.py +0 -0
- ck_demos/ace/copy_ace_to_ck.py +15 -0
- ck_demos/ace/demo_ace.py +49 -0
- ck_demos/all_demos.py +88 -0
- ck_demos/circuit/__init__.py +0 -0
- ck_demos/circuit/demo_circuit_dump.py +22 -0
- ck_demos/circuit/demo_derivatives.py +43 -0
- ck_demos/circuit_compiler/__init__.py +0 -0
- ck_demos/circuit_compiler/compare_circuit_compilers.py +32 -0
- ck_demos/circuit_compiler/show_llvm_program.py +26 -0
- ck_demos/pgm/__init__.py +0 -0
- ck_demos/pgm/demo_pgm_dump.py +18 -0
- ck_demos/pgm/demo_pgm_dump_stress.py +18 -0
- ck_demos/pgm/demo_pgm_string_rendering.py +15 -0
- ck_demos/pgm/show_examples.py +25 -0
- ck_demos/pgm_compiler/__init__.py +0 -0
- ck_demos/pgm_compiler/compare_pgm_compilers.py +63 -0
- ck_demos/pgm_compiler/demo_compiler_dump.py +60 -0
- ck_demos/pgm_compiler/demo_factor_elimination.py +47 -0
- ck_demos/pgm_compiler/demo_join_tree.py +25 -0
- ck_demos/pgm_compiler/demo_marginals_program.py +53 -0
- ck_demos/pgm_compiler/demo_mpe_program.py +55 -0
- ck_demos/pgm_compiler/demo_pgm_compiler.py +38 -0
- ck_demos/pgm_compiler/demo_recursive_conditioning.py +33 -0
- ck_demos/pgm_compiler/demo_variable_elimination.py +33 -0
- ck_demos/pgm_compiler/demo_wmc_program.py +29 -0
- ck_demos/pgm_compiler/time_fe_compiler.py +93 -0
- ck_demos/pgm_inference/__init__.py +0 -0
- ck_demos/pgm_inference/demo_inferencing_basic.py +188 -0
- ck_demos/pgm_inference/demo_inferencing_mpe_cancer.py +45 -0
- ck_demos/pgm_inference/demo_inferencing_wmc_and_mpe_sprinkler.py +154 -0
- ck_demos/pgm_inference/demo_inferencing_wmc_student.py +110 -0
- ck_demos/programs/__init__.py +0 -0
- ck_demos/programs/demo_program_buffer.py +24 -0
- ck_demos/programs/demo_program_multi.py +24 -0
- ck_demos/programs/demo_program_none.py +19 -0
- ck_demos/programs/demo_program_single.py +23 -0
- ck_demos/programs/demo_raw_program_interpreted.py +21 -0
- ck_demos/programs/demo_raw_program_llvm.py +21 -0
- ck_demos/sampling/__init__.py +0 -0
- ck_demos/sampling/check_sampler.py +71 -0
- ck_demos/sampling/demo_marginal_direct_sampler.py +40 -0
- ck_demos/sampling/demo_uniform_sampler.py +38 -0
- ck_demos/sampling/demo_wmc_direct_sampler.py +40 -0
- ck_demos/utils/__init__.py +0 -0
- ck_demos/utils/compare.py +120 -0
- ck_demos/utils/convert_network.py +45 -0
- ck_demos/utils/sample_model.py +216 -0
- ck_demos/utils/stop_watch.py +384 -0
- compiled_knowledge-4.0.0a20.dist-info/METADATA +50 -0
- compiled_knowledge-4.0.0a20.dist-info/RECORD +178 -0
- compiled_knowledge-4.0.0a20.dist-info/WHEEL +6 -0
- compiled_knowledge-4.0.0a20.dist-info/licenses/LICENSE.txt +21 -0
- compiled_knowledge-4.0.0a20.dist-info/top_level.txt +2 -0
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import pathlib as _pathlib
|
|
2
|
+
import sys as _sys
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
from typing import Optional
|
|
5
|
+
|
|
6
|
+
from ck.pgm import RandomVariable, Factor, PGM
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def render_bayesian_network(
|
|
10
|
+
pgm: PGM,
|
|
11
|
+
out=None,
|
|
12
|
+
*,
|
|
13
|
+
network_name: str = 'network',
|
|
14
|
+
pom: str = 'pom',
|
|
15
|
+
use_variable_names: bool = False,
|
|
16
|
+
bake: bool = True,
|
|
17
|
+
check_structure_bayesian: bool = True,
|
|
18
|
+
) -> None:
|
|
19
|
+
"""
|
|
20
|
+
Render a Bayesian network PGM to 'out' as a pomegranate Python script.
|
|
21
|
+
|
|
22
|
+
Args:
|
|
23
|
+
pgm: is a PGM object.
|
|
24
|
+
out: is an output file or None for stdout.
|
|
25
|
+
network_name: A Python variable name to use for the pomegranate object.
|
|
26
|
+
pom: is the Python import name for pomegranate.
|
|
27
|
+
use_variable_names: If false, then Python variable names for distribution objects will
|
|
28
|
+
be system generated, otherwise the random variable name will be used.
|
|
29
|
+
bake: If True, then {network_name}.bake() is appended to the script.
|
|
30
|
+
check_structure_bayesian: If True, then raise an exception if not pgm.is_structure_bayesian.
|
|
31
|
+
"""
|
|
32
|
+
if check_structure_bayesian:
|
|
33
|
+
if not pgm.is_structure_bayesian:
|
|
34
|
+
raise RuntimeError('Attempting to render a PGM with non-Bayesian structure')
|
|
35
|
+
|
|
36
|
+
if out is None:
|
|
37
|
+
_render_bayesian_network(pgm, _sys.stdout, network_name, pom, use_variable_names, bake)
|
|
38
|
+
elif isinstance(out, (str, _pathlib.Path)):
|
|
39
|
+
with open(out, 'w') as file:
|
|
40
|
+
_render_bayesian_network(pgm, file, network_name, pom, use_variable_names, bake)
|
|
41
|
+
else:
|
|
42
|
+
_render_bayesian_network(pgm, out, network_name, pom, use_variable_names, bake)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
# ============================================================
|
|
46
|
+
# Private support
|
|
47
|
+
# ============================================================
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
@dataclass
|
|
51
|
+
class _RVData:
|
|
52
|
+
rv: RandomVariable
|
|
53
|
+
cpt_name: str
|
|
54
|
+
factor: Optional[Factor] = None
|
|
55
|
+
wrote_cpt: bool = False
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def _render_bayesian_network(
|
|
59
|
+
pgm: PGM,
|
|
60
|
+
out,
|
|
61
|
+
network_name,
|
|
62
|
+
pom,
|
|
63
|
+
use_variable_names,
|
|
64
|
+
bake
|
|
65
|
+
):
|
|
66
|
+
"""
|
|
67
|
+
See render_bayesian_network.
|
|
68
|
+
"""
|
|
69
|
+
if use_variable_names:
|
|
70
|
+
rv_data_lookup = {rv: _RVData(rv, rv.name) for rv in pgm.rvs}
|
|
71
|
+
else:
|
|
72
|
+
rv_data_lookup = {rv: _RVData(rv, f'cpt_{i}') for i, rv in enumerate(pgm.rvs)}
|
|
73
|
+
|
|
74
|
+
def write(*args, sep=' ', end='\n'):
|
|
75
|
+
out.write(sep.join(str(arg) for arg in args))
|
|
76
|
+
out.write(end)
|
|
77
|
+
|
|
78
|
+
for factor in pgm.factors:
|
|
79
|
+
child = factor.rvs[0]
|
|
80
|
+
rv_data = rv_data_lookup.get(child)
|
|
81
|
+
if rv_data is None:
|
|
82
|
+
raise RuntimeError(f'lost random variable in factors: {child}')
|
|
83
|
+
if rv_data.factor is not None:
|
|
84
|
+
raise RuntimeError(f'duplicated child random variable in factors: {child}')
|
|
85
|
+
rv_data.factor = factor
|
|
86
|
+
|
|
87
|
+
write(f'import pomegranate as {pom}')
|
|
88
|
+
write()
|
|
89
|
+
|
|
90
|
+
for rv in pgm.rvs:
|
|
91
|
+
_write_cpt_r(rv, rv_data_lookup, pom, write)
|
|
92
|
+
|
|
93
|
+
write()
|
|
94
|
+
for rv in pgm.rvs:
|
|
95
|
+
_write_state(rv, rv_data_lookup, pom, write)
|
|
96
|
+
|
|
97
|
+
write()
|
|
98
|
+
write(f'{network_name} = {pom}.BayesianNetwork({pgm.name!r})')
|
|
99
|
+
|
|
100
|
+
write()
|
|
101
|
+
for rv in pgm.rvs:
|
|
102
|
+
write(f'{network_name}.add_nodes(s{rv.idx})')
|
|
103
|
+
|
|
104
|
+
write()
|
|
105
|
+
for rv in pgm.rvs:
|
|
106
|
+
_write_add_edges(rv, rv_data_lookup, network_name, write)
|
|
107
|
+
|
|
108
|
+
if bake:
|
|
109
|
+
write()
|
|
110
|
+
write(f'{network_name}.bake()')
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def _write_cpt_r(rv, rv_data_lookup, pom, write):
|
|
114
|
+
"""
|
|
115
|
+
Recursively write CPT for the given random variable
|
|
116
|
+
"""
|
|
117
|
+
rv_data = rv_data_lookup[rv]
|
|
118
|
+
if rv_data.wrote_cpt:
|
|
119
|
+
return
|
|
120
|
+
|
|
121
|
+
factor = rv_data.factor
|
|
122
|
+
if factor is None:
|
|
123
|
+
raise RuntimeError(f'Missing random variable factor: {rv}')
|
|
124
|
+
|
|
125
|
+
# first ensure the parent CPTs are written
|
|
126
|
+
for parent in factor.rvs[1:]:
|
|
127
|
+
_write_cpt_r(parent, rv_data_lookup, pom, write)
|
|
128
|
+
|
|
129
|
+
# now write our own CPT
|
|
130
|
+
write()
|
|
131
|
+
_write_cpt(rv, rv_data_lookup, pom, write)
|
|
132
|
+
rv_data.wrote_cpt = True
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def _write_cpt(rv, rv_data_lookup, pom, write):
|
|
136
|
+
"""
|
|
137
|
+
Write the CPT for one random variable.
|
|
138
|
+
This with either write a pomegranate
|
|
139
|
+
DiscreteDistribution or a ConditionalProbabilityTable.
|
|
140
|
+
"""
|
|
141
|
+
rv_data = rv_data_lookup[rv]
|
|
142
|
+
cpt_name = rv_data.cpt_name
|
|
143
|
+
factor = rv_data.factor
|
|
144
|
+
function = factor.function
|
|
145
|
+
parents = factor.rvs[1:]
|
|
146
|
+
|
|
147
|
+
if len(parents) == 0:
|
|
148
|
+
write(f'{cpt_name} = {pom}.DiscreteDistribution({{')
|
|
149
|
+
for i, state in enumerate(rv_data.rv.states):
|
|
150
|
+
value = function[i]
|
|
151
|
+
write(f'{state!r}: {value},')
|
|
152
|
+
write(f'}})')
|
|
153
|
+
else:
|
|
154
|
+
write(f'{cpt_name} = {pom}.ConditionalProbabilityTable([')
|
|
155
|
+
state_name_lookup = [rv.states for rv in factor.rvs]
|
|
156
|
+
for inst in factor.instances():
|
|
157
|
+
value = function[inst]
|
|
158
|
+
state_names = ', '.join(repr(states[i]) for i, states in zip(inst, state_name_lookup))
|
|
159
|
+
write(f' [{state_names}, {value}],')
|
|
160
|
+
write(f'], [')
|
|
161
|
+
for p in parents:
|
|
162
|
+
write(f' {rv_data_lookup[p].cpt_name},')
|
|
163
|
+
write('])')
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
def _write_state(rv, rv_data_lookup, pom, write):
|
|
167
|
+
rv_data: _RVData = rv_data_lookup[rv]
|
|
168
|
+
rv_idx = rv.idx
|
|
169
|
+
cpt_name = rv_data.cpt_name
|
|
170
|
+
rv_name = rv.name
|
|
171
|
+
write(f's{rv_idx} = {pom}.State({cpt_name}, name={rv_name!r})')
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def _write_add_edges(rv, rv_data_lookup, network_name, write):
|
|
175
|
+
rv_data = rv_data_lookup[rv]
|
|
176
|
+
factor = rv_data.factor
|
|
177
|
+
parents = factor.rvs[1:]
|
|
178
|
+
rv_idx = rv.idx
|
|
179
|
+
|
|
180
|
+
if parents == 0:
|
|
181
|
+
return
|
|
182
|
+
|
|
183
|
+
for parent in parents:
|
|
184
|
+
write(f'{network_name}.add_edge(s{parent.idx}, s{rv_idx})')
|