示例#1
0
文件: mlo.py 项目: icltc/MLO
def main():
    logging.info("Running MLO")

    parser = argparse.ArgumentParser()
    parser.add_argument("-f", help="fitness script, use fitnessTemplate.py")
    parser.add_argument("-c", help="config script, use test_configuration.py")
    parser.add_argument("--gui", help="enable the GUI", action="store_true")
    parser.add_argument("--restart", help="restart after crash (in the terminal mode)", action="store_true")
    args = parser.parse_args()

    if not (args.gui or args.f or args.c or args.restart):
        print "Either run the program with --gui or provide a fitness and " "configuration scripts (with -f and -c)"
        sys.exit(1)

    gui = args.gui
    restart = args.restart
    fitness = None
    configuration = None

    # Load the fitness script
    if args.f:
        fitness = load_script(args.f, "fitness")
        if fitness is None:
            sys.exit(1)

    # Load the configuration script
    if args.c:
        configuration = load_script(args.c, "configuration")
        if configuration is None:
            sys.exit(1)

    if not gui and not restart and (not fitness or not configuration):
        print "Make sure to provide both the fitness and configuration scripts"
        sys.exit(1)

    # Now we have all the inputs and can run the program

    if gui:
        logging.info("Will run with GUI")
    if restart:
        logging.info("Will restart optimisation")

    controller = Controller(gui, restart)
    if not gui:
        controller.fitness = fitness
        controller.configuration = configuration

    controller.take_over_control()

    logging.info("MLO finished successfully")
示例#2
0
 def load(self):
     logging.info('Loading the Run')
     try:
         with open(self.get_run_file(), 'rb') as outfile:
             dict = pickle.load(outfile)
             self.configuration = load_script(dict["configuration_file_name"],'configuration')
             self.fitness = load_script(dict["fitness_file_name"],'fitness')
             self.set_state_dictionary(dict)
             #self.set_start_time(datetime.now().strftime('%d-%m-%Y  %H:%M:%S'))
     except Exception, e:
         logging.error('Error loading run\'s run_data.txt. Cannot restart '
                       'without this file. {}'.format(str(e)))
         logging.error('Could not load fitness and/or configuration '
                       'scripts. Terminating...')            
         return False
示例#3
0
 def get(self):
     script = utils.load_script()
     """ send all question data for testing
     """
     for k, v in script.iteritems():
         if 'data' in v.keys():
             for d in v['data']:
                 server.send_data(d)
示例#4
0
文件: run.py 项目: FriedSock/MLO-1
    def load_run_data(self):
        """
        Loads settings of a previously crashed run from the disc.
        """
        trial_results_folders = []

        run_file = self.results_folder_path + '/run_data.txt'
        try:
            with open(run_file, 'r') as run_data:
                self.name = run_data.readline().rstrip('\n')
                fitness = run_data.readline().rstrip('\n')
                self.fitness = load_script(fitness, 'fitness')
                configuration = run_data.readline().rstrip('\n')
                self.configuration = load_script(configuration,
                                                 'configuration')
                if not self.fitness or not self.configuration:
                    raise 'Error loading scripts'

                for line in run_data:
                    trial_results_folders.append(line.rstrip('\n'))
        except:
            logging.error('Error loading run\'s run_data.txt. Cannot restart '
                          'without this file.')
            logging.error('Could not load fitness and/or configuration '
                          'scripts. Terminating...')
            print sys.exc_info()[1]
            sys.exit(1)

        self.no_of_trials = self.configuration.trials_count
        Trial = self.configuration.trials_type

        for trial_no in range(1, self.no_of_trials + 1):
            trial = Trial(trial_no, self.name, self.fitness,
                          self.configuration, self.controller,
                          self.results_folder_path)
            trial.results_folder = trial_results_folders[trial_no - 1]
            trial.run_initialize()
            if not trial.load():
                logging.error('Failed loading a trial from {}'.format(
                    trial.results_folder))
                trial.initialize_population()
            self.trials.append(trial)
示例#5
0
 def get(self, question):
     script = utils.load_script()
     for d in script[question]['data']:
         server.send_data()
示例#6
0
def start_script(server):
    script = utils.load_script()

    question_key = "question_1"
    if question_key not in script:
        raise Exception("Script has to start with question_1")

    while True:
        time.sleep(0.5)

        current_question = script[question_key]

        # Joanna Question UI
        print current_question["text"]

        server.send_message('out', current_question["text"])

        # Video File
        video_path = os.path.join("/assets/videos", current_question["video"])

        server.send_video(video_path)

        # Sleep for length of file
        time.sleep(get_length(video_path))

        if "answer" in current_question:
            answers = current_question["answer"]

            if DEMO_MODE:
                content = current_question["demo_answer"]
                time.sleep(3)
            else:
                data = record()
                response = recognize(data, RATE)

                if len(response.results) == 0:
                    continue

                result = response.results[0]
                if not result.alternatives:
                    continue

                content = result.alternatives[0].transcript

            # Patient answer
            server.send_message('in', content)

            key_list = answers.keys()
            key_list.remove("any")

            if not key_list:
                update_form(server, current_question)
                question_key = answers["any"]
            else:
                found_answer = False
                for answer in key_list:
                    if answer in content:
                        print "Found " + answer + " in " + content
                        question_key = answers[answer]
                        found_answer = True
                        update_form(server, current_question)
                        break

                if not found_answer:
                    update_form(server, current_question)
                    question_key = answers["any"]
        elif "next" in current_question:
            print 'found next'
            question_key = current_question["next"]
            print question_key
            update_form(server, current_question)
        else:
            update_form(server, current_question)
            # End the questionaire
            break
示例#7
0
def main():
    logging.info('Running MLO')

    parser = argparse.ArgumentParser()
    parser.add_argument('-f', help='fitness script, use fitnessTemplate.py')
    parser.add_argument('-c', help='config script, use test_configuration.py')
    parser.add_argument('--gui', help='enable the GUI',
                        action='store_true')
    parser.add_argument('--restart', help='restart after crash (in the terminal mode)',
                        action='store_true')
    args = parser.parse_args()

    if not (args.gui or args.f or args.c or args.restart):
        print 'Either run the program with --gui or provide a fitness and ' \
              'configuration scripts (with -f and -c)'
        sys.exit(1)
    gui = args.gui
    restart = args.restart
    fitness = None
    configuration = None

    if not gui and not restart and (not args.f or not args.c):
        print 'Make sure to provide both the fitness and configuration scripts or restart flag'
        sys.exit(1)    
    
    ## start in gui mode
    if gui:
        ## initialize controller
        restart = True
        controller = Controller(restart)
        logging.info('Will run with GUI')
        controller.view = GUIView()
    ## start in terminal mode
    else:
        ## initialize controller, restart runs only if requested
        controller = Controller(restart)
        controller.view = TerminalView()
        
        if restart:
            logging.info('Will restart most recent run, for other runs run in GUI mode..')
            fit = self.controller.get_most_recent_fitness_script_folder() + "/" + self.controller.get_most_recent_fitness_script_file() 
            config = self.controller.get_most_recent_configuration_script_folder() + "/" + self.controller.get_most_recent_configuration_script_file() 
        else:
            fit = args.f
            config = args.c
            
        # Load the fitness script
        controller.fitness = load_script(fit, 'fitness')
        if controller.fitness is None:
            logging.info('Could not load fitness file')
            sys.exit(1)

        # Load the configuration script
        controller.configuration = load_script(config, 'configuration')
        if controller.configuration is None:
            logging.info('Could not load configuration file')
            sys.exit(1)
            
    controller.take_over_control()

    logging.info('MLO finished successfully')
    sys.exit(0)