From 82bd5157f61fd254ec6c5b826d9c22a13b19aad8 Mon Sep 17 00:00:00 2001 From: Aliaksandr Yakutovich Date: Thu, 21 Nov 2024 14:38:47 +0100 Subject: [PATCH 1/2] Allow reading multiple cube files from a folder. --- cubehandler/cli/commands/shrink.py | 35 ++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/cubehandler/cli/commands/shrink.py b/cubehandler/cli/commands/shrink.py index 17344bb..684bb55 100644 --- a/cubehandler/cli/commands/shrink.py +++ b/cubehandler/cli/commands/shrink.py @@ -1,13 +1,34 @@ +from pathlib import Path + import click from ...cube import Cube @click.command(help="Shrink a cube file.") -@click.argument("input_cube", type=click.Path(exists=True)) -@click.argument("output_cube", type=click.Path()) -def shrink(input_cube, output_cube): - cube = Cube.from_file(input_cube) - cube.reduce_data_density(points_per_angstrom=2) - cube.rescale_data() - cube.write_cube_file(output_cube, low_precision=True) +@click.argument( + "input_path", + type=click.Path(exists=True, file_okay=True, dir_okay=True), + required=True, +) +@click.argument( + "output_path", type=click.Path(file_okay=True, dir_okay=True), required=True +) +@click.option("-p", "--prefix", default="reduced_") +def shrink(input_path, output_path, prefix): + inp = Path(input_path) + out = Path(output_path) + + def run_reduction(inp, out): + cube = Cube.from_file(inp) + cube.reduce_data_density(points_per_angstrom=2) + cube.rescale_data() + cube.write_cube_file(out, low_precision=True) + + if inp.is_file(): + run_reduction(inp, out) + elif inp.is_dir(): + out.mkdir(exist_ok=True) + for file in inp.glob("*cube"): + out_file = out / (prefix + file.name) + run_reduction(file.absolute(), out_file) From 14258e6525c3eed79f2a36091d809f88c332e56f Mon Sep 17 00:00:00 2001 From: Aliaksandr Yakutovich Date: Tue, 17 Dec 2024 14:09:11 +0000 Subject: [PATCH 2/2] Remove redundant rescaling --- cubehandler/cli/commands/shrink.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cubehandler/cli/commands/shrink.py b/cubehandler/cli/commands/shrink.py index 684bb55..1716567 100644 --- a/cubehandler/cli/commands/shrink.py +++ b/cubehandler/cli/commands/shrink.py @@ -22,7 +22,7 @@ def shrink(input_path, output_path, prefix): def run_reduction(inp, out): cube = Cube.from_file(inp) cube.reduce_data_density(points_per_angstrom=2) - cube.rescale_data() + # cube.rescale_data() # Rescaling happens below when low_precision is True cube.write_cube_file(out, low_precision=True) if inp.is_file():