From 07ca5589d458872f4b333b7dca46504399094a41 Mon Sep 17 00:00:00 2001 From: "William F. Broderick" Date: Wed, 9 Oct 2024 17:08:47 -0400 Subject: [PATCH] keep working on adding the initialization inset --- Snakefile | 56 +++++++++++++++++-- foveated_metamers/compose_figures.py | 28 +++++++++- foveated_metamers/style.py | 2 + ...comparison_cutout_V1_natural-seed_init.svg | 10 ++-- 4 files changed, 83 insertions(+), 13 deletions(-) diff --git a/Snakefile b/Snakefile index 3297f6f..9afcaa1 100644 --- a/Snakefile +++ b/Snakefile @@ -55,7 +55,7 @@ wildcard_constraints: logscale="log|linear", mcmc_model="partially-pooled|unpooled|partially-pooled-interactions-[.0-9]+|partially-pooled-interactions", fixation_cross="cross|nocross", - cutout="cutout|nocutout|cutout_V1_natural-seed|cutout_RGC_natural-seed|nocutout_small|cutout_downsample", + cutout="cutout|nocutout|cutout_V1_natural-seed|cutout_RGC_natural-seed|nocutout_small|cutout_downsample|cutout_V1_natural-seed_init", compressed="|_compressed", context="paper|poster", mcmc_plot_type="performance|params-(linear|log)-(none|lines|ci)", @@ -3968,6 +3968,12 @@ def get_compose_figures_input(wildcards): ] if 'performance_natural' in wildcards.fig_name: mcmc_model, details, image, comp, extra = re.findall('performance_natural_([a-z-_]+)_([a-z-]+)_image-([a-z_]+)_((?:sub-[0-9]+_)?comp-[a-z-]+)([_a-z0-9.-]+)?', wildcards.fig_name)[0] + # remove _init from extra, put in own variable + if '_init' in extra: + extra = extra.replace('_init', '') + init = '_init' + else: + init = '' # remove _compressed from extra, put in own variable if '_compressed' in extra: extra = extra.replace('_compressed', '') @@ -3977,7 +3983,7 @@ def get_compose_figures_input(wildcards): # this is hacked together to just work for this one figure. assert 'sub-00' in comp and '0.27' in extra, "Wrong image!" paths = [path_template.format(f'mcmc_{mcmc_model}_performance_{comp}{extra}'), - path_template.replace('figures', 'compose_figures').format(f'metamer_comparison_{image}_scaling-.27,.27,.27,.27,.27_cutout_V1_natural-seed{compressed}')] + path_template.replace('figures', 'compose_figures').format(f'metamer_comparison_{image}_scaling-.27,.27,.27,.27,.27_cutout_V1_natural-seed{init}{compressed}')] if 'performance_comparison' in wildcards.fig_name: mcmc_model, details, comp, extra = re.findall('performance_comparison_([a-z-_]+)_([a-z-]+)_((?:sub-[0-9]+_)?comp-[a-z-]+)([_a-z0-9.-]+)?', wildcards.fig_name)[0] paths = [path_template.format(f'mcmc_{mcmc_model}_performance_{comp}{extra}'), @@ -4042,7 +4048,8 @@ rule compose_figures: 'Luminance metamer init with white noise 2'] fig = fov.compose_figures.metamer_comparison(*input, labels, 'nocutout' not in wildcards.fig_name, - True, wildcards.context) + True, 'init' in wildcards.fig_name, + wildcards.context) else: fig = fov.compose_figures.metamer_comparison(*input, scaling, 'nocutout' not in wildcards.fig_name, @@ -4054,7 +4061,9 @@ rule compose_figures: n = 1 else: n = None - fig = fov.compose_figures.performance_comparison_natural(*input, n, wildcards.context) + fig = fov.compose_figures.performance_comparison_natural(*input, n, + 'init' in wildcards.fig_name, + wildcards.context) elif "performance_comparison" in wildcards.fig_name: if 'sub-00' in wildcards.fig_name: n = 1 @@ -4068,6 +4077,26 @@ rule compose_figures: fig.save(output[0]) +rule create_init_noise_img: + output: + op.join(config["DATA_DIR"], "initial_images", "seed-{seed}_size-{size}_white.{ext}") + log: + op.join(config["DATA_DIR"], "logs", "initial_images", "seed-{seed}_size-{size}_white_{ext}.log") + benchmark: + op.join(config["DATA_DIR"], "logs", "initial_images", "seed-{seed}_size-{size}_white_{ext}_benchmark.txt") + run: + import torch + import numpy + import foveated_metamers as fov + import plenoptic as po + seed = int(wildcards.seed) + torch.manual_seed(seed) + np.random.seed(seed) + size = [int(s) for s in wildcards.size.split(',')] + initial_image = po.to_numpy(torch.rand(size, dtype=torch.float32)).squeeze() + imageio.imwrite(output[0], fov.utils.convert_im_to_int(initial_image)) + + def get_metamer_comparison_figure_inputs(wildcards): image_name = wildcards.image_name.split(',') if len(image_name) > 1 and 'small' not in wildcards.cutout: @@ -4138,12 +4167,29 @@ def get_metamer_comparison_figure_inputs(wildcards): model_name=m, scaling=sc, gpu=0 if float(sc) < config['GPU_SPLIT'] else 1, seed=s, image_name=im, comp=comp, ext=ext) for m, im, sc, s, comp in zip(models, image_name, scaling, seeds, comps)] ] + init_ims = [] + if 'init' in wildcards.cutout: + for m, im, sc, s, comp in zip(models, image_name, scaling, seeds, comps): + mets = utils.generate_metamer_paths(gamma_corrected=True, model_name=m, scaling=sc, + gpu=0 if float(sc) < config['GPU_SPLIT'] else 1, seed_n=s, + image_name=f'{im}_range-.05,.95_size-2048,2600', + comp=comp) + assert len(mets) == 1 + if 'init-white' in mets[0]: + seed = re.findall('seed-([0-9]+)_init-white', mets[0])[0] + p = op.join(config['DATA_DIR'], 'initial_images', f'seed-{seed}_size-2048,2600_white{ext}') + else: + init = re.findall("seed-[0-9]+_init-(.+)_range-", mets[0])[0] + p = op.join(config['DATA_DIR'], 'ref_images_preproc', f'{init}_gamma-corrected_range-.05,.95_size-2048,2600{ext}') + init_ims.append(p) if 'nocutout' not in wildcards.cutout: cuts = ['with_cutout_cross', 'foveal_cutout_cross', 'peripheral_cutout_cross'] # if we're using the compressed images, want the compressed # with_cutout_cross image, but the others should be uncompressed paths[len(uniq_imgs):] = [p.replace(ext, f'_{c}{new_ext}').replace('ref_images_preproc', f'figures{os.sep}{{context}}') for p in paths[len(uniq_imgs):] for c, new_ext in zip(cuts, [ext, '.png', '.png'])] + if 'init' in wildcards.cutout: + paths.extend(init_ims) return paths @@ -5062,7 +5108,7 @@ def figure_paper_input(wildcards): op.join(config['DATA_DIR'], 'compose_figures', 'paper', "performance_comparison_scaling-extended_partially-pooled_log-ci_comp-base.svg"), op.join(config['DATA_DIR'], 'compose_figures', 'paper', 'metamer_comparison_gnarled_scaling-1.5,1.5,1.5,1.5_cutout_compressed.svg'), op.join(config['DATA_DIR'], 'compose_figures', 'paper', 'performance_scaling-extended_metamer_comparison_nyc,llama_scaling-.063,.27_nocutout_small_dpi-300.svg'), - op.join(config['DATA_DIR'], 'compose_figures', 'paper', "performance_natural_scaling-extended_partially-pooled_log-ci_image-portrait_symmetric_sub-00_comp-natural_line-scaling-0.27_compressed.svg"), + op.join(config['DATA_DIR'], 'compose_figures', 'paper', "performance_natural_scaling-extended_partially-pooled_log-ci_image-portrait_symmetric_sub-00_comp-natural_line-scaling-0.27_init_compressed.svg"), op.join(config['DATA_DIR'], 'figures', 'paper', "mcmc_scaling-extended_partially-pooled_params-log-ci_sub-00_comp-natural.svg"), op.join(config['DATA_DIR'], 'figures', 'paper', "critical_scaling_norm-False_scale-log.svg"), op.join(config['DATA_DIR'], 'figures', 'paper', "image_space_ideal.svg"), diff --git a/foveated_metamers/compose_figures.py b/foveated_metamers/compose_figures.py index 2b51435..35b4462 100644 --- a/foveated_metamers/compose_figures.py +++ b/foveated_metamers/compose_figures.py @@ -164,7 +164,9 @@ def model_schematic(schematic_fig, contour_fig_large, contour_figs_small, def metamer_comparison(metamer_fig, labels, cutout_fig=False, - natural_seed_fig=False, context='paper'): + natural_seed_fig=False, + with_initialization=False, + context='paper'): """Add text labeling model metamer scaling values. Parameters @@ -181,6 +183,9 @@ def metamer_comparison(metamer_fig, labels, cutout_fig=False, natural_seed_fig : bool, optional Whether this is the natural-seed version of this fig or not, which changes how we place the labels. + with_initialization : bool, optional + Whether this includes insets for the initial images used in metamer synthesis, + which results in a wider image and shifts label placement. context : {'paper', 'poster'}, optional plotting context that's being used for this figure (as in seaborn's set_context function). if poster, will scale things up. Note @@ -192,7 +197,10 @@ def metamer_comparison(metamer_fig, labels, cutout_fig=False, Figure containing composed plots """ - text_params, figure_width = style.plotting_style(context, 'svgutils', 'full') + if with_initialization: + text_params, figure_width = style.plotting_style(context, 'svgutils', 'extra-wide') + else: + text_params, figure_width = style.plotting_style(context, 'svgutils', 'full') figure_width = _convert_to_pix(figure_width) metamer_fig = SVG(metamer_fig, 'inkscape') metamer_move = [0, 0] @@ -210,6 +218,7 @@ def metamer_comparison(metamer_fig, labels, cutout_fig=False, font_size = _convert_to_pix(f'{font_size*5/9}pt') txt_move = [[100, 168], [375, 168], [100, 338], [375, 338], [100, 508], [375, 508]] + print(txt_move) # this has 6 subplots, and we want a label above each of them if natural_seed_fig: # want to shift the metamer figure down a little bit so there's room @@ -219,9 +228,15 @@ def metamer_comparison(metamer_fig, labels, cutout_fig=False, # +20 to account for the extra 20px added above, -170 because we want # to move everything up a row. txt_move = [[mv[0], mv[1]+20-170] for mv in txt_move] + print(txt_move) # change the x value because they're longer than the scaling labels txt_move = [[mv[0]-offset, mv[1]] for mv, offset in zip(txt_move, [10]+[63]*5)] + print(txt_move) + if with_initialization: + txt_move = [[mv[0]+1.5*offset, mv[1]] for mv, offset + in zip(txt_move, [63]*6)] + print(txt_move) return compose.Figure( figure_width, figure_height, metamer_fig.move(*metamer_move), @@ -366,6 +381,7 @@ def performance_comparison(performance_fig, param_fig, subject_n=None, context=' def performance_comparison_natural(performance_fig, metamer_fig, subject_n=1, + with_initialization=False, context='paper'): """Combine sub-00_comp-natural performance with example metamers @@ -377,6 +393,9 @@ def performance_comparison_natural(performance_fig, metamer_fig, subject_n=1, has been added). subject_n : int or None, optional If not None, add text saying "n=subject_n" on the performance_fig. + with_initialization : bool, optional + Whether this includes insets for the initial images used in metamer synthesis, + which results in a wider image and shifts label placement. context : {'paper', 'poster'}, optional plotting context that's being used for this figure (as in seaborn's set_context function). if poster, will scale things up. Note @@ -388,7 +407,10 @@ def performance_comparison_natural(performance_fig, metamer_fig, subject_n=1, Figure containing composed plots """ - text_params, figure_width = style.plotting_style(context, 'svgutils', 'full') + if with_initialization: + text_params, figure_width = style.plotting_style(context, 'svgutils', 'extra-wide') + else: + text_params, figure_width = style.plotting_style(context, 'svgutils', 'full') figure_width = _convert_to_pix(figure_width) figure_height = 1.3*figure_width+30 if subject_n is not None: diff --git a/foveated_metamers/style.py b/foveated_metamers/style.py index b3fe7c1..9fbafa3 100644 --- a/foveated_metamers/style.py +++ b/foveated_metamers/style.py @@ -114,6 +114,8 @@ def plotting_style(context='paper', for_package='matplotlib', figsize='full'): elif for_package == 'svgutils': if context == 'paper': params = {'font': 'Helvetica', 'size': '18pt'} + if figsize == 'extra-wide': + figure_width = '9in' if figsize == 'full': figure_width = '6.5in' elif figsize == 'half': diff --git a/reports/figures/metamer_comparison_cutout_V1_natural-seed_init.svg b/reports/figures/metamer_comparison_cutout_V1_natural-seed_init.svg index ba491bd..7e9dbaa 100644 --- a/reports/figures/metamer_comparison_cutout_V1_natural-seed_init.svg +++ b/reports/figures/metamer_comparison_cutout_V1_natural-seed_init.svg @@ -137,7 +137,7 @@ width="207.07634" transform="translate(13.900994)" />