plot_utils.py 46.4 KB
Newer Older
Somnath, Suhas's avatar
Somnath, Suhas committed
1
2
3
4
5
6
# -*- coding: utf-8 -*-
"""
Created on Thu May 05 13:29:12 2016

@author: Suhas Somnath
"""
Chris Smith's avatar
Chris Smith committed
7
8
# TODO: All general plotting functions should support data with 1 or 2 spatial dimensions.

Chris Smith's avatar
merged    
Chris Smith committed
9
from __future__ import division # int/int = float
10
from warnings import warn
11
import os
Chris Smith's avatar
merged    
Chris Smith committed
12
import h5py
13
import scipy
14
import matplotlib.pyplot as plt
15
from matplotlib.colors import LinearSegmentedColormap
16
from mpl_toolkits.axes_grid1 import ImageGrid
17
import numpy as np
18
from ..analysis.utils.be_loop import loop_fit_function
19
from ..io.hdf_utils import reshape_to_Ndims, get_formatted_labels
20

21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
def set_tick_font_size(axes, font_size):
    """
    Sets the font size of the ticks in the provided axes

    Parameters
    ----------
    axes : matplotlib.pyplot.axis object or list of axis objects
        axes to set font sizes
    font_size : unigned int
        Font size
    """

    def __set_axis_tick(axis):
        """
        Sets the font sizes to the x and y axis in the given axis object

        Parameters
        ----------
        axis : matplotlib.pyplot.axis object
            axis to set font sizes
        """
        for tick in axis.xaxis.get_major_ticks():
            tick.label.set_fontsize(font_size)
        for tick in axis.yaxis.get_major_ticks():
            tick.label.set_fontsize(font_size)

    if hasattr(axes, '__iter__'):
        for axis in axes:
            __set_axis_tick(axis)
    else:
        __set_axis_tick(axes)

53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
def cmap_jet_white_center():
    """
    Generates the jet colormap with a white center

    Returns
    -------
    white_jet : matplotlib.colors.LinearSegmentedColormap object
        color map object that can be used in place of plt.cm.jet
    """
    # For red - central column is like brightness
    # For blue - last column is like brightness
    cdict = {'red': ((0.00, 0.0, 0.0),
                     (0.30, 0.0, 0.0),
                     (0.50, 1.0, 1.0),
                     (0.90, 1.0, 1.0),
                     (1.00, 0.5, 1.0)),
             'green': ((0.00, 0.0, 0.0),
                       (0.10, 0.0, 0.0),
                       (0.42, 1.0, 1.0),
                       (0.58, 1.0, 1.0),
                       (0.90, 0.0, 0.0),
                       (1.00, 0.0, 0.0)),
             'blue': ((0.00, 0.0, 0.5),
                      (0.10, 1.0, 1.0),
                      (0.50, 1.0, 1.0),
                      (0.70, 0.0, 0.0),
                      (1.00, 0.0, 0.0))
             }
    return LinearSegmentedColormap('white_jet', cdict)
82

Chris Smith's avatar
Chris Smith committed
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
def cmap_hot_desaturated():
    hot_desaturated = [(1, (255, 76, 76, 255)),
                       (0.857, (107, 0, 0, 255)),
                       (0.714, (255, 96, 0, 255)),
                       (0.571, (255, 255, 0, 255)),
                       (0.429, (0, 127, 0, 255)),
                       (0.285, (0, 255, 255, 255)),
                       (0.143, (0, 0, 91, 255)),
                       (0, (71, 71, 219, 255))]

    cdict = {'red': tuple([(dist, colors[0]/255.0, colors[0]/255.0) for (dist, colors) in hot_desaturated][::-1]),
             'green': tuple([(dist, colors[1]/255.0, colors[1]/255.0) for (dist, colors) in hot_desaturated][::-1]),
             'blue': tuple([(dist, colors[2]/255.0, colors[2]/255.0) for (dist, colors) in hot_desaturated][::-1])}

    return LinearSegmentedColormap('hot_desaturated', cdict)



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
def discrete_cmap(num_bins, base_cmap=plt.cm.jet):
    """
    Create an N-bin discrete colormap from the specified input map

    Parameters
    ----------
    num_bins : unsigned int
        Number of discrete bins
    base_cmap : matplotlib.colors.LinearSegmentedColormap object
        Base color map to discretize

    Returns
    -------
    new_cmap : matplotlib.colors.LinearSegmentedColormap object
        Discretized color map

    Credits
    -------
    Jake VanderPlas
    License: BSD-style
    """

    base = plt.cm.get_cmap(base_cmap)
    color_list = base(np.linspace(0, 1, num_bins))
    cmap_name = base.name + str(num_bins)
    return base.from_list(cmap_name, color_list, num_bins)

128
129
130

def plot_loop_guess_fit(vdc, ds_proj_loops, ds_guess, ds_fit, title=''):
    """
131
132
133
134
    Plots the loop guess, fit, source projected loops for a single cycle

    Parameters
    ----------
135
    vdc - 1D float numpy array
136
137
        DC offset vector (unshifted)
    ds_proj_loops - 2D numpy array
138
        Projected loops arranged as [position, vdc]
139
140
141
142
143
144
145
146
147
148
149
150
151
    ds_guess - 1D compound numpy array
        Loop guesses arranged as [position]
    ds_fit - 1D compound numpy array
        Loop fits arranged as [position]
    title - (Optional) String / unicode
        Title for the figure

    Returns
    ----------
    fig - matplotlib.pyplot.figure object
        Figure handle
    axes - 2D array of matplotlib.pyplot.axis handles
        handles to axes in the 2d figure
152
153
154
    """
    shift_ind = int(-1 * len(vdc) / 4)
    vdc_shifted = np.roll(vdc, shift_ind)
155
156
157
158
159

    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):
160
161
162
        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')
163
164
165
166
167
168
169
170
        ax.set_xlabel('V_DC (V)')
        ax.set_ylabel('PR (a.u.)')
        ax.set_title('Loop ' + str(pos))
    ax.legend()
    fig.suptitle(title)
    fig.tight_layout()

    return fig, axes
Somnath, Suhas's avatar
Somnath, Suhas committed
171
172
173

###############################################################################

174

175
def rainbow_plot(ax, ao_vec, ai_vec, num_steps=32, cmap=plt.cm.jet, **kwargs):
Somnath, Suhas's avatar
Somnath, Suhas committed
176
177
178
179
    """
    Plots the input against the output waveform (typically loops).
    The color of the curve changes as a function of time using the jet colorscheme

180
181
    Parameters
    ----------
Somnath, Suhas's avatar
Somnath, Suhas committed
182
183
184
185
186
187
188
189
    ax : axis handle
        Axis to plot the curve
    ao_vec : 1D float numpy array
        vector that forms the X axis
    ai_vec : 1D float numpy array
        vector that forms the Y axis
    num_steps : unsigned int (Optional)
        Number of discrete color steps
190
191
    cmap : matplotlib.colors.LinearSegmentedColormap object
        Colormap to be used
Somnath, Suhas's avatar
Somnath, Suhas committed
192
193
    """
    pts_per_step = int(len(ai_vec) / num_steps)
194
    for step in range(num_steps - 1):
Somnath, Suhas's avatar
Somnath, Suhas committed
195
196
        ax.plot(ao_vec[step * pts_per_step:(step + 1) * pts_per_step],
                ai_vec[step * pts_per_step:(step + 1) * pts_per_step],
197
                color=cmap(255 * step / num_steps), **kwargs)
Somnath, Suhas's avatar
Somnath, Suhas committed
198
199
200
    # plot the remainder:
    ax.plot(ao_vec[(num_steps - 1) * pts_per_step:],
            ai_vec[(num_steps - 1) * pts_per_step:],
201
            color=cmap(255 * num_steps / num_steps), **kwargs)
Somnath, Suhas's avatar
Somnath, Suhas committed
202
203
204
205
206
    """
    CS3=plt.contourf([[0,0],[0,0]], range(0,310),cmap=plt.cm.jet)
    fig.colorbar(CS3)"""


207
208
def plot_line_family(axis, x_axis, line_family, line_names=None, label_prefix='Line', label_suffix='', cmap=plt.cm.jet,
                     **kwargs):
209
210
211
212
213
    """
    Plots a family of lines with a sequence of colors

    Parameters
    ----------
214
    axis : axis handle
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
        Axis to plot the curve
    x_axis : array-like
        Values to plot against
    line_family : 2D numpy array
        family of curves arranged as [curve_index, features]
    line_names : array-like
        array of string or numbers that represent the identity of each curve in the family
    label_prefix : string / unicode
        prefix for the legend (before the index of the curve)
    label_suffix : string / unicode
        suffix for the legend (after the index of the curve)
    cmap : matplotlib.colors.LinearSegmentedColormap object
        Colormap to be used
    """
    num_lines = line_family.shape[0]

    if line_names is None:
        line_names = ['{} {} {}'.format(label_prefix, line_ind, label_suffix) for line_ind in range(num_lines)]
    else:
        if len(line_names) != num_lines:
            warn('Line names of different length compared to provided dataset')
            line_names = ['{} {} {}'.format(label_prefix, line_ind, label_suffix) for line_ind in range(num_lines)]

238
    for line_ind in range(num_lines):
239
240
241
        axis.plot(x_axis, line_family[line_ind],
                  label=line_names[line_ind],
                  color=cmap(int(255 * line_ind / (num_lines - 1))), **kwargs)
242
243


244
def plot_map(axis, data, stdevs=2, **kwargs):
245
246
247
248
249
250
251
252
253
254
255
256
    """
    Plots a 2d map with a tight z axis, with or without color bars.
    Note that the direction of the y axis is flipped if the color bar is required

    Parameters
    ----------
    axis : matplotlib.pyplot.axis object
        Axis to plot this map onto
    data : 2D real numpy array
        Data to be plotted
    stdevs : unsigned int (Optional. Default = 2)
        Number of standard deviations to consider for plotting
257

258
259
260
261
262
    Returns
    -------
    """
    data_mean = np.mean(data)
    data_std = np.std(data)
263
    origin = kwargs.pop('origin', 'lower')
264
265
266
    im = axis.imshow(data, interpolation='none',
                     vmin=data_mean - stdevs * data_std,
                     vmax=data_mean + stdevs * data_std,
267
                     origin=origin,
268
                     **kwargs)
269
270
    axis.set_aspect('auto')

271
    return im
272

273

274
275
def plot_loops(excit_wfm, datasets, line_colors=[], dataset_names=[], evenly_spaced=True, plots_on_side=5, x_label='',
               y_label='', subtitles='Position', title='', central_resp_size=None, use_rainbow_plots=False, h5_pos=None):
Somnath, Suhas's avatar
Somnath, Suhas committed
276
    """
277
    Plots loops from multiple datasets from up to 25 evenly spaced positions
Somnath, Suhas's avatar
Somnath, Suhas committed
278
279
280
281
282

    Parameters
    -----------
    excit_wfm : 1D numpy float array
        Excitation waveform in the time domain
283
284
285
286
287
288
    datasets : list of 2D numpy arrays or 2D hyp5.Dataset objects
        Datasets containing data arranged as (pixel, time)
    line_colors : list of strings
        Colors to be used for each of the datasets
    dataset_names : (Optional) list of strings
        Names of the different datasets to be compared
Somnath, Suhas's avatar
Somnath, Suhas committed
289
290
291
292
293
294
295
296
    h5_pos : HDF5 dataset reference or 2D numpy array
        Dataset containing position indices
    central_resp_size : (optional) unsigned integer
        Number of responce sample points from the center of the waveform to show in plots. Useful for SPORC
    evenly_spaced : boolean
        Evenly spaced positions or first N positions
    plots_on_side : unsigned int
        Number of plots on each side
297
    use_rainbow_plots : (optional) Boolean
Somnath, Suhas's avatar
Somnath, Suhas committed
298
299
300
301
302
303
304
305
306
307
308
309
310
311
        Plot the lines as a function of spectral index (eg. time)
    x_label : (optional) String
        X Label for all plots
    y_label : (optional) String
        Y label for all plots
    subtitles : (optional) String
        prefix for title over each plot
    title : (optional) String
        Main plot title

    Returns
    ---------
    fig, axes
    """
312
    if type(datasets) in [h5py.Dataset, np.ndarray]:
313
314
315
        # can be numpy array or h5py.dataset
        num_pos = datasets.shape[0]
        num_points = datasets.shape[1]
Somnath, Suhas's avatar
Somnath, Suhas committed
316
        datasets = [datasets]
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
        line_colors = ['b']
        dataset_names = ['Default']
    else:
        # First check if the datasets are correctly shaped:
        num_pos_es = list()
        num_points_es = list()
        for dataset in datasets:
            num_pos_es.append(dataset.shape[0])
            num_points_es.append(dataset.shape[1])
        num_pos_es = np.array(num_pos_es)
        num_points_es = np.array(num_points_es)
        if np.unique(num_pos_es).size > 1 or np.unique(num_points_es).size > 1:
            warn('Datasets of incompatible sizes')
            return
        num_pos = np.unique(num_pos_es)[0]
        num_points = np.unique(num_points_es)[0]

        # Next the identification of datasets:
        if len(dataset_names) > len(datasets):
            # remove additional titles
            dataset_names = dataset_names[:len(datasets)]
        elif len(dataset_names) < len(datasets):
            # add titles
            dataset_names = dataset_names + ['Dataset' + ' ' + str(x) for x in range(len(dataset_names), len(datasets))]
        if len(line_colors) != len(datasets):
            color_list = ['b', 'g', 'r', 'c', 'm', 'y', 'k', 'pink', 'brown', 'orange']
            if len(datasets) < len(color_list):
                remaining_colors = [x for x in color_list if x not in line_colors]
                line_colors += remaining_colors[:len(datasets) - len(color_list)]
            else:
                warn('Insufficient number of line colors provided')
                return


    if excit_wfm.size != num_points:
        warn('Length of excitation waveform not compatible with second axis of datasets')
        return
Somnath, Suhas's avatar
Somnath, Suhas committed
354
355

    plots_on_side = min(abs(plots_on_side), 5)
356

Somnath, Suhas's avatar
Somnath, Suhas committed
357
358
359
360
361
362
363
    sq_num_plots = min(plots_on_side, int(round(num_pos ** 0.5)))
    if evenly_spaced:
        chosen_pos = np.linspace(0, num_pos - 1, sq_num_plots ** 2, dtype=int)
    else:
        chosen_pos = np.arange(sq_num_plots ** 2, dtype=int)

    fig, axes = plt.subplots(nrows=sq_num_plots, ncols=sq_num_plots, figsize=(12, 12))
364
    axes_lin = axes.flatten()
Somnath, Suhas's avatar
Somnath, Suhas committed
365

366
    cent_ind = int(0.5 * excit_wfm.size)
Somnath, Suhas's avatar
Somnath, Suhas committed
367
368
369
370
371
372
    if central_resp_size:
        sz = int(0.5 * central_resp_size)
        l_resp_ind = cent_ind - sz
        r_resp_ind = cent_ind + sz
    else:
        l_resp_ind = 0
373
        r_resp_ind = excit_wfm.size
Somnath, Suhas's avatar
Somnath, Suhas committed
374
375

    for count, posn in enumerate(chosen_pos):
376
377
        if use_rainbow_plots and len(datasets) == 1:
            rainbow_plot(axes_lin[count], excit_wfm[l_resp_ind:r_resp_ind], datasets[0][posn, l_resp_ind:r_resp_ind])
Somnath, Suhas's avatar
Somnath, Suhas committed
378
        else:
379
380
381
            for dataset, col_val in zip(datasets, line_colors):
                axes_lin[count].plot(excit_wfm[l_resp_ind:r_resp_ind], dataset[posn, l_resp_ind:r_resp_ind], color=col_val)
        if h5_pos is not None:
Somnath, Suhas's avatar
Somnath, Suhas committed
382
383
384
385
386
387
388
389
390
391
392
393
            # print 'Row ' + str(h5_pos[posn,1]) + ' Col ' + str(h5_pos[posn,0])
            axes_lin[count].set_title('Row ' + str(h5_pos[posn, 1]) + ' Col ' + str(h5_pos[posn, 0]), fontsize=12)
        else:
            axes_lin[count].set_title(subtitles + ' ' + str(posn), fontsize=12)

        if count % sq_num_plots == 0:
            axes_lin[count].set_ylabel(y_label, fontsize=12)
        if count >= (sq_num_plots - 1) * sq_num_plots:
            axes_lin[count].set_xlabel(x_label, fontsize=12)
        axes_lin[count].axis('tight')
        axes_lin[count].set_aspect('auto')
        axes_lin[count].ticklabel_format(style='sci', axis='y', scilimits=(0, 0))
394
395
    if len(datasets) > 1:
        axes_lin[count].legend(dataset_names, loc='best')
Somnath, Suhas's avatar
Somnath, Suhas committed
396
397
398
399
    if title:
        fig.suptitle(title, fontsize=14)
    plt.tight_layout()
    return fig, axes
Chris Smith's avatar
merged    
Chris Smith committed
400

Somnath, Suhas's avatar
Somnath, Suhas committed
401
402
###############################################################################

403
404

def plot_complex_map_stack(map_stack, num_comps=4, title='Eigenvectors', xlabel='UDVS Step', stdevs=2):
Somnath, Suhas's avatar
Somnath, Suhas committed
405
    """
Somnath, Suhas's avatar
Somnath, Suhas committed
406
407
408
409
    Plots the provided spectrograms from SVD V vector

    Parameters:
    -------------
410
    map_stack : 3D numpy complex matrices
Somnath, Suhas's avatar
Somnath, Suhas committed
411
412
413
414
415
416
417
418
419
420
421
422
423
        Eigenvectors rearranged as - [row, col, component]
    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
Somnath, Suhas's avatar
Somnath, Suhas committed
424
    """
Somnath, Suhas's avatar
Somnath, Suhas committed
425
426
427
428
    fig201, axes201 = plt.subplots(2, num_comps, figsize=(4 * num_comps, 8))
    fig201.subplots_adjust(hspace=0.4, wspace=0.4)
    fig201.canvas.set_window_title(title)

429
    for index in range(num_comps):
430
        cur_map = np.transpose(map_stack[index, :, :])
Somnath, Suhas's avatar
Somnath, Suhas committed
431
432
433
434
        axes = [axes201.flat[index], axes201.flat[index + num_comps]]
        funcs = [np.abs, np.angle]
        labels = ['Amplitude', 'Phase']
        for func, lab, ax in zip(funcs, labels, axes):
435
436
            amp_mean = np.mean(func(cur_map))
            amp_std = np.std(func(cur_map))
Somnath, Suhas's avatar
Somnath, Suhas committed
437
438
439
440
441
442
443
444
445
446
447
448
            ax.imshow(func(cur_map), cmap='inferno',
                      vmin=amp_mean - stdevs * amp_std,
                      vmax=amp_mean + stdevs * amp_std)
            ax.set_title('Eigenvector: %d - %s' % (index + 1, lab))
            ax.set_aspect('auto')
        ax.set_xlabel(xlabel)

    return fig201, axes201


###############################################################################

449
def plot_complex_loop_stack(loop_stack, x_axis, heading='BE Loops', subtitle='Eigenvector', num_comps=4, x_label=''):
Somnath, Suhas's avatar
Somnath, Suhas committed
450
    """
Somnath, Suhas's avatar
Somnath, Suhas committed
451
452
453
454
    Plots the provided spectrograms from SVD V vector

    Parameters:
    -------------
455
456
457
    loop_stack : 3D numpy complex matrices
        Loops rearranged as - [component, points]
    x_axis : 1D real numpy array
Somnath, Suhas's avatar
Somnath, Suhas committed
458
459
460
461
462
        The vector to plot against
    num_comps : int
        Number of components to plot
    title : String
        Title to plot above everything else
463
    x_label : String
Somnath, Suhas's avatar
Somnath, Suhas committed
464
465
466
467
468
469
470
        Label for x axis
    stdevs : int
        Number of standard deviations to consider for plotting

    Returns:
    ---------
    fig, axes
Somnath, Suhas's avatar
Somnath, Suhas committed
471
    """
Somnath, Suhas's avatar
Somnath, Suhas committed
472
473
474
475
476
    funcs = [np.abs, np.angle]
    labels = ['Amplitude', 'Phase']

    fig201, axes201 = plt.subplots(len(funcs), num_comps, figsize=(num_comps * 4, 4 * len(funcs)))
    fig201.subplots_adjust(hspace=0.4, wspace=0.4)
477
    fig201.canvas.set_window_title(heading)
Somnath, Suhas's avatar
Somnath, Suhas committed
478

479
    for index in range(num_comps):
480
        cur_map = loop_stack[index, :]
Somnath, Suhas's avatar
Somnath, Suhas committed
481
482
        axes = [axes201.flat[index], axes201.flat[index + num_comps]]
        for func, lab, ax in zip(funcs, labels, axes):
483
484
485
            ax.plot(x_axis, func(cur_map))
            ax.set_title('%s: %d - %s' % (subtitle, index + 1, lab))
        ax.set_xlabel(x_label)
Somnath, Suhas's avatar
Somnath, Suhas committed
486
487
488
489
490
491
492
    fig201.tight_layout()

    return fig201, axes201

###############################################################################


493
def plotScree(scree, title='Scree'):
Somnath, Suhas's avatar
Somnath, Suhas committed
494
    """
495
    Plots the scree or scree
Somnath, Suhas's avatar
Somnath, Suhas committed
496
497
498

    Parameters:
    -------------
499
500
    scree : 1D real numpy array
        The scree vector from SVD
Somnath, Suhas's avatar
Somnath, Suhas committed
501
502
503
504

    Returns:
    ---------
    fig, axes
Somnath, Suhas's avatar
Somnath, Suhas committed
505
    """
Somnath, Suhas's avatar
Somnath, Suhas committed
506
507
    fig203 = plt.figure(figsize=(6.5, 6))
    axes203 = fig203.add_axes([0.1, 0.1, .8, .8])  # left, bottom, width, height (range 0 to 1)
508
    axes203.loglog(np.arange(len(scree)) + 1, scree, 'b', marker='*')
Somnath, Suhas's avatar
Somnath, Suhas committed
509
510
511
    axes203.set_xlabel('Principal Component')
    axes203.set_ylabel('Variance')
    axes203.set_title(title)
512
513
    axes203.set_xlim(left=1, right=len(scree))
    axes203.set_ylim(bottom=np.min(scree), top=np.max(scree))
Somnath, Suhas's avatar
Somnath, Suhas committed
514
515
516
517
518
    fig203.canvas.set_window_title("Scree")

    return fig203, axes203


519
520
521
# ###############################################################################


522
def plot_map_stack(map_stack, num_comps=9, stdevs=2, color_bar_mode=None, evenly_spaced=False,
Somnath, Suhas's avatar
Somnath, Suhas committed
523
                   title='Component', heading='Map Stack', **kwargs):
Somnath, Suhas's avatar
Somnath, Suhas committed
524
    """
Somnath, Suhas's avatar
Somnath, Suhas committed
525
    Plots the provided stack of maps
Somnath, Suhas's avatar
Somnath, Suhas committed
526
527
528

    Parameters:
    -------------
Somnath, Suhas's avatar
Somnath, Suhas committed
529
    map_stack : 3D real numpy array
Somnath, Suhas's avatar
Somnath, Suhas committed
530
        structured as [rows, cols, component]
531
    num_comps : unsigned int
Somnath, Suhas's avatar
Somnath, Suhas committed
532
533
534
        Number of components to plot
    stdevs : int
        Number of standard deviations to consider for plotting
535
    color_bar_mode : String, Optional
536
537
538
539
540
        Options are None, single or each. Default None
    title : String or list of strings
        The titles for each of the plots.
        If a single string is provided, the plot titles become ['title 01', title 02', ...].
        if a list of strings (equal to the number of components) are provided, these are used instead.
Somnath, Suhas's avatar
Somnath, Suhas committed
541
542
543
544

    Returns:
    ---------
    fig, axes
Somnath, Suhas's avatar
Somnath, Suhas committed
545
    """
546
547
548
549
550
551
552
553
554
    num_comps = abs(num_comps)
    num_comps = min(num_comps, map_stack.shape[-1])


    if evenly_spaced:
        chosen_pos = np.linspace(0, map_stack.shape[-1] - 1, num_comps, dtype=int)
    else:
        chosen_pos = np.arange(num_comps, dtype=int)

555
556
557
558
559
560
561
562
563
564
    if isinstance(title, list):
        if len(title) > num_comps:
            # remove additional titles
            title = title[:num_comps]
        elif len(title) < num_comps:
            # add titles
            title = title + ['Component' + ' ' + str(x) for x in range(len(title), num_comps)]
    else:
        if not isinstance(title, str):
            title = 'Component'
565
        title = [title + ' ' + str(x) for x in chosen_pos]
566

567
    fig_h, fig_w = (4, 4)
568
569
    p_rows = int(np.floor(np.sqrt(num_comps)))
    p_cols = int(np.ceil(num_comps / p_rows))
570
571
    if p_rows*p_cols < num_comps:
        p_cols += 1
572
    fig202 = plt.figure(figsize=(p_cols * fig_w, p_rows * fig_h))
573
574
575
576
577
578
579
    axes202 = ImageGrid(fig202, 111, nrows_ncols=(p_rows, p_cols),
                        cbar_mode=color_bar_mode,
                        cbar_pad='1%',
                        cbar_size='5%',
                        axes_pad=(0.1*fig_w, 0.07*fig_h))
    # fig202, axes202 = plt.subplots(p_cols, p_rows, figsize=(p_cols * fig_w, p_rows * fig_h))
    # fig202.subplots_adjust(hspace=0.4, wspace=0.4)
Somnath, Suhas's avatar
Somnath, Suhas committed
580
581
    fig202.canvas.set_window_title(heading)
    fig202.suptitle(heading, fontsize=16)
Somnath, Suhas's avatar
Somnath, Suhas committed
582

583
584
    for count, index, subtitle in zip(range(chosen_pos.size), chosen_pos, title):
        im = plot_map(axes202[count],
585
                      map_stack[:, :, index],
586
587
                      stdevs=stdevs, **kwargs)
        axes202[count].set_title(subtitle)
588
        if color_bar_mode is 'each':
589
            axes202.cbar_axes[count].colorbar(im)
590
591
592

    if color_bar_mode is 'single':
        axes202.cbar_axes[0].colorbar(im)
Somnath, Suhas's avatar
Somnath, Suhas committed
593
594
595

    return fig202, axes202

596

597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
def plot_cluster_h5_group(h5_group, y_spec_label, centroids_together=True):
    """
        Plots the cluster labels and mean response for each cluster

        Parameters
        ----------
        h5_group : h5py.Datagroup object
            H5 group containing the labels and mean response
        y_spec_label : str
            Label to use for Y axis on cluster centroid plot
        centroids_together : Boolean, optional - default = True
            Whether or nor to plot all centroids together on the same plot

        Returns
        -------
        fig : Figure
            Figure containing the plots
        axes : 1D array_like of axes objects
            Axes of the individual plots within `fig`
        """
617
    # TODO: The label and units for the main dataset itself are missing in most cases! - ie. I don't know that the data is 'Current' and 'nA'
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
    h5_labels = h5_group['Labels']
    try:
        h5_mean_resp = h5_group['Mean_Response']
    except KeyError:
        # old PySPM format:
        h5_mean_resp = h5_group['Centroids']

    # Reshape the mean response to N dimensions
    mean_response, success = reshape_to_Ndims(h5_mean_resp)

    # unfortunately, we cannot use the above function for the labels
    # However, we will assume that the position values are linked to the labels:
    h5_pos_vals = h5_labels.file[h5_labels.attrs['Position_Values']]
    h5_pos_inds = h5_labels.file[h5_labels.attrs['Position_Indices']]

    # Reshape the labels correctly:
    pos_dims = []
    for col in range(h5_pos_inds.shape[1]):
        pos_dims.append(np.unique(h5_pos_inds[:, col]).size)

    pos_ticks = [h5_pos_vals[:pos_dims[0], 0], h5_pos_vals[slice(0,None,pos_dims[0]), 1]]
    # prepare the axes ticks for the map

    pos_dims.reverse()  # go from slowest to fastest
    pos_dims = tuple(pos_dims)
    label_mat = np.reshape(h5_labels.value, pos_dims)

    # Figure out the correct units and labels for mean response:
    h5_spec_vals = h5_mean_resp.file[h5_mean_resp.attrs['Spectroscopic_Values']]
    x_spec_label = get_formatted_labels(h5_spec_vals)[0]

    # Figure out the correct axes labels for label map:
    pos_labels = get_formatted_labels(h5_pos_vals)

652
653
654
655
656
657
658
    if centroids_together:
        return plot_cluster_results_together(label_mat, mean_response, spec_val=np.squeeze(h5_spec_vals[0]),
                                             spec_label=x_spec_label, resp_label=y_spec_label,
                                             pos_labels=pos_labels, pos_ticks=pos_ticks)
    else:
        return plot_cluster_results_separate(label_mat, mean_response, max_centroids=4, x_label=x_spec_label,
                                             spec_val=np.squeeze(h5_spec_vals[0]), y_label=y_spec_label)
Somnath, Suhas's avatar
Somnath, Suhas committed
659
660

###############################################################################
661
662


663
664
665
def plot_cluster_results_together(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):
Somnath, Suhas's avatar
Somnath, Suhas committed
666
    """
667
    Plot the cluster labels and mean response for each cluster in separate plots
Chris Smith's avatar
Chris Smith committed
668
669
670
671
672

    Parameters
    ----------
    label_mat : 2D ndarray or h5py.Dataset of ints
        Spatial map of cluster labels structured as [rows, cols]
673
    mean_response : 2D array or h5py.Dataset
Chris Smith's avatar
Chris Smith committed
674
675
        Mean value of each cluster over all samples 
        arranged as [cluster number, features]
676
    spec_val :  1D array or h5py.Dataset of floats, optional
Chris Smith's avatar
Chris Smith committed
677
678
679
680
681
682
683
684
685
686
687
688
        X axis to plot the centroids against
        If no value is specified, the data is plotted against the index
    cmap : plt.cm object or str, optional
        Colormap to use for the labels map and the centroid.
        Advised to pick a map where the centroid plots show clearly.
        Default = matplotlib.pyplot.cm.jet
    spec_label : str, optional
        Label to use for X axis on cluster centroid plot
        Default = 'Spectroscopic Value'
    resp_label : str, optional
        Label to use for Y axis on cluster centroid plot
         Default = 'Response'
Chris Smith's avatar
Chris Smith committed
689
690
691
692
    pos_labels : array_like of str, optional
        Labels to use for the X and Y axes on the Label map
        Default = ('X', 'Y')
    pos_ticks : array_like of int
Chris Smith's avatar
Chris Smith committed
693
694
695
696
697
698
699

    Returns
    -------
    fig : Figure
        Figure containing the plots
    axes : 1D array_like of axes objects
        Axes of the individual plots within `fig`
Somnath, Suhas's avatar
Somnath, Suhas committed
700
    """
Somnath, Suhas's avatar
Somnath, Suhas committed
701

702
    def __plot_centroids(centroids, ax, spec_val, spec_label, y_label, cmap, title=None):
703
        plot_line_family(ax, spec_val, centroids, label_prefix='Cluster', cmap=cmap)
Somnath, Suhas's avatar
Somnath, Suhas committed
704
        ax.set_ylabel(y_label)
Chris Smith's avatar
Chris Smith committed
705
        # ax.legend(loc='best')
Somnath, Suhas's avatar
Somnath, Suhas committed
706
707
708
709
710
        if title:
            ax.set_title(title)
            ax.set_xlabel(spec_label)

    if type(spec_val) == type(None):
Chris Smith's avatar
Chris Smith committed
711
        spec_val = np.arange(mean_response.shape[1])
Somnath, Suhas's avatar
Somnath, Suhas committed
712

Chris Smith's avatar
Chris Smith committed
713
    if mean_response.dtype in [np.complex64, np.complex128, np.complex]:
Somnath, Suhas's avatar
Somnath, Suhas committed
714
        fig = plt.figure(figsize=(12, 8))
Chris Smith's avatar
Chris Smith committed
715
716
717
        ax_map = plt.subplot2grid((2, 12), (0, 0), colspan=6, rowspan=2)
        ax_amp = plt.subplot2grid((2, 12), (0, 6), colspan=4)
        ax_phase = plt.subplot2grid((2, 12), (1, 6), colspan=4)
Somnath, Suhas's avatar
Somnath, Suhas committed
718
719
        axes = [ax_map, ax_amp, ax_phase]

720
        __plot_centroids(np.abs(mean_response), ax_amp, spec_val, spec_label,
Chris Smith's avatar
Chris Smith committed
721
                        resp_label + ' - Amplitude', cmap, 'Mean Response')
722
        __plot_centroids(np.angle(mean_response), ax_phase, spec_val, spec_label,
Somnath, Suhas's avatar
Somnath, Suhas committed
723
                        resp_label + ' - Phase', cmap)
Chris Smith's avatar
Chris Smith committed
724
725
        plot_handles, plot_labels = ax_amp.get_legend_handles_labels()

Somnath, Suhas's avatar
Somnath, Suhas committed
726
    else:
Chris Smith's avatar
Chris Smith committed
727
728
729
730
        fig = plt.figure(figsize=(12, 8))
        ax_map = plt.subplot2grid((1, 12), (0, 0), colspan=6)
        ax_resp = plt.subplot2grid((1, 12), (0, 6), colspan=4)
        axes = [ax_map, ax_resp]
731
        __plot_centroids(mean_response, ax_resp, spec_val, spec_label,
Chris Smith's avatar
Chris Smith committed
732
733
734
735
736
737
                        resp_label, cmap, 'Mean Response')
        plot_handles, plot_labels = ax_resp.get_legend_handles_labels()

    fleg = plt.figlegend(plot_handles, plot_labels, loc='center right',
                         borderaxespad=0.0)
    num_clusters = mean_response.shape[0]
Somnath, Suhas's avatar
Somnath, Suhas committed
738
739

    if isinstance(label_mat, h5py.Dataset):
Somnath, Suhas's avatar
Somnath, Suhas committed
740
        """
Somnath, Suhas's avatar
Somnath, Suhas committed
741
        Reshape label_mat based on linked positions
Somnath, Suhas's avatar
Somnath, Suhas committed
742
        """
Somnath, Suhas's avatar
Somnath, Suhas committed
743
744
745
746
        pos = label_mat.file[label_mat.attrs['Position_Indices']]
        nx = len(np.unique(pos[:, 0]))
        ny = len(np.unique(pos[:, 1]))
        label_mat = label_mat[()].reshape(nx, ny)
747

Chris Smith's avatar
Chris Smith committed
748
    # im = ax_map.imshow(label_mat, interpolation='none')
749
750
751
752
753
754
755
756
757
758
759
    ax_map.set_xlabel(pos_labels[0])
    ax_map.set_ylabel(pos_labels[1])

    if pos_ticks is not None:
        x_ticks = np.linspace(0, label_mat.shape[1] - 1, 5, dtype=np.uint16)
        y_ticks = np.linspace(0, label_mat.shape[0] - 1, 5, dtype=np.uint16)
        ax_map.set_xticks(x_ticks)
        ax_map.set_yticks(y_ticks)
        ax_map.set_xticklabels(pos_ticks[0][x_ticks])
        ax_map.set_yticklabels(pos_ticks[1][y_ticks])

760
    """divider = make_axes_locatable(ax_map)
Somnath, Suhas's avatar
Somnath, Suhas committed
761
    cax = divider.append_axes("right", size="5%", pad=0.05)  # space for colorbar
762
763
764
765
766
    fig.colorbar(im, cax=cax, ticks=np.arange(num_clusters),
                 cmap=discrete_cmap(num_clusters, base_cmap=plt.cm.jet))
    ax_map.axis('tight')"""
    pcol0 = ax_map.pcolor(label_mat, cmap=discrete_cmap(num_clusters, base_cmap=plt.cm.jet))
    fig.colorbar(pcol0, ax=ax_map, ticks=np.arange(num_clusters))
Somnath, Suhas's avatar
Somnath, Suhas committed
767
    ax_map.axis('tight')
768
    ax_map.set_aspect('auto')
Somnath, Suhas's avatar
Somnath, Suhas committed
769
770
771
    ax_map.set_title('Cluster Label Map')

    fig.tight_layout()
Chris Smith's avatar
Chris Smith committed
772
    fig.canvas.set_window_title('Cluster results')
Somnath, Suhas's avatar
Somnath, Suhas committed
773
774
775
776
777

    return fig, axes

###############################################################################

778

779
780
def plot_cluster_results_separate(label_mat, cluster_centroids, max_centroids=4,
                                  spec_val=None, x_label='Excitation (a.u.)', y_label='Response (a.u.)'):
