Ignore:
Timestamp:
Aug 29, 2018 10:01:23 AM (6 years ago)
Author:
Piotr Rozyczko <rozyczko@…>
Branches:
ESS_GUI, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc
Children:
9463ca2
Parents:
ce30949
git-author:
Piotr Rozyczko <rozyczko@…> (08/29/18 09:59:56)
git-committer:
Piotr Rozyczko <rozyczko@…> (08/29/18 10:01:23)
Message:

cherry picking sascalc changes from master SASVIEW-996
minor unit test fixes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/sas/sascalc/calculator/sas_gen.py

    rb58265c3 rb8080e1  
    118118        self.is_avg = is_avg 
    119119 
    120     def _gen(self, x, y, i): 
     120    def _gen(self, qx, qy): 
    121121        """ 
    122122        Evaluate the function 
     
    129129        pos_y = self.data_y 
    130130        pos_z = self.data_z 
    131         len_x = len(pos_x) 
    132131        if self.is_avg is None: 
    133             len_x *= -1 
    134132            pos_x, pos_y, pos_z = transform_center(pos_x, pos_y, pos_z) 
    135         len_q = len(x) 
    136133        sldn = copy.deepcopy(self.data_sldn) 
    137134        sldn -= self.params['solvent_SLD'] 
    138         model = mod.new_GenI(len_x, pos_x, pos_y, pos_z, 
    139                              sldn, self.data_mx, self.data_my, 
    140                              self.data_mz, self.data_vol, 
    141                              self.params['Up_frac_in'], 
    142                              self.params['Up_frac_out'], 
    143                              self.params['Up_theta']) 
    144         if y == []: 
    145             mod.genicom(model, len_q, x, i) 
    146         else: 
    147             mod.genicomXY(model, len_q, x, y, i) 
     135        # **** WARNING **** new_GenI holds pointers to numpy vectors 
     136        # be sure that they are contiguous double precision arrays and make  
     137        # sure the GC doesn't eat them before genicom is called. 
     138        # TODO: rewrite so that the parameters are passed directly to genicom 
     139        args = ( 
     140            (1 if self.is_avg else 0), 
     141            pos_x, pos_y, pos_z, 
     142            sldn, self.data_mx, self.data_my, 
     143            self.data_mz, self.data_vol, 
     144            self.params['Up_frac_in'], 
     145            self.params['Up_frac_out'], 
     146            self.params['Up_theta']) 
     147        model = mod.new_GenI(*args) 
     148        if len(qy): 
     149            qx, qy = _vec(qx), _vec(qy) 
     150            I_out = np.empty_like(qx) 
     151            #print("npoints", qx.shape, "npixels", pos_x.shape) 
     152            mod.genicomXY(model, qx, qy, I_out) 
     153            #print("I_out after", I_out) 
     154        else: 
     155            qx = _vec(qx) 
     156            I_out = np.empty_like(qx) 
     157            mod.genicom(model, qx, I_out) 
    148158        vol_correction = self.data_total_volume / self.params['total_volume'] 
    149         return  self.params['scale'] * vol_correction * i + \ 
    150                         self.params['background'] 
     159        result = (self.params['scale'] * vol_correction * I_out 
     160                  + self.params['background']) 
     161        return result 
    151162 
    152163    def set_sld_data(self, sld_data=None): 
     
    156167        self.sld_data = sld_data 
    157168        self.data_pos_unit = sld_data.pos_unit 
    158         self.data_x = sld_data.pos_x 
    159         self.data_y = sld_data.pos_y 
    160         self.data_z = sld_data.pos_z 
    161         self.data_sldn = sld_data.sld_n 
    162         self.data_mx = sld_data.sld_mx 
    163         self.data_my = sld_data.sld_my 
    164         self.data_mz = sld_data.sld_mz 
    165         self.data_vol = sld_data.vol_pix 
     169        self.data_x = _vec(sld_data.pos_x) 
     170        self.data_y = _vec(sld_data.pos_y) 
     171        self.data_z = _vec(sld_data.pos_z) 
     172        self.data_sldn = _vec(sld_data.sld_n) 
     173        self.data_mx = _vec(sld_data.sld_mx) 
     174        self.data_my = _vec(sld_data.sld_my) 
     175        self.data_mz = _vec(sld_data.sld_mz) 
     176        self.data_vol = _vec(sld_data.vol_pix) 
    166177        self.data_total_volume = sum(sld_data.vol_pix) 
    167178        self.params['total_volume'] = sum(sld_data.vol_pix) 
     
    180191        :return: (I value) 
    181192        """ 
    182         if x.__class__.__name__ == 'list': 
     193        if isinstance(x, list): 
    183194            if len(x[1]) > 0: 
    184195                msg = "Not a 1D." 
    185196                raise ValueError(msg) 
    186             i_out = np.zeros_like(x[0]) 
    187197            # 1D I is found at y =0 in the 2D pattern 
    188             out = self._gen(x[0], [], i_out) 
     198            out = self._gen(x[0], []) 
    189199            return out 
    190200        else: 
     
    199209        :Use this runXY() for the computation 
    200210        """ 
    201         if x.__class__.__name__ == 'list': 
    202             i_out = np.zeros_like(x[0]) 
    203             out = self._gen(x[0], x[1], i_out) 
    204             return out 
     211        if isinstance(x, list): 
     212            return self._gen(x[0], x[1]) 
    205213        else: 
    206214            msg = "Q must be given as list of qx's and qy's" 
     
    214222                      where qx,qy are 1D ndarrays (for 2D). 
    215223        """ 
    216         if qdist.__class__.__name__ == 'list': 
    217             if len(qdist[1]) < 1: 
    218                 out = self.run(qdist) 
    219             else: 
    220                 out = self.runXY(qdist) 
    221             return out 
     224        if isinstance(qdist, list): 
     225            return self.run(qdist) if len(qdist[1]) < 1 else self.runXY(qdist) 
    222226        else: 
    223227            mesg = "evalDistribution is expecting an ndarray of " 
    224228            mesg += "a list [qx,qy] where qx,qy are arrays." 
    225229            raise RuntimeError(mesg) 
     230 
     231def _vec(v): 
     232    return np.ascontiguousarray(v, 'd') 
    226233 
    227234class OMF2SLD(object): 
     
    10411048        self.line_z = line_z 
    10421049 
     1050def _get_data_path(*path_parts): 
     1051    from os.path import realpath, join as joinpath, dirname, abspath 
     1052    # in sas/sascalc/calculator;  want sas/sasview/test 
     1053    return joinpath(dirname(realpath(__file__)), 
     1054                    '..', '..', 'sasview', 'test', *path_parts) 
     1055 
    10431056def test_load(): 
    10441057    """ 
     
    10461059    """ 
    10471060    from mpl_toolkits.mplot3d import Axes3D 
    1048     current_dir = os.path.abspath(os.path.curdir) 
    1049     print(current_dir) 
    1050     for i in range(6): 
    1051         current_dir, _ = os.path.split(current_dir) 
    1052         tfile = os.path.join(current_dir, "test", "CoreXY_ShellZ.txt") 
    1053         ofile = os.path.join(current_dir, "test", "A_Raw_Example-1.omf") 
    1054         if os.path.isfile(tfile): 
    1055             tfpath = tfile 
    1056             ofpath = ofile 
    1057             break 
     1061    tfpath = _get_data_path("1d_data", "CoreXY_ShellZ.txt") 
     1062    ofpath = _get_data_path("coordinate_data", "A_Raw_Example-1.omf") 
     1063    if not os.path.isfile(tfpath) or not os.path.isfile(ofpath): 
     1064        raise ValueError("file(s) not found: %r, %r"%(tfpath, ofpath)) 
    10581065    reader = SLDReader() 
    10591066    oreader = OMFReader() 
    1060     output = decode(reader.read(tfpath)) 
    1061     ooutput = decode(oreader.read(ofpath)) 
     1067    output = reader.read(tfpath) 
     1068    ooutput = oreader.read(ofpath) 
    10621069    foutput = OMF2SLD() 
    10631070    foutput.set_data(ooutput) 
     
    10881095    plt.show() 
    10891096 
     1097def test_save(): 
     1098    ofpath = _get_data_path("coordinate_data", "A_Raw_Example-1.omf") 
     1099    if not os.path.isfile(ofpath): 
     1100        raise ValueError("file(s) not found: %r"%(ofpath,)) 
     1101    oreader = OMFReader() 
     1102    omfdata = oreader.read(ofpath) 
     1103    omf2sld = OMF2SLD() 
     1104    omf2sld.set_data(omfdata) 
     1105    writer = SLDReader() 
     1106    writer.write("out.txt", omf2sld.output) 
     1107 
    10901108def test(): 
    10911109    """ 
    10921110        Test code 
    10931111    """ 
    1094     current_dir = os.path.abspath(os.path.curdir) 
    1095     for i in range(3): 
    1096         current_dir, _ = os.path.split(current_dir) 
    1097         ofile = os.path.join(current_dir, "test", "A_Raw_Example-1.omf") 
    1098         if os.path.isfile(ofile): 
    1099             ofpath = ofile 
    1100             break 
     1112    ofpath = _get_data_path("coordinate_data", "A_Raw_Example-1.omf") 
     1113    if not os.path.isfile(ofpath): 
     1114        raise ValueError("file(s) not found: %r"%(ofpath,)) 
    11011115    oreader = OMFReader() 
    1102     ooutput = decode(oreader.read(ofpath)) 
    1103     foutput = OMF2SLD() 
    1104     foutput.set_data(ooutput) 
    1105     writer = SLDReader() 
    1106     writer.write(os.path.join(os.path.dirname(ofpath), "out.txt"), 
    1107                  foutput.output) 
     1116    omfdata = oreader.read(ofpath) 
     1117    omf2sld = OMF2SLD() 
     1118    omf2sld.set_data(omfdata) 
    11081119    model = GenSAS() 
    1109     model.set_sld_data(foutput.output) 
    1110     x = np.arange(1000)/10000. + 1e-5 
    1111     y = np.arange(1000)/10000. + 1e-5 
    1112     i = np.zeros(1000) 
    1113     model.runXY([x, y, i]) 
     1120    model.set_sld_data(omf2sld.output) 
     1121    x = np.linspace(0, 0.1, 11)[1:] 
     1122    return model.runXY([x, x]) 
    11141123 
    11151124if __name__ == "__main__": 
     1125    #test_load() 
     1126    #test_save() 
     1127    #print(test()) 
    11161128    test() 
    1117     test_load() 
Note: See TracChangeset for help on using the changeset viewer.