@@ -337,12 +337,16 @@ def _create_session_series_names(dataset):
337337
338338 return ses_id , ser_id
339339
340- def _uncompress_dicom_session (dic_dir : str , subj_ids = None ):
340+ def _uncompress_dicom_session (dic_dir : str ,
341+ boolrmtar : bool = False ,
342+ subj_ids = None ):
341343 """
342344 Uncompress session folders
343345 @params:
344346 dic_dir - Required : Directory containing the subjects. It assumes an organization in:
345347 <subj_id>/<session_id>/<series_id>(Str)
348+ boolrmtar - Optional : Boolean variable to remove the tar files after uncompressing the session. Default is False.
349+ subj_ids - Optional : List of subject IDs to be considered. If not provided, it will consider all the subjects in the directory.
346350 """
347351
348352 if subj_ids is None :
@@ -362,48 +366,43 @@ def _uncompress_dicom_session(dic_dir: str, subj_ids=None):
362366 elif not isinstance (subj_ids , list ):
363367 raise ValueError ("The subj_ids parameter must be a list or a string" )
364368
369+ n_subj = len (subj_ids )
365370 # Failed sessions
366371 fail_sess = []
367-
368- # Loop around all the subjects
369- nsubj = len (subj_ids )
370- for i , subj_id in enumerate (subj_ids ): # Loop along the IDs
371- subj_dir = os .path .join (dic_dir , subj_id )
372-
373- cltmisc ._printprogressbar (
374- i + 1 ,
375- nsubj ,
376- "Processing subject "
377- + subj_id
378- + ": "
379- + "("
380- + str (i + 1 )
381- + "/"
382- + str (nsubj )
383- + ")" ,
384- )
385-
386- # Loop along all the sessions inside the subject directory
387- for ses_tar in glob (
388- subj_dir + os .path .sep + "*.tar.gz"
389- ): # Loop along the session
390- # print('SubjectId: ' + subjId + ' ======> Session: ' + sesId)
391- # Compress only if it is a folder
392- if os .path .isfile (ses_tar ):
393- try :
394- # Compressing the folder
395- subprocess .run (
396- ["tar" , "xzf" , ses_tar , "-C" , subj_dir ],
397- stdout = subprocess .PIPE ,
398- universal_newlines = True ,
399- )
400-
401- # Removing the uncompressed dicom folder
402- # subprocess.run(
403- # ['rm', '-r', ses_tar], stdout=subprocess.PIPE, universal_newlines=True)
404-
405- except :
406- fail_sess .append (ses_tar )
372+ with Progress () as pb :
373+ t1 = pb .add_task ('[green]Compressing subjects...' , total = n_subj )
374+
375+
376+ # Loop around all the subjects
377+ n_subj = len (subj_ids )
378+ for i , subj_id in enumerate (subj_ids ): # Loop along the IDs
379+ subj_dir = os .path .join (dic_dir , subj_id )
380+ pb .update (task_id = t1 , description = f'[green]Uncompressing sessions for { subj_id } ({ i + 1 } /{ n_subj } )' , completed = i + 1 )
381+
382+ # Loop along all the sessions inside the subject directory
383+ for ses_tar in glob (
384+ subj_dir + os .path .sep + "*.tar.gz"
385+ ): # Loop along the session
386+ # print('SubjectId: ' + subjId + ' ======> Session: ' + sesId)
387+ # Compress only if it is a folder
388+ if os .path .isfile (ses_tar ):
389+ try :
390+ # Compressing the folder
391+ subprocess .run (
392+ ["tar" , "xzf" , ses_tar , "-C" , subj_dir ],
393+ stdout = subprocess .PIPE ,
394+ universal_newlines = True ,
395+ )
396+
397+ # Removing the uncompressed dicom folder
398+ if boolrmtar :
399+ subprocess .run (
400+ ['rm' , '-r' , ses_tar ], stdout = subprocess .PIPE , universal_newlines = True )
401+ except :
402+ fail_sess .append (ses_tar )
403+
404+ pb .update (task_id = t1 , description = f'[green]Compressing sessions for { subj_id } ({ n_subj } /{ n_subj } )' , completed = n_subj )
405+
407406 if fail_sess :
408407 print ("THE PROCESS FAILED TO UNCOMPRESS THE FOLLOWING TAR FILES:" )
409408 for i in fail_sess :
@@ -437,51 +436,53 @@ def _compress_dicom_session(dic_dir: str, subj_ids=None):
437436 elif not isinstance (subj_ids , list ):
438437 raise ValueError ("The subj_ids parameter must be a list or a string" )
439438
439+ n_subj = len (subj_ids )
440440 # Failed sessions
441441 fail_sess = []
442-
443- # Loop around all the subjects
444- nsubj = len (subj_ids )
445- for i , subj_id in enumerate (subj_ids ): # Loop along the IDs
446- subj_dir = os .path .join (dic_dir , subj_id )
447-
448- cltmisc ._printprogressbar (
449- i + 1 ,
450- nsubj ,
451- "Processing subject "
452- + subj_id
453- + ": "
454- + "("
455- + str (i + 1 )
456- + "/"
457- + str (nsubj )
458- + ")" ,
459- )
460-
461- # Loop along all the sessions inside the subject directory
462- for ses_id in os .listdir (subj_dir ): # Loop along the session
463- ses_dir = os .path .join (subj_dir , ses_id )
464- # print('SubjectId: ' + subjId + ' ======> Session: ' + sesId)
465- # Compress only if it is a folder
466- if os .path .isdir (ses_dir ):
467- tar_filename = ses_dir + ".tar.gz"
468- try :
469- # Compressing the folder
470- subprocess .run (
471- ["tar" , "-C" , subj_dir , "-czvf" , tar_filename , ses_id ],
472- stdout = subprocess .PIPE ,
473- universal_newlines = True ,
474- )
475-
476- # Removing the uncompressed dicom folder
477- subprocess .run (
478- ["rm" , "-r" , ses_dir ],
479- stdout = subprocess .PIPE ,
480- universal_newlines = True ,
481- )
482-
483- except :
484- fail_sess .append (ses_dir )
442+ with Progress () as pb :
443+ t1 = pb .add_task ('[green]Compressing subjects...' , total = n_subj )
444+
445+ # Loop around all the subjects
446+ nsubj = len (subj_ids )
447+ for i , subj_id in enumerate (subj_ids ): # Loop along the IDs
448+ subj_dir = os .path .join (dic_dir , subj_id )
449+ pb .update (task_id = t1 , description = f'[green]Compressing sessions for { subj_id } ({ i + 1 } /{ n_subj } )' , completed = i + 1 )
450+
451+ # Loop along all the sessions inside the subject directory
452+ ses_dirs = os .listdir (subj_dir )
453+
454+ # Detect which of the folders are sessions
455+ ses_dirs = [x for x in ses_dirs if os .path .isdir (os .path .join (subj_dir , x ))]
456+
457+ # Detect which of the folders start with 'ses-'
458+ ses_dirs = [x for x in ses_dirs if x .startswith ('ses-' )]
459+ n_sessions = len (ses_dirs )
460+
461+ for n_ses , ses_id in enumerate (ses_dirs ): # Loop along the session
462+ ses_dir = os .path .join (subj_dir , ses_id )
463+ # print('SubjectId: ' + subjId + ' ======> Session: ' + sesId)
464+ # Compress only if it is a folder
465+ if os .path .isdir (ses_dir ):
466+ tar_filename = ses_dir + ".tar.gz"
467+ try :
468+ # Compressing the folder
469+ subprocess .run (
470+ ["tar" , "-C" , subj_dir , "-czvf" , tar_filename , ses_id ],
471+ stdout = subprocess .PIPE ,
472+ universal_newlines = True ,
473+ )
474+
475+ # Removing the uncompressed dicom folder
476+ subprocess .run (
477+ ["rm" , "-r" , ses_dir ],
478+ stdout = subprocess .PIPE ,
479+ universal_newlines = True ,
480+ )
481+ except :
482+ fail_sess .append (ses_dir )
483+
484+ pb .update (task_id = t1 , description = f'[green]Compressing sessions for { subj_id } ({ n_subj } /{ n_subj } )' , completed = n_subj )
485+
485486 if fail_sess :
486487 print ("THE PROCESS FAILED TO COMPRESS THE FOLLOWING SESSIONS:" )
487488 for i in fail_sess :
0 commit comments