From 731753975bdad876f1fb1989d504dfc1436fe010 Mon Sep 17 00:00:00 2001 From: Ashish Kulshrestha Date: Fri, 8 Mar 2024 11:39:09 -0800 Subject: [PATCH] notebook to convert observed transit demand csv to omx --- .../convert-observed-demand-csv-to-omx.ipynb | 305 ++++++++++++++++++ 1 file changed, 305 insertions(+) create mode 100644 notebooks/convert-observed-demand-csv-to-omx.ipynb diff --git a/notebooks/convert-observed-demand-csv-to-omx.ipynb b/notebooks/convert-observed-demand-csv-to-omx.ipynb new file mode 100644 index 00000000..0aa848ed --- /dev/null +++ b/notebooks/convert-observed-demand-csv-to-omx.ipynb @@ -0,0 +1,305 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [], + "source": [ + "import openmatrix as omx\n", + "import pandas as pd\n", + "import numpy as np" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [], + "source": [ + "NUM_ZONES = 4735" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [], + "source": [ + "observed_demand_csv_file = 'V:/projects/MTC/transit_path_builder_calib/observed-demand-year-2015-am-emme-taz-by-path-trimmed.csv'\n", + "output_observed_omx_file = 'V:/projects/MTC/transit_path_builder_calib/trn_observed_demand_am.omx'" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "matrix_array_knr_trn_wlk = np.zeros((NUM_ZONES, NUM_ZONES))\n", + "matrix_array_pnr_trn_wlk = np.zeros((NUM_ZONES, NUM_ZONES))\n", + "matrix_array_wlk_trn_knr = np.zeros((NUM_ZONES, NUM_ZONES))\n", + "matrix_array_wlk_trn_pnr = np.zeros((NUM_ZONES, NUM_ZONES))\n", + "matrix_array_wlk_trn_wlk = np.zeros((NUM_ZONES, NUM_ZONES))" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "with open(observed_demand_csv_file, 'r') as f:\n", + " for idx, line in enumerate(f):\n", + " if idx == 0:\n", + " continue # skip the header row\n", + " period, path_type, orig, dest, trips = line.strip().split(',')\n", + " orig = int(orig)\n", + " dest = int(dest)\n", + " trips = float(trips)\n", + "\n", + " if path_type == 'knr_trn_wlk': \n", + " matrix_array_knr_trn_wlk[orig-1][dest-1] = trips\n", + "\n", + " if path_type == 'pnr_trn_wlk': \n", + " matrix_array_pnr_trn_wlk[orig-1][dest-1] = trips\n", + "\n", + " if path_type == 'wlk_trn_knr': \n", + " matrix_array_wlk_trn_knr[orig-1][dest-1] = trips\n", + "\n", + " if path_type == 'wlk_trn_pnr': \n", + " matrix_array_wlk_trn_pnr[orig-1][dest-1] = trips\n", + "\n", + " if path_type == 'wlk_trn_wlk': \n", + " matrix_array_wlk_trn_wlk[orig-1][dest-1] = trips \n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "output_matrix = omx.open_file(output_observed_omx_file, mode=\"w\")\n", + "output_matrix['KNR_TRN_WLK'] = matrix_array_knr_trn_wlk\n", + "output_matrix['PNR_TRN_WLK'] = matrix_array_pnr_trn_wlk\n", + "output_matrix['WLK_TRN_KNR'] = matrix_array_wlk_trn_knr\n", + "output_matrix['WLK_TRN_PNR'] = matrix_array_wlk_trn_pnr\n", + "output_matrix['WLK_TRN_WLK'] = matrix_array_wlk_trn_wlk" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "output_matrix.close()" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "mat = omx.open_file(output_observed_omx_file, 'r')" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [], + "source": [ + "mat1 = mat['KNR_TRN_WLK'].read()\n", + "mat2 = mat['PNR_TRN_WLK'].read()\n", + "mat3 = mat['WLK_TRN_KNR'].read()\n", + "mat4 = mat['WLK_TRN_PNR'].read()\n", + "mat5 = mat['WLK_TRN_WLK'].read()" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "358127.57831695606" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sum(sum(mat1)) + sum(sum(mat2)) + sum(sum(mat3)) + sum(sum(mat4)) + sum(sum(mat5))" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [], + "source": [ + "mat.close()" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [], + "source": [ + "demand_df = pd.read_csv(observed_demand_csv_file)" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
model_timepath_typeorig_emme_tazdest_emme_taztrips
0amknr_trn_wlk1295312.064273
1amknr_trn_wlk210555.721250
2amknr_trn_wlk325921.040014
3amknr_trn_wlk521784.855805
4amknr_trn_wlk739820.209921
\n", + "
" + ], + "text/plain": [ + " model_time path_type orig_emme_taz dest_emme_taz trips\n", + "0 am knr_trn_wlk 1 2953 12.064273\n", + "1 am knr_trn_wlk 2 1055 5.721250\n", + "2 am knr_trn_wlk 3 2592 1.040014\n", + "3 am knr_trn_wlk 5 2178 4.855805\n", + "4 am knr_trn_wlk 7 398 20.209921" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "demand_df.head()" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "358127.5783169544" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sum(demand_df.trips)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3.7.6 64-bit", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.6" + }, + "orig_nbformat": 4, + "vscode": { + "interpreter": { + "hash": "7101b7e646de3258f76ee66cfd1dea119f34dcc7025f6e5a1711703ceabc892c" + } + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}