compiled-knowledge 4.0.0a5__cp313-cp313-macosx_10_13_universal2.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.

Files changed (167) hide show
  1. ck/__init__.py +0 -0
  2. ck/circuit/__init__.py +13 -0
  3. ck/circuit/circuit.c +38749 -0
  4. ck/circuit/circuit.cpython-313-darwin.so +0 -0
  5. ck/circuit/circuit_py.py +807 -0
  6. ck/circuit/tmp_const.py +74 -0
  7. ck/circuit_compiler/__init__.py +2 -0
  8. ck/circuit_compiler/circuit_compiler.py +26 -0
  9. ck/circuit_compiler/cython_vm_compiler/__init__.py +1 -0
  10. ck/circuit_compiler/cython_vm_compiler/_compiler.c +17373 -0
  11. ck/circuit_compiler/cython_vm_compiler/_compiler.cpython-313-darwin.so +0 -0
  12. ck/circuit_compiler/cython_vm_compiler/cython_vm_compiler.py +96 -0
  13. ck/circuit_compiler/interpret_compiler.py +223 -0
  14. ck/circuit_compiler/llvm_compiler.py +388 -0
  15. ck/circuit_compiler/llvm_vm_compiler.py +546 -0
  16. ck/circuit_compiler/named_circuit_compilers.py +57 -0
  17. ck/circuit_compiler/support/__init__.py +0 -0
  18. ck/circuit_compiler/support/circuit_analyser.py +81 -0
  19. ck/circuit_compiler/support/input_vars.py +148 -0
  20. ck/circuit_compiler/support/llvm_ir_function.py +234 -0
  21. ck/example/__init__.py +53 -0
  22. ck/example/alarm.py +366 -0
  23. ck/example/asia.py +28 -0
  24. ck/example/binary_clique.py +32 -0
  25. ck/example/bow_tie.py +33 -0
  26. ck/example/cancer.py +37 -0
  27. ck/example/chain.py +38 -0
  28. ck/example/child.py +199 -0
  29. ck/example/clique.py +33 -0
  30. ck/example/cnf_pgm.py +39 -0
  31. ck/example/diamond_square.py +68 -0
  32. ck/example/earthquake.py +36 -0
  33. ck/example/empty.py +10 -0
  34. ck/example/hailfinder.py +539 -0
  35. ck/example/hepar2.py +628 -0
  36. ck/example/insurance.py +504 -0
  37. ck/example/loop.py +40 -0
  38. ck/example/mildew.py +38161 -0
  39. ck/example/munin.py +22982 -0
  40. ck/example/pathfinder.py +53674 -0
  41. ck/example/rain.py +39 -0
  42. ck/example/rectangle.py +161 -0
  43. ck/example/run.py +30 -0
  44. ck/example/sachs.py +129 -0
  45. ck/example/sprinkler.py +30 -0
  46. ck/example/star.py +44 -0
  47. ck/example/stress.py +64 -0
  48. ck/example/student.py +43 -0
  49. ck/example/survey.py +46 -0
  50. ck/example/triangle_square.py +54 -0
  51. ck/example/truss.py +49 -0
  52. ck/in_out/__init__.py +3 -0
  53. ck/in_out/parse_ace_lmap.py +216 -0
  54. ck/in_out/parse_ace_nnf.py +288 -0
  55. ck/in_out/parse_net.py +480 -0
  56. ck/in_out/parser_utils.py +185 -0
  57. ck/in_out/pgm_pickle.py +42 -0
  58. ck/in_out/pgm_python.py +268 -0
  59. ck/in_out/render_bugs.py +111 -0
  60. ck/in_out/render_net.py +177 -0
  61. ck/in_out/render_pomegranate.py +184 -0
  62. ck/pgm.py +3494 -0
  63. ck/pgm_circuit/__init__.py +1 -0
  64. ck/pgm_circuit/marginals_program.py +352 -0
  65. ck/pgm_circuit/mpe_program.py +237 -0
  66. ck/pgm_circuit/pgm_circuit.py +75 -0
  67. ck/pgm_circuit/program_with_slotmap.py +234 -0
  68. ck/pgm_circuit/slot_map.py +35 -0
  69. ck/pgm_circuit/support/__init__.py +0 -0
  70. ck/pgm_circuit/support/compile_circuit.py +83 -0
  71. ck/pgm_circuit/target_marginals_program.py +103 -0
  72. ck/pgm_circuit/wmc_program.py +323 -0
  73. ck/pgm_compiler/__init__.py +2 -0
  74. ck/pgm_compiler/ace/__init__.py +1 -0
  75. ck/pgm_compiler/ace/ace.py +252 -0
  76. ck/pgm_compiler/factor_elimination.py +383 -0
  77. ck/pgm_compiler/named_pgm_compilers.py +63 -0
  78. ck/pgm_compiler/pgm_compiler.py +19 -0
  79. ck/pgm_compiler/recursive_conditioning.py +226 -0
  80. ck/pgm_compiler/support/__init__.py +0 -0
  81. ck/pgm_compiler/support/circuit_table/__init__.py +9 -0
  82. ck/pgm_compiler/support/circuit_table/circuit_table.c +16042 -0
  83. ck/pgm_compiler/support/circuit_table/circuit_table.cpython-313-darwin.so +0 -0
  84. ck/pgm_compiler/support/circuit_table/circuit_table_py.py +269 -0
  85. ck/pgm_compiler/support/clusters.py +556 -0
  86. ck/pgm_compiler/support/factor_tables.py +398 -0
  87. ck/pgm_compiler/support/join_tree.py +275 -0
  88. ck/pgm_compiler/support/named_compiler_maker.py +33 -0
  89. ck/pgm_compiler/variable_elimination.py +89 -0
  90. ck/probability/__init__.py +0 -0
  91. ck/probability/empirical_probability_space.py +47 -0
  92. ck/probability/probability_space.py +568 -0
  93. ck/program/__init__.py +3 -0
  94. ck/program/program.py +129 -0
  95. ck/program/program_buffer.py +180 -0
  96. ck/program/raw_program.py +61 -0
  97. ck/sampling/__init__.py +0 -0
  98. ck/sampling/forward_sampler.py +211 -0
  99. ck/sampling/marginals_direct_sampler.py +113 -0
  100. ck/sampling/sampler.py +62 -0
  101. ck/sampling/sampler_support.py +232 -0
  102. ck/sampling/uniform_sampler.py +66 -0
  103. ck/sampling/wmc_direct_sampler.py +169 -0
  104. ck/sampling/wmc_gibbs_sampler.py +147 -0
  105. ck/sampling/wmc_metropolis_sampler.py +159 -0
  106. ck/sampling/wmc_rejection_sampler.py +113 -0
  107. ck/utils/__init__.py +0 -0
  108. ck/utils/iter_extras.py +153 -0
  109. ck/utils/map_list.py +128 -0
  110. ck/utils/map_set.py +128 -0
  111. ck/utils/np_extras.py +51 -0
  112. ck/utils/random_extras.py +64 -0
  113. ck/utils/tmp_dir.py +94 -0
  114. ck_demos/__init__.py +0 -0
  115. ck_demos/ace/__init__.py +0 -0
  116. ck_demos/ace/copy_ace_to_ck.py +15 -0
  117. ck_demos/ace/demo_ace.py +44 -0
  118. ck_demos/all_demos.py +88 -0
  119. ck_demos/circuit/__init__.py +0 -0
  120. ck_demos/circuit/demo_circuit_dump.py +22 -0
  121. ck_demos/circuit/demo_derivatives.py +43 -0
  122. ck_demos/circuit_compiler/__init__.py +0 -0
  123. ck_demos/circuit_compiler/compare_circuit_compilers.py +32 -0
  124. ck_demos/circuit_compiler/show_llvm_program.py +26 -0
  125. ck_demos/pgm/__init__.py +0 -0
  126. ck_demos/pgm/demo_pgm_dump.py +18 -0
  127. ck_demos/pgm/demo_pgm_dump_stress.py +18 -0
  128. ck_demos/pgm/demo_pgm_string_rendering.py +15 -0
  129. ck_demos/pgm/show_examples.py +25 -0
  130. ck_demos/pgm_compiler/__init__.py +0 -0
  131. ck_demos/pgm_compiler/compare_pgm_compilers.py +50 -0
  132. ck_demos/pgm_compiler/demo_compiler_dump.py +50 -0
  133. ck_demos/pgm_compiler/demo_factor_elimination.py +47 -0
  134. ck_demos/pgm_compiler/demo_join_tree.py +25 -0
  135. ck_demos/pgm_compiler/demo_marginals_program.py +53 -0
  136. ck_demos/pgm_compiler/demo_mpe_program.py +55 -0
  137. ck_demos/pgm_compiler/demo_pgm_compiler.py +38 -0
  138. ck_demos/pgm_compiler/demo_recursive_conditioning.py +33 -0
  139. ck_demos/pgm_compiler/demo_variable_elimination.py +33 -0
  140. ck_demos/pgm_compiler/demo_wmc_program.py +29 -0
  141. ck_demos/pgm_inference/__init__.py +0 -0
  142. ck_demos/pgm_inference/demo_inferencing_basic.py +188 -0
  143. ck_demos/pgm_inference/demo_inferencing_mpe_cancer.py +45 -0
  144. ck_demos/pgm_inference/demo_inferencing_wmc_and_mpe_sprinkler.py +154 -0
  145. ck_demos/pgm_inference/demo_inferencing_wmc_student.py +110 -0
  146. ck_demos/programs/__init__.py +0 -0
  147. ck_demos/programs/demo_program_buffer.py +24 -0
  148. ck_demos/programs/demo_program_multi.py +24 -0
  149. ck_demos/programs/demo_program_none.py +19 -0
  150. ck_demos/programs/demo_program_single.py +23 -0
  151. ck_demos/programs/demo_raw_program_interpreted.py +21 -0
  152. ck_demos/programs/demo_raw_program_llvm.py +21 -0
  153. ck_demos/sampling/__init__.py +0 -0
  154. ck_demos/sampling/check_sampler.py +71 -0
  155. ck_demos/sampling/demo_marginal_direct_sampler.py +40 -0
  156. ck_demos/sampling/demo_uniform_sampler.py +38 -0
  157. ck_demos/sampling/demo_wmc_direct_sampler.py +40 -0
  158. ck_demos/utils/__init__.py +0 -0
  159. ck_demos/utils/compare.py +88 -0
  160. ck_demos/utils/convert_network.py +45 -0
  161. ck_demos/utils/sample_model.py +216 -0
  162. ck_demos/utils/stop_watch.py +384 -0
  163. compiled_knowledge-4.0.0a5.dist-info/METADATA +50 -0
  164. compiled_knowledge-4.0.0a5.dist-info/RECORD +167 -0
  165. compiled_knowledge-4.0.0a5.dist-info/WHEEL +5 -0
  166. compiled_knowledge-4.0.0a5.dist-info/licenses/LICENSE.txt +21 -0
  167. compiled_knowledge-4.0.0a5.dist-info/top_level.txt +2 -0
