Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions src/imcflibs/imagej/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
18 changes: 18 additions & 0 deletions tests/interactive-imagej/test_save_params.md
Original file line number Diff line number Diff line change
@@ -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")
```
Loading