Example #1
0
 def __init__( self, config, logger, playerPos3d, 
     playerDepthSensor, playerSonar, playerFrontCamera ):
     
     ControlScript.__init__( self, config, logger, 
         playerPos3d, playerCompass = None, 
         playerDepthSensor = playerDepthSensor, playerSonar = playerSonar, 
         playerFrontCamera = playerFrontCamera )
         
     self.sonarScanner = SonarScanner( 
         logger, playerSonar, 
         playerDepthSensor, config )
     
     self.setState( self.STATE_PERFORMING_SCANS )
     self.lastCameraFrameTime = 0.0
     self.lastSonarFrameTime = 0.0
     self.lastCameraImageSaveTime = 0.0
     
     self.sonarImagesDir = string.Template( config.sonarImagesDir ).safe_substitute( os.environ )
     self.cameraImagesDir = string.Template( config.cameraImagesDir ).safe_substitute( os.environ )
     
     self.sonarScanner.setSonarConfig( 
         self.config.IC_Script_sonarRange, 
         self.config.IC_Script_sonarNumBins, 
         self.config.IC_Script_sonarGain )
     self.sonarScanner.setScanAngleRange( 
         Maths.degToRad( self.config.IC_Script_sonarScanStartAngleDegrees ), 
         Maths.degToRad( self.config.IC_Script_sonarScanEndAngleDegrees ) )
         
     self.bgrImage = None
Example #2
0
    def update( self ):
        
        curTime = time.time()
        
        if self.closeEnoughFlag == 0.0:
            if self.arbitrator.atDesiredYaw() and self.arbitrator.atDesiredDepth():
                self.arbitrator.update( self.movingLinearSpeed )
                self.forwardTimer = time.time()
            
            if curTime - self.forwardTimer >= 30.0:         # temporary value - determined by test
                self.arbitrator.update( self.noLinearSpeed )
                self.closeEnoughFlag == 1.0

        else:
            angle = self.initYawAngle
            lefScanAngleRange = self.initYawAngle - math.pi/2
            rightScanAngleRange = self.initYawAngle + math.pi/2
            
            while angle >= lefScanAngleRange and self.detectOrangeFlag == 0.0:
                self.arbitrator.setDesiredYawAngle( angle )
                self.arbitrator.update( self.noLinearSpeed )
                self.ballFinder.run( )
                self.detectOrangeFlag = self.ballFinder.target_aquired
                if self.detectOrangeFlag == 0.0:
                    angle -= Maths.degToRad( 10 )
                            
            while angle <= rightScanAngleRange and self.detectOrangeFlag == 0:
                self.arbitrator.setDesiredYawAngle( angle )
                self.arbitrator.update( noLinearSpeed )
                self.ballFinder.run( )
                self.detectOrangeFlag = self.ballFinder.target_aquired
                if self.detectOrangeFlag == 0.0:
                    angle += Maths.degToRad( 10 )
            
            if self.detectOrangeFlag == 1.0:
                compassYawAngle = self.playerCompass.pose.pyaw
                self.ballFinder.run( )
                ballX = self.ballFinder.corr_x
                self.ballArea = self.ballFinder.pixel_count
                #if self.ballArea > twoMetresArea and self.ballArea < onefiveMetresArea:
                    #distance = 2.0
                #elif self.ballArea > onefiveMetresArea and self.ballArea < oneMetresArea:
                    #distance = 1.5
                #elif self.ballArea > onefiveMetresArea and self.ballArea < oneMetresArea:
                    #distance = 1.0
                while ballX > 10 or ballX < -10:
                    if ballX > 0:
                        newYaw = compassYawAngle + Maths.degToRad( 5 )
                    elif ballX < 0:
                        newYaw = compassYawAngle - Maths.degToRad( 5 )
                    self.arbitrator.setDesiredYaw( newYaw )
                    self.arbitrator.update ( movingLinearSpeed )    # don't wait for the yaw control, because it's a rough estimate anyway
                    if self.ballArea > self.oneMetresArea:               # needs to be set
                        self.arbitrator.setDesiredDepth( self.cuttingDepth )
                        self.arbitrator.update( noLinearSpeed )
                        if atDesiredDepth():
                            self.arbitrator.update( movingLinearSpeed )
                            #"activate cutter"
                            
Example #3
0
def getarrs(num_cond, num_rows, num_epochs=D.numtrialepochs):
    all_avgs = Maths.nans(
        (D.numareas, num_rows, num_cond, num_epochs, D.n_timepoints))
    all_sem = Maths.nans(
        (D.numareas, num_rows, num_cond, num_epochs, D.n_timepoints))
    sigclusters = Maths.nans(
        (D.numareas, num_rows, num_epochs, D.n_timepoints))

    return all_avgs, all_sem, sigclusters
Example #4
0
 def __init__( self, playerPos3D, playerCompass, playerDepthSensor, playerSimPos3D = None, logger = None ):
     
     self.playerPos3D = playerPos3D
     self.playerCompass = playerCompass
     self.playerDepthSensor = playerDepthSensor
     self.playerSimPos3D = playerSimPos3D
     self.logger = logger
     
     self.pitchController = PitchControl( self.playerPos3D,
         self.playerCompass, self.playerSimPos3D )
     self.yawController = YawControl( self.playerPos3D,
         self.playerCompass, self.playerSimPos3D )
     self.depthController = DepthControl( self.playerPos3D,
         self.playerDepthSensor, self.playerSimPos3D )
     self.leftMotorUncontrolled = False
     self.rightMotorUncontrolled = False
     self.frontMotorUncontrolled = False
     self.backMotorUncontrolled = False
     
     self.depthEpsilon = 10
     self.yawEpsilon = Maths.degToRad( 2.5 )
     
     self.lastTime = time.time()
     self.lastDepth = -1.0
     self.depthControlDisabled = False
     self.numIdenticalDepthReadings = 0
Example #5
0
    def update(self):

        collision_list = arcade.check_for_collision_with_list(
            self, self.level.entities)

        for entity in collision_list:
            if isinstance(entity, Projectile):
                self.hurt(entity.damage, entity.change_x)

        player = self.level.player

        dist = Maths.manhattan_dist(self.center_x, self.center_y,
                                    player.center_x, player.center_y)
        if dist <= self.range:
            self.move_to(player)
        else:
            self.wander()

        if self.curr_invis_frame > 0 and self.curr_invis_frame % 12 < 6:
            self.texture = Textures.get_texture(15, 15)
        else:
            self.texture = self.tex

        if self.intersects(player):
            player.hurt(self.damage, self.change_x)

        super().update()
Example #6
0
    def __init__( self, config, logger, playerPos3D, playerCompass, 
        playerDepthSensor, playerSonar = None ):
        
        self.arbitrator = Arbitrator( playerPos3D, playerCompass, playerDepthSensor )
        self.ballFinder = bouy_finder.BouyFinder()

        self.pitchAngle = -5.0                       # temporary value - zero pitch
        self.initYawAngle = Maths.degToRad( 85.0 )   # temporary value - heading towards the wall
        self.initDepth = 7460.0                      # temporary value - 0.5 metres beneath the surface
        self.cuttingDepth = 7480.0
        self.noLinearSpeed = 0.0
        self.movingLinearSpeed = 1.0
        self.arbitrator.setDesiredState( self.pitchAngle, self.initYawAngle, self.initDepth )
        
        # AB: Disabled for now as this script will be running inside another script
        #self.arbitrator.update( self.noLinearSpeed )
        
        self.oneMetresArea = 2700                    # rough estimate        
        
        self.forwardTimer = time.time()
        self.closeEnoughFlag = 0.0
        self.detectOrangeFlag = 0.0
        self.ballArea = 0.0
        self.lastBallArea = 0.0
        self.image = None
Example #7
0
def analysecell(counter, output_betas, data, cell):
    # Print current progress in console
    Utils.updatecounts(counter, cell, data.n)

    # Load trial data
    trialdata = data.behavdata[cell]

    # Lets do a regression for the reward values they are given
    x = trialdata.rewgiven

    # Let's compare rare versus common trials
    common_trials = trialdata.transition == 1
    rare_trials = trialdata.transition == 2

    # Put the different conditions into a single list
    conds = (common_trials, rare_trials)

    # Loop through each trial epoch that we are interested in
    for i_epoch, epoch in enumerate(D.epochs):

        # Load the y (cell firing rate) for that epoch
        y = data.generate_epoch_norm(cell, epoch)

        # Loop through each condition
        for i_cond, cond in enumerate(conds):

            # Get data from just trials for this condition
            x_cond = x[cond]
            y_cond = y[cond]

            # Do regression on x and y data for this condition and get the beta value
            beta_val = Maths.regression(x_cond, y_cond)

            # Store the result in the output array
            output_betas[0, i_cond, i_epoch, cell] = beta_val
Example #8
0
 def __init__( self, config, logger, playerPos3d, playerCompass, 
     playerDepthSensor, playerSonar, playerFrontCamera ):
     
     ControlScript.__init__( self, config, logger, 
         playerPos3d=playerPos3d, playerCompass=playerCompass, 
         playerDepthSensor=playerDepthSensor, playerSonar=playerSonar, 
         playerFrontCamera=playerFrontCamera )
     
     self.RUN_DEPTH = self.config.QR_runDepth
     self.FORWARD_SPEED = self.config.QR_forwardSpeed
     self.START_DELAY_TIME = self.config.QR_startDelayTime
     self.END_DELAY_TIME = 40.0
     self.MOVE_FORWARD_TIME = self.config.QR_moveForwardTime  # Will probably be caught by the net
     self.HEADING_TO_GATE_DEGREES = self.config.QR_headingToGateDegrees
 
     self.USE_IMAGE_CAPTURE = self.config.useImageCapture
     
     self.imageCaptureScript = ImageCaptureScript( self.config, self.logger,
         self.playerPos3d, self.playerDepthSensor, self.playerSonar, self.playerFrontCamera )
     
     self.motorKiller = KillMotors( self.playerPos3d )
     
     # Setup the arbitrator
     self.arbitrator = Arbitrator( playerPos3d, playerCompass, playerDepthSensor )
     self.arbitrator.setDesiredPitch( Maths.degToRad( -4.0 ) )
     self.arbitrator.setControlGains(
         pitchKp=self.config.pitchKp, 
         pitchKi=self.config.pitchKi, pitchiMin=self.config.pitchiMin, pitchiMax=self.config.pitchiMax, 
         pitchKd=self.config.pitchKd,
         yawKp=self.config.yawKp, 
         yawKi=self.config.yawKi, yawiMin=self.config.yawiMin, yawiMax=self.config.yawiMax, 
         yawKd=self.config.yawKd,
         depthKp=self.config.depthKp, 
         depthKi=self.config.depthKi, depthiMin=self.config.depthiMin, depthiMax=self.config.depthiMax,
         depthKd=self.config.depthKd )
     self.arbitrator.setEpsilons( 
         self.config.arbitratorDepthEpsilon, 
         Maths.degToRad( self.config.arbitratorYawEpsilonDegrees ) )
     
     # Clear timers
     self.waitTimer = 0.0
     self.delayTimer = 0.0
     
     # Move into the first state
     self.linearSpeed = 0.0
     self.delayTimer = time.time()
     self.setState( self.STATE_WAITING_TO_START )    
Example #9
0
 def configureSonar( self ):
     
     subHeading = self.playerCompass.pose.pyaw
     angleOffset = subHeading - self.POOL_HEADING    # Diff from pool to sub heading
     
     # Scan when the sub is aligned with the pool to find the bottom right corner
     scanStartAngle = self.SONAR_HEADING_OFFSET + self.IDEAL_SCAN_START_ANGLE - angleOffset
     scanEndAngle = self.SONAR_HEADING_OFFSET + self.IDEAL_SCAN_END_ANGLE - angleOffset
 
     scanStartAngle = Maths.normaliseAngle( scanStartAngle, 0.0 )
     scanEndAngle = Maths.normaliseAngle( scanEndAngle, 0.0 )
     
     #print "From", Maths.radToDeg( startScanAngle ), "To", Maths.radToDeg( endScanAngle )
     
     # Configure the sonar scanner
     self.sonarScanner.setSonarConfig( self.SCAN_RANGE, self.NUM_BINS, self.GAIN )
     self.sonarScanner.setScanAngleRange( scanStartAngle, scanEndAngle )
    def __init__(self, function_to_run, run_sig_test, num_conds, num_rows, maintitle, ytitles, savefolder, trace_names, plotfunc, doavgs=True):
        timer = TimeFunction.Timer()

        m = MyManager()
        m.start()
        all_avgs, all_sem, sigclusters = Utils.getarrs(num_conds, num_rows)

        for i_area, area in enumerate(D.areas):

            data = ImportData.EntireArea(area)

            if D.domultiproc:
                counter = m.np_zeros(data.n)
                betas = m.np_zeros((num_rows, num_conds, D.numtrialepochs, data.n, D.n_timepoints))
                pool = Pool(D.n_cores)
                func = partial(function_to_run, counter, betas, data)
                run_list = range(data.n)
                pool.map(func, run_list)
                pool.close()
                betas = np.array(betas)
            else:
                counter = np.zeros(data.n)
                betas = np.zeros((num_rows, num_conds, D.numtrialepochs, data.n, D.n_timepoints))
                for i in range(data.n):
                    function_to_run(counter, betas, data, i)

            for i_row, row_epoch in enumerate(betas):
                for i_choice, cond_epoch in enumerate(row_epoch):  # Now trialepochs x cell x timepoints
                    for i_trial, trial_epoch in enumerate(cond_epoch):  # Now cell by timepoints
                        if doavgs:
                            avg = np.nanmean(trial_epoch, axis=0)
                            sem = Maths.sem(trial_epoch)
                            all_avgs[i_area, i_row, i_choice, i_trial] = avg
                            all_sem[i_area, i_row, i_choice, i_trial] = sem
                        else:
                            for i_t, t in enumerate(trial_epoch.T):
                                all_avgs[i_area, i_row, i_choice, i_trial, i_t] = int(len(t[t<0.05])/len(t) * 100)  # Num cells significant
                            all_sem[i_area, i_row, i_choice, i_trial] = 0


                if run_sig_test:
                    sigclusters[i_area, i_row] = Maths.permtest(row_epoch)

        print(timer.elapsedtime())

        plotfunc(all_avgs, all_sem, sigclusters, trace_names, savefolder, ytitles, maintitle, True)
Example #11
0
    def update( self ):
        
        curTime = time.time()
        
        if self.USE_IMAGE_CAPTURE \
            and self.state != self.STATE_WAITING_TO_START:
            self.imageCaptureScript.update()
        
        if self.state == self.STATE_WAITING_TO_START:
            
            timeDiff = curTime - self.delayTimer
            print "timeDiff is", timeDiff, "delay is", self.START_DELAY_TIME
            
            if curTime - self.delayTimer >= self.START_DELAY_TIME:
                self.logger.logMsg( "Going to " + str( self.RUN_DEPTH ) )
                self.arbitrator.setDesiredDepth( self.RUN_DEPTH )
                self.linearSpeed = 0.0
                self.setState( self.STATE_DIVING )
            
        elif self.state == self.STATE_DIVING:

            if self.arbitrator.atDesiredDepth():
                self.arbitrator.setDesiredYaw( 
                    Maths.degToRad( self.HEADING_TO_GATE_DEGREES ) )
                self.setState( self.STATE_TURNING_TO_GATE_1 )
            
        elif self.state == self.STATE_TURNING_TO_GATE_1:
            
            if self.arbitrator.atDesiredYaw():
                self.linearSpeed = self.FORWARD_SPEED
                self.driveTimer = time.time()
                self.setState( self.STATE_DRIVING_THROUGH_GATE_1 )
                        
        elif self.state == self.STATE_DRIVING_THROUGH_GATE_1:
            
            if curTime - self.driveTimer >= self.MOVE_FORWARD_TIME:
                self.linearSpeed = 0.0
                self.delayTimer = time.time()
                self.setState( self.STATE_SURFACING )
            
        elif self.state == self.STATE_SURFACING:

            # Wait for a bit and then exit the program
            timeDiff = curTime - self.delayTimer    
            if timeDiff >= self.END_DELAY_TIME:
                sys.exit( 0 )   # Quit
        else:
            self.logger.logError( "Unrecognised state - ({0}) - surfacing".format( self.state ) )
            self.linearSpeed = 0.0
            self.delayTimer = time.time()
            self.setState( self.STATE_SURFACING )
        
        if self.state == self.STATE_SURFACING:
            # Kill motors to come up and end mission
            self.motorKiller.update()
        else:
            self.arbitrator.update( self.linearSpeed )
Example #12
0
 def __init__( self, config, logger, playerPos3d, playerCompass, 
     playerDepthSensor, playerSonar, playerFrontCamera ):
     
     ControlScript.__init__( self, config, logger, 
         playerPos3d=playerPos3d, playerCompass=playerCompass, 
         playerDepthSensor=playerDepthSensor, playerSonar=playerSonar, 
         playerFrontCamera=playerFrontCamera )
     
     self.UP_DEPTH = self.config.B_upDepth
     self.DOWN_DEPTH = self.config.B_downDepth
     self.START_DELAY_TIME = self.config.B_startDelayTime
     
     self.USE_IMAGE_CAPTURE = self.config.useImageCapture
     
     self.imageCaptureScript = ImageCaptureScript( self.config, self.logger,
         self.playerPos3d, self.playerDepthSensor, self.playerSonar, self.playerFrontCamera )
     
     # Setup the arbitrator
     self.arbitrator = Arbitrator( playerPos3d, playerCompass, playerDepthSensor )
     self.arbitrator.setDesiredPitch( Maths.degToRad( -4.0 ) )
     self.arbitrator.setControlGains(
         pitchKp=self.config.pitchKp, 
         pitchKi=self.config.pitchKi, pitchiMin=self.config.pitchiMin, pitchiMax=self.config.pitchiMax, 
         pitchKd=self.config.pitchKd,
         yawKp=self.config.yawKp, 
         yawKi=self.config.yawKi, yawiMin=self.config.yawiMin, yawiMax=self.config.yawiMax, 
         yawKd=self.config.yawKd,
         depthKp=self.config.depthKp, 
         depthKi=self.config.depthKi, depthiMin=self.config.depthiMin, depthiMax=self.config.depthiMax,
         depthKd=self.config.depthKd )
     self.arbitrator.setEpsilons( 
         self.config.arbitratorDepthEpsilon, 
         Maths.degToRad( self.config.arbitratorYawEpsilonDegrees ) )
     
     # Clear timers
     self.waitTimer = 0.0
     self.delayTimer = 0.0
     
     # Move into the first state
     self.linearSpeed = 0.0
     self.delayTimer = time.time()
     self.setState( self.STATE_WAITING_TO_START )    
Example #13
0
 def testLogAddQuality(self):
     for trial in xrange(0, 1000):
         d = r.random() * 100
         d2 = d - 20 * r.random()
         d3 = d
         d4 = d2
         if r.random() > 0.5:
             d3 = d2
             d4 = d
         self.assertAlmostEquals(Maths.logAddQuality(d3, d4), d \
                 + math.log(1 + math.exp(d2 - d)), 0)
Example #14
0
 def testLogAddQuality(self):
     for trial in xrange(0, 1000):
         d = r.random() * 100
         d2 = d - 20 * r.random()
         d3 = d
         d4 = d2
         if r.random() > 0.5:
             d3 = d2
             d4 = d
         self.assertAlmostEquals(Maths.logAddQuality(d3, d4), d \
                 + math.log(1 + math.exp(d2 - d)), 0)
Example #15
0
def analysecell(counter, out_betas, data, cell):
    Utils.updatecounts(counter, cell, data.n)

    td = data.behavdata[cell]

    masks = (D.get_A_AR_trials(td), D.get_A_AC_trials(td), D.get_A_BR_trials(td), D.get_A_BC_trials(td))

    x = td.previousreward

    for i_epoch, epoch in enumerate(D.epochs):
        y = data.generatenormalisedepoch(cell, epoch)

        for i_mask, mask in enumerate(masks):
            out_betas[0, i_mask, i_epoch, cell] = Maths.regression(x[mask], y[mask])
def run_decoder(data, unique_func, p, accs_perms, do_permtest,
                i_cellselection):

    if p.peak_epoch is not None:

        # Need to train decoder first
        x_train, y_train = Maths.collect_trials_for_decoding(
            data, unique_func, p, True, do_permtest)
        cond_to_train = 0
        _, dec_to_test = Maths.decode_epoch_traintestsplit(x_train,
                                                           y_train,
                                                           p.peak_epoch,
                                                           cond_to_train,
                                                           y_train.shape[-1],
                                                           peak_tp=p.peak_tp)

    else:

        dec_to_test = None

    x_all, y_all = Maths.collect_trials_for_decoding(data, unique_func, p,
                                                     False, do_permtest)

    # Now we have all our data with equal number of samples for each label and each cell, we can run the decoder
    if p.stability:
        scores = Maths.decode_stability(x_all, y_all)
    else:
        scores = Maths.decode_across_epochs(x_all, y_all, dec_to_test,
                                            p.train_across_conds, do_permtest)

    accs_perms[..., i_cellselection] = scores

    if not do_permtest:
        print(f'Progress: {i_cellselection + 1}/{D.dec_numiters}')

    return np.array(accs_perms)
Example #17
0
    def update( self ):
        
        curTime = time.time()
        
        if self.state == self.STATE_GATHERING_DATA:

            if curTime - self.lastLogTime >= self.TIME_BETWEEN_DATA_LOGS:
                depth = self.playerDepthSensor.pos
                yaw = self.playerCompass.pose.pyaw
                pitch = self.playerCompass.pose.ppitch
                roll = self.playerCompass.pose.proll
                self.logger.logMsg( "DataLogger: Depth: {0}, Yaw: {1:2.3f}, Pitch: {2:2.3f}, Roll: {3:2.3f}".format(
                    depth, Maths.radToDeg( yaw ), Maths.radToDeg( pitch ), Maths.radToDeg( roll ) ) )
                
                self.lastLogTime = curTime
                
                # Check to make sure that the depth sensor hasn't crashed
                if depth == self.lastDepth:
                    self.numIdenticalDepthReadings += 1
                else:
                    self.numIdenticalDepthReadings = 0
                
                if self.numIdenticalDepthReadings > 20:
                    self.logger.logError( "Depth sensor unchanged" )
Example #18
0
    def update( self ):
        
        curTime = time.time()
        
        if self.state == self.STATE_DIVING:

            if self.arbitrator.atDesiredDepth():
                self.arbitrator.setDesiredYaw( Maths.degToRad( 0.0 ) )
                self.setState( self.STATE_TURNING_TO_GATE_1 )
                
        elif self.state == self.STATE_SURFACING:
            
            if self.arbitrator.atDesiredDepth():
                self.arbitrator.setDesiredDepth( self.scanDepth )
                self.setState( self.STATE_FINISHED )
            
        elif self.state == self.STATE_FINISHED:
            # Nothig to do
            pass
        else:
            self.logger.logError( "Unrecognised state - surfacing" )
            self.setState( self.STATE_SURFACING )
        
        self.arbitrator.update( self.linearSpeed )
    def __new__(cls,
                function_to_run,
                trace_names=None,
                epochs=D.epochs,
                epochnames=D.names_epochs,
                stability=False,
                train_epochs=None,
                peak_epoch=None,
                peak_tp=None,
                peak_halfwidth=None,
                train_across_conds=False,
                maintitle='Decoder',
                savefolder='dec/temp'):
        # stability = Test stability of decoding over time and return 2D matrix of accuracies
        # train_epochs = For stability, train and test on two different epochs and conds
        # peak_epoch, peak_tp, peak_halfwidth = train on this epoch, at this timepoint, at this halfwidth, and test on all others
        # For this condition, you must supply an extra condition and mask, to be used as the peak training data, in index 0
        # train_across_conds = collapse across conds to train decoder and then test within each cond's held out data

        timer = TimeFunction.Timer()

        # Log all parameters in single object to pass around functions
        p = types.SimpleNamespace()
        p.epochs = epochs
        p.n_epochs = len(epochs)
        p.n = D.numareas
        p.stability = stability
        p.train_epochs = train_epochs
        p.peak_epoch = peak_epoch
        p.peak_tp = peak_tp
        p.peak_halfwidth = peak_halfwidth if peak_halfwidth is not None else D.smooth_window_halfwidth
        p.train_across_conds = train_across_conds

        if p.stability and p.num_conds > 2:
            raise Exception('Stability only works with one or two conds')

        if stability:
            p.n_pnts = p.n_epochs * (D.n_timepoints + 1)
            accs_all = Maths.nans((p.n, p.n_pnts, p.n_pnts))
            sems_all, sigclusters = np.copy(accs_all), np.copy(accs_all)
            p.num_conds = 1
            if train_epochs is not None:
                p.num_conds += 1
        else:
            p.num_conds = len(trace_names)
            if peak_epoch is not None:
                p.num_conds += 1
                trace_names.insert(0, 'Train data')
            accs_all, sems_all, _ = Utils.getarrs(p.num_conds, 2, p.n_epochs)
            sigclusters = np.empty((p.n, p.num_conds))

        dists_all = np.zeros((p.n, p.num_conds, 300))

        for i_area in range(p.n):

            print(f'Analysing {D.areanames[i_area]}...')

            analysearea(function_to_run, accs_all, sems_all, sigclusters,
                        dists_all, p, i_area)

        if not stability:
            ylabel = 'Accuracy (%)'
            trials_per_label = np.array(np.nanmin(dists_all[0], axis=1),
                                        dtype=int)
            trials_per_label[
                trials_per_label < D.dec_minsamples] = D.dec_minsamples
            trace_names = [
                t + f' ({n})' for t, n in zip(trace_names, trials_per_label)
            ]

            Plot.GeneralAllAreas(accs_all[:, 0:1],
                                 sems_all[:, 0:1],
                                 sigclusters[:, 0:1],
                                 trace_names,
                                 savefolder,
                                 maintitle,
                                 scale_sig=False,
                                 names_epochs=epochnames,
                                 show_sig=False)

            Plot.DecoderSignificant_AllAreas(accs_all,
                                             sems_all,
                                             sigclusters,
                                             trace_names,
                                             savefolder,
                                             maintitle,
                                             peak_epoch=peak_epoch,
                                             peak_tp=peak_tp,
                                             names_epochs=epochnames,
                                             scale_sig=False,
                                             show_sig=False)

            if D.dec_do_perms:
                Plot.DecoderPermSig_AllAreas(accs_all,
                                             sems_all,
                                             sigclusters,
                                             trace_names,
                                             savefolder,
                                             ylabel,
                                             maintitle,
                                             scale_sig=False,
                                             show_sig=True)

            Plot.PlotDist(dists_all, savefolder)

        else:

            Plot.PlotDecStab(accs_all, savefolder, epochnames)

        print(timer.elapsedtime())

        return accs_all, sems_all, sigclusters, trace_names, savefolder, maintitle
Example #20
0
avgs = wrapper(avgs, overall)

# Now plot result
f, ax = plt.subplots(1, figsize=(5, 3))  #, 2)
for i, monk in enumerate(('Subject J', 'Subject C')):
    # Make holding arrays
    numpnts = 12
    avg = np.empty(numpnts)
    avg.fill(np.nan)
    sem = np.copy(avg)

    # Plot Common transitions
    subj_n = len(avgs[i][(avgs[i] != 0)[:, 0]])
    avg[i:6:3] = np.mean(avgs[i][(avgs[i] != 0)[:, 0]], axis=0)[0:2]
    sem[i:6:3] = Maths.sem(avgs[i][(avgs[i] != 0)[:, 0]])[0:2]
    ax.bar(range(numpnts),
           avg,
           0.9,
           label=f'{monk} ({subj_n})',
           color=f'C{i}',
           edgecolor='black')
    ax.errorbar(range(numpnts), avg, sem, color=f'black', elinewidth=2)

    # Plot Rare transitions
    avg = np.empty(numpnts)
    avg.fill(np.nan)
    sem = np.copy(avg)
    avg[i + 6::3] = np.mean(avgs[i][(avgs[i] != 0)[:, 0]], axis=0)[2:]
    sem[i + 6::3] = Maths.sem(avgs[i][(avgs[i] != 0)[:, 0]])[2:]
    ax.bar(range(numpnts), avg, 0.9, color=f'C{i}', edgecolor='black')
