Changes in / [ec8886e:a470e88] in sasview
- Files:
-
- 19 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/sascalc/dataloader/manipulations.py
rdd11014 rfd5d6eac 5 5 """ 6 6 ##################################################################### 7 # This software was developed by the University of Tennessee as part of the8 # Distributed Data Analysis of Neutron Scattering Experiments (DANSE)9 # project funded by the US National Science Foundation.10 # See the license text in license.txt11 # copyright 2008, University of Tennessee7 # 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 #TODO: copy the meta data from the 2D object to the resulting 1D object 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 15 17 import math 16 import numpy 18 import numpy as np 17 19 18 20 #from data_info import plottable_2D … … 82 84 if data2d.data is None or data2d.x_bins is None or data2d.y_bins is None: 83 85 raise ValueError, "Can't convert this data: data=None..." 84 new_x = n umpy.tile(data2d.x_bins, (len(data2d.y_bins), 1))85 new_y = n umpy.tile(data2d.y_bins, (len(data2d.x_bins), 1))86 new_x = np.tile(data2d.x_bins, (len(data2d.y_bins), 1)) 87 new_y = np.tile(data2d.y_bins, (len(data2d.x_bins), 1)) 86 88 new_y = new_y.swapaxes(0, 1) 87 89 … … 89 91 qx_data = new_x.flatten() 90 92 qy_data = new_y.flatten() 91 q_data = n umpy.sqrt(qx_data * qx_data + qy_data * qy_data)92 if data2d.err_data is None or n umpy.any(data2d.err_data <= 0):93 new_err_data = n umpy.sqrt(numpy.abs(new_data))93 q_data = np.sqrt(qx_data * qx_data + qy_data * qy_data) 94 if data2d.err_data is None or np.any(data2d.err_data <= 0): 95 new_err_data = np.sqrt(np.abs(new_data)) 94 96 else: 95 97 new_err_data = data2d.err_data.flatten() 96 mask = n umpy.ones(len(new_data), dtype=bool)97 98 # TODO: make sense of the following two lines...98 mask = np.ones(len(new_data), dtype=bool) 99 100 # TODO: make sense of the following two lines... 99 101 #from sas.sascalc.dataloader.data_info import Data2D 100 102 #output = Data2D() … … 114 116 Compute average I(Q) for a region of interest 115 117 """ 118 116 119 def __init__(self, x_min=0.0, x_max=0.0, y_min=0.0, 117 120 y_max=0.0, bin_width=0.001): … … 149 152 150 153 # Get data 151 data = data2D.data[n umpy.isfinite(data2D.data)]152 err_data = data2D.err_data[n umpy.isfinite(data2D.data)]153 qx_data = data2D.qx_data[n umpy.isfinite(data2D.data)]154 qy_data = data2D.qy_data[n umpy.isfinite(data2D.data)]154 data = data2D.data[np.isfinite(data2D.data)] 155 err_data = data2D.err_data[np.isfinite(data2D.data)] 156 qx_data = data2D.qx_data[np.isfinite(data2D.data)] 157 qy_data = data2D.qy_data[np.isfinite(data2D.data)] 155 158 156 159 # Build array of Q intervals … … 170 173 raise RuntimeError, "_Slab._avg: unrecognized axis %s" % str(maj) 171 174 172 x = n umpy.zeros(nbins)173 y = n umpy.zeros(nbins)174 err_y = n umpy.zeros(nbins)175 y_counts = n umpy.zeros(nbins)175 x = np.zeros(nbins) 176 y = np.zeros(nbins) 177 err_y = np.zeros(nbins) 178 y_counts = np.zeros(nbins) 176 179 177 180 # Average pixelsize in q space … … 205 208 continue 206 209 207 # TODO: find better definition of x[i_q] based on q_data210 # TODO: find better definition of x[i_q] based on q_data 208 211 # min_value + (i_q + 1) * self.bin_width / 2.0 209 212 x[i_q] += frac * q_value 210 213 y[i_q] += frac * data[npts] 211 214 212 if err_data ==None or err_data[npts] == 0.0:215 if err_data is None or err_data[npts] == 0.0: 213 216 if data[npts] < 0: 214 217 data[npts] = -data[npts] … … 225 228 y = y / y_counts 226 229 x = x / y_counts 227 idx = (n umpy.isfinite(y) & numpy.isfinite(x))230 idx = (np.isfinite(y) & np.isfinite(x)) 228 231 229 232 if not idx.any(): … … 237 240 Compute average I(Qy) for a region of interest 238 241 """ 242 239 243 def __call__(self, data2D): 240 244 """ … … 251 255 Compute average I(Qx) for a region of interest 252 256 """ 257 253 258 def __call__(self, data2D): 254 259 """ … … 264 269 Perform the sum of counts in a 2D region of interest. 265 270 """ 271 266 272 def __init__(self, x_min=0.0, x_max=0.0, y_min=0.0, y_max=0.0): 267 273 # Minimum Qx value [A-1] … … 304 310 raise RuntimeError, msg 305 311 # Get data 306 data = data2D.data[n umpy.isfinite(data2D.data)]307 err_data = data2D.err_data[n umpy.isfinite(data2D.data)]308 qx_data = data2D.qx_data[n umpy.isfinite(data2D.data)]309 qy_data = data2D.qy_data[n umpy.isfinite(data2D.data)]312 data = data2D.data[np.isfinite(data2D.data)] 313 err_data = data2D.err_data[np.isfinite(data2D.data)] 314 qx_data = data2D.qx_data[np.isfinite(data2D.data)] 315 qy_data = data2D.qy_data[np.isfinite(data2D.data)] 310 316 311 317 y = 0.0 … … 328 334 if self.y_min <= qy and self.y_max > qy: 329 335 frac_y = 1 330 # Find the fraction along each directions336 # Find the fraction along each directions 331 337 frac = frac_x * frac_y 332 338 if frac == 0: 333 339 continue 334 340 y += frac * data[npts] 335 if err_data ==None or err_data[npts] == 0.0:341 if err_data is None or err_data[npts] == 0.0: 336 342 if data[npts] < 0: 337 343 data[npts] = -data[npts] … … 347 353 Perform the average of counts in a 2D region of interest. 348 354 """ 355 349 356 def __init__(self, x_min=0.0, x_max=0.0, y_min=0.0, y_max=0.0): 350 357 super(Boxavg, self).__init__(x_min=x_min, x_max=x_max, … … 398 405 as a function of Q 399 406 """ 407 400 408 def __init__(self, r_min=0.0, r_max=0.0, bin_width=0.0005): 401 409 # Minimum radius included in the average [A-1] … … 414 422 """ 415 423 # Get data W/ finite values 416 data = data2D.data[n umpy.isfinite(data2D.data)]417 q_data = data2D.q_data[n umpy.isfinite(data2D.data)]418 err_data = data2D.err_data[n umpy.isfinite(data2D.data)]419 mask_data = data2D.mask[n umpy.isfinite(data2D.data)]424 data = data2D.data[np.isfinite(data2D.data)] 425 q_data = data2D.q_data[np.isfinite(data2D.data)] 426 err_data = data2D.err_data[np.isfinite(data2D.data)] 427 mask_data = data2D.mask[np.isfinite(data2D.data)] 420 428 421 429 dq_data = None … … 448 456 dq_overlap_y *= dq_overlap_y 449 457 450 dq_overlap = n umpy.sqrt((dq_overlap_x + dq_overlap_y) / 2.0)458 dq_overlap = np.sqrt((dq_overlap_x + dq_overlap_y) / 2.0) 451 459 # Final protection of dq 452 460 if dq_overlap < 0: 453 461 dq_overlap = y_min 454 dqx_data = data2D.dqx_data[numpy.isfinite(data2D.data)] 455 dqy_data = data2D.dqy_data[numpy.isfinite(data2D.data)] - dq_overlap 462 dqx_data = data2D.dqx_data[np.isfinite(data2D.data)] 463 dqy_data = data2D.dqy_data[np.isfinite( 464 data2D.data)] - dq_overlap 456 465 # def; dqx_data = dq_r dqy_data = dq_phi 457 466 # Convert dq 2D to 1D here 458 467 dqx = dqx_data * dqx_data 459 468 dqy = dqy_data * dqy_data 460 dq_data = n umpy.add(dqx, dqy)461 dq_data = n umpy.sqrt(dq_data)462 463 #q_data_max = n umpy.max(q_data)469 dq_data = np.add(dqx, dqy) 470 dq_data = np.sqrt(dq_data) 471 472 #q_data_max = np.max(q_data) 464 473 if len(data2D.q_data) == None: 465 474 msg = "Circular averaging: invalid q_data: %g" % data2D.q_data … … 469 478 nbins = int(math.ceil((self.r_max - self.r_min) / self.bin_width)) 470 479 471 x = n umpy.zeros(nbins)472 y = n umpy.zeros(nbins)473 err_y = n umpy.zeros(nbins)474 err_x = n umpy.zeros(nbins)475 y_counts = n umpy.zeros(nbins)480 x = np.zeros(nbins) 481 y = np.zeros(nbins) 482 err_y = np.zeros(nbins) 483 err_x = np.zeros(nbins) 484 y_counts = np.zeros(nbins) 476 485 477 486 for npt in range(len(data)): … … 486 495 data_n = data[npt] 487 496 488 # #No need to calculate the frac when all data are within range497 # No need to calculate the frac when all data are within range 489 498 if self.r_min >= self.r_max: 490 499 raise ValueError, "Limit Error: min > max" … … 502 511 # Take dqs from data to get the q_average 503 512 x[i_q] += frac * q_value 504 if err_data ==None or err_data[npt] == 0.0:513 if err_data is None or err_data[npt] == 0.0: 505 514 if data_n < 0: 506 515 data_n = -data_n … … 523 532 err_y[n] = -err_y[n] 524 533 err_y[n] = math.sqrt(err_y[n]) 525 # if err_x != None:534 # if err_x != None: 526 535 # err_x[n] = math.sqrt(err_x[n]) 527 536 528 537 err_y = err_y / y_counts 529 err_y[err_y == 0] = n umpy.average(err_y)538 err_y[err_y == 0] = np.average(err_y) 530 539 y = y / y_counts 531 540 x = x / y_counts 532 idx = (n umpy.isfinite(y)) & (numpy.isfinite(x))541 idx = (np.isfinite(y)) & (np.isfinite(x)) 533 542 534 543 if err_x != None: … … 556 565 in anti-clockwise starting from the x- axis on the left-hand side 557 566 """ 558 #Todo: remove center. 567 # Todo: remove center. 568 559 569 def __init__(self, r_min=0, r_max=0, center_x=0, center_y=0, nbins=36): 560 570 # Minimum radius … … 569 579 self.nbins_phi = nbins 570 580 571 572 581 def __call__(self, data2D): 573 582 """ … … 585 594 586 595 # Get data 587 data = data2D.data[n umpy.isfinite(data2D.data)]588 q_data = data2D.q_data[n umpy.isfinite(data2D.data)]589 err_data = data2D.err_data[n umpy.isfinite(data2D.data)]590 qx_data = data2D.qx_data[n umpy.isfinite(data2D.data)]591 qy_data = data2D.qy_data[n umpy.isfinite(data2D.data)]596 data = data2D.data[np.isfinite(data2D.data)] 597 q_data = data2D.q_data[np.isfinite(data2D.data)] 598 err_data = data2D.err_data[np.isfinite(data2D.data)] 599 qx_data = data2D.qx_data[np.isfinite(data2D.data)] 600 qy_data = data2D.qy_data[np.isfinite(data2D.data)] 592 601 593 602 # Set space for 1d outputs 594 phi_bins = n umpy.zeros(self.nbins_phi)595 phi_counts = n umpy.zeros(self.nbins_phi)596 phi_values = n umpy.zeros(self.nbins_phi)597 phi_err = n umpy.zeros(self.nbins_phi)603 phi_bins = np.zeros(self.nbins_phi) 604 phi_counts = np.zeros(self.nbins_phi) 605 phi_values = np.zeros(self.nbins_phi) 606 phi_err = np.zeros(self.nbins_phi) 598 607 599 608 # Shift to apply to calculated phi values in order … … 615 624 continue 616 625 # binning 617 i_phi = int(math.floor((self.nbins_phi) * \626 i_phi = int(math.floor((self.nbins_phi) * 618 627 (phi_value + phi_shift) / (2 * Pi))) 619 628 … … 623 632 phi_bins[i_phi] += frac * data[npt] 624 633 625 if err_data ==None or err_data[npt] == 0.0:634 if err_data is None or err_data[npt] == 0.0: 626 635 if data_n < 0: 627 636 data_n = -data_n … … 636 645 phi_values[i] = 2.0 * math.pi / self.nbins_phi * (1.0 * i) 637 646 638 idx = (n umpy.isfinite(phi_bins))647 idx = (np.isfinite(phi_bins)) 639 648 640 649 if not idx.any(): 641 650 msg = "Average Error: No points inside ROI to average..." 642 651 raise ValueError, msg 643 # elif len(phi_bins[idx])!= self.nbins_phi:652 # elif len(phi_bins[idx])!= self.nbins_phi: 644 653 # print "resulted",self.nbins_phi- len(phi_bins[idx]) 645 654 #,"empty bin(s) due to tight binning..." … … 748 757 starting from the x- axis on the left-hand side 749 758 """ 759 750 760 def __init__(self, r_min, r_max, phi_min=0, phi_max=2 * math.pi, nbins=20): 751 761 self.r_min = r_min … … 769 779 770 780 # Get the all data & info 771 data = data2D.data[n umpy.isfinite(data2D.data)]772 q_data = data2D.q_data[n umpy.isfinite(data2D.data)]773 err_data = data2D.err_data[n umpy.isfinite(data2D.data)]774 qx_data = data2D.qx_data[n umpy.isfinite(data2D.data)]775 qy_data = data2D.qy_data[n umpy.isfinite(data2D.data)]781 data = data2D.data[np.isfinite(data2D.data)] 782 q_data = data2D.q_data[np.isfinite(data2D.data)] 783 err_data = data2D.err_data[np.isfinite(data2D.data)] 784 qx_data = data2D.qx_data[np.isfinite(data2D.data)] 785 qy_data = data2D.qy_data[np.isfinite(data2D.data)] 776 786 dq_data = None 777 787 … … 803 813 dq_overlap_y *= dq_overlap_y 804 814 805 dq_overlap = n umpy.sqrt((dq_overlap_x + dq_overlap_y) / 2.0)815 dq_overlap = np.sqrt((dq_overlap_x + dq_overlap_y) / 2.0) 806 816 if dq_overlap < 0: 807 817 dq_overlap = y_min 808 dqx_data = data2D.dqx_data[numpy.isfinite(data2D.data)] 809 dqy_data = data2D.dqy_data[numpy.isfinite(data2D.data)] - dq_overlap 818 dqx_data = data2D.dqx_data[np.isfinite(data2D.data)] 819 dqy_data = data2D.dqy_data[np.isfinite( 820 data2D.data)] - dq_overlap 810 821 # def; dqx_data = dq_r dqy_data = dq_phi 811 822 # Convert dq 2D to 1D here 812 823 dqx = dqx_data * dqx_data 813 824 dqy = dqy_data * dqy_data 814 dq_data = n umpy.add(dqx, dqy)815 dq_data = n umpy.sqrt(dq_data)816 817 # set space for 1d outputs818 x = n umpy.zeros(self.nbins)819 y = n umpy.zeros(self.nbins)820 y_err = n umpy.zeros(self.nbins)821 x_err = n umpy.zeros(self.nbins)822 y_counts = n umpy.zeros(self.nbins)825 dq_data = np.add(dqx, dqy) 826 dq_data = np.sqrt(dq_data) 827 828 # set space for 1d outputs 829 x = np.zeros(self.nbins) 830 y = np.zeros(self.nbins) 831 y_err = np.zeros(self.nbins) 832 x_err = np.zeros(self.nbins) 833 y_counts = np.zeros(self.nbins) 823 834 824 835 # Get the min and max into the region: 0 <= phi < 2Pi … … 839 850 phi_value = math.atan2(qy_data[n], qx_data[n]) + Pi 840 851 841 # #No need to calculate the frac when all data are within range852 # No need to calculate the frac when all data are within range 842 853 if self.r_min <= q_value and q_value <= self.r_max: 843 854 frac = 1 844 855 if frac == 0: 845 856 continue 846 # In case of two ROIs (symmetric major and minor regions)(for 'q2')857 # In case of two ROIs (symmetric major and minor regions)(for 'q2') 847 858 if run.lower() == 'q2': 848 # #For minor sector wing859 # For minor sector wing 849 860 # Calculate the minor wing phis 850 861 phi_min_minor = flip_phi(phi_min - Pi) … … 852 863 # Check if phis of the minor ring is within 0 to 2pi 853 864 if phi_min_minor > phi_max_minor: 854 is_in = (phi_value > phi_min_minor or \855 865 is_in = (phi_value > phi_min_minor or 866 phi_value < phi_max_minor) 856 867 else: 857 is_in = (phi_value > phi_min_minor and \858 859 860 # For all cases(i.e.,for 'q', 'q2', and 'phi')861 # Find pixels within ROI868 is_in = (phi_value > phi_min_minor and 869 phi_value < phi_max_minor) 870 871 # For all cases(i.e.,for 'q', 'q2', and 'phi') 872 # Find pixels within ROI 862 873 if phi_min > phi_max: 863 is_in = is_in or (phi_value > phi_min or \864 874 is_in = is_in or (phi_value > phi_min or 875 phi_value < phi_max) 865 876 else: 866 is_in = is_in or (phi_value >= phi_min and \867 877 is_in = is_in or (phi_value >= phi_min and 878 phi_value < phi_max) 868 879 869 880 if not is_in: … … 885 896 i_bin = self.nbins - 1 886 897 887 # #Get the total y898 # Get the total y 888 899 y[i_bin] += frac * data_n 889 900 x[i_bin] += frac * q_value … … 923 934 #x[i] = math.sqrt((r_inner * r_inner + r_outer * r_outer) / 2) 924 935 x[i] = x[i] / y_counts[i] 925 y_err[y_err == 0] = n umpy.average(y_err)926 idx = (n umpy.isfinite(y) & numpy.isfinite(y_err))936 y_err[y_err == 0] = np.average(y_err) 937 idx = (np.isfinite(y) & np.isfinite(y_err)) 927 938 if x_err != None: 928 939 d_x = x_err[idx] / y_counts[idx] … … 932 943 msg = "Average Error: No points inside sector of ROI to average..." 933 944 raise ValueError, msg 934 # elif len(y[idx])!= self.nbins:945 # elif len(y[idx])!= self.nbins: 935 946 # print "resulted",self.nbins- len(y[idx]), 936 947 #"empty bin(s) due to tight binning..." … … 946 957 The number of bin in phi also has to be defined. 947 958 """ 959 948 960 def __call__(self, data2D): 949 961 """ … … 965 977 The number of bin in Q also has to be defined. 966 978 """ 979 967 980 def __call__(self, data2D): 968 981 """ … … 987 1000 in anti-clockwise starting from the x- axis on the left-hand side 988 1001 """ 1002 989 1003 def __init__(self, r_min=0, r_max=0, center_x=0, center_y=0): 990 1004 # Minimum radius … … 1012 1026 qx_data = data2D.qx_data 1013 1027 qy_data = data2D.qy_data 1014 q_data = n umpy.sqrt(qx_data * qx_data + qy_data * qy_data)1028 q_data = np.sqrt(qx_data * qx_data + qy_data * qy_data) 1015 1029 1016 1030 # check whether or not the data point is inside ROI … … 1023 1037 Find a rectangular 2D region of interest. 1024 1038 """ 1039 1025 1040 def __init__(self, x_min=0.0, x_max=0.0, y_min=0.0, y_max=0.0): 1026 1041 # Minimum Qx value [A-1] … … 1077 1092 and (phi_max-phi_min) should not be larger than pi 1078 1093 """ 1094 1079 1095 def __init__(self, phi_min=0, phi_max=math.pi): 1080 1096 self.phi_min = phi_min … … 1113 1129 1114 1130 # get phi from data 1115 phi_data = n umpy.arctan2(qy_data, qx_data)1131 phi_data = np.arctan2(qy_data, qx_data) 1116 1132 1117 1133 # Get the min and max into the region: -pi <= phi < Pi … … 1120 1136 # check for major sector 1121 1137 if phi_min_major > phi_max_major: 1122 out_major = (phi_min_major <= phi_data) + (phi_max_major > phi_data) 1138 out_major = (phi_min_major <= phi_data) + \ 1139 (phi_max_major > phi_data) 1123 1140 else: 1124 out_major = (phi_min_major <= phi_data) & (phi_max_major > phi_data) 1141 out_major = (phi_min_major <= phi_data) & ( 1142 phi_max_major > phi_data) 1125 1143 1126 1144 # minor sector … … 1132 1150 if phi_min_minor > phi_max_minor: 1133 1151 out_minor = (phi_min_minor <= phi_data) + \ 1134 1152 (phi_max_minor >= phi_data) 1135 1153 else: 1136 1154 out_minor = (phi_min_minor <= phi_data) & \ 1137 1155 (phi_max_minor >= phi_data) 1138 1156 out = out_major + out_minor 1139 1157 -
test/sasdataloader/test/utest_averaging.py
r9a5097c r4fb10e5 2 2 import unittest 3 3 import math 4 import os 4 5 5 6 from sas.sascalc.dataloader.loader import Loader … … 97 98 98 99 def setUp(self): 99 self.data = Loader().load('MAR07232_rest.ASC') 100 filepath = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'MAR07232_rest.ASC') 101 self.data = Loader().load(filepath) 100 102 101 103 def test_ring(self): … … 110 112 111 113 o = r(self.data) 112 answer = Loader().load('ring_testdata.txt') 114 filepath = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'ring_testdata.txt') 115 answer = Loader().load(filepath) 113 116 114 117 for i in range(r.nbins_phi - 1): … … 128 131 o = r(self.data) 129 132 130 answer = Loader().load('avg_testdata.txt') 133 filepath = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'avg_testdata.txt') 134 answer = Loader().load(filepath) 131 135 for i in range(r.nbins_phi): 132 136 self.assertAlmostEqual(o.x[i], answer.x[i], 4) … … 163 167 o = r(self.data) 164 168 165 answer = Loader().load('slabx_testdata.txt') 169 filepath = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'slabx_testdata.txt') 170 answer = Loader().load(filepath) 166 171 for i in range(len(o.x)): 167 172 self.assertAlmostEqual(o.x[i], answer.x[i], 4) … … 180 185 o = r(self.data) 181 186 182 answer = Loader().load('slaby_testdata.txt') 187 filepath = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'slaby_testdata.txt') 188 answer = Loader().load(filepath) 183 189 for i in range(len(o.x)): 184 190 self.assertAlmostEqual(o.x[i], answer.x[i], 4) … … 207 213 o = r(self.data) 208 214 209 answer = Loader().load('ring_testdata.txt') 215 filepath = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'ring_testdata.txt') 216 answer = Loader().load(filepath) 210 217 for i in range(len(o.x)): 211 218 self.assertAlmostEqual(o.x[i], answer.x[i], 4) … … 225 232 o = r(self.data) 226 233 227 answer = Loader().load('sectorphi_testdata.txt') 234 filepath = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'sectorphi_testdata.txt') 235 answer = Loader().load(filepath) 228 236 for i in range(len(o.x)): 229 237 self.assertAlmostEqual(o.x[i], answer.x[i], 4) … … 243 251 o = r(self.data) 244 252 245 answer = Loader().load('sectorq_testdata.txt') 253 filepath = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'sectorq_testdata.txt') 254 answer = Loader().load(filepath) 246 255 for i in range(len(o.x)): 247 256 self.assertAlmostEqual(o.x[i], answer.x[i], 4) -
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 y-error 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 y-error 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 y-error 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.