Ejemplo n.º 1
0
def ThreadPushMessage2Phone(when=[]):
    for w in when:
        try:
            if len(w) == 2:
                db = ModelDB()
                Timer(datetime.datetime.strptime(w[0], w[1]),
                      PushMessage2Phone, {
                          'db': db
                      }).start()
            elif len(w) == 3:
                assert w[2].lower() in ['every', 'once']
                db = ModelDB()
                Timer(datetime.datetime.strptime(w[0], w[1]),
                      PushMessage2Phone, {
                          'db': db
                      }, w[2]).start()
            elif len(w) == 4:
                assert w[2].lower() in ['every', 'once']
                db = ModelDB()
                Timer(datetime.datetime.strptime(w[0], w[1]),
                      PushMessage2Phone, {
                          'db': db,
                          "msg_type": w[3]
                      }, w[2]).start()
        except Exception, e:
            putErrorlog2DB('ThreadPushImage2Phone', e, db)
 def run(self):
     
     timer = Timer(self.timeout)
     
     ''' Creating producer and sending a message '''
     self.createProducer(self.brokerName, self.destination, timer.left)
     seq_id = uuidgen()
     sending_time = timer.time()
     self.sendMessage(self.brokerName, 
                      self.destination, 
                      { 'expires': int(sending_time * 1000 + 
                                     MONITOR_EXPIRY_TIME),
                         'persistent':'true',
                         'receipt' : 'consumer_service_%s' % seq_id,
                         'reply-to':'%s' % self.replyto, 
                         MONITOR_TEST_CLIENTNAME:'%s' % self.destclientname,
                         MONITOR_TEST_SERVERID:'%s' % self.serverid,
                         MONITOR_TEST_HEADER:'%s' % seq_id,
                         MONITOR_TEST_TIME_HEADER:'%f' % sending_time }, 
                      'testing consumer service!')
     self.waitForMessagesToBeSent(self.brokerName,
                                  self.destination,
                                  timer.left)
     
     try:
         if os.path.exists(self.logfile):
             f = open(self.logfile, 'a')
         else:
             f = open(self.logfile, 'w')
     except IOError:
         raise IOError("Error opening log file")
     f.write('%f %s\n' % (sending_time, seq_id))
     f.close()
Ejemplo n.º 3
0
def test_net(save_folder: str,
             net,
             cuda: bool,
             dataset: DETECTIONS,
             transform,
             top_k: int,
             im_size: int = 300,
             thresh: float = 0.05):
    num_images = len(dataset)
    # all detections are collected into:
    #    all_boxes[cls][image] = N x 5 array of detections in
    #    (x1, y1, x2, y2, score)
    all_boxes: List[List[np.ndarray]] = [[[] for _ in range(num_images)]
                                         for _ in range(len(labelmap) + 1)]

    # timers
    _t = dict(im_detect=Timer(), misc=Timer())
    output_dir = get_output_dirs('eval', f'ssd300_{args.dataset}', 'test')
    det_file = os.path.join(output_dir, 'detections.pkl')

    for i in range(num_images):
        im, gt, h, w = dataset.pull_item(i)

        x = Variable(im.unsqueeze(0))
        if args.cuda:
            x = x.cuda()
        _t['im_detect'].tic()
        detections = net(x).data
        detect_time = _t['im_detect'].toc(average=False)

        # skip j = 0, because it's the background class
        for j in range(1, detections.size(1), 1):
            dets = detections[0, j, :]
            mask = dets[:, 0].gt(0.).expand(5, dets.size(0)).t()
            dets = torch.masked_select(dets, mask).view(-1, 5)
            if dets.size(0) == 0:
                continue
            boxes = dets[:, 1:]
            boxes[:, 0] *= w
            boxes[:, 2] *= w
            boxes[:, 1] *= h
            boxes[:, 3] *= h
            scores = dets[:, 0].cpu().numpy()
            cls_dets = np.hstack(
                (boxes.cpu().numpy(), scores[:,
                                             np.newaxis])).astype(np.float32,
                                                                  copy=False)
            all_boxes[j][i] = cls_dets

        print('im_detect: {:d}/{:d} {:.3f}s'.format(i + 1, num_images,
                                                    detect_time))

    with open(det_file, 'wb') as file:
        pickle.dump(all_boxes, file, pickle.HIGHEST_PROTOCOL)
        # all_boxes serilies to file
    print('Evaluating detections')
    evaluate_detections(all_boxes, output_dir, dataset)
Ejemplo n.º 4
0
    def resetState(self):
        self.player1.sleeping = False

        self.player2.sleeping = False

        self.platformArray = self.handler.getLevel().platformGroup

        self.background_image = pygame.image.load(
            self.handler.getLevel().getBackImg()).convert()

        self.timer = Timer(300, self.screen)

        self.handler.setPlatformArray(self.platformArray)

        self.player1 = self.handler.player1
        self.player2 = self.handler.player2

        self.handler.setPlayer1(self.player1)
        self.handler.setPlayer2(self.player2)

        self.player2.facing = -1

        self.player1.health = self.player1.maxHP
        self.player2.health = self.player2.maxHP

        self.p1hpbar = HealthBar(self.screen, "topleft", self.player1.health,
                                 self.handler)
        self.p2hpbar = HealthBar(self.screen, "topright", self.player2.health,
                                 self.handler)

        self.p1infobar = InfoBar(self.screen, self.player1, self.handler)
        self.p2infobar = InfoBar(self.screen, self.player2, self.handler)

        self.player1.rect.x = 150
        self.player1.rect.y = 100

        self.player2.rect.x = 950
        self.player2.rect.y = 100

        self.player1.xchange = 0
        self.player1.ychange = 0

        self.player2.xchange = 0
        self.player2.ychange = 0

        self.player1.unduck()
        self.player2.unduck()

        # Timer utils
        self.count = 0
        self.end_time = 0
 def run(self):
     
     timer = Timer(self.timeout)
     
     ''' Starting consumer '''
     self.createConsumer(self.brokerName, self.destination, timer.left)
     timer.sleep(2)
     
     ''' Getting received messages '''
     messages = self.getMessages(self.brokerName, self.destination)
     self.received = self.filterMessages(messages)
     self.results['received'] = len(self.received)
     self.readLog()
     self.writeNewLog()
Ejemplo n.º 6
0
    def run(self):

        timer = Timer(self.timeout)
        ''' Starting consumers '''
        for name, host in self.otherBrokers.items():
            self.createConsumer(
                name,
                '/queue/%s.%s.%s' % (self.vtPrefix, name, self.destination),
                timer.left)
        time.sleep(1)
        ''' Creating producer and sending messages '''
        self.createProducer(self.mainBrokerName, self.destinationTopic,
                            timer.left)
        for i in range(self.messages):
            self.sendMessage(self.mainBrokerName, self.destinationTopic,
                             {'persistent': 'true'}, 'testing-%s' % i)
        self.waitForMessagesToBeSent(self.mainBrokerName,
                                     self.destinationTopic, self.messages)

        for broker in self.otherBrokers:
            self.waitForMessagesToArrive(
                broker,
                '/queue/%s.%s.%s' % (self.vtPrefix, broker, self.destination),
                self.messages, timer.left)
        ''' Wait a couple of seconds to see if we get duplicated '''
        time.sleep(2)

        for broker in self.otherBrokers:
            self.assertMessagesNumber(
                broker,
                '/queue/%s.%s.%s' % (self.vtPrefix, broker, self.destination),
                self.messages)
