def test_abs_weight_restrictions_multiplier_model(data):

    base_model = MultiplierModelBase(data, 0, MultiplierInputOrientedModel())

    bounds = {'I2': (0.01, 0.5)}
    model = MultiplierModelWithAbsoluteWeightRestrictions(base_model, bounds)
    start_time = datetime.datetime.now()
    model_solution = model.run()
    end_time = datetime.datetime.now()
    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_weights_multi_output.xls')

    bounds = {'I2': (None, 0.05)}
    model = MultiplierModelWithAbsoluteWeightRestrictions(base_model, bounds)

    start_time = datetime.datetime.now()
    model_solution = model.run()
    end_time = datetime.datetime.now()

    utils_for_tests.check_if_category_is_within_abs_limits(
        model_solution, bounds)

    work_book2 = Workbook()
    writer = XLSWriter(Parameters(), work_book2, datetime.datetime.today(),
                       (end_time - start_time).total_seconds())
    writer.write_data(model_solution)
    work_book2.save('tests/test_abs_weights_upper_bound_multi_output.xls')
Ejemplo n.º 2
0
def test_create_params_str():
    params = Parameters()
    params.update_parameter('ORIENTATION', 'input')
    params.update_parameter('RETURN_TO_SCALE', 'VRS')
    assert dea_utils.create_params_str(params) == 'input orientation, VRS'
    params.update_parameter('RETURN_TO_SCALE', 'CRS')
    assert dea_utils.create_params_str(params) == 'input orientation, CRS'
Ejemplo n.º 3
0
def test_auto_name_if_needed():
    params = Parameters()
    file_name = 'out.txt'
    params.update_parameter('OUTPUT_FILE', file_name)
    assert dea_utils.auto_name_if_needed(params, 'txt') == file_name
    params.update_parameter('OUTPUT_FILE', '')
    data_file_name = 'dataFileForAutoName.xls'
    params.update_parameter('DATA_FILE', data_file_name)
    assert dea_utils.auto_name_if_needed(
        params, 'csv') == 'dataFileForAutoName_result.csv'
    params.update_parameter('OUTPUT_FILE', 'auto')
    assert dea_utils.auto_name_if_needed(
        params, 'xls') == 'dataFileForAutoName_result.xls'
    with pytest.raises(ValueError) as excinfo:
        dea_utils.auto_name_if_needed(params, 'haha')
    assert str(excinfo.value) == 'haha is not supported output format'
Ejemplo n.º 4
0
def test_super_efficiency_medium(DEA_example2_data):
    data = DEA_example2_data
    data.add_input_category('I1')
    data.add_input_category('I2')
    data.add_input_category('I3')
    data.add_output_category('O1')
    data.add_output_category('O2')
    model = EnvelopmentModelBase(
        data,
        EnvelopmentModelInputOriented(generate_supper_efficiency_upper_bound),
        DefaultConstraintCreator())
    super_efficiency_model = SupperEfficiencyModel(model)
    start_time = datetime.datetime.now()
    solution = super_efficiency_model.run()
    end_time = datetime.datetime.now()
    utils_for_tests.check_optimal_solution_status_and_sizes(solution, data)
    dmus = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K']
    utils_for_tests.check_efficiency_scores(dmus, [
        1.652173892, 0.6485875732, 0.909247759, 1.116838534, 0.5490716156,
        2.485294106, 1.244945494, 0.824120607, 2.299898658, 0.6267333816,
        1.088235274
    ], solution, data, 1e-6)
    work_book = xlwt.Workbook()
    writer = XLSWriter(Parameters(), work_book, datetime.datetime.today(),
                       (end_time - start_time).total_seconds())
    writer.write_data(solution)
    work_book.save('tests/test_super_efficiency_output.xls')
