Skip to content

BF: Allow save and load of empty trx#99

Merged
arokem merged 4 commits intotee-ar-ex:masterfrom
skoudoro:fix-empty-trx
Feb 2, 2026
Merged

BF: Allow save and load of empty trx#99
arokem merged 4 commits intotee-ar-ex:masterfrom
skoudoro:fix-empty-trx

Conversation

@skoudoro
Copy link
Collaborator

fix #91

This pull request improves the handling and testing of empty TRX files and ensures consistent dtype comparisons in tests.

  • Updated TrxFile.deepcopy to only write positions and offsets files if the TRX is not empty, preventing unnecessary files in empty TRX archives (trx_file_memmap.py).
  • Modified _create_trx_from_pointer to immediately return an empty TrxFile if the header indicates zero streamlines and vertices, avoiding attempts to load non-existent data (trx_file_memmap.py).

This case has been encounter with Recobundles. Sometimes, the specific bundle is not in the tractogram so it should save an empty TRX instead of crashing.

Please, take a careful look @arokem and @frheault , I might miss something.

@codecov
Copy link

codecov bot commented Jan 29, 2026

Codecov Report

❌ Patch coverage is 75.92593% with 13 lines in your changes missing coverage. Please review.
✅ Project coverage is 60.06%. Comparing base (384d3c7) to head (a783377).
⚠️ Report is 7 commits behind head on master.

Files with missing lines Patch % Lines
trx/tests/test_cli.py 7.69% 12 Missing ⚠️
trx/trx_file_memmap.py 90.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master      #99      +/-   ##
==========================================
+ Coverage   59.76%   60.06%   +0.29%     
==========================================
  Files          12       12              
  Lines        2411     2454      +43     
==========================================
+ Hits         1441     1474      +33     
- Misses        970      980      +10     
Flag Coverage Δ
unittests 60.06% <75.92%> (+0.29%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

normalized[key] = _normalize_dtype_dict(value)
elif isinstance(value, np.dtype):
# Normalize to little-endian for multi-byte types
if value.byteorder == "=" and value.itemsize > 1:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure that I understand this. If we are on a big endian system, couldn't this still be set to "=" (native)? And this would change it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The goal here is to make sure we are in little endian whatever the machine (big endian system or little endian system).

for now, we do not want to stay in native.

If we are on a big endian system, couldn't this still be set to "=" (native)?

yes, but internally, we switch it to little endian

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha, looks good to me! I wonder if @frheault could give it another look, to make sure that I have not missed anything.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems good, I tested using some tracking scripts with 0 seeds and it now works great for saving empty tractogram (lazy saving too) and loading it.

trx.header = header

# Handle empty TRX files early - no positions/offsets to load
if header["NB_STREAMLINES"] == 0 and header["NB_VERTICES"] == 0:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be a or ? Since streamlines with 0 or 1 points are invalid anyway, maybe this could catch some corner case and force empty TRX?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, I hesitated because I believe we should handle this corner cases with better messaging.

At the end, that's fine. So code updated

@skoudoro
Copy link
Collaborator Author

skoudoro commented Feb 2, 2026

All comments has been addressed, this PR can be merged

@arokem arokem merged commit e5cec49 into tee-ar-ex:master Feb 2, 2026
16 checks passed
@skoudoro skoudoro deleted the fix-empty-trx branch February 3, 2026 02:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

trx fails when saving empty trx files.

3 participants