Restoring from version control

Restoring from version control#

In this example we show how to restore a model from two different sources:

  • From model data in storage (csv) format, which is the standard form to maintain Polaris models

  • Directly from version control (not necessarily available for all users and models)

Restoring from csv format#

sphinx_gallery_thumbnail_path = ‘../../examples/working_with_models/git.png’

from polaris.project.project_restorer import restore_project_from_csv


# We identify the folder in our PC where the model data is located
model_folder = "/tmp/grid_example_restore"
source_files = "/tmp/grid_raw_files"

# And restore it (we set the overwrite to true in case we already have the supply and demand databases in the root)
restore_project_from_csv(model_folder, source_files, "Grid", overwrite=True)
/builds/polaris/code/polarislib/polaris/demand/database/migrations/20240325-add_column_to_fleet_veh_dist.py:27: ParserWarning: Falling back to the 'python' engine because the 'c' engine does not support regex separators (separators > 1 char and different from '\s+' are interpreted as regex); you can avoid this warning by specifying engine='python'.
  df = pd.read_csv(fleet_veh_dist_file, sep=",|\t")

Restoring from version control#

This is available as a helper method build_from_git on the Polaris class which will

  1. Clone the correct Gitlab project to a local directory (or pull if it already exists)

  2. Use the project_restorer to create a working project from the csv files stored in git.

  3. Return a project object to manipulate the newly created model.

This has two optional parameters:
  1. inplace (True/False) - if false it will create two directories (git and build). Setting it to true can be useful if you are updating the model on Gitlab, but separate directories are good when running your own simulations.

  2. overwrite (True/False) - whether or not to regenerate the sqlite files if they exist

from polaris.project.polaris import Polaris

model_build_folder = "/tmp/grid_from_git"

city = "grid"
project = Polaris.build_from_git(model_build_folder, city, inplace=True, overwrite=True)
Cloning into '/tmp/grid_from_git/grid/git'...
/builds/polaris/code/polarislib/polaris/demand/database/migrations/20240325-add_column_to_fleet_veh_dist.py:27: ParserWarning: Falling back to the 'python' engine because the 'c' engine does not support regex separators (separators > 1 char and different from '\s+' are interpreted as regex); you can avoid this warning by specifying engine='python'.
  df = pd.read_csv(fleet_veh_dist_file, sep=",|\t")

Total running time of the script: (0 minutes 41.696 seconds)

Gallery generated by Sphinx-Gallery