(self, other_side: ndarray | list[Any])
| 137 | return f"approx({list_scalars!r})" |
| 138 | |
| 139 | def _repr_compare(self, other_side: ndarray | list[Any]) -> list[str]: |
| 140 | import itertools |
| 141 | import math |
| 142 | |
| 143 | def get_value_from_nested_list( |
| 144 | nested_list: list[Any], nd_index: tuple[Any, ...] |
| 145 | ) -> Any: |
| 146 | """ |
| 147 | Helper function to get the value out of a nested list, given an n-dimensional index. |
| 148 | This mimics numpy's indexing, but for raw nested python lists. |
| 149 | """ |
| 150 | value: Any = nested_list |
| 151 | for i in nd_index: |
| 152 | value = value[i] |
| 153 | return value |
| 154 | |
| 155 | np_array_shape = self.expected.shape |
| 156 | approx_side_as_seq = _recursive_sequence_map( |
| 157 | self._approx_scalar, self.expected.tolist() |
| 158 | ) |
| 159 | |
| 160 | # convert other_side to numpy array to ensure shape attribute is available |
| 161 | other_side_as_array = _as_numpy_array(other_side) |
| 162 | assert other_side_as_array is not None |
| 163 | |
| 164 | if np_array_shape != other_side_as_array.shape: |
| 165 | return [ |
| 166 | "Impossible to compare arrays with different shapes.", |
| 167 | f"Shapes: {np_array_shape} and {other_side_as_array.shape}", |
| 168 | ] |
| 169 | |
| 170 | number_of_elements = self.expected.size |
| 171 | max_abs_diff = -math.inf |
| 172 | max_rel_diff = -math.inf |
| 173 | different_ids = [] |
| 174 | for index in itertools.product(*(range(i) for i in np_array_shape)): |
| 175 | approx_value = get_value_from_nested_list(approx_side_as_seq, index) |
| 176 | other_value = get_value_from_nested_list(other_side_as_array, index) |
| 177 | if approx_value != other_value: |
| 178 | abs_diff = abs(approx_value.expected - other_value) |
| 179 | max_abs_diff = max(max_abs_diff, abs_diff) |
| 180 | if other_value == 0.0: |
| 181 | max_rel_diff = math.inf |
| 182 | else: |
| 183 | max_rel_diff = max(max_rel_diff, abs_diff / abs(other_value)) |
| 184 | different_ids.append(index) |
| 185 | |
| 186 | message_data = [ |
| 187 | ( |
| 188 | str(index), |
| 189 | str(get_value_from_nested_list(other_side_as_array, index)), |
| 190 | str(get_value_from_nested_list(approx_side_as_seq, index)), |
| 191 | ) |
| 192 | for index in different_ids |
| 193 | ] |
| 194 | return _compare_approx( |
| 195 | self.expected, |
| 196 | message_data, |
nothing calls this directly
no test coverage detected