Ejemplo n.º 7
0
	def __init__(self, log_dir, start_iteration=0):
		"""
		
		:param log_dir: folder to store the summaries
		:param start_iteration: iteration where the training is starting
		"""
		
		self.loss_per_iteration = []
		self.iteration = start_iteration
		self.error_counter = 0
		self.timer = Timer()
		self.timer.tic()
		self.log_dir = log_dir
		self.writer = SummaryWriter(
			log_dir=self.log_dir
		)
Ejemplo n.º 8
0
    def __init__(self, name, screen, handler, attackUpdateList):
        super().__init__(name)
        self.platformArray = handler.getLevel().platformGroup
        self.attackUpdateList = attackUpdateList

        self.timer = Timer(300, screen)
        self.testProjectile = pygame.image.load(
            "media/Misc/projectileTest.png").convert()
        self.background_image = pygame.image.load(
            handler.getLevel().getBackImg()).convert()
        self.kosprite = pygame.image.load("media/Misc/KO.png").convert()
        self.handler = handler

        handler.setPlatformArray(self.platformArray)

        self.player1 = self.handler.player1
        self.player2 = self.handler.player2

        self.screen = screen
        self.victor = 0

        self.handler.setPlayer1(self.player1)
        self.handler.setPlayer2(self.player2)

        if pygame.joystick.get_count() == 2:
            self.joystick1 = pygame.joystick.Joystick(0)
            self.joystick1.init()
            self.joystick2 = pygame.joystick.Joystick(1)
            self.joystick2.init()
            self.useJoysticksP1 = True
            self.useJoysticksP2 = True
        elif pygame.joystick.get_count() == 1:
            self.joystick1 = pygame.joystick.Joystick(0)
            self.joystick1.init()
            self.useJoysticksP1 = True
            self.useJoysticksP2 = False
        else:
            self.useJoysticksP1 = False
            self.useJoysticksP2 = False

        # Timer utils
        self.count = 0
        self.end_time = 0
Ejemplo n.º 9
0
def validation(model, criterion1, criterion2, lamb, loader, device,
               log_callback):
    end = time.time()
    model.eval()

    # return validation_loss, validation_acc
    with torch.no_grad():
        # the output of the dataloader is (batch_idx, image, mask, c, v, t)
        for batch_idx, data in enumerate(loader):
            target_image, target_mask, input_c, input_v, input_t = data
            target_image = target_image.to(device, non_blocking=True)
            target_mask = target_mask.to(device, non_blocking=True)
            input_c = input_c.to(device, non_blocking=True)
            input_v = input_v.to(device, non_blocking=True)
            input_t = input_t.to(device, non_blocking=True)

            # compute the output
            out_image, out_mask = model(input_c, input_v, input_t)

            # compute the loss
            loss1 = criterion1(out_image, target_image)
            #loss2 = criterion2(out_mask, target_mask.long().squeeze())
            loss2 = criterion2(out_mask, target_mask)
            loss = loss1 + lamb * loss2

            batch_time.update(time.time() - end)
            end = time.time()

        # records essential information into log file.
        log_callback(
            'epoch: {0}\t'
            'Time {batch_time.sum:.3f}s / {1} epochs, ({batch_time.avg:.3f})\t'
            'Data load {data_time.sum:.3f}s / {1} epochs, ({data_time.avg:3f})\n'
            'Loss = {loss:.8f}\n'.format(epoch,
                                         batch_idx,
                                         batch_time=batch_time,
                                         data_time=data_time,
                                         loss=loss.item()))

        log_callback()

        log_callback('Loss{0} = {loss1:.8f}\t'.format(1, loss1=loss1.item()))

        log_callback('Loss{0} = {loss1:.8f}\t'.format(2, loss1=loss2.item()))

        log_callback(Timer.timeString())

        batch_time.reset()

        return loss.item()
Ejemplo n.º 10
0
    def camera_detector(self, cap, wait=10):
        detect_timer = Timer()
        ret, _ = cap.read()

        while ret:
            ret, frame = cap.read()

            detect_timer.tic()
            result = self.detect(frame)
            detect_timer.toc()
            print 'Average detecting time: {:.3f}s'.format(
                detect_timer.average_time)

            self.draw_result(frame, result)
            cv2.imshow('Camera', frame)
            cv2.waitKey(wait)
Ejemplo n.º 11
0
 def run(self):
     
     timer = Timer(self.timeout)
     
     ''' Starting consumer '''
     self.createConsumer(self.brokerName, self.destination, timer.left)
     if self.destination.startswith('/topic/'):
         time.sleep(1)
     
     ''' Creating producer and sending a message '''
     self.createProducer(self.brokerName, self.destination, timer.left)
     for i in range(self.messages):
         self.sendMessage(self.brokerName, 
                          self.destination, 
                          {'persistent':'true'}, 
                          'testing-%s' % i)
     self.waitForMessagesToBeSent(self.brokerName,
                                  self.destination,
                                  timer.left)
     
     ''' Ensuring that we received a message '''
     self.waitForMessagesToArrive(self.brokerName, self.destination, self.messages, timer.left)
     self.assertMessagesNumber(self.brokerName, self.destination, self.messages)
Ejemplo n.º 12
0
    def image_detector(self, imname, wait=0, resize=False):
        if not os.path.isfile(imname):
            raise ValueError("No such file is present: {}".format(imname))
        detect_timer = Timer()
        image = cv2.imread(imname)
        if resize:
            image = cv2.resize(image, (640, 360))

        detect_timer.tic()
        result = self.detect(image)
        detect_timer.toc()
        print result
        print 'Average detecting time: {:.3f}s'.format(
            detect_timer.average_time)

        self.draw_result(image, result)
        cv2.imshow('Image', image)
        cv2.waitKey(wait)
Ejemplo n.º 13
0
class GameState(State):

    # TODO Organize Game State
    # TODO Create a better way to order when things are rendered

    def __init__(self, name, screen, handler, attackUpdateList):
        super().__init__(name)
        self.platformArray = handler.getLevel().platformGroup
        self.attackUpdateList = attackUpdateList

        self.timer = Timer(300, screen)
        self.testProjectile = pygame.image.load(
            "media/Misc/projectileTest.png").convert()
        self.background_image = pygame.image.load(
            handler.getLevel().getBackImg()).convert()
        self.kosprite = pygame.image.load("media/Misc/KO.png").convert()
        self.handler = handler

        handler.setPlatformArray(self.platformArray)

        self.player1 = self.handler.player1
        self.player2 = self.handler.player2

        self.screen = screen
        self.victor = 0

        self.handler.setPlayer1(self.player1)
        self.handler.setPlayer2(self.player2)

        if pygame.joystick.get_count() == 2:
            self.joystick1 = pygame.joystick.Joystick(0)
            self.joystick1.init()
            self.joystick2 = pygame.joystick.Joystick(1)
            self.joystick2.init()
            self.useJoysticksP1 = True
            self.useJoysticksP2 = True
        elif pygame.joystick.get_count() == 1:
            self.joystick1 = pygame.joystick.Joystick(0)
            self.joystick1.init()
            self.useJoysticksP1 = True
            self.useJoysticksP2 = False
        else:
            self.useJoysticksP1 = False
            self.useJoysticksP2 = False

        # Timer utils
        self.count = 0
        self.end_time = 0

    def resetState(self):
        self.player1.sleeping = False

        self.player2.sleeping = False

        self.platformArray = self.handler.getLevel().platformGroup

        self.background_image = pygame.image.load(
            self.handler.getLevel().getBackImg()).convert()

        self.timer = Timer(300, self.screen)

        self.handler.setPlatformArray(self.platformArray)

        self.player1 = self.handler.player1
        self.player2 = self.handler.player2

        self.handler.setPlayer1(self.player1)
        self.handler.setPlayer2(self.player2)

        self.player2.facing = -1

        self.player1.health = self.player1.maxHP
        self.player2.health = self.player2.maxHP

        self.p1hpbar = HealthBar(self.screen, "topleft", self.player1.health,
                                 self.handler)
        self.p2hpbar = HealthBar(self.screen, "topright", self.player2.health,
                                 self.handler)

        self.p1infobar = InfoBar(self.screen, self.player1, self.handler)
        self.p2infobar = InfoBar(self.screen, self.player2, self.handler)

        self.player1.rect.x = 150
        self.player1.rect.y = 100

        self.player2.rect.x = 950
        self.player2.rect.y = 100

        self.player1.xchange = 0
        self.player1.ychange = 0

        self.player2.xchange = 0
        self.player2.ychange = 0

        self.player1.unduck()
        self.player2.unduck()

        # Timer utils
        self.count = 0
        self.end_time = 0

    def reloadLevel(self):
        if len(self.handler.getLevel().platformGroup) == 0:
            self.handler.getLevel().__init__(self.screen)

        self.platformArray = self.handler.getLevel().platformGroup

        self.testProjectile = pygame.image.load(
            "media/Misc/projectileTest.png").convert()
        self.background_image = pygame.image.load(
            self.handler.getLevel().getBackImg()).convert()
        self.kosprite = pygame.image.load("media/Misc/KO.png")

        self.handler.setPlatformArray(self.platformArray)
        self.handler.getAttackList().empty()

        self.player1.setPlatArray(self.handler.getPlatformArray())
        self.player2.setPlatArray(self.handler.getPlatformArray())

    def setPlayers(self, player1, player2):
        self.player1 = player1
        self.player2 = player2

        self.handler.setPlayer1(self.player1)
        self.handler.setPlayer2(self.player2)

        self.player2.facing = -1

        self.p1hpbar = HealthBar(self.screen, "topleft", self.player1.health,
                                 self.handler)
        self.p2hpbar = HealthBar(self.screen, "topright", self.player2.health,
                                 self.handler)

        self.p1infobar = InfoBar(self.screen, self.player1, self.handler)
        self.p2infobar = InfoBar(self.screen, self.player2, self.handler)

    def update(self, screen):
        screen.blit(self.background_image, [0, 0])  # Jakob's mistake
        keys = pygame.key.get_pressed()

        self.handler.level.update(screen)

        if keys[pygame.K_a] and not (self.player1.sleeping
                                     or self.player1.stunned):
            self.player1.moveLeft()
        if keys[pygame.K_d] and not (self.player1.sleeping
                                     or self.player1.stunned):
            self.player1.moveRight()
        if keys[pygame.K_LEFT] and not (self.player2.sleeping
                                        or self.player2.stunned):
            self.player2.moveLeft()
        if keys[pygame.K_RIGHT] and not (self.player2.sleeping
                                         or self.player2.stunned):
            self.player2.moveRight()

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self.handler.setDone(True)
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_g and not (self.player1.sleeping
                                                    or self.player1.stunned):
                    self.player1.attack(screen)
                    self.player1.rangedstarttime = pygame.time.get_ticks()
                    self.player1.released = False
                elif event.key == pygame.K_f and not (self.player1.sleeping
                                                      or self.player1.stunned):
                    self.player1.special()
                elif event.key == pygame.K_r and not (self.player1.sleeping
                                                      or self.player1.stunned):
                    self.player1.meleeAttack(screen)
                elif event.key == pygame.K_w and not (self.player1.sleeping
                                                      or self.player1.stunned):
                    self.player1.jump()
                elif event.key == pygame.K_s and not (self.player1.sleeping
                                                      or self.player1.stunned):
                    self.player1.duck()
                elif event.key == pygame.K_RSHIFT and not (
                        self.player2.sleeping or self.player2.stunned):
                    self.player2.attack(screen)
                    self.player2.rangedstarttime = pygame.time.get_ticks()
                    self.player2.released = False
                elif event.key == pygame.K_RETURN and not (
                        self.player2.sleeping or self.player2.stunned):
                    self.player2.special()
                elif event.key == pygame.K_RCTRL and not (
                        self.player2.sleeping or self.player2.stunned):
                    self.player2.meleeAttack(screen)
                elif event.key == pygame.K_UP and not (self.player2.sleeping or
                                                       self.player2.stunned):
                    self.player2.jump()
                elif event.key == pygame.K_DOWN and not (
                        self.player2.sleeping or self.player2.stunned):
                    self.player2.duck()
                elif event.key == pygame.K_ESCAPE:
                    self.handler.getStateManager().setCurrentState(
                        "PausedState")
            elif event.type == pygame.KEYUP:
                if event.key == pygame.K_w:
                    self.player1.unjump()
                elif event.key == pygame.K_UP:
                    self.player2.unjump()
                if event.key == pygame.K_s:
                    self.player1.unduck()
                elif event.key == pygame.K_DOWN:
                    self.player2.unduck()
                elif event.key == pygame.K_g:
                    self.player1.rangedendtime = pygame.time.get_ticks()
                    self.player1.released = True
                elif event.key == pygame.K_RSHIFT:
                    self.player2.rangedendtime = pygame.time.get_ticks()
                    self.player2.released = True
            if self.useJoysticksP1 and event.type == pygame.JOYBUTTONDOWN:
                if event.button == CONTROLLER_RANGED and not (
                        self.player1.sleeping
                        or self.player1.stunned) and event.joy == 0:
                    self.player1.attack(screen)
                    self.player1.rangedstarttime = pygame.time.get_ticks()
                    self.player1.released = False
                if event.button == CONTROLLER_SPECIAL and not (
                        self.player1.sleeping
                        or self.player1.stunned) and event.joy == 0:
                    self.player1.special()
                if event.button == CONTROLLER_MELEE and not (
                        self.player1.sleeping
                        or self.player1.stunned) and event.joy == 0:
                    self.player1.meleeAttack(screen)
                if event.button == CONTROLLER_JUMP and not (
                        self.player1.sleeping or self.player1.stunned
                ) and event.joy == 0 and self.player1.name != "Lil' Shed":
                    self.player1.jump()
                if event.button == CONTROLLER_CROUCH and not (
                        self.player1.sleeping or self.player1.stunned
                ) and event.joy == 0 and self.player1.name != "Lil' Shed":
                    self.player1.duck()
            if self.useJoysticksP2 and event.type == pygame.JOYBUTTONDOWN:
                if event.button == CONTROLLER_JUMP and not (
                        self.player2.sleeping or self.player2.stunned
                ) and event.joy == 1 and self.player2.name != "Lil' Shed":
                    self.player2.jump()
                if event.button == CONTROLLER_MELEE and not (
                        self.player2.sleeping
                        or self.player2.stunned) and event.joy == 1:
                    self.player2.meleeAttack(screen)
                if event.button == CONTROLLER_CROUCH and not (
                        self.player2.sleeping or self.player2.stunned
                ) and event.joy == 1 and self.player2.name != "Lil' Shed":
                    self.player2.duck()
                if event.button == CONTROLLER_RANGED and not (
                        self.player2.sleeping
                        or self.player2.stunned) and event.joy == 1:
                    self.player2.attack(screen)
                    self.player2.rangedstarttime = pygame.time.get_ticks()
                    self.player2.released = False
                if event.button == CONTROLLER_SPECIAL and not (
                        self.player2.sleeping
                        or self.player2.stunned) and event.joy == 1:
                    self.player2.special()
                if event.button == CONTROLLER_PAUSE:
                    self.handler.getStateManager().setCurrentState(
                        "PausedState")
            if self.useJoysticksP1 and event.type == pygame.JOYBUTTONUP:
                if event.button == CONTROLLER_JUMP and not (
                        self.player1.sleeping or self.player1.stunned
                ) and event.joy == 0 and self.player1.name != "Lil' Shed":
                    self.player1.unjump()
                if event.button == CONTROLLER_RANGED and event.joy == 0:
                    self.player1.rangedendtime = pygame.time.get_ticks()
                    self.player1.released = True
                if event.button == CONTROLLER_CROUCH and event.joy == 0 and self.player1.name != "Lil' Shed":
                    self.player1.unduck()
            if self.useJoysticksP2 and event.type == pygame.JOYBUTTONUP:
                if event.button == CONTROLLER_JUMP and not (
                        self.player2.sleeping or self.player2.stunned
                ) and event.joy == 1 and self.player2.name != "Lil' Shed":
                    self.player2.unjump()
                if event.button == CONTROLLER_RANGED and event.joy == 1:
                    self.player2.rangedendtime = pygame.time.get_ticks()
                    self.player2.released = True
                if event.button == CONTROLLER_CROUCH and event.joy == 1 and self.player2.name != "Lil' Shed":
                    self.player2.unduck()

        if self.player1.rect.y > screen.get_size()[1]:
            self.player1.health = 0
        if self.player2.rect.y > screen.get_size()[1]:
            self.player2.health = 0

        if self.player2.health <= 0:
            self.player2.goToSleepForAnExtendedPeriodOfTime()
            screen.blit(self.kosprite,
                        [self.player2.rect.x - 15, self.player2.rect.y - 80])
        if self.player1.health <= 0:
            self.player1.goToSleepForAnExtendedPeriodOfTime()
            screen.blit(self.kosprite,
                        [self.player1.rect.x - 15, self.player1.rect.y - 80])

        if self.timer.current_time < 1:
            self.platformArray.remove(self.platformArray)

        self.player1.update(screen)
        self.player2.update(screen)
        self.player1.attackUpdate(screen)

        self.timer.update(screen)

        self.p1infobar.update(
            self.player1.ranged_cooldown.getCurrentCooldown(),
            self.player1.special_cooldown.getCurrentCooldown(),
            self.player1.health)
        self.p2infobar.update(
            self.player2.ranged_cooldown.getCurrentCooldown(),
            self.player2.special_cooldown.getCurrentCooldown(),
            self.player2.health)

        self.p1hpbar.update(self.player1.health)
        self.p2hpbar.update(self.player2.health)

        self.platformArray.update()

        for p in self.platformArray:
            if p.platform_cooldown.isDone() and p.duration != -1:
                self.platformArray.remove(p)

        self.handler.setPlayer1(self.player1)
        self.handler.setPlayer2(self.player2)

        if self.player1.name != "Lil' Shed":
            self.player1.xchange = 0
        if self.player2.name != "Lil' Shed":
            self.player2.xchange = 0

        if self.useJoysticksP1:
            if (self.joystick1.get_axis(0) > 0.01
                ) and not (self.player1.sleeping or self.player1.stunned):
                self.player1.moveRight()

            if (self.joystick1.get_axis(0) < -0.01
                ) and not (self.player1.sleeping or self.player1.stunned):
                self.player1.moveLeft()

            if (self.joystick1.get_axis(1) > 0.01) and not (
                    self.player1.sleeping or
                    self.player1.stunned) and self.player1.name == "Lil' Shed":
                self.player1.duck()
            elif self.player1.name == "Lil' Shed":
                self.player1.unduck()

            if (self.joystick1.get_axis(1) < -0.01) and not (
                    self.player1.sleeping or
                    self.player1.stunned) and self.player1.name == "Lil' Shed":
                self.player1.jump()
            elif self.player1.name == "Lil' Shed":
                self.player1.unjump()

        if self.useJoysticksP2:
            if (self.joystick2.get_axis(0) > 0.01
                ) and not (self.player2.sleeping or self.player2.stunned):
                self.player2.moveRight()

            if (self.joystick2.get_axis(0) < -0.01
                ) and not (self.player2.sleeping or self.player2.stunned):
                self.player2.moveLeft()

            if (self.joystick2.get_axis(1) > 0.01) and not (
                    self.player2.sleeping or
                    self.player2.stunned) and self.player2.name == "Lil' Shed":
                self.player2.duck()
            elif self.player2.name == "Lil' Shed":
                self.player2.unduck()

            if (self.joystick2.get_axis(1) < -0.01) and not (
                    self.player2.sleeping or
                    self.player2.stunned) and self.player2.name == "Lil' Shed":
                self.player2.jump()
            elif self.player2.name == "Lil' Shed":
                self.player2.unjump()

        if self.player1.sleeping:
            text = font.render("Player 2 Wins!", False, colors.get("BLACK"))
            screen.blit(text, ((screen.get_size()[0] / 2 - 125),
                               (screen.get_size()[1] / 2 - 200)))
            text = font.render(self.handler.getPlayer1().loseQuote, False,
                               colors.get("BLACK"))
            screen.blit(text, (self.handler.getPlayer1().rect.x,
                               self.handler.getPlayer1().rect.y - 100))
            text = font.render(self.handler.getPlayer2().winQuote, False,
                               colors.get("BLACK"))
            screen.blit(text, (self.handler.getPlayer2().rect.x,
                               self.handler.getPlayer2().rect.y - 100))
            if self.count == 0:
                self.end_time = self.timer.current_time
                self.count += 1
            if self.timer.current_time <= self.end_time - 5:
                self.handler.setDone(True)
                self.handler.getStateManager().setCurrentState("EndGameState")
            self.victor = 2
        elif self.player2.sleeping:
            text = font.render("Player 1 Wins!", False, colors.get("BLACK"))
            screen.blit(text, ((screen.get_size()[0] / 2 - 125),
                               (screen.get_size()[1] / 2 - 200)))
            text = font.render(self.handler.getPlayer2().loseQuote, False,
                               colors.get("BLACK"))
            screen.blit(text, (self.handler.getPlayer2().rect.x,
                               self.handler.getPlayer2().rect.y - 100))
            text = font.render(self.handler.getPlayer1().winQuote, False,
                               colors.get("BLACK"))
            screen.blit(text, (self.handler.getPlayer1().rect.x,
                               self.handler.getPlayer1().rect.y - 100))
            if self.count == 0:
                self.end_time = self.timer.current_time
                self.count += 1
            if self.timer.current_time <= self.end_time - 5:
                self.handler.setDone(True)
                self.handler.getStateManager().setCurrentState("EndGameState")
            self.victor = 1
