|
From: <jd...@us...> - 2007-07-20 13:44:13
|
Revision: 3586
http://matplotlib.svn.sourceforge.net/matplotlib/?rev=3586&view=rev
Author: jdh2358
Date: 2007-07-20 06:44:10 -0700 (Fri, 20 Jul 2007)
Log Message:
-----------
traits cleanup
Modified Paths:
--------------
trunk/matplotlib/mpl1/mpl1.py
trunk/matplotlib/mpl1/test.py
Removed Paths:
-------------
trunk/matplotlib/mpl1/mtraits.py
Modified: trunk/matplotlib/mpl1/mpl1.py
===================================================================
--- trunk/matplotlib/mpl1/mpl1.py 2007-07-20 13:21:42 UTC (rev 3585)
+++ trunk/matplotlib/mpl1/mpl1.py 2007-07-20 13:44:10 UTC (rev 3586)
@@ -2,9 +2,57 @@
import enthought.traits.api as traits
from matplotlib import agg
+from matplotlib import colors as mcolors
import numpy as npy
-import mtraits # some handy traits for mpl
+
+class ColorHandler(traits.TraitHandler):
+ is_mapped = True
+
+ def post_setattr(self, object, name, value):
+ object.__dict__[ name + '_' ] = self.mapped_value( value )
+
+ def mapped_value(self, value ):
+ if value is None: return None
+ return mcolors.colorConverter.to_rgba(value)
+
+
+ def validate(self, object, name, value):
+ try:
+ self.mapped_value(value)
+ except ValueError:
+ return self.error(object, name, value)
+ else:
+ return value
+
+ def info(self):
+ return """\
+any valid matplotlib color, eg an abbreviation like 'r' for red, a full
+name like 'orange', a hex color like '#efefef', a grayscale intensity
+like '0.5', or an RGBA tuple (1,0,0,1)"""
+
+class MTraitsNamespace:
+ DPI = traits.Float(72.)
+ Affine = traits.Array('d', (3,3), npy.array([[1,0,0],[0,1,0],[0,0,1]], npy.float_))
+ Alpha = traits.Range(0., 1., 0.)
+ AntiAliased = traits.true
+ Codes = traits.Array('b', value=npy.array([0,0], dtype=npy.uint8))
+ Color = traits.Trait('black', ColorHandler())
+ DPI = traits.Float(72.)
+ Interval = traits.Array('d', (2,), npy.array([0.0, 1.0], npy.float_))
+ LineStyle = traits.Trait('-', '--', '-.', ':', 'steps', None)
+ LineWidth = traits.Float(1.0)
+ Marker = traits.Trait(None, '.', ',', 'o', '^', 'v', '<', '>', 's',
+ '+', 'x', 'd', 'D', '|', '_', 'h', 'H',
+ 'p', '1', '2', '3', '4')
+ MarkerSize = traits.Float(6)
+ Verts = traits.Array('d', value=npy.array([[0,0],[0,0]], npy.float_))
+ PathData = traits.Tuple(Codes, Verts)
+ Visible = traits.true
+
+
+mtraits = MTraitsNamespace()
+
def affine_axes(rect):
'make an affine for a typical l,b,w,h axes rectangle'
@@ -35,7 +83,7 @@
dtype=npy.float_)
-class Renderer:
+class Renderer(traits.HasTraits):
dpi = traits.Float(72.)
def __init__(self, width, height):
@@ -161,7 +209,7 @@
Locs = npy.dot(affineverts, Locs)
- dpiscale = 1.0 # self.dpi/72. # for some reason this is broken
+ dpiscale = self.dpi/72. # for some reason this is broken
# this will need to be highly optimized and hooked into some
# extension code using cached marker rasters as we now do in
# _backend_agg
@@ -210,15 +258,16 @@
class Func(traits.HasTraits):
def __call__(self, X):
'transform the numpy array with shape N,2'
- raise NotImplementedError
+ return X
def invert(self, x, y):
'invert the point x, y'
- raise NotImplementedError
+ return x, y
def point(self, x, y):
'transform the point x, y'
- raise NotImplementedError
+ return x, y
+
class Identity(Func):
def __call__(self, X):
@@ -252,7 +301,7 @@
raise NotImplementedError
-mtraits.Model = traits.Trait(None, Identity, Polar)
+mtraits.Model = traits.Instance(Func, ())
@@ -268,7 +317,7 @@
fillcolor = mtraits.Color('blue')
alpha = mtraits.Alpha(1.0)
linewidth = mtraits.LineWidth(1.0)
- antialiased = mtraits.FlexibleTrueTrait()
+ antialiased = mtraits.AntiAliased
pathdata = mtraits.PathData()
affine = mtraits.Affine()
@@ -309,8 +358,8 @@
# hmm, I would have thought these would be called by the attr
# setting above
self._pathdata_changed(None, self.pathdata)
- self._fillcolor_changed(None, self.fillcolor)
- self._strokecolor_changed(None, self.strokecolor)
+ self._fillcolor__changed(None, self.fillcolor_)
+ self._strokecolor__changed(None, self.strokecolor_)
@staticmethod
@@ -334,10 +383,10 @@
self.agg_path = AggPath.make_agg_path(newdata)
- def _fillcolor_changed(self, oldcolor, newcolor):
+ def _fillcolor__changed(self, oldcolor, newcolor):
self.agg_fillcolor = self.color_to_rgba8(newcolor)
- def _strokecolor_changed(self, oldcolor, newcolor):
+ def _strokecolor__changed(self, oldcolor, newcolor):
c = self.color_to_rgba8(newcolor)
self.agg_strokecolor = c
@@ -345,7 +394,7 @@
def color_to_rgba8(self, color):
if color is None: return None
- rgba = [int(255*c) for c in color.r, color.g, color.b, color.a]
+ rgba = [int(255*c) for c in color]
return agg.rgba8(*rgba)
class Markers(traits.HasTraits):
@@ -405,8 +454,8 @@
class Artist(traits.HasTraits):
zorder = traits.Float(1.0)
- alpha = mtraits.Alpha(1.0)
- visible = mtraits.FlexibleTrueTrait()
+ alpha = mtraits.Alpha()
+ visible = mtraits.Visible()
affine = mtraits.Affine()
def __init__(self):
@@ -427,7 +476,7 @@
class Line(Artist):
linestyle = mtraits.LineStyle('-')
- antialiased = mtraits.FlexibleTrueTrait(True)
+ antialiased = mtraits.AntiAliased()
color = mtraits.Color('blue')
linewidth = mtraits.LineWidth(1.0)
marker = mtraits.Marker(None)
Deleted: trunk/matplotlib/mpl1/mtraits.py
===================================================================
--- trunk/matplotlib/mpl1/mtraits.py 2007-07-20 13:21:42 UTC (rev 3585)
+++ trunk/matplotlib/mpl1/mtraits.py 2007-07-20 13:44:10 UTC (rev 3586)
@@ -1,135 +0,0 @@
-"""
-Install instructions for traits 2.0
-
- # blow away old enthought
- rm -rf ~/dev/lib/python2.4/site-packages/enthought.*
-
- # get easy_install, if necessary
- wget http://peak.telecommunity.com/dist/ez_setup.py
- sudo python sez_setup.py
-
- sudo easy_install -f http://code.enthought.com/enstaller/eggs/source/unstable "enthought.etsconfig < 3.0a" "enthought.util <3.0a" "enthought.debug <3.0a"
-
- svn co https://svn.enthought.com/svn/enthought/branches/enthought.traits_2.0 enthought_traits
-
- cd enthought_traits/
- sudo python setup.py install
-
-
-"""
-# Here is some example code showing how to define some representative
-# rc properties and construct a matplotlib artist using traits.
-# Because matplotlib ships with enthought traits already, you can run
-# this script with just matplotlib. Unfortunately, we do not ship the
-# ex UI component so you can't test that part. I'm a bit of a traits
-# newbie so there are probably better ways to do what I have done
-# below.
-
-import sys, os, re
-import enthought.traits.api as traits
-from matplotlib.cbook import is_string_like
-from matplotlib import colors as mcolors
-import numpy as npy
-
-doprint = True
-FlexibleTrueTrait = traits.Trait(
- True,
- { 'true': True, 't': True, 'yes': True, 'y': True, 'on': True, True: True,
- 'false': False, 'f': False, 'no': False, 'n': False, 'off': False, False: False
- } )
-FlexibleFalseTrait = traits.Trait( False, FlexibleTrueTrait )
-
-colors = mcolors.cnames
-
-def hex2color(s):
- "Convert hex string (like html uses, eg, #efefef) to a r,g,b tuple"
- return tuple([int(n, 16)/255.0 for n in (s[1:3], s[3:5], s[5:7])])
-
-def color_converter(x):
- return mcolors.colorConverter(x)
-class RGBA(traits.HasTraits):
- # r,g,b,a in the range 0-1 with default color 0,0,0,1 (black)
- r = traits.Range(0., 1., 0.)
- g = traits.Range(0., 1., 0.)
- b = traits.Range(0., 1., 0.)
- a = traits.Range(0., 1., 1.)
- def __init__(self, r=0., g=0., b=0., a=1.):
- self.r = r
- self.g = g
- self.b = b
- self.a = a
-
- def __repr__(self):
- return '(%1.2f, %1.2f, %1.2f, %1.2f)'%(self.r, self.g, self.b, self.a)
-
-def tuple_to_rgba(ob, name, val):
- tup = [float(x) for x in val]
- if len(tup)==3:
- r,g,b = tup
- return RGBA(r,g,b)
- elif len(tup)==4:
- r,g,b,a = tup
- return RGBA(r,g,b,a)
- else:
- raise ValueError
-tuple_to_rgba.info = 'a RGB or RGBA tuple of floats'
-
-def hex_to_rgba(ob, name, val):
- rgx = re.compile('^#[0-9A-Fa-f]{6}$')
-
- if not is_string_like(val):
- raise TypeError
- if rgx.match(val) is None:
- raise ValueError
- r,g,b = hex2color(val)
- return RGBA(r,g,b,1.0)
-hex_to_rgba.info = 'a hex color string'
-
-def colorname_to_rgba(ob, name, val):
- hex = colors[val.lower()]
- r,g,b = hex2color(hex)
- return RGBA(r,g,b,1.0)
-colorname_to_rgba.info = 'a named color'
-
-def float_to_rgba(ob, name, val):
- val = float(val)
- return RGBA(val, val, val, 1.)
-float_to_rgba.info = 'a grayscale intensity'
-
-
-
-def file_exists(ob, name, val):
- fh = file(val, 'r')
- return val
-
-def path_exists(ob, name, val):
- os.path.exists(val)
-linestyles = ('-', '--', '-.', ':', 'steps', None)
-TICKLEFT, TICKRIGHT, TICKUP, TICKDOWN = range(4)
-linemarkers = (None, '.', ',', 'o', '^', 'v', '<', '>', 's',
- '+', 'x', 'd', 'D', '|', '_', 'h', 'H',
- 'p', '1', '2', '3', '4',
- TICKLEFT,
- TICKRIGHT,
- TICKUP,
- TICKDOWN,
- )
-
-colorfuncs = RGBA(), float_to_rgba, colorname_to_rgba, RGBA, hex_to_rgba, tuple_to_rgba, None
-
-Affine = traits.Array('d', (3,3), npy.array([[1,0,0],[0,1,0],[0,0,1]], npy.float_))
-Alpha = traits.Float(1.0)
-Alpha = traits.Range(0., 1., 0.)
-Antialiased = FlexibleTrueTrait
-Codes = traits.Array('b', value=npy.array([0,0], dtype=npy.uint8))
-Color = traits.Trait(*colorfuncs)
-DPI = traits.Float(72.)
-Interval = traits.Array('d', (2,), npy.array([0.0, 1.0], npy.float_))
-LineStyle = traits.Trait(*linestyles)
-LineWidth = traits.Float(0.5)
-Marker = traits.Trait(*linemarkers)
-MarkerSize = traits.Float(6)
-Verts = traits.Array('d', value=npy.array([[0,0],[0,0]], npy.float_))
-PathData = traits.Tuple(Codes, Verts)
-
-
Modified: trunk/matplotlib/mpl1/test.py
===================================================================
--- trunk/matplotlib/mpl1/test.py 2007-07-20 13:21:42 UTC (rev 3585)
+++ trunk/matplotlib/mpl1/test.py 2007-07-20 13:44:10 UTC (rev 3586)
@@ -1,50 +1,19 @@
-import numpy as npy
-import enthought.traits.api as traits
+from enthought.traits.api import *
+def Alias(name):
+ return Property(lambda obj: getattr(obj, name),
+ lambda obj, val: setattr(obj, name, val))
-class C(traits.HasTraits):
- x = traits.Float(0.0)
- y = traits.Float(1.0)
+class Path(HasTraits):
+ strokecolor = Color()
-class MyC(C):
- xy = traits.Float(0.0)
- def __init__(self, c):
- self.x = c.x
- self.y = c.y
+class Line(Path):
+ color = Alias('strokecolor')
- c.sync_trait('x', self)
- c.sync_trait('y', self)
-
- def _x_changed(self, old, new):
- self.xy = self.x * self.y
-
- def _y_changed(self, old, new):
- self.xy = self.x * self.y
-
-
-# C objects are created at top level
-c = C()
-c.x = 1
-c.y = 1
-
-
-
-
-
-class Backend:
-
- def register_c(self, c):
- # only gets C objects after creation
- self.myc = MyC(c)
-
-backend = Backend()
-
-backend.register_c(c)
-
-c.x = 4
-
-print backend.myc.xy
-
-
-
+ def __init__(self, x, color='red'):
+ self.x = x
+ self.color = color
+
+line = Line(1)
+print line.strokecolor
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|