Skip to content

Commit

Permalink
notebook to convert observed transit demand csv to omx
Browse files Browse the repository at this point in the history
  • Loading branch information
AshishKuls committed Mar 8, 2024
1 parent fc86cb8 commit 7317539
Showing 1 changed file with 305 additions and 0 deletions.
305 changes: 305 additions & 0 deletions notebooks/convert-observed-demand-csv-to-omx.ipynb
Original file line number Diff line number Diff line change
@@ -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": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>model_time</th>\n",
" <th>path_type</th>\n",
" <th>orig_emme_taz</th>\n",
" <th>dest_emme_taz</th>\n",
" <th>trips</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>am</td>\n",
" <td>knr_trn_wlk</td>\n",
" <td>1</td>\n",
" <td>2953</td>\n",
" <td>12.064273</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>am</td>\n",
" <td>knr_trn_wlk</td>\n",
" <td>2</td>\n",
" <td>1055</td>\n",
" <td>5.721250</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>am</td>\n",
" <td>knr_trn_wlk</td>\n",
" <td>3</td>\n",
" <td>2592</td>\n",
" <td>1.040014</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>am</td>\n",
" <td>knr_trn_wlk</td>\n",
" <td>5</td>\n",
" <td>2178</td>\n",
" <td>4.855805</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>am</td>\n",
" <td>knr_trn_wlk</td>\n",
" <td>7</td>\n",
" <td>398</td>\n",
" <td>20.209921</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"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
}

0 comments on commit 7317539

Please sign in to comment.