Exemplo 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)
Exemplo n.º 2
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)
Exemplo n.º 3
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")
Exemplo n.º 4
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)
Exemplo n.º 5
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
Exemplo n.º 6
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)
Exemplo 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
		)
Exemplo n.º 8
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)
Exemplo n.º 9
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
Exemplo n.º 10
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)
Exemplo n.º 11
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()
Exemplo n.º 12
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)
Exemplo n.º 13
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
Exemplo n.º 14
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
Exemplo 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