Commit e16c0b71 authored by Manuel Günther's avatar Manuel Günther
Browse files

Fixed py26 and py3 issues with new function

parent fb45722e
"""This file includes functionality to convert between Bob's four column or five column score files and the Matrix files used in OpenBR."""
import numpy
import sys
import logging
logger = logging.getLogger("bob.measure")
......@@ -61,10 +62,13 @@ def write_matrix(
## Helper function to write a matrix file as required by OpenBR
with open(filename, 'wb') as f:
# write the first four lines
f.write("S2\n%s\n%s\nM%s %d %d " % (gallery_file_name, probe_file_name, 'B' if matrix.dtype == numpy.uint8 else 'F', matrix.shape[0], matrix.shape[1]))
header = "S2\n%s\n%s\nM%s %d %d " % (gallery_file_name, probe_file_name, 'B' if matrix.dtype == numpy.uint8 else 'F', matrix.shape[0], matrix.shape[1])
footer = "\n"
if sys.version_info[0] > 2: header, footer = header.encode('utf-8'), footer.encode('utf-8')
f.write(header)
# write magic number
numpy.array(0x12345678, numpy.int32).tofile(f)
f.write("\n")
f.write(footer)
# write the matrix
matrix.tofile(f)
......@@ -89,8 +93,9 @@ def write_matrix(
probe_set.add(probe)
# create a shortcut to get indices for client and probe subset (to increase speed)
model_dict = {m:i for i,m in enumerate(model_names)}
probe_dict = {p:i for i,p in enumerate(probe_names)}
model_dict, probe_dict = {}, {}
for i,m in enumerate(model_names): model_dict[m]=i
for i,p in enumerate(probe_names): probe_dict[p]=i
# now, create the matrices in the desired size
matrix = numpy.ndarray((len(probe_names), len(model_names)), numpy.float32)
......
......@@ -38,10 +38,14 @@ def test_load_scores():
def _check_binary_identical(name1, name2):
# see: http://www.peterbe.com/plog/using-md5-to-check-equality-between-files
import md5
import sys
if sys.version_info[0] == 2:
from md5 import new as md5
else:
from hashlib import md5
# tests if two files are binary identical
with open(name1) as f1, open(name2) as f2:
assert md5.new(f1.read()).digest() == md5.new(f2.read()).digest()
with open(name1,'rb') as f1, open(name2,'rb') as f2:
assert md5(f1.read()).digest() == md5(f2.read()).digest()
def test_convert_openbr():
......
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