Skip to content

Commit

Permalink
check-style and apply-style for python
Browse files Browse the repository at this point in the history
  • Loading branch information
yxlao committed May 23, 2019
1 parent 3aebe83 commit d8d4df2
Show file tree
Hide file tree
Showing 5 changed files with 217 additions and 127 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ matrix:
- mkdir build
- cd build
- cmake -DPYTHON_EXECUTABLE=`which python` ..
- pip install -U yapf
- make check-style

# Build docs only
Expand Down
64 changes: 29 additions & 35 deletions src/UnitTest/Python/test_octree.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,27 @@
import pytest
import os


_eight_cubes_colors = np.array(
[
[0.0, 0.0, 0.0],
[0.1, 0.0, 0.0],
[0.0, 0.1, 0.0],
[0.1, 0.1, 0.0],
[0.0, 0.0, 0.1],
[0.1, 0.0, 0.1],
[0.0, 0.1, 0.1],
[0.1, 0.1, 0.1],
]
)

_eight_cubes_points = np.array(
[
[0.5, 0.5, 0.5],
[1.5, 0.5, 0.5],
[0.5, 1.5, 0.5],
[1.5, 1.5, 0.5],
[0.5, 0.5, 1.5],
[1.5, 0.5, 1.5],
[0.5, 1.5, 1.5],
[1.5, 1.5, 1.5],
]
)
_eight_cubes_colors = np.array([
[0.0, 0.0, 0.0],
[0.1, 0.0, 0.0],
[0.0, 0.1, 0.0],
[0.1, 0.1, 0.0],
[0.0, 0.0, 0.1],
[0.1, 0.0, 0.1],
[0.0, 0.1, 0.1],
[0.1, 0.1, 0.1],
])

_eight_cubes_points = np.array([
[0.5, 0.5, 0.5],
[1.5, 0.5, 0.5],
[0.5, 1.5, 0.5],
[1.5, 1.5, 0.5],
[0.5, 0.5, 1.5],
[1.5, 0.5, 1.5],
[0.5, 1.5, 1.5],
[1.5, 1.5, 1.5],
])


def test_octree_OctreeNodeInfo():
Expand Down Expand Up @@ -91,9 +86,11 @@ def test_octree_OctreeColorLeafNode():
assert color_leaf_node == color_leaf_node_clone
assert color_leaf_node_clone == color_leaf_node


def test_octree_init():
octree = o3d.geometry.Octree(1, [0, 0, 0], 2)


def test_octree_convert_from_point_cloud():
octree = o3d.geometry.Octree(1, [0, 0, 0], 2)

Expand All @@ -118,16 +115,14 @@ def test_octree_node_access():
f_update = o3d.geometry.OctreeColorLeafNode.get_update_function(color)
octree.insert_point(point, f_init, f_update)
for i in range(8):
np.testing.assert_equal(
octree.root_node.children[i].color, _eight_cubes_colors[i]
)
np.testing.assert_equal(octree.root_node.children[i].color,
_eight_cubes_colors[i])


def test_octree_visualize():
pwd = os.path.dirname(os.path.realpath(__file__))
data_dir = os.path.join(
pwd, os.pardir, os.pardir, os.pardir, "examples", "TestData"
)
data_dir = os.path.join(pwd, os.pardir, os.pardir, os.pardir, "examples",
"TestData")
pcd_path = os.path.join(data_dir, "fragment.ply")
pcd = o3d.io.read_point_cloud(pcd_path)
octree = o3d.geometry.Octree(8)
Expand All @@ -138,9 +133,8 @@ def test_octree_visualize():

def test_locate_leaf_node():
pwd = os.path.dirname(os.path.realpath(__file__))
data_dir = os.path.join(
pwd, os.pardir, os.pardir, os.pardir, "examples", "TestData"
)
data_dir = os.path.join(pwd, os.pardir, os.pardir, os.pardir, "examples",
"TestData")
pcd_path = os.path.join(data_dir, "fragment.ply")
pcd = o3d.io.read_point_cloud(pcd_path)

Expand Down
172 changes: 96 additions & 76 deletions src/UnitTest/Python/test_open3d_eigen.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,32 @@
import time
import pytest

@pytest.mark.parametrize("input_array, expect_exception", [
# Empty case
(np.ones((0, 3), dtype=np.float64), False),
# Wrong shape
(np.ones((2, 4), dtype=np.float64), True),
# Non-numpy array
([[1, 2, 3], [4, 5, 6]], False),
([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]], False),
# Datatypes
(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float64), False),
(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int32), False),
(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int32), False),
# Slice non-contiguous memory
(np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]], dtype=np.float64)[:, 0:6:2], False),
# Transpose view
(np.array([[1, 4], [2, 5], [3, 6]], dtype=np.float64).T, False),
# Fortran layout
(np.asfortranarray(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float64)), False),
])

@pytest.mark.parametrize(
"input_array, expect_exception",
[
# Empty case
(np.ones((0, 3), dtype=np.float64), False),
# Wrong shape
(np.ones((2, 4), dtype=np.float64), True),
# Non-numpy array
([[1, 2, 3], [4, 5, 6]], False),
([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]], False),
# Datatypes
(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float64), False),
(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int32), False),
(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int32), False),
# Slice non-contiguous memory
(np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]],
dtype=np.float64)[:, 0:6:2], False),
# Transpose view
(np.array([[1, 4], [2, 5], [3, 6]], dtype=np.float64).T, False),
# Fortran layout
(np.asfortranarray(np.array([[1, 2, 3], [4, 5, 6]],
dtype=np.float64)), False),
])
def test_Vector3dVector(input_array, expect_exception):

def run_test(input_array):
open3d_array = open3d.Vector3dVector(input_array)
output_array = np.asarray(open3d_array)
Expand All @@ -60,26 +66,32 @@ def run_test(input_array):
else:
run_test(input_array)

@pytest.mark.parametrize("input_array, expect_exception", [
# Empty case
(np.ones((0, 3), dtype=np.int32), False),
# Wrong shape
(np.ones((2, 4), dtype=np.int32), True),
# Non-numpy array
([[1, 2, 3], [4, 5, 6]], False),
([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]], False),
# Datatypes
(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float64), False),
(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int32), False),
(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int32), False),
# Slice non-contiguous memory
(np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]], dtype=np.int32)[:, 0:6:2], False),
# Transpose view
(np.array([[1, 4], [2, 5], [3, 6]], dtype=np.int32).T, False),
# Fortran layout
(np.asfortranarray(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int32)), False),
])

@pytest.mark.parametrize(
"input_array, expect_exception",
[
# Empty case
(np.ones((0, 3), dtype=np.int32), False),
# Wrong shape
(np.ones((2, 4), dtype=np.int32), True),
# Non-numpy array
([[1, 2, 3], [4, 5, 6]], False),
([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]], False),
# Datatypes
(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float64), False),
(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int32), False),
(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int32), False),
# Slice non-contiguous memory
(np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]],
dtype=np.int32)[:, 0:6:2], False),
# Transpose view
(np.array([[1, 4], [2, 5], [3, 6]], dtype=np.int32).T, False),
# Fortran layout
(np.asfortranarray(np.array([[1, 2, 3], [4, 5, 6]],
dtype=np.int32)), False),
])
def test_Vector3iVector(input_array, expect_exception):

def run_test(input_array):
open3d_array = open3d.Vector3iVector(input_array)
output_array = np.asarray(open3d_array)
Expand All @@ -92,26 +104,30 @@ def run_test(input_array):
run_test(input_array)


@pytest.mark.parametrize("input_array, expect_exception", [
# Empty case
(np.ones((0, 2), dtype=np.int32), False),
# Wrong shape
(np.ones((10, 3), dtype=np.int32), True),
# Non-numpy array
([[1, 2], [4, 5]], False),
([[1.0, 2.0], [4.0, 5.0]], False),
# Datatypes
(np.array([[1, 2], [4, 5]], dtype=np.float64), False),
(np.array([[1, 2], [4, 5]], dtype=np.int32), False),
(np.array([[1, 2], [4, 5]], dtype=np.int32), False),
# Slice non-contiguous memory
(np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]], dtype=np.int32)[:, 0:6:3], False),
# Transpose view
(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int32).T, False),
# Fortran layout
(np.asfortranarray(np.array([[1, 2], [4, 5]], dtype=np.int32)), False),
])
@pytest.mark.parametrize(
"input_array, expect_exception",
[
# Empty case
(np.ones((0, 2), dtype=np.int32), False),
# Wrong shape
(np.ones((10, 3), dtype=np.int32), True),
# Non-numpy array
([[1, 2], [4, 5]], False),
([[1.0, 2.0], [4.0, 5.0]], False),
# Datatypes
(np.array([[1, 2], [4, 5]], dtype=np.float64), False),
(np.array([[1, 2], [4, 5]], dtype=np.int32), False),
(np.array([[1, 2], [4, 5]], dtype=np.int32), False),
# Slice non-contiguous memory
(np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]],
dtype=np.int32)[:, 0:6:3], False),
# Transpose view
(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int32).T, False),
# Fortran layout
(np.asfortranarray(np.array([[1, 2], [4, 5]], dtype=np.int32)), False),
])
def test_Vector2iVector(input_array, expect_exception):

def run_test(input_array):
open3d_array = open3d.Vector2iVector(input_array)
output_array = np.asarray(open3d_array)
Expand All @@ -124,26 +140,29 @@ def run_test(input_array):
run_test(input_array)


