7. Power in Packages JHU Physics & Astronomy Python Workshop 2018

Slides:



Advertisements
Similar presentations
Chapter 3 – Web Design Tables & Page Layout
Advertisements

DOCUMENT TYPES. Digital Documents Converting documents to an electronic format will preserve those documents, but how would such a process be organized?
Python for Science Shane Grigsby. What is python? Why python? Interpreted, object oriented language Free and open source Focus is on readability Fast.
Python: Your new best friend print “Adam Avison”.
Python Crash Course PyFits, Astropy
Accessible PDF Documents with Adobe Acrobat 8 Sean Keegan Web Accessibility Specialist CSU Summer Accessibility Institute 2007.
Sarah Reonomy OSCON 2014 ANALYZING DATA WITH PYTHON.
1 of 7 This document is for informational purposes only. MICROSOFT MAKES NO WARRANTIES, EXPRESS OR IMPLIED, IN THIS DOCUMENT. © 2007 Microsoft Corporation.
XP New Perspectives on Microsoft Access 2002 Tutorial 71 Microsoft Access 2002 Tutorial 7 – Integrating Access With the Web and With Other Programs.
1 Chapter 6 Understanding Computers, 11 th Edition Software Ownership Rights Software license: agreement, either included in a software package or displayed.
IMAGE PROCESSING LIBRARY (PIL) This is a package that you can import into python and it has quite a few methods that you can process image files with.
Datasheets I: Create a table by entering data – You type, Access listens Lesson 18 By the end of this lesson you will be able to complete the following:
Functions and Demo of Astrogrid 1.1 China-VO Haijun Tian.
Pandas: Python Programming for Spreadsheets Pamela Wu Sept. 17 th 2015.
Python Crash Course Aplpy
Introduction to Python By Neil Cook Twitter: njcuk Slides/Notes:
THE LWA SOFTWARE LIBRARY Jayce Dowell – LWA Users’ Meeting – July 27, 2012.
Microsoft ® Office Access ™ 2007 Training Datasheets I: Create a table by entering data ICT Staff Development presents:
1 Basic Perl CGI Programming. 2 Issues How and when your program is invoked. Generating Response –HTTP Headers –HTML (or whatever document type you want)
CS 4720 Dynamic Web Applications CS 4720 – Web & Mobile Systems.
A Powerful Python Library for Data Analysis BY BADRI PRUDHVI BADRI PRUDHVI.
1 EndNote X2 Your Bibliographic Management Tool 29 September 2009 Humanities and Social Sciences Resource Teams.
Scientific Python Numpy Linear Algebra Matrices Scipy Signal Processing Optimization IPython Interactive Console Matplotlib Plotting Sympy Symbolic Math.
Comparison of different output options from Stata
Practical Kinetics Exercise 0: Getting Started Objectives: 1.Install Python and IPython Notebook 2.print “Hello World!”
Matplotlib SANTHOSH Boggarapu.
Practical Kinetics Exercise 2: Integral Rate Laws Objectives: 1.Import Experimental Data (Excel, CSV) 2.Fit to zero-, first-, and second-order rate laws.
Python & NetworkX Youn-Hee Han
June 27-29, DC2 Software Workshop - 1 Tom Stephens GSSC Database Programmer GSSC Data Servers for DC2.
Starting with BMEGUI Prahlad Jat (1) and Marc Serre (1) (1) University of North Carolina at Chapel Hill.
TechKnowlogy Conference August 2, 2011 Using GoogleDocs for Collaboration.
Using Python to Retrieve Data from the CUAHSI HIS Web Services Jeffery S. Horsburgh Hydroinformatics Fall 2015 This work was funded by National Science.
1 SPIE Astronomical Telescopes + Instrumentation | 26 June - 1 July 2016 | Edinburgh, United Kingdom Investigating interoperability of the LSST Data Management.
Advanced HTML Tags:.
PH2150 Scientific Computing Skills
Python for data analysis Prakhar Amlathe Utah State University
Add More Zing to your Dashboards – Creating Zing Plot Gadgets
Microsoft Office 2007-Illustrated
Formatting Output.
Intro To Pete Alonzi University of Virginia Library
IPYTHON AND MATPLOTLIB Python for computational science
Let’s Build a Dashboard (Informer 4 versus Informer 5)
Python in astronomy + Monte-Carlo techniques
Programming the Web using XHTML and JavaScript
Source Catalog Tool This presentation is an introduction to the EVLA SSS Group's Source Catalog Tool (SCT). This presentation is geared toward those users.
Microsoft Access 2003 Illustrated Complete
Central Document Library Quick Reference User Guide View User Guide
Software and Multimedia
CSC 1315! Data Science Lecture 18.
Software and Multimedia
Chapter 4 Application Software
Network Visualization
Data Science with Python Pandas
Preparing your Data using Python
Brief Intro to Python for Statistics
Preparing your Data using Python
雲端計算.
Option One Install Python via installing Anaconda:
雲端計算.
雲端計算.
Word Processing and Desktop Publishing Software
3. Numerical Methods II JHU Physics & Astronomy Python Workshop 2018
Tutorial 7 – Integrating Access With the Web and With Other Programs
雲端計算.
Data Wrangling with pandas
Boyce Astro: Online Catalogs BRIEF Boyce Astro:
Matplotlib and Pandas
Collecting, Analyzing, and Visualizing Data with Python Part I
X-ray high resolution spectra in the VO: the case of XMM-Newton RGS
Mapping packages Unfortunately none come with Anaconda (only geoprocessing is which does lat/long to Cartesian conversions). matplotlib.
Presentation transcript:

7. Power in Packages JHU Physics & Astronomy Python Workshop 2018 Instructors: Eric Winter (StScI) Kate Rowlands (JHU Astro) Jenn Wojno (JHU Astro)

Now, for some fun! We’ve been showing you how to do things in Python that you could (for the most part) in many other scripting/programming languages. Let’s show you things that make Python great!

The PANDAS in the Room Pandas is a fully-featured data analysis library. This is well beyond the scope of our workshop, but it allows a great deal of data mangling and manipulation features. Particularly useful for pivot tables & timeseries. http://pandas.pydata.org

A Quick Pandas Example Quickly turning a two-dimensional dataset into a three-dimensional dataset using a column to group on: import pandas as pd import numpy as np # Creating Random Data: tmp_dates = pd.date_range(‘2015-10-29’, periods= 100) # Date tmp_cat = np.random.randint(1, 4, 100) # Category tmp_data = np.random.randn(100) # Random normal data # Creating Pandas Dataframe: df1 = pd.DataFrame( {‘dates':tmp_dates, 'cat':tmp_cat, 'data':tmp_data} )

A Quick Pandas Example # Now to group by category as well as date: pdf1 = df1.pivot('dates', 'cat', 'data') print(pdf1) # Returns: # cat 1 2 3 # dates # 2015-10-29 0.515307 NaN NaN # 2015-10-30 NaN 0.163088 NaN # 2015-10-31 0.972008 NaN NaN # 2015-11-01 NaN -0.502585 NaN # 2015-11-02 NaN 0.274932 NaN # 2015-11-03 NaN 0.258800 NaN # 2015-11-04 -1.579318 NaN NaN

World Coordinate System Typically, you need to know where on an image each pixel is in astronomical coordinates (either RA & Dec, or maybe Galactic Longitude and Latitude). This information (typically referred to as WCS) is typically stored in the header of your FITS file. To use this information, you can use the astropy.wcs module: from astropy.io import fits from astropy.wcs import WCS # From a header: hdus = fits.open('FOC.fits') hdr0 = hdus[0].header hdus.close() w = WCS(hdr0) # Or just getting one from a file itself: w = WCS('FOC.fits')

From Pixels to Coordinates and back # Read in a FITS image. hdus = fits.open('FOC.fits') hdr0 = hdus[0].header img0 = hdus[0].data img0.shape # Get the WCS. w = WCS(hdr0) # Map some pixels RA and DEC. pixels = [[0,0], [512,512], [1023,1023]] w.all_pix2world(pixels, 0) # 0-based # Map some sky positions back to pixels. pos = [[182.636,39.4012], [182.64,39.402]] w.all_world2pix(pos, 0) # We've left the image!

