def GeneticAlgorithm(sid,ignore_result=False): desdata = DesignModel.objects.get(SID=sid) subject = "NeuroDesign: optimisation process started" sender = "NeuroDesign" sendermail = "*****@*****.**" message = "Your design optimisation has now started. You can follow the progress here:"+" http://development.neuropowertools.org/design/runGA/?retrieve="+str(desdata.shareID)+". Thank you for using NeuroDesign." recipient = str(desdata.email) key = settings.MAILGUN_KEY command = "curl -s --user '" + key + "' https://api.mailgun.net/v3/neuropowertools.org/messages -F from='" + sender + \ " <" + sendermail + ">' -F to=" + recipient + " -F subject="+subject+" -F text='" + message + "'" os.system(command) matrices = probs_and_cons(sid) if desdata.ITImodel == 1: model = "fixed" ITImean = desdata.ITIfixed ITImin = None ITImax = None elif desdata.ITImodel == 2: model = "exponential" ITImean = desdata.ITItruncmean ITImin = desdata.ITItruncmin ITImax = desdata.ITItruncmax elif desdata.ITImodel == 3: model = "uniform" ITImin = desdata.ITIunifmin ITImax = desdata.ITIunifmax ITImean = (desdata.ITIunifmin+desdata.ITIunifmax)/2. EXP = geneticalgorithm.experiment( TR = desdata.TR, n_trials = desdata.L, P = matrices['P'], C = matrices['C'], duration = desdata.duration, n_stimuli = desdata.S, rho = desdata.rho, resolution = desdata.resolution, stim_duration = desdata.stim_duration, restnum = desdata.RestNum, restdur = desdata.RestDur, ITImodel = model, ITImin = ITImin, ITImean = ITImean, ITImax = ITImax, maxrep = desdata.MaxRepeat, hardprob = desdata.HardProb, t_prestim = desdata.t_prestim, t_poststim = desdata.t_poststim ) seed = np.random.randint(10000) POP = geneticalgorithm.population( experiment = EXP, confoundorder = desdata.ConfoundOrder, G = desdata.G, R = [0.4,0.4,0.2], q = desdata.q, weights = desdata.W, I = desdata.I, preruncycles = desdata.preruncycles, cycles = desdata.cycles, convergence=desdata.conv_crit, folder=desdata.onsetsfolder, Aoptimality = True if desdata.Aoptimality == 1 else False, seed=seed ) POP.print_cmd() desdata = DesignModel.objects.get(SID=sid) runform = DesignRunForm(None, instance=desdata) form = runform.save(commit=False) form.running = 1 form.seed = seed form.cmd = POP.cmd form.save() POP.naturalselection() POP.download() # Select optimal design desdata = DesignModel.objects.get(SID=sid) runform = DesignRunForm(None, instance=desdata) form = runform.save(commit=False) form.convergence = POP.finished form.zip_filename = POP.zip_filename form.zipfile = POP.file form.save() subject = "NeuroDesign: optimisation process ended" sender = "NeuroDesign" sendermail = "*****@*****.**" message = "Your design optimisation has now ended. You can download the results here:"+" http://development.neuropowertools.org/design/runGA/?retrieve="+str(desdata.SID)+". Thank you for using NeuroDesign." recipient = str(desdata.email) key = settings.MAILGUN_KEY command = "curl -s --user '" + key + "' https://api.mailgun.net/v3/neuropowertools.org/messages -F from='" + sender + \ " <" + sendermail + ">' -F to=" + recipient + " -F subject="+subject+" -F text='" + message + "'" os.system(command)
from neurodesign import geneticalgorithm, generate, msequence, report EXP = geneticalgorithm.experiment(TR=2, n_trials=100, P=[0.33, 0.33, 0.33], C=[[1, 0, 0], [0, 1, 0], [0, 0, 1], [1, -1, 0], [0, 1, -1]], n_stimuli=3, rho=0.3, resolution=1, stim_duration=1, t_pre=0, t_post=2, restnum=0, restdur=0, ITImodel="exponential", ITImin=1, ITImean=2, ITImax=4) POP = geneticalgorithm.population( experiment=EXP, weights=[0, 0.5, 0.25, 0.25], preruncycles=20, cycles=20, seed=1, outdes=5, folder= '/Users/Joke/Documents/Onderzoek/ProjectsOngoing/Neuropower/playground/design' )
EXP = geneticalgorithm.experiment( TR=1, duration=360, P=[0.1667, 0.1667, 0.1667, 0.1667, 0.1667, 0.1667], C=[ [1, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 1], [1, -1, 0, 0, 0, 0], [1, 0, -1, 0, 0, 0], [1, 0, 0, -1, 0, 0], [1, 0, 0, 0, -1, 0], [1, 0, 0, 0, 0, -1], [0, 1, -1, 0, 0, 0], [0, 1, 0, -1, 0, 0], [0, 1, 0, 0, -1, 0], [0, 1, 0, 0, 0, -1], [0, 0, 1, -1, 0, 0], [0, 0, 1, 0, -1, 0], [0, 0, 1, 0, 0, -1], [0, 0, 0, 1, -1, 0], [0, 0, 0, 1, 0, -1], [0, 0, 0, 0, 1, -1], ], n_stimuli=6, rho=0.3, resolution=0.1, stim_duration=1, t_pre=0, t_post=2, ITImodel="exponential", ITImin=2, ITImean=3, ITImax=6)
from neurodesign import geneticalgorithm, generate, msequence import numpy as np # define experimental setup EXP = geneticalgorithm.experiment(TR=2, n_trials=20, P=[0.3, 0.3, 0.4], C=[[1, -1, 0], [0, 1, -1]], n_stimuli=3, rho=0.3, stim_duration=1, t_pre=0.5, t_post=2, ITImodel="uniform", ITImin=2, ITImax=4) # define first design with a fixed ITI DES = geneticalgorithm.design( order=[0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1], ITI=[2] * 20, experiment=EXP) # expand to design matrix DES.designmatrix() DES.FCalc(weights=[0, 0.5, 0.25, 0.25]) DES.FdCalc() DES.FcCalc()
def GeneticAlgorithm(sid,ignore_result=False): desdata = DesignModel.objects.filter(SID=sid).last() matrices = probs_and_cons(sid) if desdata.ITImodel == 1: model = "fixed" ITImean = desdata.ITIfixed ITImin = None ITImax = None elif desdata.ITImodel == 2: model = "exponential" ITImean = desdata.ITItruncmean ITImin = desdata.ITItruncmin ITImax = desdata.ITItruncmax elif desdata.ITImodel == 3: model = "uniform" ITImin = desdata.ITIunifmin ITImax = desdata.ITIunifmax ITImean = (desdata.ITIunifmin+desdata.ITIunifmax)/2. if (desdata.MaxRepeat<4 and desdata.S<5) or desdata.S==2: R = [0,1,0] else: R = [0.4,0.4,0.2] print("R: "+str(R)) EXP = geneticalgorithm.experiment( TR = desdata.TR, n_trials = desdata.L, P = matrices['P'], C = matrices['C'], duration = desdata.duration, n_stimuli = desdata.S, rho = desdata.rho, resolution = desdata.resolution, stim_duration = desdata.stim_duration, restnum = desdata.RestNum, restdur = desdata.RestDur, ITImodel = model, ITImin = ITImin, ITImean = ITImean, ITImax = ITImax, confoundorder = desdata.ConfoundOrder, maxrep = desdata.MaxRepeat, hardprob = desdata.HardProb, t_pre = desdata.t_prestim, t_post = desdata.t_poststim ) seed = np.random.randint(10000) POP = geneticalgorithm.population( experiment = EXP, G = desdata.G, R = R, q = desdata.q, weights = desdata.W, I = desdata.I, preruncycles = desdata.preruncycles, cycles = desdata.cycles, convergence=desdata.conv_crit, folder=desdata.onsets_folder, outdes=desdata.outdes, Aoptimality = True if desdata.Aoptimality == 1 else False, seed=seed ) POP.print_cmd() desdata = DesignModel.objects.filter(SID=sid).last() runform = DesignRunForm(None, instance=desdata) form = runform.save(commit=False) form.taskstatus = 2 form.running = 1 form.seed = seed form.cmd = POP.cmd form.save() local_naturalselection(POP,sid) POP.download() desdata = DesignModel.objects.filter(SID=sid).last() runform = DesignRunForm(None, instance=desdata) form = runform.save(commit=False) form.finished = True form.running = 0 form.taskstatus = 3 form.save() # list all files (with full path) for report infiles = [os.path.join(dp, f) for dp, dn, fn in os.walk(os.path.expanduser(desdata.onsets_folder)) for f in fn] # strip away /var/tmp --> design_suffix/xxx outfiles = [x[9:] for x in infiles] for file in range(len(infiles)): push_to_s3(infiles[file],"designs/"+outfiles[file]) # Select optimal design desdata = DesignModel.objects.filter(SID=sid).last() runform = DesignRunForm(None, instance=desdata) form = runform.save(commit=False) form.convergence = POP.finished form.files = outfiles form.save() subject = "NeuroDesign: optimisation process ended" sender = "NeuroDesign" sendermail = "*****@*****.**" message = "Your design optimisation has now ended. You can download the results here:"+" http://www.neuropowertools.org/design/runGA/?retrieve="+str(desdata.shareID)+". Thank you for using NeuroDesign." recipient = str(desdata.email) #recipient = '*****@*****.**' key = settings.MAILGUN_KEY command = "curl -s --user '" + key + "' https://api.mailgun.net/v3/neuropowertools.org/messages -F from='" + sender + \ " <" + sendermail + ">' -F to=" + recipient + " -F subject="+subject+" -F text='" + message + "'" os.system(command)