Changeset a48842a2 in sasview for sansdataloader/src


Ignore:
Timestamp:
Jun 13, 2012 4:40:41 PM (12 years ago)
Author:
Jae Cho <jhjcho@…>
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:
1a98ded
Parents:
9a14138
Message:

added data operation tool

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sansdataloader/src/sans/dataloader/data_info.py

    rbd365666 ra48842a2  
    625625        return self._perform_operation(other, operation) 
    626626             
    627              
     627         
     628    def __or__(self, other): 
     629        """ 
     630        Union a data set with another 
     631         
     632        :param other: data set to be unified 
     633         
     634        :return: new data set 
     635         
     636        :raise ValueError: raised when two data sets are incompatible 
     637         
     638        """ 
     639        return self._perform_union(other) 
     640         
     641    def __ror__(self, other): 
     642        """ 
     643        Union a data set with another 
     644         
     645        :param other: data set to be unified 
     646         
     647        :return: new data set 
     648         
     649        :raise ValueError: raised when two data sets are incompatible 
     650         
     651        """ 
     652        return self._perform_union(other) 
     653                 
    628654class Data1D(plottable_1D, DataInfo): 
    629655    """ 
     
    728754                    msg = "Incompatible data sets: x-values do not match" 
    729755                    raise ValueError, msg 
     756                """ 
    730757                if self.dxl != None and other.dxl == None: 
    731758                    msg = "Incompatible data sets: dxl-values do not match" 
     
    740767                    msg = "Incompatible data sets: dxw-values do not match" 
    741768                    raise ValueError, msg 
    742              
     769                """ 
    743770            # Check that the other data set has errors, otherwise 
    744771            # create zero vector 
     
    788815                if result.dxl is not None and other.dxl is not None: 
    789816                    result.dxl[i] *= self.dxl[i] 
    790                     other.dxl[i] += (other.dxl[i]**2) 
     817                    result.dxl[i] += (other.dxl[i]**2) 
    791818                    result.dxl[i] /= 2 
    792819                    result.dxl[i] = math.sqrt(result.dxl[i]) 
    793                 if result.dxw is not None and self.dxw is not None: 
    794                     result.dxw[i] *= self.dxw[i] 
    795                     other.dxw[i] += (other.dxw[i]**2) 
    796                     result.dxw[i] /= 2 
    797                     result.dxw[i] = math.sqrt(result.dxw[i]) 
    798820            else: 
    799821                b = other 
     
    802824            result.y[i] = output.x 
    803825            result.dy[i] = math.sqrt(math.fabs(output.variance)) 
     826        return result 
     827     
     828    def _validity_check_union(self, other): 
     829        """ 
     830        Checks that the data lengths are compatible. 
     831        Checks that the x vectors are compatible. 
     832        Returns errors vectors equal to original 
     833        errors vectors if they were present or vectors 
     834        of zeros when none was found. 
     835         
     836        :param other: other data set for operation 
     837         
     838        :return: bool 
     839         
     840        :raise ValueError: when data types are not compatible 
     841         
     842        """ 
     843        if not isinstance(other, Data1D): 
     844            msg = "Unable to perform operation: different types of data set" 
     845            raise ValueError, msg    
     846        return True 
     847 
     848    def _perform_union(self, other): 
     849        """ 
     850        """ 
     851        # First, check the data compatibility 
     852        self._validity_check_union(other) 
     853        result = self.clone_without_data(len(self.x) + len(other.x)) 
     854        if self.dy == None or other.dy is None: 
     855            result.dy = None 
     856        else: 
     857            result.dy = numpy.zeros(len(self.x) + len(other.x)) 
     858        if self.dx == None or other.dx is None: 
     859            result.dx = None 
     860        else: 
     861            result.dx = numpy.zeros(len(self.x) + len(other.x)) 
     862        if self.dxw == None or other.dxw is None: 
     863            result.dxw = None 
     864        else: 
     865            result.dxw = numpy.zeros(len(self.x) + len(other.x)) 
     866        if self.dxl == None or other.dxl is None: 
     867            result.dxl = None 
     868        else: 
     869            result.dxl = numpy.zeros(len(self.x) + len(other.x)) 
     870 
     871        result.x = numpy.append(self.x, other.x) 
     872        #argsorting 
     873        ind = numpy.argsort(result.x) 
     874        result.x = result.x[ind] 
     875        result.y = numpy.append(self.y, other.y) 
     876        result.y = result.y[ind] 
     877        if result.dy != None: 
     878            result.dy = numpy.append(self.dy, other.dy) 
     879            result.dy = result.dy[ind] 
     880        if result.dx is not None: 
     881            result.dx = numpy.append(self.dx, other.dx) 
     882            result.dx = result.dx[ind] 
     883        if result.dxw is not None: 
     884            result.dxw = numpy.append(self.dxw, other.dxw) 
     885            result.dxw = result.dxw[ind] 
     886        if result.dxl is not None: 
     887            result.dxl = numpy.append(self.dxl, other.dxl) 
     888            result.dxl = result.dxl[ind] 
    804889        return result 
    805890         
     
    9151000                msg = "Incompatible data sets: I-values do not match" 
    9161001                raise ValueError, msg  
    917             if self.qx_data.all() != other.qx_data.all(): 
    918                 msg = "Incompatible data sets: qx-values do not match" 
    919                 raise ValueError, msg 
    920             if self.qy_data.all() != other.qy_data.all(): 
    921                 msg = "Incompatible data sets: qy-values do not match" 
    922                 raise ValueError, msg 
     1002            for ind in range(len(self.data)): 
     1003                if self.qx_data[ind] != other.qx_data[ind]: 
     1004                    msg = "Incompatible data sets: qx-values do not match" 
     1005                    raise ValueError, msg 
     1006                if self.qy_data[ind] != other.qy_data[ind]: 
     1007                    msg = "Incompatible data sets: qy-values do not match" 
     1008                    raise ValueError, msg 
    9231009                    
    9241010            # Check that the scales match 
     
    9931079            result.err_data[i] = math.sqrt(math.fabs(output.variance)) 
    9941080        return result 
     1081     
     1082    def _validity_check_union(self, other): 
     1083        """ 
     1084        Checks that the data lengths are compatible. 
     1085        Checks that the x vectors are compatible. 
     1086        Returns errors vectors equal to original 
     1087        errors vectors if they were present or vectors 
     1088        of zeros when none was found. 
     1089         
     1090        :param other: other data set for operation 
     1091         
     1092        :return: bool 
     1093         
     1094        :raise ValueError: when data types are not compatible 
     1095         
     1096        """ 
     1097        if not isinstance(other, Data2D): 
     1098            msg = "Unable to perform operation: different types of data set" 
     1099            raise ValueError, msg    
     1100        return True 
     1101     
     1102    def _perform_union(self, other): 
     1103        """ 
     1104        Perform 2D operations between data sets 
     1105         
     1106        :param other: other data set 
     1107        :param operation: function defining the operation 
     1108         
     1109        """ 
     1110        # First, check the data compatibility 
     1111        self._validity_check_union(other) 
     1112        result = self.clone_without_data(numpy.size(self.data) + \ 
     1113                                         numpy.size(other.data)) 
     1114        result.xmin = self.xmin 
     1115        result.xmax = self.xmax 
     1116        result.ymin = self.ymin 
     1117        result.ymax = self.ymax 
     1118        if self.dqx_data == None or self.dqy_data == None or \ 
     1119                other.dqx_data == None or other.dqy_data == None : 
     1120            result.dqx_data = None 
     1121            result.dqy_data = None 
     1122        else: 
     1123            result.dqx_data = numpy.zeros(len(self.data) + \ 
     1124                                         numpy.size(other.data)) 
     1125            result.dqy_data = numpy.zeros(len(self.data) + \ 
     1126                                         numpy.size(other.data)) 
     1127         
     1128        result.data = numpy.append(self.data, other.data) 
     1129        result.qx_data = numpy.append(self.qx_data, other.qx_data) 
     1130        result.qy_data = numpy.append(self.qy_data, other.qy_data) 
     1131        result.q_data = numpy.append(self.q_data, other.q_data) 
     1132        result.mask = numpy.append(self.mask, other.mask) 
     1133        if result.err_data is not None: 
     1134            result.err_data = numpy.append(self.err_data, other.err_data)  
     1135        if self.dqx_data is not None: 
     1136            result.dqx_data = numpy.append(self.dqx_data, other.dqx_data) 
     1137        if self.dqy_data is not None: 
     1138            result.dqy_data = numpy.append(self.dqy_data, other.dqy_data) 
     1139 
     1140        return result 
Note: See TracChangeset for help on using the changeset viewer.