Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "`I'm going to illustrate a problem I found recently`"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 1,
- "metadata": {},
- "outputs": [
- {
- "name": "stderr",
- "output_type": "stream",
- "text": [
- "/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",
- " \" (e.g. in jupyter console)\", TqdmExperimentalWarning)\n"
- ]
- }
- ],
- "source": [
- "import numpy as np\n",
- "import pandas as pd\n",
- "from carsus import init_db\n",
- "from carsus.model import Atom, Ion, Level, Line\n",
- "from carsus.io.output import AtomData\n",
- "from carsus.io.nist import NISTIonizationEnergiesIngester\n",
- "from carsus.io.kurucz import GFALLIngester, GFALLReader\n",
- "\n",
- "import warnings\n",
- "warnings.simplefilter('ignore')"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# Ingesting `He` levels with SQL database"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "metadata": {
- "scrolled": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Initializing the database at sqlite://\n",
- "Ingesting basic atomic data\n",
- "Downloading ionization energies from the NIST Atomic Spectra Database\n",
- "Ingesting ionization energies from nist-asd\n",
- "Ingesting ground levels from nist-asd\n"
- ]
- }
- ],
- "source": [
- "session = init_db()\n",
- "\n",
- "ioniz_energies_ingester = NISTIonizationEnergiesIngester(session, spectra=\"He\")\n",
- "ioniz_energies_ingester.ingest(ionization_energies=True, ground_levels=True)\n",
- "session.commit()"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 3,
- "metadata": {
- "scrolled": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[\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",
- "[\u001b[1mcarsus.io.kurucz.gfall\u001b[0m][\u001b[1;37mINFO\u001b[0m ] Parsing GFALL ../gfall.dat (\u001b[1mgfall.py\u001b[0m:120)\n",
- "Ingesting levels from ku_latest\n",
- "Ingesting levels for He 0\n",
- "Ingesting levels for He 1\n",
- "[\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",
- "Ingesting lines from ku_latest\n",
- "Ingesting lines for He 0\n",
- "Ingesting lines for He 1\n"
- ]
- }
- ],
- "source": [
- "gfall_ingester = GFALLIngester(session, fname=\"../gfall.dat\", ions=\"He\")\n",
- "gfall_ingester.ingest(levels=True, lines=True)\n",
- "session.commit()"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 4,
- "metadata": {},
- "outputs": [],
- "source": [
- "atom_data = AtomData(session, selected_atoms=\"He\")"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Check how many levels are ingested in the first stage "
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 5,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "(752, 4)"
- ]
- },
- "execution_count": 5,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "atom_data._get_all_levels_data().shape"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# Now try the same thing with the new code"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 6,
- "metadata": {},
- "outputs": [],
- "source": [
- "from carsus.io.kurucz.gfall import GFALL"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 7,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[\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",
- "Downloading ionization energies from the NIST Atomic Spectra Database\n",
- "[\u001b[1mcarsus.io.kurucz.gfall\u001b[0m][\u001b[1;37mINFO\u001b[0m ] Parsing GFALL ./gfall.dat (\u001b[1mgfall.py\u001b[0m:120)\n",
- "[\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"
- ]
- }
- ],
- "source": [
- "x = GFALL('./gfall.dat', 'He')"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 8,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "(753, 4)"
- ]
- },
- "execution_count": 8,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "x._get_all_levels_data().shape"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Oops! There's one more level than `atom_data`! From here everything fails:"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 9,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "energy 745\n",
- "g 749\n",
- "metastable 753\n",
- "dtype: int64"
- ]
- },
- "execution_count": 9,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "atom_data.levels_prepared.eq(x.levels_prepared).sum()"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "The extra level is the ground level for `(2,1)`"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 10,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/html": [
- "<div>\n",
- "<style scoped>\n",
- " .dataframe tbody tr th:only-of-type {\n",
- " vertical-align: middle;\n",
- " }\n",
- "\n",
- " .dataframe tbody tr th {\n",
- " vertical-align: top;\n",
- " }\n",
- "\n",
- " .dataframe thead th {\n",
- " text-align: right;\n",
- " }\n",
- "</style>\n",
- "<table border=\"1\" class=\"dataframe\">\n",
- " <thead>\n",
- " <tr style=\"text-align: right;\">\n",
- " <th></th>\n",
- " <th></th>\n",
- " <th>g</th>\n",
- " <th>energy</th>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>atomic_number</th>\n",
- " <th>ion_number</th>\n",
- " <th></th>\n",
- " <th></th>\n",
- " </tr>\n",
- " </thead>\n",
- " <tbody>\n",
- " <tr>\n",
- " <th rowspan=\"9\" valign=\"top\">2</th>\n",
- " <th>1</th>\n",
- " <td>2</td>\n",
- " <td>0.000000</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>1</th>\n",
- " <td>2</td>\n",
- " <td>40.813028</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>1</th>\n",
- " <td>2</td>\n",
- " <td>40.813086</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>1</th>\n",
- " <td>4</td>\n",
- " <td>40.813754</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>1</th>\n",
- " <td>2</td>\n",
- " <td>48.371294</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>1</th>\n",
- " <td>2</td>\n",
- " <td>48.371312</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>1</th>\n",
- " <td>4</td>\n",
- " <td>48.371509</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>1</th>\n",
- " <td>4</td>\n",
- " <td>48.371509</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>1</th>\n",
- " <td>6</td>\n",
- " <td>48.371581</td>\n",
- " </tr>\n",
- " </tbody>\n",
- "</table>\n",
- "</div>"
- ],
- "text/plain": [
- " g energy\n",
- "atomic_number ion_number \n",
- "2 1 2 0.000000\n",
- " 1 2 40.813028\n",
- " 1 2 40.813086\n",
- " 1 4 40.813754\n",
- " 1 2 48.371294\n",
- " 1 2 48.371312\n",
- " 1 4 48.371509\n",
- " 1 4 48.371509\n",
- " 1 6 48.371581"
- ]
- },
- "execution_count": 10,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "x._get_all_levels_data().set_index(['atomic_number', 'ion_number']).loc[(2,1)]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 11,
- "metadata": {
- "scrolled": false
- },
- "outputs": [
- {
- "data": {
- "text/html": [
- "<div>\n",
- "<style scoped>\n",
- " .dataframe tbody tr th:only-of-type {\n",
- " vertical-align: middle;\n",
- " }\n",
- "\n",
- " .dataframe tbody tr th {\n",
- " vertical-align: top;\n",
- " }\n",
- "\n",
- " .dataframe thead th {\n",
- " text-align: right;\n",
- " }\n",
- "</style>\n",
- "<table border=\"1\" class=\"dataframe\">\n",
- " <thead>\n",
- " <tr style=\"text-align: right;\">\n",
- " <th></th>\n",
- " <th></th>\n",
- " <th>g</th>\n",
- " <th>energy</th>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>atomic_number</th>\n",
- " <th>ion_number</th>\n",
- " <th></th>\n",
- " <th></th>\n",
- " </tr>\n",
- " </thead>\n",
- " <tbody>\n",
- " <tr>\n",
- " <th rowspan=\"8\" valign=\"top\">2</th>\n",
- " <th>1</th>\n",
- " <td>2</td>\n",
- " <td>40.813028</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>1</th>\n",
- " <td>2</td>\n",
- " <td>40.813086</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>1</th>\n",
- " <td>4</td>\n",
- " <td>40.813754</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>1</th>\n",
- " <td>2</td>\n",
- " <td>48.371294</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>1</th>\n",
- " <td>2</td>\n",
- " <td>48.371312</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>1</th>\n",
- " <td>4</td>\n",
- " <td>48.371509</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>1</th>\n",
- " <td>4</td>\n",
- " <td>48.371509</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>1</th>\n",
- " <td>6</td>\n",
- " <td>48.371581</td>\n",
- " </tr>\n",
- " </tbody>\n",
- "</table>\n",
- "</div>"
- ],
- "text/plain": [
- " g energy\n",
- "atomic_number ion_number \n",
- "2 1 2 40.813028\n",
- " 1 2 40.813086\n",
- " 1 4 40.813754\n",
- " 1 2 48.371294\n",
- " 1 2 48.371312\n",
- " 1 4 48.371509\n",
- " 1 4 48.371509\n",
- " 1 6 48.371581"
- ]
- },
- "execution_count": 11,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "atom_data._get_all_levels_data().set_index(['atomic_number', 'ion_number']).loc[(2,1)]"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "What's happening? \n",
- "\n",
- "- Ground levels for **all ions** are ingested manually from NIST and placed on **top** of the DataFrame, despite how many ions we select. \n",
- "\n",
- "\n",
- "- 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",
- "\n",
- "\n",
- "- 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."
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "My suspicion is: ion `(2,1)` exist in `gfall.dat` but there's no ground state there."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 12,
- "metadata": {},
- "outputs": [],
- "source": [
- "from carsus.io.kurucz import GFALLReader"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 13,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[\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"
- ]
- }
- ],
- "source": [
- "gfall_reader = GFALLReader('gfall.dat')"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 14,
- "metadata": {
- "scrolled": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[\u001b[1mcarsus.io.kurucz.gfall\u001b[0m][\u001b[1;37mINFO\u001b[0m ] Parsing GFALL gfall.dat (\u001b[1mgfall.py\u001b[0m:120)\n"
- ]
- },
- {
- "data": {
- "text/html": [
- "<div>\n",
- "<style scoped>\n",
- " .dataframe tbody tr th:only-of-type {\n",
- " vertical-align: middle;\n",
- " }\n",
- "\n",
- " .dataframe tbody tr th {\n",
- " vertical-align: top;\n",
- " }\n",
- "\n",
- " .dataframe thead th {\n",
- " text-align: right;\n",
- " }\n",
- "</style>\n",
- "<table border=\"1\" class=\"dataframe\">\n",
- " <thead>\n",
- " <tr style=\"text-align: right;\">\n",
- " <th></th>\n",
- " <th>energy</th>\n",
- " <th>j</th>\n",
- " <th>label</th>\n",
- " <th>method</th>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>level_index</th>\n",
- " <th></th>\n",
- " <th></th>\n",
- " <th></th>\n",
- " <th></th>\n",
- " </tr>\n",
- " </thead>\n",
- " <tbody>\n",
- " <tr>\n",
- " <th>0</th>\n",
- " <td>329179.275</td>\n",
- " <td>0.5</td>\n",
- " <td>2P 2P</td>\n",
- " <td>meas</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>1</th>\n",
- " <td>329179.744</td>\n",
- " <td>0.5</td>\n",
- " <td>2S 2S</td>\n",
- " <td>meas</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>2</th>\n",
- " <td>329185.132</td>\n",
- " <td>1.5</td>\n",
- " <td>2P 2P</td>\n",
- " <td>meas</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>3</th>\n",
- " <td>390140.803</td>\n",
- " <td>0.5</td>\n",
- " <td>3P 2P 164</td>\n",
- " <td>meas</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>4</th>\n",
- " <td>390140.942</td>\n",
- " <td>0.5</td>\n",
- " <td>3S 2S 164</td>\n",
- " <td>meas</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>5</th>\n",
- " <td>390142.535</td>\n",
- " <td>1.5</td>\n",
- " <td>3D 2D 164</td>\n",
- " <td>meas</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>6</th>\n",
- " <td>390142.538</td>\n",
- " <td>1.5</td>\n",
- " <td>3P 2P 164</td>\n",
- " <td>meas</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>7</th>\n",
- " <td>390143.114</td>\n",
- " <td>2.5</td>\n",
- " <td>3D 2D 164</td>\n",
- " <td>meas</td>\n",
- " </tr>\n",
- " </tbody>\n",
- "</table>\n",
- "</div>"
- ],
- "text/plain": [
- " energy j label method\n",
- "level_index \n",
- "0 329179.275 0.5 2P 2P meas\n",
- "1 329179.744 0.5 2S 2S meas\n",
- "2 329185.132 1.5 2P 2P meas\n",
- "3 390140.803 0.5 3P 2P 164 meas\n",
- "4 390140.942 0.5 3S 2S 164 meas\n",
- "5 390142.535 1.5 3D 2D 164 meas\n",
- "6 390142.538 1.5 3P 2P 164 meas\n",
- "7 390143.114 2.5 3D 2D 164 meas"
- ]
- },
- "execution_count": 14,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "# No ground state for (2,1)\n",
- "gfall_reader.levels.loc[(2,1)]"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**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?"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": []
- }
- ],
- "metadata": {
- "kernelspec": {
- "display_name": "Python 3",
- "language": "python",
- "name": "python3"
- },
- "language_info": {
- "codemirror_mode": {
- "name": "ipython",
- "version": 3
- },
- "file_extension": ".py",
- "mimetype": "text/x-python",
- "name": "python",
- "nbconvert_exporter": "python",
- "pygments_lexer": "ipython3",
- "version": "3.6.7"
- },
- "widgets": {
- "application/vnd.jupyter.widget-state+json": {
- "state": {},
- "version_major": 2,
- "version_minor": 0
- }
- }
- },
- "nbformat": 4,
- "nbformat_minor": 4
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement