def spec_iterative_pca(outfile, n_ev=10, n_iter=20, norm='L2'): """ This function takes the file outputted above, performs an iterative PCA to fill in the gaps, and appends the results to the same file. """ data_in = np.load(outfile) spectra = data_in['spectra'] mask = data_in['mask'] res = iterative_pca(spectra, mask, n_ev=n_ev, n_iter=n_iter, norm=norm, full_output=True) input_dict = {key: data_in[key] for key in data_in.files} # don't save the reconstructed spectrum: this can easily # be recomputed from the other parameters. input_dict['mu'] = res[1] input_dict['evecs'] = res[2] input_dict['evals'] = res[3] input_dict['norms'] = res[4] input_dict['coeffs'] = res[5] np.savez(outfile, **input_dict)
def spec_iterative_pca(outfile, n_ev=10, n_iter=20, norm='L2'): """ This function takes the file outputted above, performs an iterative PCA to fill in the gaps, and appends the results to the same file. """ data_in = np.load(outfile) spectra = data_in['spectra'] mask = data_in['mask'] res = iterative_pca(spectra, mask, n_ev=n_ev, n_iter=n_iter, norm=norm, full_output=True) input_dict = dict([(key, data_in[key]) for key in data_in.files]) # don't save the reconstructed spectrum: this can easily # be recomputed from the other parameters. input_dict['mu'] = res[1] input_dict['evecs'] = res[2] input_dict['evals'] = res[3] input_dict['norms'] = res[4] input_dict['coeffs'] = res[5] np.savez(outfile, **input_dict)
def test_iterative_PCA(n_samples=50, n_features=40): np.random.seed(0) # construct some data that is well-approximated # by two principal components x = np.linspace(0, np.pi, n_features) x0 = np.linspace(0, np.pi, n_samples) X = np.sin(x) * np.cos(0.5 * (x - x0[:, None])) # mask 10% of the pixels M = (np.random.random(X.shape) > 0.9) # reconstruct and check accuracy for norm in (None, 'L1', 'L2'): X_recons = iterative_pca(X, M, n_ev=2, n_iter=10, norm=norm) assert_array_almost_equal(X, X_recons, decimal=2)
def eigenspectra(data, output='spectra', wvrange=(6600,9000)): # data is a fits table (or similar) 2d array notnan = (data.LBIN[0] > wvrange[0]) & (data.LBIN[0] < wvrange[1]) N = len(data) n = len(data.LBIN[0][notnan]) smooth=True spectra = np.empty((N,n)) mask = np.empty((N,n)) for i in range(N): flx = data.SPECCALIB[i][notnan] wv = data.LBIN[i][notnan] if smooth: spec = Spectrum(wv,flx) spec.smooth(width='5', filtertype='gaussian', replace=True) flx = spec.flux wv = spec.x spectra[i,:] = flx mask[i,:] = (~np.isfinite(flx)) | (flx == 0) x_recon, mu, evecs, evals, norms, coeffs = iterative_pca(spectra, mask, n_ev=8, n_iter=15, norm='L2', full_output=True) evals_cs = (evals**2).cumsum() evals_cs /= evals_cs[-1] spec_mean = x_recon.mean(0) coeffs = np.empty((N,N)) for i, spec in enumerate(x_recon): cfs = np.dot(evecs, spec/norms[i] - mu) coeffs[:,i] = cfs if output == 'spectra': return x_recon, norms, notnan, mask elif output == 'components': return evecs, evals, coeffs