Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.16 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "# Skywalker algorithm against the two mirror system"
  8. ]
  9. },
  10. {
  11. "cell_type": "code",
  12. "execution_count": 1,
  13. "metadata": {
  14. "collapsed": true
  15. },
  16. "outputs": [],
  17. "source": [
  18. "SIMULATION = True\n",
  19. "\n",
  20. "goal1 = 210 # Target pixel on first imager. NOTE: Don't do the math for flipped image\n",
  21. "goal2 = 270 # Target pixel on second imager. NOTE: Don't do the math for flipped image\n",
  22. "first_steps = 10 # Naive first step for the parameters search\n",
  23. "tolerances = 2.0 # Tolerance of each target in Pixels\n",
  24. "average = 1 # Number of shots to average over\n",
  25. "log_level = \"INFO\"\n",
  26. "\n",
  27. "# PARAMETERS FOR SIMULATION ONLY\n",
  28. "centroid_noise = 5.0 # Noise level of centroid measurements\n",
  29. "infinite_yag = False # Assume all yags are infinitely large\n",
  30. "\n",
  31. "goals = [goal1, goal2]"
  32. ]
  33. },
  34. {
  35. "cell_type": "code",
  36. "execution_count": 2,
  37. "metadata": {
  38. "collapsed": true
  39. },
  40. "outputs": [],
  41. "source": [
  42. "############\n",
  43. "# Standard #\n",
  44. "############\n",
  45. "import os\n",
  46. "import sys\n",
  47. "import logging\n",
  48. "import argparse\n",
  49. "\n",
  50. "###############\n",
  51. "# Third Party #\n",
  52. "###############\n",
  53. "from bluesky import RunEngine\n",
  54. "from bluesky.tests.utils import MsgCollector\n",
  55. "from bluesky.plans import run_wrapper\n",
  56. "from pcdsdevices.sim import pim, source, mirror"
  57. ]
  58. },
  59. {
  60. "cell_type": "code",
  61. "execution_count": 3,
  62. "metadata": {},
  63. "outputs": [
  64. {
  65. "name": "stderr",
  66. "output_type": "stream",
  67. "text": [
  68. "2017-07-20 15:45:30,268 - INFO - Patching 2 pim(s)\n"
  69. ]
  70. }
  71. ],
  72. "source": [
  73. "\"\"\"\n",
  74. "Parameters\n",
  75. "----------\n",
  76. "\"\"\"\n",
  77. "#Configure logger\n",
  78. "log_level = getattr(logging, log_level, None)\n",
  79. "\n",
  80. "#Report invalid logging level\n",
  81. "if not isinstance(log_level, int):\n",
  82. " raise ValueError(\"Invalid log level : {}\".format(log_level))\n",
  83. "\n",
  84. "#Create basic configuration\n",
  85. "logging.basicConfig(level=log_level,\n",
  86. " format='%(asctime)s - %(levelname)s - %(message)s')\n",
  87. "\n",
  88. "from pswalker.examples import patch_pims\n",
  89. "from pswalker.skywalker import skywalker\n",
  90. "\n",
  91. "#Instantiate simulation\n",
  92. "if not SIMULATION:\n",
  93. " print(\"*\"*80)\n",
  94. " print(\"* WARNING: Running on real devices in Production...\")\n",
  95. " print(\"*\"*80)\n",
  96. " input(\"Press any key to continue...\")\n",
  97. " system = homs_system()\n",
  98. " m1h = system['m1h']\n",
  99. " m1h2 = system['m1h2']\n",
  100. " m2h = system['m2h']\n",
  101. " m2h2 = system['m2h2']\n",
  102. " xrtm2 = system['xrtm2']\n",
  103. " xrtm22 = system['xrtm22']\n",
  104. " hx2 = system['hx2']\n",
  105. " dg3 = system['dg3']\n",
  106. " mfxdg1 = system['mfxdg1']\n",
  107. " m1 = m1h\n",
  108. " m2 = m2h\n",
  109. " y1 = hx2\n",
  110. " y2 = dg3\n",
  111. "else:\n",
  112. " s = source.Undulator('test_undulator')\n",
  113. " m1 = mirror.OffsetMirror('test_m1h', z=90.510, alpha=0.0014)\n",
  114. " m2 = mirror.OffsetMirror('test_m2h', x=0.0317324, z=101.843, alpha=0.0014)\n",
  115. " y1 = pim.PIM('test_p3h', x=0.0317324, z=103.660,\n",
  116. " zero_outside_yag= not infinite_yag)\n",
  117. " y2 = pim.PIM('test_dg3', x=0.0317324, z=375.000,\n",
  118. " zero_outside_yag= not infinite_yag)\n",
  119. " patch_pims([y1, y2], mirrors=[m1, m2], source=s)\n",
  120. "\n",
  121. " #Add noise\n",
  122. " y1.centroid_noise = centroid_noise\n",
  123. " y2.centroid_noise = centroid_noise\n"
  124. ]
  125. },
  126. {
  127. "cell_type": "code",
  128. "execution_count": 4,
  129. "metadata": {
  130. "collapsed": true
  131. },
  132. "outputs": [],
  133. "source": [
  134. "#Create Skywalker plan\n",
  135. "plan = skywalker([y1, y2], [m1, m2], 'detector_stats2_centroid_x', 'pitch',\n",
  136. " goals, first_steps=first_steps, tolerances=tolerances,\n",
  137. " averages=average, timeout=10)\n"
  138. ]
  139. },
  140. {
  141. "cell_type": "code",
  142. "execution_count": 5,
  143. "metadata": {
  144. "collapsed": true
  145. },
  146. "outputs": [],
  147. "source": [
  148. "#Create RunEngine\n",
  149. "RE = RunEngine({})\n",
  150. "if SIMULATION:\n",
  151. " collector = MsgCollector()\n",
  152. " RE.msg_hook = collector"
  153. ]
  154. },
  155. {
  156. "cell_type": "code",
  157. "execution_count": 6,
  158. "metadata": {},
  159. "outputs": [
  160. {
  161. "name": "stderr",
  162. "output_type": "stream",
  163. "text": [
  164. "2017-07-20 15:46:02,662 - INFO - Averaged data yielded TST:test_p3h_detector_stats2_centroid_x is at 2992.5109704925367\n",
  165. "2017-07-20 15:46:02,664 - INFO - Unable to yield estimate from model Model(linear)\n",
  166. "2017-07-20 15:46:02,665 - INFO - Walking motor, error after step #0 -> -2782.5109704925367\n",
  167. "2017-07-20 15:46:02,665 - INFO - No model yielded accurate prediction, using naive plan\n",
  168. "2017-07-20 15:46:02,777 - INFO - Averaged data yielded TST:test_p3h_detector_stats2_centroid_x is at 2970.1960811994313\n",
  169. "2017-07-20 15:46:02,778 - INFO - Walking motor, error after step #1 -> -2760.1960811994313\n",
  170. "2017-07-20 15:46:02,779 - INFO - Using model Model(linear) to determine next step.\n",
  171. "2017-07-20 15:46:02,780 - INFO - Adjusting motor MIRR:TST:test_m1h to position 1246.9316150448226\n",
  172. "2017-07-20 15:46:02,895 - INFO - Averaged data yielded TST:test_p3h_detector_stats2_centroid_x is at 231.1586845932672\n",
  173. "2017-07-20 15:46:02,896 - INFO - Walking motor, error after step #2 -> -21.158684593267196\n",
  174. "2017-07-20 15:46:02,897 - INFO - Using model Model(linear) to determine next step.\n",
  175. "2017-07-20 15:46:02,898 - INFO - Adjusting motor MIRR:TST:test_m1h to position 1256.4861192121707\n",
  176. "2017-07-20 15:46:03,011 - INFO - Averaged data yielded TST:test_p3h_detector_stats2_centroid_x is at 209.25453132668488\n",
  177. "2017-07-20 15:46:03,012 - INFO - Succesfully walked to value 209.25453132668488 (target=210) after 3 steps.\n",
  178. "2017-07-20 15:46:03,013 - INFO - Found equation of (-2.214754070224962, 2992.4321565111486) between linear fit of MIRR:TST:test_m1h to TST:test_p3h\n",
  179. "2017-07-20 15:46:03,234 - INFO - Averaged data yielded TST:test_dg3_detector_stats2_centroid_x is at -57210.2684784569\n",
  180. "2017-07-20 15:46:03,235 - INFO - Unable to yield estimate from model Model(linear)\n",
  181. "2017-07-20 15:46:03,236 - INFO - Walking motor, error after step #0 -> 57480.2684784569\n",
  182. "2017-07-20 15:46:03,237 - INFO - No model yielded accurate prediction, using naive plan\n",
  183. "2017-07-20 15:46:03,352 - INFO - Averaged data yielded TST:test_dg3_detector_stats2_centroid_x is at -56752.50884796505\n",
  184. "2017-07-20 15:46:03,353 - INFO - Walking motor, error after step #1 -> 57022.50884796505\n",
  185. "2017-07-20 15:46:03,354 - INFO - Using model Model(linear) to determine next step.\n",
  186. "2017-07-20 15:46:03,355 - INFO - Adjusting motor MIRR:TST:test_m2h to position 1255.68811087712\n",
  187. "2017-07-20 15:46:03,471 - INFO - Averaged data yielded TST:test_dg3_detector_stats2_centroid_x is at 557.7155008460301\n",
  188. "2017-07-20 15:46:03,472 - INFO - Walking motor, error after step #2 -> -287.71550084603007\n",
  189. "2017-07-20 15:46:03,473 - INFO - Using model Model(linear) to determine next step.\n",
  190. "2017-07-20 15:46:03,474 - INFO - Adjusting motor MIRR:TST:test_m2h to position 1249.4344414263348\n",
  191. "2017-07-20 15:46:03,588 - INFO - Averaged data yielded TST:test_dg3_detector_stats2_centroid_x is at 270.8871830680273\n",
  192. "2017-07-20 15:46:03,590 - INFO - Succesfully walked to value 270.8871830680273 (target=270.0) after 3 steps.\n",
  193. "2017-07-20 15:46:03,590 - INFO - Found equation of (46.00635180937579, -57211.47911131522) between linear fit of MIRR:TST:test_m2h to TST:test_dg3\n",
  194. "2017-07-20 15:46:03,822 - INFO - Averaged data yielded TST:test_p3h_detector_stats2_centroid_x is at 593.1904542619974\n",
  195. "2017-07-20 15:46:03,823 - INFO - Unable to yield estimate from model Model(linear)\n",
  196. "2017-07-20 15:46:03,824 - INFO - Walking motor, error after step #0 -> -383.1904542619974\n",
  197. "2017-07-20 15:46:03,825 - INFO - No model yielded accurate prediction, using naive plan\n",
  198. "2017-07-20 15:46:03,826 - INFO - Using gradient of -2.214754070224962 for naive step...\n",
  199. "2017-07-20 15:46:03,846 - INFO - ['TST:test_p3h_detector_stats2_centroid_x', 'MIRR:TST:test_m1h_pitch']\n",
  200. "2017-07-20 15:46:03,847 - INFO - {'TST:test_p3h_detector_stats2_centroid_x': 594.45368400973302, 'MIRR:TST:test_m1h_pitch': 1256.4861192121707}\n",
  201. "2017-07-20 15:46:03,964 - INFO - Averaged data yielded TST:test_p3h_detector_stats2_centroid_x is at 207.5383879749233\n",
  202. "2017-07-20 15:46:03,965 - INFO - Walking motor, error after step #1 -> 2.461612025076704\n",
  203. "2017-07-20 15:46:03,966 - INFO - Using model Model(linear) to determine next step.\n",
  204. "2017-07-20 15:46:03,967 - INFO - Adjusting motor MIRR:TST:test_m1h to position 1428.9674732811611\n",
  205. "2017-07-20 15:46:04,088 - INFO - Averaged data yielded TST:test_p3h_detector_stats2_centroid_x is at 210.73017411190023\n",
  206. "2017-07-20 15:46:04,089 - INFO - Succesfully walked to value 210.73017411190023 (target=210) after 2 steps.\n",
  207. "2017-07-20 15:46:04,090 - INFO - Found equation of (-2.2231997376188866, 3387.2428920483103) between linear fit of MIRR:TST:test_m1h to TST:test_p3h\n",
  208. "2017-07-20 15:46:04,336 - INFO - Averaged data yielded TST:test_dg3_detector_stats2_centroid_x is at -7995.113015803845\n",
  209. "2017-07-20 15:46:04,337 - INFO - Unable to yield estimate from model Model(linear)\n",
  210. "2017-07-20 15:46:04,338 - INFO - Walking motor, error after step #0 -> 8265.113015803847\n",
  211. "2017-07-20 15:46:04,339 - INFO - No model yielded accurate prediction, using naive plan\n",
  212. "2017-07-20 15:46:04,340 - INFO - Using gradient of 46.00635180937579 for naive step...\n",
  213. "2017-07-20 15:46:04,362 - INFO - ['TST:test_dg3_detector_stats2_centroid_x', 'MIRR:TST:test_m2h_pitch']\n",
  214. "2017-07-20 15:46:04,363 - INFO - {'TST:test_dg3_detector_stats2_centroid_x': -7996.5008068374946, 'MIRR:TST:test_m2h_pitch': 1249.4344414263348}\n",
  215. "2017-07-20 15:46:04,489 - INFO - Averaged data yielded TST:test_dg3_detector_stats2_centroid_x is at 273.0560193141365\n",
  216. "2017-07-20 15:46:04,490 - INFO - Walking motor, error after step #1 -> -3.0560193141355967\n",
  217. "2017-07-20 15:46:04,491 - INFO - Using model Model(linear) to determine next step.\n",
  218. "2017-07-20 15:46:04,492 - INFO - Adjusting motor MIRR:TST:test_m2h to position 1429.049762692403\n",
  219. "2017-07-20 15:46:04,614 - INFO - Averaged data yielded TST:test_dg3_detector_stats2_centroid_x is at 267.9541042195013\n",
  220. "2017-07-20 15:46:04,615 - INFO - Walking motor, error after step #2 -> 2.0458957804996203\n",
  221. "2017-07-20 15:46:04,617 - INFO - Using model Model(linear) to determine next step.\n",
  222. "2017-07-20 15:46:04,618 - INFO - Adjusting motor MIRR:TST:test_m2h to position 1429.0719858020007\n",
  223. "2017-07-20 15:46:04,744 - INFO - Averaged data yielded TST:test_dg3_detector_stats2_centroid_x is at 269.7150415942525\n",
  224. "2017-07-20 15:46:04,745 - INFO - Succesfully walked to value 269.7150415942525 (target=270.0000000000009) after 3 steps.\n",
  225. "2017-07-20 15:46:04,746 - INFO - Found equation of (46.01327721670974, -65486.380423690935) between linear fit of MIRR:TST:test_m2h to TST:test_dg3\n",
  226. "2017-07-20 15:46:04,988 - INFO - Averaged data yielded TST:test_p3h_detector_stats2_centroid_x is at 262.46773152297027\n",
  227. "2017-07-20 15:46:04,989 - INFO - Unable to yield estimate from model Model(linear)\n",
  228. "2017-07-20 15:46:04,990 - INFO - Walking motor, error after step #0 -> -52.46773152297027\n",
  229. "2017-07-20 15:46:04,991 - INFO - No model yielded accurate prediction, using naive plan\n",
  230. "2017-07-20 15:46:04,992 - INFO - Using gradient of -2.2231997376188866 for naive step...\n",
  231. "2017-07-20 15:46:05,070 - INFO - ['TST:test_p3h_detector_stats2_centroid_x', 'MIRR:TST:test_m1h_pitch']\n",
  232. "2017-07-20 15:46:05,071 - INFO - {'TST:test_p3h_detector_stats2_centroid_x': 265.10380530750854, 'MIRR:TST:test_m1h_pitch': 1428.9674732811611}\n",
  233. "2017-07-20 15:46:05,197 - INFO - Averaged data yielded TST:test_p3h_detector_stats2_centroid_x is at 211.1253785677299\n",
  234. "2017-07-20 15:46:05,198 - INFO - Succesfully walked to value 211.1253785677299 (target=210) after 1 steps.\n",
  235. "2017-07-20 15:46:05,199 - INFO - Found equation of (-2.124618512978424, 3299.796516592396) between linear fit of MIRR:TST:test_m1h to TST:test_p3h\n",
  236. "2017-07-20 15:46:05,470 - INFO - Averaged data yielded TST:test_dg3_detector_stats2_centroid_x is at -917.5972021739992\n",
  237. "2017-07-20 15:46:05,472 - INFO - Unable to yield estimate from model Model(linear)\n",
  238. "2017-07-20 15:46:05,474 - INFO - Walking motor, error after step #0 -> 1187.597202173999\n",
  239. "2017-07-20 15:46:05,474 - INFO - No model yielded accurate prediction, using naive plan\n",
  240. "2017-07-20 15:46:05,475 - INFO - Using gradient of 46.01327721670974 for naive step...\n",
  241. "2017-07-20 15:46:05,514 - INFO - ['TST:test_dg3_detector_stats2_centroid_x', 'MIRR:TST:test_m2h_pitch']\n",
  242. "2017-07-20 15:46:05,515 - INFO - {'TST:test_dg3_detector_stats2_centroid_x': -916.41591368831973, 'MIRR:TST:test_m2h_pitch': 1429.0719858020007}\n"
  243. ]
  244. },
  245. {
  246. "name": "stderr",
  247. "output_type": "stream",
  248. "text": [
  249. "2017-07-20 15:46:05,653 - INFO - Averaged data yielded TST:test_dg3_detector_stats2_centroid_x is at 266.927665334671\n",
  250. "2017-07-20 15:46:05,654 - INFO - Walking motor, error after step #1 -> 3.0723346653288672\n",
  251. "2017-07-20 15:46:05,655 - INFO - Using model Model(linear) to determine next step.\n",
  252. "2017-07-20 15:46:05,656 - INFO - Adjusting motor MIRR:TST:test_m2h to position 1454.9231044430674\n",
  253. "2017-07-20 15:46:05,796 - INFO - Averaged data yielded TST:test_dg3_detector_stats2_centroid_x is at 270.8546504498207\n",
  254. "2017-07-20 15:46:05,797 - INFO - Succesfully walked to value 270.8546504498207 (target=269.9999999999999) after 2 steps.\n",
  255. "2017-07-20 15:46:05,798 - INFO - Found equation of (45.93362325190344, -66559.46130659437) between linear fit of MIRR:TST:test_m2h to TST:test_dg3\n",
  256. "2017-07-20 15:46:06,081 - INFO - Averaged data yielded TST:test_p3h_detector_stats2_centroid_x is at 217.5075631044156\n",
  257. "2017-07-20 15:46:06,082 - INFO - Unable to yield estimate from model Model(linear)\n",
  258. "2017-07-20 15:46:06,083 - INFO - Walking motor, error after step #0 -> -7.5075631044155955\n",
  259. "2017-07-20 15:46:06,084 - INFO - No model yielded accurate prediction, using naive plan\n",
  260. "2017-07-20 15:46:06,088 - INFO - Using gradient of -2.124618512978424 for naive step...\n",
  261. "2017-07-20 15:46:06,135 - INFO - ['TST:test_p3h_detector_stats2_centroid_x', 'MIRR:TST:test_m1h_pitch']\n",
  262. "2017-07-20 15:46:06,139 - INFO - {'TST:test_p3h_detector_stats2_centroid_x': 218.52685462692915, 'MIRR:TST:test_m1h_pitch': 1453.7532828398321}\n",
  263. "2017-07-20 15:46:06,287 - INFO - Averaged data yielded TST:test_p3h_detector_stats2_centroid_x is at 208.55912994567362\n",
  264. "2017-07-20 15:46:06,288 - INFO - Succesfully walked to value 208.55912994567362 (target=210) after 1 steps.\n",
  265. "2017-07-20 15:46:06,289 - INFO - Found equation of (-2.3566497201899086, 3644.0044760953247) between linear fit of MIRR:TST:test_m1h to TST:test_p3h\n",
  266. "2017-07-20 15:46:06,555 - INFO - Averaged data yielded TST:test_dg3_detector_stats2_centroid_x is at 77.12057221727919\n",
  267. "2017-07-20 15:46:06,556 - INFO - Unable to yield estimate from model Model(linear)\n",
  268. "2017-07-20 15:46:06,557 - INFO - Walking motor, error after step #0 -> 192.87942778272082\n",
  269. "2017-07-20 15:46:06,558 - INFO - No model yielded accurate prediction, using naive plan\n",
  270. "2017-07-20 15:46:06,558 - INFO - Using gradient of 45.93362325190344 for naive step...\n",
  271. "2017-07-20 15:46:06,599 - INFO - ['TST:test_dg3_detector_stats2_centroid_x', 'MIRR:TST:test_m2h_pitch']\n",
  272. "2017-07-20 15:46:06,600 - INFO - {'TST:test_dg3_detector_stats2_centroid_x': 79.452746399826466, 'MIRR:TST:test_m2h_pitch': 1454.9231044430674}\n",
  273. "2017-07-20 15:46:06,734 - INFO - Averaged data yielded TST:test_dg3_detector_stats2_centroid_x is at 270.7572695128481\n",
  274. "2017-07-20 15:46:06,735 - INFO - Succesfully walked to value 270.7572695128481 (target=270.0) after 1 steps.\n",
  275. "2017-07-20 15:46:06,736 - INFO - Found equation of (46.39727064625333, -67426.17438702355) between linear fit of MIRR:TST:test_m2h to TST:test_dg3\n",
  276. "2017-07-20 15:46:06,978 - INFO - Finished in 4.54s after 8 mirror walks, 9 yag cycles, and 0 recoveries\n",
  277. "2017-07-20 15:46:06,979 - INFO - Aligned to [210.63840721342152, 270.7572695128481]\n",
  278. "2017-07-20 15:46:06,980 - INFO - Goals were [210, 270]\n",
  279. "2017-07-20 15:46:06,981 - INFO - Deltas are [0.63840721342151596, 0.75726951284809729]\n"
  280. ]
  281. },
  282. {
  283. "data": {
  284. "text/plain": [
  285. "['8d48437c-766e-44b7-8f09-fa63915f3e9b']"
  286. ]
  287. },
  288. "execution_count": 6,
  289. "metadata": {},
  290. "output_type": "execute_result"
  291. }
  292. ],
  293. "source": [
  294. "RE(run_wrapper(plan))"
  295. ]
  296. },
  297. {
  298. "cell_type": "code",
  299. "execution_count": 7,
  300. "metadata": {},
  301. "outputs": [
  302. {
  303. "name": "stdout",
  304. "output_type": "stream",
  305. "text": [
  306. "507\n"
  307. ]
  308. }
  309. ],
  310. "source": [
  311. "if SIMULATION:\n",
  312. " #Analyze Performance\n",
  313. " print(len(RE.msg_hook.msgs))"
  314. ]
  315. },
  316. {
  317. "cell_type": "code",
  318. "execution_count": null,
  319. "metadata": {
  320. "collapsed": true
  321. },
  322. "outputs": [],
  323. "source": []
  324. }
  325. ],
  326. "metadata": {
  327. "kernelspec": {
  328. "display_name": "Python 3",
  329. "language": "python",
  330. "name": "python3"
  331. },
  332. "language_info": {
  333. "codemirror_mode": {
  334. "name": "ipython",
  335. "version": 3
  336. },
  337. "file_extension": ".py",
  338. "mimetype": "text/x-python",
  339. "name": "python",
  340. "nbconvert_exporter": "python",
  341. "pygments_lexer": "ipython3",
  342. "version": "3.6.1"
  343. }
  344. },
  345. "nbformat": 4,
  346. "nbformat_minor": 2
  347. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement