Skip to content

Commit

Permalink
Merge pull request #21 from phnmnl/feature/roundoff_comparison
Browse files Browse the repository at this point in the history
A new roundoff comparison function for csv files
  • Loading branch information
ilveroluca authored May 3, 2017
2 parents 9feaeee + 8f08064 commit 590c9ff
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 2 deletions.
10 changes: 10 additions & 0 deletions tests/comparators/rounded_comparison_csv/correlation_table.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"spec","r1","r2","r3","r4"
"Br",0.538284659385681,0.347380578517914,0.984475374221802,0.858644068241119
"Cc",0.972778141498566,0.821589231491089,0.667640388011932,0.982909619808197
"Ft",0.711512565612793,0.79552161693573,0.820532739162445,0.348022520542145
"Gp",0.916094779968262,0.286471843719482,0.93779468536377,0.787193953990936
"Hc",0.64589512348175,0.552187442779541,0.625308692455292,0.823695063591003
"Mp",0.997475743293762,-0.0424592085182667,0.997138321399689,0.989661455154419
"Pu",0.68191522359848,0.419826716184616,0.923761785030365,0.409022688865662
"Po",0.900495171546936,0.641049265861511,0.985500574111938,0.607609212398529
"Rs",0.977232158184052,0,0.677042424678802,0.854979515075684
10 changes: 10 additions & 0 deletions tests/comparators/rounded_comparison_csv/diff_field.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"spec","r1","r2","r3","r4"
"Br",1.538284659385681,0.347380578517914,0.984475374221802,0.858644068241119
"Cc",0.972778141498566,0.821589231491089,0.667640388011932,0.982909619808197
"Ft",0.711512565612793,0.79552161693573,0.820532739162445,0.348022520542145
"Gp",0.916094779968262,0.286471843719482,0.93779468536377,0.787193953990936
"Hc",0.64589512348175,0.552187442779541,0.625308692455292,0.823695063591003
"Mp",0.997475743293762,-0.0424592085182667,0.997138321399689,0.989661455154419
"Pu",0.68191522359848,0.419826716184616,0.923761785030365,0.409022688865662
"Po",0.900495171546936,0.641049265861511,0.985500574111938,0.607609212398529
"Rs",0.977232158184052,0,0.677042424678802,0.854979515075684
10 changes: 10 additions & 0 deletions tests/comparators/rounded_comparison_csv/diff_heading.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"spec","Wrong","r2","r3","r4"
"Br",0.538284659385681,0.347380578517914,0.984475374221802,0.858644068241119
"Cc",0.972778141498566,0.821589231491089,0.667640388011932,0.982909619808197
"Ft",0.711512565612793,0.79552161693573,0.820532739162445,0.348022520542145
"Gp",0.916094779968262,0.286471843719482,0.93779468536377,0.787193953990936
"Hc",0.64589512348175,0.552187442779541,0.625308692455292,0.823695063591003
"Mp",0.997475743293762,-0.0424592085182667,0.997138321399689,0.989661455154419
"Pu",0.68191522359848,0.419826716184616,0.923761785030365,0.409022688865662
"Po",0.900495171546936,0.641049265861511,0.985500574111938,0.607609212398529
"Rs",0.977232158184052,0,0.677042424678802,0.854979515075684
10 changes: 10 additions & 0 deletions tests/comparators/rounded_comparison_csv/diff_under_round.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"spec","r1","r2","r3","r4"
"Br",0.538284659385689,0.347380578517914,0.984475374221802,0.858644068241119
"Cc",0.972778141498566,0.821589231491089,0.667640388011932,0.982909619808197
"Ft",0.711512565612793,0.79552161693573,0.820532739162445,0.348022520542145
"Gp",0.916094779968262,0.286471843719482,0.93779468536377,0.787193953990936
"Hc",0.64589512348175,0.552187442779541,0.625308692455292,0.823695063591003
"Mp",0.997475743293762,-0.0424592085182667,0.997138321399689,0.989661455154419
"Pu",0.68191522359848,0.419826716184616,0.923761785030365,0.409022688865662
"Po",0.900495171546936,0.641049265861511,0.985500574111938,0.607609212398529
"Rs",0.977232158184052,0,0.677042424678802,0.854979515075684
10 changes: 10 additions & 0 deletions tests/comparators/rounded_comparison_csv/wrong_correlation.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"spec","l1","r2","r3","r4"
"Br",0.538284659385682,0.347380578517914,0.984475374221802,0.858644068241119
"Cc",0.972778141498566,0.821589231491089,0.667640388011932,0.982909619808197
"Ft",0.711512565612793,0.79552161693573,0.820532739162445,0.348022520542145
"Gp",0.916094779968262,0.286471843719482,0.93779468536377,0.787193953990936
"Hc",0.64589512348175,0.552187442779541,0.625308692455292,0.823695063591003
"Mp",0.997475743293762,-0.0424592085182667,0.997138321399689,0.989661455154419
"Pu",0.68191522359848,0.419826716184616,0.923761785030365,0.409022688865662
"Po",0.900495171546936,0.641049265861511,0.985500574111938,0.607609212398529
"Rs",0.977232158184052,0,0.677042424678802,0.854979515075684
54 changes: 54 additions & 0 deletions tests/test_wft4galaxy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env python

