Casting routine from files/header#49
Conversation
There was a problem hiding this comment.
Pull request overview
This PR updates AnyTrxFile::_create_from_pointer()’s handling of groups/ entries to allow loading group membership arrays stored in additional integer dtypes by converting them into the internal uint32 representation, enabling more consistent cross-language benchmarking/interoperability.
Changes:
- Allow
groups/arrays with integer dtypes beyonduint32(e.g.,uint64,int64,int32,uint16, etc.) by casting touint32. - Add a guard intended to prevent unsafe downcasting when
NB_STREAMLINESexceeds 32-bit capacity. - Materialize group arrays to owned memory before storing in
trx.groups.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if (ext == "int64") { | ||
| const int64_t* src = reinterpret_cast<const int64_t*>(tmp_arr.owned.data()); | ||
| for (size_t i = 0; i < size; ++i) dst[i] = static_cast<uint32_t>(src[i]); | ||
| } else if (ext == "uint64") { | ||
| const uint64_t* src = reinterpret_cast<const uint64_t*>(tmp_arr.owned.data()); | ||
| for (size_t i = 0; i < size; ++i) dst[i] = static_cast<uint32_t>(src[i]); | ||
| } else if (ext == "uint8") { | ||
| const uint8_t* src = reinterpret_cast<const uint8_t*>(tmp_arr.owned.data()); | ||
| for (size_t i = 0; i < size; ++i) dst[i] = static_cast<uint32_t>(src[i]); | ||
| } else if (ext == "int8") { | ||
| const int8_t* src = reinterpret_cast<const int8_t*>(tmp_arr.owned.data()); | ||
| for (size_t i = 0; i < size; ++i) dst[i] = static_cast<uint32_t>(src[i]); | ||
| } else if (ext == "uint16") { | ||
| const uint16_t* src = reinterpret_cast<const uint16_t*>(tmp_arr.owned.data()); | ||
| for (size_t i = 0; i < size; ++i) dst[i] = static_cast<uint32_t>(src[i]); | ||
| } else if (ext == "int16") { | ||
| const int16_t* src = reinterpret_cast<const int16_t*>(tmp_arr.owned.data()); | ||
| for (size_t i = 0; i < size; ++i) dst[i] = static_cast<uint32_t>(src[i]); | ||
| } else if (ext == "int32") { | ||
| const int32_t* src = reinterpret_cast<const int32_t*>(tmp_arr.owned.data()); | ||
| for (size_t i = 0; i < size; ++i) dst[i] = static_cast<uint32_t>(src[i]); | ||
| } |
| if (ext == "int64" || ext == "uint64") { | ||
| uint64_t num_strs = static_cast<uint64_t>(header["NB_STREAMLINES"].number_value()); | ||
| if (num_strs > 4294967295ULL) { | ||
| throw TrxFormatError("downcasting is unsafe because the number of streamlines exceeds the 32-bit limit"); | ||
| } | ||
| } |
| } else if (ext == "int64" || ext == "uint64" || ext == "int32" || ext == "uint8" || ext == "int8" || ext == "uint16" || ext == "int16") { | ||
| if (ext == "int32" || ext == "uint8" || ext == "int8" || ext == "uint16" || ext == "int16") { | ||
| std::cerr << "Warning: Upcasting group from " << ext << " to uint32\n"; | ||
| } | ||
| if (ext == "int64" || ext == "uint64") { |
…iles without metadata loss
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #49 +/- ##
==========================================
- Coverage 88.64% 82.67% -5.97%
==========================================
Files 15 16 +1
Lines 7749 8416 +667
Branches 1043 1181 +138
==========================================
+ Hits 6869 6958 +89
- Misses 880 1458 +578
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
(Similar description to trx-rs PR)
Proposed modifications to allow fair comparison for benchmarking across languages, the biggest modification is to up/down-cast to uint32 offsets since it is not completely illegal. Some of the benchmark files add offsets as uint64. I believe this is mostly to smooth operations between languages, I personally think that virtually no one will create a tractogram with 40M streamlines with 100 points each, but it is possible.
I apologize for the formatting, I believe my VScode did some automatic formatting, @mattcieslak if you could tell me what standard/linter/tool to use (or maybe I should manually revert the identical lines?).
I believe some unit testing is needed to verify if everything is alright, but I tested single language round-trip (load/save) and between language compatibility. Then I benchmarked on big files: https://github.com/tee-ar-ex/trx-manuscript-2026-benchmark