示例#1
0
def start_simulation(initial_data, verbose=True):
    enable_contact_tracing = bool(initial_data['contact_tracing'])  # Enable contact tracing
    total_agents = int(initial_data['total_agents'])  # Number of Agents
    initially_infected_agents = int(initial_data['infected_agents'])  # Number of initially infected agents
    initially_healthy_agents = int(total_agents - initially_infected_agents)  # Number of initially healthy agents
    office_capacity = int(initial_data['office_capacity'])  # Capacity of agents per office
    house_capacity = int(initial_data['home_capacity'])  # Capacity of agents per house
    mortality_rate = float(initial_data['mortality_rate'])  # Mortality rate
    total_days_sick = int(initial_data['sick_days'])  # Number of days sick
    days_until_symptoms = int(initial_data['free_symptoms_days'])  # Number of days until symptoms
    total_days_simulated = int(initial_data['total_days'])  # Number of days of simulation
    risk_infection_home = float(initial_data['risk_home'])  # Risk of infection at home
    risk_infection_work = float(initial_data['risk_work'])  # Risk of infection at work
    verbose = bool(verbose)  # If we want printing during simulator run

    simulation_id = initial_data['simulation_id'].replace("-", "_")

    locations, agent_array = initialize(total_agents, initially_infected_agents, initially_healthy_agents,
                                        office_capacity, house_capacity, mortality_rate, total_days_sick,
                                        days_until_symptoms, total_days_simulated, risk_infection_home,
                                        risk_infection_work)

    saver = Saver(verbose, simulation_id)
    simulator = Simulator(enable_contact_tracing, locations, agent_array, saver, simulation_id)

    saver.initialize_db(locations, agent_array)

    while simulator.current_day <= total_days_simulated:
        simulator.step()

    return 200, saver.overview
def train():
    autoencoder = SegNetAutoencoder()
    images, labels = inputs(FLAGS.train, FLAGS.train_labels, FLAGS.batch)
    logits = inference(autoencoder, images)

    cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits,
                                                            labels,
                                                            name='xentropy')
    loss = tf.reduce_mean(cross_entropy, name='loss')
    tf.scalar_summary(loss.op.name, loss)

    optimizer = tf.train.AdamOptimizer(1e-04)
    train_step = optimizer.minimize(cross_entropy)

    init = tf.global_variables_initializer()
    saver = tf.train.Saver()
    config = tf.ConfigProto(allow_soft_placement=True)
    with tf.Session(config=config) as sess:
        sess.run(init)
        initializer.initialize(autoencoder.get_encoder_parameters(), sess)

        summary = tf.merge_all_summaries()
        summary_writer = tf.train.SummaryWriter(FLAGS.train_logs, sess.graph)

        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)

        for step in tqdm(range(FLAGS.steps + 1)):
            sess.run(train_step)

            if step % 10 == 0:
                summary_str = sess.run(summary)
                summary_writer.add_summary(summary_str, step)
                summary_writer.flush()

            if step % 1000 == 0:
                saver.save(sess, FLAGS.train_ckpt)

        coord.request_stop()
        coord.join(threads)
示例#3
0
    def __init__(
            self,
            last_end_time,  # initializer
            groupList,  # initializer
            max_time_extend,  # initializer
            max_cft_pkt,  # initializer
            min_time_extend,
            max_crafted_pkt_prob,
            show_info=False):

        self.show_info = show_info

        self.grp_size = len(groupList)
        self.groupList = groupList
        self.max_cft_pkt = max_cft_pkt
        self.max_time_extend = max_time_extend
        self.last_end_time = last_end_time
        self.proto_max_lmt = []

        self.pktList = None
        self.feature = None
        self.all_feature = None
        self.local_FE = None

        if self.show_info:
            print("----@Particle: Initializing...")

        # initialize X and V
        self.X, self.proto_max_lmt = initialize(self.grp_size, last_end_time,
                                                groupList, max_time_extend,
                                                max_cft_pkt, min_time_extend,
                                                max_crafted_pkt_prob)
        self.V = Unit(self.grp_size, self.max_cft_pkt)

        self.indi_best_X = None
        self.indi_best_dis = -1.
        self.dis = -1.
示例#4
0
    total_agents = 10000  # Number of Agents
    initially_infected_agents = 10  # Number of initially infected agents
    initially_healthy_agents = total_agents - initially_infected_agents  # Number of initially healthy agents
    office_capacity = 30  # Capacity of agents per office
    house_capacity = 4  # Capacity of agents per house
    mortality_rate = 0.1  # Mortality rate
    total_days_sick = 10  # Number of days sick
    days_until_symptoms = 7  # Number of days isolation
    total_days_simulated = 24  # Number of days of simulation
    risk_infection_home = 0.1  # Risk of infection at home
    risk_infection_work = 0.1  # Risk of infection at work
    verbose = True  # If we want printing during simulator run

number_of_offices, number_of_homes = initialize(
    total_agents, initially_infected_agents, initially_healthy_agents,
    office_capacity, house_capacity, mortality_rate, total_days_sick,
    days_until_symptoms, total_days_simulated, risk_infection_home,
    risk_infection_work)
(total_agents, initially_infected_agents, initially_healthy_agents,
 office_capacity, house_capacity, mortality_rate, total_days_sick,
 days_until_symptoms, total_days_simulated, risk_infection_home,
 risk_infection_work)
saver = Saver(verbose)
simulator = Simulator(enable_contact_tracing, saver, number_of_homes,
                      number_of_offices, total_agents, risk_infection_work,
                      risk_infection_home, total_days_sick)

while simulator.current_day <= total_days_simulated:
    simulator.step()

