From 84ebaaee36fd3feed5b1256955ce0f495d20b063 Mon Sep 17 00:00:00 2001 From: Benjamin Perret Date: Fri, 25 Jan 2019 10:45:22 +0100 Subject: [PATCH 1/2] Failure to provide a value for a parameter with a default value is not a failure. --- higra/data_cache.py | 21 +++++++++++---------- test/python/CMakeLists.txt | 1 + 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/higra/data_cache.py b/higra/data_cache.py index 57c1735d..fbbd8e1b 100644 --- a/higra/data_cache.py +++ b/higra/data_cache.py @@ -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 \ diff --git a/test/python/CMakeLists.txt b/test/python/CMakeLists.txt index cc42c334..97448fc3 100644 --- a/test/python/CMakeLists.txt +++ b/test/python/CMakeLists.txt @@ -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) From 055a089a3636f7c246da08c0ecd482163694f2d2 Mon Sep 17 00:00:00 2001 From: Benjamin Perret Date: Fri, 25 Jan 2019 10:46:47 +0100 Subject: [PATCH 2/2] Default vertex area for attribute area --- higra/attribute/attributes.py | 5 ++- test/python/test_attribute/CMakeLists.txt | 15 ++++++++ test/python/test_attribute/__init__.py | 11 ++++++ test/python/test_attribute/test_attributes.py | 34 +++++++++++++++++++ 4 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 test/python/test_attribute/CMakeLists.txt create mode 100644 test/python/test_attribute/__init__.py create mode 100644 test/python/test_attribute/test_attributes.py diff --git a/higra/attribute/attributes.py b/higra/attribute/attributes.py index dbebb2fd..ebb1659e 100644 --- a/higra/attribute/attributes.py +++ b/higra/attribute/attributes.py @@ -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. @@ -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) diff --git a/test/python/test_attribute/CMakeLists.txt b/test/python/test_attribute/CMakeLists.txt new file mode 100644 index 00000000..262d40fd --- /dev/null +++ b/test/python/test_attribute/CMakeLists.txt @@ -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}") diff --git a/test/python/test_attribute/__init__.py b/test/python/test_attribute/__init__.py new file mode 100644 index 00000000..ae942e42 --- /dev/null +++ b/test/python/test_attribute/__init__.py @@ -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. # +############################################################################ + + diff --git a/test/python/test_attribute/test_attributes.py b/test/python/test_attribute/test_attributes.py new file mode 100644 index 00000000..594cc439 --- /dev/null +++ b/test/python/test_attribute/test_attributes.py @@ -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()