Changeset 952ea1f in sasview


Ignore:
Timestamp:
Oct 11, 2018 1:33:22 PM (5 years ago)
Author:
Paul Kienzle <pkienzle@…>
Branches:
master, magnetic_scatt, release-4.2.2, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249
Children:
7ba6470
Parents:
67ed543
git-author:
Paul Kienzle <pkienzle@…> (10/11/18 13:22:07)
git-committer:
Paul Kienzle <pkienzle@…> (10/11/18 13:33:22)
Message:

move c extensions from core/module.so to _module.so

Files:
3 deleted
8 edited

Legend:

Unmodified
Added
Removed
  • run.py

    rbc8b8a1 r952ea1f  
    6767 
    6868 
    69 def prepare(): 
     69def prepare(rebuild=True): 
    7070    # Don't create *.pyc files 
    7171    sys.dont_write_bytecode = True 
     
    9595    try: 
    9696        import periodictable 
    97     except: 
     97    except ImportError: 
    9898        addpath(joinpath(root, '..', 'periodictable')) 
    9999 
    100100    try: 
    101101        import bumps 
    102     except: 
     102    except ImportError: 
    103103        addpath(joinpath(root, '..', 'bumps')) 
    104104 
    105105    try: 
    106106        import tinycc 
    107     except: 
     107    except ImportError: 
    108108        addpath(joinpath(root, '../tinycc/build/lib')) 
    109109 
     
    111111    #addpath(os.path.join(root, '..','wxPython-src-3.0.0.0','wxPython')) 
    112112 
    113     # Build project if the build directory does not already exist. 
    114     # PAK: with "update" we can always build since it is fast 
    115     if True or not os.path.exists(build_path): 
     113    # Put the sas source tree on the path 
     114    addpath(joinpath(root, 'src')) 
     115 
     116    # Put sasmodels on the path 
     117    addpath(joinpath(root, '../sasmodels/')) 
     118 
     119    # Check if the C extensions are already built 
     120    try: 
     121        from sas.sascalc.pr import _pr_inversion 
     122        from sas.sascalc.calculator import _sld2i 
     123        from sas.sascalc.file_converter import _bsl_loader 
     124    except ImportError: 
     125        rebuild = True 
     126 
     127    # Build C extensions if necessary.  Do an inplace build to simplify path. 
     128    if rebuild: 
    116129        import subprocess 
    117         build_cmd = [sys.executable, "setup.py", "build", "update"] 
     130        build_cmd = [sys.executable, "setup.py", "build_ext", "--inplace", "update"] 
    118131        if os.name == 'nt': 
    119132            build_cmd.append('--compiler=tinycc') 
     
    122135        with cd(root): 
    123136            subprocess.call(build_cmd, shell=shell) 
    124  
    125     # Put the source trees on the path 
    126     addpath(joinpath(root, 'src')) 
    127  
    128     # sasmodels on the path 
    129     addpath(joinpath(root, '../sasmodels/')) 
    130  
    131     # The sas.models package Compiled Model files should be pulled in from the build directory even though 
    132     # the source is stored in src/sas/models. 
    133  
    134     # Compiled modules need to be pulled from the build directory. 
    135     # Some packages are not where they are needed, so load them explicitly. 
    136     import sas.sascalc.pr 
    137     sas.sascalc.pr.core = import_package('sas.sascalc.pr.core', 
    138                                          joinpath(build_path, 'sas', 'sascalc', 'pr', 'core')) 
    139  
    140     # Compiled modules need to be pulled from the build directory. 
    141     # Some packages are not where they are needed, so load them explicitly. 
    142     import sas.sascalc.file_converter 
    143     sas.sascalc.file_converter.core = import_package('sas.sascalc.file_converter.core', 
    144                                                      joinpath(build_path, 'sas', 'sascalc', 'file_converter', 'core')) 
    145  
    146     import sas.sascalc.calculator 
    147     sas.sascalc.calculator.core = import_package('sas.sascalc.calculator.core', 
    148                                                  joinpath(build_path, 'sas', 'sascalc', 'calculator', 'core')) 
    149  
    150     sys.path.append(build_path) 
    151137 
    152138    set_git_tag() 
  • setup.py

    rc16172d r952ea1f  
    1212import shutil 
    1313import sys 
    14 from distutils.command.build_ext import build_ext 
    15 from distutils.core import Command 
    1614 
    1715import numpy as np 
     16 
    1817from setuptools import Extension, setup 
     18from setuptools import Command 
     19from setuptools.command.build_ext import build_ext 
    1920 
    2021try: 
     
    2223except ImportError: 
    2324    pass 
     25 
     26# Convert "test" argument to "pytest" so 'python setup.py test' works 
     27sys.argv = [("pytest" if s == "test" else s) for s in sys.argv] 
    2428 
    2529# Manage version number ###################################### 
     
    246250# sas.sascalc.calculator 
    247251gen_dir = os.path.join("src", "sas", "sascalc", "calculator", "c_extensions") 
    248 package_dir["sas.sascalc.calculator.core"] = gen_dir 
    249252package_dir["sas.sascalc.calculator"] = os.path.join( 
    250253    "src", "sas", "sascalc", "calculator") 
    251 packages.extend(["sas.sascalc.calculator", "sas.sascalc.calculator.core"]) 
    252 ext_modules.append(Extension("sas.sascalc.calculator.core.sld2i", 
     254packages.append("sas.sascalc.calculator") 
     255ext_modules.append(Extension("sas.sascalc.calculator._sld2i", 
    253256                             sources=[ 
    254257                                 os.path.join(gen_dir, "sld2i_module.c"), 
     
    258261                             ], 
    259262                             include_dirs=[gen_dir], 
    260                              ) 
    261                    ) 
     263                             )) 
    262264 
    263265# sas.sascalc.pr 
    264266srcdir = os.path.join("src", "sas", "sascalc", "pr", "c_extensions") 
    265 package_dir["sas.sascalc.pr.core"] = srcdir 
    266267package_dir["sas.sascalc.pr"] = os.path.join("src", "sas", "sascalc", "pr") 
    267 packages.extend(["sas.sascalc.pr", "sas.sascalc.pr.core"]) 
    268 ext_modules.append(Extension("sas.sascalc.pr.core.pr_inversion", 
     268packages.append("sas.sascalc.pr") 
     269ext_modules.append(Extension("sas.sascalc.pr._pr_inversion", 
    269270                             sources=[os.path.join(srcdir, "Cinvertor.c"), 
    270271                                      os.path.join(srcdir, "invertor.c"), 
     
    276277# sas.sascalc.file_converter 
    277278mydir = os.path.join("src", "sas", "sascalc", "file_converter", "c_ext") 
    278 package_dir["sas.sascalc.file_converter.core"] = mydir 
    279279package_dir["sas.sascalc.file_converter"] = os.path.join( 
    280280    "src", "sas", "sascalc", "file_converter") 
    281 packages.extend(["sas.sascalc.file_converter", 
    282                  "sas.sascalc.file_converter.core"]) 
    283 ext_modules.append(Extension("sas.sascalc.file_converter.core.bsl_loader", 
     281packages.append("sas.sascalc.file_converter") 
     282ext_modules.append(Extension("sas.sascalc.file_converter._bsl_loader", 
    284283                             sources=[os.path.join(mydir, "bsl_loader.c")], 
    285284                             include_dirs=[np.get_include()], 
     
    443442    cmdclass={'build_ext': build_ext_subclass, 
    444443              'docs': BuildSphinxCommand, 
    445               'disable_openmp': DisableOpenMPCommand} 
     444              'disable_openmp': DisableOpenMPCommand}, 
     445    setup_requires=['pytest-runner'] if 'pytest' in sys.argv else [], 
     446    tests_require=['pytest'], 
    446447) 
  • src/sas/sascalc/calculator/c_extensions/sld2i_module.c

    ra1daf86 r952ea1f  
    22  SLD2I module to perform point and I calculations 
    33 */ 
     4#include <stdio.h> 
     5 
     6//#define Py_LIMITED_API 0x03020000 
    47#include <Python.h> 
    5 #include <stdio.h> 
     8 
    69#include "sld2i.h" 
    710 
     
    160163 
    161164#define MODULE_DOC "Sld2i C Library" 
    162 #define MODULE_NAME "sld2i" 
    163 #define MODULE_INIT2 initsld2i 
    164 #define MODULE_INIT3 PyInit_sld2i 
     165#define MODULE_NAME "_sld2i" 
     166#define MODULE_INIT2 init_sld2i 
     167#define MODULE_INIT3 PyInit__sld2i 
    165168#define MODULE_METHODS module_methods 
    166169 
  • src/sas/sascalc/calculator/sas_gen.py

    r144e032a r952ea1f  
    1414import numpy as np 
    1515 
    16 from .core import sld2i as mod 
     16from . import _sld2i 
    1717from .BaseComponent import BaseComponent 
    1818 
     
    145145            self.params['Up_frac_out'], 
    146146            self.params['Up_theta']) 
    147         model = mod.new_GenI(*args) 
     147        model = _sld2i.new_GenI(*args) 
    148148        if len(qy): 
    149149            qx, qy = _vec(qx), _vec(qy) 
    150150            I_out = np.empty_like(qx) 
    151151            #print("npoints", qx.shape, "npixels", pos_x.shape) 
    152             mod.genicomXY(model, qx, qy, I_out) 
     152            _sld2i.genicomXY(model, qx, qy, I_out) 
    153153            #print("I_out after", I_out) 
    154154        else: 
    155155            qx = _vec(qx) 
    156156            I_out = np.empty_like(qx) 
    157             mod.genicom(model, qx, I_out) 
     157            _sld2i.genicom(model, qx, I_out) 
    158158        vol_correction = self.data_total_volume / self.params['total_volume'] 
    159159        result = (self.params['scale'] * vol_correction * I_out 
     
    304304                z_dir2 *= z_dir2 
    305305                mask = (x_dir2 + y_dir2 + z_dir2) <= 1.0 
    306             except Exception: 
    307                 logger.error(sys.exc_value) 
     306            except Exception as exc: 
     307                logger.error(exc) 
    308308        self.output = MagSLD(self.pos_x[mask], self.pos_y[mask], 
    309309                             self.pos_z[mask], self.sld_n[mask], 
     
    600600                        y_lines.append(y_line) 
    601601                        z_lines.append(z_line) 
    602                 except Exception: 
    603                     logger.error(sys.exc_value) 
     602                except Exception as exc: 
     603                    logger.error(exc) 
    604604 
    605605            output = MagSLD(pos_x, pos_y, pos_z, sld_n, sld_mx, sld_my, sld_mz) 
     
    691691                            _vol_pix = float(toks[7]) 
    692692                            vol_pix = np.append(vol_pix, _vol_pix) 
    693                         except Exception: 
     693                        except Exception as exc: 
    694694                            vol_pix = None 
    695                     except Exception: 
     695                    except Exception as exc: 
    696696                        # Skip non-data lines 
    697                         logger.error(sys.exc_value) 
     697                        logger.error(exc) 
    698698            output = MagSLD(pos_x, pos_y, pos_z, sld_n, 
    699699                            sld_mx, sld_my, sld_mz) 
  • src/sas/sascalc/file_converter/bsl_loader.py

    rf00691d4 r952ea1f  
    1 from sas.sascalc.file_converter.core.bsl_loader import CLoader 
     1from sas.sascalc.file_converter._bsl_loader import CLoader 
    22from sas.sascalc.dataloader.data_info import Data2D 
    33from copy import deepcopy 
     
    6767                    'swap_bytes': int(metadata[3]) 
    6868                } 
    69             except: 
     69            except Exception: 
    7070                is_valid = False 
    7171                err_msg = "Invalid metadata in header file for {}" 
  • src/sas/sascalc/file_converter/c_ext/bsl_loader.c

    rd5aeaa3 r952ea1f  
     1#include <stdio.h> 
     2#include <stdlib.h> 
     3 
     4//#define Py_LIMITED_API 0x03020000 
    15#include <Python.h> 
     6#include <structmember.h> 
    27#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION 
    38#include <numpy/arrayobject.h> 
    4 #include <stdio.h> 
    5 #include <stdlib.h> 
    6 #include "structmember.h" 
     9 
    710#include "bsl_loader.h" 
    811 
     
    292295 
    293296#define MODULE_DOC "C module for loading bsl." 
    294 #define MODULE_NAME "bsl_loader" 
    295 #define MODULE_INIT2 initbsl_loader 
    296 #define MODULE_INIT3 PyInit_bsl_loader 
     297#define MODULE_NAME "_bsl_loader" 
     298#define MODULE_INIT2 init_bsl_loader 
     299#define MODULE_INIT3 PyInit__bsl_loader 
    297300#define MODULE_METHODS module_methods 
    298301 
  • src/sas/sascalc/pr/c_extensions/Cinvertor.c

    ra52f32f r952ea1f  
    55 * 
    66 */ 
    7 #include <Python.h> 
    8 #include "structmember.h" 
    97#include <stdio.h> 
    108#include <stdlib.h> 
     
    1210#include <time.h> 
    1311 
     12//#define Py_LIMITED_API 0x03050000 
     13#include <Python.h> 
     14#include <structmember.h> 
     15 
    1416#include "invertor.h" 
    15  
    1617 
    1718/// Error object for raised exceptions 
     
    11211122 
    11221123#define MODULE_DOC "C extension module for inversion to P(r)." 
    1123 #define MODULE_NAME "pr_inversion" 
    1124 #define MODULE_INIT2 initpr_inversion 
    1125 #define MODULE_INIT3 PyInit_pr_inversion 
     1124#define MODULE_NAME "_pr_inversion" 
     1125#define MODULE_INIT2 init_pr_inversion 
     1126#define MODULE_INIT3 PyInit__pr_inversion 
    11261127#define MODULE_METHODS module_methods 
    11271128 
  • src/sas/sascalc/pr/invertor.py

    r2469df7 r952ea1f  
    1717from numpy.linalg import lstsq 
    1818from scipy import optimize 
    19 from sas.sascalc.pr.core.pr_inversion import Cinvertor 
     19from sas.sascalc.pr._pr_inversion import Cinvertor 
    2020 
    2121logger = logging.getLogger(__name__) 
     
    7171        A[j][i] = (Fourier transformed base function for point j) 
    7272 
    73     We them choose a number of r-points, n_r, to evaluate the second 
     73    We then choose a number of r-points, n_r, to evaluate the second 
    7474    derivative of P(r) at. This is used as our regularization term. 
    7575    For a vector r of length n_r, the following n_r rows are set to :: 
     
    144144        x, y, err, d_max, q_min, q_max and alpha 
    145145        """ 
    146         if   name == 'x': 
     146        if name == 'x': 
    147147            if 0.0 in value: 
    148148                msg = "Invertor: one of your q-values is zero. " 
     
    268268            A[i][j] = (Fourier transformed base function for point j) 
    269269 
    270         We them choose a number of r-points, n_r, to evaluate the second 
     270        We then choose a number of r-points, n_r, to evaluate the second 
    271271        derivative of P(r) at. This is used as our regularization term. 
    272272        For a vector r of length n_r, the following n_r rows are set to :: 
     
    416416            A[i][j] = (Fourier transformed base function for point j) 
    417417 
    418         We them choose a number of r-points, n_r, to evaluate the second 
     418        We then choose a number of r-points, n_r, to evaluate the second 
    419419        derivative of P(r) at. This is used as our regularization term. 
    420420        For a vector r of length n_r, the following n_r rows are set to :: 
     
    473473 
    474474        # Perform the inversion (least square fit) 
    475         c, chi2, _, _ = lstsq(a, b) 
     475        c, chi2, _, _ = lstsq(a, b, rcond=None) 
    476476        # Sanity check 
    477477        try: 
     
    497497            cov = np.linalg.pinv(inv_cov) 
    498498            err = math.fabs(chi2 / float(npts - nfunc)) * cov 
    499         except: 
     499        except Exception as exc: 
    500500            # We were not able to estimate the errors 
    501501            # Return an empty error matrix 
    502             logger.error(sys.exc_value) 
     502            logger.error(exc) 
    503503 
    504504        # Keep a copy of the last output 
     
    537537 
    538538        """ 
    539         from num_term import NTermEstimator 
     539        from .num_term import NTermEstimator 
    540540        estimator = NTermEstimator(self.clone()) 
    541541        try: 
    542542            return estimator.num_terms(isquit_func) 
    543         except: 
     543        except Exception as exc: 
    544544            # If we fail, estimate alpha and return the default 
    545545            # number of terms 
    546546            best_alpha, _, _ = self.estimate_alpha(self.nfunc) 
    547             logger.warning("Invertor.estimate_numterms: %s" % sys.exc_value) 
     547            logger.warning("Invertor.estimate_numterms: %s" % exc) 
    548548            return self.nfunc, best_alpha, "Could not estimate number of terms" 
    549549 
     
    631631                return best_alpha, message, elapsed 
    632632 
    633         except: 
    634             message = "Invertor.estimate_alpha: %s" % sys.exc_value 
     633        except Exception as exc: 
     634            message = "Invertor.estimate_alpha: %s" % exc 
    635635            return 0, message, elapsed 
    636636 
     
    748748                        self.cov[i][i] = float(toks2[1]) 
    749749 
    750             except: 
    751                 msg = "Invertor.from_file: corrupted file\n%s" % sys.exc_value 
     750            except Exception as exc: 
     751                msg = "Invertor.from_file: corrupted file\n%s" % exc 
    752752                raise RuntimeError(msg) 
    753753        else: 
Note: See TracChangeset for help on using the changeset viewer.