Browse code

add option to '_normalize_matrix_blocks' for more informative errors

We allow the function that produced the data to be passed to
'_normalize_matrix_blocks', and any error messages are augmented
with this information. This may prove useful for debugging.

Joseph Weston authored on 11/12/2019 17:35:59
Showing 3 changed files
... ...
@@ -2127,7 +2127,8 @@ class _VectorizedFinalizedBuilderMixin(_FinalizedBuilderMixin):
2127 2127
             to_family.norbs,
2128 2128
             from_family.norbs if not is_onsite else to_family.norbs,
2129 2129
         )
2130
-        ham = system._normalize_matrix_blocks(ham, expected_shape)
2130
+        ham = system._normalize_matrix_blocks(ham, expected_shape,
2131
+                                              calling_function=val)
2131 2132
 
2132 2133
         return ham
2133 2134
 
... ...
@@ -334,7 +334,8 @@ class _FunctionalOnsite:
334 334
             _raise_user_error(exc, self.onsite, vectorized=False)
335 335
 
336 336
         expected_shape = (len(site_offsets), norbs, norbs)
337
-        return _normalize_matrix_blocks(ret, expected_shape)
337
+        return _normalize_matrix_blocks(ret, expected_shape,
338
+                                        calling_function=self.onsite)
338 339
 
339 340
 
340 341
 class _VectorizedFunctionalOnsite:
... ...
@@ -353,7 +354,8 @@ class _VectorizedFunctionalOnsite:
353 354
             _raise_user_error(exc, self.onsite, vectorized=True)
354 355
 
355 356
         expected_shape = (len(sites), sites.family.norbs, sites.family.norbs)
356
-        return _normalize_matrix_blocks(ret, expected_shape)
357
+        return _normalize_matrix_blocks(ret, expected_shape,
358
+                                        calling_function=self.onsite)
357 359
 
358 360
 
359 361
 class _FunctionalOnsiteNoTransform:
... ...
@@ -371,7 +373,8 @@ class _FunctionalOnsiteNoTransform:
371 373
             _raise_user_error(exc, self.onsite, vectorized=False)
372 374
 
373 375
         expected_shape = (len(site_offsets), norbs, norbs)
374
-        return _normalize_matrix_blocks(ret, expected_shape)
376
+        return _normalize_matrix_blocks(ret, expected_shape,
377
+                                        calling_function=self.onsite)
375 378
 
376 379
 
377 380
 class _DictOnsite:
... ...
@@ -742,13 +742,16 @@ def is_vectorized(syst):
742 742
     return isinstance(syst, (FiniteVectorizedSystem, InfiniteVectorizedSystem))
743 743
 
744 744
 
745
-def _normalize_matrix_blocks(blocks, expected_shape):
745
+def _normalize_matrix_blocks(blocks, expected_shape, *, calling_function=None):
746 746
     """Normalize a sequence of matrices into a single 3D numpy array
747 747
 
748 748
     Parameters
749 749
     ----------
750 750
     blocks : sequence of complex array-like
751 751
     expected_shape : (int, int, int)
752
+    calling_function : callable (optional)
753
+        The function that produced 'blocks'. If provided, used to give
754
+        a more helpful error message if 'blocks' is not of the correct shape.
752 755
     """
753 756
     try:
754 757
         blocks = np.asarray(blocks, dtype=complex)
... ...
@@ -766,9 +769,11 @@ def _normalize_matrix_blocks(blocks, expected_shape):
766 769
     if blocks.shape != expected_shape:
767 770
         msg = (
768 771
             "Expected values of shape {}, but received values of shape {}"
769
-            .format(expected_shape, blocks.shape)
772
+                .format(expected_shape, blocks.shape),
773
+            "when evaluating {}".format(calling_function.__name__)
774
+                if callable(calling_function) else "",
770 775
         )
771
-        raise ValueError(msg)
776
+        raise ValueError(" ".join(msg))
772 777
 
773 778
     return blocks
774 779