Skip to content

Commit 7834a92

Browse files
Add method to save Fiji script parameters to file
Calls on scijava.script.ScriptModule through Python globals to access only the user-given parameters and not other variables at run-time.
1 parent 0e19239 commit 7834a92

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

src/imcflibs/imagej/misc.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import subprocess
88
import sys
99
import time
10+
from org.scijava.script import ScriptModule
1011

1112
from ij import IJ # pylint: disable-msg=import-error
1213
from ij.plugin import Duplicator, ImageCalculator, StackWriter
@@ -701,3 +702,35 @@ def run_imarisconvert(file_path):
701702
IJ.log("Conversion to .ims is finished.")
702703
else:
703704
IJ.log("Conversion failed with error code: %d" % result)
705+
706+
707+
def save_script_parameters(destination, save_file_name="script_parameters.txt"):
708+
"""Save all Fiji script parameters to a text file.
709+
710+
Parameters
711+
----------
712+
destination : str
713+
Directory where the script parameters file will be saved.
714+
save_file_name : str, optional
715+
Name of the script parameters file, by default "script_parameters.txt".
716+
"""
717+
# Get the ScriptModule object from globals made by Fiji
718+
module = globals().get("org.scijava.script.ScriptModule")
719+
if module is None:
720+
print("No ScriptModule found- skipping saving script parameters.")
721+
return
722+
723+
# Retrieve the input parameters from the scijava module
724+
inputs = module.getInputs()
725+
destination = str(destination)
726+
out_path = os.path.join(destination, save_file_name)
727+
728+
# Write the parameters to output file
729+
with open(out_path, "w") as f:
730+
for key in inputs.keySet():
731+
val = inputs.get(key)
732+
f.write("%s: %s\n" % (key, str(val)))
733+
734+
print("Saved script parameters to: %s" % out_path)
735+
736+

0 commit comments

Comments
 (0)