Exemplo n.º 1
0
def generate_score():
    """This is /generate-score page. It renders score.html.

    Query string format:
    /generate-score?verilog=[verilog_file]&input=[input_file]&output=[output_file]&ucf=[ucf_file]

    Returns:
        score.html
    """
    args = request.args
    inp = args['input']
    output = args['output']
    ucf = args['ucf']
    verilog = args['verilog']
    print(verilog, inp, output, ucf)

    # run optimizaiton and create a modifled file under "modified" directory
    data = optimize(inp)

    # calculate score

    score = calc_score(verilog, inp, output, ucf)
    modified_file = f'{inp}.input.json'

    return render_template('score.html',
                           score=score,
                           file=modified_file,
                           data=data)
Exemplo n.º 2
0
def main():
    instruments_list = parse(sys.argv[1])
    initial_assignment = assignment_original(instruments_list, 1)
    optimized = optimize(initial_assignment, instruments_list, 1)

    for player in optimized:
        print(player)
        print()
Exemplo n.º 3
0
def main():
    parser = build_parser()
    options = parser.parse_args()
    check_opts(options)

    style_target = get_img(options.style)
    if not options.slow:
        content_targets = _get_files(options.train_path)
    elif options.test:
        content_targets = [options.test]

    kwargs = {
        "slow": options.slow,
        "epochs": options.epochs,
        "print_iterations": options.checkpoint_iterations,
        "batch_size": options.batch_size,
        "save_path": os.path.join(options.checkpoint_dir, 'fns.ckpt'),
        "learning_rate": options.learning_rate
    }

    if options.slow:
        if options.epochs < 10:
            kwargs['epochs'] = 1000
        if options.learning_rate < 1:
            kwargs['learning_rate'] = 1e1

    args = [
        content_targets, style_target, options.content_weight,
        options.style_weight, options.tv_weight, options.vgg_path
    ]

    for preds, losses, i, epoch in optimize(*args, **kwargs):
        style_loss, content_loss, tv_loss, loss = losses

        print('Epoch %d, Iteration: %d, Loss: %s' % (epoch, i, loss))
        to_print = (style_loss, content_loss, tv_loss)
        print('style: %s, content:%s, tv: %s' % to_print)
        if options.test:
            assert options.test_dir != False
            preds_path = '%s/%s_%s.png' % (options.test_dir, epoch, i)
            if not options.slow:
                ckpt_dir = os.path.dirname(options.checkpoint_dir)
                evaluate.ffwd_to_img(options.test, preds_path,
                                     options.checkpoint_dir)
            else:
                save_img(preds_path, img)
    ckpt_dir = options.checkpoint_dir
    cmd_text = 'python evaluate.py --checkpoint %s ...' % ckpt_dir
    print("Training complete. For evaluation:\n    `%s`" % cmd_text)
Exemplo n.º 4
0
def main():
    args = parseCommandLineArguments()

    level = INFO
    if args.debug: level = 0
    elif args.verbose: level = DEBUG
    elif args.quiet: level = WARNING
    initializeLog(level)

    inputFile, reductionFile, chemkinFile, spcDict = args.requiredFiles[-4:]

    for f in [inputFile, reductionFile, chemkinFile, spcDict]:
        assert os.path.isfile(f), 'Could not find {}'.format(f)

    inputDirectory = os.path.abspath(os.path.dirname(inputFile))
    output_directory = inputDirectory

    rmg, targets, error = load(inputFile, reductionFile, chemkinFile, spcDict)
    logger.info('Allowed error in target observables: {0:.0f}%'.format(error *
                                                                       100))

    reactionModel = rmg.reactionModel
    initialize(rmg.outputDirectory, reactionModel.core.reactions)

    atol, rtol = rmg.absoluteTolerance, rmg.relativeTolerance
    index = 0
    reactionSystem = rmg.reactionSystems[index]

    #compute original target observables
    observables = computeObservables(targets, reactionModel, reactionSystem, \
     rmg.absoluteTolerance, rmg.relativeTolerance)

    logger.info('Observables of original model:')
    for target, observable in zip(targets, observables):
        logger.info('{}: {:.2f}%'.format(target, observable * 100))

    # optimize reduction tolerance
    tol, importantReactions = optimize(targets, reactionModel, rmg, index,
                                       error, observables)
    logger.info('Optimized tolerance: {:.0E}'.format(10**tol))
    logger.info('Number of reactions in optimized reduced model : {}'.format(
        len(importantReactions)))

    # plug the important reactions into the RMG object and write:
    rmg.reactionModel.core.reactions = importantReactions
    writeModel(rmg)
Exemplo n.º 5
0
def main():
    args = parseCommandLineArguments()

    level = INFO
    if args.debug: level = 0
    elif args.verbose: level = DEBUG
    elif args.quiet: level = WARNING
    initializeLog(level)

    inputFile, reductionFile, chemkinFile, spcDict = args.requiredFiles[-4:]

    for f in [inputFile, reductionFile, chemkinFile, spcDict]:
        assert os.path.isfile(f), 'Could not find {}'.format(f)

    inputDirectory = os.path.abspath(os.path.dirname(inputFile))
    output_directory = inputDirectory

    rmg, targets, error = load(inputFile, reductionFile, chemkinFile, spcDict)
    logger.info('Allowed error in target observables: {0:.0f}%'.format(error * 100))

    reactionModel = rmg.reactionModel
    initialize(rmg.outputDirectory, reactionModel.core.reactions)

    atol, rtol = rmg.absoluteTolerance, rmg.relativeTolerance
    index = 0
    reactionSystem = rmg.reactionSystems[index]    
    
    #compute original target observables
    observables = computeObservables(targets, reactionModel, reactionSystem, \
     rmg.absoluteTolerance, rmg.relativeTolerance)

    logger.info('Observables of original model:')
    for target, observable in zip(targets, observables):
        logger.info('{}: {:.2f}%'.format(target, observable * 100))

    # optimize reduction tolerance
    tol, importantReactions = optimize(targets, reactionModel, rmg, index, error, observables)
    logger.info('Optimized tolerance: {:.0E}'.format(10**tol))
    logger.info('Number of reactions in optimized reduced model : {}'.format(len(importantReactions)))

    # plug the important reactions into the RMG object and write:
    rmg.reactionModel.core.reactions = importantReactions
    writeModel(rmg)
