Exemplo n.º 1
0
def check_one_invalid_parameter(param_and_value):
    filename = 'test_invalid_params.params'
    file_with_params = open(filename, 'w')
    file_with_params.write(param_and_value + '\n')
    file_with_params.close()
    with pytest.raises(ValueError) as excinfo:
        parameters.parse_parameters_from_file(filename)
    assert str(excinfo.value) == ('File {filename} does not contain any valid'
                                  ' parameter values'.format(filename=filename))
    os.remove(filename)
Exemplo n.º 2
0
def main(filename, output_format='xlsx', output_dir='', sheet_name_usr=''):
    ''' Main function to run DEA models from terminal.

        Args:
            filename (str): path to file with parameters.
            output_format (str, optional): file format of solution file.
                This value is used
                only if OUTPUT_FILE in parameters is empty or set to auto.
                Defaults to xlsx.
            output_dir (str, optional): directory where solution must
                be written.
                If it is not given, solution will be written to current folder.
                This value is used
                only if OUTPUT_FILE in parameters is empty or set to auto.
            sheet_name_usr (str, optional): name of the sheet in xls- or
                xlsx-file with
                input data from which data will be read. If input data file is
                in csv format,
                this value is ignored.

    '''
    print('Params file', filename, 'output_format', output_format,
          'output_dir', output_dir, 'sheet_name_usr', sheet_name_usr)
    
    logger = get_logger()
    logger.info('Params file "%s", output format "%s", output directory "%s", sheet name "%s".',
                filename, output_format, output_dir, sheet_name_usr)

    params = parse_parameters_from_file(filename)
    params.print_all_parameters()
    run_method = RunMethodTerminal(params, sheet_name_usr, output_format,
                                   output_dir)
    run_method.run(params)
    clean_up_pickled_files()
    logger.info('pyDEA exited.')
Exemplo n.º 3
0
    def load_file(self):
        ''' Loads parameters from file specified by the user.
        '''
        file_name = self._get_filename_for_load()
        if file_name:

            self.str_var_for_input_output_boxes.input_categories.clear()
            self.str_var_for_input_output_boxes.output_categories.clear()

            # save previous params
            params_to_restore = dict()
            for param_name in CATEGORICAL_AND_DATA_FIELDS:
                params_to_restore[
                    param_name] = self.params.get_parameter_value(param_name)
            self.params.copy_all_params(parse_parameters_from_file(file_name))

            if self.load_without_data.get() == 0:
                self.load_data_file_and_related_params(file_name,
                                                       params_to_restore)
            else:
                self.data_from_params_file.set('')
                # restore previous parameters
                for param_name, value in params_to_restore.items():
                    self.params.update_parameter(param_name, value)
            self.options_frame.set_params_values()
Exemplo n.º 4
0
def test_file_read_comments():
    filename = 'test_params_with_comments.params'
    file_with_params = open(filename, 'w')
    file_with_params.write('#<tricky> {one}\n')
    file_with_params.write('<DATA_FILE>{value1}#<tricky> {one}\n')
    file_with_params.write('<DEA_FORM> {value2} #<tricky> {one}\n')
    file_with_params.write('   <INPUT_CATEGORIES>     \t {long one with spaces} \t'
                           ' # <tricky> {one}\n')
    file_with_params.write('\n')
    file_with_params.write('#<param1>{value1}#<tricky> {one}\n')
    file_with_params.write('<OUTPUT_CATEGORIES>{aha} #\n')
    file_with_params.write('<RETURN_TO_SCALE>{} #\n')
    file_with_params.write('#\n')
    file_with_params.close()
    p = parameters.parse_parameters_from_file(filename)
    # assert len(p.params) == 5
    assert 'DATA_FILE' in p.params
    assert p.params['DATA_FILE'] == 'value1'
    assert 'DEA_FORM' in p.params
    assert p.params['DEA_FORM'] == 'value2'
    assert 'INPUT_CATEGORIES' in p.params
    assert p.params['INPUT_CATEGORIES'] == 'long one with spaces'
    assert 'OUTPUT_CATEGORIES' in p.params
    assert p.params['OUTPUT_CATEGORIES'] == 'aha'
    assert p.params['RETURN_TO_SCALE'] == ''

    os.remove(filename)
Exemplo n.º 5
0
def test_main_correct_params():
    filename = 'tests/params_new_format.txt'
    params = parse_parameters_from_file(filename)
    auto_name = auto_name_if_needed(params, 'xlsx')
    main(filename)
    assert os.path.exists(auto_name) is True
    os.remove(auto_name)
Exemplo n.º 6
0
def test_main_csv():
    filename = 'tests/params_to_test_main_csv.txt'
    params = parse_parameters_from_file(filename)
    auto_name = auto_name_if_needed(params, 'xlsx')
    main(filename, sheet_name_usr='******')
    assert os.path.exists(auto_name) is True
    os.remove(auto_name)
Exemplo n.º 7
0
def _construct_params_and_input():
    filename = 'tests/params_new_format.txt'
    params = parse_parameters_from_file(filename)
    categories, data, dmu_name, sheet_name = read_data(
        params.get_parameter_value('DATA_FILE'))
    coefficients, has_same_dmus = convert_to_dictionary(data)
    model_input = construct_input_data_instance(categories, coefficients)
    return params, model_input
Exemplo n.º 8
0
def _construct_params_and_input():
    filename = 'tests/params_new_format.txt'
    params = parse_parameters_from_file(filename)
    categories, data, dmu_name, sheet_name = read_data(
        params.get_parameter_value('DATA_FILE'))
    coefficients, has_same_dmus = convert_to_dictionary(data)
    model_input = construct_input_data_instance(categories, coefficients)
    return params, model_input
Exemplo n.º 9
0
def test_main_output_dir():
    filename = 'tests/params_new_format.txt'
    params = parse_parameters_from_file(filename)
    auto_name = auto_name_if_needed(params, 'xls')
    output_dir = 'tests'
    main(filename, output_format='xls', output_dir=output_dir)
    output_name = os.path.join(output_dir, auto_name)
    assert os.path.exists(output_name) is True
    os.remove(output_name)
Exemplo n.º 10
0
def test_file_write_and_read_valid_parameters():
    p = parameters.Parameters()
    p.update_parameter('DEA_FORM', 'value1')
    p.update_parameter('INPUT_CATEGORIES', 'some long value')
    p.update_parameter('OUTPUT_CATEGORIES', 'some/linux/type/path/file.txt')
    p.update_parameter('DATA_FILE', '\some\Windows\like\path\p.txt')
    filename = 'test_valid_params.params'
    parameters.write_parameters_to_file(p, filename)
    paramsFromFile = parameters.parse_parameters_from_file(filename)
    for key, value in p.params.items():
        assert key in paramsFromFile.params
        assert paramsFromFile.params[key] == value
    os.remove(filename)
Exemplo n.º 11
0
def test_peel_the_onion_unbounded():
    params = parse_parameters_from_file('tests/params_for_linux.txt')

    categories, xls_data, dmu_name, sheet_name = read_data(
        params.get_parameter_value('DATA_FILE'))
    coefficients, has_same_dmus = convert_to_dictionary(xls_data)
    assert has_same_dmus is False
    assert validate_data(categories, coefficients) is True
    model_input = construct_input_data_instance(categories, coefficients)

    model_factory.add_input_and_output_categories(params, model_input)

    model = model_factory.create_model(params, model_input)

    ranks = None
    model_solution, ranks, state = peel_the_onion_method(model)
    assert state is True
    clean_up_pickled_files()
Exemplo n.º 12
0
def test_peel_the_onion_unbounded():
    params = parse_parameters_from_file('tests/params_for_linux.txt')

    categories, xls_data, dmu_name, sheet_name = read_data(
        params.get_parameter_value('DATA_FILE'))
    coefficients, has_same_dmus = convert_to_dictionary(xls_data)
    assert has_same_dmus is False
    assert validate_data(categories, coefficients) is True
    model_input = construct_input_data_instance(categories, coefficients)

    model_factory.add_input_and_output_categories(params, model_input)

    model = model_factory.create_model(params, model_input)

    ranks = None
    model_solution, ranks, state = peel_the_onion_method(model)
    assert state is True
    clean_up_pickled_files()
def test_abs_restrictions_env_model_output(data):
    filename = 'tests/DEA_Harish_parameters.txt'
    params = parse_parameters_from_file(filename)
    categories, data, dmu_name, sheet_name = read_data(
        params.get_parameter_value('DATA_FILE'))
    coefficients, has_same_dmus = convert_to_dictionary(data)
    model_input = construct_input_data_instance(categories, coefficients)
    models, all_params = build_models(params, model_input)
    assert len(models) == 1 and len(all_params) == 1
    model = models[0]

    start_time = datetime.datetime.now()
    model_solution = model.run()
    end_time = datetime.datetime.now()
    bounds = {'Urban Roads (%)': (None, 0.003)}
    utils_for_tests.check_if_category_is_within_abs_limits(
        model_solution, bounds)

    work_book = Workbook()
    writer = XLSWriter(Parameters(), work_book, datetime.datetime.today(),
                       (end_time - start_time).total_seconds())
    writer.write_data(model_solution)
    work_book.save('tests/test_abs_constraints_env_outoriented_output.xls')
def test_VRS_from_params_file():
    params = parse_parameters_from_file('tests/params_new_format.txt')

    categories, xls_data, dmu_name, sheet_name = read_data(
        params.get_parameter_value('DATA_FILE'))
    coefficients, has_same_dmus = convert_to_dictionary(xls_data)
    assert has_same_dmus is False
    assert validate_data(categories, coefficients) is True
    model_input = construct_input_data_instance(categories, coefficients)

    model_factory.add_input_and_output_categories(params, model_input)

    model = model_factory.create_model(params, model_input)
    model_solution = model.run()
    utils_for_tests.check_optimal_solution_status_and_sizes(
        model_solution, model_input)
    dmus = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K']
    utils_for_tests.check_efficiency_scores(dmus, [1, 0.86998617, 1, 0.95561335,
                                                   0.85,
                                                   1, 1, 0.84507042, 1,
                                                   0.524, 0.89058524],
                                            model_solution, model_input)
    clean_up_pickled_files()
def test_abs_restrictions_env_model_output(data):
    filename = 'tests/DEA_Harish_parameters.txt'
    params = parse_parameters_from_file(filename)
    categories, data, dmu_name, sheet_name = read_data(
        params.get_parameter_value('DATA_FILE'))
    coefficients, has_same_dmus = convert_to_dictionary(data)
    model_input = construct_input_data_instance(categories, coefficients)
    models, all_params = build_models(params, model_input)
    assert len(models) == 1 and len(all_params) == 1
    model = models[0]

    start_time = datetime.datetime.now()
    model_solution = model.run()
    end_time = datetime.datetime.now()
    bounds = {'Urban Roads (%)': (None, 0.003)}
    utils_for_tests.check_if_category_is_within_abs_limits(
        model_solution, bounds)

    work_book = xlwt.Workbook()
    writer = XLSWriter(Parameters(), work_book, datetime.datetime.today(),
                       (end_time - start_time).total_seconds())
    writer.write_data(model_solution)
    work_book.save('tests/test_abs_constraints_env_outoriented_output.xls')