Пример #1
0
    def _parse_ml_method(ml_method_id: str, ml_specification) -> tuple:

        valid_class_values = ReflectionHandler.all_nonabstract_subclass_basic_names(MLMethod, "", "ml_methods/")

        if type(ml_specification) is str:
            ml_specification = {ml_specification: {}}

        ml_specification = {**DefaultParamsLoader.load("ml_methods/", "MLMethod"), **ml_specification}
        ml_specification_keys = list(ml_specification.keys())

        ParameterValidator.assert_all_in_valid_list(list(ml_specification_keys), ["model_selection_cv", "model_selection_n_folds"] +
                                                    valid_class_values, "MLParser", ml_method_id)

        non_default_keys = [key for key in ml_specification.keys() if key not in ["model_selection_cv", "model_selection_n_folds"]]

        assert len(ml_specification_keys) == 3, f"MLParser: ML method {ml_method_id} was not correctly specified. Expected at least 1 key " \
                                                f"(ML method name), got {len(ml_specification_keys) - 2} instead: " \
                                                f"{str([key for key in non_default_keys])[1:-1]}."

        ml_method_class_name = non_default_keys[0]
        ml_method_class = ReflectionHandler.get_class_by_name(ml_method_class_name, "ml_methods/")

        ml_specification[ml_method_class_name] = {**DefaultParamsLoader.load("ml_methods/", ml_method_class_name, log_if_missing=False),
                                                  **ml_specification[ml_method_class_name]}

        method, params = MLParser.create_method_instance(ml_specification, ml_method_class, ml_method_id)
        ml_specification[ml_method_class_name] = params
        method.name = ml_method_id

        return method, ml_specification
Пример #2
0
 def _parse_to_enum_instances(params, location):
     for key in params.keys():
         class_name = DefaultParamsLoader._convert_to_camel_case(key)
         if ReflectionHandler.exists(class_name, location):
             cls = ReflectionHandler.get_class_by_name(class_name, location)
             params[key] = cls[params[key].upper()]
     return params
Пример #3
0
    def parse(self,
              key: str,
              instruction: dict,
              symbol_table: SymbolTable,
              path: str = None) -> DatasetExportInstruction:
        location = "DatasetExportParser"
        ParameterValidator.assert_keys(list(instruction.keys()),
                                       DatasetExportParser.VALID_KEYS,
                                       location, key)
        valid_formats = ReflectionHandler.all_nonabstract_subclass_basic_names(
            DataExporter, "Exporter", 'dataset_export/')
        ParameterValidator.assert_all_in_valid_list(
            instruction["export_formats"], valid_formats, location,
            "export_formats")
        ParameterValidator.assert_all_in_valid_list(
            instruction["datasets"],
            symbol_table.get_keys_by_type(SymbolType.DATASET), location,
            "datasets")

        return DatasetExportInstruction(
            datasets=[
                symbol_table.get(dataset_key)
                for dataset_key in instruction["datasets"]
            ],
            exporters=[
                ReflectionHandler.get_class_by_name(f"{key}Exporter",
                                                    "dataset_export/")
                for key in instruction["export_formats"]
            ],
            name=key)
Пример #4
0
    def test_get_class_from_path(self):

        filepath = EnvironmentSettings.root_path + "/source/util/KmerHelper.py"

        cls = ReflectionHandler.get_class_from_path(filepath, "KmerHelper")
        self.assertEqual(KmerHelper, cls)

        cls = ReflectionHandler.get_class_from_path(filepath)
        self.assertEqual(KmerHelper, cls)
Пример #5
0
    def import_hp_setting(config_dir: str) -> Tuple[HPSetting, Label]:

        config = MLMethodConfiguration()
        config.load(f'{config_dir}ml_config.yaml')

        ml_method = ReflectionHandler.get_class_by_name(
            config.ml_method, 'ml_methods/')()
        ml_method.load(config_dir)

        encoder = MLImport.import_encoder(config, config_dir)
        preprocessing_sequence = MLImport.import_preprocessing_sequence(
            config, config_dir)

        labels = list(config.labels_with_values.keys())
        assert len(
            labels
        ) == 1, "MLImport: Multiple labels set in a single ml_config file."

        label = Label(labels[0], config.labels_with_values[labels[0]])

        return HPSetting(
            encoder=encoder,
            encoder_params=config.encoding_parameters,
            encoder_name=config.encoding_name,
            ml_method=ml_method,
            ml_method_name=config.ml_method_name,
            ml_params={},
            preproc_sequence=preprocessing_sequence,
            preproc_sequence_name=config.preprocessing_sequence_name), label
Пример #6
0
    def parse_object(specs,
                     valid_class_names: list,
                     class_name_ending: str,
                     class_path: str,
                     location: str,
                     key: str,
                     builder: bool = False,
                     return_params_dict: bool = False):

        class_name = ObjectParser.get_class_name(specs, valid_class_names,
                                                 class_name_ending, location,
                                                 key)
        ParameterValidator.assert_in_valid_list(class_name, valid_class_names,
                                                location, key)

        cls = ReflectionHandler.get_class_by_name(
            f"{class_name}{class_name_ending}", class_path)
        params = ObjectParser.get_all_params(specs, class_path, class_name,
                                             key)

        try:
            if "name" not in inspect.signature(cls.__init__).parameters.keys():
                del params["name"]
            obj = cls.build_object(
                **params) if builder and hasattr(cls, "build_object") else cls(
                    **params)
        except TypeError as err:
            raise AssertionError(
                f"{location}: invalid parameter {err.args[0]} when specifying parameters in {specs} "
                f"under key {key}. Valid parameter names are: "
                f"{[name for name in inspect.signature(cls.__init__).parameters.keys()]}"
            )

        return (obj, {class_name: params}) if return_params_dict else obj
Пример #7
0
    def make_simulation_docs(path):
        instantiations = ReflectionHandler.all_nonabstract_subclasses(MotifInstantiationStrategy, "Instantiation", "motif_instantiation_strategy/")
        instantiations = [DocumentationFormat(inst, inst.__name__.replace('Instantiation', ""), DocumentationFormat.LEVELS[2])
                          for inst in instantiations]

        implanting_strategies = ReflectionHandler.all_nonabstract_subclasses(SignalImplantingStrategy, 'Implanting', 'signal_implanting_strategy/')
        implanting_strategies = [DocumentationFormat(implanting, implanting.__name__.replace('Implanting', ""), DocumentationFormat.LEVELS[2])
                                 for implanting in implanting_strategies]

        classes_to_document = [DocumentationFormat(Motif, Motif.__name__, DocumentationFormat.LEVELS[1])] + instantiations + \
                              [DocumentationFormat(Signal, Signal.__name__, DocumentationFormat.LEVELS[1])] + implanting_strategies + \
                               [DocumentationFormat(Implanting, Implanting.__name__, DocumentationFormat.LEVELS[1])]

        with open(path + "simulation.rst", "w") as file:
            for doc_format in classes_to_document:
                write_class_docs(doc_format, file)
Пример #8
0
    def generate_docs(path):
        inst_path = PathBuilder.build(f"{path}instructions/")
        instructions = sorted(ReflectionHandler.all_nonabstract_subclasses(
            Instruction, "Instruction", subdirectory='instructions/'),
                              key=lambda x: x.__name__)

        inst_paths = {}

        for instruction in instructions:
            instruction_name = instruction.__name__[:-11]
            if hasattr(InstructionParser,
                       f"make_{instruction_name.lower()}_docs"):
                fn = getattr(InstructionParser,
                             f"make_{instruction_name.lower()}_docs")
                file_path = fn(inst_path)
            else:
                file_path = InstructionParser.make_docs(
                    instruction, instruction_name, inst_path)

            inst_paths[instruction_name] = file_path

        with open(f'{inst_path}instructions.rst', 'w') as file:
            for key, item in inst_paths.items():
                lines = f"{key}\n---------------------------\n.. include:: {os.path.relpath(item, EnvironmentSettings.source_docs_path)}\n"
                file.writelines(lines)