Exemplo n.º 6
0
def main(args):
    """Main function of cellanneal."""
    if (args.start_temp is not None
            or args.end_temp is not None) and args.auto_temp == 1:
        raise Exception(
            "when auto_temp is set to 1(default value), starting temperature or ending temperature should not be set manually"
        )

    if not args.no_parallel:
        import dask
        from dask.distributed import Client, LocalCluster
        if not args.cluster:
            cluster = LocalCluster(
                n_workers=args.workers,
                local_dir="/tmp/CellUniverse/dask-worker-space")
        else:
            cluster = args.cluster

        client = Client(cluster)
    else:
        client = None

    lineagefile = None
    start = time.time()

    try:
        if args.debug:
            debugmode = True

        config = load_config(args.config)
        celltype = config['global.cellType'].lower()

        # setup the colony from a file with the initial properties
        lineageframes = LineageFrames()
        colony = lineageframes.forward()
        load_colony(colony, args.initial, config)

        # open the lineage file for writing
        lineagefile = open(args.output / 'lineage.csv', 'w')
        header = ['file', 'name']
        if celltype == 'bacilli':
            header.extend(['x', 'y', 'width', 'length', 'rotation'])
        print(','.join(header), file=lineagefile)

        if args.global_optimization:
            import global_optimization
            global_optimization.optimize(get_inputfiles(args), lineageframes,
                                         lineagefile, args, config)
            return 0

        if args.auto_temp == 1:
            temperature, end_temperature = auto_temp_schedule(
                get_inputfiles(args)[0], lineageframes.forward(), args, config)
            setattr(args, 'start_temp', temperature)
            setattr(args, 'end_temp', end_temperature)
            print("auto temperature schedule finished")
            print("starting temperature is ", args.start_temp,
                  "ending temperature is ", args.end_temp)

        for imagefile in get_inputfiles(args):

            colony = optimize(imagefile, lineageframes, args, config, client)

            # flatten modifications and save cell properties
            colony.flatten()
            for cellnode in colony:
                properties = [imagefile.name, cellnode.cell.name]
                if celltype == 'bacilli':
                    properties.extend([
                        str(cellnode.cell.x),
                        str(cellnode.cell.y),
                        str(cellnode.cell.width),
                        str(cellnode.cell.length),
                        str(cellnode.cell.rotation)
                    ])
                print(','.join(properties), file=lineagefile)

    except KeyboardInterrupt as error:
        raise error
    finally:
        if lineagefile:
            lineagefile.close()

    print(f'{time.time() - start} seconds')

    return 0
Exemplo n.º 7
0
    num_assets = len(input_returns.columns)
    turnover_counter = 0

    for date in dtindex[rebalancing_period - 1:]:
        today = date
        all_assets_returns = input_returns.loc[:today, :]
        last = all_assets_returns.index[-2]

        if today in rebalancing_dates:  # re-optimize and get new weights
            print(today)
            # Select available assets
            returns, available_assets = dp.returns_cleaning(
                input_returns, df0, rolling_window, today)

            weights.loc[today, :] = [0] * num_assets
            weights.loc[today, available_assets] = op.optimize(
                returns, risk_aversion, HMM_parameters)
            if today == rebalancing_dates[0]:
                turnover.loc[today] = 1
            else:
                turnover.loc[today] = sum(abs(weights.loc[today]-weights.loc[last, :] * (1 + all_assets_returns.loc[today, :]) \
                                    / (1 + (weights.loc[last, :] * all_assets_returns.loc[today, :]).sum())))
            turnover_counter += turnover.loc[today]

        else:  # no re-optimization, re-balance the weights

            weights.loc[today, :] = weights.loc[last, :] * (1 + all_assets_returns.loc[today, :]) \
                                    / (1 + (weights.loc[last, :] * all_assets_returns.loc[today, :]).sum())
            turnover.loc[today] = sum(abs(weights.loc[today]-weights.loc[last, :] * (1 + all_assets_returns.loc[today, :]) \
                                    / (1 + (weights.loc[last, :] * all_assets_returns.loc[today, :]).sum())))  # it's just zero

    # Create TC and No TC total returns series
Exemplo n.º 8
0
def main(args):
    """Main function of cellanneal."""
    if (args.start_temp is not None
            or args.end_temp is not None) and args.auto_temp == 1:
        raise Exception(
            "when auto_temp is set to 1(default value), starting temperature or ending temperature should not be set manually"
        )

    # if not args.no_parallel:
    #     import dask
    #     from dask.distributed import Client, LocalCluster
    #     if not args.cluster:
    #         cluster = LocalCluster(
    #             n_workers=args.workers,local_dir="/tmp/CellUniverse/dask-worker-space"
    #         )
    #     else:
    #         cluster = args.cluster
    #
    #     client = Client(cluster)
    # else:
    client = None

    lineagefile = None
    start = time.time()

    try:
        config = load_config(args.config)

        simulation_config = config["simulation"]
        #Maybe better to store the image type in the config file in the first place, instead of using cmd?
        if args.graySynthetic == True:
            simulation_config["image.type"] = "graySynthetic"
        elif args.phaseContrast == True:
            simulation_config["image.type"] = "phaseContrastImage"
        elif args.binary == True:
            simulation_config["image.type"] = "binary"
        else:
            raise ValueError(
                "Invalid Command: Synthetic image type must be specified")

        if args.debug:
            debugmode = True

        celltype = config['global.cellType'].lower()

        # setup the colony from a file with the initial properties
        lineageframes = LineageFrames()
        colony = lineageframes.forward()
        load_colony(colony, args.initial, config)
        cost_diff = (-1, -1)

        # open the lineage file for writing
        lineagefile = open(args.output / 'lineage.csv', 'w')
        header = ['file', 'name']
        if celltype == 'bacilli':
            header.extend(['x', 'y', 'width', 'length', 'rotation'])
        print(','.join(header), file=lineagefile)

        #optimze configuration
        config["simulation"] = find_optimal_simulation_conf(
            config["simulation"], load_image(get_inputfiles(args)[0]),
            list(colony))
        if args.global_optimization:
            import global_optimization
            if args.auto_temp == 1:
                print("auto temperature schedule started")
                args.start_temp, args.end_temp = \
                    global_optimization.auto_temp_schedule(get_inputfiles(args), lineageframes, lineagefile, args, config)
                print("auto temperature schedule finished")
                print("starting temperature is ", args.start_temp,
                      "ending temperature is ", args.end_temp)
            global_optimization.optimize(get_inputfiles(args), lineageframes,
                                         lineagefile, args, config)
            return 0

        if args.auto_temp == 1:
            print("auto temperature schedule started")
            args.start_temp, args.end_temp = auto_temp_schedule(
                get_inputfiles(args)[0], lineageframes.forward(), args, config)
            print("auto temperature schedule finished")
            print("starting temperature is ", args.start_temp,
                  "ending temperature is ", args.end_temp)

        frame_num = 0
        prev_cell_num = len(colony)
        for imagefile in get_inputfiles(
                args):  # Recomputing temperature when needed

            frame_num += 1

            if args.auto_meth == "frame":
                if auto_temp_schedule_frame(frame_num, 8):
                    print("auto temperature schedule started (recomputed)")
                    args.start_temp, args.end_temp = auto_temp_schedule(
                        imagefile, colony, args, config)
                    print("auto temperature schedule finished")
                    print("starting temperature is ", args.start_temp,
                          "ending temperature is ", args.end_temp)

            elif args.auto_meth == "factor":
                if auto_temp_schedule_factor(len(colony), prev_cell_num, 1.1):
                    print("auto temperature schedule started (recomputed)")
                    args.start_temp, args.end_temp = auto_temp_schedule(
                        imagefile, colony, args, config)
                    print("auto temperature schedule finished")
                    print("starting temperature is ", args.start_temp,
                          "ending temperature is ", args.end_temp)
                    prev_cell_num = len(colony)

            elif args.auto_meth == "const":
                if auto_temp_schedule_const(len(colony), prev_cell_num, 10):
                    print("auto temperature schedule started (recomputed)")
                    args.start_temp, args.end_temp = auto_temp_schedule(
                        imagefile, colony, args, config)
                    print("auto temperature schedule finished")
                    print("starting temperature is ", args.start_temp,
                          "ending temperature is ", args.end_temp)
                    prev_cell_num = len(colony)

            elif args.auto_meth == "cost":
                print(cost_diff, frame_num, auto_temp_shcedule_cost(cost_diff))
                if frame_num >= 2 and auto_temp_shcedule_cost(cost_diff):
                    print(
                        "auto temperature schedule started cost_diff (recomputed)"
                    )
                    args.start_temp, args.end_temp = auto_temp_schedule(
                        imagefile, colony, args, config)
                    print("auto temperature schedule finished")
                    print("starting temperature is ", args.start_temp,
                          "ending temperature is ", args.end_temp)

            colony = optimize(imagefile, lineageframes, args, config, client)

            cost_diff = update_cost_diff(colony, cost_diff)

            # flatten modifications and save cell properties
            colony.flatten()
            for cellnode in colony:
                properties = [imagefile.name, cellnode.cell.name]
                if celltype == 'bacilli':
                    properties.extend([
                        str(cellnode.cell.x),
                        str(cellnode.cell.y),
                        str(cellnode.cell.width),
                        str(cellnode.cell.length),
                        str(cellnode.cell.rotation)
                    ])
                print(','.join(properties), file=lineagefile)

    except KeyboardInterrupt as error:
        raise error
    finally:
        if lineagefile:
            lineagefile.close()

    print(f'{time.time() - start} seconds')

    return 0
Exemplo n.º 9
0
    # Params
    N_CLs = 11
    TR = 1.0
    AR = 12.0
    N = 80
    N_sweeps = 20
    CLs = np.linspace(0.1, 0.5, N_CLs)
    sweep_dists = np.zeros((N_CLs, N_sweeps))

    color_range = np.linspace(0, 155, N_CLs)
    colors = [
        "#" + "".join([hex(int(x)).replace('0x', '')] * 3) for x in color_range
    ]

    for i, CL in enumerate(CLs):
        _, _, sweep_dists[i] = optimize(np.zeros(N_sweeps), TR, AR, CL, N,
                                        'L-BFGS-B')

    # Plot sweep distribution
    spans = np.linspace(0.0, 1.0, N_sweeps)
    plt.figure(figsize=(5, 5))
    for i, CL in enumerate(CLs):
        plt.plot(spans,
                 sweep_dists[i],
                 label=str(round(CL, 3)),
                 color=colors[i])
    plt.xlabel("Span Fraction")
    plt.ylabel("Local Sweep Angle [deg]")
    plt.legend(title="$C_L$")
    plt.show()
