@cli.command()
@click.argument("path", type=click.Path(exists=True))
@click.argument("out_dir", type=click.Path(dir_okay=True))
@click.option("--index", default=0, show_default=True, help="Index of frame to sample.")
@click.option("--crop", default=0)
@click.option("--vmin", type=float, default=0)
@click.option("--vmax", type=float, default=255)
@click.option("--key", type=str, default=None, help="Activity column")
def sample(path, out_dir, index, crop=0, vmin=0, vmax=255, key="frames"):
"""Sample an image frame from an HDF5 file and export it as a PNG."""
click.echo(f"Sampling {path} at index {index}.")
f = h5py.File(path)
d = f[f"/{key}"]
os.makedirs(out_dir, exist_ok=True)
outpath = out_dir + os.sep + path.split("/")[-1].replace(".h5", f"_sample-{index}.png")
if crop > 0:
plt.imsave(outpath, d[index, crop:-crop, crop:-crop], vmin=vmin, vmax=vmax)
else:
plt.imsave(outpath, d[index], vmin=vmin, vmax=vmax, cmap="jet")
click.echo(f"Saved sample at {outpath}")