How Long Do Senators Actually Serve?

No comments:
Today is the 2015 State of the Union address by President Obama. His speech has already been widely written about, and as with most SOTU addresses presenting it is largely a formality. There are many traditions associated with the SOTU, including the POTUS having to be invited to give the talk.

Another SOTU tradition, which I find very American (and started in the 1960's), is the SOTU response given by a member of the opposite political party to the POTUS. This year Joni Ernst, a brand new Senator from Iowa whose campaign was dominated by hog castration, will be the 16th woman to deliver a SOTU response.

Senator Ernst may be a public figure for quite some time, so I wondered today:
how long do Senators typically serve? 
There are some very well known examples of career Senators, for example the lengendary badass, Daniel K. Inouye. But what of the typical Senator? Here are some numbers...

After nearly 230 years, the US Senate has more than 1855 distinct former members. The term length for Senators is 6 years (compared with the typical length of 2 years for representatives).
Here is the histogram of term durations for all past Senators. Note: some people served multiple, non-consecutive terms, or changed parties, which counts in this data as a separate term. You can clearly see the spikes of 1, 2, ... 6 terms. The average term is 7.6 years (median 6 years) for all past Senators.

However, the typical duration a Senator serves appears to be changing. Above I have plotted the duration versus the year elected for all past Senators. You can see in the running 2-year median the typical serving duration has increased, especially since WW2. While the overall average for length served is 7.6 years, since 1950 it has risen to 11.8 years. This means on average Senators are elected twice, and will serve during at least 2 presidencies.

This is longer than the average rein of a Pope (7.3 years), but shorter than a US Supreme Court Justice (16.4 years).

GitHub repo with data is here

Making Quality Animations in IDL

No comments:
Here is a brief post that outlines how I easily create high quality animations using IDL. I realize IDL is not the most popular language in the astrophysics community these days, but this processes works for any language that can render plots to a file. While there are other ways to do this in Python, I think this processes works well since it allows you to experiment with each stage independently. I'll also embed a few animations I have created, hopefully to give you an idea of what is possible.

The workflow is relatively simple:
render many still frames > convert to images > encode them as a video

Note, this is the same workflow to create animated gifs, just swap the last step.

Note also, this guide assumes you are using OS X or LINUX.



1. Make (Good) Still Frames

So you have a fantastic plot, or maybe a really complex dataset, but you need to animate it. I'll assume you're already using a good color scheme, and making smart choices about contrast.

The initial, very basic stage is to figure out what variable you need to step over during the course of the animation. This could be actual time, or data subset, or you could simply move the position of the visualization (e.g. making data or a sphere spin). Then you want to write the most hated of all IDL code: a FOR loop!

With each step of the FOR loop you generate a single frame of the animation. Here's some generic sample code:

Nframes = 1000
FOR i=0L,Nframes-1 DO BEGIN
    device, filename = 'frame' + string(i, f='(I05)') + $
        '.eps', /encap,/inch,/color, $
        xsize=10,ysize=10
    plot, xdata, ydata, /xstyle, xrange=[0,1]+i
    device, /close
ENDFOR


This will create a series of still frames where the data moves along the x-axis. A couple key points:
  1. This will create 1000 files with names "frame00001.eps", "frame00002.eps".... etc. Padding numbers with zeroes is a very helpful trick.
  2. Save these files as (encapsulated) postscripts in IDL. I will go against the advice of the Coyote and say use the highest quality output that IDL offers: the postscript. You'll be glad you did later. So what if these files are huge, harddrive be damned.

You see LOTS of examples in the wild where people don't generate .eps or .ps files for their animations with IDL, and instead essentially screengrab from the IDL X Window device (many of the direct-to-jpeg outputs do this). We all know that IDL's native X Window graphing is really... rough. OK, It's down right horrid. So use the postscript output and get nice, high quality frames!

The example code is virtually the same for Python, wrapping a plt.savefig() call in a for loop. Note for Python, however, you can plot directly to a JPEG file, thus skipping step 2 below.



2. Convert Still Frames to Images

Now you've got a directory full of 1000 postscript files. This is helpful, but to create the movie you'll want to convert them to JPEG (or similar).

To do this conversion, I use ImageMagick's convert command. This does exactly as you expect, you tell it what to convert images to and it just works. I like to do this on the fly while the evil FOR loop is running since convert can't (as far as I know) do batches. The example code would then be:

FOR i=0L,Nframes-1 DO BEGIN
    device, filename = 'frame' + string(i, f='(I05)') + $
        '.eps', /encap,/inch,/color, $
        xsize=10,ysize=10
    plot, xdata, ydata, /xstyle, xrange=[0,1]+i
    device, /close

    spawn, 'convert -density 150x150 -flatten ' + $
        'frame' + string(i, f='(I05)') + '.eps ' + $
        'frame' + string(i, f='(I05)') + '.jpeg'

    spawn, 'rm frame' + string(i, f='(I05)') + '.eps' 
ENDFOR

I'm being a bit verbose here, but you get the idea. IDL generates the .eps file, ImageMagick converts it to a .jpeg, and then a shell call deletes the redundant .eps file. You can play around with the jpeg resolution of course. The end result is 1000 JPEG files, ready for encoding into a single movie!



3. Encode the Video

The next step is thankfully the easiest! Some people will drag/drop files in to iMovie, adjust still frame duration, hope all the frames land in order... This is a great idea if you're comfortable with something like iMovie. (note: it also makes adding credits, effects, transitions, and audio way easier)

Instead, for my normal workflow I use FFmpeg (installed via MacPorts). The beauty of this is I can just add one more line to my IDL code (after the FOR loop), and out pops the movie!

spawn, 'ffmpeg -r 20 -i frame%05d.jpeg ' + $
    ' -qscale 1 my_movie.mp4'

done! You can adjust the frame rate (-r 20 means 20 frames per second) and output quality (qscale). FFmpeg has approximately a trillion options, including cropping and rescaling existing animations to fit different screen sizes, and handling audio tracks.

Bonus: Animated GIFs

All the videos I have featured here were created using this process. As I mentioned in the introduction, the same processes applies for creating animated gifs: just swap the JPEG for still GIF frames in step 2, and then I use the old-school Gifsicle tool to create the animated version. The one extra trick for Gifsicle is that I haven't been able to use the % wild card trick I showed above, and instead you need to hand it all the file names. I just create a giant string of all the still frames I'm rendering, and pass that to Gifscile. The example code then becomes:

Nframes = 10
Flist = ''
FOR i=0L,Nframes-1 DO BEGIN
    device, filename = 'frame' + string(i, f='(I05)') + $
        '.eps', /encap,/inch,/color, $
        xsize=10,ysize=10
    plot, xdata, ydata, /xstyle, xrange=[0,1]+i
    device, /close

    spawn, 'convert -density 150x150 -flatten ' + $
        'frame' + string(i, f='(I05)') + '.eps ' + $
        'frame' + string(i, f='(I05)') + '.gif'

    spawn, 'rm frame' + string(i, f='(I05)') + '.eps' 

    Flist = Flist+' frame'+string(i,f='(I05)')+'.gif'
ENDFOR
spawn, 'gifsicle -d 5 --loop O2' + Flist + $
    ' > my_anim.gif'

Ta-Da!

12 days of data from Kepler 17

No comments:
Here is a preliminary result from my Ph.D. research that I showed in my AAS 225 talk earlier in January. This is from the final of four journal publications that will make up my thesis. My adviser Prof. Leslie Hebb and I are working (hard!) to model the evolution of starspots on the Sun-like star, Kepler 17. I am working hard to get PhD paper #3 out to publication, and to finalize these results for publication before I graduate (shameless plug for my CV). I'm sharing this video here because I think it's an elegant visualization of the method we are using, while also demonstrating the challenges and possibilities!

This video shows a single 12-day portion of data from Kepler 17. The data (in black) come from NASA's Kepler mission. The model (in red) is our best estimate of the star+planet conditions during one full rotation of the star, or about 12 days. We then repeat this analysis every 12 days over a span of 4 years (Earth years, that is!) to learn about the evolution of the starspot features we see.



When the planet crosses in front of it's host star, we sometimes observe a "bump" in the transit. This is caused by the planet crossing or occulting a dark starspot region on the stellar surface (akin to a Sunspot). The animation shows the rotating star with spots (top), the full 12-days of data (middle), and zoom-ins during the planetary transit (bottom).

The planet (Kepler 17b) has a very short "year" indeed, going around it's host star every 1.5 days. This means during the 12 days it takes Kepler 17 to rotate, the planet eclipses 8 times! With this remarkable geometry, we are able to deduce and map the locations and sizes of at least 8 starspots. Previous techniques using imaging data without a transiting exoplanet could at most infer the approximate properties for 2 or 3 starspots. With this method we believe we can robustly detect up to 10x more starspots, and even trace their evolution. While this model has 8 spots included, there is strong evidence in the 4 year dataset that times fewer and sometimes even more spots are observed! This information will unlock details about magnetic fields and the inner workings of stars beyond our Sun. For reference, only a small handful of stars currently have detailed information about their magnetic fields or starspots. Our characterization of Kepler 17 will likely be the most detailed ever analysis of spots on any star besides the Sun.

My Thesis Talk at AAS 225

No comments:
About a week ago I gave a 15 minute dissertation talk at the American Astronomical Society's 225th meeting in Seattle, WA. I was awarded the Rodger Doxsey Travel Prize to give this presentation.

Fortunately my good friend and fellow UW alum, Dr. Adam Kowalski, recorded the talk with my iPhone! This gives me a welcomed chance to critique my public speaking (there is a lot to criticize), and an opportunity to share an astronomy research-based talk on this site!

Of course, there are several nifty visualizations for your viewing pleasure...


Related Posts Plugin for WordPress, Blogger...