import socket;
import sys;
import os;
from friendEngine import Engine;

engine = Engine();

# creating the main socket variable
engine.connectEngine();

# creating a session and getting sessionID
engine.createSession();
print("sessionID received = %s" % (engine.sessionID));

# reading the actual run number
if (len(sys.argv)>1):
   option = str(sys.argv[1]);
else:
   option = "1";

PIPELINE = int(option);

# changing the MNI mask
engine.setVariable('MNIMask', 'studydirhmat_spm_PM.nii.gz');   

# changing the MNI template
engine.setVariable('MNITemplate', 'studydirMNI152_T1_1mm_brain.nii.gz');

if (PIPELINE > 1):
   # changing the mask type
   engine.setVariable('ActivationLevelMaskType', '1');   
class PyGameVisualizer(RoiVisualizer):
    """ Handle pygame setup"""
    designObj = design()
    engine = Engine()
    cueDisplayTime = 1
    cueDisplaying = False
    useEngine = False
    delay = 2  # this is in units of TRs - e.g., 1 means get feedback from 1 TR ago
    feedbackMapping = []
    logfile = ''

    def __init__(self, TR=2, timeout=0):
        super(PyGameVisualizer, self).__init__(timeout)
        self.bg_color = (0, 0, 0)
        self.clock = pygame.time.Clock()
        self.state = 0
        self.tic = 0
        self.rate = 100
        self.period = 2000  # how often to refresh state
        self.pygame_live = False
        self.TR = 2
        self.lastVolumeIndex = -1
        self.lastBlockStartTime = 0
        self.dry_run = True

    def log_eventtime(self, eventname='event'):
        if self.logfile:
            if os.path.isfile(self.logfile):
                f = open(self.logfile, 'a+')
            else:
                f = open(self.logfile, 'w')
            f.write(str(datetime.now()) + ', ' + eventname + '\n')
            f.close()

    def setBaseline(self, condition):
        self.designObj.baselineCondition = condition

    def readDesignFile(self, filename):
        self.designObj.readFile(filename)

    def halt(self):
        self.alive = False
        if self.pygame_live:
            pygame.display.quit()
            pygame.quit()
            self.pygame_live = False

    def loadFont(self, fontName='Comic Sans MS', size=30):
        self.myfont = pygame.font.SysFont(fontName, size)

    def start_display(self, width=800, height=600, title='NeuroFeedback'):
        pygame.init()
        display_width, display_height = (int(width), int(height))
        self.display_width = display_width
        self.display_height = display_height
        self.screen = pygame.display.set_mode((display_width, display_height),
                                              pygame.RESIZABLE)
        pygame.display.set_caption(title)
        self.loadFont()
        self.pygame_live = True

    def text_objects(self, text, color=white):
        textSurface = self.myfont.render(text, True, color)
        return textSurface, textSurface.get_rect()

    def start(self):
        self.lastVolumeIndex = -1
        if not self.dry_run:
            start_scanner()
            print("STARTED SCANNER")
        self.startTime = time.time()
        self.log_eventtime('start time')
        if (not self.pygame_live):
            self.start_display()

    def connectEngine(self):
        # creating the main socket variable
        self.engine.connectEngine()

        # creating a session and getting sessionID
        self.engine.createSession()
        print("sessionID received = %s" % (self.engine.sessionID))

    # right now its rigged to the an example scanned. We need to generalize it
    # plugInType=3 is the motor plugin
    def configureEngine(self, option=1, plugInType=3, additionalFeedbacks=1):
        # initiating processing
        self.engine.doTrain = False
        self.engine.doGLM = True
        self.engine.doFeatureSelection = True
        self.engine.additionalFeedbacks = additionalFeedbacks
        self.engine.setPlugInInformation(plugInType)
        self.engine.phase = 1
        self.engine.actualVolume = 1

# call the finalization engine steps

    def processEndRun(self):
        self.engine.processEndRun()

    def processPhase(self, feedbackRun=True):
        self.engine.processPhase(feedbackRun)

