Skip to content

Commit

Permalink
ENH: refactor main scripts (theroggy#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
theroggy authored Oct 9, 2024
1 parent f2b7cd5 commit eef4329
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 25 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
`dirs.images_periodic_dir` instead.
- Rename section `dirs` to `paths`.
- Remove `general.run_id`.
- Rename some main modules to make naming more logical with non-cropclassification
markers being added (#162)

### Improvements

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"""
Main script to do a classification.
"""
"""Run a crop classification."""

import logging
import os
Expand All @@ -21,10 +19,6 @@
from cropclassification.preprocess import prepare_input
from cropclassification.preprocess import timeseries as ts

# -------------------------------------------------------------
# First define/init some general variables/constants
# -------------------------------------------------------------


def calc_marker_task(
config_paths: list[Path],
Expand Down
5 changes: 2 additions & 3 deletions cropclassification/calc_periodic_mosaic.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""Generate periodic mosaics."""

from datetime import datetime, timedelta
from pathlib import Path

Expand All @@ -15,9 +17,6 @@ def calc_periodic_mosaic_task(config_paths: list[Path], default_basedir: Path):
default_basedir (Path): the dir to resolve relative paths in the config
file to.
Raises:
Exception: [description]
Exception: [description]
"""
# Read the configuration files
conf.read_config(config_paths=config_paths, default_basedir=default_basedir)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"""
Process the tasks in the tasks directory.
"""
"""Run the tasks in the tasks directory."""

import argparse
import configparser
Expand All @@ -10,8 +8,6 @@
# Import geofilops here already, if tensorflow is loaded first leads to dll load errors
import geofileops as gfo # noqa: F401

sys.path.insert(0, str(Path(__file__).resolve().parent.parent))


def _get_version():
version_path = Path(__file__).resolve().parent / "version.txt"
Expand Down Expand Up @@ -45,11 +41,11 @@ def main():

# If tasks dir is specified, use it
if args.tasksdir is not None:
return cropclassification(tasksdir=Path(args.tasksdir))
return run_tasks(tasksdir=Path(args.tasksdir))
else:
usertasksdir = Path.home() / "cropclassification" / "tasks"
if usertasksdir.exists():
return cropclassification(tasksdir=usertasksdir)
return run_tasks(tasksdir=usertasksdir)
else:
print(
f"Error: no tasksdir specified, and default tasks dir ({usertasksdir}) "
Expand All @@ -59,7 +55,7 @@ def main():
sys.exit(1)


def cropclassification(tasksdir: Path, config_overrules: list[str] = []):
def run_tasks(tasksdir: Path, config_overrules: list[str] = []):
# Get the tasks and treat them
task_paths = sorted(tasksdir.glob("task_*.ini"))
for task_path in task_paths:
Expand Down Expand Up @@ -100,10 +96,10 @@ def cropclassification(tasksdir: Path, config_overrules: list[str] = []):
).resolve()
config_paths.append(Path(config_file_formatted))

if action == "calc_marker":
from cropclassification import calc_marker
if action in ("calc_marker", "calc_cropclass"):
from cropclassification import calc_cropclass

calc_marker.calc_marker_task(
calc_cropclass.calc_marker_task(
config_paths=config_paths,
default_basedir=default_basedir,
config_overrules=config_overrules,
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
],
entry_points="""
[console_scripts]
cropclassification=cropclassification.cropclassification:main
cropclassification=cropclassification.taskrunner:main
""",
classifiers=[
"Programming Language :: Python :: 3",
Expand Down
6 changes: 3 additions & 3 deletions tests/test_end2end.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pytest
from pandas.api.types import is_numeric_dtype

from cropclassification import cropclassification
from cropclassification import taskrunner
from cropclassification.util.zonal_stats_bulk._zonal_stats_bulk_pyqgis import HAS_QGIS
from tests import test_helper

Expand Down Expand Up @@ -34,7 +34,7 @@ def test_task_calc_marker(tmp_path, balancing_strategy, cross_pred_models):

shutil.copy(src=ignore_dir / task_ini, dst=tasks_dir / task_ini)

cropclassification.cropclassification(
taskrunner.run_tasks(
tasksdir=tasks_dir,
config_overrules=[
f"marker.balancing_strategy={balancing_strategy}",
Expand Down Expand Up @@ -80,7 +80,7 @@ def test_task_calc_periodic_mosaic(tmp_path):

shutil.copy(src=ignore_dir / task_ini, dst=tasks_dir / task_ini)

cropclassification.cropclassification(tasksdir=tasks_dir)
taskrunner.run_tasks(tasksdir=tasks_dir)

# Check if a log file was written
log_dir = marker_basedir / "log"
Expand Down

0 comments on commit eef4329

Please sign in to comment.