In [1]:
from packages.display.core import *
%pylab inline
pylab.rcParams['figure.figsize'] = (15, 2.5)


Populating the interactive namespace from numpy and matplotlib
WARNING: pylab import has clobbered these variables: ['random']
`%matplotlib` prevents importing * from pylab and numpy

In [ ]:
%qtconsole

ALMA Cycle 0


In [61]:
file_path = '../data/2011.0.00419.S/sg_ouss_id/group_ouss_id/member_ouss_2013-03-06_id/product/IRAS16547-4247_Jet_CH3OH7-6.clean.fits'
noise_pixel = (15, 4)
train_pixels = [(133, 135),(134, 135),(133, 136),(134, 136)]

In [62]:
img = fits.open(file_path)

meta = img[0].data
hdr = img[0].header

# V axis
naxisv = hdr['NAXIS3']

onevpix = hdr['CDELT3']*0.000001
v0 = hdr['CRVAL3']*0.000001
v0pix = int(hdr['CRPIX3'])
vaxis = onevpix * (np.arange(naxisv)+1-v0pix) + v0

In [63]:
values = meta[0, :, train_pixels[0][0], train_pixels[0][1]] - np.mean(meta[0, :, train_pixels[0][0], train_pixels[0][1]])
values = values/np.max(values)

plt.plot(vaxis, values)

plt.xlim(np.min(vaxis), np.max(vaxis))
plt.ylim(-1, 1)
gca().xaxis.set_major_formatter(FormatStrFormatter('%d'))



In [64]:
noise = meta[0, :, noise_pixel[0], noise_pixel[1]] - np.mean(meta[0, :, noise_pixel[0], noise_pixel[1]])
noise = noise/np.max(noise)

plt.plot(vaxis, noise)

plt.ylim(-1, 1)
plt.xlim(np.min(vaxis), np.max(vaxis))
gca().xaxis.set_major_formatter(FormatStrFormatter('%d'))


Creation of Dictionary

We create the words necessary to fit a sparse coding model to the observed spectra in the previous created cube. It returns a DataFrame with a vector for each theoretical line for each isotope in molist


In [10]:
cube_params = {
  'freq'     : vaxis[naxisv/2],
  'alpha'    : 0,
  'delta'    : 0,
  'spe_bw'   : naxisv*onevpix,
  'spe_res'  : onevpix*v0pix,
  's_f'      : 8,
  's_a'      : 0}

dictionary = gen_all_words(cube_params, True)

Recalibration of Dictionary


In [29]:
prediction = pd.DataFrame([])

for train_pixel in train_pixels:

    dictionary_recal, detected_peaks = recal_words(file_path, dictionary, cube_params, 
                                                   train_pixel, noise_pixel)

    X = get_values_filtered_normalized(file_path, train_pixel, cube_params)

    y_train = get_fortran_array(np.asmatrix(X))
    dictionary_recal_fa = np.asfortranarray(dictionary_recal,
                                            dtype= np.double)

    lambda_param = 0
    for idx in range(0, len(detected_peaks)):
        if detected_peaks[idx] != 0:
            lambda_param += 1

    param = {
      'lambda1' : lambda_param,
      # 'L': 1,
      'pos' : True,
      'mode' : 0,
      'ols' : True,
      'numThreads' : -1}

    alpha = spams.lasso(y_train, dictionary_recal_fa, **param).toarray()
    total = np.inner(dictionary_recal_fa, alpha.T)
    
    for i in range(0, len(alpha)):
        iso_col = dictionary_recal.columns[i]
        if(not prediction.columns.isin([iso_col]).any()):
            prediction[iso_col] = alpha[i]
        else:
            prediction[iso_col] = prediction[iso_col]*alpha[i]


   H2CCCHCN-f338307.6797  H2CCCHCN-f338393.2758  H2CCCHCN-f338394.9864  \
0                      0                     -0               5.030457   

   H2CCCHCN-f338400.5872  H2CCCHCN-f338433.5303&&f338433.5303  \
0                      0                                    0   

   H2CCCHCN-f338473.0368  H2CCCHCN-f338515.4437  H2CCCHCN-f338540.2052  \
0                      0                      0                      0   

   H2CCCHCN-f338553.2092  H2CCCHCN-f338598.3351&&f338598.3351  \
0               0.495737                                    0   

                   ...                    \
0                  ...                     

   SiC2v3=1-f338655.8045&&f338655.8045&&f338655.8045&&f338655.8045  \
0                                           0.013491                 

   CNCHO-f338747.5978  H2CCCHCN-f338741.071  \
0                   0                     0   

   OS18O-f338691.5731&&f338691.5905&&f338620.1046  \
0                                               0   

   OS18O-f338691.5731&&f338691.5905  CH2CN-f338706.6334&&f338706.6334  \
0                         12.464327                                 0   

   33SO2-f338712.6101&&f338712.6761  CH3OHvt=0-f338722.94  \
0                                 0                     0   

   CH3CH213CN-f338746.1199&&f338746.1199&&f338372.479  \
0                                                  0    

   CH3CH213CN-f338746.1199&&f338746.1199  
0                                      0  

[1 rows x 159 columns]

In [60]:
for p in prediction.columns:
    if(prediction[p][0] != 0):
        print(prediction[p])


0    5.030457
Name: H2CCCHCN-f338394.9864, dtype: float64
0    0.495737
Name: H2CCCHCN-f338553.2092, dtype: float64
0    4.932789
Name: H2CCCHCN-f338671.0233, dtype: float64
0    2.999577
Name: CH3C3N-f338571.3931, dtype: float64
0    0.00055
Name: CH313CH2CN-f338346.4918&&f338346.4918, dtype: float64
0    0.000007
Name: CH313CH2CN-f338449.8823, dtype: float64
0    0.483934
Name: 33SO2-f338683.0002&&f338683.0975, dtype: float64
0    1.429304
Name: CH3OHvt=0-f338486.337&&f338486.337, dtype: float64
0    0.006938
Name: CH3OHvt=0-f338583.195, dtype: float64
0    0.008504
Name: CH3OHvt=0-f338639.939, dtype: float64
0    2.941251
Name: SO2v2=1-f338348.7356, dtype: float64
0    0.305043
Name: SO2v2=1-f338376.3825, dtype: float64
0    0.000005
Name: H2CCNH-f338439.0615, dtype: float64
0    0.029845
Name: CH2CH13CN-f338380.2276, dtype: float64
0    4.074838
Name: CH3CH2CNv=0-f338435.8152, dtype: float64
0    3.946479
Name: HC3Nv7=2-f338526.9306, dtype: float64
0    13.983312
Name: 33SO-f338661.4597, dtype: float64
0    0.032195
Name: 33SO-f338673.8308, dtype: float64
0    0.013491
Name: SiC2v3=1-f338655.8045&&f338655.8045&&f338655.8045&&f338655.8045, dtype: float64
0    12.464327
Name: OS18O-f338691.5731&&f338691.5905, dtype: float64

In [76]:
latexify(8)

# Step 1: Read Cube
ax = plt.subplot(6, 1, 1)
data = get_data_from_fits(file_path)  
y = data[0, :, train_pixel[0], train_pixel[1]]
plt.xticks([])
plt.plot(vaxis, y)
lines = get_lines_from_fits(file_path)
for line in lines:
    # Shows lines really present
    isotope_frequency = int(line[1])
    isotope_name = line[0] + "-f" + str(line[1])
    plt.axvline(x=isotope_frequency, ymin=0, ymax= 3, color='g')

# 2. Normalize, filter dada
ax = plt.subplot(6, 1, 2)
plt.ylim(ymin =0,ymax = 1.15)
y = get_values_filtered_normalized(file_path, train_pixel, cube_params)
plt.xticks([])
plt.plot(vaxis, y)

# 3. Possible Words
ax = plt.subplot(6, 1, 3)
plt.ylim(ymin =0,ymax = 1.15)
plt.xticks([])
plt.plot(vaxis, dictionary)

# 4. Detect Lines
ax = plt.subplot(6, 1, 4)
plt.ylim(ymin =0,ymax = 1.15)
plt.plot(vaxis, y)
plt.xticks([])
plt.ylabel("Temperature")
for idx in range(0, len(detected_peaks)):
    if detected_peaks[idx] != 0:
        plt.axvline(x=vaxis[idx], ymin=0, ymax= 1, color='r')

# 6. Recalibrate Dictionary
ax = plt.subplot(6, 1, 5)
plt.ylim(ymin =0,ymax = 1.15)
plt.plot(vaxis, dictionary_recal_fa)
plt.xticks([])

# 6. Recover Signal
ax = plt.subplot(6, 1, 6)
plt.ylim(ymin =0,ymax = 1.15)

plt.plot(vaxis, total)

plt.xlabel("Frequency [MHz]")
gca().xaxis.set_major_formatter(FormatStrFormatter('%d'))



In [68]:
def latexify(fig_width=None, fig_height=None, columns=1):
    """Set up matplotlib's RC params for LaTeX plotting.
    Call this before plotting a figure.

    Parameters
    ----------
    fig_width : float, optional, inches
    fig_height : float,  optional, inches
    columns : {1, 2}
    """

    # code adapted from http://www.scipy.org/Cookbook/Matplotlib/LaTeX_Examples

    # Width and max height in inches for IEEE journals taken from
    # computer.org/cms/Computer.org/Journal%20templates/transactions_art_guide.pdf

    assert(columns in [1,2])

    if fig_width is None:
        fig_width = 4.89 if columns==1 else 6.9 # width in inches

    if fig_height is None:
        golden_mean = (sqrt(5)-1.0)/2.0    # Aesthetic ratio
        fig_height = fig_width*golden_mean # height in inches

    MAX_HEIGHT_INCHES = 24.0
    if fig_height > MAX_HEIGHT_INCHES:
        print("WARNING: fig_height too large:" + fig_height + 
              "so will reduce to" + MAX_HEIGHT_INCHES + "inches.")
        fig_height = MAX_HEIGHT_INCHES

    params = {'backend': 'ps',
              'text.latex.preamble': ['\usepackage{gensymb}'],
              'axes.labelsize': 8, # fontsize for x and y labels (was 10)
              'axes.titlesize': 8,
              'text.fontsize': 8, # was 10
              'legend.fontsize': 8, # was 10
              'xtick.labelsize': 10,
              'ytick.labelsize': 8,
              'text.usetex': True,
              'figure.figsize': [fig_width,fig_height],
              'font.family': 'serif'
    }

    matplotlib.rcParams.update(params)


def format_axes(ax):

    for spine in ['top', 'right']:
        ax.spines[spine].set_visible(False)

    for spine in ['left', 'bottom']:
        ax.spines[spine].set_color(SPINE_COLOR)
        ax.spines[spine].set_linewidth(0.5)

    ax.xaxis.set_ticks_position('bottom')
    ax.yaxis.set_ticks_position('left')

    for axis in [ax.xaxis, ax.yaxis]:
        axis.set_tick_params(direction='out', color=SPINE_COLOR)

    return ax

In [ ]:
for i in range(0, len((alpha > 0))):
    if((alpha > 0)[i]):
        print(dictionary_recal.columns[i])
        print(prediction)

In [ ]:
for i in range(0, len(dictionary.index)):
    print(calculate_probability(alpha, dictionary.index[i], dictionary_recal))
    print(dictionary.index[i])