Skip to content

Commit

Permalink
133 populate adlerdata from database (#162)
Browse files Browse the repository at this point in the history
* Populating AdlerData object from database.

* One more unit test, bug fix

* Explanatory notebook.
  • Loading branch information
astronomerritt authored Aug 27, 2024
1 parent 9f1077c commit 4ce7bb3
Show file tree
Hide file tree
Showing 8 changed files with 481 additions and 10 deletions.
318 changes: 318 additions & 0 deletions notebooks/adlerdata_in_and_out.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,318 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "55a1b889-7fb4-4d73-a9d0-23ab1bdb4dcb",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import sqlite3\n",
"\n",
"from adler.dataclasses.AdlerData import AdlerData\n",
"from adler.dataclasses.AdlerPlanetoid import AdlerPlanetoid\n",
"from adler.utilities.tests_utilities import get_test_data_filepath"
]
},
{
"cell_type": "markdown",
"id": "5264c132-e86d-4415-bfd4-cb1856d2fc33",
"metadata": {},
"source": [
"This is a quick notebook demonstrating how Adler's calculated values can be stored and then retrieved for later."
]
},
{
"cell_type": "markdown",
"id": "8baa263b-5f45-4f5a-b13e-565a5e2d181b",
"metadata": {},
"source": [
"First, let's make our AdlerPlanetoid object. In this case, we're populating it from a testing SQL database."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b04683c9-36db-4320-b6b7-7ef487aaf02e",
"metadata": {},
"outputs": [],
"source": [
"ssoid = \"8268570668335894776\"\n",
"test_db_path = get_test_data_filepath(\"testing_database.db\")\n",
"test_planetoid = AdlerPlanetoid.construct_from_SQL(ssoid, test_db_path, filter_list=[\"g\", \"r\"])"
]
},
{
"cell_type": "markdown",
"id": "e1de656a-1a10-418a-8646-f0e57d811dc6",
"metadata": {},
"source": [
"Now let's make up some pretend Adler calculated values, and populate the AdlerData object stored in AdlerPlanetoid."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b6e1a766-56fb-4682-8466-b42d5aa80ad2",
"metadata": {},
"outputs": [],
"source": [
"g_model_1 = {\n",
" \"model_name\": \"model_1\",\n",
" \"phaseAngle_min\": 31.0,\n",
" \"phaseAngle_range\": 32.0,\n",
" \"nobs\": 33,\n",
" \"arc\": 34.0,\n",
" \"H\": 35.0,\n",
" \"H_err\": 36.0,\n",
" \"phase_parameter_1\": 37.0,\n",
" \"phase_parameter_1_err\": 38.0,\n",
"}\n",
"\n",
"r_model_2 = {\n",
" \"model_name\": \"model_2\",\n",
" \"phaseAngle_min\": 41.0,\n",
" \"phaseAngle_range\": 42.0,\n",
" \"nobs\": 43,\n",
" \"arc\": 44.0,\n",
" \"H\": 45.0,\n",
" \"H_err\": 46.0,\n",
" \"phase_parameter_1\": 47.0,\n",
" \"phase_parameter_1_err\": 48.0,\n",
" \"phase_parameter_2\": 49.0,\n",
" \"phase_parameter_2_err\": 50.0,\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "63635480-5b9f-49a4-97f8-a70cce410829",
"metadata": {},
"outputs": [],
"source": [
"test_planetoid.AdlerData.populate_phase_parameters(\"g\", **g_model_1)\n",
"test_planetoid.AdlerData.populate_phase_parameters(\"r\", **r_model_2)"
]
},
{
"cell_type": "markdown",
"id": "1c5b2ebe-094f-4dbc-b5ef-594d60ec1b28",
"metadata": {},
"source": [
"Now we can write these out."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1e795fe1-afa7-4921-91f7-3dfc38240f80",
"metadata": {},
"outputs": [],
"source": [
"database_filepath = \"./gen_test_data/example_AdlerData_database.db\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b7637fbc-2b03-46fa-95f7-0e760b54d9e2",
"metadata": {},
"outputs": [],
"source": [
"test_planetoid.AdlerData.write_row_to_database(database_filepath)"
]
},
{
"cell_type": "markdown",
"id": "4e9eebdd-d844-4261-8521-c04688d3813a",
"metadata": {},
"source": [
"We'll use Pandas to look at what we just wrote out."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5bc42334-e6ad-451a-a3f3-17794206c82b",
"metadata": {},
"outputs": [],
"source": [
"con = sqlite3.connect(database_filepath)\n",
"adler_data_out = pd.read_sql(\"SELECT * from AdlerData\", con)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b4cb5d43-33b3-4fca-9a9b-52b781de7fb1",
"metadata": {},
"outputs": [],
"source": [
"adler_data_out"
]
},
{
"cell_type": "markdown",
"id": "3e77e0ab-1310-40f6-9380-499705849960",
"metadata": {},
"source": [
"Note that write_row_to_database() method always appends. So:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "810379ff-75bd-4c81-8ad9-66334e6ff9c5",
"metadata": {},
"outputs": [],
"source": [
"test_planetoid.AdlerData.write_row_to_database(database_filepath)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9e89559b-9500-4416-9d11-92bec442406a",
"metadata": {},
"outputs": [],
"source": [
"con = sqlite3.connect(database_filepath)\n",
"adler_data_out = pd.read_sql(\"SELECT * from AdlerData\", con)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "894623fa-7128-4518-9ca6-1b2a94d175d1",
"metadata": {},
"outputs": [],
"source": [
"adler_data_out"
]
},
{
"cell_type": "markdown",
"id": "49b89142-765f-4115-a011-d93c7c12737d",
"metadata": {},
"source": [
"Now we have added two rows."
]
},
{
"cell_type": "markdown",
"id": "65cefda8-ac98-4b04-ad4e-2175fb22b37c",
"metadata": {},
"source": [
"So perhaps we have an AdlerPlanetoid object and this time, we want to load in some previously calculated values for comparison. This is extremely easy. We'll do it on the AdlerPlanetoid object we already made."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5604ea13-4a37-44e2-bb2e-59ec2461e805",
"metadata": {},
"outputs": [],
"source": [
"test_planetoid.attach_previous_adler_data(database_filepath)"
]
},
{
"cell_type": "markdown",
"id": "8139a2c1-aa8c-4a57-86f6-5d3ac5923063",
"metadata": {},
"source": [
"This can be more easily accessed and read:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a926d90a-5d7d-4185-9f00-f06a51e06739",
"metadata": {},
"outputs": [],
"source": [
"test_planetoid.PreviousAdlerData.print_data()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "45f810b5-4d9f-46d5-8fcc-d6dddc8e6b0e",
"metadata": {},
"outputs": [],
"source": [
"test_planetoid.PreviousAdlerData.get_phase_parameters_in_filter(\"g\", \"model_1\").__dict__"
]
},
{
"cell_type": "markdown",
"id": "f0455add-ca30-417b-92c6-6cceb6f67363",
"metadata": {},
"source": [
"Or, if you don't want to work with an existing AdlerPlanetoid object, you can directly populate an AdlerData object from a database."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6ded7e1d-1016-4eb7-8442-3ff474c0d715",
"metadata": {},
"outputs": [],
"source": [
"adler_data_object = AdlerData(ssoid, [\"g\", \"r\"])\n",
"adler_data_object.populate_from_database(database_filepath)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "66a87ed1-3d99-4698-9c1f-5e6888989f0c",
"metadata": {},
"outputs": [],
"source": [
"adler_data_object.print_data()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "41dc5ab2-961e-497a-8dad-c8fb35043923",
"metadata": {},
"outputs": [],
"source": [
"adler_data_object.get_phase_parameters_in_filter(\"g\", \"model_1\").__dict__"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8a7ada08-dc87-4f5e-b72f-32caa164c210",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.10.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Binary file not shown.
Loading

0 comments on commit 4ce7bb3

Please sign in to comment.