Python to Java Delay Discounting
Hello,
I am new to the coding world and OpenSesame. My lab currently has a OpenSesame DD task that we need to get working online. However, the inline script is written in Python, and I need to translate it (if at all possible) to JavaScript. I've tried using Javascripthon and Jython as methods of converting the script, but my computer kept having issues and my attempts have been unsuccessful. I would be very grateful if anyone could help with the translation, or would know a way to implement the Du 2002 delay discounting task into OpenSesame so that it could work online. Any insight or help would be very much appreciated!
Below is the inline Python scripts I have for different parts in the sequence:
global choice response = self.get('response') if response ==1: choice = 'Twenty dollars' if response ==2: choice = 'Four dollars and eighty-six cents' if response ==3: choice = 'Ten dollars' if response ==4: choice = 'One hundred dollars' exp.set('choice',choice) data = [0.0,0.0,0.0,0.0,0.0,0.0] AUC = [0.0,0.0,0.0,0.0,0.0,0.0] days = [2.0,7.0,30.0,180.0,365.0,1826.0] tempX = 0.0 tempY = 1.0 ssr = 0 from openexp.canvas import canvas from openexp.keyboard import keyboard my_keyboard = keyboard(exp, keylist=['space'], timeout=None) data = [0.0,0.0,0.0,0.0,0.0,0.0] AUC = [0.0,0.0,0.0,0.0,0.0,0.0] days = [2.0,7.0,30.0,180.0,365.0,1826.0] tempX = 0.0 tempY = 1.0 ssr = 0 my_canvas = canvas(exp) my_canvas.text('You will now be given a series of choices about money.\nThere are no right or wrong answers. Make your choices as tough they are \nreal. Please answer honestly. You should use the \nkeyboard to make choices, Z key for \nthe left item and M key for \nthe right item. The computer will let \nyou know when the study is over.\n Press any key when you are ready.', center = True) my_canvas.show() response, timestamp = my_keyboard.get_key() exp.set('amount',100.0) exp.set('amount_now',50.0) bmax = 0.0 tmax = 100.0 ssr = bmax +(tmax-bmax)/2 exp.set('previous', self.get('amount_now')) if self.get('response') =='z': if self.get('now')=='l': tmax = ssr else: bmax = ssr else: if self.get('now')=='r': tmax = ssr else: bmax = ssr temp = bmax +(tmax-bmax)/2 mod = int(temp / 5) ssr = mod * 5.0 result = ssr/100.0 exp.set('amount_now', result) data[self.get('position')] = self.get('amount_now') exp.log ('AUC') exp.log ( 'Period,1,7,30,90,180,365') exp.log ('Results,' + str(data[0]) + ',' + str(data[1]) + ',' + str(data[2]) + ',' + str(data[3]) + ',' + str(data[4]) +',' + str(data[5]) ) sum = 0 for i in range(6): AUC[i] = (days[i]/365 -tempX) * (data[i]/100 + tempY) /2 tempY = data[i] / 100.0 tempX = days[i] / 365.0 sum = sum + AUC[i] exp.log ('AUC,' + str(AUC[0]) + ',' + str(AUC[1]) + ',' + str(AUC[2]) + ',' + str(AUC[3]) + ',' + str(AUC[4]) +',' + str(AUC[5]) ) exp.log ('Total AUC:,' + str(sum))
Comments
Hi @Megski ,
In OpenSesame, you can do much more in a Python
inline_script
than you can do in aninline_javascript
. For example, in aninline_javascript
there is noCanvas
object, etc. You can find more information about that here.So the main issue in your case is not translating Python to JavaScript, but the limitations of JavaScript as currently used in OpenSesame / OSWeb. This means that if you want to implement this task to run with OSWeb, then you'll have to go back to drawing board and come up with an implementation that uses only the subset of functionality that is supported by OSWeb.
Cheers!
Sebastiaan
Check out SigmundAI.eu for our OpenSesame AI assistant!