Пример #9
0
    def _parse_motif(key: str, motif_item: dict) -> Motif:

        motif_dict = copy.deepcopy(motif_item)

        valid_values = ReflectionHandler.all_nonabstract_subclass_basic_names(
            MotifInstantiationStrategy, "Instantiation",
            "motif_instantiation_strategy/")
        instantiation_object = ObjectParser.parse_object(
            motif_item["instantiation"], valid_values, "Instantiation",
            "motif_instantiation_strategy", "MotifParser", key)
        motif_dict["instantiation"] = instantiation_object
        motif_dict["identifier"] = key

        if "name_chain1" in motif_item:
            motif_dict["name_chain1"] = Chain[
                motif_item["name_chain1"].upper()]
        if "name_chain2" in motif_item:
            motif_dict["name_chain2"] = Chain[
                motif_item["name_chain2"].upper()]

        assert "seed" in motif_dict or all(el in motif_dict for el in ["name_chain1", "name_chain2", "seed_chain1", "seed_chain2"]), \
            "MotifParser: please check the documentation for motif definition. Either parameter `seed` has to be set (for simulation in single " \
            "chain data) or all of the parameters `name_chain1`, `name_chain2`, `seed_chain1`, `seed_chain2` (for simulation for paired chain data)."

        motif = Motif(**motif_dict)

        return motif
Пример #10
0
 def get_class(specs, valid_class_names, class_name_ending, class_path,
               location, key):
     class_name = ObjectParser.get_class_name(specs, valid_class_names,
                                              class_name_ending, location,
                                              key)
     cls = ReflectionHandler.get_class_by_name(
         f"{class_name}{class_name_ending}", class_path)
     return cls
Пример #11
0
 def build_object(dataset=None, **params):
     try:
         prepared_parameters = MatchedSequencesEncoder._prepare_parameters(**params)
         encoder = ReflectionHandler.get_class_by_name(MatchedSequencesEncoder.dataset_mapping[dataset.__class__.__name__],
                                                       "reference_encoding/")(**prepared_parameters)
     except ValueError:
         raise ValueError("{} is not defined for dataset of type {}.".format(MatchedSequencesEncoder.__name__,
                                                                             dataset.__class__.__name__))
     return encoder
Пример #12
0
    def parse_instruction(key: str, instruction: dict,
                          symbol_table: SymbolTable, path) -> tuple:
        ParameterValidator.assert_keys_present(list(instruction.keys()),
                                               ["type"],
                                               InstructionParser.__name__, key)
        valid_instructions = [
            cls[:-6]
            for cls in ReflectionHandler.discover_classes_by_partial_name(
                "Parser", "dsl/instruction_parsers/")
        ]
        ParameterValidator.assert_in_valid_list(instruction["type"],
                                                valid_instructions,
                                                "InstructionParser", "type")

        parser = ReflectionHandler.get_class_by_name(
            "{}Parser".format(instruction["type"]), "instruction_parsers/")()
        instruction_object = parser.parse(key, instruction, symbol_table, path)

        symbol_table.add(key, SymbolType.INSTRUCTION, instruction_object)
        return instruction, symbol_table
Пример #13
0
 def build_object(dataset=None, **params):
     try:
         prepared_params = Word2VecEncoder._prepare_parameters(**params)
         encoder = ReflectionHandler.get_class_by_name(
             Word2VecEncoder.dataset_mapping[dataset.__class__.__name__],
             "word2vec/")(**prepared_params)
     except ValueError:
         raise ValueError(
             "{} is not defined for dataset of type {}.".format(
                 Word2VecEncoder.__name__, dataset.__class__.__name__))
     return encoder
Пример #14
0
    def parse(self, key: str, instruction: dict, symbol_table: SymbolTable, path: str = None) -> SubsamplingInstruction:

        valid_keys = ["type", "dataset", "subsampled_dataset_sizes", "dataset_export_formats"]
        ParameterValidator.assert_keys(instruction.keys(), valid_keys, SubsamplingParser.__name__, key)

        dataset_keys = symbol_table.get_keys_by_type(SymbolType.DATASET)
        ParameterValidator.assert_in_valid_list(instruction['dataset'], dataset_keys, SubsamplingParser.__name__, f'{key}/dataset')

        dataset = symbol_table.get(instruction['dataset'])
        ParameterValidator.assert_type_and_value(instruction['subsampled_dataset_sizes'], list, SubsamplingParser.__name__, f'{key}/subsampled_dataset_sizes')
        ParameterValidator.assert_all_type_and_value(instruction['subsampled_dataset_sizes'], int, SubsamplingParser.__name__,
                                                     f'{key}/subsampled_dataset_sizes', 1, dataset.get_example_count())

        valid_export_formats = ReflectionHandler.all_nonabstract_subclass_basic_names(DataExporter, 'Exporter', "dataset_export/")
        ParameterValidator.assert_type_and_value(instruction['dataset_export_formats'], list, SubsamplingParser.__name__, f"{key}/dataset_export_formats")
        ParameterValidator.assert_all_in_valid_list(instruction['dataset_export_formats'], valid_export_formats, SubsamplingParser.__name__, f"{key}/dataset_export_formats")

        return SubsamplingInstruction(dataset=dataset, subsampled_dataset_sizes=instruction['subsampled_dataset_sizes'],
                                      dataset_export_formats=[ReflectionHandler.get_class_by_name(export_format + "Exporter", "dataset_export/")
                                                              for export_format in instruction['dataset_export_formats']], name=key)