Exemplo n.º 10
0
def run_experiment(settings_dict):
    experiment_settings = ExperimentSettings(settings_dict)
    # localize to the current computer as required
    experiment_settings.localize()

    # create an empty experiment object, with the correct parametrizations
    experiment_settings.check_stored("parametrization_settings")
    experiment_state = ExperimentState.create(
        experiment_settings.get('parametrization_settings'))
    experiment_settings.save("parametrization_settings")

    # create the data adapter
    experiment_settings.check_stored("data_settings")
    data_adapter = DataAdapterFactory(
        experiment_settings.get('data_settings')['data_type'])(
            experiment_settings.get('local_data_settings'))

    if not experiment_settings.get('data_settings')['lazy_image_loading']:
        device = torch.device(general_settings.device_name)
        image_tensors = [
            observation.get_image()
            for observation in tqdm(data_adapter.images,
                                    desc="Preloading training images")
        ]
        # now compact all observations into a few big tensors and remove the old tensors
        # this makes for much faster access/operations
        data_adapter.compound_image_tensors = {}
        data_adapter.compound_image_tensor_sizes = {}
        training_indices_batches, _ = data_adapter.get_training_info()
        testing_indices_batches, _ = data_adapter.get_testing_info()
        for batch in itertools.chain(training_indices_batches,
                                     testing_indices_batches):
            compound_H = max([image_tensors[i].shape[-2] for i in batch])
            compound_W = max([image_tensors[i].shape[-1] for i in batch])
            C = len(batch)
            compound_images = torch.zeros(C,
                                          3,
                                          compound_H,
                                          compound_W,
                                          dtype=torch.float,
                                          device=device)
            compound_sizes = torch.zeros(C, 2, dtype=torch.long, device=device)
            for idx, image_idx in enumerate(batch):
                src_tensor = image_tensors[image_idx]
                compound_images[idx, :, :src_tensor.shape[-2], :src_tensor.
                                shape[-1]] = src_tensor
                compound_sizes[idx, 0] = src_tensor.shape[-1]
                compound_sizes[idx, 1] = src_tensor.shape[-2]
                del data_adapter.images[image_idx]._image
            data_adapter.compound_image_tensors[batch] = compound_images
            data_adapter.compound_image_tensor_sizes[batch] = compound_sizes
        del image_tensors

    experiment_settings.save("data_settings")

    # initialize the parametrizations with the requested values, if the initialization is not available on disk
    initialization_state_folder = experiment_settings.get_state_folder(
        "initialization")
    if experiment_settings.check_stored("initialization_settings"):
        experiment_state.load(initialization_state_folder)
    else:
        experiment_state.initialize(
            data_adapter,
            experiment_settings.get('local_initialization_settings'))
        experiment_state.save(initialization_state_folder)
        experiment_settings.save("initialization_settings")

    # evaluate_state("initialization",
    #                experiment_settings.get('data_settings')['object_name'],
    #                experiment_settings.get('local_data_settings')['gt_scan_folder'],
    #                experiment_state)
    experiment_state.visualize_statics(experiment_settings.get_output_path(),
                                       data_adapter)

    if experiment_settings.get("higo_baseline") is not None:
        higo_state_folder = experiment_settings.get_state_folder("higo")
        if not experiment_settings.check_stored("higo_baseline"):
            higo_experiment_state = higo_baseline(
                experiment_state, data_adapter, higo_state_folder,
                experiment_settings.get('higo_baseline'))
            higo_experiment_state.visualize(
                experiment_settings.get_output_path(),
                "higo_baseline",
                data_adapter,
                losses=[],
                shadows_occlusions=False)
            higo_experiment_state.save(higo_state_folder)
            experiment_settings.save("higo_baseline")
        else:
            higo_experiment_state = ExperimentState.copy(experiment_state)
            higo_experiment_state.load(higo_state_folder)
        evaluate_state(
            "higo baseline",
            experiment_settings.get('data_settings')['object_name'],
            experiment_settings.get('local_data_settings')['gt_scan_folder'],
            higo_experiment_state)

    optimization_step_settings = experiment_settings.get(
        'default_optimization_settings')
    experiment_settings.check_stored("default_optimization_settings")
    experiment_settings.save("default_optimization_settings")

    for step_index in range(len(
            experiment_settings.get('optimization_steps'))):
        step_state_folder = experiment_settings.get_state_folder(
            "optimization_steps", step_index)

        optimization_settings = experiment_settings.get(
            "optimization_steps", step_index)

        shorthand = experiment_settings.get_shorthand("optimization_steps",
                                                      step_index)
        set_name = "%02d_%s" % (step_index, shorthand)

        if optimization_settings['visualize_initial']:
            experiment_state.visualize(experiment_settings.get_output_path(),
                                       "%02d__initial" % step_index,
                                       data_adapter,
                                       optimization_settings['losses'])

        if experiment_settings.check_stored("optimization_steps", step_index):
            experiment_state.load(step_state_folder)
        else:
            optimize(experiment_state,
                     data_adapter,
                     optimization_settings,
                     output_path_structure=os.path.join(
                         experiment_settings.get_output_path(),
                         "evolution_%%s_%s.png" % set_name))
            experiment_state.save(step_state_folder)
            experiment_settings.save("optimization_steps", step_index)

        if optimization_settings['visualize_results']:
            experiment_state.visualize(experiment_settings.get_output_path(),
                                       set_name, data_adapter,
                                       optimization_settings['losses'])

    evaluate_state(
        experiment_settings.get('data_settings').get('output_path_suffix',
                                                     'proposed'),
        experiment_settings.get('data_settings')['object_name'],
        experiment_settings.get('local_data_settings')['gt_scan_folder'],
        experiment_state)
Exemplo n.º 11
0
import optimization
from constants import *

try:
    optimization.optimize(Works.normal_precision, 3, 4, Lights.B_40_Watts, 2.5,
                          [(1, 1),
                           (2, 2)], 2.3, 1.2, [(0.5, 1)], [], [], [1.5])
except Exception as error:
    print(error)
Exemplo n.º 12
0
    filepath = os.path.join(path, f)
    inFile = TFile.Open(filepath, "READ")
    print " Reading from", f
    tree = inFile.Get("treeA")
    n = name_to_number(f)
    # print n
    no_events[0] = n
    # print no_events[0]
    Ncount = np.squeeze(tree.AsMatrix(columns=["Ncount"], dtype="int"))
    Npos = np.squeeze(tree.AsMatrix(columns=["fNtrackPos"], dtype="int"))
    Nneg = np.squeeze(tree.AsMatrix(columns=["fNtrackNeg"], dtype="int"))
    Sum = Npos + Nneg
    Diff = abs(Npos - Nneg)
    inFile.Close()
    events = Ncount.size
    N_cmin[0], Ncount_opt_wid[0], c = optimize(Ncount, events)

    Npos_cmin[0], Npos_opt_wid[0], c = optimize(Npos, events)
    # print Npos_cmin[0],Npos_opt_wid[0]
    Nneg_cmin[0], Nneg_opt_wid[0], c = optimize(Nneg, events)
    # print Nneg_cmin[0],Nneg_opt_wid[0]
    Sum_cmin[0], Sum_opt_wid[0], c = optimize(Sum, events)
    # print Sum_cmin[0],Sum_opt_wid[0]
    Diff_cmin[0], Diff_opt_wid[0], c = optimize(Diff, events)
    # print Diff_cmin[0],Diff_opt_wid[0]
    treec.Fill()
    treew.Fill()
    # break;

cwfile.Write()
print "--------------------Done-------------------------"
Exemplo n.º 13
0
        ###########################################################################
        ############ E = ∑||Gt(pi+τ(pi))−Gs(pi)||^2################################
        ###########################################################################
        cofficients_g = []
        bbss = []
        for i in range(sample_vertices_or.shape[0]):
            bb = grad2[i]*sample_vertices_or[i] + src_warp_gradient[sample_vertices_or[i,1],sample_vertices_or[i,0]] - dst_warp_gradient[sample_vertices_or[i,1],sample_vertices_or[i,0]]
            bbss.append(bb)
            for w in weight[i]:
                cofficient_g = [w*grad2[i,0],w*grad2[i,1]]
                cofficients_g.append(cofficient_g)
        cofficients_g = np.array(cofficients_g)
        cofficients_g = cofficients_g.reshape(-1,4,2)
        bbss = np.array(bbss)
        # todo three constant parameter means optical,line,vertices keeping respectively
        c = optimization.optimize(triangles, triangle_coefficient, cofficients, location, bs, mesh_boxes_src,
                                  cofficients_g, bbss, 0.25,1,2, weight_lines, location_lines)
        c = c.astype(np.int)
        c = c.reshape(dst_y_num,dst_x_num,2)


        """the offset of texture_mapping bring"""
        offset_x = abs(min(np.min(c[:,:,0]),0))
        offset_y = abs(min(np.min(c[:,:,1]),0))


        # todo get the image after warping
        final_result = texture_mapping.texture_mapping(mesh_boxes_src.astype(np.int), c.astype(np.int),
                                        src_warp)
        final_result = final_result.astype(np.uint8)

        cv2.imshow("final_result" + str(iter), final_result)
Exemplo n.º 14
0
    parser.add_argument('--load', help='Load listings from the supplied xml.', action='store_true')
    args = parser.parse_args()

    if args.buy is True and args.cart is None:
        print('Error, specify --cart')
        exit(1)

    if args.load is True and args.parts is None:
        print('Error, specify --parts')
        exit(1)

    if args.load:
        parts, stores, listings = get_listings(args.parts, args.exclude)
        pickle.dump({'parts': parts, 'stores': stores, 'listings': listings}, open('cache/loaded.p', 'wb'))
    elif args.optimize:
        loaded = pickle.load(open('cache/loaded.p', 'rb'))
        parts = loaded['parts']
        stores = loaded['stores']
        listings = loaded['listings'] 

    if args.optimize:
        parts, listings, stores = pre_optimize(parts, listings, stores, args.min_parts_per_store)
        optimal_listings = optimize(parts, listings, stores, args.shipping_costs)
        pickle.dump({'optimal_listings': optimal_listings}, open('cache/optimized.p', 'wb'))
    elif args.buy:
        optimized = pickle.load(open('cache/optimized.p', 'rb'))
        optimal_listings = optimized['optimal_listings'] 

    if args.buy:
        insert_in_cart(optimal_listings, args.cart)
Exemplo n.º 15
0
import desugaring
import c_generation
import normalization
import optimization
import parsing
import tokenization

source_path = sys.argv[1]

with open(source_path, 'r') as f:
    source = f.read()

tokens = tokenization.tokenize(source)
parsed = parsing.parse(tokens)
desugared = desugaring.desugar(parsed)
normalized = normalization.normalize(desugared)
converted = conversion.convert(normalized)

crossplatform_ir = crossplatform_ir_generation.generate(converted)
optimized = optimization.optimize(crossplatform_ir)
outputted = crossplatform_ir_generation.output(optimized)
print(outputted)

generated = c_generation.generate(optimized)

assert source_path.endswith('.fur')
destination_path = source_path + '.c'

with open(destination_path, 'w') as f:
    f.write(generated)
Exemplo n.º 16
0
def getBestSchedule (classes, option):
    global junk1
    global junk2
    (potentialSchedules, junk1, junk2) = generateAll(classes)
    schedule = optimize(potentialSchedules, heuristicDict[option])
    return schedule
Exemplo n.º 17
0
    global junk1
    global junk2
    (potentialSchedules, junk1, junk2) = generateAll(classes)
    schedule = optimize(potentialSchedules, heuristicDict[option])
    return schedule

# Example of how to use optimize
if __name__ == '__main__':
    # from user
    classes = ["15210", "15281", "84380", "21355", "11411"]
    # then generate the potentialSchedules
    
    (potentialSchedules, junk1, junk2) = generateAll(classes)
    # from user
    chosenHeuristic = 'fridayOff'
    schedule = optimize(potentialSchedules, heuristicDict[chosenHeuristic])
    chosenHeuristic = 'noRemote'
    schedule2 = optimize(potentialSchedules, heuristicDict[chosenHeuristic])
    chosenHeuristic = 'latestTime'
    schedule3 = optimize(potentialSchedules, heuristicDict[chosenHeuristic])
    chosenHeuristic = 'earliestTime'
    schedule4 = optimize(potentialSchedules, heuristicDict[chosenHeuristic])
    
    chosenHeuristic = 'shortestTime'
    schedule5 = optimize(potentialSchedules, heuristicDict[chosenHeuristic])

    infoPrint(schedule)
    infoPrint(schedule2)
    infoPrint(schedule3)
    infoPrint(schedule4)
    infoPrint(schedule5)
Exemplo n.º 18
0


# TACS
def build_tac_input(filename, x):
    '''
    sets up the yaml to feed into the java model to have ITQs running and trading
    :param filename: the name of the yaml to output
    :param x: the numpy array with the parameters. I just expect 2: quota for first and quota for second species
    :return: nothing
    '''
    with open("base.yaml", 'r') as infile:
        data = yaml.load(infile)

    data["Prototype"]["fishers"] = 100
    # change regulations
    data["Prototype"]["regulation"] = dict()
    data["Prototype"]["regulation"]["Multi-TAC"] = {
        "firstSpeciesQuota": float(x[0]),
        "otherSpeciesQuota": float(x[1])
    }
    # you also need to activate the predictors
    data['Prototype']['usePredictors'] = "true"

    with open(filename, 'w') as outfile:
        outfile.write(yaml.dump(data, default_flow_style=True))

title = "mixed_tac_100"
bounds = [(0,2000000),(0,2000000)]
optimization.optimize(title,build_tac_input,bounds)
Exemplo n.º 19
0
    # cofficients_g = cofficients_g*0
    # bbss = bbss*0
    print(">>>>>>>>>>>>>>>>>>>>>>>>>")
    print(triangles.shape)
    print(triangle_coefficient.shape)
    print(cofficients.shape)
    print(location.shape)
    print(bs.shape)
    print(mesh_boxes_src.shape)
    print(cofficients_g.shape)
    print(bbss.shape)
    # cofficients_g = cofficients_g*0
    # bbss = bbss*0
    # triangle_coefficient = triangle_coefficient*0
    c = optimization.optimize(triangles, triangle_coefficient, cofficients,
                              location, bs, mesh_boxes_src, cofficients_g,
                              bbss, 0.16)
    c = c.astype(np.int)
    c = c.reshape(dst_y_num, dst_x_num, 2)
    # c = c[:,:,(1,0)]
    # ccc = copy.deepcopy(c)
    # ccc = ccc.reshape(-1,2)
    # for i in range(ccc.shape[0]):
    #     ccc[i] = ccc[i]+[112,114]
    """the offset of texture_mapping bring"""
    offset_x = abs(min(np.min(c[:, :, 0]), 0))
    offset_y = abs(min(np.min(c[:, :, 1]), 0))

    # todo get the image after warping
    final_result = texture_mapping.texture_mapping(
        mesh_boxes_src.astype(np.int), c.astype(np.int), src_warp)
Exemplo n.º 20
0
def trial(**kwargs):
    # program tracker vars
    simtime = 0
    opttime = 0
    btime = 0
    condtime = 0
    itercount = 0
    final = dict()

    # problem specific vars/function related
    init_x = .5
    init_a = 0
    init_b = 1
    constraint = 3
    y0 = 65
    y1 = 97

    # bayesian discrete probability structure
    pdf = [0] * (y1 + 1)
    for i in range(y0, y1 + 1):
        pdf[i] = 1./(y1 - y0 + 1)

    # bisection search history data structure
    biobs = list()

    # golden search history data structure
    obs = dict()  # maps Point objects to a list of observations at that Point

    # THE FOLLOWING ARE ALL HYPERPARAMETERS
    # in common:
    sim_n = 100
    sim_n = kwargs['sim_n']

    # golden search
    # these work for good precision
    alpha = .9
    alpha = kwargs['alpha']
    gs_epsilon = .001
    gs_epsilon = kwargs['gs_epsilon']
    delta = .01
    delta = kwargs['delta']

    # gradient
    sim_grad_n = 5000
    sim_grad_n = kwargs['sim_grad_n']
    grad_epsilon = .01
    grad_epsilon = kwargs['grad_epsilon']
    stepsize = .1
    stepsize = kwargs['stepsize']

    # bisect
    b_error = .1
    b_error = kwargs['b_error']

    # bayesian
    oracle = .6
    oracle = kwargs['oracle']
    certainty = .99
    certainty = kwargs['certainty']

    # control logic vars
    u_method = 'golden'
    u_method = 'gradient'
    u_method = kwargs['u_method']
    b_method = 'bayes'
    b_method = 'bisect'
    b_method = kwargs['b_method']

    # END HYPERPARAMETERS

    # initializing vars
    outer_stop = False
    left = y0
    right = y1
    error = b_error
    cpustart = timeit.default_timer()
    itercount = 0
    old_y = 0
    bayescount = 0
    while not outer_stop:
        if b_method is 'bayes':
            y = find_median(y0, y1, pdf)
            if y == old_y:
                bayescount += 1
            else:
                bayescount = 0
            old_y = y
        if b_method is 'bisect':
            alpha = 1 - error
            y = (left + right)/2
        x = init_x
        a = init_a
        b = init_b
        stop = False
        while not stop:
            itercount += 1
            if u_method is 'gradient':
                optresult = optimize(u_method=u_method,
                                     obs=obs,
                                     stepsize=stepsize,
                                     constraint=constraint,
                                     x=x,
                                     y=y,
                                     sim_n=sim_n,
                                     sim_grad_n=sim_grad_n)
                x = optresult['x']
                cond = checkCond(u_method=u_method,
                                 constraint=constraint,
                                 epsilon=grad_epsilon,
                                 f=optresult['f'],
                                 gradient=optresult['gradient'])
                opttime += optresult['opttime']
                simtime += optresult['simtime']

            if u_method is 'golden':
                optresult = optimize(u_method=u_method,
                                     obs=obs,
                                     alpha=alpha,
                                     epsilon=gs_epsilon,
                                     sim_n=sim_n,
                                     a=a,
                                     b=b,
                                     y=y)
                a = optresult['a']
                b = optresult['b']
                f_c = optresult['f_c']
                f_d = optresult['f_d']
                opttime += optresult['opttime']
                simtime += optresult['simtime']
                cond = checkCond(u_method=u_method,
                                 a=a,
                                 b=b,
                                 delta=delta,
                                 constraint=constraint,
                                 f_c=f_c,
                                 f_d=f_d)
            stop = cond['stop']
            condtime += cond['time']

        if u_method is 'golden':
            print str(y) + ": " + str(f_c) + " " + str(f_d) + " " + str(a) + " " + str(b)
        if u_method is 'gradient':
            print str(y) + ": " + str(optresult['f']) + " " + str(x)

        print cond['result']
        # ***

        if b_method is 'bayes':
            # update priors
            if cond['result']:
                for i in range(y0, y):
                    pdf[i] *= (1 - oracle)
                for i in range(y, y1 + 1):
                    pdf[i] *= oracle
            else:
                for i in range(y0, y + 1):
                    pdf[i] *= oracle
                for i in range(y + 1, y1 + 1):
                    pdf[i] *= (1-oracle)
            # rescale
            scalefactor = 1./sum(pdf)
            for i in range(y0, y1 + 1):
                pdf[i] *= scalefactor
#            print max(pdf)
            if max(pdf) > certainty or bayescount > 25:
                outer_stop = True
                final['y'] = pdf.index(max(pdf))

        # ***

        if b_method is 'bisect':
            biobs.append(dict(left=left, y=y, right=right, result=cond['result'], error=error))
            # check for backtracking here
            # 1. identify the current T/F streak length
            streaklen = 0
            for i in range(1, len(biobs) + 1):
                if biobs[-i]['result'] == cond['result']:
                    streaklen += 1
                else:
                    break
            root = max(0, len(biobs) - streaklen)  # possible mistake index
            # 2. compare pcs/confidence for this streak with the undo probability
            backtrack = biobs[root]['error'] > ((.5 ** (streaklen - 1)) * b_error)
#            print "backtrack: " + str(backtrack)
            if backtrack:
                left = biobs[root]['left']
                right = biobs[root]['right']
                error = biobs[root]['error'] / 2
                # prune the list back to before the mistake; drop the items
                # GS values are still cached in the obs data structure
                for i in range(len(biobs) - root):
                    del biobs[-1]
            else:
                error = b_error
                if cond['result']:
                    # discard left
                    left = y
                else:
                    right = y
                if right - left == 1:
                    outer_stop = True
                    final['y'] = left
    btime = timeit.default_timer() - cpustart
    btime = btime - simtime - opttime - condtime
    final['itercount'] = itercount
    final['simtime'] = simtime
    final['optime'] = opttime
    final['condtime'] = condtime
    final['btime'] = btime
    return final
Exemplo n.º 21
0
def main(args):
    """Main function of cellanneal."""
    if (args.start_temp is not None or args.end_temp is not None) and args.auto_temp == 1:
        raise Exception("when auto_temp is set to 1(default value), starting temperature or ending temperature should not be set manually")

    # if not args.no_parallel:
    #     import dask
    #     from dask.distributed import Client, LocalCluster
    #     if not args.cluster:
    #         cluster = LocalCluster(
    #             n_workers=args.workers,local_dir="/tmp/CellUniverse/dask-worker-space"
    #         )
    #     else:
    #         cluster = args.cluster
    #
    #     client = Client(cluster)
    # else:
    client = None

    lineagefile = None
    start = time.time()

    try:
        config = load_config(args.config)
        
        simulation_config = config["simulation"]
        #Maybe better to store the image type in the config file in the first place, instead of using cmd?
        if args.graySynthetic == True:
            simulation_config["image.type"] = "graySynthetic"
        elif args.phaseContrast == True:
            simulation_config["image.type"] = "phaseContrastImage"
        elif args.binary == True:
            simulation_config["image.type"] = "binary"
        else:
            raise ValueError("Invalid Command: Synthetic image type must be specified")
        
        if not args.output.is_dir():
            args.output.mkdir()
        if not args.bestfit.is_dir():
            args.bestfit.mkdir()
        if args.residual and not args.residual.is_dir():
            args.residual.mkdir()
            
        seed = int(start * 1000) % (2**32)
        if args.seed != None:
            seed = args.seed
        np.random.seed(seed)
        print("Seed: {}".format(seed))
        
        
        celltype = config['global.cellType'].lower()

        # setup the colony from a file with the initial properties
        lineageframes = LineageFrames()
        colony = lineageframes.forward()
        imagefiles =  get_inputfiles(args)
        if args.lineage_file:
            load_colony(colony, args.lineage_file, config, initial_frame = imagefiles[0].name)
        else:
            load_colony(colony, args.initial, config)
        cost_diff = (-1, -1)

        # open the lineage file for writing
        lineagefile = open(args.output/'lineage.csv', 'w')
        header = ['file', 'name']
        if celltype == 'bacilli':
            header.extend(['x', 'y', 'width', 'length', 'rotation', "split_alpha", "opacity"])
        print(','.join(header), file=lineagefile)

        if args.debug:
            with open(args.debug/'debug.csv', 'w') as debugfile:
                print(','.join(['window_start', 'window_end', 'pbad_total', 'bad_count', 'temperature', 'total_cost_diff', 'current_iteration', 'total_iterations']), file=debugfile)


        if args.global_optimization:
            global useDistanceObjective
            
            useDistanceObjective = args.dist
            realimages = [optimization.load_image(imagefile) for imagefile in imagefiles]
            window = config["global_optimizer.window_size"]
            if args.lineage_file:
                lineage = global_optimization.build_initial_lineage(imagefiles, args.lineage_file, args.continue_from, config["simulation"])
            else:
                lineage = global_optimization.build_initial_lineage(imagefiles, args.initial, args.continue_from, config["simulation"])
            lineage = global_optimization.find_optimal_simulation_confs(imagefiles, lineage, realimages, args.continue_from)
            sim_start = args.continue_from - args.frame_first
            print(sim_start)
            shape = realimages[0].shape
            synthimages = []
            cellmaps = []
            distmaps = []
            iteration_per_cell = config["iteration_per_cell"]
            if not useDistanceObjective:
                distmaps = [None] * len(realimages)
            for window_start in range(1 - window, len(realimages)):
                 window_end = window_start + window
                 print(window_start, window_end)
                 if window_end <= len(realimages):
                        # get initial estimate
                    if window_start >= sim_start:
                        if window_end > 1:
                            lineage.copy_forward()
                    realimage = realimages[window_end - 1]
                    synthimage, cellmap = optimization.generate_synthetic_image(lineage.frames[window_end - 1].nodes, shape, lineage.frames[window_end - 1].simulation_config)
                    synthimages.append(synthimage)
                    cellmaps.append(cellmap)
                    if useDistanceObjective:
                        distmap = distance_transform_edt(realimage < .5)
                        distmap /= config[f'{config["global.cellType"].lower()}.distanceCostDivisor'] * config[
                            'global.pixelsPerMicron']
                        distmap += 1
                        distmaps.append(distmap)
                    if args.auto_temp == 1 and window_end == 1:
                        print("auto temperature schedule started")
                        args.start_temp, args.end_temp = \
                            global_optimization.auto_temp_schedule(imagefiles, lineage, realimages, synthimages, cellmaps, distmaps, 0, 1, lineagefile, args, config)
                        print("auto temperature schedule finished")
                        print("starting temperature is ", args.start_temp, "ending temperature is ", args.end_temp)
                    if args.auto_meth == "frame" and optimization.auto_temp_schedule_frame(window_end, 3):
                        print("auto temperature schedule restarted")
                        args.start_temp, args.end_temp = \
                            global_optimization.auto_temp_schedule(imagefiles, lineage, realimages, synthimages, cellmaps, distmaps, window_start, window_end, lineagefile, args, config)
                        print("auto temperature schedule finished")
                        print("starting temperature is ", args.start_temp, "ending temperature is ", args.end_temp)
                 if window_start >= sim_start:
                    if useDistanceObjective:
                        global_optimization.totalCostDiff = optimization.dist_objective(realimage, synthimage, distmap, cellmap, config["overlap.cost"])
                    else:
                        global_optimization.totalCostDiff = optimization.objective(realimage, synthimage, cellmap, config["overlap.cost"], config["cell.importance"])
                    global_optimization.optimize(imagefiles, lineage, realimages, synthimages, cellmaps, distmaps, window_start, window_end, lineagefile, args, config, iteration_per_cell)
                 if window_start >= 0:
                    global_optimization.save_lineage(imagefiles[window_start].name, lineage.frames[window_start].nodes, lineagefile)
                    global_optimization.save_output(imagefiles[window_start].name, synthimages[window_start], realimages[window_start], lineage.frames[window_start].nodes, args, config)
            return 0
        
        config["simulation"] = optimization.find_optimal_simulation_conf(config["simulation"], optimization.load_image(imagefiles[0]), list(colony))
        if args.auto_temp == 1:
            print("auto temperature schedule started")
            args.start_temp, args.end_temp = optimization.auto_temp_schedule(imagefiles[0], lineageframes.forward(), args, config)
            print("auto temperature schedule finished")
            print("starting temperature is ", args.start_temp, "ending temperature is ", args.end_temp)

        frame_num = 0
        prev_cell_num = len(colony)
        for imagefile in imagefiles: # Recomputing temperature when needed

            frame_num += 1

            if args.auto_meth == "frame":
                if optimization.auto_temp_schedule_frame(frame_num, 8):
                    print("auto temperature schedule started (recomputed)")
                    args.start_temp, args.end_temp = optimization.auto_temp_schedule(imagefile, colony, args, config)
                    print("auto temperature schedule finished")
                    print("starting temperature is ", args.start_temp, "ending temperature is ", args.end_temp)

            elif args.auto_meth == "factor":
                if optimization.auto_temp_schedule_factor(len(colony), prev_cell_num, 1.1):
                    print("auto temperature schedule started (recomputed)")
                    args.start_temp, args.end_temp = optimization.auto_temp_schedule(imagefile, colony, args, config)
                    print("auto temperature schedule finished")
                    print("starting temperature is ", args.start_temp, "ending temperature is ", args.end_temp)
                    prev_cell_num = len(colony)

            elif args.auto_meth == "const":
                if optimization.auto_temp_schedule_const(len(colony), prev_cell_num, 10):
                    print("auto temperature schedule started (recomputed)")
                    args.start_temp, args.end_temp = optimization.auto_temp_schedule(imagefile, colony, args, config)
                    print("auto temperature schedule finished")
                    print("starting temperature is ", args.start_temp, "ending temperature is ", args.end_temp)
                    prev_cell_num = len(colony)

            elif args.auto_meth == "cost":
                print(cost_diff, frame_num, optimization.auto_temp_shcedule_cost(cost_diff))
                if frame_num >= 2 and optimization.auto_temp_shcedule_cost(cost_diff):
                    print("auto temperature schedule started cost_diff (recomputed)")
                    args.start_temp, args.end_temp = optimization.auto_temp_schedule(imagefile, colony, args, config)
                    print("auto temperature schedule finished")
                    print("starting temperature is ", args.start_temp, "ending temperature is ", args.end_temp)

            colony = optimize(imagefile, lineageframes, args, config, client)

            cost_diff = optimization.update_cost_diff(colony, cost_diff)

            # flatten modifications and save cell properties

            colony.flatten()
            for cellnode in colony:
                properties = [imagefile.name, cellnode.cell.name]
                if celltype == 'bacilli':                                   
                    properties.extend([
                        str(cellnode.cell.x),
                        str(cellnode.cell.y),
                        str(cellnode.cell.width),
                        str(cellnode.cell.length),
                        str(cellnode.cell.rotation)])
                print(','.join(properties), file=lineagefile)

    except KeyboardInterrupt as error:
        raise error
    finally:
        if lineagefile:
            lineagefile.close()

    print(f'{time.time() - start} seconds')

    return 0
Exemplo n.º 22
0
def main(args):
    """Main function of cellanneal."""

    import dask
    from dask.distributed import Client, LocalCluster
    if not args.cluster:
        cluster = LocalCluster(n_workers=args.workers, )
    else:
        cluster = args.cluster

    client = Client(cluster)

    lineagefile = None
    start = time.time()

    try:
        if args.debug:
            debugmode = True

        config = load_config(args.config)
        celltype = config['global.cellType'].lower()

        # setup the colony from a file with the initial properties
        lineageframes = LineageFrames()
        colony = lineageframes.forward()
        load_colony(colony, args.initial, config)

        # open the lineage file for writing
        lineagefile = open(args.output / 'lineage.csv', 'w')
        header = ['file', 'name']
        if celltype == 'bacilli':
            header.extend(['x', 'y', 'width', 'length', 'rotation'])
        print(','.join(header), file=lineagefile)

        for imagefile in get_inputfiles(args):

            colony = optimize(imagefile, lineageframes, args, config, client)

            # flatten modifications and save cell properties
            colony.flatten()
            for cellnode in colony:
                properties = [imagefile.name, cellnode.cell.name]
                if celltype == 'bacilli':
                    properties.extend([
                        str(cellnode.cell.x),
                        str(cellnode.cell.y),
                        str(cellnode.cell.width),
                        str(cellnode.cell.length),
                        str(cellnode.cell.rotation)
                    ])
                print(','.join(properties), file=lineagefile)

    except KeyboardInterrupt as error:
        raise error
    finally:
        if lineagefile:
            lineagefile.close()

    print(f'{time.time() - start} seconds')

    return 0