source: sasview/src/sans/models/BaseModel.py @ 6b20c4e

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 6b20c4e was 5777106, checked in by Mathieu Doucet <doucetm@…>, 11 years ago

Moving things around. Will definitely not build.

  • Property mode set to 100644
File size: 3.8 KB
Line 
1#!/usr/bin/env python
2
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#############################################################################
17
18"""
19Provide base functionality for all model components
20
21The following has changed since going from BaseComponent to BaseModel:
22
23    - Arithmetic operation between models is no longer supported.
24      It was found to be of little use and not very flexible.
25   
26    - Parameters are now stored as Parameter object to provide
27      the necessary extra information like limits, units, etc...
28"""
29
30# imports   
31import copy
32   
33class Parameter(object):
34    """
35    Parameter class
36    """
37    name  = ''
38    value = 0.0
39    def __init__(self, name, value):
40        self.name = name
41        self.value = value
42       
43    def __str__(self):
44        return "%s: %g" % (self.name, self.value)
45
46class ParameterProperty(object):
47    """
48    Parameter property allow direct access to
49    the parameter values
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    """
66    Basic model component
67    """
68    ## Name of the model
69    name = "BaseModel"
70   
71    def __init__(self):
72        ## Dictionary of Parameter objects
73        self.parameters = {}
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
78    def calculate_ER(self):  return NotImplemented
79    def __call__(self, x=0):
80        """     
81        Evaluate the model. Equivalent to runXY(x)
82       
83        :param x: input value
84       
85        :return: value of the model
86       
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        """
100        Set the value of a model parameter
101   
102        :param name: name of the parameter
103        :param value: value of the parameter
104       
105        """
106        # Lowercase for case insensitivity
107        name = name.lower()
108        if name in self.parameters:
109            print "found"
110            #self.parameters[name].value = value
111        return object.__setattr__(self, name, value)
112       
113    def getParam(self, name):
114        """
115        Set the value of a model parameter
116
117        :param name: name of the parameter
118        :param value: value of the parameter
119       
120        """
121        # Lowercase for case insensitivity
122        name = name.lower()
123        return object.__getattribute__(self, name)
124       
125    def getParamList(self):
126        """
127        Return a list of all available parameters for the model
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.