当前位置: 首页>>代码示例>>Python>>正文


Python XnatUtils.clean_directory方法代码示例

本文整理汇总了Python中dax.XnatUtils.clean_directory方法的典型用法代码示例。如果您正苦于以下问题:Python XnatUtils.clean_directory方法的具体用法?Python XnatUtils.clean_directory怎么用?Python XnatUtils.clean_directory使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在dax.XnatUtils的用法示例。


在下文中一共展示了XnatUtils.clean_directory方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: zip_resource

# 需要导入模块: from dax import XnatUtils [as 别名]
# 或者: from dax.XnatUtils import clean_directory [as 别名]
def zip_resource(res_obj, directory, resource, no_delete=False, no_big=False):
    """
    Zip the files in the resource.

    :param res_obj: resource Eobject from pyxnat
    :param directory: directory to save temp files
    :param resource: resource label
    :param no_delete: do not delete the files zipped
    :param no_big: do not zip big resources
    """
    _warn = '    - warning: %s'
    fzip = '%s.zip' % resource
    if len(res_obj.files().get()) > 1:
        print("   --> downloading %s ..." % resource)
        fpaths = XnatUtils.download_files_from_obj(directory, res_obj)
        if not fpaths:
            msg = '%s -- no files.' % resource
            print(_warn % msg)
        else:
            # If the resource.zip file exists, exit
            if res_obj.file(fzip).exists():
                msg = '%s -- zip file already on XNAT but zipped files not \
deleted.' % resource
                print(_warn % msg)

            else:
                # Zip the resource if more than one
                print('   --> zipping files.')
                resource_dir = os.path.dirname(fpaths[0])
                # Get init directory
                init_dir = os.getcwd()
                # Zip all the files in the directory
                os.chdir(resource_dir)
                os.system('zip -r %s * > /dev/null' % fzip)
                # return to the initial directory:
                os.chdir(init_dir)
                # upload
                _fzip = os.path.join(resource_dir, fzip)

                if os.path.exists(_fzip):
                    # Check the size:
                    size_file = int(os.stat(_fzip).st_size) / (1024 * 1024)
                    if size_file >= LIMIT_SIZE:
                        msg = '%s too big.' % resource
                        print(_warn % msg)
                    if no_big and size_file >= LIMIT_SIZE:
                        msg = '%s too big. Skipping.' % resource
                        print(_warn % msg)
                        return

                    print('   --> uploading zip file')
                    res_obj.put_zip(_fzip, overwrite=True, extract=False)

            if no_delete:
                print('   --> not deleting original files(two copies).')
            else:
                print('   --> deleting original files and keeping zipfile.')
                delete_resources(res_obj, fzip)

    # clean tmp folder
    XnatUtils.clean_directory(directory)
开发者ID:byvernault,项目名称:scripts,代码行数:63,代码来源:zip_resource.py

示例2: convert_DICOM

# 需要导入模块: from dax import XnatUtils [as 别名]
# 或者: from dax.XnatUtils import clean_directory [as 别名]
def convert_DICOM():
    """Loop through the project scans to convert DICOM to NIFTI."""
    list_scans = XnatUtils.list_project_scans(XNAT, OPTIONS.project)
    # filter the list to keep scans with DICOM and no NIFTI
    if not OPTIONS.force:
        print "Filtering list of scans to keep scans with DICOM but no NIFTI."
        list_scans = filter(
         lambda x: 'DICOM' in x['resources'] and 'NIFTI' not in x['resources'],
         list_scans)
    else:
        print "Filtering list of scans to keep scans with DICOM."
        list_scans = filter(lambda x: 'DICOM' in x['resources'], list_scans)
    # if sessions, filter:
    if OPTIONS.sessions:
        list_scans = filter(
            lambda x: x['session_label'] in OPTIONS.sessions.split(','),
            list_scans)
    number_scans = len(list_scans)
    print "Converting the %s scans found." % (number_scans)
    for index, scan in enumerate(sorted(list_scans,
                                        key=lambda k: k['session_label'])):
        message = ' * {index}/{total} -- Session: {session} -- Scan: {scan}'
        print message.format(index=index+1,
                             total=number_scans,
                             session=scan['session_label'],
                             scan=scan['ID'])
        need_to_zip = False
        scan_obj = XnatUtils.get_full_object(XNAT, scan)
        if scan_obj.exists() and \
           len(scan_obj.resource('DICOM').files().get()) > 0:
            print "   --> downloading DICOM ..."
            fpaths = XnatUtils.download_files_from_obj(
                        OPTIONS.directory,
                        scan_obj.resource("DICOM"))
            if not fpaths:
                print '    - warning: DICOM -- no files.'
            else:
                if OPTIONS.force and scan_obj.resource('NIFTI').exists():
                    scan_obj.resource('NIFTI').delete()

                dcm_dir = os.path.join(OPTIONS.directory, 'DICOM')
                if len(fpaths) > 1:
                    need_to_zip = True

                if len(fpaths) == 1 and fpaths[0].endswith('.zip'):
                    if not os.path.exists(dcm_dir):
                        os.makedirs(dcm_dir)
                    os.system('unzip -d %s -j %s > /dev/null' % (dcm_dir,
                                                                 fpaths[0]))
                    os.remove(fpaths[0])
                dicom_files = get_dicom_list(dcm_dir)

                if dicom_files:
                    # Check for duplicate dicoms:
                    if OPTIONS.check_dcm:
                        check_duplicate_slices_dicom(dicom_files)
                    # convert dcm to nii
                    conversion_status = dcm2nii(dicom_files[0])

                    if not conversion_status:
                        # Convert dcm via dcmdjpeg
                        dcmdjpeg(dcm_dir)
                        # try again dcm2nii
                        dcm_fpath = os.path.join(dcm_dir, 'final_1.dcm')
                        conversion_status = dcm2nii(dcm_fpath)

                    # Check if Nifti created:
                    nii_li = [f for f in os.listdir(dcm_dir)
                              if f.endswith('.nii.gz') or f.endswith('.nii')]
                    if not nii_li:
                        print "    - warning: dcm to nii failed with \
conversion dcmjpeg. no upload."
                    else:
                        # UPLOADING THE RESULTS
                        upload_converted_images(dicom_files, dcm_dir, scan_obj,
                                                need_to_zip)

                    # clean tmp folder
                    XnatUtils.clean_directory(OPTIONS.directory)
                else:
                    print "    - ERROR : no proper DICOM files \
found from the resource on XNAT. "

        else:
            print "    - ERROR : issue with resource DICOM: \
开发者ID:byvernault,项目名称:scripts,代码行数:87,代码来源:dcm2nii_xnat.py

示例3: run

# 需要导入模块: from dax import XnatUtils [as 别名]
# 或者: from dax.XnatUtils import clean_directory [as 别名]
    def run(self, scan_info, scan_obj):
        """run function to convert dicom to parrec to nifti and upload data."""
        # clean tmp folder
        XnatUtils.clean_directory(self.directory)

        if not len(scan_obj.resource('DICOM').files().get()) > 0:
            LOGGER.debug('no DICOM files')
        elif scan_info['type'] in AVOID_SCANTYPES:
            LOGGER.info('avoid this scan type: {}'.format(scan_info['type']))
        else:
            LOGGER.debug('downloading all DICOMs...')

            self.dicom_paths = XnatUtils.download_files_from_obj(
                self.directory, scan_obj.resource('DICOM'))

            if not self.dicom_paths:
                msg = """dcm2nii -- %s -- No proper DICOM found in \
    resource DICOM on XNAT"""
                LOGGER.error(msg % scan_info['scan_id'])
                msg = 'No proper DICOM found in resource DICOM on XNAT'
                self.log_warning_error(msg, scan_info, error=True)
            else:
                # convert dcm to nii
                dcm_dir = os.path.dirname(self.dicom_paths[0])

                # ZIP the DICOM if more than one
                if len(self.dicom_paths) > 1 and self.zip_dicoms:
                    self.zipping_dicoms(scan_obj, dcm_dir)

                # if only one DICOM and it's a zip, unzip
                if len(self.dicom_paths) == 1 and \
                   self.dicom_paths[0].endswith('.zip'):
                    dcm_dir = os.path.dirname(self.dicom_paths[0])
                    os.system('unzip -d %s -j %s > /dev/null'
                              % (dcm_dir, self.dicom_paths[0]))
                    os.remove(self.dicom_paths[0])
                    self.dicom_paths = self.get_dicom_list(dcm_dir)

                if not self.dcm2nii(self.dicom_paths[0]):
                    # Convert dcm via dcmdjpeg
                    dicom_paths_djpeg = self.dcmdjpeg()
                    # try again dcm2nii
                    self.dcm2nii(dicom_paths_djpeg[0])
                    dcm_dir = os.path.dirname(dicom_paths_djpeg[0])

                # Check if Nifti created:
                nifti_list = [
                    f for f in os.listdir(dcm_dir)
                    if f.endswith('.nii.gz') or f.endswith('.nii')]
                if not nifti_list:
                    msg = "dcm2nii -- %s -- DCM --> NII ( preprocess \
dicom with dcmdjpeg ) conversion failure"
                    LOGGER.warn(msg % scan_info['scan_id'])
                    msg = 'Fail to convert DICOM to NIFTI '
                    self.log_warning_error(msg, scan_info)
                else:
                    # UPLOADING THE RESULTS
                    self.upload_converted_images(
                        dcm_dir, scan_obj, scan_info)

            # clean tmp folder
            LOGGER.debug('clean temp directory...')
            XnatUtils.clean_directory(self.directory)
开发者ID:byvernault,项目名称:ucl_processing,代码行数:65,代码来源:Module_dcm2nii.py


注:本文中的dax.XnatUtils.clean_directory方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。