Add any small useful functions here
Functions here are not currently in the main PEBL distribution. On each release, we will evaluate those found here, add error-checking where necessary, and incorporate the best and most appropriate into PEBL.
Convert index list to list of bools
If you have a list of indices (maybe trial numbers) that you want to convert to a list of 0/1 tags of a specific length, you can use this function. For example, IndexListToBoolList([1,3,5,9],10) will produce a list [1,0,1,0,1,0,0,0,1,0].
define IndexListToBoolList(x,l)
{
## x is an array of indices
## l is the length of the output list you want
if(not IsList(x))
{
SignalFatalError("First Argument of IndexListToBoolList(<list>) must be a list.")
}
if(not IsNumber(l))
{
SignalFatalError("Second Argument of IndexListToBoolList(<list>) must be a list.")
}
#First, create a dummy template
templ <- Sequence(1,l,1)
#Create three substitution tables:
# one replaces maps everything in X to -1;
# the second maps 1:l to 0
# the third maps -1 to 1
match1 <- Transpose([x,Repeat(-1,Length(x))])
nonmatches <- Transpose([Sequence(1,l,1),Repeat(0,l)])
match2 <-[[-1,1]]
##Apply replace three times:
a <- Replace(templ,match1)
b <- Replace(a,nonmatches)
c <- Replace(b,match2)
return c
}
Compute Stats on Data
## This finds mean and SD on the data for
## each level of factor; returns list of:
## factor number mean sd
define ComputeStats(data, factor)
{
## start by sorting both rts and delays by delays;
## then move through them and analyze subparts.
datX <- SortBy(data,factor)
facX <- Sort(factor)
trials <- Transpose([facX,datX])
stats <- []
lastfac <- First(facX)
tmpDat <- []
loop(i, trials)
{
## if the current delay differs from the previous delay,
## we should analyze what is in tmp right now.
if(First(i) != lastfac)
{
stats <- Append(stats,
[lastfac, Length(tmpDat),
Mean(tmpDat), StdDev(tmpDat)])
tmpDat<- []
}
lastfac <- First(i)
tmpDat <- Append(tmpDat, Nth(i, 2))
}
stats <- Append(stats,
[lastfac, Length(tmpDat),
Mean(tmpDat), StdDev(tmpDat)])
return stats
}
Likert-scale Question Function
Here's a function that implements a 1-6 Likert-style questionairre. It expects there to be Labels named gHeader (placed at the top), gFooter and gFooter2 (at the bottom), and a Textbox gTextBox placed in the middle. it returns a string containing the a code for the question type, the response and the response latency.
define LikertTrial(text)
{
SetText(gTextBox,text)
SetText(gHeader,"How much you agree with the following statement:")
SetText(gFooter," 1 2 3 4 5 6")
SetText(gFooter2,"Not at all Strongly")
Draw()
t1 <- GetTime()
response <-WaitForListKeyPress(["1","2","3","4","5","6"])
t2 <- GetTime()
return "LK " + response + " " + (t2 - t1)
}
Yes-No style Question Function
Here's a similar function that implements a yes-no questionairre.
define YesNoTrial(text)
{
SetText(gTextBox,text)
SetText(gHeader,"Do you agree or disagree with the following statement::")
SetText(gFooter," 'Z' '/'")
SetText(gFooter2,"YES NO")
Draw()
t1 <- GetTime()
response <-WaitForListKeyPress(["Z","/"])
t2 <- GetTime()
return "YN " + response + " " + (t2 - t1)
}
Replace Character in String
## This replaces all instances of a character in a string with another character.
## Useful for saving participant input that might have spaces.
define ReplaceChar(word,char, char2)
{
list <- SplitString(word, char)
newString <- ""
begin <- 0
loop(i,list)
{
if(begin == 0)
{
newString <- newString + i
begin <- 1
} else {
newString <- newString + char2 + i
}
}
return newString
}
Lookup value in keyed database
This function finds the first element of keylist that matches key, then returns the corresponding element of database. Try it with Lookup(key,keylist, Sequence(1,Length(keylist),1)) to find out the index of the matching key.
## Returns the element of database corresponding to the first match of
## key to items in keylist. If nothing is found, it returns an empty list.
define Lookup(key, keylist, database)
{
found <- []
table <- Transpose([keylist, database])
loop(i, table)
{
if(key == First(i))
{
found <- Nth(i,2)
break
}
}
return found
}
---
Finds if item is in list
#Determine whether an item is part of the list or not
define InList(list, item)
{
found <- 1
loop(i, list)
{
if (i == item) {
found <- 0
}
}
return found
}
