{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "adjustable-greece", "slideshow": { "slide_type": "slide" } }, "source": [ "Astronomical data analysis using Python\n", "=======\n", "\n", "Lecture 8\n", "-----------------\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Python modules for numerical computing and visualisation\n", "===\n", "\n", "* numpy + scipy\n", "* matplotlib\n", "* gnuplot.py, also sm and PGPLOT have interfaces\n", "* Mathlink - interface to Mathematica\n", "* sympy - Python module for symbolic computation\n", "* MaYaVI - 3D visualisation using VTK\n", "* pymat - Python Matlab interface\n", "* pytave - Python GNU Octave interface\n", "* Rpy - Python R interface\n", "* SageMath: alternative to Magma, Maple, Mathematica, Matlab" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "SciPy - useful stuff that would bloat numpy http://scipy.org\n", "===\n", "\n", "* constants: physical constants and conversion factors\n", "* cluster: hierarchical clustering, vector quantization, K-means\n", "* fftpack: Discrete Fourier Transform algorithms\n", "* integrate: numerical integration and ODE solvers\n", "* **interpolate**: interpolation and smoothing splines\n", "* io: data input and output\n", "* lib: Python wrappers to external libraries\n", "* linalg: linear algebra routines\n", "* **ndimage**: N dimensional image processing\n", "* odr: Orthogonal distance regression\n", "* **optimize**: optimization algorithms including linear programming\n", "* signal: signal processing tools" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# SciPy\n", "\n", "* sparse: sparse matrix and related algorithms\n", "* spatial: KD-trees, nearest neighbors, distance functions\n", "* special: special functions\n", "* **stats**: statistical distributions and functions\n", "\n", "Via RPy, SciPy can interface to the statistical package R." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Curve fitting - scipy.optimize.curve_fit\n", "===\n", "\n", "The algorithm uses the Levenberg-Marquardt algorithm through 'leastsq'. Additional keyword arguments are passed directly to that algorithm. Quick and easy way to fit an arbitary function to some x,y data.\n", "\n", "If you quickly want to fit a function to some datapoints, use this function.\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Minimization - scipy.optimize.minimize\n", "===\n", "\n", "Many different solvers available - Nelder-Mead, Powell, simulated annealing etc." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Scipy and Numpy documentation\n", "===\n", "\n", "User Guides, Reference Guides and Developer guides are available at: http://docs.scipy.org" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "Also check the SciPy Cookbook\n", "\n", "https://scipy-cookbook.readthedocs.io/\n", "\n", "This site hosts worked examples of common tasks. Some are\n", "introductory in nature, while others are quite advanced." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Some advanced books on Python\n", "\n", "* **Numerical Python** by Robert Johansson\n", "* **Programming for Computations - Python** by Linge and Langtangen \n", "* **Fluent Python** by Luciano Ramalho \n", "* **Numerical Python in Astronomy and Astrophyics** by Schmidt and Volschow\n", "* **Python for data analysis** by William McKinney\n", "\n", "As always, remember to get the most recent edition of these books." ] }, { "cell_type": "markdown", "metadata": { "id": "mathematical-carter", "slideshow": { "slide_type": "slide" } }, "source": [ "A Few Years Ago ... a messy situation existed\n", "====================\n", "\n", "Several independent developers were developing specific tools for Python...\n", "\n", "* PyFITS - enable use of Python to handle FITS files.\n", "* PyWCS - enable World Coordinate System transformations\n", "* atpy / asciitable - handle tables of all kinds \n", "* Some cosmological calculators.\n", "* Some coordinate transformation tools.\n", "* and more...\n", "\n", "The problem: Different styles, repeated efforts, no coordination.\n", "\n", "The solution: **Astropy** - unite all efforts under one banner!" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Astropy www.astropy.org\n", "\n", "\n", "https://www.astropy.org/astropy-tutorials/ - excellent resources to learn astropy.\n", "\n", "**Version 5 of astropy was released last week!**\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Astropy core, coordinated and affiliated packages\n", "\n", "* **Core package**\n", "* **Coordinated packages** -- e.g. astropy core, astroquery, photutils, specutils, ccdproc Total: 8 packages.\n", "* **Affiliated packages** - aplpy, synphot, astroML, galpy etc. Depending on your interests, you will need to use one or more of these. Total: 44 packages\n", "\n", "The astronomy community across the world has realised the importance of this effort, and many developers are now funded to add functionality to the astropy ecosystem in many countries." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# What `astropy` contains\n", "\n", "\n", "## Data structures and transformations\n", "\n", "* Constants (`astropy.constants`)\n", "* Units and Quantities (`astropy.units`)\n", "* N-Dimensional Datasets (`astropy.nddata`)\n", "* Data Tables (`astropy.table`)\n", "* Time and Dates (`astropy.time`)\n", "* Time Series (`astropy.timeseries`)\n", "* Astronomical Coordinate Systems (`astropy.coordinates`)\n", "* World Coordinate System (`astropy.wcs`)\n", "* Models and Fitting (`astropy.modeling`)\n", "* Uncertainties and Distributions (`astropy.uncertainty`)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# What `astropy` contains\n", "\n", "## Files, I/O, and Communication\n", "\n", "* Unified File Read/Write Interface\n", "* FITS File Handling (`astropy.io.fits`)\n", "* ASCII Tables (`astropy.io.ascii` `astropy.table`)\n", "* VOTable XML Handling (`astropy.io.votable`)\n", "* Miscellaneous: HDF5, YAML, ASDF, Parquet, pickle (`astropy.io.misc`)\n", "* SAMP (Simple Application Messaging Protocol) (`astropy.samp`)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# What `astropy` contains\n", "\n", "## Computations and utilities\n", "\n", "* Cosmological Calculations (`astropy.cosmology`)\n", "* Convolution and Filtering (`astropy.convolution`)\n", "* Data Visualization (`astropy.visualization`)\n", "* Astrostatistics Tools (`astropy.stats`)\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "polyphonic-buddy", "slideshow": { "slide_type": "slide" } }, "source": [ "What will we cover in astropy?\n", "===================\n", "\n", "* Table management features.\n", "* Handling FITS files, including datacubes.\n", "* WCS operations.\n", "* Cosmological Calculations." ] }, { "cell_type": "markdown", "metadata": { "id": "positive-christmas", "slideshow": { "slide_type": "slide" } }, "source": [ "Table Management in Python\n", "=========\n", "\n", " \n", "is done via the \"table\" sub-module inside Astropy." ] }, { "cell_type": "markdown", "metadata": { "id": "buried-transfer", "slideshow": { "slide_type": "slide" } }, "source": [ "astropy.io.ascii vs. astropy.table\n", "-----\n", "\n", "* astropy.io.ascii is meant purely for reading and writing tables.\n", "* Is a collection of \"extensible\" classes which can be extended to support newer formats.\n", "\n", "astropy.table \n", "\n", "* builds upon io.ascii using its functionality for reading / writing tables\n", "* and adding its own powerful table operations.\n", "\n", "You won't need to read much about io.ascii unless your tables have some special outstanding features." ] }, { "cell_type": "markdown", "metadata": { "id": "amazing-police", "slideshow": { "slide_type": "slide" } }, "source": [ "In Brief - The \"Class\" Concept\n", "-------\n", "\n", "We have discussed the concept of an \"object\" earlier. \n", "\n", "* Objects have well defined behavior.\n", "* They have methods which help you perform supported operations on them.\n", "* Where are all these rules defined?\n", "\n", "A \"class\" is crudely put, a definition which allows one to create objects. \n", "\n", "To create table objects, we will need a Table class.\n" ] }, { "cell_type": "markdown", "metadata": { "id": "assured-barbados", "slideshow": { "slide_type": "slide" } }, "source": [ "Let's Start\n", "-----" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "id": "exposed-migration", "outputId": "98becde8-33ef-4896-dc28-d3fdee4253a0", "slideshow": { "slide_type": "-" } }, "outputs": [], "source": [ "# First we need the Table class to create table objects.\n", "# The warning that will be flashed has so far not affected\n", "# any functional features of Table class\n", "from astropy.table import Table" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "id": "sorted-universal", "slideshow": { "slide_type": "-" } }, "outputs": [], "source": [ "# Next we need to create the Table object using a file.\n", "demo_table = Table.read(\"demo.txt\", format = \"ascii\")" ] }, { "cell_type": "markdown", "metadata": { "id": "aboriginal-archives", "slideshow": { "slide_type": "slide" } }, "source": [ "What if the table does not load?\n", "-------\n", "\n", "If you get errors when using read() method, it means that your file is formatted in a way that the standard parser is unable to understand the structure of your file. \n", "\n", "What to do? Understand the io.ascii.read() method in detail and supply the various options to Table.read(). \n", "\n", "eg. header_start = \";\" or delimiter=\"|\" , etc." ] }, { "cell_type": "markdown", "metadata": { "id": "heavy-pleasure", "slideshow": { "slide_type": "slide" } }, "source": [ "Displaying Tables.\n", "----" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "id": "involved-religion", "outputId": "8d3338ce-6c43-40ca-ed66-c2478e766b30", "slideshow": { "slide_type": "-" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "name obs_date mag_b mag_v\n", "---- ---------- ----- -----\n", " M31 2012-01-02 17.0 17.5\n", " M31 2012-01-02 17.1 17.4\n", "M101 2012-01-02 15.1 13.5\n", " M82 2012-02-14 16.2 14.5\n", " M31 2012-02-14 16.9 17.3\n", " M82 2012-02-14 15.2 15.5\n", "M101 2012-02-14 15.0 13.6\n", " M82 2012-03-26 15.7 16.5\n", "M101 2012-03-26 15.1 13.5\n", "M101 2012-03-26 14.8 14.3\n" ] } ], "source": [ "print (demo_table)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "id": "joint-stomach", "outputId": "a6ccd455-f7bb-4df1-fc79-14bb85901584", "slideshow": { "slide_type": "slide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "name obs_date mag_b mag_v\n", "---- ---------- ----- -----\n", " M31 2012-01-02 17.0 17.5\n", " M31 2012-01-02 17.1 17.4\n", "M101 2012-01-02 15.1 13.5\n", " M82 2012-02-14 16.2 14.5\n", " M31 2012-02-14 16.9 17.3\n", " M82 2012-02-14 15.2 15.5\n", "M101 2012-02-14 15.0 13.6\n", " M82 2012-03-26 15.7 16.5\n", "M101 2012-03-26 15.1 13.5\n", "M101 2012-03-26 14.8 14.3\n" ] } ], "source": [ "demo_table.pprint() # Does exactly the same thing.\n", "# but you can supply options such as \n", "# max_lines, max_width, show_unit, show_name" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "id": "crazy-meaning", "outputId": "3a2d68b8-8c4d-4394-e67c-3c055072c908", "slideshow": { "slide_type": "slide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " M31 2012-01-02 17.0 17.5\n", " M31 2012-01-02 17.1 17.4\n", "M101 2012-01-02 15.1 13.5\n", " M82 2012-02-14 16.2 14.5\n", " M31 2012-02-14 16.9 17.3\n", " M82 2012-02-14 15.2 15.5\n", "M101 2012-02-14 15.0 13.6\n", " M82 2012-03-26 15.7 16.5\n", "M101 2012-03-26 15.1 13.5\n", "M101 2012-03-26 14.8 14.3\n" ] } ], "source": [ "# In this example, we are suppressing column names from appearing.\n", "demo_table.pprint(show_name=False) " ] }, { "cell_type": "markdown", "metadata": { "id": "coastal-looking", "slideshow": { "slide_type": "slide" } }, "source": [ "More Ways to Print Tables.\n", "----\n", "\n", "Using an interactive table scrolling tool.\n", "\n", " demo_table.more()\n", " \n", "Or display it as a formatted table in a browser.\n", "\n", " demo_table.show_in_browser()" ] }, { "cell_type": "markdown", "metadata": { "id": "broke-secondary", "slideshow": { "slide_type": "slide" } }, "source": [ "Quickly Check Basic Properties of Loaded Table\n", "----" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "id": "wound-fabric", "outputId": "07bd453e-7439-4cb4-f079-580df9513d9b", "slideshow": { "slide_type": "-" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10\n" ] } ], "source": [ "print (len(demo_table)) # Number of rows." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "id": "future-journey", "outputId": "c1062352-78d3-46ce-cb23-eb8bc156fe8d", "scrolled": true, "slideshow": { "slide_type": "-" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['name', 'obs_date', 'mag_b', 'mag_v']\n" ] } ], "source": [ "print (demo_table.colnames) # The names of the columns." ] }, { "cell_type": "markdown", "metadata": { "id": "overall-parker", "slideshow": { "slide_type": "-" } }, "source": [ "You can also print any meta information, if available.\n", "\n", " demo_table.meta" ] }, { "cell_type": "markdown", "metadata": { "id": "steady-merit", "slideshow": { "slide_type": "slide" } }, "source": [ "Accessing Columns of the Table\n", "-----" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "id": "unexpected-server", "outputId": "99231050-517b-414c-f6cb-758c41014295", "scrolled": true, "slideshow": { "slide_type": "-" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "name\n", "----\n", " M31\n", " M31\n", "M101\n", " M82\n", " M31\n", " M82\n", "M101\n", " M82\n", "M101\n", "M101\n" ] } ], "source": [ "print (demo_table[\"name\"]) # one column" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "id": "present-bibliography", "outputId": "71369ac9-e2ad-407f-c6c0-34bc43542d17", "slideshow": { "slide_type": "slide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "name mag_b\n", "---- -----\n", " M31 17.0\n", " M31 17.1\n", "M101 15.1\n", " M82 16.2\n", " M31 16.9\n", " M82 15.2\n", "M101 15.0\n", " M82 15.7\n", "M101 15.1\n", "M101 14.8\n" ] } ], "source": [ "print (demo_table[\"name\", \"mag_b\"]) # more than one column" ] }, { "cell_type": "markdown", "metadata": { "id": "indoor-fetish", "slideshow": { "slide_type": "slide" } }, "source": [ "Accessing Rows in a Table\n", "----" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "id": "returning-stretch", "outputId": "94d0628a-83b3-4d86-a39d-0c55548f3ab2", "slideshow": { "slide_type": "-" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "name obs_date mag_b mag_v\n", "---- ---------- ----- -----\n", " M31 2012-01-02 17.0 17.5\n" ] } ], "source": [ "print (demo_table[0])" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "id": "worth-partner", "outputId": "5baad530-fd48-4832-f721-f9921b4e4a2d", "slideshow": { "slide_type": "-" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " M31 2012-01-02 17.0 17.5\n" ] } ], "source": [ "lines = demo_table.pformat() # a list of strings, each string a row, includes header.\n", "print (lines[2])" ] }, { "cell_type": "markdown", "metadata": { "id": "alternate-lesson", "slideshow": { "slide_type": "slide" } }, "source": [ "Individual Element Access\n", "-----" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "id": "respective-china", "outputId": "1b38d400-4a5e-4cdb-e649-758e3fd9fd32", "slideshow": { "slide_type": "-" } }, "outputs": [ { "data": { "text/plain": [ "'M31'" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "demo_table[\"name\"][0] " ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "id": "intimate-silly", "outputId": "9ac08eac-1193-480a-cb74-6c78effd913a", "slideshow": { "slide_type": "-" } }, "outputs": [ { "data": { "text/plain": [ "'M31'" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "demo_table[0][\"name\"] # also works the same as above." ] }, { "cell_type": "markdown", "metadata": { "id": "olympic-timeline", "slideshow": { "slide_type": "slide" } }, "source": [ "Sub-sectioning Tables\n", "---" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "id": "foreign-negotiation", "slideshow": { "slide_type": "-" } }, "outputs": [], "source": [ "subsection_col = demo_table[\"name\",\"mag_b\"] # by column." ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "id": "voluntary-laptop", "slideshow": { "slide_type": "-" } }, "outputs": [], "source": [ "subsection_row = demo_table[2:5] # by rows." ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "id": "rolled-console", "slideshow": { "slide_type": "-" } }, "outputs": [], "source": [ "subsection_row2 = demo_table[ [1,5,3] ]" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "id": "bizarre-depression", "slideshow": { "slide_type": "-" } }, "outputs": [], "source": [ "subsection_both = demo_table[\"name\", \"mag_b\"] [1:5]" ] }, { "cell_type": "markdown", "metadata": { "id": "covered-presence", "slideshow": { "slide_type": "slide" } }, "source": [ "Changing elements inside a Table\n", "----\n", "\n", "* You know how to access columns, rows and individual elements.\n", "* Using = sign, you can assign the selected col, row or element another value.\n", "\n", "So, \n", "\n", " demo_table[\"name\"] = ... list of 10 names\n", " demo_table[\"name\"] = \"SingleName\"\n", " \n", "will both work." ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "id": "experienced-athens", "outputId": "b186424f-5ec1-4c37-b54b-48a3b31defdf", "slideshow": { "slide_type": "slide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "name obs_date mag_b mag_v\n", "---- ---------- ----- -----\n", " M31 2012-01-02 17.0 17.5\n", " M31 2012-01-02 17.1 17.4\n", "M101 2012-01-02 15.1 13.5\n", " M82 2012-02-14 16.2 14.5\n", " M31 2012-02-14 16.9 17.3\n", " M82 2012-02-14 15.2 15.5\n", "M101 2012-02-14 15.0 13.6\n", " M82 2012-03-26 15.7 16.5\n", "M101 2012-03-26 15.1 13.5\n", "M101 2012-03-26 14.8 14.3\n" ] } ], "source": [ "print (demo_table)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "id": "brave-discipline", "outputId": "a8ef0932-b85f-49b1-c489-c695dccf9a59", "slideshow": { "slide_type": "slide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "name obs_date mag_b mag_v\n", "---- ---------- ----- -----\n", " X 2012-01-02 17.0 17.5\n", " X 2012-01-02 17.1 17.4\n", " X 2012-01-02 15.1 13.5\n", " X 2012-02-14 16.2 14.5\n", " X 2012-02-14 16.9 17.3\n", " X 2012-02-14 15.2 15.5\n", " X 2012-02-14 15.0 13.6\n", " X 2012-03-26 15.7 16.5\n", " X 2012-03-26 15.1 13.5\n", " X 2012-03-26 14.8 14.3\n" ] } ], "source": [ "demo_table[\"name\"] = \"X\"\n", "print (demo_table)" ] }, { "cell_type": "markdown", "metadata": { "id": "southern-paintball", "slideshow": { "slide_type": "slide" } }, "source": [ "Adding New Columns\n", "----" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "id": "sonic-ethernet", "outputId": "18dd6007-c751-4a30-9201-8a976268a556", "slideshow": { "slide_type": "-" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "name obs_date mag_b mag_v NewColumn\n", "---- ---------- ----- ----- ---------\n", " X 2012-01-02 17.0 17.5 0\n", " X 2012-01-02 17.1 17.4 1\n", " X 2012-01-02 15.1 13.5 2\n", " X 2012-02-14 16.2 14.5 3\n", " X 2012-02-14 16.9 17.3 4\n", " X 2012-02-14 15.2 15.5 5\n", " X 2012-02-14 15.0 13.6 6\n", " X 2012-03-26 15.7 16.5 7\n", " X 2012-03-26 15.1 13.5 8\n", " X 2012-03-26 14.8 14.3 9\n" ] } ], "source": [ "# Method 1\n", "demo_table[\"NewColumn\"] = range(len(demo_table))\n", "print (demo_table)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "id": "alone-friendly", "outputId": "0325a59e-3351-4cb7-b697-5f16f75151cc", "slideshow": { "slide_type": "slide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2ndNewColN name obs_date mag_b mag_v NewColumn\n", "---------- ---- ---------- ----- ----- ---------\n", " 0 X 2012-01-02 17.0 17.5 0\n", " 1 X 2012-01-02 17.1 17.4 1\n", " 2 X 2012-01-02 15.1 13.5 2\n", " 3 X 2012-02-14 16.2 14.5 3\n", " 4 X 2012-02-14 16.9 17.3 4\n", " 5 X 2012-02-14 15.2 15.5 5\n", " 6 X 2012-02-14 15.0 13.6 6\n", " 7 X 2012-03-26 15.7 16.5 7\n", " 8 X 2012-03-26 15.1 13.5 8\n", " 9 X 2012-03-26 14.8 14.3 9\n" ] } ], "source": [ "# Method 2, using Column Object\n", "from astropy.table import Column\n", "newcol = Column( data = range(len(demo_table)), name = \"2ndNewColN\")\n", "demo_table.add_column(newcol, index = 0)\n", "print (demo_table)" ] }, { "cell_type": "markdown", "metadata": { "id": "given-indie", "slideshow": { "slide_type": "slide" } }, "source": [ "Removing Columns\n", "---" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "id": "cathedral-territory", "outputId": "e72dcb6d-ce7b-4646-b70b-2270eab8397d", "slideshow": { "slide_type": "-" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "name obs_date mag_b mag_v\n", "---- ---------- ----- -----\n", " X 2012-01-02 17.0 17.5\n", " X 2012-01-02 17.1 17.4\n", " X 2012-01-02 15.1 13.5\n", " X 2012-02-14 16.2 14.5\n", " X 2012-02-14 16.9 17.3\n", " X 2012-02-14 15.2 15.5\n", " X 2012-02-14 15.0 13.6\n", " X 2012-03-26 15.7 16.5\n", " X 2012-03-26 15.1 13.5\n", " X 2012-03-26 14.8 14.3\n" ] } ], "source": [ "demo_table.remove_columns([\"NewColumn\",\"2ndNewColN\"])\n", "print (demo_table)" ] }, { "cell_type": "markdown", "metadata": { "id": "dutch-quest", "slideshow": { "slide_type": "slide" } }, "source": [ "For Rows\n", "----\n", "\n", "Similar functions exist. Please read documentation for details. Or explore using iPython.\n", "\n", " demo_table.remove_row(5)\n", " demo_table.remove_rows( [5,6])\n", " demo_table.remove_rows( slice(3,6) )" ] }, { "cell_type": "markdown", "metadata": { "id": "baking-medicine", "slideshow": { "slide_type": "slide" } }, "source": [ "Table Sorting\n", "-----" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "id": "generic-blogger", "outputId": "c19862bf-a3f9-4844-f6cb-e6b80d1ded17", "slideshow": { "slide_type": "-" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "name obs_date mag_b mag_v\n", "---- ---------- ----- -----\n", " M31 2012-01-02 17.0 17.5\n", " M31 2012-01-02 17.1 17.4\n", "M101 2012-01-02 15.1 13.5\n", " M82 2012-02-14 16.2 14.5\n", " M31 2012-02-14 16.9 17.3\n", " M82 2012-02-14 15.2 15.5\n", "M101 2012-02-14 15.0 13.6\n", " M82 2012-03-26 15.7 16.5\n", "M101 2012-03-26 15.1 13.5\n", "M101 2012-03-26 14.8 14.3\n" ] } ], "source": [ "demo_table = Table.read(\"demo.txt\", format=\"ascii\")\n", "print (demo_table)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "id": "thrown-shock", "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "demo_table.sort([\"name\", \"mag_b\"]) # sort by name, then magb" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "id": "through-wheel", "outputId": "b334c9e2-74ed-456e-e19b-5adca1b316c9", "slideshow": { "slide_type": "-" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "name obs_date mag_b mag_v\n", "---- ---------- ----- -----\n", "M101 2012-03-26 14.8 14.3\n", "M101 2012-02-14 15.0 13.6\n", "M101 2012-01-02 15.1 13.5\n", "M101 2012-03-26 15.1 13.5\n", " M31 2012-02-14 16.9 17.3\n", " M31 2012-01-02 17.0 17.5\n", " M31 2012-01-02 17.1 17.4\n", " M82 2012-02-14 15.2 15.5\n", " M82 2012-03-26 15.7 16.5\n", " M82 2012-02-14 16.2 14.5\n" ] } ], "source": [ "print (demo_table)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "id": "compatible-massage", "outputId": "9dc6ee11-0770-470b-8c69-1a713542e64b", "slideshow": { "slide_type": "slide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "name obs_date mag_b mag_v\n", "---- ---------- ----- -----\n", " M82 2012-02-14 16.2 14.5\n", " M82 2012-03-26 15.7 16.5\n", " M82 2012-02-14 15.2 15.5\n", " M31 2012-01-02 17.1 17.4\n", " M31 2012-01-02 17.0 17.5\n", " M31 2012-02-14 16.9 17.3\n", "M101 2012-03-26 15.1 13.5\n", "M101 2012-01-02 15.1 13.5\n", "M101 2012-02-14 15.0 13.6\n", "M101 2012-03-26 14.8 14.3\n" ] } ], "source": [ "demo_table.reverse() # Reverse existing table. Descending order!\n", "print (demo_table)" ] } ], "metadata": { "celltoolbar": "Slideshow", "colab": { "name": "lect8.ipynb", "provenance": [] }, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.7" } }, "nbformat": 4, "nbformat_minor": 1 }