Skip to content

Commit 7c031bf

Browse files
authored
update runner.sh (#1157)
Signed-off-by: Wenqi Li <wenqil@nvidia.com> - fixes function check_installed - fixes mixture of tab and white space ### Checks <!--- Put an `x` in all the boxes that apply, and remove the not applicable items --> - [x] Avoid including large-size files in the PR. - [x] Clean up long text outputs from code cells in the notebook. - [x] For security purposes, please check the contents and remove any sensitive info such as user names and private key. - [x] Ensure (1) hyperlinks and markdown anchors are working (2) use relative paths for tutorial repo files (3) put figure and graphs in the `./figure` folder - [ ] Notebook runs automatically `./runner.sh -t <path to .ipynb file>` Signed-off-by: Wenqi Li <wenqil@nvidia.com>
1 parent e5a1876 commit 7c031bf

File tree

1 file changed

+136
-136
lines changed

1 file changed

+136
-136
lines changed

runner.sh

Lines changed: 136 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ function print_usage {
125125
echo " -f, --failfast : stop on first error"
126126
echo " -p, --pattern : pattern of files to be run (added to \`find . -type f -name *.ipynb -and ! -wholename *.ipynb_checkpoints*\`)"
127127
echo " -h, --help : show this help message and exit"
128-
echo " -t, --test : shortcut to run a single notebook using pattern `-and -wholename`"
128+
echo " -t, --test : shortcut to run a single notebook using pattern \`-and -wholename\`"
129129
echo " -v, --version : show MONAI and system version information and exit"
130130
echo ""
131131
echo "Examples:"
@@ -206,35 +206,35 @@ done
206206

207207
# if failfast, exit returning code. else increment number of failed tests and continue
208208
function test_fail {
209-
print_style_fail_msg
210-
if [ $failfast = true ]; then
211-
exit $1
212-
fi
213-
current_test_successful=1
209+
print_style_fail_msg
210+
if [ $failfast = true ]; then
211+
exit $1
212+
fi
213+
current_test_successful=1
214214
}
215215

216216
function check_installed {
217-
set +e
218-
command -v $1 &>/dev/null
219-
set -e
220-
success=$?
221-
if [ ${success} -ne 0 ]; then
222-
print_error_msg "Missing package: $1 (try pip install -r requirements.txt)"
223-
exit $success
224-
fi
217+
set +e
218+
command -v $1 &>/dev/null
219+
success=$?
220+
set -e
221+
if [ ${success} -ne 0 ]; then
222+
print_error_msg "Missing package: $1 (trying pip install -r requirements.txt)"
223+
${PY_EXE} -m pip install -r requirements.txt
224+
fi
225225
}
226226

227227
# check that packages are installed
228228
if [ $doRun = true ]; then
229-
check_installed papermill
229+
check_installed papermill
230230
fi
231231
if [ $doChecks = true ]; then
232-
check_installed jupytext
233-
check_installed flake8
234-
if [ $autofix = true ]; then
235-
check_installed autopep8
236-
check_installed autoflake
237-
fi
232+
check_installed jupytext
233+
check_installed flake8
234+
if [ $autofix = true ]; then
235+
check_installed autopep8
236+
check_installed autoflake
237+
fi
238238
fi
239239

240240
function verify_notebook_has_key_in_cell() {
@@ -294,23 +294,23 @@ base_path="$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
294294
cd "${base_path}"
295295

296296
function replace_text {
297-
oldString="${s}\s*=\s*[0-9]\+"
298-
newString="${s} = 1"
297+
oldString="${s}\s*=\s*[0-9]\+"
298+
newString="${s} = 1"
299299

300-
before=$(echo "$notebook" | grep "$oldString")
301-
[ ! -z "$before" ] && echo Before: && echo "$before"
300+
before=$(echo "$notebook" | grep "$oldString")
301+
[ ! -z "$before" ] && echo Before: && echo "$before"
302302

303-
notebook=$(echo "$notebook" | sed "s/$oldString/$newString/g")
303+
notebook=$(echo "$notebook" | sed "s/$oldString/$newString/g")
304304

305-
after=$(echo "$notebook" | grep "$newString")
306-
[ ! -z "$after" ] && echo After: && echo "$after"
305+
after=$(echo "$notebook" | grep "$newString")
306+
[ ! -z "$after" ] && echo After: && echo "$after"
307307
}
308308

309309
# Get notebooks (pattern is an empty string unless the user specifies otherwise)
310310
files=($(echo $pattern | xargs find . -type f -name "*.ipynb" -and ! -wholename "*.ipynb_checkpoints*"))
311311
if [[ $files == "" ]]; then
312-
print_error_msg "No files match pattern"
313-
exit 0
312+
print_error_msg "No files match pattern"
313+
exit 0
314314
fi
315315
echo "Notebook files to be tested:"
316316
for i in "${files[@]}"; do echo $i; done
@@ -320,14 +320,14 @@ num_successful_tests=0
320320
num_tested=0
321321
# on finish
322322
function finish {
323-
if [[ ${num_successful_tests} -eq ${num_tested} ]]; then
324-
echo -e "\n\n\n${green}Testing finished. All ${num_tested} executed tests passed!${noColor}"
325-
else
326-
echo -e "\n\n\n${red}Testing finished. ${num_successful_tests} of ${num_tested} executed tests passed!${noColor}"
327-
fi
328-
# notification
329-
echo -e "\a"
330-
exit $((num_tested - num_successful_tests))
323+
if [[ ${num_successful_tests} -eq ${num_tested} ]]; then
324+
echo -e "\n\n\n${green}Testing finished. All ${num_tested} executed tests passed!${noColor}"
325+
else
326+
echo -e "\n\n\n${red}Testing finished. ${num_successful_tests} of ${num_tested} executed tests passed!${noColor}"
327+
fi
328+
# notification
329+
echo -e "\a"
330+
exit $((num_tested - num_successful_tests))
331331
}
332332
trap finish EXIT
333333

@@ -340,102 +340,102 @@ set +e
340340
# #
341341
########################################################################
342342
for file in "${files[@]}"; do
343-
current_test_successful=0
344-
345-
echo "${separator}${blue}Running $file${noColor}"
346-
347-
# Get to file's folder and get file contents
348-
path="$(dirname "${file}")"
349-
filename="$(basename "${file}")"
350-
cd ${base_path}/${path}
351-
352-
########################################################################
353-
# #
354-
# code checks #
355-
# #
356-
########################################################################
357-
if [ $doChecks = true ]; then
358-
359-
if [ $autofix = true ]; then
360-
echo Applying autofixes...
361-
jupytext "$filename" --opt custom_cell_magics="writefile" \
362-
--pipe "autoflake --in-place --remove-unused-variables --imports numpy,monai,matplotlib,torch,ignite {}" \
363-
--pipe "autopep8 - --ignore W291 --max-line-length 120" \
364-
--pipe "sed 's/ = list()/ = []/'"
365-
fi
366-
367-
# to check flake8, convert to python script, don't check
368-
# magic cells, and don't check line length for comment
369-
# lines (as this includes markdown), and then run flake8
370-
echo Checking PEP8 compliance...
371-
jupytext "$filename" --opt custom_cell_magics="writefile" -w --to script -o - | \
372-
sed 's/\(^\s*\)%/\1pass # %/' | \
373-
sed 's/\(^#.*\)$/\1 # noqa: E501/' | \
374-
flake8 - --show-source --max-line-length 120
375-
success=$?
376-
if [ ${success} -ne 0 ]
377-
then
378-
print_error_msg "Try running with autofixes: ${green}--autofix${noColor}"
379-
test_fail ${success}
380-
fi
381-
fi
382-
383-
########################################################################
384-
# #
385-
# run notebooks with papermill #
386-
# #
387-
########################################################################
388-
if [ $doRun = true ]; then
389-
390-
skipRun=false
391-
392-
for skip_pattern in "${skip_run_papermill[@]}"; do
393-
echo "$skip_pattern"
394-
if [[ $file =~ $skip_pattern ]]; then
395-
echo "Skip Pattern Match"
396-
skipRun=true
397-
break
398-
fi
399-
done
400-
401-
if [ $skipRun = true ]; then
402-
echo "Skipping"
403-
continue
404-
fi
405-
406-
echo Running notebook...
407-
notebook=$(cat "$filename")
408-
409-
# if compulsory keyword, max_epochs, missing...
410-
if [[ ! "$notebook" =~ "max_epochs" ]]; then
411-
# and notebook isn't in list of those expected to not have that keyword...
412-
should_contain_max_epochs=true
413-
for e in "${doesnt_contain_max_epochs[@]}"; do
414-
[[ "$e" == "$filename" ]] && should_contain_max_epochs=false && break
415-
done
416-
# then error
417-
if [[ $should_contain_max_epochs == true ]]; then
418-
print_error_msg "Couldn't find the keyword \"max_epochs\", and the notebook wasn't on the list of expected exemptions (\"doesnt_contain_max_epochs\")."
419-
test_fail 1
420-
fi
421-
fi
422-
423-
# Set some variables to 1 to speed up proceedings
424-
strings_to_replace=(max_epochs val_interval disc_train_interval disc_train_steps num_batches_for_histogram)
425-
for s in "${strings_to_replace[@]}"; do
426-
replace_text
427-
done
428-
429-
python -c 'import monai; monai.config.print_config()'
430-
time out=$(echo "$notebook" | papermill --progress-bar -k "$kernelspec")
431-
success=$?
432-
if [[ ${success} -ne 0 || "$out" =~ "\"status\": \"failed\"" ]]; then
433-
test_fail ${success}
434-
fi
435-
fi
436-
437-
num_tested=$((num_tested + 1))
438-
if [[ ${current_test_successful} -eq 0 ]]; then
439-
num_successful_tests=$((num_successful_tests + 1))
440-
fi
343+
current_test_successful=0
344+
345+
echo "${separator}${blue}Running $file${noColor}"
346+
347+
# Get to file's folder and get file contents
348+
path="$(dirname "${file}")"
349+
filename="$(basename "${file}")"
350+
cd ${base_path}/${path}
351+
352+
########################################################################
353+
# #
354+
# code checks #
355+
# #
356+
########################################################################
357+
if [ $doChecks = true ]; then
358+
359+
if [ $autofix = true ]; then
360+
echo Applying autofixes...
361+
jupytext "$filename" --opt custom_cell_magics="writefile" \
362+
--pipe "autoflake --in-place --remove-unused-variables --imports numpy,monai,matplotlib,torch,ignite {}" \
363+
--pipe "autopep8 - --ignore W291 --max-line-length 120" \
364+
--pipe "sed 's/ = list()/ = []/'"
365+
fi
366+
367+
# to check flake8, convert to python script, don't check
368+
# magic cells, and don't check line length for comment
369+
# lines (as this includes markdown), and then run flake8
370+
echo Checking PEP8 compliance...
371+
jupytext "$filename" --opt custom_cell_magics="writefile" -w --to script -o - | \
372+
sed 's/\(^\s*\)%/\1pass # %/' | \
373+
sed 's/\(^#.*\)$/\1 # noqa: E501/' | \
374+
flake8 - --show-source --max-line-length 120
375+
success=$?
376+
if [ ${success} -ne 0 ]
377+
then
378+
print_error_msg "Try running with autofixes: ${green}--autofix${noColor}"
379+
test_fail ${success}
380+
fi
381+
fi
382+
383+
########################################################################
384+
# #
385+
# run notebooks with papermill #
386+
# #
387+
########################################################################
388+
if [ $doRun = true ]; then
389+
390+
skipRun=false
391+
392+
for skip_pattern in "${skip_run_papermill[@]}"; do
393+
echo "$skip_pattern"
394+
if [[ $file =~ $skip_pattern ]]; then
395+
echo "Skip Pattern Match"
396+
skipRun=true
397+
break
398+
fi
399+
done
400+
401+
if [ $skipRun = true ]; then
402+
echo "Skipping"
403+
continue
404+
fi
405+
406+
echo Running notebook...
407+
notebook=$(cat "$filename")
408+
409+
# if compulsory keyword, max_epochs, missing...
410+
if [[ ! "$notebook" =~ "max_epochs" ]]; then
411+
# and notebook isn't in list of those expected to not have that keyword...
412+
should_contain_max_epochs=true
413+
for e in "${doesnt_contain_max_epochs[@]}"; do
414+
[[ "$e" == "$filename" ]] && should_contain_max_epochs=false && break
415+
done
416+
# then error
417+
if [[ $should_contain_max_epochs == true ]]; then
418+
print_error_msg "Couldn't find the keyword \"max_epochs\", and the notebook wasn't on the list of expected exemptions (\"doesnt_contain_max_epochs\")."
419+
test_fail 1
420+
fi
421+
fi
422+
423+
# Set some variables to 1 to speed up proceedings
424+
strings_to_replace=(max_epochs val_interval disc_train_interval disc_train_steps num_batches_for_histogram)
425+
for s in "${strings_to_replace[@]}"; do
426+
replace_text
427+
done
428+
429+
python -c 'import monai; monai.config.print_config()'
430+
time out=$(echo "$notebook" | papermill --progress-bar -k "$kernelspec")
431+
success=$?
432+
if [[ ${success} -ne 0 || "$out" =~ "\"status\": \"failed\"" ]]; then
433+
test_fail ${success}
434+
fi
435+
fi
436+
437+
num_tested=$((num_tested + 1))
438+
if [[ ${current_test_successful} -eq 0 ]]; then
439+
num_successful_tests=$((num_successful_tests + 1))
440+
fi
441441
done

0 commit comments

Comments
 (0)