Changeset fd5d6eac in sasview for src/sas


Ignore:
Timestamp:
Apr 6, 2017 9:04:42 AM (7 years ago)
Author:
Ricardo Ferraz Leal <ricleal@…>
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.2.2, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
Children:
a470e88
Parents:
00a6ec4
Message:

better code. Remove warnings

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/sas/sascalc/dataloader/manipulations.py

    rdd11014 rfd5d6eac  
    55""" 
    66##################################################################### 
    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 
     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 
    1212###################################################################### 
    1313 
    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 
    1517import math 
    16 import numpy 
     18import numpy as np 
    1719 
    1820#from data_info import plottable_2D 
     
    8284    if data2d.data is None or data2d.x_bins is None or data2d.y_bins is None: 
    8385        raise ValueError, "Can't convert this data: data=None..." 
    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)) 
     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)) 
    8688    new_y = new_y.swapaxes(0, 1) 
    8789 
     
    8991    qx_data = new_x.flatten() 
    9092    qy_data = new_y.flatten() 
    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)) 
     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)) 
    9496    else: 
    9597        new_err_data = data2d.err_data.flatten() 
    96     mask = numpy.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... 
    99101    #from sas.sascalc.dataloader.data_info import Data2D 
    100102    #output = Data2D() 
     
    114116    Compute average I(Q) for a region of interest 
    115117    """ 
     118 
    116119    def __init__(self, x_min=0.0, x_max=0.0, y_min=0.0, 
    117120                 y_max=0.0, bin_width=0.001): 
     
    149152 
    150153        # Get 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)] 
     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)] 
    155158 
    156159        # Build array of Q intervals 
     
    170173            raise RuntimeError, "_Slab._avg: unrecognized axis %s" % str(maj) 
    171174 
    172         x = numpy.zeros(nbins) 
    173         y = numpy.zeros(nbins) 
    174         err_y = numpy.zeros(nbins) 
    175         y_counts = numpy.zeros(nbins) 
     175        x = np.zeros(nbins) 
     176        y = np.zeros(nbins) 
     177        err_y = np.zeros(nbins) 
     178        y_counts = np.zeros(nbins) 
    176179 
    177180        # Average pixelsize in q space 
     
    205208                continue 
    206209 
    207             #TODO: find better definition of x[i_q] based on q_data 
     210            # TODO: find better definition of x[i_q] based on q_data 
    208211            # min_value + (i_q + 1) * self.bin_width / 2.0 
    209212            x[i_q] += frac * q_value 
    210213            y[i_q] += frac * data[npts] 
    211214 
    212             if err_data == None or err_data[npts] == 0.0: 
     215            if err_data is None or err_data[npts] == 0.0: 
    213216                if data[npts] < 0: 
    214217                    data[npts] = -data[npts] 
     
    225228        y = y / y_counts 
    226229        x = x / y_counts 
    227         idx = (numpy.isfinite(y) & numpy.isfinite(x)) 
     230        idx = (np.isfinite(y) & np.isfinite(x)) 
    228231 
    229232        if not idx.any(): 
     
    237240    Compute average I(Qy) for a region of interest 
    238241    """ 
     242 
    239243    def __call__(self, data2D): 
    240244        """ 
     
    251255    Compute average I(Qx) for a region of interest 
    252256    """ 
     257 
    253258    def __call__(self, data2D): 
    254259        """ 
     
    264269    Perform the sum of counts in a 2D region of interest. 
    265270    """ 
     271 
    266272    def __init__(self, x_min=0.0, x_max=0.0, y_min=0.0, y_max=0.0): 
    267273        # Minimum Qx value [A-1] 
     
    304310            raise RuntimeError, msg 
    305311        # Get 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)] 
     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)] 
    310316 
    311317        y = 0.0 
     
    328334            if self.y_min <= qy and self.y_max > qy: 
    329335                frac_y = 1 
    330             #Find the fraction along each directions 
     336            # Find the fraction along each directions 
    331337            frac = frac_x * frac_y 
    332338            if frac == 0: 
    333339                continue 
    334340            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: 
    336342                if data[npts] < 0: 
    337343                    data[npts] = -data[npts] 
     
    347353    Perform the average of counts in a 2D region of interest. 
    348354    """ 
     355 
    349356    def __init__(self, x_min=0.0, x_max=0.0, y_min=0.0, y_max=0.0): 
    350357        super(Boxavg, self).__init__(x_min=x_min, x_max=x_max, 
     
    398405    as a function of Q 
    399406    """ 
     407 
    400408    def __init__(self, r_min=0.0, r_max=0.0, bin_width=0.0005): 
    401409        # Minimum radius included in the average [A-1] 
     
    414422        """ 
    415423        # Get data W/ finite values 
    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)] 
     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)] 
    420428 
    421429        dq_data = None 
     
    448456            dq_overlap_y *= dq_overlap_y 
    449457 
    450             dq_overlap = numpy.sqrt((dq_overlap_x + dq_overlap_y) / 2.0) 
     458            dq_overlap = np.sqrt((dq_overlap_x + dq_overlap_y) / 2.0) 
    451459            # Final protection of dq 
    452460            if dq_overlap < 0: 
    453461                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 
    456465            # def; dqx_data = dq_r dqy_data = dq_phi 
    457466            # Convert dq 2D to 1D here 
    458467            dqx = dqx_data * dqx_data 
    459468            dqy = dqy_data * dqy_data 
    460             dq_data = numpy.add(dqx, dqy) 
    461             dq_data = numpy.sqrt(dq_data) 
    462  
    463         #q_data_max = numpy.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) 
    464473        if len(data2D.q_data) == None: 
    465474            msg = "Circular averaging: invalid q_data: %g" % data2D.q_data 
     
    469478        nbins = int(math.ceil((self.r_max - self.r_min) / self.bin_width)) 
    470479 
    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) 
     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) 
    476485 
    477486        for npt in range(len(data)): 
     
    486495            data_n = data[npt] 
    487496 
    488             ## No need to calculate the frac when all data are within range 
     497            # No need to calculate the frac when all data are within range 
    489498            if self.r_min >= self.r_max: 
    490499                raise ValueError, "Limit Error: min > max" 
     
    502511            # Take dqs from data to get the q_average 
    503512            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: 
    505514                if data_n < 0: 
    506515                    data_n = -data_n 
     
    523532                err_y[n] = -err_y[n] 
    524533            err_y[n] = math.sqrt(err_y[n]) 
    525             #if err_x != None: 
     534            # if err_x != None: 
    526535            #    err_x[n] = math.sqrt(err_x[n]) 
    527536 
    528537        err_y = err_y / y_counts 
    529         err_y[err_y == 0] = numpy.average(err_y) 
     538        err_y[err_y == 0] = np.average(err_y) 
    530539        y = y / y_counts 
    531540        x = x / y_counts 
    532         idx = (numpy.isfinite(y)) & (numpy.isfinite(x)) 
     541        idx = (np.isfinite(y)) & (np.isfinite(x)) 
    533542 
    534543        if err_x != None: 
     
    556565    in anti-clockwise starting from the x- axis on the left-hand side 
    557566    """ 
    558     #Todo: remove center. 
     567    # Todo: remove center. 
     568 
    559569    def __init__(self, r_min=0, r_max=0, center_x=0, center_y=0, nbins=36): 
    560570        # Minimum radius 
     
    569579        self.nbins_phi = nbins 
    570580 
    571  
    572581    def __call__(self, data2D): 
    573582        """ 
     
    585594 
    586595        # Get 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)] 
     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)] 
    592601 
    593602        # Set space for 1d outputs 
    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) 
     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) 
    598607 
    599608        # Shift to apply to calculated phi values in order 
     
    615624                continue 
    616625            # binning 
    617             i_phi = int(math.floor((self.nbins_phi) * \ 
     626            i_phi = int(math.floor((self.nbins_phi) * 
    618627                                   (phi_value + phi_shift) / (2 * Pi))) 
    619628 
     
    623632            phi_bins[i_phi] += frac * data[npt] 
    624633 
    625             if err_data == None or err_data[npt] == 0.0: 
     634            if err_data is None or err_data[npt] == 0.0: 
    626635                if data_n < 0: 
    627636                    data_n = -data_n 
     
    636645            phi_values[i] = 2.0 * math.pi / self.nbins_phi * (1.0 * i) 
    637646 
    638         idx = (numpy.isfinite(phi_bins)) 
     647        idx = (np.isfinite(phi_bins)) 
    639648 
    640649        if not idx.any(): 
    641650            msg = "Average Error: No points inside ROI to average..." 
    642651            raise ValueError, msg 
    643         #elif len(phi_bins[idx])!= self.nbins_phi: 
     652        # elif len(phi_bins[idx])!= self.nbins_phi: 
    644653        #    print "resulted",self.nbins_phi- len(phi_bins[idx]) 
    645654        #,"empty bin(s) due to tight binning..." 
     
    748757    starting from the x- axis on the left-hand side 
    749758    """ 
     759 
    750760    def __init__(self, r_min, r_max, phi_min=0, phi_max=2 * math.pi, nbins=20): 
    751761        self.r_min = r_min 
     
    769779 
    770780        # Get the all data & info 
    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)] 
     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)] 
    776786        dq_data = None 
    777787 
     
    803813            dq_overlap_y *= dq_overlap_y 
    804814 
    805             dq_overlap = numpy.sqrt((dq_overlap_x + dq_overlap_y) / 2.0) 
     815            dq_overlap = np.sqrt((dq_overlap_x + dq_overlap_y) / 2.0) 
    806816            if dq_overlap < 0: 
    807817                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 
    810821            # def; dqx_data = dq_r dqy_data = dq_phi 
    811822            # Convert dq 2D to 1D here 
    812823            dqx = dqx_data * dqx_data 
    813824            dqy = dqy_data * dqy_data 
    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) 
     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) 
    823834 
    824835        # Get the min and max into the region: 0 <= phi < 2Pi 
     
    839850            phi_value = math.atan2(qy_data[n], qx_data[n]) + Pi 
    840851 
    841             ## No need to calculate the frac when all data are within range 
     852            # No need to calculate the frac when all data are within range 
    842853            if self.r_min <= q_value and q_value <= self.r_max: 
    843854                frac = 1 
    844855            if frac == 0: 
    845856                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') 
    847858            if run.lower() == 'q2': 
    848                 ## For minor sector wing 
     859                # For minor sector wing 
    849860                # Calculate the minor wing phis 
    850861                phi_min_minor = flip_phi(phi_min - Pi) 
     
    852863                # Check if phis of the minor ring is within 0 to 2pi 
    853864                if phi_min_minor > phi_max_minor: 
    854                     is_in = (phi_value > phi_min_minor or \ 
    855                               phi_value < phi_max_minor) 
     865                    is_in = (phi_value > phi_min_minor or 
     866                             phi_value < phi_max_minor) 
    856867                else: 
    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 
     868                    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 
    862873            if phi_min > phi_max: 
    863                 is_in = is_in or (phi_value > phi_min or \ 
    864                                    phi_value < phi_max) 
     874                is_in = is_in or (phi_value > phi_min or 
     875                                  phi_value < phi_max) 
    865876            else: 
    866                 is_in = is_in or (phi_value >= phi_min  and \ 
    867                                     phi_value < phi_max) 
     877                is_in = is_in or (phi_value >= phi_min and 
     878                                  phi_value < phi_max) 
    868879 
    869880            if not is_in: 
     
    885896                i_bin = self.nbins - 1 
    886897 
    887             ## Get the total y 
     898            # Get the total y 
    888899            y[i_bin] += frac * data_n 
    889900            x[i_bin] += frac * q_value 
     
    923934                #x[i] = math.sqrt((r_inner * r_inner + r_outer * r_outer) / 2) 
    924935                x[i] = x[i] / y_counts[i] 
    925         y_err[y_err == 0] = numpy.average(y_err) 
    926         idx = (numpy.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)) 
    927938        if x_err != None: 
    928939            d_x = x_err[idx] / y_counts[idx] 
     
    932943            msg = "Average Error: No points inside sector of ROI to average..." 
    933944            raise ValueError, msg 
    934         #elif len(y[idx])!= self.nbins: 
     945        # elif len(y[idx])!= self.nbins: 
    935946        #    print "resulted",self.nbins- len(y[idx]), 
    936947        #"empty bin(s) due to tight binning..." 
     
    946957    The number of bin in phi also has to be defined. 
    947958    """ 
     959 
    948960    def __call__(self, data2D): 
    949961        """ 
     
    965977    The number of bin in Q also has to be defined. 
    966978    """ 
     979 
    967980    def __call__(self, data2D): 
    968981        """ 
     
    9871000    in anti-clockwise starting from the x- axis on the left-hand side 
    9881001    """ 
     1002 
    9891003    def __init__(self, r_min=0, r_max=0, center_x=0, center_y=0): 
    9901004        # Minimum radius 
     
    10121026        qx_data = data2D.qx_data 
    10131027        qy_data = data2D.qy_data 
    1014         q_data = numpy.sqrt(qx_data * qx_data + qy_data * qy_data) 
     1028        q_data = np.sqrt(qx_data * qx_data + qy_data * qy_data) 
    10151029 
    10161030        # check whether or not the data point is inside ROI 
     
    10231037    Find a rectangular 2D region of interest. 
    10241038    """ 
     1039 
    10251040    def __init__(self, x_min=0.0, x_max=0.0, y_min=0.0, y_max=0.0): 
    10261041        # Minimum Qx value [A-1] 
     
    10771092    and (phi_max-phi_min) should not be larger than pi 
    10781093    """ 
     1094 
    10791095    def __init__(self, phi_min=0, phi_max=math.pi): 
    10801096        self.phi_min = phi_min 
     
    11131129 
    11141130        # get phi from data 
    1115         phi_data = numpy.arctan2(qy_data, qx_data) 
     1131        phi_data = np.arctan2(qy_data, qx_data) 
    11161132 
    11171133        # Get the min and max into the region: -pi <= phi < Pi 
     
    11201136        # check for major sector 
    11211137        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) 
    11231140        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) 
    11251143 
    11261144        # minor sector 
     
    11321150        if phi_min_minor > phi_max_minor: 
    11331151            out_minor = (phi_min_minor <= phi_data) + \ 
    1134                             (phi_max_minor >= phi_data) 
     1152                (phi_max_minor >= phi_data) 
    11351153        else: 
    11361154            out_minor = (phi_min_minor <= phi_data) & \ 
    1137                             (phi_max_minor >= phi_data) 
     1155                (phi_max_minor >= phi_data) 
    11381156        out = out_major + out_minor 
    11391157 
Note: See TracChangeset for help on using the changeset viewer.