Migrating to the table API#
The project-table API replaces the mutable, cached Link, Node,
Zone, Mode, LinkType, Period, and ResultRecord
objects. This is a breaking change: records returned by the new API are
immutable records, and the removed classes and methods have no compatibility
aliases.
The same table workflow is used by links, nodes, modes,
link_types, periods, turn_restrictions, zones, and
results. Matrix files use the separate workflow described under Matrices
and results. The zones table has moved from project.zoning to
project.tables.zones. The table class is now Zones in
aequilibrae.project.network.zones.
In general, create rows with insert(), change them with update(), and
remove them with delete(). Similarly, issue bulk operations through
insert_from(), update_from(), and delete_from().
Read and inspect records#
Use get() to retrieve one record, iterate over a table to retrieve all
records, and use normal container operations to test existence or obtain the
row count:
links = project.tables.links
if 42 in links:
link = links.get(42)
print(link.modes)
print(f"The network has {len(links)} links")
for link in links:
print(link.link_id, link.name)
Records are frozen dataclasses. Updating a table does not alter a record that
was read earlier. Call get() again when the latest values are needed:
link = links.get(42)
previous_speed = link.speed_ab
links.update(42, speed_ab=80)
assert link.speed_ab == previous_speed
assert links.get(42).speed_ab == 80
Generated records include fields added through the table’s fields editor.
The table refreshes its generated record type after a schema change. Use
table.columns to inspect writable columns and table.data when a
DataFrame or GeoDataFrame is more appropriate.
df = links.data
print(df.head().to_string())
Replace mutable-record saves#
Previously, a record was changed in memory and then persisted with save():
# Previous API
link = project.tables.links.get(42)
link.speed_ab = 80
link.name = "Main Street"
link.save()
Pass the key and changed values to update() instead:
# Table API
links.update(42, speed_ab=80, name="Main Street")
update() changes only the supplied columns and raises ValueError if the
key does not exist. To remove a row, replace record.delete() with
table.delete(key):
project.tables.nodes.delete(17)
project.tables.zones.delete(5)
delete() also raises ValueError when no row has the requested key.
Create rows#
Replace the staged new() and save() workflow with one insert()
call. It returns the explicit or generated key:
# Previous API
mode = project.tables.modes.new("k")
mode.mode_name = "freight"
project.tables.modes.add(mode)
mode.description = "Freight vehicles"
mode.save()
# Table API
mode_id = project.tables.modes.insert(
mode_id="k",
mode_name="freight",
description="Freight vehicles",
)
assert mode_id == "k"
For spatial tables, pass Shapely geometry directly. Geometry is returned as a Shapely object when the row is read:
from shapely.geometry import LineString, Point, Polygon
nodes = project.tables.nodes
nodes.insert(node_id=1001, is_centroid=0, geometry=Point(-43.20, -22.90))
nodes.insert(node_id=1002, is_centroid=0, geometry=Point(-43.19, -22.90))
link_geometry = LineString([(-43.20, -22.90), (-43.19, -22.90)])
link_id = project.tables.links.insert(
a_node=1001,
b_node=1002,
modes="c",
geometry=link_geometry,
)
zone_polygon = Polygon(
[
(-43.21, -22.91),
(-43.18, -22.91),
(-43.18, -22.89),
(-43.21, -22.89),
(-43.21, -22.91),
]
)
project.tables.zones.insert(zone_id=1001, name="Downtown", geometry=zone_polygon)
For scalar insertion, links supplies defaults for omitted endpoints,
direction, and link type. Other omitted columns use their SQLite defaults or
remain NULL when the schema allows it.
links.copy(link_id) replaces copy_link(). The old method returned an
unsaved mutable record. The new method inserts the copy immediately and returns
its new ID:
copied_link_id = project.tables.links.copy(link_id)
copied_link = project.tables.links.get(copied_link_id)
Insert, update, or delete many rows#
Use insert_from(), update_from(), and delete_from() for bulk
operations. All operations are atomic. insert_from() accepts a
DataFrame with an explicit key column and returns the inserted keys. For
numeric-key tables, omitting the key column allocates sequential IDs. For
non-numeric-keys, the keys must be provided:
import pandas as pd
new_links = pd.DataFrame(
{
"a_node": [1001, 1002],
"b_node": [1002, 1003],
"direction": [0, 0],
"modes": ["c", "cb"],
"link_type": ["default", "default"],
"geometry": [first_geometry.wkb, second_geometry.wkb],
}
)
new_ids = project.tables.links.insert_from(new_links)
Bulk insertion uses the columns in the DataFrame and any defaults. It does
not apply the scalar insert() conversions. Values must therefore
already be serialised for SQLite, except geometry which is handled
internally.
update_from() requires the table key as a DataFrame column. Other
columns are the values to write. If any key does not exist, the operation
raises ValueError before changing the table unless allow_missing is
set to True. The error reports at most the first 10 missing keys. The
return value is the number of submitted rows:
changes = pd.DataFrame(
{
"link_id": [new_ids[0], new_ids[1]],
"speed_ab": [50.0, 40.0],
"capacity_ab": [1800.0, 1200.0],
}
)
submitted = project.tables.links.update_from(changes)
assert submitted == 2
Neither method mutates the caller’s DataFrame. Use scalar insert() or
update() when each row needs separate error handling.
delete_from() requires a list of keys to remove. Similarly to
update_from() the keys must exist, and, the return value is the number
of submitted rows, but it accepts a list of keys instead:
removals = [new_ids[0], new_ids[1]]
submitted = project.tables.links.delete_from(removals)
assert submitted == 2
None of these methods mutate the caller’s DataFrame or List.
Work with modes, link types, periods, and connectors#
Collection methods that returned dictionaries are gone. Replace
all_modes(), all_types(), and all_zones() with iteration:
modes_by_id = {
mode.mode_id: mode
for mode in project.tables.modes
}
link_types_by_id = {
link_type.link_type_id: link_type
for link_type in project.tables.link_types
}
zones_by_id = {
zone.zone_id: zone
for zone in project.tables.zones
}
Use data instead of iteration when you know you need all the
items. get_by_name() remains available for modes and link types.
Use find_missing() with a list of keys to obtain a subset which are unused.
Use available_ids() on the modes and link types tables to find unused
IDs with reasonable defaults. It assumes string.ascii_letters by
default, pass full_list to provide a custom set of candidate IDs:
available_mode_ids = project.tables.modes.available_ids()
available_link_type_ids = project.tables.link_types.available_ids(
full_list=list(string.ascii_uppercase)
)
Operations formerly performed by a row now belong to its table:
Previous workflow |
Table API |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Connecting one zone requires its geometry to limit the node search, just as the previous row method did:
zone_id = 1001
zone = project.tables.zones.get(zone_id)
project.tables.nodes.connect_mode(
zone_id,
"c",
connectors=2,
area=zone.geometry,
)
To connect every zone that has a centroid, use the table-wide operation:
project.tables.zones.connect_mode("c", connectors=2, limit_to_zone=True)
nodes.new_centroid(node_id, geometry) remains as a convenience method, but
it now requires the geometry and inserts the centroid immediately:
# Previous API
centroid = project.tables.nodes.new_centroid(1001)
centroid.geometry = Point(-43.195, -22.900)
centroid.save()
# Table API
project.tables.nodes.new_centroid(1001, Point(-43.195, -22.900))
periods.new_period(...) likewise inserts immediately and returns the period
ID:
project.tables.periods.new_period(
2,
start=7 * 60 * 60,
end=9 * 60 * 60,
description="Morning peak",
)
Turn restrictions#
Turn restrictions now use the same spatial-table API:
Previous method |
Table API |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
clear_restrictions() still deletes all turns and returns the count.
update() and delete() return nothing and raise ValueError for missing
keys. Connection arguments are gone; group writes with project.transaction().
Both scalar and bulk inserts now require explicit modes. Database triggers
reject missing, empty, unknown or repeated mode IDs and overlapping restrictions
with sqlite3.IntegrityError. Modes are stored as supplied, without sorting
or removing duplicates. Updates leave omitted fields unchanged; setting
modes=None is rejected rather than selecting all modes.
Scalar and bulk writes still normalize penalties. None, NaN and positive
infinity penalties prohibit the turn.
Transactions and custom table integrations#
A standalone scalar operation opens and finalises a transaction automatically if one is not already open, otherwise the scalar mutation joins that transaction. Bulk operations always open their own nested transaction scope.
Nested transaction scopes use SQLite savepoints. If an exception from an inner scope is caught by an outer scope, only the inner scope is rolled back.
Matrices and results#
Result records support get() and metadata update() operations. Matrix
metadata is stored in OMX files.
For matrices, replace new_record() with save_matrix() to save an in-memory
matrix and its metadata.
project.matrices.save_matrix(
"demand_2030.omx",
matrix=demand,
)
print(project.matrices.cores_on_disk("demand_2030.omx"))
Use the filename, rather than a record name, to open them:
with project.matrices.get_matrix("base_skims.omx") as base_skims:
print(list(base_skims))
Matrix metadata changes are file operations:
with project.matrices.get_matrix("demand_2030.omx", mode="r+") as matrix:
matrix.metadata["description"] = "2030 demand"
(project.matrices.folder / "demand_2030.omx").unlink()
For results, replace new_record() followed by set_data() with one
create() call. Named DataFrame index levels are stored as regular columns,
which makes a link-indexed result straightforward to retrieve:
assignment_data = pd.DataFrame(
{
"volume_ab": [1250.0, 830.0],
"volume_ba": [1100.0, 790.0],
},
index=pd.Index([42, 43], name="link_id"),
)
result = project.results.create(
"assignment_2030",
assignment_data,
procedure="traffic assignment",
procedure_id="2030-base",
procedure_report={"converged": True},
year="2030",
)
stored_data = project.results.get_results(result.table_name)
assert set(stored_data["link_id"]) == {42, 43}
project.results.delete_result(result.table_name)
create() raises an error rather than replacing an existing metadata record
or data table. delete_result() removes both metadata and its data table.
The more generic results.delete(name) removes metadata only after the data
table has been removed.
results provides clear_database() to remove stale metadata,
update_database() to register unrecorded tables, and sync() to run both
reconciliation steps. Matrix files are discovered directly and need no
synchronisation.