예제 #1
0
def test_split_epochs_rt_overlap():
    d = np.zeros([5, 5, 5, 8])
    for z in range(8):
        d[..., z] = z
    img = AslImage(name="asldata", image=d, tis=[1, 2], order='rt')
    imgs = img.split_epochs(4, overlap=2)
    assert len(imgs) == 3
    for idx, img in enumerate(imgs):
        if idx in (0, 2):
            assert img.ntis == 1
            assert img.tis == [1 + idx / 2]
            assert not img.have_plds
            assert img.rpts == [1]
            assert img.ntc == 1
            assert img.order == "rt"
            data = img.nibImage.get_data()
            # Epoch 1 is TIs 1111, data 0123, mean across repeats 1.5
            # Epoch 3 is TIs 2222, data 4567, mean across repeats 5.5
            start = idx * 2
            for z in range(data.shape[3]):
                assert np.all(data[..., z] == start + z + 1.5)
        else:
            assert img.ntis == 2
            assert img.tis == [1, 2]
            assert not img.have_plds
            assert img.rpts == [1, 1]
            assert img.ntc == 1
            assert img.order == "rt"
            data = img.nibImage.get_data()
            # Epoch 2 is TIs 1122, data 2345, mean across repeats 2.5, 4.5
            for z in range(data.shape[3]):
                assert np.all(data[..., z] == 2 * z + 2.5)
예제 #2
0
def qpdata_to_aslimage(qpd, options=None, metadata=None, grid=None):
    """ 
    Convert QpData to oxasl.AslImage using stored metadata where available 
    """

    # If metadata is not provided, get the existing metadata
    if metadata is None:
        metadata = qpd.metadata.get("AslData", {})

    # If options are provided, use them to override existing metadata
    if options:
        for opt in METADATA_ATTRS:
            val = options.pop(opt, None)
            if val is not None:
                metadata[opt] = val
            else:
                metadata.pop(opt, None)

    # Create AslImage object, this will fail if metadat is insufficient or inconsistent
    from oxasl import AslImage
    if grid is None:
        aslimage = AslImage(qpd.raw(),
                            name=qpd.name,
                            xform=qpd.grid.affine,
                            **metadata)
    else:
        aslimage = AslImage(qpd.resample(grid),
                            name=qpd.name,
                            xform=grid.affine,
                            **metadata)

    return aslimage, metadata
예제 #3
0
def test_multite_mean_across_repeats_ibf_rpt():
    d = np.zeros([5, 5, 5, 16])
    for z in range(16):
        d[..., z] = z
    img = AslImage(name="d",
                   image=d,
                   plds=[1, 2],
                   iaf="tc",
                   ibf="rpt",
                   tes=[8, 9],
                   casl=True,
                   bolus=[3, 4])
    imgmar = img.mean_across_repeats(diff=False)
    assert imgmar.ntes == 2
    assert imgmar.tes == [8, 9]
    assert imgmar.ntis == 2
    assert imgmar.have_plds
    assert imgmar.plds == [1, 2]
    assert imgmar.tis == [4, 6]
    assert imgmar.rpts == [1, 1]
    assert imgmar.ntc == 2
    assert imgmar.order == "eltr"
    assert imgmar.nvols == 8
    for z in range(8):
        # e.g. mean of 0th and 8th image = 4
        assert np.all(imgmar.data[..., z] == (z + 4))
예제 #4
0
def test_derived():
    d1 = np.random.rand(5, 5, 5, 8)
    d2 = np.random.rand(5, 5, 5, 8)
    img = AslImage(name="d1",
                   image=d1,
                   plds=[1, 2],
                   iaf="ct",
                   ibf="tis",
                   casl=True,
                   bolus=[3, 4])
    assert img.ntis == 2
    assert img.have_plds
    assert img.plds == [1, 2]
    assert img.tis == [4, 6]
    assert img.rpts == [2, 2]
    assert img.ntc == 2
    assert img.order == "lrt"

    img2 = img.derived(d2, name="d2")
    assert img2.ntis == 2
    assert img2.have_plds
    assert img2.plds == [1, 2]
    assert img2.tis == [4, 6]
    assert img2.rpts == [2, 2]
    assert img2.ntc == 2
    assert img2.order == "lrt"
예제 #5
0
def test_perf_weighted_tr():
    d = np.zeros([5, 5, 5, 8])
    for z in range(8):
        d[..., z] = z
    img = AslImage(name="asldata", image=d, tis=[1, 2], order='tr')
    pwi = img.perf_weighted()
    assert (isinstance(pwi, Image))
    data = pwi.data
    assert list(data.shape) == [5, 5, 5]
    assert np.all(np.mean(d, -1) == data)
예제 #6
0
def test_diff_ct():
    d = np.zeros([5, 5, 5, 8])
    for z in range(8):
        d[..., z] = z
    img = AslImage(name="asldata", image=d, tis=[1], iaf="ct", order='lrt')
    img = img.diff()
    assert img.ntis == 1
    assert img.tis == [1]
    assert not img.have_plds
    assert img.rpts == [4]
    assert img.ntc == 1
    assert img.order == "rt"
    data = img.nibImage.get_data()
    assert list(data.shape) == [5, 5, 5, 4]
    assert np.all(data == -1)
예제 #7
0
def test_t1im():
    """
    Check T1 image priors are correctly handled
    """
    d = np.random.rand(5, 5, 5, 6)
    img = AslImage(name="asldata", image=d, tis=[1.5], order="prt")
    wsp = Workspace(infertiss=True, inferbat=True, infert1=True)

    wsp.t1im = Image(np.random.rand(5, 5, 5))
    steps = basil.basil_steps(wsp, img)
    assert (len(steps) == 2)

    options = _get_defaults(img)
    _check_step(steps[0],
                desc_text="tissue",
                options=dict(options, **{
                    "inct1": True,
                }))

    _check_step(
        steps[1],
        desc_text="T1",
        options=dict(
            options,
            **{
                "inct1": True,
                "infert1": True,
                "PSP_byname1": "T_1",
                "PSP_byname1_type": "I",
                #"PSP_byname1_image" : "t1file",
            }))
예제 #8
0
def test_onestep():
    """
    Check that single step mode works when you would normally get multiple steps
    """
    d = np.random.rand(5, 5, 5, 6)
    img = AslImage(name="asldata", image=d, tis=[1.5], order="prt")
    wsp = Workspace(infertiss=True,
                    inferbat=True,
                    infertau=True,
                    inferart=True,
                    spatial=True,
                    onestep=True)

    steps = basil.basil_steps(wsp, img)
    assert (len(steps) == 1)

    options = _get_defaults(img)
    options.update({
        "method": "spatialvb",
        "param-spatial-priors": "N+",
        "PSP_byname1": "ftiss",
        "PSP_byname1_type": "M",
        "inctau": True,
        "incart": True,
        "inferart": True,
        "infertau": True,
        "convergence": "maxits",
    })
    options.pop("max-trials")
    _check_step(steps[0], desc_text="spatial", options=options)
예제 #9
0
def test_mean_across_repeats_tr():
    d = np.zeros([5, 5, 5, 8])
    for z in range(8):
        d[..., z] = z
    img = AslImage(name="asldata", image=d, tis=[1, 2], order='tr')
    img = img.mean_across_repeats()
    assert img.ntis == 2
    assert img.tis == [1, 2]
    assert not img.have_plds
    assert img.rpts == [1, 1]
    assert img.ntc == 1
    assert img.order == "tr"
    data = img.nibImage.get_data()
    assert list(data.shape) == [5, 5, 5, 2]
    for znew, zold in enumerate([3, 4]):
        assert np.all(data[..., znew] == zold)
예제 #10
0
def test_reorder_lrt_tlr():
    d = np.zeros([5, 5, 5, 8])
    for z in range(8):
        d[..., z] = z
    img = AslImage(name="asldata", image=d, tis=[1, 2], iaf="tc", order='lrt')
    img = img.reorder("tlr")
    assert img.ntis == 2
    assert img.tis == [1, 2]
    assert not img.have_plds
    assert img.rpts == [2, 2]
    assert img.ntc == 2
    assert img.order == "tlr"
    data = img.nibImage.get_data()
    assert list(data.shape) == [5, 5, 5, 8]
    for znew, zold in enumerate([0, 4, 1, 5, 2, 6, 3, 7]):
        assert np.all(data[..., znew] == zold)
