def create_contour_plots(conf, num_images=3, rand=False):
    model_path = conf["model_path"]
    path = str(Path(model_path).parent / "evaluation")
    path += "/predictions.h5"
    out_path = Path(model_path).parent / "evaluation"

    if not conf["fourier"]:
        click.echo("\n This is not a fourier dataset.\n")

    pred, img_test, img_true = read_pred(path)

    # inverse fourier transformation for prediction
    ifft_pred = get_ifft(pred, amp_phase=conf["amp_phase"])

    # inverse fourier transform for truth
    ifft_truth = get_ifft(img_true, amp_phase=conf["amp_phase"])

    for i, (pred, truth) in enumerate(zip(ifft_pred, ifft_truth)):
        plot_contour(
            pred,
            truth,
            out_path,
            i,
            plot_format=conf["format"],
        )
Пример #2
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
Пример #3
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
Пример #4
0
    def test_im_to_array_value(self):
        from radionets.evaluation.utils import read_pred
        from radionets.evaluation.jet_angle import im_to_array_value

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

        image = pred[0]

        x_coords, y_coords, value = im_to_array_value(image)

        assert x_coords.shape == (2, 64**2)
        assert y_coords.shape == (2, 64**2)
        assert value.shape == (2, 64**2)
Пример #5
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
Пример #6
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
def create_inspection_plots(conf, num_images=3, rand=False):
    model_path = conf["model_path"]
    path = str(Path(model_path).parent / "evaluation")
    path += "/predictions.h5"
    out_path = Path(model_path).parent / "evaluation/"

    pred, img_test, img_true = read_pred(path)
    if conf["fourier"]:
        if conf["diff"]:
            for i in range(len(img_test)):
                visualize_with_fourier_diff(
                    i,
                    pred[i],
                    img_true[i],
                    amp_phase=conf["amp_phase"],
                    out_path=out_path,
                    plot_format=conf["format"],
                )
        else:
            for i in range(len(img_test)):
                visualize_with_fourier(
                    i,
                    img_test[i],
                    pred[i],
                    img_true[i],
                    amp_phase=conf["amp_phase"],
                    out_path=out_path,
                    plot_format=conf["format"],
                )
    else:
        plot_results(
            img_test.cpu(),
            reshape_2d(pred.cpu()),
            reshape_2d(img_true),
            out_path,
            save=True,
            plot_format=conf["format"],
        )
def create_source_plots(conf, num_images=3, rand=False):
    """
    function for visualizing the output of a inverse fourier transform. For now, it is
    necessary to take the absolute of the result of the inverse fourier transform,
    because the output is complex.
    i: current index of the loop, just used for saving
    real_pred: real part of the prediction computed in visualize with fourier
    imag_pred: imaginary part of the prediction computed in visualize with fourier
    real_truth: real part of the truth computed in visualize with fourier
    imag_truth: imaginary part of the truth computed in visualize with fourier
    """
    model_path = conf["model_path"]
    path = str(Path(model_path).parent / "evaluation")
    path += "/predictions.h5"
    out_path = Path(model_path).parent / "evaluation"

    pred, img_test, img_true = read_pred(path)

    # inverse fourier transformation for prediction
    ifft_pred = get_ifft(pred, amp_phase=conf["amp_phase"])

    # inverse fourier transform for truth
    ifft_truth = get_ifft(img_true, amp_phase=conf["amp_phase"])

    for i, (pred, truth) in enumerate(zip(ifft_pred, ifft_truth)):
        visualize_source_reconstruction(
            pred,
            truth,
            out_path,
            i,
            dr=conf["vis_dr"],
            blobs=conf["vis_blobs"],
            msssim=conf["vis_ms_ssim"],
            plot_format=conf["format"],
        )

    return np.abs(ifft_pred), np.abs(ifft_truth)