 Timestamp:
 Apr 6, 2017 11:04:42 AM (2 years ago)
 Branches:
 master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, costrafo411, magnetic_scatt, release4.2.2, ticket1009, ticket1094headless, ticket12422dresolution, ticket1243, ticket1249, ticket885, unittestsaveload
 Children:
 a470e88
 Parents:
 00a6ec4
 Location:
 test
 Files:

 19 added
 1 edited
Legend:
 Unmodified
 Added
 Removed

test/sasguiframe/test/utest_manipulations.py
r9a5097c rfd5d6eac 2 2 Unit tests for data manipulations 3 3 """ 4 #TODO: what happens if you add a Data1D to a Data2D? 5 4 # TODO: what happens if you add a Data1D to a Data2D? 5 6 import math 7 import os.path 6 8 import unittest 7 import math 9 8 10 import numpy as np 9 from sas.sascalc.dataloader.loader import Loader 10 from sas.sas gui.guiframe.dataFitting import Data1D, Data2D11 12 from sas.sascalc.dataloader.loader import Loader 11 13 from sas.sasgui.guiframe.dataFitting import Data1D as Theory1D 12 13 import os.path 14 from sas.sasgui.guiframe.dataFitting import Data2D 15 14 16 15 17 class data_info_tests(unittest.TestCase): 16 18 17 19 def setUp(self): 18 20 data = Loader().load("cansas1d.xml") 19 21 self.data = data[0] 20 22 21 23 def test_clone1D(self): 22 24 """ … … 24 26 """ 25 27 clone = self.data.clone_without_data() 26 28 27 29 for i in range(len(self.data.detector)): 28 self.assertEqual(self.data.detector[i].distance, clone.detector[i].distance) 29 30 self.assertEqual( 31 self.data.detector[i].distance, clone.detector[i].distance) 32 33 30 34 class theory1d_tests(unittest.TestCase): 31 35 32 36 def setUp(self): 33 37 data = Loader().load("cansas1d.xml") 34 38 self.data = data[0] 35 39 36 40 def test_clone_theory1D(self): 37 41 """ … … 42 46 theory.copy_from_datainfo(data1d=self.data) 43 47 for i in range(len(self.data.detector)): 44 self.assertEqual(self.data.detector[i].distance, theory.detector[i].distance) 45 48 self.assertEqual( 49 self.data.detector[i].distance, theory.detector[i].distance) 50 46 51 for i in range(len(self.data.x)): 47 52 self.assertEqual(self.data.x[i], theory.x[i]) … … 49 54 self.assertEqual(self.data.dy[i], theory.dy[i]) 50 55 56 51 57 class manip_tests(unittest.TestCase): 52 58 53 59 def setUp(self): 54 60 # Create two data sets to play with 55 61 x_0 = np.ones(5) 56 62 for i in range(5): 57 x_0[i] = x_0[i] *(i+1.0)58 59 y_0 = 2.0 *np.ones(5)60 dy_0 = 0.5 *np.ones(5)63 x_0[i] = x_0[i] * (i + 1.0) 64 65 y_0 = 2.0 * np.ones(5) 66 dy_0 = 0.5 * np.ones(5) 61 67 self.data = Data1D(x_0, y_0, dy=dy_0) 62 68 63 69 x = self.data.x 64 70 y = np.ones(5) 65 71 dy = np.ones(5) 66 72 self.data2 = Data1D(x, y, dy=dy) 67 68 73 69 74 def test_load(self): 70 75 """ … … 73 78 # There should be 5 entries in the file 74 79 self.assertEqual(len(self.data.x), 5) 75 80 76 81 for i in range(5): 77 82 # The x values should be from 1 to 5 78 self.assertEqual(self.data.x[i], float(i +1))79 83 self.assertEqual(self.data.x[i], float(i + 1)) 84 80 85 # All yerror values should be 0.5 81 self.assertEqual(self.data.dy[i], 0.5) 82 86 self.assertEqual(self.data.dy[i], 0.5) 87 83 88 # All y values should be 2.0 84 self.assertEqual(self.data.y[i], 2.0) 85 89 self.assertEqual(self.data.y[i], 2.0) 90 86 91 def test_add(self): 87 result = self.data2 +self.data92 result = self.data2 + self.data 88 93 for i in range(5): 89 94 self.assertEqual(result.y[i], 3.0) 90 self.assertEqual(result.dy[i], math.sqrt(0.5**2 +1.0))91 95 self.assertEqual(result.dy[i], math.sqrt(0.5**2 + 1.0)) 96 92 97 def test_sub(self): 93 result = self.data2 self.data98 result = self.data2  self.data 94 99 for i in range(5): 95 100 self.assertEqual(result.y[i], 1.0) 96 self.assertEqual(result.dy[i], math.sqrt(0.5**2 +1.0))97 101 self.assertEqual(result.dy[i], math.sqrt(0.5**2 + 1.0)) 102 98 103 def test_mul(self): 99 result = self.data2 *self.data104 result = self.data2 * self.data 100 105 for i in range(5): 101 106 self.assertEqual(result.y[i], 2.0) 102 self.assertEqual(result.dy[i], math.sqrt((0.5*1.0)**2+(1.0*2.0)**2)) 103 107 self.assertEqual(result.dy[i], math.sqrt( 108 (0.5 * 1.0)**2 + (1.0 * 2.0)**2)) 109 104 110 def test_div(self): 105 result = self.data2 /self.data111 result = self.data2 / self.data 106 112 for i in range(5): 107 113 self.assertEqual(result.y[i], 0.5) 108 self.assertEqual(result.dy[i], math.sqrt((1.0/2.0)**2+(0.5*1.0/4.0)**2)) 109 114 self.assertEqual(result.dy[i], math.sqrt( 115 (1.0 / 2.0)**2 + (0.5 * 1.0 / 4.0)**2)) 116 110 117 def test_radd(self): 111 result = self.data +3.0118 result = self.data + 3.0 112 119 for i in range(5): 113 120 self.assertEqual(result.y[i], 5.0) 114 121 self.assertEqual(result.dy[i], 0.5) 115 116 result = 3.0 +self.data122 123 result = 3.0 + self.data 117 124 for i in range(5): 118 125 self.assertEqual(result.y[i], 5.0) 119 126 self.assertEqual(result.dy[i], 0.5) 120 127 121 128 def test_rsub(self): 122 result = self.data 3.0129 result = self.data  3.0 123 130 for i in range(5): 124 131 self.assertEqual(result.y[i], 1.0) 125 132 self.assertEqual(result.dy[i], 0.5) 126 127 result = 3.0 self.data133 134 result = 3.0  self.data 128 135 for i in range(5): 129 136 self.assertEqual(result.y[i], 1.0) 130 137 self.assertEqual(result.dy[i], 0.5) 131 138 132 139 def test_rmul(self): 133 result = self.data *3.0140 result = self.data * 3.0 134 141 for i in range(5): 135 142 self.assertEqual(result.y[i], 6.0) 136 143 self.assertEqual(result.dy[i], 1.5) 137 138 result = 3.0 *self.data144 145 result = 3.0 * self.data 139 146 for i in range(5): 140 147 self.assertEqual(result.y[i], 6.0) 141 148 self.assertEqual(result.dy[i], 1.5) 142 149 143 150 def test_rdiv(self): 144 result = self.data /4.0151 result = self.data / 4.0 145 152 for i in range(5): 146 153 self.assertEqual(result.y[i], 0.5) 147 154 self.assertEqual(result.dy[i], 0.125) 148 149 result = 6.0 /self.data155 156 result = 6.0 / self.data 150 157 for i in range(5): 151 158 self.assertEqual(result.y[i], 3.0) 152 self.assertEqual(result.dy[i], 6.0*0.5/4.0) 153 159 self.assertEqual(result.dy[i], 6.0 * 0.5 / 4.0) 160 161 154 162 class manip_2D(unittest.TestCase): 155 163 156 164 def setUp(self): 157 165 # Create two data sets to play with 158 x_0 = 2.0 *np.ones(25)159 dx_0 = 0.5 *np.ones(25)166 x_0 = 2.0 * np.ones(25) 167 dx_0 = 0.5 * np.ones(25) 160 168 qx_0 = np.arange(25) 161 169 qy_0 = np.arange(25) 162 170 mask_0 = np.zeros(25) 163 dqx_0 = np.arange(25) /100164 dqy_0 = np.arange(25) /100171 dqx_0 = np.arange(25) / 100 172 dqy_0 = np.arange(25) / 100 165 173 q_0 = np.sqrt(qx_0 * qx_0 + qy_0 * qy_0) 166 self.data = Data2D(image=x_0, err_image=dx_0, qx_data=qx_0, 167 qy_data=qy_0, q_data=q_0, mask=mask_0, 174 self.data = Data2D(image=x_0, err_image=dx_0, qx_data=qx_0, 175 qy_data=qy_0, q_data=q_0, mask=mask_0, 168 176 dqx_data=dqx_0, dqy_data=dqy_0) 169 177 170 178 y = np.ones(25) 171 179 dy = np.ones(25) … … 174 182 mask = np.zeros(25) 175 183 q = np.sqrt(qx * qx + qy * qy) 176 self.data2 = Data2D(image=y, err_image=dy, qx_data=qx, qy_data=qy, 184 self.data2 = Data2D(image=y, err_image=dy, qx_data=qx, qy_data=qy, 177 185 q_data=q, mask=mask) 178 179 186 180 187 def test_load(self): 181 188 """ … … 184 191 # There should be 5 entries in the file 185 192 self.assertEqual(np.size(self.data.data), 25) 186 193 187 194 for i in range(25): 188 195 # All yerror values should be 0.5 189 self.assertEqual(self.data.err_data[i], 0.5) 190 196 self.assertEqual(self.data.err_data[i], 0.5) 197 191 198 # All y values should be 2.0 192 self.assertEqual(self.data.data[i], 2.0) 193 199 self.assertEqual(self.data.data[i], 2.0) 200 194 201 def test_add(self): 195 result = self.data2 +self.data202 result = self.data2 + self.data 196 203 for i in range(25): 197 204 self.assertEqual(result.data[i], 3.0) 198 self.assertEqual(result.err_data[i], math.sqrt(0.5**2 +1.0))199 205 self.assertEqual(result.err_data[i], math.sqrt(0.5**2 + 1.0)) 206 200 207 def test_sub(self): 201 result = self.data2 self.data202 for i in range(25): 203 204 self.assertEqual(result.err_data[i], math.sqrt(0.5**2+1.0))205 208 result = self.data2  self.data 209 for i in range(25): 210 self.assertEqual(result.data[i], 1.0) 211 self.assertEqual(result.err_data[i], math.sqrt(0.5**2 + 1.0)) 212 206 213 def test_mul(self): 207 result = self.data2 *self.data214 result = self.data2 * self.data 208 215 for i in range(25): 209 216 self.assertEqual(result.data[i], 2.0) 210 self.assertEqual(result.err_data[i], math.sqrt((0.5*1.0)**2+(1.0*2.0)**2)) 211 217 self.assertEqual(result.err_data[i], math.sqrt( 218 (0.5 * 1.0)**2 + (1.0 * 2.0)**2)) 219 212 220 def test_div(self): 213 result = self.data2 /self.data221 result = self.data2 / self.data 214 222 for i in range(25): 215 223 self.assertEqual(result.data[i], 0.5) 216 self.assertEqual(result.err_data[i], math.sqrt((1.0/2.0)**2+(0.5*1.0/4.0)**2)) 217 224 self.assertEqual(result.err_data[i], math.sqrt( 225 (1.0 / 2.0)**2 + (0.5 * 1.0 / 4.0)**2)) 226 218 227 def test_radd(self): 219 result = self.data +3.0228 result = self.data + 3.0 220 229 for i in range(25): 221 230 self.assertEqual(result.data[i], 5.0) 222 231 self.assertEqual(result.err_data[i], 0.5) 223 224 result = 3.0 +self.data232 233 result = 3.0 + self.data 225 234 for i in range(25): 226 235 self.assertEqual(result.data[i], 5.0) 227 236 self.assertEqual(result.err_data[i], 0.5) 228 237 229 238 def test_rsub(self): 230 result = self.data 3.0239 result = self.data  3.0 231 240 for i in range(25): 232 241 self.assertEqual(result.data[i], 1.0) 233 242 self.assertEqual(result.err_data[i], 0.5) 234 235 result = 3.0 self.data243 244 result = 3.0  self.data 236 245 for i in range(25): 237 246 self.assertEqual(result.data[i], 1.0) 238 247 self.assertEqual(result.err_data[i], 0.5) 239 248 240 249 def test_rmul(self): 241 result = self.data *3.0250 result = self.data * 3.0 242 251 for i in range(25): 243 252 self.assertEqual(result.data[i], 6.0) 244 253 self.assertEqual(result.err_data[i], 1.5) 245 246 result = 3.0 *self.data254 255 result = 3.0 * self.data 247 256 for i in range(25): 248 257 self.assertEqual(result.data[i], 6.0) 249 258 self.assertEqual(result.err_data[i], 1.5) 250 259 251 260 def test_rdiv(self): 252 result = self.data /4.0261 result = self.data / 4.0 253 262 for i in range(25): 254 263 self.assertEqual(result.data[i], 0.5) 255 264 self.assertEqual(result.err_data[i], 0.125) 256 265 257 result = 6.0 /self.data266 result = 6.0 / self.data 258 267 for i in range(25): 259 268 self.assertEqual(result.data[i], 3.0) 260 self.assertEqual(result.err_data[i], 6.0*0.5/4.0) 261 269 self.assertEqual(result.err_data[i], 6.0 * 0.5 / 4.0) 270 271 262 272 class extra_manip_2D(unittest.TestCase): 263 273 264 274 def setUp(self): 265 275 # Create two data sets to play with 266 x_0 = 2.0 *np.ones(25)267 dx_0 = 0.5 *np.ones(25)276 x_0 = 2.0 * np.ones(25) 277 dx_0 = 0.5 * np.ones(25) 268 278 qx_0 = np.arange(25) 269 279 qy_0 = np.arange(25) 270 280 mask_0 = np.zeros(25) 271 dqx_0 = np.arange(25) /100272 dqy_0 = np.arange(25) /100281 dqx_0 = np.arange(25) / 100 282 dqy_0 = np.arange(25) / 100 273 283 q_0 = np.sqrt(qx_0 * qx_0 + qy_0 * qy_0) 274 self.data = Data2D(image=x_0, err_image=dx_0, qx_data=qx_0, 275 qy_data=qy_0, q_data=q_0, mask=mask_0, 284 self.data = Data2D(image=x_0, err_image=dx_0, qx_data=qx_0, 285 qy_data=qy_0, q_data=q_0, mask=mask_0, 276 286 dqx_data=dqx_0, dqy_data=dqy_0) 277 287 278 288 y = np.ones(25) 279 289 dy = np.ones(25) … … 282 292 mask = np.zeros(25) 283 293 q = np.sqrt(qx * qx + qy * qy) 284 self.data2 = Data2D(image=y, err_image=dy, qx_data=qx, qy_data=qy, 294 self.data2 = Data2D(image=y, err_image=dy, qx_data=qx, qy_data=qy, 285 295 q_data=q, mask=mask) 286 287 296 288 297 def test_load(self): 289 298 """ … … 292 301 # There should be 5 entries in the file 293 302 self.assertEqual(np.size(self.data.data), 25) 294 303 295 304 for i in range(25): 296 305 # All yerror values should be 0.5 297 self.assertEqual(self.data.err_data[i], 0.5) 298 306 self.assertEqual(self.data.err_data[i], 0.5) 307 299 308 # All y values should be 2.0 300 self.assertEqual(self.data.data[i], 2.0) 301 309 self.assertEqual(self.data.data[i], 2.0) 310 302 311 def test_add(self): 303 result = self.data2 +self.data312 result = self.data2 + self.data 304 313 for i in range(25): 305 314 self.assertEqual(result.data[i], 3.0) 306 self.assertEqual(result.err_data[i], math.sqrt(0.5**2 +1.0))307 315 self.assertEqual(result.err_data[i], math.sqrt(0.5**2 + 1.0)) 316 308 317 def test_sub(self): 309 result = self.data2 self.data310 for i in range(25): 311 312 self.assertEqual(result.err_data[i], math.sqrt(0.5**2+1.0))313 318 result = self.data2  self.data 319 for i in range(25): 320 self.assertEqual(result.data[i], 1.0) 321 self.assertEqual(result.err_data[i], math.sqrt(0.5**2 + 1.0)) 322 314 323 def test_mul(self): 315 result = self.data2 *self.data324 result = self.data2 * self.data 316 325 for i in range(25): 317 326 self.assertEqual(result.data[i], 2.0) 318 self.assertEqual(result.err_data[i], math.sqrt((0.5*1.0)**2+(1.0*2.0)**2)) 319 327 self.assertEqual(result.err_data[i], math.sqrt( 328 (0.5 * 1.0)**2 + (1.0 * 2.0)**2)) 329 320 330 def test_div(self): 321 result = self.data2 /self.data331 result = self.data2 / self.data 322 332 for i in range(25): 323 333 self.assertEqual(result.data[i], 0.5) 324 self.assertEqual(result.err_data[i], math.sqrt((1.0/2.0)**2+(0.5*1.0/4.0)**2)) 325 334 self.assertEqual(result.err_data[i], math.sqrt( 335 (1.0 / 2.0)**2 + (0.5 * 1.0 / 4.0)**2)) 336 326 337 def test_radd(self): 327 result = self.data +3.0338 result = self.data + 3.0 328 339 for i in range(25): 329 340 self.assertEqual(result.data[i], 5.0) 330 341 self.assertEqual(result.err_data[i], 0.5) 331 332 result = 3.0 +self.data342 343 result = 3.0 + self.data 333 344 for i in range(25): 334 345 self.assertEqual(result.data[i], 5.0) 335 346 self.assertEqual(result.err_data[i], 0.5) 336 347 337 348 def test_rsub(self): 338 result = self.data 3.0349 result = self.data  3.0 339 350 for i in range(25): 340 351 self.assertEqual(result.data[i], 1.0) 341 352 self.assertEqual(result.err_data[i], 0.5) 342 343 result = 3.0 self.data353 354 result = 3.0  self.data 344 355 for i in range(25): 345 356 self.assertEqual(result.data[i], 1.0) 346 357 self.assertEqual(result.err_data[i], 0.5) 347 358 348 359 def test_rmul(self): 349 result = self.data *3.0360 result = self.data * 3.0 350 361 for i in range(25): 351 362 self.assertEqual(result.data[i], 6.0) 352 363 self.assertEqual(result.err_data[i], 1.5) 353 354 result = 3.0 *self.data364 365 result = 3.0 * self.data 355 366 for i in range(25): 356 367 self.assertEqual(result.data[i], 6.0) 357 368 self.assertEqual(result.err_data[i], 1.5) 358 369 359 370 def test_rdiv(self): 360 result = self.data /4.0371 result = self.data / 4.0 361 372 for i in range(25): 362 373 self.assertEqual(result.data[i], 0.5) 363 374 self.assertEqual(result.err_data[i], 0.125) 364 375 365 result = 6.0 /self.data376 result = 6.0 / self.data 366 377 for i in range(25): 367 378 self.assertEqual(result.data[i], 3.0) 368 self.assertEqual(result.err_data[i], 6.0*0.5/4.0) 379 self.assertEqual(result.err_data[i], 6.0 * 0.5 / 4.0) 380 369 381 370 382 if __name__ == '__main__': 371 383 unittest.main() 372
Note: See TracChangeset
for help on using the changeset viewer.