https://github.com/jupyter-widgets/traittypes
Traitlets types for NumPy, SciPy and friends
https://github.com/jupyter-widgets/traittypes
Last synced: 2 months ago
JSON representation
Traitlets types for NumPy, SciPy and friends
- Host: GitHub
- URL: https://github.com/jupyter-widgets/traittypes
- Owner: jupyter-widgets
- License: bsd-3-clause
- Created: 2015-10-29T13:30:38.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2023-09-24T18:58:42.000Z (over 1 year ago)
- Last Synced: 2025-03-23T20:22:25.927Z (3 months ago)
- Language: Python
- Size: 52.7 KB
- Stars: 34
- Watchers: 6
- Forks: 22
- Open Issues: 19
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Scipy Trait Types
[](https://travis-ci.org/jupyter-widgets/traittypes)
[](http://traittypes.readthedocs.org/en/latest/?badge=latest)Trait types for NumPy, SciPy and friends
## Goals
Provide a reference implementation of trait types for common data structures
used in the scipy stack such as
- [numpy](https://github.com/numpy/numpy) arrays
- [pandas](https://github.com/pydata/pandas) and [xarray](https://github.com/pydata/xarray) data structureswhich are out of the scope of the main [traitlets](https://github.com/ipython/traitlets)
project but are a common requirement to build applications with traitlets in
combination with the scipy stack.Another goal is to create adequate serialization and deserialization routines
for these trait types to be used with the [ipywidgets](https://github.com/ipython/ipywidgets)
project (`to_json` and `from_json`). These could also return a list of binary
buffers as allowed by the current messaging protocol.## Installation
Using `pip`:
Make sure you have [pip installed](https://pip.readthedocs.org/en/stable/installing/) and run:
```
pip install traittypes
```Using `conda`:
```
conda install -c conda-forge traittypes
```## Usage
`traittypes` extends the `traitlets` library with an implementation of trait types for numpy arrays, pandas dataframes, pandas series, xarray datasets and xarray dataarrays.
- `traittypes` works around some limitations with numpy array comparison to only trigger change events when necessary.
- `traittypes` also extends the traitlets API for adding custom validators to constained proposed values for the attribute.For a general introduction to `traitlets`, check out the [traitlets documentation](https://traitlets.readthedocs.io/en/stable/).
### Example usage with a custom validator
```python
from traitlets import HasTraits, TraitError
from traittypes import Arraydef shape(*dimensions):
def validator(trait, value):
if value.shape != dimensions:
raise TraitError('Expected an of shape %s and got and array with shape %s' % (dimensions, value.shape))
else:
return value
return validatorclass Foo(HasTraits):
bar = Array(np.identity(2)).valid(shape(2, 2))
foo = Foo()foo.bar = [1, 2] # Should raise a TraitError
```