Ejemplo n.º 5
0
def test_run_with_categorical_dmus(categorical_from_book):
    data = categorical_from_book
    model = MultiplierModelBase(data, 0,
                                MultiplierInputOrientedModel())
    categorical_model = ModelWithCategoricalDMUs(model, 'Category')
    start_time = datetime.datetime.now()
    solution = categorical_model.run()
    end_time = datetime.datetime.now()
    dmus = ['L1', 'L2', 'L3', 'L4', 'L5', 'L6', 'L7', 'L8', 'L9', 'L10',
            'L11', 'L12',
            'L13', 'L14', 'L15', 'L16', 'L17', 'L18', 'L19', 'L20',
            'L21', 'L22', 'L23']
    utils_for_tests.check_efficiency_scores(dmus, [0.377, 0.879, 0.936, 1, 1, 1,
                                                   0.743, 0.648, 1, 0.815,
                                                   0.646,
                                                   0.835, 0.794, 0.835, 1,
                                                   0.687, 1, 0.787, 1,
                                                   0.849, 0.787, 0.681, 1],
                                            solution, data, 1e-3)
    work_book = Workbook()
    writer = XLSWriter(Parameters(), work_book, datetime.datetime.today(),
                       (end_time - start_time).total_seconds(),
                       categorical='Category')
    writer.write_data(solution)
    work_book.save('tests/test_categorical_output.xls')
def text_frame_price_ratio(request):
    categories = ['I1', 'I2', 'O1', 'O2']
    text_frame = TextForWeights(None,
                                'price ratio', 'Input <= 0.5', categories,
                                Parameters(), 'PRICE_RATIO_RESTRICTIONS', True)
    request.addfinalizer(text_frame.destroy)
    return text_frame
Ejemplo n.º 7
0
 def __init__(self, parent, current_categories, data_from_params_file,
              str_var_for_input_output_boxes, weights_status_str, *args,
              **kw):
     Notebook.__init__(self, parent, *args, **kw)
     self.parent = parent
     self.params = Parameters()
     self.current_categories = current_categories
     self.input_categories_frame = None
     self.output_categories_frame = None
     self.params_from_file_lbl = None
     self.data_from_params_file = data_from_params_file
     self.str_var_for_input_output_boxes = str_var_for_input_output_boxes
     self.weight_tab = None
     self.load_without_data = IntVar()
     self.options_frame = None
     self.create_widgets(weights_status_str)
def test_all_constraints_multiplier_model(data):

    model = MultiplierModelBase(data, 0, MultiplierInputOrientedModel())

    bounds = {'I1': (None, 0.4)}
    model = MultiplierModelWithVirtualWeightRestrictions(model, bounds)

    abs_bounds = {'I2': (None, 0.2)}
    model = MultiplierModelWithAbsoluteWeightRestrictions(model, abs_bounds)

    ratio_bounds = {('I1', 'I2'): (None, 0.4), ('O1', 'O2'): (0.01, None)}
    model = MultiplierModelWithPriceRatioConstraints(model, ratio_bounds)
    start_time = datetime.datetime.now()
    model_solution = model.run()
    end_time = datetime.datetime.now()
    utils_for_tests.check_if_category_is_within_abs_limits(
        model_solution, abs_bounds)
    utils_for_tests.check_if_category_is_within_virtual_limits(
        model_solution, bounds)
    utils_for_tests.check_if_category_is_within_price_ratio_constraints(
        model_solution, ratio_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_all_constraints_multi_output.xls')
Ejemplo n.º 9
0
def test_maximize_slacks_usual_weakly_disposable_vars():
    categories, xls_data, dmu_name, sheet_name = read_data(
        'tests/DEA_example2_data.xls')
    coefficients, has_same_dmus = convert_to_dictionary(xls_data)
    assert has_same_dmus is False
    assert validate_data(categories, coefficients) is True
    data = construct_input_data_instance(categories, coefficients)
    data.add_input_category('I1')
    data.add_input_category('I2')
    data.add_input_category('I3')
    data.add_output_category('O1')
    data.add_output_category('O2')
    model = EnvelopmentModelVRSDecorator(
        EnvelopmentModelBase(
            data,
            EnvelopmentModelInputOriented(
                generate_upper_bound_for_efficiency_score),
            DisposableVarsConstraintCreator(set(['I2', 'O1']))))
    model = MaximizeSlacksModel(model)
    start_time = datetime.datetime.now()
    model_solution = model.run()
    end_time = datetime.datetime.now()
    utils_for_tests.check_optimal_solution_status_and_sizes(
        model_solution, data)
    dmus = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K']
    utils_for_tests.check_efficiency_scores(
        dmus, [1, 0.86998617, 1, 1, 1, 1, 1, 1, 1, 0.6386574, 1],
        model_solution, data)
    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_max_slacks_weakly_disp_vars_output.xls')
    clean_up_pickled_files()
Ejemplo n.º 10
0
def weight_frame(request):
    parent = Tk()
    current_categories = ['I1', 'I2', 'I3']
    params = ParamsFrameMock(parent)
    weight_frame = WeightFrame(params, current_categories, Parameters(),
                               StringVar(master=parent))
    request.addfinalizer(parent.destroy)
    return weight_frame
Ejemplo n.º 11
0
 def __init__(self, parent):
     super().__init__(parent)
     self.options_frame = OptionsFrameMock(parent)
     self.input_categories_frame = CategoryFrameMock()
     self.output_categories_frame = CategoryFrameMock()
     self.clear_all_called = False
     self.weight_frame_name = ''
     self.params = Parameters()
Ejemplo n.º 12
0
def test_create_params_str():
    params = Parameters()
    params.update_parameter('ORIENTATION', 'input')
    params.update_parameter('RETURN_TO_SCALE', 'VRS')
    assert dea_utils.create_params_str(params) == 'input orientation, VRS'
    params.update_parameter('RETURN_TO_SCALE', 'CRS')
    assert dea_utils.create_params_str(params) == 'input orientation, CRS'
Ejemplo n.º 13
0
def options_frame(request):
    parent = Tk()
    params = Parameters()
    current_categories = []
    options_frame = OptionsFrameMock(parent, params, current_categories,
                                     CategoryFrameMock(),
                                     CategoryFrameMock())
    request.addfinalizer(parent.destroy)
    return options_frame
Ejemplo n.º 14
0
def box(request):
    parent = Tk()
    box = CategoriesCheckBox(parent, 'text', True,
                             Parameters(), 'INPUT_CATEGORIES')
    global categories
    for category in categories:
        box.add_category(category)
    request.addfinalizer(parent.destroy)
    return box
Ejemplo n.º 15
0
def test_write_data_xls(model):
    start_time = datetime.datetime.now()
    model_solution = model.run()
    end_time = datetime.datetime.now()
    work_book = xlwt.Workbook()
    params = Parameters()
    writer = XLSWriter(params, work_book, datetime.datetime.today(),
                       (end_time - start_time).total_seconds())
    writer.write_data(model_solution)
    work_book.save('tests/test_output.xls')
Ejemplo n.º 16
0
def test_auto_name_if_needed():
    params = Parameters()
    file_name = 'out.txt'
    params.update_parameter('OUTPUT_FILE', file_name)
    assert dea_utils.auto_name_if_needed(params, 'txt') == file_name
    params.update_parameter('OUTPUT_FILE', '')
    data_file_name = 'dataFileForAutoName.xls'
    params.update_parameter('DATA_FILE', data_file_name)
    assert dea_utils.auto_name_if_needed(
        params, 'csv') == 'dataFileForAutoName_result.csv'
    params.update_parameter('OUTPUT_FILE', 'auto')
    assert dea_utils.auto_name_if_needed(
        params, 'xls') == 'dataFileForAutoName_result.xls'
    with pytest.raises(ValueError) as excinfo:
        dea_utils.auto_name_if_needed(params, 'haha')
    assert str(excinfo.value) == 'haha is not supported output format'
def test_show_solution(sol_frame, model):
    model_solution, ranks, state = peel_the_onion_method(model)
    sol_frame.show_solution([model_solution],
                            Parameters(), ['params to print'],
                            datetime.datetime.today(),
                            5,
                            ranks=[ranks])
    assert sol_frame.nb_tabs == 8
    assert len(sol_frame.all_tabs) == 8
    assert sol_frame.nb_filled_tabs == 8
    for count, tab in enumerate(sol_frame.all_tabs):
        assert sol_frame.tab(count, 'text') == tab.name

    model_solution = model.run()
    solutions = [model_solution]
    sol_frame.show_solution(solutions, Parameters(), ['params to print'],
                            datetime.datetime.today(), 5)
    assert sol_frame.nb_tabs == 7
    assert len(sol_frame.all_tabs) == 7
    assert sol_frame.nb_filled_tabs == 7
    for count, tab in enumerate(sol_frame.all_tabs):
        assert sol_frame.tab(count, 'text') == tab.name
Ejemplo n.º 18
0
def test_add_empty_input_and_output_categories(DEA_example_data):
    allParams = Parameters()
    allParams.update_parameter('INPUT_CATEGORIES', '')
    allParams.update_parameter('OUTPUT_CATEGORIES', 'O1; O2')
    with pytest.raises(ValueError) as excinfo:
        factory.add_input_and_output_categories(allParams, DEA_example_data)
    assert str(
        excinfo.value) == 'Both input and output categories must be specified'
Ejemplo n.º 19
0
def test_add_input_and_output_categories(DEA_example_data):
    allParams = Parameters()
    allParams.update_parameter('INPUT_CATEGORIES', 'I1; I2')
    allParams.update_parameter('OUTPUT_CATEGORIES', 'O1; O2')
    factory.add_input_and_output_categories(allParams, DEA_example_data)
    assert 'I1' in DEA_example_data.input_categories
    assert 'I2' in DEA_example_data.input_categories
    assert 'O1' in DEA_example_data.output_categories
    assert 'O2' in DEA_example_data.output_categories
Ejemplo n.º 20
0
def test_on_save_solution_ok(solution_tab, model):
    model_solution = model.run()
    solutions = [model_solution]
    params = Parameters()
    param_strs = ['param_strs']
    run_date = datetime.datetime.today()
    solution_tab.show_solution(solutions, params, param_strs, run_date, 5)
    assert solution_tab.model_solutions == solutions
    assert solution_tab.params == params
    assert solution_tab.param_strs == param_strs
    assert solution_tab.run_date == run_date
    assert solution_tab.total_seconds == 5

    solution_tab.on_save_solution()
    assert os.path.isfile(solution_tab.ask_file_name_to_save(1))
    os.remove(solution_tab.ask_file_name_to_save(1))
def test_price_ratio_restrictions_env_model(data):
    model = EnvelopmentModelBase(
        data,
        EnvelopmentModelInputOriented(
            generate_upper_bound_for_efficiency_score),
        DefaultConstraintCreator())

    bounds = {('I1', 'I2'): (None, 0.4), ('O1', 'O2'): (0.01, None)}
    model = EnvelopmentModelWithPriceRatioConstraints(model, bounds)
    start_time = datetime.datetime.now()
    model_solution = model.run()
    end_time = datetime.datetime.now()
    utils_for_tests.check_if_category_is_within_price_ratio_constraints(
        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_price_ratio_env_output.xls')
def test_virual_restrictions_env_model(data):
    model = EnvelopmentModelBase(
        data,
        EnvelopmentModelInputOriented(
            generate_upper_bound_for_efficiency_score),
        DefaultConstraintCreator())

    bounds = {'I2': (0.01, 0.5)}
    model = EnvelopmentModelWithVirtualWeightRestrictions(model, bounds)
    start_time = datetime.datetime.now()
    model_solution = model.run()
    end_time = datetime.datetime.now()
    utils_for_tests.check_if_category_is_within_virtual_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_virtual_constraints_env_output.xls')
Ejemplo n.º 23
0
def test_peel_the_onion_CRS_multi_output_oriented(DEA_example2_data):
    model = _create_large_model_CRS_multi_output_oriented_with_non_discretionary(
        DEA_example2_data)
    start_time = datetime.datetime.now()
    solution, ranks, state = peel_the_onion_method(model)
    end_time = datetime.datetime.now()
    _check_large_model_CRS_multi_output_oriented_with_non_discretionary(
        solution, model.input_data)
    dmus = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K']
    expected_ranks = [1, 3, 2, 1, 3, 1, 1, 2, 1, 2, 1]
    utils_for_tests.check_onion_ranks(
        model.input_data, dmus, expected_ranks, ranks)

    work_book = Workbook()
    ranks_as_list = []
    ranks_as_list.append(ranks)
    writer = XLSWriter(Parameters(), work_book, datetime.datetime.today(),
                       (end_time - start_time).total_seconds(),
                       ranks=ranks_as_list)
    writer.write_data(solution)
    work_book.save('tests/test_peel_the_onion.xls')
Ejemplo n.º 24
0
def test_super_efficiency_with_VRS(DEA_example2_data):
    data = DEA_example2_data
    data.add_input_category('I1')
    data.add_input_category('I2')
    data.add_input_category('I3')
    data.add_output_category('O1')
    data.add_output_category('O2')
    model = EnvelopmentModelBase(
        data,
        EnvelopmentModelInputOriented(generate_supper_efficiency_upper_bound),
        DefaultConstraintCreator())
    model = EnvelopmentModelVRSDecorator(model)
    super_efficiency_model = SupperEfficiencyModel(model)
    start_time = datetime.datetime.now()
    solution = super_efficiency_model.run()
    end_time = datetime.datetime.now()
    work_book = xlwt.Workbook()
    writer = XLSWriter(Parameters(), work_book, datetime.datetime.today(),
                       (end_time - start_time).total_seconds())
    writer.write_data(solution)
    work_book.save('tests/test_super_efficiency_with_VRS.xls')
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')
Ejemplo n.º 26
0
def params(request):
    params = Parameters()
    params.update_parameter('INPUT_CATEGORIES', 'I1; I2')
    params.update_parameter('OUTPUT_CATEGORIES', 'O1; O2')
    params.update_parameter('DEA_FORM', 'env')
    params.update_parameter('RETURN_TO_SCALE', 'CRS')
    params.update_parameter('ORIENTATION', 'input')
    params.update_parameter('NON_DISCRETIONARY_CATEGORIES', '')
    params.update_parameter('WEAKLY_DISPOSAL_CATEGORIES', '')
    params.update_parameter('USE_SUPER_EFFICIENCY', '')
    params.update_parameter('ABS_WEIGHT_RESTRICTIONS', '')
    params.update_parameter('VIRTUAL_WEIGHT_RESTRICTIONS', '')
    params.update_parameter('PRICE_RATIO_RESTRICTIONS', '')
    params.update_parameter('MAXIMIZE_SLACKS', '')
    params.update_parameter('MULTIPLIER_MODEL_TOLERANCE', '0')
    params.update_parameter('CATEGORICAL_CATEGORY', '')
    params.update_parameter('PEEL_THE_ONION', '')

    return params
def text_frame(request):
    categories = ['I1', 'I2', 'O1', 'O2']
    text_frame = TextForWeights(None, 'Absolute', 'Input <= 0.5', categories,
                                Parameters(), 'ABS_WEIGHT_RESTRICTIONS')
    request.addfinalizer(text_frame.destroy)
    return text_frame
Ejemplo n.º 28
0
class ParamsFrame(Notebook):
    ''' This class implements various operations with parameters like
        loading and saving from and to file, modifying parameters values.

        Attributes:
            parent (Tk object): parent of this widget.
            params (Parameters): Parameters object with values of all
                parameters.
            current_categories (list of str): list of current valid categories.
            input_categories (CategoriesCheckBox): frame for displaying
                input categories.
            output_categories (CategoriesCheckBox): frame for displaying
                output categories.
            params_from_file_lbl (Label): label for displaying file name if
                parameters were loaded from file.
            data_from_params_file(StringVar): StringVar object used for
                communication of this widget with DataFrame. Changing the
                value of data_from_params_file
                triggers changes in DataFrame (like clearing all data and
                loading data from file).
            str_var_for_input_output_boxes (ObserverStringVar):
                ObserverStringVar
                object used for storing input and output categories and for
                tracking changes in input and output categories.
            weight_tab (WeightFrame): widget used for displaying and
                editing weights.
            load_without_data (IntVar): IntVar object used for Checkbutton,
                if its value
                is 1, then parameters will be loaded from file without data,
                if its value
                is 0, then parameters will be loaded from file with data.
            options_frame (OptionsFrame): widget used for displaying and
                modifying some of the parameters.

        Args:
            parent (Tk object): parent of this widget.
            current_categories (list of str): list of current valid categories.
            data_from_params_file(StringVar): StringVar object used for
                communication of this widget with DataFrame. Changing the value
                of data_from_params_file
                triggers changes in DataFrame (like clearing all data and
                loading data from file).
            str_var_for_input_output_boxes (ObserverStringVar):
                ObserverStringVar object used for
                storing input and output categories and for tracking changes
                in input and output categories.
            weights_status_str (StringVar): StringVar object used for changing
                label of weights editor, for details see WeightFrame.
    '''
    def __init__(self, parent, current_categories, data_from_params_file,
                 str_var_for_input_output_boxes, weights_status_str, *args,
                 **kw):
        Notebook.__init__(self, parent, *args, **kw)
        self.parent = parent
        self.params = Parameters()
        self.current_categories = current_categories
        self.input_categories_frame = None
        self.output_categories_frame = None
        self.params_from_file_lbl = None
        self.data_from_params_file = data_from_params_file
        self.str_var_for_input_output_boxes = str_var_for_input_output_boxes
        self.weight_tab = None
        self.load_without_data = IntVar()
        self.options_frame = None
        self.create_widgets(weights_status_str)

    def create_widgets(self, weights_status_str):
        ''' Creates all widgets.
        '''
        self.enable_traversal()
        self._create_params_tab()
        self.weight_tab = WeightFrame(self, self.current_categories,
                                      self.params, weights_status_str)
        self.add(self.weight_tab, text='Weights editor')

    def change_weight_tab_name(self, new_name):
        ''' Changes name of weights editor tab.

            Args:
                new_name (str): new name for weights editor tab.
        '''
        self.tab(1, text=new_name)

    def _create_params_tab(self):
        ''' Creates all widgets of the parameters tab.
        '''
        frame_for_all_objects = VerticalScrolledFrame(self)
        frame_for_all_objects.columnconfigure(0, weight=1)
        frame_for_all_objects.rowconfigure(0, weight=1)

        params_tab = frame_for_all_objects.interior
        params_tab.columnconfigure(0, weight=1, pad=5)

        frame_for_save_btns = Frame(params_tab)
        frame_for_save_btns.columnconfigure(0, weight=1)
        frame_for_save_btns.columnconfigure(1, weight=1)
        load_btn = Button(frame_for_save_btns,
                          text='Load parameters',
                          command=self.load_file)
        load_btn.grid(row=0, column=0, sticky=W + N, pady=2)
        load_wo_data_box = Checkbutton(frame_for_save_btns,
                                       text='Load without data',
                                       variable=self.load_without_data)
        load_wo_data_box.grid(row=1, column=0, sticky=W + N, pady=2)
        save_btn = Button(frame_for_save_btns,
                          text='Save parameters',
                          command=self.on_save_params)
        save_btn.grid(row=0, column=1, sticky=E + N, pady=2)
        save_btn = Button(frame_for_save_btns,
                          text='Save parameters as...',
                          command=self.on_save_params_as)
        save_btn.grid(row=1, column=1, sticky=E + N, pady=2)
        frame_for_save_btns.grid(row=0,
                                 column=0,
                                 sticky=E + W,
                                 padx=XPAD_VALUE,
                                 pady=YPAD_VALUE)

        self.params_from_file_lbl = Label(params_tab,
                                          text=TEXT_FOR_PARAMS_LBL,
                                          anchor=W,
                                          justify=LEFT,
                                          wraplength=MAX_FILE_LBL_LENGTH)
        self.params_from_file_lbl.grid(row=1,
                                       column=0,
                                       columnspan=3,
                                       sticky=W + N,
                                       padx=XPAD_VALUE,
                                       pady=YPAD_VALUE)

        input_categories_list = CategoriesCheckBox(params_tab,
                                                   'Input categories:', True,
                                                   self.params,
                                                   'INPUT_CATEGORIES')
        self.input_categories_frame = input_categories_list
        input_categories_list.grid(row=4,
                                   column=0,
                                   sticky=W + N + S + E,
                                   padx=XPAD_VALUE,
                                   pady=YPAD_VALUE,
                                   columnspan=2)
        output_categories_list = CategoriesCheckBox(params_tab,
                                                    'Output categories:',
                                                    False, self.params,
                                                    'OUTPUT_CATEGORIES')
        self.output_categories_frame = output_categories_list
        output_categories_list.grid(row=5,
                                    column=0,
                                    sticky=W + N + S + E,
                                    padx=XPAD_VALUE,
                                    pady=YPAD_VALUE,
                                    columnspan=2)

        self.options_frame = OptionsFrame(params_tab, self.params,
                                          self.current_categories,
                                          self.input_categories_frame,
                                          self.output_categories_frame)
        self.options_frame.grid(row=6,
                                column=0,
                                columnspan=2,
                                sticky=N + S + W + E,
                                padx=XPAD_VALUE,
                                pady=YPAD_VALUE)
        self.add(frame_for_all_objects, text='Parameters')

    def on_save_params(self):
        ''' Saves current parameter values to a file from where the\
            parameters were loaded.
            This file name is displayed. If no file name is displayed
            (i.e. parameters were not
            previously loaded from file), then asksaveasfilename dialogue
            is called.
        '''
        file_name = self.params_from_file_lbl.cget('text')
        if TEXT_FOR_PARAMS_LBL in file_name:
            file_name = file_name[len(TEXT_FOR_PARAMS_LBL):]
        if file_name:
            write_parameters_to_file(self.params, file_name)
        else:
            self.on_save_params_as()

    def on_save_params_as(self):
        ''' Calls asksaveasfilename dialogue and saves current values of
            parameters to
            the specified file.
        '''
        file_name = self._get_file_name_to_save()
        if file_name:
            write_parameters_to_file(self.params, file_name)

    def _get_file_name_to_save(self):
        ''' Calls asksaveasfilename dialogue. This method is overridden
            in unit tests.

            Returns:
                (str): file name.
        '''
        return asksaveasfilename(filetypes=[('Text files', '*.txt')],
                                 defaultextension='.txt')

    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()

    def _get_filename_for_load(self):
        ''' Calls askopenfilename dialogue. This method is overridden
            in unit tests.

            Returns:
                (str): file name.
        '''
        file_types = [('Text files', '*.txt'), ('All files', '*.*')]
        file_name = askopenfilename(title='Choose a file',
                                    filetypes=file_types)
        return file_name

    def load_data_file_and_related_params(self, file_name, params_to_restore):
        ''' Loads data if possible and sets widgets to proper values
            depending on parameters.

            Args:
                file_name (str): file name of file with parameters. It is needed
                    to display it on parameters frame.
                params_to_restore (dict of str to str): dictionary of
                    previous values of parameters. They are used in order
                    to restore
                    previous values if loading of data from file fails.
        '''
        data_file = self.params.get_parameter_value('DATA_FILE')
        norm_data_path = os.path.normpath(data_file)
        if os.path.isfile(norm_data_path):
            params_to_restore = dict()
            # I have to store this here, because when I clean all data
            # from data tab, it deletes these values from params
            for param_name in CATEGORICAL_AND_DATA_FIELDS:
                params_to_restore[
                    param_name] = self.params.get_parameter_value(param_name)
            # this line calls clear all from data_tab
            self.data_from_params_file.set(norm_data_path)

            self.params_from_file_lbl.config(text=TEXT_FOR_PARAMS_LBL +
                                             file_name)
            for param_name, value in params_to_restore.items():
                self.params.update_parameter(param_name, value)

            self.add_categories(
                'INPUT_CATEGORIES', self.input_categories_frame,
                self.str_var_for_input_output_boxes.input_categories)
            self.add_categories(
                'OUTPUT_CATEGORIES', self.output_categories_frame,
                self.str_var_for_input_output_boxes.output_categories)
            self.str_var_for_input_output_boxes.set('notify')
            self.weight_tab.add_weights()
        else:
            self._show_warning(norm_data_path)
            for param_name, value in params_to_restore.items():
                self.params.update_parameter(param_name, value)

    def _show_warning(self, norm_data_path):
        ''' Shows warning that data cannot be loaded from file.
            This method is overridden in unit tests.
        '''
        showwarning(
            'Warning', 'Cannot load data file: ' + norm_data_path +
            '. Parameters will be loaded without data.')

    def change_category_name(self, old_name, new_name):
        ''' Changes category name in parameters and all widgets to a new name.
            If new name is empty string, then some of the parameters might
            be lost (for example, weight restrictions will be lost).

            Args:
                old_name (str): old name of the category.
                new_name (str): new name of the category.
        '''
        if old_name != new_name:
            self.input_categories_frame.change_category_name(
                old_name, new_name)
            self.output_categories_frame.change_category_name(
                old_name, new_name)
            self.weight_tab.add_weights()
            if self.options_frame.combobox_text_var.get() == old_name:
                self.options_frame.change_categorical_box()
                self.options_frame.set_categorical_box(new_name)

    def add_categories(self, name, frame, categories_container):
        ''' Adds input or output categories to a specified widget
            with categories from parameters.

            Args:
                name (str): name of the parameter where categories come from,
                    possible values INPUT_CATEGORIES, OUTPUT_CATEGORIES.
                frame (CategoriesCheckBox): widget where categories will
                    be added.
                categories_container (list of str): list of categories where
                    categories from parameters will be added.
        '''
        categories = self.params.get_set_of_parameters(name)
        for category in categories:
            # we add only categories that are
            # present in data file
            if category in self.current_categories:
                frame.add_category(category)
                categories_container.append(category)
            else:
                self.params.remove_category_from_params(name, category)

    def clear_all(self):
        ''' Clears all parameters and corresponding widgets.
        '''
        self.input_categories_frame.remove_all_categories()
        self.output_categories_frame.remove_all_categories()
        self.options_frame.combobox_text_var.set('')
        self.weight_tab.remove_all_weights()
        self.params.clear_all_categorical_and_data_fields()
        self.params_from_file_lbl.config(text='')
        self.str_var_for_input_output_boxes.input_categories.clear()
        self.str_var_for_input_output_boxes.output_categories.clear()
Ejemplo n.º 29
0
def build_models(params, model_input):
    ''' Creates several models and parameters in the case when
        RETURN_TO_SCALE or ORIENTATION is set to both. If neither
        RETURN_TO_SCALE
        nor ORIENTATION is set to both, then no new models will be created,
        a copy of the given model and parameters will be returned.

        Args:
            params (Parameters): parameters.
            model_input (InputData): data instance.

        Returns:
            tuple of list of ModelBase, list of Parameters: tuple with two
                lists. The first list contains all created models, the
                second list contains corresponding parameters.
    '''
    model_factory.add_input_and_output_categories(params, model_input)
    rts_type = params.get_parameter_value('RETURN_TO_SCALE')
    list_of_param_objects = []
    original_params = Parameters()
    original_params.copy_all_params(params)
    list_of_param_objects.append(original_params)
    if rts_type == 'both':
        original_params.update_parameter('RETURN_TO_SCALE', 'VRS')
        params_with_crs = Parameters()
        params_with_crs.copy_all_params(original_params)
        params_with_crs.update_parameter('RETURN_TO_SCALE', 'CRS')
        list_of_param_objects.append(params_with_crs)
    orientation_type = original_params.get_parameter_value('ORIENTATION')
    nb_param_objs = len(list_of_param_objects)
    if orientation_type == 'both':
        possible_orientation = ['input', 'output']
        for count in range(nb_param_objs):
            assert(count < 2)
            param_obj = list_of_param_objects[count]
            param_obj.update_parameter('ORIENTATION',
                                       possible_orientation[count])
            new_param_obj = Parameters()
            new_param_obj.copy_all_params(param_obj)
            new_param_obj.update_parameter('ORIENTATION',
                                           possible_orientation[1 - count])
            list_of_param_objects.append(new_param_obj)

    models = []
    for param_object in list_of_param_objects:
        models.append(model_factory.create_model(param_object, model_input))
    return models, list_of_param_objects