def _tgramer(bundles, bundle_dict, affine): tgram = nib.streamlines.Tractogram([], {'bundle': []}) for b in bundles: print("Segmenting: %s" % b) this_sl = list(bundles[b]) this_tgram = nib.streamlines.Tractogram( this_sl, data_per_streamline={ 'bundle': (len(this_sl) * [bundle_dict[b]['uid']]) }, affine_to_rasmm=affine) tgram = aus.add_bundles(tgram, this_tgram) return tgram
def clean_bundles(subses_dict, bundles_file, bundle_dict, clean_params, tracking_params, segmentation_params): img = nib.load(subses_dict['dwi_file']) sft = load_tractogram(bundles_file, img, Space.VOX) img = nib.load(subses_dict['dwi_file']) start_time = time() tgram = nib.streamlines.Tractogram([], {'bundle': []}) if clean_params['return_idx']: return_idx = {} for b in bundle_dict.keys(): if b != "whole_brain": idx = np.where( sft.data_per_streamline['bundle'] == bundle_dict[b]['uid'])[0] this_tg = StatefulTractogram(sft.streamlines[idx], img, Space.VOX) this_tg = seg.clean_bundle(this_tg, **clean_params) if clean_params['return_idx']: this_tg, this_idx = this_tg idx_file = bundles_file.split('.')[0] + '.json' with open(idx_file) as ff: bundle_idx = json.load(ff)["idx"][b] return_idx[b] = np.array(bundle_idx)[this_idx].tolist() this_tgram = nib.streamlines.Tractogram( this_tg.streamlines, data_per_streamline={ 'bundle': (len(this_tg) * [bundle_dict[b]['uid']]) }, affine_to_rasmm=img.affine) tgram = aus.add_bundles(tgram, this_tgram) sft = StatefulTractogram(tgram.streamlines, sft, Space.VOX, data_per_streamline=tgram.data_per_streamline) seg_args = get_default_args(seg.clean_bundle) for k in seg_args: if callable(seg_args[k]): seg_args[k] = seg_args[k].__name__ meta = dict(source=bundles_file, Parameters=seg_args) if clean_params['return_idx']: meta["idx"] = return_idx meta["Timing"] = time() - start_time return sft, meta
def test_add_bundles(): t1 = nib.streamlines.Tractogram([ np.array([[0, 0, 0], [0, 0, 0.5], [0, 0, 1], [0, 0, 1.5]]), np.array([[0, 0, 0], [0, 0.5, 0.5], [0, 1, 1]]) ]) t2 = nib.streamlines.Tractogram([ np.array([[0, 0, 0], [0, 0, 0.5], [0, 0, 1], [0, 0, 1.5]]), np.array([[0, 0, 0], [0, 0.5, 0.5], [0, 1, 1]]) ]) added = aus.add_bundles(t1, t2) test_tgram = nib.streamlines.Tractogram([ np.array([[0, 0, 0], [0, 0, 0.5], [0, 0, 1], [0, 0, 1.5]]), np.array([[0, 0, 0], [0, 0.5, 0.5], [0, 1, 1]]), np.array([[0, 0, 0], [0, 0, 0.5], [0, 0, 1], [0, 0, 1.5]]), np.array([[0, 0, 0], [0, 0.5, 0.5], [0, 1, 1]]) ]) for sl1, sl2 in zip(added.streamlines, test_tgram.streamlines): npt.assert_array_equal(sl1, sl2)
def _clean_bundles(row, wm_labels, bundle_dict, reg_template, odf_model="DTI", directions="det", n_seeds=2, random_seeds=False, force_recompute=False): clean_bundles_file = _get_fname( row, '%s_%s_clean_bundles.trk' % (odf_model, directions)) if not op.exists(clean_bundles_file) or force_recompute: bundles_file = _bundles(row, wm_labels, bundle_dict, reg_template, odf_model=odf_model, directions=directions, n_seeds=n_seeds, random_seeds=random_seeds, force_recompute=False) tg = nib.streamlines.load(bundles_file).tractogram sl = tg.apply_affine(np.linalg.inv(row['dwi_affine'])).streamlines tgram = nib.streamlines.Tractogram([], {'bundle': []}) for b in bundle_dict.keys(): idx = np.where( tg.data_per_streamline['bundle'] == bundle_dict[b]['uid'])[0] this_sl = sl[idx] this_sl = seg.clean_fiber_group(this_sl) this_tgram = nib.streamlines.Tractogram( this_sl, data_per_streamline={ 'bundle': (len(this_sl) * [bundle_dict[b]['uid']]) }, affine_to_rasmm=row['dwi_affine']) tgram = aus.add_bundles(tgram, this_tgram) nib.streamlines.save(tgram, clean_bundles_file) return clean_bundles_file
def _clean_bundles(self, row): odf_model = self.tracking_params['odf_model'] directions = self.tracking_params['directions'] seg_algo = self.segmentation_params['seg_algo'] clean_bundles_file = self._get_fname( row, f'_space-RASMM_model-{odf_model}_desc-{directions}-' f'{seg_algo}-clean_tractography.trk') if self.force_recompute or not op.exists(clean_bundles_file): bundles_file = self._segment(row) sft = load_tractogram(bundles_file, row['dwi_img'], Space.VOX) tgram = nib.streamlines.Tractogram([], {'bundle': []}) if self.clean_params['return_idx']: return_idx = {} for b in self.bundle_dict.keys(): if b != "whole_brain": idx = np.where(sft.data_per_streamline['bundle'] == self.bundle_dict[b]['uid'])[0] this_tg = StatefulTractogram(sft.streamlines[idx], row['dwi_img'], Space.VOX) this_tg = seg.clean_bundle(this_tg, **self.clean_params) if self.clean_params['return_idx']: this_tg, this_idx = this_tg idx_file = bundles_file.split('.')[0] + '_idx.json' with open(idx_file) as ff: bundle_idx = json.load(ff)[b] return_idx[b] = \ np.array(bundle_idx)[this_idx].tolist() this_tgram = nib.streamlines.Tractogram( this_tg.streamlines, data_per_streamline={ 'bundle': (len(this_tg) * [self.bundle_dict[b]['uid']]) }, affine_to_rasmm=row['dwi_affine']) tgram = aus.add_bundles(tgram, this_tgram) save_tractogram( StatefulTractogram( tgram.streamlines, sft, Space.VOX, data_per_streamline=tgram.data_per_streamline), clean_bundles_file) seg_args = get_default_args(seg.clean_bundle) for k in seg_args: if callable(seg_args[k]): seg_args[k] = seg_args[k].__name__ meta = dict(source=bundles_file, Parameters=seg_args) meta_fname = clean_bundles_file.split('.')[0] + '.json' afd.write_json(meta_fname, meta) if self.clean_params['return_idx']: afd.write_json( clean_bundles_file.split('.')[0] + '_idx.json', return_idx) return clean_bundles_file