Esempio n. 1
0
def benchamin(arraycode):
	"""Benchmark the amin function.
	"""
	# These are set to target a balanced execution time for the different tests.
	pyitercounts = calibrationdata['amin'][0]
	afitercounts = calibrationdata['amin'][1]

	cycledata = list(range(128))
	data = array.array(arraycode, itertools.repeat(0, ARRAYSIZE))
	for x, y in zip(itertools.cycle(cycledata), itertools.repeat(0, ARRAYSIZE)):
		data[x] = x

	# Native Python time.
	starttime = time.perf_counter()
	for i in range(pyitercounts):
		x = min(data)
	endtime = time.perf_counter()

	pythontime = (endtime - starttime) / pyitercounts

	# Arrayfunc time.
	starttime = time.perf_counter()
	for i in range(afitercounts):
		x = arrayfunc.amin(data)
	endtime = time.perf_counter()

	functime = (endtime - starttime) / afitercounts

	return (pythontime, functime, pythontime / functime)
Esempio n. 2
0
    def authenticate(self, rfid):
        """
        Authenticate an ID

        Returns True if the server allows access for the ID or if the server is unavailable,
        will return True if the cache indicates the ID had recent access
        """
        print("Auth id: [{}]".format(rfid))

        values = {'id' : rfid}
        data = urllib.parse.urlencode(values)
        data = data.encode('utf-8')

        t1 = perf_counter()

        req = urllib.request.Request(self.auth_url, data)
        try:
            resp = urllib.request.urlopen(req, timeout=self.request_timeout)
        except URLError:
            print("TODO: log that the connection was rejected...")
            cached = self.auth_from_cache(rfid)
            return cached
        except timeout as err:
            cached = auth_from_cache(rfid)
            return cached

        text = resp.read()

        t2 = perf_counter()

        print("Auth got [{}] in {} sec".format(text, t2-t1))

        if text == b'Granted':
            return True
def perfTest(command, host, port):
    global exchange_size
    global tls_exchange_over
    # captureing network packets to get the overall size of the TCP exchange
    capture = Thread(target=sniff_network, args=(host, port))
    # Making the thread a daemon force it to qui when all other threads are exited.
    capture.start()

    # Thread needs a bit of time
    time.sleep(1)
    # Start the timers
    sysstart = time.perf_counter()
    start = time.process_time()
    #Run the command !
    #client_process = subprocess.Popen(command.split(), shell=False)
    #client_process.wait()
    os.system(command)
    # stop the timers
    stop = time.process_time()
    sysstop = time.perf_counter()

    global tls_exchange_over
    tls_exchange_over = True

    stats = {'Time (ms)': (sysstop-sysstart)*1000, 'CPU time (ms)': (stop-start)*1000}
    # cpu usage = processTime / System-wide time
    stats['cpu usage (%)'] = (stats['CPU time (ms)']/stats['Time (ms)'])*100

    #if capturing on localhost, divide the size by 2 because each p acket is sniffed twice.
    if host == "127.0.0.1":
        exchange_size /= 2
    stats['TCP size (bytes)'] = exchange_size
    return stats
Esempio n. 4
0
def benchfindindices(arraycode):
	"""Benchmark the findindices function.
	"""
	# These are set to target a balanced execution time for the different tests.
	pyitercounts = calibrationdata['findindices'][0]
	afitercounts = calibrationdata['findindices'][1]

	compval = comptype(arraycode, 10)
	data = array.array(arraycode, itertools.repeat(0, ARRAYSIZE))
	data[-1] = compval
	dataout = array.array('q', itertools.repeat(0, ARRAYSIZE))

	# Native Python time.
	starttime = time.perf_counter()
	for i in range(pyitercounts):
		z = 0
		for x in data:
			if x == compval:
				dataout[z] = z
				z += 1
	endtime = time.perf_counter()

	pythontime = (endtime - starttime) / pyitercounts

	dataout = array.array('q', itertools.repeat(0, ARRAYSIZE))

	# Arrayfunc time.
	starttime = time.perf_counter()
	for i in range(afitercounts):
		x = arrayfunc.findindices(arrayfunc.aops.af_eq, data, dataout, compval)
	endtime = time.perf_counter()

	functime = (endtime - starttime) / afitercounts

	return (pythontime, functime, pythontime / functime)
Esempio n. 5
0
def _generate_statements(stmt, args, iterations, duration):
    if duration is None:
        yield from itertools.repeat((stmt, args), iterations or 100)
    else:
        now = perf_counter()
        while perf_counter() - now < duration:
            yield (stmt, args)
Esempio n. 6
0
 def wait_for_light(self):
     start_time = time.perf_counter()
     while (time.perf_counter() - start_time < self.__WAIT_TIME) and (self.__ON not in self.__serialConnection.wait_for_response()):
         pass
     if start_time - curr_time >= self.__WAIT_TIME:
         return False
     return time.perf_counter() - start_time
    def run(self):

        activation_time = perf_counter()
        activation_locked = False

        while self.alive:

            # determine if any of the neighbours are active
            activity_level = 0
            num_active = 0

            for var in self.in_var.values():
                if var.val > activity_level:
                    activity_level = var.val
                if var.val > self.config["active_thres"]:
                    num_active += 1

            if self.out_var["neighbour_active"].val:
                if (
                    perf_counter() - activation_time > self.config["active_period"]
                    or activity_level < self.config["deactive_thres"]
                ):
                    self.out_var["neighbour_active"].val = False
                    activation_locked = True

            elif not activation_locked and activity_level >= self.config["active_thres"]:
                sleep(2.0)
                self.out_var["neighbour_active"].val = True
                activation_time = perf_counter()

            if activation_locked:
                if activity_level < self.config["deactive_thres"]:
                    activation_locked = False

            sleep(max(0, self.messenger.estimated_msg_period * 10))
Esempio n. 8
0
def scrap(category, field="", pages=[1], search="", use_proxy=False, proxy_file=None, use_tor=[]):
    """
    Will scrap all links from given pages (list).
    field = "time_add" for "AGE" ordering, "seeders" for "SEED".
    search = "movies", "tv" or blank.
    """
    conngen = connector_gen(use_proxy, proxy_file, use_tor)

    if use_tor[0]: kat_site = "http://lsuzvpko6w6hzpnn.onion"
    else: kat_site = choice(KATCR)

    if search: search += " " # Otherwise invalid link
    urls = [ "{}/usearch/{}category:{}/{}/".format(kat_site, search, category, page) 
             for page in pages 
             if page > 0 ]
    print(urls)
    params = {"field": field, "sorder": "desc"}

    loop = asyncio.get_event_loop()

    # Scrap links for torrents
    begin = perf_counter()
    to_do = [ get_pages(url, params, conngen) for url in urls ]
    wait_coro = asyncio.wait(to_do)
    res, _ = loop.run_until_complete(wait_coro)

    links = []
    for html in res:
        bs = Soup(html.result(), PARSER)
        torrents = bs.find_all("tr", {"id": re.compile("torrent_[A-Za-z0-9_]*")})
        links += [ choice(KATCR) + row.find("a", {"class": "cellMainLink"}).get("href")
                  for row in torrents ]
    delay(begin, "get torrents from pages.")

    # Scrap info from kickass
    begin = perf_counter()
    to_do = [ load_url(link, conngen) for link in links ]
    wait_coro = asyncio.wait(to_do)
    res, _ = loop.run_until_complete(wait_coro)
    delay(begin, "load_url().")

    # Scrap info from OMDB API
    begin = perf_counter()
    to_do = [ get_omdb(row.result(), conngen) for row in res 
              if row.result() is not None ]
    wait_coro = asyncio.wait(to_do)
    res, _ = loop.run_until_complete(wait_coro)
    delay(begin, "get_omdb().")

    # Download images
    begin = perf_counter()
    to_do = [ down_images(row.result(), conngen) for row in res ]
    wait_coro = asyncio.wait(to_do)
    res, _ = loop.run_until_complete(wait_coro)
    delay(begin, "down_images().")

    # loop.close()

    # Return list of dictionaries about torrents
    return [ tor.result() for tor in res ]
Esempio n. 9
0
	def update(self):
		if not self.paused:
			calc_start_time = time.perf_counter() # Get calculation start time
			# Run update loop until simulation is caught up with real time or time out occurs
			needs_update = (((time.perf_counter() - calc_start_time) < self.update_time_out and 
							self.sim_time < (time.perf_counter() - self.real_time_start)) or 
							self.run_max_speed)
			while needs_update:
				self.sim_time += self.dt
				self.swarm.update()
				self.predator.update()
				
				# Check termination conditions
				if (self.sim_time > self.time_out  or	# Time out
					len(self.swarm.agents) == 0 or		# All agents dead
					self.predator.is_kill):				# Predator is dead
					
					total_agent_health = 0
					for agent in self.swarm.agents:
						total_agent_health += agent.health
					
					print("time = ", self.sim_time)
					print("survivor_count = ", len(self.swarm.agents))
					print("total_agent_health = ", total_agent_health)
					print("predator_health = ", self.predator.health)
					return "terminate"
				needs_update = ((time.perf_counter() - calc_start_time) < self.update_time_out and 
							self.sim_time < (time.perf_counter() - self.real_time_start) and 
							not self.run_max_speed)
Esempio n. 10
0
 def recolorize(self):
     self.after_id = None
     if not self.delegate:
         if DEBUG: print("no delegate")
         return
     if not self.allow_colorizing:
         if DEBUG: print("auto colorizing is off")
         return
     if self.colorizing:
         if DEBUG: print("already colorizing")
         return
     try:
         self.stop_colorizing = False
         self.colorizing = True
         if DEBUG: print("colorizing...")
         t0 = time.perf_counter()
         self.recolorize_main()
         t1 = time.perf_counter()
         if DEBUG: print("%.3f seconds" % (t1-t0))
     finally:
         self.colorizing = False
     if self.allow_colorizing and self.tag_nextrange("TODO", "1.0"):
         if DEBUG: print("reschedule colorizing")
         self.after_id = self.after(1, self.recolorize)
     if self.close_when_done:
         top = self.close_when_done
         self.close_when_done = None
         top.destroy()
Esempio n. 11
0
    def format_msg(self, msg, dpth=3, ops='+fun:ln +wait==') :
        if '(==' in msg :
            # Начать замер нового интервала
            self.stms   = self.stms + [perf_counter()]
            msg = msg.replace( '(==', '(==[' + Tr.format_tm(0) + ']' )

        if '+fun:ln' in ops :
            frCaller= inspect.stack()[dpth] # 0-format_msg, 1-Tr.log|Tr.TrLiver, 2-log, 3-need func
            try:
                cls = frCaller[0].f_locals['self'].__class__.__name__ + '.'
            except:
                cls = ''
            fun     = (cls + frCaller[3]).replace('.__init__','()')
            ln      = frCaller[2]
            msg     = '[{}]{}{}:{} '.format( Tr.format_tm( perf_counter() - self.tm ), self.gap, fun, ln ) + msg
        else : 
            msg     = '[{}]{}'.format( Tr.format_tm( perf_counter() - self.tm ), self.gap ) + msg

        if '+wait==' in ops :
            if ( '==)' in msg or '==>' in msg ) and len(self.stms)>0 :
                # Закончить/продолжить замер последнего интервала и вывести его длительность
                sign    = '==)' if '==)' in msg else '==>'
                # sign    = icase( '==)' in msg, '==)', '==>' )
                stm = '[{}]'.format( Tr.format_tm( perf_counter() - self.stms[-1] ) )
                msg = msg.replace( sign, sign+stm )
                if '==)' in msg :
                    del self.stms[-1] 

            if '=}}' in msg :
                # Отменить все замеры
                self.stms   = []
                
        return msg.replace('¬',c9).replace('¶',c10)
