Commit e531a7ba authored by Somnath, Suhas's avatar Somnath, Suhas
Browse files

clean up of plot_utils. Removed redundant plotSpectrogram and plotBELoops...

clean up of plot_utils. Removed redundant plotSpectrogram and plotBELoops functions and generalized others.
parent b7847438
......@@ -18,7 +18,7 @@ from .fft import getNoiseFloor, noiseBandFilter, makeLPF, harmonicsPassFilter
from ..io.hdf_utils import getH5DsetRefs, getH5GroupRef, linkRefs
from ..io.io_utils import getTimeStamp
from ..io.microdata import MicroDataGroup, MicroDataset
from ..viz.plot_utils import rainbowPlot
from ..viz.plot_utils import rainbow_plot
###############################################################################
......@@ -125,7 +125,7 @@ def testFilter(resp_wfm, filter_parms, samp_rate, show_plots=True, rainbow_plot=
filt_data = np.real(np.fft.ifft(np.fft.ifftshift(F_pix_data)))
if show_loops:
if rainbow_plot:
rainbowPlot(ax_loops, excit_wfm[l_resp_ind:r_resp_ind], filt_data[l_resp_ind:r_resp_ind]*1E+3)
rainbow_plot(ax_loops, excit_wfm[l_resp_ind:r_resp_ind], filt_data[l_resp_ind:r_resp_ind] * 1E+3)
else:
ax_loops.plot(excit_wfm[l_resp_ind:r_resp_ind], filt_data[l_resp_ind:r_resp_ind]*1E+3)
ax_loops.set_title('AI vs AO')
......
......@@ -129,10 +129,8 @@ if __name__ == '__main__':
print 'Plotting Eigenvectors'
num_x = int(np.sqrt(h5_V.shape[1]))
for field in h5_V.dtype.names:
fig201, axes201 = plotSpectrograms(np.reshape(h5_V[:plot_comps, :][field],
[-1, num_x, num_x]),
num_comps=plot_comps,
title='Eigenvectors')
fig201, axes201 = plot_map_stack(np.reshape(h5_V[:plot_comps, :][field], [-1, num_x, num_x]),
num_comps=plot_comps, heading='Eigenvectors')
fig201.savefig(os.path.join(folder, svd_name+'_'+field+'_PCA_Eigenvectors.png'), format='png', dpi=300)
plt.close('all')
del fig201, axes201
......
......@@ -123,16 +123,17 @@ def discrete_cmap(num_bins, base_cmap=plt.cm.jet):
cmap_name = base.name + str(num_bins)
return base.from_list(cmap_name, color_list, num_bins)
def plotLoopFitNGuess(Vdc, ds_proj_loops, ds_guess, ds_fit, title=''):
'''
def plot_loop_guess_fit(vdc, ds_proj_loops, ds_guess, ds_fit, title=''):
"""
Plots the loop guess, fit, source projected loops for a single cycle
Parameters
----------
Vdc - 1D float numpy array
vdc - 1D float numpy array
DC offset vector (unshifted)
ds_proj_loops - 2D numpy array
Projected loops arranged as [position, Vdc]
Projected loops arranged as [position, vdc]
ds_guess - 1D compound numpy array
Loop guesses arranged as [position]
ds_fit - 1D compound numpy array
......@@ -146,17 +147,17 @@ def plotLoopFitNGuess(Vdc, ds_proj_loops, ds_guess, ds_fit, title=''):
Figure handle
axes - 2D array of matplotlib.pyplot.axis handles
handles to axes in the 2d figure
'''
shift_ind = int(-1 * len(Vdc) / 4)
Vdc_shifted = np.roll(Vdc, shift_ind)
"""
shift_ind = int(-1 * len(vdc) / 4)
vdc_shifted = np.roll(vdc, shift_ind)
num_plots = np.min([5, int(np.sqrt(ds_proj_loops.shape[0]))])
fig, axes = plt.subplots(nrows=num_plots, ncols=num_plots, figsize=(18, 18))
positions = np.linspace(0, ds_proj_loops.shape[0] - 1, num_plots ** 2, dtype=np.int)
for ax, pos in zip(axes.flat, positions):
ax.plot(Vdc, ds_proj_loops[pos, :], 'k', label='Raw')
ax.plot(Vdc_shifted, loop_fit_function(Vdc_shifted, np.array(list(ds_guess[pos]))), 'g', label='guess')
ax.plot(Vdc_shifted, loop_fit_function(Vdc_shifted, np.array(list(ds_fit[pos]))), 'r--', label='Fit')
ax.plot(vdc, ds_proj_loops[pos, :], 'k', label='Raw')
ax.plot(vdc_shifted, loop_fit_function(vdc_shifted, np.array(list(ds_guess[pos]))), 'g', label='guess')
ax.plot(vdc_shifted, loop_fit_function(vdc_shifted, np.array(list(ds_fit[pos]))), 'r--', label='Fit')
ax.set_xlabel('V_DC (V)')
ax.set_ylabel('PR (a.u.)')
ax.set_title('Loop ' + str(pos))
......@@ -169,7 +170,7 @@ def plotLoopFitNGuess(Vdc, ds_proj_loops, ds_guess, ds_fit, title=''):
###############################################################################
def rainbowPlot(ax, ao_vec, ai_vec, num_steps=32, cmap=plt.cm.jet, **kwargs):
def rainbow_plot(ax, ao_vec, ai_vec, num_steps=32, cmap=plt.cm.jet, **kwargs):
"""
Plots the input against the output waveform (typically loops).
The color of the curve changes as a function of time using the jet colorscheme
......@@ -327,7 +328,7 @@ def plot_loops(excit_wfm, h5_loops, h5_pos=None, central_resp_size=None,
for count, posn in enumerate(chosen_pos):
if rainbow_plot:
rainbowPlot(axes_lin[count], excit_wfm[l_resp_ind:r_resp_ind], h5_loops[posn, l_resp_ind:r_resp_ind])
rainbow_plot(axes_lin[count], excit_wfm[l_resp_ind:r_resp_ind], h5_loops[posn, l_resp_ind:r_resp_ind])
else:
axes_lin[count].plot(excit_wfm[l_resp_ind:r_resp_ind], h5_loops[posn, l_resp_ind:r_resp_ind])
......@@ -349,72 +350,17 @@ def plot_loops(excit_wfm, h5_loops, h5_pos=None, central_resp_size=None,
plt.tight_layout()
return fig, axes
def plotSpectrograms(eigenvectors, num_comps=4, title='Eigenvectors', xlabel='Step', stdevs=2,
show_colorbar=True):
# TODO: use plot_map_stack instead of plotSpectrograms
"""
Plots the provided spectrograms from SVD V vector
Parameters:
-------------
eigenvectors : 3D numpy complex matrices
Eigenvectors rearranged as - [row, col, component]
xaxis : 1D real numpy array
The vector to plot against
num_comps : int
Number of components to plot
title : String
Title to plot above everything else
xlabel : String
Label for x axis
stdevs : int
Number of standard deviations to consider for plotting
Returns:
---------
fig, axes
"""
import matplotlib.pyplot as plt
fig_h, fig_w = (4, 4 + show_colorbar * 1.00)
p_rows = int(np.ceil(np.sqrt(num_comps)))
p_cols = int(np.floor(num_comps / p_rows))
fig201, axes201 = plt.subplots(p_rows, p_cols, figsize=(p_cols * fig_w, p_rows * fig_h))
fig201.subplots_adjust(hspace=0.4, wspace=0.4)
fig201.canvas.set_window_title(title)
for index in range(num_comps):
cur_map = np.transpose(eigenvectors[index, :, :])
ax = axes201.flat[index]
mean = np.mean(cur_map)
std = np.std(cur_map)
ax.imshow(cur_map, cmap='jet',
vmin=mean - stdevs * std,
vmax=mean + stdevs * std)
ax.set_title('Eigenvector: %d' % (index + 1))
ax.set_aspect('auto')
ax.set_xlabel(xlabel)
ax.axis('tight')
return fig201, axes201
###############################################################################
def plotBEspectrograms(eigenvectors, num_comps=4, title='Eigenvectors', xlabel='UDVS Step', stdevs=2):
def plot_complex_map_stack(map_stack, num_comps=4, title='Eigenvectors', xlabel='UDVS Step', stdevs=2):
"""
Plots the provided spectrograms from SVD V vector
Parameters:
-------------
eigenvectors : 3D numpy complex matrices
map_stack : 3D numpy complex matrices
Eigenvectors rearranged as - [row, col, component]
xaxis : 1D real numpy array
The vector to plot against
num_comps : int
Number of components to plot
title : String
......@@ -433,7 +379,7 @@ def plotBEspectrograms(eigenvectors, num_comps=4, title='Eigenvectors', xlabel='
fig201.canvas.set_window_title(title)
for index in range(num_comps):
cur_map = np.transpose(eigenvectors[index, :, :])
cur_map = np.transpose(map_stack[index, :, :])
axes = [axes201.flat[index], axes201.flat[index + num_comps]]
funcs = [np.abs, np.angle]
labels = ['Amplitude', 'Phase']
......@@ -452,23 +398,21 @@ def plotBEspectrograms(eigenvectors, num_comps=4, title='Eigenvectors', xlabel='
###############################################################################
def plotBEeigenvectors(eigenvectors, num_comps=4, xlabel=''):
def plot_complex_loop_stack(loop_stack, x_axis, heading='BE Loops', subtitle='Eigenvector', num_comps=4, x_label=''):
"""
Plots the provided spectrograms from SVD V vector
Parameters:
-------------
eigenvectors : 3D numpy complex matrices
Eigenvectors rearranged as - [row, col, component]
xaxis : 1D real numpy array
loop_stack : 3D numpy complex matrices
Loops rearranged as - [component, points]
x_axis : 1D real numpy array
The vector to plot against
num_comps : int
Number of components to plot
title : String
Title to plot above everything else
xlabel : String
x_label : String
Label for x axis
stdevs : int
Number of standard deviations to consider for plotting
......@@ -482,65 +426,21 @@ def plotBEeigenvectors(eigenvectors, num_comps=4, xlabel=''):
fig201, axes201 = plt.subplots(len(funcs), num_comps, figsize=(num_comps * 4, 4 * len(funcs)))
fig201.subplots_adjust(hspace=0.4, wspace=0.4)
fig201.canvas.set_window_title("Eigenvectors")
fig201.canvas.set_window_title(heading)
for index in range(num_comps):
cur_map = eigenvectors[index, :]
cur_map = loop_stack[index, :]
axes = [axes201.flat[index], axes201.flat[index + num_comps]]
for func, lab, ax in zip(funcs, labels, axes):
ax.plot(func(cur_map))
ax.set_title('Eigenvector: %d - %s' % (index + 1, lab))
ax.set_xlabel(xlabel)
ax.plot(x_axis, func(cur_map))
ax.set_title('%s: %d - %s' % (subtitle, index + 1, lab))
ax.set_xlabel(x_label)
fig201.tight_layout()
return fig201, axes201
###############################################################################
def plotBELoops(xaxis, xlabel, amp_mat, phase_mat, num_comps, title=None):
"""
Plots the provided loops from the SHO. Replace / merge with function in BESHOUtils
Parameters:
-------------
xaxis : 1D real numpy array
The vector to plot against
xlabel : string
Label for x axis
amp_mat : 2D real numpy array
Amplitude matrix arranged as [points, component]
phase_mat : 2D real numpy array
Phase matrix arranged as [points, component]
num_comps : int
Number of components to plot
title : String
Title to plot above everything else
Returns:
---------
fig, axes
"""
fig201, axes201 = plt.subplots(2, num_comps, figsize=(4 * num_comps, 6))
fig201.subplots_adjust(hspace=0.4, wspace=0.4)
fig201.canvas.set_window_title(title)
for index in range(num_comps):
axes = [axes201.flat[index], axes201.flat[index + num_comps]]
resp_vecs = [amp_mat[index, :], phase_mat[index, :]]
resp_titles = ['Amplitude', 'Phase']
for ax, resp, titl in zip(axes, resp_vecs, resp_titles):
ax.plot(xaxis, resp)
ax.set_title('%s %d' % (titl, index + 1))
ax.set_aspect('auto')
ax.set_xlabel(xlabel)
fig201.tight_layout()
return fig201, axes201
###############################################################################
def plotScree(scree, title='Scree'):
"""
......@@ -694,7 +594,7 @@ def plotClusterResults(label_mat, mean_response, spec_val=None, cmap=plt.cm.jet,
spec_label='Spectroscopic Value', resp_label='Response',
pos_labels=('X', 'Y'), pos_ticks=None):
"""
Plot the cluster labels and mean response for each cluster
Plot the cluster labels and mean response for each cluster in separate plots
Parameters
----------
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment