Beispiel #1
0
def rec2csv(rec_array, csv_file, formatd=None, **kwargs):
    """
    Convenience wrapper function on top of mlab.rec2csv to allow fixed-
    precision output to CSV files

    Parameters
    ----------
    rec_aray : numpy 1-d recarray
        The recarray to be written out

    csv_file : str
        CSV file name

    kwargs : dict
        Keyword arguments to pass through to mlab.rec2csv

    Returns
    -------
    None
    """

    # Get the formatd objects associated with each field
    formatd = mlab.get_formatd(rec_array, formatd)

    # For all FormatFloat objects, switch to FormatDecimal objects
    for (k, v) in formatd.iteritems():
        if isinstance(v, mlab.FormatFloat):
            formatd[k] = FormatDecimal()

    # Pass this specification to mlab.rec2csv
    mlab.rec2csv(rec_array, csv_file, formatd=formatd, **kwargs)
"""
generate an editable gtk treeview widget for record arrays with custom
formatting of the cells and show how to limit string entries to a list
of strings
"""
import gtk
import numpy as np
import matplotlib.mlab as mlab
import mpl_toolkits.gtktools as gtktools

r = mlab.csv2rec('data/demodata.csv', converterd={'weekdays': str})

formatd = mlab.get_formatd(r)
formatd['date'] = mlab.FormatDate('%Y-%m-%d')
formatd['prices'] = mlab.FormatMillions(precision=1)
formatd['gain'] = mlab.FormatPercent(precision=2)

# use a drop down combo for weekdays
stringd = dict(weekdays=['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'])
constant = ['clientid']  # block editing of this field

liststore = gtktools.RecListStore(r, formatd=formatd, stringd=stringd)
treeview = gtktools.RecTreeView(liststore, constant=constant)


def mycallback(liststore, rownum, colname, oldval, newval):
    print 'verify: old=%s, new=%s, rec=%s' % (oldval, newval,
                                              liststore.r[rownum][colname])


liststore.callbacks.connect('cell_changed', mycallback)
Beispiel #3
0
    def __init__(self, r, formatd=None, stringd=None):
        """
        r is a numpy record array

        formatd is a dict mapping dtype name to mlab.FormatObj instances

        stringd, if not None, is a dict mapping dtype names to a list of
        valid strings for a combo drop down editor
        """

        if stringd is None:
            stringd = dict()

        if formatd is None:
            formatd = mlab.get_formatd(r)

        self.stringd = stringd
        self.callbacks = cbook.CallbackRegistry(['cell_changed'])

        self.r = r

        self.headers = r.dtype.names
        self.formats = [
            gtkformat_factory(formatd.get(name, mlab.FormatObj()), i)
            for i, name in enumerate(self.headers)
        ]

        # use the gtk attached versions
        self.formatd = formatd = dict(zip(self.headers, self.formats))
        types = []
        for format in self.formats:
            if isinstance(format, mlab.FormatBool):
                types.append(gobject.TYPE_BOOLEAN)
            else:
                types.append(gobject.TYPE_STRING)

        self.combod = dict()
        if len(stringd):
            types.extend([gobject.TYPE_INT] * len(stringd))

            keys = stringd.keys()
            keys.sort()

            valid = set(r.dtype.names)
            for ikey, key in enumerate(keys):
                assert (key in valid)
                combostore = gtk.ListStore(gobject.TYPE_STRING)
                for s in stringd[key]:
                    combostore.append([s])
                self.combod[key] = combostore, len(self.headers) + ikey

        gtk.ListStore.__init__(self, *types)

        for row in r:
            vals = []
            for formatter, val in zip(self.formats, row):
                if isinstance(formatter, mlab.FormatBool):
                    vals.append(val)
                else:
                    vals.append(formatter.tostr(val))
            if len(stringd):
                # todo, get correct index here?
                vals.extend([0] * len(stringd))
            self.append(vals)
def rec2csv_pretty(r,
                   fname,
                   delimiter=',',
                   formatd=None,
                   missing='',
                   missingd=None,
                   withheader=True,
                   overwrite_float_precision=True):
    """
    Save the data from numpy recarray *r* into a
    comma-/space-/tab-delimited file.  The record array dtype names
    will be used for column headers.

    *fname*: can be a filename or a file handle.  Support for gzipped
      files is automatic, if the filename ends in '.gz'

    *withheader*: if withheader is False, do not write the attribute
      names in the first row

    *overwrite_float_precision*: if True, write out floats as raw, else
      use the implied precision of formatd
      
    .. seealso::

        :func:`csv2rec`
            For information about *missing* and *missingd*, which can
            be used to fill in masked values into your CSV file.
    """

    if missingd is None:
        missingd = dict()

    def with_mask(func):
        def newfunc(val, mask, mval):
            if mask:
                return mval
            else:
                return func(val)

        return newfunc

    formatd = get_formatd(r, formatd)
    funcs = []
    for i, name in enumerate(r.dtype.names):
        if overwrite_float_precision:
            funcs.append(with_mask(csvformat_factory(formatd[name]).tostr))
        else:
            funcs.append(with_mask(formatd[name].tostr))
    fh, opened = cbook.to_filehandle(fname, 'wb', return_opened=True)
    writer = csv.writer(fh, delimiter=delimiter)
    header = r.dtype.names
    if withheader:
        writer.writerow(header)

    # Our list of specials for missing values
    mvals = []
    for name in header:
        mvals.append(missingd.get(name, missing))

    ismasked = False
    if len(r):
        row = r[0]
        ismasked = hasattr(row, '_fieldmask')

    for row in r:
        if ismasked:
            row, rowmask = row.item(), row._fieldmask.item()
        else:
            rowmask = [False] * len(row)
        srow = [
            func(val, mask, mval)
            for func, val, mask, mval in zip(funcs, row, rowmask, mvals)
        ]
        writer.writerow(srow)
    if opened:
        fh.close()