From 9392809cd1de2df3500a7e52ee18018ecebf6da8 Mon Sep 17 00:00:00 2001
From: dcarron <daniel.carron@idiap.ch>
Date: Mon, 17 Jun 2024 13:27:52 +0200
Subject: [PATCH] [classification.scripts] Move upload script to classification
 package

---
 src/mednet/libs/classification/scripts/cli.py |  3 +
 .../classification}/scripts/upload.py         | 58 ++-----------
 src/mednet/libs/common/scripts/upload.py      | 83 +++++++++++++++++++
 src/mednet/scripts/cli.py                     |  2 -
 4 files changed, 93 insertions(+), 53 deletions(-)
 rename src/mednet/{ => libs/classification}/scripts/upload.py (79%)
 create mode 100644 src/mednet/libs/common/scripts/upload.py

diff --git a/src/mednet/libs/classification/scripts/cli.py b/src/mednet/libs/classification/scripts/cli.py
index 34ba1ff3..f922cd7d 100644
--- a/src/mednet/libs/classification/scripts/cli.py
+++ b/src/mednet/libs/classification/scripts/cli.py
@@ -37,6 +37,9 @@ classification.add_command(
 classification.add_command(
     importlib.import_module("..experiment", package=__name__).experiment,
 )
+classification.add_command(
+    importlib.import_module("..upload", package=__name__).upload,
+)
 
 
 @click.group(
diff --git a/src/mednet/scripts/upload.py b/src/mednet/libs/classification/scripts/upload.py
similarity index 79%
rename from src/mednet/scripts/upload.py
rename to src/mednet/libs/classification/scripts/upload.py
index a10d6518..f4b0114c 100644
--- a/src/mednet/scripts/upload.py
+++ b/src/mednet/libs/classification/scripts/upload.py
@@ -8,6 +8,7 @@ import click
 from clapper.click import ResourceOption, verbosity_option
 from clapper.logging import setup
 from mednet.libs.common.scripts.click import ConfigCommand
+from mednet.libs.common.scripts.upload import reusable_options
 
 logger = setup(__name__.split(".")[0], format="%(levelname)s: %(message)s")
 
@@ -21,74 +22,29 @@ logger = setup(__name__.split(".")[0], format="%(levelname)s: %(message)s")
 
    .. code:: sh
 
-      mednet upload --experiment-folder=/path/to/results
+      mednet classification upload --experiment-folder=/path/to/results
 
 2. Upload an existing experiment result with an experiment name:
 
    .. code:: sh
 
-      mednet upload --experiment-folder=/path/to/results --experiment-name=exp-pasa_mc
+      mednet classification upload --experiment-folder=/path/to/results --experiment-name=exp-pasa_mc
 
 3. Upload an existing experiment result with a run name:
 
    .. code:: sh
 
-      mednet upload --experiment-folder=/path/to/results --run-name=run-1
+      mednet classification upload --experiment-folder=/path/to/results --run-name=run-1
 
 4. Upload an existing experiment result with defining a size limit of 20MB for each file:
 
    .. code:: sh
 
-      mednet upload --experiment-folder=/path/to/results --upload-limit-mb=20
+      mednet classification upload --experiment-folder=/path/to/results --upload-limit-mb=20
 
 """,
 )
-@click.option(
-    "--project-path",
-    "-p",
-    help="Path to the project where to upload model entries",
-    required=True,
-    type=str,
-    default="biosignal/software/mednet",
-    show_default=True,
-    cls=ResourceOption,
-)
-@click.option(
-    "--experiment-folder",
-    "-f",
-    help="Directory in which to upload results from",
-    required=True,
-    type=click.Path(
-        file_okay=False,
-        dir_okay=True,
-        path_type=pathlib.Path,
-    ),
-    default="results",
-    show_default=True,
-    cls=ResourceOption,
-)
-@click.option(
-    "--experiment-name",
-    "-e",
-    help='A string indicating the experiment name (e.g. "exp-pasa-mc" or "exp-densenet-mc-ch")',
-    cls=ResourceOption,
-)
-@click.option(
-    "--run-name",
-    "-r",
-    help='A string indicating the run name (e.g. "run-1")',
-    cls=ResourceOption,
-)
-@click.option(
-    "--upload-limit-mb",
-    "-l",
-    help="Maximim upload size in MB (set to 0 for no limit).",
-    show_default=True,
-    required=True,
-    default=10,
-    type=click.IntRange(min=0),
-    cls=ResourceOption,
-)
+@reusable_options
 @verbosity_option(logger=logger, cls=ResourceOption, expose_value=False)
 def upload(
     project_path: str,
@@ -123,8 +79,8 @@ def upload(
     )
 
     # get train files
-    train_log_file = experiment_folder / "trainlog.pdf"
     train_folder = experiment_folder / "model"
+    train_log_file = train_folder / "trainlog.pdf"
     train_meta_file = train_folder / "meta.json"
     train_model_file = get_checkpoint_to_run_inference(train_folder)
     train_files = [train_meta_file, train_model_file, train_log_file]
diff --git a/src/mednet/libs/common/scripts/upload.py b/src/mednet/libs/common/scripts/upload.py
new file mode 100644
index 00000000..5302adbc
--- /dev/null
+++ b/src/mednet/libs/common/scripts/upload.py
@@ -0,0 +1,83 @@
+# SPDX-FileCopyrightText: Copyright © 2023 Idiap Research Institute <contact@idiap.ch>
+#
+# SPDX-License-Identifier: GPL-3.0-or-later
+
+import functools
+import pathlib
+
+import click
+from clapper.click import ResourceOption
+from clapper.logging import setup
+
+# logger = setup(__name__.split(".")[0], format="%(levelname)s: %(message)s")
+logger = setup("mednet", format="%(levelname)s: %(message)s")
+
+
+def reusable_options(f):
+    """Wrap reusable upload script options.
+
+    This decorator equips the target function ``f`` with all (reusable)
+    ``upload`` script options.
+
+    Parameters
+    ----------
+    f
+        The target function to equip with options.  This function must have
+        parameters that accept such options.
+
+    Returns
+    -------
+        The decorated version of function ``f``
+    """
+
+    @click.option(
+        "--project-path",
+        "-p",
+        help="Path to the project where to upload model entries",
+        required=True,
+        type=str,
+        default="biosignal/software/mednet",
+        show_default=True,
+        cls=ResourceOption,
+    )
+    @click.option(
+        "--experiment-folder",
+        "-f",
+        help="Directory in which to upload results from",
+        required=True,
+        type=click.Path(
+            file_okay=False,
+            dir_okay=True,
+            path_type=pathlib.Path,
+        ),
+        default="results",
+        show_default=True,
+        cls=ResourceOption,
+    )
+    @click.option(
+        "--experiment-name",
+        "-e",
+        help='A string indicating the experiment name (e.g. "exp-pasa-mc" or "exp-densenet-mc-ch")',
+        cls=ResourceOption,
+    )
+    @click.option(
+        "--run-name",
+        "-r",
+        help='A string indicating the run name (e.g. "run-1")',
+        cls=ResourceOption,
+    )
+    @click.option(
+        "--upload-limit-mb",
+        "-l",
+        help="Maximim upload size in MB (set to 0 for no limit).",
+        show_default=True,
+        required=True,
+        default=10,
+        type=click.IntRange(min=0),
+        cls=ResourceOption,
+    )
+    @functools.wraps(f)
+    def wrapper_reusable_options(*args, **kwargs):
+        return f(*args, **kwargs)
+
+    return wrapper_reusable_options
diff --git a/src/mednet/scripts/cli.py b/src/mednet/scripts/cli.py
index 3b9beb51..59df4ef6 100644
--- a/src/mednet/scripts/cli.py
+++ b/src/mednet/scripts/cli.py
@@ -4,7 +4,6 @@ from mednet.libs.classification.scripts.cli import classification
 from mednet.libs.segmentation.scripts.cli import segmentation
 
 from .info import info
-from .upload import upload
 
 
 @click.group(
@@ -19,4 +18,3 @@ def cli():
 cli.add_command(classification)
 cli.add_command(segmentation)
 cli.add_command(info)
-cli.add_command(upload)
-- 
GitLab