Plotting a FITS Image Next, we’ll run through plotting an image: It is important to note that most often, the pixels from the FITS image are not perfectly aligned with the coordinate grid, and aren’t necessarily the same size on sky throughout the image. In these cases, it is critical to use pcolor (or pcolormesh) to get the orientations correct. If you want to use imshow, remember anything else you’d like to plot should be converted into pixel coordinates through the w.all_world2pix() function. Next, we’ll run through plotting an image:

Plotting a FITS Image PRO TIP: Note that the order of the input are in standard Cartesian ordering, and the opposite of the FITS image read in. # Continuing previous example... import numpy as np # Making Indices xpx = np.arange(img0.shape[1]+1)-0.5 # Bin edges ypx = np.arange(img0.shape[0]+1)-0.5 xlist, ylist = np.meshgrid(xpx, ypx) # 2 grids ralist, declist = w.all_pix2world(xlist, ylist, 0) # Plotting import matplotlib.pyplot as plt plt.pcolormesh(ralist, declist, img0)

Plotting a FITS Image

Plotting a FITS Image # Enhance contrast... img0.min(), img0.max() plt.clf() plt.pcolormesh(ralist, declist, img0, vmin=0, vmax=10)

Plotting a FITS Image

Plotting a FITS Image PRO TIP: Remember that RA traditionally increases to the left, so you’ll have to flip the axis manually through plt.xlim()

Coordinate Transformations Astropy provides a way of dealing with coordinates, and automatically deal with conversions: from astropy.coordinates import SkyCoord # Making Coordinates: ra, dec = 45.6, 57.8 c1 = SkyCoord(ra, dec, frame='icrs', unit='deg') l, b = 23.4, -87.9 c2 = SkyCoord(l, b, frame='galactic', unit='deg') c3 = SkyCoord('00h12m30s', '+42d12m00s') # Printing and Conversions: c1.ra, c1.dec, c1.ra.hour, c2.icrs.ra.hms, c3.dec.dms c2.fk5, c1.galactic # Converting Coordinates c2.to_string(‘decimal’), c1.to_string(‘hmsdms’)

PHYSICAL Units Astropy provides a way to manipulate quantities, automatically taking care of unit conversions automatically: from astropy import units as u # Defining Quantities with units: val1, val2 = 30.2 * u.cm, 2.2E4 * u.s val3 = val1/val2 # Will be units cm / s # Converting Units val3kmpers = val3.to(u.km/u.s) # Simplifying Units val4 = (10.3 * u.s / (3 * u.Hz)).decompose()

Physical/ASTRONOMICAL Constants Astropy also provides constants (with units): from astropy import constants as c # Some constants c.k_B, c.c, c.M_sun, c.L_sun # Can use with units energy = c.h * 30 * u.Ghz # Can convert units mass = (3.2E13 * u.kg).to(c.M_sun) mass.value

install astroquery conda install astroquery

Astronomical Querying Astroquery allows access to online databases of various sources. The documentation is located: http://astroquery.read thedocs.org/en/latest/

Astronomical Querying There are lots of possible databases to query, but as a quick example (from Simbad): from astroquery.simbad import Simbad # Simbad s = Simbad() # Search by name tab1 = s.query_object(‘M31’) # Printing Table tab1.pprint()

Astronomical Querying There are lots of possible databases to query, but as a quick example (from Simbad): from astroquery.simbad import Simbad # Simbad s = Simbad() # Searching on Region c1 = SkyCoord(298.4, -0.4, frame=‘galactic’, unit=‘deg’) tab1 = s.query_region(c1, radius=1*u.deg) # Few secs... # Printing Table tab1.pprint()

MPLD3: Matplotlib in your Browser Matplotlib Figure D3.js Webpage

MPLD3: Matplotlib in your Browser While not every matplotlib function is supported, it is easy to export your plot into an interactive HTML-based plot: import mpld3 # conda install it first! import numpy as np import matplotlib.pyplot as plt # Make a plot and export it to the web. x = np.linspace(-1, 1, 101) y = x**2 plt.plot(x, y) fig1 = plt.gcf() mpld3.save_html(fig1,'fig1.html')

Play Time!