Пример #15
0
    def _parse_dataset(key: str, dataset_specs: dict, symbol_table: SymbolTable, result_path: str) -> SymbolTable:
        location = "ImportParser"

        ParameterValidator.assert_keys(list(dataset_specs.keys()), ImportParser.valid_keys, location, f"datasets:{key}", False)

        valid_formats = ReflectionHandler.all_nonabstract_subclass_basic_names(DataImport, "Import", "IO/dataset_import/")
        ParameterValidator.assert_in_valid_list(dataset_specs["format"], valid_formats, location, "format")

        import_cls = ReflectionHandler.get_class_by_name("{}Import".format(dataset_specs["format"]))
        params = ImportParser._prepare_params(dataset_specs, result_path, key)


        if "is_repertoire" in params:
            ParameterValidator.assert_type_and_value(params["is_repertoire"], bool, location, "is_repertoire")

            if params["is_repertoire"] == True:
                if import_cls != IReceptorImport:
                    assert "metadata_file" in params, f"{location}: Missing parameter: metadata_file under {key}/params/"
                    ParameterValidator.assert_type_and_value(params["metadata_file"], str, location, "metadata_file")

            if params["is_repertoire"] == False:
                assert "paired" in params, f"{location}: Missing parameter: paired under {key}/params/"
                ParameterValidator.assert_type_and_value(params["paired"], bool, location, "paired")

                if params["paired"] == True:
                    assert "receptor_chains" in params, f"{location}: Missing parameter: receptor_chains under {key}/params/"
                    ParameterValidator.assert_in_valid_list(params["receptor_chains"], ["_".join(cp.value) for cp in ChainPair], location, "receptor_chains")

        try:
            dataset = import_cls.import_dataset(params, key)
            dataset.name = key
            symbol_table.add(key, SymbolType.DATASET, dataset)
        except KeyError as key_error:
            raise KeyError(f"{key_error}\n\nAn error occurred during parsing of dataset {key}. "
                           f"The keyword {key_error.args[0]} was missing. This either means this argument was "
                           f"not defined under definitions/datasets/{key}/params, or this column was missing from "
                           f"an input data file. ")
        except Exception as ex:
            raise Exception(f"{ex}\n\nAn error occurred while parsing the dataset {key}. See the log above for more details.")

        return symbol_table
Пример #16
0
    def get_documentation():
        doc = str(SubsamplingInstruction.__doc__)

        valid_strategy_values = ReflectionHandler.all_nonabstract_subclass_basic_names(
            DataExporter, "Exporter", "dataset_export/")
        valid_strategy_values = str(valid_strategy_values)[1:-1].replace(
            "'", "`")
        mapping = {
            "Valid formats are class names of any non-abstract class inheriting "
            ":py:obj:`~source.IO.dataset_export.DataExporter.DataExporter`.":
            f"Valid values are: {valid_strategy_values}."
        }
        doc = update_docs_per_mapping(doc, mapping)
        return doc
