Skip to content

Commit

Permalink
merge cli and maps json function
Browse files Browse the repository at this point in the history
  • Loading branch information
camillebrianceau committed Oct 14, 2024
1 parent 6f6933a commit 7159296
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
8 changes: 7 additions & 1 deletion clinicadl/commandline/pipelines/interpret/cli.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from pathlib import Path

import click

from clinicadl.commandline import arguments
Expand Down Expand Up @@ -40,8 +42,12 @@ def cli(**kwargs):
NAME is the name of the saliency map task.
METHOD is the method used to extract an attribution map.
"""
from clinicadl.utils.iotools.train_utils import merge_cli_and_maps_json_options

interpret_config = InterpretConfig(**kwargs)
dict_ = merge_cli_and_maps_json_options(
Path(kwargs["input_maps"]) / "maps.json", **kwargs
)
interpret_config = InterpretConfig(**dict_)
predict_manager = Predictor(interpret_config)
predict_manager.interpret()

Expand Down
6 changes: 5 additions & 1 deletion clinicadl/maps_manager/maps_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from clinicadl.metrics.utils import (
check_selection_metric,
)
from clinicadl.predictor.utils import get_prediction
from clinicadl.predict.utils import get_prediction
from clinicadl.splitter.config import SplitterConfig
from clinicadl.splitter.splitter import Splitter
from clinicadl.trainer.tasks_utils import (
Expand Down Expand Up @@ -139,6 +139,10 @@ def __getattr__(self, name):
else:
raise AttributeError(f"'MapsManager' object has no attribute '{name}'")

###################################
# High-level functions templates #
###################################

###############################
# Checks #
###############################
Expand Down
33 changes: 33 additions & 0 deletions clinicadl/utils/iotools/train_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,36 @@ def merge_cli_and_config_file_options(task: Task, **kwargs) -> Dict[str, Any]:
pass
###
return options


def merge_cli_and_maps_json_options(maps_json: Path, **kwargs) -> Dict[str, Any]:
"""
Merges options from the CLI (passed by the user) and from the config file
(if it exists).
Priority is given to options passed by the user via the CLI. If it is not
provided, it will look for the option in the possible config file.
If an option is not passed by the user and not found in the config file, it will
not be in the output.
Parameters
----------
task : Task
The task that is performed (e.g. classification).
Returns
-------
Dict[str, Any]
A dictionary with training options.
"""
from clinicadl.caps_dataset.caps_dataset_utils import read_json

options = read_json(maps_json)
for arg in kwargs:
if (
click.get_current_context().get_parameter_source(arg)
== ParameterSource.COMMANDLINE
):
options[arg] = kwargs[arg]

return options

0 comments on commit 7159296

Please sign in to comment.