Ejemplo n.º 14
0
from utils.Timer import Timer
from utils.AverageMeter import AverageMeter

from modules.model import Net
from Dataset import Dataset

from utils import viz_utils
from utils import torch_utils
from utils.fs_utils import create_folder

# system
import argparse
import time

create_folder('checkpoints')
folderPath = 'checkpoints/session_' + Timer.timeFilenameString() + '/'
create_folder(folderPath)

create_folder('log')
logPath = 'log/log_' + Timer.timeFilenameString()


def append_line_to_log(line='\n'):
    with open(logPath, 'a') as f:
        f.write(line + '\n')


torch.set_default_tensor_type('torch.cuda.FloatTensor')

params = load_config('config.yaml')
Ejemplo n.º 15
0
 def make(self,
          intervals,
          emotions,
          config,
          current_id=None,
          icon=None,
          overlay=None):
     setRenderTimestamp(current_id, time.time())
     makeTimer = Timer()
     timer = Timer()
     timings = {'intervals': []}
     makeTimer.start()
     hsh = self.__make_hash__(intervals, config)
     if os.path.exists("tmp/" + hsh + ".mp4"):
         return "tmp/" + hsh + ".mp4"
     files = []
     duration = 0
     setProcessStatus(current_id, "Создаем структуру")
     saveWorkingStatus()
     index = 0
     for i in range(len(intervals)):
         timings['intervals'].append({})
         if type(intervals[i]) == ImageInterval:
             print("Working on making image video")
             timer.start()
             img = load_image(intervals[i].src)
             timings['intervals'][-1]['load_image'] = timer.end()
             timer.start()
             file_name = self.__make_image_video__(
                 img, intervals[i].end - intervals[i].begin, config)
             timings['intervals'][-1]['__make_image_video__'] = timer.end()
             duration += intervals[i].end - intervals[i].begin
             files.append(file_name)
             print(file_name)
         elif type(intervals[i]) == VideoInterval:
             timer.start()
             file_name = self.__make_video_video__(intervals[i].src,
                                                   intervals[i].video_begin,
                                                   intervals[i].video_end,
                                                   config)
             timings['intervals'][-1]['__make_video_video__'] = timer.end()
             duration += intervals[i].end - intervals[i].begin
             files.append(file_name)
         else:
             print("Unknown object")
         index += 1
         setProcessStatus(
             current_id,
             "Готово фрагментов: {:d}/{:d}".format(index, len(intervals)))
         saveTimings(current_id, timings)
     print(files)
     setProcessStatus(current_id, "Сливаем видео")
     timer.start()
     full = self.__merge_videos__(files, config)
     timings['__merge_videos__'] = timer.end()
     saveTimings(current_id, timings)
     setProcessStatus(current_id, "Добавляем текст")
     timer.start()
     full_with_text = self.__add_text_to_video__(full,
                                                 intervals,
                                                 duration,
                                                 config,
                                                 icon=icon,
                                                 overlay=overlay)
     timings['__add_text_to_video__'] = timer.end()
     saveTimings(current_id, timings)
     setProcessStatus(current_id, "Добавляем аудио")
     timer.start()
     full_with_text_audio = self.__add_audio_to_video__(
         full_with_text, duration, config)
     timings['__add_audio_to_video__'] = timer.end()
     saveTimings(current_id, timings)
     # self.__copy_to_file__(full_with_text_audio, "tmp/" + hsh + ".mp4")
     setRenderTime(current_id, makeTimer.end())
     return full_with_text_audio
