From 80f45c6a716e6bf6ef2df2ecd592e2cd179fc69f Mon Sep 17 00:00:00 2001 From: Yixin Ma Date: Wed, 18 Jan 2023 12:30:10 -0500 Subject: [PATCH] try to address: 'weighting-signal-loss' is only relavant to --- shimmingtoolbox/cli/b0shim.py | 36 ++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/shimmingtoolbox/cli/b0shim.py b/shimmingtoolbox/cli/b0shim.py index aacdf475..c53090aa 100644 --- a/shimmingtoolbox/cli/b0shim.py +++ b/shimmingtoolbox/cli/b0shim.py @@ -31,14 +31,35 @@ logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) +def command_required_option_from_option(require_name, require_map): + + class CommandOptionRequiredClass(click.Command): + + def invoke(self, ctx): + require = ctx.params[require_name] + if require not in require_map: + raise click.ClickException( + "Unexpected value for --'{}': {}".format( + require_name, require)) + if ctx.params[require_map[require].lower()] is None: + raise click.ClickException( + "With {}={} must specify option --{}".format( + require_name, require, require_map[require])) + super(CommandOptionRequiredClass, self).invoke(ctx) + + return CommandOptionRequiredClass @click.group(context_settings=CONTEXT_SETTINGS, help="Shim according to the specified algorithm as an argument e.g. st_b0shim xxxxx") def b0shim_cli(): pass - -@click.command(context_settings=CONTEXT_SETTINGS) +required_options = { + 'grad': ['w_signal_loss', 'reg_factor'], + 'mse': 'reg_factor', + 'mae': 'reg_factor', +} +@click.command(context_settings=CONTEXT_SETTINGS, cls=command_required_option_from_option('opt_criteria', required_options)) @click.option('--coil', 'coils', nargs=2, multiple=True, type=(click.Path(exists=True), click.Path(exists=True)), help="Pair of filenames containing the coil profiles followed by the filename to the constraints " "e.g. --coil a.nii cons.json. If you have more than one coil, use this option more than once. " @@ -74,19 +95,16 @@ def b0shim_cli(): help="Regularization factor for the current when optimizing. A higher coefficient will penalize higher " "current values while 0 provides no regularization. Not relevant for 'pseudo-inverse' " "optimizer_method.") -######################################################################################################################## +@click.option('--optimizer-criteria', 'opt_criteria', type=click.Choice(['mse', 'mae','grad']), required=False, + default='mse', show_default=True, + help="Criteria of optimization for the optimizer 'least_squares'." + " mse: Mean Squared Error, mae: Mean Absolute Error, grad: Signal Loss") @click.option('--weighting-signal-loss', 'w_signal_loss', type=click.FLOAT, required=False, default=0.0, show_default=True, help="weighting for signal loss recovery. Since there is generally a compromise between B0 inhomogeneity" " and signal loss recovery, a higher coefficient will put more weights to recover the signal loss over " "the B0 inhomogeneity.") @click.option('--epi_echo_time', 'epi_te', type=click.FLOAT, required=False, default=0.0, show_default=True, help="EPI acquistion parameter Echo Time (TE).") -# add grad option -@click.option('--optimizer-criteria', 'opt_criteria', type=click.Choice(['mse', 'mae','grad']), required=False, - default='mse', show_default=True, - help="Criteria of optimization for the optimizer 'least_squares'." - " mse: Mean Squared Error, mae: Mean Absolute Error, grad: Signal Loss") -######################################################################################################################## @click.option('--mask-dilation-kernel-size', 'dilation_kernel_size', type=click.INT, required=False, default='3', show_default=True, help="Number of voxels to consider outside of the masked area. For example, when doing dynamic shimming "