Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

--fix-only option for hcp_fix_multi_run #297

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open

--fix-only option for hcp_fix_multi_run #297

wants to merge 8 commits into from

Conversation

demsarjure
Copy link
Contributor

This feature implements a new parameter (--fix-only) for hcp_fix_multi_run. This parameter can be used when to speed up ICAFix optimizations by skipping melodic and proceed with FIX on a previously computed ICA solution.

ICAFIX/hcp_fix_multi_run Outdated Show resolved Hide resolved
ls -lRa ${concatfmrihp}.ica
fi
if [ -d "${concatfmrihp}.ica/fix" ]; then
mv "${concatfmrihp}.ica/fix" "${concatfmrihp}.ica/fix-bkp-$(date +"%Y-%m-%d.%H%M%S")"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want the pipeline to internally make a backup? Granted, this option is a special case, but we generally don't like making backups the user doesn't know about.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Backing up previous solution is important in the process of iteratively building and evaluating FIX models, which is the use case that this option is made for. We could add another optional parameter to give the user more control, but I suspect that whoever would use --fix-only would also want to have the previous fix solutions backed up. To make the backup explicit, I have now added a message in the log (it will be in the next PR):

        if [ -d "${concatfmrihp}.ica/fix" ]; then
		fix_backup_suffix="-bkp-$(date +"%Y-%m-%d.%H%M%S")"
		mv "${concatfmrihp}.ica/fix" "${concatfmrihp}.ica/fix${fix_backup_suffix}"
		log_Msg "previous fix solution found and renamed to fix${fix_backup_suffix}"
	fi

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps that behavior should be a separate option?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@glasserm, we can make it a separate option, but I suspect it will be always used in the use cases for --skip-ica.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, the option only makes sense when using --skip-ica as with a new ica the old fix does not refer to the same components any more.

Copy link
Member

@coalsont coalsont Aug 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@demsarjure @sivcek Can you add a --backup-fix-if-ica-skipped boolean option that defaults to true? Or, we could make a more general --backup-existing-fix option that defaults to false, and also works when not using --skip-ica.

Edit: oh right, with a new ICA, a backup of fix may not be very useful, so it should probably stay specific to this new option.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happened to @coalsont's previous suggestion for an alternative name to --skip-ica? I don't recall exactly what it was, but I do recall that he made a good argument for his alternative name.

Copy link
Member

@coalsont coalsont Aug 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or, to make it more scriptable, it could be something like --fix-backup-suffix=<string> or --fix-backup-folder=<fix-bkp-something>, doesn't make a backup if the value is "", otherwise names the backup folder accordingly, rather than generating it from date. The second form could even take an absolute path, or be used to put all the backups into a backups/ subfolder instead of littering the ${concatfmrihp}.ica folder directly.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, found it. It was --reuse-existing-ica:
#297 (comment)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mharms I had resolved it as somewhat minor and we seemed close to merging, but I have unresolved it again since you seem to have an opinion on it.

@@ -124,6 +128,8 @@ opts_AddConfigOptional '--icadim-mode' 'icadimmode' 'icadimmode' '"default" or "

opts_AddOptional '--processing-mode' 'ProcessingMode' '"HCPStyleData" (default) or "LegacyStyleData"' "controls whether --icadim-mode=fewtimepoints is allowed" 'HCPStyleData'

opts_AddOptional '--fix-only' 'RunFIXOnly' 'TRUE or FALSE' 'If TRUE, it skips running ICA using melodic and proceeds directly with running FIX. A previous ICA solution has to exist!' 'FALSE'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider whether --skip-ica or --reuse-existing-ica or something might be a better name. "Only" implies that it can never be combined with other possible process-altering flags (and that if we add another step after fix, --fix-only might be expected to skip it, while --skip-ica would be expected to run it).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that --skip-ica is clearer. I changed the code for the new version of the PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I like the descriptiveness of --reuse-existing-ica, so the user doesn't think they can just skip the ica on the first run. "reuse" has some parallels to the tICA modes, too.

To be clear, please push the commits that make these changes to the same branch on github, and they will show up here. Please don't open an additional PR for changes requested on this PR.

@glasserm
Copy link
Contributor

For some reason this diff is really hard to interpret. Any idea why it is a sea of red and green even though I assume the changes were pretty minor?

@glasserm
Copy link
Contributor

Also, it looks like there are conflicts that need to be resolved.

@coalsont
Copy link
Member

Indentation. In the gear button on the "files changed" page, check "hide whitespace".

Github says no conflicts for squash, but there are some changes from -volume-math/-volume-merge to fslmaths/fslmerge, which I am unsure of the purpose of. If they weren't intentional, something may have been done something with the branch history that could cause a regression (for instance, copying an old version of a file, making edits, and then committing it to the current branch state after a reset).

ICAFIX/hcp_fix_multi_run Outdated Show resolved Hide resolved
Copy link
Contributor

@glasserm glasserm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pull request seems to be making a lot more changes to the code than are actually needed for the requested functionality. It could be that it is reverting changes that were made subsequent to whenever these commits were first made (it appears they are from some months back). We need a pull request based on the latest master.

@sivcek
Copy link
Contributor

sivcek commented Aug 21, 2024

I have committed the version with the changes requested:

  • adding the functionality to the latest master to avoid reverting existing code
  • changing --fix-only to --skip-ica
  • using opts_StringToBool to process the input string
  • adding a log message reporting that an old fix solution was found and backed up

Copy link
Contributor

@glasserm glasserm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks this is much cleaner.

ICAFIX/hcp_fix_multi_run Outdated Show resolved Hide resolved
@coalsont
Copy link
Member

coalsont commented Sep 4, 2024

The unresolved items as I see it are:

  • @glasserm suggested the backup saving should be a separate option, and I suggested having the name of the backup be passed into the script, which is more predictable than being generated from the date/time at which it reached that line in the script.
  • @mharms seconded (I think?) my --reuse-existing-ica suggestion for the flag name

@sivcek
Copy link
Contributor

sivcek commented Nov 30, 2024

This update:

  • merges the current state of master into the PR
  • renames --skip-ica option to --reuse-existing-ica
  • adds --fix-backup option that specifies the folder to which to backup a previous fix solution if one exists

The update is currently being tested.

Comment on lines +863 to 883
AlreadyHP="-1" #Don't run highpass on concatenated data (since hp was done on the individual runs)

case ${MatlabMode} in
0)
#Use compiled Matlab
compiledScript="$HCPPIPEDIR"/ICAFIX/scripts/Compiled_functionhighpassandvariancenormalize/run_functionhighpassandvariancenormalize.sh
matlab_cmd=("$compiledScript" "${MATLAB_COMPILER_RUNTIME}" "$tr" "$AlreadyHP" "$concatfmrihp" "${Caret7_Command}" "" "$volwisharts" "$ciftiwisharts" "$icadimmode")
log_Msg "Run compiled MATLAB with command..."
log_Msg "${matlab_cmd[*]}"
"${matlab_cmd[@]}"
;;

1 | 2)
# Use interpreted MATLAB or Octave
# ${hp} needs to be passed in as a string, to handle the hp=pd* case
matlab_code="${ML_PATHS} functionhighpassandvariancenormalize(${tr}, ${AlreadyHP}, '${concatfmrihp}', '${Caret7_Command}', '', ${volwisharts}, ${ciftiwisharts}, '${icadimmode}');"
log_Msg "Run interpreted MATLAB/Octave (${matlab_interpreter[*]}) with code..."
log_Msg "${matlab_code}"
"${matlab_interpreter[@]}" <<<"$matlab_code"
echo
;;
esac

Dim=$(cat ${concatfmrihp}_dims.txt)

log_Msg "Dim: ${Dim}"

if [[ $ICAmethod == MELODIC ]]; then
## ---------------------------------------------------------------------------
## Run melodic on concatenated file
## ---------------------------------------------------------------------------

log_Debug_Msg "About to run melodic: Contents of ${concatfmrihp}.ica follow"
if [ ! -z "${log_debugOn}" ] ; then
ls -lRa ${concatfmrihp}.ica
fi

#grab melodic from $PATH by default, don't hardcode it with respect to $FSLDIR
#we need to do "if which ..." because the script currently uses ERR trap
if which melodic &> /dev/null
then
MELODIC=$(which melodic 2> /dev/null)
else
#if it isn't even in $PATH, fall back on FSLDIR
MELODIC="${FSLDIR}/bin/melodic"
fi

log_Msg "Running MELODIC located at: $MELODIC"
log_Debug_Msg "Beginning of melodic version log, help, and checksum"
if [ ! -z "${log_debugOn}" ] ; then
log_Debug_Msg "$MELODIC --version"
$MELODIC --version
log_Debug_Msg "$MELODIC --help"
$MELODIC --help
log_Debug_Msg "md5sum $MELODIC"
md5sum $MELODIC
fi
log_Debug_Msg "End of melodic version log, help, and checksum"

# Use the Wishart-based dim estimate instead of the melodic-based estimate (--dim=${Dim})
# Also turn off melodic VN and feed in the input that has already been VN'ed instead (--vn flag, which turns OFF melodic's vn)
# Added "-m ${concatfmri}_brain_mask" to avoid memory issue - Takuya Hayahsi July 2018
melodic_cmd=("${MELODIC}" -i "${concatfmrihp}_vnts" -o "${concatfmrihp}.ica/filtered_func_data.ica" --nobet --report --Oall --tr="${tr}" --vn --dim="${Dim}" -m "${concatfmri}_brain_mask")

if [ ! -z "${log_debugOn}" ] ; then
melodic_cmd+=(--verbose --debug)
fi

#keep the existing handling code for melodic errors?
debug_disable_trap

log_Msg "melodic_cmd: ${melodic_cmd[*]}"
"${melodic_cmd[@]}"
return_code=$?
log_Msg "melodic has been run: return_code = ${return_code}"
log_Debug_Msg "melodic has been run: Contents of ${concatfmrihp}.ica follow"
if [ ! -z "${log_debugOn}" ] ; then
ls -lRa ${concatfmrihp}.ica
fi

if [ "${return_code}" -ne "0" ] ; then
log_Err_Abort "melodic has returned a non-zero code"
else
log_Msg "Skipping ICA and proceeding directly to FIX. All the files generated by ICA have to be present!"

# Set the variables
ConcatFolder=`dirname ${ConcatName}`
ConcatNameNoExt=$($FSLDIR/bin/remove_ext $ConcatName) # No extension, but still includes the directory path
concatfmri=`basename ${ConcatNameNoExt}` # Directory path is now removed
concatfmrihp=${concatfmri}_hp${hp}

# Switch to concat folder
cd ${ConcatFolder}

if [ -d "${concatfmrihp}.ica/fix" ] && [ -n "$FIXBackup" ]; then
pushd "${concatfmrihp}.ica"
mv fix $FIXBackup
popd
log_Msg "Previous fix solution found and moved to ${$FIXBackup}."
fi
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has quite a mix of tabs and spaces.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants