gstlal-inspiral  1.9.3
 All Classes Namespaces Files Functions
segments.py
Go to the documentation of this file.
1 # Copyright (C) 2015 Cody Messick
2 #
3 # This program is free software; you can redistribute it and/or modify it
4 # under the terms of the GNU General Public License as published by the
5 # Free Software Foundation; either version 2 of the License, or (at your
6 # option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful, but
9 # WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
11 # Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License along
14 # with this program; if not, write to the Free Software Foundation, Inc.,
15 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 
17 ## @file
18 
19 ## @package plotsegments
20 
21 import datetime
22 import matplotlib
23 matplotlib.use('agg')
24 import matplotlib.pyplot as plt
25 import matplotlib.ticker as tkr
26 import numpy
27 
28 from ligo import segments
29 from ligo.lw.ligolw import LIGOLWContentHandler
30 from ligo.lw import array as ligolw_array
31 from ligo.lw import lsctables
32 from ligo.lw import param as ligolw_param
33 from ligo.lw import utils as ligolw_utils
34 from ligo.lw.utils import segments as ligolw_segments
35 from gstlal import far
36 import lal
37 
38 #
39 # =============================================================================
40 #
41 # ligo.lw Content Handlers
42 #
43 # =============================================================================
44 #
45 
46 
47 class ligolwcontenthandler(LIGOLWContentHandler):
48  pass
49 ligolw_array.use_in(ligolwcontenthandler)
50 ligolw_param.use_in(ligolwcontenthandler)
51 lsctables.use_in(ligolwcontenthandler)
52 
53 def parse_segments_xml(path):
54  llwsegments = ligolw_segments.LigolwSegments(ligolw_utils.load_filename(path,contenthandler=ligolwcontenthandler))
55  seglistdicts = { #'frame gate': llwsegments.get_by_name(u'framesegments')}
56  'h(t) gate': llwsegments.get_by_name(u'whitehtsegments'),
57  'state vector': llwsegments.get_by_name(u'statevectorsegments'),
58  'trigger buffers': llwsegments.get_by_name(u'triggersegments') }
59  # FIXME This needs to be generalized to more than two IFOs
60  seglistdicts['joint segments'] = {'H1L1': seglistdicts['state vector'].intersection(['H1','L1'])}
61  return seglistdicts
62 
63 
64 def plot_segments_history(seglistdicts, segments_to_plot = ['trigger buffers', 'h(t) gate', 'state vector'], t_max = None, length = 86400., labelspacing = 10800., colors = {'H1': numpy.array((1.0, 0.0, 0.0)), 'L1': numpy.array((0.0, 0.8, 0.0)), 'V1': numpy.array((1.0, 0.0, 1.0)), 'H1L1': numpy.array((.5, .5, .5))}, fig = None, axes = None):
65  if fig is None:
66  fig = plt.figure(figsize=(15,5),)
67  if axes is None:
68  axes = fig.add_subplot(111)
69  # If t_max is specified, cut the segments so they end at t_max,
70  # otherwise set it to the current time
71  if t_max is None:
72  t_max = float(lal.GPSTimeNow())
73  else:
74  seglist_to_drop = segments.segmentlist([segments.segment(lal.LIGOTimeGPS(t_max),segments.PosInfinity)])
75  for seglistdict in seglistdicts.values():
76  for seglist in seglistdict.values():
77  seglist -= seglist_to_drop
78  t_min = t_max - length
79  bottom = []
80  width = []
81  left_edge = []
82  y_ticks = []
83  y_tick_labels = []
84  color_list = []
85  color_dict = {'H1': numpy.array((1.0, 0.0, 0.0)), 'L1': numpy.array((0.0, 0.8, 0.0)), 'V1': numpy.array((1.0, 0.0, 1.0)), 'H1L1': numpy.array((.5, .5, .5))}
86  x_format = tkr.FuncFormatter(lambda x, pos: datetime.datetime(*lal.GPSToUTC(int(x))[:7]).strftime('%Y-%m-%d, %H:%M:%S UTC'))
87  x_ticks = numpy.arange(t_min,t_max+labelspacing, labelspacing)
88 
89  for j, segtype in enumerate(segments_to_plot):
90  for row, (ifo, segmentlist) in enumerate(seglistdicts[segtype].items()):
91  y_ticks.append(row + 2*j + 0.5)
92  y_tick_labels.append('%s %s' % (ifo, segtype))
93  bottom.extend([row + 2*j + 0.25] * len(segmentlist))
94  color_list.extend([color_dict[ifo]] * len(segmentlist))
95  for segment in segmentlist:
96  width.append(float(segment[1]) - float(segment[0]))
97  left_edge.append(float(segment[0]))
98 
99  edgecolor_list = ['w'] * len(bottom)
100  fig.patch.set_alpha(0.0)
101  axes.barh(bottom, width, height=0.5, left=left_edge, color=color_list, edgecolor=edgecolor_list)
102  plt.xlim([t_min, t_max])
103  axes.xaxis.set_major_formatter(x_format)
104  plt.yticks(y_ticks, y_tick_labels)
105  plt.xticks(x_ticks, rotation=10.)
106  axes.tick_params(axis='y',which='both',left='off',right='off')
107  axes.grid(color=(0.1,0.4,0.5), linewidth=2)
108  # FIXME use this grid() when we have a newer matplotlib
109  # axes.grid(color=(0.1,0.4,0.5), linewidth=2, which='both', axis='x')
110  # FIXME Switch to tight_layout() when matplotlib is updated
111  try:
112  fig.tight_layout(pad = .8)
113  return fig, axes
114  except AttributeError:
115  return fig, axes