traits.trait_numeric Module

Trait definitions related to the numpy library.

Classes

class traits.trait_numeric.AbstractArray(dtype=None, shape=None, value=None, coerce=False, typecode=None, *, casting='unsafe', **metadata)[source]

Bases: traits.trait_type.TraitType

Abstract base class for defining numpy-based arrays.

validate(object, name, value)[source]

Validates that the value is a valid array.

info()[source]

Returns descriptive information about the trait.

create_editor()[source]

Returns the default UI editor for the trait.

get_default_value()[source]

Returns the default value constructor for the type (called from the trait factory.

copy_default_value(value)[source]

Returns a copy of the default value (called from the C code on first reference to a trait with no current value).

class traits.trait_numeric.Array(dtype=None, shape=None, value=None, typecode=None, *, casting='unsafe', **metadata)[source]

Bases: traits.trait_numeric.AbstractArray

A trait type whose value must be a NumPy array.

An Array trait allows only upcasting of assigned values that are already numpy arrays. It automatically casts tuples and lists of the right shape to the specified dtype (just like numpy’s array does).

The default value is either the value argument or zeros(min(shape)), where min(shape) refers to the minimum shape allowed by the array. If shape is not specified, the minimum shape is (0,).

Parameters
  • dtype (a numpy dtype (e.g., int32)) – The type of elements in the array; if omitted, no type-checking is performed on assigned values.

  • shape (a tuple) – Describes the required shape of any assigned value. Wildcards and ranges are allowed. The value None within the shape tuple means that the corresponding dimension is not checked. (For example, shape=(None,3) means that the first dimension can be any size, but the second must be 3.) A two-element tuple within the shape tuple means that the dimension must be in the specified range. The second element can be None to indicate that there is no upper bound. (For example, shape=((3,5),(2,None)) means that the first dimension must be in the range 3 to 5 (inclusive), and the second dimension must be at least 2.)

  • value (numpy array) – A default value for the array.

  • casting (str) –

    Casting rule for the array’s dtype. If dtype is set, a value can only be assigned if it passes the casting rule. Values can be:

    • ”no”: No casting is allowed

    • ”equiv”: Only byte-order changes are allowed

    • ”safe”: Only allow casting that fully preserves values (e.g. “float32” to “float64”)

    • ”same-kind”: Only safe casts or casts within a kind (e.g. “float64” to “float32”) are allowed

    • ”unsafe”: Any casting is allowed

    Default is “unsafe”.

class traits.trait_numeric.ArrayOrNone(*args, **metadata)[source]

Bases: traits.trait_numeric.CArray

A coercing trait whose value may be either a NumPy array or None.

This trait is designed to avoid the comparison issues with numpy arrays that can arise from the use of constructs like Union(None, Array).

The default value is None.

validate(object, name, value)[source]

Validates that the value is a valid array.

get_default_value()[source]

Returns the default value constructor for the type (called from the trait factory.

class traits.trait_numeric.CArray(dtype=None, shape=None, value=None, typecode=None, *, casting='unsafe', **metadata)[source]

Bases: traits.trait_numeric.AbstractArray

A coercing trait type whose value is a NumPy array.

The trait returned by CArray() is similar to that returned by Array(), except that it allows both upcasting and downcasting of assigned values that are already numpy arrays. It automatically casts tuples and lists of the right shape to the specified dtype (just like numpy’s array does).

The default value is either the value argument or zeros(min(shape)), where min(shape) refers to the minimum shape allowed by the array. If shape is not specified, the minimum shape is (0,).

Parameters
  • dtype (a numpy dtype (e.g., int32)) – The type of elements in the array.

  • shape (a tuple) – Describes the required shape of any assigned value. Wildcards and ranges are allowed. The value None within the shape tuple means that the corresponding dimension is not checked. (For example, shape=(None,3) means that the first dimension can be any size, but the second must be 3.) A two-element tuple within the shape tuple means that the dimension must be in the specified range. The second element can be None to indicate that there is no upper bound. (For example, shape=((3,5),(2,None)) means that the first dimension must be in the range 3 to 5 (inclusive), and the second dimension must be at least 2.)

  • value (numpy array) – A default value for the array.

  • casting (str) –

    Casting rule for the array’s dtype. If dtype is set, a value can only be assigned if it passes the casting rule. Values can be:

    • ”no”: No casting is allowed

    • ”equiv”: Only byte-order changes are allowed

    • ”safe”: Only allow casting that fully preserves values (e.g. “float32” to “float64”)

    • ”same-kind”: Only safe casts or casts within a kind (e.g. “float64” to “float32”) are allowed

    • ”unsafe”: Any casting is allowed

    Default is “unsafe”.

Function

traits.trait_numeric.dtype2trait(dtype)[source]

Get the corresponding trait for a numpy dtype.