import os
import sys
import unittest

MyDir = os.path.abspath(os.path.dirname(__file__))

class TestWft4Galaxy(unittest.TestCase):
def setUp(self):
pass

def tearDown(self):
pass

class TestRoundedCsvComparator(unittest.TestCase):

TestDir = os.path.join(MyDir, 'comparators', 'rounded_comparison_csv')
CorrelationFile = os.path.join(TestDir, 'correlation_table.csv')

def test_identical_files(self):
from wft4galaxy.comparators import rounded_comparison_csv
self.assertTrue(rounded_comparison_csv(self.CorrelationFile, self.CorrelationFile))

def test_diff_heading(self):
from wft4galaxy.comparators import rounded_comparison_csv
different_file = os.path.join(self.TestDir, 'diff_heading.csv')
self.assertFalse(rounded_comparison_csv(different_file, self.CorrelationFile))

def test_diff_field(self):
from wft4galaxy.comparators import rounded_comparison_csv
different_file = os.path.join(self.TestDir, 'diff_field.csv')
self.assertFalse(rounded_comparison_csv(different_file, self.CorrelationFile))

def test_diff_field_less_than_roundoff(self):
from wft4galaxy.comparators import rounded_comparison_csv
# A number in a field is different, but at a higher precision than required
# by comparison function
different_file = os.path.join(self.TestDir, 'diff_under_round.csv')
self.assertTrue(rounded_comparison_csv(different_file, self.CorrelationFile))



def suite():
s = unittest.TestLoader().loadTestsFromTestCase(TestWft4Galaxy)
s.addTests(unittest.TestLoader().loadTestsFromTestCase(TestRoundedCsvComparator))
return s

def main():
result = unittest.TextTestRunner(verbosity=2).run(suite())
return 0 if result.wasSuccessful() else 1

if __name__ == '__main__':
sys.exit(main())
45 changes: 43 additions & 2 deletions wft4galaxy/comparators/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@

from __future__ import print_function

import os as _os
import sys as _sys
import logging as _logging
from wft4galaxy import common as _common
from difflib import unified_diff as _unified_diff
from wft4galaxy import common as _common

_logger = _common.LoggerManager.get_logger(__name__)

Expand Down Expand Up @@ -45,7 +48,6 @@ def base_comparator(actual_output_filename, expected_output_filename):
out_fp.writelines("%r\n" % item.rstrip('\n') for item in ldiff)
return len(ldiff) == 0


def csv_same_row_and_col_lengths(actual_output_filename, expected_output_filename):
import csv

Expand All @@ -63,3 +65,42 @@ def csv_same_row_and_col_lengths(actual_output_filename, expected_output_filenam
if colsActual and colsExpected:
return cmp(colsActual, colsExpected) == 0
return False

def _get_float(s):
try:
return float(s)
except ValueError:
return None

def _compare_strings_as_floats(precision, a, b):
a_float = _get_float(a)
b_float = _get_float(b)
if a_float is not None and b_float is not None:
return round(a_float, precision) == round(b_float, precision)
else:
return False

def rounded_comparison_csv(actual_output, expected_output):
precision = 2
import csv
import itertools as it
try:
with open(actual_output) as actual, open(expected_output) as expected:
aout = csv.reader(actual)
eout = csv.reader(expected)
for expected_row in eout:
actual_row = next(aout)
for actual_field, expected_field in it.izip(actual_row, expected_row):
if not _compare_strings_as_floats(precision, actual_field, expected_field) \
and actual_field != expected_field:
print("Difference found between expected and actual output", file=_sys.stderr)
print("Expected field text:", expected_field, file=_sys.stderr)
print("Actual field text:", actual_field, file=_sys.stderr)
print("Expected row:", expected_row, file=_sys.stderr)
print("Actual row:", actual_row, file=_sys.stderr)
return False
except StopIteration:
print("Actual output is shorted than expected output", file=_sys.stderr)
return False

return True

0 comments on commit 590c9ff

Please sign in to comment.