[79ac6f8] | 1 | |
---|
| 2 | ################################################################################ |
---|
| 3 | #This software was developed by the University of Tennessee as part of the |
---|
| 4 | #Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
| 5 | #project funded by the US National Science Foundation. |
---|
| 6 | # |
---|
| 7 | #If you use DANSE applications to do scientific research that leads to |
---|
| 8 | #publication, we ask that you acknowledge the use of the software with the |
---|
| 9 | #following sentence: |
---|
| 10 | # |
---|
| 11 | #"This work benefited from DANSE software developed under NSF award DMR-0520547." |
---|
| 12 | # |
---|
| 13 | #copyright 2008, University of Tennessee |
---|
| 14 | ################################################################################ |
---|
| 15 | |
---|
[af03ddd] | 16 | """ |
---|
[79ac6f8] | 17 | Class definitions for python dispersion model for |
---|
| 18 | model parameters. These classes are bridges to the C++ |
---|
| 19 | dispersion object. |
---|
[af03ddd] | 20 | |
---|
[79ac6f8] | 21 | The ArrayDispersion class takes in numpy arrays only. |
---|
[af03ddd] | 22 | |
---|
[79ac6f8] | 23 | Usage: |
---|
| 24 | These classes can be used to set the dispersion model of a SANS model |
---|
| 25 | parameter: |
---|
[af03ddd] | 26 | |
---|
[79ac6f8] | 27 | cyl = CylinderModel() |
---|
| 28 | cyl.set_dispersion('radius', GaussianDispersion()) |
---|
[af03ddd] | 29 | |
---|
[79ac6f8] | 30 | |
---|
| 31 | After the dispersion model is set, you can access it's |
---|
| 32 | parameter through the dispersion dictionary: |
---|
| 33 | |
---|
| 34 | cyl.dispersion['radius']['width'] = 5.0 |
---|
| 35 | |
---|
| 36 | :TODO: For backward compatibility, the model parameters are still kept in |
---|
[af03ddd] | 37 | a dictionary. The next iteration of refactoring work should involve moving |
---|
| 38 | away from value-based parameters to object-based parameter. We want to |
---|
| 39 | store parameters as objects so that we can unify the 'params' and 'dispersion' |
---|
| 40 | dictionaries into a single dictionary of parameter objects that hold the |
---|
| 41 | complete information about the parameter (units, limits, dispersion model, etc...). |
---|
[79ac6f8] | 42 | |
---|
[af03ddd] | 43 | |
---|
| 44 | """ |
---|
| 45 | import sans_extension.c_models as c_models |
---|
| 46 | |
---|
[988130c6] | 47 | |
---|
| 48 | |
---|
[af03ddd] | 49 | class DispersionModel: |
---|
| 50 | """ |
---|
[79ac6f8] | 51 | Python bridge class for a basic dispersion model |
---|
| 52 | class with a constant parameter value distribution |
---|
[af03ddd] | 53 | """ |
---|
| 54 | def __init__(self): |
---|
| 55 | self.cdisp = c_models.new_dispersion_model() |
---|
| 56 | |
---|
| 57 | def set_weights(self, values, weights): |
---|
| 58 | """ |
---|
[79ac6f8] | 59 | Set the weights of an array dispersion |
---|
[af03ddd] | 60 | """ |
---|
| 61 | message = "set_weights is not available for DispersionModel.\n" |
---|
| 62 | message += " Solution: Use an ArrayDispersion object" |
---|
| 63 | raise "RuntimeError", message |
---|
| 64 | |
---|
| 65 | class GaussianDispersion(DispersionModel): |
---|
| 66 | """ |
---|
[79ac6f8] | 67 | Python bridge class for a dispersion model based |
---|
| 68 | on a Gaussian distribution. |
---|
[af03ddd] | 69 | """ |
---|
| 70 | def __init__(self): |
---|
| 71 | self.cdisp = c_models.new_gaussian_model() |
---|
| 72 | |
---|
| 73 | def set_weights(self, values, weights): |
---|
| 74 | """ |
---|
| 75 | Set the weights of an array dispersion |
---|
| 76 | """ |
---|
[eba9885] | 77 | message = "set_weights is not available for GaussianDispersion.\n" |
---|
| 78 | message += " Solution: Use an ArrayDispersion object" |
---|
| 79 | raise "RuntimeError", message |
---|
| 80 | |
---|
| 81 | class SchulzDispersion(DispersionModel): |
---|
| 82 | """ |
---|
| 83 | Python bridge class for a dispersion model based |
---|
| 84 | on a Schulz distribution. |
---|
| 85 | """ |
---|
| 86 | def __init__(self): |
---|
[79ac6f8] | 87 | """ |
---|
| 88 | """ |
---|
[eba9885] | 89 | self.cdisp = c_models.new_schulz_model() |
---|
| 90 | |
---|
| 91 | def set_weights(self, values, weights): |
---|
| 92 | """ |
---|
[79ac6f8] | 93 | Set the weights of an array dispersion |
---|
[eba9885] | 94 | """ |
---|
| 95 | message = "set_weights is not available for SchulzDispersion.\n" |
---|
| 96 | message += " Solution: Use an ArrayDispersion object" |
---|
| 97 | raise "RuntimeError", message |
---|
| 98 | |
---|
| 99 | class LogNormalDispersion(DispersionModel): |
---|
| 100 | """ |
---|
[79ac6f8] | 101 | Python bridge class for a dispersion model based |
---|
| 102 | on a Log Normal distribution. |
---|
[eba9885] | 103 | """ |
---|
| 104 | def __init__(self): |
---|
| 105 | self.cdisp = c_models.new_lognormal_model() |
---|
| 106 | |
---|
| 107 | def set_weights(self, values, weights): |
---|
| 108 | """ |
---|
[79ac6f8] | 109 | Set the weights of an array dispersion |
---|
[eba9885] | 110 | """ |
---|
| 111 | message = "set_weights is not available for LogNormalDispersion.\n" |
---|
[af03ddd] | 112 | message += " Solution: Use an ArrayDispersion object" |
---|
| 113 | raise "RuntimeError", message |
---|
| 114 | |
---|
| 115 | class ArrayDispersion(DispersionModel): |
---|
| 116 | """ |
---|
[79ac6f8] | 117 | Python bridge class for a dispersion model based on arrays. |
---|
| 118 | The user has to set a weight distribution that |
---|
| 119 | will be used in the averaging the model parameter |
---|
| 120 | it is applied to. |
---|
[af03ddd] | 121 | """ |
---|
| 122 | def __init__(self): |
---|
| 123 | self.cdisp = c_models.new_array_model() |
---|
| 124 | |
---|
| 125 | def set_weights(self, values, weights): |
---|
| 126 | """ |
---|
[79ac6f8] | 127 | Set the weights of an array dispersion |
---|
| 128 | Only accept numpy arrays. |
---|
| 129 | |
---|
| 130 | :param values: numpy array of values |
---|
| 131 | :param weights: numpy array of weights for each value entry |
---|
| 132 | |
---|
[af03ddd] | 133 | """ |
---|
| 134 | if len(values) != len(weights): |
---|
| 135 | raise ValueError, "ArrayDispersion.set_weights: given arrays are of different lengths" |
---|
| 136 | |
---|
| 137 | c_models.set_dispersion_weights(self.cdisp, values, weights) |
---|
[988130c6] | 138 | |
---|
[c9aa125] | 139 | models = {"gaussian":GaussianDispersion, "array":ArrayDispersion, |
---|
| 140 | "schulz":SchulzDispersion, "lognormal":LogNormalDispersion} |
---|
[af03ddd] | 141 | |
---|