psycho-spatial-orientation-2back.C

Go to the documentation of this file.
00001 /*!@file AppPsycho/psycho-spatial-orientation-2back.C Psychophysics test to measure the effect of executive tasks on spatial memory */
00002 
00003 // //////////////////////////////////////////////////////////////////// //
00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the //
00005 // University of Southern California (USC) and the iLab at USC.         //
00006 // See http://iLab.usc.edu for information about this project.          //
00007 // //////////////////////////////////////////////////////////////////// //
00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00010 // in Visual Environments, and Applications'' by Christof Koch and      //
00011 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00012 // pending; application number 09/912,225 filed July 23, 2001; see      //
00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00014 // //////////////////////////////////////////////////////////////////// //
00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00016 //                                                                      //
00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00018 // redistribute it and/or modify it under the terms of the GNU General  //
00019 // Public License as published by the Free Software Foundation; either  //
00020 // version 2 of the License, or (at your option) any later version.     //
00021 //                                                                      //
00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00025 // PURPOSE.  See the GNU General Public License for more details.       //
00026 //                                                                      //
00027 // You should have received a copy of the GNU General Public License    //
00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00030 // Boston, MA 02111-1307 USA.                                           //
00031 // //////////////////////////////////////////////////////////////////// //
00032 //
00033 // Primary maintainer for this file: Laurent Itti <itti@usc.edu>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/AppPsycho/psycho-spatial-orientation-2back.C $
00035 // $Id: psycho-spatial-memory.C 13040 2010-03-23 03:59:25Z nnoori $
00036 //
00037 
00038 #include "Component/ModelManager.H"
00039 #include "Image/Image.H"
00040 #include "Psycho/PsychoDisplay.H"
00041 #include "Psycho/EyeTrackerConfigurator.H"
00042 #include "Psycho/EyeTracker.H"
00043 #include "Psycho/PsychoOpts.H"
00044 #include "Component/EventLog.H"
00045 #include "Component/ComponentOpts.H"
00046 #include "Raster/Raster.H"
00047 #include "Util/MathFunctions.H"
00048 #include "Util/Types.H"
00049 #include "GameBoard/basic-graphics.H"
00050 #include <sys/types.h>
00051 #include <dirent.h>
00052 #include <errno.h>
00053 #include <vector>
00054 #include <string>
00055 #include <iostream>
00056 #include <SDL/SDL.h>
00057 #include <SDL/SDL_image.h>
00058 #include <SDL/SDL_mixer.h>
00059 #include <stdio.h>
00060 #include <stdlib.h>
00061 #include <sstream>
00062 #include <time.h>
00063 #include "Image/DrawOps.H"
00064 #include "GameBoard/resize.h"
00065 #include <iostream>
00066 #include <fstream>
00067 #include <set>
00068 #include <algorithm>
00069 #include <ctime>
00070 
00071 #ifndef INVT_HAVE_LIBSDL_IMAGE
00072 #include <cstdio>
00073 int main()
00074 {
00075         fprintf(stderr, "The SDL_image library must be installed to use this program\n");
00076         return 1;
00077 }
00078 
00079 #else
00080 
00081 
00082 
00083 using namespace std;
00084 
00085 // ######################################################################
00086 
00087 ModelManager manager("Psycho-Spatial-Orientation-2back");
00088 nub::soft_ref<PsychoDisplay> d(new PsychoDisplay(manager));
00089 map<uint,uint> testMap ;
00090 map<string,string> argMap ;
00091 map<string,vector<SDL_Rect*>*> clipsmap;
00092 
00093 
00094 //////////////////////////////////////////////
00095 // a functionf for stringigying things
00096 //////////////////////////////////////////////
00097 template <class T> std::string stringify(T i)
00098 {
00099         ostringstream o ;
00100         o << i ;
00101         return o.str();
00102 }
00103 
00104 
00105 double getAvarage(vector<long> v){
00106         double f = 0.0 ;
00107         for( uint i = 0 ; i <  v.size() ; i++ ){
00108                 f += v[i] ;
00109         }
00110         if (v.size()!=0) return f/v.size() ;
00111         return -1 ;
00112 }
00113 
00114 double getVariance(vector<long> v){
00115         double m = getAvarage(v);
00116         double var = 0.0 ;
00117         for( uint i = 0 ; i < v.size(); i++ ){
00118                 var += (v[i]-m)*(v[i]-m) ;
00119         }
00120         if (v.size()!=0) return var/v.size() ;
00121         return -1 ;
00122 }
00123 
00124 bool itIsInThere(int x , vector<int> bag){
00125         for( uint i=0 ; i < bag.size(); i++ ){
00126                 if(x == bag[i]) return true ;
00127         }
00128         return false ;
00129 }
00130 
00131 
00132 ////////////////////////////////////////////////////////////////////
00133 //// gets a string as the argument and returns a string composed of
00134 //// characters of the first string sorted in the ascending order
00135 ///////////////////////////////////////////////////////////////////
00136 string ascSort(string st)
00137 {
00138         string res = "" ;
00139         vector<string> v = vector<string>();
00140         for(uint i = 0 ; i < st.size() ; i++) v.push_back(st.substr(i,1)) ;
00141 
00142         std::sort(v.begin(), v.end());
00143 
00144         for ( uint i = 0 ; i < v.size() ; i++ ){
00145                 res += v[i] ;
00146         }
00147         return res;
00148 }
00149 
00150 
00151 
00152 ////////////////////////////////////////////////////////////////////
00153 //// gets a string as the argument and returns a string composed of
00154 //// characters of the first string sorted in the descending order
00155 ///////////////////////////////////////////////////////////////////
00156 string desSort(string st)
00157 {
00158         string res = "" ;
00159         vector<string> v = vector<string>();
00160         for(uint i = 0 ; i < st.size() ; i++) v.push_back(st.substr(i,1)) ;
00161         std::sort(v.begin(), v.end());
00162         std::reverse(v.begin(), v.end());
00163         for ( uint i = 0 ; i < v.size() ; i++ ){
00164                 res += v[i] ;
00165         }
00166         return res;
00167 }
00168 
00169 
00170 ////////////////////////////////////////////////////////
00171 ///// simply generates a sequence of characters out of master string
00172 ////////////////////////////////////////////////////////
00173 
00174 string getARandomString(uint l, string alphabet="0123456789" ){
00175 
00176         string test = string("") ;
00177         string retString ;
00178         test = "" ;
00179         string tp = string("") ;
00180         vector<int> pickedones = vector<int>() ;
00181         for(uint i = 0 ; i < l ; i++){
00182                         int nd;
00183             do{ nd= rand()% alphabet.size() ; }while(itIsInThere(nd,pickedones)) ;
00184                         pickedones.push_back(nd);
00185                         tp = alphabet.substr(nd,1) ;
00186                         test += tp ;
00187         }
00188         retString = test ;
00189         return retString ;
00190 }
00191 
00192 
00193 
00194 ///////////////////////////////////////////////////////
00195 //this function is not called in this program, but it generates a random string and it will show it in
00196 //a random place on the screen.
00197 //////////////////////////////////////////////////////
00198 string digitMemorizationTask(uint l, string alphabet="0123456789" , int displayFrame = 10  ){
00199         d->clearScreen() ;
00200         vector<int> pickedones = vector<int>() ;
00201         string test = string("") ;
00202         string tp = string("") ;
00203         for(uint i = 0 ; i < l ; i++){
00204                 int nd;
00205                 do{ nd= rand()% alphabet.size() ; }while(itIsInThere(nd,pickedones) && pickedones.size() <= alphabet.size()) ;
00206                 pickedones.push_back(nd);
00207                 tp = alphabet.substr(nd,1) ;
00208                 test += tp ;
00209         }
00210         d->displayText(test,true,0) ;
00211         d->waitFrames(displayFrame) ;
00212         d->clearScreen() ;
00213         return test ;
00214 }
00215 
00216 
00217 ////////////////////////////////////////////////////////
00218 ///////this will change the order of elements in a vector to a random order
00219 ////////////////////////////////////////////////////////
00220 void scramble(vector<string>& v){
00221         vector<string> tv = vector<string>() ;
00222         while(v.size()>0){
00223                 tv.push_back(v[0]);
00224                 v.erase(v.begin());
00225         }
00226         int i = 0 ;
00227         while(tv.size()>0){
00228                 i = rand()%tv.size() ;
00229                 v.push_back(tv[i]);
00230                 tv.erase(tv.begin()+i);
00231         }
00232 }
00233 
00234 void scramble(vector<int>& v){
00235     vector<int> tv = vector<int>() ;
00236     while(v.size()>0){
00237         tv.push_back(v[0]);
00238         v.erase(v.begin());
00239     }
00240     int i = 0 ;
00241     while(tv.size()>0){
00242         i = rand()%tv.size() ;
00243         v.push_back(tv[i]);
00244         tv.erase(tv.begin()+i);
00245     }
00246 }
00247 
00248 ////////////////////////////////////////////////////////////////
00249 ////This is our button factory
00250 ////////////////////////////////////////////////////////////////
00251 SDL_Surface* getButtonImage(string label , PixRGB<byte> txtcolor=PixRGB<byte>(0,0,0) , PixRGB<byte> bgcolor=PixRGB<byte>(255,255,255) ,Point2D<int> size = Point2D<int>(100,100) ,PixRGB<byte> bordercolor=PixRGB<byte>(0,0,0) , int border=3){
00252         Image<PixRGB<byte> > textIm(d->getWidth(),d->getHeight(),ZEROS);
00253         textIm.clear(bgcolor);
00254         writeText(textIm, Point2D<int>((size.i - label.length()*10)/2,(size.j-20) /2),label.c_str(),txtcolor,bgcolor);
00255         SDL_Surface *surf = d->makeBlittableSurface(textIm , true);
00256         Uint32 bc = d->getUint32color(bordercolor);
00257         drawRectangle(surf,bc,0,0,size.i -1,size.j -1 ,border);
00258         SDL_Surface* blank =getABlankSurface(size.i , size.j);
00259         SDL_Rect clip;
00260         clip.x = 0 ;
00261         clip.y = 0 ;
00262         clip.w = size.i ;
00263         clip.h = size.j ;
00264         apply_surface(0,0,*surf,*blank,clip);
00265         dumpSurface(surf) ;
00266         return blank ;
00267 }
00268 
00269 ////////////////////////////////////////////////////////////////////////
00270 ////This is the function for creating the keypad, in fact it generates
00271 ////12 buttons and associates the actions to the region for each button
00272 ////////////////////////////////////////////////////////////////////////
00273 
00274 SDL_Surface* getKeyPad(string alphabet,map<string , SDL_Rect>& buttmap){
00275         SDL_Surface* pad= getABlankSurface(d->getWidth()/4,d->getHeight()/3);
00276         SDL_Rect clip;
00277         clip.x=0;
00278         clip.y=0;
00279         int numofrows = alphabet.size()/3 +1;
00280         if(alphabet.size()%3 != 0 ) numofrows++ ;
00281         int numofcolumns = 3 ;
00282         clip.w= pad->w / numofcolumns ;
00283         clip.h = pad->h / numofrows ;
00284 
00285   //keys for 1 to 9
00286         for( int i = 0 ; i < numofrows*3 ; i++){
00287                 SDL_Surface* but ;
00288                 if((uint)i < alphabet.size()){
00289                         but = getButtonImage(alphabet.substr(i,1),PixRGB<byte>(0,0,0),PixRGB<byte>(255,255,255),Point2D<int>(pad->w / numofcolumns , pad->h / numofrows),PixRGB<byte>(255, 98 , 25),3);
00290                 }else{
00291                         but = getButtonImage(" ",PixRGB<byte>(0,0,0),PixRGB<byte>(255,255,255),Point2D<int>(pad->w / numofcolumns , pad->h / numofrows),PixRGB<byte>(255, 98 , 25),3);
00292                 }
00293 
00294                 SDL_Rect cl ;
00295                 cl.x = ((i)%numofcolumns)*(pad->w)/numofcolumns ; cl.y= ((i)/numofcolumns)*((pad->h)/numofrows) ;
00296                 cl.w = clip.w ;
00297                 cl.h = clip.h ;
00298                 apply_surface( cl.x , cl.y ,*but,*pad,clip);
00299                 if((uint)i < alphabet.size()) buttmap[alphabet.substr(i,1)] = cl ;
00300                 dumpSurface(but);
00301         }
00302         SDL_Rect cl1 ;
00303         cl1.x = 0 ; cl1.y= (numofrows-1)*((pad->h)/numofrows) ;
00304         cl1.w = clip.w ;
00305         cl1.h = clip.h ;
00306         buttmap["!"] = cl1 ;
00307         SDL_Surface* but = getButtonImage(string("<-"),PixRGB<byte>(0,0,0),PixRGB<byte>(255,255,255),Point2D<int>(pad->w / numofcolumns , pad->h / numofrows),PixRGB<byte>(255, 98 , 25),3);
00308         apply_surface(0, (numofrows-1)*((pad->h)/numofrows),*but,*pad,clip);
00309         dumpSurface(but);
00310         SDL_Rect cl2 ;
00311         cl2.x = (pad->w)/numofcolumns ; cl2.y= (numofrows-1)*((pad->h)/numofrows) ;
00312         cl2.w = clip.w ;
00313         cl2.h = clip.h ;
00314         buttmap[" "] = cl2 ;
00315         but = getButtonImage(string("spc"),PixRGB<byte>(0,0,0),PixRGB<byte>(255,255,255),Point2D<int>(pad->w / numofcolumns , pad->h / numofrows),PixRGB<byte>(255, 98 , 25),3);
00316         apply_surface((pad->w)/numofcolumns, (numofrows-1)*((pad->h)/numofrows),*but,*pad,clip);
00317         dumpSurface(but);
00318         SDL_Rect cl3 ;
00319         cl3.x = 2*(pad->w)/numofcolumns ; cl3.y= (numofrows-1)*((pad->h)/numofrows) ;
00320         cl3.w = clip.w ;
00321         cl3.h = clip.h ;
00322         buttmap["*"] = cl3 ;
00323         but = getButtonImage(string("Ok"),PixRGB<byte>(0,0,0),PixRGB<byte>(255,255,255),Point2D<int>(pad->w / numofcolumns , pad->h / numofrows),PixRGB<byte>(255, 98 , 25),3);
00324         apply_surface(2*(pad->w)/numofcolumns, (numofrows-1)*((pad->h)/numofrows),*but,*pad,clip);
00325         dumpSurface(but);
00326         return pad ;
00327 }
00328 
00329 
00330 
00331 
00332 ///////////////////////////////////////////////////////////////////////////
00333 /////this function listens to mouse clicks and then finds the region of the screen
00334 /////associated with the action, buttmap is the map of the region, offset is the offset of
00335 /////buttons
00336 ///////////////////////////////////////////////////////////////////////////
00337 string getPressedButtonCommand(map<string , SDL_Rect>& buttmap,Point2D<int> offset=Point2D<int>(0,0)){
00338         int quit = 0 ;
00339         string s ;
00340         SDL_Event event ;
00341         while( quit!=2 ){
00342                 while( SDL_PollEvent( &event ) ) {
00343                         if(event.type == SDL_MOUSEBUTTONDOWN  && event.button.button == SDL_BUTTON_LEFT ){
00344                                 for( map<string , SDL_Rect>::iterator it = buttmap.begin() ; it!=buttmap.end() ; ++it){
00345                                         if(event.button.x >= (it->second).x + offset.i && event.button.x <= (it->second).x + (it->second).w + offset.i  && event.button.y >= (it->second).y+ offset.j && event.button.y <= (it->second).y + (it->second).h + offset.j) {
00346                                                 quit = 2 ;
00347                                                 s = it->first ;
00348                                                 break;
00349                                         }
00350 
00351                                 }
00352                         }
00353 
00354                 }
00355         }
00356         return s ;
00357 
00358 }
00359 
00360 
00361 ////////////////////////////////////////////////////
00362 ////This function creates a virtual keypad, creates a map of buttons
00363 ////and their representation area and listens to the button press and at
00364 ////the end returns the keyed digits
00365 ////////////////////////////////////////////////////
00366 string getDigitSequenceFromSubject(string alphabet="0123456789" , uint maxl = 7 , string message=""){
00367         d->showCursor(true) ;
00368         //let's creat a map to map actions to regions of the screen, each region is represented as an SDL_Rect
00369         map<string , SDL_Rect>* buttmap = new map<string , SDL_Rect>();
00370         //now let's get the keypad surface while we get the actions map to regions
00371         SDL_Surface * keypad = getKeyPad(alphabet,*buttmap);
00372         //this will be the offset of displaying the keypad on the screen
00373         SDL_Rect offset ;
00374         offset.x = (d->getWidth() - keypad->w) /2;
00375         offset.y = (d-> getHeight() - keypad->h) /2;
00376         //d->displayText(message, Point2D<int>(d->getWidth()/3,d->getHeight()*2 /16) , PixRGB<byte>(0,0,0) ,PixRGB<byte>(127,127,127)) ;
00377         SDL_Surface* msgp = getButtonImage(message ,PixRGB<byte>(195,60,12) ,PixRGB<byte>(127,127,127) ,Point2D<int>(d->getWidth()/6,d->getHeight() /15) ,PixRGB<byte>(127,127,127) , 4) ;
00378         SDL_Rect msgoffs ; msgoffs.x = (d->getWidth() - msgp->w) /2 ; msgoffs.y = 2*d->getHeight()/9 ;
00379         d->displaySDLSurfacePatch(msgp , &msgoffs , NULL , -2 , false ,true ) ;
00380         dumpSurface(msgp) ;
00381         //now let's display the keypad
00382         d->displaySDLSurfacePatch(keypad , &offset,NULL , -2,false, true);
00383         //this will hold the final string keyed be the subject
00384         string p = string("") ;
00385         //this is a temporary string holding the last action related to the pressed key
00386         string tp = string("");
00387         //now let's record subject's key press
00388         while( tp.compare("*")!=0 ){
00389                 //this button is actually the display for the current string
00390                 SDL_Surface* dp = getButtonImage(p ,PixRGB<byte>(195,60,12) ,PixRGB<byte>(255,255,255) ,Point2D<int>(d->getWidth()/6,d->getHeight() /15) ,PixRGB<byte>(0,25,180) , 4) ;
00391                 SDL_Rect offs ; offs.x = (d->getWidth() - dp->w) /2 ; offs.y = d->getHeight()/6 ;
00392                 d->displaySDLSurfacePatch(dp , &offs , NULL , -2 , false ,true ) ;
00393                 //now let's listen to button events
00394                 tp = getPressedButtonCommand(*buttmap,Point2D<int>(offset.x,offset.y)) ;
00395                 dumpSurface(dp) ;
00396                 if(tp.compare("!")==0 && p.size()>=0 ) {
00397                         if (p.size()>0) p = p.substr(0,p.size()-1) ;
00398                 }else{
00399                         if(p.size() < maxl && tp.compare("*")!=0) {
00400                                 p +=tp ;
00401                         }
00402 
00403                 }
00404 
00405         }
00406         
00407         buttmap = 0 ;
00408         dumpSurface(keypad) ;
00409         d->clearScreen() ;
00410         return p ;
00411 
00412 }
00413 
00414 
00415 
00416 ///////////////////////////////////////////////////////////////
00417 //////gets the test string, answer and the mode and identifies if
00418 //////the answer matches the thing it should be, mode=0 checks if
00419 //////if the answer and test string simply the same, mode=1 matches
00420 //////the answer against the ascending sorted string of the test string
00421 //////mode=2 compares the answer against the descending sorted of
00422 //////the test string
00423 ///////////////////////////////////////////////////////////////
00424 bool isAnswerCorrect(string test , string answer , int mode){
00425 
00426         if(mode == 0 && answer.compare(test)==0) return true ;
00427 
00428         if(mode == 1 && answer.compare(ascSort(test))==0) return true ;
00429 
00430         if(mode == 2 && answer.compare(desSort(test))==0) return true ;
00431 
00432         return false;
00433 }
00434 
00435 
00436 
00437 int addArgument(const string st,const string delim="="){
00438         int i = st.find(delim) ;
00439         argMap[st.substr(0,i)] = st.substr(i+1);
00440 
00441         return 0 ;
00442 }
00443 
00444 std::string getArgumentValue(string arg){
00445         return argMap[arg] ;
00446 }
00447 
00448 
00449 
00450 vector<int> spatial_memory_task(int onsetTime, int isd , int dir ,  int ma=30 ,int Ma=120){
00451 
00452     int w = d->getWidth();
00453     int h = d->getHeight();
00454     int neg = rand()%(Ma-ma) +ma ;
00455     int pos = rand()%(Ma-ma) +ma ;
00456     //d->displayFixation();
00457     if(dir==0) {
00458         d->displayRedDotFixation(w/2+pos,h/2);
00459         d->waitFrames(onsetTime);
00460         d->clearScreen();
00461         d->waitFrames(isd);
00462         d->displayRedDotFixation(w/2-neg,h/2);
00463         d->waitFrames(onsetTime);
00464         d->clearScreen();
00465     }
00466     if(dir==1) {
00467         d->displayRedDotFixation(w/2,h/2+pos);
00468         d->waitFrames(onsetTime);
00469         d->clearScreen();
00470         d->waitFrames(isd);
00471         d->displayRedDotFixation(w/2,h/2-neg);
00472         d->waitFrames(onsetTime);
00473         d->clearScreen();
00474     }
00475     vector<int> locations;
00476     locations.push_back(neg);
00477     locations.push_back(pos);
00478     return locations ;
00479 }
00480 
00481 vector<int> getA2NonRepeatingSeqence(int numOfElements , int lenght){
00482   vector<int> sequence;
00483   //let's first push two elements in the sequence
00484   sequence.push_back(rand()%numOfElements);
00485   sequence.push_back(rand()%numOfElements);
00486   for(int i = 2 ; i < lenght ; i++){
00487     int r = rand()%(numOfElements) ;
00488     if(r== sequence[i-2]) r = (r+1)%numOfElements;
00489     sequence.push_back(r);
00490   }
00491   
00492   return sequence;
00493 }
00494 
00495 vector<int> getA2RepeatingSequence(int numOfElements , int length){
00496   vector<int> nonrepeatingsequence = getA2NonRepeatingSeqence(numOfElements,length);
00497   int repeatPlace = 2+ rand()%(length-2);
00498   nonrepeatingsequence[repeatPlace] = nonrepeatingsequence[repeatPlace-2];
00499   return nonrepeatingsequence;
00500 }
00501 
00502 
00503 vector<int> getA1NonRepeatingSeqence(int numOfElements , int lenght){
00504   vector<int> sequence;
00505   //let's first push one elements in the sequence
00506   sequence.push_back(rand()%numOfElements);
00507  // sequence.push_back(rand()%numOfElements);
00508   for(int i = 1 ; i < lenght ; i++){
00509     int r = rand()%(numOfElements) ;
00510     if(r== sequence[i-1]) r = (r+1)%numOfElements;
00511     sequence.push_back(r);
00512   }
00513   
00514   return sequence;
00515 }
00516 
00517 vector<int> getA1RepeatingSequence(int numOfElements , int length){
00518   vector<int> nonrepeatingsequence = getA2NonRepeatingSeqence(numOfElements,length);
00519   int repeatPlace = 1+ rand()%(length-1);
00520   nonrepeatingsequence[repeatPlace] = nonrepeatingsequence[repeatPlace-1];
00521   return nonrepeatingsequence;
00522 }
00523 
00524 
00525 vector<int> getNonRepeatingSeqenceForPassiveTest(int numOfElements , int lenght){
00526   vector<int> sequence;
00527   //let's first push two elements in the sequence
00528   for(int i = 0 ; i < lenght ; i++){
00529     int r = rand()%(numOfElements-1) +1 ;
00530     sequence.push_back(r);
00531   } 
00532   return sequence;
00533 }
00534 
00535 vector<int> getRepeatingSeqenceForPassiveTest(int numOfElements , int lenght){
00536   vector<int> sequence = getNonRepeatingSeqenceForPassiveTest(numOfElements,lenght);
00537   int p = rand()%lenght;
00538   sequence[p] = 0 ;
00539   return sequence;
00540 }
00541 
00542 int spatial_memory_retrival(vector<int> dotsVector,int dir ,int ma=30 , int Ma=120 ,int ds=30 ){
00543     
00544     int w = d->getWidth();
00545     int h = d->getHeight();
00546     int change = rand()%2;
00547     int neg = dotsVector.at(0); int pos = dotsVector.at(1);
00548     switch( change ){
00549         case 0 : d->pushEvent("same target");break;
00550         case 1 : d->pushEvent("target changed");break;
00551     }
00552     if(change == 1){
00553         int chDir = rand()%2;
00554         int c=0;
00555         if(chDir==0){
00556             c =((rand()%2 -0.5)/0.5 );
00557             if( neg+ ds > Ma ) c = -1 ;
00558             if(neg -ds < ma ) c=+1 ;
00559             neg = neg + c*ds ;
00560         }
00561         if(chDir==1){
00562             c =((rand()%2 -0.5)/0.5 );
00563             if( pos+ ds > Ma ) c = -1 ;
00564             if(pos -ds < ma ) c=+1 ;
00565             pos = pos + c*ds ;
00566         }
00567     }
00568     
00569     d->pushEvent("for retrieval dots showed at negative side :"+ stringify(neg)+" positive side:" + stringify(pos));
00570     if(dir == 0){
00571         d->displayRedDotFixation(w/2+pos,h/2);
00572         d->displayRedDotFixation(w/2-neg,h/2);
00573     }
00574     if(dir == 1){
00575         d->displayRedDotFixation(w/2,h/2+pos);
00576         d->displayRedDotFixation(w/2,h/2-neg);
00577     }
00578     d->waitFrames(30);
00579     d->clearScreen();
00580     string ans = getDigitSequenceFromSubject("yn-",1,"same dots?");
00581     int res = 0 ;
00582     if(change==0 && ans.compare("y")==0) res=1;
00583     if(change==1 && ans.compare("n")==0) res=1;
00584     return res ;
00585 }
00586 
00587 int playTone(Mix_Music* tone ){
00588 if(Mix_PlayMusic(tone,0)==-1){return -1;}
00589                 while(Mix_PlayingMusic()==1){} ;
00590 return 0 ;
00591 }
00592 
00593 
00594 
00595 
00596 std::string getUsageComment(){
00597 
00598         string com = string("\nlist of arguments : \n");
00599 
00600         com += "\nlogfile=[logfilename.psy] {default = psycho-sm-or.psy}\n" ;
00601         com += "\nmemo=[a_string_without_white_space]\n";
00602         com += "\nstring-size=[>0](the size of counter string){default=4} \n";
00603         com += "\nsubject=[subject_name] \n" ;
00604         com += "\nnum-of-trials=[>1] (number of trials ) {default=10}\n";
00605         com += "\nalphabet=[a string of characters](a string of characters){default=abcdefghijklmnopqrstuvwxyz}\n";
00606         com += "\nmode=[1,2,3](1 for spatial memory task 2 for single counter task, 2 for concurrent task){default=1}\n";
00607         com += "\nsingle-dot-onset=[>1](number of frames that the single dot should be presented){default=16}\n";
00608         com += "\ndots_ISI=[>1](number of frames between dots presentations){default=16}\n";
00609         com += "\ndots-radius=[>1](the radius for circle of dots in pixel){default=100}\n";
00610         com += "\ndots-min-radius=[>0](minimum distance of dots from center){default=32}\n";
00611         com += "\ndots-max-radius=[>0] (maximum distance of dots from center){default=158}\n";
00612         com += "\nspatial-delay=[>0](number of frames for spatial memory task ){default=180}\n";
00613         com += "\n spatial-counter-ISI=[>1](numer of frames between last dot presentation and start of counter task){default=16}\n";
00614         com += "\n counter-length=[>1](total lenght of counter experiment in frames){default=300}\n";
00615         com += "\n counter-spatial-query-ISI=[>1](the delay between end of counter trail and start of spatial memory query in number of frames){default=16}\n";
00616         com += "\n spatial-query-length=[>1](the total length for showing the spatial memory test in number of frames){default=60}\n";
00617         com += "\n spatial-query-counter-query-ISI=[>1](number of frames between the unspeeded spatial memory query and showing the counter query){default=30}\n";
00618         com += "\n counter-query-length=[>1](number of frames for presenting the counter query){default=60}\n";
00619         com += "\n cue-onset-frames=[>1](){default=3}\n";;
00620         com += "\nsound-dir=[path to wav files directory]{default=..}\n";
00621         com += "\ntone1=[a valid file name](name of wav file without extension for tone 1){default=sin}\n";
00622         com += "\ntone2=[a valid file name](name of wav file without extension for tone 2){default=square}\n";
00623         com += "\ndot-shift=[>1](amount of shift in dots position in pixel)[default=16]\n";
00624         com +="\n2back-isi=[>1](inter stimuli interval between two items of 2 back test){default=30}\n";
00625         return com ;
00626 }
00627 
00628 extern "C" int main(const int argc, char** argv)
00629 {
00630 
00631         MYLOGVERB = LOG_INFO;  // suppress debug messages
00632         //let's push the initial value for the parameters
00633         argMap["experiment"]="spatial-memory-test";
00634         argMap["logfile"]="psycho-sm.psy" ;
00635         argMap["string-size"]="5" ;
00636         argMap["num-of-trials"]="2";
00637         argMap["subject"]="" ;
00638         argMap["memo"]="" ;
00639         argMap["single-dot-onset"]="16" ;
00640         argMap["dots-ISI"]="16";
00641         argMap["spatial-delay"]="180";
00642         argMap["spatial-counter-ISI"]="16";
00643         argMap["counter-length"]="300";
00644         argMap["counter-spatial-query-ISI"]="16";
00645         argMap["spatial-query-length"]="60";
00646         argMap["spatial-query-counter-query-ISI"]="30";
00647         argMap["counter-query-length"]="60";
00648         argMap["alphabet"]="djs";
00649         argMap["mode"]="0" ;
00650         argMap["cue-onset-frames"] = "3" ;
00651         argMap["sound-dir"]="..";
00652         argMap["tone1"]="sine";
00653         argMap["tone2"]="square";
00654         argMap["cue-file"]="cue1";
00655         argMap["min-tone-wait"]="50";
00656         argMap["max-tone-wait"]="80";
00657         argMap["dots-min-radius"]="32";
00658         argMap["dots-max-radius"]="158";
00659         argMap["dot-shift"] = "32";
00660         argMap["backtask-length"]="7";
00661         argMap["2back-isi"]="30";
00662         manager.addSubComponent(d);
00663         nub::soft_ref<EventLog> el(new EventLog(manager));
00664         manager.addSubComponent(el);
00665         d->setEventLog(el);
00666        
00667 
00668         if (manager.parseCommandLine(argc, argv,
00669             "at least one argument needed", 1, -1)==false){
00670                     cout<<getUsageComment()<<endl;
00671                     return(1);
00672             }
00673 
00674             for(uint i = 0 ; i < manager.numExtraArgs() ; i++){
00675                     addArgument(manager.getExtraArg(i),std::string("=")) ;
00676             }
00677 
00678             manager.setOptionValString(&OPT_EventLogFileName, argMap["logfile"]);
00679 
00680   // let's get all our ModelComponent instances started:
00681             manager.start();
00682             for(map<string,string>::iterator it= argMap.begin(); it!= argMap.end() ; ++it) d->pushEvent("arg:"+ it->first+" value:"+it->second ) ;
00683   // let's display an ISCAN calibration grid:
00684             d->clearScreen();
00685             d->displayISCANcalib();
00686             d->waitForMouseClick();
00687             d->displayText("Here the experiment starts! click to start!");
00688             d->waitForMouseClick();
00689             d->clearScreen();
00690         //let's see in what mode the user like to run the program
00691             //int mode = atoi(argMap["mode"].c_str());
00692             string masterString=argMap["alphabet"];
00693             
00694             int mode = atoi(argMap["mode"].c_str());
00695             int numOfTests = atoi(argMap["num-of-trials"].c_str()) ;
00696 
00697             int dots_ISI = atoi(argMap["dots-ISI"].c_str()) ;
00698             int dot_onset = atoi(argMap["single-dot-onset"].c_str());
00699             int dots_min_radius = atoi(argMap["dots-min-radius"].c_str());
00700             int dots_max_radius = atoi(argMap["dots-max-radius"].c_str());
00701           //  int delayForSorting = atoi(argMap["sorting-delay"].c_str());
00702             int numOfBackTaskElements = masterString.size();
00703             int lengthOfBackTask = atoi(argMap["backtask-length"].c_str());
00704             int dot_max_change = atoi(argMap["dot-shift"].c_str());
00705             int two_back_isi = atoi(argMap["2back-isi"].c_str());
00706           vector<int> orientation_task_Vector;
00707           for( int i = 0 ; i< numOfTests ; i++ ){
00708               orientation_task_Vector.push_back(0);
00709               orientation_task_Vector.push_back(1);
00710           }
00711        
00712           scramble(orientation_task_Vector);
00713             //let's do calibration        
00714            
00715             
00716             d->clearScreen();
00717             
00718             cout<< stringify(d->getWidth())<<" x "<<stringify(d->getHeight())<<endl;
00719            // Mix_Music* cueMusic = NULL;//this is the cue for the start of trial 
00720             Mix_Music* tone1 = NULL;//tone 1
00721             Mix_Music* tone2 = NULL;//tone 2
00722             map<int,Mix_Music*>  audio_map ;//we will have access to the audios for stimulus presentation using this map
00723             map<string,int>  charmap ;
00724             map<int,string>  charinvmap ;
00725             //now let's open the audio channel
00726             if( Mix_OpenAudio( 22050, MIX_DEFAULT_FORMAT, 2, 4096 ) == -1 ){
00727                   LINFO( "did not open the mix-audio") ;
00728                   return -1 ;
00729             }
00730             //now that everyting is ok, let's load the main string audio files and push them in a map
00731             for(uint i = 0; i < masterString.size() ; i++){
00732                 string str = argMap["sound-dir"]+"/"+masterString.substr(i,1)+".wav";
00733                 audio_map[i]=Mix_LoadMUS(str.c_str());
00734                 charmap[masterString.substr(i,1)]=i;
00735                 charinvmap[i]=masterString.substr(i,1) ;
00736             }
00737 
00738             for( uint i=10;i<100 ;i++ ){
00739                 charmap[stringify(i)]=i;
00740                 charinvmap[i]=stringify(i);
00741             }
00742             
00743             
00744             string tmpstr = argMap["sound-dir"]+"/"+argMap["tone1"]+".wav";
00745             tone1 = Mix_LoadMUS(tmpstr.c_str());
00746             tmpstr = argMap["sound-dir"]+"/"+argMap["tone2"]+".wav";
00747             tone2 = Mix_LoadMUS(tmpstr.c_str());
00748             
00749             int cr = numOfTests ;//this is the counter for number of rounds
00750             d->showCursor(true);
00751             d->displayText("click one of the  mouse buttons to start!");
00752             d->waitForMouseClick() ;
00753             d->showCursor(false);
00754            
00755     map<int,int> correctSPMMap;correctSPMMap[0]=0;correctSPMMap[1]=0;correctSPMMap[2]=0;correctSPMMap[3]=0;
00756     map<int,int> incorrectSPMMap;incorrectSPMMap[0]=0;incorrectSPMMap[1]=0;incorrectSPMMap[2]=0;incorrectSPMMap[3]=0;
00757         cr=0;
00758     while( cr < 2*numOfTests ){
00759 d->pushEvent("**************************************") ;
00760         int flp = rand()%4;
00761         //twoBackTaskType = 0  is reserved for non-repeating 2back task twoBackTaskType = 1 is for repeating one
00762         int twoBackTaskType = 0 ; 
00763         if(flp == 0) twoBackTaskType = 1;
00764         
00765         switch(twoBackTaskType){
00766           case 0 : d->pushEvent("non-repeating string");break;
00767           case 1 : d->pushEvent("repeating string");break;
00768         }
00769         
00770         vector<int> backTaskSequence;
00771         if(mode == 2){
00772           switch(twoBackTaskType){
00773             case 0 : backTaskSequence = getA2NonRepeatingSeqence(numOfBackTaskElements,lengthOfBackTask);break;
00774             case 1 : backTaskSequence = getA2RepeatingSequence(numOfBackTaskElements,lengthOfBackTask);break; 
00775           }
00776         }
00777         if(mode==0){
00778           switch(twoBackTaskType){
00779             case 0 : backTaskSequence = getNonRepeatingSeqenceForPassiveTest(numOfBackTaskElements,lengthOfBackTask);break;
00780             case 1 : backTaskSequence = getRepeatingSeqenceForPassiveTest(numOfBackTaskElements,lengthOfBackTask);break; 
00781           }
00782         }
00783         if(mode==1){
00784           switch(twoBackTaskType){
00785             case 0 : backTaskSequence = getA1NonRepeatingSeqence(numOfBackTaskElements,lengthOfBackTask);break;
00786             case 1 : backTaskSequence = getA1RepeatingSequence(numOfBackTaskElements,lengthOfBackTask);break; 
00787           }
00788         }
00789         
00790         d->showCursor(false);
00791         d->clearScreen() ;
00792         d->clearScreen() ;
00793         d->displayFixationBlink();
00794         
00795         d->pushEvent("trial number: "+ stringify(cr));
00796         d->pushEvent("verbal task type : " + stringify(twoBackTaskType));
00797         string seq = "";
00798          for( int i = 0 ;  i < lengthOfBackTask ; i++ ){
00799             seq += stringify(backTaskSequence[i]) ;
00800         }
00801         d->pushEvent("verbal task sequence: "+seq);
00802         d->waitFrames(30);
00803         d->displayFixation();
00804         d->waitFrames(10);
00805         d->clearScreen() ;
00806          playTone(tone1);
00807         d->pushEvent("presentation orientation: " + stringify((int)(orientation_task_Vector.at(cr)%2)));
00808                 vector<int> dotsVector = spatial_memory_task(dot_onset,dots_ISI,(int)(orientation_task_Vector.at(cr)%2),dots_min_radius,dots_max_radius);
00809         d->pushEvent("dots showed at negative:"+ stringify(dotsVector.at(0))+" positive:" + stringify(dotsVector.at(1)));
00810         
00811         d->displayFixation();
00812         d->waitFrames(15);
00813         
00814         //let's play the items of 2backtask
00815         for( int i = 0 ;  i < lengthOfBackTask ; i++ ){
00816             playTone(audio_map[backTaskSequence[i]]);
00817             d->waitFrames(two_back_isi);
00818         }
00819         int currentTask=mode;
00820    
00821         if(currentTask==0){
00822             d->pushEvent("task condition: engage");
00823             
00824         }else{
00825           d->pushEvent("task condition: igore");
00826         }
00827         //d->waitFrames(delayForSorting);
00828         playTone(tone2);
00829         d->waitFrames(30);
00830         
00831         int spMemAns = spatial_memory_retrival(dotsVector,(int)(orientation_task_Vector.at(cr)%2),dots_min_radius,dots_max_radius,dot_max_change);
00832         switch( spMemAns ){
00833             case 0: incorrectSPMMap[orientation_task_Vector.at(cr)]= incorrectSPMMap[orientation_task_Vector.at(cr)]+1;d->pushEvent("incorrect spatial memory answer");break;
00834             case 1: correctSPMMap[orientation_task_Vector.at(cr)]= correctSPMMap[orientation_task_Vector.at(cr)]+1;d->pushEvent("correct spatial memory answer");break;
00835         }
00836         
00837         string ans1 = getDigitSequenceFromSubject("yn",1,"repeated?");
00838         d->pushEvent("string answer to repeated question: "+ans1);
00839         
00840             if((ans1.compare("y")==0 && twoBackTaskType== 1) || (ans1.compare("n")==0 && twoBackTaskType==0) ){
00841                 d->pushEvent("correct verbal task :)");
00842                 d->displayText(":)");
00843                 d->waitFrames(15);
00844             }else{
00845                 d->pushEvent("incorrect verbal task :(");
00846                 d->displayText(":(");
00847                 d->waitFrames(15);
00848             }
00849         
00850         
00851         cr++;
00852         }
00853         
00854         d->pushEvent("horizontal, memory correct:"+stringify(correctSPMMap[0]));
00855             d->pushEvent("vertical, memory correct:"+stringify(correctSPMMap[1]));
00856             d->pushEvent("horizontal, memory incorrect:"+stringify(incorrectSPMMap[0]));
00857             d->pushEvent("vertical, memory incorrect:"+stringify(incorrectSPMMap[1]));
00858             
00859             d->displayText("Experiment complete. Thank you!");
00860             d->waitForMouseClick();
00861 
00862           // stop all our ModelComponents
00863             manager.stop();
00864 
00865 
00866           // all done!
00867             return 0;
00868 }
00869 
00870 #endif // INVT_HAVE_LIBSDL_IMAGE
00871 
Generated on Sun May 8 08:04:13 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3