Esempio n. 12
0
 def get_next(self):
     if self.item_for_next_get is None:
         try:
             tm, val = next(self.store_iterator)
         except Exception as ex:
             self.is_playing = False
             return None
         tm = int(tm)
         # print(1, tm)
         now = time.perf_counter() * self.TIME_FACTOR
         if self.last_returned_at is None:
             # First item, so return immediately
             self.last_returned_at = (now, tm)
             return self.get_parsed(val)
         else:
             if (now - self.last_returned_at[0]) >= (tm - self.last_returned_at[1]):
                 # Sufficient time has passed
                 self.last_returned_at = (now, tm)
                 return self.get_parsed(val)
             else:
                 # Keep item to return at next get
                 self.item_for_next_get = (tm, val)
     else:
         tm, val = self.item_for_next_get
         # print(2, tm)
         now = time.perf_counter() * self.TIME_FACTOR
         if (now - self.last_returned_at[0]) >= (tm - self.last_returned_at[1]):
             # Sufficient time has passed
             self.last_returned_at = (now, tm)
             self.item_for_next_get = None
             return self.get_parsed(val)
Esempio n. 13
0
def main():
    if len(sys.argv) != 4:
        print('error: you must supply exactly four arguments\n\n' +
              'usage: python3 <Python source code file> <text file> <selection||merge> <n>')
        sys.exit(1)

    filename = sys.argv[1]
    sort_type = sys.argv[2]
    n = int(sys.argv[3])

    input_list = read_file(filename, n)

    print('Requested n = ' + str(n))
    print('Loaded ' + str(n) + ' lines from "' + filename + '"')
    first_ten(input_list)

    if sort_type == "selection":
        print('Selection Sort [O(n2)]...')
        start = time.perf_counter()
        sorted_list = selection_sort(input_list)
        end = time.perf_counter()
    elif sort_type == "merge":
        print('Merge Sort [O(n log n)]...')
        start = time.perf_counter()
        sorted_list = merge_sort(input_list)
        end = time.perf_counter()

    first_ten(sorted_list)
    print('Elapsed time: ' + str(end-start) + " seconds")
Esempio n. 14
0
def search(shelve_to_process, shelve_key):

	# start timing of search functionality
	start_time = time.perf_counter() * 1000

	s = shelve.open(shelve_to_process)

	indexedFiles = s[shelve_key]

	keepGoing = True
	while keepGoing:
		query = input("query (type . to exit):")
		if query == '.':
			keepGoing = False
		else:
			terms = query.split(" ")

			hasOr = False
			hasAnd = "and" in terms
			if not hasAnd:
				hasOr = "or" in terms
				if not hasOr:
					hasAnd = True

			#remove the delimiter terms now
			while "and" in terms:
				terms.remove("and")
			while "or" in terms:
				terms.remove("or")

			goodFiles = set()

			if hasOr:
				for term in terms:
					goodFiles = goodFiles.union(indexedFiles[term])

			if hasAnd:
				for term in terms:
					if term in indexedFiles:
						if len(goodFiles) == 0:
							goodFiles.update(indexedFiles[term])
						else:
							goodFiles = goodFiles.intersection(indexedFiles[term])

			# finish timing of search functionality
			end_time = time.perf_counter() * 1000

			# print the output
			word = "OR" if hasOr else "AND"

			print("Performing " + word + 
			   " search for: {\'" + '\', \''.join(terms) + "\'}")

			if len(goodFiles) == 0:
				print("Sorry, no files found with that query!")
			else:
				for gf in goodFiles:
					print("Found at", gf)

			print("Execution time:", int(end_time - start_time))
def main():
    if len(sys.argv) != 3:
        print('Error, must supply 2 arguments.\n\n' + 
              'Usage: python3 project_3.py <text_file> <n>')
        sys.exit(1)

    filename = sys.argv[1]
    n = int(sys.argv[2])

    # read up to n words from the file
    word_list = []
    input_file = open(filename, 'r')
    for i in range(n):
        word_list.append(input_file.readline().rstrip())
    input_file.close()

    # print input information and first 10 words of input list
    print('requested n = {}'.format(n))
    print("loaded {} lines from '{}'".format(n, filename))
    print_ten_words(word_list)
    print('\nselection sort...')

    # run and time the selection sort algorithm
    start = time.perf_counter()
    sorted_list = selection_sort(word_list)
    end = time.perf_counter()

    # print run time information and first 10 words of sorted list
    print_ten_words(sorted_list)
    print('\nelapsed time: {} seconds'.format(end - start))
Esempio n. 16
0
def test_recorder_get_next_incoming_only(recorder):
    incoming_count = 100
    incoming = [(randomString(100), randomString(6)) for _ in
                range(incoming_count)]

    while incoming:
        recorder.add_incoming(*incoming.pop())
        time.sleep(random.choice([0, 1]) + random.random())

    recorded_incomings = OrderedDict()
    keys = []
    for k, v in recorder.store.iterator(include_value=True):
        v = Recorder.get_parsed(v)
        keys.append(int(k))
        recorded_incomings[int(k)] = v

    assert len(recorded_incomings) == incoming_count
    assert sorted(keys) == keys

    max_time_to_run = incoming_count * 2 + 10
    recorder.start_playing()
    start = time.perf_counter()

    while recorder.is_playing and (time.perf_counter() < start + max_time_to_run):
        vals = recorder.get_next()
        if vals:
            check = recorded_incomings.popitem(last=False)[1]
            assert check == vals
        else:
            time.sleep(0.01)

    assert len(recorded_incomings) == 0
    assert not recorder.is_playing
Esempio n. 17
0
    def run(self):
        UPDATE_INTERVAL_MS = self.update_interval_ms
        UPDATE_PER_FRAME_LIMIT = self.update_per_frame_limit
        clock = 0
        previous_time = perf_counter()
        next_update = 0
        proceed = True

        while proceed:
            current_time = perf_counter()
            if current_time - previous_time > (UPDATE_INTERVAL_MS * UPDATE_PER_FRAME_LIMIT):
                clock += UPDATE_INTERVAL_MS * UPDATE_PER_FRAME_LIMIT
            else:
                clock += current_time - previous_time
            while clock >= next_update:
                time_elapsed = UPDATE_INTERVAL_MS
                time_current = next_update
                self.update(time_elapsed, time_current)
                next_update += UPDATE_INTERVAL_MS
            previous_time = perf_counter()
            self.render()
            if terminal.has_input():
                key = terminal.read()
                if key is terminal.TK_CLOSE:
                    proceed = False
                else:
                    proceed = self.process_input(key)
Esempio n. 18
0
    async def get(self, request, datetime=None):
        """Return history over a period of time."""
        timer_start = time.perf_counter()
        if datetime:
            datetime = dt_util.parse_datetime(datetime)

            if datetime is None:
                return self.json_message('Invalid datetime', HTTP_BAD_REQUEST)

        now = dt_util.utcnow()

        one_day = timedelta(days=1)
        if datetime:
            start_time = dt_util.as_utc(datetime)
        else:
            start_time = now - one_day

        if start_time > now:
            return self.json([])

        end_time = request.query.get('end_time')
        if end_time:
            end_time = dt_util.parse_datetime(end_time)
            if end_time:
                end_time = dt_util.as_utc(end_time)
            else:
                return self.json_message('Invalid end_time', HTTP_BAD_REQUEST)
        else:
            end_time = start_time + one_day
        entity_ids = request.query.get('filter_entity_id')
        if entity_ids:
            entity_ids = entity_ids.lower().split(',')
        include_start_time_state = 'skip_initial_state' not in request.query

        hass = request.app['hass']

        result = await hass.async_add_job(
            get_significant_states, hass, start_time, end_time,
            entity_ids, self.filters, include_start_time_state)
        result = list(result.values())
        if _LOGGER.isEnabledFor(logging.DEBUG):
            elapsed = time.perf_counter() - timer_start
            _LOGGER.debug(
                'Extracted %d states in %fs', sum(map(len, result)), elapsed)

        # Optionally reorder the result to respect the ordering given
        # by any entities explicitly included in the configuration.

        if self.use_include_order:
            sorted_result = []
            for order_entity in self.filters.included_entities:
                for state_list in result:
                    if state_list[0].entity_id == order_entity:
                        sorted_result.append(state_list)
                        result.remove(state_list)
                        break
            sorted_result.extend(result)
            result = sorted_result

        return await hass.async_add_job(self.json, result)
Esempio n. 19
0
def main():
    if len(sys.argv) != 3:
        print(
            "error: you must supply exactly two arguments\n\n"
            + "usage: python <Python source code file> <text file> <n>"
        )
        sys.exit(1)

    filename = sys.argv[1]
    n = int(sys.argv[2])

    entire_file = open(filename).read()
    print('Loaded "' + filename + '" of length ' + str(len(entire_file)))
    print("n =", n)

    # take only the first n characters of entire_file
    s = entire_file[:n]
    assert len(s) == n

    start1 = time.perf_counter()
    x = max_char(s)
    end1 = time.perf_counter()
    start2 = time.perf_counter()
    y = longest_oreo(s)
    end2 = time.perf_counter()

    print("largest char = ", ord(x))
    print("elapsed time = " + str(end1 - start1))
    print("longest oreo = [", y, "]")
    print("elapsed time = " + str(end2 - start2))
Esempio n. 20
0
def main():
	args = parser.parse_args()

	if (args.logging_config):
		import logging
		import logging.config
		logging.config.fileConfig(args.logging_config)

	if (six.PY3):
		t0 = time.perf_counter()
	else:
		t0 = time.clock()
	data = cyacd.BootloaderData.read(args.image)
	session = make_session(args, data.checksum_type)
	bl = BootloaderHost(session, sys.stdout)
	try:
		bl.bootload(
			data,
			seek_permission(
				args.downgrade,
				"Device version %d is greater than local version %d. Flash anyway? (Y/N)"),
			seek_permission(
				args.newapp,
				"Device app ID %d is different from local app ID %d. Flash anyway? (Y/N)"))
	except (protocol.BootloaderError, BootloaderError) as e:
		print ("Unhandled error: {}".format(e))
		return 1
	if (six.PY3):
		t1 = time.perf_counter()
	else:
		t1 = time.clock()
	print ("Total running time {0:02.2f}s".format(t1-t0))
	return 0
Esempio n. 21
0
def states_to_json(
        hass,
        states,
        start_time,
        entity_ids,
        filters=None,
        include_start_time_state=True):
    """Convert SQL results into JSON friendly data structure.

    This takes our state list and turns it into a JSON friendly data
    structure {'entity_id': [list of states], 'entity_id2': [list of states]}

    We also need to go back and create a synthetic zero data point for
    each list of states, otherwise our graphs won't start on the Y
    axis correctly.
    """
    result = defaultdict(list)

    # Get the states at the start time
    timer_start = time.perf_counter()
    if include_start_time_state:
        for state in get_states(hass, start_time, entity_ids, filters=filters):
            state.last_changed = start_time
            state.last_updated = start_time
            result[state.entity_id].append(state)

    if _LOGGER.isEnabledFor(logging.DEBUG):
        elapsed = time.perf_counter() - timer_start
        _LOGGER.debug(
            'getting %d first datapoints took %fs', len(result), elapsed)

    # Append all changes to it
    for ent_id, group in groupby(states, lambda state: state.entity_id):
        result[ent_id].extend(group)
    return result
def preprocess_signals_and_peaks():
    dataio = DataIO(dirname=dirname)
    catalogueconstructor = CatalogueConstructor(dataio=dataio)
    print(dataio)


    catalogueconstructor.set_preprocessor_params(chunksize=1024,
            memory_mode='memmap',
            
            #signal preprocessor
            #~ signalpreprocessor_engine='numpy',
            signalpreprocessor_engine='opencl',
            highpass_freq=300, 
            common_ref_removal=True,
            backward_chunksize=1280,
            
            #peak detector
            peakdetector_engine='numpy',
            #~ peakdetector_engine='opencl',
            peak_sign='-', 
            relative_threshold=7,
            peak_span=0.0005,
            )
            
    t1 = time.perf_counter()
    catalogueconstructor.estimate_signals_noise(seg_num=0, duration=10.)
    t2 = time.perf_counter()
    print('estimate_signals_noise', t2-t1)
    
    t1 = time.perf_counter()
    catalogueconstructor.run_signalprocessor(duration=60.)
    t2 = time.perf_counter()
    print('run_signalprocessor', t2-t1)
    
    print(catalogueconstructor)
Esempio n. 23
0
def _get_related_entity_ids(session, entity_filter):
    from homeassistant.components.recorder.models import States
    from homeassistant.components.recorder.util import \
        RETRIES, QUERY_RETRY_WAIT
    from sqlalchemy.exc import SQLAlchemyError
    import time

    timer_start = time.perf_counter()

    query = session.query(States).with_entities(States.entity_id).distinct()

    for tryno in range(0, RETRIES):
        try:
            result = [
                row.entity_id for row in query
                if entity_filter(row.entity_id)]

            if _LOGGER.isEnabledFor(logging.DEBUG):
                elapsed = time.perf_counter() - timer_start
                _LOGGER.debug(
                    'fetching %d distinct domain/entity_id pairs took %fs',
                    len(result),
                    elapsed)

            return result
        except SQLAlchemyError as err:
            _LOGGER.error("Error executing query: %s", err)

            if tryno == RETRIES - 1:
                raise
            time.sleep(QUERY_RETRY_WAIT)
Esempio n. 24
0
def run(in_globals=False):
    global output
    doc["console"].value = ''
    src = editor.getValue()
    if storage is not None:
       storage["py_src"] = src

    t0 = time.perf_counter()
    try:
        if(in_globals):
            exec(src)
        else:
            ns = {}
            exec(src, ns)
        state = 1
    except Exception as exc:
        traceback.print_exc(file=sys.stderr)
        state = 0
    output = doc["console"].value

    print('Brython: %6.2f ms' % ((time.perf_counter() - t0) * 1000.0))
    
    # run with CPython
    req = ajax.ajax()
    req.bind('complete',on_complete)
    req.set_timeout(4,err_msg)
    req.open('POST','/cgi-bin/speed.py',True)
    req.set_header('content-type','application/x-www-form-urlencoded')
    req.send({'src':src})
    
    return state
Esempio n. 25
0
    def _delay_accordingly(self, current_clock):
        scheduling_interval = 0.05  # Seconds till reschedule of process

        elapsed_time = time.perf_counter() - self._last_process_time

        if elapsed_time < scheduling_interval:
            # FIXME: Not very content with this. Limits our resolution a lot.
            #        Should be fine for interactive use but will suck for
            #        interfacing with say an outside circuit. Not that I expect
            #        us to do a PWM but 20Hz _if_ things go right isn't that
            #        great. It's totally fixable though. Should switch to a
            #        min{next_event, control_proc_schedule} with busy loop for
            #        small delays. In any case I'm not sure how much much
            #        timing precision we can squeeze out this anyways.
            time.sleep(scheduling_interval - elapsed_time)

        # Set target clock independently of history. This prevents the
        # simulation from trying to "catch" up. Imho the right way for
        # interactive control. If this behavior is wanted we should switch
        # from a delta to an absolute simulation time calculation.
        target_clock = current_clock + \
            self._simulation_rate * scheduling_interval

        self._last_process_time = time.perf_counter()
        return target_clock, self._last_process_time + scheduling_interval
Esempio n. 26
0
def timeblock(label):
    start = time.perf_counter()
    try:
        yield
    finally:
        end = time.perf_counter()
        print('{}:{}'.format(label,end - start))
Esempio n. 27
0
  def run_performance_test(self, src):
      #execute code
      t0 = time.perf_counter()
      try:
        exec(src)
        state = 1
      except Exception as exc:
        traceback.print_exc(file=sys.stderr)

      self.add_brython_result(int((time.perf_counter() - t0) * 1000.0))
      src = src.replace('\r\n','\n')
      
      self._timings[self._filename]['code'] = src

      def err_msg(*args):
          from javascript import console
          console.log(args)

      #also run this for cpython via cgi-bin
      # run with CPython
      req = ajax.ajax()
      req.bind('complete',self.on_cpython_complete)
      req.set_timeout(4,err_msg)
      req.open('POST','/cgi-bin/script_timer.py',False)
      req.set_header('content-type','application/x-www-form-urlencoded')
      req.send({'filename': self._filename})

      return state
Esempio n. 28
0
 def update_properties(self):
     """Handle data changes for node values."""
     self._state = self.values.primary.data
     if self.refresh_on_update and \
             time.perf_counter() - self.last_update > 30:
         self.last_update = time.perf_counter()
         self.node.request_state()
Esempio n. 29
0
def benchasumov(arraycode):
	"""Benchmark the asum function with overflow checking disabled.
	"""
	# These are set to target a balanced execution time for the different tests.
	pyitercounts = calibrationdata['asum'][0]
	afitercounts = calibrationdata['asum'][1]

	compval = comptype(arraycode, 10)
	data = array.array(arraycode, itertools.repeat(0, ARRAYSIZE))
	data[-1] = compval

	# Native Python time.
	starttime = time.perf_counter()
	for i in range(pyitercounts):
		x = sum(data)
	endtime = time.perf_counter()

	pythontime = (endtime - starttime) / pyitercounts

	# Arrayfunc time.
	starttime = time.perf_counter()
	for i in range(afitercounts):
		x = arrayfunc.asum(data, ov=True)
	endtime = time.perf_counter()

	functime = (endtime - starttime) / afitercounts

	return (pythontime, functime, pythontime / functime)
Esempio n. 30
0
 def wrapped_function(*args, **kwargs):
     start = time.perf_counter()
     result = function(*args, **kwargs)
     duration_ms = int((time.perf_counter() - start) * 1000)
     msg = {
         "task_name": function.__module__ + "." + function.__name__,
         "duration": duration_ms,
         # Use 'task_args' instead of 'args' because 'args' conflicts with
         # attribute of LogRecord
         "task_args": args,
         "task_kwargs": kwargs,
     }
     # Only log if slow value not provided
     # or duration in milliseconds is greater than slow value in milliseconds.
     if not slow or duration_ms > int(slow * 1000):
         if function.__name__.startswith("task_"):
             logger.info(
                 "TASK: function=%s duration=%sms",
                 function.__name__,
                 duration_ms,
                 extra=msg,
             )
         else:
             logger.info(
                 "FUNC: function=%s duration=%sms",
                 function.__name__,
                 duration_ms,
                 extra=msg,
             )
     return result
