In [1]:
# Import numpy
import numpy as np

# Importing plotting stuff
import matplotlib.pyplot as plt
%matplotlib inline
from scipy import stats

# Use seaborn plotting style defaults
#import seaborn as sns; sns.set()

# Import Astropy things we need
from astropy.io import ascii

import astropy.coordinates as coord
import astropy.units as u
from astropy.table import Table

#Import Pandas
import pandas as pd

# Importing Principal Component Analysis
from sklearn.decomposition import PCA

In [2]:
# Query OGLE-III database for Type II Cepheids: http://ogledb.astrouw.edu.pl/~ogle/CVS/
data = ascii.read('AllTypeIICepheidsinOGLE.txt') # All of the Type II Cepheids in the OGLE Survey
data


Out[2]:
<Table masked=True length=603>
IDFieldStarIDRADeclTypeIVP_1dP_1T0_1A_1R21_1phi21_1R31_1phi31_1
str18str8int64str12str11str5float64float64float64float64float64float64float64float64float64float64
OGLE-LMC-T2CEP-001LMC154.149674:38:15.33-68:15:32.0BLHer17.73418.4521.81395226.3e-062162.790060.3480.0674.8520.1942.252
OGLE-LMC-T2CEP-002LMC148.31314:42:33.95-68:37:03.7WVir15.71116.63218.32354580.00073872158.395030.90.1266.1390.0394.172
OGLE-LMC-T2CEP-003LMC145.632724:43:05.54-66:49:36.1RVTau14.16614.95335.65992980.00834192142.865670.4220.1625.8690.0675.32
OGLE-LMC-T2CEP-004LMC147.285964:44:43.64-68:05:21.3BLHer17.61218.1241.91601772.3e-062170.181290.8160.4824.6640.2852.273
OGLE-LMC-T2CEP-005LMC143.2100814:48:08.33-69:51:14.2RVTau14.73915.79633.18532680.00291842170.33970.8610.0766.2770.0122.917
OGLE-LMC-T2CEP-006LMC139.34124:49:48.87-67:25:20.6BLHer18.03718.5131.08792432.4e-062171.555160.4790.244.6080.1082.902
OGLE-LMC-T2CEP-007LMC135.7212764:50:50.43-69:19:23.7BLHer18.00518.5971.24264213.3e-062166.637170.4930.2654.3790.064.11
OGLE-LMC-T2CEP-008LMC136.7138934:51:11.51-69:57:27.0BLHer17.84218.5851.74609896.7e-062165.203690.5360.2075.4380.1382.724
OGLE-LMC-T2CEP-009LMC131.85584:52:40.07-67:09:24.3BLHer17.76218.3791.76134712.7e-062166.421980.730.4444.5820.2451.898
OGLE-LMC-T2CEP-010LMC134.3207924:54:10.61-68:33:26.8BLHer17.97918.6331.50296353.6e-062165.076070.620.3284.3960.0620.569
................................................
OGLE-BLG-T2CEP-348BLG205.66422617:57:14.02-28:50:56.1RVTau15.81318.55936.324640.00807975020.414080.3160.1690.480.1280.077
OGLE-BLG-T2CEP-349BLG214.19780817:58:42.30-28:32:10.1RVTau13.14615.25451.470410.02045185014.429210.3640.0614.7640.1331.91
OGLE-BLG-T2CEP-350BLG215.617841918:00:02.03-28:11:18.0RVTau14.15816.31243.60475260.04426395040.146020.5770.1386.0910.060.446
OGLE-BLG-T2CEP-351BLG241.513167118:04:59.19-26:12:31.9RVTau13.36416.00944.12367510.03204155015.599480.4620.0766.1510.1794.478
OGLE-BLG-T2CEP-352BLG217.510832218:05:06.31-28:05:54.9RVTau12.15414.23152.17926710.07683055029.782360.2340.4584.9320.0260.476
OGLE-BLG-T2CEP-353BLG249.45125818:06:08.88-25:41:57.2RVTau14.56817.24728.97390090.00957855010.548530.2740.0775.470.022.173
OGLE-BLG-T2CEP-354BLG236.517402018:09:34.10-26:49:10.5RVTau13.216.14733.23051810.00503555000.783240.4840.1635.6620.0545.144
OGLE-BLG-T2CEP-355BLG237.17171018:13:42.12-27:20:20.5RVTau12.5713.96221.42677460.00859915012.963630.7250.0790.3080.0642.032
OGLE-BLG-T2CEP-356BLG256.59497718:23:46.87-25:40:20.5RVTau13.44614.88935.47811550.0363265023.710380.7560.1185.8860.060.446
OGLE-BLG-T2CEP-357BLG264.510282018:25:24.49-25:02:06.1RVTau12.23213.44858.48035290.44363615006.47670.7530.1974.4570.2825.253

