-
Notifications
You must be signed in to change notification settings - Fork 34
Add Tikhonov Regularization #98
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
base: master
Are you sure you want to change the base?
Changes from 17 commits
6289ce6
274e722
6855137
76da763
d18393b
298754a
e823152
dab38d4
3c9c729
f3692ac
fc5df1d
c0b870c
90f6e47
0750c20
e9a1988
82d05c5
b62d9d4
1178876
0549648
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,4 +15,4 @@ __pycache__/ | |
| *.cpp | ||
| dist/ | ||
|
|
||
| trk2dictionary.c | ||
| trk2dictionary.c | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -639,7 +639,7 @@ cdef class Evaluation : | |
| LOG( ' [ %.1f seconds ]' % ( time.time() - tic ) ) | ||
|
|
||
|
|
||
| def build_operator( self, build_dir=None ) : | ||
| def build_operator( self, build_dir=None, tikhonov_equalizer=0, deriv_matrix=None ) : | ||
ErickHernandezGutierrez marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| """Compile/build the operator for computing the matrix-vector multiplications by A and A' | ||
| using the informations from self.DICTIONARY, self.KERNELS and self.THREADS. | ||
| NB: needs to call this function to update pointers to data structures in case | ||
|
|
@@ -652,13 +652,25 @@ cdef class Evaluation : | |
| If None (default), they will end up in the .pyxbld directory in the user’s home directory. | ||
| If using this option, it is recommended to use a temporary directory, quit your python | ||
| console between each build, and delete the content of the temporary directory. | ||
| tikhonov_equalizer: float | ||
ErickHernandezGutierrez marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| equalizer parameter of the Tikhonov regularization term | ||
ErickHernandezGutierrez marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| deriv_matrix: string | ||
| derivative matrix of the Tikhonov regularization term | ||
ErickHernandezGutierrez marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| If None (default), no regularization term is added to the model | ||
| If using this option, tikhonov_equalizer must be positive | ||
ErickHernandezGutierrez marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| """ | ||
| if self.DICTIONARY is None : | ||
| ERROR( 'Dictionary not loaded; call "load_dictionary()" first' ) | ||
| if self.KERNELS is None : | ||
| ERROR( 'Response functions not generated; call "generate_kernels()" and "load_kernels()" first' ) | ||
| if self.THREADS is None : | ||
| ERROR( 'Threads not set; call "set_threads()" first' ) | ||
| if tikhonov_equalizer < 0: | ||
ErickHernandezGutierrez marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ERROR( 'Invalid value for Tikhonov equalizer parameter; value must be positive or zero' ) | ||
| if tikhonov_equalizer > 0 and deriv_matrix == None: | ||
| ERROR( 'Tikhonov equalizer term given but derivative matrix was not selected; add "deriv_matrix" parameter in "build_operator()", valid options are \'L1\' (first derivative with free boundary conditions), \'L2\' (second derivative with free boundary conditions), \'L1z\' (first derivative with zero boundary conditions) and \'L2z\' (second derivative with zero boundary conditions)' ) | ||
|
||
| if tikhonov_equalizer > 0 and deriv_matrix!='L1' and deriv_matrix!='L2' and deriv_matrix!='L1z' and deriv_matrix!='L2z': | ||
| ERROR( 'Invalid derivative matrix selection for regularization term; valid options are \'L1\' (first derivative with free boundary conditions), \'L2\' (second derivative with free boundary conditions), \'L1z\' (first derivative with zero boundary conditions) and \'L2z\' (second derivative with zero boundary conditions)' ) | ||
|
|
||
| if self.DICTIONARY['IC']['nF'] <= 0 : | ||
| ERROR( 'No streamline found in the dictionary; check your data' ) | ||
|
|
@@ -710,7 +722,7 @@ cdef class Evaluation : | |
| else : | ||
| reload( sys.modules['commit.operator.operator'] ) | ||
|
|
||
| self.A = sys.modules['commit.operator.operator'].LinearOperator( self.DICTIONARY, self.KERNELS, self.THREADS ) | ||
| self.A = sys.modules['commit.operator.operator'].LinearOperator( self.DICTIONARY, self.KERNELS, self.THREADS, tikhonov_equalizer, deriv_matrix ) | ||
|
|
||
| LOG( ' [ %.1f seconds ]' % ( time.time() - tic ) ) | ||
|
|
||
|
|
@@ -724,7 +736,26 @@ cdef class Evaluation : | |
| ERROR( 'Dictionary not loaded; call "load_dictionary()" first' ) | ||
| if self.niiDWI is None : | ||
| ERROR( 'Data not loaded; call "load_data()" first' ) | ||
| return self.niiDWI_img[ self.DICTIONARY['MASK_ix'], self.DICTIONARY['MASK_iy'], self.DICTIONARY['MASK_iz'], : ].flatten().astype(np.float64) | ||
|
|
||
| y = self.niiDWI_img[ self.DICTIONARY['MASK_ix'], self.DICTIONARY['MASK_iy'], self.DICTIONARY['MASK_iz'], : ].flatten().astype(np.float64) | ||
|
|
||
| # extend y for the tikhonov regularization term | ||
| if self.A.tikhonov_equalizer > 0: | ||
| if self.A.deriv_matrix == 'L1': | ||
| yL = np.zeros(y.shape[0] + self.KERNELS['wmr'].shape[0]-1, dtype=np.float64) | ||
| elif self.A.deriv_matrix == 'L2': | ||
| yL = np.zeros(y.shape[0] + self.KERNELS['wmr'].shape[0]-2, dtype=np.float64) | ||
| elif self.A.deriv_matrix == 'L1z': | ||
| yL = np.zeros(y.shape[0] + self.KERNELS['wmr'].shape[0]+1, dtype=np.float64) | ||
| elif self.A.deriv_matrix == 'L2z': | ||
| yL = np.zeros(y.shape[0] + self.KERNELS['wmr'].shape[0] , dtype=np.float64) | ||
| else: | ||
| ERROR( 'Invalid derivative matrix selection for regularization term; valid options are \'L1\' (first derivative with free boundary conditions), \'L2\' (second derivative with free boundary conditions), \'L1z\' (first derivative with zero boundary conditions) and \'L2z\' (second derivative with zero boundary conditions)' ) | ||
ErickHernandezGutierrez marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| yL[0:y.shape[0]] = y | ||
| return yL | ||
| else: | ||
| return y | ||
|
|
||
|
|
||
| def fit( self, tol_fun=1e-3, tol_x=1e-6, max_iter=100, verbose=True, x0=None, regularisation=None ) : | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -185,3 +185,62 @@ void COMMIT_At( | |
| pthread_join( threads[t], NULL ); | ||
| return; | ||
| } | ||
|
|
||
| void COMMIT_L( | ||
|
||
| int nF, int nIC, int nV, int nS, double regterm, | ||
| double *vIN, double *vOUT) | ||
| { | ||
| /*for(int r = 0; r < nIC-1; r++){ | ||
ErickHernandezGutierrez marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| for(int f = 0; f < nF; f++){ | ||
| vOUT[nV*nS + r] += regterm*( -vIN[r*nF + f] + vIN[(r+1)*nF + f] ); | ||
| } | ||
| }//*/ | ||
| } | ||
|
|
||
| void COMMIT_Lt( | ||
| int nF, int nIC, int nV, int nS, double regterm, | ||
| double *vIN, double *vOUT) | ||
| { | ||
| /*for(int f = 0; f < nF; f++){ | ||
| vOUT[f] = -vIN[nV*nS]; | ||
| vOUT[nF*(nIC-1) + f] = vIN[nV*nS + nIC-2]; | ||
| } | ||
|
|
||
| for(int r = 0; r < nIC-2; r++){ | ||
| for(int f = 0; f < nF; f++){ | ||
| vOUT[nF*(r+1) + f] = vIN[nV*nS + r] + vIN[nV*nS + r+1]; | ||
| } | ||
| }//*/ | ||
| } | ||
|
|
||
|
|
||
| /*void COMMIT_L( | ||
| int nF, int nIC, int nV, int nS, double regterm, | ||
| double *vIN, double *vOUT) | ||
| { | ||
| for(int f = 0; f < nF; f++){ | ||
|
|
||
| vOUT[nV*nS] += regterm*( -2*vIN[f] + x[nF + f] ); | ||
|
|
||
| for(int r = 1; r < nIC-1; r++){ | ||
| vOUT[nV*nS + r] += regterm*( vIN[(r-1)*nF + f] -2*vIN[r*nF + f] + vIN[(r+1)*nF + f] ); | ||
| } | ||
|
|
||
| vOUT[nV*nS + nIC - 1] += regterm*( vIN[(nIC-2)*nF + f] - 2*vIN[(nIC-1)*nF + f] ); | ||
| } | ||
| } | ||
|
|
||
| void COMMIT_Lt( | ||
| int nF, int nIC, int nV, int nS, double regterm, | ||
| double *vIN, double *vOUT) | ||
| { | ||
| for(int f = 0; f < nF; f++){ | ||
| vOUT[f] += regterm*( -2*vIN[nV*nS] + vIN[nV*nS + 1] ); | ||
|
|
||
| for (int r = 0; r < nIC; r++){ | ||
| vOUT[r*nF + f] += regterm*( vIN[nV*nS + (r-1)] - 2*vIN[nV*nS + r] + vIN[nV*nS + (r+1)] ); | ||
| } | ||
|
|
||
| vOUT[(nIC-1)*nF + f] += regterm*( vIN[nV*nS + (nIC-2)] - 2*vIN[nV*nS + (nIC-1)] ); | ||
| } | ||
| }//*/ | ||
Uh oh!
There was an error while loading. Please reload this page.