示例#1
0
def HD44780_writer(inputs, out_q, nskip, debug):
    logger = Logger(out_q)

    try:
        logger.log(__name__ + ' started')

        display = HD44780()

        logger.log(__name__ + ' entering main loop')

        while True:
            for i in inputs:
                in_msg = i['queue'].get()
                if debug:
                    s = '{0} {1}'.format(in_msg.tag, in_msg.data)
                else:
                    s = '{0}: {1:7.1f} {2}'.format(i['label'], in_msg.data,
                                                   i['unit'])
                display.print(s, i['line'], AlignMode.LEFT)

            for n in range(nskip):
                for i in inputs:
                    i['queue'].get()
    except:
        logger.log(format_current_exception(__name__))
示例#2
0
def wrap_up(args):

    cloudFS = GCStorage.get_CloudFS(PROJECT_ID, GC_BUCKET, CREDENTIAL_PATH)

    us_timezone = timezone('US/Pacific')
    date = datetime.datetime.now(us_timezone).strftime("%Y-%m-%d")
    save_dir = Path(EXP_STORAGE) / date

    args.exp_name = getpass.getuser() + '_' + socket.gethostname() + \
                    '_KGAT_' + args.exp_name + '_' + args.dataset
    exp_num = get_experiment_number(save_dir, args.exp_name)

    args.exp_name = args.exp_name + '_' + str(exp_num)
    save_dir = save_dir / args.exp_name
    log_file = save_dir / 'run_log.txt'

    arg_dict = namespace_to_dict(args)
    arg_dict_text = pp.pformat(arg_dict, indent=4)

    arg_text = ' '.join(sys.argv)

    args.logger = Logger(log_file, save_dir)

    args.logger.log_text(
        {
            'setup:command_line': arg_text,
            'setup:parsed_arguments': arg_dict_text
        }, 0, False)

    return args
示例#3
0
def derivative_calculator(in_q, out_q, out_topic, nskip):
    logger = Logger(out_q)

    try:
        logger.log(__name__ + ' started')

        result = 0.0
        in_msg = in_q.get()
        last_value, last_t = in_msg.data, time()

        logger.log(__name__ + ' entering main loop')

        while True:
            in_msg = in_q.get()
            new_value, new_t = in_msg.data, time()
            result = (new_value - last_value) / (new_t - last_t)
            last_value, last_t = new_value, new_t
            out_msg = Message(out_topic, round(result, 2))
            out_q.put(out_msg)

            for n in range(nskip):
                in_q.get()
                out_q.put(out_msg)
    except:
        logger.log(format_current_exception(__name__))
示例#4
0
文件: model.py 项目: xlnwel/meta
    def _setup_logger(self, root_dir, model_name):
        log_dir = os.path.join(root_dir, model_name)

        logger = Logger(log_dir, exp_name=model_name)
        save_args(self.args, filename=log_dir + '/args.yaml')

        return logger
示例#5
0
def linreg_calculator(in_q, out_q, out_topic, buffer_size):
    logger = Logger(out_q)

    try:
        logger.log(__name__ + ' started')

        X = np.zeros(shape=(buffer_size, 1), dtype=np.double)
        y = np.zeros(shape=(buffer_size, 1), dtype=np.double)
        linregr = LinearRegression()
        for i in range(buffer_size):
            in_msg = in_q.get()
            X[i, 0] = in_msg.timestamp
            y[i, 0] = in_msg.data

        logger.log(__name__ + ' entering main loop')

        while True:
            X = np.roll(X, -1)
            y = np.roll(y, -1)
            in_msg = in_q.get()
            X[buffer_size - 1, 0] = in_msg.timestamp
            y[buffer_size - 1, 0] = in_msg.data
            linregr.fit(X, y)
            out_msg = Message(out_topic, round(linregr.coef_[0, 0], 2))
            out_q.put(out_msg)
    except:
        logger.log(format_current_exception(__name__))
示例#6
0
def bluetooth_server(inputs, out_q, nskip):
    logger = Logger(out_q)

    try:
        logger.log(__name__ + ' started')

        data_types = [
            'pressure', 'altitude', 'vspeed', 'temperature', 'battery'
        ]

        data_converters = {'vspeed': 100}

        unavailable_data = {
            'pressure': '999999',
            'altitude': '99999',
            'vspeed': '9999',
            'temperature': '99',
            'battery': '999'
        }

        server_socket = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
        server_socket.bind(('', bluetooth.PORT_ANY))
        server_socket.listen(1)

        while True:
            logger.log(__name__ + ' started')

            print('waiting bluetooth connection...')
            client_socket, client_address = server_socket.accept()
            print('connected')
            for i in inputs:
                while not inputs[i].empty():
                    inputs[i].get_nowait()

            logger.log(__name__ + ' entering main loop')

            while True:
                try:
                    message = 'LK8EX1'
                    for t in data_types:
                        message += ','
                        if t in inputs:
                            value = inputs[t].get().data
                            value = value * data_converters.get(t, 1)
                            message += '{0:.2f}'.format(value)
                        else:
                            message += unavailable_data[t]
                    message = '$' + message + ',*' + checksum(message)
                    client_socket.sendall(message)

                    for n in range(nskip):
                        for i in inputs:
                            inputs[i].get()
                except bluetooth.BluetoothError:
                    break
    except:
        logger.log(format_current_exception(__name__))
示例#7
0
def console_writer(inputs, out_q):
    logger = Logger(out_q)

    try:
        logger.log(__name__ + ' started')
        logger.log(__name__ + ' entering main loop')

        while True:
            for q in inputs:
                print(q.get())
    except:
        logger.log(format_current_exception(__name__))
示例#8
0
def file_reader(out_q, out_topic, filename, sep, field, interval):
    logger = Logger(out_q)

    try:
        logger.log(__name__ + ' started')

        with open(filename, 'r') as f:
            logger.log(__name__ + ' entering main loop')

            while True:
                for line in f:
                    tokens = line.split(sep)
                    out_msg = Message(out_topic, float(tokens[field]))
                    out_q.put(out_msg)
                    sleep(interval)
    except:
        logger.log(format_current_exception(__name__))
示例#9
0
def MPL3115A2_reader(initial_altitude, out_q, out_topic, interval):
    logger = Logger(out_q)

    try:
        logger.log(__name__ + ' started')

        sensor = MPL3115A2()
        sensor.set_altitude(initial_altitude)

        logger.log(__name__ + ' entering main loop')

        while True:
            altitude = sensor.read_altitude()
            msg = Message(out_topic, altitude)
            out_q.put(msg)

            sleep(interval)
    except:
        logger.log(format_current_exception(__name__))
示例#10
0
def data_log_writer(inputs, out_q):
    logger = Logger(out_q)

    try:
        logger.log(__name__ + ' started')

        for i in inputs:
            with open(i['file'], 'w'):
                pass

        logger.log(__name__ + ' entering main loop')

        while True:
            for i in inputs:
                in_msg = i['queue'].get()
                with open(i['file'], 'a') as f:
                    f.write('{0},{1},{2}\n'.format(in_msg.timestamp,
                                                   in_msg.data, in_msg.tag))
    except:
        logger.log(format_current_exception(__name__))
示例#11
0
def pubsub_manager(in_q, topics, out_q):
    logger = Logger(out_q)

    try:
        logger.log(__name__ + ' started')

        indexes = {}
        for t in topics:
            indexes[t] = 0

        logger.log(__name__ + ' entering main loop')

        while True:
            in_msg = in_q.get()
            t, data, timestamp = in_msg.topic, in_msg.data, in_msg.timestamp
            if t in topics:
                tag = '{0}.{1}'.format(topics[t].tag, indexes[t])
                out_msg = Message(t, data, tag, timestamp)
                indexes[t] += 1
                for q in topics[t].subscriptions:
                    q.put(out_msg)
    except:
        logger.log(format_current_exception(__name__))
示例#12
0
def network_writer(address, inputs, out_q):
    logger = Logger(out_q)

    try:
        logger.log(__name__ + ' started')

        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
            s.connect(address)

            logger.log(__name__ + ' entering main loop')

            while True:
                for label, queue in inputs:
                    in_msg = queue.get()
                    out_msg = {
                        'label': label,
                        'value': in_msg.data,
                        'tag': in_msg.tag
                    }
                    json_msg = json.dumps(out_msg)
                    s.send(json_msg.encode())
                    ack = s.recv(SOCKET_BUFFER_SIZE)
    except:
        logger.log(format_current_exception(__name__))
示例#13
0
        p.terminate()
        p.join()
    print('Exiting')
    sys.exit(0)


if __name__ == '__main__':
    try:
        mp.set_start_method('spawn')

        signal.signal(signal.SIGINT, signal.SIG_IGN)

        pubsub_queue = mp.Queue()
        topics = {}

        logger = Logger(pubsub_queue)
        logger.log("---> RASPBERRY_FLY STARTING...")

        logs_queue = pubsub_queue
        logs_topics = LOGS_TOPIC

        rwalt_topic = Topic(tag='RA')
        fltalt_topic = Topic(tag='FA')
        rwvs_topic = Topic(tag='RV')
        fltvs_topic = Topic(tag='FV')
        logs_topic = Topic(tag='LOGS')

        if SIMULATION:
            rwalt_reader = mp.Process(target=file_reader,
                                      args=(pubsub_queue, RWALT_TOPIC,
                                            RWALT_SIMULATION_FILE,
示例#14
0
class BasePage:
    def __init__(self, driver):
        self.driver = driver

    logs = Logger()

    def check_element_is_on_page(self, locator):
        self.logs.info(self.logs.locator_check_message(locator))
        try:
            WebDriverWait(self.driver, tc.wait_time).until(
                ec.presence_of_element_located(locator))
            self.logs.info(self.logs.locator_found_message(locator))
            return True
        except (NoSuchElementException, TimeoutException):
            self.logs.error(self.logs.locator_not_found(locator))
            raise

    def find_and_click_element(self, locator):
        self.logs.info(self.logs.locator_check_message(locator))
        try:
            element = WebDriverWait(self.driver, tc.wait_time).until(
                ec.element_to_be_clickable(locator))
            self.logs.info(self.logs.clicking_locator(locator))
            element.click()
            return True
        except (NoSuchElementException, TimeoutException,
                ElementNotInteractableException) as e:
            self.logs.error(self.logs.locator_not_found(locator))
            self.logs.error(self.logs.error(e))
            raise

    def scroll_to_element(self, locator):
        try:
            element = WebDriverWait(self.driver, tc.wait_time).until(
                ec.presence_of_element_located(locator))
            self.driver.execute_script("arguments[0].scrollIntoView();",
                                       element)
            return True
        except (NoSuchElementException, TimeoutException):
            self.logs.error(self.logs.locator_not_found(locator))
            raise

    def hover_element(self, locator):
        try:
            element = WebDriverWait(self.driver, tc.wait_time).until(
                ec.presence_of_element_located(locator))
            hover = ActionChains(self.driver).move_to_element(element)
            hover.perform()
            return True
        except (NoSuchElementException, TimeoutException):
            self.logs.error(self.logs.locator_not_found(locator))
            raise

    def set_text(self, locator, text):
        try:
            element = WebDriverWait(self.driver, tc.wait_time).until(
                ec.presence_of_element_located(locator))
            self.logs.info(self.logs.sending_text_to_element(text, locator))
            element.send_keys(text)
            return True
        except (NoSuchElementException, TimeoutException):
            self.logs.error(self.logs.locator_not_found(locator))
            raise

    def get_text(self, locator):
        self.logs.info(self.logs.getting_text_from_element(locator))
        try:
            element = WebDriverWait(self.driver, tc.wait_time).until(
                ec.presence_of_element_located(locator))
            self.logs.info(self.logs.text_of_locator(locator, element))
            return element.text
        except (NoSuchElementException, TimeoutException):
            self.logs.error(self.logs.locator_not_found(locator))
            raise
示例#15
0
                             batch_size=1,
                             shuffle=False,
                             num_workers=args.threads)

    # Create the model
    network = MultiTasNet(args).to(device)

    # Check the model
    network.compute_stats(train_loader)
    optimizer = Ranger(filter(lambda p: p.requires_grad, network.parameters()),
                       weight_decay=args.weight_decay)
    decay = SGDRLearningRate(optimizer,
                             args.learning_rate,
                             t_0=args.sgdr_period,
                             mul=0.85)
    logger = Logger()

    # Optionally load from a checkpoint
    if args.checkpoint is not None:
        state = torch.load(f"{args.directory}/{args.checkpoint}")
        optimizer.load_state_dict(state['optimizer'])
        network.load_state_dict(state['state_dict'])
        initial_epoch = state['epoch'] + 1
        steps = state['steps']
    else:
        initial_epoch, steps = 0, 0

    # Optionally distribute the model across more GPUs
    raw_network = network
    if torch.cuda.device_count() > 1:
        print("Let's use", torch.cuda.device_count(), "GPUs!")
示例#16
0
class Main(metaclass=Singleton):
    logger = Logger()

    def __init__(self):
        # Load Configuration
        self.utility_config = utility.utility_config
        self.controller_config = controller.controller_config
        self.sensor_config = sensor.sensor_config
        self.pilot_config = pilot.pilot_config
        # Initialize objects
        """
        We can initialize objects from each active module based on configuration files
        """
        # Initialize non parameterized objects
        """
        First Initialize non parameterized objects, and add parameterized objects into dictionary 
        for later Initialization
        """
        self.non_tp_objects = {
        }  # Dictionary of non threaded non parameterized objects
        self.non_p_objects = {
        }  # Dictionary of threaded non parameterized objects
        self.parameterized_objects = {
        }  # Dictionary of all parameterized objects
        self.sensor_objects = {}
        self.car = Car()
        self.non_p_objects['car'] = self.car
        self.non_tp_objects['logger'] = Main.logger
        # Initialize utility module objects
        self.initialize_utility()
        # Initialize controller module objects
        self.initialize_controller()
        # Initialize sensor module objects
        self.initialize_sensor()
        # Initialize pilot module objects
        self.initialize_pilot()
        # Initialize parameterized objects
        """
            parameterized_objects structure:
            Type Dictionary => key : value
                Key is the name of objects' class to be initialized.
                Value is array of 3 elements: 
                    Value[0]: un-initialized object
                    Value[1]: List of the name of parameters 
                    Value[2]: is threaded?
        """

        for key in list(self.parameterized_objects):
            # [key] will return array, [0] will return the un-initialized object
            # After it return the un-initialized object it will initialize it using the parameters from get_parameters
            if key not in self.parameterized_objects:
                continue
            obj = self.parameterized_objects[key][0](self.get_parameters(
                self.parameterized_objects[key]))
            if self.parameterized_objects[key][2]:
                self.non_p_objects[key] = obj
            else:
                self.non_tp_objects[key] = obj
            del self.parameterized_objects[key]

        # Separate sensor objects
        for k in list(self.non_p_objects):
            if k in self.sensor_config.modules:
                self.sensor_objects[k] = self.non_p_objects[k]
                del self.non_p_objects[k]

        # Initialize car object
        self.car.initialize_objects(self.non_tp_objects, self.non_p_objects,
                                    self.sensor_objects)

    def initialize_utility(self):
        for name, obj in inspect.getmembers(sys.modules['utility']):
            if inspect.isclass(obj) and name in self.utility_config.modules and \
                    self.utility_config.modules[name].is_active:
                if self.utility_config.modules[name].parameterized:
                    # append to parameterized objects dictionary for later Initialization
                    self.parameterized_objects[name] = [
                        obj, self.utility_config.modules[name].parameters,
                        self.utility_config.modules[name].threaded
                    ]
                else:
                    if self.utility_config.modules[name].threaded:
                        self.non_p_objects[name] = obj(
                        )  # Initialize object and append to threaded non parameterized
                    else:
                        # Initialize object and append to non threaded non parameterized
                        self.non_tp_objects[name] = obj()
            elif name in self.utility_config.modules and inspect.ismodule(obj) \
                    and self.utility_config.modules[name].is_active:
                print(name)
                obj = getattr(obj,
                              self.utility_config.modules[name].class_name)
                if self.utility_config.modules[name].parameterized:
                    # append to parameterized objects dictionary for later Initialization
                    self.parameterized_objects[name] = [
                        obj, self.utility_config.modules[name].parameters,
                        self.utility_config.modules[name].threaded
                    ]
                else:
                    if self.utility_config.modules[name].threaded:
                        self.non_p_objects[name] = obj(
                        )  # Initialize object and append to threaded non parameterized
                    else:
                        # Initialize object and append to non threaded non parameterized
                        self.non_tp_objects[name] = obj()

    def initialize_controller(self):
        for name, obj in inspect.getmembers(sys.modules['car.controller']):
            if inspect.isclass(obj) and name in self.controller_config.modules \
                    and self.controller_config.modules[name].is_active:
                if self.controller_config.modules[name].parameterized:
                    # append to parameterized objects dictionary for later Initialization
                    self.parameterized_objects[name] = [
                        obj, self.controller_config.modules[name].parameters,
                        self.controller_config.modules[name].threaded
                    ]
                else:
                    if self.controller_config.modules[name].threaded:
                        # Initialize object and append to threaded non parameterized
                        self.non_p_objects[name] = obj()
                    else:
                        # Initialize object and append to non threaded non parameterized
                        self.non_tp_objects[name] = obj()
            elif name in self.controller_config.modules and inspect.ismodule(obj) \
                    and self.controller_config.modules[name].is_active:
                obj = getattr(obj,
                              self.controller_config.modules[name].class_name)
                if self.controller_config.modules[name].parameterized:
                    # append to parameterized objects dictionary for later Initialization
                    self.parameterized_objects[name] = [
                        obj, self.controller_config.modules[name].parameters,
                        self.controller_config.modules[name].threaded
                    ]
                else:
                    if self.controller_config.modules[name].threaded:
                        # Initialize object and append to threaded non parameterized
                        self.non_p_objects[name] = obj()
                    else:
                        self.non_tp_objects[name] = obj()

    def initialize_sensor(self):
        for name, obj in inspect.getmembers(sys.modules['car.sensor']):
            if inspect.isclass(obj) and name in self.sensor_config.modules \
                    and self.sensor_config.modules[name].is_active:
                if self.sensor_config.modules[name].parameterized:
                    # append to parameterized objects list for later Initialization
                    self.parameterized_objects[name] = [
                        obj, self.sensor_config.modules[name].parameters,
                        self.sensor_config.modules[name].threaded
                    ]
                else:
                    if self.sensor_config.modules[name].threaded:
                        self.non_p_objects[name] = obj()
                    else:
                        self.non_tp_objects[name] = obj(
                        )  # Initialize object and append
            elif name in self.sensor_config.modules and inspect.ismodule(obj) \
                    and self.sensor_config.modules[name].is_active:
                obj = getattr(obj, self.sensor_config.modules[name].class_name)
                if self.sensor_config.modules[name].parameterized:
                    # append to parameterized objects dictionary for later Initialization
                    self.parameterized_objects[name] = [
                        obj, self.sensor_config.modules[name].parameters,
                        self.sensor_config.modules[name].threaded
                    ]
                else:
                    if self.sensor_config.modules[name].threaded:
                        # Initialize object and append to threaded non parameterized
                        self.non_p_objects[name] = obj()
                    else:
                        self.non_tp_objects[name] = obj()  #

    def initialize_pilot(self):
        for name, obj in inspect.getmembers(sys.modules['pilot.agent']):
            if inspect.isclass(obj) and name in self.pilot_config.modules \
                    and self.pilot_config.modules[name].is_active:
                if self.pilot_config.modules[name].parameterized:
                    # append to parameterized objects list for later Initialization
                    self.parameterized_objects[name] = [
                        obj, self.pilot_config.modules[name].parameters,
                        self.pilot_config.modules[name].threaded
                    ]
                else:
                    if self.pilot_config.modules[name].threaded:
                        # Initialize object and append to threaded non parameterized
                        self.non_p_objects[name] = obj()
                    else:
                        # Initialize object and append to non threaded non parameterized
                        self.non_tp_objects[name] = obj()
            elif name in self.pilot_config.modules and inspect.ismodule(obj) \
                    and self.pilot_config.modules[name].is_active:
                obj = getattr(obj, self.pilot_config.modules[name].class_name)
                if self.pilot_config.modules[name].parameterized:
                    # append to parameterized objects dictionary for later Initialization
                    self.parameterized_objects[name] = [
                        obj, self.pilot_config.modules[name].parameters,
                        self.pilot_config.modules[name].threaded
                    ]
                else:
                    if self.pilot_config.modules[name].threaded:
                        # Initialize object and append to threaded non parameterized
                        self.non_p_objects[name] = obj()
                    else:
                        self.non_tp_objects[name] = obj()

    def get_parameters(self, parameterized_object):
        p_param = {}  # dictionary of parameters to be passed to the object
        for param in parameterized_object[1]:
            if param in self.non_tp_objects:
                p_param[param] = self.non_tp_objects[param]
            elif param in self.non_p_objects:
                p_param[param] = self.non_p_objects[param]
            elif param in self.parameterized_objects:
                # Remove it from parameterized_objects dictionary and return it
                p_object = self.parameterized_objects.pop(param)
                p_object[0] = p_object[0](self.get_parameters(p_object))
                p_param[param] = p_object[0]
                if p_object[
                        2]:  # if threaded add to threaded non parameterized dictionary
                    self.non_p_objects[param] = p_object[0]
                else:
                    self.non_tp_objects[param] = p_object[0]
        return p_param
示例#17
0
 def _setup_logger(self, log_dir, model_name):
     return Logger(log_dir, model_name)
示例#18
0
    optParser.add_option("--enable-save",
                         action="store_true",
                         dest="save",
                         help="enable store output files."
                         " default won't saving anything")
    optParser.add_option("--force-update",
                         action="store_true",
                         dest="force_update",
                         help="force to renew the source data.")
    optParser.add_option("--log",
                         action="store_true",
                         dest="log",
                         help="enable logger to file."
                         " default show log at console only")
    optParser.add_option("--stemmer",
                         default=None,
                         dest="stemmer",
                         help="customize stemmer type, (porter, snowball)")
    (options, args) = optParser.parse_args()

    Settings = init_config()
    level = max(Settings.LogLevel, logging.NOTSET)
    logger = Logger('keywall.greper', level=level, enable=options.log)

    try:
        init_stemmer(options, *args, logger=logger)
        client = get_client(options, *args, logger=logger)
        client.run()
    except KeyboardInterrupt:
        logger.error("keyboard interrupt main tread shutdown")
示例#19
0
        'weights - train both with "m40". Experiments on LINEMOD ("lm") and 7-Scenes (7scenes) use no pretraining.'
    )
    args = parser.parse_args()

    # PATHS
    dataset = args.dataset
    mode = args.mode
    code_path = os.path.dirname(os.path.abspath(__file__)).replace(
        "/registration", "")
    if not os.path.exists(os.path.join(code_path, "logs")):
        os.mkdir(os.path.join(code_path, "logs"))
    if not os.path.exists(os.path.join(code_path, "weights")):
        os.mkdir(os.path.join(code_path, "weights"))
    model_path = os.path.join(code_path, f"weights/{dataset}_{mode}")
    logger = Logger(log_dir=os.path.join(code_path, f"logs/{dataset}/"),
                    log_name=f"{mode}",
                    reset_num_timesteps=True)

    # TRAINING
    agent = Agent().to(DEVICE)

    if args.mode == "pretrain" and dataset in ["m40", "7scenes"]:
        print(f"Training: dataset '{dataset}'  - mode '{args.mode}'")
        train(agent,
              logger,
              dataset,
              noise_type="clean",
              epochs=50,
              lr=1e-3,
              lr_step=10,
              alpha=0,