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