Skip to main content

Library for connection of TransCAD data files to Python

Project description

model-connector: Library for accessing travel model data

Installation:

Install using pip

pip install model-connector

Use Cases

  • TransCAD fixed format binary tables: fast read and write using pandas dataframes.
  • TransCAD matrix files: read a TransCAD matrix into a numpy array (beta)

TransCAD Binary Files

Reading from a TransCAD binary file

import model_connector as mc

# Read a TransCAD fixed format binary file into a dataframe, 
# retaining null values
df = mc.read_ffb("myfile.bin")

# Read a TransCAD fixed format binary file into a dataframe, 
# converting null values to zeros
dfz = mc.read_ffb("myfile.bin", null_to_zero=True)

Writing to a TransCAD binary file

import model_connector as mc
import pandas as pd

df = pd.DataFrame({"ID":[1,2,3], 
                   "RealField":[1.1, 2.2, 3.3, ],
                   "IntField":[1, 2, 3],
                   "DateField":[20210101, 20210115, 20210130],
                   "DateTimeField":[pd.Timestamp("2021-01-01 00:00:05"),
                                    pd.Timestamp("2021-01-15 00:10:05"),
                                    pd.Timestamp("2021-01-30 00:20:05")]})

mc.write_ffb(df, "sample_output.bin")

TransCAD Matrix Files

Matrix access requires TransCAD 9 or greater with a valid license.

Reading a TransCAD Matrix File

import model_connector as mc
import numpy as np

# Reading a TransCAD matrix into a dictionary of numpy arrays, 
# using the default behavior of converting null to zero.
matrix = mc.read_matrix_tc("myfile.mtx")
for table in matrix.keys():
    print(f"Sum of {table} = {matrix[table].sum()}" )

# Reading a TransCAD matrix into a pandas dataframe
df = mc.read_matrix_tc("myfile.mtx", format="df")
print(df.head())

# Optionally Convert null to NAN. This will fail for integer matrices since
# numpy doesn't support NAN in integer arrays.
matrix_withnan = mc.read_matrix_tc("myfile.mtx", missing="nan")
for table in matrix_withnan.keys():
    print(f"Sum of {table} = {matrix_withnan[table].sum()}" )

# By default, all matrix tables are loaded. Use the tables parameter to load only
# some matrix tables.
matrix_autos = mc.read_matrix_tc("myfile.mtx", tables=["DA", "SR2", "SR3p"])
for table in matrix_autos.keys():
    print(f"Sum of {table} = {matrix_autos[table].sum()}" )

# By default, data is read using the base index. Use the row_index and 
# col_index parameters to read data using a different index.
# Indices must be specified as an integer. the base index is 0, and
# the remaining indices are ordered as returned by GetMatrixIndexNames()
# in GISDK.

matrix_subset = mc.read_matrix_tc("myfile.mtx", row_index=1, col_index=1)
for table in matrix_subset.keys():
    print(f"Sum of {table} = {matrix_subset[table].sum()}" )

print(f"Shape of matrix is {matrix_subset["DA"].shape}")

Writing a TransCAD Matrix File

This functionality has not yet been implemented.

Specifying the TransCAD Installation

The TransCAD matrix functions search the system registry to locate a TransCAD installation. If the matrix functions fail even with a valid installation, this may be due to multiple versions or licensing methods being present on the computer.

The TransCAD program location can be specified using set_tcpath(). The current path can be obtained using get_tcpath(). If the path is not set manually, get_tcpath() will report the detected version of TransCAD.

# Set the TransCAD location as shown below
import model_connector as mc
mc.set_tcpath("C:/Program Files/TransCAD 10.0/tcw.exe")
print("TransCAD Path: ", mc.get_tcpath())

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

model_connector-0.0.4b2.tar.gz (22.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

model_connector-0.0.4b2-py3-none-any.whl (21.5 kB view details)

Uploaded Python 3

File details

Details for the file model_connector-0.0.4b2.tar.gz.

File metadata

  • Download URL: model_connector-0.0.4b2.tar.gz
  • Upload date:
  • Size: 22.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for model_connector-0.0.4b2.tar.gz
Algorithm Hash digest
SHA256 dbf05b81a01f81ce2fa802daa682ac62b84ae2a30370389e8185a952406f3533
MD5 be55e7478ab35db0459ce62cc8ff397f
BLAKE2b-256 2ad216990905cd8537875f61408ca338bb02b66c683594d447f40a27ebf9806f

See more details on using hashes here.

File details

Details for the file model_connector-0.0.4b2-py3-none-any.whl.

File metadata

File hashes

Hashes for model_connector-0.0.4b2-py3-none-any.whl
Algorithm Hash digest
SHA256 637f87f41bfdcd771c3fbf786324a3af699841c895b5dbd2c10e20b8ece6e5ca
MD5 34157e44e45afd06fcae5720bf567f1f
BLAKE2b-256 b7eb39eae1315fe7e3223fbc356bd18f819cdfc9c505c0b3631348e027fbd411

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page