Exemplo n.º 1
0
def recon_visit(visit, databin, shallow_recon=False):
    'shallow_recon will NOT check the individual files to see if they exist. Shallow Recon will stop if the top level folder exists'
    dcm2nii_object = dcm2nii(args_list=[
        '-a', 'N', '-d', 'N', '-e', 'N', '-g', 'N', '-i', 'N', '-n', 'Y', '-p',
        'N', '-m', 'N', '-r', 'N', '-v', 'Y', '-x', 'N'
    ])
    could_not_recon_list = []
    #remove garbage in quarantine
    for dicom in visit.dicomlist:
        dicom.series_folder_list.sort(key=lambda x: x.SeriesNumber
                                      )  # to ensure that 0 entry removed first
        folders_to_check = [
            x for x in dicom.series_folder_list
            if x.bad == False and x.manual_recon == False
        ]
        num_folders_to_check = len(folders_to_check)
        skip_check = False
        for series_folder in folders_to_check:
            series_info = databin.dicom_series_read.merged_series[
                visit.subject.subid][visit.visid][
                    series_folder.SeriesInstanceUID]
            dicom_series_type = series_info.DICOMseries_types.itervalues(
            ).next()
            folder_name = dicom_series_type.folder  #folder inside visit dir
            expected_image_quantity = dicom_series_type.expected_image_quantity  #number of niftis we expect
            if series_info.series == 'DTI_1' or series_info.series == 'DTI_2':
                folder_name = series_info.series
            if shallow_recon and folder_name in visit.niftis:  #i
                print "SHALLOW SKIPPING"
                continue
            if num_folders_to_check == 1:
                skip_check = True
            if databin.dicom_series_read.merged_series[visit.subject.subid][
                    visit.visid][
                        series_folder.
                        SeriesInstanceUID].Modality == 'PR':  #skip PR visits
                num_folders_to_check = num_folders_to_check - 1
                continue
            elif skip_check:
                check_recon_dicom_folder(series_folder, dcm2nii_object,
                                         folder_name, expected_image_quantity)

            if expected_image_quantity >= 0:  #not -1, the default
                generated_nifti_paths = gen_nifti_basename_fullpaths(
                    series_folder, folder_name, expected_image_quantity)
                if all([nifti_exists(x) for x in generated_nifti_paths]):
                    break
                else:
                    could_not_recon_list.append(
                        "subid:{0.visit.subject.subid} visid: {0.visit.visid} series: {0.series}"
                        .format(dicom))

    misc.set_permissions(visit.path)
    return could_not_recon_list
Exemplo n.º 2
0
def check_recon_dicom_folder(series_folder, dcm2nii_object, folder_name,
                             expected_image_quantity):
    reconstructed_image_quantity = -1
    number_paths_to_generate = expected_image_quantity
    if expected_image_quantity < 0:
        reconstructed_image_quantity = recon_into_quarantine(
            series_folder, dcm2nii_object)
        number_paths_to_generate = reconstructed_image_quantity
    generated_nifti_paths = gen_nifti_basename_fullpaths(
        series_folder, folder_name, number_paths_to_generate)

    #if all niftis exist, delete all files in quarantine and exit
    niftis_exist = [nifti_exists(x) for x in generated_nifti_paths]
    if all(niftis_exist):
        visit = series_folder.image.visit
        quar_dir = os.path.join(visit.path, 'Quarantine')
        if os.path.exists(quar_dir):
            shutil.rmtree(quar_dir)
            os.mkdir(quar_dir)
        return

    #reconstruct into nifti if we havn't already
    elif reconstructed_image_quantity < 0:  #not reconstructed into quarantine before
        reconstructed_image_quantity = recon_into_quarantine(
            series_folder, dcm2nii_object)

    #check that number of reconstructed images matches number expected
    if expected_image_quantity > 0 and expected_image_quantity != reconstructed_image_quantity:
        visit = series_folder.image.visit
        quar_dir = os.path.join(visit.path, 'Quarantine')
        reconed_files = os.listdir(quar_dir)
        reconstructed_niftis = [x for x in reconed_files if is_nifti(x)]
        quar_dir = os.path.join(visit.path, 'Quarantine')
        raise Wrong_Number_Of_Files_Error(
            quar_dir,
            "Unexpected number niftis produced by reconstruction. Expected:{0} Reconstructed:{1}\nReconstructedPaths\n{2}\nExpectedBasenames\n{3}"
            .format(expected_image_quantity, reconstructed_image_quantity,
                    '\n'.join(reconstructed_niftis),
                    '\n'.join(generated_nifti_paths)))

    move_niftis(series_folder, folder_name, generated_nifti_paths)
    move_non_niftis(series_folder, folder_name)
