def main(): runtime = Runtime(dataset=dataset, hidden_channels=Config.hidden_channels) for epoch in range(Config.epochs): loss = runtime.train() print(f'Epoch: {epoch:03d}, Loss: {loss:.4f}') test_acc = runtime.test() print(f'Test Accuracy: {test_acc:.4f}')
def main(argv): signal.signal(signal.SIGINT, signal_handler) Runtime.init_runtime() syslog = Runtime.syslog # Read configuration config = Runtime.read_config() if config == None: # error reading configuration file syslog("(FATAL) Error reading configuration file. Exiting.") return syslog("Configuration: %s" % json.dumps(config)) # Connect to GPS if 'gps' in config: gps = GPS(syslog) ret = gps.connect(config['gps']['serial'], config['gps']['speed']) if ret: syslog("Connected to GPS.") else: gps = None syslog("Error connecting to GPS on % s. Disabling." % config['gps']['serial']) # Connect the device to Cometa cometa_server = config['cometa']['server'] cometa_port = config['cometa']['port'] application_id = config['cometa']['app_key'] # use the machine's MAC address as Cometa device ID device_id = Runtime.get_serial() config['serial'] = device_id # Instantiate a Cometa object com = CometaClient(cometa_server, cometa_port, application_id, config['cometa']['ssl'], syslog) com.debug = config['app_params']['debug'] # bind the message_handler() callback com.bind_cb(api.message_handler) # Attach the device to Cometa ret = com.attach(device_id, "Autonomia") if com.error != 0: print "(FATAL) Error in attaching to Cometa.", com.perror() sys.exit(2) # Get the timestamp from the server try: ret_obj = json.loads(ret) except Exception, e: print "(FATAL) Error in parsing the message returned after attaching to Cometa. Message:", ret sys.exit(2)
def execute(self, runtime: Runtime, caller: Node = None): self.caller = caller if runtime.has(self.subject): subject = runtime.get(self.subject) args = self.prepare_args(runtime) if callable(subject): return subject(runtime, *args, caller=self) elif hasattr(subject, 'execute'): return subject.execute(runtime, *args, caller=self) else: return subject else: raise NameResolutionError(self.subject, node=self)
def _video_devices(params): """List available video devices (v4l).""" vdevices = Runtime.list_camera_devices() ret = {} ret['devices'] = vdevices[0] ret['names'] = vdevices[1] return ret
def handle_issue(pid, pat, repo_path): if not pid or not pat: return repo = Repo(repo_path) if not repo or repo.bare: return runtime = Runtime(*[argv[i] for i in range(1, 5)]) runtime.set_branch(repo.head.ref.name) # Check if there's already an issue for the runtime list_issues_url = LIST_ISSUES_URL.format(pid, runtime.get_title()) response = requests.get(list_issues_url, headers={ "Private-Token": pat, "Content-Type": "application/json" }) # There is an issue already, so just re-open it if necessary if response.status_code == 200: issues = response.json() if issues: # May actually be multiple of them # Make sure there isn't an open issue for the runtime already. for issue in issues: if issue.get("state") == "opened": return # They were all closed, so re-open the first issue GitLab handed us issue = issues[0] iid = issue.get("iid") reopen_url = REOPEN_ISSUE_URL.format(pid, iid) response = requests.put(reopen_url, headers={"Private-Token": pat}) # Issue was successfully re-opened # Leave a comment to know that it was re-opened because the runtime re-occured if response.status_code == 200: comment = "The issue has been re-opened due to the runtime re-occuring. Details are provided below.\n\n{}".format( runtime.get_body()) comment_url = COMMENT_ISSUE_URL.format(pid, iid, comment) requests.post(comment_url, headers={"Private-Token": pat}) return # At this point we know that there was no issue already, so open a new one open_url = OPEN_ISSUE_URL.format(pid, runtime.get_title(), runtime.get_body()) requests.post(open_url, headers={ "Private-Token": pat, "Content-Type": "application/json" })
def init(self, intro): self.runtime = Runtime() self.intro = intro.strip() self.intro_code = press_lang.parse(self.intro, actions=press_lang_ast.Actions) self.intro_code.file = os.path.abspath(self.file) self.intro_code.text = self.intro self.intro_code.set_parent(None) self.intro_code.execute(self.runtime)
def __init__(self, *, hexfiles: Optional[List[str]] = None) -> None: self.runtime = Runtime() self.sm = SessionManager() self.log = LoggingEventHub(ColoramaStream(sys.stdout), sm=self.sm) self.medium = SimpleMedium(evhub=self.log) self.gateway = UniversalGateway(self.runtime, self.medium) self.session: Optional[Session] = None self.sim = Simulation(self.runtime, context={ 'evhub': self.log, 'medium': self.medium }) if hexfiles is None: hexfiles = shlex.split(os.environ.get('TEST_HEXFILES', '')) for hf in hexfiles: self.sim.load_hexfile(hf)
def main(): import sys from runtime import Runtime file = sys.argv[1] code = open(file).read() asm = Program() runtime = Runtime() context = Context(Context.GLOBAL) compiler = Compiler(asm, runtime, context) ast = parse(code) fptr = compiler.compile(ast) import time start = time.clock() fptr() print time.clock() - start
'pypy_conn_p_center': 1.0, 'pypy_conn_p_sigma': 2.0, 'pypy_conn_weight_center': 1.5, 'pypy_conn_weight_sigma': 1.0, 'sample_size': 400, 'seed': 10 } # Specify independent variables and corresponding test ranges var_dict = { 'pyr_conn_p_center': [0.1 * (i + 1) for i in range(10)], 'pyr_conn_p_sigma': [0.1 * (i + 1) for i in range(20)], 'pyr_conn_weight_center': [0.1 * (i + 1) for i in range(20)], 'pyr_conn_weight_sigma': [0.1 * (i + 1) for i in range(20)] } rt = Runtime(var_dict, args['freq_num'], 'double') # Run simulations for each specified variable for var_pair in combinations(var_dict.keys(), 2): args_defaults = (args[var_pair[0]], args[var_pair[1]]) test_ranges = (var_dict[var_pair[0]], var_dict[var_pair[1]]) tot_trials = len(test_ranges[0]) * len(test_ranges[1]) x_vals = [] y_vals = [] z_vals_all = [{} for i in range(tot_trials)] sem_vals_all = [{} for i in range(tot_trials)] for x in test_ranges[0]: for y in test_ranges[1]:
def setup_game(switch_order=False): algorithm_0 = random_start_systematic_max_shade_factory() algorithm_1 = systematic_max_shade_factory() rt = Runtime(FreeForAllGame, algorithm_0, algorithm_1, print_moves=True) return rt
def generate_average(job_id): runtime = Runtime(os.path.join(ROOT_DIR, f"{job_id}/")) runtime.run_averager()
def project_images(job_id): runtime = Runtime(os.path.join(ROOT_DIR, f"{job_id}/")) runtime.run_projector()
def align_images(job_id): runtime = Runtime(os.path.join(ROOT_DIR, f"{job_id}/")) runtime.run_aligner()
'pyr_conn_p_center': [0.1 * (i + 1) for i in range(10)], 'pyr_conn_p_sigma': [0.1 * (i + 1) for i in range(20)], 'pyr_conn_weight_center': [0.1 * (i + 1) for i in range(20)], 'pyr_conn_weight_sigma': [0.1 * (i + 1) for i in range(20)], 'inh_conn_rad': [0.1 * (i + 1) for i in range(10)], 'inh_conn_p_center': [0.1 * (i + 1) for i in range(10)], 'inh_conn_p_sigma': [0.1 * (i + 1) for i in range(20)], 'inh_conn_weight_center': [0.1 * (i + 1) for i in range(20)], 'inh_conn_weight_sigma': [0.1 * (i + 1) for i in range(20)], 'pypy_conn_rad': [0.1 * (i + 1) for i in range(10)], 'pypy_conn_p_center': [0.1 * (i + 1) for i in range(10)], 'pypy_conn_p_sigma': [0.1 * (i + 1) for i in range(20)], 'pypy_conn_weight_center': [0.1 * (i + 1) for i in range(20)], 'pypy_conn_weight_sigma': [0.1 * (i + 1) for i in range(20)] } rt = Runtime(var_dict, args['freq_num']) # Run simulations for each specified variable for variable in var_dict.keys(): args_default = args[variable] test_range = var_dict[variable] ########################################### #### SIMULATION ####################### ########################################### firing_rates = [] for i in test_range: args[variable] = i var_frs = test.main(args, rt, test_range.index(i), variable) firing_rates.append(var_frs)
from runtime import Runtime import sys import pdb runt = Runtime() # receive filename from a user pdb.run("runt.top(sys.argv[1])")
def _get_config(params): """ Get configuration object """ config = Runtime.read_config() config['app_params']['verbose'] = car.verbose config['app_params']['telemetry_period'] = car.telemetry_period return config
def test_parallel_exec(self): ret = Runtime._parallel_exec(lambda x: x * 2, xrange(5), n_threads=20) self.assertEqual(ret.get(), [0, 2, 4, 6, 8])
# main program if this file get executed if __name__ == '__main__': logging.basicConfig( level=logging.DEBUG, format= '%(asctime)-15s %(threadName)-15s %(levelname)-8s %(module)-15s:%(lineno)-8s %(message)s' ) try: robot_type = argv[1] except IndexError: logging.exception("Need to supply robot type as command-line argument") raise app = Runtime(robot_type) exit_code = None try: app_initialized = True app.loop() except KeyboardInterrupt: # shutdown python program gently logging.exception("Stopped with KeyboardInterrupt!") except SystemExit as e: # shutdown via button logging.exception("Stopped via button press") app.program_stopped.set() app.lcd.print_status('Shutting down...') exit_code = e.code except Exception as e:
from sensors import Sensor from sensors import Sensors from wheels import Wheel from wheels import MockWheel from wheels import Wheels import RPi.GPIO as GPIO from runtime import Runtime from processor import Processor GPIO.setmode(GPIO.BOARD) sensor1 = Sensor(11, 13) sensor3 = Sensor(15, 16) sensor5 = Sensor(18, 19) sensor6 = Sensor(21, 22) sensor8 = Sensor(23, 24) sensor10 = Sensor(26, 29) leftMotor = Wheel(5, 3, 7, 7) rightMotor = Wheel(8, 10, 12, 1) # rightMotor = MockWheel("rightWheel") zrobot_runtime = Runtime( Sensors([sensor1, sensor3, sensor5, sensor6, sensor8, sensor10]), Processor(), Wheels(leftMotor, rightMotor)) zrobot_runtime.start()
#!/usr/bin/env python """ Script to send a GET request to a WordStream endpoint """ import inspect import sys import os # Build up the path to our code so that we can import it. this_file_path = inspect.getfile(inspect.currentframe()) this_file_home = os.path.dirname(this_file_path) project_home = os.path.join(this_file_home, '..') project_home = os.path.abspath(project_home) sys.path.append(project_home) from runtime import Runtime from fileage import command if __name__ == "__main__": exit_status = Runtime.run_command(command.Watch, project_home, os.getcwd()) exit(exit_status)
def make_runtime(self): """ this method must be called after values are set """ self.runtime = Runtime(self.values)
def generate_kubernetes_config(): config = request.get_json(silent=True, force=True) runtime = Runtime(config, cluster_config) res = make_response(runtime.GenerateKubernetesFile()) res.mimetype = "text/plain" return res
def run(runtime_provider, source_directory, scripts_directory, pipeline_controller=None, working_directory=None, output_directory=None, no_exit=False): # Build a file system file_system = FileSystem(source_directory, working_directory=working_directory, scripts_directory=scripts_directory, output_directory=output_directory) # Core engine = Engine(file_system) engine.info("Logging setup complete") # Build the runtime environment engine.info("Beginning setup of the runtime environment") runtime = Runtime(engine) engine.info("Runtime setup complete") # Load the appropriate resource properties # TODO: What if we want to be able to do both? :D engine.info("Loading resource properties") if not runtime.set_runtime_provider(runtime_provider, file_system): engine.error("Failed to setup the runtime provider.") return False # Evaluate pipeline controller if not pipeline_controller or not os.path.exists(pipeline_controller): pipeline_controller = os.path.join(file_system.get_scripts_directory(), "controller.py") if not os.path.exists(pipeline_controller): engine.error( "No valid path to a pipeline controller script was provided. Looked for: " + pipeline_controller + ". Exiting...") sys.exit(1) # Execute the pipeline - this is a delegate script call to allow pipelines to be provided dynamically engine.info("Beginning pipeline execution") try: version = sys.version_info if version >= (3, 5): import importlib.util spec = importlib.util.spec_from_file_location( "pipeline", pipeline_controller) pipeline = importlib.util.module_from_spec(spec) spec.loader.exec_module(pipeline) elif version >= (3, 3): from importlib.machinery import SourceFileLoader pipeline = SourceFileLoader("pipeline", pipeline_controller).load_module() else: engine.error("Python 2 is currently not supported") return # Make a blind function call - we assume that the loaded script conforms to our standards. If not, we catch it engine.info("Attempting engine injection") if pipeline.execute_pipeline(engine): file_system.cleanup_monitors() if no_exit: engine.debug("Returning positive outcome: True") return True else: engine.debug("Returning positive outcome: 0") sys.exit(0) else: file_system.cleanup_monitors() if no_exit: engine.debug("Returning negative outcome: False") return False else: engine.debug("Returning negative outcome: 1") sys.exit(1) except Exception as ex: engine.error("Caught exception in pipeline delegate") engine.exception(ex) sys.exit(1)
class RuntimeCsp: def __init__(self): self.uin = {} # [key: variable name (str), value: list of values] self.uex = {} # [key: variable name (str), value: list of values] self.biconst = {} # binary constraint matrices: [key: two constraining variables, value: the matrix # with the first variable on rows and the second on columns] self.const_graph = Graph( ) # a constraint graph contains all the variables that are connected by binary constraints # the constraint graph is represented by a dictionary with [key: node, value: connections] # self.assignment = {} # represent the assignment of variabels [Key: Variable, Value: value (str)] self.values = [] self.value_costs = {} self.runtime = None self.budget = 0 self.threat_level = 0 self.attack_power = 0 self.rtcost = {} def __str__(self): return "Unary Inclusive: " + str(self.uin) + "\n" + \ "Unary Exclusive: " + str(self.uex) + "\n" + \ "Binary Constraint: " + str(self.biconst.items()) + "\n" # "Binary Constraint: " + [(str(key) + ":\n" + str(value) + "\n") for key, value in self.biconst.items()] def set_rtcost_for_value(self, value, cost): """ The function also checks if the value is in the value list of this csp. If not, it will terminate the run Note that this function does not validate if all value has been assigned a value, to do so, call validate_rtcost :param value: :param cost: """ if value not in self.values: print("Invalid value in value cost: " + value + "; Killed") system.exit() self.rtcost[value] = cost def validate_rtcost(self): """ :returns true if each value in self.values has a rtcost """ for v in self.values: if v not in self.rtcost.keys(): return False return True def get_rtcost_for_value(self, value): return self.rtcost[value] def set_values(self, values): ''' :except Disused set values of all variable domain ''' self.values = values for var in self.get_all_variables(): for value in self.values: var.domain.append(value) def add_value_cost(self, val, cost): self.value_costs[val] = cost def set_budget(self, budget): self.budget = budget def get_budget(self): return self.budget def set_threat_level(self, level): self.threat_level = level def get_threat_level(self): return self.threat_level def set_attack_power(self, power): self.attack_power = power def get_attack_powere(self): return self.attack_power def make_runtime(self): """ this method must be called after values are set """ self.runtime = Runtime(self.values) def add_value(self, value): """ add a value to all variables' domain, and to the values field of the CSP object """ self.values.append(value) for var in self.get_all_variables(): var.domain.append(value) def get_values(self): return self.values def get_value_by_index(self, i): return self.values[i] def get_index_of_value(self, value): return self.values.index(value) def get_values_len(self): return len(self.values) def get_all_variables(self): return self.const_graph.get_all_vertices() def add_var_to_graph(self, var): self.const_graph.add_vertex(var) def add_uin(self, const_var, const_value): self.uin[const_var] = const_value def get_uin(self, var): if var.name in self.uin.keys(): return self.uin[var.name] def add_uex(self, const_var, const_value): self.uex[const_var] = const_value def get_uex(self, var): if var.name in self.uex.keys(): return self.uex[var.name] # TODO handle duplicate binary varible EXCEPTION # TODO should say NO ANSWER if a constraint matrix are all zeros def add_biconst(self, const_vars, equal): # constraint type """ Create a binary constraint matrix :param list const_vars: the variables to be constrained :param int equal: constraint type: 1 = binary equals, 0 = binary not equals """ # TODO what if two bi_const have the same constrainting variable # The first value in the tuple takes the rows, and the second takes the columns self.const_graph.add_edge(self.const_graph.get_vertex(const_vars[0]), self.const_graph.get_vertex(const_vars[1])) if tuple(const_vars) in self.biconst.keys(): print("duplicate binary constraint, killed") system.exit() else: if equal: const_matrix = self.biconst[tuple(const_vars)] = numpy.zeros( shape=(len(self.values), len(self.values)), dtype=int) else: const_matrix = self.biconst[tuple(const_vars)] = numpy.ones( shape=(len(self.values), len(self.values)), dtype=int) for i in range(len(self.values)): # modify the constraint matrix for j in range(len(self.values)): if self.values[i] == self.values[ j]: # if the values are the same value, they should be constrained const_matrix[i, j] = equal # 0 for not equal, 1 for equal self.consolidate_matrix() def add_bins(self, const_vars, const_values): """ Create a binary constraint matrix for NOT SIMULTANEOUS constraint :param list const_vars: the variables to be constrained :param list const_values: the values to be constrained :param dic const_values: list of values [key: index, value: variable value] """ # TODO what if two bi_const have the same constrainting variable # The first value in the tuple takes the rows, and the second takes the columns self.const_graph.add_edge(self.const_graph.get_vertex(const_vars[0]), self.const_graph.get_vertex(const_vars[1])) if tuple(const_vars) in self.biconst.keys(): print("duplicate binary constraint, killed") system.exit() else: const_matrix = self.biconst[tuple(const_vars)] = numpy.ones( shape=(len(self.values), len(self.values)), dtype=int) for i in range(len(self.values)): # modify the constraint matrix for j in range(len(self.values)): if (self.values[i], self.values[j]) == ( const_values[0], const_values[1] ): # or (self.values[i], self.values[j]) == (const_values[1], const_values[0]): const_matrix[i, j] = 0 self.consolidate_matrix() # TODO efficiency??? running this for loop everytime def consolidate_matrix(self): """ add unary constraint to all binary constraint matrices """ for const_vars, const_matrix in self.biconst.items(): for i in range(len(self.values)): # modify the constraint matrix for j in range(len(self.values)): for const_var in const_vars: if const_var in self.uex.keys( ): # the rows(indexed by i) corresponds to the zeroth const variable; the columns(indexed by j) corresponds to the first const variable if const_vars.index( const_var ) == 0 and self.values[i] in self.uex[const_var]: const_matrix[i, j] = 0 if const_vars.index( const_var ) == 1 and self.values[j] in self.uex[const_var]: const_matrix[i, j] = 0 if const_var in self.uin.keys(): if const_vars.index( const_var) == 0 and self.values[ i] not in self.uin[const_var]: const_matrix[i, j] = 0 if const_vars.index( const_var) == 1 and self.values[ j] not in self.uin[const_var]: const_matrix[i, j] = 0 # ''' # :return the constraint matrix between two variables # ''' # def get_biconst(self, var1, var2): # for key, value in self.biconst.items(): # # because we keep connection in both direction, we only need to check the constraint matrix in one direction # # if (var1.name == key[0] and var2.name == key[1]) or (var1.name == key[1] and var2.name == key[0]): # if var1.name == key[0] and var2.name == key[1]: # return value def get_biconst(self, var1, var2): """ :return the constraint matrix between two variables """ for key, value in self.biconst.items(): # because we keep connection in both direction, we only need to check the constraint matrix in one direction # if (var1.name == key[0] and var2.name == key[1]) or (var1.name == key[1] and var2.name == key[0]): if var1.name == key[0] and var2.name == key[1]: return value elif var1.name == key[1] and var2.name == key[0]: return value.transpose( ) # we transpose the matrix to match the variables' axises def get_arcs(self, var): """ :return a list of pair(list) of variables """ return self.const_graph.get_edges(var) def get_connecting_vars(self, var): return self.const_graph.get_connecting_vertices(var) def get_connecting_unassigned_vars(self, var, assignment): """ :param var: :param assignment: :return: a list of unassigned vars (with the given assignment) that are connected to :param var """ connecting_vars = self.const_graph.get_connecting_vertices(var) rtn = [] for var in connecting_vars: if assignment[var] is None: rtn.append(var) return rtn def get_all_arcs(self): return self.const_graph.get_all_edges() def print_all_variable(self): for var in self.const_graph.get_all_vertices(): print(var.name + ", Domain: " + str(var.domain)) def get_run_time(self, processor, assignment): """ get the time the processor needs to run its assignments :param str/value processor: :param dictionary [key = Variable, value = str/value] assignment: :return: the time the processor needs to run its assignments """ return self.runtime.generate_run_time(processor, assignment, self.get_all_variables()) def is_budget_met(self, assignment): return self.runtime.get_spending(assignment, self.value_costs) <= self.budget def print_process_time(self, assignment): print("Process Time for Each Processor:") for processor in self.get_values(): print("Processor " + processor + ": " + str(self.get_run_time(processor, assignment))) def print_total_run_time(self, assignment): ordered_processor = self.get_values() ordered_processor.sort(key=lambda x: self.get_run_time(x, assignment), reverse=True) print("The total run time is: " + str(self.get_run_time(ordered_processor[0], assignment)) + ", by processor " + ordered_processor[0])