<jupyter_start><jupyter_text>Embed grid of 10,000 FSPS spectra<jupyter_code># imports and plotting setup
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline
from astroML.plotting import setup_text_plots
import h5py

np.set_printoptions(suppress=True)
setup_text_plots(fontsize=16)
mpl.rc('xtick', labelsize=14)
mpl.rc('ytick', labelsize=14)
mpl.rc('font', size=16, family='serif', style='normal', variant='normal', stretch='normal', weight='bold')
mpl.rc('legend', labelspacing=0.1, handlelength=2, fontsize=10)
mpl.rc('axes', labelweight='black')
# code from megaman_spectra.py 

from megaman.geometry import Geometry
from megaman.embedding import SpectralEmbedding
from scipy.io import mmwrite
from scipy.sparse.csgraph import connected_components

f = h5py.File('../data/1e4_model_spectra.hdf5','r')
spectra = f['spectra'][:]
wave = f['wave'][:]
taus = f['taus'][:]
ages = f['ages'][:]
zs = f['zs'][:]
dusts = f['dusts'][:]
sigmas = f['sigmas'][:]
f.close()
radius = 1.05 # reduced from original 35000 to run on narrower spectral range/normalized data
adjacency_method = 'cyflann'
cyflann_kwds = {'index_type':'kdtrees', 'num_trees':100}
adjacency_kwds = {'radius':radius, 'cyflann_kwds':cyflann_kwds}
affinity_method = 'gaussian'
affinity_kwds = {'radius':radius}

geom = Geometry(adjacency_method=adjacency_method, adjacency_kwds=adjacency_kwds, 
                affinity_method=affinity_method, affinity_kwds=affinity_kwds)

geom.set_data_matrix(spectra)
adjacency_matrix = geom.compute_adjacency_matrix()
affinity_matrix = geom.compute_affinity_matrix()

new_radius = np.max(adjacency_matrix.data)

n, labels = connected_components(affinity_matrix, directed=False, return_labels=True)
print "Number of connected components: ", n
wh = np.where(labels > 0)
#geom.X = np.delete(geom.X, wh, 0) 
#
#geom.affinity_matrix = np.delete(geom.affinity_matrix, wh, 0)
#geom.affinity_matrix = np.delete(geom.affinity_matrix, wh, 1)

affinity_kwds = {'radius':new_radius}
laplacian_method = 'geometric'
laplacian_kwds = {'scaling_epps':new_radius}
geom.affinity_kwds = affinity_kwds
geom.laplacian_method = laplacian_method
geom.laplacian_kwds = laplacian_kwds

spectral = SpectralEmbedding(n_components=3, eigen_solver='amg', drop_first=True, geom=geom)
embed = spectral.fit_transform(geom.X)

mmwrite('embed3d_1e4_model_spectra.mtx', embed)
def plot_embed(color, label, axlim=True):
    fig, ax = plt.subplots(1,2, figsize=(10,4))
    
    a = ax[0].scatter(embed[:,0], embed[:,1], c=color, lw=0, s=5, cmap='viridis')
    plt.colorbar(a, ax=ax[0], label=label)
    ax[0].set_xlabel('dim0')
    ax[0].set_ylabel('dim1')

    b = ax[1].scatter(embed[:,2], embed[:,1], c=color, lw=0, s=5, cmap='viridis')
    plt.colorbar(b, ax=ax[1], label=label)
    ax[1].set_xlabel('dim2')
    ax[1].set_ylabel('dim1')
    
    if axlim:
        ax[0].set_xlim([-0.03,0.02])
        ax[0].set_ylim([-0.03, 0.02])
        ax[1].set_xlim([-0.03,0.02])
        ax[1].set_ylim([-0.03, 0.02])
        
    plt.tight_layout()<jupyter_output><empty_output><jupyter_text>Plots here show result for r=1.05<jupyter_code>plot_embed(ages, 'Mean Stellar Age(Gyr)', axlim=True)
plot_embed(zs, r'$\log(Z/Z_\odot)$', axlim=True)
plot_embed(dusts, 'Dust Attenuation Old Stars', axlim=True)
plot_embed(sigmas, 'Broadening (km/s)', axlim=True)<jupyter_output><empty_output><jupyter_text>Plots below show result for r=1.14, smallest r that gives 1 connected component<jupyter_code>plot_embed(ages, 'Mean Stellar Age(Gyr)', axlim=False)
plot_embed(zs, r'$\log(Z/Z_\odot)$', axlim=False)
plot_embed(dusts, 'Dust Attenuation Old Stars', axlim=False)
plot_embed(sigmas, 'Broadening (km/s)', axlim=False)<jupyter_output><empty_output><jupyter_text>Investigating the disconnected components<jupyter_code>hmm = np.where(labels > 0)[0][:]
np.shape(hmm)
for ii in hmm:
    print zip(ages, zs, dusts, sigmas)[ii]
np.unique(ages)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15468354430379747, 'fraction_numerical': 0.026329113924050632, 'mean_word_length': 6.440677966101695, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '386936', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1631, 'n_tokens_neox': 1513, 'n_words': 277, 'path': 'code/EmbedBigGrid.ipynb', 'repo_name': 'ogtelford/gridmodels', 'size': '3950'}