Commit 4073d2bb authored by Kipp Cannon's avatar Kipp Cannon
Browse files

snglcoinc: new LR .__call__() API

- rely on *args, **kwargs instead of passing a "params" blob around
parent b5a49bcf
......@@ -51,7 +51,7 @@ from git_version import version as __version__
#
def assign_likelihood_ratios(connection, coinc_def_id, offset_vectors, vetoseglists, events_func, veto_func, ln_likelihood_ratio_func, likelihood_params_func, verbose = False, params_func_extra_args = ()):
def assign_likelihood_ratios(connection, coinc_def_id, offset_vectors, vetoseglists, events_func, veto_func, ln_likelihood_ratio_func, verbose = False):
"""
Assigns likelihood ratio values to coincidences.
"""
......@@ -77,8 +77,7 @@ def assign_likelihood_ratios(connection, coinc_def_id, offset_vectors, vetosegli
def ln_likelihood_ratio(coinc_event_id, time_slide_id):
try:
params = likelihood_params_func([event for event in events_func(cursor, coinc_event_id) if veto_func(event, vetoseglists)], offset_vectors[time_slide_id], *params_func_extra_args)
return ln_likelihood_ratio_func(params) if params is not None else None
return ln_likelihood_ratio_func([event for event in events_func(cursor, coinc_event_id) if veto_func(event, vetoseglists)], offset_vectors[time_slide_id])
except:
traceback.print_exc()
raise
......@@ -109,7 +108,7 @@ WHERE
cursor.close()
def assign_likelihood_ratios_xml(xmldoc, coinc_def_id, offset_vectors, vetoseglists, events_func, veto_func, ln_likelihood_ratio_func, likelihood_params_func, verbose = False, params_func_extra_args = ()):
def assign_likelihood_ratios_xml(xmldoc, coinc_def_id, offset_vectors, vetoseglists, events_func, veto_func, ln_likelihood_ratio_func, verbose = False):
"""
Assigns likelihood ratio values to coincidences (XML version).
"""
......@@ -129,8 +128,7 @@ def assign_likelihood_ratios_xml(xmldoc, coinc_def_id, offset_vectors, vetosegli
progressbar.increment()
if coinc_event.coinc_def_id != coinc_def_id:
continue
params = likelihood_params_func([event for event in events_func(None, coinc_event.coinc_event_id) if veto_func(event, vetoseglists)], offset_vectors[coinc_event.time_slide_id], *params_func_extra_args)
coinc_event.likelihood = ln_likelihood_ratio_func(params) if params is not None else None
coinc_event.likelihood = ln_likelihood_ratio_func([event for event in events_func(None, coinc_event.coinc_event_id) if veto_func(event, vetoseglists)], offset_vectors[coinc_event.time_slide_id])
del progressbar
......@@ -166,11 +164,9 @@ def sngl_burst_veto_func(event, vetoseglists):
return event.ifo not in vetoseglists or event.peak not in vetoseglists[event.ifo]
def ligolw_burca2(database, ln_likelihood_ratio, params_func, verbose = False, params_func_extra_args = ()):
def ligolw_burca2(database, ln_likelihood_ratio_func, verbose = False):
"""
Assigns likelihood ratio values to excess power coincidences.
database is lalburst.SnglBurstUtils.CoincDatabase instance, and
ln_likelihood_ratio is a LnLikelihoodRatio class instance.
"""
#
# Run core function
......@@ -183,10 +179,8 @@ def ligolw_burca2(database, ln_likelihood_ratio, params_func, verbose = False, p
vetoseglists = database.vetoseglists,
events_func = lambda cursor, coinc_event_id: sngl_burst_events_func(cursor, coinc_event_id, database.sngl_burst_table.row_from_cols),
veto_func = sngl_burst_veto_func,
ln_likelihood_ratio_func = ln_likelihood_ratio,
likelihood_params_func = params_func,
verbose = verbose,
params_func_extra_args = params_func_extra_args
ln_likelihood_ratio_func = ln_likelihood_ratio_func,
verbose = verbose
)
#
......
......@@ -1535,12 +1535,12 @@ class LnLRDensity(object):
histogram of the candidates observed in a search.
Typically, the ranking statistic implementation will provide a
function to transform a candidate to a "params" object for use with
the .__call__() implementation, and so in this way a LnLRDensity
object is generally only meaningful in the context of the ranking
function to transform a candidate to the arguments to use with the
.__call__() implementation, and so in this way a LnLRDensity object
is generally only meaningful in the context of the ranking
statistic class for which it has been constructed.
"""
def __call__(self, params):
def __call__(self, *args, **kwargs):
"""
Evaluate. Return the natural logarithm of the density
evaluated at the given parameters.
......@@ -1553,10 +1553,10 @@ class LnLRDensity(object):
"""
raise NotImplementedError
def increment(self, params, weight = 1.0):
def increment(self, *args, **kwargs):
"""
Increment the counts defining this density by weight
(default = 1) at the given parameters.
Increment the counts defining this density at the given
parameters.
"""
raise NotImplementedError
......@@ -2120,7 +2120,7 @@ class LnLikelihoodRatioMixin(object):
warnings.warn("inf/inf encountered")
return lnP_signal - lnP_noise
def ln_lr_samples(self, random_params_seq, sampler_coinc_params = None, **kwargs):
def ln_lr_samples(self, random_params_seq, sampler_coinc_params = None):
"""
Generator that yields an unending sequence of 3-element
tuples. Each tuple's elements are a value of the natural
......@@ -2139,15 +2139,16 @@ class LnLikelihoodRatioMixin(object):
the two populations.
random_params_seq is a sequence (generator is OK) yielding
2-element tuples whose first element is a choice of
parameter values and whose second element is the natural
3-element tuples whose first two elements provide the *args
and **kwargs values passed to the numerator and denominator
density functions, and whose thrid element is the natural
logarithm of the probability density from which the
parameters have been drawn evaluated at the parameters.
On each iteration, the sample of parameter values yielded
by random_params_seq is passed to our own .__call__()
method to evalute the log likelihood ratio at that choice
of parameter values. If sampler_coinc_params is None the
On each iteration, the *args and **kwargs values yielded by
random_params_seq is passed to our own .__call__() method
to evalute the log likelihood ratio at that choice of
parameter values. If sampler_coinc_params is None the
parameters are also passed to the .__call__() mehods of the
.numerator and .denominator attributes of self to obtain
the signal and noise population densities at those
......@@ -2166,7 +2167,7 @@ class LnLikelihoodRatioMixin(object):
else:
lnP_signal_func = sampler_coinc_params.numerator
lnP_noise_func = sampler_coinc_params.denominator
for params, lnP_params in random_params_seq:
lnP_signal = lnP_signal_func(params, **kwargs)
lnP_noise = lnP_noise_func(params, **kwargs)
yield self(params, **kwargs), lnP_signal - lnP_params, lnP_noise - lnP_params
for args, kwargs, lnP_params in random_params_seq:
lnP_signal = lnP_signal_func(*args, **kwargs)
lnP_noise = lnP_noise_func(*args, **kwargs)
yield self(*args, **kwargs), lnP_signal - lnP_params, lnP_noise - lnP_params
......@@ -109,17 +109,14 @@ InspiralCoincDef = lsctables.CoincDef(search = u"inspiral", search_coinc_type =
class InspiralCoincTables(snglcoinc.CoincTables):
def __init__(self, xmldoc, likelihood_func = None, likelihood_params_func = None):
def __init__(self, xmldoc, likelihood_func = None):
snglcoinc.CoincTables.__init__(self, xmldoc)
#
# configure the likelihood ratio evaluator
#
if likelihood_func is None and likelihood_params_func is not None or likelihood_func is not None and likelihood_params_func is None:
raise ValueError("must provide both a likelihood function and a parameter function or neither")
self.likelihood_func = likelihood_func
self.likelihood_params_func = likelihood_params_func
#
# find the coinc_inspiral table or create one if not found
......@@ -177,7 +174,7 @@ class InspiralCoincTables(snglcoinc.CoincTables):
#
if self.likelihood_func is not None:
coinc.likelihood = self.likelihood_func(self.likelihood_params_func(events, offsetvector))
coinc.likelihood = self.likelihood_func(events, offsetvector)
#
# done
......@@ -343,7 +340,6 @@ def ligolw_thinca(
seglists = None,
veto_segments = None,
likelihood_func = None,
likelihood_params_func = None,
min_instruments = 2,
min_log_L = None,
verbose = False
......@@ -363,7 +359,7 @@ def ligolw_thinca(
if verbose:
print("indexing ...", file=sys.stderr)
coinc_tables = InspiralCoincTables(xmldoc, likelihood_func = likelihood_func, likelihood_params_func = likelihood_params_func)
coinc_tables = InspiralCoincTables(xmldoc, likelihood_func = likelihood_func)
coinc_def_id = ligolw_coincs.get_coinc_def_id(xmldoc, coinc_definer_row.search, coinc_definer_row.search_coinc_type, create_new = True, description = coinc_definer_row.description)
instruments = set(coinc_tables.time_slide_table.getColumnByName("instrument"))
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment