From: <par...@us...> - 2006-11-07 02:33:17
|
Revision: 294 http://svn.sourceforge.net/gnuplot-py/?rev=294&view=rev Author: parejkoj Date: 2006-11-06 18:33:08 -0800 (Mon, 06 Nov 2006) Log Message: ----------- Changes required to move from Numeric support over to numpy. All import commands now import numpy instead of Numeric. Comments involving Numeric were simply changed to numpy, except for JNumeric, which may not yet have a numpy version. Some syntax changed as well: NewAxis -> newaxis Float -> float_ Float32 -> float32 typecode() -> dtype.char Also, some brain-dead error handling in utils.float_array(m): the most common reason for a conversion error is a mis-match of array dimensions. It should be better about reporting that. Finally, *set -> *data in PlotItems.Data(). set is now a reserved word for the built-in set class, and I wanted to avoid collisions there. Modified Paths: -------------- trunk/ANNOUNCE.txt trunk/FAQ.txt trunk/NEWS.txt trunk/PlotItems.py trunk/README.txt trunk/TODO.txt trunk/_Gnuplot.py trunk/__init__.py trunk/demo.py trunk/funcutils.py trunk/setup.py trunk/test.py trunk/utils.py Modified: trunk/ANNOUNCE.txt =================================================================== --- trunk/ANNOUNCE.txt 2006-07-16 17:48:06 UTC (rev 293) +++ trunk/ANNOUNCE.txt 2006-11-07 02:33:08 UTC (rev 294) @@ -9,7 +9,7 @@ Prerequisites (see footnotes): the Python interpreter [1] - the Python Numeric module [3] + the Python numpy module [3] the gnuplot program [2] or, to use it under Java (experimental): @@ -20,7 +20,7 @@ Some ways this package can be used: -1. Interactive data processing: Use Python's excellent Numeric package +1. Interactive data processing: Use Python's excellent numpy package to create and manipulate arrays of numbers, and use Gnuplot.py to visualize the results. 2. Web graphics: write CGI scripts in Python that use gnuplot to @@ -76,7 +76,7 @@ portable plotting program with a command-line interface. It can make 2-d and 3-d plots and can output to myriad printers and graphics terminals. -[3] The Numeric Python extension <http://numpy.sourceforge.net/> is a +[3] The numpy Python extension <http://www.scipy.org> is a Python module that adds fast and convenient array manipulations to the Python language. [4] Jython <http://www.jython.org> is a Python interpreter that runs Modified: trunk/FAQ.txt =================================================================== --- trunk/FAQ.txt 2006-07-16 17:48:06 UTC (rev 293) +++ trunk/FAQ.txt 2006-11-07 02:33:08 UTC (rev 294) @@ -17,7 +17,7 @@ #! /usr/bin/python2 import Gnuplot, Gnuplot.funcutils -from Numeric import * +from numpy import * g = Gnuplot.Gnuplot() g.plot([[0,1.1], [1,5.8], [2,3.3], [3,4.2]]) Modified: trunk/NEWS.txt =================================================================== --- trunk/NEWS.txt 2006-07-16 17:48:06 UTC (rev 293) +++ trunk/NEWS.txt 2006-11-07 02:33:08 UTC (rev 294) @@ -235,10 +235,10 @@ dataset; e.g., what used to be written as g = Gnuplot.Gnuplot() - x = Numeric.arange(100)/10.0 + x = numpy.arange(100)/10.0 y = x**2 # Create an array of (x,y) pairs: - g.plot(Gnuplot.Data(Numeric.transpose((x, y)))) + g.plot(Gnuplot.Data(numpy.transpose((x, y)))) can now be shortened to Modified: trunk/PlotItems.py =================================================================== --- trunk/PlotItems.py 2006-07-16 17:48:06 UTC (rev 293) +++ trunk/PlotItems.py 2006-11-07 02:33:08 UTC (rev 294) @@ -1,3 +1,5 @@ +## Automatically adapted for numpy.oldnumeric Sep 22, 2006 by alter_code1.py + # $Id$ # Copyright (C) 1998-2003 Michael Haggerty <mh...@al...> @@ -21,7 +23,7 @@ except ImportError: from StringIO import StringIO -import Numeric +import numpy import gp, utils, Errors @@ -504,12 +506,12 @@ return _FileItem(filename, **keyw) -def Data(*set, **keyw): - """Create and return a _FileItem representing the data from *set. +def Data(*data, **keyw): + """Create and return a _FileItem representing the data from *data. Create a '_FileItem' object (which is a type of 'PlotItem') out of - one or more Float Python Numeric arrays (or objects that can be - converted to a Float Numeric array). If the routine is passed a + one or more Float Python numpy arrays (or objects that can be + converted to a float numpy array). If the routine is passed a single with multiple dimensions, then the last index ranges over the values comprising a single data point (e.g., [<x>, <y>, <sigma>]) and the rest of the indices select the data point. If @@ -543,29 +545,29 @@ """ - if len(set) == 1: - # set was passed as a single structure - set = utils.float_array(set[0]) + if len(data) == 1: + # data was passed as a single structure + data = utils.float_array(data[0]) # As a special case, if passed a single 1-D array, then it is # treated as one value per point (by default, plotted against # its index): - if len(set.shape) == 1: - set = set[:,Numeric.NewAxis] + if len(data.shape) == 1: + data = data[:,numpy.newaxis] else: - # set was passed column by column (for example, + # data was passed column by column (for example, # Data(x,y)); pack it into one big array (this will test # that sizes are all the same): - set = utils.float_array(set) - dims = len(set.shape) + data = utils.float_array(data) + dims = len(data.shape) # transpose so that the last index selects x vs. y: - set = Numeric.transpose(set, (dims-1,) + tuple(range(dims-1))) + data = numpy.transpose(data, (dims-1,) + tuple(range(dims-1))) if 'cols' in keyw: cols = keyw['cols'] del keyw['cols'] - if type(cols) is types.IntType: + if isinstance(cols, types.IntType): cols = (cols,) - set = Numeric.take(set, cols, -1) + data = numpy.take(data, cols, -1) if 'filename' in keyw: filename = keyw['filename'] or None @@ -585,7 +587,7 @@ # Output the content into a string: f = StringIO() - utils.write_array(f, set) + utils.write_array(f, data) content = f.getvalue() if inline: return _InlineFileItem(content, **keyw) @@ -661,7 +663,7 @@ raise Errors.DataError('data array must be two-dimensional') if xvals is None: - xvals = Numeric.arange(numx) + xvals = numpy.arange(numx) else: xvals = utils.float_array(xvals) if xvals.shape != (numx,): @@ -670,7 +672,7 @@ 'the first dimension of the data array') if yvals is None: - yvals = Numeric.arange(numy) + yvals = numpy.arange(numy) else: yvals = utils.float_array(yvals) if yvals.shape != (numy,): @@ -705,17 +707,17 @@ # documentation has the roles of x and y exchanged. We ignore # the documentation and go with the code. - mout = Numeric.zeros((numy + 1, numx + 1), Numeric.Float32) + mout = numpy.zeros((numy + 1, numx + 1), numpy.float32) mout[0,0] = numx - mout[0,1:] = xvals.astype(Numeric.Float32) - mout[1:,0] = yvals.astype(Numeric.Float32) + mout[0,1:] = xvals.astype(numpy.float32) + mout[1:,0] = yvals.astype(numpy.float32) try: # try copying without the additional copy implied by astype(): - mout[1:,1:] = Numeric.transpose(data) + mout[1:,1:] = numpy.transpose(data) except: # if that didn't work then downcasting from double # must be necessary: - mout[1:,1:] = Numeric.transpose(data.astype(Numeric.Float32)) + mout[1:,1:] = numpy.transpose(data.astype(numpy.float32)) content = mout.tostring() if (not filename) and gp.GnuplotOpts.prefer_fifo_data: @@ -726,10 +728,10 @@ # output data to file as "x y f(x)" triplets. This # requires numy copies of each x value and numx copies of # each y value. First reformat the data: - set = Numeric.transpose( - Numeric.array( - (Numeric.transpose(Numeric.resize(xvals, (numy, numx))), - Numeric.resize(yvals, (numx, numy)), + set = numpy.transpose( + numpy.array( + (numpy.transpose(numpy.resize(xvals, (numy, numx))), + numpy.resize(yvals, (numx, numy)), data)), (1,2,0)) # Now output the data with the usual routine. This will Modified: trunk/README.txt =================================================================== --- trunk/README.txt 2006-07-16 17:48:06 UTC (rev 293) +++ trunk/README.txt 2006-11-07 02:33:08 UTC (rev 294) @@ -65,8 +65,8 @@ Obviously, you must have the gnuplot program if Gnuplot.py is to be of any use to you. Gnuplot can be obtained via -<http://www.gnuplot.info>. You also need Python's Numerical -extension, which is available from <http://numpy.sourceforge.net>. +<http://www.gnuplot.info>. You also need a copy of the numpy package, which +is available from the Scipy group at <http://www.scipy.org/Download>. Gnuplot.py uses Python distutils <http://www.python.org/doc/current/inst/inst.html> and can be Modified: trunk/TODO.txt =================================================================== --- trunk/TODO.txt 2006-07-16 17:48:06 UTC (rev 293) +++ trunk/TODO.txt 2006-11-07 02:33:08 UTC (rev 294) @@ -33,4 +33,3 @@ * Support gnuplot's new abilities to allow user interaction via the mouse. I believe this will require 2-way communication between Gnuplot.py and gnuplot. - Modified: trunk/_Gnuplot.py =================================================================== --- trunk/_Gnuplot.py 2006-07-16 17:48:06 UTC (rev 293) +++ trunk/_Gnuplot.py 2006-11-07 02:33:08 UTC (rev 294) @@ -239,8 +239,8 @@ 'items' is a sequence of items, each of which should be a 'PlotItem' of some kind, a string (interpreted as a function - string for gnuplot to evaluate), or a Numeric array (or - something that can be converted to a Numeric array). + string for gnuplot to evaluate), or a numpy array (or + something that can be converted to a numpy array). """ Modified: trunk/__init__.py =================================================================== --- trunk/__init__.py 2006-07-16 17:48:06 UTC (rev 293) +++ trunk/__init__.py 2006-11-07 02:33:08 UTC (rev 294) @@ -128,9 +128,9 @@ Restrictions: - - Relies on the Numeric Python extension. This can be obtained from - "SourceForge", http://sourceforge.net/projects/numpy/. If you're - interested in gnuplot, you would probably also want NumPy anyway. + - Relies on the numpy Python extension. This can be obtained from + the Scipy group at <http://www.scipy.org/Download>. If you're + interested in gnuplot, you would probably also want numpy anyway. - Only a small fraction of gnuplot functionality is implemented as explicit method functions. However, you can give arbitrary Modified: trunk/demo.py =================================================================== --- trunk/demo.py 2006-07-16 17:48:06 UTC (rev 293) +++ trunk/demo.py 2006-11-07 02:33:08 UTC (rev 294) @@ -13,7 +13,7 @@ """ -from Numeric import * +from numpy import * # If the package has been installed correctly, this should work: import Gnuplot, Gnuplot.funcutils @@ -28,7 +28,7 @@ g = Gnuplot.Gnuplot(debug=1) g.title('A simple example') # (optional) g('set data style linespoints') # give gnuplot an arbitrary command - # Plot a list of (x, y) pairs (tuples or a Numeric array would + # Plot a list of (x, y) pairs (tuples or a numpy array would # also be OK): g.plot([[0,1.1], [1,5.8], [2,3.3], [3,4.2]]) raw_input('Please press return to continue...\n') @@ -36,7 +36,7 @@ g.reset() # Plot one dataset from an array and one via a gnuplot function; # also demonstrate the use of item-specific options: - x = arange(10, typecode=Float) + x = arange(10, dtype='float_') y1 = x**2 # Notice how this plotitem is created here but used later? This # is convenient if the same dataset has to be plotted multiple @@ -71,8 +71,8 @@ # Make a 2-d array containing a function of x and y. First create # xm and ym which contain the x and y values in a matrix form that # can be `broadcast' into a matrix of the appropriate shape: - xm = x[:,NewAxis] - ym = y[NewAxis,:] + xm = x[:,newaxis] + ym = y[newaxis,:] m = (sin(xm) + 0.1*xm) - ym**2 g('set parametric') g('set data style lines') Modified: trunk/funcutils.py =================================================================== --- trunk/funcutils.py 2006-07-16 17:48:06 UTC (rev 293) +++ trunk/funcutils.py 2006-11-07 02:33:08 UTC (rev 294) @@ -1,3 +1,5 @@ +## Automatically adapted for numpy.oldnumeric Sep 22, 2006 by alter_code1.py + #! /usr/bin/env python # $Id$ @@ -14,19 +16,19 @@ """ -import Numeric +import numpy import Gnuplot, utils -def tabulate_function(f, xvals, yvals=None, typecode=None, ufunc=0): +def tabulate_function(f, xvals, yvals=None, dtype=None, ufunc=0): """Evaluate and tabulate a function on a 1- or 2-D grid of points. f should be a function taking one or two floating-point parameters. If f takes one parameter, then xvals should be a 1-D array and - yvals should be None. The return value is a Numeric array + yvals should be None. The return value is a numpy array '[f(x[0]), f(x[1]), ..., f(x[-1])]'. If f takes two parameters, then 'xvals' and 'yvals' should each be @@ -37,7 +39,7 @@ If 'ufunc=0', then 'f' is evaluated at each point using a Python loop. This can be slow if the number of points is large. If - speed is an issue, you should write 'f' in terms of Numeric ufuncs + speed is an issue, you should write 'f' in terms of numpy ufuncs and use the 'ufunc=1' feature described next. If called with 'ufunc=1', then 'f' should be a function that is @@ -49,34 +51,34 @@ if yvals is None: # f is a function of only one variable: - xvals = Numeric.asarray(xvals, typecode) + xvals = numpy.asarray(xvals, dtype) if ufunc: return f(xvals) else: - if typecode is None: - typecode = xvals.typecode() + if dtype is None: + dtype = xvals.dtype.char - m = Numeric.zeros((len(xvals),), typecode) + m = numpy.zeros((len(xvals),), dtype) for xi in range(len(xvals)): x = xvals[xi] m[xi] = f(x) return m else: # f is a function of two variables: - xvals = Numeric.asarray(xvals, typecode) - yvals = Numeric.asarray(yvals, typecode) + xvals = numpy.asarray(xvals, dtype) + yvals = numpy.asarray(yvals, dtype) if ufunc: - return f(xvals[:,Numeric.NewAxis], yvals[Numeric.NewAxis,:]) + return f(xvals[:,numpy.newaxis], yvals[numpy.newaxis,:]) else: - if typecode is None: - # choose a result typecode based on what '+' would return + if dtype is None: + # choose a result dtype based on what '+' would return # (yecch!): - typecode = (Numeric.zeros((1,), xvals.typecode()) + - Numeric.zeros((1,), yvals.typecode())).typecode() + dtype = (numpy.zeros((1,), xvals.dtype.char) + + numpy.zeros((1,), yvals.dtype.char)).dtype.char - m = Numeric.zeros((len(xvals), len(yvals)), typecode) + m = numpy.zeros((len(xvals), len(yvals)), dtype) for xi in range(len(xvals)): x = xvals[xi] for yi in range(len(yvals)): Modified: trunk/setup.py =================================================================== --- trunk/setup.py 2006-07-16 17:48:06 UTC (rev 293) +++ trunk/setup.py 2006-11-07 02:33:08 UTC (rev 294) @@ -29,7 +29,8 @@ author_email='mh...@al...', url='http://gnuplot-py.sourceforge.net', license='LGPL', - licence='LGPL', # Spelling error in distutils + # fixed as of python2.3 + # licence='LGPL', # Spelling error in distutils # Description of the package in the distribution package_dir={'Gnuplot' : '.'}, Modified: trunk/test.py =================================================================== --- trunk/test.py 2006-07-16 17:48:06 UTC (rev 293) +++ trunk/test.py 2006-11-07 02:33:08 UTC (rev 294) @@ -1,3 +1,5 @@ +## Automatically adapted for numpy.oldnumeric Sep 22, 2006 by alter_code1.py + #! /usr/bin/env python # $Id$ @@ -15,8 +17,7 @@ """ import os, time, math, tempfile -import Numeric -from Numeric import NewAxis +import numpy try: import Gnuplot, Gnuplot.PlotItems, Gnuplot.funcutils @@ -59,7 +60,7 @@ f = open(filename1, 'w') filename2 = tempfile.mktemp() try: - for x in Numeric.arange(100)/5. - 10.: + for x in numpy.arange(100.)/5. - 10.: f.write('%s %s %s\n' % (x, math.cos(x), math.sin(x))) f.close() @@ -141,10 +142,10 @@ g.plot(f) print '############### test Data ###################################' - x = Numeric.arange(100)/5. - 10. - y1 = Numeric.cos(x) - y2 = Numeric.sin(x) - d = Numeric.transpose((x,y1,y2)) + x = numpy.arange(100)/5. - 10. + y1 = numpy.cos(x) + y2 = numpy.sin(x) + d = numpy.transpose((x,y1,y2)) wait('Plot Data against its index') g.plot(Gnuplot.Data(y2, inline=0)) @@ -187,7 +188,7 @@ g.plot(Gnuplot.Data(d, title='Cosine of x')) print '############### test compute_Data ###########################' - x = Numeric.arange(100)/5. - 10. + x = numpy.arange(100)/5. - 10. wait('Plot Data, computed by Gnuplot.py') g.plot( @@ -259,14 +260,14 @@ print '############### test GridData and compute_GridData ##########' # set up x and y values at which the function will be tabulated: - x = Numeric.arange(35)/2.0 - y = Numeric.arange(30)/10.0 - 1.5 + x = numpy.arange(35)/2.0 + y = numpy.arange(30)/10.0 - 1.5 # Make a 2-d array containing a function of x and y. First create # xm and ym which contain the x and y values in a matrix form that # can be `broadcast' into a matrix of the appropriate shape: - xm = x[:,NewAxis] - ym = y[NewAxis,:] - m = (Numeric.sin(xm) + 0.1*xm) - ym**2 + xm = x[:,numpy.newaxis] + ym = y[numpy.newaxis,:] + m = (numpy.sin(xm) + 0.1*xm) - ym**2 wait('a function of two variables from a GridData file') g('set parametric') g('set data style lines') @@ -299,12 +300,12 @@ wait('Use compute_GridData in ufunc and binary mode') g.splot(Gnuplot.funcutils.compute_GridData( - x,y, lambda x,y: Numeric.sin(x) + 0.1*x - y**2, + x,y, lambda x,y: numpy.sin(x) + 0.1*x - y**2, ufunc=1, binary=1, )) wait('Same thing, with an intermediate file') Gnuplot.funcutils.compute_GridData( - x,y, lambda x,y: Numeric.sin(x) + 0.1*x - y**2, + x,y, lambda x,y: numpy.sin(x) + 0.1*x - y**2, ufunc=1, binary=1, filename=filename1) g.splot(Gnuplot.File(filename1, binary=1)) Modified: trunk/utils.py =================================================================== --- trunk/utils.py 2006-07-16 17:48:06 UTC (rev 293) +++ trunk/utils.py 2006-11-07 02:33:08 UTC (rev 294) @@ -1,3 +1,5 @@ +## Automatically adapted for numpy.oldnumeric Sep 22, 2006 by alter_code1.py + #! /usr/bin/env python # $Id$ @@ -15,30 +17,34 @@ """ import string -import Numeric +import numpy - def float_array(m): - """Return the argument as a Numeric array of type at least 'Float32'. + """Return the argument as a numpy array of type at least 'Float32'. Leave 'Float64' unchanged, but upcast all other types to 'Float32'. Allow also for the possibility that the argument is a - python native type that can be converted to a Numeric array using - 'Numeric.asarray()', but in that case don't worry about + python native type that can be converted to a numpy array using + 'numpy.asarray()', but in that case don't worry about downcasting to single-precision float. """ try: # Try Float32 (this will refuse to downcast) - return Numeric.asarray(m, Numeric.Float32) + return numpy.asarray(m, numpy.float32) except TypeError: # That failure might have been because the input array was - # of a wider data type than Float32; try to convert to the + # of a wider data type than float32; try to convert to the # largest floating-point type available: - return Numeric.asarray(m, Numeric.Float) + # NOTE TBD: I'm not sure float_ is the best data-type for this... + try: + return numpy.asarray(m, numpy.float_) + except TypeError: + # TBD: Need better handling of this error! + print "Fatal: array dimensions not equal!" + return None - def write_array(f, set, item_sep=' ', nest_prefix='', nest_suffix='\n', nest_sep=''): This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |