Skip to content

Commit

Permalink
adds window_area_figure
Browse files Browse the repository at this point in the history
adds new figure which shows the window area as a function of
eccentricity

also renames window_size_figure to window_example_figure (and analogous)
throughout, since that's what it actually does
  • Loading branch information
billbrod committed Mar 1, 2021
1 parent bbcfad5 commit 195b2d5
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 20 deletions.
79 changes: 63 additions & 16 deletions Snakefile
Original file line number Diff line number Diff line change
Expand Up @@ -540,18 +540,33 @@ def get_windows(wildcards):
"""
window_template = op.join(config["DATA_DIR"], 'windows_cache', 'scaling-{scaling}_size-{size}'
'_e0-{min_ecc:.03f}_em-{max_ecc:.01f}_w-{t_width}_{window_type}.pt')
if 'size-' in wildcards.image_name:
im_shape = wildcards.image_name[wildcards.image_name.index('size-') + len('size-'):]
try:
if 'size-' in wildcards.image_name:
im_shape = wildcards.image_name[wildcards.image_name.index('size-') + len('size-'):]
im_shape = im_shape.replace('.png', '')
im_shape = [int(i) for i in im_shape.split(',')]
else:
try:
im = imageio.imread(REF_IMAGE_TEMPLATE_PATH.format(image_name=wildcards.image_name))
im_shape = im.shape
except FileNotFoundError:
raise Exception("Can't find input image %s or infer its shape, so don't know what "
"windows to cache!" %
REF_IMAGE_TEMPLATE_PATH.format(image_name=wildcards.image_name))
except AttributeError:
# then there was no wildcards.image_name, so grab the first one from
# the DEFAULT_METAMERS list
default_im = IMAGES[0]
im_shape = default_im[default_im.index('size-') + len('size-'):]
im_shape = im_shape.replace('.png', '')
im_shape = [int(i) for i in im_shape.split(',')]
else:
try:
im = imageio.imread(REF_IMAGE_TEMPLATE_PATH.format(image_name=wildcards.image_name))
im_shape = im.shape
except FileNotFoundError:
raise Exception("Can't find input image %s or infer its shape, so don't know what "
"windows to cache!" %
REF_IMAGE_TEMPLATE_PATH.format(image_name=wildcards.image_name))
try:
max_ecc=float(wildcards.max_ecc)
min_ecc=float(wildcards.min_ecc)
except AttributeError:
# then there was no wildcards.max/min_ecc, so grab the default values
min_ecc = config['DEFAULT_METAMERS']['min_ecc']
max_ecc = config['DEFAULT_METAMERS']['max_ecc']
if 'cosine' in wildcards.model_name:
window_type = 'cosine'
t_width = 1.0
Expand All @@ -561,8 +576,8 @@ def get_windows(wildcards):
if wildcards.model_name.startswith("RGC"):
size = ','.join([str(i) for i in im_shape])
return window_template.format(scaling=wildcards.scaling, size=size,
max_ecc=float(wildcards.max_ecc), t_width=t_width,
min_ecc=float(wildcards.min_ecc), window_type=window_type,)
max_ecc=max_ecc, t_width=t_width,
min_ecc=min_ecc, window_type=window_type,)
elif wildcards.model_name.startswith('V1'):
windows = []
# need them for every scale
Expand All @@ -573,8 +588,8 @@ def get_windows(wildcards):
for i in range(num_scales):
output_size = ','.join([str(int(np.ceil(j / 2**i))) for j in im_shape])
windows.append(window_template.format(scaling=wildcards.scaling, size=output_size,
max_ecc=float(wildcards.max_ecc),
min_ecc=float(wildcards.min_ecc),
max_ecc=max_ecc,
min_ecc=min_ecc,
t_width=t_width, window_type=window_type))
return windows

Expand Down Expand Up @@ -1227,7 +1242,39 @@ rule scaling_comparison_figure:
fig.savefig(output[0], bbox_inches='tight')


rule window_size_figure:
rule window_area_figure:
input:
windows = get_windows,
output:
report(op.join(config['DATA_DIR'], 'figures', '{context}', '{model_name}',
'scaling-{scaling}_window_area.svg'))
log:
op.join(config['DATA_DIR'], 'logs', 'figures', '{context}', '{model_name}',
'scaling-{scaling}_window_area.log')
benchmark:
op.join(config['DATA_DIR'], 'logs', 'figures', '{context}', '{model_name}',
'scaling-{scaling}_window_area_benchmark.txt')
params:
cache_dir = lambda wildcards: op.join(config['DATA_DIR'], 'windows_cache'),
run:
import foveated_metamers as fov
import seaborn as sns
import contextlib
with open(log[0], 'w', buffering=1) as log_file:
with contextlib.redirect_stdout(log_file), contextlib.redirect_stderr(log_file):
font_scale = {'poster': 1.7}.get(wildcards.context, 1)
min_ecc = config['DEFAULT_METAMERS']['min_ecc']
max_ecc = config['DEFAULT_METAMERS']['max_ecc']
with sns.plotting_context(wildcards.context, font_scale=font_scale):
# remove the normalizing aspect, since we don't need it here
model, _, _, _ = fov.create_metamers.setup_model(wildcards.model_name.replace('_norm', ''),
float(wildcards.scaling),
image, min_ecc, max_ecc, params.cache_dir)
fig = fov.figures.pooling_window_area(model.PoolingWindows)
fig.savefig(output[0])


rule window_example_figure:
input:
image = lambda wildcards: [m.replace('metamer.png', 'metamer_gamma-corrected.png') for m in
utils.generate_metamer_paths(**wildcards)],
Expand Down Expand Up @@ -1261,7 +1308,7 @@ rule window_size_figure:
model, _, _, _ = fov.create_metamers.setup_model(wildcards.model_name.replace('_norm', ''),
float(wildcards.scaling),
image, min_ecc, max_ecc, params.cache_dir)
fig = fov.figures.pooling_window_size(model.PoolingWindows, image)
fig = fov.figures.pooling_window_example(model.PoolingWindows, image)
fig.savefig(output[0])


Expand Down
51 changes: 49 additions & 2 deletions foveated_metamers/figures.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,8 @@ def scaling_comparison_figure(model_name, image_name, scaling_vals, seed, window
return fig


def pooling_window_size(windows, image, target_eccentricity=24,
windows_scale=0, **kwargs):
def pooling_window_example(windows, image, target_eccentricity=24,
windows_scale=0, **kwargs):
"""Plot example window on image.
This plots a single window, as close to the target_eccentricity as
Expand Down Expand Up @@ -502,6 +502,53 @@ def synthesis_video(metamer_save_path, model_name=None):
fig.savefig(path)


def pooling_window_area(windows, windows_scale=0, units='degrees'):
"""Plot window area as function of eccentricity.
Plots the area of the window bands as function of eccentricity, with a
horizontal line corresponding to a single pixel.
Parameters
----------
windows : po.simul.PoolingWindows
The PoolingWindows object to plot.
windows_scale : int, optional
The scale of the windows to plot. If greater than 0, we down-sampled
image by a factor of 2 that many times so they plot correctly. If
units=='degrees', only the one-pixel line will change for different
scales.
units: {'degrees', 'pixels'}, optional
Which unit to plot eccentricity and area in.
Returns
-------
fig : plt.Figure
The figure containing the plot.
"""
fig = windows.plot_window_areas(units, scale_num=windows_scale,
figsize=(15, 5), ax=ax)
if units == 'degrees':
# half is the smallest windows (for our models, which use gaussian
# windows), full the largest.
ylim = (windows.window_approx_area_degrees['half'].min(),
windows.window_approx_area_degrees['full'].max())
one_pixel_line = 1 / windows.deg_to_pix[windows_scale]
elif units == 'pixels':
# half is the smallest windows (for our models, which use gaussian
# windows), full the largest.
ylim = (windows.window_approx_area_pixels['half'].min(),
windows.window_approx_area_pixels['full'].max())
one_pixel_line = 1
ylim = plotting.get_log_ax_lims(np.array(ylim), base=10)
xlim = fig.axes[0].get_xlim()
fig.axes[0].hlines(one_pixel_line, *xlim, colors='r', linestyles='--',
label='one pixel')
fig.axes[0].set(yscale='log', xscale='log', ylim=ylim, xlim=xlim)
fig.axes[0].legend()
return fig


def simulate_num_trials(params, row='critical_scaling_true', col='variable'):
"""Create figure summarizing num_trials simulations.
Expand Down
2 changes: 1 addition & 1 deletion prince.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
{
"mem": "{resources.mem}GB"
},
"window_size_figure":
"window_example_figure":
{
"mem": "{resources.mem}GB"
},
Expand Down
2 changes: 1 addition & 1 deletion rusty.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
{
"mem": "{resources.mem}GB"
},
"window_size_figure":
"window_example_figure":
{
"mem": "{resources.mem}GB"
},
Expand Down

0 comments on commit 195b2d5

Please sign in to comment.