API Reference Documentation

The SciType trait type is the base trait type for all Scipy trait types.

It complements the traitlets.TraitType with a special API to register custom validators.

class traittypes.traittypes.SciType(default_value=traitlets.Undefined, allow_none=False, read_only=None, help=None, **kwargs)[source]

A base trait type for numpy arrays, pandas dataframes and series.


Register new trait validators

Validators are functions that take two arguments.
  • The trait instance
  • The proposed value

Validators return the (potentially modified) value, which is either assigned to the HasTraits attribute or input into the next validator.

They are evaluated in the order in which they are provided to the valid function.


# Test with a shape constraint
def 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))
            return value
    return validator

class Foo(HasTraits):
    bar = Array(np.identity(2)).valid(shape(2, 2))
foo = Foo()

foo.bar = [1, 2]  # Should raise a TraitError

The Array trait type holds a numpy Array.

class traittypes.traittypes.Array(default_value=traitlets.Undefined, allow_none=False, dtype=None, **kwargs)[source]

A numpy array trait type.

The DataFrame trait type holds a pandas DataFrame.

class traittypes.traittypes.DataFrame(default_value=traitlets.Undefined, allow_none=False, dtype=None, **kwargs)[source]

A pandas dataframe trait type.

The Series trait type holds a pandas Series.

class traittypes.traittypes.Series(default_value=traitlets.Undefined, allow_none=False, dtype=None, **kwargs)[source]

A pandas series trait type.