Esempio n. 31
0
    def run(
        cls,
        model: AbsESPnetModel,
        optimizers: Sequence[torch.optim.Optimizer],
        schedulers: Sequence[Optional[AbsScheduler]],
        train_iter_factory: AbsIterFactory,
        valid_iter_factory: AbsIterFactory,
        plot_attention_iter_factory: Optional[AbsIterFactory],
        reporter: Reporter,
        output_dir: Path,
        max_epoch: int,
        seed: int,
        patience: Optional[int],
        keep_nbest_models: int,
        early_stopping_criterion: Sequence[str],
        best_model_criterion: Sequence[Sequence[str]],
        val_scheduler_criterion: Sequence[str],
        trainer_options,
        distributed_option: DistributedOption,
    ) -> None:
        """Perform training. This method performs the main process of training."""
        assert check_argument_types()
        # NOTE(kamo): Don't check the type more strictly as far trainer_options
        assert is_dataclass(trainer_options), type(trainer_options)

        # NOTE(kamo): trainer_options doesn't always have "train_dtype"
        use_apex = getattr(trainer_options, "train_dtype", "") in (
            "O0",
            "O1",
            "O2",
            "O3",
        )
        if use_apex:
            try:
                from apex import amp
            except ImportError:
                logging.error("You need to install apex. "
                              "See https://github.com/NVIDIA/apex#linux")

        start_epoch = reporter.get_epoch() + 1
        if start_epoch == max_epoch + 1:
            logging.warning(
                f"The training has already reached at max_epoch: {start_epoch}"
            )

        if distributed_option.distributed:
            # Use torch DDP instead of apex DDP
            # https://github.com/NVIDIA/apex/issues/494
            dp_model = torch.nn.parallel.DistributedDataParallel(
                model,
                device_ids=(
                    # Perform multi-Process with multi-GPUs
                    [torch.cuda.current_device()]
                    if distributed_option.ngpu == 1
                    # Perform single-Process with multi-GPUs
                    else None),
                output_device=(torch.cuda.current_device()
                               if distributed_option.ngpu == 1 else None),
            )
        elif distributed_option.ngpu > 1:
            # apex.amp supports DataParallel now.
            dp_model = torch.nn.parallel.DataParallel(
                model,
                device_ids=list(range(distributed_option.ngpu)),
            )
        else:
            # NOTE(kamo): DataParallel also should work with ngpu=1,
            # but for debuggability it's better to keep this block.
            dp_model = model

        if not distributed_option.distributed or distributed_option.dist_rank == 0:
            summary_writer = SummaryWriter(str(output_dir / "tensorboard"))
        else:
            summary_writer = None

        start_time = time.perf_counter()
        for iepoch in range(start_epoch, max_epoch + 1):
            if iepoch != start_epoch:
                logging.info(
                    "{}/{}epoch started. Estimated time to finish: {}".format(
                        iepoch,
                        max_epoch,
                        humanfriendly.format_timespan(
                            (time.perf_counter() - start_time) /
                            (iepoch - start_epoch) * (max_epoch - iepoch + 1)),
                    ))
            else:
                logging.info(f"{iepoch}/{max_epoch}epoch started")
            set_all_random_seed(seed + iepoch)

            reporter.set_epoch(iepoch)
            # 1. Train and validation for one-epoch
            with reporter.observe("train") as sub_reporter:
                all_steps_are_invalid = cls.train_one_epoch(
                    model=dp_model,
                    optimizers=optimizers,
                    schedulers=schedulers,
                    iterator=train_iter_factory.build_iter(iepoch),
                    reporter=sub_reporter,
                    options=trainer_options,
                )

            with reporter.observe("valid") as sub_reporter:
                cls.validate_one_epoch(
                    model=dp_model,
                    iterator=valid_iter_factory.build_iter(iepoch),
                    reporter=sub_reporter,
                    options=trainer_options,
                )

            if not distributed_option.distributed or distributed_option.dist_rank == 0:
                # att_plot doesn't support distributed
                if plot_attention_iter_factory is not None:
                    with reporter.observe("att_plot") as sub_reporter:
                        cls.plot_attention(
                            model=model,
                            output_dir=output_dir / "att_ws",
                            summary_writer=summary_writer,
                            iterator=plot_attention_iter_factory.build_iter(
                                iepoch),
                            reporter=sub_reporter,
                            options=trainer_options,
                        )

            # 2. LR Scheduler step
            for scheduler in schedulers:
                if isinstance(scheduler, AbsValEpochStepScheduler):
                    scheduler.step(
                        reporter.get_value(*val_scheduler_criterion))
                elif isinstance(scheduler, AbsEpochStepScheduler):
                    scheduler.step()

            if not distributed_option.distributed or distributed_option.dist_rank == 0:
                # 3. Report the results
                logging.info(reporter.log_message())
                reporter.matplotlib_plot(output_dir / "images")
                reporter.tensorboard_add_scalar(summary_writer)

                # 4. Save/Update the checkpoint
                torch.save(
                    {
                        "model":
                        model.state_dict(),
                        "reporter":
                        reporter.state_dict(),
                        "optimizers": [o.state_dict() for o in optimizers],
                        "schedulers": [
                            s.state_dict() if s is not None else None
                            for s in schedulers
                        ],
                        "amp":
                        amp.state_dict() if use_apex else None,
                    },
                    output_dir / "checkpoint.pth",
                )

                # 5. Save the model and update the link to the best model
                torch.save(model.state_dict(),
                           output_dir / f"{iepoch}epoch.pth")

                # Creates a sym link latest.pth -> {iepoch}epoch.pth
                p = output_dir / "latest.pth"
                if p.is_symlink() or p.exists():
                    p.unlink()
                p.symlink_to(f"{iepoch}epoch.pth")

                _improved = []
                for _phase, k, _mode in best_model_criterion:
                    # e.g. _phase, k, _mode = "train", "loss", "min"
                    if reporter.has(_phase, k):
                        best_epoch = reporter.get_best_epoch(_phase, k, _mode)
                        # Creates sym links if it's the best result
                        if best_epoch == iepoch:
                            p = output_dir / f"{_phase}.{k}.best.pth"
                            if p.is_symlink() or p.exists():
                                p.unlink()
                            p.symlink_to(f"{iepoch}epoch.pth")
                            _improved.append(f"{_phase}.{k}")
                if len(_improved) == 0:
                    logging.info("There are no improvements in this epoch")
                else:
                    logging.info("The best model has been updated: " +
                                 ", ".join(_improved))

                # 6. Remove the model files excluding n-best epoch and latest epoch
                _removed = []
                # Get the union set of the n-best among multiple criterion
                nbests = set().union(*[
                    set(reporter.sort_epochs(ph, k, m)[:keep_nbest_models])
                    for ph, k, m in best_model_criterion
                    if reporter.has(ph, k)
                ])
                for e in range(1, iepoch):
                    p = output_dir / f"{e}epoch.pth"
                    if p.exists() and e not in nbests:
                        p.unlink()
                        _removed.append(str(p))
                if len(_removed) != 0:
                    logging.info("The model files were removed: " +
                                 ", ".join(_removed))

            # 7. If any updating haven't happened, stops the training
            if all_steps_are_invalid:
                logging.warning(
                    f"The gradients at all steps are invalid in this epoch. "
                    f"Something seems wrong. This training was stopped at {iepoch}epoch"
                )
                break

            # 8. Check early stopping
            if patience is not None:
                if reporter.check_early_stopping(patience,
                                                 *early_stopping_criterion):
                    break

        else:
            logging.info(f"The training was finished at {max_epoch} epochs ")
Esempio n. 32
0
 def Saw(self):
     print('\n开始爬取' + self.id + '的读过列表')
     beg = eval(input('请输入你要爬取的起始页码(比如1):'))
     end = eval(input('请输入终止页码(建议一次爬取10页以下):'))
     page = beg
     homepage = 'https://book.douban.com/people/' + self.id
     tr = self.s.get(homepage)
     Sfirstpage = 'https://book.douban.com/people/' + self.id + '/collect?&sort=time&start=' + str(
         (beg - 1) * 30) + '&filter=all&mode=list'
     req = self.s.get(Sfirstpage)
     soup = BeautifulSoup(req.text, 'html.parser')
     print(f'第{page}页', req.status_code)
     #get book name and id
     saw = soup.find_all(class_=['item'])
     for i in range(len(saw)):
         date, star, comment, owntag, name, bid = self.saw_get(saw[i])
         self.saw_dict[bid]={'书名':name,'作者':'','译者':'','原作名':'','出版社':'',\
                             '出版年':'','页数':'','ISBN':'','评分':'','评分人数':'',\
                             '用户评分':star,'短评':comment,'用户标签':owntag,'标记日期':date}
     #get all saw list
     while 1:
         sleep(uniform(1.5, 4))
         if page == end:
             break
         try:
             NextPage = 'https://book.douban.com' + soup.find(
                 class_='next').link.get('href')
         except:
             break
         else:
             req = self.s.get(NextPage)
             soup = BeautifulSoup(req.text, 'html.parser')
             page += 1
             print(f'第{page}页', req.status_code)
             saw = soup.find_all(class_=['item'])
             for i in range(len(saw)):
                 date, star, comment, owntag, name, bid = self.saw_get(
                     saw[i])
                 self.saw_dict[bid]={'书名':name,'作者':'','译者':'','原作名':'','出版社':'',\
                                     '出版年':'','页数':'','ISBN':'','评分':'','评分人数':'',\
                                     '用户评分':star,'短评':comment,'用户标签':owntag,'标记日期':date}
     #add feature for every book
     count = 0
     st = perf_counter()
     total = len(self.saw_dict)
     fail = []
     for bid in self.saw_dict.keys():
         count += 1
         if count % 50 == 0:
             sleep(10)
         sleep(uniform(1.5, 4))
         timebar(30, st, count / total)
         fail.append(self.get_feature(bid, 'saw'))
     print('\n再次尝试打开失败的书籍页')
     sleep(10)
     for fbid in fail:
         if fbid != None:
             sleep(2)
             print()
             self.get_feature(fbid, 'saw')
     return self.saw_dict
Esempio n. 33
0
def face_locator(names_known, encodings_known, names_new, encodings_new, \
		input_queue, ouput_queue, quit_queue, tracking_names_queue):
	# to save time in loops initialize variables before
	base_frame = None; regular_frame = None; p_time = None
	quit = False
	boxes = None; encodings = None; trackers = None; names = None
	encoding = None; box = None
	matched_names = None; probabilities = None; found = None
	name = None; encod = None
	matches = None
	startX = None; startY = None; endX = None; endY = None;
	rect = None; tracker = None
	m = None; max_idxs = None; temp_names = None

	to_mark = 1

	tracking_names = []
	#print('in to face_locator Process   pid:', getpid())

	# function work until will get True in quit_queue
	while True:
		# handle situation to exit from while loop
		if quit_queue.qsize():
			# clean queues
			_ = quit_queue.get()
			
			q_size = input_queue.qsize()
			for i_empty in range(q_size):
				_,_,_ = input_queue.get()
			#print('clean {} items on input_queue pid: {}'.format(q_size, getpid()))

			q_size = ouput_queue.qsize()
			for i_empty in range(q_size):
				_,_,_,_ = ouput_queue.get()
			#print('clean {} items on output_queue pid: {}'.format(q_size, getpid()))
			#print('out 1 of face_locator Process pid:', getpid())
			return

		trackers = []; names = []

		# if available frame - make it base frame and track other frames on input_queue
		if input_queue.qsize():
			#print('started base frame with {} frame. Process pid: {}'.format(input_queue.qsize(), getpid()))
			tic = perf_counter()
			base_frame, to_mark, p_time = input_queue.get()
			base_frame = cv2.cvtColor(base_frame, cv2.COLOR_BGR2RGB)

			boxes = face_recognition.face_locations(base_frame, number_of_times_to_upsample=1, model='hog')
			encodings = face_recognition.face_encodings(base_frame, boxes)

			# go througth all boxes find name and start tracker
			for (encoding, box) in zip(encodings, boxes):
				startX = box[3]; startY = box[0]; endX = box[1]; endY = box[2]
				rect = dlib.rectangle(startX, startY, endX, endY)
				tracker = dlib.correlation_tracker()
				tracker.start_track(base_frame, rect)
				matched_names = [] # for current encoding gather all matched names with different probabilities
				probabilities = []
				temp_names = ''
				found = False

				# to speed up algorithm in first place compare encodigs with current names
				if tracking_names_queue.qsize():
					tracking_names = tracking_names_queue.get()
				for name in tracking_names:
					idx = names_known.index(name)
					matches = face_recognition.compare_faces(encodings_known[idx], encoding, tolerance=.6)
					if any(matches):
						names.append(name)
						trackers.append(tracker)
						if to_mark:
							cv2.rectangle(base_frame, (startX, startY), (endX, endY), (0, 255, 0), 2)
							cv2.putText(base_frame, name, (startX, startY - 15), cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 255, 0), 2)
						found = True
						break

				if found:		# jump to next encoding
					found = False
					continue

				# searching in rest names
				for (name, encod) in zip(names_known, encodings_known):
					# check if we already compared with this name above
					if name in tracking_names:
						continue

					matches = face_recognition.compare_faces(encod, encoding, tolerance=.6)
					if all(matches) and len(matches) > 1:
						names.append(name)
						trackers.append(tracker)
						if to_mark:
							cv2.rectangle(base_frame, (startX, startY), (endX, endY), (0, 255, 0), 2)
							cv2.putText(base_frame, name, (startX, startY - 15), cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 255, 0), 2)
						found = True
						break
					elif all(matches) and len(matches) == 1:
						matched_names.append(name)
						probabilities.append(.5)
						continue
					elif any(matches) and len(matches) > 1:
						matched_names.append(name)
						probabilities.append(sum(matches) / len(matches))
				if found:		# jump to next encoding
					found = False
					continue
				if not len(matched_names):			# current encoding are undefined
					names.append('underfined')
					trackers.append(tracker)
					continue
				m = max(probabilities)				# case when couple names with same probability
				max_idxs = [i for i, j in enumerate(probabilities) if j == m]
				for i in max_idxs:
					temp_names += matched_names[i]
					temp_names += '_or_'
				if temp_names.endswith('_or_'):
					temp_names = temp_names[0:-4]

				names.append(temp_names)
				trackers.append(tracker)
				if to_mark:
					cv2.rectangle(base_frame, (startX, startY), (endX, endY), (0, 255, 0), 2)
					cv2.putText(base_frame, temp_names, (startX, startY - 15), cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 255, 0), 2)

			base_frame = cv2.cvtColor(base_frame, cv2.COLOR_RGB2BGR)
			ouput_queue.put((base_frame, names, 0, p_time));#print('names:', names, ' Process pid:', getpid())
			
			toc = perf_counter()
			time_locate = toc - tic
			#print('time to locate', time_locate, ' Process pid:', getpid())

			# tracking all rest frames
			tic = perf_counter()
			#print('regular frames:', input_queue.qsize(), ' pid:', getpid())
			while input_queue.qsize():
				#print('while input_queue.qsize():', input_queue.qsize(), ' pid:', getpid())
				# handle situation to exit from while loop
				if quit_queue.qsize():
					# clean queues
					_ = quit_queue.get()
					
					q_size = input_queue.qsize()
					for i_empty in range(q_size):
						_,_,_ = input_queue.get()
					#print('clean {} items on input_queue pid: {}'.format(q_size, getpid()))

					q_size = ouput_queue.qsize()
					for i_empty in range(q_size):
						_,_,_,_ = ouput_queue.get()
					#print('clean {} items on output_queue pid: {}'.format(q_size, getpid()))
					#print('out 2 of face_locator Process pid:', getpid())
					return

				regular_frame, to_mark, p_time = input_queue.get()
				if len(names):
					regular_frame = cv2.cvtColor(regular_frame, cv2.COLOR_BGR2RGB)

					for (tracker, name) in zip(trackers, names):
						tracker.update(regular_frame)
						if to_mark:
							pos = tracker.get_position()
							startX = int(pos.left())
							startY = int(pos.top())
							endX = int(pos.right())
							endY = int(pos.bottom())
							cv2.rectangle(regular_frame, (startX, startY), (endX, endY), (0, 255, 0), 2)
							cv2.putText(regular_frame, name, (startX, startY + 15), cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 255, 0), 2)
					regular_frame = cv2.cvtColor(regular_frame, cv2.COLOR_RGB2BGR)

				# if it is the last frame
				# then it transfer to main Process with frame_koef
				# calculate this value are possible only after curren while loop
				if input_queue.qsize():
					ouput_queue.put((regular_frame, names, 0, p_time))

			toc = perf_counter()
			time_track = toc - tic
			frame_koef = 1.4 - time_track / time_locate
			ouput_queue.put((regular_frame, names, frame_koef, p_time)) # transfer to main Process last frame with frame_koef
        for i in np.arange(1, len(SampledMatrix[:, 1]), 1):
            if SampledMatrix[i, 0] != SampledMatrix[i - 1, 0]:
                AcceptanceNumber = AcceptanceNumber + 1
        Acceptprob = AcceptanceNumber / Acceptancetotal
        print(Acceptprob)
        if Acceptprob < 0.3:
            print(MCPAR[5])
            MCPAR[5] = MCPAR[5] + 10
            MCPAR[6] = MCPAR[6] + 10
        if Acceptprob > 0.45:
            print(MCPAR[5])
            MCPAR[5] = MCPAR[5] - 10
            MCPAR[6] = MCPAR[6] - 10

    start_time = time.perf_counter()
    MCPAR[0] = 24
    MCPAR[2] = 400000
    MCMCInitial = MCMCInit_ID1(FITPAR, FITPARLB, FITPARUB, MCPAR)
    MCMC_List = [0] * int(MCPAR[0])

    for i in range(int(MCPAR[0])):
        MCMC_List[i] = MCMCInitial[i, :]
    if __name__ == '__main__':
        pool = Pool(processes=24)

        F = pool.map(MCMC_ID1, MCMC_List)

        Savename = 'P' + str(int(Pitch)) + '_' + 'W' + str(
            int(W)) + '_' + 'H' + str(int(H)) + '_' + 'A' + str(int(Angle))
        end_time = time.perf_counter()
