Mercurial > hg > segmentation
view utils/OnsetPlotProc.py @ 19:890cfe424f4a tip
added annotations
author | mitian |
---|---|
date | Fri, 11 Dec 2015 09:47:40 +0000 |
parents | b4bf37f94e92 |
children |
line wrap: on
line source
import numpy as np from numpy import * import sys, resource # import matplotlib.pyplot as plt import subprocess as sp import cPickle as pickle # set this True or False to enable/disable plotting # plot_on = False plot_on = True if plot_on : import matplotlib.pyplot as plt gt = np.genfromtxt('/Users/mitian/Documents/hg/segmenter/annotation/qupujicheng_annotation_30_3/10hongniang01.csv', delimiter=',')[:,0] # gt = np.genfromtxt('../annotation/TheBeatles/seg_tut/03LucyInTheSkyWithDiamonds.lab',)[:,0] class OnsetPlot(object): '''This class allows creating a series of plots easily.''' def __init__(self): self.signal_plots = [] self.marker_plots = [] self.subplots = {} def reset(self): self.__init__() def update(self,obj): self.signal_plots = obj.signal_plots self.marker_plots = obj.marker_plots self.subplots = obj.subplots def add_plot(self,signal,subplot=-1,title=""): self.signal_plots.append([signal,subplot,title]) def plot_signal(self,signal,subplot,title): axes = self.figure.add_subplot(subplot) axes.plot(signal) axes.get_xaxis().set_visible(False) axes.vlines(gt / gt[-1] * len(signal), np.min(signal), np.max(signal), colors='k') if title : axes.set_title(title, fontsize=20) plt.axis('tight') self.subplots.update({subplot:(axes,signal)}) def plot_markers(self,markers,symbol,subplot): values = zeros(len(markers)) + 1 axes,signal = self.subplots[subplot] axes.plot(markers,np.array(signal)[markers],symbol,markersize=12) def add_markers(self,markers,symbol="g^",subplot=-1): self.marker_plots.append([markers,symbol,subplot]) def show_in_subprocess(self): '''Marshal the object into a separate subprocess to fix OS/X issues with threading.''' cmd = "python %s" %__file__ print "executing:", cmd proc = sp.Popen(cmd, stdin = sp.PIPE, stdout = sp.PIPE, shell=True) pickle.dump(self,proc.stdin) result = proc.stdout.readlines() for item in result: print str(item).strip() proc.wait() return result def show(self): self.figure = plt.figure(figsize=(15,6)) num_plots = len(self.signal_plots) for i,s in enumerate(self.signal_plots) : j = i+1 if s[1] < 1 : s[1] = int("%(num_plots)s1%(j)s" %locals()) else : k = s[1] s[1] = int("%(num_plots)s1%(k)s" %locals()) print "plotting:",s[2] s = tuple(s) self.plot_signal(*s) # plot marketrs on the last subplot for m in self.marker_plots : if m[2] < 1 or m[2] > num_plots: m[2] = int("%(num_plots)s1%(num_plots)s" %locals()) else : k = m[2] m[2] = int("%(num_plots)s1%(k)s" %locals()) m = tuple(m) self.plot_markers(*m) plt.show(block=True) # create a module-level instance that can be reused across several files onset_plot = OnsetPlot() if __name__ == '__main__': obj = pickle.load(sys.stdin) onset_plot = OnsetPlot() onset_plot.update(obj) onset_plot.show()