def test_fast_indexing(): D = np.random.randn(1000, 1000) rows = np.random.randint(0, 1000, (400)) cols = np.random.randint(0, 1000, (400)) a = tikutils.fast_indexing(D, rows, cols) b = D[rows, :][:, cols] assert np.allclose(a, b) a = tikutils.fast_indexing(D, rows) b = D[rows, :] assert np.allclose(a, b) a = tikutils.fast_indexing(D.T, cols).T b = D[:, cols] assert np.allclose(a, b)
def test_smoothness_prior(): delays = range(10) ndelays = len(delays) for order in [1, 2, 4]: C = tikutils.difference_operator(order, ndelays) raw_penalty = np.dot(C, C.T) # create object prior = tp.SmoothnessPrior(delays=delays, order=order) assert np.allclose(prior.asarray.shape, (ndelays, ndelays)) assert np.allclose(prior.penalty, raw_penalty) # create prior prior.get_prior(dodetnorm=False) raw_prior = np.linalg.inv(raw_penalty) assert np.allclose(raw_prior, prior.asarray) # update regularizer oprior = prior.get_prior(hhparam=2.0, dodetnorm=False) raw_prior = np.linalg.inv(raw_penalty + 2.0 * np.eye(ndelays)) assert np.allclose(raw_prior, oprior) # check delay sub-selection delays = np.asarray([1, 3, 6, 8]) ndelays = len(delays) fulldelays = range(max(delays) + 1) C = tikutils.difference_operator(order, len(fulldelays)) raw_penalty = np.dot(C, C.T) # create object prior = tp.SmoothnessPrior(delays=delays, order=order) assert np.allclose(prior.get_prior().shape, (ndelays, ndelays)) assert np.allclose(prior.penalty.shape, len(fulldelays), len(fulldelays)) assert np.allclose(prior.penalty, raw_penalty) # create prior prior.get_prior(dodetnorm=False) raw_prior = np.linalg.inv(raw_penalty) raw_prior = tikutils.fast_indexing(raw_prior, delays, delays) assert np.allclose(raw_prior, prior.asarray) # update regularizer oprior = prior.get_prior(hhparam=2.0, dodetnorm=False) raw_prior = np.linalg.inv(raw_penalty + 2.0 * np.eye(len(fulldelays))) raw_prior = tikutils.fast_indexing(raw_prior, delays, delays) assert np.allclose(raw_prior, oprior)
def get_delays_from_prior(raw_prior, delays): ''' Parameters ---------- raw_prior : 2D np.ndarray (k, k) The raw array defined continuously from the first delay to the last: `[min(delays),..., max(delays)]`, and `max(delays) - min(delays) = k`. delays : None or list_like (d) When delays is None, the delays of the `raw_prior` are assumed to be `[0, 1, ..., k-1]`. Otherwise, the delays are specified and are relative to the `raw_prior`. Return ------- prior : 2D np.ndarray ( ''' if delays is None: prior = raw_prior delays = np.arange(raw_prior.shape[0]) else: assert min(delays) >= 0 and max(delays) < raw_prior.shape[0] delays = np.asarray(delays) prior = tikutils.fast_indexing(raw_prior, delays, delays) return prior, delays
def get_delays_from_prior(raw_prior, delays): if delays is None: prior = raw_prior delays = np.arange(raw_prior.shape[0]) else: assert min(delays) >= 0 and max(delays) < raw_prior.shape[0] delays = np.asarray(delays) prior = tikutils.fast_indexing(raw_prior, delays, delays) return prior, delays
def test_cv_api(show_figures=False, ntest=50): # if show_figures=True, this function will create # images of the temporal priors, and the feature prior hyparams in 3D ridges = [0., 1e-03, 1., 10.0, 100.] nridges = len(ridges) ndelays = 10 delays = range(ndelays) features_train, features_test, responses_train, responses_test = get_abc_data( ) features_sizes = [fs.shape[1] for fs in features_train] spatial_priors = [ sps.SphericalPrior(features_sizes[0]), sps.SphericalPrior(features_sizes[1], hyparams=np.logspace(-3, 3, 7)), sps.SphericalPrior(features_sizes[2], hyparams=np.logspace(-3, 3, 7)), ] # do not scale first. this removes duplicates spatial_priors[0].set_hyparams(1.0) # non-diagonal hyper-prior W = np.random.randn(ndelays, ndelays) W = np.dot(W.T, W) tpriors = [ tps.SphericalPrior(delays), tps.SmoothnessPrior(delays, hhparams=np.logspace(-3, 1, 8)), tps.SmoothnessPrior(delays, wishart=True), tps.SmoothnessPrior(delays, wishart=False), tps.SmoothnessPrior(delays, wishart=W, hhparams=np.logspace(-3, 3, 5)), tps.GaussianKernelPrior(delays, hhparams=np.linspace(1, ndelays / 2, ndelays)), tps.HRFPrior([1] if delays == [0] else delays), ] nfolds = (1, 5) # 1 times 5-fold cross-validation folds = tikutils.generate_trnval_folds(responses_train.shape[0], sampler='bcv', nfolds=nfolds) nfolds = np.prod(nfolds) for ntp, temporal_prior in enumerate(tpriors): print(temporal_prior) all_temporal_hypers = [temporal_prior.get_hhparams()] all_spatial_hypers = [t.get_hyparams() for t in spatial_priors] # get all combinations of hyparams all_hyperparams = list( itertools.product(*(all_temporal_hypers + all_spatial_hypers))) nspatial_hyperparams = np.prod([len(t) for t in all_spatial_hypers]) ntemporal_hyperparams = np.prod([len(t) for t in all_temporal_hypers]) population_mean = False results = np.zeros( (nfolds, ntemporal_hyperparams, nspatial_hyperparams, nridges, 1 if population_mean else responses_train.shape[-1]), dtype=[ ('fold', np.float32), ('tp', np.float32), ('sp', np.float32), ('ridges', np.float32), ('responses', np.float32), ]) for hyperidx, spatiotemporal_hyperparams in enumerate(all_hyperparams): temporal_hyperparam = spatiotemporal_hyperparams[0] spatial_hyperparams = spatiotemporal_hyperparams[1:] spatial_hyperparams /= np.linalg.norm(spatial_hyperparams) # get indices shyperidx = np.mod(hyperidx, nspatial_hyperparams) thyperidx = int(hyperidx // nspatial_hyperparams) print(thyperidx, temporal_hyperparam), (shyperidx, spatial_hyperparams) this_temporal_prior = temporal_prior.get_prior( alpha=1.0, hhparam=temporal_hyperparam) if show_figures: from matplotlib import pyplot as plt if (hyperidx == 0) and (ntp == 0): # show points in 3D from tikreg import priors cartesian_points = [ t[1:] / np.linalg.norm(t[1:]) for t in all_hyperparams ] angles = priors.cartesian2polar( np.asarray(cartesian_points)) priors.show_spherical_angles(angles[:, 0], angles[:, 1]) if hyperidx == 0: # show priors with different hyper-priors oldthyper = 0 plt.matshow(this_temporal_prior, cmap='inferno') else: if thyperidx > oldthyper: oldthyper = thyperidx plt.matshow(this_temporal_prior, cmap='inferno') # only run a few if hyperidx > ntest: continue Ktrain = 0. Kval = 0. for fdx, (fs_train, fs_test, fs_prior, fs_hyper) in enumerate( zip(features_train, features_test, spatial_priors, spatial_hyperparams)): kernel_train = models.kernel_spatiotemporal_prior( fs_train, this_temporal_prior, fs_prior.get_prior(fs_hyper), delays=delays) Ktrain += kernel_train kernel_normalizer = tikutils.determinant_normalizer(Ktrain) Ktrain /= float(kernel_normalizer) # cross-validation for ifold, (trnidx, validx) in enumerate(folds): ktrn = tikutils.fast_indexing(Ktrain, trnidx, trnidx) kval = tikutils.fast_indexing(Ktrain, validx, trnidx) fit = models.solve_l2_dual(ktrn, responses_train[trnidx], kval, responses_train[validx], ridges=ridges, verbose=False, performance=True) if population_mean: cvfold = np.nan_to_num(fit['performance']).mean(-1)[..., None] else: cvfold = fit['performance'] results[ifold, thyperidx, shyperidx] = cvfold
def test_hrf_prior(): H = tikutils.hrf_default_basis(dt=2.0, duration=20.) raw_prior = np.dot(H, H.T).astype(np.float64) # test initialization prior = tp.HRFPrior() assert np.allclose(prior.asarray, raw_prior) # test delay seletion delays = np.arange(10) tt = tikutils.fast_indexing(raw_prior, delays, delays) prior = tp.HRFPrior() assert np.allclose(prior.asarray, tt) # change temporal resolution H = tikutils.hrf_default_basis(dt=1.0, duration=20.) raw_prior = np.dot(H, H.T).astype(np.float64) prior = tp.HRFPrior(dt=1.0) assert np.allclose(prior.asarray, raw_prior) # change total duration H = tikutils.hrf_default_basis(dt=2.0, duration=40.) raw_prior = np.dot(H, H.T).astype(np.float64) prior = tp.HRFPrior(duration=40) assert np.allclose(prior.asarray, raw_prior) # change resolution and duratoin H = tikutils.hrf_default_basis(dt=1.0, duration=40.) raw_prior = np.dot(H, H.T).astype(np.float64) prior = tp.HRFPrior(dt=1.0, duration=40) assert np.allclose(prior.asarray, raw_prior) # change resolution and duration and subselect delays H = tikutils.hrf_default_basis(dt=1.0, duration=40.) raw_prior = np.dot(H, H.T).astype(np.float64) delays = np.arange(1, 40) tt = tikutils.fast_indexing(raw_prior, delays, delays) prior = tp.HRFPrior(dt=1.0, duration=40, delays=delays) assert np.allclose(prior.asarray, tt) # change resolution and duration and subselect disjoint delays H = tikutils.hrf_default_basis(dt=1.0, duration=40.) raw_prior = np.dot(H, H.T).astype(np.float64) delays = np.asarray([1, 2, 10, 30, 35]) tt = tikutils.fast_indexing(raw_prior, delays, delays) prior = tp.HRFPrior(dt=1.0, duration=40, delays=delays) assert np.allclose(prior.asarray, tt) # grab continuous delays H = tikutils.hrf_default_basis(dt=2.0, duration=20.) raw_prior = np.dot(H, H.T).astype(np.float64) delays = np.arange(1, 10) tt = tikutils.fast_indexing(raw_prior, delays, delays) prior = tp.HRFPrior(delays) assert np.allclose(prior.asarray, tt) assert np.allclose(prior.delays, delays) assert np.allclose(prior.ndelays, len(delays)) # grab disjoint delays delays = np.asarray([1, 3, 6, 9]) tt = tikutils.fast_indexing(raw_prior, delays, delays) prior = tp.HRFPrior(delays) assert np.allclose(prior.asarray, tt) assert np.allclose(prior.delays, delays) assert np.allclose(prior.ndelays, len(delays))
def test_prior_from_penalty(): tmp = np.random.randn(10, 10) raw_penalty = np.dot(tmp, tmp.T) prior = tp.PriorFromPenalty(raw_penalty, dodetnorm=False) assert np.allclose(raw_penalty, prior.penalty) penalty = prior.prior2penalty(dodetnorm=False) assert np.allclose(raw_penalty, penalty) penalty = prior.prior2penalty(dodetnorm=True) pdetnorm = tikutils.determinant_normalizer(raw_penalty) assert np.allclose(raw_penalty / pdetnorm, penalty) # this must break because the prior hasn't been updated try: penalty = prior.prior2penalty() except AssertionError: pass # generate the prior from penalty prior.get_prior(dodetnorm=False) raw_prior = np.linalg.inv(raw_penalty) assert np.allclose(raw_prior, prior.asarray) assert np.allclose(prior.penalty, raw_penalty) # generate a regularized prior from penalty penalty = prior.prior2penalty(dodetnorm=True) reg_penalty = np.linalg.inv(raw_prior) reg_pdetnorm = tikutils.determinant_normalizer(reg_penalty) assert np.allclose(prior.penalty, raw_penalty) assert np.allclose(reg_penalty / reg_pdetnorm, penalty) # subselect delays delays = np.asarray([1, 2, 3, 4]) prior = tp.PriorFromPenalty(raw_penalty, delays=delays) assert np.allclose(prior.delays, delays) # generate prior prior.get_prior(dodetnorm=False) raw_prior = np.linalg.inv(raw_penalty) raw_prior = tikutils.fast_indexing(raw_prior, delays, delays) # prior should only contain delays of interest assert np.allclose(raw_prior, prior.asarray) # penalty should be kept as original assert np.allclose(prior.penalty, raw_penalty) # check default wishart covariance assert np.allclose(prior.wishart, np.eye(raw_penalty.shape[0])) # regularize penalty before inverting oprior = prior.get_prior(hhparam=2.0, dodetnorm=False) raw_prior = np.linalg.inv(raw_penalty + 2.0 * np.eye(raw_penalty.shape[0])) raw_prior = tikutils.fast_indexing(raw_prior, delays, delays) assert np.allclose(raw_prior, oprior) # regularize oprior = prior.get_prior(hhparam=2.0, dodetnorm=True) detnorm = tikutils.determinant_normalizer(raw_prior) assert np.allclose(raw_prior / detnorm, oprior) # set a non-diagonal wishart prior a = np.random.randn(10, 10) W = np.dot(a, a.T) prior.set_wishart(W) assert np.allclose(prior.wishart, W) # check the update works oprior = prior.get_prior(hhparam=2.0, dodetnorm=True) raw_prior = np.linalg.inv(raw_penalty + 2.0 * W) raw_prior = tikutils.fast_indexing(raw_prior, delays, delays) detnorm = tikutils.determinant_normalizer(raw_prior) assert np.allclose(raw_prior / detnorm, oprior)