Example #1
0
def test_close():
    """
    Test the close function. Only needed for H5 data loaders for now. Since fixed/moving loaders are the same for
    unpaired data loader, only need to test the moving.
    """
    for key_file_loader, file_loader in FileLoaderDict.items():
        for split in ["train", "test"]:

            data_dir_path = join(DataPaths[key_file_loader], split)
            image_shape = (64, 64, 60)
            common_args = dict(
                file_loader=file_loader,
                labeled=True,
                sample_label="all",
                seed=None if split == "train" else 0,
            )

            data_loader = UnpairedDataLoader(data_dir_path=data_dir_path,
                                             image_shape=image_shape,
                                             **common_args)

            if key_file_loader == "h5":
                data_loader.close()
                assert data_loader.loader_moving_image.h5_file.__bool__(
                ) is False
                assert data_loader.loader_moving_image.h5_file.__bool__(
                ) is False
Example #2
0
def get_single_data_loader(data_type, data_config, common_args, data_dir_path):
    if data_type == "paired":
        moving_image_shape = data_config["moving_image_shape"]
        fixed_image_shape = data_config["fixed_image_shape"]
        return PairedDataLoader(
            data_dir_path=data_dir_path,
            moving_image_shape=moving_image_shape,
            fixed_image_shape=fixed_image_shape,
            **common_args,
        )
    elif data_type == "grouped":
        image_shape = data_config["image_shape"]
        intra_group_prob = data_config["intra_group_prob"]
        intra_group_option = data_config["intra_group_option"]
        sample_image_in_group = data_config["sample_image_in_group"]
        return GroupedDataLoader(
            data_dir_path=data_dir_path,
            intra_group_prob=intra_group_prob,
            intra_group_option=intra_group_option,
            sample_image_in_group=sample_image_in_group,
            image_shape=image_shape,
            **common_args,
        )
    elif data_type == "unpaired":
        image_shape = data_config["image_shape"]
        return UnpairedDataLoader(
            data_dir_path=data_dir_path, image_shape=image_shape, **common_args
        )
    else:
        raise ValueError(
            "Unknown data format. "
            "Supported types are paired, unpaired, and grouped, got {}\n".format(
                data_type
            )
        )
Example #3
0
def test_validate_data_files():
    """
    Test the validate_data_files functions that looks for inconsistencies in the fixed/moving image and label lists.
    If there is any issue it will raise an error, otherwise it returns None.
    """
    for key_file_loader, file_loader in FileLoaderDict.items():
        for split in ["train", "test"]:
            data_dir_path = join(DataPaths[key_file_loader], split)
            image_shape = (64, 64, 60)
            common_args = dict(
                file_loader=file_loader,
                labeled=True,
                sample_label="all",
                seed=None if split == "train" else 0,
            )

            data_loader = UnpairedDataLoader(data_dir_path=data_dir_path,
                                             image_shape=image_shape,
                                             **common_args)

            assert data_loader.validate_data_files() is None
Example #4
0
def test_sample_index_generator():
    """
    Test to check the randomness and deterministic index generator for train/test respectively.
    """
    image_shape = (64, 64, 60)

    for key_file_loader, file_loader in FileLoaderDict.items():
        for split in ["train", "test"]:
            data_dir_path = join(DataPaths[key_file_loader], split)
            indices_to_compare = []

            for seed in [0, 1, 0]:
                data_loader = UnpairedDataLoader(
                    data_dir_path=data_dir_path,
                    image_shape=image_shape,
                    file_loader=file_loader,
                    labeled=True,
                    sample_label="all",
                    seed=seed,
                )

                data_indices = []
                for (
                        moving_index,
                        fixed_index,
                        indices,
                ) in data_loader.sample_index_generator():
                    assert isinstance(moving_index, int)
                    assert isinstance(fixed_index, int)
                    assert isinstance(indices, list)
                    data_indices += indices

                indices_to_compare.append(data_indices)

            # test different seeds give different indices
            assert np.allclose(indices_to_compare[0],
                               indices_to_compare[1]) is False
            # test same seeds give the same indices
            assert np.allclose(indices_to_compare[0],
                               indices_to_compare[2]) is True
Example #5
0
def get_single_data_loader(
    data_type: str, data_config: dict, common_args: dict, data_dir_path: str
) -> DataLoader:
    """
    Return one single data loader.
    :param data_type: type of the data, paired / unpaired / grouped
    :param data_config: dictionary containing the configuration of the data
    :param common_args: some shared arguments for all data loaders
    :param data_dir_path: path of the directory containing data
    :return: a basic data loader
    """
    try:
        if data_type == "paired":
            moving_image_shape = data_config["moving_image_shape"]
            fixed_image_shape = data_config["fixed_image_shape"]
            return PairedDataLoader(
                data_dir_path=data_dir_path,
                moving_image_shape=moving_image_shape,
                fixed_image_shape=fixed_image_shape,
                **common_args,
            )
        elif data_type == "grouped":
            image_shape = data_config["image_shape"]
            intra_group_prob = data_config["intra_group_prob"]
            intra_group_option = data_config["intra_group_option"]
            sample_image_in_group = data_config["sample_image_in_group"]
            return GroupedDataLoader(
                data_dir_path=data_dir_path,
                intra_group_prob=intra_group_prob,
                intra_group_option=intra_group_option,
                sample_image_in_group=sample_image_in_group,
                image_shape=image_shape,
                **common_args,
            )
        elif data_type == "unpaired":
            image_shape = data_config["image_shape"]
            return UnpairedDataLoader(
                data_dir_path=data_dir_path, image_shape=image_shape, **common_args
            )
    except KeyError as e:
        msg = f"{e.args[0]} is not provided in the dataset config for paired data.\n"
        if data_type == "paired":
            msg += (
                "Paired Loader requires 'moving_image_shape' and 'fixed_image_shape'.\n"
            )
        elif data_type == "grouped":
            msg += (
                "Grouped Loader requires 'image_shape', "
                "as the data are not paired and will be resized to the same shape.\n"
                "It also requires 'intra_group_prob', 'intra_group_option', and 'sample_image_in_group'.\n"
            )
        elif data_type == "unpaired":
            msg += (
                "Unpaired Loader requires 'image_shape', "
                "as the data are not paired and will be resized to the same shape.\n"
            )
        raise ValueError(f"{msg}" f"The given dataset config is {data_config}\n")
    raise ValueError(
        f"Unknown data format {data_type}. "
        f"Supported types are paired, unpaired, and grouped.\n"
    )