Exemple #1
0
def consinput(request):

    # Get the template/step status

    template = "design/cons.html"
    context = {}

    # Get the session ID and database entry

    sid = get_session_id(request)
    context["steps"] = get_design_steps(template, sid)

    try:
        desdata = DesignModel.objects.get(SID=sid)
    except DesignModel.DoesNotExist:
        return HttpResponseRedirect('../maininput/')

    # Define form

    # If page was result of POST or not valid: show form with db entries
    # Else: go to next page

    if desdata.nested == True:
        a = np.array(['P0','P1','P2','P3','P4','P5','P6','P7','P8','P9'])
        b = np.array(desdata.nest_structure)
        Pmat = [a[b==(i+1)].tolist() for i in xrange(desdata.nest_classes)]
        consform = DesignNestedConsForm(
            request.POST or None, instance=desdata, stim=desdata.S, cons=desdata.Clen, structure=Pmat, classes=desdata.nest_structure)
    else:
        consform = DesignConsForm(
            request.POST or None, instance=desdata, stim=desdata.S, cons=desdata.Clen)

    if not request.method == "POST":
        context["consform"] = consform
        return render(request, template, context)
    else:
        form = consform.save(commit=False)
        form.SID = sid
        form.conpars = True
        form.save()

        # get data and change parameters

        consform = DesignProbsForm(None, instance=desdata)
        consform = consform.save(commit=False)
        matrices = probs_and_cons(sid)
        if matrices['empty'] == True:
            context['message'] = "Please fill out all probabilities and contrasts"
            context["consform"] = DesignConsForm(
                request.POST or None, instance=desdata, stim=desdata.S, cons=desdata.Clen)
            return render(request, "design/cons.html", context)
        consform.P = matrices['P']
        consform.C = matrices['C']
        if desdata.HardProb == True:
            consform.G = 200
            consform.I = 100
        consform.save()

        return HttpResponseRedirect('../review/')
Exemple #2
0
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)
Exemple #3
0
def review(request):

    # Get the template/step status

    template = "design/review.html"
    context = {}

    # Get the session ID and database entry

    sid = get_session_id(request)
    context["steps"] = get_design_steps(template, sid)

    try:
        desdata = DesignModel.objects.get(SID=sid)
    except DesignModel.DoesNotExist:
        return HttpResponseRedirect('../maininput/')

    # Define form

    revform = DesignReviewForm(request.POST or None, instance=desdata)
    context["revform"] = revform

    # Set summary variables in context

    matrices = probs_and_cons(sid)
    context["Phtml"] = matrices["Phtml"]
    context["Chtml"] = matrices["Chtml"]
    context["Whtml"] = weights_html(desdata.W)
    context['desdata'] = desdata

    context["message"] = ""
    if desdata.HardProb == True:
        context["message"] = context["message"] + \
            "<br><p><b>Warning:</b> Because of the hard limit on the frequencies, we increased the size of the generation and the number of random designs per generation.  This might slow down the optimisation.  </p>"
    if desdata.MaxRepeat < 10 and desdata.S == 2:
        context["message"] = context["message"] + "<br><p><b>Warning:</b> With only 2 stimuli, many random designs have repetitions larger than " + \
            str(desdata.MaxRepeat) + \
            ".  We increased the number of random designs per generation, but this might slow down the optimisation.  </p>"
    if desdata.S>5 and desdata.L>200 and desdata.ITImax>3 and (desdata.Restnum<30 and desdata.Resdur>30) and desdata.C.shape[0]>5:
        context['message'] = context['message']+"<br><p><b>Warning:</b>This is a long and complex design.  Be aware that the optimisation will take a <b>long</b> time.</p>"

    # Duration
    if desdata.ITImodel == 1:
        context['ITImodel'] = "fixed"
        mean = desdata.ITIfixed
        context['ITI'] = "The ITI's are equal to "+str(mean)+" seconds."
    elif desdata.ITImodel == 2:
        context['ITImodel'] = 'truncated exponential'
        mean = desdata.ITItruncmean
        context['ITI'] = "The ITI's are between "+str(desdata.ITItruncmin)+" and "+str(desdata.ITItruncmax)+" seconds and on average "+str(mean)+" seconds."
    elif desdata.ITImodel == 3:
        context['ITImodel'] = 'uniform'
        mean = (desdata.ITIunifmin+desdata.ITIunifmax)/2.
        context['ITI'] = "The ITI's are between "+str(desdata.ITIunifmin)+" and "+str(desdata.ITIunifmax)+" seconds and on average "+str(mean)+" seconds."

    if desdata.L:
        dur = mean*desdata.L+desdata.RestNum*desdata.RestDur
    elif desdata.duration:
        dur = desdata.duration
    if dur > 1800:
        context['message'] = context['message'] + "<p><b>Warning:</b> The run you request is longer dan 30 minutes.  This optimisation will take <b>a long</b> time.  You could set the resolution lower, or split the experiment in multiple shorter runs.  Or you could grab a coffee and wait a few hours for the optimisation to complete.</p>"
    # If page was result of POST: show summary
    # Else: go to next page

    if not request.method == "POST":
        return render(request, template, context)
    else:
        form = revform.save(commit=False)
        form.SID = sid
        form.save()

        return HttpResponseRedirect('../runGA/')
Exemple #4
0
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)