Exemplo n.º 1
0
    def test_contour(self):
        from radionets.evaluation.utils import (
            read_config,
            get_ifft,
            read_pred,
        )
        from radionets.evaluation.contour import (
            area_of_contour, )
        import toml
        import numpy as np

        config = toml.load("./tests/evaluate.toml")
        conf = read_config(config)

        pred, img_test, img_true = read_pred(
            "./tests/build/test_training/evaluation/predictions_model_eval.h5")

        ifft_pred = get_ifft(pred, amp_phase=conf["amp_phase"])
        ifft_truth = get_ifft(img_true, amp_phase=conf["amp_phase"])

        assert ~np.isnan([ifft_pred, ifft_truth]).any()

        assert ifft_pred[0].shape == (64, 64)
        assert ifft_truth[0].shape == (64, 64)

        val = area_of_contour(ifft_pred[0], ifft_truth[0])

        assert isinstance(val, np.float64)
        assert ~np.isnan(val).any()
        assert val > 0
Exemplo n.º 2
0
    def test_calc_blobs_and_crop_first_comp(self):
        import torch
        import toml
        import numpy as np
        from radionets.evaluation.utils import read_config, get_ifft, read_pred
        from radionets.evaluation.blob_detection import calc_blobs, crop_first_component

        config = toml.load("./tests/evaluate.toml")
        conf = read_config(config)

        pred, _, img_true = read_pred(
            "./tests/build/test_training/evaluation/predictions_model_eval.h5")

        ifft_pred = get_ifft(torch.tensor(pred[0]),
                             conf["amp_phase"]).reshape(64, 64)
        ifft_truth = get_ifft(torch.tensor(img_true[0]),
                              conf["amp_phase"]).reshape(64, 64)

        blobs_pred, blobs_truth = calc_blobs(ifft_pred, ifft_truth)

        assert ~np.isnan(blobs_pred).any()
        assert ~np.isnan(blobs_truth).any()
        assert blobs_pred.all() >= 0
        assert blobs_truth.all() >= 0
        assert len(blobs_truth[0]) == 3

        flux_pred, flux_truth = crop_first_component(ifft_pred, ifft_truth,
                                                     blobs_truth[0])

        assert ~np.isnan(flux_pred).any()
        assert ~np.isnan(flux_truth).any()
        assert flux_pred.all() > 0
        assert flux_truth.all() > 0
Exemplo n.º 3
0
    def test_pca(self):
        import torch
        import toml
        from radionets.evaluation.jet_angle import im_to_array_value, bmul, pca
        from radionets.evaluation.utils import read_pred, get_ifft, read_config

        config = toml.load("./tests/evaluate.toml")
        conf = read_config(config)

        torch.set_printoptions(precision=16)

        pred, img_test, img_true = read_pred(
            "./tests/build/test_training/evaluation/predictions_model_eval.h5")

        ifft_pred = get_ifft(pred, conf["amp_phase"])
        assert ifft_pred.shape == (10, 64, 64)

        pix_x, pix_y, image = im_to_array_value(torch.tensor(ifft_pred))

        cog_x = (torch.sum(pix_x * image, axis=1) /
                 torch.sum(image, axis=1)).unsqueeze(-1)
        cog_y = (torch.sum(pix_y * image, axis=1) /
                 torch.sum(image, axis=1)).unsqueeze(-1)

        assert cog_x.shape == (10, 1)
        assert cog_y.shape == (10, 1)

        delta_x = pix_x - cog_x
        delta_y = pix_y - cog_y

        inp = torch.cat([delta_x.unsqueeze(1), delta_y.unsqueeze(1)], dim=1)

        cov_w = bmul(
            (cog_x - 1 * torch.sum(image * image, axis=1).unsqueeze(-1) /
             cog_x).squeeze(1),
            (torch.matmul(image.unsqueeze(1) * inp, inp.transpose(1, 2))),
        )

        eig_vals_torch, eig_vecs_torch = torch.linalg.eigh(cov_w, UPLO='U')

        assert eig_vals_torch.shape == (10, 2)
        assert eig_vecs_torch.shape == (10, 2, 2)

        _, _, psi_torch = pca(torch.tensor(ifft_pred))

        assert len(psi_torch) == 10
        assert len(psi_torch[psi_torch > 360]) == 0
