Casting routine from files/header#49
Open
frheault wants to merge 3 commits into
Open
Conversation
Contributor
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.
Comment on lines
+537
to
+558
| 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]); | ||
| } |
Comment on lines
+523
to
+528
| 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"); | ||
| } | ||
| } |
Comment on lines
+519
to
+523
| } 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
(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