scientific-computing-system 1.0.0 → 2.2.1

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.
Files changed (430) hide show
  1. package/CHANGELOG.md +365 -0
  2. package/LICENSE +0 -0
  3. package/README.md +146 -217
  4. package/SECURITY.md +32 -6
  5. package/bin/scs.js +12 -2
  6. package/index.js +115 -1
  7. package/package.json +45 -1
  8. package/.binder/postBuild +0 -9
  9. package/.binder/requirements.txt +0 -10
  10. package/.gitattributes +0 -35
  11. package/.github/CODEOWNERS +0 -4
  12. package/.github/FUNDING.yml +0 -3
  13. package/.github/ISSUE_TEMPLATE/bug_report.md +0 -50
  14. package/.github/ISSUE_TEMPLATE/config.yml +0 -8
  15. package/.github/ISSUE_TEMPLATE/feature_request.md +0 -28
  16. package/.github/PULL_REQUEST_TEMPLATE.md +0 -29
  17. package/.github/dependabot.yml +0 -32
  18. package/.github/labeler.yml +0 -50
  19. package/.github/workflows/benchmarks.yml +0 -97
  20. package/.github/workflows/bot-reviewer.yml +0 -58
  21. package/.github/workflows/changelog.yml +0 -130
  22. package/.github/workflows/cli-package-smoke.yml +0 -45
  23. package/.github/workflows/codeql.yml +0 -55
  24. package/.github/workflows/npm-publish.yml +0 -33
  25. package/.github/workflows/p08-cancel-noncanonical-runs.yml +0 -41
  26. package/.github/workflows/pr-automation.yml +0 -78
  27. package/.github/workflows/public-external-validation-p08-v2-glm.yml +0 -161
  28. package/.github/workflows/public-external-validation-p08-v2-recovery-merge-gate.yml +0 -72
  29. package/.github/workflows/public-external-validation-p08-v2-recovery-merge.yml +0 -113
  30. package/.github/workflows/public-external-validation-p08-v2-run-recovery-anatomy-gate.yml +0 -57
  31. package/.github/workflows/public-external-validation-p08-v2-run-recovery-merge-gate.yml +0 -63
  32. package/.github/workflows/public-external-validation-p08-v2-run-recovery-merge.yml +0 -89
  33. package/.github/workflows/public-external-validation-p08-v2-run-recovery.yml +0 -182
  34. package/.github/workflows/public-external-validation-p08-v3-glm-gate.yml +0 -67
  35. package/.github/workflows/public-external-validation-p08-v4-recovery-glm-gate.yml +0 -78
  36. package/.github/workflows/public-external-validation-p08-v4-recovery-glm.yml +0 -248
  37. package/.github/workflows/public-external-validation-p08-v5-run-recovery-glm-gate.yml +0 -66
  38. package/.github/workflows/public-external-validation-p08-v5-run-recovery-glm.yml +0 -218
  39. package/.github/workflows/pypi-registry-smoke.yml +0 -116
  40. package/.github/workflows/release.yml +0 -359
  41. package/.github/workflows/tests.yml +0 -250
  42. package/.github/workflows/tmp-compassion-raw-acquire.yml +0 -63
  43. package/.github/workflows/wsl-science-runner-smoke.yml +0 -45
  44. package/.pre-commit-config.yaml +0 -48
  45. package/CITATION.cff +0 -29
  46. package/CODE_OF_CONDUCT.md +0 -25
  47. package/CONTRIBUTING.md +0 -275
  48. package/ROADMAP.md +0 -155
  49. package/assets/logo.svg +0 -38
  50. package/assets/social_preview.png +0 -0
  51. package/benchmarks/results.json +0 -43
  52. package/benchmarks/robust_runner.py +0 -118
  53. package/benchmarks/run_benchmarks.py +0 -394
  54. package/cliff.toml +0 -100
  55. package/dashboard/app.py +0 -224
  56. package/docs/ARCHITECTURE.md +0 -252
  57. package/docs/CASE_STUDY_HUBBLE.md +0 -78
  58. package/docs/CASE_STUDY_QUANTUM_ML.md +0 -79
  59. package/docs/api.md +0 -133
  60. package/docs/assets/icon.svg +0 -17
  61. package/docs/assets/logo.svg +0 -38
  62. package/docs/benchmarks.md +0 -62
  63. package/docs/cookbook.md +0 -930
  64. package/docs/getting-started.md +0 -185
  65. package/docs/index.md +0 -111
  66. package/docs/maintenance.md +0 -61
  67. package/docs/ml_reference.md +0 -92
  68. package/docs/research-workflows.md +0 -82
  69. package/docs/stylesheets/extra.css +0 -122
  70. package/docs/tour_of_numerical_methods.md +0 -330
  71. package/docs/tutorials/core_demo.md +0 -35
  72. package/docs/tutorials/data_analysis_demo.md +0 -43
  73. package/docs/tutorials/diffeq_demo.md +0 -48
  74. package/docs/tutorials/fft2_demo.md +0 -67
  75. package/docs/tutorials/graph_demo.md +0 -45
  76. package/docs/tutorials/hypothesis_demo.md +0 -44
  77. package/docs/tutorials/hypothesis_tests_demo.md +0 -170
  78. package/docs/tutorials/hypothesis_with_stats_demo.md +0 -80
  79. package/docs/tutorials/knowledge_demo.md +0 -114
  80. package/docs/tutorials/linalg_demo.md +0 -103
  81. package/docs/tutorials/math_utils_demo.md +0 -46
  82. package/docs/tutorials/ml_advanced_demo.md +0 -96
  83. package/docs/tutorials/ml_and_viz_demo.md +0 -93
  84. package/docs/tutorials/ml_demo.md +0 -70
  85. package/docs/tutorials/modeling_demo.md +0 -82
  86. package/docs/tutorials/montecarlo_demo.md +0 -42
  87. package/docs/tutorials/nlp_attention_demo.md +0 -107
  88. package/docs/tutorials/nlp_bpe_demo.md +0 -103
  89. package/docs/tutorials/nlp_mini_gpt_demo.md +0 -110
  90. package/docs/tutorials/nlp_viz.md +0 -76
  91. package/docs/tutorials/numerical_integration_demo.md +0 -65
  92. package/docs/tutorials/optimization_demo.md +0 -106
  93. package/docs/tutorials/pandas_io_demo.md +0 -160
  94. package/docs/tutorials/pca_demo.md +0 -87
  95. package/docs/tutorials/probability_demo.md +0 -48
  96. package/docs/tutorials/quantum_demo.md +0 -31
  97. package/docs/tutorials/quick_start.md +0 -44
  98. package/docs/tutorials/scientific_demo.md +0 -46
  99. package/docs/tutorials/signals_demo.md +0 -167
  100. package/docs/tutorials/stats_demo.md +0 -94
  101. package/docs/tutorials/stiff_ode_demo.md +0 -84
  102. package/docs/tutorials/time_series_demo.md +0 -145
  103. package/examples/core_demo.py +0 -40
  104. package/examples/data_analysis_demo.py +0 -60
  105. package/examples/diffeq_demo.py +0 -34
  106. package/examples/discovery_pipeline_demo.py +0 -57
  107. package/examples/ensemble_showcase.py +0 -81
  108. package/examples/fft2_demo.py +0 -32
  109. package/examples/graph_demo.py +0 -49
  110. package/examples/hypothesis_custom_generator.py +0 -121
  111. package/examples/hypothesis_demo.py +0 -48
  112. package/examples/hypothesis_tests_demo.py +0 -52
  113. package/examples/hypothesis_with_stats_demo.py +0 -43
  114. package/examples/interpolate_demo.py +0 -57
  115. package/examples/knowledge_demo.py +0 -100
  116. package/examples/linalg_demo.py +0 -61
  117. package/examples/math_utils_demo.py +0 -53
  118. package/examples/ml_and_viz_demo.py +0 -47
  119. package/examples/modeling_demo.py +0 -110
  120. package/examples/montecarlo_demo.py +0 -41
  121. package/examples/nlp_attention_demo.py +0 -168
  122. package/examples/nlp_bpe_demo.py +0 -79
  123. package/examples/nlp_mini_gpt_demo.py +0 -76
  124. package/examples/nlp_viz_demo.py +0 -98
  125. package/examples/numerical_integration_demo.py +0 -60
  126. package/examples/optimization_demo.py +0 -34
  127. package/examples/pca_demo.py +0 -124
  128. package/examples/pde_demo.py +0 -65
  129. package/examples/plot_demo.py +0 -84
  130. package/examples/plotting_notebook.ipynb +0 -87
  131. package/examples/probability_demo.py +0 -39
  132. package/examples/quantum_demo.py +0 -42
  133. package/examples/scientific_demo.py +0 -49
  134. package/examples/signals_demo.py +0 -86
  135. package/examples/stats_demo.py +0 -40
  136. package/examples/tour_of_numerical_methods.ipynb +0 -497
  137. package/mkdocs.yml +0 -159
  138. package/pyproject.toml +0 -288
  139. package/requirements-dev.lock +0 -427
  140. package/requirements-dev.txt +0 -39
  141. package/requirements.lock +0 -6
  142. package/requirements.txt +0 -13
  143. package/scripts/build_gif.py +0 -111
  144. package/scripts/build_video.py +0 -214
  145. package/scripts/cleanup_deployments.py +0 -173
  146. package/scripts/crop_readme_png.py +0 -40
  147. package/scripts/make_pi_dashboard.py +0 -316
  148. package/scripts/make_pi_gif.py +0 -138
  149. package/scripts/publish.py +0 -203
  150. package/scripts/render_promo.mjs +0 -93
  151. package/scripts/render_readme.py +0 -211
  152. package/scripts/render_readme_sections.py +0 -168
  153. package/scripts/verify_cli_install.py +0 -99
  154. package/scripts/verify_cookbook.py +0 -601
  155. package/scripts/verify_d1_numerics.py +0 -504
  156. package/scripts/verify_d2_edge_cases.py +0 -442
  157. package/scripts/verify_d4_hypothesis.py +0 -337
  158. package/scripts/verify_ml_reference.py +0 -160
  159. package/scripts/verify_pypi_release.py +0 -97
  160. package/scripts/verify_release_integrity.py +0 -194
  161. package/src/cds/__init__.py +0 -132
  162. package/src/cds/__main__.py +0 -8
  163. package/src/cds/_version.py +0 -11
  164. package/src/cds/bayes/__init__.py +0 -25
  165. package/src/cds/bayes/conjugate.py +0 -560
  166. package/src/cds/cli/__init__.py +0 -104
  167. package/src/cds/cli/__main__.py +0 -8
  168. package/src/cds/cli/_handlers.py +0 -323
  169. package/src/cds/cli/_parser.py +0 -155
  170. package/src/cds/cli/_style.py +0 -208
  171. package/src/cds/cli/_system_info.py +0 -71
  172. package/src/cds/core/__init__.py +0 -5
  173. package/src/cds/core/_numeric.py +0 -92
  174. package/src/cds/core/models.py +0 -166
  175. package/src/cds/data_analysis/__init__.py +0 -30
  176. package/src/cds/data_analysis/dataset.py +0 -122
  177. package/src/cds/data_analysis/loader.py +0 -75
  178. package/src/cds/data_analysis/pandas_io.py +0 -108
  179. package/src/cds/data_analysis/transform.py +0 -36
  180. package/src/cds/data_analysis/viz.py +0 -88
  181. package/src/cds/data_io/__init__.py +0 -27
  182. package/src/cds/data_io/streaming.py +0 -314
  183. package/src/cds/diffeq/__init__.py +0 -37
  184. package/src/cds/diffeq/_implicit.py +0 -349
  185. package/src/cds/diffeq/solvers.py +0 -312
  186. package/src/cds/diffeq/symplectic.py +0 -225
  187. package/src/cds/fractals/__init__.py +0 -34
  188. package/src/cds/fractals/sets.py +0 -356
  189. package/src/cds/genetics/__init__.py +0 -22
  190. package/src/cds/genetics/sequence.py +0 -199
  191. package/src/cds/graph/__init__.py +0 -31
  192. package/src/cds/graph/algorithms.py +0 -399
  193. package/src/cds/hypothesis/__init__.py +0 -51
  194. package/src/cds/hypothesis/evaluator.py +0 -311
  195. package/src/cds/hypothesis/generator.py +0 -275
  196. package/src/cds/hypothesis/mining.py +0 -314
  197. package/src/cds/infotheory/__init__.py +0 -25
  198. package/src/cds/infotheory/measures.py +0 -276
  199. package/src/cds/interpolate/__init__.py +0 -156
  200. package/src/cds/knowledge/__init__.py +0 -45
  201. package/src/cds/knowledge/graph.py +0 -489
  202. package/src/cds/knowledge/notes.py +0 -231
  203. package/src/cds/knowledge/retrieval.py +0 -339
  204. package/src/cds/math_utils/__init__.py +0 -46
  205. package/src/cds/math_utils/calculus.py +0 -40
  206. package/src/cds/math_utils/linalg.py +0 -496
  207. package/src/cds/math_utils/special.py +0 -167
  208. package/src/cds/math_utils/svd.py +0 -261
  209. package/src/cds/ml/__init__.py +0 -60
  210. package/src/cds/ml/boosting.py +0 -304
  211. package/src/cds/ml/clustering.py +0 -190
  212. package/src/cds/ml/decomposition.py +0 -172
  213. package/src/cds/ml/ensemble.py +0 -152
  214. package/src/cds/ml/linear_models.py +0 -225
  215. package/src/cds/ml/metrics.py +0 -227
  216. package/src/cds/ml/model_selection.py +0 -150
  217. package/src/cds/ml/naive_bayes.py +0 -179
  218. package/src/cds/ml/neighbors.py +0 -162
  219. package/src/cds/ml/neural.py +0 -204
  220. package/src/cds/ml/preprocessing.py +0 -131
  221. package/src/cds/ml/tree.py +0 -228
  222. package/src/cds/ml/voting.py +0 -175
  223. package/src/cds/modeling/__init__.py +0 -44
  224. package/src/cds/modeling/_base.py +0 -210
  225. package/src/cds/modeling/_nodes.py +0 -526
  226. package/src/cds/modeling/expression.py +0 -81
  227. package/src/cds/modeling/fitting.py +0 -477
  228. package/src/cds/modeling/model.py +0 -134
  229. package/src/cds/modeling/solver.py +0 -187
  230. package/src/cds/montecarlo/__init__.py +0 -24
  231. package/src/cds/montecarlo/markov.py +0 -112
  232. package/src/cds/montecarlo/methods.py +0 -294
  233. package/src/cds/nlp/__init__.py +0 -170
  234. package/src/cds/nlp/attention.py +0 -285
  235. package/src/cds/nlp/autograd/__init__.py +0 -70
  236. package/src/cds/nlp/autograd/_grad.py +0 -120
  237. package/src/cds/nlp/autograd/ops.py +0 -133
  238. package/src/cds/nlp/autograd/tensor.py +0 -298
  239. package/src/cds/nlp/bpe.py +0 -449
  240. package/src/cds/nlp/data.py +0 -70
  241. package/src/cds/nlp/embed.py +0 -190
  242. package/src/cds/nlp/layers.py +0 -259
  243. package/src/cds/nlp/model.py +0 -456
  244. package/src/cds/nlp/optim.py +0 -163
  245. package/src/cds/nlp/training.py +0 -167
  246. package/src/cds/nlp/viz.py +0 -254
  247. package/src/cds/numerical_integration/__init__.py +0 -31
  248. package/src/cds/numerical_integration/quadrature.py +0 -480
  249. package/src/cds/optimization/__init__.py +0 -26
  250. package/src/cds/optimization/_metaheuristics.py +0 -287
  251. package/src/cds/optimization/constrained.py +0 -212
  252. package/src/cds/optimization/minimize.py +0 -425
  253. package/src/cds/pde/__init__.py +0 -266
  254. package/src/cds/plot/__init__.py +0 -49
  255. package/src/cds/plot/_backend.py +0 -32
  256. package/src/cds/plot/charts.py +0 -462
  257. package/src/cds/probability/__init__.py +0 -65
  258. package/src/cds/probability/_advanced.py +0 -283
  259. package/src/cds/probability/distributions.py +0 -236
  260. package/src/cds/probability/extra_distributions.py +0 -199
  261. package/src/cds/provenance/__init__.py +0 -25
  262. package/src/cds/provenance/manifest.py +0 -355
  263. package/src/cds/py.typed +0 -0
  264. package/src/cds/quantum/__init__.py +0 -51
  265. package/src/cds/quantum/circuit.py +0 -122
  266. package/src/cds/quantum/multi_qubit.py +0 -310
  267. package/src/cds/quantum/simulator.py +0 -53
  268. package/src/cds/scientific/__init__.py +0 -34
  269. package/src/cds/scientific/constants.py +0 -35
  270. package/src/cds/scientific/formulas.py +0 -116
  271. package/src/cds/sensitivity.py +0 -204
  272. package/src/cds/signals/__init__.py +0 -59
  273. package/src/cds/signals/filters.py +0 -449
  274. package/src/cds/signals/processing.py +0 -224
  275. package/src/cds/signals/stft.py +0 -177
  276. package/src/cds/signals/wavelet.py +0 -146
  277. package/src/cds/stats/__init__.py +0 -99
  278. package/src/cds/stats/_distributions.py +0 -102
  279. package/src/cds/stats/bootstrap.py +0 -188
  280. package/src/cds/stats/descriptive.py +0 -172
  281. package/src/cds/stats/hypothesis_tests.py +0 -294
  282. package/src/cds/stats/nonparametric.py +0 -141
  283. package/src/cds/stats/power.py +0 -290
  284. package/src/cds/stats/regression.py +0 -51
  285. package/src/cds/stats/time_series.py +0 -420
  286. package/src/cds/tools/__init__.py +0 -29
  287. package/src/cds/tools/adapters.py +0 -218
  288. package/src/cds/tools/registry.py +0 -181
  289. package/src/cds/uncertainty/__init__.py +0 -17
  290. package/src/cds/uncertainty/propagation.py +0 -233
  291. package/src/cds/units/__init__.py +0 -45
  292. package/src/cds/units/core.py +0 -199
  293. package/src/cds/validation/__init__.py +0 -41
  294. package/src/cds/validation/adequacy.py +0 -157
  295. package/src/cds/validation/checks.py +0 -485
  296. package/src/cds/validation/report.py +0 -50
  297. package/src/cds/wavelets/__init__.py +0 -5
  298. package/src/cds/wavelets/haar.py +0 -219
  299. package/src/cds/workflow/__init__.py +0 -77
  300. package/src/cds/workflow/engine.py +0 -116
  301. package/src/cds/workflow/gates.py +0 -120
  302. package/src/cds/workflow/orchestrator.py +0 -431
  303. package/src/cds/workflow/selection.py +0 -454
  304. package/src/cds/workflow/tooling.py +0 -155
  305. package/src/cds/workflow/types.py +0 -107
  306. package/tests/__init__.py +0 -0
  307. package/tests/conftest.py +0 -109
  308. package/tests/test_advanced_fitting.py +0 -247
  309. package/tests/test_audit_completion_coverage.py +0 -183
  310. package/tests/test_audit_guard_coverage.py +0 -115
  311. package/tests/test_audit_hardening.py +0 -138
  312. package/tests/test_audit_hardening_coverage.py +0 -111
  313. package/tests/test_bayes_v2.py +0 -208
  314. package/tests/test_benchmarks.py +0 -111
  315. package/tests/test_bootstrap.py +0 -135
  316. package/tests/test_cli.py +0 -51
  317. package/tests/test_cli_color.py +0 -114
  318. package/tests/test_cli_extended.py +0 -239
  319. package/tests/test_cli_system_info.py +0 -36
  320. package/tests/test_constrained_opt.py +0 -174
  321. package/tests/test_core.py +0 -273
  322. package/tests/test_data_analysis.py +0 -146
  323. package/tests/test_data_analysis_lazy_bridge.py +0 -68
  324. package/tests/test_data_analysis_pandas_io.py +0 -119
  325. package/tests/test_data_io.py +0 -149
  326. package/tests/test_dataset.py +0 -91
  327. package/tests/test_diffeq.py +0 -186
  328. package/tests/test_diffeq_implicit.py +0 -173
  329. package/tests/test_domain_validation.py +0 -66
  330. package/tests/test_edge_misc_numerical.py +0 -666
  331. package/tests/test_edge_ml_quantum_linalg.py +0 -191
  332. package/tests/test_edge_nlp.py +0 -229
  333. package/tests/test_edge_quantum_evaluator_cli.py +0 -601
  334. package/tests/test_edge_stats_optimization_signals.py +0 -406
  335. package/tests/test_extra_distributions.py +0 -206
  336. package/tests/test_fft2.py +0 -67
  337. package/tests/test_fitting_bootstrap.py +0 -202
  338. package/tests/test_fractals.py +0 -387
  339. package/tests/test_genetics.py +0 -159
  340. package/tests/test_graph.py +0 -380
  341. package/tests/test_hardening_privacy_provenance.py +0 -306
  342. package/tests/test_hypothesis.py +0 -216
  343. package/tests/test_hypothesis_engine.py +0 -59
  344. package/tests/test_hypothesis_mining.py +0 -247
  345. package/tests/test_hypothesis_tests.py +0 -277
  346. package/tests/test_infotheory.py +0 -302
  347. package/tests/test_interpolate.py +0 -189
  348. package/tests/test_knowledge.py +0 -1149
  349. package/tests/test_layering.py +0 -112
  350. package/tests/test_linalg_extended.py +0 -179
  351. package/tests/test_linalg_qr_cholesky.py +0 -68
  352. package/tests/test_markov.py +0 -155
  353. package/tests/test_math_utils.py +0 -157
  354. package/tests/test_ml_boosting.py +0 -148
  355. package/tests/test_ml_clustering.py +0 -92
  356. package/tests/test_ml_ensemble.py +0 -103
  357. package/tests/test_ml_knn.py +0 -95
  358. package/tests/test_ml_linreg.py +0 -70
  359. package/tests/test_ml_logreg.py +0 -84
  360. package/tests/test_ml_metrics.py +0 -125
  361. package/tests/test_ml_model_selection.py +0 -108
  362. package/tests/test_ml_naive_bayes.py +0 -183
  363. package/tests/test_ml_pca.py +0 -98
  364. package/tests/test_ml_preprocessing.py +0 -104
  365. package/tests/test_ml_tree.py +0 -89
  366. package/tests/test_ml_viz.py +0 -58
  367. package/tests/test_ml_voting.py +0 -219
  368. package/tests/test_modeling.py +0 -726
  369. package/tests/test_modeling_fit_validation.py +0 -46
  370. package/tests/test_montecarlo.py +0 -245
  371. package/tests/test_multi_qubit.py +0 -466
  372. package/tests/test_nlp_attention.py +0 -335
  373. package/tests/test_nlp_autograd.py +0 -435
  374. package/tests/test_nlp_bpe.py +0 -463
  375. package/tests/test_nlp_embed.py +0 -240
  376. package/tests/test_nlp_layers.py +0 -276
  377. package/tests/test_nlp_model.py +0 -172
  378. package/tests/test_nlp_training.py +0 -262
  379. package/tests/test_nlp_viz.py +0 -188
  380. package/tests/test_numerical_integration.py +0 -374
  381. package/tests/test_numerical_invariants.py +0 -768
  382. package/tests/test_numerical_oracles.py +0 -241
  383. package/tests/test_optimization.py +0 -139
  384. package/tests/test_optimization_metaheuristics.py +0 -221
  385. package/tests/test_pde.py +0 -294
  386. package/tests/test_plot_charts.py +0 -247
  387. package/tests/test_plot_lazy.py +0 -48
  388. package/tests/test_power.py +0 -226
  389. package/tests/test_probability.py +0 -274
  390. package/tests/test_probability_advanced.py +0 -226
  391. package/tests/test_property_invariants.py +0 -277
  392. package/tests/test_provenance.py +0 -215
  393. package/tests/test_provenance_audit_hardening.py +0 -45
  394. package/tests/test_public_api.py +0 -75
  395. package/tests/test_quantum.py +0 -195
  396. package/tests/test_release_contract.py +0 -105
  397. package/tests/test_retrieval.py +0 -255
  398. package/tests/test_scientific.py +0 -185
  399. package/tests/test_selection_audit_hardening.py +0 -55
  400. package/tests/test_sensitivity.py +0 -59
  401. package/tests/test_sensitivity_global.py +0 -73
  402. package/tests/test_signals.py +0 -146
  403. package/tests/test_signals_filters.py +0 -460
  404. package/tests/test_special_invariants.py +0 -222
  405. package/tests/test_special_numerics.py +0 -44
  406. package/tests/test_statistical_audit.py +0 -75
  407. package/tests/test_stats.py +0 -206
  408. package/tests/test_stats_audit_coverage.py +0 -17
  409. package/tests/test_stats_nonparametric.py +0 -87
  410. package/tests/test_stats_time_series.py +0 -352
  411. package/tests/test_stft.py +0 -208
  412. package/tests/test_streaming_analytics.py +0 -128
  413. package/tests/test_svd.py +0 -170
  414. package/tests/test_symplectic.py +0 -200
  415. package/tests/test_tool_adapters.py +0 -197
  416. package/tests/test_tools_registry.py +0 -132
  417. package/tests/test_uncertainty.py +0 -171
  418. package/tests/test_units.py +0 -139
  419. package/tests/test_validation.py +0 -100
  420. package/tests/test_validation_adequacy.py +0 -129
  421. package/tests/test_validation_advanced.py +0 -110
  422. package/tests/test_wavelet.py +0 -170
  423. package/tests/test_wavelets_v2.py +0 -119
  424. package/tests/test_workflow.py +0 -173
  425. package/tests/test_workflow_gates.py +0 -144
  426. package/tests/test_workflow_orchestrator.py +0 -516
  427. package/tests/test_workflow_orchestrator_blocked_review.py +0 -95
  428. package/tests/test_workflow_selection.py +0 -312
  429. package/tests/test_workflow_selection_coverage.py +0 -114
  430. package/tests/test_workflow_tooling.py +0 -190
@@ -1,1149 +0,0 @@
1
- """Tests for cds.knowledge — concept graphs, notes, and structured retrieval."""
2
-
3
- from __future__ import annotations
4
-
5
- import json
6
- from pathlib import Path
7
- from typing import cast
8
-
9
- import pytest
10
-
11
- from cds.knowledge import (
12
- Concept,
13
- KnowledgeGraph,
14
- Note,
15
- Notebook,
16
- Relation,
17
- SearchResult,
18
- search,
19
- search_concepts,
20
- search_notes,
21
- )
22
- from cds.knowledge.retrieval import NAME_TAG_SCORE, SUBSTRING_SCORE
23
-
24
- # ---------------------------------------------------------------------- #
25
- # Concept
26
- # ---------------------------------------------------------------------- #
27
-
28
-
29
- class TestConcept:
30
- def test_defaults_are_empty_not_none(self) -> None:
31
- c = Concept("x")
32
- assert c.name == "x"
33
- assert c.description is None
34
- assert c.tags == []
35
- assert c.metadata == {}
36
-
37
- def test_stores_fields(self) -> None:
38
- c = Concept("x", description="d", tags=["a", "b"], metadata={"k": "v"})
39
- assert c.description == "d"
40
- assert c.tags == ["a", "b"]
41
- assert c.metadata == {"k": "v"}
42
-
43
- def test_equality_by_name(self) -> None:
44
- assert Concept("x") == Concept("x", description="different")
45
- assert Concept("x") != Concept("y")
46
-
47
- def test_equality_with_non_concept(self) -> None:
48
- assert Concept("x") != "x"
49
- assert Concept("x") != 42
50
- assert Concept("x") != None # noqa: E711
51
-
52
- def test_hash_is_stable_and_name_based(self) -> None:
53
- c1 = Concept("x", description="one")
54
- c2 = Concept("x", description="two")
55
- assert hash(c1) == hash(c2)
56
- # Usable as a set member / dict key.
57
- assert {c1, c2, Concept("y")} == {c1, Concept("y")}
58
-
59
- def test_to_dict_roundtrips_fields(self) -> None:
60
- c = Concept("x", description="d", tags=["a"], metadata={"k": "v"})
61
- d = c.to_dict()
62
- assert d == {"name": "x", "description": "d", "tags": ["a"], "metadata": {"k": "v"}}
63
-
64
- def test_to_dict_copies_mutable_fields(self) -> None:
65
- c = Concept("x", tags=["a"], metadata={"k": "v"})
66
- d = c.to_dict()
67
- cast(list[str], d["tags"]).append("b")
68
- cast(dict[str, str], d["metadata"])["z"] = "1"
69
- assert c.tags == ["a"]
70
- assert c.metadata == {"k": "v"}
71
-
72
- def test_from_dict_valid(self) -> None:
73
- c = Concept.from_dict(
74
- {"name": "x", "description": "d", "tags": ["a"], "metadata": {"k": "v"}}
75
- )
76
- assert c == Concept("x", description="d", tags=["a"], metadata={"k": "v"})
77
-
78
- def test_from_dict_null_description(self) -> None:
79
- c = Concept.from_dict({"name": "x", "description": None, "tags": [], "metadata": {}})
80
- assert c.description is None
81
-
82
- def test_from_dict_roundtrip(self) -> None:
83
- original = Concept("x", description="d", tags=["a"], metadata={"k": "v"})
84
- assert Concept.from_dict(original.to_dict()) == original
85
-
86
- def test_from_dict_invalid_name_type(self) -> None:
87
- with pytest.raises(ValueError, match="Invalid concept name"):
88
- Concept.from_dict({"name": 123, "description": None, "tags": [], "metadata": {}})
89
-
90
- def test_from_dict_invalid_description_type(self) -> None:
91
- with pytest.raises(ValueError, match="Invalid concept description"):
92
- Concept.from_dict({"name": "x", "description": 5, "tags": [], "metadata": {}})
93
-
94
- def test_from_dict_invalid_tags_not_list(self) -> None:
95
- with pytest.raises(ValueError, match="Invalid concept tags"):
96
- Concept.from_dict({"name": "x", "description": None, "tags": "a", "metadata": {}})
97
-
98
- def test_from_dict_invalid_tags_element(self) -> None:
99
- with pytest.raises(ValueError, match="Invalid concept tags"):
100
- Concept.from_dict({"name": "x", "description": None, "tags": [1], "metadata": {}})
101
-
102
- def test_from_dict_invalid_metadata_not_dict(self) -> None:
103
- with pytest.raises(ValueError, match="Invalid concept metadata"):
104
- Concept.from_dict({"name": "x", "description": None, "tags": [], "metadata": []})
105
-
106
- def test_from_dict_invalid_metadata_value_type(self) -> None:
107
- with pytest.raises(ValueError, match="Invalid concept metadata"):
108
- Concept.from_dict({"name": "x", "description": None, "tags": [], "metadata": {"k": 1}})
109
-
110
- def test_from_dict_missing_key_raises(self) -> None:
111
- with pytest.raises(KeyError):
112
- Concept.from_dict({"description": None, "tags": [], "metadata": {}})
113
-
114
-
115
- # ---------------------------------------------------------------------- #
116
- # Relation
117
- # ---------------------------------------------------------------------- #
118
-
119
-
120
- class TestRelation:
121
- def test_defaults_weight_to_one(self) -> None:
122
- r = Relation("a", "b", "is-a")
123
- assert r.weight == 1.0
124
-
125
- def test_stores_fields(self) -> None:
126
- r = Relation("a", "b", "is-a", weight=2.5)
127
- assert r.source == "a"
128
- assert r.target == "b"
129
- assert r.kind == "is-a"
130
- assert r.weight == 2.5
131
-
132
- def test_to_dict(self) -> None:
133
- r = Relation("a", "b", "is-a", weight=0.5)
134
- assert r.to_dict() == {"source": "a", "target": "b", "kind": "is-a", "weight": 0.5}
135
-
136
- def test_from_dict_valid(self) -> None:
137
- r = Relation.from_dict({"source": "a", "target": "b", "kind": "is-a", "weight": 1.0})
138
- assert r == Relation("a", "b", "is-a", weight=1.0)
139
-
140
- def test_from_dict_coerces_int_weight_to_float(self) -> None:
141
- r = Relation.from_dict({"source": "a", "target": "b", "kind": "is-a", "weight": 3})
142
- assert r.weight == 3.0
143
- assert isinstance(r.weight, float)
144
-
145
- def test_from_dict_roundtrip(self) -> None:
146
- original = Relation("a", "b", "is-a", weight=0.5)
147
- assert Relation.from_dict(original.to_dict()) == original
148
-
149
- def test_from_dict_invalid_source(self) -> None:
150
- with pytest.raises(ValueError, match="Invalid relation source"):
151
- Relation.from_dict({"source": 1, "target": "b", "kind": "is-a", "weight": 1.0})
152
-
153
- def test_from_dict_invalid_target(self) -> None:
154
- with pytest.raises(ValueError, match="Invalid relation target"):
155
- Relation.from_dict({"source": "a", "target": 1, "kind": "is-a", "weight": 1.0})
156
-
157
- def test_from_dict_invalid_kind(self) -> None:
158
- with pytest.raises(ValueError, match="Invalid relation kind"):
159
- Relation.from_dict({"source": "a", "target": "b", "kind": 1, "weight": 1.0})
160
-
161
- def test_from_dict_invalid_weight_type(self) -> None:
162
- with pytest.raises(ValueError, match="Invalid relation weight"):
163
- Relation.from_dict({"source": "a", "target": "b", "kind": "is-a", "weight": "x"})
164
-
165
- def test_from_dict_rejects_bool_weight(self) -> None:
166
- with pytest.raises(ValueError, match="Invalid relation weight"):
167
- Relation.from_dict({"source": "a", "target": "b", "kind": "is-a", "weight": True})
168
-
169
- def test_from_dict_missing_key_raises(self) -> None:
170
- with pytest.raises(KeyError):
171
- Relation.from_dict({"source": "a", "target": "b", "kind": "is-a"})
172
-
173
-
174
- # ---------------------------------------------------------------------- #
175
- # KnowledgeGraph — construction
176
- # ---------------------------------------------------------------------- #
177
-
178
-
179
- class TestGraphConstruction:
180
- def test_empty_graph(self) -> None:
181
- kg = KnowledgeGraph(name="empty")
182
- assert kg.concepts == {}
183
- assert kg.relations == []
184
-
185
- def test_add_concept_returns_stored_node(self) -> None:
186
- kg = KnowledgeGraph(name="g")
187
- c = kg.add_concept("x", description="d", tags=["t"], metadata={"k": "v"})
188
- assert c is kg.concepts["x"]
189
- assert c.name == "x"
190
- assert c.description == "d"
191
-
192
- def test_add_concept_is_idempotent(self) -> None:
193
- kg = KnowledgeGraph(name="g")
194
- first = kg.add_concept("x", description="first")
195
- second = kg.add_concept("x", description="second")
196
- assert first is second
197
- assert second.description == "first" # not overwritten
198
-
199
- def test_add_concept_tags_default_empty_list(self) -> None:
200
- kg = KnowledgeGraph(name="g")
201
- c = kg.add_concept("x")
202
- assert c.tags == []
203
- assert c.metadata == {}
204
-
205
- def test_add_concept_copies_input_collections(self) -> None:
206
- kg = KnowledgeGraph(name="g")
207
- tags = ["a"]
208
- meta = {"k": "v"}
209
- c = kg.add_concept("x", tags=tags, metadata=meta)
210
- tags.append("b")
211
- meta["z"] = "1"
212
- assert c.tags == ["a"]
213
- assert c.metadata == {"k": "v"}
214
-
215
- def test_add_relation_requires_known_endpoints(self) -> None:
216
- kg = KnowledgeGraph(name="g")
217
- kg.add_concept("a")
218
- kg.add_concept("b")
219
- r = kg.add_relation("a", "b", "is-a", weight=2.0)
220
- assert r in kg.relations
221
- assert r.weight == 2.0
222
-
223
- def test_add_relation_unknown_source_raises(self) -> None:
224
- kg = KnowledgeGraph(name="g")
225
- kg.add_concept("b")
226
- with pytest.raises(KeyError, match="unknown source concept"):
227
- kg.add_relation("missing", "b", "is-a")
228
-
229
- def test_add_relation_unknown_target_raises(self) -> None:
230
- kg = KnowledgeGraph(name="g")
231
- kg.add_concept("a")
232
- with pytest.raises(KeyError, match="unknown target concept"):
233
- kg.add_relation("a", "missing", "is-a")
234
-
235
- def test_link_concepts_creates_missing(self) -> None:
236
- kg = KnowledgeGraph(name="g")
237
- kg.link_concepts("a", "b", "is-a")
238
- assert "a" in kg.concepts
239
- assert "b" in kg.concepts
240
- assert len(kg.relations) == 1
241
-
242
- def test_link_concepts_reuses_existing(self) -> None:
243
- kg = KnowledgeGraph(name="g")
244
- kg.add_concept("a", description="keep")
245
- kg.link_concepts("a", "b", "is-a")
246
- # Existing concept is preserved (idempotent add).
247
- assert kg.concepts["a"].description == "keep"
248
-
249
-
250
- # ---------------------------------------------------------------------- #
251
- # KnowledgeGraph — neighbor queries
252
- # ---------------------------------------------------------------------- #
253
-
254
-
255
- class TestGraphNeighbors:
256
- def _graph(self) -> KnowledgeGraph:
257
- # a --is-a--> b --is-a--> c
258
- # a --related-to--> c
259
- kg = KnowledgeGraph(name="g")
260
- kg.add_concept("a")
261
- kg.add_concept("b")
262
- kg.add_concept("c")
263
- kg.add_relation("a", "b", "is-a")
264
- kg.add_relation("b", "c", "is-a")
265
- kg.add_relation("a", "c", "related-to")
266
- return kg
267
-
268
- def test_neighbors_undirected_both_directions(self) -> None:
269
- kg = self._graph()
270
- assert set(kg.neighbors("a")) == {"b", "c"}
271
- assert set(kg.neighbors("b")) == {"a", "c"}
272
- assert set(kg.neighbors("c")) == {"a", "b"}
273
-
274
- def test_neighbors_filtered_by_kind(self) -> None:
275
- kg = self._graph()
276
- assert set(kg.neighbors("a", kind="is-a")) == {"b"}
277
- assert set(kg.neighbors("a", kind="related-to")) == {"c"}
278
- assert kg.neighbors("a", kind="nonexistent") == []
279
-
280
- def test_neighbors_dedupes(self) -> None:
281
- kg = KnowledgeGraph(name="g")
282
- kg.add_concept("a")
283
- kg.add_concept("b")
284
- # Two relations between the same pair: b should appear once.
285
- kg.add_relation("a", "b", "is-a")
286
- kg.add_relation("b", "a", "related-to")
287
- assert kg.neighbors("a") == ["b"]
288
-
289
- def test_neighbors_unknown_concept_raises(self) -> None:
290
- kg = self._graph()
291
- with pytest.raises(KeyError, match="unknown concept"):
292
- kg.neighbors("missing")
293
-
294
- def test_neighbors_out_only_outgoing(self) -> None:
295
- kg = self._graph()
296
- assert set(kg.neighbors_out("a")) == {"b", "c"}
297
- assert kg.neighbors_out("b") == ["c"]
298
- assert kg.neighbors_out("c") == []
299
-
300
- def test_neighbors_out_filtered_by_kind(self) -> None:
301
- kg = self._graph()
302
- assert kg.neighbors_out("a", kind="is-a") == ["b"]
303
- assert kg.neighbors_out("a", kind="related-to") == ["c"]
304
-
305
- def test_neighbors_out_unknown_raises(self) -> None:
306
- kg = self._graph()
307
- with pytest.raises(KeyError, match="unknown concept"):
308
- kg.neighbors_out("missing")
309
-
310
- def test_neighbors_in_only_incoming(self) -> None:
311
- kg = self._graph()
312
- assert kg.neighbors_in("a") == []
313
- assert set(kg.neighbors_in("b")) == {"a"}
314
- assert set(kg.neighbors_in("c")) == {"a", "b"}
315
-
316
- def test_neighbors_in_filtered_by_kind(self) -> None:
317
- kg = self._graph()
318
- assert kg.neighbors_in("c", kind="is-a") == ["b"]
319
- assert kg.neighbors_in("c", kind="related-to") == ["a"]
320
-
321
- def test_neighbors_in_unknown_raises(self) -> None:
322
- kg = self._graph()
323
- with pytest.raises(KeyError, match="unknown concept"):
324
- kg.neighbors_in("missing")
325
-
326
-
327
- # ---------------------------------------------------------------------- #
328
- # KnowledgeGraph — traversal (path, reachability, cycles)
329
- # ---------------------------------------------------------------------- #
330
-
331
-
332
- class TestGraphTraversal:
333
- def test_find_path_source_equals_target(self) -> None:
334
- kg = KnowledgeGraph(name="g")
335
- kg.add_concept("a")
336
- assert kg.find_path("a", "a") == ["a"]
337
-
338
- def test_find_path_direct_neighbor(self) -> None:
339
- kg = KnowledgeGraph(name="g")
340
- kg.link_concepts("a", "b", "is-a")
341
- assert kg.find_path("a", "b") == ["a", "b"]
342
-
343
- def test_find_path_multi_hop(self) -> None:
344
- kg = KnowledgeGraph(name="g")
345
- for pair in [("a", "b"), ("b", "c"), ("c", "d")]:
346
- kg.link_concepts(*pair, kind="is-a")
347
- assert kg.find_path("a", "d") == ["a", "b", "c", "d"]
348
-
349
- def test_find_path_traverses_against_direction(self) -> None:
350
- # Only edge is b -> a; path from a to b must go "backwards".
351
- kg = KnowledgeGraph(name="g")
352
- kg.add_concept("a")
353
- kg.add_concept("b")
354
- kg.add_relation("b", "a", "is-a")
355
- assert kg.find_path("a", "b") == ["a", "b"]
356
-
357
- def test_find_path_no_connection_returns_none(self) -> None:
358
- kg = KnowledgeGraph(name="g")
359
- kg.add_concept("a")
360
- kg.add_concept("b")
361
- assert kg.find_path("a", "b") is None
362
-
363
- def test_find_path_unknown_source_returns_none(self) -> None:
364
- kg = KnowledgeGraph(name="g")
365
- kg.add_concept("a")
366
- assert kg.find_path("missing", "a") is None
367
-
368
- def test_find_path_unknown_target_returns_none(self) -> None:
369
- kg = KnowledgeGraph(name="g")
370
- kg.add_concept("a")
371
- assert kg.find_path("a", "missing") is None
372
-
373
- def test_find_path_returns_shortest(self) -> None:
374
- # Triangle: a-b-c plus a direct shortcut a-c.
375
- kg = KnowledgeGraph(name="g")
376
- kg.link_concepts("a", "b", "is-a")
377
- kg.link_concepts("b", "c", "is-a")
378
- kg.link_concepts("a", "c", "is-a")
379
- # BFS finds the 2-hop [a, c] via the shortcut, not [a,b,c].
380
- assert len(kg.find_path("a", "c") or []) == 2 # noqa: PLR2004
381
-
382
- def test_reachable_includes_self(self) -> None:
383
- kg = KnowledgeGraph(name="g")
384
- kg.add_concept("a")
385
- assert kg.reachable("a") == {"a"}
386
-
387
- def test_reachable_traverses_graph(self) -> None:
388
- kg = KnowledgeGraph(name="g")
389
- kg.link_concepts("a", "b", "is-a")
390
- kg.link_concepts("b", "c", "is-a")
391
- assert kg.reachable("a") == {"a", "b", "c"}
392
-
393
- def test_reachable_unknown_concept_returns_empty(self) -> None:
394
- kg = KnowledgeGraph(name="g")
395
- assert kg.reachable("missing") == set()
396
-
397
- def test_find_cycles_empty(self) -> None:
398
- kg = KnowledgeGraph(name="g")
399
- kg.link_concepts("a", "b", "is-a")
400
- assert kg.find_cycles() == []
401
-
402
- def test_find_cycles_simple_triangle(self) -> None:
403
- kg = KnowledgeGraph(name="g")
404
- kg.link_concepts("a", "b", "is-a")
405
- kg.link_concepts("b", "c", "is-a")
406
- kg.link_concepts("c", "a", "is-a")
407
- cycles = kg.find_cycles()
408
- assert cycles == [["a", "b", "c"]]
409
-
410
- def test_find_cycles_normalized_to_smallest_member(self) -> None:
411
- # Same cycle entered from different nodes collapses to one entry.
412
- kg = KnowledgeGraph(name="g")
413
- kg.link_concepts("c", "a", "is-a") # entered via c first
414
- kg.link_concepts("a", "b", "is-a")
415
- kg.link_concepts("b", "c", "is-a")
416
- cycles = kg.find_cycles()
417
- assert cycles == [["a", "b", "c"]]
418
-
419
- def test_find_cycles_self_loop(self) -> None:
420
- kg = KnowledgeGraph(name="g")
421
- kg.add_concept("a")
422
- kg.add_relation("a", "a", "is-a")
423
- assert kg.find_cycles() == [["a"]]
424
-
425
- def test_find_cycles_multiple_distinct(self) -> None:
426
- kg = KnowledgeGraph(name="g")
427
- for n in ["a", "b", "c", "d", "e", "f"]:
428
- kg.add_concept(n)
429
- # Cycle 1: a -> b -> c -> a
430
- kg.add_relation("a", "b", "is-a")
431
- kg.add_relation("b", "c", "is-a")
432
- kg.add_relation("c", "a", "is-a")
433
- # Cycle 2: d -> e -> f -> d
434
- kg.add_relation("d", "e", "is-a")
435
- kg.add_relation("e", "f", "is-a")
436
- kg.add_relation("f", "d", "is-a")
437
- cycles = kg.find_cycles()
438
- assert cycles == [["a", "b", "c"], ["d", "e", "f"]]
439
-
440
- def test_find_cycles_does_not_hit_recursion_limit(self) -> None:
441
- # A long chain with a closing edge back to the start.
442
- n = 5000
443
- kg = KnowledgeGraph(name="deep")
444
- for i in range(n):
445
- kg.add_concept(f"c{i}")
446
- for i in range(n - 1):
447
- kg.add_relation(f"c{i}", f"c{i + 1}", "is-a")
448
- kg.add_relation(f"c{n - 1}", "c0", "is-a") # close the loop
449
- cycles = kg.find_cycles()
450
- assert len(cycles) == 1
451
- assert len(cycles[0]) == n
452
- assert cycles[0][0] == "c0"
453
-
454
-
455
- # ---------------------------------------------------------------------- #
456
- # KnowledgeGraph — rendering & serialization
457
- # ---------------------------------------------------------------------- #
458
-
459
-
460
- class TestGraphRendering:
461
- def test_to_markdown_full(self) -> None:
462
- kg = KnowledgeGraph(name="My Graph")
463
- kg.link_concepts("a", "b", "is-a")
464
- md = kg.to_markdown()
465
- assert "# Knowledge Graph: My Graph" in md
466
- assert "## Concepts" in md
467
- assert "## Relations" in md
468
- assert "**a**" in md
469
- assert "`a` --is-a--> `b`" in md
470
-
471
- def test_to_markdown_no_concepts(self) -> None:
472
- kg = KnowledgeGraph(name="empty")
473
- md = kg.to_markdown()
474
- assert "_No concepts._" in md
475
- assert "_No relations._" in md
476
-
477
- def test_to_markdown_concepts_sorted(self) -> None:
478
- kg = KnowledgeGraph(name="g")
479
- kg.add_concept("zeta")
480
- kg.add_concept("alpha")
481
- md = kg.to_markdown()
482
- assert md.index("**alpha**") < md.index("**zeta**")
483
-
484
- def test_to_dict_structure(self) -> None:
485
- kg = KnowledgeGraph(name="g")
486
- kg.link_concepts("a", "b", "is-a")
487
- d = kg.to_dict()
488
- assert d["name"] == "g"
489
- concepts = cast(list[dict[str, object]], d["concepts"])
490
- assert [c["name"] for c in concepts] == ["a", "b"]
491
- relations = cast(list[dict[str, object]], d["relations"])
492
- assert relations[0]["kind"] == "is-a"
493
-
494
- def test_from_dict_roundtrip(self) -> None:
495
- kg = KnowledgeGraph(name="g")
496
- kg.add_concept("a", description="d", tags=["t"])
497
- kg.link_concepts("a", "b", "is-a", weight=2.0)
498
- rebuilt = KnowledgeGraph.from_dict(kg.to_dict())
499
- assert rebuilt.name == "g"
500
- assert set(rebuilt.concepts) == {"a", "b"}
501
- assert rebuilt.concepts["a"].description == "d"
502
- assert len(rebuilt.relations) == 1
503
- assert rebuilt.relations[0].weight == 2.0
504
-
505
- def test_from_dict_preserves_concept_order(self) -> None:
506
- kg = KnowledgeGraph(name="g")
507
- kg.add_concept("z")
508
- kg.add_concept("a")
509
- rebuilt = KnowledgeGraph.from_dict(kg.to_dict())
510
- # concepts dict preserves insertion order from the serialized list.
511
- assert list(rebuilt.concepts.keys()) == ["z", "a"]
512
-
513
- def test_from_dict_invalid_name(self) -> None:
514
- with pytest.raises(ValueError, match="Invalid graph name"):
515
- KnowledgeGraph.from_dict({"name": 1, "concepts": [], "relations": []})
516
-
517
- def test_from_dict_invalid_concepts_list(self) -> None:
518
- with pytest.raises(ValueError, match="Invalid concepts list"):
519
- KnowledgeGraph.from_dict({"name": "g", "concepts": "x", "relations": []})
520
-
521
- def test_from_dict_invalid_relations_list(self) -> None:
522
- with pytest.raises(ValueError, match="Invalid relations list"):
523
- KnowledgeGraph.from_dict({"name": "g", "concepts": [], "relations": "x"})
524
-
525
- def test_from_dict_invalid_concept_entry(self) -> None:
526
- with pytest.raises(ValueError, match="Invalid concept entry"):
527
- KnowledgeGraph.from_dict({"name": "g", "concepts": ["not-a-dict"], "relations": []})
528
-
529
- def test_from_dict_invalid_relation_entry(self) -> None:
530
- with pytest.raises(ValueError, match="Invalid relation entry"):
531
- KnowledgeGraph.from_dict({"name": "g", "concepts": [], "relations": ["x"]})
532
-
533
- def test_save_load_roundtrip(self, tmp_path: Path) -> None:
534
- kg = KnowledgeGraph(name="g")
535
- kg.link_concepts("a", "b", "is-a")
536
- path = tmp_path / "kg.json"
537
- kg.save(path)
538
- loaded = KnowledgeGraph.load(path)
539
- assert set(loaded.concepts) == {"a", "b"}
540
- assert len(loaded.relations) == 1
541
-
542
- def test_save_writes_indented_utf8_json(self, tmp_path) -> None: # type: ignore[no-untyped-def]
543
- kg = KnowledgeGraph(name="g")
544
- kg.add_concept("λ") # non-ASCII to verify ensure_ascii=False
545
- path = tmp_path / "kg.json"
546
- kg.save(path)
547
- text = path.read_text(encoding="utf-8")
548
- assert "λ" in text
549
- assert "\n" in text # indented
550
-
551
- def test_load_non_dict_raises(self, tmp_path) -> None: # type: ignore[no-untyped-def]
552
- path = tmp_path / "bad.json"
553
- path.write_text("[1, 2, 3]", encoding="utf-8")
554
- with pytest.raises(ValueError, match="Invalid graph file"):
555
- KnowledgeGraph.load(path)
556
-
557
- def test_load_roundtrips_loaded_via_json_module(self, tmp_path) -> None: # type: ignore[no-untyped-def]
558
- # Ensure the persisted form is plain JSON any reader can parse.
559
- kg = KnowledgeGraph(name="g")
560
- kg.link_concepts("a", "b", "is-a")
561
- path = tmp_path / "kg.json"
562
- kg.save(path)
563
- raw = json.loads(path.read_text(encoding="utf-8"))
564
- assert raw["name"] == "g"
565
-
566
-
567
- # ---------------------------------------------------------------------- #
568
- # Note
569
- # ---------------------------------------------------------------------- #
570
-
571
-
572
- class TestNote:
573
- def test_defaults(self) -> None:
574
- n = Note(id="n1", title="t", body="b")
575
- assert n.tags == []
576
- assert n.linked_concepts == []
577
- assert n.created is None
578
-
579
- def test_to_dict(self) -> None:
580
- n = Note(id="n1", title="t", body="b", tags=["x"], linked_concepts=["c"], created="2024")
581
- assert n.to_dict() == {
582
- "id": "n1",
583
- "title": "t",
584
- "body": "b",
585
- "tags": ["x"],
586
- "linked_concepts": ["c"],
587
- "created": "2024",
588
- }
589
-
590
- def test_to_dict_copies_mutable_fields(self) -> None:
591
- n = Note(id="n1", title="t", body="b", tags=["x"], linked_concepts=["c"])
592
- d = n.to_dict()
593
- cast(list[str], d["tags"]).append("y")
594
- cast(list[str], d["linked_concepts"]).append("z")
595
- assert n.tags == ["x"]
596
- assert n.linked_concepts == ["c"]
597
-
598
- def test_from_dict_valid(self) -> None:
599
- n = Note.from_dict(
600
- {
601
- "id": "n1",
602
- "title": "t",
603
- "body": "b",
604
- "tags": ["x"],
605
- "linked_concepts": ["c"],
606
- "created": "2024",
607
- }
608
- )
609
- assert n.id == "n1"
610
- assert n.tags == ["x"]
611
- assert n.created == "2024"
612
-
613
- def test_from_dict_null_created(self) -> None:
614
- n = Note.from_dict(
615
- {
616
- "id": "n1",
617
- "title": "t",
618
- "body": "b",
619
- "tags": [],
620
- "linked_concepts": [],
621
- "created": None,
622
- }
623
- )
624
- assert n.created is None
625
-
626
- def test_from_dict_roundtrip(self) -> None:
627
- original = Note(
628
- id="n1", title="t", body="b", tags=["x"], linked_concepts=["c"], created="2024"
629
- )
630
- rebuilt = Note.from_dict(original.to_dict())
631
- assert rebuilt.id == original.id
632
- assert rebuilt.tags == original.tags
633
-
634
- def test_from_dict_invalid_id(self) -> None:
635
- with pytest.raises(ValueError, match="Invalid note id"):
636
- Note.from_dict(
637
- {
638
- "id": 1,
639
- "title": "t",
640
- "body": "b",
641
- "tags": [],
642
- "linked_concepts": [],
643
- "created": None,
644
- }
645
- )
646
-
647
- def test_from_dict_invalid_title(self) -> None:
648
- with pytest.raises(ValueError, match="Invalid note title"):
649
- Note.from_dict(
650
- {
651
- "id": "n1",
652
- "title": 1,
653
- "body": "b",
654
- "tags": [],
655
- "linked_concepts": [],
656
- "created": None,
657
- }
658
- )
659
-
660
- def test_from_dict_invalid_body(self) -> None:
661
- with pytest.raises(ValueError, match="Invalid note body"):
662
- Note.from_dict(
663
- {
664
- "id": "n1",
665
- "title": "t",
666
- "body": 1,
667
- "tags": [],
668
- "linked_concepts": [],
669
- "created": None,
670
- }
671
- )
672
-
673
- def test_from_dict_invalid_tags(self) -> None:
674
- with pytest.raises(ValueError, match="Invalid note tags"):
675
- Note.from_dict(
676
- {
677
- "id": "n1",
678
- "title": "t",
679
- "body": "b",
680
- "tags": [1],
681
- "linked_concepts": [],
682
- "created": None,
683
- }
684
- )
685
-
686
- def test_from_dict_invalid_linked_concepts(self) -> None:
687
- with pytest.raises(ValueError, match="Invalid note linked_concepts"):
688
- Note.from_dict(
689
- {
690
- "id": "n1",
691
- "title": "t",
692
- "body": "b",
693
- "tags": [],
694
- "linked_concepts": [1],
695
- "created": None,
696
- }
697
- )
698
-
699
- def test_from_dict_invalid_created(self) -> None:
700
- with pytest.raises(ValueError, match="Invalid note created"):
701
- Note.from_dict(
702
- {
703
- "id": "n1",
704
- "title": "t",
705
- "body": "b",
706
- "tags": [],
707
- "linked_concepts": [],
708
- "created": 2024,
709
- }
710
- )
711
-
712
- def test_from_dict_missing_key_raises(self) -> None:
713
- with pytest.raises(KeyError):
714
- Note.from_dict(
715
- {"title": "t", "body": "b", "tags": [], "linked_concepts": [], "created": None}
716
- )
717
-
718
- def test_to_markdown_with_all_fields(self) -> None:
719
- n = Note(
720
- id="n1",
721
- title="t",
722
- body="b",
723
- tags=["x", "y"],
724
- linked_concepts=["c1", "c2"],
725
- created="2024",
726
- )
727
- md = n.to_markdown()
728
- assert md.startswith("# t")
729
- assert "_Created: 2024_" in md
730
- assert "`x`" in md and "`y`" in md
731
- assert "## Linked concepts" in md
732
- assert "`c1`" in md
733
- assert md.rstrip().endswith("b") is False or "b" in md # body present
734
-
735
- def test_to_markdown_minimal(self) -> None:
736
- n = Note(id="n1", title="t", body="b")
737
- md = n.to_markdown()
738
- assert "_Created:" not in md
739
- assert "Tags:" not in md
740
- assert "## Linked concepts" not in md
741
-
742
-
743
- # ---------------------------------------------------------------------- #
744
- # Notebook
745
- # ---------------------------------------------------------------------- #
746
-
747
-
748
- class TestNotebook:
749
- def test_add_note_returns_stored(self) -> None:
750
- nb = Notebook(name="nb")
751
- n = nb.add_note("n1", "title", "body", tags=["t"], linked_concepts=["c"], created="2024")
752
- assert n is nb.notes["n1"]
753
- assert n.title == "title"
754
-
755
- def test_add_note_overwrites_existing(self) -> None:
756
- nb = Notebook(name="nb")
757
- nb.add_note("n1", "first", "b1")
758
- nb.add_note("n1", "second", "b2")
759
- assert len(nb.notes) == 1
760
- assert nb.notes["n1"].title == "second"
761
-
762
- def test_add_note_defaults_empty_collections(self) -> None:
763
- nb = Notebook(name="nb")
764
- n = nb.add_note("n1", "t", "b")
765
- assert n.tags == []
766
- assert n.linked_concepts == []
767
- assert n.created is None
768
-
769
- def test_add_note_copies_input_collections(self) -> None:
770
- nb = Notebook(name="nb")
771
- tags = ["a"]
772
- linked = ["c"]
773
- n = nb.add_note("n1", "t", "b", tags=tags, linked_concepts=linked)
774
- tags.append("b")
775
- linked.append("d")
776
- assert n.tags == ["a"]
777
- assert n.linked_concepts == ["c"]
778
-
779
- def test_get_note_found(self) -> None:
780
- nb = Notebook(name="nb")
781
- nb.add_note("n1", "t", "b")
782
- assert nb.get_note("n1").title == "t"
783
-
784
- def test_get_note_unknown_raises(self) -> None:
785
- nb = Notebook(name="nb")
786
- with pytest.raises(KeyError, match="unknown note"):
787
- nb.get_note("missing")
788
-
789
- def test_notes_for_concept(self) -> None:
790
- nb = Notebook(name="nb")
791
- nb.add_note("n1", "t1", "b", linked_concepts=["c1", "c2"])
792
- nb.add_note("n2", "t2", "b", linked_concepts=["c2"])
793
- nb.add_note("n3", "t3", "b", linked_concepts=[])
794
- assert [n.id for n in nb.notes_for_concept("c2")] == ["n1", "n2"]
795
- assert nb.notes_for_concept("c1") == [nb.notes["n1"]]
796
- assert nb.notes_for_concept("absent") == []
797
-
798
- def test_notes_by_tag(self) -> None:
799
- nb = Notebook(name="nb")
800
- nb.add_note("n1", "t1", "b", tags=["exp", "fail"])
801
- nb.add_note("n2", "t2", "b", tags=["exp"])
802
- assert [n.id for n in nb.notes_by_tag("exp")] == ["n1", "n2"]
803
- assert nb.notes_by_tag("fail") == [nb.notes["n1"]]
804
- assert nb.notes_by_tag("absent") == []
805
-
806
- def test_to_markdown_empty(self) -> None:
807
- nb = Notebook(name="empty")
808
- md = nb.to_markdown()
809
- assert "# Notebook: empty" in md
810
- assert "_No notes._" in md
811
-
812
- def test_to_markdown_with_notes_sorted_by_id(self) -> None:
813
- nb = Notebook(name="nb")
814
- nb.add_note("n2", "second", "b", tags=["t"])
815
- nb.add_note("n1", "first", "b")
816
- md = nb.to_markdown()
817
- assert md.index("**n1**") < md.index("**n2**")
818
- assert "(t)" in md # tags rendered for n2
819
-
820
- def test_to_dict_structure(self) -> None:
821
- nb = Notebook(name="nb")
822
- nb.add_note("n1", "t", "b")
823
- d = nb.to_dict()
824
- assert d["name"] == "nb"
825
- assert cast(list[dict[str, object]], d["notes"])[0]["id"] == "n1"
826
-
827
- def test_from_dict_roundtrip(self) -> None:
828
- nb = Notebook(name="nb")
829
- nb.add_note("n1", "t", "b", tags=["x"], linked_concepts=["c"], created="2024")
830
- rebuilt = Notebook.from_dict(nb.to_dict())
831
- assert rebuilt.name == "nb"
832
- assert rebuilt.get_note("n1").title == "t"
833
- assert rebuilt.get_note("n1").tags == ["x"]
834
-
835
- def test_from_dict_invalid_name(self) -> None:
836
- with pytest.raises(ValueError, match="Invalid notebook name"):
837
- Notebook.from_dict({"name": 1, "notes": []})
838
-
839
- def test_from_dict_invalid_notes_list(self) -> None:
840
- with pytest.raises(ValueError, match="Invalid notes list"):
841
- Notebook.from_dict({"name": "nb", "notes": "x"})
842
-
843
- def test_from_dict_invalid_note_entry(self) -> None:
844
- with pytest.raises(ValueError, match="Invalid note entry"):
845
- Notebook.from_dict({"name": "nb", "notes": ["not-a-dict"]})
846
-
847
- def test_save_load_roundtrip(self, tmp_path: Path) -> None:
848
- nb = Notebook(name="nb")
849
- nb.add_note("n1", "t", "b")
850
- path = tmp_path / "nb.json"
851
- nb.save(path)
852
- loaded = Notebook.load(path)
853
- assert loaded.get_note("n1").title == "t"
854
-
855
- def test_load_non_dict_raises(self, tmp_path) -> None: # type: ignore[no-untyped-def]
856
- path = tmp_path / "bad.json"
857
- path.write_text("[]", encoding="utf-8")
858
- with pytest.raises(ValueError, match="Invalid notebook file"):
859
- Notebook.load(path)
860
-
861
- def test_save_writes_utf8(self, tmp_path) -> None: # type: ignore[no-untyped-def]
862
- nb = Notebook(name="café")
863
- nb.add_note("n1", "tître", "b")
864
- path = tmp_path / "nb.json"
865
- nb.save(path)
866
- text = path.read_text(encoding="utf-8")
867
- assert "café" in text
868
- assert "tître" in text
869
-
870
-
871
- # ---------------------------------------------------------------------- #
872
- # Retrieval — search_concepts
873
- # ---------------------------------------------------------------------- #
874
-
875
-
876
- class TestSearchConcepts:
877
- def _graph(self) -> KnowledgeGraph:
878
- kg = KnowledgeGraph(name="g")
879
- kg.add_concept("Gravity", description="attractive force between masses", tags=["physics"])
880
- kg.add_concept("Graviton", description="hypothetical quantum of gravity", tags=["physics"])
881
- kg.add_concept("Energy", description="capacity to do work", tags=["physics", "abstract"])
882
- kg.add_concept("Mass", tags=["physics"])
883
- return kg
884
-
885
- def test_exact_name_match_scores_highest(self) -> None:
886
- kg = self._graph()
887
- results = search_concepts(kg, "gravity")
888
- assert results[0].concept_name == "Gravity"
889
- assert results[0].score == NAME_TAG_SCORE
890
- assert results[0].matched_on == "name"
891
-
892
- def test_substring_name_match(self) -> None:
893
- kg = self._graph()
894
- results = search_concepts(kg, "grav")
895
- names = [r.concept_name for r in results]
896
- assert "Gravity" in names
897
- assert "Graviton" in names
898
- assert all(r.score == SUBSTRING_SCORE for r in results)
899
-
900
- def test_description_substring_match(self) -> None:
901
- kg = self._graph()
902
- results = search_concepts(kg, "masses")
903
- assert any(r.concept_name == "Gravity" and r.matched_on == "description" for r in results)
904
-
905
- def test_case_insensitive(self) -> None:
906
- kg = self._graph()
907
- assert search_concepts(kg, "GRAVITY")[0].concept_name == "Gravity"
908
- assert search_concepts(kg, "gravity")[0].concept_name == "Gravity"
909
-
910
- def test_no_match_returns_empty(self) -> None:
911
- kg = self._graph()
912
- assert search_concepts(kg, "nonexistent") == []
913
-
914
- def test_tag_filter_excludes_non_matching(self) -> None:
915
- kg = self._graph()
916
- results = search_concepts(kg, "energy", tag="abstract")
917
- assert [r.concept_name for r in results] == ["Energy"]
918
- # Without the abstract tag, Energy would still match.
919
- results2 = search_concepts(kg, "energy", tag="physics")
920
- assert "Energy" in [r.concept_name for r in results2]
921
-
922
- def test_tag_filter_no_matches_returns_empty(self) -> None:
923
- kg = self._graph()
924
- assert search_concepts(kg, "gravity", tag="absenttag") == []
925
-
926
- def test_ties_break_alphabetically(self) -> None:
927
- kg = self._graph()
928
- results = search_concepts(kg, "grav")
929
- # Gravity & Graviton both substring (0.5); ties break alphabetically:
930
- # "Graviton" < "Gravity" because 'i' < 'y'.
931
- assert [r.concept_name for r in results] == ["Graviton", "Gravity"]
932
-
933
- def test_results_sorted_by_score_then_name(self) -> None:
934
- kg = KnowledgeGraph(name="g")
935
- kg.add_concept("Zeta", description="contains needle")
936
- kg.add_concept("needle") # exact match -> 1.0
937
- kg.add_concept("Alpha", description="contains needle")
938
- results = search_concepts(kg, "needle")
939
- # Exact (needle) first, then Alpha & Zeta alphabetical.
940
- assert [r.concept_name for r in results] == ["needle", "Alpha", "Zeta"]
941
-
942
- def test_description_none_does_not_match_substring(self) -> None:
943
- # 'Mass' has description=None; a query present only in it must not match.
944
- kg = self._graph()
945
- results = search_concepts(kg, "physics-label")
946
- assert not any(r.concept_name == "Mass" for r in results)
947
-
948
-
949
- # ---------------------------------------------------------------------- #
950
- # Retrieval — search_notes
951
- # ---------------------------------------------------------------------- #
952
-
953
-
954
- class TestSearchNotes:
955
- def _notebook(self) -> Notebook:
956
- nb = Notebook(name="nb")
957
- nb.add_note("n1", "Gravity", "discusses masses", tags=["physics"])
958
- nb.add_note("n2", "Misc", "gravity is mentioned in body", tags=["physics"])
959
- nb.add_note("n3", "Unrelated", "nothing here")
960
- return nb
961
-
962
- def test_exact_title_match_scores_highest(self) -> None:
963
- nb = self._notebook()
964
- results = search_notes(nb, "gravity")
965
- assert results[0].note_id == "n1"
966
- assert results[0].score == NAME_TAG_SCORE
967
- assert results[0].matched_on == "title"
968
-
969
- def test_substring_title_match(self) -> None:
970
- nb = Notebook(name="nb")
971
- nb.add_note("n1", "Gravitational waves", "b")
972
- results = search_notes(nb, "grav")
973
- assert results[0].note_id == "n1"
974
- assert results[0].matched_on == "title"
975
- assert results[0].score == SUBSTRING_SCORE
976
-
977
- def test_body_substring_match(self) -> None:
978
- nb = self._notebook()
979
- results = search_notes(nb, "gravity")
980
- ids = [r.note_id for r in results]
981
- assert "n2" in ids
982
- n2 = next(r for r in results if r.note_id == "n2")
983
- assert n2.matched_on == "body"
984
- assert n2.score == SUBSTRING_SCORE
985
-
986
- def test_title_match_preferred_over_body(self) -> None:
987
- # n1 matches title exactly (1.0), n2 matches body (0.5).
988
- nb = self._notebook()
989
- results = search_notes(nb, "gravity")
990
- assert results[0].note_id == "n1"
991
-
992
- def test_case_insensitive(self) -> None:
993
- nb = self._notebook()
994
- assert search_notes(nb, "GRAVITY")[0].note_id == "n1"
995
-
996
- def test_no_match_returns_empty(self) -> None:
997
- nb = self._notebook()
998
- assert search_notes(nb, "nonexistent") == []
999
-
1000
- def test_tag_filter(self) -> None:
1001
- nb = self._notebook()
1002
- results = search_notes(nb, "gravity", tag="physics")
1003
- assert {r.note_id for r in results} == {"n1", "n2"}
1004
- # n3 has no 'physics' tag and wouldn't match anyway.
1005
-
1006
- def test_tag_filter_excludes(self) -> None:
1007
- nb = Notebook(name="nb")
1008
- nb.add_note("n1", "gravity", "b", tags=["a"])
1009
- nb.add_note("n2", "gravity", "b", tags=["b"])
1010
- results = search_notes(nb, "gravity", tag="a")
1011
- assert [r.note_id for r in results] == ["n1"]
1012
-
1013
- def test_results_sorted_by_score_then_id(self) -> None:
1014
- nb = Notebook(name="nb")
1015
- nb.add_note("zzz", "needle", "b") # exact 1.0
1016
- nb.add_note("bbb", "x", "needle in body") # substring 0.5
1017
- nb.add_note("aaa", "x", "needle in body") # substring 0.5
1018
- results = search_notes(nb, "needle")
1019
- assert [r.note_id for r in results] == ["zzz", "aaa", "bbb"]
1020
-
1021
-
1022
- # ---------------------------------------------------------------------- #
1023
- # Retrieval — combined search
1024
- # ---------------------------------------------------------------------- #
1025
-
1026
-
1027
- class TestSearchCombined:
1028
- def test_merges_concepts_and_notes(self) -> None:
1029
- kg = KnowledgeGraph(name="g")
1030
- kg.add_concept("gravity")
1031
- nb = Notebook(name="nb")
1032
- nb.add_note("n1", "gravity note", "b")
1033
- results = search(kg, nb, "gravity")
1034
- assert {r.concept_name for r in results if r.concept_name} == {"gravity"}
1035
- assert {r.note_id for r in results if r.note_id} == {"n1"}
1036
-
1037
- def test_combined_ranking(self) -> None:
1038
- kg = KnowledgeGraph(name="g")
1039
- kg.add_concept("needle", description="contains needle") # exact 1.0
1040
- nb = Notebook(name="nb")
1041
- nb.add_note("n1", "needle", "b") # exact 1.0
1042
- nb.add_note("n2", "x", "needle body") # substring 0.5
1043
- results = search(kg, nb, "needle")
1044
- # Two exact matches (1.0) first, ordered by identifier alphabetically,
1045
- # then the 0.5 body match. Note id "n1" and concept name "needle" sort
1046
- # before "n2"; "n1" < "needle" lexicographically.
1047
- scores = [r.score for r in results]
1048
- assert scores == sorted(scores, reverse=True)
1049
- assert results[-1].note_id == "n2"
1050
-
1051
- def test_combined_tag_filter_applied_to_both(self) -> None:
1052
- kg = KnowledgeGraph(name="g")
1053
- kg.add_concept("needle", tags=["keep"])
1054
- kg.add_concept("needle2", tags=["drop"])
1055
- nb = Notebook(name="nb")
1056
- nb.add_note("n1", "needle", "b", tags=["keep"])
1057
- nb.add_note("n2", "needle", "b", tags=["drop"])
1058
- results = search(kg, nb, "needle", tag="keep")
1059
- assert {r.concept_name for r in results if r.concept_name} == {"needle"}
1060
- assert {r.note_id for r in results if r.note_id} == {"n1"}
1061
-
1062
- def test_empty_stores_return_empty(self) -> None:
1063
- kg = KnowledgeGraph(name="g")
1064
- nb = Notebook(name="nb")
1065
- assert search(kg, nb, "anything") == []
1066
-
1067
-
1068
- # ---------------------------------------------------------------------- #
1069
- # Public API surface
1070
- # ---------------------------------------------------------------------- #
1071
-
1072
-
1073
- class TestPublicAPI:
1074
- def test_search_result_dataclass_fields(self) -> None:
1075
- r = SearchResult(concept_name="x", note_id=None, score=1.0, matched_on="name")
1076
- assert r.concept_name == "x"
1077
- assert r.note_id is None
1078
- assert r.score == 1.0
1079
- assert r.matched_on == "name"
1080
-
1081
- def test_search_result_note_variant(self) -> None:
1082
- r = SearchResult(concept_name=None, note_id="n1", score=0.5, matched_on="body")
1083
- assert r.concept_name is None
1084
- assert r.note_id == "n1"
1085
-
1086
- def test_all_documented_names_exported(self) -> None:
1087
- # cds.knowledge is already imported at module scope (above), so read
1088
- # it back from sys.modules instead of re-``import``-ing, which would
1089
- # mix ``import cds.knowledge`` with the module-scope ``from`` form.
1090
- import sys
1091
-
1092
- mod = sys.modules["cds.knowledge"]
1093
-
1094
- expected = {
1095
- "Concept",
1096
- "Relation",
1097
- "KnowledgeGraph",
1098
- "Note",
1099
- "Notebook",
1100
- "SearchResult",
1101
- "search",
1102
- "search_concepts",
1103
- "search_notes",
1104
- "RetrievalHit",
1105
- "rank_tfidf",
1106
- }
1107
- assert expected.issubset(set(dir(mod)))
1108
- assert expected == set(mod.__all__)
1109
-
1110
-
1111
- # ---------------------------------------------------------------------- #
1112
- # Integration: the documented end-to-end workflow
1113
- # ---------------------------------------------------------------------- #
1114
-
1115
-
1116
- class TestIntegration:
1117
- def test_full_workflow_roundtrip(self, tmp_path) -> None: # type: ignore[no-untyped-def]
1118
- kg = KnowledgeGraph(name="Cosmology")
1119
- kg.add_concept("Dark Energy", description="drives expansion", tags=["cosmology"])
1120
- kg.add_concept("Hubble Constant", tags=["cosmology"])
1121
- kg.add_relation("Dark Energy", "Hubble Constant", kind="affects")
1122
-
1123
- nb = Notebook(name="Lab")
1124
- nb.add_note(
1125
- "n1",
1126
- "Hubble Tension",
1127
- "local vs cmb measurements",
1128
- tags=["exp"],
1129
- linked_concepts=["Hubble Constant"],
1130
- )
1131
-
1132
- # Persistence round-trips for both.
1133
- gpath = tmp_path / "g.json"
1134
- npath = tmp_path / "n.json"
1135
- kg.save(gpath)
1136
- nb.save(npath)
1137
- kg2 = KnowledgeGraph.load(gpath)
1138
- nb2 = Notebook.load(npath)
1139
-
1140
- # Search works on reloaded data.
1141
- results = search(kg2, nb2, "hubble")
1142
- assert any(r.concept_name == "Hubble Constant" for r in results)
1143
- assert any(r.note_id == "n1" for r in results)
1144
-
1145
- # Traversal survives the round-trip.
1146
- assert kg2.find_path("Dark Energy", "Hubble Constant") == [
1147
- "Dark Energy",
1148
- "Hubble Constant",
1149
- ]