Пример #17
0
    def _get_implanting_strategy(key: str, signal: dict) -> SignalImplantingStrategy:

        valid_strategies = [cls[:-10] for cls in
                            ReflectionHandler.discover_classes_by_partial_name("Implanting", "simulation/signal_implanting_strategy/")]
        ParameterValidator.assert_in_valid_list(signal["implanting"], valid_strategies, "SignalParser", key)

        defaults = DefaultParamsLoader.load("signal_implanting_strategy/", f"{signal['implanting']}Implanting")
        signal = {**defaults, **signal}

        ParameterValidator.assert_keys_present(list(signal.keys()), ["motifs", "implanting", "sequence_position_weights"], SignalParser.__name__, key)

        implanting_comp = None
        if 'implanting_computation' in signal:
            implanting_comp = signal['implanting_computation'].lower()
            ParameterValidator.assert_in_valid_list(implanting_comp, [el.name.lower() for el in ImplantingComputation], SignalParser.__name__,
                                                    'implanting_computation')
            implanting_comp = ImplantingComputation[implanting_comp.upper()]

        implanting_strategy = ReflectionHandler.get_class_by_name(f"{signal['implanting']}Implanting")(GappedMotifImplanting(),
                                                                                                       signal["sequence_position_weights"],
                                                                                                       implanting_comp)

        return implanting_strategy
Пример #18
0
 def __init__(self,
              distance_metric: DistanceMetricType,
              attributes_to_match: list,
              sequence_batch_size: int,
              context: dict = None,
              name: str = None):
     self.distance_metric = distance_metric
     self.distance_fn = ReflectionHandler.import_function(
         self.distance_metric.value, DistanceMetrics)
     self.attributes_to_match = attributes_to_match
     self.sequence_batch_size = sequence_batch_size
     self.context = context
     self.name = name
     self.comparison = None
Пример #19
0
    def get_documentation():
        initial_doc = str(Signal.__doc__)

        valid_implanting_values = str(
            ReflectionHandler.all_nonabstract_subclass_basic_names(
                SignalImplantingStrategy, 'Implanting',
                'signal_implanting_strategy/'))[1:-1].replace("'", "`")

        docs_mapping = {
            "Valid values for this argument are class names of different signal implanting strategies.":
            f"Valid values are: {valid_implanting_values}"
        }

        doc = update_docs_per_mapping(initial_doc, docs_mapping)
        return doc
Пример #20
0
    def make_reports_docs(path):
        filename = "reports.rst"

        open(path + filename, "w").close()

        for report_type_class in [DataReport, EncodingReport, MLReport, TrainMLModelReport, MultiDatasetReport]:
            with open(path + filename, "a") as file:
                doc_format = DocumentationFormat(cls=report_type_class,
                                                 cls_name=f"**{report_type_class.get_title()}**",
                                                 level_heading=DocumentationFormat.LEVELS[1])
                write_class_docs(doc_format, file)

            subdir = DefaultParamsLoader.convert_to_snake_case(report_type_class.__name__) + "s"

            classes = ReflectionHandler.all_nonabstract_subclasses(report_type_class, "", f"reports/{subdir}/")
            make_docs(path, classes, filename, "", "a")
Пример #21
0
    def _parse_report(key: str, params: dict, symbol_table: SymbolTable):
        valid_values = ReflectionHandler.all_nonabstract_subclass_basic_names(
            Report, "", "reports/")
        report_object, params = ObjectParser.parse_object(
            params,
            valid_values,
            "",
            "reports/",
            "ReportParser",
            key,
            builder=True,
            return_params_dict=True)

        symbol_table.add(key, SymbolType.REPORT, report_object)

        return symbol_table, params
Пример #22
0
    def get_documentation():
        doc = str(Motif.__doc__)

        valid_strategy_values = ReflectionHandler.all_nonabstract_subclass_basic_names(
            MotifInstantiationStrategy, "Instantiation",
            "motif_instantiation_strategy/")
        valid_strategy_values = str(valid_strategy_values)[1:-1].replace(
            "'", "`")
        chain_values = str([name for name in Chain])[1:-1].replace("'", "`")
        mapping = {
            "It should be one of the classes inheriting MotifInstantiationStrategy.":
            f"Valid values are: {valid_strategy_values}.",
            "The value should be an instance of :py:obj:`~source.data_model.receptor.receptor_sequence.Chain.Chain`.":
            f"Valid values are: {chain_values}."
        }
        doc = update_docs_per_mapping(doc, mapping)
        return doc
Пример #23
0
    def _parse_sequence(key: str, preproc_sequence: list,
                        symbol_table: SymbolTable) -> SymbolTable:

        sequence = []

        valid_preprocessing_classes = ReflectionHandler.all_nonabstract_subclass_basic_names(
            Preprocessor, "", "preprocessing/")

        for item in preproc_sequence:
            for step_key, step in item.items():
                obj, params = ObjectParser.parse_object(
                    step, valid_preprocessing_classes, "", "preprocessing/",
                    "PreprocessingParser", step_key, True, True)
                step = params
                sequence.append(obj)

        symbol_table.add(key, SymbolType.PREPROCESSING, sequence)
        return symbol_table
