from gwpy.plot import Plot plot = Plot(figsize=(12, 6)) ax = plot.gca() for specgram in spectrograms: ax.imshow(specgram) ax.set_xscale('auto-gps', epoch='Sep 16 2010') ax.set_xlim('Sep 16 2010', 'Sep 17 2010') ax.set_ylim(40, 2000) ax.set_yscale('log') ax.set_ylabel('Frequency [Hz]') ax.set_title('LIGO-Hanford strain data') ax.colorbar(cmap='viridis', norm='log', clim=(1e-23, 1e-19), label=r'Strain noise [1/\rtHz]') plot.add_segments_bar(h1segs) plot.show()
# Now, we can loop through the active segments of ``'H1_DATA'`` and fetch the # strain `TimeSeries` for each segment, calculating a # :class:`~gwpy.spectrogram.Spectrogram` for each segment. from gwpy.timeseries import TimeSeries spectrograms = [] for start, end in h1segs.active: h1strain = TimeSeries.fetch_open_data('H1', start, end, verbose=True) specgram = h1strain.spectrogram(30, fftlength=4)**(1 / 2.) spectrograms.append(specgram) # Finally, we can build a :meth:`~gwpy.spectrogram.Spectrogram.plot`: from gwpy.plot import Plot plot = Plot(figsize=(12, 6)) ax = plot.gca() for specgram in spectrograms: ax.imshow(specgram) ax.set_xscale('auto-gps', epoch='Sep 16 2010') ax.set_xlim('Sep 16 2010', 'Sep 17 2010') ax.set_ylim(40, 2000) ax.set_yscale('log') ax.set_ylabel('Frequency [Hz]') ax.set_title('LIGO-Hanford strain data') ax.colorbar(cmap='viridis', norm='log', clim=(1e-23, 1e-19), label=r'Strain noise [1/\rtHz]') plot.add_segments_bar(h1segs) plot.show()
def cluster_plotter(channels, start, stop, prefix='.', label='kmeans-labels', groups=None, filename=DEFAULT_FILENAME, dqflag='L1:DMT-ANALYSIS_READY:1', xscale=None, unit=None, progressbar=True, **kwargs): """ Plots data with clusters labeled by color in the working directory, or a relative path given by prefix. Requires a .hdf5 file produced with a clustering function defined in this module to be in the working directory. **kwargs are forwarded to TimeSeries.plot(). :param prefix: relative path to output images. :param label: name attribute of labels TimeSeries saved in filename. :param groups: groups of channels to plot in the same figure. See the example. :param dqflag: data quality flag for segments bar. :param xscale: gps x-axis scale to use. :param unit: override y-axis unit. :param progressbar: show progress bar. >>> from gwpy.time import tconvert, from_gps >>> from datetime import timedelta >>> from cluster import cluster_plotter >>> >>> channels = [f'L1:ISI-GND_STS_ETMX_Z_BLRMS_1_3.mean,m-trend', 'L1:ISI-GND_STS_ETMY_Z_BLRMS_1_3.mean,m-trend'] >>> groups = [[channels, ('ETMX', 'ETMY'), 'L1:ISI-GND_STS_BLRMS_1_3 Z-axis']] # plot on the same figure. >>> >>> stop = from_gps(60 * (int(tconvert('now')) // 60)) # gets nearest minute to now >>> start = stop - timedelta(days=1) # cluster the past day >>> cluster_plotter(channels, start, stop, filename='my_kmeans.hdf5', groups=groups) """ # some defaults. if not kwargs: kwargs['color'] = 'k' kwargs['alpha'] = 0.3 if groups is None: groups = channels # read the data from the save file. data = TimeSeriesDict.read(filename, channels + [label], start=to_gps(start), end=to_gps(stop)) logger.info(f'Read {start} to {stop} from {filename}') # get segments for the duration specified. Note that this may require doing `ligo-proxy-init -p`. logger.debug(f'Getting segments for {dqflag} from {start} to {stop}...') dq = DataQualityFlag.query(dqflag, to_gps(start), to_gps(stop)) logger.info(f'Got segments for {dqflag} from {start} to {stop}.') # plotting is slow, so show a nice progress bar. logger.debug('Initiating plotting routine...') with Progress('plotting', len(channels), quiet=not progressbar) as progress: for p, (group, labels, title) in enumerate(groups): # plot the group in one figure. plt = Plot(*(data[channel] for channel in group), separate=True, sharex=True, zorder=1, **kwargs) # modify the axes one by one. axes = plt.get_axes() for i, ax in enumerate(axes): # namely, add a colored overlay that indicates clustering labels. ax.scatter(data[group[i]].times, data[group[i]].value, c=[colors[j] for j in data[label]], edgecolor='', s=4, zorder=2) ax.set_ylabel( f'{labels[i]} {data[group[i]].unit if unit is None else unit}' ) setp(ax.get_xticklabels(), visible=False) # modify the figure as a whole. plt.add_segments_bar(dq, label='') if xscale is not None: plt.gca().set_xscale(xscale) plt.suptitle(title) # save to png. progress(plt.save, p, get_path(title, 'png', prefix=prefix)) logger.info(f'Completed plotting for {start} to {stop} from {filename}')