def convert_dicom_to_raw_nii(scan_folder_name): ret = data_customs.convert_dicom_to_nifti( os.path.join(rootconfig.dms.folder_temp, 'DICOM'), os.path.join(rootconfig.dms.folder_temp, 'NII')) if ret == 0: logging.info('{} Successfully converted {}'.format( clock.now(), scan_folder_name)) else: logging.error('{} Error converting scan {} with return code {}'.format( clock.now(), scan_folder_name, ret)) with open(os.path.join(rootconfig.dms.folder_temp, 'NII', 'scan_info.json')) as f: jsonObject = json.load(f) print('Subject ID: %s' % jsonObject['Patient']['ID']) try: # shutil.copytree(os.path.join(rootconfig.dms.folder_temp, 'NII'), os.path.join(rootconfig.dms.folder_rawnii, scan_folder_name)) os.system('robocopy /S "{}" "{}"'.format( os.path.join(rootconfig.dms.folder_temp, 'NII'), os.path.join(rootconfig.dms.folder_rawnii, scan_folder_name))) # remove temp folder os.system('rmdir /S /Q "{}"'.format(rootconfig.dms.folder_temp)) except FileExistsError as e: logging.error('{} Destination already exists.\n{}'.format( clock.now(), e)) exit() except PermissionError as e: print('PermissionError') print(e) logging.error('{} PermissionError.\n{}'.format(clock.now(), e)) exit() logging.info('{} raw nii moved to NAS and temp folder removed'.format( clock.now()))
def extract_modalities(scan_folder_name): worker = data_customs.MRIScanImporter( os.path.join(rootconfig.dms.folder_rawnii), os.path.join(rootconfig.dms.folder_mridata), cls_niftigetter=data_customs.ChanggungNiftiGetter) logging.info('{} Extracting modalities...'.format(clock.now())) modalities_coverage = worker.copy_one_nifti(scan_folder_name) logging.info('{} Extracting modalities finished with coverage: {}'.format( clock.now(), modalities_coverage)) return modalities_coverage
def compress_dicom(scan_folder_name): """ compress local dicom folder as a zip and move to NAS """ logging.info('{} Compressing...'.format(clock.now())) shutil.make_archive( os.path.join(rootconfig.dms.folder_temp, scan_folder_name), 'zip', os.path.join(rootconfig.dms.folder_temp, 'DICOM')) os.system('robocopy "{}" "{}" {}'.format( rootconfig.dms.folder_temp, rootconfig.dms.folder_dicom, '{}.zip'.format(scan_folder_name))) logging.info('{} Compressed'.format(clock.now()))
def copy_dicom_from_CD(): """ copy dicom from CD to local temp folder dcm2niix conversion is faster on local """ try: # shutil.make_archive(os.path.join(rootconfig.dms.folder_dicom, scan_folder_name), 'zip', rootconfig.dms.CD_dicom_path) # shutil.copytree(rootconfig.dms.CD_dicom_path, os.path.join(rootconfig.dms.folder_temp, 'DICOM')) os.system('robocopy /S "{}" "{}"'.format( rootconfig.dms.CD_dicom_path, os.path.join(rootconfig.dms.folder_temp, 'DICOM'))) except FileExistsError as e: logging.error('{} Destination already exists.\n{}'.format( clock.now(), e)) exit() logging.info('{} Dicom copied.'.format(clock.now()))
def main(): parser = argparse.ArgumentParser() parser.add_argument('--name', help='name of the subject.', required=True, default='') parser.add_argument('--zh', help='name_zh of the subject.', required=True, default='') parser.add_argument('--date', help='scanning date', required=True, default='') args = parser.parse_args() subject_name = args.name name_zh = args.zh scan_date = args.date scan_folder_name = '%s_%s' % (subject_name, scan_date) logging.basicConfig(filename='import_changgung.log', level=logging.DEBUG) logging.info('{} New Run {}'.format(clock.now(), scan_folder_name)) copy_dicom_from_CD() compress_dicom(scan_folder_name) convert_dicom_to_raw_nii(scan_folder_name) modalities_coverage = extract_modalities(scan_folder_name) update_sql_database(scan_folder_name, modalities_coverage) update_PMAR_database(scan_folder_name, name_zh)
def run1(f, argvec, processes=None): """Run function f len(argvec) times, each time use one arg in argvec.""" processes = get_processes(processes) with multiprocessing.Pool(processes) as p: m = multiprocessing.Manager() q = m.Queue() fwrap = FWrap(f, q) result = p.map_async(fwrap.run, argvec) ntotal = len(argvec) print('Begin proc, {} cpus, {} left, start at {}'.format( processes, ntotal, clock.now())) nfinished = 0 while True: if result.ready(): break else: res = q.get() nfinished += 1 print('{} just finished. {} left, at {}'.format( res, ntotal - nfinished, clock.now())) print('End proc, end at {}'.format(clock.now())) outputs = result.get() return outputs
rets = [] ret = self.work_attr(mriscan) rets.extend(ret) #ret = self.work_net(mriscan) #rets.extend(ret) return rets def flattenlist(ll): ls = [] for l in ll: ls.extend(l) return ls if __name__ == '__main__': atlasobj = atlas.getbyenv('aal') fu = fusion.create_by_folder(atlasobj, 'E:/MMDPSoftware/mmdps/pipeline/Fusion') mriscans = fu.groups['entire'].mriscans foreach_scan = MRIScanReport(fu, mriscans) funcslist = foreach_scan.run() funcs = flattenlist(funcslist) print(len(funcs)) print(clock.now()) parabase.run_callfunc(funcs) ## for func in funcs: ## func() print(clock.now())
def run(f, argvec, processes=None): """Run function f len(argvec) times, each time use one arg in argvec.""" processes = get_processes(processes) estimated_task_time_cost = -1 with multiprocessing.Pool(processes) as pool: manager = multiprocessing.Manager() managerQueue = manager.Queue() fwrap = FWrap(f, managerQueue) result = pool.map_async(fwrap.run, argvec) ntotal = len(argvec) nError = 0 errorList = [] print('Begin proc, {} cpus, {} left, start at {}'.format( processes, ntotal, clock.now())) nfinished = 0 while True: if result.ready(): break else: try: ret = managerQueue.get(timeout=1) except queue.Empty: continue else: nfinished += 1 elapsed_time = time.time() - ret['start_time'] if estimated_task_time_cost < 0: estimated_task_time_cost = elapsed_time else: estimated_task_time_cost = 0.75 * estimated_task_time_cost + 0.25 * elapsed_time res = ret['message'] retCode = int(res[res.find('res: ') + 5:]) if retCode != 0: nError += 1 errorList.append(res) print( '{} just finished with error after {:1.2f} s execution. {} left, at {}. Estimated time left: {} (HMS)' .format( res, elapsed_time, ntotal - nfinished, clock.now(), str( datetime.timedelta( seconds=(ntotal - nfinished) * estimated_task_time_cost)))) else: print( '{} just finished after {:1.2f} s execution. {} left, at {}. Estimated time left: {} (HMS)' .format( res, elapsed_time, ntotal - nfinished, clock.now(), str( datetime.timedelta( seconds=(ntotal - nfinished) * estimated_task_time_cost)))) print('End proc, end at {}. {} error.'.format(clock.now(), nError)) if nError != 0: print('Listing errs') for err in errorList: print(err) outputs = result.get() # print(outputs) # a list of return codes, should be all zero return outputs
def genlogfilename(info=''): """Generate a log file name base on current time and supplied info.""" path.makedirs('log') timestr = clock.now() return 'log/log_{}_{}.txt'.format(timestr, info)
import os import argparse import logging from mmdps.dms import converter, importer, dbgen from mmdps.util.loadsave import load_txt from mmdps import rootconfig from mmdps.util import clock logging.basicConfig(filename='import_changgung.log', level=logging.DEBUG) logging.info('New Run: {}'.format(clock.now())) def apppath(s): return os.path.join(WORKING_FOLDER, s) class ChanggungNiftiGetter(converter.NiftiGetter): def __init__(self, niftifolder): super().__init__(niftifolder) def get_T1(self): return self.fnmatch_one('*OSag_3D_T1BRAVO*.nii.gz') def get_T2(self): return self.fnmatch_one('*OAx_T2_PROPELLER*.nii.gz') def get_BOLD(self): return self.fnmatch_one('*BOLD-rest*.nii.gz') def get_DWI(self): nii = self.fnmatch_one('*DTI_24_Directions*.nii.gz')