In [3]:
# Getting all Type II Cepheids coordinates in OGLE
ra = coord.Angle(data['RA'], unit=u.hour)
ra.degree
ra = ra.wrap_at(180*u.degree)
dec = coord.Angle(data['Decl'], unit=u.degree)
dec.degree

# Plotting all Cepheids
fig = plt.figure(figsize=(8,6))
ax = fig.add_subplot(111, projection="mollweide")
ax.scatter(ra.radian, dec.radian)


Out[3]:
<matplotlib.collections.PathCollection at 0x7f0cfa6fddd0>

In [4]:
# Dec, RA in hours.
fig = plt.figure(figsize=(8,6))
ax = fig.add_subplot(111, projection="mollweide")
ax.scatter(ra.radian, dec.radian)
ax.set_xticklabels(['14h','16h','18h','20h','22h','0h','2h','4h','6h','8h','10h'])
ax.grid(True)



In [5]:
# Save map of Type IIs in LMC, SMC, Bulge in pdf.
fig.savefig("TypeIICepheidsMap.pdf")

In [6]:
# Put Period and I-band mean in arrays.
#P = data['P_1']
#I_mean = data['I']
#V_mean = data['V']
#labels = data['Type']

plt.clf()

def find_unique_elements(arr):
    elements_list = []
    elements_list.append(arr[0])
    for i in range(1,len(arr)):
        if arr[i] not in elements_list:
            elements_list.append(arr[i])
    return elements_list

#number_list = [1, 5, 0, 3, 3]
label_list = find_unique_elements(data['Type'])
label_list


Out[6]:
['BLHer', 'WVir', 'RVTau', 'pWVir']
<matplotlib.figure.Figure at 0x7f0cfa185890>

In [7]:
colors = plt.cm.jet(np.linspace(0,1,len(label_list)))


# Plot Period and I_mean.
for j in range(len(label_list)):
    #index = np.where(np.asarray(label_list) == labels)[0]
    plt.semilogx(data['P_1'][data['Type'] == label_list[j]], data['I'][data['Type'] == label_list[j]], label=label_list[j],\
                 linestyle='None', marker='o', color=colors[j])
    plt.title("LMC, SMC & Bulge Type II Cepheids in OGLE-III")
    plt.ylabel("I Magnitude")
    plt.xlabel("log(P)")
    plt.legend(loc='best')

plt.gca().invert_yaxis()
plt.show


Out[7]:
<function matplotlib.pyplot.show>

In [8]:
# Putting Period and I_mag in same array.
P_I = np.array([data['P_1'], data['I']]).T

In [10]:
# Using 2 PCA Componenets to fit P_I_array.
pca = PCA(n_components=2)
pca.fit(P_I)
print(pca.explained_variance_)
print(pca.components_)


[ 191.27598047    2.05518509]
[[ 0.99826687 -0.05884942]
 [ 0.05884942  0.99826687]]

In [15]:
from sklearn.cluster import KMeans
est = KMeans(4)  # 4 clusters
est.fit(P_I)
y_kmeans = est.predict(P_I)
plt.scatter(P_I[:, 0], P_I[:, 1], c=y_kmeans, s=50, cmap='rainbow')
x



In [32]:
labels = "pWVir"
len(data['P_1'][data['Type'] == labels])


Out[32]:
24

In [43]:
print np.where(np.asarray(label_list) == 'BLHer')
ex = np.array([0, 1, 2])

print np.where(ex == 1)


(array([0]),)
(array([1]),)

In [38]:
print label_list


['BLHer', 'WVir', 'RVTau', 'pWVir']

In [ ]: