From: <jd...@us...> - 2008-04-03 15:25:28
|
Revision: 5028 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=5028&view=rev Author: jdh2358 Date: 2008-04-03 08:24:20 -0700 (Thu, 03 Apr 2008) Log Message: ----------- some small fixes to excel tools Modified Paths: -------------- branches/v0_91_maint/examples/rec_groupby_demo.py branches/v0_91_maint/lib/matplotlib/mlab.py branches/v0_91_maint/lib/matplotlib/toolkits/exceltools.py Modified: branches/v0_91_maint/examples/rec_groupby_demo.py =================================================================== --- branches/v0_91_maint/examples/rec_groupby_demo.py 2008-04-02 13:33:21 UTC (rev 5027) +++ branches/v0_91_maint/examples/rec_groupby_demo.py 2008-04-03 15:24:20 UTC (rev 5028) @@ -6,14 +6,20 @@ r.sort() def daily_return(prices): + 'an array of daily returns from price array' g = np.zeros_like(prices) g[1:] = (prices[1:]-prices[:-1])/prices[:-1] return g def volume_code(volume): + 'code the continuous volume data categorically' ind = np.searchsorted([1e5,1e6, 5e6,10e6, 1e7], volume) return ind +# a list of (dtype_name, summary_function, output_dtype_name). +# rec_summarize will call on each function on the indicated recarray +# attribute, and the result assigned to output name in the return +# record array. summaryfuncs = ( ('date', lambda x: [thisdate.year for thisdate in x], 'years'), ('date', lambda x: [thisdate.month for thisdate in x], 'months'), @@ -24,6 +30,10 @@ rsum = mlab.rec_summarize(r, summaryfuncs) +# stats is a list of (dtype_name, function, output_dtype_name). +# rec_groupby will summarize the attribute identified by the +# dtype_name over the groups in the groupby list, and assign the +# result to the output_dtype_name stats = ( ('dreturn', len, 'rcnt'), ('dreturn', np.mean, 'rmean'), @@ -31,6 +41,7 @@ ('dreturn', np.std, 'rsigma'), ) +# you can summarize over a single variable, like years or months print 'summary by years' ry = mlab.rec_groupby(rsum, ('years',), stats) print mlab. rec2txt(ry) @@ -39,6 +50,7 @@ rm = mlab.rec_groupby(rsum, ('months',), stats) print mlab.rec2txt(rm) +# or over multiple variables like years and months print 'summary by year and month' rym = mlab.rec_groupby(rsum, ('years','months'), stats) print mlab.rec2txt(rym) Modified: branches/v0_91_maint/lib/matplotlib/mlab.py =================================================================== --- branches/v0_91_maint/lib/matplotlib/mlab.py 2008-04-02 13:33:21 UTC (rev 5027) +++ branches/v0_91_maint/lib/matplotlib/mlab.py 2008-04-03 15:24:20 UTC (rev 5028) @@ -1944,6 +1944,13 @@ else: return b +def safe_isinf(x): + 'isnan for arbitrary types' + try: b = npy.isinf(x) + except NotImplementedError: return False + else: return b + + def rec_append_field(rec, name, arr, dtype=None): 'return a new record array with field name populated with data from array arr' arr = npy.asarray(arr) Modified: branches/v0_91_maint/lib/matplotlib/toolkits/exceltools.py =================================================================== --- branches/v0_91_maint/lib/matplotlib/toolkits/exceltools.py 2008-04-02 13:33:21 UTC (rev 5027) +++ branches/v0_91_maint/lib/matplotlib/toolkits/exceltools.py 2008-04-03 15:24:20 UTC (rev 5028) @@ -5,7 +5,7 @@ import matplotlib.mlab as mlab import matplotlib.toolkits.exceltools as exceltools - + r = mlab.csv2rec('somefile.csv', checkrows=0) formatd = dict( @@ -107,6 +107,10 @@ else: if mlab.safe_isnan(val): ws.write(rownum, colnum+i, 'NaN') + elif mlab.safe_isinf(val): + if val<0: sign='-' + else: sign='+' + ws.write(rownum, colnum+i, '%sInf'%sign) else: ws.write(rownum, colnum+i, val, format.xlstyle) rownum += 1 This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |