source: sasview/sansmodels/src/sans/models/BaseComponent.py @ 8ba103f

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 8ba103f was 25a608f5, checked in by Gervaise Alina <gervyh@…>, 16 years ago

add list of orientation _parameters to models

  • Property mode set to 100644
File size: 5.2 KB
Line 
1#!/usr/bin/env python
2"""
3    Provide base functionality for all model components
4"""
5
6# imports   
7import copy
8#TO DO: that about a way to make the parameter
9#is self return if it is fittable or not 
10
11class BaseComponent:
12    """
13        Basic model component
14       
15        Since version 0.5.0, basic operations are no longer supported.
16    """
17
18    def __init__(self):
19        """ Initialization"""
20       
21        ## Name of the model
22        self.name   = "BaseComponent"
23       
24        ## Parameters to be accessed by client
25        self.params = {}
26        self.details = {}
27        ## Dictionary used to store the dispersity/averaging
28        #  parameters of dispersed/averaged parameters.
29        self.dispersion = {}
30        # string containing information about the model such as the equation
31        #of the given model, exception or possible use
32        self.description=''
33        #list of parameter that can be fitted
34        self.fixed= []
35        ## parameters with orientation
36        self.orientation_params =[]
37           
38    def __str__(self):
39        """
40            @return: string representation
41        """
42        return self.name
43   
44    def is_fittable(self, par_name):
45        """
46            Check if a given parameter is fittable or not
47            @param par_name: the parameter name to check
48        """
49        return par_name.lower() in self.fixed
50        #For the future
51        #return self.params[str(par_name)].is_fittable()
52   
53    def run(self, x): return NotImplemented
54    def runXY(self, x): return NotImplemented 
55   
56    def clone(self):
57        """ Returns a new object identical to the current object """
58        obj = copy.deepcopy(self)
59        return self._clone(obj)
60   
61    def _clone(self, obj):
62        """
63            Internal utility function to copy the internal
64            data members to a fresh copy.
65        """
66        obj.params     = copy.deepcopy(self.params)
67        obj.details    = copy.deepcopy(self.details)
68        obj.dispersion = copy.deepcopy(self.dispersion)
69        return obj
70
71    def setParam(self, name, value):
72        """
73            Set the value of a model parameter
74       
75            @param name: name of the parameter
76            @param value: value of the parameter
77        """
78        # Look for dispersion parameters
79        toks = name.split('.')
80        if len(toks)==2:
81            for item in self.dispersion.keys():
82                if item.lower()==toks[0].lower():
83                    for par in self.dispersion[item]:
84                        if par.lower() == toks[1].lower():
85                            self.dispersion[item][par] = value
86                            return
87        else:
88            # Look for standard parameter
89            for item in self.params.keys():
90                if item.lower()==name.lower():
91                    self.params[item] = value
92                    return
93           
94        raise ValueError, "Model does not contain parameter %s" % name
95       
96    def getParam(self, name):
97        """
98            Set the value of a model parameter
99
100            @param name: name of the parameter
101        """
102        # Look for dispersion parameters
103        toks = name.split('.')
104        if len(toks)==2:
105            for item in self.dispersion.keys():
106                if item.lower()==toks[0].lower():
107                    for par in self.dispersion[item]:
108                        if par.lower() == toks[1].lower():
109                            return self.dispersion[item][par]
110        else:
111            # Look for standard parameter
112            for item in self.params.keys():
113                if item.lower()==name.lower():
114                    return self.params[item]
115           
116        raise ValueError, "Model does not contain parameter %s" % name
117     
118    def getParamList(self):
119        """
120            Return a list of all available parameters for the model
121        """ 
122        list = self.params.keys()
123        # WARNING: Extending the list with the dispersion parameters
124        list.extend(self.getDispParamList())
125        return list
126   
127    def getDispParamList(self):
128        """
129            Return a list of all available parameters for the model
130        """ 
131        list = []
132       
133        for item in self.dispersion.keys():
134            for p in self.dispersion[item].keys():
135                if p not in ['type']:
136                    list.append('%s.%s' % (item.lower(), p.lower()))
137                   
138        return list
139   
140    # Old-style methods that are no longer used
141    def setParamWithToken(self, name, value, token, member): return NotImplemented
142    def getParamWithToken(self, name, token, member): return NotImplemented
143    def getParamListWithToken(self, token, member): return NotImplemented
144    def __add__(self, other): raise ValueError, "Model operation are no longer supported"
145    def __sub__(self, other): raise ValueError, "Model operation are no longer supported"
146    def __mul__(self, other): raise ValueError, "Model operation are no longer supported"
147    def __div__(self, other): raise ValueError, "Model operation are no longer supported"
148       
Note: See TracBrowser for help on using the repository browser.