예제 #11
0
def test_pvc():
    """
    FIXME we need to test the PVC initialization step
    and how to do this is not finalized
    """
    d = np.random.rand(5, 5, 5, 6)
    img = AslImage(name="asldata", image=d, tis=[1.5], order="prt")
    wsp = Workspace(infertiss=True, inferbat=True)

    wsp.pgm = Image(np.random.rand(5, 5, 5))
    wsp.pwm = Image(np.random.rand(5, 5, 5))

    steps = basil.basil_steps(wsp, img)
    assert (len(steps) == 3)

    options = _get_defaults(img)
    #    options.update({
    #        "incpve" : True,
    #    })
    _check_step(steps[0], desc_text="tissue", options=options)
    #    _check_step(steps[1], desc_text="PVE", options=options)

    options.update({
        "method": "spatialvb",
        "param-spatial-priors": "N+",
        "PSP_byname1": "ftiss",
        "PSP_byname1_type": "M",
        "max-iterations": 200,
        "convergence": "maxits",
    })
    options.pop("max-trials")
    _check_step(steps[2], desc_text="spatial")
예제 #12
0
def test_create_data_singleti():
    d = np.random.rand(5, 5, 5, 6)
    img = AslImage(name="asldata", image=d, tis=[1.5], iaf="tc", order="lrt")
    assert img.ntis == 1
    assert img.tis == [1.5]
    assert not img.have_plds
    assert img.rpts == [3]
    assert img.order == "lrt"
예제 #13
0
def test_mean_across_repeats_var_rpts():
    d = np.random.rand(5, 5, 5, 86)
    #for z in range(8): d[..., z] = z
    img = AslImage(name="asldata",
                   image=d,
                   tis=[1, 2, 3, 4, 5],
                   rpts=[6, 6, 6, 10, 15],
                   iaf="tc",
                   order='lrt')
    img = img.mean_across_repeats()
    assert img.ntis == 5
    assert img.tis == [1, 2, 3, 4, 5]
    assert img.rpts == [1, 1, 1, 1, 1]
    assert img.ntc == 1
    assert img.order == "rt"
    data = img.nibImage.get_data()
    assert list(data.shape) == [5, 5, 5, 5]
예제 #14
0
def test_create_data_numtis_multi():
    d = np.random.rand(5, 5, 5, 8)
    img = AslImage(name="asldata", image=d, ntis=2, iaf="tc", order="lrt")
    assert img.ntis == 2
    assert not img.tis
    assert not img.have_plds
    assert img.rpts == [2, 2]
    assert img.order == "lrt"
예제 #15
0
def test_reorder_lrt_ltr_var_rpts():
    """ 
    This reordering with variable repeats is not supported. In priciple
    it is possible (TI1_R1, TI2_R1, TI2_R2, TI2_R3) but this seems unlikely
    and is probably more likely an error
    """
    d = np.zeros([5, 5, 5, 8])
    for z in range(8):
        d[..., z] = z
    img = AslImage(name="asldata",
                   image=d,
                   tis=[1, 2],
                   rpts=[1, 3],
                   iaf="tc",
                   order='lrt')
    with pytest.raises(Exception):
        img.reorder("ltr")
예제 #16
0
def test_create_data_diff():
    d = np.random.rand(5, 5, 5, 8)
    img = AslImage(name="asldata", image=d, tis=[1.5], iaf="diff", order='rt')
    assert img.ntis == 1
    assert img.tis == [1.5]
    assert not img.have_plds
    assert img.rpts == [8]
    assert img.ntc == 1
    assert img.order == "rt"
예제 #17
0
def test_reorder_lrt_rtl():
    d = np.zeros([5, 5, 5, 8])
    for z in range(8):
        d[..., z] = z
    img = AslImage(name="asldata", image=d, tis=[1], iaf="tc", order='lrt')
    img = img.reorder("rtl")
    assert img.ntis == 1
    assert img.tis == [1]
    assert not img.have_plds
    assert img.rpts == [4]
    assert img.ntc == 2
    assert img.order == "rtl"
    data = img.nibImage.get_data()
    assert list(data.shape) == [5, 5, 5, 8]
    for z in range(8):
        if z < 4:
            assert np.all(data[..., z] == z * 2)
        else:
            assert np.all(data[..., z] == (z - 4) * 2 + 1)
