openclimatefix / nowcasting_datamodel

Datamodel for the nowcasting project
6 stars 6 forks source link

nowcasting_datamodel

All Contributors

Datamodel for the nowcasting project

The data model has been made using sqlalchemy with a mirrored model in pydantic.

โš ๏ธ Database tables are currently made automatically, but in the future there should be a migration process

Future: The data model could be moved, to be a more modular solution.

nowcasting_datamodel

models.py

All models are in nowcasting_datamodel.models.py. The diagram below shows how the different tables are connected.

Models Models

connection.py

nowcasting_datamodel.connection.py contains a connection class which can be used to make a sqlalchemy session.

from nowcasting_datamodel.connection import DatabaseConnection

# make connection object
db_connection = DatabaseConnection(url='sqlite:///test.db')

# make sessions
with db_connection.get_session() as session:

    # do something with the database
    pass

๐Ÿ‘“ read.py

nowcasting_datamodel.read.py contains functions to read the database. The idea is that these are easy to use functions that query the database in an efficient and easy way.

from nowcasting_datamodel.connection import DatabaseConnection
from nowcasting_datamodel.read import get_latest_forecast

# make connection object
db_connection = DatabaseConnection(url='sqlite:///test.db')

# make sessions
with db_connection.get_session() as session:
    f = get_latest_forecast(session=session, gsp_id=1)

๐Ÿ’พ save.py

nowcasting_datamodel.save.py has one functions to save a list of Forecast to the database

๐Ÿ‡ฌ๐Ÿ‡ง national.py

nowcasting_datamodel.fake.py has a useful function for adding up forecasts for all GSPs into a national Forecast.

fake.py

nowcasting_datamodel.fake.py

Functions used to make fake model data.

๐Ÿฉบ Testing

Tests are run by using the following command

docker stop $(docker ps -a -q)
docker-compose -f test-docker-compose.yml build
docker-compose -f test-docker-compose.yml run tests

These sets up postgres in a docker container and runs the tests in another docker container. This slightly more complicated testing framework is needed (compared to running pytest) as some queries can not be fully tested on a sqlite database

Mac M1 users

An upstream builds issue of libgp may cause the following error:

sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) SCRAM authentication requires libpq version 10 or above

As suggested in this thread, a temporary fix is to set the env variable DOCKER_DEFAULT_PLATFORM=linux/amd64 prior to building the test images - although this reportedly comes with performance penalties.

๐Ÿ› ๏ธ infrastructure

.github/workflows contains a number of CI actions

  1. linters.yaml: Runs linting checks on the code
  2. release.yaml: Make and pushes docker files on a new code release
  3. test-docker.yaml': Runs tests on every push

The docker file is in the folder infrastructure/docker/

The version is bumped automatically for any push to main.

Environmental Variables

Contributors โœจ

Thanks goes to these wonderful people (emoji key):

Brandon Ly
Brandon Ly

๐Ÿ’ป
Chris Lucas
Chris Lucas

๐Ÿ’ป
James Fulton
James Fulton

๐Ÿ’ป
Rosheen Naeem
Rosheen Naeem

๐Ÿ’ป
Henri Dewilde
Henri Dewilde

๐Ÿ’ป

This project follows the all-contributors specification. Contributions of any kind welcome!