Ejemplo n.º 16
0
    except Exception as e:
        print
        str(e)


uri = uris[0]
total_seconds = 0
num_calls = 30
lowest = float('inf')
fastest = 0

for i in range(num_calls):
    print
    "Call # %d" % (i + 1)
    path = 'http://' + base_address + '/' + base_version + uri
    with Timer() as t:
        call_maker(path)
    print
    "Time elapsed : %f" % t.secs
    lowest = t.secs if t.secs < lowest else lowest
    fastest = t.secs if t.secs > fastest else fastest
    total_seconds += t.secs

print
"\n\n\nSeconds total: %f average : %f fastest %f lowest %f" % (
total_seconds, float(total_seconds) / num_calls, lowest, fastest)


# for call in calls:
# 	call.start()
Ejemplo n.º 17
0
    def train(self):

        train_timer = Timer()
        load_timer = Timer()

        for step in xrange(1, self.max_iter + 1):
            load_timer.tic()
            images, labels = self.data.get()
            load_timer.toc()
            feed_dict = {self.net.x: images, self.net.labels: labels}

            train_timer.tic()
            # summary_str, loss = self.sess.run([self.summary_op, self.net.loss], feed_dict=feed_dict)
            _, summary_str, loss = self.sess.run(
                [self.train_op, self.summary_op, self.net.loss],
                feed_dict=feed_dict)
            # _, loss = self.sess.run([self.train_op, self.net.loss], feed_dict=feed_dict)
            train_timer.toc()
            log_str = (
                '{} Epoch: {}, Step: {},'
                ' Loss: {:5.3f} Speed: {:.3f}s/iter,'
                ' Load: {:.3f}s/iter, Remain: {}'
            ).format(
                datetime.datetime.now().strftime('%m/%d %H:%M:%S'),
                self.data.epoch,
                int(step),
                # round(self.initial_learning_rate.eval(session=self.sess), 6),
                loss,
                train_timer.average_time,
                load_timer.average_time,
                train_timer.remain(step, self.max_iter))
            print log_str
            self.log_file.write(log_str + "\n")

            self.writer.add_summary(summary_str, step)

            if step % self.save_iter == 0:
                log_str = '{} Saving checkpoint file to: {}'.format(
                    datetime.datetime.now().strftime('%m/%d %H:%M:%S'),
                    self.output_dir)
                print log_str
                self.log_file.write(log_str + "\n")
                path_where_variables_saved = self.saver.save(
                    self.sess,
                    self.ckpt_file,
                    global_step=self.global_step,
                    write_meta_graph=False)
                log_str = '{} Session variables successfully saved to: {}'.format(
                    datetime.datetime.now().strftime('%m/%d %H:%M:%S'),
                    path_where_variables_saved)
                print log_str
                self.log_file.write(log_str + "\n")
Ejemplo n.º 18
0
from train import train, validation
from evaluation import evaluation
from Dataset import NACCDataset

from utils.config_utils import load_config
from utils.fs_utils import create_folder
from utils.Timer import Timer

# system
import argparse
import time
import pickle

# create folder to store checkpoints
create_folder('checkpoints')
checkPath = 'checkpoints/session_' + Timer.timeFilenameString()
create_folder(checkPath)

# create folder to store models for each epoch
create_folder("models")
modelPath = "models/session_" + Timer.timeFilenameString()
create_folder(modelPath)

# create folder to store log files
create_folder('logs')
logPath = 'logs/log_' + Timer.timeFilenameString()

