#!/usr/bin/env python
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
from __future__ import print_function # Python 2/3 compatibility
__doc__ = """
Examples of design matrices specification and and computation (event-related
design, FIR design, etc)

Requires matplotlib

Author : Bertrand Thirion: 2009-2010
"""
print(__doc__)

import numpy as np

try:
    import matplotlib.pyplot as plt
except ImportError:
    raise RuntimeError("This script needs the matplotlib library")

from nipy.modalities.fmri.design_matrix import make_dmtx
from nipy.modalities.fmri.experimental_paradigm import (EventRelatedParadigm,
                                                        BlockParadigm)

# frame times
tr = 1.0
nscans = 128
frametimes = np.linspace(0, (nscans - 1) * tr, nscans)

# experimental paradigm
conditions = ['c0', 'c0', 'c0', 'c1', 'c1', 'c1', 'c3', 'c3', 'c3']
onsets = [30, 70, 100, 10, 30, 90, 30, 40, 60]
hrf_model = 'canonical'
motion = np.cumsum(np.random.randn(128, 6), 0)
add_reg_names = ['tx', 'ty', 'tz', 'rx', 'ry', 'rz']

#event-related design matrix
paradigm = EventRelatedParadigm(conditions, onsets)

X1 = make_dmtx(
    frametimes, paradigm, drift_model='polynomial', drift_order=3,
    add_regs=motion, add_reg_names=add_reg_names)

# block design matrix
duration = 7 * np.ones(9)
paradigm = BlockParadigm(con_id=conditions, onset=onsets,
                             duration=duration)

X2 = make_dmtx(frametimes, paradigm, drift_model='polynomial',
               drift_order=3)

# FIR model
paradigm = EventRelatedParadigm(conditions, onsets)
hrf_model = 'FIR'
X3 = make_dmtx(frametimes, paradigm, hrf_model='fir',
               drift_model='polynomial', drift_order=3,
               fir_delays=np.arange(1, 6))

# plot the results
fig = plt.figure(figsize=(10, 6))
ax = plt.subplot(1, 3, 1)
X1.show(ax=ax)
ax.set_title('Event-related design matrix', fontsize=12)
ax = plt.subplot(1, 3, 2)
X2.show(ax=ax)
ax.set_title('Block design matrix', fontsize=12)
ax = plt.subplot(1, 3, 3)
X3.show(ax=ax)
ax.set_title('FIR design matrix', fontsize=12)
plt.subplots_adjust(top=0.9, bottom=0.25)
plt.show()

[((29, 13, 29, 54), 'numpy.linspace', 'np.linspace', ({(29, 25, 29, 26): '0', (29, 28, 29, 45): '(nscans - 1) * tr', (29, 47, 29, 53): 'nscans'}, {}), '(0, (nscans - 1) * tr, nscans)', True, 'import numpy as np\n'), ((39, 11, 39, 51), 'nipy.modalities.fmri.experimental_paradigm.EventRelatedParadigm', 'EventRelatedParadigm', ({(39, 32, 39, 42): 'conditions', (39, 44, 39, 50): 'onsets'}, {}), '(conditions, onsets)', False, 'from nipy.modalities.fmri.experimental_paradigm import EventRelatedParadigm, BlockParadigm\n'), ((41, 5, 43, 49), 'nipy.modalities.fmri.design_matrix.make_dmtx', 'make_dmtx', (), '', False, 'from nipy.modalities.fmri.design_matrix import make_dmtx\n'), ((47, 11, 48, 47), 'nipy.modalities.fmri.experimental_paradigm.BlockParadigm', 'BlockParadigm', (), '', False, 'from nipy.modalities.fmri.experimental_paradigm import EventRelatedParadigm, BlockParadigm\n'), ((50, 5, 51, 29), 'nipy.modalities.fmri.design_matrix.make_dmtx', 'make_dmtx', (), '', False, 'from nipy.modalities.fmri.design_matrix import make_dmtx\n'), ((54, 11, 54, 51), 'nipy.modalities.fmri.experimental_paradigm.EventRelatedParadigm', 'EventRelatedParadigm', ({(54, 32, 54, 42): 'conditions', (54, 44, 54, 50): 'onsets'}, {}), '(conditions, onsets)', False, 'from nipy.modalities.fmri.experimental_paradigm import EventRelatedParadigm, BlockParadigm\n'), ((61, 6, 61, 33), 'matplotlib.pyplot.figure', 'plt.figure', (), '', True, 'import matplotlib.pyplot as plt\n'), ((62, 5, 62, 25), 'matplotlib.pyplot.subplot', 'plt.subplot', ({(62, 17, 62, 18): '1', (62, 20, 62, 21): '3', (62, 23, 62, 24): '1'}, {}), '(1, 3, 1)', True, 'import matplotlib.pyplot as plt\n'), ((65, 5, 65, 25), 'matplotlib.pyplot.subplot', 'plt.subplot', ({(65, 17, 65, 18): '1', (65, 20, 65, 21): '3', (65, 23, 65, 24): '2'}, {}), '(1, 3, 2)', True, 'import matplotlib.pyplot as plt\n'), ((68, 5, 68, 25), 'matplotlib.pyplot.subplot', 'plt.subplot', ({(68, 17, 68, 18): '1', (68, 20, 68, 21): '3', (68, 23, 68, 24): '3'}, {}), '(1, 3, 3)', True, 'import matplotlib.pyplot as plt\n'), ((71, 0, 71, 41), 'matplotlib.pyplot.subplots_adjust', 'plt.subplots_adjust', (), '', True, 'import matplotlib.pyplot as plt\n'), ((72, 0, 72, 10), 'matplotlib.pyplot.show', 'plt.show', ({}, {}), '()', True, 'import matplotlib.pyplot as plt\n'), ((35, 19, 35, 42), 'numpy.random.randn', 'np.random.randn', ({(35, 35, 35, 38): '128', (35, 40, 35, 41): '6'}, {}), '(128, 6)', True, 'import numpy as np\n'), ((46, 15, 46, 25), 'numpy.ones', 'np.ones', ({(46, 23, 46, 24): '(9)'}, {}), '(9)', True, 'import numpy as np\n'), ((58, 26, 58, 41), 'numpy.arange', 'np.arange', ({(58, 36, 58, 37): '1', (58, 39, 58, 40): '6'}, {}), '(1, 6)', True, 'import numpy as np\n')]