diff --git a/src/imcflibs/imagej/misc.py b/src/imcflibs/imagej/misc.py index 8e7d1411..9b36dd4d 100644 --- a/src/imcflibs/imagej/misc.py +++ b/src/imcflibs/imagej/misc.py @@ -766,3 +766,68 @@ def bytes_to_human_readable(size): # If the value is larger than the largest unit, fall back to TB with # the current value (already divided accordingly). return "%3.1f %s" % (size, "TB") + + +def save_script_parameters( + destination, save_file_name="script_parameters.txt", script_globals=None +): + """Save all Fiji script parameters to a text file. + + Parameters + ---------- + destination : str + Directory where the script parameters file will be saved. + save_file_name : str, optional + Name of the script parameters file, by default "script_parameters.txt". + script_globals : dict, optional + The globals dictionary from the Fiji script, default None. + Must be passed explicitly as ``globals()`` from the script. + + Notes + ----- + This function records all input parameters defined in the Fiji script header + (e.g. `#@ String`) to a text file. + + The following parameters are excluded: + - Parameters explicitly declared with `style="password"` are ignored. + - Runtime keys (e.g. 'SJLOG', 'COMMAND', 'RM') are also skipped. + + Examples + -------- + In a Fiji script, you can call this function as follows to save the parameters: + save_script_parameters(destination="params.txt", script_globals=globals()) + """ + # script_globals must be passed explicitly as globals() from the script. + g = script_globals if script_globals is not None else {} + module = g.get("org.scijava.script.ScriptModule") + if module is None: + timed_log("No ScriptModule found - skipping saving script parameters.") + return + + destination = str(destination) + out_path = os.path.join(destination, save_file_name) + + # Access script metadata and inputs + script_info = module.getInfo() + inputs = module.getInputs() + + # Keys to skip explicitly + skip_keys = ["USERNAME", "SJLOG", "COMMAND", "RM"] + + with open(out_path, "w") as f: + for item in script_info.inputs(): + key = item.getName() + + # Skip if any keys are in the skip list + if any(skip in key.upper() for skip in skip_keys): + continue + + # Skip if parameter is declared with password style + if WidgetStyle.isStyle(item, TextWidget.PASSWORD_STYLE): + continue + + if inputs.containsKey(key): + val = inputs.get(key) + f.write("%s: %s\n" % (key, str(val))) + + timed_log("Saved script parameters to: %s" % out_path) diff --git a/tests/interactive-imagej/test_save_params.md b/tests/interactive-imagej/test_save_params.md new file mode 100644 index 00000000..3e0ba1e5 --- /dev/null +++ b/tests/interactive-imagej/test_save_params.md @@ -0,0 +1,18 @@ +# Testing script for misc.save_script_parameters() + +```python +# @ String(label="Password", description="please enter your password", style="password") PASSWORD +# @ String(label="USERNAME", description="please enter your USR") USERNAME +# @ File(label="Path for storage", style="directory") outputPath +# @ Integer threshold +# @ Boolean taDa +# @ RoiManager rm +# @ CommandService command +# @ LogService sjlog + +import os +from imcflibs.imagej import misc, omerotools + +misc.save_script_parameters(outputPath, script_globals=globals()) +print("Saved params") +```