Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

Convert python script to javascript

Dear everyone, I have an experiment that is running on desktop just fine. It involves a sound file played in the background with variable (defined through a variable) fade-out times, while the subject is performing a simple attention task. I have achieved this by creating the following Python_inline script:

src = pool['Imm']

my_sampler = Sampler(src)

my_sampler.play(block=False, fade_in=self.get('fade'))

The script above runs inside the sequence, and then after the sequence is done, the following inline script stops it, so that it doesn't play during the break

my_sampler.stop()

For greater clarity, here is an image of my experiment setup:


So, I'd like to publish the experiment online through JATOS, but I do not know anything about JavaScript. I have consulted the manual, but have only found that there is the pool[].data.play() function, but I don't know what additional arguments it takes, or how could I use it to achieve the same effect that I did with my Python script. Googling was no help either.


I hope that I've been clear and I thank you in advance for your help.


Kindest regards,

Slobodan

Comments

  • edited October 2022

    Hi @Slobodanp ,


    You could make a `sampler` item stop playing in JavaScript like so:


    const sample = runner._experiment.items._items['new_sampler']._sampler.sample
    sample.pause()
    
    


    See also the attached dummy experiment that keeps playing background noise until a loop of five trials is looped through.

    Does this help?


    Cheers,


    Lotje

    Did you like my answer? Feel free to Buy Me A Coffee :)

  • Hi Lotje, thank you for your answer!

    Sadly, it did not resolve my issue. I get the following error message:

    Stopped

    The experiment did not finish normally for the following reason:


    Error while executing inline script

    Details

    item: new_inline_javascript_1

    phase: run

    item-stack: experiment[run].meta_loop[run].new_sequence_4[run].new_inline_javascript_1[run]

    time: Tue Oct 25 13:10:13 2022

    exception type: JsException

    exception message: ReferenceError: runner is not defined

    Traceback (also in debug window)

     File "C:\Program Files (x86)\OpenSesame\Lib\site-packages\libopensesame\inline_script.py", line 116, in run

      self.workspace._exec(self.crun)

     File "C:\Program Files (x86)\OpenSesame\share\opensesame_plugins\inline_javascript\inline_javascript.py", line 59, in _exec

      exec(compiled, self._context)

     File "<EvalJS snippet>", line 2, in <module>

     File "C:\Program Files (x86)\OpenSesame\Lib\site-packages\js2py\base.py", line 1158, in get

      raise MakeError('ReferenceError', '%s is not defined' % prop)

    js2py.internals.simplex.JsException: ReferenceError: runner is not defined


    Not sure on what may be causing it, I see that it's running fine in the experiment that you've sent. Let me know if you could use any additional information.


    Thank you once again,

    Slobodan

  • Hi @Slobodanp ,


    The first thing that comes to mind is that your experiment does not contain a sampler item or that you are not referring to the correct name of the sampler item. In my example, the sampler is called "new_sampler". Make sure the name in the script is identical to the name of the item in the overview area.


    If this doesn't help, could you upload your experiment here?


    Cheers,


    Lotje

    Did you like my answer? Feel free to Buy Me A Coffee :)

  • Hi @lvanderlinden, thanks for the quick answer, sorry for not getting back to you earlier.


    The sampler is named 'new_sampler', so that shouldn't be the issue.

    I seem to be failing to upload the experiment directly here, so here's a gdrive link to it. I left in only the troublesome part to make it clearer.


    Thank you again for your support!

    Slobodan

  • edited November 2022

    Hi @Slobodanp ,


    Did you try running the experiment in the browser? Some JavaScript code does not run on your desktop but only in the browser. (Click the play button with the green circle around it to do so.)


    Also, the hierarchy of your experiment didn't seem logic to me. I think something like the following makes more sense (at least make sure that your logger item is always placed at the end of the trial sequence:


    See here a working example (I lowered the number of repetitions for testing purposes).



    Cheers,


    Lotje

    Did you like my answer? Feel free to Buy Me A Coffee :)

  • Hi,


    See, that's the thing. The point of the experiment is for the sound to go off at a pseudo-random (fixed time-points, random order throughout the experiment) point in time and then continue going until the main_loop finishes looping (until the break). Here's the experiment with a python inline script that achieves exactly that, so it's certainly possible, I'm not sure why is it executing it differently with the built-in sampler. When I try to put the sampler into the loot in your version of the experiment, it just doesn't stop it when the break comes.

    Here is otherwise the same experiment, with the python script working as intended and the js script not:


    Thank you again,

    Slobodan

  • Hi Slobodan,

    When I try to put the sampler into the loot in your version of the experiment, it just doesn't stop it when the break comes.

    While this seems to be a potential bug in Opensesame, I am a bit confused why you can't use Lotje's example? The sampler plays the sound during the blocks and pauses when the inline_script is hit? How should the behavior be different for your use case?

    Eduard

    Buy Me A Coffee

  • Hi Eduard,


    The difference is that I don't need the sampler to play at the beginning of each trial, but instead at the number of trial that I've defined in the sequence in the loop in which the stimuli are. It plays it when the sequence loops for the nth time, where n is defined through the "Time" variable. I've defined this by having the sound run only when the n of the loops is equal to the predefined Time variable, as so:

    Basically, in different blocks of the experiment, I have the sound start playing at the 8th, 16th, or 24th loop of the stimuli. That way the participants can't get accommodated to the sound starting at a specific time during the block.

    I hope that clears it up and brings us closer to a solution.


    Thanks to all,


    Slobodan

  • Hi Slobodan,

    Thanks, much clearer indeed.

    The question is whether the the run_if fields in sequences can be used in OSweb the same way as in Opensesame, specifically regarding dynamically setting them. I just had a quick go at it, but as far as I see it doesn't work. Disclaimer, I am not very familiar with OSweb. @lvanderlinden, @Fab or @sebastiaan might know whether such a functionality is possible with osweb.

    Eduard

    Buy Me A Coffee

  • Hi Eduard,


    Thank you for the response, although there is no solution as of yet. I hope that someone else will be able to help as well :)

    Cheers!

    Slobodan

  • Hi @Slobodanp, (cc @eduard)

    I just had a quick look. I think the issue is that when converted to Javascrip to run in the browser, OS doesn't handle variables entered into the "run if" field as it would it it ran in Python (@lvanderlinden and @sebastiaan are a lot more knowledgeable about this than me and may be able to confirm). In any case, going along with my gut feeling, I play around a little and found -I think- a work around mehtod: you just need to move this part of the condition to a variable :

    to a variable defined in Javascript, whcih you can then call upon using the [ ] method in the "Run if" field:

    I think that running the code in the "prepare" phase should make sure it is all calculated ahead of the sequence running but you'd need to check whether this is the case or not (if not, the sound would play on the trial next to that you planned to present the sound in, a problem you could deal with by modifying the trial numbers on which the sound must be played on).

    You then edit the "Run if" condition to:

    I don't know all of the intricacies and details of your task, and so you'd need to see what needs to be adapted, but at least now the code is playing the sound on specific trials only and it runs in the browser.

    Here's my modification: https://www.dropbox.com/s/megp5hl755zdinp/online%20js%20Fab.osexp?dl=0

    Note that in order to test it in the browser (OSWeb), I had to disable the "Log all variables" in the logger (OSWeb does not like this option because logging everything is heavy and slows down the task). You must make sure to include manually the variables you do want logged.

    Hope this helps!

    Fabrice.

    Buy Me A Coffee

  • Hello Fabrice, and thank you very much, this does seem to get us really close to the finish line. Your work-around seems to work, as the sound does start on specific trials, which is great, but the stopper does not seem to work when I run it. The sampler continues playing throughout the break screen and afterwards.


    If we solve that issue, then I believe that we would have everything sorted out.

    Thank you all again very much,


    Slobodan

  • Hi @Slobodanp,

    I had not seen your reply (you forgot to use my handler, so I didn't get a notification). To user handlers, just type "@" and the handler of your correspondent (as you start typing, it'll suggest options). Using handlers ensures that your correspondent is notified of the message.

    Before I look further into this, I just want to make sure I understand how your task is structured. You do have the code stopping the sampler after the main loop so, once started, the sampler would run until all 32 trials have been presented. If this is not what you aimed for, please describe when and under what conditions you'd like the sound to be stopped.

    Kind regards,

    Fabrice.

    Buy Me A Coffee

  • Hi @Fab, thanks for the explanation regarding handlers.

    The sampler should start running at the defined time and stop after the 32 trials are over - to put it plainly, the brakes should be silent.

    As it is now, at least when I test it, the sound does not stop after a main loop is ended. Is this different on your machine?

    Again, thank you very much!

    Slobodan

  • Hi @Slobodanp,

    I tried it in Edge and in Chrome and it does stop after the 32nd trial, though it fails occasionally and stops earlier. Not sure why. But it does not carry on playing after the main loop has ended.

    Fabrice.

    Buy Me A Coffee

  • Hi @Fab ,


    I'm not sure why this is the case, but for me, the sound does not stop until the audio file ends (this is likely the reason that you experience the stop before the end of trials). There's no difference in terms of what browser I use. I'll try to have someone else do the experiment through Mindprobe to see if that works.

    I'll get back to you,

    Slobodan

  • Hi everyone, old post but I'm coming back to this design and I have encountered a new issue. When I run the experiment, it all works fine until it's time to execute the inline js to stop the sampler:

    const sample = runner._experiment.items._items['new_sampler']._sampler.samplesample.pause()

    Then, it gives me the following error:

    Do you have any idea why this may be the case? Did the commands change with the introduction of OS4 and webOS2? Thanks in advance! @Fab @eduard @lvanderlinden

  • Hi @Slobodanp,

    I went back to the sampe task I jhad sent you back then (available here: https://we.tl/t-pDd308Kt04) and tried it in the browser with the latest version of OSWeb and got no error message (I used Chrome). So I the sample.pause() command does appear to work. The issue must come from somewhere else, but it's hard to tell without seeing the task.

    Best,

    Fabrice.

    Buy Me A Coffee

Sign In or Register to comment.

agen judi bola , sportbook, casino, togel, number game, singapore, tangkas, basket, slot, poker, dominoqq, agen bola. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 50.000 ,- bonus cashback hingga 10% , diskon togel hingga 66% bisa bermain di android dan IOS kapanpun dan dimana pun. poker , bandarq , aduq, domino qq , dominobet. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 10.000 ,- bonus turnover 0.5% dan bonus referral 20%. Bonus - bonus yang dihadirkan bisa terbilang cukup tinggi dan memuaskan, anda hanya perlu memasang pada situs yang memberikan bursa pasaran terbaik yaitu http://45.77.173.118/ Bola168. Situs penyedia segala jenis permainan poker online kini semakin banyak ditemukan di Internet, salah satunya TahunQQ merupakan situs Agen Judi Domino66 Dan BandarQ Terpercaya yang mampu memberikan banyak provit bagi bettornya. Permainan Yang Di Sediakan Dewi365 Juga sangat banyak Dan menarik dan Peluang untuk memenangkan Taruhan Judi online ini juga sangat mudah . Mainkan Segera Taruhan Sportbook anda bersama Agen Judi Bola Bersama Dewi365 Kemenangan Anda Berapa pun akan Terbayarkan. Tersedia 9 macam permainan seru yang bisa kamu mainkan hanya di dalam 1 ID saja. Permainan seru yang tersedia seperti Poker, Domino QQ Dan juga BandarQ Online. Semuanya tersedia lengkap hanya di ABGQQ. Situs ABGQQ sangat mudah dimenangkan, kamu juga akan mendapatkan mega bonus dan setiap pemain berhak mendapatkan cashback mingguan. ABGQQ juga telah diakui sebagai Bandar Domino Online yang menjamin sistem FAIR PLAY disetiap permainan yang bisa dimainkan dengan deposit minimal hanya Rp.25.000. DEWI365 adalah Bandar Judi Bola Terpercaya & resmi dan terpercaya di indonesia. Situs judi bola ini menyediakan fasilitas bagi anda untuk dapat bermain memainkan permainan judi bola. Didalam situs ini memiliki berbagai permainan taruhan bola terlengkap seperti Sbobet, yang membuat DEWI365 menjadi situs judi bola terbaik dan terpercaya di Indonesia. Tentunya sebagai situs yang bertugas sebagai Bandar Poker Online pastinya akan berusaha untuk menjaga semua informasi dan keamanan yang terdapat di POKERQQ13. Kotakqq adalah situs Judi Poker Online Terpercayayang menyediakan 9 jenis permainan sakong online, dominoqq, domino99, bandarq, bandar ceme, aduq, poker online, bandar poker, balak66, perang baccarat, dan capsa susun. Dengan minimal deposit withdraw 15.000 Anda sudah bisa memainkan semua permaina pkv games di situs kami. Jackpot besar,Win rate tinggi, Fair play, PKV Games