Skip to content

Commit

Permalink
Add platform support to Makita (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
J535D165 authored Jun 10, 2023
1 parent fc1d271 commit 411f45b
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 27 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/ci-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ name: test-suite
on: [push, pull_request]
jobs:
test-template-and-lint:
name: test-templates-and-scripts
runs-on: ubuntu-latest
strategy:
matrix:
os: [macos-latest, windows-latest, ubuntu-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@master
- uses: actions/setup-python@v4
Expand Down Expand Up @@ -38,6 +40,7 @@ jobs:
asreview makita template multiple_models | tee output.txt
grep -q "ERROR" output.txt && exit 1 || true
- name: Run ShellCheck
if: ${{ matrix.os != 'windows-latest' }}
uses: ludeeus/action-shellcheck@master
with:
scandir: './tmp'
Expand Down
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,20 @@ sh jobs.sh

The `jobs.sh` script is a shell script that runs all jobs in the project folder.

### Windows support
### Platform support

For Windows users, ASReview Makita offers support for batch files. Use the `-f` option to generate a `jobs.bat` script instead of the default `jobs.sh` script.
By default, ASReview Makita renders job files for the platform of rendering. It is possible to render jobs for other platforms as well. Use the argument `--platform` with values "Windows", "Linux", or "Darwin" (MacOS) to change the output.

```console
asreview makita template basic -f jobs.bat
asreview makita template basic --platform Windows
```

By default, the job file depends on the platform. Windows users will see a `jobs.bat` file, while other users will see `jobs.sh`. You can overwrite this with

```console
asreview makita template basic --job_file my_jobs_file.my_ext
```

> Alternatively, Windows CMD does not support the `sh` command, and a bash shell is required. You can use tools such as Git Bash, Cygwin, WSL, etc. to run the `jobs.sh` script instead.

## Templates

Expand Down Expand Up @@ -203,7 +208,7 @@ Use `-s` (source) and `-o` (output) to tweak paths.

Some scripts are added automatically to the folder, as they are part of the
template. For example, the `get_plot.py` script is added to the generated folder
when using any template, as it is used to generate the plots.
when using any template, as it is used to generate the plots.

Still, `get_plot.py` can be used on its own, as it is a standalone script. To use it,
use `-s` (source) and `-o` (output) to tweak paths.
Expand Down
43 changes: 25 additions & 18 deletions asreviewcontrib/makita/entrypoint.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import argparse
import os
from pathlib import Path

from asreview.entry_points import BaseEntryPoint
Expand All @@ -21,13 +22,6 @@ def is_valid_template(fp):
raise ValueError(f"Template {fp} not found")


def _valid_job_file(param):
ext = Path(param).suffix
if ext.lower() not in (".sh", ".bat", ".yaml"):
raise argparse.ArgumentTypeError("File must have a .sh, .bat, .yaml extension")
return param


def _shell_to_batch(job):
job = f"@ echo off\nCOLOR E0{job}"
job = job.replace("#", "::")
Expand Down Expand Up @@ -133,7 +127,8 @@ def _template(self, args_name, args_program):
init_seed=args.init_seed,
model_seed=args.model_seed,
fp_template=fp_template,
job_file=args.f,
job_file=args.job_file,
platform_sys=args.platform,
)

elif args_program.name in ["arfi"]:
Expand All @@ -145,7 +140,8 @@ def _template(self, args_name, args_program):
init_seed=args.init_seed,
model_seed=args.model_seed,
fp_template=fp_template,
job_file=args.f,
job_file=args.job_file,
platform_sys=args.platform,
)

elif args_program.name in ["multiple_models"]:
Expand All @@ -160,7 +156,8 @@ def _template(self, args_name, args_program):
all_feature_extractors=args.feature_extractors,
impossible_models=args.impossible_models,
fp_template=fp_template,
job_file=args.f,
job_file=args.job_file,
platform_sys=args.platform,
)

else:
Expand All @@ -171,17 +168,20 @@ def _template(self, args_name, args_program):
init_seed=args.init_seed,
model_seed=args.model_seed,
fp_template=fp_template,
job_file=args.f,
job_file=args.job_file,
platform_sys=args.platform,
)

if Path(args.f).suffix.lower() == ".bat":
if args.platform == "Windows" or (args.platform is None and os.name == "nt"):
job = _shell_to_batch(job)
job_file = "jobs.bat" if args.job_file is None else args.job_file
else:
job_file = "jobs.sh" if args.job_file is None else args.job_file

# store result in output folder
Path(args.f).parent.mkdir(parents=True, exist_ok=True)
with open(args.f, "w") as f:
with open(job_file, "w") as f:
f.write(job)
print(f"Rendered template {args_program.name} and saved to {args.f}")
print(f"Rendered template {args_program.name} and saved to {job_file}")

def _add_script(self, args_name, args_program):
# initialize file handler
Expand Down Expand Up @@ -235,10 +235,11 @@ def _parse_arguments_template():
parser = argparse.ArgumentParser(prog="asreview makita", add_help=True)

parser.add_argument(
"--job_file",
"-f",
type=_valid_job_file,
default="jobs.sh",
help="Script with study jobs to run",
type=str,
help="The name of the file with jobs. Default "
"jobs.bat for Windows, otherwise jobs.sh.",
)
parser.add_argument("-s", type=str, default="data", help="Dataset folder")
parser.add_argument("-o", type=str, default="output", help="Output folder")
Expand All @@ -257,6 +258,12 @@ def _parse_arguments_template():
parser.add_argument(
"--template", type=str, help="Overwrite template with template file path."
)
parser.add_argument(
"--platform",
type=str,
help="Platform to run jobs: Windows, Darwin, Linux. "
"Default: the system of rendering templates.",
)
return parser


Expand Down
12 changes: 11 additions & 1 deletion asreviewcontrib/makita/template_arfi.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Render ARFI template."""

import os
import platform
from pathlib import Path

import numpy as np
Expand All @@ -19,9 +21,16 @@ def render_jobs_arfi(
init_seed=535,
model_seed=165,
fp_template=None,
job_file="jobs.sh",
job_file=None,
platform_sys=None,
):
"""Render jobs."""

if not platform_sys:
platform_sys = platform.system()
if not job_file:
job_file = "jobs.bat" if os.name == "nt" else "jobs.sh"

params = []

# initialize file handler
Expand Down Expand Up @@ -86,6 +95,7 @@ def render_jobs_arfi(
"init_seed": init_seed,
"output_folder": output_folder,
"scripts_folder": scripts_folder,
"platform": platform_sys,
"version": __version__,
}
)
Expand Down
10 changes: 10 additions & 0 deletions asreviewcontrib/makita/template_basic.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Render basic template."""

import os
import platform
from pathlib import Path

from cfgtemplater.config_template import ConfigTemplate
Expand All @@ -18,8 +20,15 @@ def render_jobs_basic(
model_seed=165,
fp_template=None,
job_file=None,
platform_sys=None,
):
"""Render jobs."""

if not platform_sys:
platform_sys = platform.system()
if not job_file:
job_file = "jobs.bat" if os.name == "nt" else "jobs.sh"

params = []

# initialize file handler
Expand Down Expand Up @@ -83,6 +92,7 @@ def render_jobs_basic(
"datasets": params,
"output_folder": output_folder,
"scripts_folder": scripts_folder,
"platform_sys": platform_sys,
"version": __version__,
}
)
12 changes: 11 additions & 1 deletion asreviewcontrib/makita/template_multiple_models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Render multiple_models template."""

import os
import platform
from pathlib import Path

from cfgtemplater.config_template import ConfigTemplate
Expand All @@ -20,7 +22,8 @@ def render_jobs_multiple_models(
all_feature_extractors=None,
impossible_models=None,
fp_template=None,
job_file="jobs.sh",
job_file=None,
platform_sys=None,
):
if all_classifiers is None:
all_classifiers = ["logistic", "nb", "rf", "svm"]
Expand All @@ -32,6 +35,12 @@ def render_jobs_multiple_models(
impossible_models = ["nb,doc2vec", "nb,sbert"]

"""Render jobs."""

if not platform_sys:
platform_sys = platform.system()
if not job_file:
job_file = "jobs.bat" if os.name == "nt" else "jobs.sh"

params = []

# initialize file handler
Expand Down Expand Up @@ -95,6 +104,7 @@ def render_jobs_multiple_models(
"output_folder": output_folder,
"n_runs": n_runs,
"scripts_folder": scripts_folder,
"platform": platform_sys,
"version": __version__,
"all_classifiers": all_classifiers,
"all_feature_extractors": all_feature_extractors,
Expand Down

0 comments on commit 411f45b

Please sign in to comment.