def test_close():
    """
    check if close method works as intended
    """
    dir_path = "./data/test/nifti/paired/test"
    name = "fixed_images"

    loader = NiftiFileLoader(dir_path=dir_path, name=name, grouped=False)
    loader.close()
Exemple #2
0
def test_close():
    """
    check if close method works as intended
    close is the same code for all cases, so no need to test all cases
    """
    # paired
    dir_paths = ["./data/test/nifti/paired/test"]
    name = "fixed_images"
    loader = NiftiFileLoader(dir_paths=dir_paths, name=name, grouped=False)
    loader.close()
def test_init_sufficient_args():
    """
    check if init method of loader returns any errors when all required
    arguments given
    """
    dir_path = "./data/test/nifti/paired/test"
    name = "fixed_images"

    loader = NiftiFileLoader(dir_path=dir_path, name=name, grouped=False)
    loader.__init__(dir_path=dir_path, name=name, grouped=False)
    loader.close()
def test_get_num_images():
    """
    check if the get_num_images method works as expected
    """
    dir_path = "./data/test/nifti/paired/test"
    name = "fixed_images"

    loader = NiftiFileLoader(dir_path=dir_path, name=name, grouped=False)
    got = int(loader.get_num_images())
    expected = int(2)
    loader.close()
    assert got == expected
def test_get_data_ids():
    """
    check if the get_data_ids method works as expected
    """
    dir_path = "./data/test/nifti/paired/test"
    name = "fixed_images"

    loader = NiftiFileLoader(dir_path=dir_path, name=name, grouped=False)
    got = loader.get_data_ids()
    expected = ["/case000025.nii.gz", "/case000026.nii.gz"]
    loader.close()
    assert got == expected
def test_file_paths():
    """
    check if the filepaths are the same as expected
    """
    dir_path = "./data/test/nifti/paired/test"
    name = "fixed_images"

    loader = NiftiFileLoader(dir_path=dir_path, name=name, grouped=False)
    got = loader.file_paths
    expected = [
        "./data/test/nifti/paired/test/fixed_images/case000025.nii.gz",
        "./data/test/nifti/paired/test/fixed_images/case000026.nii.gz",
    ]
    loader.close()
    assert got == expected
def test_get_data_grouped():
    """
    check if the get_data method works as expected and returns array
    as expected
    """
    dir_path = "./data/test/nifti/grouped/test"
    name = "images"

    loader = NiftiFileLoader(dir_path=dir_path, name=name, grouped=True)
    index = (0, 1)
    array = loader.get_data(index)
    got = [
        np.shape(array),
        [np.amax(array), np.amin(array), np.mean(array), np.std(array)],
    ]
    expected = [(64, 64, 60), [255.0, 0.0, 85.67942, 49.193127]]
    loader.close()
    if got[0] == expected[0]:
        assert check_equal(np.array(got[1]), np.array(expected[1])).all()
    else:
        raise AssertionError
def test_get_data():
    """
    check if the get_data method works as expected and returns array
    as expected
    """
    dir_path = "./data/test/nifti/paired/test"
    name = "fixed_images"

    loader = NiftiFileLoader(dir_path=dir_path, name=name, grouped=False)
    index = 0
    array = loader.get_data(index)
    got = [
        np.shape(array),
        [np.amax(array), np.amin(array), np.mean(array), np.std(array)],
    ]
    expected = [(44, 59, 41), [255.0, 0.0, 68.359276, 65.84009]]
    loader.close()
    if got[0] == expected[0]:
        assert check_equal(np.array(got[1]), np.array(expected[1])).all()
    else:
        raise AssertionError
def test_set_group_structure():
    """
    check if the set_group_structure method works as intended when data is
    grouped
    """
    dir_path = "./data/test/nifti/grouped/test"
    name = "images"

    loader = NiftiFileLoader(dir_path=dir_path, name=name, grouped=True)
    loader.set_group_structure()
    got = [loader.group_ids, loader.group_sample_dict]
    expected = [
        ["./data/test/nifti/grouped/test/images/group1"],
        {
            "./data/test/nifti/grouped/test/images/group1": [
                "./data/test/nifti/grouped/test/images/group1/case000025.nii.gz",
                "./data/test/nifti/grouped/test/images/group1/case000026.nii.gz",
            ]
        },
    ]

    loader.close()
    assert got == expected
Exemple #10
0
def test_get_num_images():
    """
    check if the get_num_images method works as expected
    """
    # paired
    dir_paths = ["./data/test/nifti/paired/test"]
    name = "fixed_images"
    loader = NiftiFileLoader(dir_paths=dir_paths, name=name, grouped=False)
    got = loader.get_num_images()
    expected = 2
    loader.close()
    assert got == expected

    # unpaired
    dir_paths = ["./data/test/nifti/unpaired/test"]
    name = "images"
    loader = NiftiFileLoader(dir_paths=dir_paths, name=name, grouped=False)
    got = loader.get_num_images()
    expected = 2
    loader.close()
    assert got == expected

    # grouped
    dir_paths = ["./data/test/nifti/grouped/test"]
    name = "images"
    loader = NiftiFileLoader(dir_paths=dir_paths, name=name, grouped=True)
    got = loader.get_num_images()
    expected = 2
    loader.close()
    assert got == expected

    # multi dirs
    dir_paths = [
        "./data/test/nifti/grouped/train", "./data/test/nifti/grouped/test"
    ]
    name = "images"
    loader = NiftiFileLoader(dir_paths=dir_paths, name=name, grouped=True)
    got = loader.get_num_images()
    expected = 9
    loader.close()
    assert got == expected