Esempio n. 35
0
def main():
    node = os.getenv('NODE_NAME')
    if node:
        import kubernetes.config
        import kubernetes.client
        try:
            kubernetes.config.load_incluster_config()
        except Exception:
            kubernetes.config.load_kube_config()
        kube = kubernetes.client.CoreV1Api()
        # verify that we can talk to the node
        kube.read_node(node)

    path_to_check = os.getenv('PATH_TO_CHECK', '/var/lib/docker')
    delay = float(os.getenv('IMAGE_GC_DELAY', '1'))
    gc_low = float(os.getenv('IMAGE_GC_THRESHOLD_LOW', '60'))
    gc_high = float(os.getenv('IMAGE_GC_THRESHOLD_HIGH', '80'))
    client = docker.from_env(version='auto')
    images = get_docker_images(client)

    logging.info(
        f'Pruning docker images when {path_to_check} has {gc_high}% inodes or blocks used'
    )

    while True:
        used = get_used_percent(path_to_check)
        logging.info(f'{used:.1f}% used')
        if used < gc_high:
            # Do nothing! We have enough space
            pass
        else:
            images = get_docker_images(client)
            if not images:
                logging.info(f'No images to delete')
            else:
                logging.info(f'{len(images)} images available to prune')

            start = time.perf_counter()
            images_before = len(images)

            if node:
                logging.info(f"Cordoning node {node}")
                cordon(kube, node)

            deleted = 0

            while images and get_used_percent(path_to_check) > gc_low:
                # Ensure the node is still cordoned
                if node:
                    logging.info(f"Cordoning node {node}")
                    cordon(kube, node)
                # Remove biggest image
                image = images.pop(0)
                if image.tags:
                    # does it have a name, e.g. jupyter/base-notebook:12345
                    name = image.tags[0]
                else:
                    # no name, use id
                    name = image.id
                gb = image.attrs['Size'] / (2**30)
                logging.info(f'Removing {name} (size={gb:.2f}GB)')
                try:
                    client.images.remove(image=image.id, force=True)
                    logging.info(f'Removed {name}')
                    # Delay between deletions.
                    # A sleep here avoids monopolizing the Docker API with deletions.
                    time.sleep(delay)
                except docker.errors.APIError as e:
                    if e.status_code == 409:
                        # This means the image can not be removed right now
                        logging.info(
                            f'Failed to remove {name}, skipping this image')
                        logging.info(str(e))
                    elif e.status_code == 404:
                        logging.info(
                            f'{name} not found, probably already deleted')
                    else:
                        raise
                except requests.exceptions.ReadTimeout:
                    logging.warning(f'Timeout removing {name}')
                    # Delay longer after a timeout, which indicates that Docker is overworked
                    time.sleep(max(delay, 30))
                else:
                    deleted += 1

            if node:
                logging.info(f"Uncordoning node {node}")
                uncordon(kube, node)

            # log what we did and how long it took
            duration = time.perf_counter() - start
            images_deleted = images_before - len(images)
            logging.info(
                f"Deleted {images_deleted} images in {int(duration)} seconds")

        time.sleep(60)
Esempio n. 36
0
print(
    "Iterating qubit count over the following values:\n{}".format(qubit_iter))
trials_iter = list(range(N_TRIALS))
print("Running random circuits for {} trials".format(N_TRIALS))

# Iterate over all benchmarking parameters
results = np.zeros((len(cpu_iter), len(qubit_iter), N_TRIALS),
                   dtype=np.float64)
for i, n_cpu in enumerate(cpu_iter):
    if n_cpu is not None:
        pool = multiproc.MultiprocContext(n_cpus=n_cpu).pool()
    for j, n_qubits in enumerate(qubit_iter):
        for k, trial in enumerate(trials_iter):

            # simulate NUM_CIRCUITS_TO_RUN-many circuits specified by the
            # parameter package below
            circuit_specs = [(n_qubits, CIRCUIT_DEPTH, OP_DENSITY, SEED + el)
                             for el in range(NUM_CIRCUITS_TO_RUN)]
            t0 = time.perf_counter()
            if n_cpu is not None:
                pool.map(randcircuit_f, circuit_specs)
            else:
                for package in circuit_specs:
                    randcircuit_f(package)
            results[i, j, k] = time.perf_counter() - t0
    pool.close()