@@ -0,0 +1,45 @@
1
+ from pathlib import Path
2
+
3
+ from ck.in_out.parse_net import read_network
4
+ from ck.in_out.pgm_python import write_python
5
+ from ck.pgm import PGM
6
+
7
+
8
+ def convert_network(network_path: Path, file=None) -> None:
9
+ """
10
+ Convert a Hugin 'net' format to our PGM format.
11
+
12
+ Args:
13
+ network_path: path to a Hugin 'net' file.
14
+ file: destination, as per the `print` function.
15
+ """
16
+ # Read the Hugin 'net' file.
17
+ with open(network_path) as in_file:
18
+ pgm: PGM = read_network(in_file)
19
+
20
+ # Replace functions that may be better being sparse
21
+ for factor in pgm.factors:
22
+ function = factor.function
23
+ total_params: int = function.number_of_parameters
24
+ zero_params: int = sum(1 for _, value in function.params if value == 0)
25
+ if zero_params > 10 and zero_params / total_params > 0.1:
26
+ new_function = factor.set_sparse()
27
+ for key, _, value in function.keys_with_param:
28
+ new_function[key] = value
29
+
30
+ # Write the PGM Python code.
31
+ write_python(pgm, file=file)
32
+
33
+
34
+ def main() -> None:
35
+ """
36
+ Demo of `convert_network`.
37
+ """
38
+ network_directory = r'E:\Dropbox\Research\data\BN\networks'
39
+ network_name = 'pathfinder'
40
+
41
+ convert_network(Path(network_directory) / f'{network_name}.net')
42
+
43
+
44
+ if __name__ == '__main__':
45
+ main()
@@ -0,0 +1,216 @@
1
+ import random
2
+ from typing import Optional, Dict, Callable, List
3
+
4
+ import numpy as np
5
+
6
+ from ck.pgm import rv_instances, PGM, RandomVariable, Indicator
7
+ from ck.pgm_compiler import factor_elimination
8
+ from ck.pgm_circuit.marginals_program import MarginalsProgram
9
+ from ck.pgm_circuit import PGMCircuit
10
+ from ck.pgm_circuit.wmc_program import WMCProgram
11
+ from ck.sampling.forward_sampler import ForwardSampler
12
+ from ck.sampling.sampler import Sampler
13
+ from ck.utils.random_extras import random_permute
14
+ from ck_demos.utils.stop_watch import StopWatch
15
+
16
+ SamplerFactory = Callable[[PGM, WMCProgram, MarginalsProgram, List[RandomVariable], List[Indicator]], Sampler]
17
+
18
+ BURN_IN: int = 1000 # Burn in for standard samplers, where needed. Not all samplers use burn in.
19
+
20
+ # Standard Samplers (by name)
21
+ STANDARD_SAMPLERS: Dict[str, SamplerFactory] = {
22
+ 'Direct-wmc': (
23
+ lambda pgm, wmc, mar, sample_rvs, condition:
24
+ wmc.sample_direct(rvs=sample_rvs, condition=condition)
25
+ ),
26
+ 'Direct-mar': (
27
+ lambda pgm, wmc, mar, sample_rvs, condition:
28
+ mar.sample_direct(rvs=sample_rvs, condition=condition)
29
+ ),
30
+ 'Rejection': (
31
+ lambda pgm, wmc, mar, sample_rvs, condition:
32
+ wmc.sample_rejection(rvs=sample_rvs, condition=condition)
33
+ ),
34
+ 'Gibbs': (
35
+ lambda pgm, wmc, mar, sample_rvs, condition:
36
+ wmc.sample_gibbs(burn_in=BURN_IN, rvs=sample_rvs, condition=condition)
37
+ ),
38
+ 'Metropolis': (
39
+ lambda pgm, wmc, mar, sample_rvs, condition:
40
+ wmc.sample_metropolis(burn_in=BURN_IN, rvs=sample_rvs, condition=condition)
41
+ ),
42
+ 'Forward': (
43
+ lambda pgm, wmc, mar, sample_rvs, condition:
44
+ ForwardSampler(pgm, sample_rvs, condition, check_is_bayesian_network=True)
45
+ ),
46
+ 'Uniform': (
47
+ lambda pgm, wmc, mar, sample_rvs, condition:
48
+ wmc.sample_uniform(rvs=sample_rvs, condition=condition)
49
+ ),
50
+ }
51
+
52
+
53
+ def sample_model(
54
+ pgm: PGM,
55
+ samplers: Dict[str, SamplerFactory],
56
+ num_of_trials: int,
57
+ num_of_samples: int,
58
+ limit_conditioning: Optional[int] = None,
59
+ show_each_analysis: bool = True,
60
+ line: str = '-' * 80,
61
+ ):
62
+ """
63
+ Evaluate the given samplers on the given PGM.
64
+
65
+ Results are printed to standard out.
66
+
67
+ Args:
68
+ pgm: is the model to sample.
69
+ samplers: is a dict from sampler name to factory method. The
70
+ factor method type is (pgm, wmc, mar, sample_rvs, condition) -> Sampler.
71
+ num_of_trials: how many trials to perform.
72
+ num_of_samples: how many num_of_samples to draw from each sampler, for each trial.
73
+ limit_conditioning: maximum number of indicators to use when determining
74
+ conditioning for a trial, or None then pgm.number_of_random_variables is used.
75
+ show_each_analysis: if True, then extra details is printed.
76
+ line: is the 'line' string to use to delimit trials.
77
+ """
78
+ print(f'Model: {pgm.name}')
79
+ print(f'Number of random variables: {pgm.number_of_rvs}')
80
+ print(f'Number of indicators: {pgm.number_of_indicators}')
81
+ print(f'States space: {pgm.number_of_states:,}')
82
+
83
+ # compile
84
+ pgm_cct: PGMCircuit = factor_elimination.compile_pgm(pgm)
85
+ wmc = WMCProgram(pgm_cct)
86
+ mar = MarginalsProgram(pgm_cct)
87
+
88
+ rvs = pgm.rvs
89
+ num_of_rvs = len(rvs)
90
+ sampler_names = list(samplers.keys())
91
+ overall_max_difference = {name: 0 for name in sampler_names}
92
+ overall_sum_difference = {name: 0 for name in sampler_names}
93
+ overall_time = {name: 0 for name in sampler_names}
94
+ errors = {name: [] for name in sampler_names}
95
+
96
+ name_pad = max(
97
+ max(len(name) for name in sampler_names) + 1,
98
+ max(len(rv.name) for rv in rvs) + 1
99
+ )
100
+
101
+ for trial in range(1, 1 + num_of_trials):
102
+ print(line)
103
+
104
+ # what random variables to sample
105
+ num_rvs_to_sample = random.randint(1, num_of_rvs)
106
+ sample_rvs = list(rvs)
107
+ random_permute(sample_rvs)
108
+ del sample_rvs[num_rvs_to_sample:]
109
+ sample_rvs.sort(key=(lambda rv: rv.idx))
110
+ rvs_str = ', '.join([str(rv) for rv in sample_rvs])
111
+
112
+ # what conditions
113
+ if limit_conditioning is None:
114
+ limit_conditioning = pgm.number_of_rvs
115
+ if limit_conditioning == 0:
116
+ condition = ()
117
+ condition_str = ''
118
+ else:
119
+ while True:
120
+ num_indicators_to_condition = random.randint(0, limit_conditioning)
121
+ rand_rvs = list(rvs)
122
+ random_permute(rand_rvs)
123
+ condition = []
124
+ while len(condition) < num_indicators_to_condition and len(rand_rvs) > 0:
125
+ rv = rand_rvs.pop()
126
+ max_rv_indicators_to_condition = min(len(rv) - 1, num_indicators_to_condition - len(condition))
127
+ assert max_rv_indicators_to_condition >= 1, 'assumption check'
128
+ num_rv_indicators_to_condition = random.randint(1, max_rv_indicators_to_condition)
129
+ indicators = list(rv)
130
+ random_permute(indicators)
131
+ condition += sorted(indicators[:num_rv_indicators_to_condition])
132
+
133
+ if len(condition) == 0:
134
+ condition_str = ''
135
+ break
136
+
137
+ condition_str = ' | ' + pgm.condition_str(*condition)
138
+
139
+ # only accept the condition if the Pr(condition) > 0
140
+ if wmc.probability(*condition) > 0:
141
+ break
142
+ print(f'Note: discarded impossible condition{condition_str}')
143
+
144
+ # show the trial parameters
145
+ print(f'trial {trial} of {num_of_trials}: {rvs_str}{condition_str}')
146
+
147
+ # create state indexes for printing
148
+ state_to_index = {}
149
+ all_states = []
150
+ for i, state in enumerate(rv_instances(*sample_rvs)):
151
+ state = tuple(state)
152
+ all_states.append(state)
153
+ state_to_index[state] = i
154
+
155
+ # print detailed results - header
156
+ for i, rv in enumerate(sample_rvs):
157
+ print(str(rv).ljust(name_pad), end='')
158
+ print(' '.join([f'{str(state[i]).ljust(7)}' for state in all_states]))
159
+
160
+ # pgm_stats
161
+ print('PGM'.ljust(name_pad), end='')
162
+ pgm_stats = np.array(wmc.marginal_distribution(*sample_rvs, condition=condition))
163
+ print(' '.join([f'{p:.5f}' for p in pgm_stats]))
164
+
165
+ for sampler_name in sampler_names:
166
+ print(sampler_name.ljust(name_pad), end='')
167
+
168
+ # sample_stats
169
+ try:
170
+ sample_stats = np.zeros(len(all_states))
171
+ sampler = samplers[sampler_name](pgm, wmc, mar, sample_rvs, condition)
172
+ stop_watch = StopWatch()
173
+ for state in sampler.take(num_of_samples):
174
+ i = state_to_index[tuple(state)]
175
+ sample_stats[i] += 1
176
+ stop_watch.stop()
177
+ sample_stats /= np.sum(sample_stats)
178
+ except (ValueError, RuntimeError, AssertionError) as err:
179
+ errors[sampler_name].append(repr(err))
180
+ print(repr(err))
181
+ continue
182
+
183
+ # print detailed results - for this sampler
184
+ print(' '.join([f'{p:.5f}' for p in sample_stats]))
185
+
186
+ # analyse
187
+ max_difference = 0
188
+ sum_difference = 0
189
+ for pgm_stat, sample_stat in zip(pgm_stats, sample_stats):
190
+ diff = abs(pgm_stat - sample_stat)
191
+ max_difference = max(max_difference, diff)
192
+ sum_difference += diff
193
+ overall_max_difference[sampler_name] = max(overall_max_difference[sampler_name], max_difference)
194
+ overall_sum_difference[sampler_name] = max(overall_sum_difference[sampler_name], sum_difference)
195
+ overall_time[sampler_name] += stop_watch.seconds()
196
+
197
+ if show_each_analysis:
198
+ print(
199
+ ' ' * name_pad +
200
+ f'max_difference = {max_difference}, '
201
+ f'sum_difference = {sum_difference}, '
202
+ f'time = {stop_watch.seconds()}'
203
+ )
204
+
205
+ print(line)
206
+ sep: str = ', '
207
+ print(' ' * name_pad + sep.join(['overall_max_difference', 'overall_sum_difference', 'overall_time', 'errors']))
208
+ for sampler_name in sampler_names:
209
+ print(
210
+ f'{sampler_name.ljust(name_pad)}'
211
+ f'{overall_max_difference[sampler_name]}{sep}'
212
+ f'{overall_sum_difference[sampler_name]}{sep}'
213
+ f'{overall_time[sampler_name]}{sep}'
214
+ f'{len(errors[sampler_name])}'
215
+ )
216
+ print()
@@ -0,0 +1,384 @@
1
+ """
2
+ A simple code execution timer.
3
+
4
+ Example usage:
5
+ ```
6
+ time = StopWatch()
7
+ # Do some work
8
+ time.stop()
9
+
10
+ print('time:', time)
11
+ ```
12
+ Alternate usage:
13
+ ```
14
+ with timer('stuff'):
15
+ # do some stuff
16
+ ```
17
+ Usage of ProgressCheck:
18
+ ```
19
+ check = ProgressCheck(60)
20
+ for iteration in range(max_iterations):
21
+ # Do one iteration.
22
+ ...
23
+
24
+ if check:
25
+ print(f'progress: {iteration=} time={check}')
26
+ ```
27
+ """
28
+ from __future__ import annotations
29
+
30
+ import timeit as _timeit
31
+ from typing import Tuple, Dict, Any, Optional
32
+
33
+
34
+ class StopWatch:
35
+ __slots__ = ('start_time', 'stop_time', 'offset_seconds', 'multiplier')
36
+
37
+ def __init__(self, offset_seconds: float = 0, multiplier: float = 1, running: bool = True):
38
+ """
39
+ Create a StopWatch to start timing, by using timeit.default_timer().
40
+ A StopWatch will be created in the running state.
41
+ Call self.stop() to stop (or pause) the StopWatch.
42
+
43
+ Args:
44
+ offset_seconds: is an initial time offset.
45
+ multiplier: is an initial time multiplier (also applied to offset_seconds).
46
+ running: is a Boolean flag to set the stopwatch running (default True).
47
+ """
48
+ assert multiplier > 0, 'multiplier must be positive'
49
+ self.start_time = _timeit.default_timer()
50
+ self.stop_time = None if running else self.start_time
51
+ self.offset_seconds = offset_seconds
52
+ self.multiplier = multiplier
53
+
54
+ def copy(self, running: Optional[bool] = None) -> StopWatch:
55
+ """
56
+ Return a copy of this stop watch.
57
+
58
+ Args:
59
+ running: controls the running state of the copy.
60
+ If True, the copy will be running (continued),
61
+ if False, the copy will be stopped,
62
+ if None, the copy will be in the same state as this stop watch.
63
+ """
64
+ result = StopWatch(
65
+ offset_seconds=self.offset_seconds,
66
+ multiplier=self.multiplier,
67
+ )
68
+ result.start_time = self.start_time
69
+ result.stop_time = self.stop_time
70
+
71
+ if running is not None:
72
+ if running:
73
+ if self.stop_time is not None:
74
+ # starting
75
+ result.continu()
76
+ else:
77
+ if self.stop_time is None:
78
+ # stopping
79
+ result.stop()
80
+ return result
81
+
82
+ def start(self, offset_seconds: float = 0, multiplier: float = 1) -> None:
83
+ """
84
+ Mark the start time for the timer as now.
85
+ Cancels any previous start and stop.
86
+
87
+ Args:
88
+ offset_seconds: is an initial time offset.
89
+ multiplier: is an initial time multiplier (also applied to offset_seconds).
90
+ """
91
+ assert multiplier > 0, 'multiplier must be positive'
92
+ self.start_time = _timeit.default_timer()
93
+ self.stop_time = None
94
+ self.offset_seconds = offset_seconds
95
+ self.multiplier = multiplier
96
+
97
+ def stop(self) -> None:
98
+ """
99
+ Mark the stop time for the timer as now.
100
+ If the stop watch was already stopped, then this overrides the previous stop.
101
+ """
102
+ self.stop_time = _timeit.default_timer()
103
+
104
+ def continu(self) -> None:
105
+ """
106
+ Continue the timer, cancelling any previous stop.
107
+ Any 'pause' time between a stop and continu is not included in the elapsed time.
108
+ """
109
+ if self.stop_time is not None:
110
+ paused_seconds = _timeit.default_timer() - self.stop_time
111
+ self.offset_seconds -= paused_seconds
112
+ self.stop_time = None
113
+
114
+ @property
115
+ def running(self) -> bool:
116
+ """
117
+ Is this stopwatch running?
118
+
119
+ Returns:
120
+ true if the stopwatch is running, false otherwise.
121
+ """
122
+ return self.stop_time is None
123
+
124
+ def set(self, seconds: float, multiplier: float = 1) -> None:
125
+ """
126
+ Set the stopwatch to the given number of seconds.
127
+ This stops the stopwatch and resets the time multiplier.
128
+
129
+ Args:
130
+ seconds: is the value to set the stop watch to.
131
+ multiplier: is reset the time multiplier (also applied to seconds).
132
+ """
133
+ self.start_time = _timeit.default_timer()
134
+ self.stop_time = self.start_time
135
+ self.offset_seconds = seconds
136
+ self.multiplier = multiplier
137
+
138
+ def add(self, seconds: float | StopWatch) -> None:
139
+ """
140
+ Add the given number of seconds to the stopwatch.
141
+ The number of seconds added is not affected by the time multiplier.
142
+ """
143
+ if isinstance(seconds, StopWatch):
144
+ seconds = seconds.seconds()
145
+ self.offset_seconds += seconds / self.multiplier
146
+
147
+ def subtract(self, seconds: float | StopWatch) -> None:
148
+ """
149
+ Subtract the given number of seconds from the stopwatch.
150
+ The number of seconds subtracted is not affected by the time multiplier.
151
+ """
152
+ if isinstance(seconds, StopWatch):
153
+ seconds = seconds.seconds()
154
+ self.offset_seconds -= seconds / self.multiplier
155
+
156
+ def multiply(self, multiplier: float) -> None:
157
+ """
158
+ Multiply the rate of time by the given multiplier.
159
+ Multiplication is accumulative.
160
+ """
161
+ assert multiplier > 0, 'multiplier must be positive'
162
+ self.multiplier *= multiplier
163
+
164
+ def seconds(self) -> float:
165
+ """Number of seconds of elapsed time."""
166
+ if self.stop_time is None:
167
+ time = _timeit.default_timer() - self.start_time
168
+ else:
169
+ time = self.stop_time - self.start_time
170
+ return (time + self.offset_seconds) * self.multiplier
171
+
172
+ def minutes(self) -> float:
173
+ """Number of minutes elapsed."""
174
+ return self.seconds() / 60.0
175
+
176
+ def hours(self) -> float:
177
+ """Number of hours elapsed."""
178
+ return self.seconds() / 3600.0
179
+
180
+ def hms(self) -> Tuple[int, int, float]:
181
+ """
182
+ (hours, minutes, seconds) of elapsed time.
183
+ Hours and minutes will always be integers.
184
+ Only the absolute value of time will be reported
185
+ (i.e., if negative time offsets are used).
186
+ """
187
+ elapsed = abs(self.seconds())
188
+ hours, rem = divmod(elapsed, 3600)
189
+ minutes, seconds = divmod(rem, 60)
190
+ return int(hours), int(minutes), seconds
191
+
192
+ def __str__(self) -> str:
193
+ (hours, minutes, seconds) = self.hms()
194
+ if hours > 0:
195
+ return f'{hours:}:{minutes:0>2}:{seconds:06.3f}'
196
+ elif minutes > 0:
197
+ return f'{minutes:}:{seconds:06.3f}'
198
+ elif seconds >= 0.1:
199
+ return f'{seconds:.3f}'
200
+ elif seconds >= 0.01:
201
+ return f'{seconds:.4f}'
202
+ elif seconds >= 0.001:
203
+ return f'{seconds:.5f}'
204
+ elif seconds >= 0.0001:
205
+ return f'{seconds:.6f}'
206
+ else:
207
+ return str(seconds)
208
+
209
+ def __repr__(self) -> str:
210
+ offset_seconds = self.seconds()
211
+ multiplier = self.multiplier
212
+ running = self.running
213
+ name = self.__class__.__name__
214
+ return f'{name}(offset_seconds={offset_seconds}, multiplier={multiplier}, running={running})'
215
+
216
+ def __float__(self) -> float:
217
+ return self.seconds()
218
+
219
+ def __add__(self, other: float | StopWatch) -> StopWatch:
220
+ """The returned stop watch will be stopped."""
221
+ s = self.copy(running=False)
222
+ s.add(other)
223
+ return s
224
+
225
+ def __iadd__(self, other: float | StopWatch) -> StopWatch:
226
+ self.add(other)
227
+ return self
228
+
229
+ def __sub__(self, other: float | StopWatch) -> StopWatch:
230
+ """The returned stop watch will be stopped."""
231
+ s = self.copy(running=False)
232
+ s.subtract(other)
233
+ return s
234
+
235
+ def __isub__(self, other: float | StopWatch) -> StopWatch:
236
+ self.subtract(other)
237
+ return self
238
+
239
+ def __mul__(self, multiplier: float) -> StopWatch:
240
+ """The returned stop watch will be stopped."""
241
+ s = self.copy(running=False)
242
+ s.multiply(multiplier)
243
+ return s
244
+
245
+ def __imul__(self, multiplier: float) -> StopWatch:
246
+ self.multiply(multiplier)
247
+ return self
248
+
249
+ def __eq__(self, other: StopWatch) -> bool:
250
+ return self.seconds() == other.seconds()
251
+
252
+ def __ne__(self, other: StopWatch) -> bool:
253
+ return self.seconds() != other.seconds()
254
+
255
+ def __lt__(self, other: StopWatch) -> bool:
256
+ return self.seconds() < other.seconds()
257
+
258
+ def __gt__(self, other: StopWatch) -> bool:
259
+ return self.seconds() > other.seconds()
260
+
261
+ def __le__(self, other: StopWatch) -> bool:
262
+ return self.seconds() <= other.seconds()
263
+
264
+ def __ge__(self, other: StopWatch) -> bool:
265
+ return self.seconds() >= other.seconds()
266
+
267
+ def __hash__(self):
268
+ return hash(self.seconds())
269
+
270
+
271
+ class ProgressCheck(StopWatch):
272
+ """
273
+ A class to support simple progress checking in a loop.
274
+ """
275
+
276
+ def __init__(self, reporting_seconds: float, first_check: Optional[float] = None):
277
+ """
278
+ Args:
279
+ reporting_seconds: how often (in seconds) should 'check' return True.
280
+ This is the minimum time between 'check' returning True.
281
+ first_check: when should 'check' first return True (in seconds).
282
+ If first_check is None the default value is reporting_seconds.
283
+ """
284
+ self.reporting_seconds = reporting_seconds
285
+ self.next_check = reporting_seconds if first_check is None else first_check
286
+ super().__init__()
287
+
288
+ def check(self) -> bool:
289
+ """
290
+ Returns:
291
+ True only if it has been long enough since the last True check.
292
+ """
293
+ seconds = self.seconds()
294
+ if seconds > self.next_check:
295
+ self.next_check = seconds + self.reporting_seconds
296
+ return True
297
+ else:
298
+ return False
299
+
300
+ def __bool__(self) -> bool:
301
+ return self.check()
302
+
303
+
304
+ class timer(StopWatch):
305
+
306
+ def __init__(
307
+ self,
308
+ label: str = 'a',
309
+ start_message: str = '{label} timer started',
310
+ stop_message: str = '{label} timer stopped: {time}',
311
+ file=None,
312
+ logger=None
313
+ ):
314
+ """
315
+ Create a timer that will use a stop watch to time a section of code within a 'with' statement.
316
+ The timer label will be printed on entering the 'with' statement.
317
+ The timer label and time taken will be printed on exiting the 'with' statement.
318
+
319
+ Args:
320
+ label: A text string to label the timer.
321
+ start_message: How the 'enter' message will be formatted, including format fields.
322
+ stop_message: How the 'exit' message will be formatted, including format fields.
323
+ file: Where messages should be printed - an output stream.
324
+ logger: Where messages should be printed - a print function.
325
+
326
+ Available format fields:
327
+ {label} the label parameter as passed at construction time.
328
+ {time} the time rendered as per StopWatch.__str__.
329
+ {seconds} the time, in seconds.
330
+ {minutes} the time, in minutes.
331
+ {hours} the time, in hours.
332
+
333
+ Either file or logger may be specified, not both. If neither,
334
+ then the standard output is used.
335
+ """
336
+ super().__init__(running=False)
337
+ self._label = '' if label is None else label
338
+ self._start_message = start_message
339
+ self._stop_message = stop_message
340
+ self._file = file
341
+ self._logger = logger
342
+
343
+ if self._file is not None:
344
+ if self._logger is not None:
345
+ raise RuntimeError('cannot specify both file and logger')
346
+ self._print = self._print_file
347
+ elif self._logger is not None:
348
+ self._print = self._print_logger
349
+ else:
350
+ self._print = self._print_stdout
351
+
352
+ def _print(self, *args):
353
+ pass # dynamically set at construction time
354
+
355
+ def _format_fields(self) -> Dict[str, Any]:
356
+ return {
357
+ 'label': self._label,
358
+ 'time': str(self),
359
+ 'seconds': self.seconds(),
360
+ 'minutes': self.minutes(),
361
+ 'hours': self.hours(),
362
+ }
363
+
364
+ @staticmethod
365
+ def _print_stdout(*args):
366
+ print(*args)
367
+
368
+ def _print_file(self, *args):
369
+ print(*args, file=self._file)
370
+
371
+ def _print_logger(self, *args):
372
+ self._logger(*args)
373
+
374
+ def __enter__(self):
375
+ if self._start_message is not None:
376
+ self._print(self._start_message.format(**self._format_fields()))
377
+ self.start()
378
+ return self
379
+
380
+ def __exit__(self, exc_type, exc_val, exc_tb):
381
+ self.stop()
382
+ if self._stop_message is not None:
383
+ self._print(self._stop_message.format(**self._format_fields()))
384
+ return exc_val is None
@@ -0,0 +1,50 @@
1
+ Metadata-Version: 2.4
2
+ Name: compiled-knowledge
3
+ Version: 4.0.0a5
4
+ Summary: A Python package for compiling and querying discrete probabilistic graphical models.
5
+ Author-email: Barry Drake <barry@compiledknowledge.org>
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://github.com/ropeless/compiled_knowledge
8
+ Project-URL: Issues, https://github.com/ropeless/compiled_knowledge/issues
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: Operating System :: OS Independent
11
+ Requires-Python: >=3.12
12
+ Description-Content-Type: text/markdown
13
+ License-File: LICENSE.txt
14
+ Requires-Dist: llvmlite
15
+ Requires-Dist: numpy
16
+ Dynamic: license-file
17
+
18
+ CompiledKnowledge
19
+ =================
20
+
21
+ CompiledKnowledge is a Python package for compiling and querying discrete probabilistic graphical models.
22
+
23
+ The aim of this repository is:
24
+ - to provide a Python library for compiling and querying
25
+ probabilistic graphical models, specifically discrete factor graphs
26
+ - to be extremely efficient, flexible, and easy to use
27
+ - to exhibit excellent design, code, and documentation
28
+ - support researchers and businesses wanting to explore and use
29
+ probabilistic artificial intelligence.
30
+
31
+ License
32
+ =======
33
+
34
+ See the file `LICENSE.txt`.
35
+
36
+
37
+ More Information
38
+ ================
39
+
40
+ For documentation see the Jupyter notebooks in the directory `docs`.
41
+
42
+ For many example scripts, see the package `ck_demos`.
43
+
44
+ For unit tests, see the package `ck_tests`.
45
+
46
+ For information about contributing to this library, see `CONTRIBUTING.md`.
47
+
48
+ The primary code repository for Compiled Knowledge is https://github.com/ropeless/compiled_knowledge.
49
+
50
+ For more information email [info@compiledknowledge.org](mailto:info@compiledknowledge.org).