Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.28 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "`I'm going to illustrate a problem I found recently`"
  8. ]
  9. },
  10. {
  11. "cell_type": "code",
  12. "execution_count": 1,
  13. "metadata": {},
  14. "outputs": [
  15. {
  16. "name": "stderr",
  17. "output_type": "stream",
  18. "text": [
  19. "/home/epassaro/miniconda3/envs/carsus/lib/python3.6/site-packages/tqdm/autonotebook/__init__.py:18: TqdmExperimentalWarning: Using `tqdm.autonotebook.tqdm` in notebook mode. Use `tqdm.tqdm` instead to force console mode (e.g. in jupyter console)\n",
  20. " \" (e.g. in jupyter console)\", TqdmExperimentalWarning)\n"
  21. ]
  22. }
  23. ],
  24. "source": [
  25. "import numpy as np\n",
  26. "import pandas as pd\n",
  27. "from carsus import init_db\n",
  28. "from carsus.model import Atom, Ion, Level, Line\n",
  29. "from carsus.io.output import AtomData\n",
  30. "from carsus.io.nist import NISTIonizationEnergiesIngester\n",
  31. "from carsus.io.kurucz import GFALLIngester, GFALLReader\n",
  32. "\n",
  33. "import warnings\n",
  34. "warnings.simplefilter('ignore')"
  35. ]
  36. },
  37. {
  38. "cell_type": "markdown",
  39. "metadata": {},
  40. "source": [
  41. "# Ingesting `He` levels with SQL database"
  42. ]
  43. },
  44. {
  45. "cell_type": "code",
  46. "execution_count": 2,
  47. "metadata": {
  48. "scrolled": false
  49. },
  50. "outputs": [
  51. {
  52. "name": "stdout",
  53. "output_type": "stream",
  54. "text": [
  55. "Initializing the database at sqlite://\n",
  56. "Ingesting basic atomic data\n",
  57. "Downloading ionization energies from the NIST Atomic Spectra Database\n",
  58. "Ingesting ionization energies from nist-asd\n",
  59. "Ingesting ground levels from nist-asd\n"
  60. ]
  61. }
  62. ],
  63. "source": [
  64. "session = init_db()\n",
  65. "\n",
  66. "ioniz_energies_ingester = NISTIonizationEnergiesIngester(session, spectra=\"He\")\n",
  67. "ioniz_energies_ingester.ingest(ionization_energies=True, ground_levels=True)\n",
  68. "session.commit()"
  69. ]
  70. },
  71. {
  72. "cell_type": "code",
  73. "execution_count": 3,
  74. "metadata": {
  75. "scrolled": false
  76. },
  77. "outputs": [
  78. {
  79. "name": "stdout",
  80. "output_type": "stream",
  81. "text": [
  82. "[\u001b[1mcarsus.io.kurucz.gfall\u001b[0m][\u001b[1;33mWARNING\u001b[0m] A specific combination to identify unique levels from the gfall data has not been given. Defaulting to [\"energy\", \"j\"]. (\u001b[1mgfall.py\u001b[0m:72)\n",
  83. "[\u001b[1mcarsus.io.kurucz.gfall\u001b[0m][\u001b[1;37mINFO\u001b[0m ] Parsing GFALL ../gfall.dat (\u001b[1mgfall.py\u001b[0m:120)\n",
  84. "Ingesting levels from ku_latest\n",
  85. "Ingesting levels for He 0\n",
  86. "Ingesting levels for He 1\n",
  87. "[\u001b[1mcarsus.io.kurucz.gfall\u001b[0m][\u001b[1;37mINFO\u001b[0m ] Extracting line data: atomic_number, ion_charge, energy_lower, j_lower, energy_upper, j_upper, wavelength, loggf (\u001b[1mgfall.py\u001b[0m:296)\n",
  88. "Ingesting lines from ku_latest\n",
  89. "Ingesting lines for He 0\n",
  90. "Ingesting lines for He 1\n"
  91. ]
  92. }
  93. ],
  94. "source": [
  95. "gfall_ingester = GFALLIngester(session, fname=\"../gfall.dat\", ions=\"He\")\n",
  96. "gfall_ingester.ingest(levels=True, lines=True)\n",
  97. "session.commit()"
  98. ]
  99. },
  100. {
  101. "cell_type": "code",
  102. "execution_count": 4,
  103. "metadata": {},
  104. "outputs": [],
  105. "source": [
  106. "atom_data = AtomData(session, selected_atoms=\"He\")"
  107. ]
  108. },
  109. {
  110. "cell_type": "markdown",
  111. "metadata": {},
  112. "source": [
  113. "Check how many levels are ingested in the first stage "
  114. ]
  115. },
  116. {
  117. "cell_type": "code",
  118. "execution_count": 5,
  119. "metadata": {},
  120. "outputs": [
  121. {
  122. "data": {
  123. "text/plain": [
  124. "(752, 4)"
  125. ]
  126. },
  127. "execution_count": 5,
  128. "metadata": {},
  129. "output_type": "execute_result"
  130. }
  131. ],
  132. "source": [
  133. "atom_data._get_all_levels_data().shape"
  134. ]
  135. },
  136. {
  137. "cell_type": "markdown",
  138. "metadata": {},
  139. "source": [
  140. "# Now try the same thing with the new code"
  141. ]
  142. },
  143. {
  144. "cell_type": "code",
  145. "execution_count": 6,
  146. "metadata": {},
  147. "outputs": [],
  148. "source": [
  149. "from carsus.io.kurucz.gfall import GFALL"
  150. ]
  151. },
  152. {
  153. "cell_type": "code",
  154. "execution_count": 7,
  155. "metadata": {},
  156. "outputs": [
  157. {
  158. "name": "stdout",
  159. "output_type": "stream",
  160. "text": [
  161. "[\u001b[1mcarsus.io.kurucz.gfall\u001b[0m][\u001b[1;33mWARNING\u001b[0m] A specific combination to identify unique levels from the gfall data has not been given. Defaulting to [\"energy\", \"j\"]. (\u001b[1mgfall.py\u001b[0m:72)\n",
  162. "Downloading ionization energies from the NIST Atomic Spectra Database\n",
  163. "[\u001b[1mcarsus.io.kurucz.gfall\u001b[0m][\u001b[1;37mINFO\u001b[0m ] Parsing GFALL ./gfall.dat (\u001b[1mgfall.py\u001b[0m:120)\n",
  164. "[\u001b[1mcarsus.io.kurucz.gfall\u001b[0m][\u001b[1;37mINFO\u001b[0m ] Extracting line data: atomic_number, ion_charge, energy_lower, j_lower, energy_upper, j_upper, wavelength, loggf (\u001b[1mgfall.py\u001b[0m:296)\n"
  165. ]
  166. }
  167. ],
  168. "source": [
  169. "x = GFALL('./gfall.dat', 'He')"
  170. ]
  171. },
  172. {
  173. "cell_type": "code",
  174. "execution_count": 8,
  175. "metadata": {},
  176. "outputs": [
  177. {
  178. "data": {
  179. "text/plain": [
  180. "(753, 4)"
  181. ]
  182. },
  183. "execution_count": 8,
  184. "metadata": {},
  185. "output_type": "execute_result"
  186. }
  187. ],
  188. "source": [
  189. "x._get_all_levels_data().shape"
  190. ]
  191. },
  192. {
  193. "cell_type": "markdown",
  194. "metadata": {},
  195. "source": [
  196. "Oops! There's one more level than `atom_data`! From here everything fails:"
  197. ]
  198. },
  199. {
  200. "cell_type": "code",
  201. "execution_count": 9,
  202. "metadata": {},
  203. "outputs": [
  204. {
  205. "data": {
  206. "text/plain": [
  207. "energy 745\n",
  208. "g 749\n",
  209. "metastable 753\n",
  210. "dtype: int64"
  211. ]
  212. },
  213. "execution_count": 9,
  214. "metadata": {},
  215. "output_type": "execute_result"
  216. }
  217. ],
  218. "source": [
  219. "atom_data.levels_prepared.eq(x.levels_prepared).sum()"
  220. ]
  221. },
  222. {
  223. "cell_type": "markdown",
  224. "metadata": {},
  225. "source": [
  226. "The extra level is the ground level for `(2,1)`"
  227. ]
  228. },
  229. {
  230. "cell_type": "code",
  231. "execution_count": 10,
  232. "metadata": {},
  233. "outputs": [
  234. {
  235. "data": {
  236. "text/html": [
  237. "<div>\n",
  238. "<style scoped>\n",
  239. " .dataframe tbody tr th:only-of-type {\n",
  240. " vertical-align: middle;\n",
  241. " }\n",
  242. "\n",
  243. " .dataframe tbody tr th {\n",
  244. " vertical-align: top;\n",
  245. " }\n",
  246. "\n",
  247. " .dataframe thead th {\n",
  248. " text-align: right;\n",
  249. " }\n",
  250. "</style>\n",
  251. "<table border=\"1\" class=\"dataframe\">\n",
  252. " <thead>\n",
  253. " <tr style=\"text-align: right;\">\n",
  254. " <th></th>\n",
  255. " <th></th>\n",
  256. " <th>g</th>\n",
  257. " <th>energy</th>\n",
  258. " </tr>\n",
  259. " <tr>\n",
  260. " <th>atomic_number</th>\n",
  261. " <th>ion_number</th>\n",
  262. " <th></th>\n",
  263. " <th></th>\n",
  264. " </tr>\n",
  265. " </thead>\n",
  266. " <tbody>\n",
  267. " <tr>\n",
  268. " <th rowspan=\"9\" valign=\"top\">2</th>\n",
  269. " <th>1</th>\n",
  270. " <td>2</td>\n",
  271. " <td>0.000000</td>\n",
  272. " </tr>\n",
  273. " <tr>\n",
  274. " <th>1</th>\n",
  275. " <td>2</td>\n",
  276. " <td>40.813028</td>\n",
  277. " </tr>\n",
  278. " <tr>\n",
  279. " <th>1</th>\n",
  280. " <td>2</td>\n",
  281. " <td>40.813086</td>\n",
  282. " </tr>\n",
  283. " <tr>\n",
  284. " <th>1</th>\n",
  285. " <td>4</td>\n",
  286. " <td>40.813754</td>\n",
  287. " </tr>\n",
  288. " <tr>\n",
  289. " <th>1</th>\n",
  290. " <td>2</td>\n",
  291. " <td>48.371294</td>\n",
  292. " </tr>\n",
  293. " <tr>\n",
  294. " <th>1</th>\n",
  295. " <td>2</td>\n",
  296. " <td>48.371312</td>\n",
  297. " </tr>\n",
  298. " <tr>\n",
  299. " <th>1</th>\n",
  300. " <td>4</td>\n",
  301. " <td>48.371509</td>\n",
  302. " </tr>\n",
  303. " <tr>\n",
  304. " <th>1</th>\n",
  305. " <td>4</td>\n",
  306. " <td>48.371509</td>\n",
  307. " </tr>\n",
  308. " <tr>\n",
  309. " <th>1</th>\n",
  310. " <td>6</td>\n",
  311. " <td>48.371581</td>\n",
  312. " </tr>\n",
  313. " </tbody>\n",
  314. "</table>\n",
  315. "</div>"
  316. ],
  317. "text/plain": [
  318. " g energy\n",
  319. "atomic_number ion_number \n",
  320. "2 1 2 0.000000\n",
  321. " 1 2 40.813028\n",
  322. " 1 2 40.813086\n",
  323. " 1 4 40.813754\n",
  324. " 1 2 48.371294\n",
  325. " 1 2 48.371312\n",
  326. " 1 4 48.371509\n",
  327. " 1 4 48.371509\n",
  328. " 1 6 48.371581"
  329. ]
  330. },
  331. "execution_count": 10,
  332. "metadata": {},
  333. "output_type": "execute_result"
  334. }
  335. ],
  336. "source": [
  337. "x._get_all_levels_data().set_index(['atomic_number', 'ion_number']).loc[(2,1)]"
  338. ]
  339. },
  340. {
  341. "cell_type": "code",
  342. "execution_count": 11,
  343. "metadata": {
  344. "scrolled": false
  345. },
  346. "outputs": [
  347. {
  348. "data": {
  349. "text/html": [
  350. "<div>\n",
  351. "<style scoped>\n",
  352. " .dataframe tbody tr th:only-of-type {\n",
  353. " vertical-align: middle;\n",
  354. " }\n",
  355. "\n",
  356. " .dataframe tbody tr th {\n",
  357. " vertical-align: top;\n",
  358. " }\n",
  359. "\n",
  360. " .dataframe thead th {\n",
  361. " text-align: right;\n",
  362. " }\n",
  363. "</style>\n",
  364. "<table border=\"1\" class=\"dataframe\">\n",
  365. " <thead>\n",
  366. " <tr style=\"text-align: right;\">\n",
  367. " <th></th>\n",
  368. " <th></th>\n",
  369. " <th>g</th>\n",
  370. " <th>energy</th>\n",
  371. " </tr>\n",
  372. " <tr>\n",
  373. " <th>atomic_number</th>\n",
  374. " <th>ion_number</th>\n",
  375. " <th></th>\n",
  376. " <th></th>\n",
  377. " </tr>\n",
  378. " </thead>\n",
  379. " <tbody>\n",
  380. " <tr>\n",
  381. " <th rowspan=\"8\" valign=\"top\">2</th>\n",
  382. " <th>1</th>\n",
  383. " <td>2</td>\n",
  384. " <td>40.813028</td>\n",
  385. " </tr>\n",
  386. " <tr>\n",
  387. " <th>1</th>\n",
  388. " <td>2</td>\n",
  389. " <td>40.813086</td>\n",
  390. " </tr>\n",
  391. " <tr>\n",
  392. " <th>1</th>\n",
  393. " <td>4</td>\n",
  394. " <td>40.813754</td>\n",
  395. " </tr>\n",
  396. " <tr>\n",
  397. " <th>1</th>\n",
  398. " <td>2</td>\n",
  399. " <td>48.371294</td>\n",
  400. " </tr>\n",
  401. " <tr>\n",
  402. " <th>1</th>\n",
  403. " <td>2</td>\n",
  404. " <td>48.371312</td>\n",
  405. " </tr>\n",
  406. " <tr>\n",
  407. " <th>1</th>\n",
  408. " <td>4</td>\n",
  409. " <td>48.371509</td>\n",
  410. " </tr>\n",
  411. " <tr>\n",
  412. " <th>1</th>\n",
  413. " <td>4</td>\n",
  414. " <td>48.371509</td>\n",
  415. " </tr>\n",
  416. " <tr>\n",
  417. " <th>1</th>\n",
  418. " <td>6</td>\n",
  419. " <td>48.371581</td>\n",
  420. " </tr>\n",
  421. " </tbody>\n",
  422. "</table>\n",
  423. "</div>"
  424. ],
  425. "text/plain": [
  426. " g energy\n",
  427. "atomic_number ion_number \n",
  428. "2 1 2 40.813028\n",
  429. " 1 2 40.813086\n",
  430. " 1 4 40.813754\n",
  431. " 1 2 48.371294\n",
  432. " 1 2 48.371312\n",
  433. " 1 4 48.371509\n",
  434. " 1 4 48.371509\n",
  435. " 1 6 48.371581"
  436. ]
  437. },
  438. "execution_count": 11,
  439. "metadata": {},
  440. "output_type": "execute_result"
  441. }
  442. ],
  443. "source": [
  444. "atom_data._get_all_levels_data().set_index(['atomic_number', 'ion_number']).loc[(2,1)]"
  445. ]
  446. },
  447. {
  448. "cell_type": "markdown",
  449. "metadata": {},
  450. "source": [
  451. "What's happening? \n",
  452. "\n",
  453. "- Ground levels for **all ions** are ingested manually from NIST and placed on **top** of the DataFrame, despite how many ions we select. \n",
  454. "\n",
  455. "\n",
  456. "- In case we are not ingesting all ions for a selected atom (or some ions are not present in `gfall.dat`) a `.drop_duplicates(keep='last')` removes duplicated levels, keeping ground states for non-selected (or existing) ions at the top and the rest distributed across the DataFrame. \n",
  457. "\n",
  458. "\n",
  459. "- If we ingest all the ions for a selected atom, all the ground states in the top dissapear, and ground levels are distributed across the DataFrame."
  460. ]
  461. },
  462. {
  463. "cell_type": "markdown",
  464. "metadata": {},
  465. "source": [
  466. "My suspicion is: ion `(2,1)` exist in `gfall.dat` but there's no ground state there."
  467. ]
  468. },
  469. {
  470. "cell_type": "code",
  471. "execution_count": 12,
  472. "metadata": {},
  473. "outputs": [],
  474. "source": [
  475. "from carsus.io.kurucz import GFALLReader"
  476. ]
  477. },
  478. {
  479. "cell_type": "code",
  480. "execution_count": 13,
  481. "metadata": {},
  482. "outputs": [
  483. {
  484. "name": "stdout",
  485. "output_type": "stream",
  486. "text": [
  487. "[\u001b[1mcarsus.io.kurucz.gfall\u001b[0m][\u001b[1;33mWARNING\u001b[0m] A specific combination to identify unique levels from the gfall data has not been given. Defaulting to [\"energy\", \"j\"]. (\u001b[1mgfall.py\u001b[0m:72)\n"
  488. ]
  489. }
  490. ],
  491. "source": [
  492. "gfall_reader = GFALLReader('gfall.dat')"
  493. ]
  494. },
  495. {
  496. "cell_type": "code",
  497. "execution_count": 14,
  498. "metadata": {
  499. "scrolled": false
  500. },
  501. "outputs": [
  502. {
  503. "name": "stdout",
  504. "output_type": "stream",
  505. "text": [
  506. "[\u001b[1mcarsus.io.kurucz.gfall\u001b[0m][\u001b[1;37mINFO\u001b[0m ] Parsing GFALL gfall.dat (\u001b[1mgfall.py\u001b[0m:120)\n"
  507. ]
  508. },
  509. {
  510. "data": {
  511. "text/html": [
  512. "<div>\n",
  513. "<style scoped>\n",
  514. " .dataframe tbody tr th:only-of-type {\n",
  515. " vertical-align: middle;\n",
  516. " }\n",
  517. "\n",
  518. " .dataframe tbody tr th {\n",
  519. " vertical-align: top;\n",
  520. " }\n",
  521. "\n",
  522. " .dataframe thead th {\n",
  523. " text-align: right;\n",
  524. " }\n",
  525. "</style>\n",
  526. "<table border=\"1\" class=\"dataframe\">\n",
  527. " <thead>\n",
  528. " <tr style=\"text-align: right;\">\n",
  529. " <th></th>\n",
  530. " <th>energy</th>\n",
  531. " <th>j</th>\n",
  532. " <th>label</th>\n",
  533. " <th>method</th>\n",
  534. " </tr>\n",
  535. " <tr>\n",
  536. " <th>level_index</th>\n",
  537. " <th></th>\n",
  538. " <th></th>\n",
  539. " <th></th>\n",
  540. " <th></th>\n",
  541. " </tr>\n",
  542. " </thead>\n",
  543. " <tbody>\n",
  544. " <tr>\n",
  545. " <th>0</th>\n",
  546. " <td>329179.275</td>\n",
  547. " <td>0.5</td>\n",
  548. " <td>2P 2P</td>\n",
  549. " <td>meas</td>\n",
  550. " </tr>\n",
  551. " <tr>\n",
  552. " <th>1</th>\n",
  553. " <td>329179.744</td>\n",
  554. " <td>0.5</td>\n",
  555. " <td>2S 2S</td>\n",
  556. " <td>meas</td>\n",
  557. " </tr>\n",
  558. " <tr>\n",
  559. " <th>2</th>\n",
  560. " <td>329185.132</td>\n",
  561. " <td>1.5</td>\n",
  562. " <td>2P 2P</td>\n",
  563. " <td>meas</td>\n",
  564. " </tr>\n",
  565. " <tr>\n",
  566. " <th>3</th>\n",
  567. " <td>390140.803</td>\n",
  568. " <td>0.5</td>\n",
  569. " <td>3P 2P 164</td>\n",
  570. " <td>meas</td>\n",
  571. " </tr>\n",
  572. " <tr>\n",
  573. " <th>4</th>\n",
  574. " <td>390140.942</td>\n",
  575. " <td>0.5</td>\n",
  576. " <td>3S 2S 164</td>\n",
  577. " <td>meas</td>\n",
  578. " </tr>\n",
  579. " <tr>\n",
  580. " <th>5</th>\n",
  581. " <td>390142.535</td>\n",
  582. " <td>1.5</td>\n",
  583. " <td>3D 2D 164</td>\n",
  584. " <td>meas</td>\n",
  585. " </tr>\n",
  586. " <tr>\n",
  587. " <th>6</th>\n",
  588. " <td>390142.538</td>\n",
  589. " <td>1.5</td>\n",
  590. " <td>3P 2P 164</td>\n",
  591. " <td>meas</td>\n",
  592. " </tr>\n",
  593. " <tr>\n",
  594. " <th>7</th>\n",
  595. " <td>390143.114</td>\n",
  596. " <td>2.5</td>\n",
  597. " <td>3D 2D 164</td>\n",
  598. " <td>meas</td>\n",
  599. " </tr>\n",
  600. " </tbody>\n",
  601. "</table>\n",
  602. "</div>"
  603. ],
  604. "text/plain": [
  605. " energy j label method\n",
  606. "level_index \n",
  607. "0 329179.275 0.5 2P 2P meas\n",
  608. "1 329179.744 0.5 2S 2S meas\n",
  609. "2 329185.132 1.5 2P 2P meas\n",
  610. "3 390140.803 0.5 3P 2P 164 meas\n",
  611. "4 390140.942 0.5 3S 2S 164 meas\n",
  612. "5 390142.535 1.5 3D 2D 164 meas\n",
  613. "6 390142.538 1.5 3P 2P 164 meas\n",
  614. "7 390143.114 2.5 3D 2D 164 meas"
  615. ]
  616. },
  617. "execution_count": 14,
  618. "metadata": {},
  619. "output_type": "execute_result"
  620. }
  621. ],
  622. "source": [
  623. "# No ground state for (2,1)\n",
  624. "gfall_reader.levels.loc[(2,1)]"
  625. ]
  626. },
  627. {
  628. "cell_type": "markdown",
  629. "metadata": {},
  630. "source": [
  631. "**My question:** is the old code failing at appending ground states when the ion exist in `gfall.dat` but there's no zero `energy` level?"
  632. ]
  633. },
  634. {
  635. "cell_type": "code",
  636. "execution_count": null,
  637. "metadata": {},
  638. "outputs": [],
  639. "source": []
  640. }
  641. ],
  642. "metadata": {
  643. "kernelspec": {
  644. "display_name": "Python 3",
  645. "language": "python",
  646. "name": "python3"
  647. },
  648. "language_info": {
  649. "codemirror_mode": {
  650. "name": "ipython",
  651. "version": 3
  652. },
  653. "file_extension": ".py",
  654. "mimetype": "text/x-python",
  655. "name": "python",
  656. "nbconvert_exporter": "python",
  657. "pygments_lexer": "ipython3",
  658. "version": "3.6.7"
  659. },
  660. "widgets": {
  661. "application/vnd.jupyter.widget-state+json": {
  662. "state": {},
  663. "version_major": 2,
  664. "version_minor": 0
  665. }
  666. }
  667. },
  668. "nbformat": 4,
  669. "nbformat_minor": 4
  670. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement