1 | #!/usr/bin/env python |
---|
2 | """ |
---|
3 | Class to validate a given model by reading a test data set |
---|
4 | generated from the IGOR SANS analysis tool from the NCNR |
---|
5 | """ |
---|
6 | import sys, math |
---|
7 | |
---|
8 | |
---|
9 | class Validate1D: |
---|
10 | """ |
---|
11 | Class to validate a given model by reading a test data set |
---|
12 | generated from the IGOR SANS analysis tool from the NCNR |
---|
13 | """ |
---|
14 | |
---|
15 | def __init__(self): |
---|
16 | """ Initialization """ |
---|
17 | # Precision for the result comparison |
---|
18 | self.precision = 0.0001 |
---|
19 | # Flag for end result |
---|
20 | self.passed = True |
---|
21 | |
---|
22 | def __call__(self, filename): |
---|
23 | """ |
---|
24 | Perform test on a data file |
---|
25 | @param filename: name of the test data set |
---|
26 | """ |
---|
27 | from sans.models.ModelFactory import ModelFactory |
---|
28 | |
---|
29 | # Read the data file |
---|
30 | file_obj = open(filename,'r') |
---|
31 | content = file_obj.read() |
---|
32 | |
---|
33 | # Flag to determine whether we are in the DATA section |
---|
34 | started_data = False |
---|
35 | # Model to test |
---|
36 | model_object = None |
---|
37 | |
---|
38 | # Process each line of the file |
---|
39 | for line in content.split('\n'): |
---|
40 | if len(line)==0: |
---|
41 | continue |
---|
42 | try: |
---|
43 | # Catch class name |
---|
44 | if line.count("pythonclass")>0 and model_object==None: |
---|
45 | toks = line.split('=') |
---|
46 | print "Found class", toks[1] |
---|
47 | classname = toks[1].lstrip().rstrip() |
---|
48 | model_object = ModelFactory().getModel(classname) |
---|
49 | |
---|
50 | # Output file for plotting |
---|
51 | file_out = open("%s_out.txt" % classname, 'w') |
---|
52 | file_out.write("<q> <I_danse> <I_igor>\n") |
---|
53 | |
---|
54 | # Process data |
---|
55 | elif started_data: |
---|
56 | toks = line.split() |
---|
57 | q = float(toks[0]) |
---|
58 | iq = float(toks[1]) |
---|
59 | |
---|
60 | value = model_object.run(q) |
---|
61 | |
---|
62 | file_out.write("%g %g %g\n" % (q, value, iq)) |
---|
63 | |
---|
64 | if math.fabs( (value - iq)/iq )>self.precision: |
---|
65 | self.passed = False |
---|
66 | print "ERROR q=%g: %g <> %g" % (q, model_object.run(q), iq) |
---|
67 | |
---|
68 | # Catch DATA tag |
---|
69 | elif line.count("DATA")>0: |
---|
70 | started_data = True |
---|
71 | |
---|
72 | # Process parameters |
---|
73 | elif started_data == False and not model_object==None: |
---|
74 | toks = line.split('=') |
---|
75 | if len(toks)==2: |
---|
76 | print "Setting parameter", line |
---|
77 | model_object.setParam(toks[0].lstrip().rstrip(), |
---|
78 | float(toks[1])) |
---|
79 | except: |
---|
80 | print "Could not parse line:\n %s" % line |
---|
81 | print sys.exc_value |
---|
82 | |
---|
83 | file_obj.close() |
---|
84 | file_out.close() |
---|
85 | |
---|
86 | print "Test passed = ", self.passed |
---|
87 | return self.passed |
---|
88 | |
---|
89 | if __name__ == '__main__': |
---|
90 | validator = Validate1D() |
---|
91 | all_pass = True |
---|
92 | all_pass = all_pass and validator("sphere_testdata.txt") |
---|
93 | print '\n' |
---|
94 | all_pass = all_pass and validator("cylinder_testdata.txt") |
---|
95 | print '\n' |
---|
96 | all_pass = all_pass and validator("core_shell_cyl_testdata.txt") |
---|
97 | print '\n' |
---|
98 | all_pass = all_pass and validator("core_shell_testdata.txt") |
---|
99 | print '\n' |
---|
100 | all_pass = all_pass and validator("ellipsoid_testdata.txt") |
---|
101 | print '\n' |
---|
102 | all_pass = all_pass and validator("elliptical_cylinder_testdata.txt") |
---|
103 | |
---|
104 | print '\nOverall result:', all_pass |
---|
105 | |
---|
106 | |
---|
107 | |
---|
108 | |
---|
109 | |
---|