Last active
December 12, 2015 08:09
-
-
Save taldcroft/4742168 to your computer and use it in GitHub Desktop.
Example of writing a Table object to an ASCII file and reading it back with no loss of metadata
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "metadata": { | |
| "name": "test_asciitable_format" | |
| }, | |
| "nbformat": 3, | |
| "nbformat_minor": 0, | |
| "worksheets": [ | |
| { | |
| "cells": [ | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "import astropy.io.ascii as ascii\n", | |
| "from astropy.table import Table, Column" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 1 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "# Make a table with some non-generic data types and meta values\n", | |
| "a = Column(name='a', data=[1, 2], dtype='uint8', units='m s**2', format='%04d', description='Column a')\n", | |
| "b = Column(name='b', data=[3, 4], dtype='float16', units='kg', format='%.3f', description='Column b')\n", | |
| "t = Table([a, b])\n", | |
| "t.meta['meta1'] = ['a', 1, {'c': 2}]\n", | |
| "t.meta['meta2'] = 'meta2'\n", | |
| "t.meta['meta3'] = 'meta3'\n", | |
| "t['a'].meta['a'] = ['hello', 'world']\n", | |
| "t['a'].units = 'm s**2'" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 2 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "from StringIO import StringIO\n", | |
| "out = StringIO()\n", | |
| "ascii.write(t, out, Writer=ascii.AsciiTable, delimiter=',')" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 3 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "# Here is the output of write() in the new \"AsciiTable\" format\n", | |
| "# The table metadata are encoded in pretty-printed JSON.\n", | |
| "# The first and last lines are just the column names, which makes\n", | |
| "# it easy for simple CSV-style readers to parse this output, assuming\n", | |
| "# they use either the first or last commented line to find the column\n", | |
| "# names.\n", | |
| "print out.getvalue()" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "# a,b\n", | |
| "# \n", | |
| "# -*- ASCIITABLE-FORMAT-HEADER-JSON-START -*-\n", | |
| "# {\n", | |
| "# \"table_meta\": {\n", | |
| "# \"meta1\": [\n", | |
| "# \"a\",\n", | |
| "# 1,\n", | |
| "# {\n", | |
| "# \"c\": 2\n", | |
| "# }\n", | |
| "# ],\n", | |
| "# \"meta2\": \"meta2\",\n", | |
| "# \"meta3\": \"meta3\"\n", | |
| "# },\n", | |
| "# \"cols\": [\n", | |
| "# {\n", | |
| "# \"name\": \"a\",\n", | |
| "# \"units\": \"m s2\",\n", | |
| "# \"format\": \"%04d\",\n", | |
| "# \"description\": \"Column a\",\n", | |
| "# \"dtype\": \"uint8\",\n", | |
| "# \"meta\": {\n", | |
| "# \"a\": [\n", | |
| "# \"hello\",\n", | |
| "# \"world\"\n", | |
| "# ]\n", | |
| "# }\n", | |
| "# },\n", | |
| "# {\n", | |
| "# \"name\": \"b\",\n", | |
| "# \"units\": \"kg\",\n", | |
| "# \"format\": \"%.3f\",\n", | |
| "# \"description\": \"Column b\",\n", | |
| "# \"dtype\": \"float16\",\n", | |
| "# \"meta\": {}\n", | |
| "# }\n", | |
| "# ]\n", | |
| "# }\n", | |
| "# -*- ASCIITABLE-FORMAT-HEADER-JSON-STOP -*-\n", | |
| "# \n", | |
| "# a,b\n", | |
| "0001,3.000\n", | |
| "0002,4.000\n", | |
| "\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 4 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "# Now read the output of the previous write()\n", | |
| "# \n", | |
| "t = ascii.read(out.getvalue(), Reader=ascii.AsciiTable)" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 5 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "# Column units\n", | |
| "print type(t['a'].units)\n", | |
| "t['a'].units" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "<class 'astropy.units.core.CompositeUnit'>\n" | |
| ] | |
| }, | |
| { | |
| "latex": [ | |
| "$\\mathrm{s^{2}\\ m}$" | |
| ], | |
| "output_type": "pyout", | |
| "prompt_number": 6, | |
| "text": [ | |
| "Unit(\"m s2\")" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 6 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "# Column meta data\n", | |
| "t['a'].meta" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "pyout", | |
| "prompt_number": 7, | |
| "text": [ | |
| "OrderedDict([('a', ['hello', 'world'])])" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 7 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "# Table metadata preserved as an OrderedDict (with correct order)\n", | |
| "# This probably needs some work, it depends on the json.loads()\n", | |
| "# object_pairs_hook option, which is only in python 2.7+.\n", | |
| "# \n", | |
| "# There is also a hack because JSON turns everything into unicode,\n", | |
| "# which messes up Numpy. So right now there is a simple recursive\n", | |
| "# routine to encode everything as UTF-8. This needs work.\n", | |
| "t.meta" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "pyout", | |
| "prompt_number": 8, | |
| "text": [ | |
| "OrderedDict([('meta1', ['a', 1, OrderedDict([('c', 2)])]), ('meta2', 'meta2'), ('meta3', 'meta3')])" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 8 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "t # table formatting preserved" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "html": [ | |
| "<table><tr><th>a</th><th>b</th></tr><tr><td>0001</td><td>3.000</td></tr><tr><td>0002</td><td>4.000</td></tr></table>" | |
| ], | |
| "output_type": "pyout", | |
| "prompt_number": 9, | |
| "text": [ | |
| "<Table rows=2 names=('a','b')>\n", | |
| "array([(1, 3.0), (2, 4.0)], \n", | |
| " dtype=[('a', '|u1'), ('b', '<f2')])" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 9 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "# Dtypes preserved\n", | |
| "print t['a'].dtype\n", | |
| "print t['b'].dtype" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "uint8\n", | |
| "float16\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 10 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 10 | |
| } | |
| ], | |
| "metadata": {} | |
| } | |
| ] | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment