Skip to content

Commit

Permalink
Merge pull request #25 from AgPipeline/update_info
Browse files Browse the repository at this point in the history
Update info and adding output file command line option
  • Loading branch information
Chris-Schnaufer authored Oct 9, 2020
2 parents 9e13085 + b04f86b commit bb8b899
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ The command line parameters after the image name are passed to the software insi
Note that the paths provided are relative to the running image (see the --mount option specified above).

- `--working_space "/mnt"` specifies the folder to use as a workspace
- `--metadata "/mnt/experiment.yaml"` is the name of the source metadata to be cleaned
- `--metadata "/mnt/experiment.yaml"` is the name of the source metadata
- `"/mnt/orthomosaic.tif"` is the name of the image to mask

## Acceptance Testing
Expand Down
10 changes: 5 additions & 5 deletions configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ class ConfigurationSoilmask(Configuration):
# pylint: disable=too-few-public-methods

# The version number of the transformer
transformer_version = '2.0'
transformer_version = '2.2'

# The transformer description
transformer_description = 'Stereo RGB Image Enhancement & Masking'
transformer_description = 'RGB Image Soil Masking'

# Short name of the transformer
transformer_name = 'terra.stereo-rgb.rgbmask'
transformer_name = 'soilmask'

# The name of the author of the extractor
author_name = 'Chris Schnaufer'
Expand All @@ -24,7 +24,7 @@ class ConfigurationSoilmask(Configuration):
author_email = '[email protected]'

# Repository URI of where the source code lives
repository = 'https://github.com/Chris-Schnaufer/rgbmask.git'
repository = 'https://github.com/AgPipeline/transformer-soilmask'

# Contributors to this transformer
contributors = ['Max Burnette', 'Zongyang Li', 'Todd Nicholson']
Expand All @@ -33,4 +33,4 @@ class ConfigurationSoilmask(Configuration):
transformer_sensor = 'stereoTop'

# The transformer type (eg: 'rgbmask', 'plotclipper')
transformer_type = 'rgb_mask'
transformer_type = 'rgbmask'
21 changes: 19 additions & 2 deletions soilmask.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"""Soil masking Transformer
"""

import argparse
import logging
import os
import numpy as np
Expand Down Expand Up @@ -327,6 +328,16 @@ def supported_file_ext(self) -> tuple:
"""Returns a tuple of supported file extensions in lowercase (with the preceeding dot: eg '.tif')"""
return '.tiff', '.tif'

def add_parameters(self, parser: argparse.ArgumentParser) -> None:
"""Adds parameters
Arguments:
parser: instance of argparse
"""
# pylint: disable=no-self-use
parser.add_argument('--out_file', type=str, help='the path to save the masked file to')

parser.epilog += ' Mask files are saved with the .msk filename extension added when not specified.'

def check_continue(self, environment: Environment, check_md: dict, transformer_md: list,
full_md: list) -> tuple:
"""Checks if conditions are right for continuing processing
Expand Down Expand Up @@ -402,8 +413,14 @@ def perform_process(self, environment: Environment, check_md: dict, transformer_
os.path.basename(one_file))
continue

# Get the mask name using the original name as reference
rgb_mask_tif = os.path.join(check_md['working_folder'], __internal__.get_maskfilename(one_file))
# Get the mask name
if environment.args.out_file:
rgb_mask_tif = environment.args.out_file
if not os.path.dirname(rgb_mask_tif):
rgb_mask_tif = os.path.join(check_md['working_folder'], rgb_mask_tif)
else:
# Use the original name
rgb_mask_tif = os.path.join(check_md['working_folder'], __internal__.get_maskfilename(one_file))

# Create the mask file
logging.debug("Creating mask file '%s'", rgb_mask_tif)
Expand Down
33 changes: 32 additions & 1 deletion tests/test_soilmask.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def test_prepare_metadata_for_geotiff():
assert value == str(test[METADATA_KEY_TRANSLATION['transformer_repo']]['repUrl'])


def test_command_line():
def test_simple_line():
"""Runs the command line and tests the result"""
orthomosaic_mask_name = 'orthomosaic_mask.tif'
result_name = 'result.json'
Expand Down Expand Up @@ -98,3 +98,34 @@ def test_command_line():
img = gdal.Open(os.path.join(working_space, orthomosaic_mask_name)).ReadAsArray()
assert img is not None
assert isinstance(img, np.ndarray)


def test_outputfile_command_line():
"""Runs the command line and tests the result"""
orthomosaic_mask_name = 'soilmask.tif'
result_name = 'result.json'
source_image = os.path.join(TESTING_FILE_PATH, 'orthomosaic.tif')
source_metadata = os.path.join(TESTING_FILE_PATH, 'experiment.yaml')
assert os.path.exists(source_image)
assert os.path.exists(source_metadata)

working_space = os.path.realpath('./test_results')
os.makedirs(working_space, exist_ok=True)

command_line = [SOURCE_PATH, '--metadata', source_metadata, '--working_space', working_space,
'--out_file', orthomosaic_mask_name, source_image]
subprocess.run(command_line, check=True)

# Check that the expected files were created
for expected_file in [result_name, orthomosaic_mask_name]:
assert os.path.exists(os.path.join(working_space, expected_file))

# Inspect the created files
with open(os.path.join(working_space, result_name)) as in_file:
res = json.load(in_file)
assert 'code' in res
assert res['code'] == 0

img = gdal.Open(os.path.join(working_space, orthomosaic_mask_name)).ReadAsArray()
assert img is not None
assert isinstance(img, np.ndarray)

0 comments on commit bb8b899

Please sign in to comment.