Blogging with ipynb

To use ipynb

Download an IPython compatible theme. To list all the available themes in Nikola:

nikola install_theme -l

To install

nikola install_theme jinja-site-ipython

Modify your file. Add the following lines to POSTS:

("posts/*.ipynb", "posts", "post.tmpl", True),

and add the following line to PAGES:

("stories/*.ipynb", "stories", "story.tmpl", False),

and set THEME to:

THEME = 'jinja-site-ipython'

To write a new post,

nikola new_post -f ipynb

-f ipynb is not needed if POSTS/PAGES only have ipynb option

IRkernel/IRkernel: Running R kernel for Jupyter

In order to be able to use native R kernel for Jupyter, you need to do the following:

1) Install R

brew tap homebrew/science 
brew install r

2) Run R in a terminal, the run the following

install.packages(c('repr', 'pbdZMQ', 'devtools'))
devtools::install_github(c('IRkernel/IRdisplay', 'IRkernel/IRkernel'))
IRkernel::installspec(user = FALSE)


brew update
brew doctor

The SDSS 1D Spectrum of Q1357+0525

In [1]:
#Font style
from IPython.core.display import HTML
css = open('style-table.css').read() \
+ open('style-notebook.css').read()

Here, I demonstrate that we can make plots of SDSS spectra without downloading the actual fits file into a local machine. In this example, we grab the 1D spectrum of quasar Q1357+0525 from the SDSS DR12 Data Archive Server (DAS).

The wavelength range of the plot can be interactively adjusted using the sliders.

SDSS Notes:

  • ### Physical quantities pertinent to the object obtained by SDSS can be accessed here.

  • ### The 1D spectrum can be pre-viewed here.

In [2]:
import as fits
import numpy as np

import matplotlib
import matplotlib.pyplot as plt

from matplotlib.ticker import AutoMinorLocator
from matplotlib.ticker import FuncFormatter
import matplotlib.font_manager as font_manager
from matplotlib import rcParams

from ipywidgets import interact, fixed

%matplotlib inline

We now grab the spectrum and plot. Extract the wavelength, flux and error columns.

In [3]:
def plot_spec(min_wave=3800, max_wave=9200):
    spec, header = fits.getdata('', 1, header=True)
    wave = 10**spec['loglam']
    flux = spec['flux']
    error = np.sqrt(1./spec['ivar'])
    plt.plot(wave, flux, drawstyle='steps')
    #plt.plot(wave, error)
    if min_wave >= max_wave:
        min_wave = 3800
        max_wave = 9200
    plt.xlim(min_wave, max_wave)
    flux_vals = flux[np.where((wave >= min_wave) & (wave <= max_wave)) ]
    plt.ylim(flux_vals.min(), flux_vals.max())
    plt.xlabel('Observed Wavelength '+r'[$\mathrm{\AA}$]', fontsize=25)
    plt.ylabel(r'$f_{\lambda}$ '+ r'$\mathrm{[10^{-17} ergs/cm/\AA/s]}$' , fontsize=25)
    plt.tick_params('both', length=8, width=2, which='minor')
    plt.tick_params('both', length=12, width=3, which='major')
In [4]:
#Make the plot HUGE and vivid
minorLocatorx   = AutoMinorLocator(10)
minorLocatory   = AutoMinorLocator(4)
matplotlib.rc('xtick', labelsize=20) 
matplotlib.rc('ytick', labelsize=20) 
matplotlib.rcParams['axes.linewidth'] = 2.
plt.rcParams['axes.linewidth'] = 4
plt.rc('font', family='serif')
plt.rc('font', serif='Times New Roman') 
fig_size = plt.rcParams["figure.figsize"]
fig_size[0] = 18
fig_size[1] = 9
plt.rcParams["figure.figsize"] = fig_size

# Use interact to explore the galaxy detection algorithm.
interact(plot_spec, min_wave=(3800, 9000, 100), max_wave=(4000,9200, 100));

SSH Login Without Password

If you want to ssh from 'userx' of host X to 'usery' of host Y without typing password, you need to do the following.

  1. Log in on host X as userx and generate a pair of authentication keys. Make sure not to enter a passphrase:
    userx@X:~> ssh-keygen -t rsa
    Generating public/private rsa key pair.
    Enter file in which to save the key (/home/userx/.ssh/id_rsa): 
    Created directory '/home/userx/.ssh'.
    Enter passphrase (empty for no passphrase): 
    Enter same passphrase again: 
    Your identification has been saved in /home/userx/.ssh/id_rsa.
    Your public key has been saved in /home/userx/.ssh/
    The key fingerprint is:
    3x:5f:05:79:3d:8f:9e:7c:3b:ad:e9:58:37:bc:37:e4 userx@X

    Read more…

Installing Python mysql.connector

import mysql.connector

For OSX, the import above requires installation of

For Ubuntu (or a Debian based distro),

sudo pip install mysql-connector-python --allow-external mysql-connector-python

OCaml and Jupyter

I decided to try out OCaml as a kernel in a Jupyter notebook. Doing it was pretty straight forward. All I did was:

1) Install OPAM

brew install opam

2) Install OCaml and some libraries

opam init
eval `opam config env`
opam install batteries core

3) Install iocaml

opam install iocaml
In [ ]:

Calculating dndz parameters.

The low-redshift $\frac{\partial n}{\partial z}$ of HI

This code calculates an $n_0$ and an $\alpha$ via least-squares $\chi^{2}$ fit.

Read more…

Effects of the New HST 2016 Calibration Corrections on M31 Results (PID: 11658)

In [1]:
#Font style
from IPython.core.display import HTML
css = open('style-table.css').read() \
+ open('style-notebook.css').read()

Old versus New HST Calibrations for G230L M31 Data

Read more…

Python Cheatsheet

Python Cheatsheet



a = 1           # integer
b = 1.0         # float
c = 1.0e5       # exponential
d = 1.0 + 2.5j  # complex
e = 1 > 0       # boolean
f = 'string'    # string

Read more…