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,)
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)
bounds = ( x_bound, y_bound, s_bound, h_bound, b_bound, u_bound, ) # fit #fit = dog.DifferenceOfGaussiansFit(model, data, grids, bounds, Ns=5, #voxel_index=(1,2,3), auto_fit=True, verbose=1) # fit fit = og.GaussianFit(model, data[:, 0], grids, bounds, Ns=4, voxel_index=(1, 2, 3), auto_fit=True, verbose=0) import ctypes, multiprocessing import numpy as np import sharedmem import popeye.og_hrf as og import popeye.utilities as utils from popeye.visual_stimulus import VisualStimulus, simulate_bar_stimulus # seed random number generator so we get the same answers ... np.random.seed(2764932) ### STIMULUS
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)
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()))