fname = f"benchmark_{N_CPU}_{N_QUBITS_MAX}_{N_TRIALS}_{NUM_CIRCUITS_TO_RUN}"
np.save(fname, results)
print(f"Results saved to: {fname}.npy")
Esempio n. 37
0
    def train_one_epoch(
        cls,
        model: torch.nn.Module,
        iterator: Iterable[Tuple[List[str], Dict[str, torch.Tensor]]],
        optimizers: Sequence[torch.optim.Optimizer],
        schedulers: Sequence[Optional[AbsScheduler]],
        reporter: SubReporter,
        options: TrainerOptions,
    ) -> bool:
        assert check_argument_types()

        # Note(kamo): assumes one optimizer
        assert cls.num_optimizers == 1, cls.num_optimizers
        assert len(optimizers) == 1, len(optimizers)
        optimizer = optimizers[0]
        scheduler = schedulers[0]

        grad_noise = options.grad_noise
        accum_grad = options.accum_grad
        grad_clip = options.grad_clip
        log_interval = options.log_interval
        no_forward_run = options.no_forward_run
        ngpu = options.ngpu
        distributed = isinstance(model,
                                 torch.nn.parallel.DistributedDataParallel)
        use_apex = options.train_dtype in ("O0", "O1", "O2", "O3")

        if log_interval is None:
            try:
                log_interval = max(len(iterator) // 20, 10)
            except TypeError:
                log_interval = 100

        model.train()
        all_steps_are_invalid = True
        # [For distributed] Because iteration counts are not always equals between
        # processes, send stop-flag to the other processes if iterator is finished
        iterator_stop = torch.tensor(0).to("cuda" if ngpu > 0 else "cpu")

        start_time = time.perf_counter()
        for iiter, (_, batch) in enumerate(
                reporter.measure_iter_time(iterator, "iter_time"), 1):
            assert isinstance(batch, dict), type(batch)

            if distributed:
                torch.distributed.all_reduce(iterator_stop, ReduceOp.SUM)
                if iterator_stop > 0:
                    break

            batch = to_device(batch, "cuda" if ngpu > 0 else "cpu")
            if no_forward_run:
                all_steps_are_invalid = False
                reporter.register({})
                continue

            with reporter.measure_time("forward_time"):
                loss, stats, weight = model(**batch)
            if ngpu > 1 or distributed:
                # Apply weighted averaging for loss and stats
                loss = (loss * weight.type(loss.dtype)).sum()

                # if distributed, this method can also apply all_reduce()
                stats, weight = recursive_average(stats, weight, distributed)

                # Now weight is summation over all workers
                loss /= weight
            if distributed:
                # NOTE(kamo): Multiply world_size because DistributedDataParallel
                # automatically normalizes the gradient by world_size.
                loss *= torch.distributed.get_world_size()

            reporter.register(stats, weight)

            loss /= accum_grad
            with reporter.measure_time("backward_time"):
                if use_apex:
                    try:
                        from apex import amp
                    except ImportError:
                        logging.error(
                            "You need to install apex. "
                            "See https://github.com/NVIDIA/apex#linux")

                    with amp.scale_loss(loss, optimizers) as scaled_loss:
                        scaled_loss.backward()
                else:
                    loss.backward()

            if iiter % accum_grad == 0:
                # gradient noise injection
                if grad_noise:
                    add_gradient_noise(
                        model,
                        reporter.get_total_count(),
                        duration=100,
                        eta=1.0,
                        scale_factor=0.55,
                    )

                # compute the gradient norm to check if it is normal or not
                grad_norm = torch.nn.utils.clip_grad_norm_(
                    model.parameters(), grad_clip)
                # PyTorch<=1.4, clip_grad_norm_ returns float value
                if not isinstance(grad_norm, torch.Tensor):
                    grad_norm = torch.tensor(grad_norm)

                if not torch.isfinite(grad_norm):
                    logging.warning(
                        f"The grad norm is {grad_norm}. Skipping updating the model."
                    )
                else:
                    all_steps_are_invalid = False
                    with reporter.measure_time("optim_step_time"):
                        optimizer.step()
                    if isinstance(scheduler, AbsBatchStepScheduler):
                        scheduler.step()
                optimizer.zero_grad()

                # Register lr and train/load time[sec/step],
                # where step refers to accum_grad * mini-batch
                reporter.register(
                    dict(
                        {
                            f"lr_{i}": pg["lr"]
                            for i, pg in enumerate(optimizer.param_groups)
                            if "lr" in pg
                        },
                        train_time=time.perf_counter() - start_time,
                    ),
                    # Suppress to increment the internal counter.
                    not_increment_count=True,
                )
                start_time = time.perf_counter()

            if iiter % log_interval == 0:
                logging.info(reporter.log_message())

        else:
            if distributed:
                iterator_stop.fill_(1)
                torch.distributed.all_reduce(iterator_stop, ReduceOp.SUM)

        return all_steps_are_invalid
Esempio n. 38
0
def recordGhostData(obj, currentGhost):
    if (not logic.finishedLastLap):
        startTime = time.perf_counter()

        currentGhost["frames"].append(getFrameData(obj, currentGhost))
        endTime = time.perf_counter()
Esempio n. 39
0
    await asyncio.sleep(len(line) / 5 + 2)
    delta = time.perf_counter() - start
    print(f'Time of execution md5 with {line} is {delta}')
    return hashlib.md5(line.encode())


async def sha1(line):
    line = prepare_line(line)
    start = time.perf_counter()
    await asyncio.sleep(len(line) / 5)
    delta = time.perf_counter() - start
    print(f'Time of execution sha1 with {line} is {delta}')
    return hashlib.sha1(line.encode())


async def chain(i, line):
    hashed_line = await md5(line)
    hashed_line = await sha1(hashed_line)
    print(f'{i} chain. {str(hashed_line)}')


async def main(lines):
    await asyncio.gather(*(chain(i, line) for i, line in enumerate(lines)))


if __name__ == '__main__':
    main_start = time.perf_counter()
    asyncio.run(main(LINES))
    main_delta = time.perf_counter() - main_start
    print(f'Time of execution: {main_delta}s')
Esempio n. 40
0
        if create_spoiler:
            command += " --create_spoiler"
            if create_spoiler == 2:
                command += " --skip_playthrough"
        if zip_diffs:
            command += " --create_diff"
        if race:
            command += " --race"
        if os.path.exists(os.path.join(player_files_path, meta_file_path)):
            command += f" --meta {os.path.join(player_files_path, meta_file_path)}"

        print(command)
        import time

        start = time.perf_counter()
        text = subprocess.check_output(command, shell=True).decode()
        print(
            f"Took {time.perf_counter() - start:.3f} seconds to generate multiworld."
        )
        seedname = ""

        for segment in text.split():
            if segment.startswith("M"):
                seedname = segment
                break

        multidataname = f"BM_{seedname}.multidata"
        spoilername = f"BM_{seedname}_Spoiler.txt"
        romfilename = ""
def stopwatch(hide_text: bool = False) -> str:
    return curses.wrapper(run, perf_counter(), not hide_text)
Esempio n. 42
0
import socket
import time

ip = '0.0.0.0'
porta = 9999

tcp = socket.socket(socket.AF_INET,socket.SOCK_STREAM)

tcp.bind((ip,porta))
tcp.listen(5)

print ("Ouvindo dados em %s %d" %(ip,porta))
while True:
    concetcion, cliente = tcp.accept()
    print(concetcion,cliente)
    print ('Concetado por', cliente)
    

    while True:
        start = time.perf_counter() * 1000
        msg = concetcion.recv(1024)
        if not msg: 
            break
        end = time.perf_counter() * 1000
        latencia = end - start
        vazão = (1000/latencia) * 1024
        print("vazão: %.4f" %vazão)
        print("latencia: %.2f" %latencia)       
        print (cliente, msg)
        print ('Finalizando conexao do cliente', cliente)
        concetcion.close()
    def fit(self, X_l, X_u, patClassId):
        """
        Training the classifier
        
         Xl             Input data lower bounds (rows = objects, columns = features)
         Xu             Input data upper bounds (rows = objects, columns = features)
         patClassId     Input data class labels (crisp). patClassId[i] = 0 corresponds to an unlabeled item
        
        """
        print('--Online Learning--')

        if self.isNorm == True:
            X_l, X_u = self.dataPreprocessing(X_l, X_u)
        X_l = X_l.astype(np.float32)
        X_u = X_u.astype(np.float32)
        time_start = time.perf_counter()

        yX, xX = X_l.shape
        teta = self.teta

        mark = np.array([
            '*', 'o', 'x', '+', '.', ',', 'v', '^', '<', '>', '1', '2', '3',
            '4', '8', 's', 'p', 'P', 'h', 'H', 'X', 'D', '|', '_'
        ])
        mark_col = np.array(['r', 'g', 'b', 'y', 'c', 'm', 'k'])

        listLines = list()
        listInputSamplePoints = list()

        if self.isDraw:
            drawing_canvas = self.initialize_canvas_graph(
                "GFMM - Online learning", xX)

            if self.V.size > 0:
                # draw existed hyperboxes
                color_ = np.array(['k'] * len(self.classId), dtype=object)
                for c in range(len(self.classId)):
                    if self.classId[c] < len(mark_col):
                        color_[c] = mark_col[self.classId[c]]

                hyperboxes = drawbox(self.V[:, 0:np.minimum(xX, 3)],
                                     self.W[:, 0:np.minimum(xX, 3)],
                                     drawing_canvas, color_)
                listLines.extend(hyperboxes)
                self.delay()

        self.misclass = 1

        while self.misclass > 0 and teta >= self.tMin:
            # for each input sample
            for i in range(yX):
                classOfX = patClassId[i]
                # draw input samples
                if self.isDraw:
                    if i == 0 and len(listInputSamplePoints) > 0:
                        # reset input point drawing
                        for point in listInputSamplePoints:
                            point.remove()
                        listInputSamplePoints.clear()

                    color_ = 'k'
                    if classOfX < len(mark_col):
                        color_ = mark_col[classOfX]

                    if (X_l[i, :] == X_u[i, :]).all():
                        marker_ = 'd'
                        if classOfX < len(mark):
                            marker_ = mark[classOfX]

                        if xX == 2:
                            inputPoint = drawing_canvas.plot(X_l[i, 0],
                                                             X_l[i, 1],
                                                             color=color_,
                                                             marker=marker_)
                        else:
                            inputPoint = drawing_canvas.plot([X_l[i, 0]],
                                                             [X_l[i, 1]],
                                                             [X_l[i, 2]],
                                                             color=color_,
                                                             marker=marker_)

                        #listInputSamplePoints.append(inputPoint)
                    else:
                        inputPoint = drawbox(
                            np.asmatrix(X_l[i, 0:np.minimum(xX, 3)]),
                            np.asmatrix(X_u[i, 0:np.minimum(xX, 3)]),
                            drawing_canvas, color_)

                    listInputSamplePoints.append(inputPoint[0])
                    self.delay()

                if self.V.size == 0:  # no model provided - starting from scratch
                    self.V = np.array([X_l[0]])
                    self.W = np.array([X_u[0]])
                    self.classId = np.array([patClassId[0]])

                    if self.isDraw == True:
                        # draw hyperbox
                        box_color = 'k'
                        if patClassId[0] < len(mark_col):
                            box_color = mark_col[patClassId[0]]

                        hyperbox = drawbox(
                            np.asmatrix(self.V[0, 0:np.minimum(xX, 3)]),
                            np.asmatrix(self.W[0, 0:np.minimum(xX, 3)]),
                            drawing_canvas, box_color)
                        listLines.append(hyperbox[0])
                        self.delay()

                else:
                    b = membership_gfmm(X_l[i], X_u[i], self.V, self.W,
                                        self.gamma)

                    index = np.argsort(b)[::-1]
                    bSort = b[index]

                    if bSort[0] != 1 or (classOfX != self.classId[index[0]]
                                         and classOfX != 0):
                        adjust = False
                        for j in index:
                            # test violation of max hyperbox size and class labels
                            if (classOfX == self.classId[j]
                                    or self.classId[j] == 0 or classOfX
                                    == 0) and ((np.maximum(self.W[j], X_u[i]) -
                                                np.minimum(self.V[j], X_l[i]))
                                               <= teta).all() == True:
                                # adjust the j-th hyperbox
                                self.V[j] = np.minimum(self.V[j], X_l[i])
                                self.W[j] = np.maximum(self.W[j], X_u[i])
                                indOfWinner = j
                                adjust = True
                                if classOfX != 0 and self.classId[j] == 0:
                                    self.classId[j] = classOfX

                                if self.isDraw:
                                    # Handle drawing graph
                                    box_color = 'k'
                                    if self.classId[j] < len(mark_col):
                                        box_color = mark_col[self.classId[j]]

                                    try:
                                        listLines[j].remove()
                                    except:
                                        pass

                                    hyperbox = drawbox(
                                        np.asmatrix(
                                            self.V[j, 0:np.minimum(xX, 3)]),
                                        np.asmatrix(
                                            self.W[j, 0:np.minimum(xX, 3)]),
                                        drawing_canvas, box_color)
                                    listLines[j] = hyperbox[0]
                                    self.delay()

                                break

                        # if i-th sample did not fit into any existing box, create a new one
                        if not adjust:
                            self.V = np.concatenate(
                                (self.V, X_l[i].reshape(1, -1)), axis=0)
                            self.W = np.concatenate(
                                (self.W, X_u[i].reshape(1, -1)), axis=0)
                            self.classId = np.concatenate(
                                (self.classId, [classOfX]))

                            if self.isDraw:
                                # handle drawing graph
                                box_color = 'k'
                                if self.classId[-1] < len(mark_col):
                                    box_color = mark_col[self.classId[-1]]

                                hyperbox = drawbox(
                                    np.asmatrix(X_l[i, 0:np.minimum(xX, 3)]),
                                    np.asmatrix(X_u[i, 0:np.minimum(xX, 3)]),
                                    drawing_canvas, box_color)
                                listLines.append(hyperbox[0])
                                self.delay()

                        elif self.V.shape[0] > 1:
                            for ii in range(self.V.shape[0]):
                                if ii != indOfWinner and self.classId[
                                        ii] != self.classId[indOfWinner]:
                                    caseDim = hyperbox_overlap_test(
                                        self.V, self.W, indOfWinner,
                                        ii)  # overlap test

                                    if caseDim.size > 0:
                                        self.V, self.W = hyperbox_contraction(
                                            self.V, self.W, caseDim, ii,
                                            indOfWinner)
                                        if self.isDraw:
                                            # Handle graph drawing
                                            boxii_color = boxwin_color = 'k'
                                            if self.classId[ii] < len(
                                                    mark_col):
                                                boxii_color = mark_col[
                                                    self.classId[ii]]

                                            if self.classId[indOfWinner] < len(
                                                    mark_col):
                                                boxwin_color = mark_col[
                                                    self.classId[indOfWinner]]

                                            try:
                                                listLines[ii].remove()
                                                listLines[indOfWinner].remove()
                                            except:
                                                pass

                                            hyperboxes = drawbox(
                                                self.V[[ii, indOfWinner],
                                                       0:np.minimum(xX, 3)],
                                                self.W[[ii, indOfWinner],
                                                       0:np.minimum(xX, 3)],
                                                drawing_canvas,
                                                [boxii_color, boxwin_color])
                                            listLines[ii] = hyperboxes[0]
                                            listLines[
                                                indOfWinner] = hyperboxes[1]
                                            self.delay()

            teta = teta * 0.9
            if teta >= self.tMin:
                result = predict(self.V, self.W, self.classId, X_l, X_u,
                                 patClassId, self.gamma, self.oper)
                self.misclass = result.summis

        # Draw last result


#        if self.isDraw == True:
#            # Handle drawing graph
#            drawing_canvas.cla()
#            color_ = np.empty(len(self.classId), dtype = object)
#            for c in range(len(self.classId)):
#                color_[c] = mark_col[self.classId[c]]
#
#            drawbox(self.V[:, 0:np.minimum(xX, 3)], self.W[:, 0:np.minimum(xX, 3)], drawing_canvas, color_)
#            self.delay()
#
#        if self.isDraw:
#            plt.show()

        time_end = time.perf_counter()
        self.elapsed_training_time = time_end - time_start

        return self
Esempio n. 44
0
"""
Представлен список чисел. Определить элементы списка, не имеющие повторений. Сформировать из этих элементов список с сохранением порядка их следования в исходном списке, например:
src = [2, 2, 2, 7, 23, 1, 44, 44, 3, 2, 10, 7, 4, 11]
result = [23, 1, 3, 10, 4, 11]
Подсказка: напишите сначала решение «в лоб». Потом подумайте об оптимизации.
"""

import time


start = time.perf_counter()
src = [2, 2, 2, 7, 23, 1, 44, 44, 3, 2, 10, 7, 4, 11, 11]
result = set()
tmp = set()

for i in src:
    if i not in tmp:
        result.add(i)
    else:
        result.discard(i)
    tmp.add(i)

# print(perf_counter() - start)
result_ord = [i for i in src if i in result]
print(result_ord)
print(time.perf_counter() - start)
            test.comparing_versions(before_flash, after_flash, dut_ip)
            test.storing_results(before_flash, after_flash, dut_ip, cwd)
            return flashDict

    else:
        print("DUT IP: %s is not live.\n" % dut_ip)
        test.adding_to_results(("DUT IP: %s   -->   OFFLINE" % dut_ip),cwd)
    flashDict[dut_ip] = flashing_status
    resultDict.update(flashDict)
    return flashDict   


if __name__ == "__main__":
    email=input("Please enter an E-mail to receive version comparisons (or press enter to skip): ")
    # email='*****@*****.**'
    t1=time.perf_counter()
    flash_ec = flash_cb = False
    resultDict = dict()

    #Check if destination folder exist else exit
    if not os.path.isdir(bin_location):
        print("Binaries folder doesn't exist. Creating one. Copy binaries into folder named latest and rerun flashing script!")
        createFolders(bin_location)
        sys.exit(1)

    binaryDict = find_and_return_latest_binaries(bin_location)

    if binaryDict:
        if not "ec" in binaryDict:
            binaryDict["ec"] = ""
        if not "cb" in binaryDict:
Esempio n. 46
0
SYS = sys()


#%% set derivites of driving function specific to this problem
def df(self):
    return 0  #no driving function here


def ddf(self):
    return 0  #no driving function here


setattr(SYS, 'df', df)
setattr(SYS, 'ddf', ddf)
#%%
tic = ttime.perf_counter()
SYS = dynamic_analysis(SYS, t_start, t_step, t_end, file)
toc = ttime.perf_counter()

elapsed_time = toc - tic
print("time elapsed=", elapsed_time)

#%%
#Output can be saved easily to .csv, pickle, or your favorite file type.
# output_csv_file=os.getcwd()+"\\out.csv"
# output_pickle_file=os.getcwd()+"\\out.pkl"
# SYS.outputs.to_csv(output_csv_file)
# SYS.outputs.to_pickle(output_pickle_file)
#%%

#Get positions from output DataFrame
Esempio n. 47
0
            if (producerThread.is_alive() == False):
                print(threading.current_thread().name + " stopped")
                break


# 1 producer and many consumer thread

if __name__ == "__main__":
    stop = True

    producerThread = Thread(name='pr', target=read_thread, args=(qu, ))
    producerThread.start()

    array_of_threads = []

    start = perf_counter()
    array = [
        Thread(name=str(i), target=process_thread, args=(qu, ))
        for i in range(3)
    ]

    for i in array:
        i.start()

    for i in array:
        i.join()

    producerThread.join()

    end = perf_counter()
Esempio n. 48
0
    def run(self):
        # Priority settings for "MasterBoardTask"
        print(
            f"Master board task started with pid: {os.getpid()} and priority: {os.sched_getparam(os.getpid())}"
        )

        pid = os.getpid()  # Identify process ID
        sched = os.SCHED_FIFO  # FIFO real time scheduler
        param = os.sched_param(90)  # Process priority

        os.sched_setscheduler(pid, sched, param)  # Update priority settings

        print(
            f"Master board task with pid: {os.getpid()} changed to priority: {os.sched_getparam(os.getpid())}\n"
        )

        # Local variables
        dt = config.dt
        timeout = False
        current_time = 0.0
        cpt = 0

        # Troubleshooting arrays (Normally not used)
        plot_duration = 1000

        plot_array_size = int(plot_duration / dt)

        time_per_timestep_array = np.zeros(plot_array_size + 1)
        cycle_time_array = np.zeros(plot_array_size + 1)
        ic_time_array = np.zeros(plot_array_size + 1)

        # Trajectory and Queue initiation
        self.trajectory_queue_init()

        q, q_prime = self.master_board.get_state()
        trajectory = None

        # Timer initiation
        cycle_time = 0
        program_start = time.perf_counter()
        timestep_start = time.perf_counter()
        timestep_end = time.perf_counter()
        cycle_start = time.perf_counter()

        # Communication with MasterBoard
        while (self._running and not timeout):
            if trajectory == None:

                time_before_initial_condition = time.perf_counter()

                if not self.message_queue.empty():
                    input_message = self.message_queue.get(block=False)
                    self.handle_input(input_message, q)

                # No trajectory, try to get new from queue.
                elif not self.trajectory_queue.empty():
                    trajectory = self.trajectory_queue.get(
                        block=False)  # Check queue

                    current_position = self.master_board.get_state()[
                        0]  # Update joints current position

                    trajectory.set_initial_conditions(
                        q,
                        current_time)  # Set initial condition for trajectory

                else:
                    if self._terminating:
                        self._running = False

                ic_time_array[cpt] = time.perf_counter(
                ) - time_before_initial_condition
            else:
                # Follow current trajectory
                q, q_prime = trajectory.step(current_time)
                timeout = self.master_board.set_reference(q, q_prime)

                if trajectory.Done():
                    if self._terminating:
                        self.terminate()
                    else:
                        trajectory = None

            #Shared position. This allows InputTask to read the current position from a shared array
            current_position = self.master_board.get_actual_state()[0]
            with self.shared_position.get_lock():
                self.shared_position[:] = current_position

            # Update current reference to each joint.
            timeout = self.master_board.track_reference()
            self.plot.add_time(current_time)

            # Cycle time is time spent calculating trajectory
            cycle_time = time.perf_counter() - cycle_start
            cycle_time_array[cpt] = cycle_time

            if cycle_time >= dt:
                cycle_time = dt

            # Sleep MasterBoard for 1ms.
            time.sleep(dt - cycle_time)  # ideally: dt - cycle_time = 1ms
            cycle_start = time.perf_counter()  # Start time for next cycle

            timestep_end = cycle_start
            time_per_timestep_array[cpt] = timestep_end - timestep_start
            timestep_start = cycle_start

            current_time = current_time + dt

            if cpt < plot_array_size:
                cpt = cpt + 1

        self.master_board.terminate()

        #Reset prio
        sched = os.SCHED_OTHER
        param = os.sched_param(os.sched_get_priority_min(sched))
        os.sched_setscheduler(pid, sched, param)

        print(
            f"Master board task with pid: {os.getpid()} changed to priority: {os.sched_getparam(os.getpid())}"
        )

        plt.plot(time_per_timestep_array[0:cpt - 1])
        plt.show()
        plt.plot(cycle_time_array[0:cpt - 1])
        plt.show()
        plt.plot(ic_time_array[0:cpt - 1])
        plt.show()

        for i in range(config.num_joints // 2):
            self.plot.create_axis([
                f"Joint {2 * i}, Position Reference",
                f"Joint {2 * i}, Position Reading"
            ])
            self.plot.create_axis([
                f"Joint {2 * i + 1}, Position Reference",
                f"Joint {2 * i + 1}, Position Reading"
            ])
            self.plot.plot()
Esempio n. 49
0
    def run(self):
        """Run pipeline."""
        n_pts = 0
        frame_id = 0
        t1 = time.perf_counter()
        if self.video:
            self.rgbd_frame = self.video.next_frame()
        else:
            self.rgbd_frame = self.camera.capture_frame(
                wait=True, align_depth_to_color=True)

        pcd_errors = 0
        while (not self.flag_exit
               and (self.video is None or  # Camera
                    (self.video and not self.video.is_eof()))):  # Video
            if self.video:
                future_rgbd_frame = self.executor.submit(self.video.next_frame)
            else:
                future_rgbd_frame = self.executor.submit(
                    self.camera.capture_frame,
                    wait=True,
                    align_depth_to_color=True)

            if self.flag_save_pcd:
                self.save_pcd()
                self.flag_save_pcd = False
            try:
                self.rgbd_frame = self.rgbd_frame.to(self.o3d_device)
                self.pcd_frame = o3d.t.geometry.PointCloud.create_from_rgbd_image(
                    self.rgbd_frame, self.intrinsic_matrix, self.extrinsics,
                    self.rgbd_metadata.depth_scale, self.depth_max,
                    self.pcd_stride, self.flag_normals)
                depth_in_color = self.rgbd_frame.depth.colorize_depth(
                    self.rgbd_metadata.depth_scale, 0, self.depth_max)
            except RuntimeError:
                pcd_errors += 1

            if self.pcd_frame.is_empty():
                log.warning(f"No valid depth data in frame {frame_id})")
                continue

            n_pts += self.pcd_frame.point['points'].shape[0]
            if frame_id % 60 == 0:
                t0, t1 = t1, time.perf_counter()
                self.status_message = f"FPS: {60./(t1-t0):0.2f}"
                log.debug(
                    f"\nframe_id = {frame_id}, \t {(t1-t0)*1000./60:0.2f}"
                    f"ms/frame \t {(t1-t0)*1e9/n_pts} ms/Mp\t")
                n_pts = 0
            frame_elements = {
                'color': self.rgbd_frame.color,
                'depth': depth_in_color,
                'pcd': self.pcd_frame,
                'status_message': self.status_message
            }
            self.update_view(frame_elements)

            if self.flag_save_rgbd:
                self.save_rgbd()
                self.flag_save_rgbd = False
            self.rgbd_frame = future_rgbd_frame.result()
            with self.cv_capture:  # Wait for capture to be enabled
                self.cv_capture.wait_for(
                    predicate=lambda: self.flag_capture or self.flag_exit)
            self.toggle_record()
            frame_id += 1

        if self.camera:
            self.camera.stop_capture()
        else:
            self.video.close()
        self.executor.shutdown()
        log.debug(f"create_from_depth_image() errors = {pcd_errors}")
Esempio n. 50
0
                       epochs=5000,
                       validation_data=(X_test, y_test))

    plt.plot(hist.history['loss'], 'red', label='loss')
    plt.plot(hist.history['val_loss'], 'blue', label='val_loss')
    plt.show()

    y_predict = dlModel.predict(X_val)
    plt.plot(y_val.ravel()[-2200:], 'red')
    plt.plot(y_predict.ravel()[-2200:], 'green')
    plt.show()

    return dlModel


starttime = time.perf_counter()
pvf_predictor = train_predictor(102800)
endtime = time.perf_counter() - starttime
print('training time', endtime)
# valdf = load_data(0, 'susan_sbaseline')
#
# # instnum = np.array(data['INSTNUM'])[0: totallength].reshape(-1, 1)
# testpair = define_dpairs(valdf, 7000)
# # print(testpair)
#
# testx = testpair['features'][200 * 7000:]
# testy = testpair['target'][200 * 7000:]
# testx = np.array(np.split(testx, 200, axis=0))
# testy = np.array(np.split(testy, 200, axis=0))
#
# vf_predict = pvf_predictor.predict(testx)
Esempio n. 51
0
 def start_time(self):
     self.start = time.perf_counter()
     self.estimator()
Esempio n. 52
0
                                      bins=histBins,
                                      range=(-padding, padding))
        blHist = blHist / max(blHist) * max(hist)

        # determine the phasic current by subtracting-out the baseline
        diff = hist - blHist
        return diff / abf.pointsPerSec  # charge/sec


if __name__ == "__main__":
    #abfPath=r"X:\Data\2P01\2016\2016-09-01 PIR TGOT"
    abfPath = r"C:\Users\scott\Documents\important\demodata"
    #abf=ABF2(os.path.join(abfPath,"16d14036.abf"))
    abf = ABF2(os.path.join(abfPath, "16d16007.abf"))

    t = time.perf_counter()
    Xs = np.arange(abf.sweeps) * abf.sweepLength
    pos, neg = np.zeros(len(Xs)), np.zeros(len(Xs))
    for sweep in abf.setsweeps():
        phasic = abf.phasicTonic(.5)
        neg[sweep], pos[sweep] = np.sum(np.split(phasic, 2), 1)
    t = time.perf_counter() - t

    plt.figure(figsize=(10, 5))
    plt.grid()
    plt.title("analysis of %s completed in %.02f S" % (abf.ID, t))
    plt.plot(Xs, pos, '.', color='b', alpha=.3)
    plt.plot(Xs,
             swhlab.common.lowpass(pos),
             '-',
             color='b',
Esempio n. 53
0
def basic_controls():
    global lastEnemy
    box.movement()

    if box.currHealth <= 0:
        box.alive = False

    for bullet in bullets:
        if bullet.x_pos < window_width and bullet.x_pos > 0:
            bullet.x_pos += bullet.vel
        else:  # bullet is off screen kill it
            bullets.pop(bullets.index(bullet))

    if (keys[pygame.K_DOWN] or keys[pygame.K_SPACE] or keys[pygame.K_s]) and not box.reloading and box.alive:
        sounds.gunShoot.play()
        if box.direction > 0:
            bullets.append(
                projectile(round(box.x_pos + box.width), round(box.y_pos + (box.height // 2)), box.direction))
        else:
            bullets.append(projectile(round(box.x_pos), round(box.y_pos + (box.height // 2)), box.direction))
        box.curr_ammo -= 1

    if not box.curr_ammo > 0:
        box.lastReloadTime = time.perf_counter()
        box.reloading = True
        box.curr_ammo = box.max_ammo

    if box.reloading and time.perf_counter() - box.lastReloadTime > 2:
        box.reloading = False
        sounds.reloaded = False

    if box.reloading and time.perf_counter() - box.lastReloadTime > 0.5:
        if not sounds.reloaded:
            sounds.gunReload.play()
            sounds.reloaded = True

    if box.immune:
        if box.hurtTime % 2 == 0:
            box.color = 255, 0, 0
            box.visible = True
        else:
            box.visible = False
    else:
        box.color = 255, 100, 100
        box.visible = True

    for beatBox in enemies:
        if beatBox.immune:
            if beatBox.hurtTime % 2 == 0:
                beatBox.color = 255, 0, 0
                beatBox.visible = True
            else:
                beatBox.visible = False
        else:
            beatBox.color = 84, 197, 113
            beatBox.visible = True

        if beatBox.currHealth <= 0:
            sounds.enemyDeath.play()
            box.score += 1
            enemies.pop(enemies.index(beatBox))

    if box.alive:
        if len(enemies) < box.score // 5 + 1:
            spawnEnemy()
            lastEnemy = time.perf_counter()
        elif len(enemies) <= box.score * 10:
            if time.perf_counter() - lastEnemy > random.randint(30, 60):
                spawnEnemy()
                lastEnemy = time.perf_counter()
Esempio n. 54
0
# -*- coding: utf-8 -*-
"""
Created on Tue Apr 28 18:08:25 2020

@author: Damara
"""
# In 1
import time 

start = time.perf_counter()


def do_something():
    print('Sleeping 1 second....')
    time.sleep(1)
    print('Done sleeping...')
    
    
do_something()


finish = time.perf_counter()


print(f'Finished in {round(finish-start, 2)} second(s)')

# In 2

import time 

start = time.perf_counter()
Esempio n. 55
0
 def now(self):
     return time.perf_counter()
Esempio n. 56
0
def main():
    args = parse_args()

    cfg = Config.fromfile(args.config)
    # set cudnn_benchmark
    if cfg.get('cudnn_benchmark', False):
        torch.backends.cudnn.benchmark = True
    cfg.model.backbone.pretrained = None
    cfg.data.test.test_mode = True

    # build the dataloader
    dataset = build_dataset(cfg.data.test, dict(test_mode=True))
    data_loader = build_dataloader(dataset,
                                   videos_per_gpu=1,
                                   workers_per_gpu=cfg.data.workers_per_gpu,
                                   persistent_workers=cfg.data.get(
                                       'persistent_workers', False),
                                   dist=False,
                                   shuffle=False)

    # build the model and load checkpoint
    model = build_model(cfg.model,
                        train_cfg=None,
                        test_cfg=cfg.get('test_cfg'))
    fp16_cfg = cfg.get('fp16', None)
    if fp16_cfg is not None:
        wrap_fp16_model(model)
    if args.fuse_conv_bn:
        model = fuse_conv_bn(model)

    model = MMDataParallel(model, device_ids=[0])

    model.eval()

    # the first several iterations may be very slow so skip them
    num_warmup = 5
    pure_inf_time = 0

    # benchmark with 2000 video and take the average
    for i, data in enumerate(data_loader):

        torch.cuda.synchronize()
        start_time = time.perf_counter()

        with torch.no_grad():
            model(return_loss=False, **data)

        torch.cuda.synchronize()
        elapsed = time.perf_counter() - start_time

        if i >= num_warmup:
            pure_inf_time += elapsed
            if (i + 1) % args.log_interval == 0:
                fps = (i + 1 - num_warmup) / pure_inf_time
                print(
                    f'Done video [{i + 1:<3}/ 2000], fps: {fps:.1f} video / s')

        if (i + 1) == 200:
            pure_inf_time += elapsed
            fps = (i + 1 - num_warmup) / pure_inf_time
            print(f'Overall fps: {fps:.1f} video / s')
            break
Esempio n. 57
0
 def _before_epoch(self) -> None:
     self.start_time = time.perf_counter()
Esempio n. 58
0
            if end - x > 200 and abs(box.x_pos - x) > 100:
                goodCoordinates = True
        else:
            x = random.randint(0, window_width - w)
            end = window_width - 50

            if abs(box.x_pos - x) > 100:
                goodCoordinates = True
    global enemies
    enemies.append(enemy(x, 450 - h, end, w, h, hel, aitype))

box = player(50, 400, 20, 50, 10)
bullets = []
font = pygame.font.Font("font.ttf", 40)
enemies = [enemy(350, 400, 700, 25, 50, 2, 1)]
lastEnemy = time.perf_counter()
pygame.mixer.music.play(-1)
clock = pygame.time.Clock()
high_score  = int(open('save.dat','r').read())

# main program
run = True
while run:
    pygame.time.delay(100)
    keys = pygame.key.get_pressed()

    for event in pygame.event.get():

        # Quit
        if event.type == pygame.QUIT:
            print("See You Later")
Esempio n. 59
0
from sklearn.metrics import precision_score
from sklearn.metrics import r2_score
from sklearn.metrics import mean_squared_log_error
from sklearn.metrics import accuracy_score
from sklearn.model_selection import KFold
from sklearn.model_selection import cross_val_score
from sklearn.metrics import confusion_matrix, classification_report

import datetime


def calculateMeanSquareError(known, pred):
    return mean_squared_log_error(known, pred)


tStart = time.perf_counter()

seenMovie = pd.read_csv("./seenMovieLikelihood.csv")
metadata = pd.read_csv("./metadataLikelihood.csv")
# print(seenMovie)
# print(metadata)
seenMovie.drop(seenMovie.columns[[0]], axis=1, inplace=True)
metadata.drop(metadata.columns[[0]], axis=1, inplace=True)

print(seenMovie)
print(metadata)
print("Data loaded")
print(seenMovie.shape, '\t', metadata.shape)
seenMovie = seenMovie.astype('int')
# split train and test set
X_train, X_test, y_train, y_test = train_test_split(metadata,
Esempio n. 60
0
 def _after_epoch(self) -> None:
     self.end_time = time.perf_counter()