#saver.print_overview()
def main(argv=None):
    ''' Runs the program. There are three ways to pass arguments
    1) environment variables TFB_*
    2) configuration file benchmark.cfg
    3) command line flags
    In terms of precedence, 3 > 2 > 1, so config file trumps environment variables
    but command line flags have the final say
    '''
    # Do argv default this way, as doing it in the functional declaration sets it at compile time
    if argv is None:
        argv = sys.argv

    # Enable unbuffered output so messages will appear in the proper order with subprocess output.
    sys.stdout = Unbuffered(sys.stdout)

    # Update python environment
    # 1) Ensure the current directory (which should be the benchmark home directory) is in the path so that the tests can be imported.
    sys.path.append('.')
    # 2) Ensure toolset/setup/linux is in the path so that the tests can "import setup_util".
    sys.path.append('toolset/setup/linux')

    # Update environment for shell scripts
    os.environ['FWROOT'] = setup_util.get_fwroot()
    os.environ['IROOT'] = os.environ['FWROOT'] + '/installs'
    # 'Ubuntu', '14.04', 'trusty' respectively
    os.environ['TFB_DISTRIB_ID'], os.environ[
        'TFB_DISTRIB_RELEASE'], os.environ[
            'TFB_DISTRIB_CODENAME'] = platform.linux_distribution()
    # App server cpu count
    os.environ['CPU_COUNT'] = str(multiprocessing.cpu_count())

    conf_parser = argparse.ArgumentParser(
        description=__doc__,
        formatter_class=argparse.RawDescriptionHelpFormatter,
        add_help=False)
    conf_parser.add_argument(
        '--conf_file',
        default='benchmark.cfg',
        metavar='FILE',
        help=
        'Optional configuration file to provide argument defaults. All config options can be overridden using the command line.'
    )
    args, remaining_argv = conf_parser.parse_known_args()

    defaults = {}
    try:
        if not os.path.exists(
                os.path.join(
                    os.environ['FWROOT'],
                    args.conf_file)) and not os.path.exists(
                        os.path.join(os.environ['FWROOT'] + 'benchmark.cfg')):
            print("No config file found. Aborting!")
            exit(1)
        with open(os.path.join(os.environ['FWROOT'], args.conf_file)):
            config = ConfigParser.SafeConfigParser()
            config.read([os.path.join(os.environ['FWROOT'], args.conf_file)])
            defaults.update(dict(config.items("Defaults")))
            # Convert strings into proper python types
            for k, v in defaults.iteritems():
                try:
                    defaults[k] = literal_eval(v)
                except Exception:
                    pass
    except IOError:
        print("Configuration file not found!")
        exit(1)

    ##########################################################
    # Set up default values
    ##########################################################

    # Verify and massage options
    if defaults['client_user'] is None or defaults['client_host'] is None:
        print("client_user and client_host are required!")
        print("Please check your configuration file.")
        print("Aborting!")
        exit(1)

    if defaults['database_user'] is None:
        defaults['database_user'] = defaults['client_user']
    if defaults['database_host'] is None:
        defaults['database_host'] = defaults['client_host']
    if defaults['server_host'] is None:
        defaults['server_host'] = defaults['client_host']
    if defaults['ulimit'] is None:
        defaults['ulimit'] = 200000

    os.environ['ULIMIT'] = str(defaults['ulimit'])

    ##########################################################
    # Set up argument parser
    ##########################################################
    parser = argparse.ArgumentParser(
        description="Install or run the Framework Benchmarks test suite.",
        parents=[conf_parser],
        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
        epilog=
        '''If an argument includes (type int-sequence), then it accepts integer lists in multiple forms.
        Using a single number e.g. 5 will create a list [5]. Using commas will create a list containing those
        values e.g. 1,3,6 creates [1, 3, 6]. Using three colon-separated numbers of start:step:end will create a
        list, using the semantics of python's range function, e.g. 1:3:15 creates [1, 4, 7, 10, 13] while
        0:1:5 creates [0, 1, 2, 3, 4]
        ''')

    # Install options
    parser.add_argument('--init',
                        action='store_true',
                        default=False,
                        help='Initializes the benchmark environment')

    # Suite options
    parser.add_argument('--clean',
                        action='store_true',
                        default=False,
                        help='Removes the results directory')
    parser.add_argument('--new',
                        action='store_true',
                        default=False,
                        help='Initialize a new framework test')
    parser.add_argument(
        '-v',
        '--verbose',
        action='store_true',
        default=False,
        help=
        'Causes the configuration to print before any other commands are executed.'
    )
    parser.add_argument(
        '--quiet',
        action='store_true',
        default=False,
        help=
        'Only print a limited set of messages to stdout, keep the bulk of messages in log files only'
    )
    parser.add_argument(
        '--results-name',
        help='Gives a name to this set of results, formatted as a date',
        default='(unspecified, datetime = %Y-%m-%d %H:%M:%S)')
    parser.add_argument(
        '--results-environment',
        help='Describes the environment in which these results were gathered',
        default='(unspecified, hostname = %s)' % socket.gethostname())
    parser.add_argument(
        '--results-upload-uri',
        default=None,
        help=
        'A URI where the in-progress results.json file will be POSTed periodically'
    )
    parser.add_argument(
        '--parse',
        help=
        'Parses the results of the given timestamp and merges that with the latest results'
    )

    # Test options
    parser.add_argument('--test', nargs='+', help='names of tests to run')
    parser.add_argument(
        '--test-dir',
        nargs='+',
        dest='test_dir',
        help='name of framework directory containing all tests to run')
    parser.add_argument(
        '--test-lang',
        nargs='+',
        dest='test_lang',
        help='name of language directory containing all tests to run')
    parser.add_argument('--exclude',
                        nargs='+',
                        help='names of tests to exclude')
    parser.add_argument('--type',
                        choices=[
                            'all', 'json', 'db', 'query', 'cached_query',
                            'fortune', 'update', 'plaintext'
                        ],
                        default='all',
                        help='which type of test to run')
    parser.add_argument(
        '-m',
        '--mode',
        choices=['benchmark', 'verify', 'debug'],
        default='benchmark',
        help=
        'verify mode will only start up the tests, curl the urls and shutdown. debug mode will skip verification and leave the server running.'
    )
    parser.add_argument('--list-tests',
                        action='store_true',
                        default=False,
                        help='lists all the known tests that can run')

    # Benchmark options
    parser.add_argument('--duration',
                        default=15,
                        help='Time in seconds that each test should run for.')
    parser.add_argument(
        '--sleep',
        type=int,
        default=60,
        help=
        'the amount of time to sleep after starting each test to allow the server to start up.'
    )

    parser.set_defaults(
        **defaults
    )  # Must do this after add, or each option's default will override the configuration file default
    args = parser.parse_args(remaining_argv)

    if args.new:
        Scaffolding().scaffold()
        return 0

    if args.init:
        initialize(args)
        return 0

    benchmarker = Benchmarker(vars(args))

    if args.clean:
        benchmarker.clean_all()
        return 0

    # Run the benchmarker in the specified mode
    #   Do not use benchmarker variables for these checks,
    #   they are either str or bool based on the python version
    if args.list_tests:
        benchmarker.run_list_tests()
    elif args.parse != None:
        benchmarker.parse_timestamp()
    else:
        return benchmarker.run()
