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):
pos = sorted(positives)
N = len(neg)
P = len(pos)
I = N+P
# now, iterate through both score sets and add a 0 for negative and 1 for positive scores
n, p = 0,0
I = N + P
# now, iterate through both score sets and add a 0 for negative and 1 for
# positive scores
n, p = 0, 0
ideal = numpy.zeros(I)
neg_indices = [0]*N
pos_indices = [0]*P
neg_indices = [0] * N
pos_indices = [0] * P
for i in range(I):
if p < P and (n == N or neg[n] > pos[p]):
pos_indices[p] = i
......@@ -88,12 +89,12 @@ def min_cllr(negatives, positives):
# disable runtime warnings for a short time since log(0) will raise a warning
old_warn_setup = numpy.seterr(divide='ignore')
# ... compute logs
posterior_log_odds = numpy.log(popt)-numpy.log(1.-popt);
log_prior_odds = math.log(float(P)/float(N));
posterior_log_odds = numpy.log(popt) - numpy.log(1. - popt)
log_prior_odds = math.log(float(P) / float(N))
# ... activate old warnings
numpy.seterr(**old_warn_setup)
llrs = posterior_log_odds - log_prior_odds;
llrs = posterior_log_odds - log_prior_odds
# some weired addition
# for i in range(I):
......
This diff is collapsed.
This diff is collapsed.
......@@ -55,7 +55,8 @@ def open_file(filename, mode='rt'):
tar_info = tar.next()
# check that one file was found in the archive
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
return tar.extractfile(tar_info)
......@@ -348,7 +349,6 @@ def cmc(filename, ncolumns=None):
return cmc_five_column(filename)
def load_score(filename, ncolumns=None, minimal=False, **kwargs):
"""Load scores using numpy.loadtxt and return the data as a numpy array.
......@@ -383,29 +383,29 @@ def load_score(filename, ncolumns=None, minimal=False, **kwargs):
if ncolumns == 4:
names = ('claimed_id', 'real_id', 'test_label', 'score')
converters = {
0: convertfunc,
1: convertfunc,
2: convertfunc,
3: float}
0: convertfunc,
1: convertfunc,
2: convertfunc,
3: float}
if minimal:
usecols = (0, 1, 3)
elif ncolumns == 5:
names = ('claimed_id', 'model_label', 'real_id', 'test_label', 'score')
converters = {
0: convertfunc,
1: convertfunc,
2: convertfunc,
3: convertfunc,
4: float}
0: convertfunc,
1: convertfunc,
2: convertfunc,
3: convertfunc,
4: float}
if minimal:
usecols = (0, 2, 4)
else:
raise ValueError("ncolumns of 4 and 5 are supported only.")
score_lines = numpy.genfromtxt(
open_file(filename, mode='rb'), dtype=None, names=names,
converters=converters, invalid_raise=True, usecols=usecols, **kwargs)
open_file(filename, mode='rb'), dtype=None, names=names,
converters=converters, invalid_raise=True, usecols=usecols, **kwargs)
new_dtype = []
for name in score_lines.dtype.names[:-1]:
new_dtype.append((name, str(score_lines.dtype[name]).replace('S', 'U')))
......@@ -506,17 +506,19 @@ def _iterate_score_file(filename):
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.
"""
positives, negatives = [], []
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])
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.
"""
if probe_name_index is None:
......@@ -526,7 +528,8 @@ def _split_cmc_scores(score_lines, real_id_index, probe_name_index = None, claim
neg_dict = {}
# read four column list
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]
# append score
if probe_name not in which:
......@@ -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())))
# get all scores in the desired format
return [(
numpy.array(neg_dict[probe_name], 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
numpy.array(neg_dict[probe_name],
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]
This diff is collapsed.
This diff is collapsed.
......@@ -3,7 +3,7 @@
# 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
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):
"""
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):
......@@ -88,12 +88,12 @@ def roc(negatives, positives, npoints=100, CAR=False, **kwargs):
from . import roc as calc
out = calc(negatives, positives, npoints)
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:
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).
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):
from matplotlib import pyplot
from . import roc_for_far as calc
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):
......@@ -189,11 +189,11 @@ def precision_recall_curve(negatives, positives, npoints=100, **kwargs):
from matplotlib import pyplot
from . import precision_recall_curve as calc
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,
npoints=100, **kwargs):
npoints=100, **kwargs):
"""Plots Expected Performance Curve (EPC) as defined in the paper:
Bengio, S., Keller, M., Mariéthoz, J. (2004). The Expected Performance Curve.
......@@ -255,8 +255,8 @@ def epc(dev_negatives, dev_positives, test_negatives, test_positives,
from . import epc as calc
out = calc(dev_negatives, dev_positives, test_negatives, test_positives,
npoints)
return pyplot.plot(out[0,:], 100.0*out[1,:], **kwargs)
npoints)
return pyplot.plot(out[0, :], 100.0 * out[1, :], **kwargs)
def det(negatives, positives, npoints=100, axisfontsize='x-small', **kwargs):
......@@ -354,7 +354,7 @@ def det(negatives, positives, npoints=100, axisfontsize='x-small', **kwargs):
"0.995", "0.998", "0.999",
"0.9995", "0.9998", "0.9999",
"0.99995", "0.99998", "0.99999"
]
]
desiredLabels = [
"0.001", "0.002", "0.005",
......@@ -366,7 +366,7 @@ def det(negatives, positives, npoints=100, axisfontsize='x-small', **kwargs):
"99.5", "99.8", "99.9",
"99.95", "99.98", "99.99",
"99.995", "99.998", "99.999"
]
]
# this will actually do the plotting
from matplotlib import pyplot
......@@ -374,11 +374,11 @@ def det(negatives, positives, npoints=100, axisfontsize='x-small', **kwargs):
from . import ppndf
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
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_xticklabels(desiredLabels, size=axisfontsize)
ax.set_yticks(pticks)
......@@ -421,9 +421,10 @@ def det_axis(v, **kwargs):
# treat input
try:
tv = list(v) #normal input
if len(tv) != 4: raise IndexError
tv = [ppndf(float(k)/100) for k in tv]
tv = list(v) # normal input
if len(tv) != 4:
raise IndexError
tv = [ppndf(float(k) / 100) for k in tv]
cur = pyplot.axis()
# limits must be within bounds
......@@ -446,7 +447,7 @@ def det_axis(v, **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.
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):
out = calc(cmc_scores)
if logx:
pyplot.semilogx(range(1, len(out)+1), out * 100, **kwargs)
pyplot.semilogx(range(1, len(out) + 1), out * 100, **kwargs)
else:
pyplot.plot(range(1, len(out)+1), out * 100, **kwargs)
pyplot.plot(range(1, len(out) + 1), out * 100, **kwargs)
return len(out)
def detection_identification_curve(cmc_scores, far_values = log_values(), rank
= 1, logx = True, **kwargs):
def detection_identification_curve(cmc_scores, far_values=log_values(), rank=1, logx=True, **kwargs):
"""Plots the Detection & Identification curve over the FAR
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
from matplotlib import pyplot
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
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)
# for each probe, for which no positives exists, get the highest negative
# 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:
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
thresholds = [far_threshold(negatives, [], v, True) for v in far_values]
# compute detection and identification rate based on the thresholds for the given rank
rates = [100.*detection_identification_rate(cmc_scores, t, rank) for t in thresholds]
# compute detection and identification rate based on the thresholds for
# the given rank
rates = [
100. * detection_identification_rate(cmc_scores, t, rank) for t in thresholds]
# plot curve
if logx:
......
......@@ -33,11 +33,10 @@ Examples:
import os
import sys
from .eval_threshold import apthres
import bob.core
logger = bob.core.log.setup("bob.measure")
from .eval_threshold import apthres
def main(user_input=None):
......@@ -51,14 +50,12 @@ def main(user_input=None):
completions = dict(
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(
__doc__ % completions,
argv=argv,
version=completions['version'],
)
version=completions['version'],)
# Sets-up logging
verbosity = int(args['--verbose'])
......@@ -67,9 +64,9 @@ def main(user_input=None):
# handles threshold validation
try:
args['<threshold>'] = float(args['<threshold>'])
except:
raise docopt.DocoptExit("cannot convert %s into float for threshold" % \
args['<threshold>'])
except Exception:
raise docopt.DocoptExit("cannot convert %s into float for threshold" %
args['<threshold>'])
from ..load import load_score, get_negatives_positives
neg, pos = get_negatives_positives(load_score(args['<scores>']))
......
This diff is collapsed.
This diff is collapsed.
......@@ -40,7 +40,7 @@ def test_compute_perf():
DEV_SCORES,
TEST_SCORES,
'--output=' + tmp_output.name,
]
]
from .script.compute_perf import main
nose.tools.eq_(main(cmdline), 0)
......@@ -56,7 +56,7 @@ def test_compute_perf_only_dev():
cmdline = [
DEV_SCORES,
'--output=' + tmp_output.name,
]
]
from .script.compute_perf import main
nose.tools.eq_(main(cmdline), 0)
......@@ -81,7 +81,7 @@ def test_apply_threshold():
cmdline = [
'0.5',
TEST_SCORES,
]
]
from .script.apply_threshold import main
nose.tools.eq_(main(cmdline), 0)
......@@ -99,7 +99,7 @@ def test_compute_perf_5col():
DEV_SCORES_5COL,
TEST_SCORES_5COL,
'--output=' + tmp_output.name,
]
]
from .script.compute_perf import main
nose.tools.eq_(main(cmdline), 0)
......@@ -117,22 +117,22 @@ def test_compute_cmc():
tmp_output = tempfile.NamedTemporaryFile(prefix=__name__, suffix='.pdf')
nose.tools.eq_(main([
SCORES_4COL_CMC,
'--log-x-scale',
'--output=%s' % tmp_output.name,
]), 0)
SCORES_4COL_CMC,
'--log-x-scale',
'--output=%s' % tmp_output.name,
]), 0)
tmp_output = tempfile.NamedTemporaryFile(prefix=__name__, suffix='.pdf')
nose.tools.eq_(main([
SCORES_5COL_CMC,
'--output=%s' % tmp_output.name,
]), 0)
SCORES_5COL_CMC,
'--output=%s' % tmp_output.name,
]), 0)
tmp_output = tempfile.NamedTemporaryFile(prefix=__name__, suffix='.pdf')
nose.tools.eq_(main([
SCORES_4COL_CMC_OS,
'--rank=1',
'--output=%s' % tmp_output.name,
]), 0)
SCORES_4COL_CMC_OS,
'--rank=1',
'--output=%s' % tmp_output.name,
]), 0)
......@@ -8,74 +8,89 @@
#define BOB_IMPORT_VERSION
#include <bob.blitz/capi.h>
#include <bob.core/config.h>
#include <bob.math/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) return 0;
PyObject *retval = PyDict_New();
if (!retval)
return 0;
auto retval_ = make_safe(retval);
if (!dict_steal(retval, "Blitz++", blitz_version())) return 0;
if (!dict_steal(retval, "Boost", boost_version())) return 0;
if (!dict_steal(retval, "Compiler", compiler_version())) return 0;
if (!dict_steal(retval, "Python", python_version())) return 0;
if (!dict_steal(retval, "NumPy", numpy_version())) 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;
if (!dict_steal(retval, "Blitz++", blitz_version()))
return 0;
if (!dict_steal(retval, "Boost", boost_version()))
return 0;
if (!dict_steal(retval, "Compiler", compiler_version()))
return 0;
if (!dict_steal(retval, "Python", python_version()))
return 0;
if (!dict_steal(retval, "NumPy", numpy_version()))
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);
}
static PyMethodDef module_methods[] = {
{0} /* Sentinel */
{0} /* Sentinel */
};
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
static PyModuleDef module_definition = {
PyModuleDef_HEAD_INIT,
BOB_EXT_MODULE_NAME,
module_docstr,
-1,
module_methods,
0, 0, 0, 0
};
static PyModuleDef module_definition = {PyModuleDef_HEAD_INIT,
BOB_EXT_MODULE_NAME,
module_docstr,
-1,
module_methods,
0,
0,
0,
0};
#endif
static PyObject* create_module (void) {
static PyObject *create_module(void) {
# if PY_VERSION_HEX >= 0x03000000
PyObject* m = PyModule_Create(&module_definition);
#if PY_VERSION_HEX >= 0x03000000
PyObject *m = PyModule_Create(&module_definition);
auto m_ = make_xsafe(m);
const char* ret = "O";
# else
PyObject* m = Py_InitModule3(BOB_EXT_MODULE_NAME, module_methods, module_docstr);
const char* ret = "N";
# endif
if (!m) return 0;
const char *ret = "O";
#else
PyObject *m =
Py_InitModule3(BOB_EXT_MODULE_NAME, module_methods, module_docstr);
const char *ret = "N";
#endif
if (!m)
return 0;
/* 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();
if (!externals) return 0;
if (PyModule_AddObject(m, "externals", externals) < 0) return 0;
PyObject *externals = build_version_dictionary();
if (!externals)
return 0;
if (PyModule_AddObject(m, "externals", externals) < 0)
return 0;
return Py_BuildValue(ret, m);
}
PyMODINIT_FUNC BOB_EXT_ENTRY_NAME (void) {
# if PY_VERSION_HEX >= 0x03000000
PyMODINIT_FUNC BOB_EXT_ENTRY_NAME(void) {
#if PY_VERSION_HEX >= 0x03000000
return
# endif
create_module();
#endif
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