-
Notifications
You must be signed in to change notification settings - Fork 9
[FIX] Add NUMBA backend; correctly handle symmetric/nonsymmetric spheres #41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0a1169d
[FIX] allow full basis in accelerated tractography
36000 f245353
[FIX] translate full_basis support to Metal and WebGPU backends
Copilot dbd2d60
add numba tracker
36000 db2c234
add numba dependency
36000 397ed7a
BFs, conform APIs
36000 1a5ea20
more BFs
36000 9bd7008
JIT only once
36000 ce696f2
better phrased as sphere_symm than full_basis, also, some ptt fixes
36000 f3a44e5
fixing ptt bugs and switching dynamic shared mem to static
36000 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| import logging | ||
| import numpy as np | ||
| from tqdm import tqdm | ||
| from trx.trx_file_memmap import TrxFile | ||
| from dipy.io.stateful_tractogram import Space, StatefulTractogram | ||
| from nibabel.streamlines.array_sequence import ArraySequence | ||
| from nibabel.streamlines.tractogram import Tractogram | ||
|
|
||
| logger = logging.getLogger("GPUStreamlines") | ||
|
|
||
|
|
||
| class GenericTracker: | ||
| def __enter__(self): | ||
| return self | ||
|
|
||
| def __exit__(self, exc_type, exc, tb): | ||
| return False | ||
|
|
||
| def _ngpus(self): | ||
| if hasattr(self, "ngpus"): | ||
| return self.ngpus | ||
| else: | ||
| return 1 | ||
|
|
||
| def _divide_chunks(self, seeds): | ||
| global_chunk_sz = self.chunk_size * self._ngpus() | ||
| nchunks = (seeds.shape[0] + global_chunk_sz - 1) // global_chunk_sz | ||
| return global_chunk_sz, nchunks | ||
|
|
||
| def generate_sft(self, seeds, ref_img): | ||
| global_chunk_sz, nchunks = self._divide_chunks(seeds) | ||
| buffer_size = 0 | ||
| generators = [] | ||
|
|
||
| with tqdm(total=seeds.shape[0]) as pbar: | ||
| for idx in range(nchunks): | ||
| self.seed_propagator.propagate( | ||
| seeds[idx * global_chunk_sz : (idx + 1) * global_chunk_sz] | ||
| ) | ||
| buffer_size += self.seed_propagator.get_buffer_size() | ||
| generators.append(self.seed_propagator.as_generator()) | ||
| pbar.update( | ||
| seeds[idx * global_chunk_sz : (idx + 1) * global_chunk_sz].shape[0] | ||
| ) | ||
| array_sequence = ArraySequence( | ||
| (item for gen in generators for item in gen), buffer_size | ||
| ) | ||
|
36000 marked this conversation as resolved.
|
||
| return StatefulTractogram(array_sequence, ref_img, Space.VOX) | ||
|
|
||
| def generate_trx(self, seeds, ref_img): | ||
| global_chunk_sz, nchunks = self._divide_chunks(seeds) | ||
|
|
||
| # Will resize by a factor of 2 if these are exceeded | ||
| sl_len_guess = 100 | ||
| sl_per_seed_guess = 2 | ||
| n_sls_guess = sl_per_seed_guess * seeds.shape[0] | ||
|
|
||
| # trx files use memory mapping | ||
| trx_reference = TrxFile(reference=ref_img) | ||
| trx_reference.streamlines._data = trx_reference.streamlines._data.astype( | ||
| np.float32 | ||
| ) | ||
| trx_reference.streamlines._offsets = trx_reference.streamlines._offsets.astype( | ||
| np.uint64 | ||
| ) | ||
|
|
||
| trx_file = TrxFile( | ||
| nb_streamlines=n_sls_guess, | ||
| nb_vertices=n_sls_guess * sl_len_guess, | ||
| init_as=trx_reference, | ||
| ) | ||
| offsets_idx = 0 | ||
| sls_data_idx = 0 | ||
|
|
||
| with tqdm(total=seeds.shape[0]) as pbar: | ||
| for idx in range(int(nchunks)): | ||
| self.seed_propagator.propagate( | ||
| seeds[idx * global_chunk_sz : (idx + 1) * global_chunk_sz] | ||
| ) | ||
| tractogram = Tractogram( | ||
| self.seed_propagator.as_array_sequence(), | ||
| affine_to_rasmm=ref_img.affine, | ||
| ) | ||
| tractogram.to_world() | ||
| sls = tractogram.streamlines | ||
|
|
||
| new_offsets_idx = offsets_idx + len(sls._offsets) | ||
| new_sls_data_idx = sls_data_idx + len(sls._data) | ||
|
|
||
| if ( | ||
| new_offsets_idx > trx_file.header["NB_STREAMLINES"] | ||
| or new_sls_data_idx > trx_file.header["NB_VERTICES"] | ||
| ): | ||
| logger.info("TRX resizing...") | ||
| trx_file.resize( | ||
| nb_streamlines=new_offsets_idx * 2, | ||
| nb_vertices=new_sls_data_idx * 2, | ||
| ) | ||
|
36000 marked this conversation as resolved.
|
||
|
|
||
| # TRX uses memmaps here | ||
| trx_file.streamlines._data[sls_data_idx:new_sls_data_idx] = sls._data | ||
| trx_file.streamlines._offsets[offsets_idx:new_offsets_idx] = ( | ||
| sls_data_idx + sls._offsets | ||
| ) | ||
| trx_file.streamlines._lengths[offsets_idx:new_offsets_idx] = ( | ||
| sls._lengths | ||
| ) | ||
|
|
||
| offsets_idx = new_offsets_idx | ||
| sls_data_idx = new_sls_data_idx | ||
| pbar.update( | ||
| seeds[idx * global_chunk_sz : (idx + 1) * global_chunk_sz].shape[0] | ||
| ) | ||
| trx_file.resize() | ||
|
|
||
| return trx_file | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.