File: trait_numeric.py

package info (click to toggle)
matplotlib 0.98.1-1%2Blenny4
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 18,624 kB
  • ctags: 22,599
  • sloc: python: 76,915; cpp: 63,459; ansic: 5,353; makefile: 111; sh: 12
file content (231 lines) | stat: -rw-r--r-- 8,486 bytes parent folder | download | duplicates (2)
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
#------------------------------------------------------------------------------
# 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 trait_base \
    import SequenceTypes, enumerate
    
from trait_handlers \
    import TraitHandler
    
from traits \
    import Str, Any
    
from traits \
    import Int as TInt
    
from traits \
    import Float as TFloat

import warnings


#-------------------------------------------------------------------------------
#  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


#-------------------------------------------------------------------------------
#  'TraitArray' class:
#-------------------------------------------------------------------------------

class TraitArray ( TraitHandler ):
    """ Handles assignment to traits based on Numpy arrays.
    """
    default_value_type = 7

    #---------------------------------------------------------------------------
    #  Initializes the object:
    #---------------------------------------------------------------------------
    
    def __init__ ( self, dtype = None, shape = None, coerce = False,
                   typecode = None):
        global ndarray, asarray
        
        # Initialize module-level globals
        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

        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:
                dtype = numpy.dtype(dtype)
            except TypeError, e:
                raise TraitError("could not convert %r to a numpy dtype" % dtype)

        self.dtype  = dtype
        self.shape  = shape
        self.coerce = coerce
 
    #---------------------------------------------------------------------------
    #  Validates that a value is legal for the trait:
    #---------------------------------------------------------------------------
        
    def validate ( self, object, name, value ):
        """ Validates that a value is legal for the trait.
        """
        #try:
        if 1:
            # Make sure the value is an array:
            type_value = type( value )
            if not isinstance( value, ndarray ): 
                if not isinstance(value, (list,tuple)):
                    self.error( object, name, self.repr( 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:
                    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

        #    print "*** pass through"
        #except Exception, e:
        #    print "*** exception:", e
        self.error( object, name, self.repr( value ) ) 

    #---------------------------------------------------------------------------
    #  Returns the default value constructor for the type (called from the
    #  trait factory):
    #---------------------------------------------------------------------------
        
    def default_value ( self, value ):
        """ Returns the default value constructor for the type (called from the
            trait factory).
        """
        return ( self.copy_default_value, 
                 ( self.validate( None, None, value ), ), None )

    #---------------------------------------------------------------------------
    #  Returns a copy of the default value (called from the C code on first
    #  reference to a trait with no current value):
    #---------------------------------------------------------------------------
                  
    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()        

    #---------------------------------------------------------------------------
    #  Returns descriptive information about the trait:
    #---------------------------------------------------------------------------
        
    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 )

    #---------------------------------------------------------------------------
    #  Gets the trait editor associated with the trait:
    #---------------------------------------------------------------------------

    def get_editor ( self, trait ):
        """ Gets the trait editor associated with the trait.
        """
        from enthought.traits.ui.api import TupleEditor
        
        if self.dtype is None:
            traits = TFloat
        else:
            traits = dtype2trait(self.dtype)
            
        return TupleEditor( traits = traits,
                            labels = trait.labels or [],
                            cols   = trait.cols   or 1  )