Changeset cfc6f3c7 in sasview for src/sas/sascalc/dataloader


Ignore:
Timestamp:
Apr 5, 2017 5:21:31 AM (7 years ago)
Author:
krzywon
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:
69400ec
Parents:
270c882b (diff), a2e980b (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' into ticket-876

Location:
src/sas/sascalc/dataloader
Files:
1 added
13 edited

Legend:

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

    r2ffe241 r9a5097c  
    2323#from sas.guitools.plottables import Data1D as plottable_1D 
    2424from sas.sascalc.data_util.uncertainty import Uncertainty 
    25 import numpy 
     25import numpy as np 
    2626import math 
    2727 
     
    5151 
    5252    def __init__(self, x, y, dx=None, dy=None, dxl=None, dxw=None, lam=None, dlam=None): 
    53         self.x = numpy.asarray(x) 
    54         self.y = numpy.asarray(y) 
     53        self.x = np.asarray(x) 
     54        self.y = np.asarray(y) 
    5555        if dx is not None: 
    56             self.dx = numpy.asarray(dx) 
     56            self.dx = np.asarray(dx) 
    5757        if dy is not None: 
    58             self.dy = numpy.asarray(dy) 
     58            self.dy = np.asarray(dy) 
    5959        if dxl is not None: 
    60             self.dxl = numpy.asarray(dxl) 
     60            self.dxl = np.asarray(dxl) 
    6161        if dxw is not None: 
    62             self.dxw = numpy.asarray(dxw) 
     62            self.dxw = np.asarray(dxw) 
    6363        if lam is not None: 
    64             self.lam = numpy.asarray(lam) 
     64            self.lam = np.asarray(lam) 
    6565        if dlam is not None: 
    66             self.dlam = numpy.asarray(dlam) 
     66            self.dlam = np.asarray(dlam) 
    6767 
    6868    def xaxis(self, label, unit): 
     
    109109                 qy_data=None, q_data=None, mask=None, 
    110110                 dqx_data=None, dqy_data=None): 
    111         self.data = numpy.asarray(data) 
    112         self.qx_data = numpy.asarray(qx_data) 
    113         self.qy_data = numpy.asarray(qy_data) 
    114         self.q_data = numpy.asarray(q_data) 
    115         self.mask = numpy.asarray(mask) 
    116         self.err_data = numpy.asarray(err_data) 
     111        self.data = np.asarray(data) 
     112        self.qx_data = np.asarray(qx_data) 
     113        self.qy_data = np.asarray(qy_data) 
     114        self.q_data = np.asarray(q_data) 
     115        self.mask = np.asarray(mask) 
     116        self.err_data = np.asarray(err_data) 
    117117        if dqx_data is not None: 
    118             self.dqx_data = numpy.asarray(dqx_data) 
     118            self.dqx_data = np.asarray(dqx_data) 
    119119        if dqy_data is not None: 
    120             self.dqy_data = numpy.asarray(dqy_data) 
     120            self.dqy_data = np.asarray(dqy_data) 
    121121 
    122122    def xaxis(self, label, unit): 
     
    734734        """ 
    735735        def _check(v): 
    736             if (v.__class__ == list or v.__class__ == numpy.ndarray) \ 
     736            if (v.__class__ == list or v.__class__ == np.ndarray) \ 
    737737                and len(v) > 0 and min(v) > 0: 
    738738                return True 
     
    752752 
    753753        if clone is None or not issubclass(clone.__class__, Data1D): 
    754             x = numpy.zeros(length) 
    755             dx = numpy.zeros(length) 
    756             y = numpy.zeros(length) 
    757             dy = numpy.zeros(length) 
    758             lam = numpy.zeros(length) 
    759             dlam = numpy.zeros(length) 
     754            x = np.zeros(length) 
     755            dx = np.zeros(length) 
     756            y = np.zeros(length) 
     757            dy = np.zeros(length) 
     758            lam = np.zeros(length) 
     759            dlam = np.zeros(length) 
    760760            clone = Data1D(x, y, lam=lam, dx=dx, dy=dy, dlam=dlam) 
    761761 
     
    806806            dy_other = other.dy 
    807807            if other.dy == None or (len(other.dy) != len(other.y)): 
    808                 dy_other = numpy.zeros(len(other.y)) 
     808                dy_other = np.zeros(len(other.y)) 
    809809 
    810810        # Check that we have errors, otherwise create zero vector 
    811811        dy = self.dy 
    812812        if self.dy == None or (len(self.dy) != len(self.y)): 
    813             dy = numpy.zeros(len(self.y)) 
     813            dy = np.zeros(len(self.y)) 
    814814 
    815815        return dy, dy_other 
     
    824824            result.dxw = None 
    825825        else: 
    826             result.dxw = numpy.zeros(len(self.x)) 
     826            result.dxw = np.zeros(len(self.x)) 
    827827        if self.dxl == None: 
    828828            result.dxl = None 
    829829        else: 
    830             result.dxl = numpy.zeros(len(self.x)) 
     830            result.dxl = np.zeros(len(self.x)) 
    831831 
    832832        for i in range(len(self.x)): 
     
    886886            result.dy = None 
    887887        else: 
    888             result.dy = numpy.zeros(len(self.x) + len(other.x)) 
     888            result.dy = np.zeros(len(self.x) + len(other.x)) 
    889889        if self.dx == None or other.dx is None: 
    890890            result.dx = None 
    891891        else: 
    892             result.dx = numpy.zeros(len(self.x) + len(other.x)) 
     892            result.dx = np.zeros(len(self.x) + len(other.x)) 
    893893        if self.dxw == None or other.dxw is None: 
    894894            result.dxw = None 
    895895        else: 
    896             result.dxw = numpy.zeros(len(self.x) + len(other.x)) 
     896            result.dxw = np.zeros(len(self.x) + len(other.x)) 
    897897        if self.dxl == None or other.dxl is None: 
    898898            result.dxl = None 
    899899        else: 
    900             result.dxl = numpy.zeros(len(self.x) + len(other.x)) 
    901  
    902         result.x = numpy.append(self.x, other.x) 
     900            result.dxl = np.zeros(len(self.x) + len(other.x)) 
     901 
     902        result.x = np.append(self.x, other.x) 
    903903        #argsorting 
    904         ind = numpy.argsort(result.x) 
     904        ind = np.argsort(result.x) 
    905905        result.x = result.x[ind] 
    906         result.y = numpy.append(self.y, other.y) 
     906        result.y = np.append(self.y, other.y) 
    907907        result.y = result.y[ind] 
    908908        if result.dy != None: 
    909             result.dy = numpy.append(self.dy, other.dy) 
     909            result.dy = np.append(self.dy, other.dy) 
    910910            result.dy = result.dy[ind] 
    911911        if result.dx is not None: 
    912             result.dx = numpy.append(self.dx, other.dx) 
     912            result.dx = np.append(self.dx, other.dx) 
    913913            result.dx = result.dx[ind] 
    914914        if result.dxw is not None: 
    915             result.dxw = numpy.append(self.dxw, other.dxw) 
     915            result.dxw = np.append(self.dxw, other.dxw) 
    916916            result.dxw = result.dxw[ind] 
    917917        if result.dxl is not None: 
    918             result.dxl = numpy.append(self.dxl, other.dxl) 
     918            result.dxl = np.append(self.dxl, other.dxl) 
    919919            result.dxl = result.dxl[ind] 
    920920        return result 
     
    970970 
    971971        if clone is None or not issubclass(clone.__class__, Data2D): 
    972             data = numpy.zeros(length) 
    973             err_data = numpy.zeros(length) 
    974             qx_data = numpy.zeros(length) 
    975             qy_data = numpy.zeros(length) 
    976             q_data = numpy.zeros(length) 
    977             mask = numpy.zeros(length) 
     972            data = np.zeros(length) 
     973            err_data = np.zeros(length) 
     974            qx_data = np.zeros(length) 
     975            qy_data = np.zeros(length) 
     976            q_data = np.zeros(length) 
     977            mask = np.zeros(length) 
    978978            dqx_data = None 
    979979            dqy_data = None 
     
    10311031            if other.err_data == None or \ 
    10321032                (len(other.err_data) != len(other.data)): 
    1033                 err_other = numpy.zeros(len(other.data)) 
     1033                err_other = np.zeros(len(other.data)) 
    10341034 
    10351035        # Check that we have errors, otherwise create zero vector 
     
    10371037        if self.err_data == None or \ 
    10381038            (len(self.err_data) != len(self.data)): 
    1039             err = numpy.zeros(len(other.data)) 
     1039            err = np.zeros(len(other.data)) 
    10401040        return err, err_other 
    10411041 
     
    10491049        # First, check the data compatibility 
    10501050        dy, dy_other = self._validity_check(other) 
    1051         result = self.clone_without_data(numpy.size(self.data)) 
     1051        result = self.clone_without_data(np.size(self.data)) 
    10521052        if self.dqx_data == None or self.dqy_data == None: 
    10531053            result.dqx_data = None 
    10541054            result.dqy_data = None 
    10551055        else: 
    1056             result.dqx_data = numpy.zeros(len(self.data)) 
    1057             result.dqy_data = numpy.zeros(len(self.data)) 
    1058         for i in range(numpy.size(self.data)): 
     1056            result.dqx_data = np.zeros(len(self.data)) 
     1057            result.dqy_data = np.zeros(len(self.data)) 
     1058        for i in range(np.size(self.data)): 
    10591059            result.data[i] = self.data[i] 
    10601060            if self.err_data is not None and \ 
    1061                 numpy.size(self.data) == numpy.size(self.err_data): 
     1061                            np.size(self.data) == np.size(self.err_data): 
    10621062                result.err_data[i] = self.err_data[i] 
    10631063            if self.dqx_data is not None: 
     
    11181118        # First, check the data compatibility 
    11191119        self._validity_check_union(other) 
    1120         result = self.clone_without_data(numpy.size(self.data) + \ 
    1121                                          numpy.size(other.data)) 
     1120        result = self.clone_without_data(np.size(self.data) + \ 
     1121                                         np.size(other.data)) 
    11221122        result.xmin = self.xmin 
    11231123        result.xmax = self.xmax 
     
    11291129            result.dqy_data = None 
    11301130        else: 
    1131             result.dqx_data = numpy.zeros(len(self.data) + \ 
    1132                                          numpy.size(other.data)) 
    1133             result.dqy_data = numpy.zeros(len(self.data) + \ 
    1134                                          numpy.size(other.data)) 
    1135  
    1136         result.data = numpy.append(self.data, other.data) 
    1137         result.qx_data = numpy.append(self.qx_data, other.qx_data) 
    1138         result.qy_data = numpy.append(self.qy_data, other.qy_data) 
    1139         result.q_data = numpy.append(self.q_data, other.q_data) 
    1140         result.mask = numpy.append(self.mask, other.mask) 
     1131            result.dqx_data = np.zeros(len(self.data) + \ 
     1132                                       np.size(other.data)) 
     1133            result.dqy_data = np.zeros(len(self.data) + \ 
     1134                                       np.size(other.data)) 
     1135 
     1136        result.data = np.append(self.data, other.data) 
     1137        result.qx_data = np.append(self.qx_data, other.qx_data) 
     1138        result.qy_data = np.append(self.qy_data, other.qy_data) 
     1139        result.q_data = np.append(self.q_data, other.q_data) 
     1140        result.mask = np.append(self.mask, other.mask) 
    11411141        if result.err_data is not None: 
    1142             result.err_data = numpy.append(self.err_data, other.err_data) 
     1142            result.err_data = np.append(self.err_data, other.err_data) 
    11431143        if self.dqx_data is not None: 
    1144             result.dqx_data = numpy.append(self.dqx_data, other.dqx_data) 
     1144            result.dqx_data = np.append(self.dqx_data, other.dqx_data) 
    11451145        if self.dqy_data is not None: 
    1146             result.dqy_data = numpy.append(self.dqy_data, other.dqy_data) 
     1146            result.dqy_data = np.append(self.dqy_data, other.dqy_data) 
    11471147 
    11481148        return result 
  • src/sas/sascalc/dataloader/manipulations.py

    rb2b36932 rdd11014  
    8080 
    8181    """ 
    82     if data2d.data == None or data2d.x_bins == None or data2d.y_bins == None: 
     82    if data2d.data is None or data2d.x_bins is None or data2d.y_bins is None: 
    8383        raise ValueError, "Can't convert this data: data=None..." 
    8484    new_x = numpy.tile(data2d.x_bins, (len(data2d.y_bins), 1)) 
     
    9090    qy_data = new_y.flatten() 
    9191    q_data = numpy.sqrt(qx_data * qx_data + qy_data * qy_data) 
    92     if data2d.err_data == None or numpy.any(data2d.err_data <= 0): 
     92    if data2d.err_data is None or numpy.any(data2d.err_data <= 0): 
    9393        new_err_data = numpy.sqrt(numpy.abs(new_data)) 
    9494    else: 
  • src/sas/sascalc/dataloader/readers/IgorReader.py

    rb699768 rdd11014  
    1313############################################################################# 
    1414import os 
    15 import numpy 
    16 import math 
    17 #import logging 
     15 
    1816from sas.sascalc.dataloader.data_info import Data2D 
    1917from sas.sascalc.dataloader.data_info import Detector 
    2018from sas.sascalc.dataloader.manipulations import reader2D_converter 
     19import numpy as np 
    2120 
    2221# Look for unit converter 
     
    4039        """ Read file """ 
    4140        if not os.path.isfile(filename): 
    42             raise ValueError, \ 
    43             "Specified file %s is not a regular file" % filename 
    44          
    45         # Read file 
    46         f = open(filename, 'r') 
    47         buf = f.read() 
    48          
    49         # Instantiate data object 
     41            raise ValueError("Specified file %s is not a regular " 
     42                             "file" % filename) 
     43         
    5044        output = Data2D() 
     45 
    5146        output.filename = os.path.basename(filename) 
    5247        detector = Detector() 
    53         if len(output.detector) > 0: 
    54             print str(output.detector[0]) 
     48        if len(output.detector): 
     49            print(str(output.detector[0])) 
    5550        output.detector.append(detector) 
    56                  
    57         # Get content 
    58         dataStarted = False 
    59          
    60         lines = buf.split('\n') 
    61         itot = 0 
    62         x = [] 
    63         y = [] 
    64          
    65         ncounts = 0 
    66          
    67         xmin = None 
    68         xmax = None 
    69         ymin = None 
    70         ymax = None 
    71          
    72         i_x = 0 
    73         i_y = -1 
    74         i_tot_row = 0 
    75          
    76         isInfo = False 
    77         isCenter = False 
    78         
    79         data_conv_q = None 
    80         data_conv_i = None 
    81          
    82         if has_converter == True and output.Q_unit != '1/A': 
     51 
     52        data_conv_q = data_conv_i = None 
     53         
     54        if has_converter and output.Q_unit != '1/A': 
    8355            data_conv_q = Converter('1/A') 
    8456            # Test it 
    8557            data_conv_q(1.0, output.Q_unit) 
    8658             
    87         if has_converter == True and output.I_unit != '1/cm': 
     59        if has_converter and output.I_unit != '1/cm': 
    8860            data_conv_i = Converter('1/cm') 
    8961            # Test it 
    9062            data_conv_i(1.0, output.I_unit) 
    91           
    92         for line in lines: 
    93              
    94             # Find setup info line 
    95             if isInfo: 
    96                 isInfo = False 
    97                 line_toks = line.split() 
    98                 # Wavelength in Angstrom 
    99                 try: 
    100                     wavelength = float(line_toks[1]) 
    101                 except: 
    102                     msg = "IgorReader: can't read this file, missing wavelength" 
    103                     raise ValueError, msg 
    104                  
    105             #Find # of bins in a row assuming the detector is square. 
    106             if dataStarted == True: 
    107                 try: 
    108                     value = float(line) 
    109                 except: 
    110                     # Found a non-float entry, skip it 
    111                     continue 
    112                  
    113                 # Get total bin number 
    114                  
    115             i_tot_row += 1 
    116         i_tot_row = math.ceil(math.sqrt(i_tot_row)) - 1 
    117         #print "i_tot", i_tot_row 
    118         size_x = i_tot_row  # 192#128 
    119         size_y = i_tot_row  # 192#128 
    120         output.data = numpy.zeros([size_x, size_y]) 
    121         output.err_data = numpy.zeros([size_x, size_y]) 
    122       
    123         #Read Header and 2D data 
    124         for line in lines: 
    125             # Find setup info line 
    126             if isInfo: 
    127                 isInfo = False 
    128                 line_toks = line.split() 
    129                 # Wavelength in Angstrom 
    130                 try: 
    131                     wavelength = float(line_toks[1]) 
    132                 except: 
    133                     msg = "IgorReader: can't read this file, missing wavelength" 
    134                     raise ValueError, msg 
    135                 # Distance in meters 
    136                 try: 
    137                     distance = float(line_toks[3]) 
    138                 except: 
    139                     msg = "IgorReader: can't read this file, missing distance" 
    140                     raise ValueError, msg 
    141                  
    142                 # Distance in meters 
    143                 try: 
    144                     transmission = float(line_toks[4]) 
    145                 except: 
    146                     msg = "IgorReader: can't read this file, " 
    147                     msg += "missing transmission" 
    148                     raise ValueError, msg 
    149                                              
    150             if line.count("LAMBDA") > 0: 
    151                 isInfo = True 
    152                  
    153             # Find center info line 
    154             if isCenter: 
    155                 isCenter = False 
    156                 line_toks = line.split() 
    157                  
    158                 # Center in bin number: Must substrate 1 because 
    159                 #the index starts from 1 
    160                 center_x = float(line_toks[0]) - 1 
    161                 center_y = float(line_toks[1]) - 1 
    162  
    163             if line.count("BCENT") > 0: 
    164                 isCenter = True 
    165                  
    166             # Find data start 
    167             if line.count("***")>0: 
    168                 dataStarted = True 
    169                  
    170                 # Check that we have all the info 
    171                 if wavelength == None \ 
    172                     or distance == None \ 
    173                     or center_x == None \ 
    174                     or center_y == None: 
    175                     msg = "IgorReader:Missing information in data file" 
    176                     raise ValueError, msg 
    177                  
    178             if dataStarted == True: 
    179                 try: 
    180                     value = float(line) 
    181                 except: 
    182                     # Found a non-float entry, skip it 
    183                     continue 
    184                  
    185                 # Get bin number 
    186                 if math.fmod(itot, i_tot_row) == 0: 
    187                     i_x = 0 
    188                     i_y += 1 
    189                 else: 
    190                     i_x += 1 
    191                      
    192                 output.data[i_y][i_x] = value 
    193                 ncounts += 1 
    194                  
    195                 # Det 640 x 640 mm 
    196                 # Q = 4pi/lambda sin(theta/2) 
    197                 # Bin size is 0.5 cm  
    198                 #REmoved +1 from theta = (i_x-center_x+1)*0.5 / distance 
    199                 # / 100.0 and  
    200                 #REmoved +1 from theta = (i_y-center_y+1)*0.5 / 
    201                 # distance / 100.0 
    202                 #ToDo: Need  complete check if the following 
    203                 # covert process is consistent with fitting.py. 
    204                 theta = (i_x - center_x) * 0.5 / distance / 100.0 
    205                 qx = 4.0 * math.pi / wavelength * math.sin(theta/2.0) 
    206  
    207                 if has_converter == True and output.Q_unit != '1/A': 
    208                     qx = data_conv_q(qx, units=output.Q_unit) 
    209  
    210                 if xmin == None or qx < xmin: 
    211                     xmin = qx 
    212                 if xmax == None or qx > xmax: 
    213                     xmax = qx 
    214                  
    215                 theta = (i_y - center_y) * 0.5 / distance / 100.0 
    216                 qy = 4.0 * math.pi / wavelength * math.sin(theta / 2.0) 
    217  
    218                 if has_converter == True and output.Q_unit != '1/A': 
    219                     qy = data_conv_q(qy, units=output.Q_unit) 
    220                  
    221                 if ymin == None or qy < ymin: 
    222                     ymin = qy 
    223                 if ymax == None or qy > ymax: 
    224                     ymax = qy 
    225                  
    226                 if not qx in x: 
    227                     x.append(qx) 
    228                 if not qy in y: 
    229                     y.append(qy) 
    230                  
    231                 itot += 1 
    232                    
    233                    
     63 
     64        data_row = 0 
     65        wavelength = distance = center_x = center_y = None 
     66        dataStarted = isInfo = isCenter = False 
     67 
     68        with open(filename, 'r') as f: 
     69            for line in f: 
     70                data_row += 1 
     71                # Find setup info line 
     72                if isInfo: 
     73                    isInfo = False 
     74                    line_toks = line.split() 
     75                    # Wavelength in Angstrom 
     76                    try: 
     77                        wavelength = float(line_toks[1]) 
     78                    except ValueError: 
     79                        msg = "IgorReader: can't read this file, missing wavelength" 
     80                        raise ValueError(msg) 
     81                    # Distance in meters 
     82                    try: 
     83                        distance = float(line_toks[3]) 
     84                    except ValueError: 
     85                        msg = "IgorReader: can't read this file, missing distance" 
     86                        raise ValueError(msg) 
     87 
     88                    # Distance in meters 
     89                    try: 
     90                        transmission = float(line_toks[4]) 
     91                    except: 
     92                        msg = "IgorReader: can't read this file, " 
     93                        msg += "missing transmission" 
     94                        raise ValueError(msg) 
     95 
     96                if line.count("LAMBDA"): 
     97                    isInfo = True 
     98 
     99                # Find center info line 
     100                if isCenter: 
     101                    isCenter = False 
     102                    line_toks = line.split() 
     103 
     104                    # Center in bin number: Must subtract 1 because 
     105                    # the index starts from 1 
     106                    center_x = float(line_toks[0]) - 1 
     107                    center_y = float(line_toks[1]) - 1 
     108 
     109                if line.count("BCENT"): 
     110                    isCenter = True 
     111 
     112                # Find data start 
     113                if line.count("***"): 
     114                    # now have to continue to blank line 
     115                    dataStarted = True 
     116 
     117                    # Check that we have all the info 
     118                    if (wavelength is None 
     119                            or distance is None 
     120                            or center_x is None 
     121                            or center_y is None): 
     122                        msg = "IgorReader:Missing information in data file" 
     123                        raise ValueError(msg) 
     124 
     125                if dataStarted: 
     126                    if len(line.rstrip()): 
     127                        continue 
     128                    else: 
     129                        break 
     130 
     131        # The data is loaded in row major order (last index changing most 
     132        # rapidly). However, the original data is in column major order (first 
     133        # index changing most rapidly). The swap to column major order is done 
     134        # in reader2D_converter at the end of this method. 
     135        data = np.loadtxt(filename, skiprows=data_row) 
     136        size_x = size_y = int(np.rint(np.sqrt(data.size))) 
     137        output.data = np.reshape(data, (size_x, size_y)) 
     138        output.err_data = np.zeros_like(output.data) 
     139 
     140        # Det 640 x 640 mm 
     141        # Q = 4 * pi/lambda * sin(theta/2) 
     142        # Bin size is 0.5 cm 
     143        # Removed +1 from theta = (i_x - center_x + 1)*0.5 / distance 
     144        # / 100.0 and 
     145        # Removed +1 from theta = (i_y - center_y + 1)*0.5 / 
     146        # distance / 100.0 
     147        # ToDo: Need  complete check if the following 
     148        # convert process is consistent with fitting.py. 
     149 
     150        # calculate qx, qy bin centers of each pixel in the image 
     151        theta = (np.arange(size_x) - center_x) * 0.5 / distance / 100. 
     152        qx = 4 * np.pi / wavelength * np.sin(theta/2) 
     153 
     154        theta = (np.arange(size_y) - center_y) * 0.5 / distance / 100. 
     155        qy = 4 * np.pi / wavelength * np.sin(theta/2) 
     156 
     157        if has_converter and output.Q_unit != '1/A': 
     158            qx = data_conv_q(qx, units=output.Q_unit) 
     159            qy = data_conv_q(qx, units=output.Q_unit) 
     160 
     161        xmax = np.max(qx) 
     162        xmin = np.min(qx) 
     163        ymax = np.max(qy) 
     164        ymin = np.min(qy) 
     165 
     166        # calculate edge offset in q. 
    234167        theta = 0.25 / distance / 100.0 
    235         xstep = 4.0 * math.pi / wavelength * math.sin(theta / 2.0) 
     168        xstep = 4.0 * np.pi / wavelength * np.sin(theta / 2.0) 
    236169         
    237170        theta = 0.25 / distance / 100.0 
    238         ystep = 4.0 * math.pi/ wavelength * math.sin(theta / 2.0) 
     171        ystep = 4.0 * np.pi/ wavelength * np.sin(theta / 2.0) 
    239172         
    240173        # Store all data ###################################### 
    241174        # Store wavelength 
    242         if has_converter == True and output.source.wavelength_unit != 'A': 
     175        if has_converter and output.source.wavelength_unit != 'A': 
    243176            conv = Converter('A') 
    244177            wavelength = conv(wavelength, units=output.source.wavelength_unit) 
     
    246179 
    247180        # Store distance 
    248         if has_converter == True and detector.distance_unit != 'm': 
     181        if has_converter and detector.distance_unit != 'm': 
    249182            conv = Converter('m') 
    250183            distance = conv(distance, units=detector.distance_unit) 
     
    254187        output.sample.transmission = transmission 
    255188         
    256         # Store pixel size 
     189        # Store pixel size (mm) 
    257190        pixel = 5.0 
    258         if has_converter == True and detector.pixel_size_unit != 'mm': 
     191        if has_converter and detector.pixel_size_unit != 'mm': 
    259192            conv = Converter('mm') 
    260193            pixel = conv(pixel, units=detector.pixel_size_unit) 
     
    267200         
    268201        # Store limits of the image (2D array) 
    269         xmin = xmin - xstep / 2.0 
    270         xmax = xmax + xstep / 2.0 
    271         ymin = ymin - ystep / 2.0 
    272         ymax = ymax + ystep / 2.0 
    273         if has_converter == True and output.Q_unit != '1/A': 
     202        xmin -= xstep / 2.0 
     203        xmax += xstep / 2.0 
     204        ymin -= ystep / 2.0 
     205        ymax += ystep / 2.0 
     206        if has_converter and output.Q_unit != '1/A': 
    274207            xmin = data_conv_q(xmin, units=output.Q_unit) 
    275208            xmax = data_conv_q(xmax, units=output.Q_unit) 
     
    282215         
    283216        # Store x and y axis bin centers 
    284         output.x_bins = x 
    285         output.y_bins = y 
     217        output.x_bins = qx.tolist() 
     218        output.y_bins = qy.tolist() 
    286219         
    287220        # Units 
  • src/sas/sascalc/dataloader/readers/abs_reader.py

    rb699768 r9a5097c  
    99###################################################################### 
    1010 
    11 import numpy 
     11import numpy as np 
    1212import os 
    1313from sas.sascalc.dataloader.data_info import Data1D 
     
    5353                buff = input_f.read() 
    5454                lines = buff.split('\n') 
    55                 x  = numpy.zeros(0) 
    56                 y  = numpy.zeros(0) 
    57                 dy = numpy.zeros(0) 
    58                 dx = numpy.zeros(0) 
     55                x  = np.zeros(0) 
     56                y  = np.zeros(0) 
     57                dy = np.zeros(0) 
     58                dx = np.zeros(0) 
    5959                output = Data1D(x, y, dy=dy, dx=dx) 
    6060                detector = Detector() 
     
    204204                                _dy = data_conv_i(_dy, units=output.y_unit) 
    205205                            
    206                             x = numpy.append(x, _x) 
    207                             y = numpy.append(y, _y) 
    208                             dy = numpy.append(dy, _dy) 
    209                             dx = numpy.append(dx, _dx) 
     206                            x = np.append(x, _x) 
     207                            y = np.append(y, _y) 
     208                            dy = np.append(dy, _dy) 
     209                            dx = np.append(dx, _dx) 
    210210                             
    211211                        except: 
  • src/sas/sascalc/dataloader/readers/ascii_reader.py

    rd2471870 r9a5097c  
    1414 
    1515 
    16 import numpy 
     16import numpy as np 
    1717import os 
    1818from sas.sascalc.dataloader.data_info import Data1D 
     
    6969 
    7070                # Arrays for data storage 
    71                 tx = numpy.zeros(0) 
    72                 ty = numpy.zeros(0) 
    73                 tdy = numpy.zeros(0) 
    74                 tdx = numpy.zeros(0) 
     71                tx = np.zeros(0) 
     72                ty = np.zeros(0) 
     73                tdy = np.zeros(0) 
     74                tdx = np.zeros(0) 
    7575 
    7676                # The first good line of data will define whether 
     
    140140                            is_data == False: 
    141141                            try: 
    142                                 tx = numpy.zeros(0) 
    143                                 ty = numpy.zeros(0) 
    144                                 tdy = numpy.zeros(0) 
    145                                 tdx = numpy.zeros(0) 
     142                                tx = np.zeros(0) 
     143                                ty = np.zeros(0) 
     144                                tdy = np.zeros(0) 
     145                                tdx = np.zeros(0) 
    146146                            except: 
    147147                                pass 
    148148 
    149149                        if has_error_dy == True: 
    150                             tdy = numpy.append(tdy, _dy) 
     150                            tdy = np.append(tdy, _dy) 
    151151                        if has_error_dx == True: 
    152                             tdx = numpy.append(tdx, _dx) 
    153                         tx = numpy.append(tx, _x) 
    154                         ty = numpy.append(ty, _y) 
     152                            tdx = np.append(tdx, _dx) 
     153                        tx = np.append(tx, _x) 
     154                        ty = np.append(ty, _y) 
    155155 
    156156                        #To remember the # of columns on the current line 
     
    188188                #Let's re-order the data to make cal. 
    189189                # curve look better some cases 
    190                 ind = numpy.lexsort((ty, tx)) 
    191                 x = numpy.zeros(len(tx)) 
    192                 y = numpy.zeros(len(ty)) 
    193                 dy = numpy.zeros(len(tdy)) 
    194                 dx = numpy.zeros(len(tdx)) 
     190                ind = np.lexsort((ty, tx)) 
     191                x = np.zeros(len(tx)) 
     192                y = np.zeros(len(ty)) 
     193                dy = np.zeros(len(tdy)) 
     194                dx = np.zeros(len(tdx)) 
    195195                output = Data1D(x, y, dy=dy, dx=dx) 
    196196                self.filename = output.filename = basename 
     
    212212                output.y = y[x != 0] 
    213213                output.dy = dy[x != 0] if has_error_dy == True\ 
    214                     else numpy.zeros(len(output.y)) 
     214                    else np.zeros(len(output.y)) 
    215215                output.dx = dx[x != 0] if has_error_dx == True\ 
    216                     else numpy.zeros(len(output.x)) 
     216                    else np.zeros(len(output.x)) 
    217217 
    218218                output.xaxis("\\rm{Q}", 'A^{-1}') 
  • src/sas/sascalc/dataloader/readers/cansas_reader.py

    rc221349 r8434365  
    930930            self._write_data(datainfo, entry_node) 
    931931        # Transmission Spectrum Info 
    932         self._write_trans_spectrum(datainfo, entry_node) 
     932        # TODO: fix the writer to linearize all data, including T_spectrum 
     933        # self._write_trans_spectrum(datainfo, entry_node) 
    933934        # Sample info 
    934935        self._write_sample_info(datainfo, entry_node) 
  • src/sas/sascalc/dataloader/readers/cansas_reader_HDF5.py

    rd0764bf rc94280c  
    99import sys 
    1010 
    11 from sas.sascalc.dataloader.data_info import plottable_1D, plottable_2D, Data1D, Data2D, DataInfo, Process, Aperture 
    12 from sas.sascalc.dataloader.data_info import Collimation, TransmissionSpectrum, Detector 
     11from sas.sascalc.dataloader.data_info import plottable_1D, plottable_2D,\ 
     12    Data1D, Data2D, DataInfo, Process, Aperture, Collimation, \ 
     13    TransmissionSpectrum, Detector 
    1314from sas.sascalc.dataloader.data_info import combine_data_info_with_plottable 
    14  
    1515 
    1616 
    1717class Reader(): 
    1818    """ 
    19     A class for reading in CanSAS v2.0 data files. The existing iteration opens Mantid generated HDF5 formatted files 
    20     with file extension .h5/.H5. Any number of data sets may be present within the file and any dimensionality of data 
    21     may be used. Currently 1D and 2D SAS data sets are supported, but future implementations will include 1D and 2D 
    22     SESANS data. 
    23  
    24     Any number of SASdata sets may be present in a SASentry and the data within can be either 1D I(Q) or 2D I(Qx, Qy). 
     19    A class for reading in CanSAS v2.0 data files. The existing iteration opens 
     20    Mantid generated HDF5 formatted files with file extension .h5/.H5. Any 
     21    number of data sets may be present within the file and any dimensionality 
     22    of data may be used. Currently 1D and 2D SAS data sets are supported, but 
     23    future implementations will include 1D and 2D SESANS data. 
     24 
     25    Any number of SASdata sets may be present in a SASentry and the data within 
     26    can be either 1D I(Q) or 2D I(Qx, Qy). 
    2527 
    2628    Also supports reading NXcanSAS formatted HDF5 files 
     
    3032    """ 
    3133 
    32     ## CanSAS version 
     34    # CanSAS version 
    3335    cansas_version = 2.0 
    34     ## Logged warnings or messages 
     36    # Logged warnings or messages 
    3537    logging = None 
    36     ## List of errors for the current data set 
     38    # List of errors for the current data set 
    3739    errors = None 
    38     ## Raw file contents to be processed 
     40    # Raw file contents to be processed 
    3941    raw_data = None 
    40     ## Data info currently being read in 
     42    # Data info currently being read in 
    4143    current_datainfo = None 
    42     ## SASdata set currently being read in 
     44    # SASdata set currently being read in 
    4345    current_dataset = None 
    44     ## List of plottable1D objects that should be linked to the current_datainfo 
     46    # List of plottable1D objects that should be linked to the current_datainfo 
    4547    data1d = None 
    46     ## List of plottable2D objects that should be linked to the current_datainfo 
     48    # List of plottable2D objects that should be linked to the current_datainfo 
    4749    data2d = None 
    48     ## Data type name 
     50    # Data type name 
    4951    type_name = "CanSAS 2.0" 
    50     ## Wildcards 
     52    # Wildcards 
    5153    type = ["CanSAS 2.0 HDF5 Files (*.h5)|*.h5"] 
    52     ## List of allowed extensions 
     54    # List of allowed extensions 
    5355    ext = ['.h5', '.H5'] 
    54     ## Flag to bypass extension check 
     56    # Flag to bypass extension check 
    5557    allow_all = True 
    56     ## List of files to return 
     58    # List of files to return 
    5759    output = None 
    5860 
     
    6466        :return: List of Data1D/2D objects and/or a list of errors. 
    6567        """ 
    66         ## Reinitialize the class when loading a new data file to reset all class variables 
     68        # Reinitialize when loading a new data file to reset all class variables 
    6769        self.reset_class_variables() 
    68         ## Check that the file exists 
     70        # Check that the file exists 
    6971        if os.path.isfile(filename): 
    7072            basename = os.path.basename(filename) 
     
    7274            # If the file type is not allowed, return empty list 
    7375            if extension in self.ext or self.allow_all: 
    74                 ## Load the data file 
     76                # Load the data file 
    7577                self.raw_data = h5py.File(filename, 'r') 
    76                 ## Read in all child elements of top level SASroot 
     78                # Read in all child elements of top level SASroot 
    7779                self.read_children(self.raw_data, []) 
    78                 ## Add the last data set to the list of outputs 
     80                # Add the last data set to the list of outputs 
    7981                self.add_data_set() 
    80                 ## Close the data file 
     82                # Close the data file 
    8183                self.raw_data.close() 
    82         ## Return data set(s) 
     84        # Return data set(s) 
    8385        return self.output 
    8486 
     
    110112        """ 
    111113 
    112         ## Loop through each element of the parent and process accordingly 
     114        # Loop through each element of the parent and process accordingly 
    113115        for key in data.keys(): 
    114             ## Get all information for the current key 
     116            # Get all information for the current key 
    115117            value = data.get(key) 
    116118            if value.attrs.get(u'canSAS_class') is not None: 
     
    126128                self.parent_class = class_name 
    127129                parent_list.append(key) 
    128                 ## If this is a new sasentry, store the current data sets and create a fresh Data1D/2D object 
     130                # If a new sasentry, store the current data sets and create 
     131                # a fresh Data1D/2D object 
    129132                if class_prog.match(u'SASentry'): 
    130133                    self.add_data_set(key) 
    131134                elif class_prog.match(u'SASdata'): 
    132135                    self._initialize_new_data_set(parent_list) 
    133                 ## Recursion step to access data within the group 
     136                # Recursion step to access data within the group 
    134137                self.read_children(value, parent_list) 
    135138                self.add_intermediate() 
     
    137140 
    138141            elif isinstance(value, h5py.Dataset): 
    139                 ## If this is a dataset, store the data appropriately 
     142                # If this is a dataset, store the data appropriately 
    140143                data_set = data[key][:] 
    141144                unit = self._get_unit(value) 
    142145 
    143                 ## I and Q Data 
     146                # I and Q Data 
    144147                if key == u'I': 
    145                     if type(self.current_dataset) is plottable_2D: 
     148                    if isinstance(self.current_dataset, plottable_2D): 
    146149                        self.current_dataset.data = data_set 
    147150                        self.current_dataset.zaxis("Intensity", unit) 
     
    151154                    continue 
    152155                elif key == u'Idev': 
    153                     if type(self.current_dataset) is plottable_2D: 
     156                    if isinstance(self.current_dataset, plottable_2D): 
    154157                        self.current_dataset.err_data = data_set.flatten() 
    155158                    else: 
     
    158161                elif key == u'Q': 
    159162                    self.current_dataset.xaxis("Q", unit) 
    160                     if type(self.current_dataset) is plottable_2D: 
     163                    if isinstance(self.current_dataset, plottable_2D): 
    161164                        self.current_dataset.q = data_set.flatten() 
    162165                    else: 
     
    166169                    self.current_dataset.dx = data_set.flatten() 
    167170                    continue 
     171                elif key == u'dQw': 
     172                    self.current_dataset.dxw = data_set.flatten() 
     173                    continue 
     174                elif key == u'dQl': 
     175                    self.current_dataset.dxl = data_set.flatten() 
     176                    continue 
    168177                elif key == u'Qy': 
    169178                    self.current_dataset.yaxis("Q_y", unit) 
     
    183192                    self.current_dataset.mask = data_set.flatten() 
    184193                    continue 
     194                # Transmission Spectrum 
     195                elif (key == u'T' 
     196                      and self.parent_class == u'SAStransmission_spectrum'): 
     197                    self.trans_spectrum.transmission = data_set.flatten() 
     198                    continue 
     199                elif (key == u'Tdev' 
     200                      and self.parent_class == u'SAStransmission_spectrum'): 
     201                    self.trans_spectrum.transmission_deviation = \ 
     202                        data_set.flatten() 
     203                    continue 
     204                elif (key == u'lambda' 
     205                      and self.parent_class == u'SAStransmission_spectrum'): 
     206                    self.trans_spectrum.wavelength = data_set.flatten() 
     207                    continue 
    185208 
    186209                for data_point in data_set: 
    187                     ## Top Level Meta Data 
     210                    # Top Level Meta Data 
    188211                    if key == u'definition': 
    189212                        self.current_datainfo.meta_data['reader'] = data_point 
     
    201224                        self.current_datainfo.notes.append(data_point) 
    202225 
    203                     ## Sample Information 
    204                     elif key == u'Title' and self.parent_class == u'SASsample': # CanSAS 2.0 format 
     226                    # Sample Information 
     227                    # CanSAS 2.0 format 
     228                    elif key == u'Title' and self.parent_class == u'SASsample': 
    205229                        self.current_datainfo.sample.name = data_point 
    206                     elif key == u'ID' and self.parent_class == u'SASsample': # NXcanSAS format 
     230                    # NXcanSAS format 
     231                    elif key == u'name' and self.parent_class == u'SASsample': 
    207232                        self.current_datainfo.sample.name = data_point 
    208                     elif key == u'thickness' and self.parent_class == u'SASsample': 
     233                    # NXcanSAS format 
     234                    elif key == u'ID' and self.parent_class == u'SASsample': 
     235                        self.current_datainfo.sample.name = data_point 
     236                    elif (key == u'thickness' 
     237                          and self.parent_class == u'SASsample'): 
    209238                        self.current_datainfo.sample.thickness = data_point 
    210                     elif key == u'temperature' and self.parent_class == u'SASsample': 
     239                    elif (key == u'temperature' 
     240                          and self.parent_class == u'SASsample'): 
    211241                        self.current_datainfo.sample.temperature = data_point 
    212                     elif key == u'transmission' and self.parent_class == u'SASsample': 
     242                    elif (key == u'transmission' 
     243                          and self.parent_class == u'SASsample'): 
    213244                        self.current_datainfo.sample.transmission = data_point 
    214                     elif key == u'x_position' and self.parent_class == u'SASsample': 
     245                    elif (key == u'x_position' 
     246                          and self.parent_class == u'SASsample'): 
    215247                        self.current_datainfo.sample.position.x = data_point 
    216                     elif key == u'y_position' and self.parent_class == u'SASsample': 
     248                    elif (key == u'y_position' 
     249                          and self.parent_class == u'SASsample'): 
    217250                        self.current_datainfo.sample.position.y = data_point 
    218                     elif key == u'polar_angle' and self.parent_class == u'SASsample': 
     251                    elif key == u'pitch' and self.parent_class == u'SASsample': 
    219252                        self.current_datainfo.sample.orientation.x = data_point 
    220                     elif key == u'azimuthal_angle' and self.parent_class == u'SASsample': 
     253                    elif key == u'yaw' and self.parent_class == u'SASsample': 
     254                        self.current_datainfo.sample.orientation.y = data_point 
     255                    elif key == u'roll' and self.parent_class == u'SASsample': 
    221256                        self.current_datainfo.sample.orientation.z = data_point 
    222                     elif key == u'details' and self.parent_class == u'SASsample': 
     257                    elif (key == u'details' 
     258                          and self.parent_class == u'SASsample'): 
    223259                        self.current_datainfo.sample.details.append(data_point) 
    224260 
    225                     ## Instrumental Information 
    226                     elif key == u'name' and self.parent_class == u'SASinstrument': 
     261                    # Instrumental Information 
     262                    elif (key == u'name' 
     263                          and self.parent_class == u'SASinstrument'): 
    227264                        self.current_datainfo.instrument = data_point 
    228265                    elif key == u'name' and self.parent_class == u'SASdetector': 
     
    231268                        self.detector.distance = float(data_point) 
    232269                        self.detector.distance_unit = unit 
    233                     elif key == u'slit_length' and self.parent_class == u'SASdetector': 
     270                    elif (key == u'slit_length' 
     271                          and self.parent_class == u'SASdetector'): 
    234272                        self.detector.slit_length = float(data_point) 
    235273                        self.detector.slit_length_unit = unit 
    236                     elif key == u'x_position' and self.parent_class == u'SASdetector': 
     274                    elif (key == u'x_position' 
     275                          and self.parent_class == u'SASdetector'): 
    237276                        self.detector.offset.x = float(data_point) 
    238277                        self.detector.offset_unit = unit 
    239                     elif key == u'y_position' and self.parent_class == u'SASdetector': 
     278                    elif (key == u'y_position' 
     279                          and self.parent_class == u'SASdetector'): 
    240280                        self.detector.offset.y = float(data_point) 
    241281                        self.detector.offset_unit = unit 
    242                     elif key == u'polar_angle' and self.parent_class == u'SASdetector': 
     282                    elif (key == u'pitch' 
     283                          and self.parent_class == u'SASdetector'): 
    243284                        self.detector.orientation.x = float(data_point) 
    244285                        self.detector.orientation_unit = unit 
    245                     elif key == u'azimuthal_angle' and self.parent_class == u'SASdetector': 
     286                    elif key == u'roll' and self.parent_class == u'SASdetector': 
    246287                        self.detector.orientation.z = float(data_point) 
    247288                        self.detector.orientation_unit = unit 
    248                     elif key == u'beam_center_x' and self.parent_class == u'SASdetector': 
     289                    elif key == u'yaw' and self.parent_class == u'SASdetector': 
     290                        self.detector.orientation.y = float(data_point) 
     291                        self.detector.orientation_unit = unit 
     292                    elif (key == u'beam_center_x' 
     293                          and self.parent_class == u'SASdetector'): 
    249294                        self.detector.beam_center.x = float(data_point) 
    250295                        self.detector.beam_center_unit = unit 
    251                     elif key == u'beam_center_y' and self.parent_class == u'SASdetector': 
     296                    elif (key == u'beam_center_y' 
     297                          and self.parent_class == u'SASdetector'): 
    252298                        self.detector.beam_center.y = float(data_point) 
    253299                        self.detector.beam_center_unit = unit 
    254                     elif key == u'x_pixel_size' and self.parent_class == u'SASdetector': 
     300                    elif (key == u'x_pixel_size' 
     301                          and self.parent_class == u'SASdetector'): 
    255302                        self.detector.pixel_size.x = float(data_point) 
    256303                        self.detector.pixel_size_unit = unit 
    257                     elif key == u'y_pixel_size' and self.parent_class == u'SASdetector': 
     304                    elif (key == u'y_pixel_size' 
     305                          and self.parent_class == u'SASdetector'): 
    258306                        self.detector.pixel_size.y = float(data_point) 
    259307                        self.detector.pixel_size_unit = unit 
    260                     elif key == u'SSD' and self.parent_class == u'SAScollimation': 
     308                    elif (key == u'distance' 
     309                          and self.parent_class == u'SAScollimation'): 
    261310                        self.collimation.length = data_point 
    262311                        self.collimation.length_unit = unit 
    263                     elif key == u'name' and self.parent_class == u'SAScollimation': 
     312                    elif (key == u'name' 
     313                          and self.parent_class == u'SAScollimation'): 
    264314                        self.collimation.name = data_point 
    265  
    266                     ## Process Information 
    267                     elif key == u'name' and self.parent_class == u'SASprocess': 
     315                    elif (key == u'shape' 
     316                          and self.parent_class == u'SASaperture'): 
     317                        self.aperture.shape = data_point 
     318                    elif (key == u'x_gap' 
     319                          and self.parent_class == u'SASaperture'): 
     320                        self.aperture.size.x = data_point 
     321                    elif (key == u'y_gap' 
     322                          and self.parent_class == u'SASaperture'): 
     323                        self.aperture.size.y = data_point 
     324 
     325                    # Process Information 
     326                    elif (key == u'Title' 
     327                          and self.parent_class == u'SASprocess'): # CanSAS 2.0 
    268328                        self.process.name = data_point 
    269                     elif key == u'Title' and self.parent_class == u'SASprocess': # CanSAS 2.0 format 
     329                    elif (key == u'name' 
     330                          and self.parent_class == u'SASprocess'): # NXcanSAS 
    270331                        self.process.name = data_point 
    271                     elif key == u'name' and self.parent_class == u'SASprocess': # NXcanSAS format 
    272                         self.process.name = data_point 
    273                     elif key == u'description' and self.parent_class == u'SASprocess': 
     332                    elif (key == u'description' 
     333                          and self.parent_class == u'SASprocess'): 
    274334                        self.process.description = data_point 
    275335                    elif key == u'date' and self.parent_class == u'SASprocess': 
    276336                        self.process.date = data_point 
     337                    elif key == u'term' and self.parent_class == u'SASprocess': 
     338                        self.process.term = data_point 
    277339                    elif self.parent_class == u'SASprocess': 
    278340                        self.process.notes.append(data_point) 
    279341 
    280                     ## Transmission Spectrum 
    281                     elif key == u'T' and self.parent_class == u'SAStransmission_spectrum': 
    282                         self.trans_spectrum.transmission.append(data_point) 
    283                     elif key == u'Tdev' and self.parent_class == u'SAStransmission_spectrum': 
    284                         self.trans_spectrum.transmission_deviation.append(data_point) 
    285                     elif key == u'lambda' and self.parent_class == u'SAStransmission_spectrum': 
    286                         self.trans_spectrum.wavelength.append(data_point) 
    287  
    288                     ## Source 
    289                     elif key == u'wavelength' and self.parent_class == u'SASdata': 
     342                    # Source 
     343                    elif (key == u'wavelength' 
     344                          and self.parent_class == u'SASdata'): 
    290345                        self.current_datainfo.source.wavelength = data_point 
    291346                        self.current_datainfo.source.wavelength_unit = unit 
    292                     elif key == u'incident_wavelength' and self.parent_class == u'SASsource': 
     347                    elif (key == u'incident_wavelength' 
     348                          and self.parent_class == 'SASsource'): 
    293349                        self.current_datainfo.source.wavelength = data_point 
    294350                        self.current_datainfo.source.wavelength_unit = unit 
    295                     elif key == u'wavelength_max' and self.parent_class == u'SASsource': 
     351                    elif (key == u'wavelength_max' 
     352                          and self.parent_class == u'SASsource'): 
    296353                        self.current_datainfo.source.wavelength_max = data_point 
    297354                        self.current_datainfo.source.wavelength_max_unit = unit 
    298                     elif key == u'wavelength_min' and self.parent_class == u'SASsource': 
     355                    elif (key == u'wavelength_min' 
     356                          and self.parent_class == u'SASsource'): 
    299357                        self.current_datainfo.source.wavelength_min = data_point 
    300358                        self.current_datainfo.source.wavelength_min_unit = unit 
    301                     elif key == u'wavelength_spread' and self.parent_class == u'SASsource': 
    302                         self.current_datainfo.source.wavelength_spread = data_point 
    303                         self.current_datainfo.source.wavelength_spread_unit = unit 
    304                     elif key == u'beam_size_x' and self.parent_class == u'SASsource': 
     359                    elif (key == u'incident_wavelength_spread' 
     360                          and self.parent_class == u'SASsource'): 
     361                        self.current_datainfo.source.wavelength_spread = \ 
     362                            data_point 
     363                        self.current_datainfo.source.wavelength_spread_unit = \ 
     364                            unit 
     365                    elif (key == u'beam_size_x' 
     366                          and self.parent_class == u'SASsource'): 
    305367                        self.current_datainfo.source.beam_size.x = data_point 
    306368                        self.current_datainfo.source.beam_size_unit = unit 
    307                     elif key == u'beam_size_y' and self.parent_class == u'SASsource': 
     369                    elif (key == u'beam_size_y' 
     370                          and self.parent_class == u'SASsource'): 
    308371                        self.current_datainfo.source.beam_size.y = data_point 
    309372                        self.current_datainfo.source.beam_size_unit = unit 
    310                     elif key == u'beam_shape' and self.parent_class == u'SASsource': 
     373                    elif (key == u'beam_shape' 
     374                          and self.parent_class == u'SASsource'): 
    311375                        self.current_datainfo.source.beam_shape = data_point 
    312                     elif key == u'radiation' and self.parent_class == u'SASsource': 
     376                    elif (key == u'radiation' 
     377                          and self.parent_class == u'SASsource'): 
    313378                        self.current_datainfo.source.radiation = data_point 
    314                     elif key == u'transmission' and self.parent_class == u'SASdata': 
     379                    elif (key == u'transmission' 
     380                          and self.parent_class == u'SASdata'): 
    315381                        self.current_datainfo.sample.transmission = data_point 
    316382 
    317                     ## Everything else goes in meta_data 
     383                    # Everything else goes in meta_data 
    318384                    else: 
    319                         new_key = self._create_unique_key(self.current_datainfo.meta_data, key) 
     385                        new_key = self._create_unique_key( 
     386                            self.current_datainfo.meta_data, key) 
    320387                        self.current_datainfo.meta_data[new_key] = data_point 
    321388 
    322389            else: 
    323                 ## I don't know if this reachable code 
     390                # I don't know if this reachable code 
    324391                self.errors.add("ShouldNeverHappenException") 
    325392 
    326393    def add_intermediate(self): 
    327394        """ 
    328         This method stores any intermediate objects within the final data set after fully reading the set. 
    329  
    330         :param parent: The NXclass name for the h5py Group object that just finished being processed 
     395        This method stores any intermediate objects within the final data set 
     396        after fully reading the set. 
     397 
     398        :param parent: The NXclass name for the h5py Group object that just 
     399                       finished being processed 
    331400        """ 
    332401 
     
    347416            self.aperture = Aperture() 
    348417        elif self.parent_class == u'SASdata': 
    349             if type(self.current_dataset) is plottable_2D: 
     418            if isinstance(self.current_dataset, plottable_2D): 
    350419                self.data2d.append(self.current_dataset) 
    351             elif type(self.current_dataset) is plottable_1D: 
     420            elif isinstance(self.current_dataset, plottable_1D): 
    352421                self.data1d.append(self.current_dataset) 
    353422 
    354423    def final_data_cleanup(self): 
    355424        """ 
    356         Does some final cleanup and formatting on self.current_datainfo and all data1D and data2D objects and then 
    357         combines the data and info into Data1D and Data2D objects 
    358         """ 
    359  
    360         ## Type cast data arrays to float64 
     425        Does some final cleanup and formatting on self.current_datainfo and 
     426        all data1D and data2D objects and then combines the data and info into 
     427        Data1D and Data2D objects 
     428        """ 
     429 
     430        # Type cast data arrays to float64 
    361431        if len(self.current_datainfo.trans_spectrum) > 0: 
    362432            spectrum_list = [] 
     
    364434                spectrum.transmission = np.delete(spectrum.transmission, [0]) 
    365435                spectrum.transmission = spectrum.transmission.astype(np.float64) 
    366                 spectrum.transmission_deviation = np.delete(spectrum.transmission_deviation, [0]) 
    367                 spectrum.transmission_deviation = spectrum.transmission_deviation.astype(np.float64) 
     436                spectrum.transmission_deviation = np.delete( 
     437                    spectrum.transmission_deviation, [0]) 
     438                spectrum.transmission_deviation = \ 
     439                    spectrum.transmission_deviation.astype(np.float64) 
    368440                spectrum.wavelength = np.delete(spectrum.wavelength, [0]) 
    369441                spectrum.wavelength = spectrum.wavelength.astype(np.float64) 
     
    372444            self.current_datainfo.trans_spectrum = spectrum_list 
    373445 
    374         ## Append errors to dataset and reset class errors 
     446        # Append errors to dataset and reset class errors 
    375447        self.current_datainfo.errors = self.errors 
    376448        self.errors.clear() 
    377449 
    378         ## Combine all plottables with datainfo and append each to output 
    379         ## Type cast data arrays to float64 and find min/max as appropriate 
     450        # Combine all plottables with datainfo and append each to output 
     451        # Type cast data arrays to float64 and find min/max as appropriate 
    380452        for dataset in self.data2d: 
    381453            dataset.data = dataset.data.astype(np.float64) 
     
    397469            zeros = np.ones(dataset.data.size, dtype=bool) 
    398470            try: 
    399                 for i in range (0, dataset.mask.size - 1): 
     471                for i in range(0, dataset.mask.size - 1): 
    400472                    zeros[i] = dataset.mask[i] 
    401473            except: 
    402474                self.errors.add(sys.exc_value) 
    403475            dataset.mask = zeros 
    404             ## Calculate the actual Q matrix 
     476            # Calculate the actual Q matrix 
    405477            try: 
    406478                if dataset.q_data.size <= 1: 
    407                     dataset.q_data = np.sqrt(dataset.qx_data * dataset.qx_data + dataset.qy_data * dataset.qy_data) 
     479                    dataset.q_data = np.sqrt(dataset.qx_data 
     480                                             * dataset.qx_data 
     481                                             + dataset.qy_data 
     482                                             * dataset.qy_data) 
    408483            except: 
    409484                dataset.q_data = None 
     
    415490                dataset.data = dataset.data.flatten() 
    416491 
    417             final_dataset = combine_data_info_with_plottable(dataset, self.current_datainfo) 
     492            final_dataset = combine_data_info_with_plottable( 
     493                dataset, self.current_datainfo) 
    418494            self.output.append(final_dataset) 
    419495 
     
    435511            if dataset.dy is not None: 
    436512                dataset.dy = dataset.dy.astype(np.float64) 
    437             final_dataset = combine_data_info_with_plottable(dataset, self.current_datainfo) 
     513            final_dataset = combine_data_info_with_plottable( 
     514                dataset, self.current_datainfo) 
    438515            self.output.append(final_dataset) 
    439516 
    440517    def add_data_set(self, key=""): 
    441518        """ 
    442         Adds the current_dataset to the list of outputs after preforming final processing on the data and then calls a 
    443         private method to generate a new data set. 
     519        Adds the current_dataset to the list of outputs after preforming final 
     520        processing on the data and then calls a private method to generate a 
     521        new data set. 
    444522 
    445523        :param key: NeXus group name for current tree level 
     
    453531 
    454532 
    455     def _initialize_new_data_set(self, parent_list = None): 
    456         """ 
    457         A private class method to generate a new 1D or 2D data object based on the type of data within the set. 
    458         Outside methods should call add_data_set() to be sure any existing data is stored properly. 
     533    def _initialize_new_data_set(self, parent_list=None): 
     534        """ 
     535        A private class method to generate a new 1D or 2D data object based on 
     536        the type of data within the set. Outside methods should call 
     537        add_data_set() to be sure any existing data is stored properly. 
    459538 
    460539        :param parent_list: List of names of parent elements 
     
    473552    def _find_intermediate(self, parent_list, basename=""): 
    474553        """ 
    475         A private class used to find an entry by either using a direct key or knowing the approximate basename. 
    476  
    477         :param parent_list: List of parents to the current level in the HDF5 file 
     554        A private class used to find an entry by either using a direct key or 
     555        knowing the approximate basename. 
     556 
     557        :param parent_list: List of parents nodes in the HDF5 file 
    478558        :param basename: Approximate name of an entry to search for 
    479559        :return: 
     
    486566            top = top.get(parent) 
    487567        for key in top.keys(): 
    488             if (key_prog.match(key)): 
     568            if key_prog.match(key): 
    489569                entry = True 
    490570                break 
     
    516596        """ 
    517597        unit = value.attrs.get(u'units') 
    518         if unit == None: 
     598        if unit is None: 
    519599            unit = value.attrs.get(u'unit') 
    520         ## Convert the unit formats 
     600        # Convert the unit formats 
    521601        if unit == "1/A": 
    522602            unit = "A^{-1}" 
  • src/sas/sascalc/dataloader/readers/danse_reader.py

    rb699768 r9a5097c  
    1515import os 
    1616import sys 
    17 import numpy 
     17import numpy as np 
    1818import logging 
    1919from sas.sascalc.dataloader.data_info import Data2D, Detector 
     
    7979            output.detector.append(detector) 
    8080             
    81             output.data = numpy.zeros([size_x,size_y]) 
    82             output.err_data = numpy.zeros([size_x, size_y]) 
     81            output.data = np.zeros([size_x,size_y]) 
     82            output.err_data = np.zeros([size_x, size_y]) 
    8383             
    8484            data_conv_q = None 
  • src/sas/sascalc/dataloader/readers/hfir1d_reader.py

    rb699768 r9a5097c  
    99#copyright 2008, University of Tennessee 
    1010###################################################################### 
    11 import numpy 
     11import numpy as np 
    1212import os 
    1313from sas.sascalc.dataloader.data_info import Data1D 
     
    5252                buff = input_f.read() 
    5353                lines = buff.split('\n') 
    54                 x = numpy.zeros(0) 
    55                 y = numpy.zeros(0) 
    56                 dx = numpy.zeros(0) 
    57                 dy = numpy.zeros(0) 
     54                x = np.zeros(0) 
     55                y = np.zeros(0) 
     56                dx = np.zeros(0) 
     57                dy = np.zeros(0) 
    5858                output = Data1D(x, y, dx=dx, dy=dy) 
    5959                self.filename = output.filename = basename 
     
    8888                            _dy = data_conv_i(_dy, units=output.y_unit) 
    8989                                                     
    90                         x = numpy.append(x, _x) 
    91                         y = numpy.append(y, _y) 
    92                         dx = numpy.append(dx, _dx) 
    93                         dy = numpy.append(dy, _dy) 
     90                        x = np.append(x, _x) 
     91                        y = np.append(y, _y) 
     92                        dx = np.append(dx, _dx) 
     93                        dy = np.append(dy, _dy) 
    9494                    except: 
    9595                        # Couldn't parse this line, skip it  
  • src/sas/sascalc/dataloader/readers/red2d_reader.py

    rb699768 r9a5097c  
    1010###################################################################### 
    1111import os 
    12 import numpy 
     12import numpy as np 
    1313import math 
    1414from sas.sascalc.dataloader.data_info import Data2D, Detector 
     
    198198                break 
    199199        # Make numpy array to remove header lines using index 
    200         lines_array = numpy.array(lines) 
     200        lines_array = np.array(lines) 
    201201 
    202202        # index for lines_array 
    203         lines_index = numpy.arange(len(lines)) 
     203        lines_index = np.arange(len(lines)) 
    204204         
    205205        # get the data lines 
     
    225225 
    226226        # numpy array form 
    227         data_array = numpy.array(data_list1) 
     227        data_array = np.array(data_list1) 
    228228        # Redimesion based on the row_num and col_num, 
    229229        #otherwise raise an error. 
     
    235235        ## Get the all data: Let's HARDcoding; Todo find better way 
    236236        # Defaults 
    237         dqx_data = numpy.zeros(0) 
    238         dqy_data = numpy.zeros(0) 
    239         err_data = numpy.ones(row_num) 
    240         qz_data = numpy.zeros(row_num) 
    241         mask = numpy.ones(row_num, dtype=bool) 
     237        dqx_data = np.zeros(0) 
     238        dqy_data = np.zeros(0) 
     239        err_data = np.ones(row_num) 
     240        qz_data = np.zeros(row_num) 
     241        mask = np.ones(row_num, dtype=bool) 
    242242        # Get from the array 
    243243        qx_data = data_point[0] 
     
    254254            dqy_data = data_point[(5 + ver)] 
    255255        #if col_num > (6 + ver): mask[data_point[(6 + ver)] < 1] = False 
    256         q_data = numpy.sqrt(qx_data*qx_data+qy_data*qy_data+qz_data*qz_data) 
     256        q_data = np.sqrt(qx_data*qx_data+qy_data*qy_data+qz_data*qz_data) 
    257257            
    258258        # Extra protection(it is needed for some data files):  
     
    262262   
    263263        # Store limits of the image in q space 
    264         xmin = numpy.min(qx_data) 
    265         xmax = numpy.max(qx_data) 
    266         ymin = numpy.min(qy_data) 
    267         ymax = numpy.max(qy_data) 
     264        xmin = np.min(qx_data) 
     265        xmax = np.max(qx_data) 
     266        ymin = np.min(qy_data) 
     267        ymax = np.max(qy_data) 
    268268 
    269269        # units 
     
    287287         
    288288        # store x and y axis bin centers in q space 
    289         x_bins = numpy.arange(xmin, xmax + xstep, xstep) 
    290         y_bins = numpy.arange(ymin, ymax + ystep, ystep) 
     289        x_bins = np.arange(xmin, xmax + xstep, xstep) 
     290        y_bins = np.arange(ymin, ymax + ystep, ystep) 
    291291        
    292292        # get the limits of q values 
     
    300300        output.data = data 
    301301        if (err_data == 1).all(): 
    302             output.err_data = numpy.sqrt(numpy.abs(data)) 
     302            output.err_data = np.sqrt(np.abs(data)) 
    303303            output.err_data[output.err_data == 0.0] = 1.0 
    304304        else: 
     
    335335                # tranfer the comp. to cartesian coord. for newer version. 
    336336                if ver != 1: 
    337                     diag = numpy.sqrt(qx_data * qx_data + qy_data * qy_data) 
     337                    diag = np.sqrt(qx_data * qx_data + qy_data * qy_data) 
    338338                    cos_th = qx_data / diag 
    339339                    sin_th = qy_data / diag 
    340                     output.dqx_data = numpy.sqrt((dqx_data * cos_th) * \ 
     340                    output.dqx_data = np.sqrt((dqx_data * cos_th) * \ 
    341341                                                 (dqx_data * cos_th) \ 
    342342                                                 + (dqy_data * sin_th) * \ 
    343343                                                  (dqy_data * sin_th)) 
    344                     output.dqy_data = numpy.sqrt((dqx_data * sin_th) * \ 
     344                    output.dqy_data = np.sqrt((dqx_data * sin_th) * \ 
    345345                                                 (dqx_data * sin_th) \ 
    346346                                                 + (dqy_data * cos_th) * \ 
  • src/sas/sascalc/dataloader/readers/sesans_reader.py

    r7caf3e5 r9a5097c  
    66    Jurrian Bakker  
    77""" 
    8 import numpy 
     8import numpy as np 
    99import os 
    1010from sas.sascalc.dataloader.data_info import Data1D 
     
    6060                buff = input_f.read() 
    6161                lines = buff.splitlines() 
    62                 x  = numpy.zeros(0) 
    63                 y  = numpy.zeros(0) 
    64                 dy = numpy.zeros(0) 
    65                 lam  = numpy.zeros(0) 
    66                 dlam = numpy.zeros(0) 
    67                 dx = numpy.zeros(0) 
     62                x  = np.zeros(0) 
     63                y  = np.zeros(0) 
     64                dy = np.zeros(0) 
     65                lam  = np.zeros(0) 
     66                dlam = np.zeros(0) 
     67                dx = np.zeros(0) 
    6868                 
    6969               #temp. space to sort data 
    70                 tx  = numpy.zeros(0) 
    71                 ty  = numpy.zeros(0) 
    72                 tdy = numpy.zeros(0) 
    73                 tlam  = numpy.zeros(0) 
    74                 tdlam = numpy.zeros(0) 
    75                 tdx = numpy.zeros(0) 
     70                tx  = np.zeros(0) 
     71                ty  = np.zeros(0) 
     72                tdy = np.zeros(0) 
     73                tlam  = np.zeros(0) 
     74                tdlam = np.zeros(0) 
     75                tdx = np.zeros(0) 
    7676                output = Data1D(x=x, y=y, lam=lam, dy=dy, dx=dx, dlam=dlam, isSesans=True) 
    7777                self.filename = output.filename = basename 
     
    128128 
    129129                x,y,lam,dy,dx,dlam = [ 
    130                    numpy.asarray(v, 'double') 
     130                    np.asarray(v, 'double') 
    131131                   for v in (x,y,lam,dy,dx,dlam) 
    132132                ] 
  • src/sas/sascalc/dataloader/readers/tiff_reader.py

    rb699768 r9a5097c  
    1313import logging 
    1414import os 
    15 import numpy 
     15import numpy as np 
    1616from sas.sascalc.dataloader.data_info import Data2D 
    1717from sas.sascalc.dataloader.manipulations import reader2D_converter 
     
    5656 
    5757        # Initiazed the output data object 
    58         output.data = numpy.zeros([im.size[0], im.size[1]]) 
    59         output.err_data = numpy.zeros([im.size[0], im.size[1]]) 
    60         output.mask = numpy.ones([im.size[0], im.size[1]], dtype=bool) 
     58        output.data = np.zeros([im.size[0], im.size[1]]) 
     59        output.err_data = np.zeros([im.size[0], im.size[1]]) 
     60        output.mask = np.ones([im.size[0], im.size[1]], dtype=bool) 
    6161         
    6262        # Initialize 
     
    9494        output.x_bins = x_vals 
    9595        output.y_bins = y_vals 
    96         output.qx_data = numpy.array(x_vals) 
    97         output.qy_data = numpy.array(y_vals) 
     96        output.qx_data = np.array(x_vals) 
     97        output.qy_data = np.array(y_vals) 
    9898        output.xmin = 0 
    9999        output.xmax = im.size[0] - 1 
  • src/sas/sascalc/dataloader/loader.py

    rb699768 r270c882b  
    11""" 
    22    File handler to support different file extensions. 
    3     Uses reflectometry's registry utility. 
     3    Uses reflectometer registry utility. 
    44 
    55    The default readers are found in the 'readers' sub-module 
     
    2929# Default readers are defined in the readers sub-module 
    3030import readers 
     31from loader_exceptions import NoKnownLoaderException, FileContentsException 
    3132from readers import ascii_reader 
    3233from readers import cansas_reader 
     34from readers import cansas_reader_HDF5 
     35 
    3336 
    3437class Registry(ExtensionRegistry): 
     
    3740    Readers and writers are supported. 
    3841    """ 
    39  
    4042    def __init__(self): 
    4143        super(Registry, self).__init__() 
    4244 
    43         ## Writers 
     45        # Writers 
    4446        self.writers = {} 
    4547 
    46         ## List of wildcards 
     48        # List of wildcards 
    4749        self.wildcards = ['All (*.*)|*.*'] 
    4850 
    49         ## Creation time, for testing 
     51        # Creation time, for testing 
    5052        self._created = time.time() 
    5153 
     
    6163            of a particular reader 
    6264 
    63         Defaults to the ascii (multi-column) reader 
    64         if no reader was registered for the file's 
    65         extension. 
     65        Defaults to the ascii (multi-column), cansas XML, and cansas NeXuS 
     66        readers if no reader was registered for the file's extension. 
    6667        """ 
    6768        try: 
    6869            return super(Registry, self).load(path, format=format) 
    69         except: 
    70             try: 
    71                 # No reader was found. Default to the ascii reader. 
    72                 ascii_loader = ascii_reader.Reader() 
    73                 return ascii_loader.read(path) 
    74             except: 
    75                 cansas_loader = cansas_reader.Reader() 
    76                 return cansas_loader.read(path) 
     70        except NoKnownLoaderException as e: 
     71            pass  # try the ASCII reader 
     72        except FileContentsException as e: 
     73            pass 
     74        try: 
     75            ascii_loader = ascii_reader.Reader() 
     76            return ascii_loader.read(path) 
     77        except FileContentsException: 
     78            pass  # try the cansas XML reader 
     79        try: 
     80            cansas_loader = cansas_reader.Reader() 
     81            return cansas_loader.read(path) 
     82        except FileContentsException: 
     83            pass  # try the cansas NeXuS reader 
     84        try: 
     85            cansas_nexus_loader = cansas_reader_HDF5.Reader() 
     86            return cansas_nexus_loader.read(path) 
     87        except FileContentsException: 
     88            # No known reader available. Give up and throw an error 
     89            msg = "\n\tUnknown data format: %s.\n\tThe file is not a " % path 
     90            msg += "known format that can be loaded by SasView." 
     91            raise NoKnownLoaderException(msg) 
    7792 
    7893    def find_plugins(self, dir): 
Note: See TracChangeset for help on using the changeset viewer.