2020from determineNshm import verifyNshmForAll , determineNshm
2121from util import *
2222from fileUtil import read_caselist , check_dir , check_csv
23- from conversion import read_imgs_masks
2423
2524N_CPU = psutil .cpu_count ()
2625SCRIPTDIR = dirname (__file__ )
2726
2827
29- def printStat (ref_mean , csvFile ):
30-
31- print ('mean FA over IIT_mean_FA_skeleton.nii.gz for all cases: ' )
32- imgs , _ = read_imgs_masks (csvFile )
33- for i , imgPath in enumerate (imgs ):
34- print (basename (imgPath ), ref_mean [i ])
35-
36- print ('' )
37- print ('mean meanFA: ' , np .mean (ref_mean ))
38- print ('std meanFA: ' , np .std (ref_mean ))
39- print ('' )
40-
41-
42-
4328class pipeline (cli .Application ):
4429
4530 """
@@ -62,19 +47,19 @@ class pipeline(cli.Application):
6247 ref_csv = cli .SwitchAttr (
6348 ['--ref_list' ],
6449 cli .ExistingFile ,
65- help = 'reference csv/txt file with first column for dwi and 2nd column for mask: dwi1,mask1\n dwi2 ,mask2\n ...' ,
50+ help = 'reference csv/txt file with first column for dwi and 2nd column for mask: dwi1,mask1\\ n dwi2 ,mask2\ \ n...' ,
6651 mandatory = False )
6752
6853 target_csv = cli .SwitchAttr (
6954 ['--tar_list' ],
7055 cli .ExistingFile ,
71- help = 'target csv/txt file with first column for dwi and 2nd column for mask: dwi1,mask1\n dwi2 ,mask2\n ...' ,
56+ help = 'target csv/txt file with first column for dwi and 2nd column for mask: dwi1,mask1\\ n dwi2 ,mask2\n ...' ,
7257 mandatory = False )
7358
7459 harm_csv = cli .SwitchAttr (
7560 ['--harm_list' ],
7661 cli .ExistingFile ,
77- help = 'harmonized csv/txt file with first column for dwi and 2nd column for mask: dwi1,mask1\n dwi2 ,mask2\n ...' ,
62+ help = 'harmonized csv/txt file with first column for dwi and 2nd column for mask: dwi1,mask1\\ n dwi2 ,mask2\n ...' ,
7863 mandatory = False )
7964
8065 templatePath = cli .SwitchAttr (
@@ -145,7 +130,7 @@ class pipeline(cli.Application):
145130 reference = cli .SwitchAttr (
146131 '--ref_name' ,
147132 help = 'reference site name' ,
148- mandatory = True )
133+ mandatory = False )
149134
150135 target = cli .SwitchAttr (
151136 '--tar_name' ,
@@ -263,6 +248,7 @@ def createTemplate(self):
263248 def harmonizeData (self ):
264249
265250 from reconstSignal import reconst
251+ from preprocess import dti_harm
266252
267253 # check the templatePath
268254 if not exists (self .templatePath ):
@@ -324,7 +310,7 @@ def harmonizeData(self):
324310 pool .close ()
325311 pool .join ()
326312
327-
313+
328314 # loop for debugging
329315 # res= []
330316 # for imgPath, maskPath in zip(imgs, masks):
@@ -341,6 +327,17 @@ def harmonizeData(self):
341327 if preFlag :
342328 fm .close ()
343329 fh .close ()
330+
331+
332+ if self .debug :
333+ harmImgs , harmMasks = read_caselist (self .harm_csv )
334+ pool = multiprocessing .Pool (self .N_proc )
335+ for imgPath ,maskPath in zip (harmImgs ,harmMasks ):
336+ pool .apply_async (func = dti_harm , args = (imgPath ,maskPath ))
337+ pool .close ()
338+ pool .join ()
339+
340+
344341 print ('\n \n Harmonization completed\n \n ' )
345342
346343
@@ -365,42 +362,56 @@ def showStat(self):
365362
366363 from debug_fa import analyzeStat
367364 from datetime import datetime
365+ from harm_plot import generate_csv , harm_plot
366+ import pandas as pd
368367
369- print ('Printing statistics :\n \n ' )
370-
371- # save statistics for future
372- statFile = pjoin (self .templatePath , 'meanFAstat.txt' )
373- f = open (statFile ,'a' )
374- stdout = sys .stdout
375- sys .stdout = f
376-
377- print (datetime .now ().strftime ('%c' ),'\n ' )
378-
379- print ('b-shell' , self .bshell_b , '\n ' )
368+ print ('\n \n Computing statistics\n \n ' )
380369
381370 print (f'{ self .reference } site: ' )
382371 ref_mean = analyzeStat (self .ref_csv , self .templatePath )
383- printStat ( ref_mean , self .ref_csv )
372+ generate_csv ( self . ref_csv , ref_mean , pjoin ( self .templatePath , self . reference ), self . bshell_b )
384373
385374 print (f'{ self .target } site before harmonization: ' )
386- target_mean_before = analyzeStat (self .tar_unproc_csv , self .templatePath )
387- printStat ( target_mean_before , self .tar_unproc_csv )
375+ target_mean_before = analyzeStat (self .tar_unproc_csv , self .templatePath )
376+ generate_csv ( self . tar_unproc_csv , target_mean_before , pjoin ( self .templatePath , self . target ) + '_before' , self . bshell_b )
388377
389378 print (f'{ self .target } site after harmonization: ' )
390- target_mean_after = analyzeStat (self .harm_csv , self .templatePath )
391- printStat (target_mean_after , self .harm_csv )
379+ target_mean_after = analyzeStat (self .harm_csv , self .templatePath )
380+ generate_csv (self .harm_csv , target_mean_after , pjoin (self .templatePath , self .target )+ '_after' , self .bshell_b )
381+
382+
383+ print ('\n \n Printing statistics\n \n ' )
384+ # save statistics for future
385+ statFile = pjoin (self .templatePath , 'meanFAstat.csv' )
386+ if isfile (statFile ):
387+ df = pd .read_csv (statFile )
388+ else :
389+ timestamp = datetime .now ().strftime ('%m/%d/%y %H:%M' )
390+ sites = [f'{ self .reference } ' ,f'{ self .target } _before' ,f'{ self .target } _after' ]
391+ df = pd .DataFrame ({timestamp :sites })
392+
393+ header = f'mean meanFA b{ self .bshell_b } '
394+ value = [np .mean (x ) for x in [ref_mean , target_mean_before , target_mean_after ]]
395+ df = df .assign (** {header :value })
396+
397+ # print an empty line so future results, if appended, are visually separate
398+ # df=df.append(pd.Series(),ignore_index=True)
399+
400+ df .to_csv (statFile , index = False )
392401
393- print ('\n \n ' )
394-
395- f .close ()
396- sys .stdout = stdout
397-
398402 # print statistics on console
399403 with open (statFile ) as f :
400- print (f .read ())
404+ print (f .read ())
401405
402- print ('\n The statistics are also saved in ' , statFile )
403-
406+ # generate graph
407+ ebar = harm_plot ([ref_mean , target_mean_before , target_mean_after ],
408+ labels = [self .reference , self .target + '_before' , self .target + '_after' ],
409+ outPrefix = pjoin (self .templatePath ,'meanFAstat' ), bshell_b = self .bshell_b )
410+
411+ print (f'\n Detailed statistics, summary results, and demonstrative plots are saved in:\n \n { self .templatePath } /*_stat.csv'
412+ f'\n { statFile } \n { ebar } \n ' )
413+
414+
404415
405416
406417 def sanityCheck (self ):
@@ -447,9 +458,9 @@ def main(self):
447458 # determine N_shm in default mode during template creation
448459 if self .N_shm == - 1 and self .create :
449460 if self .ref_csv :
450- ref_nshm_img = read_imgs_masks (self .ref_csv )[0 ][0 ]
461+ ref_nshm_img = read_caselist (self .ref_csv )[0 ][0 ]
451462 elif self .target_csv :
452- ref_nshm_img = read_imgs_masks (self .target_csv )[0 ][0 ]
463+ ref_nshm_img = read_caselist (self .target_csv )[0 ][0 ]
453464
454465 directory = dirname (ref_nshm_img )
455466 prefix = basename (ref_nshm_img ).split ('.nii' )[0 ]
0 commit comments