Commit 4fa89b84 authored by Alex Codoreanu's avatar Alex Codoreanu Committed by Patrick Clearwater
Browse files

Add CI/CD YAML file; final PEP8 changes

parent e4a1a5f9
check pep8 standards:
image: python:2.7
before_script:
- /usr/local/bin/python -m pip install --upgrade pip
- pip install yapf
script:
- yapf --recursive --diff --style="pep8" gstlal-spiir
only:
- merge_requests
check clang standards:
image: ubuntu
before_script:
- apt-get update && apt-get install -y clang-format-6.0 git-core
script:
- echo `which clang-format-6.0`
- ln -s `which clang-format-6.0` /usr/bin/clang-format
- cd gstlal-spiir
- linter_errors=$(git-clang-format-6.0 --commit "HEAD~1" -q --diff | grep -v --color=never "no modified files to format" || true)
- echo "$linter_errors"
- if [ ! -z "$linter_errors" ]; then echo "Detected clang formatting issues; please fix"; exit 1; else echo "Clang formatting is correct"; exit 0; fi
only:
- merge_requests
......@@ -34,40 +34,54 @@ Public-domain cartographic data is courtesy of Natural Earth
#
#This program is edited by Manoj(PhD) and Chichi(Post-Doc).
# Command line interface
## EDIT: added colorbar label
import argparse
from lalinference.bayestar import command
parser = command.ArgumentParser(parents=[command.figure_parser])
parser.add_argument('--annotate',
default=False,
action='store_true',
help='annotate plot with information about the event')
parser.add_argument(
'--annotate', default=False, action='store_true',
help='annotate plot with information about the event')
parser.add_argument(
'--contour', metavar='PERCENT', type=float, nargs='+',
'--contour',
metavar='PERCENT',
type=float,
nargs='+',
help='plot contour enclosing this percentage of'
' probability mass [may be specified multiple times, default: none]')
parser.add_argument('--colorbar',
default=False,
action='store_true',
help='Show colorbar [default: %(default)s]')
parser.add_argument('--label', type=str, help='colorbar label')
parser.add_argument('--radec',
nargs=2,
metavar='deg',
type=float,
action='append',
default=[],
help='right ascension (deg) and declination (deg) to mark'
' [may be specified multiple times, default: none]')
parser.add_argument(
'--colorbar', default=False, action='store_true',
help='Show colorbar [default: %(default)s]')
parser.add_argument(
'--label',type=str,
help='colorbar label')
parser.add_argument(
'--radec', nargs=2, metavar='deg', type=float, action='append',
default=[], help='right ascension (deg) and declination (deg) to mark'
' [may be specified multiple times, default: none]')
parser.add_argument(
'--inj-database', metavar='FILE.sqlite', type=command.SQLiteType('r'),
'--inj-database',
metavar='FILE.sqlite',
type=command.SQLiteType('r'),
help='read injection positions from database [default: none]')
parser.add_argument(
'--geo', action='store_true', default=False,
'--geo',
action='store_true',
default=False,
help='Plot in geographic coordinates, (lat, lon) instead of (RA, Dec)'
' [default: %(default)s]')
parser.add_argument(
'input', metavar='INPUT.fits[.gz]', type=argparse.FileType('rb'),
default='-', nargs='?', help='Input FITS file [default: stdin]')
parser.add_argument('input',
metavar='INPUT.fits[.gz]',
type=argparse.FileType('rb'),
default='-',
nargs='?',
help='Input FITS file [default: stdin]')
opts = parser.parse_args()
# Late imports
......@@ -91,8 +105,8 @@ skymap, metadata = fits.read_sky_map(opts.input.name, nest=None)
nside = hp.npix2nside(len(skymap))
if opts.geo:
dlon = -lal.GreenwichMeanSiderealTime(
lal.LIGOTimeGPS(metadata['gps_time'])) % (2*np.pi)
dlon = -lal.GreenwichMeanSiderealTime(lal.LIGOTimeGPS(
metadata['gps_time'])) % (2 * np.pi)
else:
dlon = 0
......@@ -104,8 +118,11 @@ probperdeg2 = skymap
# Plot sky map.
vmax = probperdeg2.max()
vmin = probperdeg2.min()
plot.healpix_heatmap(
probperdeg2, dlon=dlon, nest=metadata['nest'], vmin=vmin, vmax=vmax)
plot.healpix_heatmap(probperdeg2,
dlon=dlon,
nest=metadata['nest'],
vmin=vmin,
vmax=vmax)
# Add colorbar.
if opts.colorbar:
......@@ -116,16 +133,19 @@ if opts.colorbar:
## EDIT: changed colour of the contour lines
if opts.contour:
cls = 100 * postprocess.find_greedy_credible_levels(skymap)
cs = plot.healpix_contour(
cls, dlon=dlon, nest=metadata['nest'],
colors='g', linewidths=0.2, levels=opts.contour)
cs = plot.healpix_contour(cls,
dlon=dlon,
nest=metadata['nest'],
colors='g',
linewidths=0.2,
levels=opts.contour)
fmt = r'%g\%%' if rcParams['text.usetex'] else '%g%%'
plt.clabel(cs, inline=True)
# Add continents.
if opts.geo:
geojson_filename = os.path.join(
os.path.dirname(plot.__file__), 'ne_simplified_coastline.json')
geojson_filename = os.path.join(os.path.dirname(plot.__file__),
'ne_simplified_coastline.json')
with open(geojson_filename, 'r') as geojson_file:
geojson = json.load(geojson_file)
for shape in geojson['geometries']:
......@@ -140,8 +160,8 @@ if opts.inj_database:
INNER JOIN coinc_event_map AS cm2
ON (cm1.coinc_event_id = cm2.coinc_event_id)
WHERE cm2.event_id = ?'''
(ra, dec), = opts.inj_database.execute(
query, (metadata['objid'],)).fetchall()
(ra, dec), = opts.inj_database.execute(query,
(metadata['objid'], )).fetchall()
radecs.append([ra, dec])
# Add markers (e.g., for injections or external triggers).
......@@ -153,9 +173,12 @@ for ra, dec in radecs:
ra = plot.reference_angle(ra + dlon)
else:
ra = plot.wrapped_angle(ra + dlon)
ax.plot(
ra, dec, 'x',
markerfacecolor='white', markeredgecolor='black', markersize=3)
ax.plot(ra,
dec,
'x',
markerfacecolor='white',
markeredgecolor='black',
markersize=3)
# Add a white outline to all text to make it stand out from the background.
plot.outline_text(ax)
......@@ -170,8 +193,9 @@ if opts.annotate:
text.append('event ID: {}'.format(objid))
if opts.contour:
pp = np.round(opts.contour).astype(int)
ii = np.round(np.searchsorted(np.sort(cls), opts.contour) *
deg2perpix).astype(int)
ii = np.round(
np.searchsorted(np.sort(cls), opts.contour) *
deg2perpix).astype(int)
for i, p in zip(ii, pp):
# FIXME: use Unicode symbol instead of TeX '$^2$'
# because of broken fonts on Scientific Linux 7.
......
......@@ -16,7 +16,6 @@
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
import os
import sys
import re
......@@ -27,10 +26,11 @@ import logging
from optparse import OptionParser
try:
import sqlite3
import sqlite3
except ImportError:
# pre 2.5.x
from pysqlite2 import dbapi2 as sqlite3
# pre 2.5.x
from pysqlite2 import dbapi2 as sqlite3
sqlite3.enable_callback_tracebacks(True)
from glue.ligolw import dbtables
#from glue.ligolw.utils import process as ligolw_process
......@@ -42,105 +42,147 @@ from glue import iterutils
from gstlal import cohfar
import pdb
logging.basicConfig(format='%(asctime)s %(message)s', level=logging.DEBUG)
logging.basicConfig(format='%(asctime)s %(message)s', level = logging.DEBUG)
class DefaultContentHandler(ligolw.LIGOLWContentHandler):
pass
param.use_in(DefaultContentHandler)
lsctables.use_in(DefaultContentHandler)
def append_missing_options(missing_options, options, option):
if getattr(options, option) is None:
missing_options.append("--%s" % option.replace("_", "-"))
if getattr(options, option) is None:
missing_options.append("--%s" % option.replace("_", "-"))
def parse_command_line():
parser = OptionParser(usage = "%prog [options] [file ...]", description = "%prog to assign FAR to a sql file")
# generic data source options
parser.add_option("--input-stats-filename", default = "", metavar = "name", help = "Set the input stats file name")
parser.add_option("--database", default = "", metavar = "name", help = "Set the input sql file name")
parser.add_option("--livetime", default = None, help ="input coinc time")
parser.add_option("--mul-factor", default = 1, help ="input multiplication factor to combine far.")
parser.add_option("--verbose", "-v", action = "store_true", help = "Be verbose.")
parser.add_option("--segments-file", default = None, metavar = "name", help = "Set the input segments file name")
parser.add_option("--segments-name", default = None, metavar = "name", help = "Set the input segments file name")
parser.add_option("--vetoes-file", default = None, metavar = "name", help = "Set the input vetoes file name")
parser.add_option("--vetoes-name", default = None, metavar = "name", help = "Set the input vetoes name in the vetoes file")
parser.add_option("--shrink-data-segments", metavar = "float", type = float, help = "Shrink data segments by this much (on each side). e.g. specifying 2 will shrink each datasegment by 4 seconds")
parser.add_option("--extend-veto-segments", metavar = "float", type = float, help = "Pad segments by this number of seconds (on both sides)")
options, tmp = parser.parse_args()
missing_options = []
append_missing_options(missing_options, options, "database")
append_missing_options(missing_options, options, "input_stats_filename")
if not options.livetime and not (options.segments_file and options.segments_name):
raise ValueError, "specify live time or segments file and name"
if missing_options:
raise ValueError, "missing required option(s) %s" % ", ".join(sorted(missing_options))
return options
parser = OptionParser(usage="%prog [options] [file ...]",
description="%prog to assign FAR to a sql file")
# generic data source options
parser.add_option("--input-stats-filename",
default="",
metavar="name",
help="Set the input stats file name")
parser.add_option("--database",
default="",
metavar="name",
help="Set the input sql file name")
parser.add_option("--livetime", default=None, help="input coinc time")
parser.add_option("--mul-factor",
default=1,
help="input multiplication factor to combine far.")
parser.add_option("--verbose",
"-v",
action="store_true",
help="Be verbose.")
parser.add_option("--segments-file",
default=None,
metavar="name",
help="Set the input segments file name")
parser.add_option("--segments-name",
default=None,
metavar="name",
help="Set the input segments file name")
parser.add_option("--vetoes-file",
default=None,
metavar="name",
help="Set the input vetoes file name")
parser.add_option("--vetoes-name",
default=None,
metavar="name",
help="Set the input vetoes name in the vetoes file")
parser.add_option(
"--shrink-data-segments",
metavar="float",
type=float,
help=
"Shrink data segments by this much (on each side). e.g. specifying 2 will shrink each datasegment by 4 seconds"
)
parser.add_option(
"--extend-veto-segments",
metavar="float",
type=float,
help="Pad segments by this number of seconds (on both sides)")
options, tmp = parser.parse_args()
missing_options = []
append_missing_options(missing_options, options, "database")
append_missing_options(missing_options, options, "input_stats_filename")
if not options.livetime and not (options.segments_file
and options.segments_name):
raise ValueError, "specify live time or segments file and name"
if missing_options:
raise ValueError, "missing required option(s) %s" % ", ".join(
sorted(missing_options))
return options
options = parse_command_line()
options = parse_command_line()
zerolag_sql_list = options.database.split(",")
for filename in zerolag_sql_list:
# read in the stats for corresponding ifos
# FIXME: ifos read from filename hard-coded
ifos = filename.split("/")[-1].split("_")[0].split("-")[0]
if options.verbose:
logging.info("reading stats file %s" % options.input_stats_filename)
ranking_stats = cohfar.RankingData(options.input_stats_filename, ifos, hist_trials = 100)
pdb.set_trace()
working_filename = dbtables.get_connection_filename(filename, tmp_path = None, verbose = options.verbose)
if options.verbose:
logging.info("reading zerolag file %s" % working_filename)
zlag_connection = sqlite3.connect(working_filename)
if options.livetime:
livetime = float(options.livetime)
elif options.segments_file:
on_instruments = set(re.findall(r"[A-Z][0-9]", ifos))
livetime = cohfar.get_livetime(options, on_instruments, verbose = options.verbose)
else:
raise ValueError, "missing livetime, either from options.livetime or from options.segment_file"
myfar = cohfar.FAPFAR(ranking_stats, zlag_connection, livetime = livetime)
#
# record our passage
#
# xmldoc = dbtables.get_xml(connection)
# process = ligolw_process.register_to_xmldoc(xmldoc, u"gstlal_cohfar_assign_far", {})
#
#
# assign FAPs and FARs
#
if options.verbose:
logging.info("assign fars to zerolags")
myfar.assign_fars_sql(zlag_connection, mul_factor = options.mul_factor)
#
# done, restore file to original location
#
# ligolw_process.set_process_end_time(process)
# connection.cursor().execute("UPDATE process SET end_time = ? WHERE process_id == ?", (process.end_time, process.process_id))
zlag_connection.commit()
zlag_connection.close()
dbtables.put_connection_filename(filename, working_filename, verbose = options.verbose)
# read in the stats for corresponding ifos
# FIXME: ifos read from filename hard-coded
ifos = filename.split("/")[-1].split("_")[0].split("-")[0]
if options.verbose:
logging.info("reading stats file %s" % options.input_stats_filename)
ranking_stats = cohfar.RankingData(options.input_stats_filename,
ifos,
hist_trials=100)
pdb.set_trace()
working_filename = dbtables.get_connection_filename(
filename, tmp_path=None, verbose=options.verbose)
if options.verbose:
logging.info("reading zerolag file %s" % working_filename)
zlag_connection = sqlite3.connect(working_filename)
if options.livetime:
livetime = float(options.livetime)
elif options.segments_file:
on_instruments = set(re.findall(r"[A-Z][0-9]", ifos))
livetime = cohfar.get_livetime(options,
on_instruments,
verbose=options.verbose)
else:
raise ValueError, "missing livetime, either from options.livetime or from options.segment_file"
myfar = cohfar.FAPFAR(ranking_stats, zlag_connection, livetime=livetime)
#
# record our passage
#
# xmldoc = dbtables.get_xml(connection)
# process = ligolw_process.register_to_xmldoc(xmldoc, u"gstlal_cohfar_assign_far", {})
#
#
# assign FAPs and FARs
#
if options.verbose:
logging.info("assign fars to zerolags")
myfar.assign_fars_sql(zlag_connection, mul_factor=options.mul_factor)
#
# done, restore file to original location
#
# ligolw_process.set_process_end_time(process)
# connection.cursor().execute("UPDATE process SET end_time = ? WHERE process_id == ?", (process.end_time, process.process_id))
zlag_connection.commit()
zlag_connection.close()
dbtables.put_connection_filename(filename,
working_filename,
verbose=options.verbose)
......@@ -16,7 +16,6 @@
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
import os
import numpy as np
import scipy
......@@ -25,14 +24,16 @@ import csv
import logging
from optparse import OptionParser
import matplotlib
matplotlib.use('Agg')
from matplotlib import pyplot
try:
import sqlite3
import sqlite3
except ImportError:
# pre 2.5.x
from pysqlite2 import dbapi2 as sqlite3
# pre 2.5.x
from pysqlite2 import dbapi2 as sqlite3
sqlite3.enable_callback_tracebacks(True)
from glue import iterutils
......@@ -45,89 +46,103 @@ import subprocess
import pdb
# def postcoh_table_from_xml(filename, contenthandler = DefaultContentHandler, verbose = False):
#
#
# # Load document
# xmldoc = utils.load_filename(filename, contenthandler = contenthandler, verbose = verbose)
#
#
# postcoh = PostcohInspiralTable.get_table(xmldoc)
# return postcoh
def append_missing_options(missing_options, options, option):
if getattr(options, option) is None:
missing_options.append("--%s" % option.replace("_", "-"))
if getattr(options, option) is None:
missing_options.append("--%s" % option.replace("_", "-"))
def parse_command_line():
parser = OptionParser(version = git_version.verbose_msg, usage = "%prog [options] [file ...]", description = "%prog to plot pdf and cdf")
parser.add_option("--output-filename", default = "", metavar = "name", help = "Set the output file name")
parser.add_option("--input-zerolag-filename", default = "", metavar = "name", help = "Set the input stats file name")
parser.add_option("--livetime", type = "float", help ="input coinc time")
parser.add_option("--label", default = "shifted", metavar = "name", help = "Set the output file name")
parser.add_option("--verbose", action = "store_true", help = "Be verbose")
options, tmp = parser.parse_args()
missing_options = []
append_missing_options(missing_options, options, "output_filename")
append_missing_options(missing_options, options, "input_zerolag_filename")
if missing_options:
raise ValueError, "missing required option(s) %s" % ", ".join(sorted(missing_options))
parser = OptionParser(version=git_version.verbose_msg,
usage="%prog [options] [file ...]",
description="%prog to plot pdf and cdf")
parser.add_option("--output-filename",
default="",
metavar="name",
help="Set the output file name")
parser.add_option("--input-zerolag-filename",
default="",
metavar="name",
help="Set the input stats file name")
parser.add_option("--livetime", type="float", help="input coinc time")
parser.add_option("--label",
default="shifted",
metavar="name",
help="Set the output file name")
parser.add_option("--verbose", action="store_true", help="Be verbose")
options, tmp = parser.parse_args()
missing_options = []
append_missing_options(missing_options, options, "output_filename")
append_missing_options(missing_options, options, "input_zerolag_filename")
if missing_options:
raise ValueError, "missing required option(s) %s" % ", ".join(
sorted(missing_options))
return options
return options
options = parse_command_line()
logging.info("count the number of zerolags above a series of IFAR thresholds")
step=50
step = 50
tick_lgifar = np.linspace(-1, 7, step)
theory_lgcevent = np.zeros(len(tick_lgifar))
theory_lgcevent = - tick_lgifar + np.log10(float(options.livetime))
theory_lgcevent = -tick_lgifar + np.log10(float(options.livetime))
pdb.set_trace()
if options.input_zerolag_filename.endswith("xml.gz"):
zerolag_lgcevent_kde = cohfar.count_above_ifar_xml(options.input_zerolag_filename, tick_lgifar)
zerolag_lgcevent_kde = cohfar.count_above_ifar_xml(
options.input_zerolag_filename, tick_lgifar)
elif options.input_zerolag_filename.endswith("sqlite"):
zerolag_fname_list = options.input_zerolag_filename.split(',')
# only deal with one sql file
assert len(zerolag_fname_list) == 1
fname = zerolag_fname_list[0]
working_filename = dbtables.get_connection_filename(fname, tmp_path = None, verbose = options.verbose)
working_filename = dbtables.get_connection_filename(
fname, tmp_path=None, verbose=options.verbose)
zlag_connection = sqlite3.connect(working_filename)
zerolag_lgcevent_kde = cohfar.count_above_ifar_sql(zlag_connection, tick_lgifar)
zerolag_lgcevent_kde = cohfar.count_above_ifar_sql(zlag_connection,
tick_lgifar)
logging.info("plot the numbers of zerolags versus IFAR thresholds")
if 0:
pyplot.figure()
#p1 = pyplot.plot(tick_lgcdf, back_lgcevent)
p2, = pyplot.plot(tick_lgcdf, back_lgfar, label = 'background')
# p3, = pyplot.plot(tick_lgcdf[nonzero_tick:], zerolag_lgcevent[nonzero_tick:], label = '8' r'$\pi$' 'shifted')
pyplot.gca().invert_xaxis()
pyplot.xlabel('lg' r'$d_0$')
pyplot.ylabel('lg FAR' r'($s^{-1}$)')
pyplot.legend()
pyplot.savefig("P0_vs_far.png")
pyplot.figure()
#p1 = pyplot.plot(tick_lgcdf, back_lgcevent)
p2, = pyplot.plot(tick_lgcdf, back_lgfar, label='background')
# p3, = pyplot.plot(tick_lgcdf[nonzero_tick:], zerolag_lgcevent[nonzero_tick:], label = '8' r'$\pi$' 'shifted')
pyplot.gca().invert_xaxis()
pyplot.xlabel('lg' r'$d_0$')