dmri-commit 2.2.0__tar.gz → 2.3.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {dmri-commit-2.2.0 → dmri_commit-2.3.1}/LICENSE +0 -0
- {dmri-commit-2.2.0 → dmri_commit-2.3.1}/MANIFEST.in +1 -0
- {dmri-commit-2.2.0/dmri_commit.egg-info → dmri_commit-2.3.1}/PKG-INFO +2 -2
- {dmri-commit-2.2.0 → dmri_commit-2.3.1}/README.md +0 -0
- {dmri-commit-2.2.0 → dmri_commit-2.3.1}/commit/core.pyx +108 -135
- {dmri-commit-2.2.0 → dmri_commit-2.3.1}/commit/models.py +1 -0
- {dmri-commit-2.2.0 → dmri_commit-2.3.1}/commit/operator/operator.pyx +76 -22
- dmri_commit-2.3.1/commit/operator/operator_c.c +8589 -0
- {dmri-commit-2.2.0 → dmri_commit-2.3.1}/commit/proximals.pyx +0 -0
- {dmri-commit-2.2.0 → dmri_commit-2.3.1}/commit/solvers.py +40 -40
- {dmri-commit-2.2.0 → dmri_commit-2.3.1}/commit/trk2dictionary/ProgressBar.h +0 -0
- {dmri-commit-2.2.0 → dmri_commit-2.3.1}/commit/trk2dictionary/Vector.h +0 -0
- {dmri-commit-2.2.0 → dmri_commit-2.3.1}/commit/trk2dictionary/trk2dictionary.pyx +59 -48
- {dmri-commit-2.2.0 → dmri_commit-2.3.1}/commit/trk2dictionary/trk2dictionary_c.cpp +19 -17
- {dmri-commit-2.2.0 → dmri_commit-2.3.1/dmri_commit.egg-info}/PKG-INFO +2 -2
- {dmri-commit-2.2.0 → dmri_commit-2.3.1}/dmri_commit.egg-info/SOURCES.txt +2 -4
- {dmri-commit-2.2.0 → dmri_commit-2.3.1}/dmri_commit.egg-info/dependency_links.txt +0 -0
- {dmri-commit-2.2.0 → dmri_commit-2.3.1}/dmri_commit.egg-info/requires.txt +1 -1
- {dmri-commit-2.2.0 → dmri_commit-2.3.1}/dmri_commit.egg-info/top_level.txt +0 -0
- {dmri-commit-2.2.0 → dmri_commit-2.3.1}/pyproject.toml +6 -3
- {dmri-commit-2.2.0 → dmri_commit-2.3.1}/setup.cfg +0 -0
- {dmri-commit-2.2.0 → dmri_commit-2.3.1}/setup.py +28 -6
- dmri_commit-2.3.1/setup_operator.py +932 -0
- {dmri-commit-2.2.0 → dmri_commit-2.3.1}/tests/test_demo.py +0 -0
- dmri-commit-2.2.0/commit/operator/config.py +0 -6
- dmri-commit-2.2.0/commit/operator/operator.pyxbld +0 -39
- dmri-commit-2.2.0/commit/operator/operator_noLUT.c +0 -187
- dmri-commit-2.2.0/commit/operator/operator_withLUT.c +0 -2247
- {dmri-commit-2.2.0 → dmri_commit-2.3.1}/commit/__init__.py +0 -0
- {dmri-commit-2.2.0 → dmri_commit-2.3.1}/commit/operator/__init__.py +0 -0
|
File without changes
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: dmri-commit
|
|
3
|
-
Version: 2.
|
|
3
|
+
Version: 2.3.1
|
|
4
4
|
Summary: Convex Optimization Modeling for Microstructure Informed Tractography (COMMIT)
|
|
5
5
|
Author-email: Alessandro Daducci <alessandro.daducci@univr.it>
|
|
6
6
|
Maintainer-email: DICE lab <DICE_lab@strutture.univr.it>
|
|
@@ -106,7 +106,7 @@ Description-Content-Type: text/markdown
|
|
|
106
106
|
License-File: LICENSE
|
|
107
107
|
Requires-Dist: dmri-amico>=2.0.1
|
|
108
108
|
Requires-Dist: dmri-dicelib>=1.1.0
|
|
109
|
-
Requires-Dist: numpy
|
|
109
|
+
Requires-Dist: numpy<2.0.0,>=1.24.4
|
|
110
110
|
Requires-Dist: scipy>=1.10.1
|
|
111
111
|
|
|
112
112
|
[](https://pypi.org/project/dmri-commit/)
|
|
File without changes
|
|
@@ -27,6 +27,7 @@ from dicelib.utils import format_time
|
|
|
27
27
|
|
|
28
28
|
import commit.models
|
|
29
29
|
import commit.solvers
|
|
30
|
+
from commit.operator import operator
|
|
30
31
|
|
|
31
32
|
|
|
32
33
|
logger = setup_logger('core')
|
|
@@ -78,6 +79,7 @@ cdef class Evaluation :
|
|
|
78
79
|
cdef public A
|
|
79
80
|
cdef public regularisation_params
|
|
80
81
|
cdef public x
|
|
82
|
+
cdef public x_nnls
|
|
81
83
|
cdef public CONFIG
|
|
82
84
|
cdef public temp_data
|
|
83
85
|
cdef public confidence_map_img
|
|
@@ -103,6 +105,7 @@ cdef class Evaluation :
|
|
|
103
105
|
self.regularisation_params = None # set by "set_regularisation" method
|
|
104
106
|
self.x = None # set by "fit" method
|
|
105
107
|
self.confidence_map_img = None # set by "fit" method
|
|
108
|
+
self.x_nnls = None # set by "fit" method (coefficients of IC compartment estimated without regularization)
|
|
106
109
|
self.verbose = 3
|
|
107
110
|
|
|
108
111
|
# store all the parameters of an evaluation with COMMIT
|
|
@@ -223,8 +226,9 @@ cdef class Evaluation :
|
|
|
223
226
|
|
|
224
227
|
if self.get_config('doNormalizeSignal') :
|
|
225
228
|
if self.scheme.b0_count > 0:
|
|
226
|
-
|
|
227
|
-
|
|
229
|
+
log_list = []
|
|
230
|
+
ret_subinfo = logger.subinfo('Normalizing to b0:', with_progress=True, indent_char='*', indent_lvl=1)
|
|
231
|
+
with ProgressBar(disable=self.verbose < 3, hide_on_exit=True, subinfo=ret_subinfo, log_list=log_list):
|
|
228
232
|
b0 = np.mean( self.niiDWI_img[:,:,:,self.scheme.b0_idx], axis=3 )
|
|
229
233
|
idx = b0 <= b0_min_signal * b0[b0>0].mean()
|
|
230
234
|
b0[ idx ] = 1
|
|
@@ -352,8 +356,9 @@ cdef class Evaluation :
|
|
|
352
356
|
tic = time.time()
|
|
353
357
|
logger.subinfo('')
|
|
354
358
|
logger.info( 'Loading the kernels' )
|
|
355
|
-
|
|
356
|
-
|
|
359
|
+
log_list = []
|
|
360
|
+
ret_subinfo = logger.subinfo( 'Resampling LUT for subject "%s":' % self.get_config('subject'), indent_char='*', indent_lvl=1, with_progress=True ) # TODO: check why not printed
|
|
361
|
+
with ProgressBar(disable=self.verbose < 3, hide_on_exit=True, subinfo=ret_subinfo, log_list=log_list):
|
|
357
362
|
# auxiliary data structures
|
|
358
363
|
idx_OUT, Ylm_OUT = amico.lut.aux_structures_resample( self.scheme, self.get_config('lmax') )
|
|
359
364
|
|
|
@@ -375,8 +380,9 @@ cdef class Evaluation :
|
|
|
375
380
|
|
|
376
381
|
# De-mean kernels
|
|
377
382
|
if self.get_config('doDemean') :
|
|
378
|
-
|
|
379
|
-
|
|
383
|
+
log_list = []
|
|
384
|
+
ret_subinfo = logger.subinfo('Demeaning signal', with_progress=True, indent_lvl=2, indent_char='-' )
|
|
385
|
+
with ProgressBar(disable=self.verbose < 3, hide_on_exit=True, subinfo=ret_subinfo, log_list=log_list):
|
|
380
386
|
for j in xrange(self.get_config('ndirs')) :
|
|
381
387
|
for i in xrange(nIC) :
|
|
382
388
|
self.KERNELS['wmr'][i,j,:] -= self.KERNELS['wmr'][i,j,:].mean()
|
|
@@ -387,8 +393,9 @@ cdef class Evaluation :
|
|
|
387
393
|
|
|
388
394
|
# Normalize atoms
|
|
389
395
|
if self.get_config('doNormalizeKernels') :
|
|
390
|
-
|
|
391
|
-
|
|
396
|
+
log_list = []
|
|
397
|
+
ret_subinfo = logger.subinfo('Normalizing kernels', with_progress=True, indent_lvl=2, indent_char='-' )
|
|
398
|
+
with ProgressBar(disable=self.verbose < 3, hide_on_exit=True, subinfo=ret_subinfo, log_list=log_list):
|
|
392
399
|
self.KERNELS['wmr_norm'] = np.zeros( nIC )
|
|
393
400
|
for i in xrange(nIC) :
|
|
394
401
|
self.KERNELS['wmr_norm'][i] = np.linalg.norm( self.KERNELS['wmr'][i,0,:] )
|
|
@@ -458,8 +465,9 @@ cdef class Evaluation :
|
|
|
458
465
|
|
|
459
466
|
# segments from the tracts
|
|
460
467
|
# ------------------------
|
|
461
|
-
|
|
462
|
-
|
|
468
|
+
log_list = []
|
|
469
|
+
ret_subinfo = logger.subinfo('Segments from the tracts:', indent_char='*', indent_lvl=1, with_progress=True )
|
|
470
|
+
with ProgressBar(disable=self.verbose < 3, hide_on_exit=True, subinfo=ret_subinfo, log_list=log_list):
|
|
463
471
|
self.DICTIONARY['TRK'] = {}
|
|
464
472
|
self.DICTIONARY['TRK']['kept'] = np.fromfile( pjoin(self.get_config('TRACKING_path'),'dictionary_TRK_kept.dict'), dtype=np.uint8 )
|
|
465
473
|
self.DICTIONARY['TRK']['norm'] = np.fromfile( pjoin(self.get_config('TRACKING_path'),'dictionary_TRK_norm.dict'), dtype=np.float32 )
|
|
@@ -498,8 +506,9 @@ cdef class Evaluation :
|
|
|
498
506
|
|
|
499
507
|
# segments from the peaks
|
|
500
508
|
# -----------------------
|
|
501
|
-
|
|
502
|
-
|
|
509
|
+
log_list = []
|
|
510
|
+
ret_subinfo = logger.subinfo('Segments from the peaks:', indent_char='*', indent_lvl=1, with_progress=True )
|
|
511
|
+
with ProgressBar(disable=self.verbose < 3, hide_on_exit=True, subinfo=ret_subinfo, log_list=log_list):
|
|
503
512
|
self.DICTIONARY['EC'] = {}
|
|
504
513
|
self.DICTIONARY['EC']['v'] = np.fromfile( pjoin(self.get_config('TRACKING_path'),'dictionary_EC_v.dict'), dtype=np.uint32 )
|
|
505
514
|
self.DICTIONARY['EC']['o'] = np.fromfile( pjoin(self.get_config('TRACKING_path'),'dictionary_EC_o.dict'), dtype=np.uint16 )
|
|
@@ -515,8 +524,9 @@ cdef class Evaluation :
|
|
|
515
524
|
|
|
516
525
|
# isotropic compartments
|
|
517
526
|
# ----------------------
|
|
518
|
-
|
|
519
|
-
|
|
527
|
+
log_list = []
|
|
528
|
+
ret_subinfo = logger.subinfo('Isotropic contributions:', indent_char='*', indent_lvl=1, with_progress=True )
|
|
529
|
+
with ProgressBar(disable=self.verbose < 3, hide_on_exit=True, subinfo=ret_subinfo, log_list=log_list):
|
|
520
530
|
self.DICTIONARY['ISO'] = {}
|
|
521
531
|
|
|
522
532
|
self.DICTIONARY['ISO']['v'] = np.fromfile( pjoin(self.get_config('TRACKING_path'),'dictionary_ISO_v.dict'), dtype=np.uint32 )
|
|
@@ -533,8 +543,9 @@ cdef class Evaluation :
|
|
|
533
543
|
|
|
534
544
|
# post-processing
|
|
535
545
|
# ---------------
|
|
536
|
-
|
|
537
|
-
|
|
546
|
+
log_list = []
|
|
547
|
+
ret_subinfo = logger.subinfo('Post-processing', indent_char='*', indent_lvl=1, with_progress=True )
|
|
548
|
+
with ProgressBar(disable=self.verbose < 3, hide_on_exit=True, subinfo=ret_subinfo, log_list=log_list):
|
|
538
549
|
# get the indices to extract the VOI as in MATLAB (in place of DICTIONARY.MASKidx)
|
|
539
550
|
idx = self.DICTIONARY['MASK'].ravel(order='F').nonzero()[0]
|
|
540
551
|
self.DICTIONARY['MASK_ix'], self.DICTIONARY['MASK_iy'], self.DICTIONARY['MASK_iz'] = np.unravel_index( idx, self.DICTIONARY['MASK'].shape, order='F' )
|
|
@@ -557,7 +568,7 @@ cdef class Evaluation :
|
|
|
557
568
|
----------
|
|
558
569
|
n : integer
|
|
559
570
|
Number of threads to use (default : number of CPUs in the system)
|
|
560
|
-
"""
|
|
571
|
+
"""
|
|
561
572
|
if n is None :
|
|
562
573
|
# Use the same number of threads used in trk2dictionary
|
|
563
574
|
n = self.DICTIONARY['n_threads']
|
|
@@ -574,7 +585,7 @@ cdef class Evaluation :
|
|
|
574
585
|
self.THREADS['n'] = n
|
|
575
586
|
|
|
576
587
|
cdef :
|
|
577
|
-
long [:] C
|
|
588
|
+
long long [:] C
|
|
578
589
|
long t, tot, i1, i2, N, c
|
|
579
590
|
int i
|
|
580
591
|
|
|
@@ -584,8 +595,9 @@ cdef class Evaluation :
|
|
|
584
595
|
logger.subinfo('Number of threads: %d' % n , indent_char='*', indent_lvl=1 )
|
|
585
596
|
|
|
586
597
|
# Distribute load for the computation of A*x product
|
|
587
|
-
|
|
588
|
-
|
|
598
|
+
log_list = []
|
|
599
|
+
ret_subinfo = logger.subinfo('A operator ', indent_char='*', indent_lvl=1, with_progress=True )
|
|
600
|
+
with ProgressBar(disable=self.verbose < 3, hide_on_exit=True, subinfo=ret_subinfo, log_list=log_list):
|
|
589
601
|
if self.DICTIONARY['IC']['n'] > 0 :
|
|
590
602
|
self.THREADS['IC'] = np.zeros( n+1, dtype=np.uint32 )
|
|
591
603
|
if n > 1 :
|
|
@@ -636,8 +648,9 @@ cdef class Evaluation :
|
|
|
636
648
|
|
|
637
649
|
|
|
638
650
|
# Distribute load for the computation of At*y product
|
|
639
|
-
|
|
640
|
-
|
|
651
|
+
log_list = []
|
|
652
|
+
ret_subinfo = logger.subinfo('A\' operator', indent_char='*', indent_lvl=1, with_progress=True )
|
|
653
|
+
with ProgressBar(disable=self.verbose < 3, hide_on_exit=True, subinfo=ret_subinfo, log_list=log_list):
|
|
641
654
|
if self.DICTIONARY['IC']['n'] > 0 :
|
|
642
655
|
self.THREADS['ICt'] = np.full( self.DICTIONARY['IC']['n'], n-1, dtype=np.uint8 )
|
|
643
656
|
if n > 1 :
|
|
@@ -692,19 +705,9 @@ cdef class Evaluation :
|
|
|
692
705
|
logger.info( f'[ {format_time(time.time() - tic)} ]' )
|
|
693
706
|
|
|
694
707
|
|
|
695
|
-
def build_operator( self
|
|
696
|
-
"""
|
|
708
|
+
def build_operator( self ) :
|
|
709
|
+
"""Build the operator for computing the matrix-vector multiplications by A and A'
|
|
697
710
|
using the informations from self.DICTIONARY, self.KERNELS and self.THREADS.
|
|
698
|
-
NB: needs to call this function to update pointers to data structures in case
|
|
699
|
-
the data is changed in self.DICTIONARY, self.KERNELS or self.THREADS.
|
|
700
|
-
|
|
701
|
-
Parameters
|
|
702
|
-
----------
|
|
703
|
-
build_dir : string
|
|
704
|
-
The folder in which to store the compiled files.
|
|
705
|
-
If None (default), they will end up in the .pyxbld directory in the user’s home directory.
|
|
706
|
-
If using this option, it is recommended to use a temporary directory, quit your python
|
|
707
|
-
console between each build, and delete the content of the temporary directory.
|
|
708
711
|
"""
|
|
709
712
|
if self.DICTIONARY is None :
|
|
710
713
|
logger.error( 'Dictionary not loaded; call "load_dictionary()" first' )
|
|
@@ -722,49 +725,7 @@ cdef class Evaluation :
|
|
|
722
725
|
logger.subinfo('')
|
|
723
726
|
logger.info( 'Building linear operator A' )
|
|
724
727
|
|
|
725
|
-
|
|
726
|
-
from commit.operator import config
|
|
727
|
-
|
|
728
|
-
compilation_is_needed = False
|
|
729
|
-
|
|
730
|
-
if config.nTHREADS is None or config.nTHREADS != self.THREADS['n']:
|
|
731
|
-
compilation_is_needed = True
|
|
732
|
-
if config.nIC is None or config.nIC != self.KERNELS['wmr'].shape[0]:
|
|
733
|
-
compilation_is_needed = True
|
|
734
|
-
if config.model is None or config.model != self.model.id:
|
|
735
|
-
compilation_is_needed = True
|
|
736
|
-
if config.nEC is None or config.nEC != self.KERNELS['wmh'].shape[0]:
|
|
737
|
-
compilation_is_needed = True
|
|
738
|
-
if config.nISO is None or config.nISO != self.KERNELS['iso'].shape[0]:
|
|
739
|
-
compilation_is_needed = True
|
|
740
|
-
if config.build_dir != build_dir:
|
|
741
|
-
compilation_is_needed = True
|
|
742
|
-
|
|
743
|
-
if compilation_is_needed or not 'commit.operator.operator' in sys.modules :
|
|
744
|
-
|
|
745
|
-
if build_dir is not None:
|
|
746
|
-
if isdir(build_dir) and not len(listdir(build_dir)) == 0:
|
|
747
|
-
logger.error( '\nbuild_dir is not empty, unsafe build option.' )
|
|
748
|
-
elif config.nTHREADS is not None:
|
|
749
|
-
logger.error( '\nThe parameter build_dir has changed, unsafe build option.' )
|
|
750
|
-
else:
|
|
751
|
-
logger.warning( '\nUsing build_dir, always quit your python console between COMMIT Evaluation.' )
|
|
752
|
-
|
|
753
|
-
config.nTHREADS = self.THREADS['n']
|
|
754
|
-
config.model = self.model.id
|
|
755
|
-
config.nIC = self.KERNELS['wmr'].shape[0]
|
|
756
|
-
config.nEC = self.KERNELS['wmh'].shape[0]
|
|
757
|
-
config.nISO = self.KERNELS['iso'].shape[0]
|
|
758
|
-
config.build_dir = build_dir
|
|
759
|
-
|
|
760
|
-
sys.dont_write_bytecode = True
|
|
761
|
-
pyximport.install( reload_support=True, language_level=3, build_dir=build_dir, build_in_temp=True, inplace=False )
|
|
762
|
-
|
|
763
|
-
if 'commit.operator.operator' in sys.modules :
|
|
764
|
-
del sys.modules['commit.operator.operator']
|
|
765
|
-
import commit.operator.operator
|
|
766
|
-
|
|
767
|
-
self.A = commit.operator.operator.LinearOperator( self.DICTIONARY, self.KERNELS, self.THREADS )
|
|
728
|
+
self.A = operator.LinearOperator( self.DICTIONARY, self.KERNELS, self.THREADS, True if hasattr(self.model, 'nolut') else False )
|
|
768
729
|
|
|
769
730
|
logger.info( f'[ {format_time(time.time() - tic)} ]' )
|
|
770
731
|
|
|
@@ -869,7 +830,7 @@ cdef class Evaluation :
|
|
|
869
830
|
This field can be specified only if regularisers[0] is 'group_lasso' or 'sparse_group_lasso'.
|
|
870
831
|
NB: this array must have the same size as the number of groups in the IC compartment and contain only non-negative values.
|
|
871
832
|
'coeff_weights' - np.array(np.float64) :
|
|
872
|
-
weights associated to each individual element of the compartment (implemented for
|
|
833
|
+
weights associated to each individual element of the compartment (for the moment implemented only for IC compartment).
|
|
873
834
|
This field can be specified only if the chosen regulariser is 'lasso' or 'sparse_group_lasso'.
|
|
874
835
|
NB: this array must have the same size as the number of elements in the compartment and contain only non-negative values.
|
|
875
836
|
|
|
@@ -960,7 +921,7 @@ cdef class Evaluation :
|
|
|
960
921
|
logger.error('Regularisation parameters for the IC compartment ara not exactly two')
|
|
961
922
|
elif lambdas[0][0] < 0 or lambdas[0][1] < 0:
|
|
962
923
|
logger.error('Regularisation parameters for the IC compartment must be greater than 0')
|
|
963
|
-
elif lambdas[0][0] == 0
|
|
924
|
+
elif lambdas[0][0] == 0 and lambdas[0][1] == 0:
|
|
964
925
|
logger.warning('Regularisation parameters for the IC compartment are both 0, the solution will be the same as the one without regularisation')
|
|
965
926
|
regularisation['lambdaIC_perc'] = lambdas[0]
|
|
966
927
|
else:
|
|
@@ -972,7 +933,7 @@ cdef class Evaluation :
|
|
|
972
933
|
|
|
973
934
|
# check if coeff_weights is consistent with the regularisation
|
|
974
935
|
if regularisation['regIC'] not in ['lasso', 'sparse_group_lasso'] and dictIC_params is not None and 'coeff_weights' in dictIC_params:
|
|
975
|
-
logger.warning('Coefficients weights are allowed only for lasso and sparse_group_lasso
|
|
936
|
+
logger.warning('Coefficients weights are allowed only for lasso and sparse_group_lasso regularisations. Ignoring "coeff_weights"')
|
|
976
937
|
|
|
977
938
|
# check if coeff_weights is consistent with the compartment size
|
|
978
939
|
if regularisation['regIC'] == 'lasso' or regularisation['regIC'] == 'sparse_group_lasso':
|
|
@@ -981,7 +942,7 @@ cdef class Evaluation :
|
|
|
981
942
|
logger.error('All coefficients weights must be non-negative')
|
|
982
943
|
if dictIC_params['coeff_weights'].size != len(self.DICTIONARY['TRK']['kept']):
|
|
983
944
|
logger.error(f'"coeff_weights" must have the same size as the number of elements in the IC compartment (got {dictIC_params["coeff_weights"].size} but {len(self.DICTIONARY["TRK"]["kept"])} expected)')
|
|
984
|
-
dictIC_params['
|
|
945
|
+
dictIC_params['coeff_weights_kept'] = dictIC_params['coeff_weights'][self.DICTIONARY['TRK']['kept']==1]
|
|
985
946
|
|
|
986
947
|
# check if group parameters are consistent with the regularisation
|
|
987
948
|
if regularisation['regIC'] not in ['group_lasso', 'sparse_group_lasso'] and dictIC_params is not None:
|
|
@@ -1025,6 +986,9 @@ cdef class Evaluation :
|
|
|
1025
986
|
|
|
1026
987
|
# check if group_weights_extra is consistent with the number of groups
|
|
1027
988
|
if (regularisation['regIC'] == 'group_lasso' or regularisation['regIC'] == 'sparse_group_lasso') and 'group_weights_extra' in dictIC_params:
|
|
989
|
+
if type(dictIC_params['group_weights_extra']) not in [list, np.ndarray]:
|
|
990
|
+
logger.error('"group_weights_extra" must be a list or a numpy array')
|
|
991
|
+
dictIC_params['group_weights_extra'] = np.array(dictIC_params['group_weights_extra'], dtype=np.float64)
|
|
1028
992
|
if np.any(dictIC_params['group_weights_extra'] < 0):
|
|
1029
993
|
logger.error('All group weights must be non-negative')
|
|
1030
994
|
if dictIC_params['group_weights_extra'].size != dictIC_params['group_idx'].size:
|
|
@@ -1058,24 +1022,25 @@ cdef class Evaluation :
|
|
|
1058
1022
|
newweightsIC_group.append(weightsIC_group[count])
|
|
1059
1023
|
|
|
1060
1024
|
newweightsIC_group = np.array(newweightsIC_group, dtype=np.float64)
|
|
1061
|
-
dictIC_params['
|
|
1025
|
+
dictIC_params['group_idx_kept'] = np.array(newICgroup_idx, dtype=np.object_)
|
|
1062
1026
|
if weightsIC_group.size != newweightsIC_group.size:
|
|
1063
1027
|
logger.warning(f"""\
|
|
1064
1028
|
Not all the original groups are kept.
|
|
1065
|
-
{weightsIC_group.size - newweightsIC_group.size} groups have been removed because their streamlines didn't
|
|
1029
|
+
{weightsIC_group.size - newweightsIC_group.size} groups have been removed because their streamlines didn't satisfy the criteria set in trk2dictionary.""")
|
|
1066
1030
|
else:
|
|
1067
1031
|
newweightsIC_group = weightsIC_group
|
|
1032
|
+
dictIC_params['group_idx_kept'] = dictIC_params['group_idx']
|
|
1068
1033
|
|
|
1069
1034
|
# compute group weights
|
|
1070
1035
|
if regularisation['regIC'] == 'group_lasso' or regularisation['regIC'] == 'sparse_group_lasso':
|
|
1071
1036
|
if dictIC_params['group_weights_cardinality']:
|
|
1072
|
-
group_size = np.array([g.size for g in dictIC_params['
|
|
1037
|
+
group_size = np.array([g.size for g in dictIC_params['group_idx_kept']], dtype=np.int32)
|
|
1073
1038
|
newweightsIC_group *= np.sqrt(group_size)
|
|
1074
1039
|
if dictIC_params['group_weights_adaptive']:
|
|
1075
|
-
if self.
|
|
1040
|
+
if self.x_nnls is None: #or self.regularisation_params['regIC'] is not None:
|
|
1076
1041
|
logger.error('Group weights cannot be computed if the fit without regularisation has not been performed before')
|
|
1077
|
-
x_nnls, _, _ = self.get_coeffs(get_normalized=False)
|
|
1078
|
-
group_x_norm = np.array([np.linalg.norm(x_nnls[g])+1e-12 for g in dictIC_params['
|
|
1042
|
+
# x_nnls, _, _ = self.get_coeffs(get_normalized=False)
|
|
1043
|
+
group_x_norm = np.array([np.linalg.norm(self.x_nnls[g])+1e-12 for g in dictIC_params['group_idx_kept']], dtype=np.float64)
|
|
1079
1044
|
newweightsIC_group /= group_x_norm
|
|
1080
1045
|
dictIC_params['group_weights'] = newweightsIC_group
|
|
1081
1046
|
|
|
@@ -1083,21 +1048,24 @@ cdef class Evaluation :
|
|
|
1083
1048
|
|
|
1084
1049
|
# update lambdas using lambda_max
|
|
1085
1050
|
if regularisation['regIC'] == 'lasso':
|
|
1086
|
-
if dictIC_params is not None and '
|
|
1087
|
-
regularisation['lambdaIC_max'] = compute_lambda_max_lasso(regularisation['startIC'], regularisation['sizeIC'], dictIC_params['
|
|
1051
|
+
if dictIC_params is not None and 'coeff_weights_kept' in dictIC_params:
|
|
1052
|
+
regularisation['lambdaIC_max'] = compute_lambda_max_lasso(regularisation['startIC'], regularisation['sizeIC'], dictIC_params['coeff_weights_kept'])
|
|
1088
1053
|
else:
|
|
1089
1054
|
regularisation['lambdaIC_max'] = compute_lambda_max_lasso(regularisation['startIC'], regularisation['sizeIC'], np.ones(regularisation['sizeIC'], dtype=np.float64))
|
|
1090
1055
|
regularisation['lambdaIC'] = regularisation['lambdaIC_perc'] * regularisation['lambdaIC_max']
|
|
1091
1056
|
if regularisation['regIC'] == 'group_lasso':
|
|
1092
|
-
regularisation['lambdaIC_max'] = compute_lambda_max_group(dictIC_params['group_weights'], dictIC_params['
|
|
1057
|
+
regularisation['lambdaIC_max'] = compute_lambda_max_group(dictIC_params['group_weights'], dictIC_params['group_idx_kept'])
|
|
1093
1058
|
regularisation['lambdaIC'] = regularisation['lambdaIC_perc'] * regularisation['lambdaIC_max']
|
|
1094
1059
|
if regularisation['regIC'] == 'sparse_group_lasso':
|
|
1095
|
-
|
|
1060
|
+
if 'coeff_weights_kept' in dictIC_params:
|
|
1061
|
+
regularisation['lambdaIC_max'] = ( compute_lambda_max_lasso(regularisation['startIC'], regularisation['sizeIC'], dictIC_params['coeff_weights_kept']), compute_lambda_max_group(dictIC_params['group_weights'], dictIC_params['group_idx_kept']) )
|
|
1062
|
+
else:
|
|
1063
|
+
regularisation['lambdaIC_max'] = ( compute_lambda_max_lasso(regularisation['startIC'], regularisation['sizeIC'], np.ones(regularisation['sizeIC'], dtype=np.float64)), compute_lambda_max_group(dictIC_params['group_weights'], dictIC_params['group_idx_kept']) )
|
|
1096
1064
|
regularisation['lambdaIC'] = ( regularisation['lambdaIC_perc'][0] * regularisation['lambdaIC_max'][0], regularisation['lambdaIC_perc'][1] * regularisation['lambdaIC_max'][1] )
|
|
1097
1065
|
|
|
1098
1066
|
# print
|
|
1099
1067
|
if regularisation['regIC'] is not None:
|
|
1100
|
-
if (regularisation['regIC'] == 'lasso' or regularisation['regIC'] == 'sparse_group_lasso') and dictIC_params is not None and '
|
|
1068
|
+
if (regularisation['regIC'] == 'lasso' or regularisation['regIC'] == 'sparse_group_lasso') and dictIC_params is not None and 'coeff_weights_kept' in dictIC_params:
|
|
1101
1069
|
logger.subinfo( f'Regularisation type: {regularisation["regIC"]} (weighted version)', indent_lvl=2, indent_char='-' )
|
|
1102
1070
|
else:
|
|
1103
1071
|
logger.subinfo( f'Regularisation type: {regularisation["regIC"]}', indent_lvl=2, indent_char='-' )
|
|
@@ -1108,8 +1076,8 @@ cdef class Evaluation :
|
|
|
1108
1076
|
logger.debug( f'% lambda: {regularisation["lambdaIC_perc"]}' )
|
|
1109
1077
|
logger.debug( f'Lambda used: {regularisation["lambdaIC"]}' )
|
|
1110
1078
|
if regularisation['regIC'] == 'group_lasso' or regularisation['regIC'] == 'sparse_group_lasso':
|
|
1111
|
-
logger.debug( f'Number of groups: {len(dictIC_params["
|
|
1112
|
-
if dictIC_params['group_weights_cardinality']==False and dictIC_params['group_weights_adaptive']==False and
|
|
1079
|
+
logger.debug( f'Number of groups: {len(dictIC_params["group_idx_kept"])}' )
|
|
1080
|
+
if dictIC_params['group_weights_cardinality']==False and dictIC_params['group_weights_adaptive']==False and not ('group_weights_extra' in dictIC_params):
|
|
1113
1081
|
logger.debug( 'Group weights are not considered (all ones)' )
|
|
1114
1082
|
else:
|
|
1115
1083
|
str_weights = 'Group weights computed using '
|
|
@@ -1139,9 +1107,9 @@ cdef class Evaluation :
|
|
|
1139
1107
|
regularisation['lambdaEC_perc'] = lambdas[1]
|
|
1140
1108
|
else:
|
|
1141
1109
|
regularisation['lambdaEC_perc'] = lambdas[1]
|
|
1142
|
-
if dictEC_params is not None and 'coeff_weights' in dictEC_params:
|
|
1143
|
-
|
|
1144
|
-
|
|
1110
|
+
# if dictEC_params is not None and 'coeff_weights' in dictEC_params:
|
|
1111
|
+
# if dictEC_params['coeff_weights'].size != regularisation['sizeEC']:
|
|
1112
|
+
# logger.error(f'"coeff_weights" must have the same size as the number of elements in the EC compartment (got {dictEC_params["coeff_weights"].size} but {regularisation["sizeEC"]} expected)')
|
|
1145
1113
|
elif regularisation['regEC'] == 'smoothness':
|
|
1146
1114
|
logger.error('Not yet implemented')
|
|
1147
1115
|
elif regularisation['regEC'] == 'group_lasso':
|
|
@@ -1155,18 +1123,18 @@ cdef class Evaluation :
|
|
|
1155
1123
|
|
|
1156
1124
|
# update lambdas using lambda_max
|
|
1157
1125
|
if regularisation['regEC'] == 'lasso':
|
|
1158
|
-
if dictEC_params is not None and 'coeff_weights' in dictEC_params:
|
|
1159
|
-
|
|
1160
|
-
else:
|
|
1161
|
-
|
|
1126
|
+
# if dictEC_params is not None and 'coeff_weights' in dictEC_params:
|
|
1127
|
+
# regularisation['lambdaEC_max'] = compute_lambda_max_lasso(regularisation['startEC'], regularisation['sizeEC'], dictEC_params['coeff_weights'])
|
|
1128
|
+
# else:
|
|
1129
|
+
regularisation['lambdaEC_max'] = compute_lambda_max_lasso(regularisation['startEC'], regularisation['sizeEC'], np.ones(regularisation['sizeEC'], dtype=np.float64))
|
|
1162
1130
|
regularisation['lambdaEC'] = regularisation['lambdaEC_perc'] * regularisation['lambdaEC_max']
|
|
1163
1131
|
|
|
1164
1132
|
# print
|
|
1165
1133
|
if regularisation['regEC'] is not None:
|
|
1166
|
-
if regularisation['regEC'] == 'lasso' and dictEC_params is not None and 'coeff_weights' in dictEC_params:
|
|
1167
|
-
|
|
1168
|
-
else:
|
|
1169
|
-
|
|
1134
|
+
# if regularisation['regEC'] == 'lasso' and dictEC_params is not None and 'coeff_weights' in dictEC_params:
|
|
1135
|
+
# logger.subinfo( f'Regularisation type: {regularisation["regEC"]} (weighted version)', indent_lvl=2, indent_char='-' )
|
|
1136
|
+
# else:
|
|
1137
|
+
logger.subinfo( f'Regularisation type: {regularisation["regEC"]}', indent_lvl=2, indent_char='-' )
|
|
1170
1138
|
|
|
1171
1139
|
logger.subinfo( f'Non-negativity constraint: {regularisation["nnEC"]}', indent_char='-', indent_lvl=2 )
|
|
1172
1140
|
|
|
@@ -1194,9 +1162,9 @@ cdef class Evaluation :
|
|
|
1194
1162
|
regularisation['lambdaISO_perc'] = lambdas[2]
|
|
1195
1163
|
else:
|
|
1196
1164
|
regularisation['lambdaISO_perc'] = lambdas[2]
|
|
1197
|
-
if dictISO_params is not None and 'coeff_weights' in dictISO_params:
|
|
1198
|
-
|
|
1199
|
-
|
|
1165
|
+
# if dictISO_params is not None and 'coeff_weights' in dictISO_params:
|
|
1166
|
+
# if dictISO_params['coeff_weights'].size != regularisation['sizeISO']:
|
|
1167
|
+
# logger.error(f'"coeff_weights" must have the same size as the number of elements in the ISO compartment (got {dictISO_params["coeff_weights"].size} but {regularisation["sizeISO"]} expected)')
|
|
1200
1168
|
elif regularisation['regISO'] == 'smoothness':
|
|
1201
1169
|
logger.error('Not yet implemented')
|
|
1202
1170
|
elif regularisation['regISO'] == 'group_lasso':
|
|
@@ -1210,18 +1178,18 @@ cdef class Evaluation :
|
|
|
1210
1178
|
|
|
1211
1179
|
# update lambdas using lambda_max
|
|
1212
1180
|
if regularisation['regISO'] == 'lasso':
|
|
1213
|
-
if dictISO_params is not None and 'coeff_weights' in dictISO_params:
|
|
1214
|
-
|
|
1215
|
-
else:
|
|
1216
|
-
|
|
1181
|
+
# if dictISO_params is not None and 'coeff_weights' in dictISO_params:
|
|
1182
|
+
# regularisation['lambdaISO_max'] = compute_lambda_max_lasso(regularisation['startISO'], regularisation['sizeISO'], dictISO_params['coeff_weights'])
|
|
1183
|
+
# else:
|
|
1184
|
+
regularisation['lambdaISO_max'] = compute_lambda_max_lasso(regularisation['startISO'], regularisation['sizeISO'], np.ones(regularisation['sizeISO'], dtype=np.float64))
|
|
1217
1185
|
regularisation['lambdaISO'] = regularisation['lambdaISO_perc'] * regularisation['lambdaISO_max']
|
|
1218
1186
|
|
|
1219
1187
|
# print
|
|
1220
1188
|
if regularisation['regISO'] is not None:
|
|
1221
|
-
if regularisation['regISO'] == 'lasso' and dictISO_params is not None and 'coeff_weights' in dictISO_params:
|
|
1222
|
-
|
|
1223
|
-
else:
|
|
1224
|
-
|
|
1189
|
+
# if regularisation['regISO'] == 'lasso' and dictISO_params is not None and 'coeff_weights' in dictISO_params:
|
|
1190
|
+
# logger.subinfo( f'Regularisation type: {regularisation["regISO"]} (weighted version)', indent_lvl=2, indent_char='-' )
|
|
1191
|
+
# else:
|
|
1192
|
+
logger.subinfo( f'Regularisation type: {regularisation["regISO"]}', indent_lvl=2, indent_char='-' )
|
|
1225
1193
|
|
|
1226
1194
|
logger.subinfo( f'Non-negativity constraint: {regularisation["nnISO"]}', indent_char='-', indent_lvl=2 )
|
|
1227
1195
|
if regularisation['regISO'] is not None:
|
|
@@ -1353,12 +1321,15 @@ cdef class Evaluation :
|
|
|
1353
1321
|
|
|
1354
1322
|
# run solver
|
|
1355
1323
|
t = time.time()
|
|
1356
|
-
with ProgressBar(disable=self.verbose!=3, hide_on_exit=True)
|
|
1324
|
+
with ProgressBar(disable=self.verbose!=3, hide_on_exit=True):
|
|
1357
1325
|
self.x, opt_details = commit.solvers.solve(self.get_y(), self.A, self.A.T, tol_fun=tol_fun, tol_x=tol_x, max_iter=max_iter, verbose=self.verbose, x0=x0, regularisation=self.regularisation_params, confidence_array=confidence_array)
|
|
1358
1326
|
|
|
1359
1327
|
self.CONFIG['optimization']['fit_details'] = opt_details
|
|
1360
1328
|
self.CONFIG['optimization']['fit_time'] = time.time()-t
|
|
1361
1329
|
|
|
1330
|
+
if self.regularisation_params['regIC'] is None and self.x_nnls is None:
|
|
1331
|
+
self.x_nnls, _, _ = self.get_coeffs(get_normalized=False)
|
|
1332
|
+
|
|
1362
1333
|
logger.info( f'[ {format_time(self.CONFIG["optimization"]["fit_time"])} ]' )
|
|
1363
1334
|
|
|
1364
1335
|
|
|
@@ -1515,8 +1486,9 @@ cdef class Evaluation :
|
|
|
1515
1486
|
# Map of compartment contributions
|
|
1516
1487
|
logger.subinfo('Voxelwise contributions:', indent_char='*', indent_lvl=1)
|
|
1517
1488
|
|
|
1518
|
-
|
|
1519
|
-
|
|
1489
|
+
log_list = []
|
|
1490
|
+
ret_subinfo = logger.subinfo('Intra-axonal', indent_lvl=2, indent_char='-', with_progress=True)
|
|
1491
|
+
with ProgressBar(disable=self.verbose < 3, hide_on_exit=True, subinfo=ret_subinfo, log_list=log_list):
|
|
1520
1492
|
niiIC_img = np.zeros( self.get_config('dim'), dtype=np.float32 )
|
|
1521
1493
|
if len(self.KERNELS['wmr']) > 0 :
|
|
1522
1494
|
offset = nF * self.KERNELS['wmr'].shape[0]
|
|
@@ -1526,8 +1498,9 @@ cdef class Evaluation :
|
|
|
1526
1498
|
).astype(np.float32)
|
|
1527
1499
|
niiIC_img[ self.DICTIONARY['MASK_ix'], self.DICTIONARY['MASK_iy'], self.DICTIONARY['MASK_iz'] ] = xv
|
|
1528
1500
|
|
|
1529
|
-
|
|
1530
|
-
|
|
1501
|
+
log_list = []
|
|
1502
|
+
ret_subinfo = logger.subinfo('Extra-axonal', indent_lvl=2, indent_char='-', with_progress=True)
|
|
1503
|
+
with ProgressBar(disable=self.verbose < 3, hide_on_exit=True, subinfo=ret_subinfo, log_list=log_list):
|
|
1531
1504
|
niiEC_img = np.zeros( self.get_config('dim'), dtype=np.float32 )
|
|
1532
1505
|
if len(self.KERNELS['wmh']) > 0 :
|
|
1533
1506
|
offset = nF * self.KERNELS['wmr'].shape[0]
|
|
@@ -1535,8 +1508,9 @@ cdef class Evaluation :
|
|
|
1535
1508
|
xv = np.bincount( self.DICTIONARY['EC']['v'], weights=tmp, minlength=nV ).astype(np.float32)
|
|
1536
1509
|
niiEC_img[ self.DICTIONARY['MASK_ix'], self.DICTIONARY['MASK_iy'], self.DICTIONARY['MASK_iz'] ] = xv
|
|
1537
1510
|
|
|
1538
|
-
|
|
1539
|
-
|
|
1511
|
+
log_list = []
|
|
1512
|
+
ret_subinfo = logger.subinfo('Isotropic ', indent_lvl=2, indent_char='-', with_progress=True)
|
|
1513
|
+
with ProgressBar(disable=self.verbose < 3, hide_on_exit=True, subinfo=ret_subinfo, log_list=log_list):
|
|
1540
1514
|
niiISO_img = np.zeros( self.get_config('dim'), dtype=np.float32 )
|
|
1541
1515
|
if len(self.KERNELS['iso']) > 0 :
|
|
1542
1516
|
offset = nF * self.KERNELS['wmr'].shape[0] + nE * self.KERNELS['wmh'].shape[0]
|
|
@@ -1560,8 +1534,9 @@ cdef class Evaluation :
|
|
|
1560
1534
|
|
|
1561
1535
|
# Configuration and results
|
|
1562
1536
|
logger.subinfo('Configuration and results:', indent_char='*', indent_lvl=1)
|
|
1563
|
-
|
|
1564
|
-
|
|
1537
|
+
log_list = []
|
|
1538
|
+
ret_subinfo = logger.subinfo('streamline_weights.txt', indent_lvl=2, indent_char='-', with_progress=True)
|
|
1539
|
+
with ProgressBar(disable=self.verbose < 3, hide_on_exit=True, subinfo=ret_subinfo, log_list=log_list):
|
|
1565
1540
|
xic, _, _ = self.get_coeffs()
|
|
1566
1541
|
if stat_coeffs != 'all' and xic.size > 0 :
|
|
1567
1542
|
xic = np.reshape( xic, (-1,self.DICTIONARY['TRK']['kept'].size) )
|
|
@@ -1620,20 +1595,18 @@ cdef class Evaluation :
|
|
|
1620
1595
|
# item 0: dictionary with all the configuration details
|
|
1621
1596
|
# item 1: np.array obtained through the optimisation process with the normalised kernels
|
|
1622
1597
|
# item 2: np.array renormalisation of coeffs in item 1
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
x = self.x
|
|
1627
|
-
if self.get_config('doNormalizeKernels') :
|
|
1628
|
-
x = x * np.hstack( (norm1*norm_fib,norm2,norm3) )
|
|
1598
|
+
log_list = []
|
|
1599
|
+
ret_subinfo = logger.subinfo('results.pickle', indent_char='-', indent_lvl=2, with_progress=True)
|
|
1600
|
+
with ProgressBar(disable=self.verbose < 3, hide_on_exit=True, subinfo=ret_subinfo, log_list=log_list):
|
|
1629
1601
|
with open( pjoin(RESULTS_path,'results.pickle'), 'wb+' ) as fid :
|
|
1630
1602
|
self.CONFIG['optimization']['regularisation'].pop('omega', None)
|
|
1631
1603
|
self.CONFIG['optimization']['regularisation'].pop('prox', None)
|
|
1632
|
-
pickle.dump( [self.CONFIG, x,
|
|
1604
|
+
pickle.dump( [self.CONFIG, self.x, x], fid, protocol=2 )
|
|
1633
1605
|
|
|
1634
|
-
if save_est_dwi
|
|
1635
|
-
|
|
1636
|
-
|
|
1606
|
+
if save_est_dwi:
|
|
1607
|
+
log_list = []
|
|
1608
|
+
ret_subinfo = logger.subinfo('Estimated signal:', indent_char='-', indent_lvl=2, with_progress=True)
|
|
1609
|
+
with ProgressBar(disable=self.verbose < 3, hide_on_exit=True, subinfo=ret_subinfo, log_list=log_list):
|
|
1637
1610
|
self.niiDWI_img[ self.DICTIONARY['MASK_ix'], self.DICTIONARY['MASK_iy'], self.DICTIONARY['MASK_iz'], : ] = y_est
|
|
1638
1611
|
nibabel.save( nibabel.Nifti1Image( self.niiDWI_img , affine ), pjoin(RESULTS_path,'fit_signal_estimated.nii.gz') )
|
|
1639
1612
|
self.niiDWI_img[ self.DICTIONARY['MASK_ix'], self.DICTIONARY['MASK_iy'], self.DICTIONARY['MASK_iz'], : ] = y_mea
|