Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added machine learning tools #59

Merged
merged 3 commits into from
Feb 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ whiteboxR.Rproj
.vscode/
/doc/
/Meta/
*.zip
51 changes: 30 additions & 21 deletions PY2R/automation.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import urllib.request

# Extract function header


def function_header(line):
if line.startswith("def"):
line = line.replace("self, i,", "input,")
Expand All @@ -45,7 +47,7 @@ def function_header(line):
# Extract function name
def function_name(line):
line = line.strip()
name = line[0 : line.find("(")]
name = line[0: line.find("(")]
return name


Expand All @@ -61,7 +63,7 @@ def is_float(string):
# Generate R function block
def function_block(line, ff):
line = line.strip()
function_name = line[0 : line.find("(")]
function_name = line[0: line.find("(")]
start = line.find("(") + 1
end = len(line) - 1
argument = line[start:end]
Expand All @@ -74,7 +76,8 @@ def function_block(line, ff):
item = item.strip()
if "=" not in item:
ff.write(
' args <- paste(args, paste0("--' + item + '=", ' + item + "))" + "\n"
' args <- paste(args, paste0("--' + item +
'=", ' + item + "))" + "\n"
)
elif "verbose" in item:
continue
Expand Down Expand Up @@ -184,7 +187,8 @@ def function_example(fun_name):
duplicate_problems.append("lidar_block_maximum")
duplicate_problems.append("flightline_overlap")
if fun_name in duplicate_problems:
example = example.replace("--input=file.las --output=outfile.tif", "", 1)
example = example.replace(
"--input=file.las --output=outfile.tif", "", 1)
example = example.replace("--palette=light_quant.plt", "")

if fun_name == "k_nearest_mean_filter":
Expand Down Expand Up @@ -220,6 +224,7 @@ def function_example(fun_name):
"# Hydrological Analysis #": "hydro_analysis.R",
"# Image Processing Tools #": "image_analysis.R",
"# LiDAR Tools #": "lidar_analysis.R",
"# Machine Learning #": "machine_learning.R",
"# Math and Stats Tools #": "math_stat_analysis.R",
"# Precision Agriculture #": "precision_agriculture.R",
"# Stream Network Analysis #": "stream_network_analysis.R",
Expand Down Expand Up @@ -262,7 +267,8 @@ def function_example(fun_name):

# Create an R script for each toolbox
if line in toolboxes:
script_path = os.path.join(dir_path, "scripts", toolboxes[line])
script_path = os.path.join(
dir_path, "scripts", toolboxes[line])
ff = open(script_path, "w")
print(script_path)

Expand Down Expand Up @@ -316,7 +322,7 @@ def function_example(fun_name):
if add_example:
fun_name = function_name(fun_head)
wbt_fun_name = "wbt_" + fun_name
fun_params = fun_head[fun_head.index("(") :]
fun_params = fun_head[fun_head.index("("):]

if (fun_params == "(input, output, verbose_mode=FALSE)") and (
fun_name != "raster_histogram"
Expand Down Expand Up @@ -346,25 +352,28 @@ def function_example(fun_name):
ff.write("#' }\n")
# print(line1)
# print(line2)

skip_fun_tests = ["raster_histogram", "attribute_correlation",
"conditional_evaluation", "crispness_index",
"ks_test_for_normality", "rescale_value_range", "trend_surface"]
if (fun_params.startswith("(input, output") and not any(fun_name in s for s in skip_fun_tests)):
# write test scripts
test_file_name = "test-" + wbt_fun_name + ".R"
test_file_path = os.path.join(dir_path, "tests", test_file_name)
f = open(test_file_path, "w")
f.write('context("{}")\n\n'.format(wbt_fun_name))
f.write('test_that("' + desc + '", {\n\n')
f.write(" skip_on_cran()\n")
f.write(" skip_if_not(check_whitebox_binary())\n")
f.write(' dem <- system.file("extdata", "DEM.tif", package = "whitebox")\n')
f.write(' ret <- {}(input = dem, output = "output.tif")\n'.format(wbt_fun_name))
f.write(' expect_match(ret, "Elapsed Time")\n\n')
f.write("})\n")
print(test_file_path)
f.close()
# write test scripts
test_file_name = "test-" + wbt_fun_name + ".R"
test_file_path = os.path.join(
dir_path, "tests", test_file_name)
f = open(test_file_path, "w")
f.write('context("{}")\n\n'.format(wbt_fun_name))
f.write('test_that("' + desc + '", {\n\n')
f.write(" skip_on_cran()\n")
f.write(" skip_if_not(check_whitebox_binary())\n")
f.write(
' dem <- system.file("extdata", "DEM.tif", package = "whitebox")\n')
f.write(
' ret <- {}(input = dem, output = "output.tif")\n'.format(wbt_fun_name))
f.write(' expect_match(ret, "Elapsed Time")\n\n')
f.write("})\n")
print(test_file_path)
f.close()

# fun_name = function_name(fun_head)
# example = function_example(fun_name)
Expand Down
Loading