/
my_plot.py
93 lines (79 loc) · 3.24 KB
/
my_plot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
82
83
84
85
86
87
88
89
90
91
92
93
import matplotlib.pyplot as plt
import numpy as np
import math
import torch
from neural.torch_utils import tensor_to_array
def array_to_img(arr, path='', close=True):
if type(arr) == list:
num_arrs = len(arr)
_, axs = plt.subplots(num_arrs)
for i in range(num_arrs):
axs[i].imshow(arr[i])
else:
plt.imshow(arr)
if path != '':
plt.savefig(path)
if close:
plt.close()
def spec_to_notebook_img(arr, rot=1):
if type(arr) == torch.Tensor:
arr = tensor_to_array(arr)
for i in range(rot):
arr = np.rot90(arr)
array_to_img(arr, '', close=False)
"""Determines what the correct axis is for viewing in matplotlib"""
def fix_axes(melspec, librosa_used):
if librosa_used: melspec = np.flip(melspec, axis=-2) #librosa has all numpy flipped compared to intuitive reading
else: melspec = np.rot90(melspec, axes=(-2,-1)) #if not used, timestamps are vertical and the axes must be rotated
return melspec
""" Allows one to plot spectrograms
Must be fed an np array of spectrograms (all same size)
If it is fed a filepath string, this will be the location the plot is saved to
list_of_strings provides it with x_labels, y_labels and all remaining titles
num_cols determines how many columns the subplots will take form in.
"""
def plot_specs(array_of_specs, file_path=None, list_of_strings=None, num_cols=1, figsize=(20,5)):
num_specs = array_of_specs.shape[0]
num_feats = array_of_specs.shape[1]
num_frames = array_of_specs.shape[2]
x = np.arange(num_frames)
num_rows = math.ceil(num_specs/num_cols)
if list_of_strings == None: list_of_strings = ['No Title' for i in range(num_specs+2)]
x_label = list_of_strings[0]
y_label = list_of_strings[1]
labels = list_of_strings[2:]
plt.figure(figsize=figsize)
for i in range(num_specs):
this_array = array_of_specs[i]
plt.subplot(num_rows, num_cols, i+1)
plt.xlabel(x_label)
plt.ylabel(y_label)
plt.imshow(this_array)
plt.show()
if not file_path == None:
plt.savefig(file_path)
""" Plots contours such as pitch info or the performance of a model.
Feed this an np array of one or more data vectors.
If it is fed a filepath string, this will be the location the plot is saved to
list_of_strings provides it with x_labels, y_labels and all remaining titles
num_cols determines how many columns the subplots will take form in.
"""
def plot_contours(array_of_contours, file_path=None, list_of_strings=None, num_cols=1, figsize=(20,5)):
num_contours = array_of_contours.shape[0]
num_steps = array_of_contours.shape[1]
x = np.arange(num_steps)
num_rows = math.ceil(num_contours/num_cols)
if list_of_strings == None: list_of_strings = ['No Title' for i in range(num_contours+2)]
x_label = list_of_strings[0]
y_label = list_of_strings[1]
labels = list_of_strings[2:]
plt.figure(figsize=figsize)
for i in range(num_contours):
plt.subplot(num_rows, num_cols, i+1)
plt.xlabel(x_label)
plt.ylabel(y_label)
plt.plot(x, array_of_contours[i], 'r--',label=labels[i])
plt.legend()
plt.show()
if not file_path == None:
plt.savefig(file_path)