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

Attribute area default param #13

Merged
merged 2 commits into from
Jan 25, 2019
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
5 changes: 4 additions & 1 deletion higra/attribute/attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def attribute_vertex_perimeter(graph):

@hg.data_provider("area")
@hg.argument_helper(hg.CptHierarchy, ("leaf_graph", "vertex_area"))
def attribute_area(tree, vertex_area, leaf_graph=None):
def attribute_area(tree, vertex_area=None, leaf_graph=None):
"""
Compute the area of each node the given tree.
The area of a node is equal to the sum of the area of the leaves of the subtree rooted in the node.
Expand All @@ -68,6 +68,9 @@ def attribute_area(tree, vertex_area, leaf_graph=None):
:param leaf_graph: (deduced from :class:`~higra.CptHierarchy`)
:return: a 1d array (Concept :class:`~higra.CptValuedHierarchy`)
"""
if vertex_area is None:
vertex_area = np.ones((tree.num_leaves(),), dtype=np.int64)

if leaf_graph is not None:
vertex_area = hg.linearize_vertex_weights(vertex_area, leaf_graph)
return hg.accumulate_sequential(vertex_area, hg.Accumulators.sum, tree)
Expand Down
21 changes: 11 additions & 10 deletions higra/data_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,16 +348,17 @@ def wrapper(*args, **kwargs):
__resolve_dependency(arg_value, concept, concept, data_cache, kwargs)

except __CacheLookupException as e:
if data_debug:
err = "Error during the resolution of the arguments of the function '" \
+ fun.__name__
raise Exception(err) from e
else: # swallow exception chain for readability
err = "Error during the resolution of the arguments of the function '" + fun.__name__ + "'.\n" \
+ str(e) \
+ "\nYou can call your function with the extra parameter 'data_debug=True' to " \
"get more information about this error."
raise Exception(err) from None
if signature.parameters[arg_name].default is signature.parameters[arg_name].empty:
if data_debug:
err = "Error during the resolution of the arguments of the function '" \
+ fun.__name__
raise Exception(err) from e
else: # swallow exception chain for readability
err = "Error during the resolution of the arguments of the function '" + fun.__name__ + "'.\n" \
+ str(e) \
+ "\nYou can call your function with the extra parameter 'data_debug=True' to " \
"get more information about this error."
raise Exception(err) from None
except TypeError as e:
err = "Lookup for the following argument failed: '" \
+ arg_name \
Expand Down
1 change: 1 addition & 0 deletions test/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ REGISTER_PYTHON_MODULE_FILES("${PY_FILES}")
add_subdirectory(test_accumulator)
add_subdirectory(test_algo)
add_subdirectory(test_assessment)
add_subdirectory(test_attribute)
add_subdirectory(test_hierarchy)
add_subdirectory(test_image)
add_subdirectory(test_io_utils)
Expand Down
15 changes: 15 additions & 0 deletions test/python/test_attribute/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
############################################################################
# Copyright ESIEE Paris (2018) #
# #
# Contributor(s) : Benjamin Perret #
# #
# Distributed under the terms of the CECILL-B License. #
# #
# The full license is in the file LICENSE, distributed with this software. #
############################################################################

set(PY_FILES
__init__.py
test_attributes.py)

REGISTER_PYTHON_MODULE_FILES("${PY_FILES}")
11 changes: 11 additions & 0 deletions test/python/test_attribute/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
############################################################################
# Copyright ESIEE Paris (2018) #
# #
# Contributor(s) : Benjamin Perret #
# #
# Distributed under the terms of the CECILL-B License. #
# #
# The full license is in the file LICENSE, distributed with this software. #
############################################################################


34 changes: 34 additions & 0 deletions test/python/test_attribute/test_attributes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
############################################################################
# Copyright ESIEE Paris (2018) #
# #
# Contributor(s) : Benjamin Perret #
# #
# Distributed under the terms of the CECILL-B License. #
# #
# The full license is in the file LICENSE, distributed with this software. #
############################################################################

import unittest
import numpy as np
import higra as hg


class TestAttributes(unittest.TestCase):

def test_area(self):
g = hg.get_4_adjacency_graph((2, 3))
edge_weights = np.asarray((1, 4, 6, 5, 2, 7, 3))

ref_area = (1, 1, 1, 1, 1, 1, 2, 2, 3, 3, 6)

tree, altitudes = hg.bpt_canonical(edge_weights, g)
area = hg.attribute_area(tree)
self.assertTrue(np.all(ref_area == area))

tree2 = hg.Tree(tree.parents())
area2 = hg.attribute_area(tree2, data_debug=True)
self.assertTrue(np.all(ref_area == area2))


if __name__ == '__main__':
unittest.main()