Last active
July 3, 2017 15:56
-
-
Save FilipDominec/ebf75043a5641b3d849e951fa91a8d74 to your computer and use it in GitHub Desktop.
similar to previous gist, but draws 2D maps for each SVD components - good for analysis of 2D spectral mapping of samples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#plotstyle = "basis" | |
plotstyle = "amplitude" | |
decimatex = 1 | |
ncomponents = 6 | |
a = ys[:, ::decimatex] | |
xs = xs[:, ::decimatex] | |
U, s, V = np.linalg.svd(a, full_matrices=False) | |
if plotstyle=="basis": | |
for x, y, label in zip(xs[:ncomponents], V[:ncomponents], range(ncomponents)): | |
ax.plot(x, y, label="SVD component %d" % (label+1), lw=5*.6**label) | |
ax.set_xlabel('wavelength (nm)') | |
ax.set_ylabel('spectral intensity') | |
ax.legend(loc='best', prop={'size':10}) | |
np.savetxt('svd-basis.txt', np.vstack([xs[0], V[:ncomponents]]).T, fmt="%.6f") | |
if plotstyle=="amplitude": | |
print(U.shape, s.shape) | |
res = (np.dot(U,np.diag(s)).T) | |
## assume the spectra were measured in a square NxN pattern: | |
dim = int(len(res)**.5) | |
subplotx,subploty=3,2 | |
fig.delaxes(ax) | |
axs = fig.subplots(subploty,subplotx) | |
axs = [ax for axx in axs for ax in axx] ## flatten | |
print(enumerate(axs), axs) | |
for compn,ax in enumerate(axs): | |
print(compn) | |
print(res[compn]) | |
component = res[compn].reshape(dim,dim) | |
x,y = np.arange(dim), np.arange(dim)[::-1] | |
levels = np.linspace(np.min(component), np.max(component), 50) | |
ax.contourf(x,y,component[::1][::-1], levels=levels, extend='both', cmap=cm.jet) | |
ax.contour(x,y,component[::1][::-1], levels=levels, cmap=cm.jet) # avoid white stripes | |
ax.set_xlim(x[0], x[-1]) | |
ax.set_ylim(y[0], y[-1]) | |
ax.set_title('SVD coefficient {}'.format(compn+1)) | |
np.savetxt('svd-map-of-coefficient{}.txt'.format(compn), (component).T, fmt="%.6f") | |
np.savetxt('svd-map-of-coefficient{}_yflip.txt'.format(compn), (component[:][::-1]).T, fmt="%.6f") | |
np.savetxt('svd-map-of-coefficient{}_xyflip.txt'.format(compn), component, fmt="%.6f") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment