[ae3ce4e] | 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 | """ |
---|
[0abf7bf] | 27 | from sans.models.CylinderModel import CylinderModel |
---|
[ae3ce4e] | 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() |
---|
[0abf7bf] | 48 | exec "from sans.models.%s import %s" % (classname, classname) |
---|
| 49 | exec "model_object = %s()" % classname |
---|
[ae3ce4e] | 50 | |
---|
| 51 | # Output file for plotting |
---|
| 52 | file_out = open("%s_out.txt" % classname, 'w') |
---|
| 53 | file_out.write("<q> <I_danse> <I_igor>\n") |
---|
| 54 | |
---|
| 55 | # Process data |
---|
| 56 | elif started_data: |
---|
| 57 | toks = line.split() |
---|
| 58 | q = float(toks[0]) |
---|
| 59 | iq = float(toks[1]) |
---|
| 60 | |
---|
| 61 | value = model_object.run(q) |
---|
| 62 | |
---|
| 63 | file_out.write("%g %g %g\n" % (q, value, iq)) |
---|
| 64 | |
---|
| 65 | if math.fabs( (value - iq)/iq )>self.precision: |
---|
| 66 | self.passed = False |
---|
| 67 | print "ERROR q=%g: %g <> %g" % (q, model_object.run(q), iq) |
---|
| 68 | |
---|
| 69 | # Catch DATA tag |
---|
| 70 | elif line.count("DATA")>0: |
---|
| 71 | started_data = True |
---|
| 72 | |
---|
| 73 | # Process parameters |
---|
| 74 | elif started_data == False and not model_object==None: |
---|
| 75 | toks = line.split('=') |
---|
| 76 | if len(toks)==2: |
---|
| 77 | print "Setting parameter", line |
---|
| 78 | model_object.setParam(toks[0].lstrip().rstrip(), |
---|
| 79 | float(toks[1])) |
---|
| 80 | except: |
---|
| 81 | print "Could not parse line:\n %s" % line |
---|
| 82 | print sys.exc_value |
---|
| 83 | |
---|
| 84 | file_obj.close() |
---|
| 85 | file_out.close() |
---|
| 86 | |
---|
| 87 | print "Test passed = ", self.passed |
---|
| 88 | return self.passed |
---|
| 89 | |
---|
| 90 | if __name__ == '__main__': |
---|
| 91 | validator = Validate1D() |
---|
| 92 | all_pass = True |
---|
| 93 | all_pass = all_pass and validator("sphere_testdata.txt") |
---|
| 94 | print '\n' |
---|
| 95 | all_pass = all_pass and validator("cylinder_testdata.txt") |
---|
| 96 | print '\n' |
---|
| 97 | all_pass = all_pass and validator("core_shell_cyl_testdata.txt") |
---|
| 98 | print '\n' |
---|
| 99 | all_pass = all_pass and validator("core_shell_testdata.txt") |
---|
| 100 | print '\n' |
---|
| 101 | all_pass = all_pass and validator("ellipsoid_testdata.txt") |
---|
| 102 | print '\n' |
---|
| 103 | all_pass = all_pass and validator("elliptical_cylinder_testdata.txt") |
---|
| 104 | |
---|
| 105 | print '\nOverall result:', all_pass |
---|
| 106 | |
---|
| 107 | |
---|
| 108 | |
---|
| 109 | |
---|
| 110 | |
---|