Skip to content

Commit 19f95d0

Browse files
author
aglavic
committed
- Start UnitTest framework
- make scipy import optional for peakfinder - fix errors from refactoring
1 parent 9fc9fbc commit 19f95d0

File tree

8 files changed

+116
-20
lines changed

8 files changed

+116
-20
lines changed

mime_types/mbe.xml

-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44
<comment>MBE Log File</comment>
55
<glob pattern="*.log"/>
66
</mime-type>
7-
<mime-type type="text/aes">
8-
<comment>MBE AES File</comment>
9-
<glob pattern="*.dat"/>
10-
</mime-type>
117
<mime-type type="image/png">
128
<comment>PNG image</comment>
139
<glob pattern="*.png"/>

plot_script/fit_data.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -3149,8 +3149,9 @@ def simulate(self):
31493149
data_x=[d[0] for d in data_xy]
31503150
# only interpolate if the number of points isn't too large
31513151
use_interpolate=min(5, 5000/len(data_x))
3152-
fit_x, fit_y=function[0].simulate(data_x, inside_fitrange=getattr(self, 'restrict_to_region', False),
3153-
interpolate=use_interpolate)
3152+
fit_x, fit_y=function[0].simulate(data_x,
3153+
inside_fitrange=getattr(self, 'restrict_to_region', False),
3154+
interpolate=use_interpolate)
31543155
for i in range(len(fit_x)):
31553156
result.append((fit_x[i], fit_y[i]))
31563157
function_text=function[0].fit_function_text_eval

plot_script/gtkgui/dialogs.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,8 @@ def __init__(self, title, entries, *args, **opts):
435435
'''
436436
# Initialize this dialog
437437
opts['title']=title
438-
opts['buttons']=('OK', 1, 'Cancel', 0)
438+
if not 'buttons' in opts:
439+
opts['buttons']=('OK', 1, 'Cancel', 0)
439440
if 'description' in opts:
440441
description=gtk.Label(opts['description'])
441442
del(opts['description'])

plot_script/measurement_data_structure.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -684,8 +684,8 @@ def list(self): #@ReservedAssignment
684684
zd=self.zdata
685685
if (xd>=0) and (yd>=0):
686686
if (zd<0):
687-
return data[numpy.array([xd, yd])].tolist()
688-
return data[numpy.array([xd, yd, zd])].tolist()
687+
return data[numpy.array([xd, yd])].transpose().tolist()
688+
return data[numpy.array([xd, yd, zd])].transpose().tolist()
689689
elif yd>=0:
690690
return numpy.vstack([numpy.arange(len(data[0])), data[yd]]).transpose().tolist()
691691
elif xd>=0:
@@ -706,8 +706,8 @@ def list_err(self):
706706
return [point+[0] for point in self.list()]
707707
if (xd>=0) and (yd>=0):
708708
if (zd<0):
709-
return data[numpy.array([xd, yd, ye])].tolist()
710-
return data[numpy.array([xd, yd, zd, ye])].tolist()
709+
return data[numpy.array([xd, yd, ye])].transpose().tolist()
710+
return data[numpy.array([xd, yd, zd, ye])].transpose().tolist()
711711
elif yd>=0:
712712
return numpy.vstack([numpy.arange(len(data[0])),
713713
data[numpy.array([yd, ye])]]).transpose().tolist()
@@ -2865,7 +2865,8 @@ def _propagate_errors(self, out_arr, context):
28652865
try:
28662866
if len(context[1])==1:
28672867
# only a function of one parameter
2868-
out_arr.error=abs(derivatives[context[0].__str__()](self.view(numpy.ndarray))*self.error)
2868+
out_arr.error=abs(derivatives[context[0].__str__()](
2869+
self.view(numpy.ndarray))*self.error)
28692870
elif context[0].__name__ in compare_functions:
28702871
other=context[1][1]
28712872
# if both arguments to compare function have an error value
@@ -2892,7 +2893,8 @@ def _propagate_errors(self, out_arr, context):
28922893
)
28932894
# only the first argument has an error value
28942895
else:
2895-
out_arr.error=abs(derivatives[context[0].__str__()][0](self.view(numpy.ndarray), numpy.array(other))*self.error)
2896+
out_arr.error=abs(derivatives[context[0].__str__()][0](self.view(numpy.ndarray),
2897+
numpy.array(other))*self.error)
28962898
else:
28972899
# the second argument is self, so the first is no PhysicalProperty instance
28982900
out_arr.error=abs(derivatives[context[0].__str__()][1](numpy.array(context[1][0]), self.view(numpy.ndarray))*self.error)

plot_script/peakfinder.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
'''
1313

1414
import numpy
15-
from scipy.stats.mstats import mquantiles
15+
try:
16+
from scipy.stats.mstats import mquantiles
17+
except ImportError:
18+
pass
1619

