/usr/share/pyshared/traits/trait_numeric.py is in python-traits 4.4.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 | #------------------------------------------------------------------------------
#
# Copyright (c) 2005, Enthought, Inc.
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in enthought/LICENSE.txt and may be redistributed only
# under the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!
#
# Author: David C. Morrill
# Date: 12/13/2004
#
#------------------------------------------------------------------------------
""" Trait definitions related to the numpy library.
"""
#-------------------------------------------------------------------------------
# Imports:
#-------------------------------------------------------------------------------
from __future__ import absolute_import
import warnings
from .trait_base import SequenceTypes
from .trait_errors import TraitError
from .trait_handlers import TraitType, OBJECT_IDENTITY_COMPARE
from .trait_types import Str, Any, Int as TInt, Float as TFloat
#-------------------------------------------------------------------------------
# Deferred imports from numpy:
#-------------------------------------------------------------------------------
ndarray = None
asarray = None
#-------------------------------------------------------------------------------
# numpy dtype mapping:
#-------------------------------------------------------------------------------
def dtype2trait ( dtype ):
""" Get the corresponding trait for a numpy dtype.
"""
import numpy
if dtype.char in numpy.typecodes['Float']:
return TFloat
elif dtype.char in numpy.typecodes['AllInteger']:
return TInt
elif dtype.char[0] == 'S':
return Str
else:
return Any
#-------------------------------------------------------------------------------
# 'AbstractArray' trait base class:
#-------------------------------------------------------------------------------
class AbstractArray ( TraitType ):
""" Abstract base class for defining numpy-based arrays.
"""
def __init__ ( self, dtype = None, shape = None, value = None,
coerce = False, typecode = None, **metadata ):
""" Returns an AbstractArray trait.
"""
global ndarray, asarray
try:
import numpy
except ImportError:
raise TraitError( "Using Array or CArray trait types requires the "
"numpy package to be installed." )
from numpy import array, asarray, ndarray, zeros
# Mark this as being an 'array' trait:
metadata[ 'array' ] = True
# Normally use object identity to detect array values changing:
metadata.setdefault( 'comparison_mode', OBJECT_IDENTITY_COMPARE )
if typecode is not None:
warnings.warn( 'typecode is a deprecated argument; use dtype '
'instead', DeprecationWarning )
if (dtype is not None) and (dtype != typecode):
raise TraitError( 'Inconsistent usage of the dtype and '
'typecode arguments; use dtype alone.' )
else:
dtype = typecode
if dtype is not None:
try:
# Convert the argument into an actual numpy dtype object:
dtype = numpy.dtype( dtype )
except TypeError:
raise TraitError( 'could not convert %r to a numpy dtype' %
dtype )
if shape is not None:
if isinstance( shape, SequenceTypes ):
for item in shape:
if ((item is None) or (type( item ) is int) or
(isinstance( item, SequenceTypes ) and
(len( item ) == 2) and
(type( item[0] ) is int) and (item[0] >= 0) and
((item[1] is None) or ((type( item[1] ) is int) and
(item[0] <= item[1]))))):
continue
raise TraitError, "shape should be a list or tuple"
else:
raise TraitError, "shape should be a list or tuple"
if value is None:
if dtype is None:
# Compatibility with the default of Traits 2.0
dt = int
else:
dt = dtype
if shape is None:
value = zeros( ( 0, ), dt )
else:
size = []
for item in shape:
if item is None:
item = 1
elif type( item ) in SequenceTypes:
# XXX: what is this supposed to do?
item = item[0]
size.append( item )
value = zeros( size, dt )
self.dtype = dtype
self.shape = shape
self.coerce = coerce
super( AbstractArray, self ).__init__( value, **metadata )
def validate ( self, object, name, value ):
""" Validates that the value is a valid array.
"""
try:
# Make sure the value is an array:
type_value = type( value )
if not isinstance( value, ndarray ):
if not isinstance( value, SequenceTypes ):
self.error( object, name, value )
if self.dtype is not None:
value = asarray( value, self.dtype )
else:
value = asarray( value )
# Make sure the array is of the right type:
if ((self.dtype is not None) and
(value.dtype != self.dtype)):
if self.coerce:
value = value.astype( self.dtype )
else:
# XXX: this also coerces.
value = asarray( value, self.dtype )
# If no shape requirements, then return the value:
trait_shape = self.shape
if trait_shape is None:
return value
# Else make sure that the value's shape is compatible:
value_shape = value.shape
if len( trait_shape ) == len( value_shape ):
for i, dim in enumerate( value_shape ):
item = trait_shape[i]
if item is not None:
if type( item ) is int:
if dim != item:
break
elif ((dim < item[0]) or
((item[1] is not None) and (dim > item[1]))):
break
else:
return value
except:
pass
self.error( object, name, value )
def info ( self ):
""" Returns descriptive information about the trait.
"""
dtype = shape = ''
if self.shape is not None:
shape = []
for item in self.shape:
if item is None:
item = '*'
elif type( item ) is not int:
if item[1] is None:
item = '%d..' % item[0]
else:
item = '%d..%d' % item
shape.append( item )
shape = ' with shape %s' % ( tuple( shape ), )
if self.dtype is not None:
# FIXME: restore nicer descriptions of dtypes.
dtype = ' of %s values' % self.dtype
return 'an array%s%s' % ( dtype, shape )
def get_editor ( self, trait = None ):
""" Returns the default UI editor for the trait.
"""
editor = None
auto_set = False
if self.auto_set is None:
auto_set = True
enter_set = self.enter_set or False
if self.shape is not None and len( self.shape ) == 2:
from traitsui.api import ArrayEditor
editor = ArrayEditor( auto_set=auto_set, enter_set=enter_set )
else:
from traitsui.api import TupleEditor
if self.dtype is None:
types = Any
else:
types = dtype2trait( self.dtype )
editor = TupleEditor( types = types,
labels = self.labels or [],
cols = self.cols or 1,
auto_set = auto_set,
enter_set = enter_set )
return editor
#-- Private Methods --------------------------------------------------------
def get_default_value ( self ):
""" Returns the default value constructor for the type (called from the
trait factory.
"""
return ( 7, ( self.copy_default_value,
( self.validate( None, None, self.default_value ), ), None ) )
def copy_default_value ( self, value ):
""" Returns a copy of the default value (called from the C code on
first reference to a trait with no current value).
"""
return value.copy()
#-------------------------------------------------------------------------------
# 'Array' trait:
#-------------------------------------------------------------------------------
class Array ( AbstractArray ):
""" Defines a trait whose value must be a numpy array.
"""
def __init__ ( self, dtype = None, shape = None, value = None,
typecode = None, **metadata ):
""" Returns an Array trait.
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.
Default Value
-------------
*value* 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,).
Description
-----------
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).
"""
super( Array, self ).__init__( dtype, shape, value, False,
typecode = typecode, **metadata )
#-------------------------------------------------------------------------------
# 'CArray' trait:
#-------------------------------------------------------------------------------
class CArray ( AbstractArray ):
""" Defines a trait whose value must be a numpy array, with casting
allowed.
"""
def __init__ ( self, dtype = None, shape = None, value = None,
typecode = None, **metadata ):
""" Returns a CArray trait.
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.
Default Value
-------------
*value* 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,).
Description
-----------
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).
"""
super( CArray, self ).__init__( dtype, shape, value, True,
typecode = typecode, **metadata )
|