forked from snowex-hackweek/website-2024
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Working through snowex-hackweek#2 of the db tutorial
- Loading branch information
1 parent
ab89920
commit dc4ccbe
Showing
2 changed files
with
62 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,143 +2,113 @@ | |
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"id": "dangerous-decrease", | ||
"id": "443a62b0", | ||
"metadata": {}, | ||
"source": [ | ||
"# How is the Database Structured?\n", | ||
"\n", | ||
"The goal of the database is to hold as much of the SnowEx data in one place and make it easier to \n", | ||
"do research with. With that in mind follow the steps below to see how the the data base is structured.\n", | ||
"\n", | ||
"\n", | ||
"## What were about to do\n", | ||
"\n", | ||
"1. Access the database using the snowexsql python library \n", | ||
"2. Query the database to see the underlying tables\n", | ||
"3. Query each table to see what columns are available\n", | ||
"4. Query to see what datasets are available\n", | ||
"\n", | ||
"## Process\n", | ||
"\n", | ||
"### Step 1: Get a database session" | ||
"do research with. With that in mind follow the steps below to see how the the data base is structured.\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "artistic-thought", | ||
"cell_type": "markdown", | ||
"id": "e1b9c8e3", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Import the connection function from the snowexsql library\n", | ||
"from snowexsql.db import get_db\n", | ||
"### Where do datasets live (i.e. tables)?\n", | ||
"\n", | ||
"# This is what you will use for all of hackweek to access the db\n", | ||
"db_name = 'snow:[email protected]/snowex'\n", | ||
"Data in the database lives in 1 of 4 places. \n", | ||
"\n", | ||
"# Using the function get_db, we receive 2 ways to interact with the database\n", | ||
"engine, session = get_db(db_name)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "intensive-tracy", | ||
"metadata": {}, | ||
"source": [ | ||
"### Step 2: Query the DB to see what tables are available" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "random-healthcare", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Output the list of tables in the database \n", | ||
"engine.table_names()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "varying-anime", | ||
"metadata": {}, | ||
"source": [ | ||
"We can also import classes that reflect these tables in python!" | ||
"<img src=\"./images/structure.png\" alt=\"table_structure\" width=\"1000px\">\n", | ||
"\n", | ||
"\n", | ||
"The 4th table is a table detailing the site informations. Lots and lots of metadata for which the API has not been written yet." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "imposed-thomson", | ||
"execution_count": 3, | ||
"id": "a4a28a93", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from snowexsql.data import LayerData, PointData, ImageData, SiteData" | ||
"from snowexsql.api import PointMeasurements, LayerMeasurements, RasterMeasurements" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "liberal-binary", | ||
"id": "a4fbe854", | ||
"metadata": {}, | ||
"source": [ | ||
"### Step 3: Query a Table to see what columns you can use!\n", | ||
"\n", | ||
"In our python library [snowexsql](https://github.com/SnowEx/snowexsql/) there are classes that reflect the database tables. This makes it easier to use in python.\n", | ||
"For google purposes this is also called Object Relational Mapping (ORM). \n", | ||
"\n", | ||
"Import the table class from [`snowexsql.data`](https://snowexsql.readthedocs.io/en/latest/snowexsql.html#module-snowexsql.data) and [`snowexsql.db.get_table_attributes`](https://snowexsql.readthedocs.io/en/latest/snowexsql.html#snowexsql.db.get_table_attributes). The use `get_table_attributes` to see what\n", | ||
"columns are in each table!" | ||
"### What info is available?\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "operational-province", | ||
"execution_count": 10, | ||
"id": "05edebd8", | ||
"metadata": {}, | ||
"outputs": [], | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"These are the available columns in the table:\n", | ||
" \n", | ||
"* version_number\n", | ||
"* equipment\n", | ||
"* value\n", | ||
"* latitude\n", | ||
"* longitude\n", | ||
"* northing\n", | ||
"* easting\n", | ||
"* elevation\n", | ||
"* utm_zone\n", | ||
"* geom\n", | ||
"* time\n", | ||
"* site_id\n", | ||
"* site_name\n", | ||
"* date\n", | ||
"* time_created\n", | ||
"* time_updated\n", | ||
"* id\n", | ||
"* doi\n", | ||
"* date_accessed\n", | ||
"* instrument\n", | ||
"* type\n", | ||
"* units\n", | ||
"* observers\n", | ||
"\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"# Import the class reflecting the points table in the db\n", | ||
"from snowexsql.data import PointData\n", | ||
"\n", | ||
"# Import the function to investigate a table\n", | ||
"from snowexsql.db import get_table_attributes\n", | ||
"from snowexsql.api import PointMeasurements as measurements\n", | ||
"\n", | ||
"# Use the function to see what columns are available to use. \n", | ||
"db_columns = get_table_attributes(PointData)\n", | ||
"# Grab one measurment to see what attributes are available\n", | ||
"df = measurements.from_filter(type=\"density\", limit=1)\n", | ||
"\n", | ||
"# Print out the results nicely\n", | ||
"print(\"These are the available columns in the table:\\n \\n* {}\\n\".format('\\n* '.join(db_columns)))\n" | ||
"print(\"These are the available columns in the table:\\n \\n* {}\\n\".format('\\n* '.join(df.columns)))\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "fatal-collection", | ||
"id": "6bc99e67", | ||
"metadata": {}, | ||
"source": [ | ||
"**Try this:** Using what we just did, use `get_table_attributes` to look at the other tables.\n", | ||
"\n", | ||
"**Hint**: You have to change the table class name in two places in the above code block.\n", | ||
"**Try this:** Using what we just did, but swap out PointMeasurements for LayerMeasurements.\n", | ||
"\n", | ||
"## Discussion: What's the difference in these tables?\n", | ||
"`RasterMeasurements` is still being built out for ease of use but it would still have some limitations. So for now it doesnt have the `from_filter` function.\n", | ||
"\n", | ||
"If working by yourself checkout the readthedocs page on [database structure](https://snowexsql.readthedocs.io/en/latest/database_structure.html) to see how data gets categorized. \n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "92bfaa6c-d489-4abc-a485-757cb914358a", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Close out the session to avoid hanging transactions\n", | ||
"session.close()" | ||
"For more detail, checkout the readthedocs page on [database structure](https://snowexsql.readthedocs.io/en/latest/database_structure.html) to see how data gets categorized. " | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "immune-symphony", | ||
"id": "81088ff6", | ||
"metadata": {}, | ||
"source": [ | ||
"## Bonus Step: Learning to help yourself\n", | ||
|
@@ -162,7 +132,7 @@ | |
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "aging-volunteer", | ||
"id": "96478a34", | ||
"metadata": {}, | ||
"source": [ | ||
"## Recap \n", | ||
|
@@ -193,7 +163,7 @@ | |
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.12.4" | ||
"version": "3.8.10" | ||
} | ||
}, | ||
"nbformat": 4, | ||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.