Beispiel #1
0
def test_main(rec=False, verb=False, throw=True):

    pet.MessageRedirector()

    for scheme in ("file", "memory"):
        pet.AcquisitionData.set_storage_scheme(scheme)

        original_verb = pet.get_verbosity()
        pet.set_verbosity(False)
        # create an acq_model that is explicitly a RayTracingMatrix
        am = pet.AcquisitionModelUsingRayTracingMatrix()
        # load sample data
        data_path = pet.examples_data_path('PET')
        raw_data_file = pet.existing_filepath(data_path,
                                              'Utahscat600k_ca_seg4.hs')
        ad = pet.AcquisitionData(raw_data_file)
        # create sample image
        image = pet.ImageData()
        image.initialise(dim=(31, 111, 111), vsize=(2.25, 2.25, 2.25))
        # set up Acquisition Model
        am.set_up(ad, image)
        # test for adjointnesss
        if not is_operator_adjoint(am, verbose=verb):
            raise AssertionError(
                'AcquisitionModelUsingRayTracingMatrix is not adjoint')

    # Reset original verbose-ness
    pet.set_verbosity(original_verb)
    return 0, 1
Beispiel #2
0
def test_main(rec=False, verb=False, throw=True):
    datafile = RE_PYEXT.sub(".txt", __file__)
    test = pTest(datafile, rec, throw=throw)
    test.verbose = verb

    data_path = examples_data_path('MR')
    AcquisitionData.set_storage_scheme('memory')
    input_data = AcquisitionData(
            data_path + '/simulated_MR_2D_cartesian_Grappa2.h5')
    test.check(input_data.norm())

    prep_gadgets = ['RemoveROOversamplingGadget']
    processed_data = input_data.process(prep_gadgets)
    test.check(processed_data.norm())

    recon = CartesianGRAPPAReconstructor()
    recon.compute_gfactors(False)
    recon.set_input(processed_data)
    recon.process()
    complex_images = recon.get_output()
    test.check(complex_images.norm())

    csms = CoilSensitivityData()

    processed_data.sort()
    csms.calculate(processed_data)

    am = AcquisitionModel(processed_data, complex_images)
    am.set_coil_sensitivity_maps(csms)

    fwd_acqs = am.forward(complex_images)
    fwd_acqs_norm = fwd_acqs.norm()
    test.check(fwd_acqs_norm, rel_tol = 1e-2)

    acqs_diff = fwd_acqs - processed_data
    rr = acqs_diff.norm()/fwd_acqs_norm
    test.check(rr, abs_tol = 1e-4)

    bwd_images = am.backward(processed_data)
    imgs_diff = bwd_images - complex_images
    rd = imgs_diff.norm()/complex_images.norm()
    test.check(rd, abs_tol = 1e-4)

    if not is_operator_adjoint(am, max_err = 1e-3):
      raise AssertionError("Gadgetron operator is not adjoint")

    return test.failed, test.ntest
Beispiel #3
0
def test_main(rec=False, verb=False, throw=True):

    # Set STIR verbosity to off
    original_verb = pet.get_verbosity()
    pet.set_verbosity(1)

    time.sleep(0.5)
    sys.stderr.write("Testing NiftyPET projector...")
    time.sleep(0.5)

    # Get image
    image = get_image()

    # Get AM
    try:
        acq_model = pet.AcquisitionModelUsingNiftyPET()
    except:
        return 1, 1
    acq_model.set_cuda_verbosity(verb)

    data_path = examples_data_path('PET')
    # raw_data_path = pet.existing_filepath(os.path.join(data_path, 'mMR'), 'mMR_template_span11.hs')
    raw_data_path = os.path.join(data_path, 'mMR')
    template_acq_data = pet.AcquisitionData(
        os.path.join(raw_data_path, 'mMR_template_span11.hs'))

    acq_model.set_up(template_acq_data, image)

    # Test operator adjointness
    if verb:
        print('testing adjointness')
    if not is_operator_adjoint(acq_model, num_tests=1, verbose=True):
        raise AssertionError('NiftyPet AcquisitionModel is not adjoint')

    # Generate test data
    simulated_acq_data = acq_model.forward(image)
    simulated_acq_data_w_noise = add_noise(simulated_acq_data, 10)

    obj_fun = pet.make_Poisson_loglikelihood(template_acq_data)
    obj_fun.set_acquisition_model(acq_model)

    recon = pet.OSMAPOSLReconstructor()
    recon.set_objective_function(obj_fun)
    recon.set_num_subsets(1)
    recon.set_num_subiterations(1)
    recon.set_input(simulated_acq_data_w_noise)
    if verb:
        print('setting up, please wait...')
    initial_estimate = image.get_uniform_copy()
    recon.set_up(initial_estimate)

    if verb:
        print('reconstructing...')
    recon.set_current_estimate(initial_estimate)
    recon.process()
    reconstructed_im = recon.get_output()
    if not reconstructed_im:
        raise AssertionError()

    # Reset original verbose-ness
    pet.set_verbosity(original_verb)

    return 0, 1
Beispiel #4
0
def try_niftymomo(na):
    time.sleep(0.5)
    sys.stderr.write(
        '\n# --------------------------------------------------------------------------------- #\n'
    )
    sys.stderr.write(
        '#                             Starting NiftyMomMo test...\n')
    sys.stderr.write(
        '# --------------------------------------------------------------------------------- #\n'
    )
    time.sleep(0.5)

    # The forward and the adjoint should meet the following criterion:
    # | < x, Ty > - < y, Tsx > | / 0.5 * (| < x, Ty > | + | < y, Tsx > |) < epsilon
    # for all images x and y, where T is the transform and Ts is the adjoint.

    x = ref_aladin
    T = na.get_transformation_matrix_forward()
    y = flo_aladin

    # Add in a magnification to make things interesting
    t = T.as_array()
    t[0][0] = 1.5
    T = sirf.Reg.AffineTransformation(t)

    # make it slightly unsquare to spice things up
    min_idx = [0, 1, 2]
    y_dims = y.get_dimensions()
    max_idx = [y_dims[1] - 3, y_dims[2] - 1, y_dims[3] - 5]
    y.crop(min_idx, max_idx)

    sys.stderr.write('Testing adjoint resample...\n')
    nr = sirf.Reg.NiftyResampler()
    nr.set_reference_image(x)
    nr.set_floating_image(y)
    nr.set_interpolation_type_to_linear()
    nr.add_transformation(T)

    # Check the adjoint is truly the adjoint with: |<x, Ty> - <y, Tsx>| / 0.5*(|<x, Ty>|+|<y, Tsx>|) < epsilon
    if not is_operator_adjoint(nr):
        raise AssertionError("NiftyResampler::adjoint() failed")

    # Check that the following give the same result
    #       out = resample.adjoint(in)
    #       resample.adjoint(out, in)
    out1 = nr.adjoint(x)
    out2 = y.deep_copy()
    nr.backward(out=out2, x=x)
    if out1 != out2:
        raise AssertionError(
            'out = NiftyResampler::adjoint(in) and NiftyResampler::adjoint(out, in) do not give same result.'
        )

    time.sleep(0.5)
    sys.stderr.write(
        '\n# --------------------------------------------------------------------------------- #\n'
    )
    sys.stderr.write(
        '#                             Finished NiftyMoMo test.\n')
    sys.stderr.write(
        '# --------------------------------------------------------------------------------- #\n'
    )
    time.sleep(0.5)