From 536c47045ed1ae02ab89403d175f9222d1f0ceb1 Mon Sep 17 00:00:00 2001 From: Simon Nilsson Date: Thu, 5 Oct 2023 11:01:58 -0400 Subject: [PATCH] Add files via upload --- .../boolean_conditional_slicer_pup_up.py | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 simba/ui/pop_ups/boolean_conditional_slicer_pup_up.py diff --git a/simba/ui/pop_ups/boolean_conditional_slicer_pup_up.py b/simba/ui/pop_ups/boolean_conditional_slicer_pup_up.py new file mode 100644 index 000000000..ffcf8b905 --- /dev/null +++ b/simba/ui/pop_ups/boolean_conditional_slicer_pup_up.py @@ -0,0 +1,70 @@ +from tkinter import * + +from simba.mixins.pop_up_mixin import PopUpMixin +from simba.mixins.config_reader import ConfigReader +from simba.ui.tkinter_functions import DropDownMenu, CreateLabelFrameWithIcon, Entry_Box, LabelFrame, Label +from simba.utils.enums import Keys, Links, Formats, Options +from simba.utils.checks import check_if_filepath_list_is_empty +from simba.utils.read_write import read_df +from simba.utils.errors import CountError, DuplicationError + + +class BooleanConditionalSlicerPopUp(PopUpMixin, ConfigReader): + def __init__(self, + config_path: str): + + ConfigReader.__init__(self, config_path=config_path) + PopUpMixin.__init__(self, title='CONDITIONAL AGGREGATE STATISTICS', size=(600, 400)) + + self.rule_cnt_frm = CreateLabelFrameWithIcon(parent=self.main_frm, header='CONDITIONAL RULES #', icon_name=Keys.DOCUMENTATION.value, icon_link=Links.PATH_PLOTS.value) + self.rule_cnt_dropdown = DropDownMenu(self.rule_cnt_frm, '# RULES:', list(range(2, 21)), '25', com=self.create_rules_frames) + self.rule_cnt_dropdown.setChoices(2) + self.rule_cnt_frm.grid(row=0, column=0, sticky='NW') + self.rule_cnt_dropdown.grid(row=0, column=0, sticky='NW') + + self.create_run_frm(run_function=self.run) + check_if_filepath_list_is_empty(filepaths=self.feature_file_paths, error_msg=f'No data found in {self.features_dir}') + data_df = read_df(file_path=self.feature_file_paths[0], file_type=self.file_type) + self.bool_cols = data_df.columns[data_df.apply(self._is_bool)] + if len(self.bool_cols) < 2: + raise CountError(msg=f'The data file {self.feature_file_paths[0]} contains less than 2 boolean columns', source=self.__class__.__name__) + self.create_rules_frames(rules_cnt=2) + self.main_frm.mainloop() + + @staticmethod + def _is_bool(column): + unique_values = set(column) + return unique_values.issubset({0, 1}) + + def create_rules_frames(self, rules_cnt: int): + if hasattr(self, 'rule_definitions_frame'): + self.rule_definitions_frame.destroy() + self.rule_definitions_frame = LabelFrame(self.main_frm, text='CONDITINAL RULES', font=Formats.LABELFRAME_HEADER_FORMAT.value, pady=5, padx=5) + self.rule_definitions_frame.grid(row=1, column=0, sticky='NW') + Label(self.rule_definitions_frame, text='RULE #').grid(row=0, column=0, sticky=NW) + Label(self.rule_definitions_frame, text='BEHAVIOR').grid(row=0, column=1, sticky=NW, padx=5) + Label(self.rule_definitions_frame, text='STATUS').grid(row=0, column=2, sticky=NW, padx=5) + + self.rules = {} + for rule_cnt in range(1, rules_cnt + 1): + self.rules[rule_cnt] = {} + Label(self.rule_definitions_frame, text=str(rule_cnt), font=Formats.LABELFRAME_HEADER_FORMAT.value).grid(row=rule_cnt, column=0, sticky=NW) + self.rules[rule_cnt]['behavior_drpdwn'] = DropDownMenu(self.rule_definitions_frame, '', self.bool_cols, '1', ) + self.rules[rule_cnt]['behavior_drpdwn'].setChoices(self.bool_cols[rule_cnt-1]) + self.rules[rule_cnt]['behavior_drpdwn'].grid(row=rule_cnt, column=1, sticky=NW) + self.rules[rule_cnt]['status_drpdwn'] = DropDownMenu(self.rule_definitions_frame, '', Options.BOOL_STR_OPTIONS.value, '1') + self.rules[rule_cnt]['status_drpdwn'].setChoices(Options.BOOL_STR_OPTIONS.value[0]) + self.rules[rule_cnt]['status_drpdwn'].grid(row=rule_cnt, column=2, sticky=NW) + + def run(self): + unique_rule_behaviors = [] + selections = {} + for rule_id, rule_data in self.rules.items(): + unique_rule_behaviors.append(rule_data['behavior_drpdwn'].getChoices()) + selections[rule_data['behavior_drpdwn'].getChoices()] = rule_data['status_drpdwn'].getChoices() + duplicates = list(set([x for x in unique_rule_behaviors if unique_rule_behaviors.count(x) > 1])) + if len(duplicates) > 0: + raise DuplicationError(msg=f'Each row should be a unique behavior. However, behaviors {duplicates} are selected in more than 1 rows.') + +#roi_featurizer = BooleanConditionalSlicerPopUp(config_path='/Users/simon/Desktop/envs/troubleshooting/two_animals_16bp_032023/project_folder/project_config.ini') +#roi_featurizer = BooleanConditionalSlicerPopUp(config_path='/Users/simon/Desktop/envs/troubleshooting/two_black_animals_14bp/project_folder/project_config.ini') \ No newline at end of file