Exemplo n.º 1
0
def test_resurrect_model():
    
    # stimulus features
    viewing_distance = 38
    screen_width = 25
    thetas = np.arange(0,360,90)
    thetas = np.insert(thetas,0,-1)
    thetas = np.append(thetas,-1)
    num_blank_steps = 20
    num_bar_steps = 20
    ecc = 10
    tr_length = 1.5
    frames_per_tr = 1.0
    scale_factor = 0.50
    pixels_across = 100
    pixels_down = 100
    dtype = ctypes.c_int16
    
    # create the sweeping bar stimulus in memory
    bar = simulate_bar_stimulus(pixels_across, pixels_down, viewing_distance, screen_width, 
                                thetas, num_bar_steps, num_blank_steps, ecc, clip=0.01)
                                
    # create an instance of the Stimulus class
    stimulus = VisualStimulus(bar, viewing_distance, screen_width, scale_factor, tr_length, dtype)
    
    # set cache grids
    x_grid = utils.grid_slice(-10, 10, 5)
    y_grid = utils.grid_slice(-10, 10, 5)
    s_grid = utils.grid_slice(0.55,5.25, 5)
    grids = (x_grid, y_grid, s_grid,)
    
    # initialize the gaussian model
    model = og.GaussianModel(stimulus, utils.spm_hrf)
    model.hrf_delay = 0
    model.mask_size = 5
    
    # seed rng
    np.random.seed(4932)
    
    # cache the model
    cache = model.cache_model(grids, ncpus=3)
    
    # resurrect cached model
    cached_model_path = '/tmp/og_cached_model.pkl'
    model = og.GaussianModel(stimulus, utils.double_gamma_hrf, cached_model_path=cached_model_path)
    model.hrf_delay = 0
    model.mask_size = 5
    
    # make sure the same
    nt.assert_true(np.sum([c[0] for c in cache] -  model.cached_model_timeseries) == 0)
    nt.assert_true(np.sum([c[1] for c in cache] -  model.cached_model_parameters) == 0)
Exemplo n.º 2
0
    def __init__(self, dm, viewing_distance, screen_width, tr_dm):

        self.stimulus = VisualStimulus(stim_arr=dm,
                                       viewing_distance=viewing_distance,
                                       screen_width=screen_width,
                                       scale_factor=1,
                                       tr_length=tr_dm,
                                       dtype=np.short)

        self.model_func = OGModel.GaussianModel(stimulus=self.stimulus,
                                                hrf_model=utils.spm_hrf)
        self.model_func.hrf_delay = 0
        self.predictions = None
Exemplo n.º 3
0
def test_cache_model_Ns():

    # stimulus features
    viewing_distance = 38
    screen_width = 25
    thetas = np.arange(0,360,90)
    thetas = np.insert(thetas,0,-1)
    thetas = np.append(thetas,-1)
    num_blank_steps = 20
    num_bar_steps = 20
    ecc = 10
    tr_length = 1.5
    frames_per_tr = 1.0
    scale_factor = 0.50
    pixels_across = 100
    pixels_down = 100
    dtype = ctypes.c_int16
    Ns = 5
    
    # create the sweeping bar stimulus in memory
    bar = simulate_bar_stimulus(pixels_across, pixels_down, viewing_distance, screen_width, 
                                thetas, num_bar_steps, num_blank_steps, ecc, clip=0.01)
                                
    # create an instance of the Stimulus class
    stimulus = VisualStimulus(bar, viewing_distance, screen_width, scale_factor, tr_length, dtype)
    
    # initialize the gaussian model
    model = og.GaussianModel(stimulus, utils.spm_hrf)
    model.hrf_delay = 0
    model.mask_size = 5
    
    # set cache grids
    x_grid = (-10, 10)
    y_grid = (-10, 10)
    s_grid = (0.55,5.25)
    grids = (x_grid, y_grid, s_grid,)
    
    # seed rng
    np.random.seed(4932)
    
    # cache the pRF model
    cache = model.cache_model(grids, Ns=Ns, ncpus=3)
    
    # save it out
    pickle.dump(cache, open('/tmp/og_cached_model.pkl','wb'))
    
    # make sure its the right size
    cached_model = pickle.load(open('/tmp/og_cached_model.pkl','rb'))
    
    nt.assert_equal(np.sum([c[0] for c in cache]),np.sum([c[0] for c in cached_model]))
Exemplo n.º 4
0
def fit_voxel(tup):
    (ii, vx, js) = tup
    stdat = js['Stimulus']
    if pimms.is_list(stdat): stdat = stdat[0]
    height = stdat['fieldofviewVert']
    width = stdat['fieldofviewHorz']
    ### STIMULUS
    # First get a viewing distance and screen size
    dist = 100 # 100 cm is arbitrary
    stim_width = 2 * dist * np.tan(np.pi/180 * width/2)
    stimulus = VisualStimulus(stim, dist, stim_width, 1.0, float(js['TR']), ctypes.c_int16)
    if fixed_hrf is not False:
        model = og_nohrf.GaussianModel(stimulus, utils.double_gamma_hrf)
        model.hrf_delay = fixed_hrf
    else: model = og.GaussianModel(stimulus, utils.double_gamma_hrf)
    ### FIT
    ## define search grids
    # these define min and max of the edge of the initial brute-force search.
    x_grid = (-width/2,width/2)
    y_grid = (-height/2,height/2)
    s_grid = (1/stimulus.ppd + 0.25, 5.25)
    h_grid = (-1.0, 1.0)
    ## define search bounds
    # these define the boundaries of the final gradient-descent search.
    x_bound = (-width, width)
    y_bound = (-height, height)
    s_bound = (1/stimulus.ppd, 12.0) # smallest sigma is a pixel
    b_bound = (1e-8,None)
    u_bound = (None,None)
    h_bound = (-3.0,3.0)
    ## package the grids and bounds
    if fixed_hrf is not False:
        grids = (x_grid, y_grid, s_grid)
        bounds = (x_bound, y_bound, s_bound, b_bound, u_bound,)
    else:
        grids = (x_grid, y_grid, s_grid, h_grid)
        bounds = (x_bound, y_bound, s_bound, h_bound, b_bound, u_bound,)
    ## fit the response
    # auto_fit = True fits the model on assignment
    # verbose = 0 is silent
    # verbose = 1 is a single print
    # verbose = 2 is very verbose
    if fixed_hrf is not False:
        fit = og_nohrf.GaussianFit(model, vx, grids, bounds, Ns=Ns,
                                   voxel_index=(ii, 1, 1), auto_fit=True, verbose=2)
    else:
        fit = og.GaussianFit(model, vx, grids, bounds, Ns=Ns,
                             voxel_index=(ii, 1, 1), auto_fit=True, verbose=2)
    return (ii, vx) + tuple(fit.overloaded_estimate) + (fit.prediction,)
Exemplo n.º 5
0
def test_parallel_fit_manual_grids():

    # stimulus features
    viewing_distance = 38
    screen_width = 25
    thetas = np.arange(0, 360, 45)
    num_blank_steps = 0
    num_bar_steps = 30
    ecc = 10
    tr_length = 1.0
    frames_per_tr = 1.0
    scale_factor = 0.10
    pixels_down = 100
    pixels_across = 100
    dtype = ctypes.c_int16
    voxel_index = (1, 2, 3)
    auto_fit = True
    verbose = 1

    # create the sweeping bar stimulus in memory
    bar = simulate_bar_stimulus(pixels_across, pixels_down, viewing_distance,
                                screen_width, thetas, num_bar_steps,
                                num_blank_steps, ecc)

    # create an instance of the Stimulus class
    stimulus = VisualStimulus(bar, viewing_distance, screen_width,
                              scale_factor, tr_length, dtype)

    # initialize the gaussian model
    model = og.GaussianModel(stimulus, utils.double_gamma_hrf)
    model.hrf_delay = 0

    # generate a random pRF estimate
    x = -5.24
    y = 2.58
    sigma = 1.24
    beta = 2.5
    baseline = -0.25

    # create the "data"
    data = model.generate_prediction(x, y, sigma, beta, baseline)

    # set search grid
    x_grid = slice(-5, 4, 5)
    y_grid = slice(-5, 7, 5)
    s_grid = slice(1 / stimulus.ppd, 5.25, 5)
    b_grid = slice(0.1, 4.0, 5)

    # set search bounds
    x_bound = (-12.0, 12.0)
    y_bound = (-12.0, 12.0)
    s_bound = (1 / stimulus.ppd, 12.0)
    b_bound = (1e-8, 1e2)
    m_bound = (None, None)

    # loop over each voxel and set up a GaussianFit object
    grids = (
        x_grid,
        y_grid,
        s_grid,
    )
    bounds = (x_bound, y_bound, s_bound, b_bound, m_bound)

    # make 3 voxels
    all_data = np.array([data, data, data])
    num_voxels = data.shape[0]
    indices = [(1, 2, 3)] * 3

    # bundle the voxels
    bundle = utils.multiprocess_bundle(og.GaussianFit, model, all_data, grids,
                                       bounds, indices)

    # run analysis
    with sharedmem.Pool(np=3) as pool:
        output = pool.map(utils.parallel_fit, bundle)

    # assert equivalence
    for fit in output:
        npt.assert_almost_equal(fit.x, x, 2)
        npt.assert_almost_equal(fit.y, y, 2)
        npt.assert_almost_equal(fit.sigma, sigma, 2)
        npt.assert_almost_equal(fit.beta, beta, 2)
        npt.assert_almost_equal(fit.baseline, baseline, 2)
Exemplo n.º 6
0
def test_recast_estimation_results():

    # stimulus features
    viewing_distance = 38
    screen_width = 25
    thetas = np.arange(0, 360, 45)
    num_blank_steps = 0
    num_bar_steps = 30
    ecc = 10
    tr_length = 1.0
    frames_per_tr = 1.0
    scale_factor = 0.10
    pixels_down = 100
    pixels_across = 100
    dtype = ctypes.c_int16
    voxel_index = (1, 2, 3)
    auto_fit = True
    verbose = 1

    # create the sweeping bar stimulus in memory
    bar = simulate_bar_stimulus(pixels_across, pixels_down, viewing_distance,
                                screen_width, thetas, num_bar_steps,
                                num_blank_steps, ecc)

    # create an instance of the Stimulus class
    stimulus = VisualStimulus(bar, viewing_distance, screen_width,
                              scale_factor, tr_length, dtype)

    # initialize the gaussian model
    model = og.GaussianModel(stimulus, utils.spm_hrf)
    model.hrf_delay = 0

    # generate a random pRF estimate
    x = -5.24
    y = 2.58
    sigma = 1.24
    beta = 2.5
    baseline = -0.25

    # create the "data"
    data = model.generate_prediction(x, y, sigma, beta, baseline)

    # set search grid
    x_grid = utils.grid_slice(-5, 4, 5)
    y_grid = utils.grid_slice(-5, 7, 5)
    s_grid = utils.grid_slice(1 / stimulus.ppd, 5.25, 5)
    b_grid = utils.grid_slice(0.1, 4.0, 5)

    # set search bounds
    x_bound = (-12.0, 12.0)
    y_bound = (-12.0, 12.0)
    s_bound = (1 / stimulus.ppd, 12.0)
    b_bound = (1e-8, 1e2)
    m_bound = (None, None)

    # loop over each voxel and set up a GaussianFit object
    grids = (
        x_grid,
        y_grid,
        s_grid,
    )
    bounds = (x_bound, y_bound, s_bound, b_bound, m_bound)

    # create 3 voxels of data
    all_data = np.array([data, data, data])
    indices = [(0, 0, 0), (0, 0, 1), (0, 0, 2)]

    # bundle the voxels
    bundle = utils.multiprocess_bundle(og.GaussianFit, model, all_data, grids,
                                       bounds, indices)

    # run analysis
    with sharedmem.Pool(np=3) as pool:
        output = pool.map(utils.parallel_fit, bundle)

    # create grid parent
    arr = np.zeros((1, 1, 3))
    grid_parent = nibabel.Nifti1Image(arr, np.eye(4, 4))

    # recast the estimation results
    nif = utils.recast_estimation_results(output, grid_parent)
    dat = nif.get_data()

    # assert equivalence
    npt.assert_almost_equal(np.mean(dat[..., 0]), x)
    npt.assert_almost_equal(np.mean(dat[..., 1]), y)
    npt.assert_almost_equal(np.mean(dat[..., 2]), sigma)
    npt.assert_almost_equal(np.mean(dat[..., 3]), beta)
    npt.assert_almost_equal(np.mean(dat[..., 4]), baseline)

    # recast the estimation results - OVERLOADED
    nif = utils.recast_estimation_results(output, grid_parent, True)
    dat = nif.get_data()

    # assert equivalence
    npt.assert_almost_equal(np.mean(dat[..., 0]), np.arctan2(y, x), 2)
    npt.assert_almost_equal(np.mean(dat[..., 1]), np.sqrt(x**2 + y**2), 2)
    npt.assert_almost_equal(np.mean(dat[..., 2]), sigma)
    npt.assert_almost_equal(np.mean(dat[..., 3]), beta)
    npt.assert_almost_equal(np.mean(dat[..., 4]), baseline)
Exemplo n.º 7
0
def test_bootstrap():

    # stimulus features
    viewing_distance = 38
    screen_width = 25
    thetas = np.array([-1, 0, 90, 180, 270, -1])
    num_blank_steps = 30
    num_bar_steps = 30
    ecc = 10
    tr_length = 1.0
    frames_per_tr = 1.0
    scale_factor = 0.10
    pixels_down = 100
    pixels_across = 100
    dtype = ctypes.c_int16
    voxel_index = (1, 2, 3)
    auto_fit = True
    verbose = 1

    # rng
    np.random.seed(2764932)

    # create the sweeping bar stimulus in memory
    bar = simulate_bar_stimulus(pixels_across, pixels_down, viewing_distance,
                                screen_width, thetas, num_bar_steps,
                                num_blank_steps, ecc)

    # create an instance of the Stimulus class
    stimulus = VisualStimulus(bar, viewing_distance, screen_width,
                              scale_factor, tr_length, dtype)

    # initialize the gaussian model
    model = og.GaussianModel(stimulus, utils.spm_hrf)
    model.hrf_delay = 0

    # generate a random pRF estimate
    x = -5.24
    y = 2.58
    sigma = 1.24
    beta = 2.5
    baseline = -0.25

    # create the "data"
    data = model.generate_prediction(x, y, sigma, beta, baseline)

    # set search grid
    x_grid = utils.grid_slice(-10, 10, 5)
    y_grid = utils.grid_slice(-10, 10, 5)
    s_grid = utils.grid_slice(0.5, 3.25, 5)

    # set search bounds
    x_bound = (-12.0, 12.0)
    y_bound = (-12.0, 12.0)
    s_bound = (0.001, 12.0)
    b_bound = (1e-8, None)
    m_bound = (None, None)

    # loop over each voxel and set up a GaussianFit object
    grids = (
        x_grid,
        y_grid,
        s_grid,
    )
    bounds = (x_bound, y_bound, s_bound, b_bound, m_bound)

    # pack multiple "runs"
    data = np.vstack((data, data, data))

    # make it a singular "voxel"
    data = np.reshape(data, (1, data.shape[0], data.shape[1]))

    # set bootstraps and resamples
    bootstraps = 2
    resamples = np.array((2, ))

    # make fodder
    bundle = utils.bootstrap_bundle(bootstraps, resamples, og.GaussianFit,
                                    model, data, grids, bounds,
                                    np.tile((1, 2, 3), (bootstraps, 1)))

    # test
    for b in bundle:
        fit = utils.parallel_bootstrap(b)
        npt.assert_almost_equal(fit.rss, 0)
        npt.assert_equal(fit.n_resamples, resamples[0])
        npt.assert_equal(np.sum(fit.resamples),
                         np.sum(np.arange(resamples[0])))
Exemplo n.º 8
0
def test_resurrect_model():
    
    # stimulus features
    viewing_distance = 38
    screen_width = 25
    thetas = np.arange(0,360,90)
    thetas = np.insert(thetas,0,-1)
    thetas = np.append(thetas,-1)
    num_blank_steps = 20
    num_bar_steps = 20
    ecc = 10
    tr_length = 1.5
    frames_per_tr = 1.0
    scale_factor = 1.0
    pixels_across = 100
    pixels_down = 100
    dtype = ctypes.c_int16
    
    # create the sweeping bar stimulus in memory
    bar = simulate_bar_stimulus(pixels_across, pixels_down, viewing_distance, screen_width, 
                                thetas, num_bar_steps, num_blank_steps, ecc, clip=0.01)
                                
    # create an instance of the Stimulus class
    stimulus = VisualStimulus(bar, viewing_distance, screen_width, scale_factor, tr_length, dtype)
    
    # set cache grids
    x_grid = utils.grid_slice(-10, 10, 5)
    y_grid = utils.grid_slice(-10, 10, 5)
    s_grid = utils.grid_slice(0.55,5.25, 5)
    grids = (x_grid, y_grid, s_grid,)
    
    # set search bounds
    x_bound = (-12.0,12.0)
    y_bound = (-12.0,12.0)
    s_bound = (0.001,12.0)
    b_bound = (1e-8,None)
    m_bound = (None,None)
    bounds = (x_bound, y_bound, s_bound, b_bound, m_bound)
    
    # initialize the gaussian model
    model = og.GaussianModel(stimulus, utils.double_gamma_hrf)
    model.hrf_delay = 0
    model.mask_size = 5
    
    cache = model.cache_model(grids, ncpus=3)
    
    # seed rng
    np.random.seed(4932)
    
    # pluck an estimate and create timeseries
    x, y, sigma = cache[51][1]
    beta = 1.25
    baseline = 0.25
    
    # create "data"
    data = cache[51][0]
    
    # fit it
    fit = og.GaussianFit(model, data, grids, bounds, verbose=0)
    
    # assert
    npt.assert_equal(fit.estimate,fit.ballpark)
    
    # create "data"
    data = model.generate_prediction(x,y,sigma,beta,baseline)
    
    # fit it
    fit = og.GaussianFit(model, data, grids, bounds, verbose=0)
    
    # assert
    npt.assert_almost_equal(np.sum(fit.scaled_ballpark_prediction-fit.data)**2,0)
    
    
    
Exemplo n.º 9
0
viewing_distance = 15
screen_width = 6
TR = 2.7
n_pixels = 50

sweeps = np.array([-1, 45, 135, -1, 225, 315, -1])  # in degrees, -1 is blank
#stimulus = VisualStimulus(stim_arr=dm,
#viewing_distance=viewing_distance,
#screen_width=screen_width,
#scale_factor=1,
#tr_length=1.0,
#dtype=np.short)
bar = simulate_bar_stimulus(100, 100, 40, 40, sweeps, 30, 30, 20, 0.67)
stimulus = VisualStimulus(bar, 50, 25, 0.50, 1.0, ctypes.c_int16)

model = og.GaussianModel(stimulus, utils.double_gamma_hrf)
model.hrf_delay = 0

pars = dict(
    np.load(
        '/data/odc/derivatives/voxel_prf/modelfree/sub-tr_desc-None_prf_pars.npz'
    ))

x = np.cos(pars['angle']) * pars['ecc']
y = np.cos(pars['angle']) * pars['ecc']

### FIT
## define search grids
# these define min and max of the edge of the initial brute-force search.
x_grid = (-10, 10)
y_grid = (-10, 10)
Exemplo n.º 10
0
def test_bounded_amplitude_failure():

    # stimulus features
    viewing_distance = 38
    screen_width = 25
    thetas = np.arange(0, 360, 90)
    thetas = np.insert(thetas, 0, -1)
    thetas = np.append(thetas, -1)
    num_blank_steps = 30
    num_bar_steps = 30
    ecc = 12
    tr_length = 1.0
    frames_per_tr = 1.0
    scale_factor = 1.0
    pixels_across = 100
    pixels_down = 100
    dtype = ctypes.c_int16

    # create the sweeping bar stimulus in memory
    bar = simulate_bar_stimulus(pixels_across, pixels_down, viewing_distance,
                                screen_width, thetas, num_bar_steps,
                                num_blank_steps, ecc)

    # create an instance of the Stimulus class
    stimulus = VisualStimulus(bar, viewing_distance, screen_width,
                              scale_factor, tr_length, dtype)

    # initialize the gaussian model
    model = og.GaussianModel(stimulus, utils.double_gamma_hrf,
                             utils.percent_change)
    model.hrf_delay = 0
    model.mask_size = 6

    # generate a random pRF estimate
    x = -5.24
    y = 2.58
    sigma = 1.24
    beta = -0.25
    baseline = 0.25

    # create the "data"
    data = model.generate_prediction(x, y, sigma, beta, baseline)

    # set search grid
    x_grid = utils.grid_slice(-10, 10, 5)
    y_grid = utils.grid_slice(-10, 10, 5)
    s_grid = utils.grid_slice(0.25, 5.25, 5)

    # set search bounds
    x_bound = (-12.0, 12.0)
    y_bound = (-12.0, 12.0)
    s_bound = (0.001, 12.0)
    b_bound = (1e-8, None)
    m_bound = (None, None)

    # loop over each voxel and set up a GaussianFit object
    grids = (
        x_grid,
        y_grid,
        s_grid,
    )
    bounds = (x_bound, y_bound, s_bound, b_bound, m_bound)

    # fit the response
    fit = og.GaussianFit(model, data, grids, bounds)

    nt.assert_true(fit.model.bounded_amplitude)
    nt.assert_true(fit.slope > 0)
    nt.assert_true(fit.beta0 > 0)
    nt.assert_true(fit.beta > 0)
    nt.assert_true(fit.beta != beta)
Exemplo n.º 11
0
def test_og_fit():

    # stimulus features
    viewing_distance = 38
    screen_width = 25
    thetas = np.arange(0, 360, 90)
    thetas = np.insert(thetas, 0, -1)
    thetas = np.append(thetas, -1)
    num_blank_steps = 30
    num_bar_steps = 30
    ecc = 12
    tr_length = 1.0
    frames_per_tr = 1.0
    scale_factor = 1.0
    pixels_across = 100
    pixels_down = 100
    dtype = ctypes.c_int16

    # create the sweeping bar stimulus in memory
    bar = simulate_bar_stimulus(pixels_across, pixels_down, viewing_distance,
                                screen_width, thetas, num_bar_steps,
                                num_blank_steps, ecc)

    # create an instance of the Stimulus class
    stimulus = VisualStimulus(bar, viewing_distance, screen_width,
                              scale_factor, tr_length, dtype)

    # initialize the gaussian model
    model = og.GaussianModel(stimulus, utils.spm_hrf)
    model.hrf_delay = 0
    model.mask_size = 6

    # generate a random pRF estimate
    x = -5.24
    y = 2.58
    sigma = 1.24
    beta = 2.5
    baseline = -0.25

    # create the "data"
    data = model.generate_prediction(x, y, sigma, beta, baseline)

    # set search grid
    x_grid = utils.grid_slice(-10, 10, 5)
    y_grid = utils.grid_slice(-10, 10, 5)
    s_grid = utils.grid_slice(0.25, 5.25, 5)

    # set search bounds
    x_bound = (-12.0, 12.0)
    y_bound = (-12.0, 12.0)
    s_bound = (0.001, 12.0)
    b_bound = (1e-8, None)
    m_bound = (None, None)

    # loop over each voxel and set up a GaussianFit object
    grids = (
        x_grid,
        y_grid,
        s_grid,
    )
    bounds = (x_bound, y_bound, s_bound, b_bound, m_bound)

    # fit the response
    fit = og.GaussianFit(model, data, grids, bounds)

    # coarse fit
    ballpark = [-5., 5., 2.75]

    npt.assert_almost_equal((fit.x0, fit.y0, fit.s0), ballpark)
    # the baseline/beta should be 0/1 when regressed data vs. estimate
    (m, b) = np.polyfit(fit.scaled_ballpark_prediction, data, 1)
    npt.assert_almost_equal(m, 1.0)
    npt.assert_almost_equal(b, 0.0)

    # assert equivalence
    npt.assert_almost_equal(fit.x, x)
    npt.assert_almost_equal(fit.y, y)
    npt.assert_almost_equal(fit.sigma, sigma)
    npt.assert_almost_equal(fit.beta, beta)

    # test receptive field
    rf = generate_og_receptive_field(x, y, sigma, fit.model.stimulus.deg_x,
                                     fit.model.stimulus.deg_y)
    rf /= (2 * np.pi * sigma**2) * 1 / np.diff(model.stimulus.deg_x[0, 0:2])**2
    npt.assert_almost_equal(np.round(rf.sum()),
                            np.round(fit.receptive_field.sum()))

    # test model == fit RF
    npt.assert_almost_equal(
        np.round(fit.model.generate_receptive_field(x, y, sigma).sum()),
        np.round(fit.receptive_field.sum()))