Changes in / [68b8734:3b12dea] in sasmodels


Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • sasmodels/sasview_model.py

    rde97440 r2622b3f  
    2323from . import core 
    2424 
    25 def standard_models(): 
    26     return [make_class(model_name) for model_name in core.list_models()] 
    27  
    28 def make_class(model_name, namestyle='name'): 
     25def make_class(model_info, dtype='single', namestyle='name'): 
    2926    """ 
    3027    Load the sasview model defined in *kernel_module*. 
    3128 
    32     Returns a class that can be used directly as a sasview model.t 
     29    Returns a class that can be used directly as a sasview model. 
    3330 
    3431    Defaults to using the new name for a model.  Setting 
     
    3633    compatible with SasView. 
    3734    """ 
    38     model_info = core.load_model_info(model_name) 
     35    model = core.build_model(model_info, dtype=dtype) 
    3936    def __init__(self, multfactor=1): 
    40         SasviewModel.__init__(self, model_info) 
     37        SasviewModel.__init__(self, model) 
    4138    attrs = dict(__init__=__init__) 
    42     ConstructedModel = type(model_info[namestyle], (SasviewModel,), attrs) 
     39    ConstructedModel = type(model.info[namestyle], (SasviewModel,), attrs) 
    4340    return ConstructedModel 
    4441 
     
    4744    Sasview wrapper for opencl/ctypes model. 
    4845    """ 
    49     def __init__(self, model_info): 
    50         self._model_info = model_info 
    51         self._kernel = None 
    52  
    53         self.name = model_info['name'] 
    54         self.oldname = model_info['oldname'] 
    55         self.description = model_info['description'] 
     46    def __init__(self, model): 
     47        """Initialization""" 
     48        self._model = model 
     49 
     50        self.name = model.info['name'] 
     51        self.oldname = model.info['oldname'] 
     52        self.description = model.info['description'] 
    5653        self.category = None 
    5754        self.multiplicity_info = None 
     
    6360        self.params = collections.OrderedDict() 
    6461        self.dispersion = dict() 
    65         partype = model_info['partype'] 
    66  
    67         for p in model_info['parameters']: 
     62        partype = model.info['partype'] 
     63 
     64        for p in model.info['parameters']: 
    6865            self.params[p.name] = p.default 
    6966            self.details[p.name] = [p.units] + p.limits 
     
    8683 
    8784        ## independent parameter name and unit [string] 
    88         self.input_name = model_info.get("input_name", "Q") 
    89         self.input_unit = model_info.get("input_unit", "A^{-1}") 
    90         self.output_name = model_info.get("output_name", "Intensity") 
    91         self.output_unit = model_info.get("output_unit", "cm^{-1}") 
     85        self.input_name = model.info.get("input_name", "Q") 
     86        self.input_unit = model.info.get("input_unit", "A^{-1}") 
     87        self.output_name = model.info.get("output_name", "Intensity") 
     88        self.output_unit = model.info.get("output_unit", "cm^{-1}") 
    9289 
    9390        ## _persistency_dict is used by sas.perspectives.fitting.basepage 
     
    9895        ## New fields introduced for opencl rewrite 
    9996        self.cutoff = 1e-5 
    100  
    101     def __get_state__(self): 
    102         state = self.__dict__.copy() 
    103         model_id = self._model_info['id'] 
    104         state.pop('_kernel') 
    105         # May need to reload model info on set state since it has pointers 
    106         # to python implementations of Iq, etc. 
    107         #state.pop('_model_info') 
    108         return state 
    109  
    110     def __set_state__(self, state): 
    111         self.__dict__ = state 
    112         self._kernel = None 
    11397 
    11498    def __str__(self): 
     
    203187        # TODO: fix test so that parameter order doesn't matter 
    204188        ret = ['%s.%s' % (d.lower(), p) 
    205                for d in self._model_info['partype']['pd-2d'] 
     189               for d in self._model.info['partype']['pd-2d'] 
    206190               for p in ('npts', 'nsigmas', 'width')] 
    207191        #print(ret) 
     
    277261            # Check whether we have a list of ndarrays [qx,qy] 
    278262            qx, qy = qdist 
    279             partype = self._model_info['partype'] 
     263            partype = self._model.info['partype'] 
    280264            if not partype['orientation'] and not partype['magnetic']: 
    281265                return self.calculate_Iq(np.sqrt(qx ** 2 + qy ** 2)) 
     
    300284        to the card for each evaluation. 
    301285        """ 
    302         if self._kernel is None: 
    303             self._kernel = core.build_model(self._model_info) 
    304286        q_vectors = [np.asarray(q) for q in args] 
    305         fn = self._kernel(q_vectors) 
     287        fn = self._model(q_vectors) 
    306288        pars = [self.params[v] for v in fn.fixed_pars] 
    307289        pd_pars = [self._get_weights(p) for p in fn.pd_pars] 
     
    317299        :return: the value of the effective radius 
    318300        """ 
    319         ER = self._model_info.get('ER', None) 
     301        ER = self._model.info.get('ER', None) 
    320302        if ER is None: 
    321303            return 1.0 
     
    332314        :return: the value of the volf ratio 
    333315        """ 
    334         VR = self._model_info.get('VR', None) 
     316        VR = self._model.info.get('VR', None) 
    335317        if VR is None: 
    336318            return 1.0 
     
    376358        parameter set in the vector. 
    377359        """ 
    378         pars = self._model_info['partype']['volume'] 
     360        pars = self._model.info['partype']['volume'] 
    379361        return core.dispersion_mesh([self._get_weights(p) for p in pars]) 
    380362 
     
    386368        from . import weights 
    387369 
    388         relative = self._model_info['partype']['pd-rel'] 
    389         limits = self._model_info['limits'] 
     370        relative = self._model.info['partype']['pd-rel'] 
     371        limits = self._model.info['limits'] 
    390372        dis = self.dispersion[par] 
    391373        value, weight = weights.get_weights( 
     
    393375            self.params[par], limits[par], par in relative) 
    394376        return value, weight / np.sum(weight) 
    395  
Note: See TracChangeset for help on using the changeset viewer.