Exemplo n.º 3
0
def check_recon_dicom_folder(series_folder, dcm2nii_object, folder_name, expected_image_quantity):
    reconstructed_image_quantity = -1
    number_paths_to_generate = expected_image_quantity
    if expected_image_quantity < 0:
        reconstructed_image_quantity = recon_into_quarantine(series_folder, dcm2nii_object)
        number_paths_to_generate = reconstructed_image_quantity
    generated_nifti_paths = gen_nifti_basename_fullpaths(series_folder, folder_name, number_paths_to_generate)

    #if all niftis exist, delete all files in quarantine and exit
    niftis_exist = [nifti_exists(x) for x in generated_nifti_paths]
    if all(niftis_exist):
        visit = series_folder.image.visit
        quar_dir = os.path.join(visit.path, 'Quarantine')
        if os.path.exists(quar_dir):
            shutil.rmtree(quar_dir)
            os.mkdir(quar_dir)
        return

    #reconstruct into nifti if we havn't already
    elif reconstructed_image_quantity < 0: #not reconstructed into quarantine before
        reconstructed_image_quantity = recon_into_quarantine(series_folder, dcm2nii_object)

    #check that number of reconstructed images matches number expected
    if expected_image_quantity > 0 and expected_image_quantity != reconstructed_image_quantity:
        visit = series_folder.image.visit
        quar_dir = os.path.join(visit.path, 'Quarantine')
        reconed_files = os.listdir(quar_dir)
        reconstructed_niftis = [x for x in reconed_files if is_nifti(x)]
        quar_dir = os.path.join(visit.path, 'Quarantine')
        raise Wrong_Number_Of_Files_Error(quar_dir, "Unexpected number niftis produced by reconstruction. Expected:{0} Reconstructed:{1}\nReconstructedPaths\n{2}\nExpectedBasenames\n{3}".format(
                        expected_image_quantity,
                        reconstructed_image_quantity,
                        '\n'.join(reconstructed_niftis),
                        '\n'.join(generated_nifti_paths)
                        ))

    move_niftis(series_folder, folder_name, generated_nifti_paths)
    move_non_niftis(series_folder, folder_name)
Exemplo n.º 4
0
def recon_visit(visit, databin, shallow_recon=False):
    'shallow_recon will NOT check the individual files to see if they exist. Shallow Recon will stop if the top level folder exists'
    dcm2nii_object = dcm2nii(args_list=['-a', 'N', '-d', 'N', '-e', 'N', '-g', 'N', '-i', 'N', '-n', 'Y', '-p', 'N', '-m', 'N', '-r', 'N', '-v', 'Y', '-x', 'N'])
    could_not_recon_list = []
    #remove garbage in quarantine
    for dicom in visit.dicomlist:
        dicom.series_folder_list.sort(key=lambda x: x.SeriesNumber) # to ensure that 0 entry removed first
        folders_to_check = [x for x in dicom.series_folder_list if x.bad == False and x.manual_recon == False]
        num_folders_to_check = len(folders_to_check)
        skip_check = False
        for series_folder in folders_to_check:
            series_info = databin.dicom_series_read.merged_series[visit.subject.subid][visit.visid][series_folder.SeriesInstanceUID]
            dicom_series_type = series_info.DICOMseries_types.itervalues().next()
            folder_name = dicom_series_type.folder #folder inside visit dir
            expected_image_quantity = dicom_series_type.expected_image_quantity #number of niftis we expect
            if series_info.series == 'DTI_1' or series_info.series == 'DTI_2':
                folder_name = series_info.series
            if shallow_recon and folder_name in visit.niftis:  #i
                print "SHALLOW SKIPPING"
                continue
            if num_folders_to_check == 1:
                skip_check = True
            if databin.dicom_series_read.merged_series[visit.subject.subid][visit.visid][series_folder.SeriesInstanceUID].Modality == 'PR': #skip PR visits
                num_folders_to_check = num_folders_to_check - 1
                continue
            elif skip_check:
                check_recon_dicom_folder(series_folder, dcm2nii_object, folder_name, expected_image_quantity)
            
            if expected_image_quantity >= 0: #not -1, the default
                generated_nifti_paths = gen_nifti_basename_fullpaths(series_folder, folder_name, expected_image_quantity)
                if all([nifti_exists(x) for x in generated_nifti_paths]):
                    break
                else:
                    could_not_recon_list.append("subid:{0.visit.subject.subid} visid: {0.visit.visid} series: {0.series}".format(dicom))
    
    misc.set_permissions(visit.path)
    return could_not_recon_list