source: sasview/sansmodels/src/sans/models/BaseModel.py @ 3aab96b

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 3aab96b was 79ac6f8, checked in by Gervaise Alina <gervyh@…>, 15 years ago

working on documentation

  • Property mode set to 100644
File size: 3.8 KB
RevLine 
[1b162dfa]1#!/usr/bin/env python
2
[79ac6f8]3############################################################################
4#This software was developed by the University of Tennessee as part of the
5#Distributed Data Analysis of Neutron Scattering Experiments (DANSE)
6#project funded by the US National Science Foundation.
7#
8#If you use DANSE applications to do scientific research that leads to
9#publication, we ask that you acknowledge the use of the software with the
10#following sentence:
11#
12#"This work benefited from DANSE software developed
13# under NSF award DMR-0520547."
14#
15#copyright 2008, University of Tennessee
16#############################################################################
[1b162dfa]17
[79ac6f8]18"""
19Provide base functionality for all model components
[1b162dfa]20
[79ac6f8]21The following has changed since going from BaseComponent to BaseModel:
[1b162dfa]22
[79ac6f8]23    - Arithmetic operation between models is no longer supported.
24      It was found to be of little use and not very flexible.
[1b162dfa]25   
[79ac6f8]26    - Parameters are now stored as Parameter object to provide
27      the necessary extra information like limits, units, etc...
[1b162dfa]28"""
29
30# imports   
31import copy
32   
33class Parameter(object):
34    """
[79ac6f8]35    Parameter class
[1b162dfa]36    """
37    name  = ''
38    value = 0.0
39    def __init__(self, name, value):
40        self.name = name
41        self.value = value
42       
[8809e48]43    def __str__(self):
44        return "%s: %g" % (self.name, self.value)
[1b162dfa]45
46class ParameterProperty(object):
47    """
[79ac6f8]48    Parameter property allow direct access to
49    the parameter values
[1b162dfa]50    """
51    def __init__(self,name,**kw):
52        self.name = name
53       
54    def __get__(self,instance,cls):
55        return instance.parameters[self.name].value
56
57    def __set__(self,instance,value):
58        instance.parameters[self.name].value = value
59
60
61   
62from ModelAdaptor import ModelAdaptor
63
64class BaseModel(ModelAdaptor):
65    """
[79ac6f8]66    Basic model component
[1b162dfa]67    """
68    ## Name of the model
69    name = "BaseModel"
[ae60f86]70   
71    def __init__(self):
72        ## Dictionary of Parameter objects
73        self.parameters = {}
[1b162dfa]74
75    # Evaluation methods to be implemented by the models
76    def run(self, x=0):  return NotImplemented
77    def runXY(self, x=0):  return NotImplemented
[f9bf661]78    def calculate_ER(self):  return NotImplemented
[1b162dfa]79    def __call__(self, x=0):
80        """     
[79ac6f8]81        Evaluate the model. Equivalent to runXY(x)
82       
83        :param x: input value
84       
85        :return: value of the model
86       
[1b162dfa]87        """
88        return self.runXY(x)
89   
90    def clone(self):
91        """ Returns a new object identical to the current object """
92       
93        obj = copy.deepcopy(self)
94        obj.params = copy.deepcopy(self.params)
95        obj.details = copy.deepcopy(self.details)
96        return obj
97   
98    def setParam(self, name, value):
99        """
[79ac6f8]100        Set the value of a model parameter
101   
102        :param name: name of the parameter
103        :param value: value of the parameter
[1b162dfa]104       
105        """
106        # Lowercase for case insensitivity
107        name = name.lower()
[ae60f86]108        if name in self.parameters:
109            print "found"
[8809e48]110            #self.parameters[name].value = value
[ae60f86]111        return object.__setattr__(self, name, value)
[1b162dfa]112       
113    def getParam(self, name):
114        """
[79ac6f8]115        Set the value of a model parameter
[1b162dfa]116
[79ac6f8]117        :param name: name of the parameter
118        :param value: value of the parameter
119       
[1b162dfa]120        """
121        # Lowercase for case insensitivity
122        name = name.lower()
123        return object.__getattribute__(self, name)
124       
125    def getParamList(self):
126        """
[79ac6f8]127        Return a list of all available parameters for the model
[1b162dfa]128        """
129        param_list = self.params.keys()
130        return param_list
131       
132   
133if __name__ == "__main__":
134    b = BaseModel() 
135    print b.operateOn
136   
Note: See TracBrowser for help on using the repository browser.