Ejemplo n.º 1
0
 def test_simple_dictionary(self):
     expected = 'title:' + os.linesep + '  a  1' + os.linesep
     self.assertEqual(echo_arguments('title', {'a':1}), expected)
Ejemplo n.º 2
0
 def test_empty_dictionary(self):
     expected = 'title:'+os.linesep
     self.assertEqual(echo_arguments('title', {}), expected)
Ejemplo n.º 3
0
def standard(ct_file_name, rods_file_name, density_file_name, densities,
             csv_file_name):
    '''Determine the calibration equation for a standard phantom.'''
    click.echo(echo_arguments('Standard Calibration', locals()))

    # Read
    click.echo('Reading input {}'.format(ct_file_name))
    ct = sitk.ReadImage(ct_file_name)

    click.echo('Reading input {}'.format(rods_file_name))
    rods = sitk.ReadImage(rods_file_name)

    # Determine number of components
    click.echo('Determining mean intensities')
    filt = sitk.LabelStatisticsImageFilter()
    filt.Execute(ct, rods)

    n_labels = len(filt.GetLabels())
    click.echo('Found {} labels'.format(n_labels))

    if len(densities) < 2:
        click.fail('Need at least 2 samples to fit a function.'
                   ' Only found {}'.format(len(densities)))

    # Get the HU samples
    HU = []
    for label in filt.GetLabels():
        # Skip background label
        if label == 0:
            continue

        HU.append(filt.GetMean(label))

    click.echo('Fitting parameters:')
    click.echo('  Densities: {}'.format(densities))
    click.echo('  HU:        {}'.format(HU))

    # Perform calibration
    calibrator = StandardCalibration()
    calibrator.fit(HU, densities)

    click.echo('Found fit:')
    click.echo('  Slope:     {}'.format(calibrator.slope))
    click.echo('  Intercept: {}'.format(calibrator.intercept))
    click.echo('  R^2:       {}'.format(calibrator.r_value**2))

    header = [
        'CT File Name', 'Rods File Name', 'Density File Name', 'slope',
        'intercept', 'r_value', 'p_value', 'std_err'
    ]
    data = {
        'CT File Name': ct_file_name,
        'Rods File Name': rods_file_name,
        'Density File Name': density_file_name,
        'slope': round(calibrator.slope, 16),
        'intercept': round(calibrator.intercept, 16),
        'r_value': round(calibrator.r_value, 16),
        'p_value': round(calibrator.p_value, 16),
        'std_err': round(calibrator.std_err, 16)
    }

    click.echo('Writing complete results to {}'.format(csv_file_name))
    write_csv(data, csv_file_name, header)

    click.echo('Calibrating density file')
    density = calibrator.slope * sitk.Cast(ct, sitk.sitkFloat32) + \
        calibrator.intercept

    click.echo('Writing density file to {}'.format(density_file_name))
    sitk.ReadImage(density, density_file_name)
Ejemplo n.º 4
0
def resample(input_file_name, output_file_name,
             isotropic_resolution):
    '''Resample a CT image to an isotropic resolution'''
    click.echo(echo_arguments('Isotropic Resampling', locals()))

    # Read
    click.echo('Reading input {}'.format(input_file_name))
    image = sitk.ReadImage(input_file_name)

    # Determine sigma
    click.echo('Resampling to a resolution of {}'.format(isotropic_resolution))
    sigma = isotropic_resolution * np.sqrt(2 * np.log(2)) / np.pi
    click.echo('  For a half-power cut-off frequency of {:0.3f}, '
               'a standard deviation of {} is being used.'.format(
                  1/(2.0*isotropic_resolution), sigma))

    # Filter each sampling directions as needed
    for i, spacing in enumerate(image.GetSpacing()):
        if spacing > isotropic_resolution:
            click.echo('  No antialiasing in direction {}'.format(i))
            continue

        click.echo('  Antialiasing in direction {}'.format(i))
        image = sitk.RecursiveGaussian(
          image,
          sigma, False,
          sitk.RecursiveGaussianImageFilter.ZeroOrder,
          i
        )

    # Determine the output size
    resolution = [isotropic_resolution for i in range(image.GetDimension())]
    size = [int(np.ceil(s * i / o)) for o, i, s in
            zip(resolution, image.GetSpacing(), image.GetSize())]
    click.echo('  Input Size:  {}'.format(image.GetSize()))
    click.echo('  Output Size: {}'.format(size))

    click.echo('Finding minimum in image')
    stats = sitk.StatisticsImageFilter()
    stats.Execute(image)
    vox_min = stats.GetMinimum()
    click.echo('  Minimum intensity: {}'.format(vox_min))

    click.echo('Resampling...')
    transform = sitk.Euler3DTransform()
    transform.SetIdentity()

    click.echo('  Using BSpline interpolation')

    output = sitk.Resample(
      image,
      size,
      transform,
      sitk.sitkBSpline,
      image.GetOrigin(),
      resolution,
      image.GetDirection(),
      vox_min,
      image.GetPixelID()
    )

    click.echo('Writing output to file {}'.format(output_file_name))
    sitk.WriteImage(output, output_file_name)
Ejemplo n.º 5
0
def mindways(ct_file_name, rods_file_name, density_file_name, csv_file_name,
             water, densities):
    '''Determine the calibration equation for a Mindways phantom.

    Water and K2HPO4 densities are taken from the QC documentation
    accompanying your specific phantom.'''
    click.echo(echo_arguments('Mindways Calibration', locals()))

    # Read
    click.echo('Reading input {}'.format(ct_file_name))
    ct = sitk.ReadImage(ct_file_name)

    click.echo('Reading input {}'.format(rods_file_name))
    rods = sitk.ReadImage(rods_file_name)

    # Determine number of components
    click.echo('Determining mean intensities')
    filt = sitk.LabelStatisticsImageFilter()
    filt.Execute(ct, rods)

    n_labels = len(filt.GetLabels())
    click.echo('Found {} labels'.format(n_labels))

    if len(water) != len(densities) or len(water) != n_labels - 1:
        click.fail(
            'Number of water, K2HPO4, and segmented labels are not the same '
            '({}, {}, and {})'.format(len(water), len(densities),
                                      n_labels - 1))

    if len(water) < 2:
        click.fail('Need at least 2 samples to fit a function.'
                   ' Only found {}'.format(len(water)))

    # Get the HU samples
    HU = []
    for label in filt.GetLabels():
        # Skip background label
        if label == 0:
            continue

        HU.append(filt.GetMean(label))

    click.echo('Fitting parameters:')
    click.echo('  Water:  {}'.format(water))
    click.echo('  K2HPO4: {}'.format(densities))
    click.echo('  HU:     {}'.format(HU))

    # Perform calibration
    calibrator = MindwaysCalibration()
    calibrator.fit(HU, densities, water)

    click.echo('Found fit:')
    click.echo('  Slope:     {}'.format(calibrator.slope))
    click.echo('  Intercept: {}'.format(calibrator.intercept))
    click.echo('  R^2:       {}'.format(calibrator.r_value**2))

    header = [
        'CT File Name', 'Rods File Name', 'Density File Name', 'sigma_ref',
        'beta_ref', 'sigma_ct', 'beta_ct', 'slope', 'intercept', 'r_value',
        'p_value', 'std_err'
    ]
    data = {
        'CT File Name': ct_file_name,
        'Rods File Name': rods_file_name,
        'Density File Name': density_file_name,
        'sigma_ref': round(calibrator.sigma_ref, 16),
        'beta_ref': round(calibrator.beta_ref, 16),
        'sigma_ct': round(calibrator.sigma_ct, 16),
        'beta_ct': round(calibrator.beta_ct, 16),
        'slope': round(calibrator.slope, 16),
        'intercept': round(calibrator.intercept, 16),
        'r_value': round(calibrator.r_value, 16),
        'p_value': round(calibrator.p_value, 16),
        'std_err': round(calibrator.std_err, 16)
    }

    click.echo('Writing complete results to {}'.format(csv_file_name))
    write_csv(data, csv_file_name, header)

    click.echo('Calibrating density file')
    density = calibrator.slope * sitk.Cast(ct, sitk.sitkFloat32) + \
        calibrator.intercept

    click.echo('Writing density file to {}'.format(density_file_name))
    sitk.ReadImage(density, density_file_name)