Somnath, Suhas's avatar
Somnath, Suhas committed
781
    """
782
    Plots the provided labels mat and centroids from clustering
Somnath, Suhas's avatar
Somnath, Suhas committed
783

784
785
    Parameters
    ----------
Somnath, Suhas's avatar
Somnath, Suhas committed
786
787
788
789
    label_mat : 2D int numpy array
                structured as [rows, cols]
    cluster_centroids: 2D real numpy array
                       structured as [cluster,features]
790
791
    max_centroids : unsigned int
                    Number of centroids to plot
792
793
794
    spec_val :  array-like
        X axis to plot the centroids against
        If no value is specified, the data is plotted against the index
795
796
797
798
    x_label : String / unicode
              X label for centroid plots
    y_label : String / unicode
              Y label for centroid plots
Somnath, Suhas's avatar
Somnath, Suhas committed
799

800
801
    Returns
    -------
Somnath, Suhas's avatar
Somnath, Suhas committed
802
    fig
Somnath, Suhas's avatar
Somnath, Suhas committed
803
    """
Somnath, Suhas's avatar
Somnath, Suhas committed
804

805
    if max_centroids < 5:
Somnath, Suhas's avatar
Somnath, Suhas committed
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833

        fig501 = plt.figure(figsize=(20, 10))
        fax1 = plt.subplot2grid((2, 4), (0, 0), colspan=2, rowspan=2)
        fax2 = plt.subplot2grid((2, 4), (0, 2))
        fax3 = plt.subplot2grid((2, 4), (0, 3))
        fax4 = plt.subplot2grid((2, 4), (1, 2))
        fax5 = plt.subplot2grid((2, 4), (1, 3))
        fig501.tight_layout()
        axes_handles = [fax1, fax2, fax3, fax4, fax5]

    else:
        fig501 = plt.figure(figsize=(20, 10))
        # make subplot for cluster map
        fax1 = plt.subplot2grid((3, 6), (0, 0), colspan=3, rowspan=3)  # For cluster map
        fax1.set_xmargin(0.50)
        # make subplot for cluster centers
        fax2 = plt.subplot2grid((3, 6), (0, 3))
        fax3 = plt.subplot2grid((3, 6), (0, 4))
        fax4 = plt.subplot2grid((3, 6), (0, 5))
        fax5 = plt.subplot2grid((3, 6), (1, 3))
        fax6 = plt.subplot2grid((3, 6), (1, 4))
        fax7 = plt.subplot2grid((3, 6), (1, 5))
        fax8 = plt.subplot2grid((3, 6), (2, 3))
        fax9 = plt.subplot2grid((3, 6), (2, 4))
        fax10 = plt.subplot2grid((3, 6), (2, 5))
        fig501.tight_layout()
        axes_handles = [fax1, fax2, fax3, fax4, fax5, fax6, fax7, fax8, fax9, fax10]

834
    # First plot the labels map:
835
836
    pcol0 = fax1.pcolor(label_mat, cmap=discrete_cmap(cluster_centroids.shape[0],
                                                      base_cmap=plt.cm.jet))
837
    fig501.colorbar(pcol0, ax=fax1, ticks=np.arange(cluster_centroids.shape[0]))
838
839
    fax1.axis('tight')
    fax1.set_aspect('auto')
840
    fax1.set_title('Cluster Label Map')
841
    """im = fax1.imshow(label_mat, interpolation='none')
842
843
    divider = make_axes_locatable(fax1)
    cax = divider.append_axes("right", size="5%", pad=0.05)  # space for colorbar
844
845
846
847
    plt.colorbar(im, cax=cax)"""

    if spec_val is None and cluster_centroids.ndim == 2:
        spec_val = np.arange(cluster_centroids.shape[1])
848
849

    # Plot results
850
851
852
853
854
855
856
    for ax, index in zip(axes_handles[1: max_centroids + 1], np.arange(max_centroids)):
        if cluster_centroids.ndim == 2:
            ax.plot(spec_val, cluster_centroids[index, :],
                    color=plt.cm.jet(int(255 * index / (cluster_centroids.shape[0] - 1))))
            ax.set_xlabel(x_label)
            ax.set_ylabel(y_label)
        elif cluster_centroids.ndim == 3:
857
            plot_map(ax, cluster_centroids[index])
858
        ax.set_title('Centroid: %d' % index)
Somnath, Suhas's avatar
Somnath, Suhas committed
859
860

    fig501.subplots_adjust(hspace=0.60, wspace=0.60)
861
    fig501.tight_layout()
Somnath, Suhas's avatar
Somnath, Suhas committed
862
863
864
865
866
867

    return fig501


###############################################################################

868
869
def plot_cluster_dendrogram(label_mat, e_vals, num_comp, num_cluster, mode='Full', last=None,
                            sort_type='distance', sort_mode=True):
Somnath, Suhas's avatar
Somnath, Suhas committed
870
    """
Somnath, Suhas's avatar
Somnath, Suhas committed
871
872
873
874
875
876
    Creates and plots the dendrograms for the given label_mat and
    eigenvalues

    Parameters
    -------------
    label_mat : 2D real numpy array
877
        structured as [rows, cols], from KMeans clustering
Somnath, Suhas's avatar
Somnath, Suhas committed
878
    e_vals: 3D real numpy array of eigenvalues
879
        structured as [component, rows, cols]
880
    num_comp : int
881
882
883
        Number of components used to make eigenvalues
    num_cluster : int
        Number of cluster used to make the label_mat
Somnath, Suhas's avatar
Somnath, Suhas committed
884
    mode: str, optional
885
886
887
        How should the dendrograms be created.
        "Full" -- use all clusters when creating the dendrograms
        "Truncated" -- stop showing clusters after 'last'
Somnath, Suhas's avatar
Somnath, Suhas committed
888
    last: int, optional - should be provided when using "Truncated"
889
890
891
892
893
894
895
896
897
898
899
900
901
902
        How many merged clusters should be shown when using
        "Truncated" mode
    sort_type: {'count', 'distance'}, optional
        What type of sorting should be used when plotting the
        dendrograms.  Options are:
        count - Uses the count_sort from scipy.cluster.hierachy.dendrogram
        distance - Uses the distance_sort from scipy.cluster.hierachy.dendrogram
    sort_mode: {False, True, 'ascending', 'descending'}, optional
        For the chosen sort_type, which mode should be used.
        False - Does no sorting
        'ascending' or True - The child with the minimum of the chosen sort
        parameter is plotted first
        'descending' - The child with the maximum of the chosen sort parameter is
        plotted first
Somnath, Suhas's avatar
Somnath, Suhas committed
903
904
905

    Returns
    ---------
906
907
    fig : matplotlib.pyplot Figure object
        Figure containing the dendrogram
Somnath, Suhas's avatar
Somnath, Suhas committed
908
    """
Somnath, Suhas's avatar
Somnath, Suhas committed
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
    if mode == 'Truncated' and not last:
        warn('Warning: Truncated dendrograms requested, but no last cluster given.  Reverting to full dendrograms.')
        mode = 'Full'

    if mode == 'Full':
        print 'Creating full dendrogram from clusters'
        mode = None
    elif mode == 'Truncated':
        print 'Creating truncated dendrogram from clusters.  Will stop at {}.'.format(last)
        mode = 'lastp'
    else:
        raise ValueError('Error: Unknown mode requested for plotting dendrograms. mode={}'.format(mode))

    c_sort = False
    d_sort = False
    if sort_type == 'count':
        c_sort = sort_mode
        if c_sort == 'descending':
            c_sort = 'descendent'
    elif sort_type == 'distance':
        d_sort = sort_mode

    centroid_mat = np.zeros([num_cluster, num_comp])
932
    for k1 in range(num_cluster):
Somnath, Suhas's avatar
Somnath, Suhas committed
933
934
        [i_x, i_y] = np.where(label_mat == k1)
        u_stack = np.zeros([len(i_x), num_comp])
935
        for k2 in range(len(i_x)):
Somnath, Suhas's avatar
Somnath, Suhas committed
936
937
938
939
            u_stack[k2, :] = np.abs(e_vals[i_x[k2], i_y[k2], :num_comp])

        centroid_mat[k1, :] = np.mean(u_stack, 0)

940
    # Get the distrance between cluster means
941
    distance_mat = scipy.spatial.distance.pdist(centroid_mat)
Somnath, Suhas's avatar
Somnath, Suhas committed
942
943

    # get hierachical pairings of clusters
944
    linkage_pairing = scipy.cluster.hierarchy.linkage(distance_mat, 'weighted')
Somnath, Suhas's avatar
Somnath, Suhas committed
945
946
947
    linkage_pairing[:, 3] = linkage_pairing[:, 3] / max(linkage_pairing[:, 3])

    fig = plt.figure()
948
949
950
    scipy.cluster.hierarchy.dendrogram(linkage_pairing, p=last, truncate_mode=mode,
                                       count_sort=c_sort, distance_sort=d_sort,
                                       leaf_rotation=90)
Somnath, Suhas's avatar
Somnath, Suhas committed
951
952
953
954
955
956
957
958

    fig.axes[0].set_title('Dendrogram')
    fig.axes[0].set_xlabel('Index or (cluster size)')
    fig.axes[0].set_ylabel('Distance')

    return fig


959
def plot_1d_spectrum(data_vec, freq, title, figure_path=None):
Somnath, Suhas's avatar
Somnath, Suhas committed
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
    """
    Plots the Step averaged BE response

    Parameters
    ------------
    data_vec : 1D numpy array
        Response of one BE pulse
    freq : 1D numpy array
        BE frequency that serves as the X axis of the plot
    title : String
        Plot group name
    figure_path : String / Unicode
        Absolute path of the file to write the figure to

    Returns
    ---------
    fig : Matplotlib.pyplot figure
        Figure handle
    ax : Matplotlib.pyplot axis
        Axis handle
    """
    if len(data_vec) != len(freq):
982
983
        warn('plot_1d_spectrum: Incompatible data sizes!!!!')
        print('1D:', data_vec.shape, freq.shape)
Somnath, Suhas's avatar
Somnath, Suhas committed
984
        return
985
986
    freq *= 1E-3  # to kHz
    fig, ax = plt.subplots(nrows=2, ncols=1, sharex=True)
Somnath, Suhas's avatar
Somnath, Suhas committed
987
988
989
990
991
992
993
994
    ax[0].plot(freq, np.abs(data_vec) * 1E+3)
    ax[0].set_title('Amplitude (mV)')
    ax[1].plot(freq, np.angle(data_vec) * 180 / np.pi)
    ax[1].set_title('Phase (deg)')
    ax[1].set_xlabel('Frequency (kHz)')
    fig.suptitle(title + ': mean UDVS, mean spatial response')
    if figure_path:
        plt.savefig(figure_path, format='png', dpi=300)
995
    return fig, ax
Somnath, Suhas's avatar
Somnath, Suhas committed
996
997
998
999


###############################################################################

1000
def plot_2d_spectrogram(mean_spectrogram, freq, title, figure_path=None):
Somnath, Suhas's avatar
Somnath, Suhas committed
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
    """
    Plots the position averaged spectrogram

    Parameters
    ------------
    mean_spectrogram : 2D numpy complex array
        Means spectrogram arranged as [frequency, UDVS step]
    freq : 1D numpy float array
        BE frequency that serves as the X axis of the plot
    title : String
        Plot group name
    figure_path : String / Unicode
        Absolute path of the file to write the figure to

    Returns
    ---------
    fig : Matplotlib.pyplot figure
        Figure handle
    ax : Matplotlib.pyplot axis
        Axis handle
    """
    if mean_spectrogram.shape[1] != len(freq):
1023
1024
        warn('plot_2d_spectrogram: Incompatible data sizes!!!!')
        print('2D:', mean_spectrogram.shape, freq.shape)
Somnath, Suhas's avatar
Somnath, Suhas committed
1025
        return
1026
1027
    freq *= 1E-3  # to kHz
    fig, ax = plt.subplots(nrows=2, ncols=1, sharex=True)
Somnath, Suhas's avatar
Somnath, Suhas committed
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
    # print mean_spectrogram.shape
    # print freq.shape
    ax[0].imshow(np.abs(mean_spectrogram), interpolation='nearest',
                 extent=[freq[0], freq[-1], mean_spectrogram.shape[0], 0])
    ax[0].set_title('Amplitude')
    # ax[0].set_xticks(freq)
    # ax[0].set_ylabel('UDVS Step')
    ax[0].axis('tight')
    ax[1].imshow(np.angle(mean_spectrogram), interpolation='nearest',
                 extent=[freq[0], freq[-1], mean_spectrogram.shape[0], 0])
    ax[1].set_title('Phase')
    ax[1].set_xlabel('Frequency (kHz)')
    # ax[0].set_ylabel('UDVS Step')
    ax[1].axis('tight')
    fig.suptitle(title)
    if figure_path:
        plt.savefig(figure_path, format='png', dpi=300)
1045
    return fig, ax
Somnath, Suhas's avatar
Somnath, Suhas committed
1046
1047
1048

###############################################################################

1049
1050

def plot_histgrams(p_hist, p_hbins, title, figure_path=None):
Somnath, Suhas's avatar
Somnath, Suhas committed
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
    """
    Plots the position averaged spectrogram

    Parameters
    ------------
    p_hist : 2D numpy array
        histogram data arranged as [physical quantity, frequency bin]
    p_hbins : 1D numpy array
        BE frequency that serves as the X axis of the plot
    title : String
        Plot group name
    figure_path : String / Unicode
        Absolute path of the file to write the figure to

    Returns
    ---------
    fig : Matplotlib.pyplot figure
        Figure handle
    """

    base_fig_size = 7
    h_fig = base_fig_size
    w_fig = base_fig_size * 4

    fig = plt.figure(figsize=(w_fig, h_fig))
    fig.suptitle(title)
    iplot = 0

    p_Nx, p_Ny = np.amax(p_hbins, axis=1) + 1

    p_hist = np.reshape(p_hist, (4, p_Ny, p_Nx))

    iplot += 1
    p_plot_title = 'Spectral BEHistogram Amp (log10 of counts)'
    p_plot = fig.add_subplot(1, 4, iplot, title=p_plot_title)
    p_im = p_plot.imshow(np.rot90(np.log10(p_hist[0])), interpolation='nearest')
    p_plot.axis('tight')
    fig.colorbar(p_im, fraction=0.1)

    iplot += 1
    p_plot_title = 'Spectral BEHistogram Phase (log10 of counts)'
    p_plot = fig.add_subplot(1, 4, iplot, title=p_plot_title)
    p_im = p_plot.imshow(np.rot90(np.log10(p_hist[1])), interpolation='nearest')
    p_plot.axis('tight')
    fig.colorbar(p_im, fraction=0.1)

    iplot += 1
    p_plot_title = 'Spectral BEHistogram Real (log10 of counts)'
    p_plot = fig.add_subplot(1, 4, iplot, title=p_plot_title)
    p_im = p_plot.imshow(np.rot90(np.log10(p_hist[2])), interpolation='nearest')
    p_plot.axis('tight')
    fig.colorbar(p_im, fraction=0.1)

    iplot += 1
    p_plot_title = 'Spectral BEHistogram Imag (log10 of counts)'
    p_plot = fig.add_subplot(1, 4, iplot, title=p_plot_title)
    p_im = p_plot.imshow(np.rot90(np.log10(p_hist[3])), interpolation='nearest')
    p_plot.axis('tight')
    fig.colorbar(p_im, fraction=0.1)

    if figure_path:
        plt.savefig(figure_path, format='png')