Example #21
0
import Maths

print(Maths.add(5, 7))
Example #22
0
    def setState( self, newState ):
        
        self.exitState()
        
        if newState == self.STATE_WAITING_TO_START:
            
            self.linearSpeed = 0.0
            self.stateTimer = time.time()
            
        elif newState == self.STATE_DIVING:

            self.linearSpeed = 0.0
            self.logAction( "Descending to " + str( self.NORMAL_DEPTH ) )
            self.arbitrator.setDesiredDepth( self.NORMAL_DEPTH )

        elif newState == self.STATE_TURNING_TO_GATE_1:

            self.linearSpeed = 0.0
            self.arbitrator.setDesiredYaw( 
                Maths.degToRad( self.NORTH_HEADING_DEGREES ) )
            self.setSonarLocatorActive( True )
                        
        elif newState == self.STATE_DRIVING_THROUGH_GATE_1:
            
            self.linearSpeed = self.FORWARD_SPEED
            self.staterTimer = time.time()
    
        elif newState == self.STATE_DESCENDING_TO_PIPE:
            
            self.linearSpeed = 0.0
            self.logAction( "Descending to " + str( self.PIPE_SURVEY_DEPTH ) )
            self.arbitrator.setDesiredDepth( self.PIPE_SURVEY_DEPTH )
            
        elif newState == self.STATE_TURNING_TO_GATE_2:
            
            self.linearSpeed = 0.0
            self.arbitrator.setDesiredYaw( 
                Maths.degToRad( self.SOUTH_HEADING_DEGREES ) )
                
        elif newState == self.STATE_SURVEYING_PIPE:
            
            self.linearSpeed = self.FORWARD_SPEED
            self.staterTimer = time.time()
            
        elif newState == self.STATE_RETURNING_TO_NORMAL_DEPTH:
            
            self.linearSpeed = 0.0
            self.logAction( "Climbing to " + str( self.NORMAL_DEPTH ) )
            self.arbitrator.setDesiredDepth( self.NORMAL_DEPTH )
            
        elif newState == self.STATE_TURNING_TO_WALL:
            
            self.linearSpeed = 0.0
            self.arbitrator.setDesiredYaw( 
                Maths.degToRad( self.EAST_HEADING_DEGREES ) )
            
        elif newState == self.STATE_APPROACHING_WALL:
            
            self.linearSpeed = self.FORWARD_SPEED
            self.staterTimer = time.time()
            
        elif newState == self.STATE_SURVEYING_WALL:
            
            self.linearSpeed = 0.0
            self.stateTimer = time.time()
            self.setSonarLocatorActive( False )
            self.imageCaptureScript.config.IC_Script_enableSonar = True
            
        elif newState == self.STATE_TURNING_TO_HUNT_BUOY:
            
            self.linearSpeed = 0.0
            self.arbitrator.setDesiredYaw( 
                Maths.degToRad( self.WEST_HEADING_DEGREES ) )
            
        elif newState == self.STATE_HUNTING_BUOY:
    
            # Go, go, go!
            self.ballChopper.initYawAngle = self.playerCompass.pose.pyaw
            self.stateTimer = time.time()
            
        elif newState == self.STATE_RETURNING_TO_CENTRE:
    
            # Blast forward in hopeful direction
            self.linearSpeed = self.FORWARD_SPEED
            heading = ( self.WEST_HEADING_DEGREES + 45.0 )
            self.arbitrator.setDesiredYaw( 
                Maths.degToRad( heading ) )
            self.stateTimer = time.time()
    
        elif newState == self.STATE_SURFACING:
            
            self.linearSpeed = 0.0
            self.stateTimer = time.time()
            
        else:
            self.logger.logError( "Enter State - Unrecognised state - ({0})".format( self.state ) )
            return
        
        ControlScript.setState( self, newState )
Example #23
0
 def __init__( self, config, logger, playerPos3d, playerCompass, 
     playerDepthSensor, playerSonar, playerFrontCamera ):
     
     ControlScript.__init__( self, config, logger, 
         playerPos3d=playerPos3d, playerCompass=playerCompass, 
         playerDepthSensor=playerDepthSensor, playerSonar=playerSonar, 
         playerFrontCamera=playerFrontCamera )
     
     self.NORMAL_DEPTH = self.config.HM_normalDepth
     self.PIPE_SURVEY_DEPTH = self.config.HM_pipeSurveyDepth
     self.FORWARD_SPEED = self.config.HM_forwardSpeed
     self.START_DELAY_TIME = self.config.HM_startDelayTime
     self.END_DELAY_TIME = self.config.HM_endDelayTime
     self.DRIVING_THROUGH_GATE_1_TIME = self.config.HM_drivingThroughGate_1_Time
     self.SURVEYING_PIPE_TIME = self.config.HM_surveyingPipeTime
     self.APPROACHING_WALL_TIME = self.config.HM_approachingWallTime
     self.SURVEYING_WALL_TIME = self.config.HM_surveyingWallTime
     self.MAX_HUNTING_BUOY_TIME = self.config.HM_maxHuntingBuoyTime
     self.RETURNING_TO_CENTRE_TIME = self.config.HM_returningToCentreTime
     self.NORTH_HEADING_DEGREES = self.config.HM_northHeadingDegrees
     self.EAST_HEADING_DEGREES = self.config.HM_eastHeadingDegrees
     self.SOUTH_HEADING_DEGREES = self.config.HM_southHeadingDegrees
     self.WEST_HEADING_DEGREES = self.config.HM_westHeadingDegrees
     
     # Configure the image capture script as we please
     imageCaptureConfig = copy.deepcopy( self.config )
     imageCaptureConfig.IC_Script_sonarGain = 0.3
     imageCaptureConfig.IC_Script_sonarRange = 25
     imageCaptureConfig.IC_Script_sonarNumBins = 300
     imageCaptureConfig.IC_Script_sonarScanStartAngleDegrees = 0.0
     imageCaptureConfig.IC_Script_sonarScanEndAngleDegrees = 350.0
     imageCaptureConfig.IC_Script_numImagesSavedPerSecond = 1.0
     imageCaptureConfig.IC_Script_enableCamera = True
     imageCaptureConfig.IC_Script_enableSonar = False
     
     self.imageCaptureScript = ImageCaptureScript( imageCaptureConfig, self.logger,
         self.playerPos3d, self.playerDepthSensor, self.playerSonar, self.playerFrontCamera )
     
     self.motorKiller = KillMotors( self.playerPos3d )
     self.ballChopper = OrangeBallScript( config, logger, 
         playerPos3d, playerCompass, playerDepthSensor )
     
     # Setup the arbitrator
     self.arbitrator = Arbitrator( playerPos3d, playerCompass, playerDepthSensor, logger=logger )
     self.arbitrator.setDesiredPitch( Maths.degToRad( -4.0 ) )
     self.arbitrator.setControlGains(
         pitchKp=self.config.pitchKp, 
         pitchKi=self.config.pitchKi, pitchiMin=self.config.pitchiMin, pitchiMax=self.config.pitchiMax, 
         pitchKd=self.config.pitchKd,
         yawKp=self.config.yawKp, 
         yawKi=self.config.yawKi, yawiMin=self.config.yawiMin, yawiMax=self.config.yawiMax, 
         yawKd=self.config.yawKd,
         depthKp=self.config.depthKp, 
         depthKi=self.config.depthKi, depthiMin=self.config.depthiMin, depthiMax=self.config.depthiMax,
         depthKd=self.config.depthKd )
     self.arbitrator.setEpsilons( 
         self.config.arbitratorDepthEpsilon, 
         Maths.degToRad( self.config.arbitratorYawEpsilonDegrees ) )
     
     # Clear timer
     self.stateTimer = 0.0
     
     # Move into the first state
     self.setState( self.STATE_WAITING_TO_START )    
Example #24
0
 def DefineCurrentActivation(self):
     self.CurrentActivation = Decimal(
         Maths.AdjustedSigmoid(self.SumOfInputActivations))
     if self.CurrentActivation < self.Threshold: self.CurrentActivation = 0
Example #25
0
    def update( self ):
        
        curTime = time.time()
        
        if self.state == self.STATE_DIVING:

            if self.arbitrator.atDesiredDepth():
                self.arbitrator.setDesiredYaw( Maths.degToRad( 0.0 ) )
                self.setState( self.STATE_TURNING_TO_GATE_1 )
            
        elif self.state == self.STATE_TURNING_TO_GATE_1:
            
            if self.arbitrator.atDesiredYaw():
                self.linearSpeed = 1.0
                self.driveTimer = time.time()
                self.setState( self.STATE_DRIVING_THROUGH_GATE_1 )
                        
        elif self.state == self.STATE_DRIVING_THROUGH_GATE_1:
            
            if curTime - self.driveTimer >= 20.0:
                self.linearSpeed = 0.0
                self.arbitrator.setDesiredYaw( Maths.degToRad( 180.0 ) )
                self.setState( self.STATE_TURNING_TO_GATE_2 )
            
        elif self.state == self.STATE_TURNING_TO_GATE_2:
            
            if self.arbitrator.atDesiredYaw():
                self.linearSpeed = 1.0
                self.driveTimer = time.time()
                self.setState( self.STATE_DRIVING_THROUGH_GATE_2 )
            
        elif self.state == self.STATE_DRIVING_THROUGH_GATE_2:
            
            if curTime - self.driveTimer >= 40.0:
                self.linearSpeed = 0.0
                self.arbitrator.setDesiredYaw( Maths.degToRad( 0.0 ) )
                self.setState( self.STATE_TURNING_TO_START )
            
        elif self.state == self.STATE_TURNING_TO_START:
            
            if self.arbitrator.atDesiredYaw():
                self.linearSpeed = 1.0
                self.driveTimer = time.time()
                self.setState( self.STATE_DRIVING_BACK_TO_START )
            
        elif self.state == self.STATE_DRIVING_BACK_TO_START:
            
            if curTime - self.driveTimer >= 20.0:
                self.linearSpeed = 0.0
                self.arbitrator.setDesiredDepth( 0.0 )
                self.setState( self.STATE_SURFACING )
            
        elif self.state == self.STATE_SURFACING:
            
            if self.arbitrator.atDesiredDepth():
                self.setState( self.STATE_FINISHED )
            
        elif self.state == self.STATE_FINISHED:
            # Nothing to do
            pass
        else:
            self.logger.logError( "Unrecognised state - surfacing" )
            self.setState( self.STATE_SURFACING )
        
        self.arbitrator.update( self.linearSpeed )
def analysearea(unique_func, num_conds, accs_all, sems_all, run_sig_test,
                sigclusters, decoder, minsamples, dists_all, i_area):

    data = ImportData.EntireArea(D.areas[i_area])
    counts = np.empty((num_conds, data.n), dtype=int)
    validcells = np.ones(data.n, bool)
    min_trials = 999
    for cell in range(data.n):
        td = data.behavdata[cell]

        x_data, masks = unique_func(td)

        masks = [removeinvalidentries(mask, x_data) for mask in masks]
        x_data = removeinvalidentries(x_data, x_data)

        labels = [np.unique(x_data[mask]) for mask in masks]
        numitems = [len(label) for label in labels]

        cell_min = 999
        for i, (mask, label) in enumerate(zip(masks, labels)):
            this_min = np.min([sum(x_data[mask] == lab) for lab in label])

            if this_min < cell_min:
                cell_min = this_min

            counts[i, cell] = this_min

        dists_all[i_area, cell] = cell_min

        if cell_min < min_trials:
            min_trials = cell_min

        if cell_min < minsamples:
            validcells[cell] = False

    print(
        f'Area: {D.areas[i_area]} has {min_trials} minimum samples, {int(np.mean(validcells)*100)}% cells included ({sum(validcells)})'
    )

    y_all = np.empty((D.numtrialepochs, num_conds, sum(validcells),
                      min_trials * np.max(numitems), D.num_timepoints))
    x_all = np.empty((D.numtrialepochs, num_conds, sum(validcells),
                      min_trials * np.max(numitems)))

    y_all.fill(np.nan)
    x_all.fill(np.nan)

    accs_perms = np.empty((num_conds, D.numtrialepochs, D.num_timepoints,
                           D.dec_numiters_cellselection))

    for i_cellselection in range(D.dec_numiters_cellselection):

        for i_cell, cell in enumerate(np.where(validcells == 1)[0]):

            td = data.behavdata[cell]

            x_data, masks = unique_func(td)

            for i_epoch, epoch in enumerate(D.epochs):

                y = data.generatenormalisedepoch(cell, epoch)

                for i, mask in enumerate(masks):
                    y_masked = y[mask]
                    x_masked = x_data[mask]

                    y_masked = removeinvalidentries(y_masked, x_masked)
                    x_masked = removeinvalidentries(x_masked, x_masked)

                    for i_x, x_val in enumerate(np.unique(x_masked)):
                        y_for_xval = y_masked[x_masked == x_val]

                        ind_inc_trials = np.random.choice(len(y_for_xval),
                                                          min_trials,
                                                          replace=False)

                        y_all[i_epoch, i, i_cell,
                              min_trials * (i_x):min_trials *
                              (i_x + 1), :] = y_for_xval[ind_inc_trials]
                        x_all[i_epoch, i, i_cell,
                              min_trials * (i_x):min_trials * (i_x + 1)] = i_x

        accs_perms[:, :, :,
                   i_cellselection], sem_traintestsplit = Maths.decode_across_epochs(
                       x_all, y_all, decoder)

        if i_area == 0:
            print(
                f'Progress: {i_cellselection}/{D.dec_numiters_cellselection}')

    accs = np.mean(accs_perms, axis=3)
    accs_all[i_area, 0] = accs

    std = np.nanstd(accs_perms, axis=3)
    sem_cellselection = std / np.sqrt(D.dec_numiters_cellselection)

    # Can either store the SEM for sampling different subsets of cells, or the SEM from different train/test splits of the data
    sems_all[i_area, 0] = sem_traintestsplit

    if run_sig_test:
        sigclusters[i_area, 0] = Maths.permtest(accs)
def analysearea(unique_func, accs_out, sems_out, sigclusters, dists_all, p,
                i_area):

    data = ImportData.EntireArea(D.areas[i_area])

    p.num_labels, p.num_samples_per_label, p.validcells, dists_all[
        i_area] = Maths.calculate_min_trials_per_area(data, unique_func,
                                                      dists_all, i_area)

    # If training on one epoch, then calculate train trial numbers seperately
    if p.peak_epoch is not None:

        p.num_labels_train, p.num_samples_per_label_train, _, _ = Maths.calculate_min_trials_per_area(
            data, unique_func, dists_all, i_area, train_epoch_only=True)

        # And include all cells so have same features in all decoders
        p.validcells = np.ones(dists_all[i_area].shape, dtype=bool)

    # Make holding arrays for analysis
    if not p.stability:
        accs_allperms = np.empty(
            (p.num_conds, p.n_epochs, D.n_timepoints, D.dec_numiters))
    else:
        n_pnts = p.n_epochs * (D.n_timepoints + 1)
        accs_allperms = Maths.nans((n_pnts, n_pnts, D.dec_numiters))

    if D.domultiproc:

        m = MyManager()
        m.start()

        accs_allperms = m.np_zeros(accs_allperms.shape)

        pool = Pool(D.n_cores)
        func = partial(run_decoder, data, unique_func, p, accs_allperms, False)
        pool.map(func, range(D.dec_numiters))
        pool.close()

        accs_allperms = np.array(accs_allperms)

    else:

        for i_cellselection in range(D.dec_numiters):

            accs_allperms = run_decoder(data, unique_func, p, accs_allperms,
                                        False, i_cellselection)

    if not p.stability:

        # Get average and SEM
        accs_out[i_area, 0] = np.mean(accs_allperms, axis=3)
        std = np.nanstd(accs_allperms, axis=3)
        sem = std / np.sqrt(D.dec_numiters)
        sems_out[i_area, 0] = sem

        # T-test at every time point
        accs_out[i_area,
                 1] = Maths.ttest_every_timepoint(accs_allperms, p.num_labels)

        print(f'{D.areanames[i_area]} Finished decoding')

        if D.dec_do_perms:

            accs_permtest = np.empty((p.num_conds, D.numperms))

            for i in range(D.numperms):

                one_perm = run_decoder(True)

                accs_permtest[:, i] = np.mean(one_perm[:, 0, 0, :], axis=1)

                print(
                    f'{D.areanames[i_area]} Permutation progress: {i+1}/{D.numperms}'
                )

            # Get CI
            accs_sorted = np.sort(accs_permtest, axis=1)  # Sort permutations
            sigthreshold = D.sigthreshold_onetailed
            if sigthreshold == 0: sigthreshold = 1  # -0 indexing doesn't work
            accs_ci = accs_sorted[:,
                                  -sigthreshold]  # Take the 95th highest permutation
            if p.num_conds > D.numtrialepochs:
                raise Exception(
                    'You cannot have more conds than epochs as no where to store the perm data'
                )

            sigclusters[i_area] = accs_ci

    else:

        accs_out[i_area] = np.mean(accs_allperms, axis=2)