示例#6
0
    def __init__(self):

        QtWidgets.QWidget.__init__(self)
        app.setStyle('Fusion')
        self.iconFile = 'atlogo.ico'

        self.status = " "

        data_file = "data.json"
        if not os.path.exists(data_file):
            name = self.getText()
            email = self.getText2()
            user_data.get_user_data(name, email)

        import initializer
        initializer.initialize()

        import mail
        if Clock.get_day(self) == 7:
            mail.send_mail()


        self.time_selected = 0
        user = user_data.extract_data()
        directory = user['directory']
        self.program_dir = directory
        self.directory_to = 'timesheets'
        self.file_name = Clock.get_year(self) + '_timebook.xlsx'

        self.threadpool = QThreadPool()

        # Init QSystemTrayIcon
        self.tray_icon = QSystemTrayIcon(QIcon(self.iconFile))
        self.tray_icon.show()

        # Tray menu
        show_action = QAction("Show", self)
        quit_action = QAction("Exit", self)
        hide_action = QAction("Hide", self)
        show_action.triggered.connect(self.show)
        hide_action.triggered.connect(self.hide)
        quit_action.triggered.connect(app.quit)

        tray_menu = QMenu()
        tray_menu.addAction(show_action)
        tray_menu.addAction(hide_action)
        tray_menu.addAction(quit_action)
        self.tray_icon.setContextMenu(tray_menu)
        self.tray_icon.show()

        # App window
        self.setGeometry(300, 300, 300, 180)
        self.setStyleSheet("background-color: White;")
        self.setWindowTitle("Auto Timesheet")
        self.setFixedSize(300, 180)
        self.center()

        # Widgets
        self.titlelabel = QtWidgets.QLabel(self)
        self.titlelabel.setGeometry(QtCore.QRect(50, 10, 200, 40))
        self.titlelabel.setText("")
        self.titlelabel.setPixmap(QtGui.QPixmap('CNRtitle.png'))
        self.titlelabel.setScaledContents(True)
        self.titlelabel.setObjectName("titlelabel")

        self.Label = QtWidgets.QLabel(self)
        self.Label.setText("Submission @ 16:00")
        self.Label.setAlignment(QtCore.Qt.AlignCenter)
        self.Label.move(85, 60)
        self.Label.setFont(QFont("calibri", 11))
        self.Label.adjustSize()


        self.submitButton = QtWidgets.QPushButton(self)
        self.submitButton.setText("Submit Now")
        self.submitButton.move(105, 100)
        self.submitButton.clicked.connect(self.submit_clicked)
        self.submitButton.setFont(QFont("Calibri", 11))
        self.submitButton.setStyleSheet("QPushButton::hover"
                                        "{"
                                        "background-color : lightgrey;"
                                        "}")

        self.timeButton = QtWidgets.QPushButton(self)
        self.timeButton.setText("Review")
        self.timeButton.move(107, 130)
        self.timeButton.clicked.connect(self.review_clicked)
        self.timeButton.setFont(QFont("Calibri", 11))

        self.Status = QtWidgets.QLabel(self)
        self.Status.setGeometry(QtCore.QRect(50, 10, 200, 40))
        self.Status.move(10, 165)
        self.Status.setText(str(self.status))
        self.Status.setObjectName("Promptlabel")
        self.Status.setFont(QFont("calibri", 6))
        self.Status.adjustSize()

        self.name = QtWidgets.QLabel(self)
        self.name.setGeometry(QtCore.QRect(50, 10, 200, 40))
        self.name.move(240, 165)
        self.name.setText("made by grizzly")
        self.name.setObjectName("Promptlabel")
        self.name.setFont(QFont("calibri", 6))
        self.name.adjustSize()

        self.wb = load_workbook(str(self.locate_timebook(self.documents_dir() + '\\Timesheets\\')))

        self.Create()

        self.temp_wb = load_workbook(str(self.locate_template(self.program_dir)))

        self.active_sheet = self.wb[Clock.get_month(self)]
        self.check_timebook(self.program_dir)

        self.timer()
示例#7
0
    logging.basicConfig(level=logging.INFO)

    # Log (rotated) to backup-agent.log
    logfile = os.path.join(environment.PATH_WORK, "backup-agent.log")
    fileHandler = logging.handlers.RotatingFileHandler(logfile,
                                                       maxBytes=50000000,
                                                       backupCount=5)
    fileHandler.setFormatter(
        logging.Formatter(
            "%(asctime)s [%(threadName)-12.12s] [%(levelname)-5.5s]  %(message)s"
        ))
    LOGGER.addHandler(fileHandler)

    LOGGER.info("starting safeplan backup agent for device %s",
                os.environ['SAFEPLAN_ID'])

    if initializer.initialize():
        SCHEDULER.start()
        SCHEDULER.add_job(worker.do_work,
                          'interval',
                          seconds=environment.EXECUTE_WORKER_EVERY_SECONDS,
                          id='worker')
        signal.signal(signal.SIGTERM, shutdown_handler)

        worker.do_work()

        start_swagger()
    else:
        cc.report_to_control_center("fail", "device initialization failed")
        sys.exit('Initializing failed, exiting.')
示例#8
0
def do_experiment(experiment_dir,beats=0,bars=0,nCodes=0,nIter=1e7,
                  partialbar=0,keyInv=False,songKeyInv=True,lrate=1e-3,
                  mat_dir='',useModel='VQ',autobar=False,randoffset=False):
    """
    Main function to run an experiment, train a model and save to dir.
    """

    # check for 'done' file
    if os.path.exists(os.path.join(experiment_dir,'DONE.txt')):
        return

    # if experiment folder does not exist, create it
    if not os.path.exists(experiment_dir):
        print 'creating experiment dir:',experiment_dir
        os.mkdir(experiment_dir)

    # ec = exit code for training, 0 = ok, >0 = bad
    ec = 1

    # check if saved model exists
    alldirs = glob.glob(os.path.join(experiment_dir,'*'))
    if len(alldirs) > 0:
        alldirs = filter(lambda x: os.path.isdir(x), alldirs)
        alldirs = filter(lambda x: os.path.split(x)[-1][:4] == 'exp_',alldirs)
        # trim badly saved models
        alldirs = filter(lambda x: check_saved_model_full(x), alldirs)
    continue_training = len(alldirs) > 0

    # continue from saved model
    if continue_training:
        # find most recent saved mdoel, and continue!
        # ec = exit_code, 0 if due to StopIteration, >0 otherwise
        savedmodel = np.sort(alldirs)[-1]
        ec = trainer.train(savedmodel)

    # no prior saved model
    if not continue_training:
        #initialize and save codebook
        codebook = initializer.initialize(nCodes, pSize=beats, usebars=bars,
                                          keyInv=keyInv, songKeyInv=songKeyInv,
                                          positive=True, do_resample=True,
                                          partialbar=partialbar, nThreads=4,
                                          oracle='MAT', artistsdb='',
                                          matdir=mat_dir,randoffset=randoffset)
        codebook_fname = os.path.join(experiment_dir,'codebook.mat')
        scipy.io.savemat(codebook_fname,{'codebook':codebook})
        print 'after initialization, codebook saved to:',codebook_fname

        # train (from scratch)
        # ec = exit_code, 0 if due to StopIteration, >0 otherwise
        ec = trainer.train(codebook_fname, expdir=experiment_dir, pSize=beats,
                           usebars=bars, keyInv=keyInv, songKeyInv=songKeyInv,
                           positive=True, do_resample=True,
                           partialbar=partialbar, lrate=lrate, nThreads=4,
                           oracle='MAT', artistsdb='',
                           matdir=mat_dir, nIterations=nIter, useModel=useModel,
                           autobar=autobar,randoffset=randoffset)

    # write done file
    if ec == 0:
        f = open(os.path.join(experiment_dir,'DONE.txt'),'w')
        f.write('experiment appear to be done\n')
        f.close()

    # assume it's a keyboard interrupt, for multiprocessing purposes
    else:
        raise KeyboardInterruptError()