# only leave the loop when the preproc phase is finished

    def startEngine(self, feedbackRun=True):
        while (self.engine.phase != 2):
            self.engine.processPhase(feedbackRun, True)
            time.sleep(self.engine.TR / 7)
        self.engine.processPhase(feedbackRun, True)

    def actualVolumeIndex(self):
        return ((time.time() - self.startTime) // self.TR) + 1

    def hasNewVolume(self):
        actualIndex = self.actualVolumeIndex()
        if (actualIndex > self.lastVolumeIndex):
            self.lastVolumeIndex = actualIndex
            return True
        else:
            return False

    def displayCue(self):
        self.clearDisplay()
        self.message_display(self.designObj.actualCondition())
        self.cueDisplaying = True

    def clearDisplay(self, color=black):
        self.screen.fill(color)

    def message_display(self, text):
        TextSurf, TextRect = self.text_objects(text)
        TextRect.center = ((self.display_width / 2), (self.display_height / 2))
        self.screen.blit(TextSurf, TextRect)
        pygame.display.update()

    def refreshDisplay(self):
        if (self.cueDisplaying) and (
            (time.time() - self.lastBlockStartTime) > self.cueDisplayTime):
            self.clearDisplay()
            self.message_display(" ")
            self.cueDisplaying = False

    def displayActivation(self):
        return True

# verifies the scan end

    def endScan(self):
        return self.designObj.scanEnd(self.actualVolumeIndex())

    def changeInternalState(self):
        index = self.actualVolumeIndex()
        if (self.designObj.blockStart(index)):
            self.lastBlockStartTime = time.time()
            self.designObj.setActualBlock(index)
            self.displayCue()
        else:
            if (not self.cueDisplaying) and (self.designObj.activationBlock()):
                self.displayActivation()
import socket;
import sys;
import os;
from friendEngine import Engine;

engine = Engine();

# creating the main socket variable
engine.connectEngine();

# creating a session and getting sessionID
engine.createSession();
print("sessionID received = %s" % (engine.sessionID));

# reading the actual run number
if (len(sys.argv)>1):
   option = str(sys.argv[1]);
else:
   option = "1";

PIPELINE = int(option);
 
# changing the MNI mask
engine.setVariable('MNIMask', 'studydirhmat_spm_final.nii');   

# changing the MNI template
engine.setVariable('MNITemplate', 'studydirMNI152_T1_1mm_brain.nii.gz');

# changing the directory of the volumes
engine.setVariable('Prefix', 'outputdirRUN0' + option + os.path.sep + 'DRIN-');   
import socket;
import sys;
import os;
from friendEngine import Engine;

engine = Engine();

# creating the main socket variable
engine.connectEngine();

# creating a session and getting sessionID
engine.createSession();
print("sessionID received = %s" % (engine.sessionID));

# reading the actual run number
if (len(sys.argv)>1):
   option = str(sys.argv[1]);
else:
   option = "1";

PIPELINE = int(option);
 
# changing the MNI mask
engine.setVariable('MNIMask', 'studydirhmat_spm_final.nii');   

# changing the MNI template
engine.setVariable('MNITemplate', 'studydirMNI152_T1_1mm_brain.nii.gz');

# changing the directory of the volumes
engine.setVariable('Prefix', 'outputdirRUN0' + option + os.path.sep + 'DRIN-');   
Exemple #5
0
import socket;
import sys;
import os;
from friendEngine import Engine;

engine = Engine();

# creating the main socket variable
engine.connectEngine();

# creating a session and getting sessionID
engine.createSession();
print("sessionID received = %s" % (engine.sessionID));

# reading the actual run number
if (len(sys.argv)>1):
   option = str(sys.argv[1]);
else:
   option = "1";

PIPELINE = int(option);
 
if (PIPELINE > 1):
   # changing the directory of the volumes
   engine.setVariable('Prefix', 'outputdirRUN0' + option + os.path.sep + 'DRIN-');   
   # changing the current suffix
   engine.setVariable('ModelRunSuffix', 'RUN01');   
   # changing the current suffix
   engine.setVariable('CurrentRunSuffix', 'RUN0' + option);   

   # not using all the subject mask
Exemple #6
0
import socket
import sys
import os
from friendEngine import Engine

engine = Engine()

# creating the main socket variable
engine.connectEngine()

# creating a session and getting sessionID
engine.createSession()
print("sessionID received = %s" % (engine.sessionID))

# reading the actual run number
if (len(sys.argv) > 1):
    option = str(sys.argv[1])
else:
    option = "1"

PIPELINE = int(option)

# changing the MNI mask
engine.setVariable('MNIMask', 'studydirhmat_spm_final.nii')

# changing the MNI template
engine.setVariable('MNITemplate', 'studydirMNI152_T1_1mm_brain.nii.gz')

# changing the directory of the volumes
engine.setVariable('Prefix', 'outputdirRUN0' + option + os.path.sep + 'DRIN-')
Exemple #7
0
import socket
import sys
import os
from friendEngine import Engine

engine = Engine()

# creating the main socket variable
engine.connectEngine()

# creating a session and getting sessionID
engine.createSession()
print("sessionID received = %s" % (engine.sessionID))

# reading the actual run number
if (len(sys.argv) > 1):
    option = str(sys.argv[1])
else:
    option = "1"

PIPELINE = int(option)

# changing the MNI mask
engine.setVariable('MNIMask', 'studydirhmat_spm_PM.nii.gz')

# changing the MNI template
engine.setVariable('MNITemplate', 'studydirMNI152_T1_1mm_brain.nii.gz')

if (PIPELINE > 1):
    # changing the mask type
    engine.setVariable('ActivationLevelMaskType', '1')