In [2]:
%matplotlib inline
import os
import h5py

import numpy as np
import matplotlib.pyplot as plt
from astropy.io import fits
import pandas as pd

import matplotlib
matplotlib.rcParams['axes.grid'] = False
matplotlib.rcParams['axes.facecolor'] = 'white'

from bubbly.cluster import merge, xmatch
from bubbly.dr1 import bubble_params

figures = {}

basedir = os.path.abspath(os.path.join('..', 'bubbly', 'data', 
                                       'galaxy', 'registered'))


DEBUG:Cloud:Log file (/Users/beaumont/.picloud/cloud.log) opened

In [3]:
data = pd.read_csv('../data/pdr1.csv')
data['diam'] = 2 * np.sqrt(data.a * data.b)

low = data[data.prob < .5]
mid = data[(data.prob >= .5) & (data.prob < .9)]
hi = data[data.prob >= .9]

nlow = data[~(data.prob < .5)]
nmid = data[~((data.prob >= .5) & (data.prob < .9))]
nhi = data[~(data.prob >= .9)]

In [4]:
f = h5py.File('../data/full_search/317.h5', 'r')
np.isnan(f['scores'][...]).sum()


Out[4]:
17310

In [5]:
def legend():
    colors = ['#1D91C0', '#FE9929', '#D9D9D9', '#C51B7D']
    labels = ['P > 0.9', '0.5 < P < 0.9', 'P < 0.5', 'New']
    ys = [.95, .88, .81, .74]
    for color, label, y in zip(colors, labels, ys):
        plt.annotate(label, (.98, y), xycoords="axes fraction", color=color,
                     fontsize=14, ha='right')


def blind_bubbles(lon, run_merge=True):
    with h5py.File('../data/full_search/%3.3i.h5' % lon, 'r') as infile:
        stamps = infile['stamps'][...]
        scores = infile['scores'][...]
   
    print np.unique(stamps[:, -1])
    mask = scores > 0.4
    if run_merge:
        merged, scores = merge(stamps[mask], scores[mask])
    else:
        merged, scores = stamps[mask], scores[mask]
    
    mwp = [b for b in bubble_params() if np.abs(b[1] - lon) <= 2]
    match = xmatch(merged, mwp)
    merged = merged[match == -1]
    merged = pd.DataFrame({'lon':merged[:, 1], 'lat': merged[:, 2], 'diam': merged[:, 3] * 2})
    return merged

In [9]:
figures['l317'] = plt.figure(figsize=(5, 5), dpi=200, tight_layout=True)

r = fits.getdata(os.path.join(basedir, '317_mips.fits'))
g = fits.getdata(os.path.join(basedir, '317_i4.fits'))
#b = fits.getdata(os.path.join(basedir, '317_i3.fits'))
b = g * 0

bad = r == 0

def logscale(x, a, b):
    n = matplotlib.colors.LogNorm(vmin=a, vmax=b, clip=True)
    return (n(x) * 255).astype(np.uint8)

g = logscale(g, 50, 400)
r = logscale(r, 27, 1105)
b = logscale(b, 27, 5000) * 0

r[bad] = 255

rgb = np.dstack((r, g, b))
rgb[0:180, :, :] = 0
rgb[-180:, :, :] = 0

plt.imshow(rgb, extent=[319, 315, -1, 1])


def plot_bubbles(df, color, center, label, alpha=1.0, lw=2, ls='solid'):
    c = None
    xlim = plt.gca().get_xlim()
    ylim = plt.gca().get_ylim()
    xthresh = xlim[0] + .6 * (xlim[1] - xlim[0])
    ythresh = ylim[0] + .6 * (ylim[1] - ylim[0])
    for i, row in df.iterrows():
        if np.abs(row['lon'] - center) > 1.5:            
            continue
        if (row['lon'] < xthresh) & (row['lat'] > ythresh):
            continue
            
        c = plt.Circle((row['lon'], row['lat']), row['diam'] / 2, 
                       edgecolor=color, facecolor='none', lw=lw, alpha=alpha, ls=ls)
        plt.gca().add_patch(c)
    if c is not None:
        c.set_label(label)
        
#overplot new bubbles discovered during blind search
merged = blind_bubbles(317)
param = np.array([[317, 360 - 43, -0.15, 1.1]])
ex = pd.DataFrame(param, columns=['field', 'lon', 'lat', 'diam'])

plot_bubbles(hi, '#1D91C0', 317, 'P > 0.9')
plot_bubbles(mid, '#FE9929', 317, '0.5 < P < 0.9')
plot_bubbles(low, '#D9D9D9', 317, 'P < 0.5')
plot_bubbles(merged, '#C51B7D', 317, 'New')
plot_bubbles(ex, '#FFFFFF', 317, 'Undetectable', ls='dashed')


plt.xlim(317.8, 316.2)
plt.ylim(-.8, .8)
plt.xlabel("Galactic Longitude")
plt.ylabel("Galactic Latitude")
plt.annotate('Undetectable', fontsize = 16, xy=(360-43.2, -.75), color='white')
xt = plt.xticks()
plt.xticks(xt[0], xt[0] - 360)

legend()
    
plt.show()


[ 0.02222222  0.02777778  0.03444444  0.04277778  0.05333333  0.06666667
  0.08333334  0.10388889  0.12944445  0.16166666  0.20166667  0.25166667
  0.31444445  0.39277777  0.49055555  0.61277777  0.76555556  0.95666665]

In [7]:
figures['l305'] = plt.figure(figsize=(5, 5), dpi=200)

r = fits.getdata(os.path.join(basedir, '305_mips.fits'))
g = fits.getdata(os.path.join(basedir, '305_i4.fits'))

bad = r == 0
g = logscale(g, 60, 700)
r = logscale(r, 30, 1200)
b = r * 0
r[bad] = 255
rgb = np.dstack((r, g, b))
rgb[0:180, :, :] = 0
rgb[-220:, :, :] = 0

plt.imshow(rgb, extent=[307, 303, -1, 1], 
           interpolation='bicubic')


#overplot new bubbles discovered during blind search
merged = blind_bubbles(305)
unmerged = blind_bubbles(305, run_merge=False)
#plot_bubbles(unmerged, '#AACC55', 305, None, alpha = 1.0, lw=.7)

plot_bubbles(hi, '#1D91C0', 305, 'P > 0.9')
plot_bubbles(mid, '#FE9929', 305, '0.5 < P < 0.9')
plot_bubbles(low, '#D9D9D9', 305, 'P < 0.5')
plot_bubbles(merged, '#C51B7D', 305, 'New')

#reference location
param = np.array([[305, 360 - 54.6, 0, 1.1]])
ex = pd.DataFrame(param, columns=['field', 'lon', 'lat', 'diam'])
plot_bubbles(ex, '#FFFFFF', 305, '', ls='dashed')

plt.xlim(306, 304.4)
plt.ylim(-.8, .8)
plt.xlabel("Galactic Longitude")
plt.ylabel("Galactic Latitude")

plt.annotate('Undetectable', fontsize = 16, xy=(360-55.1, -.4), color='white')

xt = plt.xticks()
plt.xticks(xt[0], xt[0] - 360)

legend()
plt.show()


[ 0.02222222  0.02777778  0.03444444  0.04277778  0.05333333  0.06666667
  0.08333334  0.10388889  0.12944445  0.16166666  0.20166667  0.25166667
  0.31444445  0.39277777  0.49055555  0.61277777  0.76555556  0.95666665]
[ 0.02222222  0.02777778  0.03444444  0.04277778  0.05333333  0.06666667
  0.08333334  0.10388889  0.12944445  0.16166666  0.20166667  0.25166667
  0.31444445  0.39277777  0.49055555  0.61277777  0.76555556  0.95666665]

In [83]:
from bubbly.model import ModelGroup
p = param.copy()
ModelGroup.load('../models/full_classifier.dat').decision_function(p)


Loading a new field at l=317
DEBUG:bubbly.field:Loading a new field at l=317
Out[83]:
array([ nan])

In [52]:
merged


Out[52]:
diam lat lon
0 0.027778 -0.658056 304.719177
1 0.022222 -0.801944 304.770844
2 0.314444 0.099167 305.214722
3 0.027778 0.286389 305.274719

In [6]:
figures['l299'] = plt.figure(figsize=(5, 5), dpi=200)

r = fits.getdata(os.path.join(basedir, '299_mips.fits'))
g = fits.getdata(os.path.join(basedir, '299_i4.fits'))

bad = r == 0
g = logscale(g, 40, 300)
r = logscale(r, 10, 1200)
b = r * 0
r[bad] = 255
rgb = np.dstack((r, g, b))
rgb[0:180, :, :] = 0
rgb[-220:, :, :] = 0

plt.imshow(rgb, extent=[300, 298, -1, 1], 
           interpolation='bicubic')

plot_bubbles(hi, '#1D91C0', 299, 'P > 0.9')
plot_bubbles(mid, '#FE9929', 299, '0.5 < P < 0.9')
plot_bubbles(low, '#D9D9D9', 299, 'P < 0.5')

merged = blind_bubbles(299)
plot_bubbles(merged, '#C51B7D', 299, 'New')

plt.xlim(299.6, 298)
plt.ylim(-.8, .6)
plt.xlabel("Galactic Longitude")
plt.ylabel("Galactic Latitude")

xt = plt.xticks()
plt.xticks(xt[0], xt[0] - 360)

legend()
plt.show()



In [ ]: