[ae3ce4e] | 1 | #!/usr/bin/env python |
---|
| 2 | """ Pyre Component Model factory |
---|
| 3 | Factory class that creates pyre component models |
---|
| 4 | |
---|
| 5 | @copyright: Mathieu Doucet (University of Tennessee), for the DANSE project |
---|
| 6 | """ |
---|
| 7 | #pylint: disable-msg=R0904 |
---|
| 8 | #pylint: disable-msg=R0912 |
---|
| 9 | #pylint: disable-msg=R0201 |
---|
| 10 | #pylint: disable-msg=W0142 |
---|
| 11 | |
---|
| 12 | from pyre.components.Component import Component |
---|
| 13 | |
---|
| 14 | def ScatteringIntensityFactory( ModelClass ): |
---|
| 15 | """ Returns the model if found |
---|
| 16 | @param name: class of the model [class] |
---|
| 17 | @return: the pyre component class [class] |
---|
| 18 | """ |
---|
| 19 | |
---|
| 20 | ## Model object to inspect to write the adapter |
---|
| 21 | model = ModelClass() |
---|
| 22 | |
---|
| 23 | class ScatteringModel(Component): |
---|
| 24 | """ |
---|
| 25 | Pyre component adapter class for the specified model |
---|
| 26 | """ |
---|
| 27 | has_changed = False |
---|
| 28 | |
---|
| 29 | class Inventory(Component.Inventory): |
---|
| 30 | """ |
---|
| 31 | Inventory class for the model adapter |
---|
| 32 | """ |
---|
| 33 | import pyre.inventory as pyre_inventory |
---|
| 34 | |
---|
| 35 | # Create the inventory items from the parameters |
---|
| 36 | # of the model |
---|
| 37 | for name in model.params: |
---|
| 38 | line = '%s=pyre_inventory.float( name, default = %g )' \ |
---|
| 39 | % (name, model.params[name]) |
---|
| 40 | exec line in locals() |
---|
| 41 | continue |
---|
| 42 | |
---|
| 43 | def __init__(self, name, facility="sans"): |
---|
| 44 | """ Initialize the component |
---|
| 45 | @param name: name of the component object |
---|
| 46 | @param facility: name of the facility for the object |
---|
| 47 | """ |
---|
| 48 | Component.__init__(self, name, facility) |
---|
| 49 | |
---|
| 50 | ## Model to be manipulated through the adapter |
---|
| 51 | self._engine = ModelClass() |
---|
| 52 | return |
---|
| 53 | |
---|
| 54 | |
---|
| 55 | def __call__(self, *args, **kwds): |
---|
| 56 | """ |
---|
| 57 | Evaluate the model |
---|
| 58 | """ |
---|
| 59 | return self._engine.run(args[0]) |
---|
| 60 | |
---|
| 61 | def _configure(self): |
---|
| 62 | """ |
---|
| 63 | Configure the component |
---|
| 64 | """ |
---|
| 65 | Component._configure(self) |
---|
| 66 | |
---|
| 67 | # Add the inventory items to the list of attributes |
---|
| 68 | for name in model.params: |
---|
| 69 | line = "self.%s = self.inventory.%s" % (name, name) |
---|
| 70 | exec line in locals() |
---|
| 71 | continue |
---|
| 72 | return |
---|
| 73 | |
---|
| 74 | |
---|
| 75 | def _init(self): |
---|
| 76 | """ |
---|
| 77 | Initialization of the underlying model |
---|
| 78 | """ |
---|
| 79 | Component._init(self) |
---|
| 80 | |
---|
| 81 | # Set the parameters of the underlying model |
---|
| 82 | for name in model.params: |
---|
| 83 | if hasattr(self, name): |
---|
| 84 | self.set(name, getattr(self, name)) |
---|
| 85 | else: |
---|
| 86 | raise ValueError, "%s not an attribute" % name |
---|
| 87 | |
---|
| 88 | return |
---|
| 89 | |
---|
| 90 | def set(self, key, value): |
---|
| 91 | """ |
---|
| 92 | Set a parameter of the underlying model |
---|
| 93 | """ |
---|
| 94 | |
---|
| 95 | # Check that the parameter to set is available |
---|
| 96 | if key not in model.params: |
---|
| 97 | raise ValueError, "Model doesn't have param %s" % key |
---|
| 98 | |
---|
| 99 | # Make use of pyre's validator to check the inputs |
---|
| 100 | cmd = "self.inventory.%s = value" % key |
---|
| 101 | exec cmd in locals() |
---|
| 102 | |
---|
| 103 | # Pass the parameter to the underlying model |
---|
| 104 | self._engine.setParam(key, value) |
---|
| 105 | self.has_changed = True |
---|
| 106 | |
---|
| 107 | def __setattr__(self, key, value): |
---|
| 108 | self.__dict__["has_changed"] = True |
---|
| 109 | return Component.__setattr__(self, key, value) |
---|
| 110 | |
---|
| 111 | def changed(self): |
---|
| 112 | tmp = self.has_changed |
---|
| 113 | self.has_changed = False |
---|
| 114 | return tmp |
---|
| 115 | |
---|
| 116 | def xaxis(self): |
---|
| 117 | return "time", "t", "s" |
---|
| 118 | def yaxis(self): |
---|
| 119 | return "amplitude", "A", "" |
---|
| 120 | |
---|
| 121 | |
---|
| 122 | |
---|
| 123 | return ScatteringModel |
---|