Skip to content

Generalization of save_script_parameters聽#145

Description

@ehrenfeu

Actually... While doing #140 a few more ideas for this function popped 馃挕 up:

  1. It would be nice if the function were to automagically determine whether the parameters should be saved or not. Then it could simply always be called from our scripts and do its service. A reasonable 馃 approach would be to have an empty default value for the target location by using destination="" and skip parameter saving if it's not defined.

  2. Instead of supplying the saving location explicitly in parameter destination, we could modify it such that the location is defined within the Script Parameters (see illustration example below) using a default parameter name (e.g. OUTPUT_PATH or similar) and only have an optional parameter for save_script_parameters that allows to specify which Script Parameter contains the saving location if it differs from the standard.

  3. To achieve this, processing 鈿欙笍 of the Script Parameters in save_script_parameters would need to be modified such that it works through the items obtained via script_globals and assembles a list of things 馃搼 to be saved while determining the saving location 馃捑 at the same time - only using open(out_path, "w") as f: after all Script Parameters have been processed (as only then out_path would be known).

  4. We should consider to construct the file name used to save the parameters and values automatically, e.g. following this pattern: <calling_script_name>_run-<timestamp>_params.txt - this would allow for saving the parameters at each run without having to worry (too much) about overwriting previously saved ones. For hints on how to identify the name of the calling script, this SO thread might contain useful ideas - 馃毃 careful though, it's pretty old and might not work the same way in ImageJ-Jython! 鈽濔煆糡he .py suffix should be stripped from the pattern.

  5. In case saving the parameters in the OUTPUT_PATH location suggested above fails (location might be read-only, ...), falling back to e.g. the user's home directory would be nice (plus issuing a 鈿狅笍 warning message obviously).

example1.py

#@ File(label="Input data", style="directory") INPUT_PATH
#@ File(label="Path for results", style="directory") OUTPUT_PATH

from imcflibs.imagej.misc import save_script_parameters

save_script_parameters(script_globals=globals())

Running the above with OUTPUT_PATH=/data/project23/results would result in this:

Saved 2 script parameters to: /data/project23/results/example1_run-2026-04-10--10-41_params.txt

example2.py

This illustrates the option of using a different Script Parameter to define where the parameters should be saved:

#@ File(label="Input data", style="directory") INPUT_PATH
#@ File(label="Path for results", style="directory") OUTPUT_PATH
#@ File(label="Path to store parameters", style="directory") PARAMS_PATH

from imcflibs.imagej.misc import save_script_parameters

save_script_parameters(script_globals=globals(), location_param="PARAMS_PATH")

Running this with PARAMS_PATH=/data/project23/runs would give:

Saved 3 script parameters to: /data/project23/runs/example2_run-2026-04-10--10-50_params.txt

馃く 馃槼

Now, obviously - these are a lot of changes, and they definitely qualify for a separate issue. In my opinion, the main question (apart from discussing the stuff mentioned above) is whether to have this for the 2.0.0 milestone or if this should go into a subsequent release (馃毃 having a high probability of becoming a breaking change and hence requiring a new major release...)?

Originally posted by @ehrenfeu in #140 (comment)

Activity

  1. added theissue type on Apr 10, 2026
  2. moved this to Ready in IMCF-Fijion Apr 10, 2026
  3. added this to the 2.1.0 milestone on Apr 10, 2026
  4. ehrenfeu commented on Apr 10, 2026

    @ehrenfeu
    MemberAuthor

    example2.py

    This should better be written as:

    #@ File(label="Input data", style="directory") INPUT_PATH
    
    from imcflibs.imagej.misc import save_script_parameters
    
    save_script_parameters(script_globals=globals(), location_param="INPUT_PATH")

    in order to prevent the impression an additional Script Parameter is required for this solution / approach.
    馃挕Hint: 鉃★笍 it's not.

    Running this with INPUT_PATH=/data/project23 would give:

    Saved 1 script parameters to: /data/project23/example2_run-2026-04-10--14-18_params.txt
    
  5. rohangirishrao commented on Sep 17, 2026

    @rohangirishrao
    Contributor

    (1) It would be nice if the function were to automagically determine whether the parameters should be saved or not. Then it could simply always be called from our scripts and do its service. A reasonable 馃 approach would be to have an empty default value for the target location by using destination="" and skip parameter saving if it's not defined.

    (1) Agreed, easy to implement 馃憤馃徑

    (2) Instead of supplying the saving location explicitly in parameter destination, we could modify it such that the location is defined within the Script Parameters (see illustration example below) using a default parameter name (e.g. OUTPUT_PATH or similar) and only have an optional parameter for save_script_parameters that allows to specify which Script Parameter contains the saving location if it differs from the standard.

    (2) I'm not so sure this is a super helpful way of doing this, as this assumes that the script parameter path name will be known - e.g. many scripts use RESULTS_DIR as the output path, which would require passing the this path anyway. The added complexity of reading through all parameters, parsing the output path, and then opening the file to write to might be a bit overkill, especially as there is no real consistency across scripts as to the naming of the output path. Of course, this might introduce that consistency..

    (3) To achieve this, processing 鈿欙笍 of the Script Parameters in save_script_parameters would need to be modified such that it works through the items obtained via script_globals and assembles a list of things 馃搼 to be saved while determining the saving location 馃捑 at the same time - only using open(out_path, "w") as f: after all Script Parameters have been processed (as only then out_path would be known).

    (3) Same logic as above, linked to (2).

    (4) We should consider to construct the file name used to save the parameters and values automatically, e.g. following this pattern: <calling_script_name>_run-<timestamp>_params.txt - this would allow for saving the parameters at each run without having to worry (too much) about overwriting previously saved ones. For hints on how to identify the name of the calling script, this SO thread might contain useful ideas - 馃毃 careful though, it's pretty old and might not work the same way in ImageJ-Jython! 鈽濔煆糡he .py suffix should be stripped from the pattern.

    (4) Also makes a lot of sense, timestamp is easy, but the script name might be trickier. I think script_info.getLocation might do the trick.

    (5) In case saving the parameters in the OUTPUT_PATH location suggested above fails (location might be read-only, ...), falling back to e.g. the user's home directory would be nice (plus issuing a 鈿狅笍 warning message obviously).

    (5) is mostly moot if we do not implement 2./3., as the results directory set by the user should always be writable? We can wrap it in a try/except for the writing and opening file part, so that it fails gracefully if there are permission or path issues.

    edited by @ehrenfeu to include the referenced sections for easier understanding

  6. rohangirishrao commented on Sep 30, 2026

    @rohangirishrao
    Contributor

    This discussion should continue in PR #156 where some of the changes suggested have already been implemented!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

enhancementNew feature or requestunit testingA unit test should be created

Projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions