forked from andersonwinkler/PALM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalm
executable file
·129 lines (118 loc) · 4.87 KB
/
palm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/bin/bash
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# PALM -- Permutation Analysis of Linear Models
# Copyright (C) 2015 Anderson M. Winkler
# FMRIB / Univ. of Oxford
# Mar/2014
# http://brainder.org
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# This is a wrapper to call PALM in Octave or Matlab, depending
# on the user's preferences and what is available in the system.
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# =========================================================================
# Do you prefer Matlab or Octave? Octave is free (it doesn't need licences),
# is faster to load, but somewhat slower to run. Matlab uses licenses (this
# can be expensive, or may bother other users in a network if the number of
# licenses available is limited), is slower to load, but runs faster
# once loaded. In the variable below, use 1 for Octave, 2 for Matlab:
WHICH_TO_RUN=1
# If Octave isn't in the path, or to use a specific version, add here the
# path or command that invokes the Octave executable binary.
# This only has effect if WHICH_TO_RUN above os set as 1.
OCTAVEBIN=/usr/bin/octave
#OCTAVEBIN="/usr/bin/flatpak run org.octave.Octave"
# If Matlab isn't in the path, or to use a specific version, add here the
# path or command that invokes the Matlab executable binary.
# This only has effect if WHICH_TO_RUN above os set as 2.
MATLABBIN=/opt/r16b/bin/matlab
# If you are intending to run PALM in a cluster, for some environments
# it may be necessary to change the variable below to 1.
# In this case, also, provide the path to a temporary directory where
# a tiny temporary file can be saved.
IS_CLUSTER=0
TEMP_DIR=/tmp
# =========================================================================
# Normally there is no need to edit below this line:
# Locate PALM
cmdfull=${BASH_SOURCE[0]}
PALMDIR=$(dirname ${cmdfull})
rlink=$(readlink ${cmdfull})
cnt=1
while [[ ${rlink} != "" ]] && [[ ${cnt} -lt 100 ]]; do
dirnam=$(dirname ${rlink})
if [[ ${dirnam:0} == "/" ]] ; then
PALMDIR=${dirnam}
else
PALMDIR="${PALMDIR}/${dirnam}"
fi
rlink=$(readlink ${rlink})
cnt=$(expr ${cnt} + 1)
done
if [[ ${cnt} -eq 100 ]] ; then
echo "Error: Too many levels of symbolic links."
exit 1
fi
# Command to run inside either Octave or Matlab. It varies slightly
# depending whether it's a cluster environment or not.
RUNCMD="addpath('$PALMDIR'); try palm ${1+"$@"} ; catch ME, palm_error(ME), exit(1), end; exit(0)"
if [[ ${IS_CLUSTER} -ne 0 ]] ; then
TEMP_NAME=$(tr -cd '[:alnum:]' < /dev/urandom | fold -w10 | head -n 1)
echo ${RUNCMD} > ${TEMP_DIR}/palm_temp_${TEMP_NAME}.m
RUNCMD="${TEMP_DIR}/palm_temp_${TEMP_NAME}.m"
fi
# Call either Octave or Matlab depending on the user's choice.
set -o pipefail
if [[ ${WHICH_TO_RUN} -eq 1 ]] ; then
if [[ ${OCTAVEBIN} == "" ]] || [[ ! -e $(echo ${OCTAVEBIN} |awk '{print $1}') ]] ; then
OCTAVECMD=octave
else
OCTAVECMD=${OCTAVEBIN}
fi
minver=$(${OCTAVECMD} --version | awk 'NR==1 {print "4.0.0\n" $NF}' | sort -V | head -n 1)
if [[ "${minver}" != "4.0.0" ]] ; then
# Simpler version with no error catching, for Octave < 4.0.0. This will always end
# with a success status, even if there was a crash within the code. The user can still
# for success should they need by verifying that the *_elapsed.csv was created.
RUNCMD="addpath('$PALMDIR'); palm ${1+"$@"} ; exit"
fi
${OCTAVECMD} -q --eval "${RUNCMD}"
status=$?
elif [[ ${WHICH_TO_RUN} -eq 2 ]] ; then
if [[ ${MATLABBIN} == "" ]] || [[ ! -e ${MATLABBIN} ]]; then
MATLABCMD=matlab
else
MATLABCMD=${MATLABBIN}
fi
if [[ "$(uname)" == "Darwin" ]] ; then
SEDOPT="-l"
elif [[ "$(uname)" == "Linux" ]] ; then
SEDOPT="-u"
else
SEDOPT=""
fi
ML_OPTS="-nodesktop -nosplash -singleCompThread"
SED_STR='1,/^.......................................................................$/ d'
if [[ ${IS_CLUSTER} -eq 0 ]] ; then
${MATLABCMD} ${ML_OPTS} -r "${RUNCMD}" | sed ${SEDOPT} -e "$SED_STR"
else
${MATLABCMD} ${ML_OPTS} < "${RUNCMD}" | sed ${SEDOPT} -e "$SED_STR"
fi
status=$?
fi
# If a temp file was created earlier, delete it now and exit.
rm -rf ${TEMP_DIR}/palm_temp_${TEMP_NAME}.m
exit ${status}
# That's it!