SHARE
TWEET

Untitled

a guest Jun 17th, 2019 103 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {
  2.  "cells": [
  3.   {
  4.    "cell_type": "code",
  5.    "execution_count": 1,
  6.    "metadata": {},
  7.    "outputs": [
  8.     {
  9.      "name": "stderr",
  10.      "output_type": "stream",
  11.      "text": [
  12.       "/home/epassaro/miniconda3/envs/carsus/lib/python3.6/site-packages/tqdm/autonotebook/__init__.py:14: TqdmExperimentalWarning: Using `tqdm.autonotebook.tqdm` in notebook mode. Use `tqdm.tqdm` instead to force console mode (e.g. in jupyter console)\n",
  13.       "  \" (e.g. in jupyter console)\", TqdmExperimentalWarning)\n"
  14.      ]
  15.     }
  16.    ],
  17.    "source": [
  18.     "from sqlalchemy import create_engine, inspect\n",
  19.     "from sqlalchemy.orm import Session\n",
  20.     "from carsus.model import Level, DataSource  # I don't know so much about the Carsus data model"
  21.    ]
  22.   },
  23.   {
  24.    "cell_type": "code",
  25.    "execution_count": 2,
  26.    "metadata": {},
  27.    "outputs": [],
  28.    "source": [
  29.     "engine = create_engine('sqlite:////home/epassaro/carsus-db/test_databases/test.db')\n",
  30.     "connection = engine.connect()\n",
  31.     "\n",
  32.     "trans = connection.begin()\n",
  33.     "session = Session(bind=connection)"
  34.    ]
  35.   },
  36.   {
  37.    "cell_type": "code",
  38.    "execution_count": 3,
  39.    "metadata": {},
  40.    "outputs": [
  41.     {
  42.      "name": "stdout",
  43.      "output_type": "stream",
  44.      "text": [
  45.       "\n",
  46.       "Table: atom\n",
  47.       "Column: atomic_number\n",
  48.       "Column: symbol\n",
  49.       "Column: name\n",
  50.       "Column: group\n",
  51.       "Column: period\n",
  52.       "\n",
  53.       "Table: atom_quantity\n",
  54.       "Column: _value\n",
  55.       "Column: uncert\n",
  56.       "Column: method\n",
  57.       "Column: reference\n",
  58.       "Column: atom_qty_id\n",
  59.       "Column: atomic_number\n",
  60.       "Column: type\n",
  61.       "Column: data_source_id\n",
  62.       "\n",
  63.       "Table: data_source\n",
  64.       "Column: data_source_id\n",
  65.       "Column: short_name\n",
  66.       "Column: name\n",
  67.       "Column: description\n",
  68.       "Column: data_source_quality\n",
  69.       "\n",
  70.       "Table: e_collision\n",
  71.       "Column: e_col_id\n",
  72.       "Column: bt92_ttype\n",
  73.       "Column: bt92_cups\n",
  74.       "\n",
  75.       "Table: e_collision_qty\n",
  76.       "Column: _value\n",
  77.       "Column: uncert\n",
  78.       "Column: method\n",
  79.       "Column: reference\n",
  80.       "Column: e_col_qty_id\n",
  81.       "Column: e_col_id\n",
  82.       "Column: type\n",
  83.       "Column: data_source_id\n",
  84.       "\n",
  85.       "Table: e_collision_temp_strength\n",
  86.       "Column: e_col_temp_strength_id\n",
  87.       "Column: temp\n",
  88.       "Column: strength\n",
  89.       "Column: e_col_id\n",
  90.       "\n",
  91.       "Table: ion\n",
  92.       "Column: atomic_number\n",
  93.       "Column: ion_charge\n",
  94.       "\n",
  95.       "Table: ion_quantity\n",
  96.       "Column: _value\n",
  97.       "Column: uncert\n",
  98.       "Column: method\n",
  99.       "Column: reference\n",
  100.       "Column: ion_qty_id\n",
  101.       "Column: atomic_number\n",
  102.       "Column: ion_charge\n",
  103.       "Column: type\n",
  104.       "Column: data_source_id\n",
  105.       "\n",
  106.       "Table: level\n",
  107.       "Column: level_id\n",
  108.       "Column: atomic_number\n",
  109.       "Column: ion_charge\n",
  110.       "Column: data_source_id\n",
  111.       "Column: level_index\n",
  112.       "Column: configuration\n",
  113.       "Column: L\n",
  114.       "Column: J\n",
  115.       "Column: spin_multiplicity\n",
  116.       "Column: parity\n",
  117.       "Column: term\n",
  118.       "\n",
  119.       "Table: level_quantity\n",
  120.       "Column: _value\n",
  121.       "Column: uncert\n",
  122.       "Column: method\n",
  123.       "Column: reference\n",
  124.       "Column: level_qty_id\n",
  125.       "Column: level_id\n",
  126.       "Column: type\n",
  127.       "Column: data_source_id\n",
  128.       "\n",
  129.       "Table: line\n",
  130.       "Column: line_id\n",
  131.       "\n",
  132.       "Table: line_quantity\n",
  133.       "Column: _value\n",
  134.       "Column: uncert\n",
  135.       "Column: method\n",
  136.       "Column: reference\n",
  137.       "Column: line_qty_id\n",
  138.       "Column: line_id\n",
  139.       "Column: type\n",
  140.       "Column: data_source_id\n",
  141.       "Column: medium\n",
  142.       "\n",
  143.       "Table: temperature\n",
  144.       "Column: id\n",
  145.       "Column: value\n",
  146.       "\n",
  147.       "Table: transition\n",
  148.       "Column: transition_id\n",
  149.       "Column: type\n",
  150.       "Column: lower_level_id\n",
  151.       "Column: upper_level_id\n",
  152.       "Column: data_source_id\n",
  153.       "\n",
  154.       "Table: zeta\n",
  155.       "Column: id\n",
  156.       "Column: atomic_number\n",
  157.       "Column: ion_charge\n",
  158.       "Column: zeta\n",
  159.       "Column: temp_id\n",
  160.       "Column: data_source_id\n"
  161.      ]
  162.     }
  163.    ],
  164.    "source": [
  165.     "# Inspect the database\n",
  166.     "inspector = inspect(engine)\n",
  167.     "for table_name in inspector.get_table_names():\n",
  168.     "    print('\\nTable: %s' % table_name)\n",
  169.     "    for column in inspector.get_columns(table_name):\n",
  170.     "       print(\"Column: %s\" % column['name'])"
  171.    ]
  172.   },
  173.   {
  174.    "cell_type": "code",
  175.    "execution_count": 4,
  176.    "metadata": {},
  177.    "outputs": [],
  178.    "source": [
  179.     "# Trying with nist-asd data source\n",
  180.     "nist_ds = session.query(DataSource).filter(DataSource.short_name == 'nist-asd').one()"
  181.    ]
  182.   },
  183.   {
  184.    "cell_type": "code",
  185.    "execution_count": 5,
  186.    "metadata": {},
  187.    "outputs": [],
  188.    "source": [
  189.     "subq = (\n",
  190.     "        session.\n",
  191.     "            query(Level.level_id).\n",
  192.     "            filter(Level.atomic_number.in_([4])).\n",
  193.     "            filter(Level.data_source_id == nist_ds.data_source_id)\n",
  194.     "        ).subquery()"
  195.    ]
  196.   },
  197.   {
  198.    "cell_type": "code",
  199.    "execution_count": 6,
  200.    "metadata": {},
  201.    "outputs": [],
  202.    "source": [
  203.     "levels_data_q = (\n",
  204.     "        session.\n",
  205.     "        query(\n",
  206.     "            Level.level_id.label('level_id'),\n",
  207.     "            Level.atomic_number.label('atomic_number'),\n",
  208.     "            Level.ion_charge.label('ion_number'),\n",
  209.     "            Level.g.label('g'))\n",
  210.     "            ).join(subq,\n",
  211.     "            Level.level_id == subq.c.level_id\n",
  212.     "            )"
  213.    ]
  214.   },
  215.   {
  216.    "cell_type": "code",
  217.    "execution_count": 7,
  218.    "metadata": {},
  219.    "outputs": [
  220.     {
  221.      "data": {
  222.       "text/plain": [
  223.        "[(7, 4, 0, 1), (8, 4, 1, 2), (9, 4, 2, 1), (10, 4, 3, 2)]"
  224.       ]
  225.      },
  226.      "execution_count": 7,
  227.      "metadata": {},
  228.      "output_type": "execute_result"
  229.     }
  230.    ],
  231.    "source": [
  232.     "levels_data_q.all()  # Seems ok"
  233.    ]
  234.   },
  235.   {
  236.    "cell_type": "code",
  237.    "execution_count": 8,
  238.    "metadata": {},
  239.    "outputs": [],
  240.    "source": [
  241.     "# Trying with chianti_v8.0.2 data source (the one used in the fixtures)\n",
  242.     "ch_ds = session.query(DataSource).filter(DataSource.short_name == 'chianti_v8.0.2').one()"
  243.    ]
  244.   },
  245.   {
  246.    "cell_type": "code",
  247.    "execution_count": 9,
  248.    "metadata": {},
  249.    "outputs": [],
  250.    "source": [
  251.     "subq = (\n",
  252.     "        session.\n",
  253.     "            query(Level.level_id).\n",
  254.     "            filter(Level.atomic_number.in_([4])).\n",
  255.     "            filter(Level.data_source_id == ch_ds.data_source_id)\n",
  256.     "        ).subquery()"
  257.    ]
  258.   },
  259.   {
  260.    "cell_type": "code",
  261.    "execution_count": 10,
  262.    "metadata": {},
  263.    "outputs": [],
  264.    "source": [
  265.     "levels_data_q = (\n",
  266.     "        session.\n",
  267.     "        query(\n",
  268.     "            Level.level_id.label('level_id'),\n",
  269.     "            Level.atomic_number.label('atomic_number'),\n",
  270.     "            Level.ion_charge.label('ion_number'),\n",
  271.     "            Level.g.label('g'))\n",
  272.     "            ).join(subq,\n",
  273.     "            Level.level_id == subq.c.level_id\n",
  274.     "            )"
  275.    ]
  276.   },
  277.   {
  278.    "cell_type": "code",
  279.    "execution_count": 11,
  280.    "metadata": {},
  281.    "outputs": [
  282.     {
  283.      "data": {
  284.       "text/plain": [
  285.        "[]"
  286.       ]
  287.      },
  288.      "execution_count": 11,
  289.      "metadata": {},
  290.      "output_type": "execute_result"
  291.     }
  292.    ],
  293.    "source": [
  294.     "levels_data_q.all()"
  295.    ]
  296.   },
  297.   {
  298.    "cell_type": "code",
  299.    "execution_count": 12,
  300.    "metadata": {},
  301.    "outputs": [],
  302.    "source": [
  303.     "# subq for data source chianti_v8.0.2 gives an empty list -> this is the problem!"
  304.    ]
  305.   }
  306.  ],
  307.  "metadata": {
  308.   "kernelspec": {
  309.    "display_name": "Python 3",
  310.    "language": "python",
  311.    "name": "python3"
  312.   },
  313.   "language_info": {
  314.    "codemirror_mode": {
  315.     "name": "ipython",
  316.     "version": 3
  317.    },
  318.    "file_extension": ".py",
  319.    "mimetype": "text/x-python",
  320.    "name": "python",
  321.    "nbconvert_exporter": "python",
  322.    "pygments_lexer": "ipython3",
  323.    "version": "3.6.7"
  324.   },
  325.   "widgets": {
  326.    "application/vnd.jupyter.widget-state+json": {
  327.     "state": {},
  328.     "version_major": 2,
  329.     "version_minor": 0
  330.    }
  331.   }
  332.  },
  333.  "nbformat": 4,
  334.  "nbformat_minor": 2
  335. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top