@@ -163,13 +163,13 @@ def createTemplate(self):
163163 # createTemplate steps -----------------------------------------------------------------------------------------
164164
165165 # read image lists
166- refImgs , refMasks = common_processing (self .ref_csv )
166+ refImgs , refMasks = common_processing (self .ref_unproc_csv )
167167 if not self .ref_csv .endswith ('.modified' ):
168168 self .ref_csv += '.modified'
169169 # debug: use the following line to omit processing again
170170 # refImgs, refMasks = read_caselist(self.ref_csv)
171171
172- targetImgs , targetMasks = common_processing (self .target_csv )
172+ targetImgs , targetMasks = common_processing (self .tar_unproc_csv )
173173 if not self .target_csv .endswith ('.modified' ):
174174 self .target_csv += '.modified'
175175 # debug: use the following line to omit processing again
@@ -248,8 +248,8 @@ def createTemplate(self):
248248
249249 def harmonizeData (self ):
250250
251- from reconstSignal import reconst
252- from preprocess import dti_harm
251+ from reconstSignal import reconst , approx
252+ from preprocess import dti_harm , preprocessing , common_processing
253253
254254 # check the templatePath
255255 if not exists (self .templatePath ):
@@ -258,49 +258,59 @@ def harmonizeData(self):
258258 if not listdir (self .templatePath ):
259259 raise ValueError (f'{ self .templatePath } is empty' )
260260
261+
262+ # fit spherical harmonics on reference site
263+ if self .debug and self .ref_csv :
264+ check_csv (self .ref_unproc_csv , self .force )
265+ refImgs , refMasks = read_caselist (self .ref_unproc_csv )
266+
267+ # reference data is not manipulated in multi-shell-dMRIharmonization i.e. bvalMapped, resampled, nor denoised
268+ # this block may be uncommented in a future design
269+ # res= []
270+ # pool = multiprocessing.Pool(self.N_proc)
271+ # for imgPath, maskPath in zip(refImgs, refMasks):
272+ # res.append(pool.apply_async(func=preprocessing, args=(imgPath, maskPath)))
273+ #
274+ # attributes = [r.get() for r in res]
275+ #
276+ # pool.close()
277+ # pool.join()
278+ #
279+ # for i in range(len(refImgs)):
280+ # refImgs[i] = attributes[i][0]
281+ # refMasks[i] = attributes[i][1]
282+
283+ pool = multiprocessing .Pool (self .N_proc )
284+ for imgPath , maskPath in zip (refImgs , refMasks ):
285+ pool .apply_async (func = approx , args = (imgPath ,maskPath ,))
286+
287+ pool .close ()
288+ pool .join ()
289+
290+
291+
261292 # go through each file listed in csv, check their existence, create dti and harm directories
262293 check_csv (self .target_csv , self .force )
263-
264- # target data is not manipulated in multi-shell-dMRIharmonization i.e. bvalMapped, resampled, nor denoised
265- # this block may be uncommented in a future design
266- # from preprocess import dti_harm
267- # if self.debug:
268- # # calcuate diffusion measures of target site before any processing so we are able to compare
269- # # with the ones after harmonization
270- # imgs, masks= read_caselist(self.tar_unproc_csv)
271- # pool = multiprocessing.Pool(self.N_proc)
272- # for imgPath, maskPath in zip(imgs, masks):
273- # imgPath= convertedPath(imgPath)
274- # maskPath= convertedPath(maskPath)
275- # pool.apply_async(func= dti_harm, args= ((imgPath, maskPath, )))
276- #
277- # pool.close()
278- # pool.join()
294+ targetImgs , targetMasks = common_processing (self .tar_unproc_csv )
279295
280296 # reconstSignal steps ------------------------------------------------------------------------------------------
281297
282298 # read target image list
283299 moving = pjoin (self .templatePath , f'Mean_{ self .target } _FA_b{ self .bshell_b } .nii.gz' )
284- imgs , masks = read_caselist (self .tar_unproc_csv )
285300
286- fm = None
287301 if not self .target_csv .endswith ('.modified' ):
288302 self .target_csv += '.modified'
289- fm = open (self .target_csv , 'w' )
290303
291304
292305 self .harm_csv = self .target_csv + '.harmonized'
293306 fh = open (self .harm_csv , 'w' )
294307 pool = multiprocessing .Pool (self .N_proc )
295308 res = []
296- for imgPath , maskPath in zip (imgs , masks ):
309+ for imgPath , maskPath in zip (targetImgs , targetMasks ):
297310 res .append (pool .apply_async (func = reconst , args = (imgPath , maskPath , moving , self .templatePath ,)))
298311
299312 for r in res :
300- imgPath , maskPath , harmImg , harmMask = r .get ()
301-
302- if isinstance (fm , io .IOBase ):
303- fm .write (imgPath + ',' + maskPath + '\n ' )
313+ harmImg , harmMask = r .get ()
304314 fh .write (harmImg + ',' + harmMask + '\n ' )
305315
306316
@@ -314,23 +324,18 @@ def harmonizeData(self):
314324 # res.append(reconst(imgPath, maskPath, moving, self.templatePath))
315325 #
316326 # for r in res:
317- # imgPath, maskPath, harmImg, harmMask= r
318- #
319- # if isinstance(fm, io.IOBase):
320- # fm.write(imgPath + ',' + maskPath + '\n')
327+ # harmImg, harmMask= r
321328 # fh.write(harmImg + ',' + harmMask + '\n')
322329
323330
324- if isinstance (fm , io .IOBase ):
325- fm .close ()
326331 fh .close ()
327332
328333
329334 if self .debug :
330335 harmImgs , harmMasks = read_caselist (self .harm_csv )
331336 pool = multiprocessing .Pool (self .N_proc )
332337 for imgPath ,maskPath in zip (harmImgs ,harmMasks ):
333- pool .apply_async (func = dti_harm , args = (imgPath ,maskPath ))
338+ pool .apply_async (func = dti_harm , args = (imgPath ,maskPath , ))
334339 pool .close ()
335340 pool .join ()
336341
@@ -440,10 +445,10 @@ def main(self):
440445 if self .N_proc == - 1 :
441446 self .N_proc = N_CPU
442447
443- if self . target_csv . endswith ( '.modified' ):
444- self .tar_unproc_csv = str ( self . target_csv ). split ( '.modified' )[ 0 ]
445- else :
446- self .tar_unproc_csv = str ( self .target_csv )
448+
449+ if self .ref_csv :
450+ self . ref_unproc_csv = self . ref_csv . strip ( '.modified' )
451+ self .tar_unproc_csv = self .target_csv . strip ( '.modified' )
447452
448453
449454 # check appropriateness of N_shm
@@ -525,3 +530,4 @@ def main(self):
525530if __name__ == '__main__' :
526531 pipeline .run ()
527532
533+
0 commit comments