The first thing to do is to open up a pywwt window in this notebook session. As is generally the case, we have to start with some Python imports:
from pywwt.jupyter import WWTJupyterWidget
We'll also set up a utility function:
def datapath(*args): from os.path import join, pardir return join(pardir, 'data', *args)
Next, create a widget and display it inline. (That's why the final line is a bare
wwt = WWTJupyterWidget() wwt
It might look like you got a black screen but it's probably OK. Click your mouse in the widget view and drag to either side. You should see the Milky Way scroll into view! Check with a neighbor if you're concerned that things are not working.
Next, we will move the WWT view to a new window pane, which makes it much easier to interact with the visualization while writing code:
Stop here so we can bring it back together and introduce the first activity.
from astropy.table import Table exoplanets = Table.read(datapath('planets_2019.09.30_16.25.03.votable'))
Here's a helpful technique: take a look at the first 5 rows of the table to get a sense of what the data look like.
How many rows are there in total?
WWT has a model of different "layers" that add data to the view. Once we have an Astropy data table, adding it to the view is easy. We start off by calling
add_table_layer to create the layer:
layer = wwt.layers.add_table_layer(exoplanets)
By default the points are quite small, so we can enlarge them:
layer.size_scale = 50
You should now see points appear in the widget above. Have a look around and see what the distribution of these confirmed exoplanets is on the sky. For example, you can use the following to point the view at the Kepler field:
from astropy import units as u from astropy.coordinates import SkyCoord wwt.center_on_coordinates(SkyCoord(76.32, 13.5, unit='deg', frame='galactic'), fov=20 * u.deg)
All the points are currently white, but you can also choose to color code points by one of the other attributes. For example, we can color code the points by effective temperature of the parent star:
layer.cmap_att = 'st_teff'
cmap_vmax properties can be used to specify which values should be used as the extremes for the colorbar:
layer.cmap_vmin = 5000 layer.cmap_vmax = 8000
and finally the colormap can be set using the
cmap attribute, which takes either the name of a Matplotlib colormap or a Matplotlib colormap object:
layer.cmap = 'RdYlBu'
We can also scale the size of the points by one of the attributes — for example by the mass of the planet, and similarly we can set lower and upper values for the scaling:
layer.size_att = 'pl_bmassj' layer.size_vmin = 0 layer.size_vmax = 30
Luckily, the exoplanet catalog contains Gaia distances to most of the planetary systems, so we can now tell the layer about the third dimension:
layer.alt_att = 'gaia_dist' layer.alt_type = 'distance'
Here's an important step. WWT includes a 3D mode that lets us explore this data table in, well, 3D:
Start zooming out, with a two finger scroll, using your mouse wheel, or by pressing 'x'. It will take a while, but eventually you'll see 3D motion of the points from our data table. Some of the larger points dominate the view, so we can turn off the scaling by size:
layer.size_att = ''
Keep on zooming out until you can see (an artist’s rendering of) the plane of the Milky Way. At this point, if your orient the view in the right 3D direction, you can see a cone of systems which corresponds to those discovered in the original Kepler field!
Note: in Firefox, this stage can briefly slow down your browser a lot. Let WWT grind through for a minute and it will become smooth again.
You may notice that some points appear and disappear as you rotate around. To avoid this effect, set the following option:
layer.far_side_visible = True
When this option is
False (the default), points on the other side of the Sun (or the reference object being used for plotting) are hidden — this is useful when plotting on e.g. planets and the Sun, but not when looking at this kind of data.
When you're done playing around with this view, reset the widget so that we can begin the next stage of the tutorial:
We like to say that WWT is a "4D" engine because it also knows about the passage of time. In this section, we'll demonstrate this functionality using a table of GRB observations.
First, we'll need some imports:
from astropy.coordinates import SkyCoord from astropy.table import Table from astropy.time import Time, TimeDelta from astropy import units as u from datetime import datetime
Now, we'll load up a table of GRB data. As before, we'll print out the first few rows to get a sense of the data structure. In this case it's pretty simple.
bursts = Table.read(datapath('grb_table_lite.ecsv'), format='ascii.ecsv') bursts[:5]
The WWT engine doesn't know enough to know that the Julian Date values above are actually dates (and not just some random floating-point measurements), so we convert them to the ISO8601 format:
bursts['dates_ISOT'] = Time(bursts['dates_JD'], format='jd').isot bursts[:5]
As before, let's create a table layer:
layer = wwt.layers.add_table_layer(table=bursts, frame='Sky', lon_att='ra', lat_att='dec', size_scale=200)
Now let's tell the engine that we want to pay attentio to the time axis of the table. This will make all of the points disappear, since the engine's clock is synchronized to your computer — it's plotting data for the year 2019, and all of our GRBs are much older than that.
layer.time_series = True layer.time_att = 'dates_ISOT'
We can check the latter assertion using the
Let's demonstrate that things are actually working by centering on the first GRB and setting the time to just before it goes off.
first_grb = bursts wwt.center_on_coordinates(SkyCoord(first_grb['ra'], first_grb['dec'], unit=u.deg)) wwt.set_current_time(Time(first_grb['dates_ISOT'], format='isot') - TimeDelta(3 * u.second))
You should see a white dot appear in the screen center after three seconds!
The WWT timeseries support has the concept of a "decay time". Points that appear on the screen slowly fade over the decay timescale — according to the WWT simulation clock, that is, not wallclock time. The default is 16 days. That's a bit short for visualizing GRBs, so let's make it longer:
layer.time_decay = 1 * u.yr
Now, let's speed time up to see the GRBs pop off over time. If you find the right patch of the sky (namely, the ecliptic), you might also see the Sun and planets moving around at enhanced speed.
Once you've had your fill, reset the widget again:
wwt.play_time() # this resets the clock to run at 1 second per second wwt.reset()
Stop here so that we can bring it back together, ask questions, and discuss how things went.
In this activity, we will visualize a WISE 12µm image towards the Westerhout 5 star forming region, and we will take a look at some of the advanced visualization options.
Images, like data tables, are represented in WWT as "layers" that can be added to the view. With a standard FITS file, all you need to do is provide a pathname:
layer = wwt.layers.add_image_layer(datapath('w5.fits'))
The viewer will automatically center and zoom to the image you've loaded. You may get a warning from the
reproject module; this can safely be ignored.
"Printing" the following variable will create a set of widgets that let you adjust how the data are visualized:
The image color scaling is controlled by the sliders in the "Fine min/max" row; the "Coarse min/max" boxes control the bounds that are placed on the range of those sliders.
You should try sliding the image opacity back and forth to check the agreement between the morphology of the W5 image and the WWT all-sky map.
All of the parameters that are controlled by the widgets above can be manipulated programmatically as well. Let's set a bunch of them at once:
layer.cmap = 'plasma' layer.vmin = 400 layer.vmax = 1000 layer.stretch = 'sqrt' layer.opacity = 0.9
Note that the settings in the widgets adjusted automatically to match what you entered. Fancy!
After you're done playing around, let's reset the WWT widget as usual:
Let's pause for the next segment.
Because pywwt is a Python module, not a standalone application, it gains a lot of power by being able to integrate with other components of the modern, Web-oriented astronomical software ecosystem.
For instance, it is easy to use the Python module astroquery to load in data directly from archive queries, without the requirement to save any files locally. Let's fetch 2MASS Ks-band images of the field of supernova 2011fe:
from astroquery.skyview import SkyView img_list = SkyView.get_images(position='SN 2011FE', survey='2MASS-K', pixels=500) # you can adjust the size if you want assert len(img_list) == 1 # there's only one matching item in this example twomass_img = img_list twomass_img.info()
We can import it into WWT in the usual way:
twomass_layer = wwt.layers.add_image_layer(twomass_img)
Once again you should see the view automatically center on your image. Let's adjust the background imagery to be more relevant:
wwt.background = wwt.imagery.ir.twomass wwt.foreground_opacity = 0
We also provide interactive controls to let you adjust the parameters of the contextual imagery that's being shown. Try choosing different sets of all-sky imagery and adjusting the blend between them:
Here are some settings that we like:
wwt.background = wwt.imagery.visible.sdss wwt.foreground = wwt.imagery.gamma.fermi wwt.foreground_opacity = .7
Now we'll load up another image of the same field that came from Swift, this time stored as a local file as in our previous activity:
swift_layer = wwt.layers.add_image_layer(datapath('m101_swiftx.fits'))
Create controls to adjust all of the visualization parameters. If you want to go wild, you can overlay data from four different wavelengths in this one view!
Exporting everything is easy, but due to an unfortunate bug in our JupyterLab support, it doesn't work reliably when you separate out the WWT window as have done in this tutorial. So, let's create a secondary WWT window:
wwt2 = WWTJupyterWidget() wwt2
For what it's worth, this does demonstrate that you can create multiple WWT widgets in the same notebook!
Now, let's repopulate it with our images:
twomass_layer2 = wwt2.layers.add_image_layer(twomass_img) swift_layer2 = wwt2.layers.add_image_layer(datapath('m101_swiftx.fits'), opacity=0.5, cmap='plasma')
You could tweak the appearance here by inserting cells to create control widgets with
Once we've jumped through this hoop, exporting the HTML is this easy:
You should see a file named
figure.zip appear in the folder view sidebar to the left of this notebook. (It might take a few seconds to appear.) You can right-click that file and select "Download" to save the Zip bundle to your local machine.
You might want to open up this Zip file and inspect its contents. You will see that it contains an
data directory containing two FITS files. These correspond to the images that you just loaded into your view.
If you have Node.js installed on your computer, you can view your exported HTML in a straightforward way:
cdto the unpacked directory (the one containing
npx httpserver 12345
Note: alas, we also have a race condition bug that sometimes pops up here. You might need to reload the page for the images to appear.
Other one-liner web servers might work, but not all of them serve files with the correct
Content-Type headers, which is needed for everything to work here.
Want to get more involved in the AAS WorldWide Telescope community? Here are some next steps you can take: