Take random data from CSV file which satisfy some parameter condition - psychopy

I have a CSV file which looks like this:
cond, arg
T, arg1
T, arg2
T, arg3
T, arg4
T, arg5
P, arg6
P, arg7
P, arg8
P, arg9
P, arg10
Right now I am able to get random value from “arg” column just putting the value “$arg” into “Text element”
My question is, just by using the “Builder view”, for example the “Text element” and the “Code element”, is possible to get randomly the values belonging to the rows which satisfy the condition $cond=‘P’?

I would suggest that the easiest way to do this in Builder would be to split your CSV file into two separate files (in this case, one containing "arg1"-"arg5" and the other containing "arg6"-"arg10"), named, say, T.csv and P.csv.
Then in your loop, instead of having a hard-coded conditions filename, like "conditions.csv", put a variable name, like "$conditionFilename" (without the quotes). You then just need a way to specify the value of that variable before the loop gets created. e.g. Let's say you add a field called "conditionType" to the Experiment Settings dialog which the experimenter will set before each run, which will contain the values "P" or "T". Then in a code component, in the "Begin Experiment" tab, put something like this:
if expInfo['conditionType'] == 'P':
conditionFilename = 'P.csv'
else:
conditionFilename = 'T.csv'
Set the loop to be random, and you will get random selection within those particular trials.
I think the conditionType variable will be automatically stored in your data along with the other experiment settings.

Related

LUA After adding names from _ENV into a table, how do I print the VALUE from the name and not just the "name"

X=123
Y=321
TAB1={}
z=1
for n in pairs(_ENV) do
TAB1[z] = n
z=z+1
end
-- did some more steps that removes general _ENV and only left with my script global variables
TAB2={x, y}
print(TAB2[1])
what I want is "123" but I'm getting "x"
I've tried gmatch, gsub, tostring, but I still can't get it to print the value of x (123). it's only printing "x"
**sorry I don't know how to work this text box correctly for my question
Ive tried gmatch, gsub, tostring, but I still can't get it to print the value of x (123). it's only printing "x"
These functions are for string manipulation so I don't see how they'd be useful here. The reason for the lack of "value" is that your loop is for n in pairs(_ENV) do. pairs is just a wrapper for next which returns key and value, but you're currently discarding the value. Simply add a second variable to the for name list: for name, value in pairs(_ENV) do print(value) end. Alternatively, index _ENV using n: print(_ENV[n]) (inside the loop).

Livecode: How do I program a button to create unique variables?

I apologize if this has been asked before (I couldn't find anything).
I'm an extreme noob in Livecode, and I want to know if there is a way of programming a button to create many new, unique variables and assign a value to them. I apologize if this is a dumb question.
Usually you use an array for that. An array is basically a list of things, where each thing is associated with an "index". An index can be any word, so you can use an array like a dictionary, where you'd e.g. have French words as the index, and English words as the value, like:
put "cow" into myDictionary["vache"]
But you can also just use numbers as the keys and make them a numbered list:
put "cow" into allMyAnimals[1]
put "duck" into allMyAnimals[2]
In end effect, you create one variable and put several things in it. For example if you had a loop that calculated something (in this example a number +100) and you wanted to have variables containing all those numbers, but named with 100 less, you'd do something like:
repeat with x = 1 to 250
put x +100 into twoHundredFiftyNumbersFrom101[x]
end repeat
And to read the first one:
answer "the first number is" && twoHundredFiftyNumbersFrom101[1]
Or all of them:
repeat with x = 1 to 250
answer twoHundredFiftyNumbersFrom101[x]
end repeat
Or whatever. You could also use 'do' to build the lines of code as a string, but then you have to make sure your variable names are generated in a fashion that makes them valid identifiers (e.g. have no spaces in them, no special characters). An array key can be any valid string, and the compiler can optimize them, and you can treat them as a whole and pass them between handlers.
Or you can do this "in the clear" with a "do" construction:
on mouseUp
repeat with y = 1 to 10
get random(100)
do "put it into onTheFlyVariable" & y
end repeat
end mouseUp
Step through this handler and watch the variables assemble themselves.

How to load multiple values for same key in property files?

See this property file:
S=1
M=2
[...]
IA=i
S=g
First, the value 1 will be assigned to S and then g will be assigned to S in the last line.
I want to keep multiple values for the same key S, how can I do this?
I think you need to specify whether this is a Java properties file or some other file, as I've rarely seen it possible to define two separate "keys" with different values without some kind of a section break (ie .ini files).
The only other way I can think of for reading this type of file would be to pull it in as nested dict using the alphabet as the index (a-z, aa-az, etc), storing the key value pairs you've seen, so for example you'd have "a" = {S='1'}, "b" = {M='2'} [..] "z" = {S='g'} and then you could do a query for "letterKey" in dict "alpha" where key.value("innerKey") = 'S', which would give you both 'a' and 'z' due to the 'S' = '1' and 'g'. This may not be any easier than simply rewriting some of the existing code though.
Since a dictionary can't have multiple keys with the same "index" ie 'S' can't appear twice, if you could do as a commenter suggested and store two values in an array in 'S' and reference them via position S.value([0]) and S.value([1]), you'd have a much better program overall.

one variable for all the sql output

myRs=myStmt.executeQuery("select i_col,col_name from tab_col")
i=0
while (myRs.next()):
list= myRs.getString("I_COL")+','+myRs.getString("COL_NAME")
i have a jython code to run a sql statement i want to store all the row of the sql into a single variable. I used to list to store the value but its always storing only the single line , so is there is way to append all the rows and keep adding to single variable.
Thanks so much for your help.
With that code you overwrite the "list" variable in every iteration of the while loop (= is an assignment), try something like this (I used rs rather than list to avoid a name clash with the builtin function list()):
myRs=myStmt.executeQuery("select i_col,col_name from tab_col")
rs=[]
i=0
while (myRs.next()):
rs.append(myRs.getString("I_COL")+','+myRs.getString("COL_NAME"))

Dynamically assigned table variables?

Writing a function in Lua, which creates two tables. I want the tables to be assigned to the value name with an x added, and one with a y added. For example if name was line, it would create two tables linex and liney, but I can't figure out how to do it. The following obviously doesn't work (and is just for display purposes) but how would I go about doing this?
function makelinep(x,y,minrand,maxrand,name,length)
name..x = {}
name..y = {}
Later I hope to access "linex" and "liney" after values have been written.
If you want these in the global name space you would use
_G[name..'x']={}
_G[name..'y']={}
For a module you'd use _M in place of _G.