def test_init():
    temp1 = ProblemTemplate(name="test1", template="template")
    assert temp1.name == "test1"
    assert temp1.template == "template"
    assert temp1.params == []

    temp2 = ProblemTemplate(name="test2", template="template", params="params")
    assert temp2.name == "test2"
    assert temp2.template == "template"
    assert temp2.params == "params"
    return
def test_str(mock_read_csv, mock_get_template, fullTemplate1, emptyTemplate,
             paramsDf, template):
    mock_get_template.return_value = template
    mock_read_csv.return_value = paramsDf
    newTemplate = ProblemTemplate.from_file("templateName",
                                            "params",
                                            name="test_problem")
    assert str(newTemplate) == fullTemplate1

    mock_read_csv.return_value = []
    newnewTemplate = ProblemTemplate.from_file("templateName2",
                                               "params2",
                                               name="test_problem")
    assert str(newnewTemplate) == emptyTemplate
    return
def test_read_template(mock_get_template, template):
    mock_get_template.return_value = template

    templ = ProblemTemplate(template="template")
    templ.read_template("someDir/templ")
    assert templ.template == template

    mock_get_template.side_effect = Exception()
    templ = ProblemTemplate(template="template")
    templ.read_template("someDir/othertempl")
    assert templ.template == None
    return
def test_read_param_csv(mock_read_csv, paramsDf, paramsList):
    mock_read_csv.return_value = paramsDf
    templ = ProblemTemplate("templateFile", "paramFile", "test_problem")
    templ.read_param_csv("paramFile", overwrite=True)
    assert templ.params == paramsList

    templ.read_param_csv("paramFile", overwrite=False)
    assert templ.params == paramsList + paramsList

    # Test exception
    mock_read_csv.side_effect = Exception()
    templ.read_param_csv("paramFile", overwrite=True)
    assert templ.params == []
    return
def test_from_file(mock_read_csv, mock_get_template, paramsDf, template,
                   paramsList):
    # Set up mock return values with fixtures.
    mock_get_template.return_value = template
    mock_read_csv.return_value = paramsDf

    templ = ProblemTemplate.from_file("templateName",
                                      "params",
                                      name="test_problem")
    assert templ.name == "test_problem"
    assert templ.template == template
    assert templ.params == paramsList
    return
def test_read(mock_get_template, mock_read_csv, template, paramsDf,
              paramsList):
    mock_get_template.return_value = template
    mock_read_csv.return_value = paramsDf

    templ = ProblemTemplate("template", "params", "test_problem")
    templ.read("templatefile", "paramfile", True)
    assert templ.params == paramsList
    assert templ.template == template

    templ.read("templatefile", "paramfile", False)
    assert templ.params == paramsList + paramsList
    assert templ.template == template
def test_write_to_dir(mock_read_csv, mock_get_template, template, paramsDf,
                      fullTemplate1, fullTemplate2):
    mock_get_template.return_value = template
    mock_read_csv.return_value = paramsDf

    m = mock_open()
    # with patch('cvxbenchmarks.problem_generator.open', m, create=True):
    with patch('cvxbenchmarks.problem_generator.open', m):
        templ = ProblemTemplate.from_file("templateName",
                                          "params",
                                          name="test_problem")
        templ.write_to_dir("problemDir")
        expected_calls = [
            call("problemDir/test_problem_p1.py", "w"),
            call().__enter__(),
            call().write(fullTemplate1),
            call().__exit__(None, None, None),
            call("problemDir/test_problem_p2.py", "w"),
            call().__enter__(),
            call().write(fullTemplate2),
            call().__exit__(None, None, None)
        ]
        assert expected_calls == m.mock_calls
Пример #8
0
def main(args):
    all_template_files = [
        ("least_squares.j2", "least_squares_params.csv"),
        ("lasso.j2", "lasso_params.csv"),
        ("control.j2", "control_params.csv"),
        # ("portfolio_opt.j2", "portfolio_opt_params.csv"),
        ("lp.j2", "lp_params.csv"),
        ("basis_pursuit.j2", "basis_pursuit_params.csv"),
        ("chebyshev.j2", "chebyshev_params.csv"),
        ("chebyshev_epigraph.j2", "chebyshev_params.csv"),
        ("covsel.j2", "covsel_params.csv"),
        ("fused_lasso.j2", "fused_lasso_params.csv"),
        ("hinge_l1.j2", "hinge_l1_params.csv"),
        ("hinge_l2.j2", "hinge_l2_params.csv"),
        ("huber.j2", "huber_params.csv"),
        ("infinite_push.j2", "infinite_push_params.csv"),
        ("least_abs_dev.j2", "least_abs_dev_params.csv"),
        ("logreg_l1.j2", "logreg_l1_params.csv"),
        ("max_gaussian.j2", "max_gaussian_params.csv"),
        ("max_gaussian_epigraph.j2", "max_gaussian_params.csv"),
        ("max_softmax.j2", "max_softmax_params.csv"),
        ("max_softmax_epigraph.j2", "max_softmax_params.csv"),
        ("oneclass_svm.j2", "oneclass_svm_params.csv"),
        ("oneclass_svm_epigraph.j2", "oneclass_svm_params.csv"),
        ("oneclass_svm_original.j2", "oneclass_svm_params.csv"),
        ("portfolio.j2", "portfolio_params.csv"),
        ("qp.j2", "qp_params.csv"),
        ("quantile.j2", "quantile_params.csv"),
        ("robust_pca.j2", "robust_pca_params.csv"),
        ("robust_svm.j2", "robust_svm_params.csv"),
        ("robust_svm_epigraph.j2", "robust_svm_params.csv"),
        ("tv_1d.j2", "tv_1d_params.csv")
    ]
    # print(args)
    templateDir = args.templateDir
    paramDir = args.paramDir
    problemDir = args.problemDir
    use_index = args.use_index
    print("Loading templates from: {}".format(templateDir))
    print("Loading parameters from: {}".format(paramDir))
    print("Target directory: {}".format(problemDir))

    if args.templates is not None and args.params is not None:
        if len(args.templates) == len(args.params):
            template_files = zip(args.templates, args.params)
        else:
            raise RuntimeError(("Argument lists for --templates and --params " + 
                               "should be the same length."))
    elif (args.templates is None and args.params is not None) or \
         (args.templates is not None and args.params is None):
         raise RuntimeError(("--templates and --params must be specified together."))
    else:
        template_files = all_template_files

    # Collect problems from template files (1 per template)
    for templateFile, paramFile in template_files:
        # Remove file extension
        # https://stackoverflow.com/questions/4250364/
        #   how-to-trim-a-file-extension-from-a-string-in-javascript
        paramFileFull = os.path.join(paramDir, paramFile)
        templateFileFull = os.path.join(templateDir, templateFile)
        templ = ProblemTemplate.from_file(templateFileFull, 
                                   paramFileFull, 
                                   re.sub(r"\.[^/.]+$", "", templateFile))
        templ.write_to_dir(problemDir)
        print("Finished writing {} and {} to {}".format(templateFile,
                                             paramFile,
                                             problemDir))

    # Generate index file
    index = Index(problemDir)
    index.write()