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", + " | model_time | \n", + "path_type | \n", + "orig_emme_taz | \n", + "dest_emme_taz | \n", + "trips | \n", + "
---|---|---|---|---|---|
0 | \n", + "am | \n", + "knr_trn_wlk | \n", + "1 | \n", + "2953 | \n", + "12.064273 | \n", + "
1 | \n", + "am | \n", + "knr_trn_wlk | \n", + "2 | \n", + "1055 | \n", + "5.721250 | \n", + "
2 | \n", + "am | \n", + "knr_trn_wlk | \n", + "3 | \n", + "2592 | \n", + "1.040014 | \n", + "
3 | \n", + "am | \n", + "knr_trn_wlk | \n", + "5 | \n", + "2178 | \n", + "4.855805 | \n", + "
4 | \n", + "am | \n", + "knr_trn_wlk | \n", + "7 | \n", + "398 | \n", + "20.209921 | \n", + "