From: <md...@us...> - 2007-12-17 15:43:08
|
Revision: 4758 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=4758&view=rev Author: mdboom Date: 2007-12-17 07:43:03 -0800 (Mon, 17 Dec 2007) Log Message: ----------- Merged revisions 4735-4757 via svnmerge from http://matplotlib.svn.sf.net/svnroot/matplotlib/trunk/matplotlib ........ r4745 | jdh2358 | 2007-12-15 16:33:38 -0500 (Sat, 15 Dec 2007) | 1 line changed %g to %r for rec2csv ........ r4747 | astraw | 2007-12-16 14:28:46 -0500 (Sun, 16 Dec 2007) | 1 line Add test for rec2csv and csv2rec roundtrip not losing precision. ........ r4748 | astraw | 2007-12-16 15:53:35 -0500 (Sun, 16 Dec 2007) | 1 line rec2csv does not close filehandles passed in open ........ r4749 | astraw | 2007-12-16 18:19:59 -0500 (Sun, 16 Dec 2007) | 1 line fix csv2rec roundtrip for funky strings, too ........ r4755 | jdh2358 | 2007-12-16 23:37:38 -0500 (Sun, 16 Dec 2007) | 1 line mods to support dates in csv2rec and friends ........ Modified Paths: -------------- branches/transforms/CHANGELOG branches/transforms/lib/matplotlib/cbook.py branches/transforms/lib/matplotlib/mlab.py branches/transforms/lib/matplotlib/mpl-data/matplotlib.conf.template Added Paths: ----------- branches/transforms/unit/mlab_unit.py Property Changed: ---------------- branches/transforms/ Property changes on: branches/transforms ___________________________________________________________________ Name: svnmerge-integrated - /trunk/matplotlib:1-4734 + /trunk/matplotlib:1-4757 Modified: branches/transforms/CHANGELOG =================================================================== --- branches/transforms/CHANGELOG 2007-12-17 15:41:47 UTC (rev 4757) +++ branches/transforms/CHANGELOG 2007-12-17 15:43:03 UTC (rev 4758) @@ -1,3 +1,6 @@ +2007-12-16 rec2csv saves doubles without losing precision. Also, it + does not close filehandles passed in open. - JDH,ADS + 2007-12-13 Moved rec2gtk to matplotlib.toolkits.gtktools and rec2excel to matplotlib.toolkits.exceltools - JDH Modified: branches/transforms/lib/matplotlib/cbook.py =================================================================== --- branches/transforms/lib/matplotlib/cbook.py 2007-12-17 15:41:47 UTC (rev 4757) +++ branches/transforms/lib/matplotlib/cbook.py 2007-12-17 15:43:03 UTC (rev 4758) @@ -225,7 +225,7 @@ except TypeError: return False else: return True -def to_filehandle(fname, flag='r'): +def to_filehandle(fname, flag='r', return_opened=False): """ fname can be a filename or a file handle. Support for gzipped files is automatic, if the filename ends in .gz. flag is a @@ -237,10 +237,14 @@ fh = gzip.open(fname, flag) else: fh = file(fname, flag) + opened = True elif hasattr(fname, 'seek'): fh = fname + opened = False else: raise ValueError('fname must be a string or file handle') + if return_opened: + return fh, opened return fh def flatten(seq, scalarp=is_scalar): Modified: branches/transforms/lib/matplotlib/mlab.py =================================================================== --- branches/transforms/lib/matplotlib/mlab.py 2007-12-17 15:41:47 UTC (rev 4757) +++ branches/transforms/lib/matplotlib/mlab.py 2007-12-17 15:43:03 UTC (rev 4758) @@ -2129,6 +2129,7 @@ process_skiprows(reader) + dateparser = dateutil.parser.parse def myfloat(x): if x==missing: @@ -2136,9 +2137,18 @@ else: return float(x) + def mydate(x): + # try and return a date object + d = dateparser(x) + + if d.hour>0 or d.minute>0 or d.second>0: + raise ValueError('not a date') + return d.date() + + def get_func(item, func): # promote functions in this order - funcmap = {int:myfloat, myfloat:dateutil.parser.parse, dateutil.parser.parse:str} + funcmap = {int:myfloat, myfloat:mydate, mydate:dateparser, dateparser:str} try: func(item) except: if func==str: @@ -2238,8 +2248,12 @@ class FormatString(FormatObj): def tostr(self, x): - return '"%s"'%self.toval(x) + val = repr(x) + return val[1:-1] +#class FormatString(FormatObj): +# def tostr(self, x): +# return '"%r"'%self.toval(x) class FormatFormatStr(FormatObj): def __init__(self, fmt): @@ -2297,7 +2311,7 @@ npy.float32 : FormatFloat(), npy.float64 : FormatFloat(), npy.object_ : FormatObj(), - npy.string_ : FormatObj(), + npy.string_ : FormatString(), } def get_formatd(r, formatd=None): @@ -2317,7 +2331,7 @@ format = copy.deepcopy(format) if isinstance(format, FormatFloat): format.scale = 1. # override scaling for storage - format.fmt = '%g' # maximal precision + format.fmt = '%r' return format def rec2csv(r, fname, delimiter=',', formatd=None): @@ -2335,13 +2349,14 @@ for i, name in enumerate(r.dtype.names): funcs.append(csvformat_factory(formatd[name]).tostr) - fh = cbook.to_filehandle(fname, 'w') + fh, opened = cbook.to_filehandle(fname, 'w', return_opened=True) writer = csv.writer(fh, delimiter=delimiter) header = r.dtype.names writer.writerow(header) for row in r: writer.writerow([func(val) for func, val in zip(funcs, row)]) - fh.close() + if opened: + fh.close() Modified: branches/transforms/lib/matplotlib/mpl-data/matplotlib.conf.template =================================================================== --- branches/transforms/lib/matplotlib/mpl-data/matplotlib.conf.template 2007-12-17 15:41:47 UTC (rev 4757) +++ branches/transforms/lib/matplotlib/mpl-data/matplotlib.conf.template 2007-12-17 15:43:03 UTC (rev 4758) @@ -3,32 +3,32 @@ # This is a sample matplotlib configuration file. It should be placed # in HOME/.matplotlib/matplotlibrc (unix/linux like systems) and # C:\Documents and Settings\yourname\.matplotlib (win32 systems) -# +# # By default, the installer will overwrite the existing file in the install # path, so if you want to preserve yours, please move it to your HOME dir and # set the environment variable if necessary. -# +# # This file is best viewed in a editor which supports ini or conf mode syntax # highlighting. -# +# # Blank lines, or lines starting with a comment symbol, are ignored, # as are trailing comments. Other lines must have the format -# +# # key = val optional comment -# +# # val should be valid python syntax, just as you would use when setting # properties using rcParams. This should become more obvious by inspecting # the default values listed herein. -# +# # Colors: for the color values below, you can either use # - a matplotlib color string, such as r | k | b # - an rgb tuple, such as (1.0, 0.5, 0.0) # - a hex string, such as #ff00ff or ff00ff # - a scalar grayscale intensity such as 0.75 # - a legal html color name, eg red | blue | darkslategray -# +# # Interactivity: see http://matplotlib.sourceforge.net/interactive.html. -# +# # ### CONFIGURATION BEGINS HERE ### # a value of type 'str' @@ -42,7 +42,7 @@ # 'Africa/Abidjan' or 'Africa/Accra' or 'Africa/Addis_Ababa' or # 'Africa/Algiers' or 'Africa/Asmara' or 'Africa/Asmera' or 'Africa/Bamako' or # 'Africa/Bangui' or 'Africa/Banjul' or 'Africa/Bissau' or 'Africa/Blantyre' -# <...snipped 156 lines...> +# <...snipped 156 lines...> # 'US/Michigan' or 'US/Mountain' or 'US/Pacific' or 'US/Pacific-New' or # 'US/Samoa' or 'UTC' or 'Universal' or 'W-SU' or 'WET' or 'Zulu' or # 'posixrules' @@ -108,10 +108,10 @@ [[ps]] # 3 or 42 fonttype = 3 - # 'auto' or 'letter' or 'legal' or 'ledger' or 'A0' or 'A1' or 'A2' or - # 'A3' or 'A4' or 'A5' or 'A6' or 'A7' or 'A8' or 'A9' or 'A10' or - # 'B0' or 'B1' or 'B2' or 'B3' or 'B4' or 'B5' or 'B6' or 'B7' or 'B8' - # or 'B9' or 'B10' + # auto | letter | legal | ledger | A0 | A1 | A2 | + # A3 | A4 | A5 | A6 | A7 | A8 | A9 | A10 | + # B0 | B1 | B2 | B3 | B4 | B5 | B6 | B7 | B8 + # | B9 | B10 papersize = 'letter' # a value of type 'bool' useafm = False @@ -216,7 +216,7 @@ # 'Accent' or 'Accent_r' or 'Blues' or 'Blues_r' or 'BrBG' or 'BrBG_r' or # 'BuGn' or 'BuGn_r' or 'BuPu' or 'BuPu_r' or 'Dark2' or 'Dark2_r' or # 'GnBu' or 'GnBu_r' or 'Greens' or 'Greens_r' or 'Greys' or 'Greys_r' or - # <...snipped 16 lines...> + # <...snipped 16 lines...> # 'pink_r' or 'prism' or 'prism_r' or 'spectral' or 'spectral_r' or # 'spring' or 'spring_r' or 'summer' or 'summer_r' or 'winter' or # 'winter_r' @@ -404,4 +404,4 @@ # a value of type 'float' pad = 4.0 # a value of type 'float' - size = 2.0 \ No newline at end of file + size = 2.0 Copied: branches/transforms/unit/mlab_unit.py (from rev 4755, trunk/matplotlib/unit/mlab_unit.py) =================================================================== --- branches/transforms/unit/mlab_unit.py (rev 0) +++ branches/transforms/unit/mlab_unit.py 2007-12-17 15:43:03 UTC (rev 4758) @@ -0,0 +1,59 @@ +import datetime, StringIO, unittest +import matplotlib.mlab as mlab +import numpy + +class TestMlab(unittest.TestCase): + def test_csv2rec_closefile(self): + # If passed a file-like object, rec2csv should not close it. + ra=numpy.rec.array([(123, 1197346475.0137341), (456, 123.456)], + dtype=[('a', '<i8'), ('b', '<f8')]) + fh = StringIO.StringIO() + mlab.rec2csv( ra, fh ) + self.failIf( fh.closed ) + + def test_csv2rec_roundtrip(self): + + # Make sure double-precision floats and strings pass through a + # roundtrip unaltered. + + # A bug in numpy (fixed in r4602) meant that numpy scalars + # lost precision when passing through repr(). csv2rec was + # affected by this. This test will only pass on numpy >= + # 1.0.5. + delta = datetime.timedelta(days=1) + date0 = datetime.date(2007,12,16) + date1 = date0 + delta + date2 = date1 + delta + + delta = datetime.timedelta(days=1) + datetime0 = datetime.datetime(2007,12,16,22,29,34,924122) + datetime1 = datetime0 + delta + datetime2 = datetime1 + delta + ra=numpy.rec.fromrecords([ + (123, date0, datetime0, 1197346475.0137341, 'a,bc'), + (456, date1, datetime1, 123.456, 'd\'ef'), + (789, date2, datetime2, 0.000000001, 'ghi'), + ], + names='intdata,datedata,datetimedata,floatdata,stringdata') + + fh = StringIO.StringIO() + mlab.rec2csv( ra, fh ) + fh.seek(0) + if 0: + print 'CSV contents:','-'*40 + print fh.read() + print '-'*40 + fh.seek(0) + ra2 = mlab.csv2rec(fh) + fh.close() + #print 'ra', ra + #print 'ra2', ra2 + for name in ra.dtype.names: + if 0: + print name, repr(ra[name]), repr(ra2[name]) + dt = ra.dtype[name] + print 'repr(dt.type)',repr(dt.type) + self.failUnless( numpy.all(ra[name] == ra2[name]) ) # should not fail with numpy 1.0.5 + +if __name__=='__main__': + unittest.main() This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |