Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
276 changes: 173 additions & 103 deletions docs/users-guide/modelsetup.rst
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,9 @@ There are two recommended ways to install ActivitySim:

2. Using the :ref:`UV Package and Project Manager`

The first is recommended for users who do not need to change the Python code and are on Windows,
and the second is recommended for users who need to change/customize the Python code.
The first is recommended for users who are new to Python and use Windows, do not actively create and manage Python virtual environments,
and do not need to change the ActivitySim code. The second is recommended for users who actively create and manage Python virtual environments,
and/or want to change/customize the ActivitySim code.


Pre-packaged Installer
Expand Down Expand Up @@ -125,173 +126,242 @@ UV Package and Project Manager
______________________________________

This method is recommended for ActivitySim users who are familiar with
Python and optionally wish to customize the Python code to run their models.
Python, create and manage ActivitySim Python virtual environments, and optionally wish to customize ActivitySim code to run their models.
UV is a free open source cross-platform package and project manager that runs
on Windows, OS X, and Linux. It is 10-100x faster than conda, and pip itself, which is
the standard Python package manager. The *uv* features include automatic
environment management including installation and management of Python
versions and dependency locking.

.. note::
There are two options for using *uv* to install ActivitySim.

The first is to use *uv* to install an official ActivitySim release from the Python Package Index (PyPI).
The second is to use *uv* to install ActivitySim from a local directory, which should be the cloned ActivitySim repository.
Install UV
^^^^^^^^^^^^^^

.. note::
The first *uv* option is recommended for users who want to install ActivitySim from an official release and do not wish to change the Python code.
However, they may end up using different deep dependencies than those tested by the developers.
The second *uv* option is recommended for users who may want to customize the Python code, and who want to run ActivitySim
exactly as it was tested by the developers using the dependency lockfile which results in the exact same deep dependencies.
We recommend installing UV as an independent tool on your machine, separate from any existing package managers you may have such as conda or pip.

The steps involved are described as follows.

Option 1: Install ActivitySim from PyPI
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
For Windows users, run the following command in PowerShell to install *uv*. It does not require administrator privileges and installs *uv* for the current user only.
by default, uv is installed to `~/.local/bin` directory. Usually, this is `C:/Users/<username>/.local/bin`.
::
# Run the installer. Please review the printed message after installation.
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"

(Note: This step may fail at the moment because the ActivitySim version available on PyPI has dependency conflicts.
This step should work when ActivitySim release *<placeholder for version number>* which is built with *uv* is available on PyPI.
In the meantime, use Option 2 below to install ActivitySim from the lockfile.)
# Add uv to PATH
$env:PATH = "$env:USERPROFILE\.local\bin;$env:Path"

1. Install *uv*. Instructions can be found
`here <https://docs.astral.sh/uv/getting-started/installation/>`_.
If an agency wants to install *uv* globally for all users on Windows, run PowerShell as Administrator and run the following command.
::
# Run the installer with a custom install directory
powershell -ExecutionPolicy ByPass -c {$env:UV_INSTALL_DIR = "C:\custom\install\directory";irm https://astral.sh/uv/install.ps1 | iex}

2. Create a new project and virtual environment to work from and add ActivitySim. (Warning: This approach is quickest
for getting started but does not rely on the lockfile to install dependencies so you may
end up with different versions. If you want to use the lockfile, see Option 2 below.)
# Add uv to PATH for all users (requires administrator privileges)
[Environment]::SetEnvironmentVariable("PATH", $env:PATH + ";C:\custom\install\directory", [EnvironmentVariableTarget]::Machine)

Open a terminal, such as Command Prompt (note: not Anaconda Prompt), and run the following commands.
For more instructions on installing *uv* on Windows, MacOS, or Linux, please visit https://docs.astral.sh/uv/getting-started/installation/.

To verify that *uv* is installed correctly, open a new Command Prompt (not Anaconda Prompt) and run the following command.
::

mkdir asim_project
cd asim_project
echo 3.10 > .python-version # This sets the Python version to 3.10, which is currently used for ActivitySim development.
uv init
uv add activitysim
uv --version

*uv* will create a new virtual environment within the `asim_project` project folder
and install ActivitySim and its dependencies. The virtual environment is a hidden folder
within the `asim_project` directory called `.venv` and operates the same way as Python's classic *venv*.
.. note::
If you already have *uv* installed from an older project and you encounter errors
such as
::

3. Run an ActivitySim command using the following.
error: Failed to parse uv.lock... missing field version...

later in the process, you may need to update *uv* to the latest version by reinstalling it via the official
installation script: https://docs.astral.sh/uv/getting-started/installation/#standalone-installer.
You can check the version of *uv* you have installed by running
::

::
uv --version

uv run ...

For example, run the ActivitySim commandline using the following.
More information about the commandline interface is available in
the :ref:`Ways to Run the Model` section.

::
Install ActivitySim with UV
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

uv run activitysim run -c configs -o output -d data
There are two options to install ActivitySim using *uv*.

The first is to use *uv* to install an official ActivitySim release from the Python Package Index (PyPI).
The second is to use *uv* to install ActivitySim from the source code repository and use the dependency lockfile.

Run ActivitySim from a Different Directory
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
If you want to run ActivitySim from a directory different than where the code lives,
use the `project` option to point *uv* to this project using relative paths:
.. note::
The first option (:ref:`From PyPI`) is the quickest way to install ActivitySim from an official release and is recommended for users who do not wish to change the Python code.
However, they may end up using different deep dependencies than those tested by the developers.
The second option (:ref:`From Source with Lockfile`) is recommended for users who may want to customize the Python code, and/or who want to run ActivitySim
exactly as it was tested by the developers using the dependency lockfile which results in the exact same deep dependencies.

::
The steps involved are described as follows.

uv run --project relative/path/to/asim_project activitysim run -c configs -o output -d data
Option 1: From PyPI
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

You will use *uv* to create a project and virtual environment to work from and add ActivitySim.

You could also activate the virtual environment created by *uv* and run ActivitySim from any directory.
Open Command Prompt (not Anaconda Prompt), and run the following commands.

::

.venv\Scripts\activate
# create a new project directory and cd into it
mkdir asim_project
cd asim_project

With this command, you have activated the virtual environment created by *uv* and can run ActivitySim commands from any directory.
# initialize a virtual environment
# This sets the Python version to 3.10, which is currently fully tested for ActivitySim development
uv init --python 3.10

For more on *uv*, visit https://docs.astral.sh/uv/.
# add ActivitySim package from the latest release on PyPI
uv add activitysim

Option 2: Install ActivitySim from the lockfile
*uv* will create a new virtual environment within the `asim_project` project folder
and install ActivitySim and its dependencies. The virtual environment is a hidden folder
within the `asim_project` directory called `.venv` and operates the same way as Python's classic *venv*. You will notice
two new files created in the `asim_project` directory: `pyproject.toml` and `uv.lock`. These files
are automatically created, updated, and used by *uv* to manage your `asim_project` project and its dependencies.
You can share these files with others to recreate the same environment for your `asim_project` project. For more guidance on sharing your working environment,
see the Common Q&A :ref:`How to share my working environment with others?` section below.

By running the command `uv add activitysim`, you install the official release of ActivitySim from PyPI and its direct dependencies
listed in ActivitySim's `pyproject.toml` file. This approach is the quickest
for getting started but it does not rely on ActivitySim's own lockfile to install deep dependencies so you may
end up with different versions of deep dependencies than those tested by ActivitySim developers.
If you want to ensure exact versions of ActivitySim's deep dependencies, you should install ActivitySim using Option 2: From Source with Lockfile.

Option 2: From Source with Lockfile
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

To install dependencies from the lockfile and run ActivitySim exactly how
its developers tested it, after installing *uv* clone the code repository
and then run code.

1. Install *uv*. Instructions can be found
`here <https://docs.astral.sh/uv/getting-started/installation/>`_. (Skip
if already installed above. It only needs to be installed once per machine.)

.. note::
If you already have *uv* installed from an older project and you encounter errors
such as
::

error: Failed to parse uv.lock... missing field version...

later in the process, you may need to update *uv* to the latest version by reinstalling it via the official
installation script: https://docs.astral.sh/uv/getting-started/installation/#standalone-installer.
You can check the version of *uv* you have installed by running
::

uv --version

1. Clone the ActivitySim project using Git. (If Git is not installed,
its developers tested it, after installing *uv*, open Command Prompt, clone the ActivitySim project using Git. (If Git is not installed,
instructions can be found `here <https://git-scm.com/downloads>`_.)

::

git clone https://github.com/ActivitySim/activitysim.git
cd activitysim

3. Optionally create the virtual environment. This is created automatically
when running code in the next step, but manually syncing is an option too.
This step creates a hidden folder within the current directory called
`.venv` and operates the same way as Python's classic *venv*. (If you
want to install the project in a non-editable mode so that users on
your machine cannot accidentally change the source code, use the
`--no-editable` option.)
Run the `uv sync` command to create a virtual environment using the lockfile. It will initialize a virtual environment within the `activitysim` directory
and install ActivitySim and all its dependencies exactly as specified in the `uv.lock` file.
The virtual environment is a hidden folder within the current directory called
`.venv` and operates the same way as Python's classic *venv*.

::

uv sync --no-editable

4. Run an ActivitySim command using the following. (This will automatically
create a virtual environment from the lockfile, if it does not already
exist.)

::

uv run ...

uv sync
# or uv sync --no-editable

It is worth pointing out that by default, *uv* installs projects in
editable mode, such that changes to the source code are immediately reflected
in the environment. `uv sync` and `uv run` both accept a `--no-editable`
in the environment. `uv sync` accepts a `--no-editable`
flag, which instructs *uv* to install the project in non-editable mode,
removing any dependency on the source code.

Also, `uv run` automatically installs the dependencies listed in `pyproject.toml`
Also, `uv sync` automatically installs the dependencies listed in `pyproject.toml`
under `dependencies` under `[project]`, and it also installs those listed
under `dev` under `[dependency-groups]`. If you want to
skip the dependency groups entirely with a *uv* install (and only install those
that would install via `pip` from 'pypi`), use the `--no-default-groups` flag
with `uv sync`.

Run ActivitySim from a Different Directory
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
If you want to run ActivitySim from a directory different than where the code lives,
use the `project` option to point *uv* to this project using relative paths:

::
Which Option Should I Use?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

uv run --project relative/path/to/asim_project activitysim run -c configs -o output -d data
+--------------------------------------------------------------------------------+-----------+---------------------------+
| If I want to ... | From PyPI | From Source with Lockfile |
+================================================================================+===========+===========================+
| Install an official release of ActivitySim. | Yes | |
+--------------------------------------------------------------------------------+-----------+---------------------------+
| Install a development version of ActivitySim. | | Yes |
+--------------------------------------------------------------------------------+-----------+---------------------------+
| Install ActivitySim quickly to run models without changing the code. | Yes | |
+--------------------------------------------------------------------------------+-----------+---------------------------+
| Do ActivitySim code development. | | Yes |
+--------------------------------------------------------------------------------+-----------+---------------------------+
| Run ActivitySim with deep dependencies exactly as tested by the developers. | | Yes |
+--------------------------------------------------------------------------------+-----------+---------------------------+


You could also activate the virtual environment created by *uv* and run ActivitySim from any directory.
Run ActivitySim with UV
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Activate the virtual environment created by *uv*. This option is similar to using Python's classic venv or Conda env.
::
# cd into the project directory if not already there
## if you used the From PyPI option
cd asim_project
## if you used the From Source with Lockfile option
cd activitysim

# Activate the virtual environment
.venv\Scripts\activate

With this command, you have activated the virtual environment created by *uv* and can run ActivitySim commands from any directory.
Once the virtual environment is activated, you can run ActivitySim commands directly using the `activitysim` command.
For example, run the ActivitySim commandline using the following. More information about the commandline interface is available in
the :ref:`Ways to Run the Model` section.
::

activitysim run -c configs -o output -d data

For more on *uv*, visit https://docs.astral.sh/uv/.
Alternatively, you can run ActivitySim commands directly using *uv* without activating the virtual environment.
::

uv run activitysim run -c configs -o output -d data

Common Q&A
^^^^^^^^^^^^^^^^
My travel demand model requires additional Python packages not included with ActivitySim. How do I add them?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
You can add additional packages to your *uv* project by using the `uv add` command. For example, to add the `geopandas` package,
run the following command within your existing *uv* project directory.
::
# cd into your project directory
cd asim_project

# Add geopandas package
uv add geopandas
This will add the package to your virtual environment and update the `pyproject.toml` and the `uv.lock` file to include the new package and its dependencies.

If you envision having a version of Python packages that is different from the one used by ActivitySim, e.g., you need pandas 1.x for visualization (for some reason),
we recommend creating a separate *uv* project for your custom packages and managing them independently from ActivitySim.
::
# Open Command Prompt
mkdir viz_project
cd viz_project
uv init
uv add pandas==1.5.3

Many agencies use commerical software that have Python APIs and dependencies that may conflict with ActivitySim dependencies.
In such cases, we also recommend creating a separate *uv* project for the commerical software and managing them independently from ActivitySim.
::
# Open Command Prompt
mkdir emme_project
cd emme_project
uv init --python 2.7
# Then copy the emme.pth file (provides EMME API handshakes) from the Emme installation directory to emme_project/.venv/Lib/site-packages/

How to share my working environment with others?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
You can share your working environment with others by sharing the `uv.lock` file located in your project directory.
This file contains the exact versions of all packages and dependencies used in your project.
Others can recreate the same environment by running the `uv sync` command in a new project directory containing the shared `uv.lock` file.
::
# Copy uv.lock file to new project directory
uv sync

If I use the From PyPI option to install ActivitySim, would I run into dependency issues?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Using the :ref:`From PyPI` option to install ActivitySim may result in different versions of deep dependencies than those tested by ActivitySim developers.
This is because the :ref:`From PyPI` option installs only the direct dependencies listed in ActivitySim's `pyproject.toml` file,
and relies on *uv* to resolve and install the deep dependencies. It is likely that a newer version of ActivitySim deep dependencies
may cause compatibility issues. For example, see this recent update with `numexpr`: https://github.com/pydata/numexpr/issues/540

When that happens, we recommend using the :ref:`From Source with Lockfile` option to install ActivitySim, which ensures that
you are using the exact same deep dependencies as those tested by ActivitySim developers. In the meantime, you can also
report the compatibility issues to the ActivitySim development team via GitHub Issues, so that they can address them in future releases.

If I want to use `uv run` to run ActivitySim commands, do I still need to activate the virtual environment?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
No, if you use `uv run` to run ActivitySim commands, you do not need to activate the virtual environment first.
However, you will need to call `uv run` in the project directory where the virtual environment is located. Also, like `uv sync`,
`uv run` automatically updates the lockfile and installs any missing dependencies before running the command.