dmri-commit 2.3.0__cp38-cp38-win32.whl

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.
commit/__init__.py ADDED
@@ -0,0 +1,10 @@
1
+ from importlib import metadata
2
+
3
+ from .core import Evaluation, setup
4
+
5
+ __all__ = ['core','models','solvers','trk2dictionary']
6
+
7
+ try:
8
+ __version__ = metadata.version('dmri-commit')
9
+ except metadata.PackageNotFoundError:
10
+ __version__ = 'not installed'
Binary file
commit/models.py ADDED
@@ -0,0 +1,110 @@
1
+ from Cython.Build.Inline import _get_build_extension
2
+ from Cython.Build import cythonize
3
+ from os import environ
4
+ import os
5
+ from setuptools import Extension
6
+ import sys
7
+
8
+ import numpy as np
9
+
10
+ from amico.models import BaseModel, StickZeppelinBall as _StickZeppelinBall, CylinderZeppelinBall as _CylinderZeppelinBall
11
+ import amico.util as util
12
+
13
+ from dicelib.ui import setup_logger
14
+
15
+
16
+ logger = setup_logger('models')
17
+
18
+ try:
19
+ sys.path.append(environ["WIP_MODEL"])
20
+ extension = Extension(name='commitwipmodels',
21
+ language='c++',
22
+ sources=[os.environ['WIP_MODEL'] + '/commitwipmodels.pyx'],
23
+ extra_compile_args=['-w'])
24
+ build_extension = _get_build_extension()
25
+ build_extension.extensions = cythonize([extension],
26
+ include_path=[],
27
+ quiet=False)
28
+ build_extension.build_temp = os.environ['WIP_MODEL'] + '/build'
29
+ build_extension.build_lib = os.environ['WIP_MODEL']
30
+
31
+ build_extension.run()
32
+ from commitwipmodels import *
33
+
34
+ except ValueError:
35
+ # check if .so exists
36
+ path_files = os.listdir(environ["WIP_MODEL"])
37
+ for f in path_files:
38
+ if f.startswith('commitwipmodels') and f.endswith('.so'):
39
+ from commitwipmodels import *
40
+ else:
41
+ pass
42
+
43
+ except KeyError:
44
+ pass
45
+ except ImportError:
46
+ pass
47
+
48
+
49
+ class StickZeppelinBall(_StickZeppelinBall):
50
+ """Simulate the response functions according to the Stick-Zeppelin-Ball model.
51
+ See the AMICO.model module for details.
52
+ """
53
+ def resample(self, in_path, idx_out, Ylm_out, doMergeB0, ndirs):
54
+ util.set_verbose(2)
55
+ return super().resample(in_path, idx_out, Ylm_out, doMergeB0, ndirs)
56
+
57
+
58
+
59
+ class CylinderZeppelinBall(_CylinderZeppelinBall):
60
+ """Simulate the response functions according to the Cylinder-Zeppelin-Ball model.
61
+ See the AMICO.model module for details.
62
+ """
63
+ def resample(self, in_path, idx_out, Ylm_out, doMergeB0, ndirs):
64
+ util.set_verbose(2)
65
+ return super().resample(in_path, idx_out, Ylm_out, doMergeB0, ndirs)
66
+
67
+ class VolumeFractions(BaseModel):
68
+ """Implements a simple model where each compartment contributes only with
69
+ its own volume fraction. This model has been created to test there
70
+ ability to remove false positive fibers with COMMIT.
71
+ """
72
+ def __init__(self):
73
+ self.id = 'VolumeFractions'
74
+ self.name = 'Volume fractions'
75
+ self.maps_name = []
76
+ self.maps_descr = []
77
+ self.nolut = True
78
+
79
+ def set(self):
80
+ return
81
+
82
+ def get_params(self):
83
+ params = {}
84
+ params['id'] = self.id
85
+ params['name'] = self.name
86
+ return params
87
+
88
+ def set_solver(self):
89
+ ERROR('Not implemented')
90
+
91
+ def generate(self, out_path, aux, idx_in, idx_out, ndirs):
92
+ return
93
+
94
+ def resample(self, in_path, idx_out, Ylm_out, doMergeB0, ndirs):
95
+ if doMergeB0:
96
+ nS = 1 + self.scheme.dwi_count
97
+ merge_idx = np.hstack((self.scheme.b0_idx[0], self.scheme.dwi_idx))
98
+ else:
99
+ nS = self.scheme.nS
100
+ merge_idx = np.arange(nS)
101
+
102
+ KERNELS = {}
103
+ KERNELS['model'] = self.id
104
+ KERNELS['wmr'] = np.ones((1, ndirs, nS), dtype=np.float32)
105
+ KERNELS['wmh'] = np.ones((0, ndirs, nS), dtype=np.float32)
106
+ KERNELS['iso'] = np.ones((0, nS), dtype=np.float32)
107
+ return KERNELS
108
+
109
+ def fit(self, evaluation):
110
+ ERROR('Not implemented')
File without changes
Binary file
@@ -0,0 +1,246 @@
1
+ #!python
2
+ #cython: language_level=3, boundscheck=False, wraparound=False, profile=False
3
+
4
+ import numpy as np
5
+
6
+ from dicelib.ui import setup_logger
7
+
8
+ # Interfaces to actual C code performing the multiplications
9
+ cdef extern void COMMIT_A(
10
+ int _nF, int _nE, int _nV, int _nS, int _ndirs,
11
+ double *_v_in, double *_v_out,
12
+ unsigned int *_ICf, unsigned int *_ICv, unsigned short *_ICo, float *_ICl,
13
+ unsigned int *_ECv, unsigned short *_ECo,
14
+ unsigned int *_ISOv,
15
+ float *_wmrSFP, float *_wmhSFP, float *_isoSFP,
16
+ unsigned int* _ICthreads, unsigned int* _ECthreads, unsigned int* _ISOthreads,
17
+ unsigned int _nIC, unsigned int _nEC, unsigned int _nISO, unsigned int _nThreads
18
+ ) nogil
19
+
20
+ cdef extern void COMMIT_At(
21
+ int _nF, int _n, int _nE, int _nV, int _nS, int _ndirs,
22
+ double *_v_in, double *_v_out,
23
+ unsigned int *_ICf, unsigned int *_ICv, unsigned short *_ICo, float *_ICl,
24
+ unsigned int *_ECv, unsigned short *_ECo,
25
+ unsigned int *_ISOv,
26
+ float *_wmrSFP, float *_wmhSFP, float *_isoSFP,
27
+ unsigned char* _ICthreadsT, unsigned int* _ECthreadsT, unsigned int* _ISOthreadsT,
28
+ unsigned int _nIC, unsigned int _nEC, unsigned int _nISO, unsigned int _nThreads
29
+ ) nogil
30
+
31
+ cdef extern void COMMIT_A_nolut(
32
+ int _nF,
33
+ double *_v_in, double *_v_out,
34
+ unsigned int *_ICf, unsigned int *_ICv, float *_ICl,
35
+ unsigned int *_ISOv,
36
+ unsigned int* _ICthreads, unsigned int* _ISOthreads,
37
+ unsigned int _nISO, unsigned int _nThreads
38
+ ) nogil
39
+
40
+ cdef extern void COMMIT_At_nolut(
41
+ int _nF, int _n,
42
+ double *_v_in, double *_v_out,
43
+ unsigned int *_ICf, unsigned int *_ICv, float *_ICl,
44
+ unsigned int *_ISOv,
45
+ unsigned char* _ICthreadsT, unsigned int* _ISOthreadsT,
46
+ unsigned int _nISO, unsigned int _nThreads
47
+ ) nogil
48
+
49
+ logger = setup_logger('operator')
50
+
51
+ cdef class LinearOperator :
52
+ """This class is a wrapper to the C code for performing marix-vector multiplications
53
+ with the COMMIT linear operator A. The multiplications are done using C code
54
+ that uses information from the DICTIONARY, KERNELS and THREADS data structures.
55
+ """
56
+ cdef int nS, nF, nR, nE, nT, nV, nI, n, ndirs
57
+ cdef public int adjoint, n1, n2
58
+
59
+ cdef DICTIONARY
60
+ cdef KERNELS
61
+ cdef THREADS
62
+ cdef nolut
63
+
64
+ cdef unsigned int* ICf
65
+ cdef float* ICl
66
+ cdef unsigned int* ICv
67
+ cdef unsigned short* ICo
68
+ cdef unsigned int* ECv
69
+ cdef unsigned short* ECo
70
+ cdef unsigned int* ISOv
71
+
72
+ cdef float* LUT_IC
73
+ cdef float* LUT_EC
74
+ cdef float* LUT_ISO
75
+
76
+ cdef unsigned int* ICthreads
77
+ cdef unsigned int* ECthreads
78
+ cdef unsigned int* ISOthreads
79
+
80
+ cdef unsigned char* ICthreadsT
81
+ cdef unsigned int* ECthreadsT
82
+ cdef unsigned int* ISOthreadsT
83
+
84
+
85
+ def __init__( self, DICTIONARY, KERNELS, THREADS, nolut=False ) :
86
+ """Set the pointers to the data structures used by the C code."""
87
+ self.DICTIONARY = DICTIONARY
88
+ self.KERNELS = KERNELS
89
+ self.THREADS = THREADS
90
+ self.nolut = nolut
91
+
92
+ self.nF = DICTIONARY['IC']['nF'] # number of FIBERS
93
+ self.nR = KERNELS['wmr'].shape[0] # number of FIBER RADII
94
+ self.nE = DICTIONARY['EC']['nE'] # number of EC segments
95
+ self.nT = KERNELS['wmh'].shape[0] # number of EC TORTUOSITY values
96
+ self.nV = DICTIONARY['nV'] # number of VOXELS
97
+ self.nI = KERNELS['iso'].shape[0] # number of ISO contributions
98
+ self.n = DICTIONARY['IC']['n'] # numbner of IC segments
99
+ self.ndirs = KERNELS['wmr'].shape[1] # number of directions
100
+
101
+ if KERNELS['wmr'].size > 0 :
102
+ self.nS = KERNELS['wmr'].shape[2] # number of SAMPLES
103
+ elif KERNELS['wmh'].size > 0 :
104
+ self.nS = KERNELS['wmh'].shape[2]
105
+ else :
106
+ self.nS = KERNELS['wmr'].shape[1]
107
+
108
+ self.adjoint = 0 # direct of inverse product
109
+
110
+ self.n1 = self.nV*self.nS
111
+ self.n2 = self.nR*self.nF + self.nT*self.nE + self.nI*self.nV
112
+
113
+ # get C pointers to arrays in DICTIONARY
114
+ cdef unsigned int [::1] ICf = DICTIONARY['IC']['fiber']
115
+ self.ICf = &ICf[0]
116
+ cdef float [::1] ICl = DICTIONARY['IC']['len']
117
+ self.ICl = &ICl[0]
118
+ cdef unsigned int [::1] ICv = DICTIONARY['IC']['v']
119
+ self.ICv = &ICv[0]
120
+ cdef unsigned short [::1] ICo = DICTIONARY['IC']['o']
121
+ self.ICo = &ICo[0]
122
+ cdef unsigned int [::1] ECv = DICTIONARY['EC']['v']
123
+ self.ECv = &ECv[0]
124
+ cdef unsigned short [::1] ECo = DICTIONARY['EC']['o']
125
+ self.ECo = &ECo[0]
126
+ cdef unsigned int [::1] ISOv = DICTIONARY['ISO']['v']
127
+ self.ISOv = &ISOv[0]
128
+
129
+ # get C pointers to arrays in KERNELS
130
+ cdef float [:, :, ::1] wmrSFP = KERNELS['wmr']
131
+ self.LUT_IC = &wmrSFP[0,0,0]
132
+ cdef float [:, :, ::1] wmhSFP = KERNELS['wmh']
133
+ self.LUT_EC = &wmhSFP[0,0,0]
134
+ cdef float [:, ::1] isoSFP = KERNELS['iso']
135
+ self.LUT_ISO = &isoSFP[0,0]
136
+
137
+ # get C pointers to arrays in THREADS
138
+ cdef unsigned int [::1] ICthreads = THREADS['IC']
139
+ self.ICthreads = &ICthreads[0]
140
+ cdef unsigned int [::1] ECthreads = THREADS['EC']
141
+ self.ECthreads = &ECthreads[0]
142
+ cdef unsigned int [::1] ISOthreads = THREADS['ISO']
143
+ self.ISOthreads = &ISOthreads[0]
144
+
145
+ cdef unsigned char [::1] ICthreadsT = THREADS['ICt']
146
+ self.ICthreadsT = &ICthreadsT[0]
147
+ cdef unsigned int [::1] ECthreadsT = THREADS['ECt']
148
+ self.ECthreadsT = &ECthreadsT[0]
149
+ cdef unsigned int [::1] ISOthreadsT = THREADS['ISOt']
150
+ self.ISOthreadsT = &ISOthreadsT[0]
151
+
152
+
153
+ @property
154
+ def T( self ) :
155
+ """Transpose of the explicit matrix."""
156
+ C = LinearOperator( self.DICTIONARY, self.KERNELS, self.THREADS, self.nolut )
157
+ C.adjoint = 1 - C.adjoint
158
+ return C
159
+
160
+
161
+ @property
162
+ def shape( self ) :
163
+ """Size of the explicit matrix."""
164
+ if not self.adjoint :
165
+ return ( self.n1, self.n2 )
166
+ else :
167
+ return ( self.n2, self.n1 )
168
+
169
+
170
+ def dot( self, double [::1] v_in ):
171
+ """Wrapper to C code for efficiently performing the matrix-vector multiplications.
172
+
173
+ Parameters
174
+ ----------
175
+ v_in : 1D numpy.array of double
176
+ Input vector for the matrix-vector multiplication
177
+
178
+ Returns
179
+ -------
180
+ v_out : 1D numpy.array of double
181
+ Results of the multiplication
182
+ """
183
+
184
+ # Permit only matrix-vector multiplications
185
+ if v_in.size != self.shape[1] :
186
+ raise RuntimeError( "A.dot(): dimensions do not match" )
187
+
188
+ # Create output array
189
+ cdef double [::1] v_out = np.zeros( self.shape[0], dtype=np.float64 )
190
+
191
+ cdef unsigned int nthreads = self.THREADS['n']
192
+ cdef unsigned int nIC = self.KERNELS['wmr'].shape[0]
193
+ cdef unsigned int nEC = self.KERNELS['wmh'].shape[0]
194
+ cdef unsigned int nISO = self.KERNELS['iso'].shape[0]
195
+
196
+ # Call the cython function to read the memory pointers
197
+ if not self.adjoint :
198
+ # DIRECT PRODUCT A*x
199
+ if self.nolut:
200
+ with nogil:
201
+ COMMIT_A_nolut(
202
+ self.nF,
203
+ &v_in[0], &v_out[0],
204
+ self.ICf, self.ICv, self.ICl,
205
+ self.ISOv,
206
+ self.ICthreads, self.ISOthreads,
207
+ nISO, nthreads
208
+ )
209
+ else:
210
+ with nogil:
211
+ COMMIT_A(
212
+ self.nF, self.nE, self.nV, self.nS, self.ndirs,
213
+ &v_in[0], &v_out[0],
214
+ self.ICf, self.ICv, self.ICo, self.ICl,
215
+ self.ECv, self.ECo,
216
+ self.ISOv,
217
+ self.LUT_IC, self.LUT_EC, self.LUT_ISO,
218
+ self.ICthreads, self.ECthreads, self.ISOthreads,
219
+ nIC, nEC, nISO, nthreads
220
+ )
221
+ else :
222
+ # INVERSE PRODUCT A'*y
223
+ if self.nolut:
224
+ with nogil:
225
+ COMMIT_At_nolut(
226
+ self.nF, self.n,
227
+ &v_in[0], &v_out[0],
228
+ self.ICf, self.ICv, self.ICl,
229
+ self.ISOv,
230
+ self.ICthreadsT, self.ISOthreadsT,
231
+ nISO, nthreads
232
+ )
233
+ else:
234
+ with nogil:
235
+ COMMIT_At(
236
+ self.nF, self.n, self.nE, self.nV, self.nS, self.ndirs,
237
+ &v_in[0], &v_out[0],
238
+ self.ICf, self.ICv, self.ICo, self.ICl,
239
+ self.ECv, self.ECo,
240
+ self.ISOv,
241
+ self.LUT_IC, self.LUT_EC, self.LUT_ISO,
242
+ self.ICthreadsT, self.ECthreadsT, self.ISOthreadsT,
243
+ nIC, nEC, nISO, nthreads
244
+ )
245
+
246
+ return v_out