# create folder to store history dictionary
create_folder("history")
hist_file = "history/session_" + Timer.timeFilenameString() + ".pkl"
Ejemplo n.º 19
0
class DataSummary(object):
	"""
	DataSummary class. Used to store all kind of summaries during ytaining
	"""
	def __init__(self, log_dir, start_iteration=0):
		"""
		
		:param log_dir: folder to store the summaries
		:param start_iteration: iteration where the training is starting
		"""
		
		self.loss_per_iteration = []
		self.iteration = start_iteration
		self.error_counter = 0
		self.timer = Timer()
		self.timer.tic()
		self.log_dir = log_dir
		self.writer = SummaryWriter(
			log_dir=self.log_dir
		)
	
	def add_summaries(self, loss_value, n_steps=1):
		"""
		add summaries to object
		:param loss_value: loss value of one forward pass or mean loss value
		:param n_steps: number of past iterations
		:return:
		"""
		self.loss_per_iteration.append(loss_value)
		self.iteration += n_steps
		
	def give_summary(self, reset=True):
		"""
		Store summaries and print them to std
		:param reset: reset summaries
		:return:
		"""
		mean_los = np.mean(self.loss_per_iteration)
		
		logging.info('Iteration: {}, Mean loss: {:.4f}, Mean run time: {:.4f}'.format(self.iteration, mean_los, self.timer.toc(average=False)))
		self.writer.add_scalar('training/mean_loss', mean_los, self.iteration)
		if reset:
			self.resert_summaries()
			self.timer.tic()
			
	def resert_summaries(self):
		"""
		Reset summaries in the model
		:return:
		"""
		self.loss_per_iteration = []
		
	def error(self, e):
		"""
		write error
		:return:
		"""
		self.error_counter += 1
		error_msg = 'Error in iteration: {}, Error: {}'.format(self.iteration, e)
		logging.info(error_msg)
		self.writer.add_text('Error', error_msg, self.iteration)
	
	def store_histogram(self, name, param):
		"""
		store model weight histograms
		:param name: layer name
		:param param: model param
		:return:
		"""
		
		self.writer.add_histogram(name, param.clone().cpu().data.numpy(), self.iteration)
Ejemplo n.º 20
0
class Logger:
    trial_key, setup_info, _schemata = dict(animal_id=0,
                                            session=1,
                                            trial_idx=0), dict(), dict()
    lock, queue, ping_timer, logger_timer, total_reward, curr_state = False, PriorityQueue(
    ), Timer(), Timer(), 0, ''

    def __init__(self, protocol=False):
        self.setup = socket.gethostname()
        self.is_pi = os.uname(
        )[4][:3] == 'arm' if os.name == 'posix' else False
        self.setup_status = 'running' if protocol else 'ready'
        fileobject = open(
            os.path.dirname(os.path.abspath(__file__)) +
            '/../dj_local_conf.json')
        con_info = json.loads(fileobject.read())
        self.private_conn = dj.Connection(con_info['database.host'],
                                          con_info['database.user'],
                                          con_info['database.password'])
        for schema, value in schemata.items(
        ):  # separate connection for internal comminication
            self._schemata.update({
                schema:
                dj.create_virtual_module(schema,
                                         value,
                                         connection=self.private_conn)
            })
        self.thread_end, self.thread_lock = threading.Event(), threading.Lock()
        self.inserter_thread = threading.Thread(target=self.inserter)
        self.getter_thread = threading.Thread(target=self.getter)
        self.inserter_thread.start()
        self.log_setup(protocol)
        self.getter_thread.start()
        self.logger_timer.start()

    def put(self, **kwargs):
        item = PrioritizedItem(**kwargs)
        self.queue.put(item)
        if not item.block: self.queue.task_done()
        else: self.queue.join()

    def inserter(self):
        while not self.thread_end.is_set():
            if self.queue.empty():
                time.sleep(.5)
                continue
            item = self.queue.get()
            skip = False if item.replace else True
            table = rgetattr(self._schemata[item.schema], item.table)
            self.thread_lock.acquire()
            try:
                table.insert1(item.tuple,
                              ignore_extra_fields=item.ignore_extra_fields,
                              skip_duplicates=skip,
                              replace=item.replace)
                if item.validate:  # validate tuple exists in database
                    key = {
                        k: v
                        for (k, v) in item.tuple.items()
                        if k in table.primary_key
                    }
                    if 'status' in item.tuple.keys():
                        key['status'] = item.tuple['status']
                    while not len(table & key) > 0:
                        time.sleep(.5)
            except Exception as e:
                if item.error:
                    self.thread_end.set()
                    raise
                print('Failed to insert:\n', item.tuple, '\n in ', table,
                      '\n With error:\n', e, '\nWill retry later')
                item.error = True
                item.priority = item.priority + 2
                self.queue.put(item)
            self.thread_lock.release()
            if item.block: self.queue.task_done()

    def getter(self):
        while not self.thread_end.is_set():
            self.thread_lock.acquire()
            self.setup_info = (self._schemata['experiment'].Control()
                               & dict(setup=self.setup)).fetch1()
            self.thread_lock.release()
            self.setup_status = self.setup_info['status']
            time.sleep(1)  # update once a second

    def log(self, table, data=dict(), **kwargs):
        tmst = self.logger_timer.elapsed_time()
        self.put(table=table,
                 tuple={
                     **self.trial_key, 'time': tmst,
                     **data
                 },
                 **kwargs)
        return tmst

    def log_setup(self, task_idx=False):
        rel = experiment.Control() & dict(setup=self.setup)
        key = rel.fetch1() if numpy.size(rel.fetch()) else dict(
            setup=self.setup)
        if task_idx: key['task_idx'] = task_idx
        key = {**key, 'ip': self.get_ip(), 'status': self.setup_status}
        self.put(table='Control',
                 tuple=key,
                 replace=True,
                 priority=1,
                 block=True,
                 validate=True)

    def get_last_session(self):
        last_sessions = (experiment.Session() & dict(
            animal_id=self.get_setup_info('animal_id'))).fetch('session')
        return 0 if numpy.size(last_sessions) == 0 else numpy.max(
            last_sessions)

    def log_session(self, params, log_protocol=False):
        self.total_reward = 0
        self.trial_key = dict(animal_id=self.get_setup_info('animal_id'),
                              trial_idx=0,
                              session=self.get_last_session() + 1)
        session_key = {
            **self.trial_key,
            **params, 'setup': self.setup,
            'user_name': params['user'] if 'user_name' in params else 'bot'
        }
        self.put(table='Session',
                 tuple=session_key,
                 priority=1,
                 validate=True,
                 block=True)
        if log_protocol:
            pr_name, pr_file = self.get_protocol(raw_file=True)
            git_hash = subprocess.check_output(
                ['git', 'rev-parse', '--short',
                 'HEAD']).decode('ascii').strip()
            self.put(table='Session.Protocol',
                     tuple={
                         **self.trial_key, 'protocol_name': pr_name,
                         'protocol_file': pr_file,
                         'git_hash': git_hash
                     })
        self.put(table='Configuration',
                 tuple=self.trial_key,
                 schema='behavior',
                 priority=2,
                 validate=True,
                 block=True)
        self.put(table='Configuration',
                 tuple=self.trial_key,
                 schema='stimulus',
                 priority=2,
                 validate=True,
                 block=True)
        ports = (experiment.SetupConfiguration.Port & {
            'setup_conf_idx': params['setup_conf_idx']
        }).fetch(as_dict=True)
        for port in ports:
            self.put(table='Configuration.Port',
                     tuple={
                         **port,
                         **self.trial_key
                     },
                     schema='behavior')
        screens = (experiment.SetupConfiguration.Screen & {
            'setup_conf_idx': params['setup_conf_idx']
        }).fetch(as_dict=True)
        for scr in screens:
            self.put(table='Configuration.Screen',
                     tuple={
                         **scr,
                         **self.trial_key
                     },
                     schema='stimulus')
        balls = (experiment.SetupConfiguration.Ball & {
            'setup_conf_idx': params['setup_conf_idx']
        }).fetch(as_dict=True)
        for ball in balls:
            self.put(table='Configuration.Ball',
                     tuple={
                         **ball,
                         **self.trial_key
                     },
                     schema='behavior')

        key = {
            'session': self.trial_key['session'],
            'trials': 0,
            'total_liquid': 0,
            'difficulty': 1
        }
        if 'start_time' in params:
            tdelta = lambda t: datetime.strptime(
                t, "%H:%M:%S") - datetime.strptime("00:00:00", "%H:%M:%S")
            key = {
                **key, 'start_time': str(tdelta(params['start_time'])),
                'stop_time': str(tdelta(params['stop_time']))
            }
        self.update_setup_info(key)
        self.logger_timer.start()  # start session time

    def update_setup_info(self, info):
        self.setup_info = {
            **(experiment.Control() & dict(setup=self.setup)).fetch1(),
            **info
        }
        block = True if 'status' in info else False
        self.put(table='Control',
                 tuple=self.setup_info,
                 replace=True,
                 priority=1,
                 block=block,
                 validate=block)
        self.setup_status = self.setup_info['status']

    def get_setup_info(self, field):
        return (experiment.Control() & dict(setup=self.setup)).fetch1(field)

    def get(self,
            schema='experiment',
            table='Control',
            fields='',
            key='',
            **kwargs):
        table = rgetattr(eval(schema), table)
        return (table() & key).fetch(*fields, **kwargs)

    def get_protocol(self, task_idx=None, raw_file=False):
        if not task_idx: task_idx = self.get_setup_info('task_idx')
        if not len(experiment.Task() & dict(task_idx=task_idx)) > 0:
            return False
        protocol = (experiment.Task()
                    & dict(task_idx=task_idx)).fetch1('protocol')
        path, filename = os.path.split(protocol)
        if not path:
            protocol = str(pathlib.Path(
                __file__).parent.absolute()) + '/../conf/' + filename
        if raw_file:
            return protocol, np.fromfile(protocol, dtype=np.int8)
        else:
            return protocol

    def update_trial_idx(self, trial_idx):
        self.trial_key['trial_idx'] = trial_idx

    def ping(self, period=5000):
        if self.ping_timer.elapsed_time(
        ) >= period:  # occasionally update control table
            self.ping_timer.start()
            self.update_setup_info({
                'last_ping':
                str(datetime.now().strftime("%Y-%m-%d %H:%M:%S")),
                'queue_size':
                self.queue.qsize(),
                'trials':
                self.trial_key['trial_idx'],
                'total_liquid':
                self.total_reward,
                'state':
                self.curr_state
            })

    def cleanup(self):
        while not self.queue.empty():
            print('Waiting for empty queue... qsize: %d' % self.queue.qsize())
            time.sleep(1)
        self.thread_end.set()

    @staticmethod
    def get_ip():
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        try:
            s.connect(("8.8.8.8", 80))
            IP = s.getsockname()[0]
        except Exception:
            IP = '127.0.0.1'
        finally:
            s.close()
        return IP
Ejemplo n.º 21
0
    def all(self,
            limit,
            offset,
            PRONAC=None,
            nome=None,
            proponente=None,
            cgccpf=None,
            area=None,
            segmento=None,
            UF=None,
            municipio=None,
            data_inicio=None,
            data_inicio_min=None,
            data_inicio_max=None,
            data_termino=None,
            data_termino_min=None,
            data_termino_max=None,
            ano_projeto=None,
            sort_field=None,
            sort_order=None):

        text_fields = (
            PreProjetoModel.Acessibilidade.label('acessibilidade'),
            PreProjetoModel.Objetivos.label('objetivos'),
            PreProjetoModel.Justificativa.label('justificativa'),
            PreProjetoModel.DemocratizacaoDeAcesso.label('democratizacao'),
            PreProjetoModel.EtapaDeTrabalho.label('etapa'),
            PreProjetoModel.FichaTecnica.label('ficha_tecnica'),
            PreProjetoModel.ResumoDoProjeto.label('resumo'),
            PreProjetoModel.Sinopse.label('sinopse'),
            PreProjetoModel.ImpactoAmbiental.label('impacto_ambiental'),
            PreProjetoModel.EspecificacaoTecnica.label(
                'especificacao_tecnica'),
            PreProjetoModel.EstrategiadeExecucao.label('estrategia_execucao'),
            ProjetoModel.ProvidenciaTomada.label('providencia'),
        )

        valor_proposta_case = coalesce(
            func.sac.dbo.fnValorDaProposta(ProjetoModel.idProjeto),
            func.sac.dbo.fnValorSolicitado(ProjetoModel.AnoProjeto,
                                           ProjetoModel.Sequencial))

        valor_aprovado_case = case([
            (ProjetoModel.Mecanismo == '2' or ProjetoModel.Mecanismo == '6',
             func.sac.dbo.fnValorAprovadoConvenio(ProjetoModel.AnoProjeto,
                                                  ProjetoModel.Sequencial)),
        ],
                                   else_=func.sac.dbo.fnValorAprovado(
                                       ProjetoModel.AnoProjeto,
                                       ProjetoModel.Sequencial))

        valor_projeto_case = case(
            [
                (ProjetoModel.Mecanismo == '2'
                 or ProjetoModel.Mecanismo == '6',
                 func.sac.dbo.fnValorAprovadoConvenio(
                     ProjetoModel.AnoProjeto, ProjetoModel.Sequencial)),
            ],
            else_=func.sac.dbo.fnValorAprovado(ProjetoModel.AnoProjeto,
                                               ProjetoModel.Sequencial) +
            func.sac.dbo.fnOutrasFontes(ProjetoModel.IdPRONAC))

        enquadramento_case = case(
            [(EnquadramentoModel.Enquadramento == '1', 'Artigo 26'),
             (EnquadramentoModel.Enquadramento == '2', 'Artigo 18')],
            else_='Nao enquadrado')

        ano_case = case([
            (ProjetoModel.Mecanismo == '2' or ProjetoModel.Mecanismo == '6',
             func.sac.dbo.fnValorAprovadoConvenio(ProjetoModel.AnoProjeto,
                                                  ProjetoModel.Sequencial)),
        ],
                        else_=func.sac.dbo.fnValorAprovado(
                            ProjetoModel.AnoProjeto, ProjetoModel.Sequencial))

        sort_mapping_fields = {
            'valor_solicitado':
            func.sac.dbo.fnValorSolicitado(ProjetoModel.AnoProjeto,
                                           ProjetoModel.Sequencial),
            'PRONAC':
            ProjetoModel.PRONAC,
            'outras_fontes':
            func.sac.dbo.fnOutrasFontes(ProjetoModel.IdPRONAC),
            'valor_captado':
            func.sac.dbo.fnCustoProjeto(ProjetoModel.AnoProjeto,
                                        ProjetoModel.Sequencial),
            'valor_proposta':
            valor_proposta_case,
            'valor_aprovado':
            valor_aprovado_case,
            'valor_projeto':
            valor_projeto_case,
            'ano_projeto':
            ProjetoModel.AnoProjeto,
            'data_inicio':
            ProjetoModel.DtInicioExecucao,
            'data_termino':
            ProjetoModel.DtFimExecucao,
        }

        if sort_field == None:
            sort_field = 'ano_projeto'
            sort_order = 'desc'

        sort_field = sort_mapping_fields[sort_field]

        with Timer(action='Database query for get_projeto_list method',
                   verbose=True):
            res = self.sql_connector.session.query(
                ProjetoModel.NomeProjeto.label('nome'),
                ProjetoModel.PRONAC.label('PRONAC'),
                ProjetoModel.AnoProjeto.label('ano_projeto'),
                ProjetoModel.UfProjeto.label('UF'),
                InteressadoModel.Cidade.label('municipio'),
                ProjetoModel.DtInicioExecucao.label('data_inicio'),
                ProjetoModel.DtFimExecucao.label('data_termino'),
                ProjetoModel.IdPRONAC,

                AreaModel.Descricao.label('area'),
                SegmentoModel.Descricao.label('segmento'),
                SituacaoModel.Descricao.label('situacao'),
                InteressadoModel.Nome.label('proponente'),
                InteressadoModel.CgcCpf.label('cgccpf'),
                MecanismoModel.Descricao.label('mecanismo'),

                func.sac.dbo.fnValorSolicitado(
                    ProjetoModel.AnoProjeto, ProjetoModel.Sequencial).label('valor_solicitado'),
                func.sac.dbo.fnOutrasFontes(
                    ProjetoModel.IdPRONAC).label('outras_fontes'),
                func.sac.dbo.fnCustoProjeto(
                    ProjetoModel.AnoProjeto, ProjetoModel.Sequencial).label('valor_captado'),
                valor_proposta_case.label('valor_proposta'),
                valor_aprovado_case.label('valor_aprovado'),
                valor_projeto_case.label('valor_projeto'),

                enquadramento_case.label('enquadramento'),

                *text_fields

            ).join(AreaModel)\
                .join(SegmentoModel)\
                .join(SituacaoModel)\
                .join(InteressadoModel)\
                .join(PreProjetoModel)\
                .join(MecanismoModel)\
                .outerjoin(EnquadramentoModel, EnquadramentoModel.IdPRONAC == ProjetoModel.IdPRONAC)

        if PRONAC is not None:
            res = res.filter(ProjetoModel.PRONAC == PRONAC)

        if area is not None:
            res = res.filter(AreaModel.Codigo == area)

        if segmento is not None:
            res = res.filter(SegmentoModel.Codigo == segmento)

        if proponente is not None:
            res = res.filter(InteressadoModel.Nome.like('%' + proponente +
                                                        '%'))

        if cgccpf is not None:
            res = res.filter(InteressadoModel.CgcCpf.like('%' + cgccpf + '%'))

        if nome is not None:
            res = res.filter(ProjetoModel.NomeProjeto.like('%' + nome + '%'))

        if UF is not None:
            res = res.filter(InteressadoModel.Uf == UF)

        if municipio is not None:
            res = res.filter(InteressadoModel.Cidade == municipio)

        if data_inicio is not None:
            res = res.filter(
                ProjetoModel.DtInicioExecucao >= data_inicio).filter(
                    projetoModel.DtInicioExecucao <= data_inicio + ' 23:59:59')

        if data_inicio_min is not None:
            res = res.filter(ProjetoModel.DtInicioExecucao >= data_inicio_min)

        if data_inicio_max is not None:
            res = res.filter(
                ProjetoModel.DtInicioExecucao <= data_inicio_max + ' 23:59:59')

        if data_termino is not None:
            res = res.filter(
                ProjetoModel.DtFimExecucao >= data_termino).filter(
                    ProjetoModel.DtFimExecucao <= data_termino + ' 23:59:59')

        if data_termino_min is not None:
            res = res.filter(ProjetoModel.DtFimExecucao >= data_termino_min)

        if data_termino_max is not None:
            res = res.filter(
                ProjetoModel.DtFimExecucao <= data_termino_max + ' 23:59:59')

        if ano_projeto is not None:
            res = res.filter(ProjetoModel.AnoProjeto == ano_projeto)

        # order by descending
        if sort_order == 'desc':
            res = res.order_by(desc(sort_field))
        # order by ascending
        else:
            res = res.order_by(sort_field)

        #res = res.order_by("AnoProjeto")

        total_records = self.count(res)

        # with Timer(action = 'Projects count() slow ', verbose = True):
        #   total_records = res.count()
        #   #Log.debug("total : "+str(total_records))

        # with Timer(action = 'Projects slice()', verbose = True):
        # res = res.slice(start_row, end_row)
        res = res.limit(limit).offset(offset)

        return res.all(), total_records