예제 #18
0
def test_infer_nothing():
    """
    Check we get an error if there is nothing to infer
    """
    d = np.random.rand(5, 5, 5, 6)
    img = AslImage(name="asldata", image=d, tis=[1.5], order="prt")
    wsp = Workspace()

    with pytest.raises(ValueError):
        steps = basil.basil_steps(wsp, img)
예제 #19
0
def test_calib_first_vol_fail():
    """ Claim there is a calibration image as first vol but actually there isn't """
    d = np.random.rand(5, 5, 5, 8)
    with pytest.raises(Exception):
        img = AslImage(name="asldata",
                       image=d,
                       tis=[1.5, 2.0],
                       iaf="tc",
                       order="lrt",
                       calib_first_vol=True)
예제 #20
0
def test_reorder_tc_ct():
    d = np.zeros([5, 5, 5, 8])
    for z in range(8):
        d[..., z] = z
    img = AslImage(name="asldata", image=d, tis=[1], iaf="tc", order='lrt')
    img = img.reorder(iaf="ct")
    assert img.ntis == 1
    assert img.tis == [1]
    assert not img.have_plds
    assert img.rpts == [4]
    assert img.ntc == 2
    assert img.order == "lrt"
    assert img.iaf == "ct"
    data = img.nibImage.get_data()
    assert list(data.shape) == [5, 5, 5, 8]
    for z in range(8):
        if z % 2 == 0:
            assert np.all(data[..., z] == z + 1)
        else:
            assert np.all(data[..., z] == z - 1)
예제 #21
0
def test_pvc_only_one_map_given2():
    """
    Check that PVC correction fails if you only give the WM map
    """
    d = np.random.rand(5, 5, 5, 6)
    img = AslImage(name="asldata", image=d, tis=[1.5], order="prt")
    wsp = Workspace(infertiss=True, inferbat=True)

    wsp.pwm = Image(np.random.rand(5, 5, 5))
    with pytest.raises(ValueError):
        basil.basil_steps(wsp, img)
예제 #22
0
def test_split_epochs_rt():
    d = np.zeros([5, 5, 5, 8])
    for z in range(8):
        d[..., z] = z
    img = AslImage(name="asldata", image=d, tis=[1, 2], order='rt')
    imgs = img.split_epochs(4)
    assert len(imgs) == 2
    for idx, img in enumerate(imgs):
        assert img.ntis == 1
        assert img.tis == [1 + idx]
        assert not img.have_plds
        assert img.rpts == [1]
        assert img.ntc == 1
        assert img.order == "rt"
        data = img.nibImage.get_data()
        # Epoch 1 is TIs 1111, data 0123, mean across repeats 1.5
        # Epoch 2 is TIs 2222, data 4567, mean across repeats 5.5
        start = idx * 4
        for z in range(data.shape[3]):
            assert np.all(data[..., z] == start + z + 1.5)
예제 #23
0
def test_split_epochs_reorder():
    d = np.zeros([5, 5, 5, 8])
    for z in range(8):
        d[..., z] = z
    img = AslImage(name="asldata", image=d, tis=[1, 2], order='rt')
    imgs = img.split_epochs(4, time_order="tr")
    assert len(imgs) == 2
    for idx, img in enumerate(imgs):
        assert img.ntis == 2
        assert img.tis == [1, 2]
        assert not img.have_plds
        assert img.rpts == [1, 1]
        assert img.ntc == 1
        assert img.order == "tr"
        data = img.nibImage.get_data()
        # reordered = [0, 4, 1, 5, 2, 6, 3, 7]
        # Epoch 1 is TIs 1212, data 0415, mean across repeats 0.5, 4.5
        # Epoch 2 is TIs 1212, data 2637, mean across repeats 2.5, 6.5
        for z in range(data.shape[3]):
            assert np.all(data[..., z] == idx * 2 + 0.5 + z * 4)
예제 #24
0
def test_get_nativeref_calib():
    """
    Test brain extracted calibration image is used for differenced data
    """
    wsp = get_wsp()
    wsp.asldata = AslImage(np.random.rand(5, 5, 5, 4),
                           tis=[1, 2],
                           iaf="diff",
                           ibf="rpt")
    reg.get_nativeref(wsp)
    calib_brain = brain.brain(wsp, wsp.calib, thresh=0.2)
    assert (np.allclose(calib_brain.data, wsp.reg.nativeref.data))
예제 #25
0
def test_get_nativeref_asldata_mean_ct():
    """
    Test brain extracted ASL mean is used for CT data
    """
    wsp = get_wsp()
    wsp.asldata = AslImage(np.random.rand(5, 5, 5, 4),
                           tis=[1, 2],
                           iaf="ct",
                           ibf="rpt")
    reg.get_nativeref(wsp)
    meanasl_brain = brain.brain(wsp, wsp.asldata.mean(), thresh=0.2)
    assert (np.allclose(meanasl_brain.data, wsp.reg.nativeref.data))
예제 #26
0
def test_preproc_none():
    wsp = Workspace()
    d = np.random.rand(5, 5, 5, 6)
    wsp.asldata = AslImage(d, name="asldata", tis=[1.5], iaf="tc", order="lrt")

    preproc.preprocess(wsp)
    assert wsp.asldata.ntis == wsp.asldata_preproc.ntis
    assert wsp.asldata.tis == wsp.asldata_preproc.tis
    assert wsp.asldata.have_plds == wsp.asldata_preproc.have_plds
    assert wsp.asldata.rpts == wsp.asldata_preproc.rpts
    assert wsp.asldata.ntc == wsp.asldata_preproc.ntc
    assert wsp.asldata.order == wsp.asldata_preproc.order
예제 #27
0
def test_get_regfrom_asldata_mean_tc():
    """
    Test brain extracted ASL mean is used for TC data
    """
    wsp = get_wsp()
    wsp.asldata = AslImage(np.random.rand(5, 5, 5, 4),
                           tis=[1, 2],
                           iaf="tc",
                           ibf="rpt")
    reg.get_regfrom(wsp)
    meanasl_brain = brain.brain(wsp, wsp.asldata.mean(), thresh=0.2)
    assert (np.allclose(meanasl_brain.data, wsp.reg.regfrom.data))
예제 #28
0
def test_split_epochs_overlap():
    d = np.zeros([5, 5, 5, 8])
    for z in range(8):
        d[..., z] = z
    img = AslImage(name="asldata", image=d, tis=[1, 2], order='tr')
    imgs = img.split_epochs(4, overlap=2)
    assert len(imgs) == 3
    for idx, img in enumerate(imgs):
        assert img.ntis == 2
        assert img.tis == [1, 2]
        assert not img.have_plds
        assert img.rpts == [1, 1]
        assert img.ntc == 1
        assert img.order == "tr"
        data = img.nibImage.get_data()
        # Epoch 1 is TIs 1212, data 0123, mean across repeats 12
        # Epoch 2 is TIs 1212, data 2345, mean across repeats 34
        # Epoch 3 is TIs 1212, data 4567, mean across repeats 56
        start = idx * 2
        for z in range(data.shape[3]):
            assert np.all(data[..., z] == start + z + 1)
예제 #29
0
def test_pvc_no_tissue():
    """
    Check that PVC correction fails if you do not infer the tissue component
    """
    d = np.random.rand(5, 5, 5, 6)
    img = AslImage(name="asldata", image=d, tis=[1.5], order="prt")
    wsp = Workspace(infertiss=False, inferbat=True)

    wsp.pgm = Image(np.random.rand(5, 5, 5))
    wsp.pwm = Image(np.random.rand(5, 5, 5))

    with pytest.raises(ValueError):
        basil.basil_steps(wsp, img)
예제 #30
0
def test_create_data_multiti_var_repeats():
    d = np.random.rand(5, 5, 5, 8)
    img = AslImage(name="asldata",
                   image=d,
                   tis=[1.5, 2.0],
                   iaf="tc",
                   order="lrt",
                   rpts=[1, 3])
    assert img.ntis == 2
    assert img.tis == [1.5, 2.0]
    assert not img.have_plds
    assert img.rpts == [1, 3]
    assert img.order == "lrt"