1114
1115
1116
    return fig


1117
def visualize_sho_results(h5_main, save_plots=True, show_plots=True):
1118
    """
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
    Plots some loops, amplitude, phase maps for BE-Line and BEPS datasets.\n
    Note: The file MUST contain SHO fit gusses at the very least

    Parameters
    ----------
    h5_main : HDF5 Dataset
        dataset to be plotted
    save_plots : (Optional) Boolean
        Whether or not to save plots to files in the same directory as the h5 file
    show_plots : (Optional) Boolean
        Whether or not to display the plots on the screen

    Returns
    -------
    None
1134
    """
1135
1136
1137
1138

    def __plot_loops_maps(ac_vec, resp_mat, grp_name, win_title, spec_var_title, meas_var_title, save_plots,
                          folder_path, basename, num_rows, num_cols):
        plt_title = grp_name + '_' + win_title + '_Loops'
1139
        fig, ax = plot_loops(ac_vec, resp_mat, evenly_spaced=True, plots_on_side=5, use_rainbow_plots=False,
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
                             x_label=spec_var_title, y_label=meas_var_title, subtitles='Loop', title=plt_title)
        if save_plots:
            fig.savefig(os.path.join(folder_path, basename + '_' + plt_title + '.png'), format='png', dpi=300)

        plt_title = grp_name + '_' + win_title + '_Snaps'
        fig, axes = plot_map_stack(resp_mat.reshape(num_rows, num_cols, resp_mat.shape[1]),
                                   color_bar_mode="each", evenly_spaced=True, title='UDVS Step #',
                                   heading=plt_title, cmap=cmap_jet_white_center())
        if save_plots:
            fig.savefig(os.path.join(folder_path, basename + '_' + plt_title + '.png'), format='png', dpi=300)

1151
1152
1153
    plt_path = None

    print('Creating plots of SHO Results from {}.'.format(h5_main.name))
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179

    h5_file = h5_main.file

    expt_type = h5_file.attrs['data_type']
    if expt_type not in ['BEPSData', 'BELineData']:
        warn('Unsupported data format')
        return
    isBEPS = expt_type == 'BEPSData'

    (folder_path, basename) = os.path.split(h5_file.filename)
    basename, _ = os.path.splitext(basename)

    sho_grp = h5_main.parent
    chan_grp = sho_grp.parent

    grp_name = '_'.join(chan_grp.name[1:].split('/'))
    grp_name = '_'.join([grp_name, sho_grp.name.split('/')[-1].split('-')[0], h5_main.name.split('/')[-1]])

    try:
        h5_pos = h5_file[h5_main.attrs['Position_Indices']]
    except KeyError:
        print('No Position_Indices found as attribute of {}'.format(h5_main.name))
        print('Rows and columns will be calculated from dataset shape.')
        num_rows = int(np.floor((np.sqrt(h5_main.shape[0]))))
        num_cols = int(np.reshape(h5_main, [num_rows, -1, h5_main.shape[1]]).shape[1])
    else:
1180
1181
        num_rows = len(np.unique(h5_pos[:, 0]))
        num_cols = len(np.unique(h5_pos[:, 1]))
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209

    try:
        h5_spec_vals = h5_file[h5_main.attrs['Spectroscopic_Values']]
    # except KeyError:
    #     warn('No Spectrosocpic Datasets found as attribute of {}'.format(h5_main.name))
    #     raise
    except:
        raise

    # Assume that there's enough memory to load all the guesses into memory
    amp_mat = h5_main['Amplitude [V]'] * 1000  # convert to mV ahead of time
    freq_mat = h5_main['Frequency [Hz]'] / 1000
    q_mat = h5_main['Quality Factor']
    phase_mat = h5_main['Phase [rad]']
    rsqr_mat = h5_main['R2 Criterion']

    if isBEPS:
        meas_type = chan_grp.parent.attrs['VS_mode']
        # basically 3 kinds for now - DC/current, AC, UD - lets ignore this
        if meas_type == 'load user defined VS Wave from file':
            warn('Not handling custom experiments for now')
            h5_file.close()
            return

        # Plot amplitude and phase maps at one or more UDVS steps

        if meas_type == 'AC modulation mode with time reversal':
            center = int(h5_spec_vals.shape[1] * 0.5)
1210
            ac_vec = np.squeeze(h5_spec_vals[h5_spec_vals.attrs['AC_Amplitude']][0:center])
1211

1212
1213
            forw_resp = np.squeeze(amp_mat[:, slice(0, center)])
            rev_resp = np.squeeze(amp_mat[:, slice(center, None)])
1214
1215
1216
1217

            for win_title, resp_mat in zip(['Forward', 'Reverse'], [forw_resp, rev_resp]):
                __plot_loops_maps(ac_vec, resp_mat, grp_name, win_title, 'AC Amplitude', 'Amplitude', save_plots,
                                  folder_path, basename, num_rows, num_cols)
1218
1219
        else:
            # plot loops at a few locations
1220
            dc_vec = np.squeeze(h5_spec_vals[h5_spec_vals.attrs['DC_Offset']])
1221
1222
            if chan_grp.parent.attrs['VS_measure_in_field_loops'] == 'in and out-of-field':

1223
1224
                dc_vec = np.squeeze(dc_vec[slice(0, None, 2)])

1225
1226
1227
1228
                in_phase = np.squeeze(phase_mat[:, slice(0, None, 2)])
                in_amp = np.squeeze(amp_mat[:, slice(0, None, 2)])
                out_phase = np.squeeze(phase_mat[:, slice(1, None, 2)])
                out_amp = np.squeeze(amp_mat[:, slice(1, None, 2)])
1229
1230
1231
1232

                for win_title, resp_mat in zip(['In_Field', 'Out_of_Field'], [in_phase * in_amp, out_phase * out_amp]):
                    __plot_loops_maps(dc_vec, resp_mat, grp_name, win_title, 'DC Bias', 'Piezoresponse (a.u.)',
                                      save_plots, folder_path, basename, num_rows, num_cols)
1233
            else:
1234
1235
                __plot_loops_maps(dc_vec, phase_mat * amp_mat, grp_name, '', 'DC Bias', 'Piezoresponse (a.u.)',
                                  save_plots, folder_path, basename, num_rows, num_cols)
1236
1237
1238
1239
1240
1241
1242
1243
1244

    else:  # BE-Line can only visualize the amplitude and phase maps:
        amp_mat = amp_mat.reshape(num_rows, num_cols)
        freq_mat = freq_mat.reshape(num_rows, num_cols)
        q_mat = q_mat.reshape(num_rows, num_cols)
        phase_mat = phase_mat.reshape(num_rows, num_cols)
        rsqr_mat = rsqr_mat.reshape(num_rows, num_cols)
        if save_plots:
            plt_path = os.path.join(folder_path, basename + '_' + grp_name + 'Maps.png')
1245
1246
1247
1248
1249
1250

        fig_ms, ax_ms = plot_map_stack(np.dstack((amp_mat, freq_mat, q_mat, phase_mat, rsqr_mat)),
                                       num_comps=5, color_bar_mode='each', heading=grp_name,
                                       title=['Amplitude (mV)', 'Frequency (kHz)', 'Quality Factor', 'Phase (deg)',
                                              'R^2 Criterion'], cmap=cmap_jet_white_center())
        fig_ms.savefig(plt_path, format='png', dpi=300)
1251
1252
1253
1254
1255

    if show_plots:
        plt.show()

    plt.close('all')