Skip to content

Instantly share code, notes, and snippets.

@epassaro
Last active August 7, 2019 12:43
Show Gist options
  • Save epassaro/82be42e2491511c07bb566354927a780 to your computer and use it in GitHub Desktop.
Save epassaro/82be42e2491511c07bb566354927a780 to your computer and use it in GitHub Desktop.
GFALL levels & lines
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"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"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Initializing the database at sqlite://\n",
"Ingesting basic atomic data\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\n",
"session = init_db()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# This is the usual SQL stuff"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"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": [
"ioniz_energies_ingester = NISTIonizationEnergiesIngester(session, spectra=\"Si\")\n",
"ioniz_energies_ingester.ingest(ionization_energies=True, ground_levels=True)\n",
"session.commit()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"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",
"[\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 Si 1\n",
"Ingesting levels for Si 2\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 Si 1\n",
"Ingesting lines for Si 2\n"
]
}
],
"source": [
"gfall_ingester = GFALLIngester(session, fname=\"../gfall.dat\", ions=\"Si 1-2\")\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=\"Si\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Now we try to replicate the same DataFrame with our new class"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"from carsus.io.kurucz.gfall import GFALL"
]
},
{
"cell_type": "code",
"execution_count": 6,
"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",
"[\u001b[1mpy.warnings \u001b[0m][\u001b[1;33mWARNING\u001b[0m] /home/epassaro/Desktop/gsoc/notebooks/carsus/io/kurucz/gfall.py:546: PerformanceWarning: indexing past lexsort depth may impact performance.\n",
" df = gf.lines.loc[ion]\n",
" (\u001b[1mwarnings.py\u001b[0m:99)\n",
"[\u001b[1mpy.warnings \u001b[0m][\u001b[1;33mWARNING\u001b[0m] /home/epassaro/Desktop/gsoc/notebooks/carsus/io/kurucz/gfall.py:546: PerformanceWarning: indexing past lexsort depth may impact performance.\n",
" df = gf.lines.loc[ion]\n",
" (\u001b[1mwarnings.py\u001b[0m:99)\n",
"[\u001b[1mpy.warnings \u001b[0m][\u001b[1;33mWARNING\u001b[0m] /home/epassaro/Desktop/gsoc/notebooks/carsus/io/kurucz/gfall.py:549: PerformanceWarning: indexing past lexsort depth may impact performance.\n",
" lvl_index2id = levels.set_index(['atomic_number', 'ion_number']).loc[ion]\n",
" (\u001b[1mwarnings.py\u001b[0m:99)\n",
"[\u001b[1mpy.warnings \u001b[0m][\u001b[1;33mWARNING\u001b[0m] /home/epassaro/Desktop/gsoc/notebooks/carsus/io/kurucz/gfall.py:549: PerformanceWarning: indexing past lexsort depth may impact performance.\n",
" lvl_index2id = levels.set_index(['atomic_number', 'ion_number']).loc[ion]\n",
" (\u001b[1mwarnings.py\u001b[0m:99)\n",
"[\u001b[1mpy.warnings \u001b[0m][\u001b[1;33mWARNING\u001b[0m] /home/epassaro/miniconda3/envs/carsus/lib/python3.6/site-packages/pandas/core/frame.py:6692: FutureWarning: Sorting because non-concatenation axis is not aligned. A future version\n",
"of pandas will change to not sort by default.\n",
"\n",
"To accept the future behavior, pass 'sort=False'.\n",
"\n",
"To retain the current behavior and silence the warning, pass 'sort=True'.\n",
"\n",
" sort=sort)\n",
" (\u001b[1mwarnings.py\u001b[0m:99)\n",
"[\u001b[1mpy.warnings \u001b[0m][\u001b[1;33mWARNING\u001b[0m] /home/epassaro/miniconda3/envs/carsus/lib/python3.6/site-packages/pandas/core/frame.py:6692: FutureWarning: Sorting because non-concatenation axis is not aligned. A future version\n",
"of pandas will change to not sort by default.\n",
"\n",
"To accept the future behavior, pass 'sort=False'.\n",
"\n",
"To retain the current behavior and silence the warning, pass 'sort=True'.\n",
"\n",
" sort=sort)\n",
" (\u001b[1mwarnings.py\u001b[0m:99)\n"
]
}
],
"source": [
"x = GFALL('./gfall.dat', 'Si 1-2')"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"# Saving to HDF5\n",
"x.to_hdf('test_si2_levels_lines.h5')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Compare the two DataFrames"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"levels = pd.read_hdf('test_si2_levels_lines.h5', key='levels') \n",
"lines = pd.read_hdf('test_si2_levels_lines.h5', key='lines') "
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[\u001b[1mpy.warnings \u001b[0m][\u001b[1;33mWARNING\u001b[0m] /home/epassaro/miniconda3/envs/carsus/lib/python3.6/site-packages/pandas/core/frame.py:6692: FutureWarning: Sorting because non-concatenation axis is not aligned. A future version\n",
"of pandas will change to not sort by default.\n",
"\n",
"To accept the future behavior, pass 'sort=False'.\n",
"\n",
"To retain the current behavior and silence the warning, pass 'sort=True'.\n",
"\n",
" sort=sort)\n",
" (\u001b[1mwarnings.py\u001b[0m:99)\n",
"[\u001b[1mpy.warnings \u001b[0m][\u001b[1;33mWARNING\u001b[0m] /home/epassaro/miniconda3/envs/carsus/lib/python3.6/site-packages/pandas/core/frame.py:6692: FutureWarning: Sorting because non-concatenation axis is not aligned. A future version\n",
"of pandas will change to not sort by default.\n",
"\n",
"To accept the future behavior, pass 'sort=False'.\n",
"\n",
"To retain the current behavior and silence the warning, pass 'sort=True'.\n",
"\n",
" sort=sort)\n",
" (\u001b[1mwarnings.py\u001b[0m:99)\n"
]
},
{
"data": {
"text/plain": [
"(272, 3)"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"atom_data.levels_prepared.shape"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"energy 272\n",
"g 272\n",
"metastable 272\n",
"dtype: int64"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"atom_data.levels_prepared.eq(levels).sum()"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(1815, 8)"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"atom_data.lines_prepared.shape"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"line_id 1815\n",
"wavelength 1815\n",
"f_ul 1815\n",
"f_lu 1815\n",
"nu 1815\n",
"B_lu 1815\n",
"B_ul 1815\n",
"A_ul 1815\n",
"dtype: int64"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"atom_data.lines_prepared.eq(lines).sum()"
]
}
],
"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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment