Changeset 442f42f in sasview
- Timestamp:
- Aug 29, 2008 5:20:02 PM (16 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, release-4.1.1, release-4.1.2, release-4.2.2, release_4.0.1, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
- Children:
- b88c6e0
- Parents:
- 579ba85
- Location:
- DataLoader
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
DataLoader/data_info.py
r579ba85 r442f42f 81 81 82 82 def __init__(self, data=None, err_data=None): 83 self.data = data84 self.err_data = err_data83 self.data = numpy.asarray(data) 84 self.err_data = numpy.asarray(err_data) 85 85 86 86 def xaxis(self, label, unit): … … 687 687 return _str 688 688 689 def clone_without_data(self, length=0): 690 """ 691 Clone the current object, without copying the data (which 692 will be filled out by a subsequent operation). 693 The data arrays will be initialized to zero. 694 695 @param length: length of the data array to be initialized 696 """ 697 from copy import deepcopy 698 699 data = numpy.zeros(length) 700 err_data = numpy.zeros(length) 701 702 clone = Data2D(data, err_data) 703 clone.title = self.title 704 clone.run = self.run 705 clone.filename = self.filename 706 clone.notes = deepcopy(self.notes) 707 clone.process = deepcopy(self.process) 708 clone.detector = deepcopy(self.detector) 709 clone.sample = deepcopy(self.sample) 710 clone.source = deepcopy(self.source) 711 clone.collimation = deepcopy(self.collimation) 712 clone.meta_data = deepcopy(self.meta_data) 713 clone.errors = deepcopy(self.errors) 714 715 return clone 716 717 718 def _validity_check(self, other): 719 """ 720 Checks that the data lengths are compatible. 721 Checks that the x vectors are compatible. 722 Returns errors vectors equal to original 723 errors vectors if they were present or vectors 724 of zeros when none was found. 725 726 @param other: other data set for operation 727 @return: dy for self, dy for other [numpy arrays] 728 @raise ValueError: when lengths are not compatible 729 """ 730 err_other = None 731 if isinstance(other, Data2D): 732 # Check that data lengths are the same 733 if numpy.size(self.data) != numpy.size(other.data): 734 raise ValueError, "Unable to perform operation: data length are not equal" 735 736 # Check that the scales match 737 #TODO: matching scales? 738 739 # Check that the other data set has errors, otherwise 740 # create zero vector 741 #TODO: test this 742 err_other = other.err_data 743 if other.err_data==None or (numpy.size(other.err_data) != numpy.size(other.data)): 744 err_other = numpy.zeros([numpy.size(other.data,0), numpy.size(other.data,1)]) 745 746 # Check that we have errors, otherwise create zero vector 747 err = self.err_data 748 if self.err_data==None or (numpy.size(self.err_data) != numpy.size(self.data)): 749 err = numpy.zeros([numpy.size(self.data,0), numpy.size(self.data,1)]) 750 751 return err, err_other 752 753 754 def _perform_operation(self, other, operation): 755 """ 756 Perform 2D operations between data sets 757 758 @param other: other data set 759 @param operation: function defining the operation 760 """ 761 # First, check the data compatibility 762 dy, dy_other = self._validity_check(other) 763 764 result = self.clone_without_data([numpy.size(self.data,0), numpy.size(self.data,1)]) 765 766 for i in range(numpy.size(self.data,0)): 767 for j in range(numpy.size(self.data,1)): 768 result.data[i][j] = self.data[i][j] 769 if self.err_data is not None and numpy.size(self.data)==numpy.size(self.err_data): 770 result.err_data[i][j] = self.err_data[i][j] 771 772 a = Uncertainty(self.data[i][j], dy[i][j]**2) 773 if isinstance(other, Data2D): 774 b = Uncertainty(other.data[i][j], dy_other[i][j]**2) 775 else: 776 b = other 777 778 output = operation(a, b) 779 result.data[i][j] = output.x 780 result.err_data[i][j] = math.sqrt(math.fabs(output.variance)) 781 return result 782 -
DataLoader/test/utest_manipulations.py
r9198b83 r442f42f 2 2 Unit tests for data manipulations 3 3 """ 4 #TODO: what happens if you add a Data1D to a Data2D? 4 5 5 6 import unittest 6 7 import numpy, math 7 8 from DataLoader.loader import Loader 8 from DataLoader.data_info import Data1D 9 from DataLoader.data_info import Data1D, Data2D 9 10 10 11 import os.path … … 128 129 self.assertEqual(result.dy[i], 6.0*0.5/4.0) 129 130 131 class manip_2D(unittest.TestCase): 132 133 def setUp(self): 134 # Create two data sets to play with 135 x_0 = 2.0*numpy.ones([5,4]) 136 dx_0 = 0.5*numpy.ones([5,4]) 137 self.data = Data2D(x_0, dx_0) 138 139 y = numpy.ones([5,4]) 140 dy = numpy.ones([5,4]) 141 self.data2 = Data2D(y, dy) 142 143 144 def test_load(self): 145 """ 146 Test whether the test file was loaded properly 147 """ 148 # There should be 5 entries in the file 149 self.assertEqual(numpy.size(self.data.data, 0), 5) 150 self.assertEqual(numpy.size(self.data.data, 1), 4) 151 152 for i in range(5): 153 for j in range(4): 154 # All y-error values should be 0.5 155 self.assertEqual(self.data.err_data[i][j], 0.5) 156 157 # All y values should be 2.0 158 self.assertEqual(self.data.data[i][j], 2.0) 159 160 def test_add(self): 161 result = self.data2+self.data 162 for i in range(5): 163 for j in range(4): 164 self.assertEqual(result.data[i][j], 3.0) 165 self.assertEqual(result.err_data[i][j], math.sqrt(0.5**2+1.0)) 166 167 def test_sub(self): 168 result = self.data2-self.data 169 for i in range(5): 170 for j in range(4): 171 self.assertEqual(result.data[i][j], -1.0) 172 self.assertEqual(result.err_data[i][j], math.sqrt(0.5**2+1.0)) 173 174 def test_mul(self): 175 result = self.data2*self.data 176 for i in range(5): 177 for j in range(4): 178 self.assertEqual(result.data[i][j], 2.0) 179 self.assertEqual(result.err_data[i][j], math.sqrt((0.5*1.0)**2+(1.0*2.0)**2)) 180 181 def test_div(self): 182 result = self.data2/self.data 183 for i in range(5): 184 for j in range(4): 185 self.assertEqual(result.data[i][j], 0.5) 186 self.assertEqual(result.err_data[i][j], math.sqrt((1.0/2.0)**2+(0.5*1.0/4.0)**2)) 187 188 def test_radd(self): 189 result = self.data+3.0 190 for i in range(5): 191 for j in range(4): 192 self.assertEqual(result.data[i][j], 5.0) 193 self.assertEqual(result.err_data[i][j], 0.5) 194 195 result = 3.0+self.data 196 for i in range(5): 197 for j in range(4): 198 self.assertEqual(result.data[i][j], 5.0) 199 self.assertEqual(result.err_data[i][j], 0.5) 200 201 def test_rsub(self): 202 result = self.data-3.0 203 for i in range(5): 204 for j in range(4): 205 self.assertEqual(result.data[i][j], -1.0) 206 self.assertEqual(result.err_data[i][j], 0.5) 207 208 result = 3.0-self.data 209 for i in range(5): 210 for j in range(4): 211 self.assertEqual(result.data[i][j], 1.0) 212 self.assertEqual(result.err_data[i][j], 0.5) 213 214 def test_rmul(self): 215 result = self.data*3.0 216 for i in range(5): 217 for j in range(4): 218 self.assertEqual(result.data[i][j], 6.0) 219 self.assertEqual(result.err_data[i][j], 1.5) 220 221 result = 3.0*self.data 222 for i in range(5): 223 for j in range(4): 224 self.assertEqual(result.data[i][j], 6.0) 225 self.assertEqual(result.err_data[i][j], 1.5) 226 227 def test_rdiv(self): 228 result = self.data/4.0 229 for i in range(5): 230 for j in range(4): 231 self.assertEqual(result.data[i][j], 0.5) 232 self.assertEqual(result.err_data[i][j], 0.125) 233 234 result = 6.0/self.data 235 for i in range(5): 236 for j in range(4): 237 self.assertEqual(result.data[i][j], 3.0) 238 self.assertEqual(result.err_data[i][j], 6.0*0.5/4.0) 239 130 240 131 241 if __name__ == '__main__':
Note: See TracChangeset
for help on using the changeset viewer.