gstlal 1.13.0
Loading...
Searching...
No Matches
util.py
Go to the documentation of this file.
1# Copyright (C) 2013-2016 Kipp Cannon
2# Copyright (C) 2015 Chad Hanna
3#
4# This program is free software; you can redistribute it and/or modify it
5# under the terms of the GNU General Public License as published by the
6# Free Software Foundation; either version 2 of the License, or (at your
7# option) any later version.
8#
9# This program is distributed in the hope that it will be useful, but
10# WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
12# Public License for more details.
13#
14# You should have received a copy of the GNU General Public License along
15# with this program; if not, write to the Free Software Foundation, Inc.,
16# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17#
18
19
31
32
33import math
34import numpy
35import re
36from matplotlib.colors import hex2color
37
38
39golden_ratio = (1. + math.sqrt(5.)) / 2.
40
41
42def colour_from_instruments(instruments, colours = {
43 "G1": numpy.array(hex2color('#222222')),
44 "H1": numpy.array(hex2color('#ee0000')),
45 "L1": numpy.array(hex2color('#4ba6ff')),
46 "V1": numpy.array(hex2color('#9b59b6')),
47 "K1": numpy.array(hex2color('#ffb200')),
48 "E1": numpy.array((1.0, 0.0, 0.0)),
49 "E2": numpy.array((0.0, 0.8, 0.0)),
50 "E3": numpy.array((1.0, 0.0, 1.0)),
51}):
52 # mix colours additively
53 colour = sum(map(colours.__getitem__, instruments))
54 # use single-instrument colours as-given
55 if len(instruments) > 1:
56 # desaturate
57 colour += len(instruments) - 1
58 # normalize
59 colour /= colour.max()
60 return colour
61
62
63#
64# =============================================================================
65#
66# TeX Helpers
67#
68# =============================================================================
69#
70
71
72floatpattern = re.compile("([+-]?[.0-9]+)[Ee]([+-]?[0-9]+)")
73
74def latexnumber(s):
75 """
76 Convert a string of the form "d.dddde-dd" to "d.dddd \\times
77 10^{-dd}". Strings that contain neither an "e" nor an "E" are
78 returned unchanged.
79
80 Example:
81
82 >>> import math
83 >>> latexnumber("%.12g" % (math.pi * 1e18))
84 '3.14159265359 \\\\times 10^{18}'
85 """
86 if "e" not in s and "E" not in s:
87 return s
88 m, e = floatpattern.match(s).groups()
89 return r"%s \times 10^{%d}" % (m, int(e))
90
91
92def latexfilename(s):
93 """
94 Escapes "\\" and "_" characters, and replaces " " with "~"
95 (non-breaking space).
96 """
97 return s.replace("\\", "\\\\").replace("_", "\\_").replace(" ", "~")