Commit 2c6288d
Fix precision validation gaps and enhance insert() capabilities (#55)
* Fix precision validation gaps and enhance insert() capabilities
This commit addresses multiple precision and validation issues identified
in the codebase analysis:
## 1. Input Validation
- Add NaN/Inf validation to insert() methods for both float32 and float64
- Ensures consistency with constructor validation
- Prevents invalid data from entering the tree structure
## 2. Float64 Insert Support
- Add float64 overload for insert() method
- Maintains idx2exact map for dynamically inserted items
- Preserves double-precision refinement capability for inserted boxes
- Uses explicit py::overload_cast in Python bindings to handle overloads
## 3. Precision Testing
- Add comprehensive tests for NaN/Inf validation in insert operations
- Add tests for float64 insert() maintaining precision
- Add tests verifying rebuild() preserves idx2exact
- Add systematic precision boundary tests (adjusted for float32 limits)
- Document float32 precision limitations in test comments
## Technical Notes
- Float64 input is converted to float32 for tree structure
- Double-precision refinement helps reduce false positives
- Precision limits: gaps below ~1e-7 may not be reliably detected
- At large magnitudes (e.g., 1e6), absolute precision degrades
Fixes validation gaps in insert operations and maintains precision
capabilities for dynamically updated trees.
* Add adaptive epsilon and configurable precision parameters
Addresses Float32 Precision Issues from precision validation gaps:
- Add adaptive epsilon calculation that scales with coordinate magnitude
- Add configurable precision parameters (relative_epsilon, absolute_epsilon)
- Implement subnormal number detection in validate_box()
- Update both insert() overloads to use adaptive epsilon
- Add precision control methods (setters/getters) to PRTree class
- Expose precision control to Python via pybind11 bindings
- Add comprehensive test suite for adaptive epsilon behavior
This improves precision handling across different coordinate scales,
from small (< 1.0) to large (> 1e6) magnitudes.
Note: Current architecture still forces float32 tree structure on all
users. Float64 data only used for idx2exact refinement. Future work
should consider separating float32/float64 builds or templating Real type.
* Complete architectural refactoring for native precision support
This commit eliminates the complex idx2exact post-processing architecture
and replaces it with native float32/float64 template specialization,
significantly simplifying the codebase and optimizing for each precision level.
Key Changes:
- Templated PRTree with Real type parameter (float or double)
- Removed idx2exact map and refine_candidates() complexity entirely
- Exposed 6 separate C++ classes (_PRTree{2D,3D,4D}_{float32,float64})
- Added automatic dtype-based precision selection in Python wrapper
- Propagated Real template parameter through all detail classes:
- BB (bounding_box.h)
- DataType (data_type.h)
- PRTreeNode, PRTreeLeaf, PRTreeElement (nodes.h)
- PseudoPRTree, PseudoPRTreeNode (pseudo_tree.h)
Benefits:
- Eliminates "strange post-processing" that forced float32 on all users
- Each precision level now uses native types throughout
- Simpler, more maintainable codebase
- Better performance through compile-time type optimization
- Users get the precision they request without conversion overhead
All tests passing with new architecture.
* Add adaptive epsilon and configurable precision parameters
- Fix query methods to use Real type instead of hardcoded float
- find_one() now accepts vec<Real> for proper float64 precision
- find_all() now accepts py::array_t<Real> matching tree precision
- Fix Python wrapper to preserve precision settings on first insert
- Handle subnormal detection disabled case with workaround
- Preserve relative_epsilon, absolute_epsilon, adaptive_epsilon settings
- Remove obsolete query_exact and refine_candidates code
- All precision tests now passing
* Fix precision validation gaps and enhance insert() capabilities
This commit addresses remaining issues with precision handling:
1. **Auto-detect precision when loading from file**: When loading a tree
from a saved file via `PRTree3D(filepath)`, the wrapper now
automatically tries both float32 and float64 to determine which
precision was used when the tree was saved. This fixes the
`test_save_load_float32_no_regression` test failure where loading
a float32 tree defaulted to float64 and caused std::bad_alloc.
2. **Improved error handling**: If both precision attempts fail when
loading from file, provides an informative error message about
potential file corruption.
The fix ensures that precision is correctly preserved across save/load
cycles without requiring users to manually specify the precision when
loading.
Fixes:
- test_save_load_float32_no_regression now passes
- test_save_load_float64_matteo_case continues to pass
* Update documentation for native precision support
This commit updates all project documentation to reflect the v0.7.0
architectural changes:
1. **README.md**:
- Updated precision section to describe native float32/float64 support
- Added documentation for new precision control methods
- Documented auto-detection behavior for both construction and loading
- Updated version history with v0.7.0 changes
2. **CHANGES.md**:
- Added comprehensive v0.7.0 release notes
- Documented native precision architecture changes
- Listed all new precision control methods
- Described bug fixes related to precision handling
- Updated test count (991/991 tests passing)
3. **docs/ARCHITECTURE.md**:
- Updated PRTree template signature to include Real parameter
- Documented 6 exposed C++ classes (float32/float64 variants)
- Updated data flow diagrams for precision selection
- Added design decision section for native precision support
- Explained trade-offs and benefits of new architecture
The documentation now accurately reflects:
- Template signature: PRTree<T, B, D, Real>
- Automatic precision selection based on numpy dtype
- Auto-detection when loading from files
- Precision settings preservation across operations
- Elimination of idx2exact refinement approach
* Update C++ standard requirement from C++17 to C++20
The project uses C++20 features (concepts, likely/unlikely attributes, etc.)
as noted in CHANGES.md. Updated prerequisite documentation to reflect this:
- CONTRIBUTING.md: Updated compiler requirements to C++20 (GCC 10+, Clang 10+, MSVC 2019+)
- docs/DEVELOPMENT.md: Updated compiler requirements to C++20
This ensures developers are aware of the correct C++ standard required
for building the project.
* Bump version to v0.7.1
Release v0.7.1 with native precision support and architectural improvements:
- Native float32/float64 precision throughout the entire stack
- Eliminated idx2exact complexity for simpler code
- Auto-detection for precision based on dtype and file loading
- Advanced precision control (adaptive epsilon, configurable epsilon)
- Fixed precision validation gaps and query precision issues
Changes:
- pyproject.toml: 0.7.0 → 0.7.1
- __init__.py: 0.7.0 → 0.7.1
- CHANGES.md: Updated release date to 2025-11-09
- README.md: Updated version history to v0.7.1
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent ddb3f0c commit 2c6288d
File tree
16 files changed
+1469
-423
lines changed- docs
- include/prtree/core
- detail
- src
- cpp/bindings
- python_prtree
- tests/unit
16 files changed
+1469
-423
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
| 3 | + | |
4 | 4 | | |
5 | | - | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
6 | 88 | | |
7 | 89 | | |
8 | 90 | | |
9 | 91 | | |
10 | | - | |
| 92 | + | |
11 | 93 | | |
12 | 94 | | |
13 | 95 | | |
14 | | - | |
| 96 | + | |
15 | 97 | | |
16 | 98 | | |
17 | 99 | | |
18 | 100 | | |
19 | 101 | | |
20 | | - | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
| 102 | + | |
25 | 103 | | |
26 | 104 | | |
27 | 105 | | |
| |||
30 | 108 | | |
31 | 109 | | |
32 | 110 | | |
| 111 | + | |
| 112 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
11 | | - | |
| 11 | + | |
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
171 | 171 | | |
172 | 172 | | |
173 | 173 | | |
174 | | - | |
175 | | - | |
176 | | - | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
177 | 193 | | |
178 | 194 | | |
179 | 195 | | |
| |||
219 | 235 | | |
220 | 236 | | |
221 | 237 | | |
222 | | - | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
223 | 243 | | |
224 | 244 | | |
225 | 245 | | |
226 | | - | |
227 | 246 | | |
228 | 247 | | |
229 | 248 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
83 | 83 | | |
84 | 84 | | |
85 | 85 | | |
86 | | - | |
| 86 | + | |
87 | 87 | | |
88 | 88 | | |
89 | 89 | | |
| 90 | + | |
90 | 91 | | |
91 | 92 | | |
92 | 93 | | |
93 | 94 | | |
94 | 95 | | |
| 96 | + | |
95 | 97 | | |
96 | 98 | | |
97 | 99 | | |
| |||
116 | 118 | | |
117 | 119 | | |
118 | 120 | | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
119 | 127 | | |
120 | 128 | | |
121 | 129 | | |
122 | 130 | | |
123 | 131 | | |
| 132 | + | |
124 | 133 | | |
125 | 134 | | |
126 | 135 | | |
| |||
135 | 144 | | |
136 | 145 | | |
137 | 146 | | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
138 | 150 | | |
139 | 151 | | |
140 | 152 | | |
141 | 153 | | |
142 | 154 | | |
| 155 | + | |
143 | 156 | | |
144 | 157 | | |
145 | 158 | | |
146 | | - | |
| 159 | + | |
147 | 160 | | |
148 | 161 | | |
149 | | - | |
| 162 | + | |
150 | 163 | | |
151 | | - | |
152 | | - | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
153 | 167 | | |
154 | | - | |
155 | | - | |
156 | | - | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
157 | 171 | | |
158 | 172 | | |
159 | | - | |
| 173 | + | |
160 | 174 | | |
161 | 175 | | |
162 | 176 | | |
163 | 177 | | |
164 | 178 | | |
165 | | - | |
166 | | - | |
167 | | - | |
168 | | - | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
169 | 183 | | |
170 | 184 | | |
171 | 185 | | |
| |||
249 | 263 | | |
250 | 264 | | |
251 | 265 | | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
252 | 286 | | |
253 | 287 | | |
254 | 288 | | |
| |||
257 | 291 | | |
258 | 292 | | |
259 | 293 | | |
| 294 | + | |
260 | 295 | | |
261 | 296 | | |
262 | 297 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
39 | | - | |
| 39 | + | |
40 | 40 | | |
41 | 41 | | |
42 | 42 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
17 | | - | |
18 | | - | |
19 | | - | |
| 17 | + | |
20 | 18 | | |
21 | 19 | | |
22 | 20 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | | - | |
| 16 | + | |
17 | 17 | | |
18 | | - | |
| 18 | + | |
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
23 | | - | |
| 23 | + | |
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
28 | | - | |
| 28 | + | |
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
| |||
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
46 | 46 | | |
47 | 47 | | |
0 commit comments