Exemplo n.º 4
0
    def test_calc_jet_angle(self):
        import torch
        import toml
        from radionets.evaluation.jet_angle import calc_jet_angle
        from radionets.evaluation.utils import read_config, read_pred, get_ifft

        config = toml.load("./tests/evaluate.toml")
        conf = read_config(config)

        pred, _, _ = read_pred(
            "./tests/build/test_training/evaluation/predictions_model_eval.h5")

        image = get_ifft(pred, conf["amp_phase"])
        assert image.shape == (10, 64, 64)

        if not isinstance(image, torch.Tensor):
            image = torch.tensor(image)
        image = image.clone()
        img_size = image.shape[-1]
        # ignore negative pixels, which can appear in predictions
        image[image < 0] = 0

        if len(image.shape) == 2:
            image = image.unsqueeze(0)

        bs = image.shape[0]

        # only use brightest pixel
        max_val = torch.tensor([(i.max() * 0.4) for i in image])
        max_arr = (torch.ones(img_size, img_size, bs) * max_val).permute(
            2, 0, 1)
        image[image < max_arr] = 0

        assert image.shape == (10, 64, 64)

        m, n, alpha = calc_jet_angle(image)

        assert len(n) == 10
        assert len(alpha) == 10
        assert len(alpha[alpha > 360]) == 0
Exemplo n.º 5
0
    def test_get_prediction(self):
        from pathlib import Path
        from radionets.evaluation.utils import (
            read_config,
            save_pred,
        )
        import toml
        import torch
        from radionets.evaluation.train_inspection import get_prediction

        config = toml.load("./tests/evaluate.toml")
        conf = read_config(config)

        pred, img_test, img_true = get_prediction(conf)
        assert str(pred.device) == "cpu"

        # test for uncertainty
        if pred.shape[1] == 4:
            pred_1 = pred[:, 0, :].unsqueeze(1)
            pred_2 = pred[:, 2, :].unsqueeze(1)
            pred = torch.cat((pred_1, pred_2), dim=1)

        assert pred.shape == (10, 2, 64, 64)
        assert img_test.shape == (10, 2, 64, 64)
        assert img_true.shape == (10, 2, 64, 64)

        pred = pred.numpy()
        out_path = Path("./tests/build/test_training/evaluation/")
        out_path.mkdir(parents=True, exist_ok=True)
        save_pred(
            str(out_path) + "/predictions_model_eval.h5",
            pred,
            img_test,
            img_true,
            "pred",
            "img_test",
            "img_true",
        )
def main(configuration_path):
    """
    Start evaluation of trained deep learning model.

    Parameters
    ----------
    configuration_path: str
        Path to the configuration toml file
    """
    conf = toml.load(configuration_path)
    eval_conf = read_config(conf)

    click.echo("\nEvaluation config:")
    print(eval_conf, "\n")

    for entry in conf["inspection"]:
        if (
            conf["inspection"][entry] is not False
            and isinstance(conf["inspection"][entry], bool)
            and entry != "random"
        ):
            if (
                not check_outpath(eval_conf["model_path"])
                or conf["inspection"]["random"]
            ):
                create_predictions(eval_conf)
                break

    if eval_conf["vis_pred"]:
        create_inspection_plots(
            eval_conf,
            num_images=eval_conf["num_images"],
            rand=eval_conf["random"],
        )

        click.echo(f"\nCreated {eval_conf['num_images']} test predictions.\n")

    if eval_conf["vis_blobs"]:
        click.echo("\nBlob visualization is enabled for source plots.\n")

    if eval_conf["vis_ms_ssim"]:
        click.echo("\nVisualization of ms ssim is enabled for source plots.\n")

    if eval_conf["vis_dr"]:
        click.echo(f"\nCreated {eval_conf['num_images']} dynamic range plots.\n")

    if eval_conf["vis_source"]:
        create_source_plots(
            eval_conf, num_images=eval_conf["num_images"], rand=eval_conf["random"]
        )

        click.echo(f"\nCreated {eval_conf['num_images']} source predictions.\n")

    if eval_conf["plot_contour"]:
        create_contour_plots(
            eval_conf, num_images=eval_conf["num_images"], rand=eval_conf["random"]
        )

        click.echo(f"\nCreated {eval_conf['num_images']} contour plots.\n")

    if eval_conf["viewing_angle"]:
        click.echo("\nStart evaluation of viewing angles.\n")
        evaluate_viewing_angle(eval_conf)

    if eval_conf["dynamic_range"]:
        click.echo("\nStart evaluation of dynamic ranges.\n")
        evaluate_dynamic_range(eval_conf)

    if eval_conf["ms_ssim"]:
        click.echo("\nStart evaluation of ms ssim.\n")
        evaluate_ms_ssim(eval_conf)

    if eval_conf["mean_diff"]:
        click.echo("\nStart evaluation of mean difference.\n")
        evaluate_mean_diff(eval_conf)

    if eval_conf["area"]:
        click.echo("\nStart evaluation of the area.\n")
        evaluate_area(eval_conf)

    if eval_conf["point"]:
        click.echo("\nStart evaluation of point sources.\n")
        evaluate_point(eval_conf)