import pylab
import numpy as N

from neuroimaging.core.image.image import Image
from neuroimaging.ui.visualization.viewer import BoxViewer

import keith

def nipy_run(subject=3, run=3, which='contrasts', contrast='overall',
             stat='t', **extra):

    runfile = '/home/analysis/FIAC/fiac%d/fonc%d/fsl/fmristat_run/%s/%s/%s.img' % (subject, run, which, contrast, stat)

    return Image(runfile)


def nipy_rho(subject=3, run=3):

    runfile = '/home/analysis/FIAC/fiac%d/fonc%d/fsl/fmristat_run/rho.img' % (subject, run)

    return Image(runfile)

def mask(subject=3, run=3, **kw):
    runfile = '/home/analysis/FIAC/fiac%d/fonc%d/fsl/mask.img' % (subject, run)

    return Image(runfile)

import optparse

parser = optparse.OptionParser()

parser.add_option('', '--which', help='contrasts or delays', dest='which',
                      default='contrasts')
parser.add_option('', '--run', help='which run?', dest='run', default=1,
                  type='int')
parser.add_option('', '--subject', help='which subject?', dest='subject',
                  default=0, type='int')
parser.add_option('', '--stat', help='t, sd or effect?', dest='stat', default='t')
parser.add_option('', '--contrast', help='overall, sentence, speaker, or interaction', dest='contrast', default='overall')
parser.add_option('', '--vmin', help='min for colorbar', dest='m', type='float')
parser.add_option('', '--vmax', help='max for colorbar', dest='M', type='float')
parser.add_option('', '--rho', help='compare AR(1) coefs', dest='rho',
                  action='store_true', default=False)

options, args = parser.parse_args()
options = parser.values.__dict__

print options

if not parser.values.rho:
    x = nipy_run(**options)
    y = keith.run(**options)
else:
    x = nipy_rho(**options)
    y = keith.rho(**options)
    
m = mask(**options)

vx = BoxViewer(x, mask=m, colormap='spectral')
vy = BoxViewer(y, mask=m, colormap='spectral')

if options['m'] is not None:
    vx.m = options['m']
    vy.m = options['m']

if options['M'] is not None:
    vx.M = options['M']
    vy.M = options['M']

vx.draw()
vy.draw()

X = x.readall() * m.readall()
X.shape = N.product(X.shape)

Y = y.readall() * m.readall()
Y.shape = N.product(Y.shape)

print N.corrcoef(X, Y)

pylab.show()

@@ -147,16 +147,46 @@
  keith%0A%0A
+path = %22/home/analysis/FIAC%22%0A%0A
 def nipy
@@ -296,35 +296,18 @@
 file = '
-/home/analysis/FIAC
+%25s
 /fiac%25d/
@@ -339,32 +339,38 @@
 s/%25s/%25s.img' %25 (
+path, 
 subject, run, wh
@@ -467,35 +467,18 @@
 file = '
-/home/analysis/FIAC
+%25s
 /fiac%25d/
@@ -505,32 +505,38 @@
 run/rho.img' %25 (
+path, 
 subject, run)%0A%0A 
@@ -610,35 +610,21 @@
 file = '
-/home/analysis/FIAC
+%25path
 /fiac%25d/
@@ -647,16 +647,22 @@
 img' %25 (
+path, 
 subject,
@@ -1664,20 +1664,16 @@
 ons%0A%0Aif 
-not 
 parser.v
@@ -1697,18 +1697,18 @@
 = nipy_r
-un
+ho
 (**optio
@@ -1726,18 +1726,18 @@
  keith.r
-un
+ho
 (**optio
@@ -1752,34 +1752,34 @@
 :%0A    x = nipy_r
-ho
+un
 (**options)%0A    
@@ -1781,34 +1781,34 @@
 %0A    y = keith.r
-ho
+un
 (**options)%0A    

add path variable and reorder some things to be easier on the brain