Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "This notebook demonstrates an example of a model, where the `evaluate` method dispatches to something not unit aware (like C code), where the input and output needs to be in fixed units."
  8. ]
  9. },
  10. {
  11. "cell_type": "code",
  12. "execution_count": 31,
  13. "metadata": {
  14. "collapsed": true
  15. },
  16. "outputs": [],
  17. "source": [
  18. "import numpy as np\n",
  19. "\n",
  20. "import astropy.units as u\n",
  21. "from astropy.modeling import models as m\n",
  22. "from astropy.modeling import Model\n",
  23. "from astropy.utils.decorators import wraps"
  24. ]
  25. },
  26. {
  27. "cell_type": "markdown",
  28. "metadata": {},
  29. "source": [
  30. "Define a decorator to create a quantity object from the return value of a function."
  31. ]
  32. },
  33. {
  34. "cell_type": "code",
  35. "execution_count": 86,
  36. "metadata": {
  37. "collapsed": true
  38. },
  39. "outputs": [],
  40. "source": [
  41. "class CreateReturnQuantity(object):\n",
  42. " \"\"\"\n",
  43. " Create a quantity of the return value and the return annotation as a unit.\n",
  44. " \"\"\"\n",
  45. " @classmethod\n",
  46. " def as_decorator(cls, func=None, **kwargs):\n",
  47. " self = cls(**kwargs)\n",
  48. " if func is not None and not kwargs:\n",
  49. " return self(func)\n",
  50. " else:\n",
  51. " return self\n",
  52. " \n",
  53. " def __init__(self, return_unit=None):\n",
  54. " self.return_unit = return_unit\n",
  55. " \n",
  56. " def __call__(self, func):\n",
  57. " if hasattr(func, '__annotations__'):\n",
  58. " return_unit = func.__annotations__['return']\n",
  59. " elif self.return_unit:\n",
  60. " return_unit = self.return_unit\n",
  61. " else:\n",
  62. " raise ValueError(\"The return unit must be specified either as a\"\n",
  63. " \" function annotation or as an argument to the decorator.\")\n",
  64. " \n",
  65. " @wraps(func)\n",
  66. " def wrapper(*args, **kwargs):\n",
  67. " return u.Quantity(func(*args, **kwargs), unit=return_unit)\n",
  68. " \n",
  69. " return wrapper\n",
  70. " \n",
  71. "create_return_quantity = CreateReturnQuantity.as_decorator"
  72. ]
  73. },
  74. {
  75. "cell_type": "markdown",
  76. "metadata": {},
  77. "source": [
  78. "Our dummy function that `evaluate` will use."
  79. ]
  80. },
  81. {
  82. "cell_type": "code",
  83. "execution_count": 77,
  84. "metadata": {
  85. "collapsed": true
  86. },
  87. "outputs": [],
  88. "source": [
  89. "def some_unitless_function(ham, eggs):\n",
  90. " \"\"\"\n",
  91. " This function does something in code that has no idea what a unit is.\n",
  92. " \n",
  93. " Parameters\n",
  94. " ----------\n",
  95. " ham : `float`\n",
  96. " ham in units of kg\n",
  97. " \n",
  98. " eggs : `float`\n",
  99. " eggs in units of m\n",
  100. " \n",
  101. " Returns\n",
  102. " -------\n",
  103. " Ni : `float`\n",
  104. " The result of ham combined with eggs. In units of J.\n",
  105. " \"\"\"\n",
  106. " return np.array(ham) + np.array(eggs)"
  107. ]
  108. },
  109. {
  110. "cell_type": "markdown",
  111. "metadata": {},
  112. "source": [
  113. "A model:"
  114. ]
  115. },
  116. {
  117. "cell_type": "code",
  118. "execution_count": 91,
  119. "metadata": {
  120. "collapsed": false
  121. },
  122. "outputs": [],
  123. "source": [
  124. "class Spam(Model):\n",
  125. " inputs = ('ham', 'eggs')\n",
  126. " outputs = ('Ni',)\n",
  127. " \n",
  128. " @property\n",
  129. " def input_units(self):\n",
  130. " return u.kg, u.m\n",
  131. " \n",
  132. " @classmethod\n",
  133. " @create_return_quantity\n",
  134. " def evaluate(cls, x, y) -> u.J:\n",
  135. " # Compatibility with these units is ensured by `input_units`\n",
  136. " # however, this no longer supports non-quantity input.\n",
  137. " x = x.to(u.kg)\n",
  138. " y = y.to(u.m)\n",
  139. " # Call the external function\n",
  140. " return some_unitless_function(x, y)"
  141. ]
  142. },
  143. {
  144. "cell_type": "code",
  145. "execution_count": 88,
  146. "metadata": {
  147. "collapsed": true
  148. },
  149. "outputs": [],
  150. "source": [
  151. "s = Spam()"
  152. ]
  153. },
  154. {
  155. "cell_type": "code",
  156. "execution_count": 89,
  157. "metadata": {
  158. "collapsed": false
  159. },
  160. "outputs": [
  161. {
  162. "data": {
  163. "text/latex": [
  164. "$0.11 \\; \\mathrm{J}$"
  165. ],
  166. "text/plain": [
  167. "<Quantity 0.11 J>"
  168. ]
  169. },
  170. "execution_count": 89,
  171. "metadata": {},
  172. "output_type": "execute_result"
  173. }
  174. ],
  175. "source": [
  176. "s(10*u.g, 10*u.cm)"
  177. ]
  178. }
  179. ],
  180. "metadata": {
  181. "kernelspec": {
  182. "display_name": "Environment (astropy-dev)",
  183. "language": "python",
  184. "name": "astropy-dev"
  185. },
  186. "language_info": {
  187. "codemirror_mode": {
  188. "name": "ipython",
  189. "version": 3
  190. },
  191. "file_extension": ".py",
  192. "mimetype": "text/x-python",
  193. "name": "python",
  194. "nbconvert_exporter": "python",
  195. "pygments_lexer": "ipython3",
  196. "version": "3.5.2"
  197. }
  198. },
  199. "nbformat": 4,
  200. "nbformat_minor": 2
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement