Python and C++ code refactoring - #9
Conversation
|
Hello David, thank you for reviewing my changes, they are a lot:
|
|
Another issue is that I have retrocompatibility with the tests until the tag v0.5.2 This KDE learning faster and removes the Singular Covariance Matrix error. If you like this change, I could adapt the tests so that they pass the results given this new method. |
|
Thank you @carloslihu. I will try to create a test with a case of almost 0 variance to try to control it. First, I wanna know where it is raising the error. About the diagonal matrices: yes, many authors recommend using diagonal matrices. Less parameters to estimate and usually small degradation of performance. That is why I defined ProductKDE (https://pybnesian.readthedocs.io/en/latest/api/factors.html#pybnesian.ProductKDE). I know that ProductKDE is not exactly the same as KDE with diagonal bandwidth matrix, but:
|
|
Hello David, For my implementation I wanted to use ProductKDE for Hill Climbing, but by lack of skill and time, I wasn't able to do so. This is just a temporary fix, I think that the best solution would be to allow the option of allowing the selection of your ProductKDE implementation for the Hill Climbing algorithm, but my lack of knowledge wouldn't let me. |
|
Hello David, I will continue using the diagonal bandwidth in another personal branch (feature/diagonal-bandwidth) for the moment |
Mixed kcmi & vp trees
… for float32 sampling
…onal dependencies
- Updated all instances of uppercase variable names (A, B, C, D) to lowercase (a, b, c, d) in the serialization tests for various Bayesian network models. - Ensured consistency across fixtures and assertions to reflect the new naming convention.
There was a problem hiding this comment.
🟡 Changes recommended
There are several correctness/build issues in the current diff (test import path, missing C++ includes, an integer-division bug in smoothing, and build configuration pitfalls) that should be addressed before merging.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
This PR modernizes PyBNesian’s Python/C++ codebase via formatting and documentation improvements, while also introducing new learning/independence-test functionality and build/dependency tweaks (including a version bump).
Changes:
- Reformats and refactors many Python tests and updates documentation/changelogs for the 0.6.0 release.
- Updates C++ internals around scoring/operators, KDE bandwidth handling, mutual information behavior, and introduces VP-tree–backed mixed-data mutual information tooling/bindings.
- Adjusts build/packaging configuration (vcpkg, CMake, requirements/pyproject metadata, CI workflows).
File summaries
| File | Description |
|---|---|
| vcpkg.json | Disables Arrow default features in vcpkg manifest. |
| vcpkg-configuration.json | Reformatting only. |
| tests/serialization/serialize_models_type_test.py | Import/order formatting and fixture/test formatting cleanup. |
| tests/serialization/serialize_factor_type_test.py | Refactors tests to use pbn.* types consistently; formatting. |
| tests/serialization/serialize_factor_test.py | Refactors to pbn.* usage and reformats; minor assertion adjustments. |
| tests/models/HeterogeneousBN_test.py | Formatting and minor comment cleanup. |
| tests/models/DynamicBayesianNetwork_test.py | Refactors to shared helpers, reformats, and adjusts regex handling. |
| tests/models/BayesianNetwork_type_test.py | Refactors to shared helpers and pbn.* usage. |
| tests/learning/scores/bic_test.py | Uses shared data helpers/constants; formatting. |
| tests/learning/parameters/mle_test.py | Uses shared data helpers/constants; small assertion additions. |
| tests/learning/operators/operatorstabuset_test.py | Formatting/whitespace cleanup. |
| tests/learning/operators/operatorset_test.py | Uses shared data helpers/constants; formatting; variable-name updates. |
| tests/learning/operators/operators_test.py | Formatting and consistent quoting. |
| tests/learning/operators/operatorpool_test.py | Uses shared data helpers/constants; adds simple pool creation check. |
| tests/learning/independence_tests/independence_test.py | New test suite for multiple independence tests. |
| tests/learning/algorithms/hillclimbing_test.py | Refactors to shared helpers; adds custom BN type/classes in-test; formatting. |
| tests/learning/algorithms/constraint_test.py | Refactors to pbn.* usage; formatting; adjusts node labels. |
| tests/helpers/util_test.py | Removes legacy helper module (replaced by tests/helpers/data.py). |
| tests/helpers/kde.py | Adds helper KDE routines for diagonal bandwidth / logpdf checks. |
| tests/factors/factor_type_test.py | Refactors to pbn.* base classes and formatting. |
| tests/factors/discrete/DiscreteFactor_test.py | Uses shared discrete data helper and refactors variable/category conventions. |
| tests/dataset/holdout_test.py | Uses shared data helpers/constants; formatting. |
| tests/conftest.py | Alters sys.path handling for test helper imports. |
| requirements.txt | Removes version specifiers for dependencies. |
| README.md | Updates badges/headers, repo clone URL, and formatting; links INSTALLATION.md. |
| pytest.ini | Expands norecursedirs and adds -s default addopts. |
| pyproject.toml | Version bump to 0.6.0; authors expanded; dependency metadata consolidated. |
| pybnesian/vptree/vptree.hpp | New VP-tree interface/types for kNN-based algorithms. |
| pybnesian/util/validate_whitelists.hpp | Adds Doxygen-style documentation blocks. |
| pybnesian/util/validate_options.hpp | Changes score-validation seed type to unsigned int. |
| pybnesian/util/validate_options.cpp | Adds documentation/comments; default score selection commentary; minor message text. |
| pybnesian/util/progress.hpp | Adds formatted_log_t and spinner documentation. |
| pybnesian/util/basic_eigen_ops.hpp | Refactors PSD check implementation and signature. |
| pybnesian/pybindings/pybindings_learning/pybindings_independences.cpp | Adds Python bindings for MixedKMutualInformation. |
| pybnesian/pybindings/pybindings_learning/pybindings_algorithms.cpp | Fixes docstring typo for validated-lik. |
| pybnesian/opencl/opencl_config.hpp | Adds comments clarifying logsumexp steps. |
| pybnesian/models/BayesianNetwork.hpp | Adds documentation and comments around blacklist/whitelist handling. |
| pybnesian/learning/scores/validated_likelihood.hpp | Adds class/method documentation comments. |
| pybnesian/learning/scores/holdout_likelihood.hpp | Adds class documentation comments. |
| pybnesian/learning/scores/cv_likelihood.hpp | Adds class documentation comments. |
| pybnesian/learning/scores/cv_likelihood.cpp | Adds documentation and clarifying comments. |
| pybnesian/learning/parameters/mle_DiscreteFactor.cpp | Adds smoothing for discrete MLE probability estimation. |
| pybnesian/learning/operators/operators.hpp | Adds deterministic comparator for delta sorting; adds documentation/comments. |
| pybnesian/learning/operators/operators.cpp | Adds documentation/comments; refactors scoring update comments; minor logic commentary. |
| pybnesian/learning/independences/hybrid/mutual_information.cpp | Ensures MI/CMI returns at least machine tolerance; guards pvalue against inf/nan. |
| pybnesian/learning/independences/hybrid/mixed_knncmi.hpp | New mixed-data kNN-CMI interface declarations. |
| pybnesian/learning/algorithms/hillclimbing.cpp | Adds extensive function documentation and commentary. |
| pybnesian/kde/ScottsBandwidth.hpp | Improves messages/comments; enforces diagonal covariance handling and PSD checks. |
| pybnesian/kde/ProductKDE.hpp | Include order tweak; adds bandwidth-related comments. |
| pybnesian/kde/opencl_kernels/KDE.cl.src | Expands header comment. |
| pybnesian/kde/NormalReferenceRule.hpp | Improves messages/comments; diagonal covariance handling adjustments. |
| pybnesian/kde/KDE.cpp | Adds method documentation comments. |
| pybnesian/factors/discrete/DiscreteFactor.cpp | Adds clarifying comment in ToString. |
| pybnesian/factors/continuous/LinearGaussianCPD.hpp | Makes data_type() depend on training type. |
| pybnesian/factors/continuous/LinearGaussianCPD.cpp | Tracks training type, improves sampling to support float/double, updates pickle state. |
| pybnesian/factors/continuous/CKDE.hpp | Adds _fit documentation comment. |
| pybnesian/factors/continuous/CKDE.cpp | Adds fit documentation comment. |
| pybnesian/dataset/holdout_adaptator.hpp | Fixes formatting in thrown message. |
| pybnesian/dataset/dataset.hpp | Fixes preprocessor conditional formatting. |
| pybnesian/dataset/dataset.cpp | Fixes preprocessor conditional formatting; adds documentation comments. |
| lib/eigen-3.3.7/scripts/relicense.py | Python 3 style/format fixes. |
| lib/eigen-3.3.7/debug/gdb/printers.py | Python 3 style/format fixes and type-ignore. |
| INSTALLATION.md | New detailed installation guide. |
| expand_sources.py | Formatting and minor refactor for file operations. |
| docs/source/conf.py | Updates version/authors and reformats Sphinx config. |
| docs/source/changelog.rst | Adds v0.5.2 entry. |
| docs/source/api/learning/independences.rst | Adds bibliography entries for new MI estimators. |
| conv_template.py | Formatting, typing, and minor structure cleanup. |
| CMakeLists.txt | Refactors conditionals; adds ccache; changes python invocation; adds vptree/mixed_knncmi sources. |
| CHANGELOG.md | Adds entries for 0.6.0/0.5.3 and formatting improvements. |
| .readthedocs.yaml | Minor formatting fix. |
| .gitignore | Adds wheelhouse to ignored outputs. |
| .github/workflows/test_release.yml | Captures/uploads vcpkg logs on failure. |
| .github/workflows/release.yml | Captures/uploads vcpkg logs on failure. |
Review details
- Files reviewed: 86/89 changed files
- Comments generated: 7
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| import sys | ||
| sys.path.append(os.path.join(os.path.dirname(__file__), "helpers")) | ||
|
|
||
| sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "tests"))) |
| #include <indicators/indicators.hpp> | ||
| // #include <sstream> | ||
| // #include <boost/format.hpp> // sudo apt install libboost-all-dev | ||
| #include <iostream> | ||
| // enum class log_level_t { LOG_NOTHING, LOG_CRITICAL, LOG_ERROR, LOG_WARNING, LOG_INFO, LOG_DEBUG }; |
| #include <dataset/dataset.hpp> | ||
| #include <queue> | ||
| #include <random> | ||
| #include <algorithm> | ||
| #include <boost/functional/hash/hash.hpp> |
| // Schurmann-Grassberger smoothing, lambda = 1 (uniform prior) | ||
| double lambda = 1 / cardinality(0); | ||
| double logsum_configuration = std::log(static_cast<double>(sum_configuration + lambda * cardinality(0))); |
| if(UNIX) | ||
| set(CMAKE_C_COMPILER "gcc") | ||
| set(CMAKE_CXX_COMPILER "g++") | ||
| endif() |
| "\" specified. The possible alternatives are " | ||
| "\"bic\" (Bayesian Information Criterion), \"bge\" (Bayesian Gaussian equivalent), " | ||
| "\"cv-lik\" (Cross-Validated likelihood), \"holdout-l\" (Hold-out likelihood) " | ||
| " or \"validated-lik\" (Validated likelihood with cross-validation)."); |
| ## 0.6.0 | ||
| - Fixed a bug with LinearGaussianCPD sampling | ||
| - Fixed pytests to work with diagonal bandwidth matrix. | ||
| - Added delta index comparator to fix seed in hc learning | ||
| - Refactor variable names in serialization tests to lowercase | ||
| - Updated installation guide with improved instructions and additional dependencies |
blackandisort, and has been refactored according toPEP 8style guides.googledocstring format.doxygendocstring format.bandwidthcalculation have been reordered and commented.ArcOperatorSet::update_incoming_arcs_scoresformulas have been reordered and commented.