Beispiel #1
0
from cosgen.cross_over import cross_over
from cosgen.immigrants import generate_immigrants
import cosgen.models as models
from cosgen.statistics import Statistics

import numpy as np
import pandas as pd
from functools import partial

storage_path = '~/.cosgen'
seqlength = 1490
TR = 1
nstimtypes = 2
population_size = 20

ecm = models.get_ar1_cov(seqlength, 0.78)  #ecm = estimator covariance matrix
whitening_mat = np.linalg.inv(np.linalg.cholesky(ecm))


#def design_matrix(sequence):
#	hrf = np.load('/home/wguest/hrf.npy')
#	hrflength = len(hrf)
#	ls = len(sequence.l)
#	DM = np.empty((ls,4))
#	DM[:,0]=np.ones(seqlength)
#	DM[:,1]=np.linspace(-0.5,0.5,seqlength)
#	X = np.array([sequence.l == i for i in range(1,sequence.nstimtypes+1)],dtype=int)
#	blocks = sequence.get_block_representation()
#	Xconfoundregressor = np.zeros((1,sequence.seqlen))
#	for idx, val in enumerate(blocks):
#		Xconfoundregressor[0,val[0]:val[1]] = len(blocks)-idx
Beispiel #2
0
def cli_algorithm(population_size=20,
                  library_size=20,
                  storage_path='~/.cosgen',
                  seqlength=100,
                  maxblocklength=40,
                  minblocklength=5,
                  nstimtypes=1,
                  generations=10000,
                  survivors=5,
                  nimmigrants=4,
                  hrflength=30,
                  TR=1,
                  model_type='detection',
                  autoregression=0.5,
                  baseline='auto'):
    """
	Run default optimization.

	This function attempts to optimize towards a viable sequence, while only requiring few input parameters.
	It uses a generic design matrix construction and a first order autoregressive model.

	Parameters
	----------
	population_size : int
	    Size of the population used for the genetic algorithm.
	library_size : int
	    Number of sequences included in the output. The library_size best
	    sequences are saved.
	storage_path : string
	    Path to folder where output is saved.
	seqlength : int
	    Length of the sequence that is optimized.
	maxblocklength : int
	    Maximum block length allowed.
	minblocklength : int
	    Minimum block length allowed.
	nstimtypes : int
	    Number of possible stimulus types in the sequence.
	generations : int
	    Number of iterations the genetic algorithm performs.
	survivors : int
	    Number of sequences that are carried over from the previous iteration.
	nimmigrants : int
	    Number of new (randomly generated) sequences add in each iteration.
	hrflength : int
	    Length of the HRF.
	TR : float
	    Repetition time of scans in seconds.
	model_type : string
	    Can be 'detection' in order to optimize for contrast detection
	    or 'estimation' for HRF estimation.
	autoregression : float
	    Coefficient of the fist order autoregressive noise model.
	baseline : string or int
	    Number of baseline TR before the stimulation starts. Can be 'auto'.
	"""
    storage_path = os.path.expanduser(storage_path)
    storage_path = os.path.join(
        storage_path, '{:%Y%m%d%H%M%S}'.format(datetime.datetime.now()))
    try:
        os.makedirs(storage_path)
    except OSError as exc:
        if exc.errno == errno.EEXIST and os.path.isdir(path):
            pass
        else:
            raise
    with open(os.path.join(storage_path, 'parameters.txt'), 'w+') as f:
        f.write('Population size = ' + str(population_size) + '\n')
        f.write('Library size = ' + str(library_size) + '\n')
        f.write('Sequence length = ' + str(seqlength) + '\n')
        f.write('Number of stimulus types = ' + str(nstimtypes) + '\n')
        f.write('Number of generations = ' + str(generations) + '\n')
        f.write('Number of survivors = ' + str(survivors) + '\n')
        f.write('Number of immigrants = ' + str(nimmigrants) + '\n')
        f.write('HRF length = ' + str(hrflength) + '\n')
        f.write('TR = ' + str(TR) + '\n')
        f.write('Autoregression = ' + str(autoregression) + '\n')
        f.write('Baseline = ' + str(baseline))

    fcts = FunctionCrate()
    gamma_hrf = models.get_gamma_hrf(TR, hrflength)
    ar1_cov = models.get_ar1_cov(seqlength, autoregression)
    extra_evs = np.empty((seqlength, 2))
    extra_evs[:, 0] = np.ones(seqlength)
    extra_evs[:, 1] = np.linspace(-0.5, 0.5, seqlength)
    if model_type == 'detection':
        model = models.DetectionModel(gamma_hrf,
                                      err_cov_mat=ar1_cov,
                                      filterfunc=partial(
                                          models.gaussian_highpass, sigma=225),
                                      extra_evs=extra_evs)
    elif model_type == 'estimation':
        basis_set = models.get_FIR_basis_set(hrflength)
        model = models.EstimationModel(basis_set, err_cov_mat=ar1_cov)
    fcts.add_fitness_measure(
        'cov',
        partial(fitness_measures.estimator_variance,
                model=model,
                optimality='a'))
    fcts.set_mutate(mutate)
    fcts.set_cross_over(cross_over)
    optimal_block_size = estimate_optimal_block_size(seqlength, fcts)
    if baseline == 'auto':
        baseline = 2 * optimal_block_size
    fcts.set_generate_immigrants(
        partial(generate_block_immigrants,
                seqlen=seqlength,
                nstimtypes=nstimtypes,
                block_size=list(range(minblocklength, maxblocklength)),
                gap_size=50,
                gap_sigma=30))
    statistics = Statistics(storage_path)
    population = [
        Sequence(seqlength, nstimtypes) for i in range(population_size - 1)
    ]
    population.append(
        Sequence(seqlength,
                 nstimtypes,
                 'block',
                 block_size=max(min(optimal_block_size, maxblocklength),
                                minblocklength)))
    population = ga(population, fcts, generations, survivors, nimmigrants,
                    statistics)
    for i, seq in enumerate(fcts.find_best(population, library_size)):
        seq.add_baseline(baseline)
        seq.dump(storage_path, index=i, TR=TR)
        print(seq.l, seq.fitness)
Beispiel #3
0
ny = 10
X = np.linspace(1, 3, nx)
Y = np.linspace(9, 11, ny)
hrfs = [[get_irf(x, y) for y in Y] for x in X]

#nx = 20
#ny = 5
#X = np.linspace(4,8,nx)
#Y = np.linspace(0,1,ny)
#hrfs = [[models.get_gamma_hrf(1,32,a1=x,a6=y) for y in Y] for x in X]

block_size = np.empty((nx, ny))

seql = 1490  #sequence length
ecm = np.identity(seql)  #error covarianve matrix
ecm = models.get_ar1_cov(seql, 0.5)
extra_evs = np.empty((seql, 2))
extra_evs[:, 0] = np.ones(seql)  #constant confound regressor
extra_evs[:, 1] = np.linspace(-0.5, 0.5, seql)  #linear confound regressor
fc = FunctionCrate()
c = np.zeros(3)
c[0] = 0
c[1] = 0
c[2] = 1
print(len(X))
print(len(Y))
print(len(hrfs))
print(len(hrfs[0]))
for i in range(nx):
    for j in range(ny):
        print('i', i)