Exemplo n.º 1
0
 def test_list_flat_map(self):
     expected = List(
         ['john dupont', 'toto dupont', 'john dupond', 'toto dupond'])
     self.assertEqual(
         expected,
         List(['dupont',
               'dupond']).flat_map(lambda x: [f'john {x}', f'toto {x}']))
Exemplo n.º 2
0
    def table_formatter(cls, table: list,
                        output: OutputFormat) -> (list, list):
        # Find headers that will be displayed
        analyzed_columns = [
            'accuracy_cost', 'cohen_kappa_cost', 'f1_cost', 'roc_auc_cost',
            'average_precision_cost', 'precision_cost', 'recall_cost',
            'log_loss_cost', 'f1_micro_cost', 'f1_macro_cost',
            'roc_auc_micro_cost', 'roc_auc_macro_cost',
            'average_precision_micro_cost', 'average_precision_macro_cost',
            'mape_cost', 'r2_cost', 'mae_cost', 'mse_cost'
        ]
        row_from_column_key = lambda column_key: List(table).map(
            lambda x: x.get(column_key, None))
        tuple_from_key = lambda column_key: (column_key,
                                             row_from_column_key(column_key).
                                             count(lambda x: x is None))
        number_of_none = List(analyzed_columns).map(tuple_from_key).filter(
            lambda d: d[1] != len(table)).to_dict()
        final_header = list(number_of_none.keys())

        # Reformat row with red and green
        formatted_table = []
        for x in table:
            x_copy = copy.copy(x)
            for column_name in final_header:
                max_column = max([
                    x[column_name] for x in table if x[column_name] is not None
                ])
                min_column = min([
                    x[column_name] for x in table if x[column_name] is not None
                ])
                if x[column_name] == max_column and output == OutputFormat.TABLE:
                    x_copy[column_name] = colored(max_column, 'red')
                if x[column_name] == min_column and output == OutputFormat.TABLE:
                    x_copy[column_name] = colored(min_column, 'green')

            # Replace all None value with '-'
            for key, val in x_copy.items():
                if val is None:
                    x_copy[key] = '-'

            formatted_table.append(x_copy)

        return [
            'uuid', 'status', 'config_name', 'past_steps', 'horizon',
            'discount'
        ] + final_header, formatted_table
    def interactive_kwargs_instanciation(self) -> Config:
        """
        Instanciate dictionary of 'kwargs' arguments from an interactive prompt
        :return: the 'kwargs' dictionary
        """
        questions = [
            x.get_pyinquirer_question() for x in self.get_hyperparameters()
        ]
        conditions = self.get_conditions()

        # applying conditions if any
        for condition in conditions:
            child = condition.get_child()
            parent = condition.get_parent()
            condition_type = condition.get_type()
            values = condition.get_values()

            question = List([x for x in questions if x.get('name') == child])\
                .head_option()\
                .get_or_else(None)

            if question is not None and condition_type == 'IN':
                question['name'] = parent
                # Need to keep the double lambda because of python scope and closure stuff pb
                question['when'] = (lambda parent_key, values_value: lambda x:
                                    x.get(parent_key) in values_value)(parent,
                                                                       values)

        # Put the question with a 'when' value at the end
        questions = sorted(questions,
                           key=lambda q: str(q.get('when') is not None))

        # In case of time series forecast, add horizon and discount
        if self.category == AlgorithmConfigurationCategory.TIME_SERIES_FORECAST:
            questions.append({
                'type': 'input',
                'name': 'forecasting_horizon_steps',
                'message': f'forecasting_horizon_steps must be at least 1',
                'default': str(1),
                'validate': IntegerValidator,
                'filter': int
            })
            questions.append({
                'type': 'input',
                'name': 'forecasting_discount',
                'message':
                f'forecasting_discount must be between 0.0 (excluded) and 1.1 (included)',
                'default': str(1.0),
                'validate': FloatValidator,
                'filter': float
            })

        # Prompting for answers
        answers = questionary.prompt(questions)
        config = self.instance_config()
        for k, v in answers.items():
            config.add_kwargs(k, v)
        return config
Exemplo n.º 4
0
    def exec(self, args):
        maybe_cmd = List(self.sub_commands)\
            .find(lambda cmd: cmd.should_exec(args))\
            .get_or_else(None)

        if maybe_cmd is None:
            subject = get_args_or_prompt_list(
                arg_name='subject',
                args=args,
                message='Please select an command to execute',
                choices_function=lambda:
                [cmd.name for cmd in self.sub_commands])
            args[f'{self.name}_subject'] = subject
            maybe_cmd = List(self.sub_commands) \
                .find(lambda cmd: cmd.should_exec(args)) \
                .get_or_else(None)

        maybe_cmd.exec(args)
Exemplo n.º 5
0
    def exec(self, args: dict):
        interactive_mode = args.get('dataset-id') is None
        dataset_id = get_args_or_prompt_list(
            arg_name='dataset-id',
            args=args,
            message='Which dataset do you want to launch an evaluation on ?',
            choices_function=lambda: [
                x.dataset_id()
                for x in self.prescience_client.datasets(page=1).content
            ],
            force_interactive=interactive_mode)
        interactive_mode = interactive_mode or args.get(
            'custom-config') is None
        if not interactive_mode:
            prescience_config = Config(
                json_dict=json.loads(args.get('custom-config')))
        else:
            # Use interactive mode to create the configuration
            dataset = self.prescience_client.dataset(dataset_id=dataset_id)
            all_config_list = dataset.get_associated_algorithm()
            choice_function = lambda: UtilList(all_config_list).flat_map(
                lambda x: x.get_algorithm_list_names()).value
            algo_id = get_args_or_prompt_list(
                arg_name='algo_id',
                args=args,
                message='Which algorithm ID do you want to get ?',
                choices_function=choice_function)
            algorithm = UtilList(all_config_list) \
                .map(lambda x: x.get_algorithm(algo_id)) \
                .find(lambda x: x is not None) \
                .get_or_else(None)

            prescience_config = algorithm.interactive_kwargs_instanciation()
        watch = get_args_or_prompt_confirm(
            arg_name='watch',
            args=args,
            message='Do you want to keep watching for the task until it ends ?',
            force_interactive=interactive_mode)
        print(json.dumps(prescience_config.to_dict(), indent=4))
        task = self.prescience_client.custom_config(dataset_id=dataset_id,
                                                    config=prescience_config)
        if watch:
            task.watch()
Exemplo n.º 6
0
 def test_list_find_smthg(self):
     self.assertEqual(Option(1), List([1, 2, 3]).find(lambda x: x == 1))
Exemplo n.º 7
0
 def test_list_map_int(self):
     self.assertEqual(List([1, 2, 3]), List([0, 1, 2]).map(lambda x: x + 1))
Exemplo n.º 8
0
 def test_list_flatten(self):
     self.assertEqual(List([1, 2, 3, 4]), List([[1, 2], [3, 4]]).flatten())
Exemplo n.º 9
0
 def test_list_find_None(self):
     self.assertEqual(Option(None), List([1, 2, 3]).find(lambda x: x == 4))
Exemplo n.º 10
0
 def test_list_count(self):
     self.assertEqual(2, List([0, 0, 0, 2, 2]).count(lambda x: x > 1))
Exemplo n.º 11
0
 def test_list_is_empty_true(self):
     self.assertEqual(True, List([]).is_empty())
Exemplo n.º 12
0
 def test_list_to_dict_index(self):
     self.assertEqual({0: 'toto'}, List(['toto']).to_dict())
Exemplo n.º 13
0
 def test_list_head(self):
     self.assertEqual(1, List([1, 2, 3]).head())
Exemplo n.º 14
0
 def test_list_to_dict_empty(self):
     self.assertEqual({}, List([]).to_dict())
Exemplo n.º 15
0
 def test_list_map_string(self):
     self.assertEqual(List(['toto dupond', 'john dupond']),
                      List(['toto', 'john']).map(lambda x: x + ' dupond'))
Exemplo n.º 16
0
 def test_list_tail_option(self):
     self.assertEqual(Option(3), List([1, 2, 3]).tail_option())
Exemplo n.º 17
0
 def test_list_tail(self):
     self.assertEqual(3, List([1, 2, 3]).tail())
Exemplo n.º 18
0
 def test_list_head_option(self):
     self.assertEqual(Option(1), List([1, 2, 3]).head_option())
Exemplo n.º 19
0
 def test_list_is_empty_false(self):
     self.assertEqual(False, List(['toto']).is_empty())
Exemplo n.º 20
0
 def test_list_filter(self):
     self.assertEqual(List([2, 2]),
                      List([0, 0, 0, 2, 2]).filter(lambda x: x > 1))
Exemplo n.º 21
0
 def test_list_to_dict_tuple(self):
     self.assertEqual({'key': 'value'}, List([('key', 'value')]).to_dict())
Exemplo n.º 22
0
 def test_list_size(self):
     self.assertEqual(3, List([1, 2, 3]).size())