Lovely-Numpy: Enhanced Array Representation
lovely-numpy (version 0.2.22) is a utility library that enhances the default string representation of NumPy arrays, making them significantly more readable and debug-friendly. It achieves this by overriding `np.ndarray.__repr__` to display arrays with improved formatting, optional colors, configurable precision, and a more compact summary for large arrays. The library is actively maintained and released as needed for improvements and bug fixes, focusing on ease of use for interactive data exploration.
Common errors
-
Arrays are not formatted by lovely-numpy; they still look like standard NumPy output.
cause The `ln.override_array_repr()` function, which activates the custom representation, was not called or was called too late.fixEnsure `import lovely_numpy as ln` and `ln.override_array_repr()` are both present and executed before you print NumPy arrays. -
lovely-numpy output suddenly reverted to default NumPy representation, or output is inconsistent.
cause Another library or a part of your code might have called `ln.restore_array_repr()` or globally overridden `np.ndarray.__repr__` after lovely-numpy was activated.fixRe-call `ln.override_array_repr()` after the conflicting operation. Identify and manage `__repr__` overrides if multiple libraries are involved, ensuring lovely-numpy's override is the last one applied. -
Output is too verbose / not verbose enough / wrong precision / lacks color.
cause The default configuration of lovely-numpy does not match desired output, or `ln.config()` was not used effectively.fixUse `ln.config(param=value, ...)` after `ln.override_array_repr()` to adjust settings like `precision`, `threshold`, `colored`, `science_mode`, etc., to your preference. Refer to the documentation for available configuration options.
Warnings
- gotcha lovely-numpy globally overrides `np.ndarray.__repr__`, which can conflict with other libraries or user code that also modifies this attribute. The last library to set `__repr__` will take precedence.
- gotcha Configuration changes made via `ln.config()` affect the global state of lovely-numpy, impacting all subsequent array representations. This can lead to unexpected behavior in multi-threaded environments or within tests if not managed.
- gotcha While designed for readability, processing the `__repr__` of extremely large NumPy arrays (e.g., millions of elements) can introduce a minor performance overhead, especially if complex formatting or coloring is enabled, compared to the default, simpler NumPy representation.
Install
-
pip install lovely-numpy
Imports
- lovely_numpy
import lovely_numpy as ln
- override_array_repr
from lovely_numpy import override_array_repr override_array_repr()
import lovely_numpy as ln ln.override_array_repr()
Quickstart
import lovely_numpy as ln
import numpy as np
# Apply lovely-numpy's enhanced representation
ln.override_array_repr()
# Create a sample NumPy array
arr = np.random.randn(2, 3, 4) * 100 + 50 # Make it a bit more interesting
# Print the array to see the lovely-numpy formatting
print("Original array:")
print(arr)
# You can also configure its behavior
ln.config(
precision=2, # Display 2 decimal places
colored=True, # Enable colored output
threshold=10, # Apply repr for arrays with > 10 elements
science_mode=False # Disable scientific notation for small numbers
)
print("\nArray with custom configuration:")
print(arr)
# Restore original numpy repr for specific sections if needed
ln.restore_array_repr()
print("\nArray after restoring original numpy repr:")
print(arr)