예제 #1
0
 def input_to_image_grid(self, inputs, nrows=None):
     nrows, ncols = get_layout(n=len(inputs))
     outputs = self.generate_from_condition(inputs=inputs)
     image_array = [[
         image.reshape([self._image_shape[0], self._image_shape[1]])
         for image in outputs[r * ncols:(r + 1) * ncols]
     ] for r in range(nrows)]
     return image_array
예제 #2
0
    def build_simulated_events(self, condition, tracker_image, calo_image, eval_functions,
                               cgan_image=None, n=10, title=None, reference_images=None, func_titles=None, x_labels=None,
                               scaler=1):

        inputs = np.array([condition for _ in range(n)])
        outputs = self.generate_batches(list_of_inputs=inputs, batch_size=100)*scaler
        mean_output = np.mean(outputs, axis=0).reshape(calo_image.shape)

        layout = get_layout(n=5+len(eval_functions)) # Tracker, Calo, GAN, Mean generated
        fig, ax = plt.subplots(nrows=2, ncols=5, figsize=(16, 12))
        fig.subplots_adjust(left=0.05, bottom=0.07, right=0.99, top=0.99, wspace=0.2, hspace=0.)
        ax = ax.ravel()

        if cgan_image is None:
            E_max = np.max(np.stack((calo_image, mean_output), axis=0))
            titles = ["Tracker", "Calorimeter", "Generated average (n={})".format(n)]
            show_images = [tracker_image, calo_image, mean_output]
        else:
            E_max = np.max(np.stack((calo_image, cgan_image, mean_output), axis=0))
            titles = ["Tracker", "Calorimeter", "STP", "CIR average (n={})".format(n)]
            show_images = [tracker_image, calo_image, cgan_image, mean_output]

        if reference_images is not None:
            E_max = np.max([E_max, np.max(reference_images)])
            titles.append("TIP average (n={})".format(len(reference_images)))
            show_images.append(np.mean(reference_images, axis=0).reshape(calo_image.shape))

        for i, image in enumerate(show_images):
            if titles[i] == "Tracker":
                ax[i].imshow(image)
            else:
                im = ax[i].imshow(image)#, vmin=0, vmax=E_max)
            ax[i].get_xaxis().set_visible(False)
            ax[i].get_yaxis().set_visible(False)
            ax[i].set_title(titles[i])
        # fig.colorbar(im, ax=ax[4], shrink=0.6)

        if func_titles is None:
            func_titles = [func.__name__ for func in eval_functions]
        if x_labels is None:
            x_labels = ["" for _ in eval_functions]

        for i, (func, params) in enumerate(eval_functions.items()):
            if ("cells" in func.__name__) or ("max" in func.__name__):
                fake_values = np.array(func(crop_images(outputs, **self._padding), **params))
                real_value = np.array(func(crop_images(np.array([calo_image]), **self._padding), **params))
                if reference_images is not None:
                    ref_values = np.array(func(crop_images(reference_images, **self._padding), **params))
            else:
                fake_values = np.array(func(outputs, **params))
                real_value = np.array(func(np.array([calo_image]), **params))
                if reference_images is not None:
                    ref_values = np.array(func(reference_images, **params))

            if "mass_x" in func.__name__:
                fake_values /= self._image_shape[1]
                real_value /= self._image_shape[1]
                if reference_images is not None:
                    ref_values /= self._image_shape[1]
                ax[i+5].set_xlim(-1, 1)
            elif "mass_y" in func.__name__:
                fake_values /= self._image_shape[0]
                real_value /= self._image_shape[0]
                if reference_images is not None:
                    ref_values /= self._image_shape[0]
                ax[i+5].set_xlim(-1, 1)

            elif "resolution" in func.__name__:
                ax[i+5].set_xlabel("tracker-reco")

            if reference_images is not None:
                ax[i+5].hist([ref_values, fake_values], bins=20, histtype="step", stacked=False,
                             label=["TIP", "CIR"], density=True)
                ax[i+5].legend()
            else:
                ax[i+5].hist(fake_values, bins=20)
            ax[i+5].axvline(real_value, color='k', linestyle='dashed')
            ax[i+5].set_title(func_titles[i])
            ax[i+5].set_xlabel(x_labels[i])

        if title is not None:
            plt.text(0.05, 0.95, title, transform=fig.transFigure, size=24)
        return fig, ax
예제 #3
0
    def build_simulated_events(self,
                               condition,
                               tracker_image,
                               calo_image,
                               eval_functions,
                               gen_scaler=1,
                               n=10,
                               title=None,
                               reference_images=None):
        tracker_image = np.sum(tracker_image, axis=0)
        calo_image = np.sum(calo_image, axis=0).reshape(self._image_shape[:-1])

        inputs = [condition for _ in range(n)]
        outputs = self.generate_multiple_overlay(
            list_of_inputs=inputs) * gen_scaler
        mean_output = np.mean(outputs, axis=0).reshape(calo_image.shape)

        layout = get_layout(
            n=4 +
            len(eval_functions))  # Tracker, Calo, Mean generated, Reference
        fig, ax = plt.subplots(nrows=layout[0],
                               ncols=layout[1],
                               figsize=(layout[1] * 6, layout[0] * 6))
        fig.subplots_adjust(wspace=0.2, hspace=0.3)
        ax = ax.ravel()

        E_min = 0
        E_max = np.max(np.stack((calo_image, mean_output), axis=0))
        titles = [
            "Tracker", "Calorimeter", "Generated average (n={})".format(n)
        ]
        for i, image in enumerate([tracker_image, calo_image, mean_output]):
            if titles[i] == "Tracker":
                ax[i].imshow(image)
            else:
                im = ax[i].imshow(image, vmin=E_min, vmax=E_max)
            ax[i].get_xaxis().set_visible(False)
            ax[i].get_yaxis().set_visible(False)
            ax[i].set_title(titles[i])
        fig.colorbar(im, ax=ax[:3], shrink=0.6)

        for i, (func, params) in enumerate(eval_functions.items()):
            if ("cells" in func.__name__) or ("max" in func.__name__):
                fake_values = np.array(
                    func(crop_images(outputs, **self._padding), **params))
                real_value = np.array(
                    func(crop_images(np.array([calo_image]), **self._padding),
                         **params))
                if reference_images is not None:
                    ref_values = np.array(
                        func(crop_images(reference_images, **self._padding),
                             **params))
            else:
                fake_values = np.array(func(outputs, **params))
                real_value = np.array(func(np.array([calo_image]), **params))
                if reference_images is not None:
                    ref_values = np.array(func(reference_images, **params))

            if "mass_x" in func.__name__:
                fake_values /= self._image_shape[1]
                real_value /= self._image_shape[1]
                if reference_images is not None:
                    ref_values /= self._image_shape[1]
                ax[i + 3].set_xlim(-1, 1)
            elif "mass_y" in func.__name__:
                fake_values /= self._image_shape[0]
                real_value /= self._image_shape[0]
                if reference_images is not None:
                    ref_values /= self._image_shape[0]
                ax[i + 3].set_xlim(-1, 1)
            elif "resolution" in func.__name__:
                ax[i + 3].set_xlabel("true-reco")

            if reference_images is not None:
                ax[i + 3].hist([ref_values, fake_values],
                               bins=20,
                               histtype="step",
                               stacked=False,
                               label=["reference", "generated"],
                               density=True)
                ax[i + 3].legend()
            else:
                ax[i + 3].hist(fake_values, bins=20)
            ax[i + 3].axvline(real_value, color='k', linestyle='dashed')
            if "get_energies" == func.__name__:
                ax[i + 3].axvline(real_value + 0.1 * real_value,
                                  color='gray',
                                  linestyle='dashed')
                ax[i + 3].axvline(real_value - 0.1 * real_value,
                                  color='gray',
                                  linestyle='dashed')

            ax[i + 3].set_title(func.__name__)

        if reference_images is not None:
            ax[-1].imshow(get_mean_image(reference_images))
            ax[-1].set_title("{} references".format(len(reference_images)))
        if title is not None:
            plt.text(0.05, 0.95, title, transform=fig.transFigure, size=24)
        return fig, ax
예제 #4
0
def create_image_summary(paths_to_outputs,
                         image_folder,
                         nr_images,
                         save_path,
                         ignore=None,
                         image_name="samples"):
    if isinstance(paths_to_outputs, str):
        paths_to_outputs = [paths_to_outputs]
    if ignore is None:
        ignore = "__//##//"
    subfolders = [
        f.path + "/" + image_folder for fpath in paths_to_outputs
        for f in os.scandir(fpath) if f.is_dir() and ignore not in f.path
    ]
    subfolders.sort(key=natural_keys)
    figs = []
    nrows, ncols = get_layout(n=nr_images)
    for i, folder in enumerate(subfolders):
        if i % 10 == 0:
            print(i + 1, "/", len(subfolders), ":", folder)
        if ignore in folder:
            continue
        try:
            image_paths = [f.path for f in os.scandir(folder)]
        except FileNotFoundError:
            print("File not found for {}.".format(folder))
            continue
        image_paths.sort(key=natural_keys)
        im_ids = (np.linspace(0, 1, nr_images) *
                  (len(image_paths) - 1)).astype(int)

        fig, axs = plt.subplots(nrows=nrows, ncols=ncols, figsize=(20, 16))
        fig.suptitle(folder)
        axs = np.ravel(axs)
        for ax_idx, ax in enumerate(axs):
            if im_ids[ax_idx] >= len(image_paths):
                break
            im_id = im_ids[ax_idx] if nr_images != 1 else -1
            current_image_path = image_paths[im_id]
            image = imageio.imread(current_image_path)
            im_name = current_image_path.partition(image_folder + "/")[2]
            ax.set_title(im_name)
            ax.imshow(image)
        figs.append(fig)
        if (i + 1) % 10 == 0:
            savefigs(figures=figs,
                     save_path=save_path + "/samples_{}.pdf".format(i + 1))
            plt.cla()
            plt.clf()
            for fig in figs:
                del fig
            figs = []
        elif (i + 1) == len(subfolders):
            savefigs(figures=figs,
                     save_path=save_path + "/samples_{}.pdf".format(i + 1))

    merger = PdfFileMerger()
    pdfs = [
        f.path for f in os.scandir(save_path)
        if ".pdf" in f.path and "samples_" in f.path
    ]
    pdfs.sort(key=natural_keys)
    for pdf in pdfs:
        merger.append(pdf)
        os.remove(pdf)
    merger.write(save_path + image_name + ".pdf")
    merger.close()