def test_bundles_to_tgram(): affine = np.array([[2., 0., 0., -80.], [0., 2., 0., -120.], [0., 0., 2., -60.], [0., 0., 0., 1.]]) img = nib.Nifti1Image(np.ones((10, 10, 10, 30)), affine) bundles = { 'b1': StatefulTractogram([ 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]]) ], img, Space.VOX), 'b2': StatefulTractogram([ np.array([[0, 0, 0], [0, 0, 0.5], [0, 0, 2], [0, 0, 2.5]]), np.array([[0, 0, 0], [0, 0.5, 0.5], [0, 2, 2]]) ], img, Space.VOX) } bundle_dict = {'b1': {'uid': 1}, 'b2': {'uid': 2}} tgram = aus.bundles_to_tgram(bundles, bundle_dict, img) new_bundles = aus.tgram_to_bundles(tgram, bundle_dict, img) for k1 in bundles.keys(): for k2 in bundles[k1].__dict__.keys(): for sl1, sl2 in zip(bundles[k1].streamlines, new_bundles[k1].streamlines): npt.assert_equal(sl1, sl2)
def _recobundles(row, wm_labels, bundle_dict, reg_template, odf_model="DTI", directions="det", n_seeds=2, random_seeds=False, force_recompute=False): bundles_file = _get_fname(row, '%s_%s_bundles.trk' % (odf_model, directions)) if not op.exists(bundles_file) or force_recompute: streamlines_file = _streamlines(row, wm_labels, odf_model=odf_model, directions=directions, n_seeds=n_seeds, random_seeds=random_seeds, force_recompute=force_recompute) tg = nib.streamlines.load(streamlines_file).tractogram sl = tg.apply_affine(np.linalg.inv(row['dwi_affine'])).streamlines bundles = seg.recobundles(sl, bundle_dict) tgram = aus.bundles_to_tgram(bundles, bundle_dict, row['dwi_affine']) nib.streamlines.save(tgram, bundles_file) return bundles_file
def _segment(self, row): # We pass `clean_params` here, but do not use it, so we have the # same signature as `_clean_bundles`. odf_model = self.tracking_params["odf_model"] directions = self.tracking_params["directions"] seg_algo = self.segmentation_params["seg_algo"] bundles_file = self._get_fname( row, f'_space-RASMM_model-{odf_model}_desc-{directions}-' f'{seg_algo}_tractography.trk') if self.force_recompute or not op.exists(bundles_file): streamlines_file = self._streamlines(row) img = nib.load(row['dwi_file']) tg = load_tractogram(streamlines_file, img, Space.VOX) if self.use_prealign: reg_prealign = np.load(self._reg_prealign(row)) else: reg_prealign = None segmentation = seg.Segmentation(**self.segmentation_params) bundles = segmentation.segment(self.bundle_dict, tg, row['dwi_file'], row['bval_file'], row['bvec_file'], reg_template=self.reg_template, mapping=self._mapping(row), reg_prealign=reg_prealign) if self.segmentation_params['return_idx']: idx = { bundle: bundles[bundle]['idx'].tolist() for bundle in self.bundle_dict } afd.write_json(bundles_file.split('.')[0] + '_idx.json', idx) bundles = { bundle: bundles[bundle]['sl'] for bundle in self.bundle_dict } tgram = aus.bundles_to_tgram(bundles, self.bundle_dict, img) save_tractogram(tgram, bundles_file) meta = dict(source=streamlines_file, Parameters=self.segmentation_params) meta_fname = bundles_file.split('.')[0] + '.json' afd.write_json(meta_fname, meta) return bundles_file
def test_bundles_to_tgram(): bundles = { 'b1': [ 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]]) ], 'b2': [ 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]]) ] } bundle_dict = {'b1': {'uid': 1}, 'b2': {'uid': 2}} affine = np.array([[2., 0., 0., -80.], [0., 2., 0., -120.], [0., 0., 2., -60.], [0., 0., 0., 1.]]) tgram = aus.bundles_to_tgram(bundles, bundle_dict, affine) new_bundles = aus.tgram_to_bundles(tgram, bundle_dict) npt.assert_equal(new_bundles, bundles)
def segment(subses_dict, bundle_dict, data_imap, reg_template, mapping_imap, tractography_imap, tracking_params, segmentation_params): streamlines_file = tractography_imap["streamlines_file"] # We pass `clean_params` here, but do not use it, so we have the # same signature as `_clean_bundles`. img = nib.load(subses_dict['dwi_file']) tg = load_tractogram(streamlines_file, img, Space.VOX, bbox_valid_check=False) tg.remove_invalid_streamlines() start_time = time() segmentation = seg.Segmentation(**segmentation_params) bundles = segmentation.segment(bundle_dict, tg, subses_dict['dwi_file'], data_imap["bval_file"], data_imap["bvec_file"], reg_template=reg_template, mapping=mapping_imap["mapping"]) if segmentation_params['return_idx']: idx = { bundle: bundles[bundle]['idx'].tolist() for bundle in bundle_dict } bundles = {bundle: bundles[bundle]['sl'] for bundle in bundle_dict} tgram = aus.bundles_to_tgram(bundles, bundle_dict, img) segmentation_params_out = {} for arg_name, value in segmentation_params.items(): if isinstance(value, (int, float, bool, str))\ or (value is None): segmentation_params_out[arg_name] = value else: segmentation_params_out[arg_name] = str(value) meta = dict(source=streamlines_file, Parameters=segmentation_params_out) if segmentation_params['return_idx']: meta["idx"] = idx meta["Timing"] = time() - start_time return tgram, meta
def _bundles(row, wm_labels, bundle_dict, reg_template, odf_model="DTI", directions="det", n_seeds=2, random_seeds=False, force_recompute=False): bundles_file = _get_fname(row, '%s_%s_bundles.trk' % (odf_model, directions)) if not op.exists(bundles_file) or force_recompute: streamlines_file = _streamlines(row, wm_labels, odf_model=odf_model, directions=directions, n_seeds=n_seeds, random_seeds=random_seeds, force_recompute=force_recompute) tg = nib.streamlines.load(streamlines_file).tractogram sl = tg.apply_affine(np.linalg.inv(row['dwi_affine'])).streamlines reg_prealign = np.load( _reg_prealign(row, force_recompute=force_recompute)) mapping = reg.read_mapping(_mapping(row, reg_template), row['dwi_file'], reg_template, prealign=np.linalg.inv(reg_prealign)) bundles = seg.segment(row['dwi_file'], row['bval_file'], row['bvec_file'], sl, bundle_dict, reg_template=reg_template, mapping=mapping) tgram = aus.bundles_to_tgram(bundles, bundle_dict, row['dwi_affine']) nib.streamlines.save(tgram, bundles_file) return bundles_file
def test_bundles_to_tgram(): bundles = { 'b1': dts.Streamlines([ 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]]) ]), 'b2': dts.Streamlines([ np.array([[0, 0, 0], [0, 0, 0.5], [0, 0, 2], [0, 0, 2.5]]), np.array([[0, 0, 0], [0, 0.5, 0.5], [0, 2, 2]]) ]) } bundle_dict = {'b1': {'uid': 1}, 'b2': {'uid': 2}} affine = np.array([[2., 0., 0., -80.], [0., 2., 0., -120.], [0., 0., 2., -60.], [0., 0., 0., 1.]]) tgram = aus.bundles_to_tgram(bundles, bundle_dict, affine) new_bundles = aus.tgram_to_bundles(tgram, bundle_dict) for k1 in bundles.keys(): for k2 in bundles[k1].__dict__.keys(): npt.assert_equal(new_bundles[k1].__dict__[k2], bundles[k1].__dict__[k2])