示例#1
0
文件: JSD.py 项目: flalix/mia
 def __init__(self, desk):
     # self.base = base
     self.desk = desk
     
     self.clearEntropySeqs()
     self.time = timePack.Timing()
     
     self.myPlot = graphPac.Plot()
示例#2
0
# *****************************************************************
# IonControl:  Copyright 2016 Sandia Corporation
# This Software is released under the GPL license detailed
# in the file "license.txt" in the top-level IonControl directory
# *****************************************************************
import os
import sys

import Timing

sys.path.insert(0, os.path.abspath('..'))

#create the test object whcih will control the niSync card
test = Timing.Timing()

#write to the object's data to configure the niSync card
test.sampleRate = 100e3

try:
    #initialize will connect all clock and trigger terminals
    test.init('PXI1Slot2')

    #send a trigger programatically
    test.sendSoftwareTrigger()

#except Timing.niSyncError as e :
#    if e.code == -1073807240:
#        print 'Warning:'
#        print e
#    else:
#        raise
示例#3
0
class Pong:
    # Utility classes
    timer = Timing.Timing()
    board = Draw.Board(Constants.length, Constants.height)

    # Gameplay classes
    player_one = Bat()
    player_two = Bat()
    ball = Ball()

    player_one_controller = JoystickController.JoystickController(
        0x10, 10, 9, True, True)
    player_two_controller = JoystickController.JoystickController(
        0x20, 0x00, 0x00, True, True)

    # Game state vars
    serving = True
    playerTwoIsServing = False
    serves = 0
    gameOver = False

    def start(self):
        # Let's hope this works...
        Sound.play_async(Sound.start_music)

        self.board.prepare()
        # Setup the board by putting the bats and ball in the right place
        self.board.updateBats(self.player_one.position,
                              self.player_two.position, self.player_one.is_big,
                              self.player_two.is_big)
        self.board.updateBall(self.ball.position[0], self.ball.position[1])
        self.board.updateScore(0, False)
        self.board.updateScore(0, True)

        # Run the game loop until the game is over
        while not self.gameOver:
            self.game_loop()

    def game_loop(self):
        # Get the last frame time
        self.timer.update_time()

        # Update the status of the joystick controllers
        self.player_one_controller.update()
        self.player_two_controller.update()

        # Emulate random bat movement and update the big_time on the Bat
        self.player_one.update(
            self.timer.deltaTime,
            self.player_one_controller.get_resistor_screen_position() + 1)
        if self.ball.direction_x == 1:  # If the ball is coming towards the AI
            ai_pos_delta = (self.ball.position[1] - self.player_two.position)
        else:
            ai_pos_delta = (Constants.height // 2 - self.player_two.position)
        if ai_pos_delta != 0:  # If we're not /ing by 0
            if random.randint(0, 1) == 1:  # 50/50 chance to move
                self.player_two.update(
                    self.timer.deltaTime, self.player_two.position +
                    int(abs(ai_pos_delta) / ai_pos_delta))

        # If the current game state is serving, set up a serve
        if self.serving:
            self.setup_serve()

            # Emulate a serve
            if not self.playerTwoIsServing:
                if self.player_one_controller.button_1_pressed == 0:
                    self.ball.set_direction([1, random.randrange(-1, 1)])
                    self.serving = False
                    self.serves += 1
                    if self.serves % 5 == 0:
                        self.playerTwoIsServing = not self.playerTwoIsServing
            else:
                if random.randrange(1, 100) == 9:
                    self.ball.set_direction([-1, random.randrange(-1, 1)])
                    self.serving = False
                    self.serves += 1
                    if self.serves % 5 == 0:
                        self.playerTwoIsServing = not self.playerTwoIsServing

        # If the current game state is not serving (ie. playing)
        else:
            # Move the ball
            self.ball.update(self.timer.deltaTime)

            if self.ball.position[1] == 0:
                self.ball.direction_y *= -1
                self.ball.set_position(
                    [self.ball.position[0], self.ball.position[1] + 1])
            elif self.ball.position[1] == Constants.height - 1:
                self.ball.direction_y *= -1
                self.ball.set_position(
                    [self.ball.position[0], self.ball.position[1] - 1])

            if self.ball.position[0] == 2 or self.ball.position[0] == 1:
                deltaOne = self.player_one.check_hit(self.ball.position[1])
                if deltaOne:
                    self.reverse_ball_direction(deltaOne)
                    Sound.play_async(Sound.hit_sequence)

            elif self.ball.position[
                    0] == Constants.length - 3 or self.ball.position[
                        0] == Constants.length - 2:
                deltaTwo = self.player_two.check_hit(self.ball.position[1])
                if deltaTwo:
                    self.reverse_ball_direction(deltaTwo)
                    Sound.play_async(Sound.hit_sequence)

            # Check if the player wants to be big
            if self.player_one_controller.button_2_pressed == 0:
                self.player_one.make_big()
            if self.player_two_controller.button_2_pressed == 0:
                self.player_two.make_big()

        # Write the current game state to the board
        self.board.prepare()
        self.board.updateBats(self.player_one.position,
                              self.player_two.position, self.player_one.is_big,
                              self.player_two.is_big)

        self.board.updateBall(self.ball.position[0], self.ball.position[1])
        self.board.updateScore(self.player_one.score)
        self.board.updateScore(self.player_two.score, True)

        # If a player has scored, update their score and change the state to serving
        if self.ball.position[0] == 0:
            self.player_two.score += 1
            Glow.score()
            self.set_serving()
        elif self.ball.position[0] == Constants.length - 1:
            self.player_one.score += 1
            Glow.score()
            self.set_serving()

        # Check if either player has won the game
        self.check_winner()

        # Draw the board and wait until it needs to be drawn again
        self.board.draw()
        self.timer.wait_for_update()

    def set_serving(self):
        self.serving = True
        self.player_one.set_position(None)
        self.player_two.set_position(None)

    def setup_serve(self):
        if not self.playerTwoIsServing:
            self.ball.set_position([3, self.player_one.position])
        else:
            self.ball.set_position(
                [Constants.length - 4, self.player_two.position])

    def reverse_ball_direction(self, delta=0):
        if delta < 0:
            self.ball.direction_y = -1
        elif delta > 0:
            self.ball.direction_y = 1
        else:
            self.ball.direction_y = 0

        self.ball.direction_y = random.randrange(-1, 1)
        self.ball.direction_x *= -1
        self.ball.set_speed(random.choice(Constants.speed))

    def check_winner(self):
        # If either player has a score greater than 9, they win
        if self.player_one.score > 9:
            self.board.updateWinner(False)
            self.gameOver = True
            return True
        elif self.player_two.score > 9:
            self.board.updateWinner(True)
            self.gameOver = True
            return True
        return False
示例#4
0
    def prepareVerticalMutualInfo(self,
                                  desk,
                                  sufix,
                                  which_module,
                                  showmessage=False):
        self.desk = desk
        de_novo = desk.de_novo

        # desk.showmsg_obs('Prepare Mutual Information: num indiv: %i length: %i' % (self.ent.mySequence.getNumOfSeqs(),self.ent.mySequence.getLengthSequence()))
        ''' setNames define completeFilename +++ SPECIE !'''
        if not self.entFile.setPrefixSufix(
                root=desk.rootTable, prefix='VMI', sufix=sufix):
            return

        filename = self.entFile.MI_prefix_sufix_txt(self.entFile.prefix_sufix,
                                                    'mi')
        filename = desk.rootTable + filename

        time = crono.Timing()
        time.start()

        if not os.path.exists(filename) or de_novo:
            stri = "new: lines %i cols %i, %s" % (len(
                self.ent.mySequence.seqs), len(
                    self.ent.mySequence.seqs[0]), filename)
            desk.showmsg_obs(stri)


            self.dicPiList, \
            self.HShannonList, self.HShannonCorrList,\
            self.SeHShannonList, self.SeHShannonCorrList, \
            self.MIlist, self.MIcorrList, self.SeMIList, self.SeMICorrList, self.ijList = \
                self.ent.calcVerticalMutualInfo(desk, self.ent.mySequence.seqs, showmessage=showmessage)

            self.entFile.write_VMI_files(sufix, self.dicPiList, self.HShannonList, self.HShannonCorrList,  \
                                         self.SeHShannonList, self.SeHShannonCorrList, \
                                         self.MIlist, self.MIcorrList, \
                                         self.SeMIList, self.SeMICorrList, self.ijList, showmessage=False)
        else:
            stri = "reading MI: %i lines, %i cols, %s" % (len(
                self.ent.mySequence.seqs), len(
                    self.ent.mySequence.seqs[0]), filename)
            desk.showmsg_obs(stri)

            if which_module == 'all':
                ret, msg, self.HShannonList, self.HShannonCorrList = \
                    self.entFile.read_VMI_files(desk.rootTable, sufix, 'hShannon', showmessage=False)

                if ret:
                    ret, msg, self.SeHShannonList, self.SeHShannonCorrList = \
                        self.entFile.read_VMI_files(desk.rootTable, sufix, 'seh', showmessage=False)

                ret, msg, self.MIlist, self.MIcorrList = \
                    self.entFile.read_VMI_files(desk.rootTable, sufix, 'mi', showmessage=False)

                if ret:
                    ret, msg, self.SeMIList, self.SeMICorrList = \
                        self.entFile.read_VMI_files(desk.rootTable, sufix, 'se', showmessage=False)

            elif which_module == 'Entropy':
                ret, msg, self.HShannonList, self.HShannonCorrList = \
                    self.entFile.read_VMI_files(desk.rootTable, sufix, 'hShannon', showmessage=False)

                if ret:
                    ret, msg, self.SeHShannonList, self.SeHShannonCorrList = \
                        self.entFile.read_VMI_files(desk.rootTable, sufix, 'seh', showmessage=False)

            else:
                ret, msg, self.MIlist, self.MIcorrList = \
                    self.entFile.read_VMI_files(desk.rootTable, sufix, 'mi', showmessage=False)

                if ret:
                    ret, msg, self.SeMIList, self.SeMICorrList = \
                        self.entFile.read_VMI_files(desk.rootTable, sufix, 'se', showmessage=False)

            if ret:
                ret, msg, self.dicPiList, self.ijList = \
                    self.entFile.read_VMI_files(desk.rootTable, sufix, 'pij', showmessage=False)

            if not ret:
                print msg + '. Recalculating ....',

                self.dicPiList, \
                self.HShannonList, self.HShannonCorrList,\
                self.SeHShannonList, self.SeHShannonCorrList, \
                self.MIlist, self.MIcorrList, self.SeMIList, self.SeMICorrList, self.ijList = \
                    self.ent.calcVerticalMutualInfo(self.desk, self.ent.mySequence.seqs, showmessage=showmessage)

        time.finish()
        print '>>>', time.milli(), 'ms'

        return True, 'ok'
示例#5
0
    queue_stat = Queue()
    queue_gop = Queue()
    esci = False
    stat_process = Process(target=Statistiche.stat,
                           args=((queue_stat, ip_receiver, port_stat,
                                  num_porte), ))
    #image_process = Process(target=Image_Handler.analyzer, args=((queue_gop, pcap_path, gop_dir, img_dir), ))  # da usare
    #image_process.start()  # da usare
    stat_process.start()
    try:
        num_canali, quali = queue_stat.get()
    except KeyboardInterrupt:
        exit(-1)
    packets = rdpcap(sys.argv[1], 1)
    timer = Timing()
    start = time.time()
    start_time = 0
    pkt_start_time = packets[0].time
    #numero_totale_pkt = 0
    bind_layers(UDP, RTP)
    try:
        with PcapReader(pcap_path) as pcap_reader:
            for index, pkt in enumerate(pcap_reader):
                if IP in pkt and RTP in pkt:
                    #numero_totale_pkt = index
                    try:
                        num_canali, quali = queue_stat.get(block=False)
                    except queue.Empty:
                        pass
                    if start_time == 0:  # per avere uno start time più fedele possibile