@pytest.mark.parametrize("input_array, expect_exception", [
# Empty case
(np.ones((0, 4, 4), dtype=np.float64), False),
# Wrong shape
(np.ones((10, 3), dtype=np.float64), True),
(np.ones((10, 3, 3), dtype=np.float64), True),
# Non-numpy array
([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]]], False),
# Datatypes
(np.random.randint(10, size=(10, 4, 4)).astype(np.float64), False),
(np.random.randint(10, size=(10, 4, 4)).astype(np.int32), False),
# Slice non-contiguous memory
(np.random.random((10, 8, 8)).astype(np.float64)[:, 0:8:2, 0:8:2], False),
# Fortran layout
(np.asfortranarray(np.array(np.random.random((10, 4, 4)), dtype=np.float64)), False),
])
@pytest.mark.parametrize(
"input_array, expect_exception",
[
# Empty case
(np.ones((0, 4, 4), dtype=np.float64), False),
# Wrong shape
(np.ones((10, 3), dtype=np.float64), True),
(np.ones((10, 3, 3), dtype=np.float64), True),
# Non-numpy array
([[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13, 14, 15]]], False
),
# Datatypes
(np.random.randint(10, size=(10, 4, 4)).astype(np.float64), False),
(np.random.randint(10, size=(10, 4, 4)).astype(np.int32), False),
# Slice non-contiguous memory
(np.random.random(
(10, 8, 8)).astype(np.float64)[:, 0:8:2, 0:8:2], False),
# Fortran layout
(np.asfortranarray(
np.array(np.random.random((10, 4, 4)), dtype=np.float64)), False),
])
def test_Matrix4dVector(input_array, expect_exception):

def run_test(input_array):
open3d_array = open3d.Matrix4dVector(input_array)
output_array = np.asarray(open3d_array)
Expand All @@ -155,6 +174,7 @@ def run_test(input_array):
else:
run_test(input_array)


# Run with pytest -s to show output
def test_benchmark():
vector_size = int(2e6)
Expand Down
48 changes: 40 additions & 8 deletions util/scripts/apply-style.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,39 @@
# limitations under the License.
# ******************************************************************************

# Tries to locate "clang-format-5.0" and then "clang-format"
# Try to locate "yapf"
find_program(YAPF yapf PATHS ENV PATH)
if (YAPF)
message(STATUS "yapf found at: ${YAPF}")
execute_process(COMMAND ${YAPF} --version)
else()
message(STATUS "Please Install YAPF (https://github.com/google/yapf)")
message(STATUS "With PyPI: `pip install yapf`")
message(STATUS "With Conda: `conda install yapf`")
message(FATAL_ERROR "yapf not found, python not available")
endif()

function(style_apply_file_python FILE)
execute_process(COMMAND ${YAPF} -i ${FILE})
endfunction()

set(DIRECTORIES_OF_INTEREST_PYTHON
examples/Python
src/UnitTest/Python
)

message(STATUS "Python apply-style...")
foreach(DIRECTORY ${DIRECTORIES_OF_INTEREST_PYTHON})
set(PY_GLOB "${PROJECT_SOURCE_DIR}/${DIRECTORY}/*.py")
file(GLOB_RECURSE FILES ${PY_GLOB})
foreach(FILE ${FILES})
style_apply_file_python(${FILE})
endforeach(FILE)
endforeach(DIRECTORY)
message(STATUS "Python apply-style done")


# Try to locate "clang-format-5.0" and then "clang-format"
find_program(CLANG_FORMAT clang-format-5.0 PATHS ENV PATH)
if (NOT CLANG_FORMAT)
find_program(CLANG_FORMAT clang-format PATHS ENV PATH)
Expand All @@ -30,30 +62,30 @@ else()
message(FATAL_ERROR "clang-format not found, style not available")
endif()

function(style_apply_file PATH)
function(style_apply_file_cpp FILE)
execute_process(
COMMAND ${CLANG_FORMAT} -style=file -output-replacements-xml ${FILE}
OUTPUT_VARIABLE STYLE_CHECK_RESULT
)
if("${STYLE_CHECK_RESULT}" MATCHES ".*<replacement .*")
message(STATUS "Style applied for: ${FILE}")
execute_process(COMMAND ${CLANG_FORMAT} -style=file -i ${PATH})
execute_process(COMMAND ${CLANG_FORMAT} -style=file -i ${FILE})
endif()

endfunction()

set(DIRECTORIES_OF_INTEREST
set(DIRECTORIES_OF_INTEREST_CPP
src
examples
docs/_static
)

foreach(DIRECTORY ${DIRECTORIES_OF_INTEREST})
message(STATUS "C++ apply-style...")
foreach(DIRECTORY ${DIRECTORIES_OF_INTEREST_CPP})
set(CPP_GLOB "${PROJECT_SOURCE_DIR}/${DIRECTORY}/*.cpp")
set(H_GLOB "${PROJECT_SOURCE_DIR}/${DIRECTORY}/*.h")
file(GLOB_RECURSE FILES ${CPP_GLOB} ${H_GLOB})
foreach(FILE ${FILES})
style_apply_file(${FILE})
style_apply_file_cpp(${FILE})
endforeach(FILE)
endforeach(DIRECTORY)
message(STATUS "apply-style done")
message(STATUS "C++ apply-style done")
Loading

0 comments on commit d8d4df2

Please sign in to comment.