Check for pixel spacing between two folder images Im trying to match the pixel spacing between two folders, here matches has 994 keys and it shows that i may take more than a day to compute this. Is there any way i can speed up this operation.

the load series function looks like this 

import SimpleITK as sitk
def load_series(path):
    reader = sitk.ImageSeriesReader()
    dicom_names = reader.GetGDCMSeriesFileNames(path)
    reader.SetFileNames(dicom_names)
    return reader.Execute()

import csv
bar = ProgressBar()
file = open('BenignTest.csv', 'w')
writer = csv.writer(file)
for item in bar(matches.keys()):
    for case in matches[item]:
        load_case = load_series(case)
        if load_case.GetSpacing() == load_series(item).GetSpacing():
            writer.writerow((item , case))
            break


The matches dictionary looks like this, 

{'/mnt/sdd1/DSB2017/stage1/0030a160d58723ff36d73f41b170ec21': ['/mnt/sde1/Cancer/128443/01-02-1999-LSS-59120/2-0OPAGELSPLUSD3602.512060.00.11.5-98506',
  '/mnt/sde1/Cancer/213485/01-02-1999-ACRIN-13247/3-0OPAGELSPLUSLUNG3502.51204026.71.5-85097',
  '/mnt/sde1/Cancer/206342/01-02-1999-ACRIN-68321/3-0OPAGELSPLUSLUNG3702.51205033.31.5-72233',
  '/mnt/sde1/Cancer/200209/01-02-2000-CRIN-10216/3-1OPAGELSPLUSLUNG4002.51204026.71.5-42354']...........}


Thanks in advance
The simplest improvement that you can do right now is to avoid recomputing the same reference image over and over for each case of an item: just store load_series(item).GetSpacing() before looping over each case.

You can also use writer.writerows and feed it a list or a generator instead of writer.writerow. This should optimize disk writes a bit, even though reading images should be the task that take up the most resources here.

Proposed improvements:

import csv
import SimpleITK as sitk


def load_series(path):
    reader = sitk.ImageSeriesReader()
    dicom_names = reader.GetGDCMSeriesFileNames(path)
    reader.SetFileNames(dicom_names)
    return reader.Execute()


def generate_equal_spacing(matches, progress_bar=None):
    items = matches.keys()
    if progress_bar is not None:
        items = progress_bar(items)

    for item in items:
        reference_spacing = load_series(item).GetSpacing()
        for case in matches[item]:
            if load_series(case).GetSpacing() == reference_spacing:
                yield (item, case)
                break


def main(matches, output_filename='BenignTest.csv'):
    with open(output_filename, 'w') as f:
        writer = csv.writer(f)
        writer.writerows(generate_equal_spacing(matches, ProgressBar()))


if __name__ == '__main__':
    main({
        '/mnt/sdd1/DSB2017/stage1/0030a160d58723ff36d73f41b170ec21': [
            '/mnt/sde1/Cancer/128443/01-02-1999-LSS-59120/2-0OPAGELSPLUSD3602.512060.00.11.5-98506',
            '/mnt/sde1/Cancer/213485/01-02-1999-ACRIN-13247/3-0OPAGELSPLUSLUNG3502.51204026.71.5-85097',
            '/mnt/sde1/Cancer/206342/01-02-1999-ACRIN-68321/3-0OPAGELSPLUSLUNG3702.51205033.31.5-72233',
            '/mnt/sde1/Cancer/200209/01-02-2000-CRIN-10216/3-1OPAGELSPLUSLUNG4002.51204026.71.5-42354',
        ],
        ...
    })