1 | """ |
---|
2 | Module that contains classes to hold information read from |
---|
3 | reduced data files. |
---|
4 | |
---|
5 | A good description of the data members can be found in |
---|
6 | the CanSAS 1D XML data format: |
---|
7 | |
---|
8 | http://www.smallangles.net/wgwiki/index.php/cansas1d_documentation |
---|
9 | """ |
---|
10 | |
---|
11 | """ |
---|
12 | This software was developed by the University of Tennessee as part of the |
---|
13 | Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
14 | project funded by the US National Science Foundation. |
---|
15 | |
---|
16 | See the license text in license.txt |
---|
17 | |
---|
18 | copyright 2008, University of Tennessee |
---|
19 | """ |
---|
20 | |
---|
21 | from sans.guitools.plottables import Data1D as plottable_1D |
---|
22 | |
---|
23 | class Data2D: |
---|
24 | """ |
---|
25 | Data2D is a place holder for 2D plottables, which are |
---|
26 | not yet implemented. |
---|
27 | """ |
---|
28 | xmin = None |
---|
29 | xmax = None |
---|
30 | ymin = None |
---|
31 | ymax = None |
---|
32 | image = None |
---|
33 | |
---|
34 | class Vector: |
---|
35 | """ |
---|
36 | Vector class to hold multi-dimensional objects |
---|
37 | """ |
---|
38 | ## x component |
---|
39 | x = None |
---|
40 | ## y component |
---|
41 | y = None |
---|
42 | ## z component |
---|
43 | z = None |
---|
44 | |
---|
45 | def __init__(self, x=None, y=None, z=None): |
---|
46 | """ |
---|
47 | Initialization. Components that are not |
---|
48 | set a set to None by default. |
---|
49 | |
---|
50 | @param x: x component |
---|
51 | @param y: y component |
---|
52 | @param z: z component |
---|
53 | """ |
---|
54 | self.x = x |
---|
55 | self.y = y |
---|
56 | self.z = z |
---|
57 | |
---|
58 | def __str__(self): |
---|
59 | return "x = %s\ty = %s\tz = %s" % (str(self.x), str(self.y), str(self.z)) |
---|
60 | |
---|
61 | |
---|
62 | class Detector: |
---|
63 | """ |
---|
64 | Class to hold detector information |
---|
65 | """ |
---|
66 | ## Name of the instrument [string] |
---|
67 | name = '' |
---|
68 | ## Sample to detector distance [float] [mm] |
---|
69 | distance = None |
---|
70 | distance_unit = 'm' |
---|
71 | ## Offset of this detector position in X, Y, (and Z if necessary) [Vector] [mm] |
---|
72 | offset = Vector() |
---|
73 | offset_unit = 'mm' |
---|
74 | ## Orientation (rotation) of this detector in roll, pitch, and yaw [Vector] [degrees] |
---|
75 | orientation = Vector() |
---|
76 | orientation_unit = 'degree' |
---|
77 | ## Center of the beam on the detector in X and Y (and Z if necessary) [Vector] [pixel] |
---|
78 | beam_center = Vector() |
---|
79 | beam_center_unit = 'mm' |
---|
80 | ## Pixel size in X, Y, (and Z if necessary) [Vector] [mm] |
---|
81 | pixel_size = Vector() |
---|
82 | pixel_size_unit = 'mm' |
---|
83 | ## Slit length of the instrument for this detector.[float] [mm] |
---|
84 | slit_length = None |
---|
85 | slit_length_unit = 'mm' |
---|
86 | |
---|
87 | def __str__(self): |
---|
88 | _str = "Detector:\n" |
---|
89 | _str += " Name: %s\n" % self.name |
---|
90 | _str += " Distance: %s [%s]\n" % \ |
---|
91 | (str(self.distance), str(self.distance_unit)) |
---|
92 | _str += " Offset: %s [%s]\n" % \ |
---|
93 | (str(self.offset), str(self.offset_unit)) |
---|
94 | _str += " Orientation: %s [%s]\n" % \ |
---|
95 | (str(self.orientation), str(self.orientation_unit)) |
---|
96 | _str += " Beam center: %s [%s]\n" % \ |
---|
97 | (str(self.beam_center), str(self.beam_center_unit)) |
---|
98 | _str += " Pixel size: %s [%s]\n" % \ |
---|
99 | (str(self.pixel_size), str(self.pixel_size_unit)) |
---|
100 | _str += " Slit length: %s [%s]\n" % \ |
---|
101 | (str(self.slit_length), str(self.slit_length_unit)) |
---|
102 | return _str |
---|
103 | |
---|
104 | class Collimation: |
---|
105 | """ |
---|
106 | Class to hold collimation information |
---|
107 | """ |
---|
108 | class Aperture: |
---|
109 | # Aperture size [Vector] |
---|
110 | size = Vector() |
---|
111 | size_unit = 'mm' |
---|
112 | # Aperture distance [float] |
---|
113 | distance = None |
---|
114 | distance_unit = 'mm' |
---|
115 | |
---|
116 | ## Length [float] [mm] |
---|
117 | length = None |
---|
118 | length_unit = 'mm' |
---|
119 | ## Aperture |
---|
120 | aperture = [] |
---|
121 | |
---|
122 | def __str__(self): |
---|
123 | _str = "Collimation:\n" |
---|
124 | _str += " Length: %s [%s]\n" % \ |
---|
125 | (str(self.length), str(self.length_unit)) |
---|
126 | for item in self.aperture: |
---|
127 | _str += " Aperture size:%s [%s]\n" % \ |
---|
128 | (str(item.size), str(item.size_unit)) |
---|
129 | _str += " Aperture_dist:%s [%s]\n" % \ |
---|
130 | (str(item.distance), str(item.distance_unit)) |
---|
131 | return _str |
---|
132 | |
---|
133 | class Source: |
---|
134 | """ |
---|
135 | Class to hold source information |
---|
136 | """ |
---|
137 | ## Radiation type [string] |
---|
138 | radiation = '' |
---|
139 | ## Beam size [Vector] [mm] |
---|
140 | beam_size = Vector() |
---|
141 | beam_size_unit = 'mm' |
---|
142 | ## Beam shape [string] |
---|
143 | beam_shape = '' |
---|
144 | ## Wavelength [float] [Angstrom] |
---|
145 | wavelength = None |
---|
146 | wavelength_unit = 'A' |
---|
147 | ## Minimum wavelength [float] [Angstrom] |
---|
148 | wavelength_min = None |
---|
149 | wavelength_min_unit = 'nm' |
---|
150 | ## Maximum wavelength [float] [Angstrom] |
---|
151 | wavelength_max = None |
---|
152 | wavelength_max_unit = 'nm' |
---|
153 | ## Wavelength spread [float] [Angstrom] |
---|
154 | wavelength_spread = None |
---|
155 | wavelength_spread_unit = 'percent' |
---|
156 | |
---|
157 | def __str__(self): |
---|
158 | _str = "Source:\n" |
---|
159 | _str += " Radiation: %s\n" % str(self.radiation) |
---|
160 | _str += " Shape: %s\n" % str(self.beam_shape) |
---|
161 | _str += " Wavelength: %s [%s]\n" % \ |
---|
162 | (str(self.wavelength), str(self.wavelength_unit)) |
---|
163 | _str += " Waveln_min: %s [%s]\n" % \ |
---|
164 | (str(self.wavelength_min), str(self.wavelength_min_unit)) |
---|
165 | _str += " Waveln_max: %s [%s]\n" % \ |
---|
166 | (str(self.wavelength_max), str(self.wavelength_max_unit)) |
---|
167 | _str += " Waveln_spread:%s [%s]\n" % \ |
---|
168 | (str(self.wavelength_spread), str(self.wavelength_spread_unit)) |
---|
169 | _str += " Beam_size: %s [%s]\n" % \ |
---|
170 | (str(self.beam_size), str(self.beam_size_unit)) |
---|
171 | return _str |
---|
172 | |
---|
173 | |
---|
174 | """ |
---|
175 | Definitions of radiation types |
---|
176 | """ |
---|
177 | NEUTRON = 'neutron' |
---|
178 | XRAY = 'x-ray' |
---|
179 | MUON = 'muon' |
---|
180 | ELECTRON = 'electron' |
---|
181 | |
---|
182 | class Sample: |
---|
183 | """ |
---|
184 | Class to hold the sample description |
---|
185 | """ |
---|
186 | ## ID |
---|
187 | ID = '' |
---|
188 | ## Thickness [float] [mm] |
---|
189 | thickness = None |
---|
190 | thickness_unit = 'mm' |
---|
191 | ## Transmission [float] [fraction] |
---|
192 | transmission = None |
---|
193 | ## Temperature [float] [C] |
---|
194 | temperature = None |
---|
195 | temperature_unit = 'C' |
---|
196 | ## Position [Vector] [mm] |
---|
197 | position = Vector() |
---|
198 | position_unit = 'mm' |
---|
199 | ## Orientation [Vector] [degrees] |
---|
200 | orientation = Vector() |
---|
201 | orientation_unit = 'degree' |
---|
202 | ## Details |
---|
203 | details = [] |
---|
204 | |
---|
205 | def __str__(self): |
---|
206 | _str = "Sample:\n" |
---|
207 | _str += " ID: %s\n" % str(self.ID) |
---|
208 | _str += " Transmission: %s\n" % str(self.transmission) |
---|
209 | _str += " Thickness: %s [%s]\n" % \ |
---|
210 | (str(self.thickness), str(self.thickness_unit)) |
---|
211 | _str += " Temperature: %s [%s]\n" % \ |
---|
212 | (str(self.temperature), str(self.temperature_unit)) |
---|
213 | _str += " Position: %s [%s]\n" % \ |
---|
214 | (str(self.position), str(self.position_unit)) |
---|
215 | _str += " Orientation: %s [%s]\n" % \ |
---|
216 | (str(self.orientation), str(self.orientation_unit)) |
---|
217 | |
---|
218 | _str += " Details:\n" |
---|
219 | for item in self.details: |
---|
220 | _str += " %s\n" % item |
---|
221 | |
---|
222 | return _str |
---|
223 | |
---|
224 | class Process: |
---|
225 | """ |
---|
226 | Class that holds information about the processes |
---|
227 | performed on the data. |
---|
228 | """ |
---|
229 | name = '' |
---|
230 | date = '' |
---|
231 | description= '' |
---|
232 | term = [] |
---|
233 | notes = [] |
---|
234 | |
---|
235 | def __str__(self): |
---|
236 | _str = "Process:\n" |
---|
237 | _str += " Name: %s\n" % self.name |
---|
238 | _str += " Date: %s\n" % self.date |
---|
239 | _str += " Description: %s\n" % self.description |
---|
240 | for item in self.term: |
---|
241 | _str += " Term: %s\n" % item |
---|
242 | for item in self.notes: |
---|
243 | _str += " Note: %s\n" % item |
---|
244 | return _str |
---|
245 | |
---|
246 | |
---|
247 | class DataInfo: |
---|
248 | """ |
---|
249 | Class to hold the data read from a file. |
---|
250 | It includes four blocks of data for the |
---|
251 | instrument description, the sample description, |
---|
252 | the data itself and any other meta data. |
---|
253 | """ |
---|
254 | ## Title |
---|
255 | title = '' |
---|
256 | ## Run number |
---|
257 | run = None |
---|
258 | ## File name |
---|
259 | filename = '' |
---|
260 | ## Notes |
---|
261 | notes = [] |
---|
262 | ## Processes (Action on the data) |
---|
263 | process = [] |
---|
264 | ## Instrument name |
---|
265 | instrument = '' |
---|
266 | ## Detector information |
---|
267 | detector = [] |
---|
268 | ## Sample information |
---|
269 | sample = Sample() |
---|
270 | ## Source information |
---|
271 | source = Source() |
---|
272 | ## Collimation information |
---|
273 | collimation = [] |
---|
274 | ## Additional meta-data |
---|
275 | meta_data = {} |
---|
276 | ## Loading errors |
---|
277 | errors = [] |
---|
278 | |
---|
279 | def __add__(self, data): |
---|
280 | """ |
---|
281 | Add two data sets |
---|
282 | |
---|
283 | @param data: data set to add to the current one |
---|
284 | @return: new data set |
---|
285 | @raise ValueError: raised when two data sets are incompatible |
---|
286 | """ |
---|
287 | raise RuntimeError, "DataInfo addition is not implemented yet" |
---|
288 | |
---|
289 | def __sub__(self, data): |
---|
290 | """ |
---|
291 | Subtract two data sets |
---|
292 | |
---|
293 | @param data: data set to subtract from the current one |
---|
294 | @return: new data set |
---|
295 | @raise ValueError: raised when two data sets are incompatible |
---|
296 | """ |
---|
297 | raise RuntimeError, "DataInfo subtraction is not implemented yet" |
---|
298 | |
---|
299 | def __mul__(self, constant): |
---|
300 | """ |
---|
301 | Multiply every entry of the current data set by a constant |
---|
302 | |
---|
303 | @param constant: constant to multiply the data by |
---|
304 | @return: new data set |
---|
305 | @raise ValueError: raised when the constant is not a float |
---|
306 | """ |
---|
307 | raise RuntimeError, "DataInfo multiplication is not implemented yet" |
---|
308 | |
---|
309 | def __div__(self, constant): |
---|
310 | """ |
---|
311 | """ |
---|
312 | raise RuntimeError, "DataInfo division is not implemented yet" |
---|
313 | |
---|
314 | |
---|
315 | class Data1D(plottable_1D, DataInfo): |
---|
316 | """ |
---|
317 | 1D data class |
---|
318 | """ |
---|
319 | x_unit = '1/A' |
---|
320 | y_unit = '1/cm' |
---|
321 | |
---|
322 | def __init__(self, x, y, dx=None, dy=None): |
---|
323 | plottable_1D.__init__(self, x, y, dx, dy) |
---|
324 | |
---|
325 | def __str__(self): |
---|
326 | """ |
---|
327 | Nice printout |
---|
328 | """ |
---|
329 | _str = "File: %s\n" % self.filename |
---|
330 | _str += "Title: %s\n" % self.title |
---|
331 | _str += "Run: %s\n" % str(self.run) |
---|
332 | _str += "Instrument: %s\n" % str(self.instrument) |
---|
333 | _str += "%s\n" % str(self.sample) |
---|
334 | _str += "%s\n" % str(self.source) |
---|
335 | for item in self.detector: |
---|
336 | _str += "%s\n" % str(item) |
---|
337 | for item in self.collimation: |
---|
338 | _str += "%s\n" % str(item) |
---|
339 | for item in self.process: |
---|
340 | _str += "%s\n" % str(item) |
---|
341 | for item in self.notes: |
---|
342 | _str += "%s\n" % str(item) |
---|
343 | |
---|
344 | |
---|
345 | _str += "Data:\n" |
---|
346 | _str += " Type: %s\n" % self.__class__.__name__ |
---|
347 | _str += " X-axis: %s\t[%s]\n" % (self._xaxis, self._xunit) |
---|
348 | _str += " Y-axis: %s\t[%s]\n" % (self._yaxis, self._yunit) |
---|
349 | _str += " Length: %g\n" % len(self.x) |
---|
350 | |
---|
351 | return _str |
---|
352 | |
---|
353 | |
---|
354 | |
---|
355 | |
---|