Commit c2697a01 authored by Amir Mohammadi's avatar Amir Mohammadi

lint

parent dfd1ea75
Pipeline #8620 passed with stages
in 7 minutes and 43 seconds
...@@ -66,12 +66,13 @@ def min_cllr(negatives, positives): ...@@ -66,12 +66,13 @@ def min_cllr(negatives, positives):
pos = sorted(positives) pos = sorted(positives)
N = len(neg) N = len(neg)
P = len(pos) P = len(pos)
I = N+P I = N + P
# now, iterate through both score sets and add a 0 for negative and 1 for positive scores # now, iterate through both score sets and add a 0 for negative and 1 for
n, p = 0,0 # positive scores
n, p = 0, 0
ideal = numpy.zeros(I) ideal = numpy.zeros(I)
neg_indices = [0]*N neg_indices = [0] * N
pos_indices = [0]*P pos_indices = [0] * P
for i in range(I): for i in range(I):
if p < P and (n == N or neg[n] > pos[p]): if p < P and (n == N or neg[n] > pos[p]):
pos_indices[p] = i pos_indices[p] = i
...@@ -88,12 +89,12 @@ def min_cllr(negatives, positives): ...@@ -88,12 +89,12 @@ def min_cllr(negatives, positives):
# disable runtime warnings for a short time since log(0) will raise a warning # disable runtime warnings for a short time since log(0) will raise a warning
old_warn_setup = numpy.seterr(divide='ignore') old_warn_setup = numpy.seterr(divide='ignore')
# ... compute logs # ... compute logs
posterior_log_odds = numpy.log(popt)-numpy.log(1.-popt); posterior_log_odds = numpy.log(popt) - numpy.log(1. - popt)
log_prior_odds = math.log(float(P)/float(N)); log_prior_odds = math.log(float(P) / float(N))
# ... activate old warnings # ... activate old warnings
numpy.seterr(**old_warn_setup) numpy.seterr(**old_warn_setup)
llrs = posterior_log_odds - log_prior_odds; llrs = posterior_log_odds - log_prior_odds
# some weired addition # some weired addition
# for i in range(I): # for i in range(I):
......
This diff is collapsed.
This diff is collapsed.
...@@ -55,7 +55,8 @@ def open_file(filename, mode='rt'): ...@@ -55,7 +55,8 @@ def open_file(filename, mode='rt'):
tar_info = tar.next() tar_info = tar.next()
# check that one file was found in the archive # check that one file was found in the archive
if tar_info is None: if tar_info is None:
raise IOError("The given file is a .tar file, but it does not contain any file.") raise IOError(
"The given file is a .tar file, but it does not contain any file.")
# open the file for reading # open the file for reading
return tar.extractfile(tar_info) return tar.extractfile(tar_info)
...@@ -348,7 +349,6 @@ def cmc(filename, ncolumns=None): ...@@ -348,7 +349,6 @@ def cmc(filename, ncolumns=None):
return cmc_five_column(filename) return cmc_five_column(filename)
def load_score(filename, ncolumns=None, minimal=False, **kwargs): def load_score(filename, ncolumns=None, minimal=False, **kwargs):
"""Load scores using numpy.loadtxt and return the data as a numpy array. """Load scores using numpy.loadtxt and return the data as a numpy array.
...@@ -506,17 +506,19 @@ def _iterate_score_file(filename): ...@@ -506,17 +506,19 @@ def _iterate_score_file(filename):
yield splits yield splits
def _split_scores(score_lines, real_id_index, claimed_id_index = 0, score_index = -1): def _split_scores(score_lines, real_id_index, claimed_id_index=0, score_index=-1):
"""Take the output of :py:func:`four_column` or :py:func:`five_column` and return negatives and positives. """Take the output of :py:func:`four_column` or :py:func:`five_column` and return negatives and positives.
""" """
positives, negatives = [], [] positives, negatives = [], []
for line in score_lines: for line in score_lines:
which = positives if line[claimed_id_index] == line[real_id_index] else negatives which = positives if line[claimed_id_index] == line[
real_id_index] else negatives
which.append(line[score_index]) which.append(line[score_index])
return (numpy.array(negatives), numpy.array(positives)) return (numpy.array(negatives), numpy.array(positives))
def _split_cmc_scores(score_lines, real_id_index, probe_name_index = None, claimed_id_index = 0, score_index = -1):
def _split_cmc_scores(score_lines, real_id_index, probe_name_index=None, claimed_id_index=0, score_index=-1):
"""Takes the output of :py:func:`four_column` or :py:func:`five_column` and return cmc scores. """Takes the output of :py:func:`four_column` or :py:func:`five_column` and return cmc scores.
""" """
if probe_name_index is None: if probe_name_index is None:
...@@ -526,7 +528,8 @@ def _split_cmc_scores(score_lines, real_id_index, probe_name_index = None, claim ...@@ -526,7 +528,8 @@ def _split_cmc_scores(score_lines, real_id_index, probe_name_index = None, claim
neg_dict = {} neg_dict = {}
# read four column list # read four column list
for line in score_lines: for line in score_lines:
which = pos_dict if line[claimed_id_index] == line[real_id_index] else neg_dict which = pos_dict if line[claimed_id_index] == line[
real_id_index] else neg_dict
probe_name = line[probe_name_index] probe_name = line[probe_name_index]
# append score # append score
if probe_name not in which: if probe_name not in which:
...@@ -537,6 +540,8 @@ def _split_cmc_scores(score_lines, real_id_index, probe_name_index = None, claim ...@@ -537,6 +540,8 @@ def _split_cmc_scores(score_lines, real_id_index, probe_name_index = None, claim
probe_names = sorted(set(neg_dict.keys()).union(set(pos_dict.keys()))) probe_names = sorted(set(neg_dict.keys()).union(set(pos_dict.keys())))
# get all scores in the desired format # get all scores in the desired format
return [( return [(
numpy.array(neg_dict[probe_name], numpy.float64) if probe_name in neg_dict else None, numpy.array(neg_dict[probe_name],
numpy.array(pos_dict[probe_name], numpy.float64) if probe_name in pos_dict else None numpy.float64) if probe_name in neg_dict else None,
numpy.array(pos_dict[probe_name],
numpy.float64) if probe_name in pos_dict else None
) for probe_name in probe_names] ) for probe_name in probe_names]
This diff is collapsed.
This diff is collapsed.
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
# Mon 23 May 2011 14:36:14 CEST # Mon 23 May 2011 14:36:14 CEST
def log_values(min_step = -4, counts_per_step = 4): def log_values(min_step=-4, counts_per_step=4):
"""Computes log-scaled values between :math:`10^{M}` and 1 """Computes log-scaled values between :math:`10^{M}` and 1
This function computes log-scaled values between :math:`10^{M}` and 1 This function computes log-scaled values between :math:`10^{M}` and 1
...@@ -32,7 +32,7 @@ def log_values(min_step = -4, counts_per_step = 4): ...@@ -32,7 +32,7 @@ def log_values(min_step = -4, counts_per_step = 4):
""" """
import math import math
return [math.pow(10., i * 1./counts_per_step) for i in range(min_step*counts_per_step,0)] + [1.] return [math.pow(10., i * 1. / counts_per_step) for i in range(min_step * counts_per_step, 0)] + [1.]
def roc(negatives, positives, npoints=100, CAR=False, **kwargs): def roc(negatives, positives, npoints=100, CAR=False, **kwargs):
...@@ -88,12 +88,12 @@ def roc(negatives, positives, npoints=100, CAR=False, **kwargs): ...@@ -88,12 +88,12 @@ def roc(negatives, positives, npoints=100, CAR=False, **kwargs):
from . import roc as calc from . import roc as calc
out = calc(negatives, positives, npoints) out = calc(negatives, positives, npoints)
if not CAR: if not CAR:
return pyplot.plot(100.0*out[0,:], 100.0*out[1,:], **kwargs) return pyplot.plot(100.0 * out[0, :], 100.0 * out[1, :], **kwargs)
else: else:
return pyplot.semilogx(100.0*out[0,:], 100.0*(1-out[1,:]), **kwargs) return pyplot.semilogx(100.0 * out[0, :], 100.0 * (1 - out[1, :]), **kwargs)
def roc_for_far(negatives, positives, far_values = log_values(), **kwargs): def roc_for_far(negatives, positives, far_values=log_values(), **kwargs):
"""Plots the ROC curve for the given list of False Acceptance Rates (FAR). """Plots the ROC curve for the given list of False Acceptance Rates (FAR).
This method will call ``matplotlib`` to plot the ROC curve for a system which This method will call ``matplotlib`` to plot the ROC curve for a system which
...@@ -141,7 +141,7 @@ def roc_for_far(negatives, positives, far_values = log_values(), **kwargs): ...@@ -141,7 +141,7 @@ def roc_for_far(negatives, positives, far_values = log_values(), **kwargs):
from matplotlib import pyplot from matplotlib import pyplot
from . import roc_for_far as calc from . import roc_for_far as calc
out = calc(negatives, positives, far_values) out = calc(negatives, positives, far_values)
return pyplot.semilogx(100.0*out[0,:], 100.0*(1-out[1,:]), **kwargs) return pyplot.semilogx(100.0 * out[0, :], 100.0 * (1 - out[1, :]), **kwargs)
def precision_recall_curve(negatives, positives, npoints=100, **kwargs): def precision_recall_curve(negatives, positives, npoints=100, **kwargs):
...@@ -189,7 +189,7 @@ def precision_recall_curve(negatives, positives, npoints=100, **kwargs): ...@@ -189,7 +189,7 @@ def precision_recall_curve(negatives, positives, npoints=100, **kwargs):
from matplotlib import pyplot from matplotlib import pyplot
from . import precision_recall_curve as calc from . import precision_recall_curve as calc
out = calc(negatives, positives, npoints) out = calc(negatives, positives, npoints)
return pyplot.plot(100.0*out[0,:], 100.0*out[1,:], **kwargs) return pyplot.plot(100.0 * out[0, :], 100.0 * out[1, :], **kwargs)
def epc(dev_negatives, dev_positives, test_negatives, test_positives, def epc(dev_negatives, dev_positives, test_negatives, test_positives,
...@@ -256,7 +256,7 @@ def epc(dev_negatives, dev_positives, test_negatives, test_positives, ...@@ -256,7 +256,7 @@ def epc(dev_negatives, dev_positives, test_negatives, test_positives,
out = calc(dev_negatives, dev_positives, test_negatives, test_positives, out = calc(dev_negatives, dev_positives, test_negatives, test_positives,
npoints) npoints)
return pyplot.plot(out[0,:], 100.0*out[1,:], **kwargs) return pyplot.plot(out[0, :], 100.0 * out[1, :], **kwargs)
def det(negatives, positives, npoints=100, axisfontsize='x-small', **kwargs): def det(negatives, positives, npoints=100, axisfontsize='x-small', **kwargs):
...@@ -374,11 +374,11 @@ def det(negatives, positives, npoints=100, axisfontsize='x-small', **kwargs): ...@@ -374,11 +374,11 @@ def det(negatives, positives, npoints=100, axisfontsize='x-small', **kwargs):
from . import ppndf from . import ppndf
out = calc(negatives, positives, npoints) out = calc(negatives, positives, npoints)
retval = pyplot.plot(out[0,:], out[1,:], **kwargs) retval = pyplot.plot(out[0, :], out[1, :], **kwargs)
# now the trick: we must plot the tick marks by hand using the PPNDF method # now the trick: we must plot the tick marks by hand using the PPNDF method
pticks = [ppndf(float(v)) for v in desiredTicks] pticks = [ppndf(float(v)) for v in desiredTicks]
ax = pyplot.gca() #and finally we set our own tick marks ax = pyplot.gca() # and finally we set our own tick marks
ax.set_xticks(pticks) ax.set_xticks(pticks)
ax.set_xticklabels(desiredLabels, size=axisfontsize) ax.set_xticklabels(desiredLabels, size=axisfontsize)
ax.set_yticks(pticks) ax.set_yticks(pticks)
...@@ -421,9 +421,10 @@ def det_axis(v, **kwargs): ...@@ -421,9 +421,10 @@ def det_axis(v, **kwargs):
# treat input # treat input
try: try:
tv = list(v) #normal input tv = list(v) # normal input
if len(tv) != 4: raise IndexError if len(tv) != 4:
tv = [ppndf(float(k)/100) for k in tv] raise IndexError
tv = [ppndf(float(k) / 100) for k in tv]
cur = pyplot.axis() cur = pyplot.axis()
# limits must be within bounds # limits must be within bounds
...@@ -446,7 +447,7 @@ def det_axis(v, **kwargs): ...@@ -446,7 +447,7 @@ def det_axis(v, **kwargs):
return pyplot.axis(tv, **kwargs) return pyplot.axis(tv, **kwargs)
def cmc(cmc_scores, logx = True, **kwargs): def cmc(cmc_scores, logx=True, **kwargs):
"""Plots the (cumulative) match characteristics and returns the maximum rank. """Plots the (cumulative) match characteristics and returns the maximum rank.
This function plots a CMC curve using the given CMC scores, which can be read This function plots a CMC curve using the given CMC scores, which can be read
...@@ -483,15 +484,14 @@ def cmc(cmc_scores, logx = True, **kwargs): ...@@ -483,15 +484,14 @@ def cmc(cmc_scores, logx = True, **kwargs):
out = calc(cmc_scores) out = calc(cmc_scores)
if logx: if logx:
pyplot.semilogx(range(1, len(out)+1), out * 100, **kwargs) pyplot.semilogx(range(1, len(out) + 1), out * 100, **kwargs)
else: else:
pyplot.plot(range(1, len(out)+1), out * 100, **kwargs) pyplot.plot(range(1, len(out) + 1), out * 100, **kwargs)
return len(out) return len(out)
def detection_identification_curve(cmc_scores, far_values = log_values(), rank def detection_identification_curve(cmc_scores, far_values=log_values(), rank=1, logx=True, **kwargs):
= 1, logx = True, **kwargs):
"""Plots the Detection & Identification curve over the FAR """Plots the Detection & Identification curve over the FAR
This curve is designed to be used in an open set identification protocol, and This curve is designed to be used in an open set identification protocol, and
...@@ -539,16 +539,21 @@ def detection_identification_curve(cmc_scores, far_values = log_values(), rank ...@@ -539,16 +539,21 @@ def detection_identification_curve(cmc_scores, far_values = log_values(), rank
from matplotlib import pyplot from matplotlib import pyplot
from . import far_threshold, detection_identification_rate from . import far_threshold, detection_identification_rate
# for each probe, for which no positives exists, get the highest negative score; and sort them to compute the FAR thresholds # for each probe, for which no positives exists, get the highest negative
negatives = sorted(max(neg) for neg,pos in cmc_scores if (pos is None or not numpy.array(pos).size) and neg is not None) # score; and sort them to compute the FAR thresholds
negatives = sorted(max(neg) for neg, pos in cmc_scores if (
pos is None or not numpy.array(pos).size) and neg is not None)
if not negatives: if not negatives:
raise ValueError("There need to be at least one pair with only negative scores") raise ValueError(
"There need to be at least one pair with only negative scores")
# compute thresholds based on FAR values # compute thresholds based on FAR values
thresholds = [far_threshold(negatives, [], v, True) for v in far_values] thresholds = [far_threshold(negatives, [], v, True) for v in far_values]
# compute detection and identification rate based on the thresholds for the given rank # compute detection and identification rate based on the thresholds for
rates = [100.*detection_identification_rate(cmc_scores, t, rank) for t in thresholds] # the given rank
rates = [
100. * detection_identification_rate(cmc_scores, t, rank) for t in thresholds]
# plot curve # plot curve
if logx: if logx:
......
...@@ -33,11 +33,10 @@ Examples: ...@@ -33,11 +33,10 @@ Examples:
import os import os
import sys import sys
from .eval_threshold import apthres
import bob.core import bob.core
logger = bob.core.log.setup("bob.measure") logger = bob.core.log.setup("bob.measure")
from .eval_threshold import apthres
def main(user_input=None): def main(user_input=None):
...@@ -51,14 +50,12 @@ def main(user_input=None): ...@@ -51,14 +50,12 @@ def main(user_input=None):
completions = dict( completions = dict(
prog=os.path.basename(sys.argv[0]), prog=os.path.basename(sys.argv[0]),
version=pkg_resources.require('bob.measure')[0].version version=pkg_resources.require('bob.measure')[0].version)
)
args = docopt.docopt( args = docopt.docopt(
__doc__ % completions, __doc__ % completions,
argv=argv, argv=argv,
version=completions['version'], version=completions['version'],)
)
# Sets-up logging # Sets-up logging
verbosity = int(args['--verbose']) verbosity = int(args['--verbose'])
...@@ -67,8 +64,8 @@ def main(user_input=None): ...@@ -67,8 +64,8 @@ def main(user_input=None):
# handles threshold validation # handles threshold validation
try: try:
args['<threshold>'] = float(args['<threshold>']) args['<threshold>'] = float(args['<threshold>'])
except: except Exception:
raise docopt.DocoptExit("cannot convert %s into float for threshold" % \ raise docopt.DocoptExit("cannot convert %s into float for threshold" %
args['<threshold>']) args['<threshold>'])
from ..load import load_score, get_negatives_positives from ..load import load_score, get_negatives_positives
......
This diff is collapsed.
This diff is collapsed.
...@@ -8,26 +8,36 @@ ...@@ -8,26 +8,36 @@
#define BOB_IMPORT_VERSION #define BOB_IMPORT_VERSION
#include <bob.blitz/capi.h> #include <bob.blitz/capi.h>
#include <bob.core/config.h> #include <bob.core/config.h>
#include <bob.math/config.h>
#include <bob.io.base/config.h> #include <bob.io.base/config.h>
#include <bob.math/config.h>
static PyObject *build_version_dictionary() {
static PyObject* build_version_dictionary() { PyObject *retval = PyDict_New();
if (!retval)
PyObject* retval = PyDict_New(); return 0;
if (!retval) return 0;
auto retval_ = make_safe(retval); auto retval_ = make_safe(retval);
if (!dict_steal(retval, "Blitz++", blitz_version())) return 0; if (!dict_steal(retval, "Blitz++", blitz_version()))
if (!dict_steal(retval, "Boost", boost_version())) return 0; return 0;
if (!dict_steal(retval, "Compiler", compiler_version())) return 0; if (!dict_steal(retval, "Boost", boost_version()))
if (!dict_steal(retval, "Python", python_version())) return 0; return 0;
if (!dict_steal(retval, "NumPy", numpy_version())) return 0; if (!dict_steal(retval, "Compiler", compiler_version()))
if (!dict_steal(retval, "HDF5", hdf5_version())) return 0; return 0;
if (!dict_steal(retval, "bob.blitz", bob_blitz_version())) return 0; if (!dict_steal(retval, "Python", python_version()))
if (!dict_steal(retval, "bob.core", bob_core_version())) return 0; return 0;
if (!dict_steal(retval, "bob.math", bob_math_version())) return 0; if (!dict_steal(retval, "NumPy", numpy_version()))
if (!dict_steal(retval, "bob.io.base", bob_io_base_version())) return 0; return 0;
if (!dict_steal(retval, "HDF5", hdf5_version()))
return 0;
if (!dict_steal(retval, "bob.blitz", bob_blitz_version()))
return 0;
if (!dict_steal(retval, "bob.core", bob_core_version()))
return 0;
if (!dict_steal(retval, "bob.math", bob_math_version()))
return 0;
if (!dict_steal(retval, "bob.io.base", bob_io_base_version()))
return 0;
return Py_BuildValue("O", retval); return Py_BuildValue("O", retval);
} }
...@@ -37,45 +47,50 @@ static PyMethodDef module_methods[] = { ...@@ -37,45 +47,50 @@ static PyMethodDef module_methods[] = {
}; };
PyDoc_STRVAR(module_docstr, PyDoc_STRVAR(module_docstr,
"Information about software used to compile the C++ Bob API" "Information about software used to compile the C++ Bob API");
);
#if PY_VERSION_HEX >= 0x03000000 #if PY_VERSION_HEX >= 0x03000000
static PyModuleDef module_definition = { static PyModuleDef module_definition = {PyModuleDef_HEAD_INIT,
PyModuleDef_HEAD_INIT,
BOB_EXT_MODULE_NAME, BOB_EXT_MODULE_NAME,
module_docstr, module_docstr,
-1, -1,
module_methods, module_methods,
0, 0, 0, 0 0,
}; 0,
0,
0};
#endif #endif
static PyObject* create_module (void) { static PyObject *create_module(void) {
# if PY_VERSION_HEX >= 0x03000000 #if PY_VERSION_HEX >= 0x03000000
PyObject* m = PyModule_Create(&module_definition); PyObject *m = PyModule_Create(&module_definition);
auto m_ = make_xsafe(m); auto m_ = make_xsafe(m);
const char* ret = "O"; const char *ret = "O";
# else #else
PyObject* m = Py_InitModule3(BOB_EXT_MODULE_NAME, module_methods, module_docstr); PyObject *m =
const char* ret = "N"; Py_InitModule3(BOB_EXT_MODULE_NAME, module_methods, module_docstr);
# endif const char *ret = "N";
if (!m) return 0; #endif
if (!m)
return 0;
/* register version numbers and constants */ /* register version numbers and constants */
if (PyModule_AddStringConstant(m, "module", BOB_EXT_MODULE_VERSION) < 0) return 0; if (PyModule_AddStringConstant(m, "module", BOB_EXT_MODULE_VERSION) < 0)
return 0;
PyObject* externals = build_version_dictionary(); PyObject *externals = build_version_dictionary();
if (!externals) return 0; if (!externals)
if (PyModule_AddObject(m, "externals", externals) < 0) return 0; return 0;
if (PyModule_AddObject(m, "externals", externals) < 0)
return 0;
return Py_BuildValue(ret, m); return Py_BuildValue(ret, m);
} }
PyMODINIT_FUNC BOB_EXT_ENTRY_NAME (void) { PyMODINIT_FUNC BOB_EXT_ENTRY_NAME(void) {
# if PY_VERSION_HEX >= 0x03000000 #if PY_VERSION_HEX >= 0x03000000
return return
# endif #endif
create_module(); create_module();
} }
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