Example #1
0
    def _initialise_repo(self, repo_path: str) -> List[OperationMetaInfo]:
        """ Method parse JSON repository with operations descriptions and
        wrapped information into OperationMetaInfo, then put it into the list

        :return operations_list: list with OperationMetaInfo for every operation
        from json repository
        """
        with open(repo_path) as repository_json_file:
            repository_json = json.load(repository_json_file)

        metadata_json = repository_json['metadata']
        operations_json = repository_json['operations']

        operations_list = []
        for current_operation_key in operations_json:
            # Get information about operation
            # properties - information about operation by key, for example tags
            # metadata - information about meta of the operation
            properties = operations_json.get(current_operation_key)
            metadata = metadata_json[properties['meta']]

            task_types = eval_field_str(metadata['tasks'])
            input_type = eval_field_str(metadata['input_type'])
            output_type = eval_field_str(metadata['output_type'])

            # Get available strategies for obtained metadata
            supported_strategies = self.get_strategies_by_metadata(metadata)

            accepted_node_types = read_field(metadata, 'accepted_node_types', ['any'])
            forbidden_node_types = read_field(metadata, 'forbidden_node_types', [])

            # Get tags for meta and for operation
            meta_tags = read_field(metadata, 'tags', [])
            operation_tags = read_field(properties, 'tags', [])

            allowed_positions = ['primary', 'secondary', 'root']

            if accepted_node_types and accepted_node_types != 'all':
                allowed_positions = accepted_node_types
            if forbidden_node_types:
                allowed_positions = [pos for pos in allowed_positions if
                                     pos not in forbidden_node_types]

            # Unit tags
            tags = meta_tags + operation_tags

            operation = OperationMetaInfo(id=current_operation_key,
                                          input_types=input_type,
                                          output_types=output_type,
                                          task_type=task_types,
                                          supported_strategies=supported_strategies,
                                          allowed_positions=allowed_positions,
                                          tags=tags)
            operations_list.append(operation)

        return operations_list
Example #2
0
    def get_strategies_by_metadata(metadata: dict):
        """ Method allow obtain strategy instance by the metadata

        :param metadata: information about meta of the operation
        :return supported_strategies: available strategies for current metadata
        """
        strategies_json = metadata['strategies']
        if isinstance(strategies_json, list):
            supported_strategies = eval_strategy_str(strategies_json)
        else:
            supported_strategies = {}
            for strategy_dict_key in strategies_json.keys():
                # Convert string into class path for import
                import_path = eval_field_str(strategy_dict_key)
                strategy_class = eval_strategy_str(strategies_json[strategy_dict_key])

                supported_strategies.update({import_path: strategy_class})
        return supported_strategies
Example #3
0
def test_eval_field_str():
    model_metadata = _model_metadata_example(mocked_path())
    task_types = eval_field_str(model_metadata['tasks'])

    assert len(task_types) == 1
    assert task_types[0] == TaskTypesEnum.classification
Example #4
0
    def _initialise_repo(self, repo_path: str) -> List[ModelMetaInfo]:
        with open(repo_path) as repository_json_file:
            repository_json = json.load(repository_json_file)

        metadata_json = repository_json['metadata']
        models_json = repository_json['models']

        models_list = []

        for current_model_key in models_json:
            model_properties = [
                model_properties
                for model_key, model_properties in list(models_json.items())
                if model_key == current_model_key
            ][0]
            model_metadata = metadata_json[model_properties['meta']]

            task_types = eval_field_str(model_metadata['tasks'])
            input_type = eval_field_str(model_metadata['input_type'])
            output_type = eval_field_str(model_metadata['output_type'])

            strategies_json = model_metadata['strategies']
            if isinstance(strategies_json, list):
                supported_strategies = eval_strategy_str(strategies_json)
            else:
                supported_strategies = {}
                for strategy_dict_key in strategies_json.keys():
                    supported_strategies[eval_field_str(
                        strategy_dict_key)] = eval_strategy_str(
                            strategies_json[strategy_dict_key])

            accepted_node_types = read_field(model_metadata,
                                             'accepted_node_types', ['any'])
            forbidden_node_types = read_field(model_metadata,
                                              'forbidden_node_types', [])
            meta_tags = read_field(model_metadata, 'tags', [])

            model_tags = read_field(model_properties, 'tags', [])

            allowed_positions = ['primary', 'secondary', 'root']

            if accepted_node_types and accepted_node_types != 'all':
                allowed_positions = accepted_node_types
            if forbidden_node_types:
                allowed_positions = [
                    pos for pos in allowed_positions
                    if pos not in forbidden_node_types
                ]

            tags = list(set(meta_tags + model_tags))

            model = ModelMetaInfo(id=current_model_key,
                                  input_types=input_type,
                                  output_types=output_type,
                                  task_type=task_types,
                                  supported_strategies=supported_strategies,
                                  allowed_positions=allowed_positions,
                                  tags=tags)
            models_list.append(model)

        return models_list