Ejemplo n.º 22
0
Copyright (C) 2014 - 2017, Vlon Jang(WeChat:wangqingbaidu)
Institute of Computing Technology, Chinese Academy of Sciences, Beijing, China.

The codes are mainly developed by Zhiwei Zhang.
As an open-source project, your can use or modify it as you want.

Contact Info: you can send an email to [email protected](Vlon) 
  or visit my website www.wangqingbaidu.cn

Note: Please keep the above information whenever or wherever the codes are used.
'''
from utils.Timer import Timer
from datetime import datetime
import time


def testfunc1():
    print "func 1"


def testfunc2(a, b):
    print a, b


now = datetime.strptime("", "")
print now
Timer(now, testfunc1).start()
Timer(now, testfunc2, ('wo', 'rk')).start()

# time.sleep(60)
Ejemplo n.º 23
0
def train(epoch, model, optimizer, criterion1, criterion2, lamb, loader,
          device, log_callback):

    end = time.time()
    model.train()

    for param_group in optimizer.param_groups:
        learning_rate = param_group['lr']

    # the output of the dataloader is (batch_idx, image, mask, c, v, t)
    for batch_idx, data in enumerate(loader):
        target_image, target_mask, input_c, input_v, input_t = data
        target_image = target_image.to(device, non_blocking=True)
        target_mask = target_mask.to(device, non_blocking=True)
        input_c = input_c.to(device, non_blocking=True)
        input_v = input_v.to(device, non_blocking=True)
        input_t = input_t.to(device, non_blocking=True)

        data_time.update(time.time() - end)

        # input all the input vectors into the model
        out_image, out_mask = model(input_c, input_v, input_t)

        # compute the loss according to the paper
        loss1 = criterion1(out_image, target_image)
        # Note that the target should remove the channel size as stated in document
        #loss2 = criterion2(out_mask, target_mask.long().squeeze())
        loss2 = criterion2(out_mask, target_mask)
        loss = loss1 + lamb * loss2

        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        batch_time.update(time.time() - end)
        end = time.time()

        # record essential informations into log file.
        if batch_idx % args.log_interval == 0:
            log_callback(
                'Epoch: {0}\t'
                'Time {batch_time.sum:.3f}s / {1} batches, ({batch_time.avg:.3f})\t'
                'Data load {data_time.sum:.3f}s / {1} batches, ({data_time.avg:3f})\n'
                'Learning rate = {2}\n'.format(epoch,
                                               args.log_interval,
                                               learning_rate,
                                               batch_time=batch_time,
                                               data_time=data_time))

            log_callback(
                'Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
                    epoch, batch_idx * len(data), len(loader.dataset),
                    100. * batch_idx / len(loader), loss.item()))
            log_callback()

            log_callback('Loss{0} = {loss1:.8f}\t'.format(1,
                                                          loss1=loss1.item()))

            log_callback('Loss{0} = {loss1:.8f}\t'.format(2,
                                                          loss1=loss2.item()))

            log_callback()
            log_callback("current time: " + Timer.timeString())

            batch_time.reset()
            data_time.reset()

    torch_utils.save(folderPath + 'ChairCNN_' + str(epoch) + '.cpkt', epoch,
                     model, optimizer, scheduler)