Exemple #11
0
def test_init():
    """
    check init of NiftiFileLoader
    this includes some test on set_data_structure/set_group_structure
    as these functions are called in init
    """

    # paired
    # test data_path_splits
    dir_paths = ["./data/test/nifti/paired/test"]
    name = "fixed_images"
    loader = NiftiFileLoader(dir_paths=dir_paths, name=name, grouped=False)
    got = loader.data_path_splits
    expected = [
        ("./data/test/nifti/paired/test", "case000025", "nii.gz"),
        ("./data/test/nifti/paired/test", "case000026", "nii.gz"),
    ]
    loader.close()
    assert got == expected

    # unpaired
    # test data_path_splits
    dir_paths = ["./data/test/nifti/unpaired/test"]
    name = "images"
    loader = NiftiFileLoader(dir_paths=dir_paths, name=name, grouped=False)
    got = loader.data_path_splits
    expected = [
        ("./data/test/nifti/unpaired/test", "case000025", "nii.gz"),
        ("./data/test/nifti/unpaired/test", "case000026", "nii"),
    ]
    loader.close()
    assert got == expected

    # grouped
    # test data_path_splits and group_struct
    dir_paths = ["./data/test/nifti/grouped/test"]
    name = "images"
    loader = NiftiFileLoader(dir_paths=dir_paths, name=name, grouped=True)
    got = [loader.data_path_splits, loader.group_struct]
    expected = [
        [
            ("./data/test/nifti/grouped/test", "group1", "case000025",
             "nii.gz"),
            ("./data/test/nifti/grouped/test", "group1", "case000026",
             "nii.gz"),
        ],
        [[0, 1]],
    ]
    loader.close()
    assert got == expected

    # multi dirs
    # test data_path_splits and group_struct
    dir_paths = [
        "./data/test/nifti/grouped/train", "./data/test/nifti/grouped/test"
    ]
    name = "images"
    loader = NiftiFileLoader(dir_paths=dir_paths, name=name, grouped=True)
    got = [loader.data_path_splits, loader.group_struct]
    expected = [
        [
            ("./data/test/nifti/grouped/test", "group1", "case000025",
             "nii.gz"),
            ("./data/test/nifti/grouped/test", "group1", "case000026",
             "nii.gz"),
            ("./data/test/nifti/grouped/train", "group1", "case000000",
             "nii.gz"),
            ("./data/test/nifti/grouped/train", "group1", "case000001",
             "nii.gz"),
            ("./data/test/nifti/grouped/train", "group1", "case000003",
             "nii.gz"),
            ("./data/test/nifti/grouped/train", "group1", "case000008",
             "nii.gz"),
            ("./data/test/nifti/grouped/train", "group2", "case000009",
             "nii.gz"),
            ("./data/test/nifti/grouped/train", "group2", "case000011",
             "nii.gz"),
            ("./data/test/nifti/grouped/train", "group2", "case000012",
             "nii.gz"),
        ],
        [[0, 1], [2, 3, 4, 5], [6, 7, 8]],
    ]
    loader.close()
    assert got == expected

    # duplicated dir_paths
    dir_paths = [
        "./data/test/nifti/grouped/test", "./data/test/nifti/grouped/test"
    ]
    name = "images"
    with pytest.raises(ValueError) as err_info:
        NiftiFileLoader(dir_paths=dir_paths, name=name, grouped=True)
    assert "dir_paths have repeated elements" in str(err_info.value)
Exemple #12
0
def test_get_data_ids():
    """
    check if the get_data_ids method works as expected
    """
    # paired
    dir_paths = ["./data/test/nifti/paired/test"]
    name = "fixed_images"
    loader = NiftiFileLoader(dir_paths=dir_paths, name=name, grouped=False)
    got = loader.get_data_ids()
    expected = [
        ("./data/test/nifti/paired/test", "case000025"),
        ("./data/test/nifti/paired/test", "case000026"),
    ]
    loader.close()
    assert got == expected

    # unpaired
    dir_paths = ["./data/test/nifti/unpaired/test"]
    name = "images"
    loader = NiftiFileLoader(dir_paths=dir_paths, name=name, grouped=False)
    got = loader.get_data_ids()
    expected = [
        ("./data/test/nifti/unpaired/test", "case000025"),
        ("./data/test/nifti/unpaired/test", "case000026"),
    ]
    loader.close()
    assert got == expected

    # grouped
    dir_paths = ["./data/test/nifti/grouped/test"]
    name = "images"
    loader = NiftiFileLoader(dir_paths=dir_paths, name=name, grouped=True)
    got = loader.get_data_ids()
    expected = [
        ("./data/test/nifti/grouped/test", "group1", "case000025"),
        ("./data/test/nifti/grouped/test", "group1", "case000026"),
    ]
    loader.close()
    assert got == expected

    # multi dirs
    dir_paths = [
        "./data/test/nifti/grouped/train", "./data/test/nifti/grouped/test"
    ]
    name = "images"
    loader = NiftiFileLoader(dir_paths=dir_paths, name=name, grouped=True)
    got = loader.get_data_ids()
    expected = [
        ("./data/test/nifti/grouped/test", "group1", "case000025"),
        ("./data/test/nifti/grouped/test", "group1", "case000026"),
        ("./data/test/nifti/grouped/train", "group1", "case000000"),
        ("./data/test/nifti/grouped/train", "group1", "case000001"),
        ("./data/test/nifti/grouped/train", "group1", "case000003"),
        ("./data/test/nifti/grouped/train", "group1", "case000008"),
        ("./data/test/nifti/grouped/train", "group2", "case000009"),
        ("./data/test/nifti/grouped/train", "group2", "case000011"),
        ("./data/test/nifti/grouped/train", "group2", "case000012"),
    ]
    loader.close()
    assert got == expected

    # wrong index for paired
    dir_paths = ["./data/test/nifti/paired/test"]
    name = "fixed_images"
    loader = NiftiFileLoader(dir_paths=dir_paths, name=name, grouped=False)
    with pytest.raises(AssertionError):
        loader.get_data(index=(0, 1))
    with pytest.raises(ValueError) as err_info:
        loader.get_data(index=[0])
    assert "must be int, or tuple" in str(err_info.value)

    # wrong index for grouped
    dir_paths = ["./data/test/nifti/grouped/test"]
    name = "images"
    loader = NiftiFileLoader(dir_paths=dir_paths, name=name, grouped=True)
    with pytest.raises(AssertionError):
        # negative group_index
        loader.get_data(index=(-1, 1))
    with pytest.raises(IndexError):
        # out of range group_index
        loader.get_data(index=(32, 1))
    with pytest.raises(AssertionError):
        # negative in_group_data_index
        loader.get_data(index=(0, -1))
    with pytest.raises(IndexError):
        # out of range in_group_data_index
        loader.get_data(index=(0, 32))
Exemple #13
0
def test_get_data():
    """
    test get_data method by verifying returns' shape and value stats
    """
    # paired
    dir_paths = ["./data/test/nifti/paired/test"]
    name = "fixed_images"
    loader = NiftiFileLoader(dir_paths=dir_paths, name=name, grouped=False)
    index = 0
    array = loader.get_data(index)
    got = [
        np.shape(array),
        [np.amax(array),
         np.amin(array),
         np.mean(array),
         np.std(array)],
    ]
    expected = [(44, 59, 41), [255.0, 0.0, 68.359276, 65.84009]]
    loader.close()
    assert got[0] == expected[0]
    assert is_equal_np(got[1], expected[1])

    # unpaired
    dir_paths = ["./data/test/nifti/unpaired/test"]
    name = "images"
    loader = NiftiFileLoader(dir_paths=dir_paths, name=name, grouped=False)
    index = 0
    array = loader.get_data(index)
    got = [
        np.shape(array),
        [np.amax(array),
         np.amin(array),
         np.mean(array),
         np.std(array)],
    ]
    expected = [(64, 64, 60), [255.0, 0.0, 60.073948, 47.27648]]
    loader.close()
    assert got[0] == expected[0]
    assert is_equal_np(got[1], expected[1])

    # grouped
    dir_paths = ["./data/test/nifti/grouped/test"]
    name = "images"
    loader = NiftiFileLoader(dir_paths=dir_paths, name=name, grouped=True)
    index = (0, 1)
    array = loader.get_data(index)
    got = [
        np.shape(array),
        [np.amax(array),
         np.amin(array),
         np.mean(array),
         np.std(array)],
    ]
    expected = [(64, 64, 60), [255.0, 0.0, 85.67942, 49.193127]]
    loader.close()
    assert got[0] == expected[0]
    assert is_equal_np(got[1], expected[1])

    # multi dirs
    dir_paths = [
        "./data/test/nifti/grouped/train", "./data/test/nifti/grouped/test"
    ]
    name = "images"
    loader = NiftiFileLoader(dir_paths=dir_paths, name=name, grouped=True)
    index = (0, 1)
    array = loader.get_data(index)
    got = [
        np.shape(array),
        [np.amax(array),
         np.amin(array),
         np.mean(array),
         np.std(array)],
    ]
    expected = [(64, 64, 60), [255.0, 0.0, 85.67942, 49.193127]]
    loader.close()
    assert got[0] == expected[0]
    assert is_equal_np(got[1], expected[1])