1 | from mantid.simpleapi import * |
---|
2 | from mantid.api import FrameworkManager |
---|
3 | import os |
---|
4 | import re |
---|
5 | #import stresstesting |
---|
6 | |
---|
7 | BANNED_FILES = ['992 Descriptions.txt', |
---|
8 | 'BASIS_AutoReduction_Mask.xml', |
---|
9 | 'BioSANS_dark_current.xml', |
---|
10 | 'BioSANS_empty_cell.xml', |
---|
11 | 'BioSANS_empty_trans.xml', |
---|
12 | 'BioSANS_exp61_scan0004_0001.xml', |
---|
13 | 'BioSANS_flood_data.xml', |
---|
14 | 'BioSANS_sample_trans.xml', |
---|
15 | 'BioSANS_test_data.xml', |
---|
16 | 'det_corrected7.dat', |
---|
17 | 'eqsans_configuration.1463', |
---|
18 | 'FLAT_CELL.061', |
---|
19 | 'IN10_P3OT_350K.inx', |
---|
20 | 'IN16_65722.asc', |
---|
21 | 'IP0005.dat', |
---|
22 | 'batch_input.csv', |
---|
23 | 'mar11015.msk', |
---|
24 | 'LET_hard.msk', #It seems loade does not understand it? |
---|
25 | 'MASK.094AA', |
---|
26 | 'MASKSANS2D_094i_RKH.txt', |
---|
27 | 'MASKSANS2D.091A', |
---|
28 | 'MASKSANS2Doptions.091A', |
---|
29 | 'MAP17269.raw', # Don't need to check multiple MAPS files |
---|
30 | 'MAP17589.raw', |
---|
31 | 'MER06399.raw', # Don't need to check multiple MERLIN files |
---|
32 | 'PG3_11485-1.dat', # Generic load doesn't do very well with ASCII files |
---|
33 | 'PG3_2538_event.nxs', # Don't need to check all of the PG3 files |
---|
34 | 'PG3_9829_event.nxs', |
---|
35 | 'REF_M_9684_event.nxs', |
---|
36 | 'REF_M_9709_event.nxs', |
---|
37 | 'SANS2D_periodTests.csv', |
---|
38 | 'SANS2D_992_91A.csv', |
---|
39 | 'testCansas1DMultiEntry.xml', |
---|
40 | 'Wish_Diffuse_Scattering_ISAW_UB.mat', |
---|
41 | 'SANS2D_periodTests.csv' |
---|
42 | ] |
---|
43 | |
---|
44 | EXPECTED_EXT = '.expected' |
---|
45 | |
---|
46 | BANNED_REGEXP = [r'SANS2D\d+.log$', |
---|
47 | r'SANS2D00000808_.+.txt$', |
---|
48 | r'.*_reduction.log$', |
---|
49 | r'.+_characterization_\d+_\d+_\d+.*\.txt', |
---|
50 | r'.+_d\d+_\d+_\d+_\d+.cal', |
---|
51 | r'.*Grouping\.xml', |
---|
52 | r'.*\.map', |
---|
53 | r'.*\.irf', |
---|
54 | r'.*\.hkl', |
---|
55 | r'EVS.*\.raw'] |
---|
56 | |
---|
57 | def useDir(direc): |
---|
58 | """Only allow directories that aren't test output or |
---|
59 | reference results.""" |
---|
60 | if "ReferenceResults" in direc: |
---|
61 | return False |
---|
62 | if "logs" in direc: |
---|
63 | return False |
---|
64 | return ("Data" in direc) |
---|
65 | |
---|
66 | def useFile(direc, filename): |
---|
67 | """Returns (useFile, abspath)""" |
---|
68 | # list of explicitly banned files at the top of this script |
---|
69 | if filename in BANNED_FILES: |
---|
70 | return (False, filename) |
---|
71 | |
---|
72 | # is an 'expected' file |
---|
73 | if filename.endswith(EXPECTED_EXT): |
---|
74 | return (False, filename) |
---|
75 | |
---|
76 | # list of banned files by regexp |
---|
77 | for regexp in BANNED_REGEXP: |
---|
78 | if re.match(regexp, filename) is not None: |
---|
79 | return (False, filename) |
---|
80 | |
---|
81 | filename = os.path.join(direc, filename) |
---|
82 | if os.path.isdir(filename): |
---|
83 | return (False, filename) |
---|
84 | return (True, filename) |
---|
85 | |
---|
86 | class LoadLotsOfFiles():#(stresstesting.MantidStressTest): |
---|
87 | def __getDataFileList__(self): |
---|
88 | # get a list of directories to look in |
---|
89 | dirs = config['datasearch.directories'].split(';') |
---|
90 | dirs = [item for item in dirs if useDir(item)] |
---|
91 | print "Looking for data files in:", ', '.join(dirs) |
---|
92 | |
---|
93 | # Files and their corresponding sizes. the low-memory win machines |
---|
94 | # fair better loading the big files first |
---|
95 | files = {} |
---|
96 | for direc in dirs: |
---|
97 | myFiles = os.listdir(direc) |
---|
98 | for filename in myFiles: |
---|
99 | (good, filename) = useFile(direc, filename) |
---|
100 | #print "***", good, filename |
---|
101 | if good: |
---|
102 | files[filename] = os.path.getsize(filename) |
---|
103 | return sorted(files, key=lambda key: files[key], reverse=True) |
---|
104 | |
---|
105 | def __runExtraTests__(self, wksp, filename): |
---|
106 | """Runs extra tests that are specified in '.expected' files |
---|
107 | next to the data files""" |
---|
108 | expected = filename + EXPECTED_EXT |
---|
109 | if not os.path.exists(expected): #file exists |
---|
110 | return True |
---|
111 | if os.path.getsize(expected) <= 0: #non-zero length |
---|
112 | return True |
---|
113 | |
---|
114 | print "Found an expected file '%s' file" % expected |
---|
115 | expectedfile = open(expected) |
---|
116 | tests = expectedfile.readlines() |
---|
117 | failed = [] # still run all of the tests |
---|
118 | for test in tests: |
---|
119 | test = test.strip() |
---|
120 | result = eval(test) |
---|
121 | if not (result == True): |
---|
122 | failed.append((test, result)) |
---|
123 | if len(failed) > 0: |
---|
124 | for item in failed: |
---|
125 | print " Failed test '%s' returned '%s' instead of 'True'" % (item[0], item[1]) |
---|
126 | return False |
---|
127 | return True |
---|
128 | |
---|
129 | |
---|
130 | def __loadAndTest__(self, filename): |
---|
131 | """Do all of the real work of loading and testing the file""" |
---|
132 | print "----------------------------------------" |
---|
133 | print "Loading '%s'" % filename |
---|
134 | from mantid.api import Workspace |
---|
135 | from mantid.api import IMDEventWorkspace |
---|
136 | # Output can be a tuple if the Load algorithm has extra output properties |
---|
137 | # but the output workspace should always be the first argument |
---|
138 | outputs = Load(filename) |
---|
139 | if type(outputs) == tuple: |
---|
140 | wksp = outputs[0] |
---|
141 | else: |
---|
142 | wksp = outputs |
---|
143 | |
---|
144 | if not isinstance(wksp, Workspace): |
---|
145 | print "Unexpected output type from Load algorithm: Type found=%s" % str(type(outputs)) |
---|
146 | return False |
---|
147 | |
---|
148 | if wksp is None: |
---|
149 | print 'Load returned None' |
---|
150 | return False |
---|
151 | |
---|
152 | # generic checks |
---|
153 | if wksp.getName() is None or len(wksp.getName()) <= 0: |
---|
154 | print "Workspace does not have a name" |
---|
155 | del wksp |
---|
156 | return False |
---|
157 | |
---|
158 | id = wksp.id() |
---|
159 | if id is None or len(id) <= 0: |
---|
160 | print "Workspace does not have an id" |
---|
161 | del wksp |
---|
162 | return False |
---|
163 | |
---|
164 | # checks based on workspace type |
---|
165 | if hasattr(wksp, "getNumberHistograms"): |
---|
166 | if wksp.getNumberHistograms() <= 0: |
---|
167 | print "Workspace has zero histograms" |
---|
168 | del wksp |
---|
169 | return False |
---|
170 | if "managed" not in id.lower() and wksp.getMemorySize() <= 0: |
---|
171 | print "Workspace takes no memory: Memory used=" + str(wksp.getMemorySize()) |
---|
172 | del wksp |
---|
173 | return False |
---|
174 | |
---|
175 | # checks for EventWorkspace |
---|
176 | if hasattr(wksp, "getNumberEvents"): |
---|
177 | if wksp.getNumberEvents() <= 0: |
---|
178 | print "EventWorkspace does not have events" |
---|
179 | del wksp |
---|
180 | return False |
---|
181 | |
---|
182 | # do the extra checks |
---|
183 | result = self.__runExtraTests__(wksp, filename) |
---|
184 | |
---|
185 | # cleanup |
---|
186 | del wksp |
---|
187 | return result |
---|
188 | |
---|
189 | def runTest(self): |
---|
190 | """Main entry point for the test suite""" |
---|
191 | files = self.__getDataFileList__() |
---|
192 | |
---|
193 | # run the tests |
---|
194 | failed = [] |
---|
195 | for filename in files: |
---|
196 | try: |
---|
197 | if not self.__loadAndTest__(filename): |
---|
198 | print "FAILED TO LOAD '%s'" % filename |
---|
199 | failed.append(filename) |
---|
200 | except Exception, e: |
---|
201 | print "FAILED TO LOAD '%s' WITH ERROR:" % filename |
---|
202 | print e |
---|
203 | failed.append(filename) |
---|
204 | finally: |
---|
205 | # Clear everything for the next test |
---|
206 | FrameworkManager.Instance().clear() |
---|
207 | |
---|
208 | # final say on whether or not it 'worked' |
---|
209 | print "----------------------------------------" |
---|
210 | if len(failed) != 0: |
---|
211 | print "SUMMARY OF FAILED FILES" |
---|
212 | for filename in failed: |
---|
213 | print filename |
---|
214 | raise RuntimeError("Failed to load %d of %d files" \ |
---|
215 | % (len(failed), len(files))) |
---|
216 | else: |
---|
217 | print "Successfully loaded %d files" % len(files) |
---|
218 | |
---|
219 | |
---|
220 | a = LoadLotsOfFiles() |
---|
221 | for f in a.__getDataFileList__()[:50]: |
---|
222 | try: |
---|
223 | Load(f,OutputWorkspace=f.split('/')[-1]) |
---|
224 | except: |
---|
225 | pass |
---|
226 | |
---|