Changes in / [a470e88:ec8886e] in sasview
- Files:
-
- 19 deleted
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/sascalc/dataloader/manipulations.py
rfd5d6eac rdd11014 5 5 """ 6 6 ##################################################################### 7 # 8 # 9 # 10 # 11 # 7 #This software was developed by the University of Tennessee as part of the 8 #Distributed Data Analysis of Neutron Scattering Experiments (DANSE) 9 #project funded by the US National Science Foundation. 10 #See the license text in license.txt 11 #copyright 2008, University of Tennessee 12 12 ###################################################################### 13 13 14 # If you want to run just a single test from this file: 15 # PYTHONPATH=../src/ python2 -m sasdataloader.test.utest_averaging data_info_tests.test_sectorq_full 16 # TODO: copy the meta data from the 2D object to the resulting 1D object 14 #TODO: copy the meta data from the 2D object to the resulting 1D object 17 15 import math 18 import numpy as np16 import numpy 19 17 20 18 #from data_info import plottable_2D … … 84 82 if data2d.data is None or data2d.x_bins is None or data2d.y_bins is None: 85 83 raise ValueError, "Can't convert this data: data=None..." 86 new_x = n p.tile(data2d.x_bins, (len(data2d.y_bins), 1))87 new_y = n p.tile(data2d.y_bins, (len(data2d.x_bins), 1))84 new_x = numpy.tile(data2d.x_bins, (len(data2d.y_bins), 1)) 85 new_y = numpy.tile(data2d.y_bins, (len(data2d.x_bins), 1)) 88 86 new_y = new_y.swapaxes(0, 1) 89 87 … … 91 89 qx_data = new_x.flatten() 92 90 qy_data = new_y.flatten() 93 q_data = n p.sqrt(qx_data * qx_data + qy_data * qy_data)94 if data2d.err_data is None or n p.any(data2d.err_data <= 0):95 new_err_data = n p.sqrt(np.abs(new_data))91 q_data = numpy.sqrt(qx_data * qx_data + qy_data * qy_data) 92 if data2d.err_data is None or numpy.any(data2d.err_data <= 0): 93 new_err_data = numpy.sqrt(numpy.abs(new_data)) 96 94 else: 97 95 new_err_data = data2d.err_data.flatten() 98 mask = n p.ones(len(new_data), dtype=bool)99 100 # 96 mask = numpy.ones(len(new_data), dtype=bool) 97 98 #TODO: make sense of the following two lines... 101 99 #from sas.sascalc.dataloader.data_info import Data2D 102 100 #output = Data2D() … … 116 114 Compute average I(Q) for a region of interest 117 115 """ 118 119 116 def __init__(self, x_min=0.0, x_max=0.0, y_min=0.0, 120 117 y_max=0.0, bin_width=0.001): … … 152 149 153 150 # Get data 154 data = data2D.data[n p.isfinite(data2D.data)]155 err_data = data2D.err_data[n p.isfinite(data2D.data)]156 qx_data = data2D.qx_data[n p.isfinite(data2D.data)]157 qy_data = data2D.qy_data[n p.isfinite(data2D.data)]151 data = data2D.data[numpy.isfinite(data2D.data)] 152 err_data = data2D.err_data[numpy.isfinite(data2D.data)] 153 qx_data = data2D.qx_data[numpy.isfinite(data2D.data)] 154 qy_data = data2D.qy_data[numpy.isfinite(data2D.data)] 158 155 159 156 # Build array of Q intervals … … 173 170 raise RuntimeError, "_Slab._avg: unrecognized axis %s" % str(maj) 174 171 175 x = n p.zeros(nbins)176 y = n p.zeros(nbins)177 err_y = n p.zeros(nbins)178 y_counts = n p.zeros(nbins)172 x = numpy.zeros(nbins) 173 y = numpy.zeros(nbins) 174 err_y = numpy.zeros(nbins) 175 y_counts = numpy.zeros(nbins) 179 176 180 177 # Average pixelsize in q space … … 208 205 continue 209 206 210 # 207 #TODO: find better definition of x[i_q] based on q_data 211 208 # min_value + (i_q + 1) * self.bin_width / 2.0 212 209 x[i_q] += frac * q_value 213 210 y[i_q] += frac * data[npts] 214 211 215 if err_data isNone or err_data[npts] == 0.0:212 if err_data == None or err_data[npts] == 0.0: 216 213 if data[npts] < 0: 217 214 data[npts] = -data[npts] … … 228 225 y = y / y_counts 229 226 x = x / y_counts 230 idx = (n p.isfinite(y) & np.isfinite(x))227 idx = (numpy.isfinite(y) & numpy.isfinite(x)) 231 228 232 229 if not idx.any(): … … 240 237 Compute average I(Qy) for a region of interest 241 238 """ 242 243 239 def __call__(self, data2D): 244 240 """ … … 255 251 Compute average I(Qx) for a region of interest 256 252 """ 257 258 253 def __call__(self, data2D): 259 254 """ … … 269 264 Perform the sum of counts in a 2D region of interest. 270 265 """ 271 272 266 def __init__(self, x_min=0.0, x_max=0.0, y_min=0.0, y_max=0.0): 273 267 # Minimum Qx value [A-1] … … 310 304 raise RuntimeError, msg 311 305 # Get data 312 data = data2D.data[n p.isfinite(data2D.data)]313 err_data = data2D.err_data[n p.isfinite(data2D.data)]314 qx_data = data2D.qx_data[n p.isfinite(data2D.data)]315 qy_data = data2D.qy_data[n p.isfinite(data2D.data)]306 data = data2D.data[numpy.isfinite(data2D.data)] 307 err_data = data2D.err_data[numpy.isfinite(data2D.data)] 308 qx_data = data2D.qx_data[numpy.isfinite(data2D.data)] 309 qy_data = data2D.qy_data[numpy.isfinite(data2D.data)] 316 310 317 311 y = 0.0 … … 334 328 if self.y_min <= qy and self.y_max > qy: 335 329 frac_y = 1 336 # 330 #Find the fraction along each directions 337 331 frac = frac_x * frac_y 338 332 if frac == 0: 339 333 continue 340 334 y += frac * data[npts] 341 if err_data isNone or err_data[npts] == 0.0:335 if err_data == None or err_data[npts] == 0.0: 342 336 if data[npts] < 0: 343 337 data[npts] = -data[npts] … … 353 347 Perform the average of counts in a 2D region of interest. 354 348 """ 355 356 349 def __init__(self, x_min=0.0, x_max=0.0, y_min=0.0, y_max=0.0): 357 350 super(Boxavg, self).__init__(x_min=x_min, x_max=x_max, … … 405 398 as a function of Q 406 399 """ 407 408 400 def __init__(self, r_min=0.0, r_max=0.0, bin_width=0.0005): 409 401 # Minimum radius included in the average [A-1] … … 422 414 """ 423 415 # Get data W/ finite values 424 data = data2D.data[n p.isfinite(data2D.data)]425 q_data = data2D.q_data[n p.isfinite(data2D.data)]426 err_data = data2D.err_data[n p.isfinite(data2D.data)]427 mask_data = data2D.mask[n p.isfinite(data2D.data)]416 data = data2D.data[numpy.isfinite(data2D.data)] 417 q_data = data2D.q_data[numpy.isfinite(data2D.data)] 418 err_data = data2D.err_data[numpy.isfinite(data2D.data)] 419 mask_data = data2D.mask[numpy.isfinite(data2D.data)] 428 420 429 421 dq_data = None … … 456 448 dq_overlap_y *= dq_overlap_y 457 449 458 dq_overlap = n p.sqrt((dq_overlap_x + dq_overlap_y) / 2.0)450 dq_overlap = numpy.sqrt((dq_overlap_x + dq_overlap_y) / 2.0) 459 451 # Final protection of dq 460 452 if dq_overlap < 0: 461 453 dq_overlap = y_min 462 dqx_data = data2D.dqx_data[np.isfinite(data2D.data)] 463 dqy_data = data2D.dqy_data[np.isfinite( 464 data2D.data)] - dq_overlap 454 dqx_data = data2D.dqx_data[numpy.isfinite(data2D.data)] 455 dqy_data = data2D.dqy_data[numpy.isfinite(data2D.data)] - dq_overlap 465 456 # def; dqx_data = dq_r dqy_data = dq_phi 466 457 # Convert dq 2D to 1D here 467 458 dqx = dqx_data * dqx_data 468 459 dqy = dqy_data * dqy_data 469 dq_data = n p.add(dqx, dqy)470 dq_data = n p.sqrt(dq_data)471 472 #q_data_max = n p.max(q_data)460 dq_data = numpy.add(dqx, dqy) 461 dq_data = numpy.sqrt(dq_data) 462 463 #q_data_max = numpy.max(q_data) 473 464 if len(data2D.q_data) == None: 474 465 msg = "Circular averaging: invalid q_data: %g" % data2D.q_data … … 478 469 nbins = int(math.ceil((self.r_max - self.r_min) / self.bin_width)) 479 470 480 x = n p.zeros(nbins)481 y = n p.zeros(nbins)482 err_y = n p.zeros(nbins)483 err_x = n p.zeros(nbins)484 y_counts = n p.zeros(nbins)471 x = numpy.zeros(nbins) 472 y = numpy.zeros(nbins) 473 err_y = numpy.zeros(nbins) 474 err_x = numpy.zeros(nbins) 475 y_counts = numpy.zeros(nbins) 485 476 486 477 for npt in range(len(data)): … … 495 486 data_n = data[npt] 496 487 497 # No need to calculate the frac when all data are within range488 ## No need to calculate the frac when all data are within range 498 489 if self.r_min >= self.r_max: 499 490 raise ValueError, "Limit Error: min > max" … … 511 502 # Take dqs from data to get the q_average 512 503 x[i_q] += frac * q_value 513 if err_data isNone or err_data[npt] == 0.0:504 if err_data == None or err_data[npt] == 0.0: 514 505 if data_n < 0: 515 506 data_n = -data_n … … 532 523 err_y[n] = -err_y[n] 533 524 err_y[n] = math.sqrt(err_y[n]) 534 # 525 #if err_x != None: 535 526 # err_x[n] = math.sqrt(err_x[n]) 536 527 537 528 err_y = err_y / y_counts 538 err_y[err_y == 0] = n p.average(err_y)529 err_y[err_y == 0] = numpy.average(err_y) 539 530 y = y / y_counts 540 531 x = x / y_counts 541 idx = (n p.isfinite(y)) & (np.isfinite(x))532 idx = (numpy.isfinite(y)) & (numpy.isfinite(x)) 542 533 543 534 if err_x != None: … … 565 556 in anti-clockwise starting from the x- axis on the left-hand side 566 557 """ 567 # Todo: remove center. 568 558 #Todo: remove center. 569 559 def __init__(self, r_min=0, r_max=0, center_x=0, center_y=0, nbins=36): 570 560 # Minimum radius … … 579 569 self.nbins_phi = nbins 580 570 571 581 572 def __call__(self, data2D): 582 573 """ … … 594 585 595 586 # Get data 596 data = data2D.data[n p.isfinite(data2D.data)]597 q_data = data2D.q_data[n p.isfinite(data2D.data)]598 err_data = data2D.err_data[n p.isfinite(data2D.data)]599 qx_data = data2D.qx_data[n p.isfinite(data2D.data)]600 qy_data = data2D.qy_data[n p.isfinite(data2D.data)]587 data = data2D.data[numpy.isfinite(data2D.data)] 588 q_data = data2D.q_data[numpy.isfinite(data2D.data)] 589 err_data = data2D.err_data[numpy.isfinite(data2D.data)] 590 qx_data = data2D.qx_data[numpy.isfinite(data2D.data)] 591 qy_data = data2D.qy_data[numpy.isfinite(data2D.data)] 601 592 602 593 # Set space for 1d outputs 603 phi_bins = n p.zeros(self.nbins_phi)604 phi_counts = n p.zeros(self.nbins_phi)605 phi_values = n p.zeros(self.nbins_phi)606 phi_err = n p.zeros(self.nbins_phi)594 phi_bins = numpy.zeros(self.nbins_phi) 595 phi_counts = numpy.zeros(self.nbins_phi) 596 phi_values = numpy.zeros(self.nbins_phi) 597 phi_err = numpy.zeros(self.nbins_phi) 607 598 608 599 # Shift to apply to calculated phi values in order … … 624 615 continue 625 616 # binning 626 i_phi = int(math.floor((self.nbins_phi) * 617 i_phi = int(math.floor((self.nbins_phi) * \ 627 618 (phi_value + phi_shift) / (2 * Pi))) 628 619 … … 632 623 phi_bins[i_phi] += frac * data[npt] 633 624 634 if err_data isNone or err_data[npt] == 0.0:625 if err_data == None or err_data[npt] == 0.0: 635 626 if data_n < 0: 636 627 data_n = -data_n … … 645 636 phi_values[i] = 2.0 * math.pi / self.nbins_phi * (1.0 * i) 646 637 647 idx = (n p.isfinite(phi_bins))638 idx = (numpy.isfinite(phi_bins)) 648 639 649 640 if not idx.any(): 650 641 msg = "Average Error: No points inside ROI to average..." 651 642 raise ValueError, msg 652 # 643 #elif len(phi_bins[idx])!= self.nbins_phi: 653 644 # print "resulted",self.nbins_phi- len(phi_bins[idx]) 654 645 #,"empty bin(s) due to tight binning..." … … 757 748 starting from the x- axis on the left-hand side 758 749 """ 759 760 750 def __init__(self, r_min, r_max, phi_min=0, phi_max=2 * math.pi, nbins=20): 761 751 self.r_min = r_min … … 779 769 780 770 # Get the all data & info 781 data = data2D.data[n p.isfinite(data2D.data)]782 q_data = data2D.q_data[n p.isfinite(data2D.data)]783 err_data = data2D.err_data[n p.isfinite(data2D.data)]784 qx_data = data2D.qx_data[n p.isfinite(data2D.data)]785 qy_data = data2D.qy_data[n p.isfinite(data2D.data)]771 data = data2D.data[numpy.isfinite(data2D.data)] 772 q_data = data2D.q_data[numpy.isfinite(data2D.data)] 773 err_data = data2D.err_data[numpy.isfinite(data2D.data)] 774 qx_data = data2D.qx_data[numpy.isfinite(data2D.data)] 775 qy_data = data2D.qy_data[numpy.isfinite(data2D.data)] 786 776 dq_data = None 787 777 … … 813 803 dq_overlap_y *= dq_overlap_y 814 804 815 dq_overlap = n p.sqrt((dq_overlap_x + dq_overlap_y) / 2.0)805 dq_overlap = numpy.sqrt((dq_overlap_x + dq_overlap_y) / 2.0) 816 806 if dq_overlap < 0: 817 807 dq_overlap = y_min 818 dqx_data = data2D.dqx_data[np.isfinite(data2D.data)] 819 dqy_data = data2D.dqy_data[np.isfinite( 820 data2D.data)] - dq_overlap 808 dqx_data = data2D.dqx_data[numpy.isfinite(data2D.data)] 809 dqy_data = data2D.dqy_data[numpy.isfinite(data2D.data)] - dq_overlap 821 810 # def; dqx_data = dq_r dqy_data = dq_phi 822 811 # Convert dq 2D to 1D here 823 812 dqx = dqx_data * dqx_data 824 813 dqy = dqy_data * dqy_data 825 dq_data = n p.add(dqx, dqy)826 dq_data = n p.sqrt(dq_data)827 828 # 829 x = n p.zeros(self.nbins)830 y = n p.zeros(self.nbins)831 y_err = n p.zeros(self.nbins)832 x_err = n p.zeros(self.nbins)833 y_counts = n p.zeros(self.nbins)814 dq_data = numpy.add(dqx, dqy) 815 dq_data = numpy.sqrt(dq_data) 816 817 #set space for 1d outputs 818 x = numpy.zeros(self.nbins) 819 y = numpy.zeros(self.nbins) 820 y_err = numpy.zeros(self.nbins) 821 x_err = numpy.zeros(self.nbins) 822 y_counts = numpy.zeros(self.nbins) 834 823 835 824 # Get the min and max into the region: 0 <= phi < 2Pi … … 850 839 phi_value = math.atan2(qy_data[n], qx_data[n]) + Pi 851 840 852 # No need to calculate the frac when all data are within range841 ## No need to calculate the frac when all data are within range 853 842 if self.r_min <= q_value and q_value <= self.r_max: 854 843 frac = 1 855 844 if frac == 0: 856 845 continue 857 # 846 #In case of two ROIs (symmetric major and minor regions)(for 'q2') 858 847 if run.lower() == 'q2': 859 # For minor sector wing848 ## For minor sector wing 860 849 # Calculate the minor wing phis 861 850 phi_min_minor = flip_phi(phi_min - Pi) … … 863 852 # Check if phis of the minor ring is within 0 to 2pi 864 853 if phi_min_minor > phi_max_minor: 865 is_in = (phi_value > phi_min_minor or 866 phi_value < phi_max_minor)854 is_in = (phi_value > phi_min_minor or \ 855 phi_value < phi_max_minor) 867 856 else: 868 is_in = (phi_value > phi_min_minor and 869 phi_value < phi_max_minor)870 871 # 872 # 857 is_in = (phi_value > phi_min_minor and \ 858 phi_value < phi_max_minor) 859 860 #For all cases(i.e.,for 'q', 'q2', and 'phi') 861 #Find pixels within ROI 873 862 if phi_min > phi_max: 874 is_in = is_in or (phi_value > phi_min or 875 phi_value < phi_max)863 is_in = is_in or (phi_value > phi_min or \ 864 phi_value < phi_max) 876 865 else: 877 is_in = is_in or (phi_value >= phi_min and878 phi_value < phi_max)866 is_in = is_in or (phi_value >= phi_min and \ 867 phi_value < phi_max) 879 868 880 869 if not is_in: … … 896 885 i_bin = self.nbins - 1 897 886 898 # Get the total y887 ## Get the total y 899 888 y[i_bin] += frac * data_n 900 889 x[i_bin] += frac * q_value … … 934 923 #x[i] = math.sqrt((r_inner * r_inner + r_outer * r_outer) / 2) 935 924 x[i] = x[i] / y_counts[i] 936 y_err[y_err == 0] = n p.average(y_err)937 idx = (n p.isfinite(y) & np.isfinite(y_err))925 y_err[y_err == 0] = numpy.average(y_err) 926 idx = (numpy.isfinite(y) & numpy.isfinite(y_err)) 938 927 if x_err != None: 939 928 d_x = x_err[idx] / y_counts[idx] … … 943 932 msg = "Average Error: No points inside sector of ROI to average..." 944 933 raise ValueError, msg 945 # 934 #elif len(y[idx])!= self.nbins: 946 935 # print "resulted",self.nbins- len(y[idx]), 947 936 #"empty bin(s) due to tight binning..." … … 957 946 The number of bin in phi also has to be defined. 958 947 """ 959 960 948 def __call__(self, data2D): 961 949 """ … … 977 965 The number of bin in Q also has to be defined. 978 966 """ 979 980 967 def __call__(self, data2D): 981 968 """ … … 1000 987 in anti-clockwise starting from the x- axis on the left-hand side 1001 988 """ 1002 1003 989 def __init__(self, r_min=0, r_max=0, center_x=0, center_y=0): 1004 990 # Minimum radius … … 1026 1012 qx_data = data2D.qx_data 1027 1013 qy_data = data2D.qy_data 1028 q_data = n p.sqrt(qx_data * qx_data + qy_data * qy_data)1014 q_data = numpy.sqrt(qx_data * qx_data + qy_data * qy_data) 1029 1015 1030 1016 # check whether or not the data point is inside ROI … … 1037 1023 Find a rectangular 2D region of interest. 1038 1024 """ 1039 1040 1025 def __init__(self, x_min=0.0, x_max=0.0, y_min=0.0, y_max=0.0): 1041 1026 # Minimum Qx value [A-1] … … 1092 1077 and (phi_max-phi_min) should not be larger than pi 1093 1078 """ 1094 1095 1079 def __init__(self, phi_min=0, phi_max=math.pi): 1096 1080 self.phi_min = phi_min … … 1129 1113 1130 1114 # get phi from data 1131 phi_data = n p.arctan2(qy_data, qx_data)1115 phi_data = numpy.arctan2(qy_data, qx_data) 1132 1116 1133 1117 # Get the min and max into the region: -pi <= phi < Pi … … 1136 1120 # check for major sector 1137 1121 if phi_min_major > phi_max_major: 1138 out_major = (phi_min_major <= phi_data) + \ 1139 (phi_max_major > phi_data) 1122 out_major = (phi_min_major <= phi_data) + (phi_max_major > phi_data) 1140 1123 else: 1141 out_major = (phi_min_major <= phi_data) & ( 1142 phi_max_major > phi_data) 1124 out_major = (phi_min_major <= phi_data) & (phi_max_major > phi_data) 1143 1125 1144 1126 # minor sector … … 1150 1132 if phi_min_minor > phi_max_minor: 1151 1133 out_minor = (phi_min_minor <= phi_data) + \ 1152 (phi_max_minor >= phi_data)1134 (phi_max_minor >= phi_data) 1153 1135 else: 1154 1136 out_minor = (phi_min_minor <= phi_data) & \ 1155 (phi_max_minor >= phi_data)1137 (phi_max_minor >= phi_data) 1156 1138 out = out_major + out_minor 1157 1139 -
test/sasdataloader/test/utest_averaging.py
r4fb10e5 r9a5097c 2 2 import unittest 3 3 import math 4 import os5 4 6 5 from sas.sascalc.dataloader.loader import Loader … … 98 97 99 98 def setUp(self): 100 filepath = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'MAR07232_rest.ASC') 101 self.data = Loader().load(filepath) 99 self.data = Loader().load('MAR07232_rest.ASC') 102 100 103 101 def test_ring(self): … … 112 110 113 111 o = r(self.data) 114 filepath = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'ring_testdata.txt') 115 answer = Loader().load(filepath) 112 answer = Loader().load('ring_testdata.txt') 116 113 117 114 for i in range(r.nbins_phi - 1): … … 131 128 o = r(self.data) 132 129 133 filepath = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'avg_testdata.txt') 134 answer = Loader().load(filepath) 130 answer = Loader().load('avg_testdata.txt') 135 131 for i in range(r.nbins_phi): 136 132 self.assertAlmostEqual(o.x[i], answer.x[i], 4) … … 167 163 o = r(self.data) 168 164 169 filepath = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'slabx_testdata.txt') 170 answer = Loader().load(filepath) 165 answer = Loader().load('slabx_testdata.txt') 171 166 for i in range(len(o.x)): 172 167 self.assertAlmostEqual(o.x[i], answer.x[i], 4) … … 185 180 o = r(self.data) 186 181 187 filepath = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'slaby_testdata.txt') 188 answer = Loader().load(filepath) 182 answer = Loader().load('slaby_testdata.txt') 189 183 for i in range(len(o.x)): 190 184 self.assertAlmostEqual(o.x[i], answer.x[i], 4) … … 213 207 o = r(self.data) 214 208 215 filepath = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'ring_testdata.txt') 216 answer = Loader().load(filepath) 209 answer = Loader().load('ring_testdata.txt') 217 210 for i in range(len(o.x)): 218 211 self.assertAlmostEqual(o.x[i], answer.x[i], 4) … … 232 225 o = r(self.data) 233 226 234 filepath = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'sectorphi_testdata.txt') 235 answer = Loader().load(filepath) 227 answer = Loader().load('sectorphi_testdata.txt') 236 228 for i in range(len(o.x)): 237 229 self.assertAlmostEqual(o.x[i], answer.x[i], 4) … … 251 243 o = r(self.data) 252 244 253 filepath = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'sectorq_testdata.txt') 254 answer = Loader().load(filepath) 245 answer = Loader().load('sectorq_testdata.txt') 255 246 for i in range(len(o.x)): 256 247 self.assertAlmostEqual(o.x[i], answer.x[i], 4) -
test/sasguiframe/test/utest_manipulations.py
rfd5d6eac r9a5097c 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 unittest 6 7 import math 8 import numpy as np 9 from sas.sascalc.dataloader.loader import Loader 10 from sas.sasgui.guiframe.dataFitting import Data1D, Data2D 11 from sas.sasgui.guiframe.dataFitting import Data1D as Theory1D 12 7 13 import os.path 8 import unittest9 10 import numpy as np11 12 from sas.sascalc.dataloader.loader import Loader13 from sas.sasgui.guiframe.dataFitting import Data1D as Theory1D14 from sas.sasgui.guiframe.dataFitting import Data2D15 16 14 17 15 class data_info_tests(unittest.TestCase): 18 16 19 17 def setUp(self): 20 18 data = Loader().load("cansas1d.xml") 21 19 self.data = data[0] 22 20 23 21 def test_clone1D(self): 24 22 """ … … 26 24 """ 27 25 clone = self.data.clone_without_data() 28 26 29 27 for i in range(len(self.data.detector)): 30 self.assertEqual( 31 self.data.detector[i].distance, clone.detector[i].distance) 32 33 28 self.assertEqual(self.data.detector[i].distance, clone.detector[i].distance) 29 34 30 class theory1d_tests(unittest.TestCase): 35 31 36 32 def setUp(self): 37 33 data = Loader().load("cansas1d.xml") 38 34 self.data = data[0] 39 35 40 36 def test_clone_theory1D(self): 41 37 """ … … 46 42 theory.copy_from_datainfo(data1d=self.data) 47 43 for i in range(len(self.data.detector)): 48 self.assertEqual( 49 self.data.detector[i].distance, theory.detector[i].distance) 50 44 self.assertEqual(self.data.detector[i].distance, theory.detector[i].distance) 45 51 46 for i in range(len(self.data.x)): 52 47 self.assertEqual(self.data.x[i], theory.x[i]) … … 54 49 self.assertEqual(self.data.dy[i], theory.dy[i]) 55 50 56 57 51 class manip_tests(unittest.TestCase): 58 52 59 53 def setUp(self): 60 54 # Create two data sets to play with 61 55 x_0 = np.ones(5) 62 56 for i in range(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)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) 67 61 self.data = Data1D(x_0, y_0, dy=dy_0) 68 62 69 63 x = self.data.x 70 64 y = np.ones(5) 71 65 dy = np.ones(5) 72 66 self.data2 = Data1D(x, y, dy=dy) 73 67 68 74 69 def test_load(self): 75 70 """ … … 78 73 # There should be 5 entries in the file 79 74 self.assertEqual(len(self.data.x), 5) 80 75 81 76 for i in range(5): 82 77 # The x values should be from 1 to 5 83 self.assertEqual(self.data.x[i], float(i +1))84 78 self.assertEqual(self.data.x[i], float(i+1)) 79 85 80 # All y-error values should be 0.5 86 self.assertEqual(self.data.dy[i], 0.5) 87 81 self.assertEqual(self.data.dy[i], 0.5) 82 88 83 # All y values should be 2.0 89 self.assertEqual(self.data.y[i], 2.0) 90 84 self.assertEqual(self.data.y[i], 2.0) 85 91 86 def test_add(self): 92 result = self.data2 +self.data87 result = self.data2+self.data 93 88 for i in range(5): 94 89 self.assertEqual(result.y[i], 3.0) 95 self.assertEqual(result.dy[i], math.sqrt(0.5**2 +1.0))96 90 self.assertEqual(result.dy[i], math.sqrt(0.5**2+1.0)) 91 97 92 def test_sub(self): 98 result = self.data2 -self.data93 result = self.data2-self.data 99 94 for i in range(5): 100 95 self.assertEqual(result.y[i], -1.0) 101 self.assertEqual(result.dy[i], math.sqrt(0.5**2 +1.0))102 96 self.assertEqual(result.dy[i], math.sqrt(0.5**2+1.0)) 97 103 98 def test_mul(self): 104 result = self.data2 *self.data99 result = self.data2*self.data 105 100 for i in range(5): 106 101 self.assertEqual(result.y[i], 2.0) 107 self.assertEqual(result.dy[i], math.sqrt( 108 (0.5 * 1.0)**2 + (1.0 * 2.0)**2)) 109 102 self.assertEqual(result.dy[i], math.sqrt((0.5*1.0)**2+(1.0*2.0)**2)) 103 110 104 def test_div(self): 111 result = self.data2 /self.data105 result = self.data2/self.data 112 106 for i in range(5): 113 107 self.assertEqual(result.y[i], 0.5) 114 self.assertEqual(result.dy[i], math.sqrt( 115 (1.0 / 2.0)**2 + (0.5 * 1.0 / 4.0)**2)) 116 108 self.assertEqual(result.dy[i], math.sqrt((1.0/2.0)**2+(0.5*1.0/4.0)**2)) 109 117 110 def test_radd(self): 118 result = self.data +3.0111 result = self.data+3.0 119 112 for i in range(5): 120 113 self.assertEqual(result.y[i], 5.0) 121 114 self.assertEqual(result.dy[i], 0.5) 122 123 result = 3.0 +self.data115 116 result = 3.0+self.data 124 117 for i in range(5): 125 118 self.assertEqual(result.y[i], 5.0) 126 119 self.assertEqual(result.dy[i], 0.5) 127 120 128 121 def test_rsub(self): 129 result = self.data -3.0122 result = self.data-3.0 130 123 for i in range(5): 131 124 self.assertEqual(result.y[i], -1.0) 132 125 self.assertEqual(result.dy[i], 0.5) 133 134 result = 3.0 -self.data126 127 result = 3.0-self.data 135 128 for i in range(5): 136 129 self.assertEqual(result.y[i], 1.0) 137 130 self.assertEqual(result.dy[i], 0.5) 138 131 139 132 def test_rmul(self): 140 result = self.data *3.0133 result = self.data*3.0 141 134 for i in range(5): 142 135 self.assertEqual(result.y[i], 6.0) 143 136 self.assertEqual(result.dy[i], 1.5) 144 145 result = 3.0 *self.data137 138 result = 3.0*self.data 146 139 for i in range(5): 147 140 self.assertEqual(result.y[i], 6.0) 148 141 self.assertEqual(result.dy[i], 1.5) 149 142 150 143 def test_rdiv(self): 151 result = self.data /4.0144 result = self.data/4.0 152 145 for i in range(5): 153 146 self.assertEqual(result.y[i], 0.5) 154 147 self.assertEqual(result.dy[i], 0.125) 155 156 result = 6.0 /self.data148 149 result = 6.0/self.data 157 150 for i in range(5): 158 151 self.assertEqual(result.y[i], 3.0) 159 self.assertEqual(result.dy[i], 6.0 * 0.5 / 4.0) 160 161 152 self.assertEqual(result.dy[i], 6.0*0.5/4.0) 153 162 154 class manip_2D(unittest.TestCase): 163 155 164 156 def setUp(self): 165 157 # Create two data sets to play with 166 x_0 = 2.0 *np.ones(25)167 dx_0 = 0.5 *np.ones(25)158 x_0 = 2.0*np.ones(25) 159 dx_0 = 0.5*np.ones(25) 168 160 qx_0 = np.arange(25) 169 161 qy_0 = np.arange(25) 170 162 mask_0 = np.zeros(25) 171 dqx_0 = np.arange(25) /100172 dqy_0 = np.arange(25) /100163 dqx_0 = np.arange(25)/100 164 dqy_0 = np.arange(25)/100 173 165 q_0 = np.sqrt(qx_0 * qx_0 + qy_0 * qy_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, 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, 176 168 dqx_data=dqx_0, dqy_data=dqy_0) 177 169 178 170 y = np.ones(25) 179 171 dy = np.ones(25) … … 182 174 mask = np.zeros(25) 183 175 q = np.sqrt(qx * qx + qy * qy) 184 self.data2 = Data2D(image=y, err_image=dy, qx_data=qx, qy_data=qy, 176 self.data2 = Data2D(image=y, err_image=dy, qx_data=qx, qy_data=qy, 185 177 q_data=q, mask=mask) 186 178 179 187 180 def test_load(self): 188 181 """ … … 191 184 # There should be 5 entries in the file 192 185 self.assertEqual(np.size(self.data.data), 25) 193 186 194 187 for i in range(25): 195 188 # All y-error values should be 0.5 196 self.assertEqual(self.data.err_data[i], 0.5) 197 189 self.assertEqual(self.data.err_data[i], 0.5) 190 198 191 # All y values should be 2.0 199 self.assertEqual(self.data.data[i], 2.0) 200 192 self.assertEqual(self.data.data[i], 2.0) 193 201 194 def test_add(self): 202 result = self.data2 +self.data195 result = self.data2+self.data 203 196 for i in range(25): 204 197 self.assertEqual(result.data[i], 3.0) 205 self.assertEqual(result.err_data[i], math.sqrt(0.5**2 +1.0))206 198 self.assertEqual(result.err_data[i], math.sqrt(0.5**2+1.0)) 199 207 200 def test_sub(self): 208 result = self.data2 - self.data 201 result = self.data2-self.data 202 for i in range(25): 203 self.assertEqual(result.data[i], -1.0) 204 self.assertEqual(result.err_data[i], math.sqrt(0.5**2+1.0)) 205 206 def test_mul(self): 207 result = self.data2*self.data 208 for i in range(25): 209 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 212 def test_div(self): 213 result = self.data2/self.data 214 for i in range(25): 215 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 218 def test_radd(self): 219 result = self.data+3.0 220 for i in range(25): 221 self.assertEqual(result.data[i], 5.0) 222 self.assertEqual(result.err_data[i], 0.5) 223 224 result = 3.0+self.data 225 for i in range(25): 226 self.assertEqual(result.data[i], 5.0) 227 self.assertEqual(result.err_data[i], 0.5) 228 229 def test_rsub(self): 230 result = self.data-3.0 209 231 for i in range(25): 210 232 self.assertEqual(result.data[i], -1.0) 211 self.assertEqual(result.err_data[i], math.sqrt(0.5**2 + 1.0)) 212 213 def test_mul(self): 214 result = self.data2 * self.data 215 for i in range(25): 216 self.assertEqual(result.data[i], 2.0) 217 self.assertEqual(result.err_data[i], math.sqrt( 218 (0.5 * 1.0)**2 + (1.0 * 2.0)**2)) 219 220 def test_div(self): 221 result = self.data2 / self.data 222 for i in range(25): 223 self.assertEqual(result.data[i], 0.5) 224 self.assertEqual(result.err_data[i], math.sqrt( 225 (1.0 / 2.0)**2 + (0.5 * 1.0 / 4.0)**2)) 226 227 def test_radd(self): 228 result = self.data + 3.0 229 for i in range(25): 230 self.assertEqual(result.data[i], 5.0) 231 self.assertEqual(result.err_data[i], 0.5) 232 233 result = 3.0 + self.data 234 for i in range(25): 235 self.assertEqual(result.data[i], 5.0) 236 self.assertEqual(result.err_data[i], 0.5) 237 238 def test_rsub(self): 239 result = self.data - 3.0 240 for i in range(25): 241 self.assertEqual(result.data[i], -1.0) 242 self.assertEqual(result.err_data[i], 0.5) 243 244 result = 3.0 - self.data 233 self.assertEqual(result.err_data[i], 0.5) 234 235 result = 3.0-self.data 245 236 for i in range(25): 246 237 self.assertEqual(result.data[i], 1.0) 247 238 self.assertEqual(result.err_data[i], 0.5) 248 239 249 240 def test_rmul(self): 250 result = self.data *3.0241 result = self.data*3.0 251 242 for i in range(25): 252 243 self.assertEqual(result.data[i], 6.0) 253 244 self.assertEqual(result.err_data[i], 1.5) 254 255 result = 3.0 *self.data245 246 result = 3.0*self.data 256 247 for i in range(25): 257 248 self.assertEqual(result.data[i], 6.0) 258 249 self.assertEqual(result.err_data[i], 1.5) 259 250 260 251 def test_rdiv(self): 261 result = self.data /4.0252 result = self.data/4.0 262 253 for i in range(25): 263 254 self.assertEqual(result.data[i], 0.5) 264 255 self.assertEqual(result.err_data[i], 0.125) 265 256 266 result = 6.0 /self.data257 result = 6.0/self.data 267 258 for i in range(25): 268 259 self.assertEqual(result.data[i], 3.0) 269 self.assertEqual(result.err_data[i], 6.0 * 0.5 / 4.0) 270 271 260 self.assertEqual(result.err_data[i], 6.0*0.5/4.0) 261 272 262 class extra_manip_2D(unittest.TestCase): 273 263 274 264 def setUp(self): 275 265 # Create two data sets to play with 276 x_0 = 2.0 *np.ones(25)277 dx_0 = 0.5 *np.ones(25)266 x_0 = 2.0*np.ones(25) 267 dx_0 = 0.5*np.ones(25) 278 268 qx_0 = np.arange(25) 279 269 qy_0 = np.arange(25) 280 270 mask_0 = np.zeros(25) 281 dqx_0 = np.arange(25) /100282 dqy_0 = np.arange(25) /100271 dqx_0 = np.arange(25)/100 272 dqy_0 = np.arange(25)/100 283 273 q_0 = np.sqrt(qx_0 * qx_0 + qy_0 * qy_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, 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, 286 276 dqx_data=dqx_0, dqy_data=dqy_0) 287 277 288 278 y = np.ones(25) 289 279 dy = np.ones(25) … … 292 282 mask = np.zeros(25) 293 283 q = np.sqrt(qx * qx + qy * qy) 294 self.data2 = Data2D(image=y, err_image=dy, qx_data=qx, qy_data=qy, 284 self.data2 = Data2D(image=y, err_image=dy, qx_data=qx, qy_data=qy, 295 285 q_data=q, mask=mask) 296 286 287 297 288 def test_load(self): 298 289 """ … … 301 292 # There should be 5 entries in the file 302 293 self.assertEqual(np.size(self.data.data), 25) 303 294 304 295 for i in range(25): 305 296 # All y-error values should be 0.5 306 self.assertEqual(self.data.err_data[i], 0.5) 307 297 self.assertEqual(self.data.err_data[i], 0.5) 298 308 299 # All y values should be 2.0 309 self.assertEqual(self.data.data[i], 2.0) 310 300 self.assertEqual(self.data.data[i], 2.0) 301 311 302 def test_add(self): 312 result = self.data2 +self.data303 result = self.data2+self.data 313 304 for i in range(25): 314 305 self.assertEqual(result.data[i], 3.0) 315 self.assertEqual(result.err_data[i], math.sqrt(0.5**2 +1.0))316 306 self.assertEqual(result.err_data[i], math.sqrt(0.5**2+1.0)) 307 317 308 def test_sub(self): 318 result = self.data2 - self.data 309 result = self.data2-self.data 310 for i in range(25): 311 self.assertEqual(result.data[i], -1.0) 312 self.assertEqual(result.err_data[i], math.sqrt(0.5**2+1.0)) 313 314 def test_mul(self): 315 result = self.data2*self.data 316 for i in range(25): 317 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 320 def test_div(self): 321 result = self.data2/self.data 322 for i in range(25): 323 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 326 def test_radd(self): 327 result = self.data+3.0 328 for i in range(25): 329 self.assertEqual(result.data[i], 5.0) 330 self.assertEqual(result.err_data[i], 0.5) 331 332 result = 3.0+self.data 333 for i in range(25): 334 self.assertEqual(result.data[i], 5.0) 335 self.assertEqual(result.err_data[i], 0.5) 336 337 def test_rsub(self): 338 result = self.data-3.0 319 339 for i in range(25): 320 340 self.assertEqual(result.data[i], -1.0) 321 self.assertEqual(result.err_data[i], math.sqrt(0.5**2 + 1.0)) 322 323 def test_mul(self): 324 result = self.data2 * self.data 325 for i in range(25): 326 self.assertEqual(result.data[i], 2.0) 327 self.assertEqual(result.err_data[i], math.sqrt( 328 (0.5 * 1.0)**2 + (1.0 * 2.0)**2)) 329 330 def test_div(self): 331 result = self.data2 / self.data 332 for i in range(25): 333 self.assertEqual(result.data[i], 0.5) 334 self.assertEqual(result.err_data[i], math.sqrt( 335 (1.0 / 2.0)**2 + (0.5 * 1.0 / 4.0)**2)) 336 337 def test_radd(self): 338 result = self.data + 3.0 339 for i in range(25): 340 self.assertEqual(result.data[i], 5.0) 341 self.assertEqual(result.err_data[i], 0.5) 342 343 result = 3.0 + self.data 344 for i in range(25): 345 self.assertEqual(result.data[i], 5.0) 346 self.assertEqual(result.err_data[i], 0.5) 347 348 def test_rsub(self): 349 result = self.data - 3.0 350 for i in range(25): 351 self.assertEqual(result.data[i], -1.0) 352 self.assertEqual(result.err_data[i], 0.5) 353 354 result = 3.0 - self.data 341 self.assertEqual(result.err_data[i], 0.5) 342 343 result = 3.0-self.data 355 344 for i in range(25): 356 345 self.assertEqual(result.data[i], 1.0) 357 346 self.assertEqual(result.err_data[i], 0.5) 358 347 359 348 def test_rmul(self): 360 result = self.data *3.0349 result = self.data*3.0 361 350 for i in range(25): 362 351 self.assertEqual(result.data[i], 6.0) 363 352 self.assertEqual(result.err_data[i], 1.5) 364 365 result = 3.0 *self.data353 354 result = 3.0*self.data 366 355 for i in range(25): 367 356 self.assertEqual(result.data[i], 6.0) 368 357 self.assertEqual(result.err_data[i], 1.5) 369 358 370 359 def test_rdiv(self): 371 result = self.data /4.0360 result = self.data/4.0 372 361 for i in range(25): 373 362 self.assertEqual(result.data[i], 0.5) 374 363 self.assertEqual(result.err_data[i], 0.125) 375 364 376 result = 6.0 /self.data365 result = 6.0/self.data 377 366 for i in range(25): 378 367 self.assertEqual(result.data[i], 3.0) 379 self.assertEqual(result.err_data[i], 6.0 * 0.5 / 4.0) 380 368 self.assertEqual(result.err_data[i], 6.0*0.5/4.0) 381 369 382 370 if __name__ == '__main__': 383 371 unittest.main() 372
Note: See TracChangeset
for help on using the changeset viewer.