Can we randomize time with newTimer() ?

PennController for IBEX Forums Support Can we randomize time with newTimer() ?

Viewing 15 posts - 31 through 45 (of 59 total)
  • Author
    Posts
  • #7933
    Jeremy
    Keymaster

    Add newVar("practiceRTs").set( [] ) , before jump(startsWith("p_trial")) to reset the Var element, and yes, if you drop /1000 you will have a value in seconds rather than in milliseconds

    Jeremy

    #7934
    Larissa_Cury
    Participant

    All right! Thank you!!! Considering the Var method above,

    timerd1duration = 400+Math.round(1200*Math.random()) // <------------- are we creating an object (such as we do in R?) i tried this, but it returned me an error. Shouldn't It be a a new Var too? 
    ,
    newTimer("timer-D1",timerd1duration).start().wait()
    ,
    getText("D1").remove()
    ,

    what about the D1 text? shouldn’t I keep it too? Would it be sth like this?

    timerd1duration = 400+Math.round(1200*Math.random()) // (which returned the error)
    ,
    newText("D1", "<br><b>+</b>")
        .cssContainer({
        "font-size":"100px",
        "color":"blue"})
        .center()
        .print()
    ,
       newTimer("timer-D1",400+Math.round(1600*Math.random()))
        .start()
        .wait()
    ,
       getText("D1")
       .remove()
    ,

    etc

    • This reply was modified 2 years, 6 months ago by Larissa_Cury.
    #7936
    Larissa_Cury
    Participant

    orrrrr, should I do this = getVar("newTimerDuration").set( v=> 3500 - timerd1duration - (Date.now()-v) )

    replacing timerd1duration by “timer-D1”, since it’s the label for my d1 timer?

    #7937
    Jeremy
    Keymaster

    If you’re referring to the code from this message, it’s not a full code: it’s a streamlined excerpt of your code to which I added the relevant bits to use the Var method

    timerd1duration = 400+Math.round(1200*Math.random()) creates a javascript variable named timerd1duration that will store the generated random value (which, in the code from my message, I use when creating the Timer element named timer-D1 and when calculating the duration of the last Timer element). I took getText("D1").remove() directly from your code, it’s your call to keep it or not, depending on what you want to happen at that time in the trial

    If you use newTimer("timer-D1",400+Math.round(1600*Math.random())) then there’s no point in using timerd1duration = 400+Math.round(1200*Math.random()). You also will get a duration between 400ms and 2000ms (because 400+1600=2000). "timer-D1" is a string (as you can tell by the double quotes) so you don’t want to include that in an arithmetic operation

    Jeremy

    #7938
    Larissa_Cury
    Participant

    Ohhh, all right! Then, I should do this, right:

    , 
    timerd1duration = 400+Math.round(1200*Math.random())
    ,

    I just didn’t understand why 1200 and not 1600 ?

    Best,

    #7939
    Jeremy
    Keymaster

    400+1200=1600, but 400+1600=2000. I was under the impression that you wanted a random duration between 400ms and 1600ms, not between 400ms and 2000ms

    Also, I’m just realizing that I reference that variable in a function in .set on the Var element, and because timerd1duration is set in a Template environment, by the time the function from set is executed it will always have the value it got in the last generated trial, so my code won’t have the desired outcome. More details on the timeline of javascript code injection on this page

    Jeremy

    #7940
    Larissa_Cury
    Participant

    Ohhhhhhh, now I got the reason why 1200! I’m sorry, that’s right. I need between 400 and 1600 exactly. Thank you! What do you mean that won’t work? What should I do to change it for the desirable outcome?

    #7941
    Larissa_Cury
    Participant

    That’s what I’ve done so far:

    Template("tabela-target.csv" , row => 
        newTrial("trial_2_center_cue_UP",
    defaultText
            .center()
           .cssContainer({"position": "absolute",
                          "top": "50%",
                          "left": "50%",
                          "transform": "translate(-50%, +50%)"})
            .print()
    ,
        newText("D1", "<br><b>+</b>")
        .cssContainer({
        "font-size":"100px",
        "color":"blue"})
        .center()
        .print()
    ,
    timerd1duration = 400+Math.round(1200*Math.random())
    ,
       newTimer("timer-D1",timerd1duration)
        .start()
        .wait()
    ,
       getText("D1")
       .remove()
    ,
    
    newText("cue", "<br> <b>*</b>")
       .cssContainer({
       "font-size":"100px",
       "color":"green"})
       .center()
       .print()
    ,
        newTimer("timer_cue_D2",100)
        .start()
        .wait()
    ,
       getText("cue","D2")
       .remove()
    ,
       newText("D3", "<br> <b>+</b>")
       .cssContainer({
       "font-size":"100px",
       "color":"pink"})
       .center()
       .print()
    ,
       newTimer("timer_D3",400)
        .start()
        .wait()
    ,
       getText("D3")
       .remove()
    ,
    newVar("newTimerDuration").set( v=>Date.now() )
    ,
    newTimer("timer-RT",1700)
        .start()
    ,
        newImage("imagens", row.imagem)
        .size(500, 200)
    ,
       newText("cruz_central", "<br> <b>+</b>")
       .cssContainer({
       "font-size":"100px",
       "color":"black"})
       .center()
       .print()
    ,
        //para cima//
        newCanvas("center", 150,150)
        .add( "center at 50%" , "center at 50%" , getImage("imagens"))
        .cssContainer({
        "position": "absolute",
        "margin-top": "85px"})
        .center()
        .log()
        .print()
    ,
        newKey("keypress1","SK")
        .log()
        .callback( getTimer("timer-RT").stop() )
    ,
        getTimer("timer-RT").wait()
    ,
    getVar("newTimerDuration").set( v=> 3500 - timerd1duration - (Date.now()-v) )
    ,
     newTimer("wait-separacao").set( getVar("newTimerDuration") )
        .start()
    ,
         getKey("keypress1").disable(),
         getText("cruz_central"),
         getCanvas("center")
        .remove()
    ,
       newText("separacao", "<br> <b>+</b>")
       .cssContainer({
        "font-size":"100px",
        "color":"yellow"})
       .center()
       .print()
    ,
      getTimer("wait-separacao")
      .wait()
    )
      .log("imagens", row.imagem)
      .log("item", row.versao)
    );

    that’s the error I get = Invalid duration for Timer “wait-separacao” (newTrial: 47)…What should I do?

    • This reply was modified 2 years, 6 months ago by Larissa_Cury. Reason: include information
    • This reply was modified 2 years, 6 months ago by Larissa_Cury. Reason: add information
    #7944
    Jeremy
    Keymaster

    The issue I’m raising in my previous message means one needs to revise the approach if one wants to use the Var method. I can’t spend more time on this today, but feel free to look for an alternative method yourself

    Jeremy

    #7945
    Larissa_Cury
    Participant

    Thank you, Jeremy! I’m total new to Java script (all I know concerns the Pcibex script itself)…I read the documentation you’ve recommended, but I couldn’t find a solution to solve the set problem. I really wanted to use this approach. Thank you once more for your time and patience.

    • This reply was modified 2 years, 6 months ago by Larissa_Cury. Reason: add information
    #7949
    Jeremy
    Keymaster

    Hi Larissa,

    This should work:

    Template("tabela-target.csv" , row => 
      newTrial("trial_2_center_cue_UP",
        defaultText
            .center()
            .bold()
            .cssContainer({"position": "absolute",
                           "top": "50%",
                           "left": "50%",
                           "transform": "translate(-50%, +50%)",
                           "font-size": "100px",
                           "margin-top": "1em"})
            .print()
        ,
        newText("D1", "+").color("blue")
        ,
        newVar("timer-D1-duration").set( 400+Math.round(1200*Math.random() ) )
        ,
        newTimer("timer-D1", 1).set( getVar("timer-D1-duration") ).start().wait()
        ,
        getText("D1").remove()
        ,
        newText("cue", "*").color("green")
        ,
        newTimer("timer_cue_D2",100).start().wait()
        ,
        getText("cue").remove()
        ,
        newText("D3", "+").color("pink")
        ,
        newTimer("timer_D3",400).start().wait()
        ,
        getText("D3").remove()
        ,
        newVar("newTimerDuration").set( v=>Date.now() )
        ,
        newTimer("timer-RT",1700).start()
        ,
        newText("cruz_central", "+").color("black")
        ,
        //para cima//
        newCanvas("center", 500,200)
            .add( "center at 50%" , "center at 50%" , newImage("imagens", row.imagem).size(500, 200) )
            .log()
            .print("center at 50vw", "top at 85px")
        ,
        newKey("keypress1","SK")
            .log("all")
            .callback( getTimer("timer-RT").stop() )
        ,
        getTimer("timer-RT").wait()
        ,
        getVar("newTimerDuration").set( v=> 3500 - getVar("timer-D1-duration").value - (Date.now()-v) )
        ,
        newTimer("wait-separacao", 1).set( getVar("newTimerDuration") ).start()
        ,
        getKey("keypress1").disable(),
        getText("cruz_central").remove(),
        getCanvas("center").remove()
        ,
        newText("separacao", "+").color("yellow")
        ,
        getTimer("wait-separacao").wait()
      )
      .log("imagens", row.imagem)
      .log("item", row.versao)
    )

    Feel free to edit the command on the Canvas element .print("center at 50vw", "top at 85px") if the image does not appear where you would like it to appear (I suspect that you want instead is "middle at 50vh" in place of "top at 85px", but it’s your call)

    I have tested this with one trial twice, and the total duration of the trial was around 4040ms both times

    Jeremy

    #7954
    Larissa_Cury
    Participant

    Dear Jeremy,
    Thank you for your kind answer as always. I’ve went through what we’ve discussed and I wasn’t able to notice before that the 4000 timeout trial had, indeed, the same formula as the original paper had (D1 + D2 + D3 + DRt + DWait = 4000 equals DWait = 3500 – D1 – DRT), I’m so sorry I took your time for trying to explaining that to me, but I really appreaciate your kindness, so, thank you once more. and thank you more going over the alternative method, I didn’t have a clue about using the Java option, so i’m sure that will contribuite for my future codes! I’ve already updated the whole model with the 4000 timeout approach, I only have 2 final concerns:

    In order to use this approach to the practice trial, I’ve ajusted the Wait timer to 6000, not 4000, since the feedback takes 2000ms, like this:

    //////////////////////////////////////////trial_1 - no_cue// PARA CIMA/LEFT//////////////////////////////////////////////////////
        Template("tabela-target_left.csv" , row => 
        newTrial("p_trial_1_no_cue_UP",
    defaultText
            .center()
           .cssContainer({"position": "absolute",
                          "top": "50%",
                          "left": "50%",
                          "transform": "translate(-50%, +50%)"})
            .print()
    ,
    newTimer("wait-separacao",6000).start()
    ,
        newText("D1", " <br> <b>+</b>")
        .cssContainer({
        "font-size":"100px",
        "color":"blue"})
        .center()
        .print()
    ,
       newTimer("timer-D1",400+Math.round(1200*Math.random()))
        .start()
        .wait()
    ,
       getText("D1")
       .remove()
    ,
    newText("cue", "<br> <b>+</b>")
       .cssContainer({
       "font-size":"100px",
       "color":"green"})
       .center()
       .print()
    ,
        newTimer("timer_cue_D2",100)
        .start()
        .wait()
    ,
       getText("cue","D2")
       .remove()
    ,
       newText("D3", "<br> <b>+</b>")
       .cssContainer({
       "font-size":"100px",
       "color":"pink"})
       .center()
       .print()
    ,
       newTimer("timer_D3",400)
        .start()
        .wait()
    ,
       getText("D3")
       .remove()
    ,
    newTimer("timer-RT",1700).start()
    ,
        newImage("imagens", row.imagem)
        .size(500, 200)
    ,
       newText("cruz_central", "<br> <b>+</b>")
       .cssContainer({
       "font-size":"100px",
       "color":"black"})
       .center()
       .print()
    ,
        //para cima//
        newCanvas("center", 150,150)
        .add( "center at 50%" , "center at 50%" , getImage("imagens"))
        .cssContainer({
        "position": "absolute",
        "margin-top": "85px"})
        .center()
        .log()
        .print()
    ,
    newVar("localRT").set(v=>Date.now()),
        newKey("keypress1","SK")
        .log()
        .callback( getTimer("timer-RT").stop() )
    ,
     getTimer("timer-RT").wait()
    ,
    getKey("keypress1").disable(),
        getText("cruz_central").remove()
    ,
        getCanvas("center").remove()
    ,
        getVar("localRT").set(v=>Date.now()-v),
        newVar("practiceRTs",[]).global().set(v=>[...v,getVar("localRT").value])
    ,
        newVar("accurate", []).global()
    ,
        getKey("keypress1")
         .test.pressed("S")
        .success(
        newVar("positiveFeedback").set( getVar("localRT") ).set( v=> "Correto! Você demorou "+v+"ms para responder" )
        ,
        newText("success")
            .text( getVar("positiveFeedback") )
            .cssContainer({"font-size":"30px", "text-align":"center", "margin-top":"255px","font-family":"Comic Sans MS", "color":"green","white-space":"nowrap"})
        ,
        getVar("accurate").set(v=>[...v,true])
    )
        .failure(
        newVar("negativeFeedback").set( getVar("localRT") ).set( v=> "Incorreto! Você demorou "+v+"ms para responder" )
        ,
        newText("failure")
            .text( getVar("negativeFeedback") )
            .cssContainer({"font-size":"30px", "text-align":"center", "margin-top":"255px","font-family":"Comic Sans MS", "color":"red","white-space":"nowrap"})
    ,
        getVar("accurate").set(v=>[...v,false])
    )
    ,
        newTimer("wait-success",2000) //timer for the success or failure messenge
        .start()
        .wait()
    ,
        getText("success")
        .remove()
    ,
        getText("failure")
        .remove()
    ,
       newText("separacao", "<br> <b>+</b>")
       .cssContainer({
        "font-size":"100px",
        "color":"yellow"})
       .center()
       .print()
    ,
        getTimer("wait-separacao").wait()
    ));

    It seems to me that it’s working just fine, I’d just like to check if the math is right, is it?

    Btw, I wasn’t able to fiz the “come back” button as you’ve explained me to do, it keeps on duplicating the trials, I’m sure I’ve did something wrong, but I don’t know what:

    newText("instructions-14","<p><center> Se você quiser praticar de novo, clique aqui:")
        .cssContainer({
            "font-size":"22px",
            "text-align": "center"
        })
        .center()
        .print()
      ,
      newVar("practiceRTs").set( [] ) // --------------------------------- here  (I've tried to put it before the jumping inside the callback(), but it didn't work too (I probably did sth wrong, though)
      ,
      newButton("come back","PRATICAR DE NOVO")
        .callback(jump(startsWith("p_trial")) , end() )
        .css("margin","1.5em")
        .center()
        .print()

    best,

    • This reply was modified 2 years, 6 months ago by Larissa_Cury. Reason: add relevant information to the post
    #7957
    Jeremy
    Keymaster

    Hi Larissa,

    The math sounds good to me too: the max duration of timer-D1 is 1600ms, then timer_cue_D2 is 100ms and timer_D3 is 400ms, the max (effective) duration of timer-RT is 1700ms, and you add the 2000ms duration of wait-success, which gives you a max total of 5800ms, so by the time your script reaches getTimer("wait-separacao").wait() it will still still have at least 200ms to wait (or more, if timer-D1 was shorter and/or the participant pressed a key before the end of the 1700ms of timer-RT)

    You need to place newVar("practiceRTs").global().set( [] ) , before before jump(startsWith("p_trial")) in the callback command, not before newButton, but I forgot to include .global() in my message above, so mea culpa

    Jeremy

    #7958
    Larissa_Cury
    Participant

    Dear Jeremy,

    Huhh….I guess I’m still doing sth wrong, ’cause it is still couting the 24 trials twice, let me show it you:

    newButton(“come back”,”PRATICAR DE NOVO”)
    .callback(newVar(“practiceRTs”).global().set( [] ),jump(startsWith(“p_trial”)) , end() )
    .css(“margin”,”1.5em”)
    .center()
    .print()
    ?

    I still get: “Você acertou 14 do total de 48” (which means you’ve got 14 out of 48)

    ps: do you think that maybe setting 5800 rather than 6000 would be more precise? My idea was: “ok, I have to show a 2000ms feedback, so I’ll just count this time”

    • This reply was modified 2 years, 6 months ago by Larissa_Cury. Reason: add a question
    #7960
    Jeremy
    Keymaster

    The Var element named “practiceRTs” (which is the one you brought up in your messageEDIT: my bad, the lines of code that refer to that element in your message actually concern another question) keeps track of reaction times, not of accuracy. You need to reset the Var element that keeps track of accuracy too

    Jeremy

    • This reply was modified 2 years, 6 months ago by Jeremy. Reason: correction
Viewing 15 posts - 31 through 45 (of 59 total)
  • You must be logged in to reply to this topic.