Skip to content

Commit 42a6e9f

Browse files
authored
Merge pull request #85 from miykael/initiate_cast_examples
Adds datalad's terminal cast routine - allows interactive nipype intro
2 parents 80b9358 + 7674c5b commit 42a6e9f

File tree

3 files changed

+228
-0
lines changed

3 files changed

+228
-0
lines changed

casts/cast_ipython.rc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# This file contains ipython configuration variables to be used for generating
2+
# asciinema demos to guarantee consistent appearance.
3+
4+
# make a fake temporary home dir and go into it
5+
SCREENCAST_HOME=~/demo
6+
if [ ! -e "$SCREENCAST_HOME" ]; then
7+
mkdir -p ${SCREENCAST_HOME} || {
8+
echo "FAILED to create $SCREENCAST_HOME" >&2
9+
exit 1; # we need demo directory!
10+
}
11+
fi
12+
cd $SCREENCAST_HOME
13+
ipython
14+
15+
# cleanup at the end
16+
trap "cd ; rm -rf ~/demo > /dev/null 2>&1" EXIT

casts/cast_live_python

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#!/bin/bash
2+
#
3+
set -u -e
4+
5+
test ! -e $1 && echo "input file does not exist" && exit 1
6+
title="$(echo $(basename $1) | sed -e 's/.sh$//')"
7+
bashrc_file="$(dirname $0)/cast_ipython.rc"
8+
9+
# shortcut for making xdotool use the right window
10+
function xdt() {
11+
winid=$1
12+
shift
13+
xdotool windowactivate --sync $winid
14+
if [ "$#" -gt 0 ]; then
15+
xdotool "$@"
16+
fi
17+
}
18+
19+
# make sure the target xterm is up and running
20+
width=106
21+
height=29
22+
fs=15
23+
text_width=$(($width - 8))
24+
25+
geometry=${width}x${height}
26+
this_window=$(xdotool getwindowfocus)
27+
28+
# For consistent appearance
29+
xterm +sb -fa Hermit -fs $fs -bg black -fg white -geometry $geometry -title Screencast-xterm -e "bash --rcfile cast_ipython.rc" &
30+
xterm_pid=$!
31+
sleep 2
32+
33+
xterm_window=$(xdotool search --pid $xterm_pid)
34+
35+
# By default should stay in the xterm window, so when we need to deal with
36+
# current one (waiting etc), then switch
37+
function wait () {
38+
xdt $this_window
39+
read -p "$@" in
40+
echo "$in"
41+
xdt $xterm_window
42+
}
43+
function instruct () {
44+
xdt $this_window
45+
wait "$@"
46+
}
47+
function type () {
48+
xdt $xterm_window type --clearmodifiers --delay 40 "$1"
49+
}
50+
function key () {
51+
xdt $xterm_window key --clearmodifiers $*
52+
}
53+
function sleep () {
54+
xdotool sleep $1
55+
}
56+
function execute () {
57+
xdt $xterm_window sleep 0.5 key Return
58+
sleep 0.2
59+
}
60+
function say()
61+
{
62+
ac=$(instruct "SAY: $1")
63+
if [ "$ac" != "s" ] ; then
64+
echo "skipping"
65+
return
66+
fi
67+
type "$(printf "#\n# $1" | fmt -w ${text_width} --prefix '# ')"
68+
key Return
69+
}
70+
function show () {
71+
xdt $xterm_window type --clearmodifiers --delay 10 "$(printf "\n$1" | sed -e 's/^/# /g')"
72+
sleep 0.1
73+
key Return
74+
}
75+
function run () {
76+
help="Press Enter to type, s to skip this action"
77+
ac=$(instruct "EXEC: $1. $help")
78+
if [ "$ac" = "s" ]; then
79+
echo "skipping"
80+
return
81+
fi
82+
type "$1"
83+
ac=$(instruct "EXEC: $1. $help")
84+
if [ "$ac" = "s" ]; then
85+
echo "skipping"
86+
return
87+
fi
88+
execute
89+
}
90+
function run_expfail () {
91+
# TODO we could announce or visualize the expected failure
92+
run "$1"
93+
}
94+
95+
xdt $xterm_window sleep 0.1
96+
97+
echo "xterm PID $xterm_pid (window $xterm_window) this window $this_window"
98+
99+
# now get the process tree attached to the terminal so we can
100+
# figure out when it is idle, and when it is not
101+
# XXX must happen after asciinema is running
102+
xterm_pstree="$(pstree -p -A $xterm_pid)"
103+
104+
. $1
105+
106+
sleep 1
107+
108+
show "$(cowsay "Demo was using $(datalad --version 2>&1 | head -n1). Discover more at http://datalad.org")"
109+
110+
# key Control_L+d
111+
112+
echo "INSTRUCTION: Press Ctrl-D or run exit to close the terminal"

casts/nipype_tutorial_showcase.sh

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
say "Import nipype building blocks"
2+
show "Import nipype building blocks"
3+
run "from nipype import Node, Workflow"
4+
5+
say "Import relevant interfaces"
6+
show "Import relevant interfaces"
7+
run "from nipype.interfaces.fsl import SliceTimer, MCFLIRT, Smooth"
8+
9+
say "Create SliceTime correction node"
10+
show "Create SliceTime correction node"
11+
run "slicetimer = Node(SliceTimer(index_dir=False,
12+
interleaved=True,
13+
time_repetition=2.5),
14+
name='slicetimer')
15+
"
16+
17+
say "Create Motion correction node"
18+
show "Create Motion correction node"
19+
run "mcflirt = Node(MCFLIRT(mean_vol=True,
20+
save_plots=True),
21+
name='mcflirt')
22+
"
23+
24+
say "Create Smoothing node"
25+
show "Create Smoothing node"
26+
run "smooth = Node(Smooth(fwhm=4), name='smooth')"
27+
28+
say "Create Workflow"
29+
show "Create Workflow"
30+
run "preproc01 = Workflow(name='preproc01', base_dir='.')"
31+
32+
say "Connect nodes within the workflow"
33+
show "Connect nodes within the workflow"
34+
run "preproc01.connect([(slicetimer, mcflirt, [('slice_time_corrected_file', 'in_file')]),
35+
(mcflirt, smooth, [('out_file', 'in_file')])])
36+
"
37+
38+
say "Create a visualization of the workflow"
39+
show "Create a visualization of the workflow"
40+
run "preproc01.write_graph(graph2use='orig')"
41+
42+
say "Visualize the figure"
43+
show "Visualize the figure"
44+
run "!eog preproc01/graph_detailed.png
45+
"
46+
47+
say "Feed some input to the workflow"
48+
show "Feed some input to the workflow"
49+
run "slicetimer.inputs.in_file = 'path/to/your/func.nii.gz'"
50+
51+
say "Run the Workflow and stop the time"
52+
show "Run the Workflow and stop the time"
53+
run "%time preproc01.run('MultiProc', plugin_args={'n_procs': 5})"
54+
55+
say "Investigate the output"
56+
show "Investigate the output"
57+
run "!tree preproc01 -I '*js|*json|*pklz|_report|*.dot|*html'"
58+
59+
say "Change the size of the smoothing kernel"
60+
show "Change the size of the smoothing kernel"
61+
run "smooth.inputs.fwhm = 2"
62+
63+
say "Rerun the workflow"
64+
show "Rerun the workflow"
65+
run "%time preproc01.run('MultiProc', plugin_args={'n_procs': 5})"
66+
67+
say "Create 4 additional copies of the workflow"
68+
show "Create 4 additional copies of the workflow"
69+
run "preproc02 = preproc01.clone('preproc02')
70+
preproc03 = preproc01.clone('preproc03')
71+
preproc04 = preproc01.clone('preproc04')
72+
preproc05 = preproc01.clone('preproc05')
73+
"
74+
75+
say "Create a new workflow - metaflow"
76+
show "Create a new workflow - metaflow"
77+
run "metaflow = Workflow(name='metaflow', base_dir='.')"
78+
79+
say "Add the 5 workflows to this metaflow"
80+
show "Add the 5 workflows to this metaflow"
81+
run "metaflow.add_nodes([preproc01, preproc02, preproc03,
82+
preproc04, preproc05])
83+
"
84+
85+
say "Visualize the workflow"
86+
show "Visualize the workflow"
87+
run "metaflow.write_graph(graph2use='flat')
88+
!eog metaflow/graph_detailed.png
89+
"
90+
91+
say "Run this metaflow in parallel"
92+
show "Run this metaflow in parallel"
93+
run "%time metaflow.run('MultiProc', plugin_args={'n_procs': 5})"
94+
95+
say "Investigate the output"
96+
show "Investigate the output"
97+
run "!tree metaflow -I '*js|*json|*pklz|_report|*.dot|*html'"
98+
99+
say "The End."
100+
show "The End."

0 commit comments

Comments
 (0)