1720
class PeakFinder(object):
1821
'''

plot_script/sessions/dns.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ def read_argument_add(self, argument, last_argument_option=[False, ''], input_fi
532532
file_postfix=str(last_argument_option[2][3][0])+last_argument_option[2][4]
533533
try:
534534
first_file=sorted([file_ for file_ in os.listdir(directory) \
535-
if file.startswith(file_prefix) and file.endswith(file_postfix)])[0]
535+
if file_.startswith(file_prefix) and file_.endswith(file_postfix)])[0]
536536
except IndexError:
537537
erg=last_argument_option[2]
538538
print """No file found for the -files options:
@@ -639,18 +639,18 @@ def read_files(self, file_name):
639639
if len(file_list)==0:
640640
return None
641641
file_list.sort()
642-
# Read the raw data
643-
self.file_data[file_name+'|raw_data']=[]
644642
print "Reading files %s{num}%s with num from %i to %i."%(prefix, postfix, num_range[0], num_range[1])
645-
for file_name in file_list:
643+
self.file_data[file_name+'|raw_data']=[]
644+
for file_i in file_list:
645+
# Read the raw data
646646
# get integer number of the file_name, catch errors form wrong file_name selection
647647
try:
648-
active_number=int(os.path.join(folder, file_name).rsplit(postfix)[0].split(prefix, 1)[1])
648+
active_number=int(os.path.join(folder, file_i).rsplit(postfix)[0].split(prefix, 1)[1])
649649
except ValueError:
650650
continue
651651
if (active_number>=num_range[0]) and (active_number<=num_range[1] or num_range[1]==-1):
652652
# read the datafile into a MeasurementData object.
653-
dataset=read_data.read_data(os.path.join(folder, file_name))
653+
dataset=read_data.read_data(os.path.join(folder, file_i))
654654
dataset.number=str(active_number)
655655
self.file_data[file_name+'|raw_data'].append(dataset)
656656
print "\tRead, creating map."

tests/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'''
2+
Package providing unit tests for the plot_script package.
3+
'''

tests/datatype.py

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#-*- coding: utf8 -*-
2+
'''
3+
Testing the datatype framewok.
4+
'''
5+
6+
from plot_script.measurement_data_structure import PhysicalProperty
7+
from numpy import *
8+
import unittest
9+
10+
class TestPhysicalProperty(unittest.TestCase):
11+
'''
12+
Check the behavior of the base data holding array type.
13+
'''
14+
15+
def setUp(self):
16+
self.error=array([2. for ignore in range(100)], dtype=float32)
17+
self.values=arange(1., 101., 1.0, dtype=float32)
18+
self.data=PhysicalProperty('x', '°', self.values, self.error)
19+
20+
def test_add(self):
21+
# test the base calculations with addition
22+
add_res=self.data+self.data
23+
self.assertEqual(add_res.view(ndarray).tolist(),
24+
(2.*self.values).tolist(),
25+
"Add results")
26+
self.assertEqual(add_res.error.tolist(),
27+
(sqrt(2.)*self.error).tolist(),
28+
"Add errors")
29+
30+
def test_sub(self):
31+
# test the base calculations with subtraction
32+
sub_res=self.data-self.data
33+
self.assertEqual(sub_res.view(ndarray).tolist(),
34+
(zeros_like(self.values)).tolist(),
35+
"Subtract results")
36+
self.assertEqual(sub_res.error.tolist(),
37+
(sqrt(2.)*self.error).tolist(),
38+
"Subtract errors")
39+
40+
def test_mul(self):
41+
# test the base calculations with multiplication
42+
mul_res=self.data*self.data
43+
self.assertEqual(mul_res.view(ndarray).tolist(),
44+
(self.values**2).tolist(),
45+
"Multiply results")
46+
self.assertEqual(mul_res.error.tolist(),
47+
(sqrt((self.error*self.values)**2+
48+
(self.error*self.values)**2)).tolist(),
49+
"Multiply errors")
50+
51+
def test_div(self):
52+
# test the base calculations with division
53+
div_res=self.data/self.data
54+
self.assertEqual(div_res.view(ndarray).tolist(),
55+
(ones_like(self.values)).tolist(),
56+
"Devision results")
57+
self.assertEqual(div_res.error.tolist(),
58+
(sqrt((self.error/self.values)**2+
59+
(self.error/self.values)**2)).tolist(),
60+
"Devision errors")
61+
62+
def test_unit_conversions(self):
63+
# test automatic and manual unit conversion
64+
deg=self.data
65+
rad=deg%'rad'
66+
self.assertEqual(rad.unit, 'rad',
67+
"Unit conversion from ° to rad - Unit")
68+
self.assertEqual((deg*(pi/180.)).tolist(),
69+
rad.tolist(),
70+
"Unit conversion from ° to rad - Value")
71+
# automatic conversion when added
72+
degrad=deg+rad
73+
self.assertEqual(degrad.tolist(), (2.*deg).tolist(),
74+
"Unit auto conversion in addition")
75+
# automatic conversion for angle functions
76+
degsin=sin(deg)
77+
radsin=sin(rad)
78+
self.assertEqual(degsin.tolist(), radsin.tolist(),
79+
"Unit auto conversion in sin(x)")
80+
81+
82+
class TestBla(unittest.TestCase):
83+
pass
84+
85+
if __name__=='__main__':
86+
#unittest.main()
87+
loader=unittest.TestLoader()
88+
suite=loader.loadTestsFromTestCase(TestPhysicalProperty)
89+
suite.addTest(loader.loadTestsFromTestCase(TestBla))
90+
unittest.TextTestRunner(verbosity=2).run(suite)

0 commit comments

Comments
 (0)