Skip to content

Commit

Permalink
upgrade python tests to Python 3
Browse files Browse the repository at this point in the history
  • Loading branch information
wojdyr committed Aug 7, 2022
1 parent 5762bbe commit d1ede74
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 38 deletions.
2 changes: 1 addition & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ check-cfityk:

check-python:
PYTHONPATH=fityk/swig/.libs:$(srcdir)/fityk/swig/ \
python -m unittest discover -s $(srcdir)/tests/
$(PYTHON) -m unittest discover -s $(srcdir)/tests/

check-help:
echo '$$ fityk -h' > $(srcdir)/doc/help-message.txt
Expand Down
2 changes: 1 addition & 1 deletion samples/cfityk.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
"""
Equivalent of cfityk in Python.
"""
Expand Down
16 changes: 8 additions & 8 deletions samples/hello.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3

import os.path, sys
from fityk import Fityk
Expand All @@ -10,23 +10,23 @@ def __init__(self, filename):
raise ValueError("File `%s' not found." % filename)
self.filename = filename
self.execute("@0 < '%s'" % filename)
print "Data info:", self.get_info("data", 0)
print("Data info:", self.get_info("data", 0))

def run(self):
self.execute("guess Gaussian")
gauss = self.all_functions()[-1] # the last function (just created)
print "initial Gaussian center: %g" % gauss.get_param_value("center")
print "Fitting %s ..." % self.filename
print("initial Gaussian center: %g" % gauss.get_param_value("center"))
print("Fitting %s ..." % self.filename)
self.execute("fit")
print "WSSR=", self.get_wssr()
print "Gaussian center: %g" % gauss.get_param_value("center")
print("WSSR=", self.get_wssr())
print("Gaussian center: %g" % gauss.get_param_value("center"))

def save_session(self, filename):
self.execute("info state >'%s'" % filename)

f = Fityk()
print f.get_info("version")
print "ln(2) =", f.calculate_expr("ln(2)")
print(f.get_info("version"))
print("ln(2) =", f.calculate_expr("ln(2)"))
del f

g = GaussianFitter(os.path.join(os.path.dirname(sys.argv[0]), "nacl01.dat"))
Expand Down
14 changes: 7 additions & 7 deletions tests/test_data_load.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# run tests with: python -m unittest test_data_load
Expand All @@ -22,7 +22,7 @@ def setUp(self):
f = tempfile.NamedTemporaryFile(prefix=prefix, delete=False)
self.data = self.generate_data()
for d in self.data:
f.write(self.line_format(d))
f.write(self.line_format(d).encode())
f.close()
self.data.sort()
self.filename = f.name
Expand All @@ -32,7 +32,7 @@ def generate_data(self):
for _ in range(30)]

def tearDown(self):
#print "remove " + self.filename
#print("remove " + self.filename)
os.unlink(self.filename)

def compare(self, out, ndigits):
Expand Down Expand Up @@ -110,7 +110,7 @@ def test_load_default(self):
def test_load_with_index(self):
self.ftk.execute("@0 < '%s:0:2::'" % self.filename)
out = self.ftk.get_data()
self.assertEqual([i.x for i in out], range(len(out)))
self.assertEqual([i.x for i in out], list(range(len(out))))
def test_load_with_sigma(self):
self.ftk.execute("@0 < '%s:1:2:3:'" % self.filename)
out = self.ftk.get_data()
Expand All @@ -133,7 +133,7 @@ def setUp(self):
self.ftk = fityk.Fityk()
self.ftk.set_option_as_number("verbosity", -1)
f = tempfile.NamedTemporaryFile(delete=False)
f.write("M=1\n X[0]=1.1, Y[0]=-5, S[0]=0.8")
f.write(b"M=1\n X[0]=1.1, Y[0]=-5, S[0]=0.8")
f.close()
self.filename = f.name

Expand All @@ -154,8 +154,8 @@ def setUp(self):
self.ftk.set_option_as_number("verbosity", -1)
f = tempfile.NamedTemporaryFile(suffix='.gz', delete=False)
self.filename = f.name
gf = gzip.GzipFile(fileobj=f)
gf.write("M=1\n X[0]=1.1, Y[0]=-5, S[0]=0.8")
gf = gzip.GzipFile(fileobj=f, mode='wb')
gf.write(b"M=1\n X[0]=1.1, Y[0]=-5, S[0]=0.8")
gf.close()

def tearDown(self):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_guess.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3

import os
import sys
Expand Down
2 changes: 1 addition & 1 deletion tests/test_info.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3

# run tests with: python -m unittest test_info
# or python -m unittest discover
Expand Down
2 changes: 1 addition & 1 deletion tests/test_model.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3

# run tests with: python -m unittest test_model
# or python -m unittest discover
Expand Down
34 changes: 19 additions & 15 deletions tests/test_nist.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#!/usr/bin/env python
#!/usr/bin/env python3

# run tests with: python -m unittest discover

import os
import sys
import re
import urllib2
from urllib.request import urlopen
import unittest
import fityk

Expand Down Expand Up @@ -40,14 +40,17 @@ def open_nist_data(name):
name_ext = name + ".dat"
local_file = os.path.join(CACHE_DIR, name_ext)
if os.path.exists(local_file):
text = open(local_file).read()
with open(local_file, 'rb') as f:
text = f.read()
else:
sys.stderr.write("Local data copy not found. Trying itl.nist.gov...\n")
text = urllib2.urlopen(DATA_URL_BASE + name_ext).read()
text = urlopen(DATA_URL_BASE + name_ext).read()
if not os.path.isdir(CACHE_DIR):
os.mkdir(CACHE_DIR)
open(local_file, "wb").write(text)
return text
with open(local_file, "wb") as f:
sys.stderr.write("Writing " + local_file + ' ...\n')
f.write(text)
return text.decode()


def read_reference_data(name):
Expand Down Expand Up @@ -100,12 +103,13 @@ def run(data_name, fit_method, easy=True):
tolerance["err"] = 5e-5
#if fit_method in ("mpfit", "levenberg_marquardt"):
if VERBOSE > 0:
print "Testing %s (start%s) on %-10s" % (fit_method, easy+1, data_name),
print("Testing %s (start%s) on %-10s" % (fit_method, easy+1, data_name),
end='')
if VERBOSE > 1:
print
print()
ref = read_reference_data(data_name)
if VERBOSE > 2:
print ref.model
print(ref.model)
ftk = fityk.Fityk()
if VERBOSE < 3:
ftk.execute("set verbosity=-1")
Expand Down Expand Up @@ -134,7 +138,7 @@ def run(data_name, fit_method, easy=True):
#ftk.execute("set fitting_method=levenberg_marquardt")
#ftk.execute("fit")
except fityk.ExecuteError as e:
print "fityk.ExecuteError: %s" % e
print("fityk.ExecuteError: %s" % e)
return False
ssr = ftk.get_ssr()
ssr_diff = (ssr - ref.ssr) / ref.ssr
Expand All @@ -147,10 +151,10 @@ def run(data_name, fit_method, easy=True):

ok = (abs(ssr_diff) < tolerance["wssr"])
if ref.ssr > 1e-10 and ssr_diff < -1e-10:
print "Eureka! %.10E < %.10E" % (ssr, ref.ssr)
print("Eureka! %.10E < %.10E" % (ssr, ref.ssr))
fmt = " %8s %13E %13E %+.1E"
if VERBOSE > 2 or (VERBOSE == 2 and not ok):
print fmt % ("SSR", ssr, ref.ssr, ssr_diff)
print(fmt % ("SSR", ssr, ref.ssr, ssr_diff))
our_func = ftk.all_functions()[0]
for par in ref.parameters:
calc_value = our_func.get_param_value(par.name)
Expand All @@ -163,9 +167,9 @@ def run(data_name, fit_method, easy=True):
err_diff = (calc_err - par.stddev) / par.stddev
err_ok = (abs(err_diff) < tolerance["err"])
if VERBOSE > 2 or (VERBOSE == 2 and (not param_ok or not err_ok)):
print fmt % (par.name, calc_value, par.value, val_diff)
print(fmt % (par.name, calc_value, par.value, val_diff))
if "err" in tolerance:
print fmt % ("+/-", calc_err, par.stddev, err_diff)
print(fmt % ("+/-", calc_err, par.stddev, err_diff))
ok = (ok and param_ok and err_ok)
if VERBOSE == 1:
print("OK" if ok else "FAILED")
Expand Down Expand Up @@ -255,7 +259,7 @@ def try_method(method):
if ok:
ok_count += 1
sys.stdout.flush()
print "OK: %2d / %2d" % (ok_count, all_count)
print("OK: %2d / %2d" % (ok_count, all_count))


if __name__ == '__main__':
Expand Down
6 changes: 3 additions & 3 deletions tests/test_tranform.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3

# run tests with: python -m unittest test_tranform
# python -m unittest discover
Expand Down Expand Up @@ -30,8 +30,8 @@ def test_load(self):
self.assertEqual(xx, self.x)
self.assertEqual(yy, self.y)
self.assertEqual(ss, self.sigma)
#print self.x
#print self.y
#print(self.x)
#print(self.y)

def assert_expr(self, expr, val, places=None):
expr_val = self.ftk.calculate_expr(expr)
Expand Down

0 comments on commit d1ede74

Please sign in to comment.