-
Notifications
You must be signed in to change notification settings - Fork 1
/
testing.py
executable file
·119 lines (97 loc) · 3.86 KB
/
testing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/env python3
"""Test script for algorithm_lidar code
"""
import os
import sys
import numpy as np
import laspy
import algorithm_lidar
def print_usage():
"""Displays information on how to use this script
"""
argc = len(sys.argv)
if argc:
our_name = os.path.basename(sys.argv[0])
else:
our_name = os.path.basename(__file__)
print(our_name + " <folder>|<filename> ...")
print(" folder: path to folder containing files to process")
print(" filename: path to a lidar file to process")
print("")
print(" One or more folders and/or filenames can be used")
print(" Only files at the top level of a folder are processed")
def check_arguments():
"""Checks that we have script argument parameters that appear valid
"""
argc = len(sys.argv)
if argc < 2:
sys.stderr.write("One or more paths to images need to be specified on the command line\n")
print_usage()
return False
# Check that the paths exist.
have_errors = False
for idx in range(1, argc):
if not os.path.exists(sys.argv[idx]):
print("The following path doesn't exist: " + sys.argv[idx])
have_errors = True
if have_errors:
sys.stderr.write("Please correct any problems and try again\n")
return not have_errors
def check_configuration():
"""Checks if the configuration is setup properly for testing
"""
if not hasattr(algorithm_lidar, 'VARIABLE_NAMES') or not algorithm_lidar.VARIABLE_NAMES:
sys.stderr.write("Variable names configuration variable is not defined yet. Please define and try again")
sys.stderr.write(" Update configuration.py and set VALUE_NAMES variable with your variable names")
return False
return True
def run_test(filename):
"""Runs the extractor code using pixels from the file
Args:
filename(str): Path to image file
Return:
The result of calling the extractor's calculate() method
Notes:
Assumes the path passed in is valid. An error is reported if
the file is not an image file.
"""
try:
open_file = laspy.file.File(filename, mode = "r")
if open_file:
# Get the pixels and call the calculation
pix = np.vstack([open_file.X, open_file.Y, open_file.Z])
calc_val = algorithm_lidar.calculate(pix)
# Check for unsupported types
if isinstance(calc_val, set):
raise RuntimeError("A 'set' type of data was returned and isn't supported. Please use a list or a tuple instead")
# Perform any type conversions to a printable string
if isinstance(calc_val, str):
print_val = calc_val
else:
# Check if the return is iterable and comma separate the values if it is
try:
_ = iter(calc_val)
print_val = ",".join(map(str, calc_val))
except Exception:
print_val = str(calc_val)
print(filename + "," + print_val)
except Exception as ex:
sys.stderr.write("Exception caught: " + str(ex) + "\n")
sys.stderr.write(" File: " + filename + "\n")
def process_files():
"""Processes the command line file/folder arguments
"""
argc = len(sys.argv)
if argc:
print("Filename," + algorithm_lidar.VARIABLE_NAMES)
for idx in range(1, argc):
cur_path = sys.argv[idx]
if not os.path.isdir(cur_path):
run_test(cur_path)
else:
allfiles = [os.path.join(cur_path, fn) for fn in os.listdir(cur_path) if os.path.isfile(os.path.join(cur_path, fn))]
for one_file in allfiles:
run_test(one_file)
if __name__ == "__main__":
if check_arguments() and check_configuration():
process_files()