Пример #24
0
    def parse_encoder(key: str, specs: dict):
        class_path = "encodings"
        valid_encoders = ReflectionHandler.all_nonabstract_subclass_basic_names(
            DatasetEncoder, "Encoder", class_path)
        encoder = ObjectParser.get_class(specs, valid_encoders, "Encoder",
                                         class_path, "EncodingParser", key)
        params = ObjectParser.get_all_params(specs, class_path,
                                             encoder.__name__[:-7], key)

        required_params = [
            p for p in list(
                inspect.signature(encoder.__init__).parameters.keys())
            if p != "self"
        ]
        ParameterValidator.assert_all_in_valid_list(
            params.keys(), required_params, "EncoderParser",
            f"{key}/{encoder.__name__.replace('Encoder', '')}")

        return encoder, params
Пример #25
0
def run_immuneML(namespace: argparse.Namespace):

    if os.path.isdir(namespace.result_path) and len(
            os.listdir(namespace.result_path)) != 0:
        raise ValueError(
            f"Directory {namespace.result_path} already exists. Please specify a new output directory for the analysis."
        )
    PathBuilder.build(namespace.result_path)

    logging.basicConfig(filename=namespace.result_path + "/log.txt",
                        level=logging.INFO,
                        format='%(asctime)s %(levelname)s: %(message)s')
    warnings.showwarning = lambda message, category, filename, lineno, file=None, line=None: logging.warning(
        message)

    if namespace.tool is None:
        app = ImmuneMLApp(namespace.specification_path, namespace.result_path)
    else:
        app_cls = ReflectionHandler.get_class_by_name(namespace.tool, "api/")
        app = app_cls(**vars(namespace))

    app.run()
Пример #26
0
    def prepare_reference(reference_params: dict, location: str, paired: bool):
        ParameterValidator.assert_keys(list(reference_params.keys()),
                                       ["format", "params"], location,
                                       "reference")

        seq_import_params = reference_params[
            "params"] if "params" in reference_params else {}

        assert os.path.isfile(seq_import_params["path"]), f"{location}: the file {seq_import_params['path']} does not exist. " \
                                                  f"Specify the correct path under reference."

        if "paired" in seq_import_params:
            assert seq_import_params[
                "paired"] == paired, f"{location}: paired must be {paired} for SequenceImport"
        else:
            seq_import_params["paired"] = paired

        format_str = reference_params["format"]

        if format_str == "IRIS":  # todo refactor this when refactoring IRISSequenceImport
            receptors = IRISSequenceImport.import_items(**seq_import_params)
        else:
            import_class = ReflectionHandler.get_class_by_name(
                "{}Import".format(format_str))
            params = DefaultParamsLoader.load(
                EnvironmentSettings.default_params_path + "datasets/",
                DefaultParamsLoader.convert_to_snake_case(format_str))
            for key, value in seq_import_params.items():
                params[key] = value
            params["paired"] = paired

            processed_params = DatasetImportParams.build_object(**params)

            receptors = ImportHelper.import_items(
                import_class, reference_params["params"]["path"],
                processed_params)

        return receptors
Пример #27
0
 def get_documentation():
     doc = str(TrainMLModelInstruction.__doc__)
     valid_values = str([metric.name.lower()
                         for metric in Metric])[1:-1].replace("'", "`")
     valid_strategies = str(ReflectionHandler.all_nonabstract_subclass_basic_names(HPOptimizationStrategy, "",
                                                                                   "hyperparameter_optimization/strategy/"))[1:-1]\
         .replace("'", "`")
     mapping = {
         "dataset (Dataset)":
         "dataset",
         "hp_strategy (HPOptimizationStrategy)":
         "strategy",
         "hp_settings":
         "settings",
         "assessment (SplitConfig)":
         "assessment",
         "selection (SplitConfig)":
         "selection",
         "optimization_metric (Metric)":
         "optimization_metric",
         "label_configuration (LabelConfiguration)":
         "labels (list)",
         "data_reports":
         "reports",
         "a list of metrics":
         f"a list of metrics ({valid_values})",
         "a metric to use for optimization":
         f"a metric to use for optimization (one of {valid_values})",
         "Valid values are objects of any class inheriting :py:obj:`~source.hyperparameter_optimization.strategy."
         "HPOptimizationStrategy.HPOptimizationStrategy`.":
         f"Valid values are: {valid_strategies}.",
         "the reports to be specified here have to be :py:obj:`source.reports.train_ml_model_reports.TrainMLModelReport.TrainMLModelReport` reports.":
         f"the reports that can be provided here are :ref:`{TrainMLModelReport.get_title()}`."
     }
     doc = update_docs_per_mapping(doc, mapping)
     return doc
Пример #28
0
def parse_commandline_arguments(args):
    ReflectionHandler.get_classes_by_partial_name("", "ml_methods/")
    ml_method_names = [
        cl.__name__
        for cl in ReflectionHandler.all_nonabstract_subclasses(MLMethod)
    ] + ["SimpleLogisticRegression"]

    parser = argparse.ArgumentParser(
        description="tool for building immuneML Galaxy YAML from arguments")
    parser.add_argument(
        "-o",
        "--output_path",
        required=True,
        help="Output location for the generated yaml file (directiory).")
    parser.add_argument(
        "-f",
        "--file_name",
        default="specs.yaml",
        help=
        "Output file name for the yaml file. Default name is 'specs.yaml' if not specified."
    )
    parser.add_argument(
        "-l",
        "--labels",
        required=True,
        help=
        "Which metadata labels should be predicted for the dataset (separated by comma)."
    )
    parser.add_argument(
        "-m",
        "--ml_methods",
        nargs="+",
        choices=ml_method_names,
        required=True,
        help="Which machine learning methods should be applied.")
    parser.add_argument("-t",
                        "--training_percentage",
                        type=float,
                        required=True,
                        help="The percentage of data used for training.")
    parser.add_argument(
        "-c",
        "--split_count",
        type=int,
        required=True,
        help=
        "The number of times to repeat the training process with a different random split of the data."
    )
    parser.add_argument(
        "-s",
        "--sequence_type",
        choices=["complete", "subsequence"],
        default=["subsequence"],
        nargs="+",
        help="Whether complete CDR3 sequences are used, or k-mer subsequences."
    )
    parser.add_argument(
        "-p",
        "--position_type",
        choices=["invariant", "positional"],
        nargs="+",
        help=
        "Whether IMGT-positional information is used for k-mers, or the k-mer positions are position-invariant."
    )
    parser.add_argument("-g",
                        "--gap_type",
                        choices=["gapped", "ungapped"],
                        nargs="+",
                        help="Whether the k-mers contain gaps.")
    parser.add_argument("-k", "--k", type=int, nargs="+", help="K-mer size.")
    parser.add_argument("-kl",
                        "--k_left",
                        type=int,
                        nargs="+",
                        help="Length before gap when k-mers are used.")
    parser.add_argument("-kr",
                        "--k_right",
                        type=int,
                        nargs="+",
                        help="Length after gap when k-mers are used.")
    parser.add_argument("-gi",
                        "--min_gap",
                        type=int,
                        nargs="+",
                        help="Minimal gap length when gapped k-mers are used.")
    parser.add_argument("-ga",
                        "--max_gap",
                        type=int,
                        nargs="+",
                        help="Maximal gap length when gapped k-mers are used.")
    parser.add_argument(
        "-r",
        "--reads",
        choices=[ReadsType.UNIQUE.value, ReadsType.ALL.value],
        nargs="+",
        default=[ReadsType.UNIQUE.value],
        help=
        "Whether k-mer counts should be scaled by unique clonotypes or all observed receptor sequences"
    )

    return parser.parse_args(args)
Пример #29
0
 def import_encoder(config: MLMethodConfiguration, config_dir: str):
     encoder_class = ReflectionHandler.get_class_by_name(
         config.encoding_class)
     encoder = encoder_class.load_encoder(config_dir + config.encoding_file)
     return encoder
Пример #30
0
 def parse_encoder_internal(short_class_name: str, encoder_params: dict):
     encoder_class = ReflectionHandler.get_class_by_name(
         f"{short_class_name}Encoder", "encodings")
     params = ObjectParser.get_all_params(
         {short_class_name: encoder_params}, "encodings", short_class_name)
     return encoder_class, params, params