From e645d0fb16b1d2aaad5e9b8479454eb5c3d12851 Mon Sep 17 00:00:00 2001 From: Raunak laptop Date: Tue, 12 May 2020 11:35:36 +0530 Subject: [PATCH 1/3] write txt files from julia for idm features and true accelerations, notebook for lmfit --- .gitignore | 4 + scripts/helpers.jl | 57 +++++++++++-- scripts/pythonscripts/idm_lmfit.ipynb | 114 ++++++++++++++++++++++++++ 3 files changed, 166 insertions(+), 9 deletions(-) create mode 100644 scripts/pythonscripts/idm_lmfit.ipynb diff --git a/.gitignore b/.gitignore index 8277e8b..eb6d1d3 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,10 @@ test/media/ # media files generated by scripts scripts/media/ +# text and ipynb checkpoints in pythonscripts +scripts/pythonscripts/*.txt +scripts/pythonscripts/.ipynb_checkpoints + # Files generated by invoking Julia with --code-coverage *.jl.cov *.jl.*.cov diff --git a/scripts/helpers.jl b/scripts/helpers.jl index 4acfa96..cec39c2 100644 --- a/scripts/helpers.jl +++ b/scripts/helpers.jl @@ -7,6 +7,7 @@ Most functions have the script within the doc string as example using Distributions # provides `mean`: to compute mean of particle dist over cars using JLD # to save models using AutomotiveInteraction +using DelimitedFiles # to write to txt file that will be read in by python # We need a function to read in the jld file and extract metrics based on it """ @@ -456,26 +457,64 @@ function scenelist2idmfeatures(f,scene_list;id_list=[]) # Example ```julia -f = FilteringEnvironment() -filename = "media/upper_4.jld" -models,id_list,ts,te = JLD.load(filename,"m","veh_id_list","ts","te") -scene_list_true = replay_scenelist(f,id_list=id_list,ts=ts,te=te) -feat_dict = scenelist2idmfeatures(f,scene_list_true,id_list=id_list) +using AutomotiveInteraction +using AutomotiveSimulator +using JLD +cd("scripts") +include("helpers.jl") +f = FilteringEnvironment(); +filename = "media/upper_4.jld"; +models,id_list,ts,te = JLD.load(filename,"m","veh_id_list","ts","te"); +scene_list_true = replay_scenelist(f,id_list=id_list,ts=ts,te=te); +feat_dict = scenelist2idmfeatures(f,scene_list_true,id_list=id_list); ``` """ function scenelist2idmfeatures(f,scene_list;id_list=[]) numscenes=length(scene_list) idmfeat_dict = Dict() for vehid in id_list - idmfeats = fill(0.,numscenes,4) # Because 3 idm features and 1 true accl + data = fill(0.,numscenes,4) # Because 3 idm features and 1 true accl acc_trace = acc(f.roadway,scene_list,vehid) acc_trace[1] = -1. # Just to get rid of missing. This will be thrown away later for (i,scene) in enumerate(scene_list) scene = scene_list[i] - idmfeats[i,1],idmfeats[i,2],idmfeats[i,3] = extract_idm_features(f,scene,vehid) - idmfeats[i,4] = acc_trace[i] # Caution: 1st elem will be missing + data[i,1],data[i,2],data[i,3] = extract_idm_features(f,scene,vehid) + data[i,4] = acc_trace[i] # Caution: 1st elem will be missing end - idmfeat_dict[vehid] = idmfeats + + temp = removeNaNrows(data) + idm_feats = temp[2:end,1:3] + acc_trace = temp[2:end,4] + + writedlm("pythonscripts/$(vehid)_idmfeats.txt",idm_feats) + writedlm("pythonscripts/$(vehid)_trueacc.txt",acc_trace) + + idmfeat_dict[vehid] = temp end return idmfeat_dict end + +""" +function removeNaNrows(a) +- Remove rows with any NaNs in them from the input matrix `a` + +# Example +```julia +a = rand(6,3) +a[2,2] = NaN, a[5,3]=NaN +b = removeNaNrows(a) +``` +""" +function removeNaNrows(a) + nan_row_idx = [] + for i in 1:size(a,1) + curr_row = a[i,:] + + if sum(isnan.(curr_row))>0 + push!(nan_row_idx,i) + end + end + + b = a[setdiff(1:end,nan_row_idx),:] + return b +end diff --git a/scripts/pythonscripts/idm_lmfit.ipynb b/scripts/pythonscripts/idm_lmfit.ipynb new file mode 100644 index 0000000..6ba0514 --- /dev/null +++ b/scripts/pythonscripts/idm_lmfit.ipynb @@ -0,0 +1,114 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "- Helper to AutomotiveInteraction.jl\n", + "- Perform non-linear least squares fit using Levenberg-Marquardt\n", + "- Adapted from Jeremy Morton's code in LSTM-acc-predict\n", + "- The IDM features and true acceleration value is provided by the Julia code in the form of .txt file\n", + "- Should think about integrating python part i.e. this file directly using PyCall but leave for later" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "from lmfit import minimize, Parameters, Parameter, report_fit\n", + "import math\n", + "import numpy as np" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "# Function to minimize\n", + "def IDM(params, i, true_acc):\n", + " dmn = params['dmn'].value\n", + " T = params['T'].value\n", + " bcmf = params['bcmf'].value\n", + " smx = params['smx'].value\n", + " amx = params['amx'].value\n", + " \n", + " d = x[i, 0]\n", + " r = x[i, 1]\n", + " s = x[i, 2]\n", + " \n", + " d_des = dmn + T*s - s*r/2/math.sqrt(amx*bcmf)\n", + " pred = amx*(1 - (s/smx)**4 - (d_des/d)**2)\n", + " \n", + " return pred - true_acc" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "# Define parameters; outcome is not particularly sensitive to dmn or smx values so boundaries\n", + "# are (admittedly) a little arbitrary\n", + "params = Parameters()\n", + "params.add('dmn', value = 6., min = 0.1, max = 5.25) \n", + "params.add('T', value = 1., min = 0.1, max = 5.0)\n", + "params.add('bcmf', value = 1.0, min = 0.5, max = 5.0) \n", + "params.add('smx', value = 15.0, min = 1.0, max = 30.0)\n", + "params.add('amx', value = 1.0, min = 0.75, max = 3.0)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "result = minimize(IDM, params, args=(range(len(acc)), acc))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "result.params" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "sum(abs(result.residual))/count" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.3" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} From f438b903a177d4d98806d0b622df23d139fe94af Mon Sep 17 00:00:00 2001 From: Raunak laptop Date: Tue, 12 May 2020 16:58:15 +0530 Subject: [PATCH 2/3] update lmfit notebook to read in julia generated features and acceleration traces for idm --- scripts/pythonscripts/idm_lmfit.ipynb | 107 +++++++++++++++++++++++--- 1 file changed, 97 insertions(+), 10 deletions(-) diff --git a/scripts/pythonscripts/idm_lmfit.ipynb b/scripts/pythonscripts/idm_lmfit.ipynb index 6ba0514..2c1a19e 100644 --- a/scripts/pythonscripts/idm_lmfit.ipynb +++ b/scripts/pythonscripts/idm_lmfit.ipynb @@ -8,7 +8,11 @@ "- Perform non-linear least squares fit using Levenberg-Marquardt\n", "- Adapted from Jeremy Morton's code in LSTM-acc-predict\n", "- The IDM features and true acceleration value is provided by the Julia code in the form of .txt file\n", - "- Should think about integrating python part i.e. this file directly using PyCall but leave for later" + "- Should think about integrating python part i.e. this file directly using PyCall but leave for later\n", + "\n", + "- How this works\n", + " - Reads in idm feature values from \"66_idmfeats.txt\" and true acceleration from \"66_trueacc.txt\"\n", + " - Calls lmfit.minimize to run the Leveberg-Marquardt algorithm for fitting" ] }, { @@ -25,7 +29,11 @@ { "cell_type": "code", "execution_count": 2, - "metadata": {}, + "metadata": { + "code_folding": [ + 0 + ] + }, "outputs": [], "source": [ "# Function to minimize\n", @@ -49,7 +57,11 @@ { "cell_type": "code", "execution_count": 3, - "metadata": {}, + "metadata": { + "code_folding": [ + 0 + ] + }, "outputs": [], "source": [ "# Define parameters; outcome is not particularly sensitive to dmn or smx values so boundaries\n", @@ -64,29 +76,104 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, + "execution_count": 17, + "metadata": { + "code_folding": [ + 0 + ] + }, "outputs": [], "source": [ + "# Read in the x and the acceleration provided a vehicle id\n", + "vehid = 66\n", + "x = np.loadtxt(\"{}_idmfeats.txt\".format(vehid))\n", + "acc = np.loadtxt(\"{}_trueacc.txt\".format(vehid))" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": { + "code_folding": [ + 0 + ] + }, + "outputs": [], + "source": [ + "# Perform the LM fit\n", "result = minimize(IDM, params, args=(range(len(acc)), acc))" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 24, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/html": [ + "
name value standard error relative error initial value min max vary
dmn 3.35795599 0.25548101 (7.61%) 5.25 0.10000000 5.25000000 True
T 0.10000987 0.02930858 (29.31%) 1.0 0.10000000 5.00000000 True
bcmf 5.00000000 0.49746135 (9.95%) 1.0 0.50000000 5.00000000 True
smx 10.5436958 0.35315817 (3.35%) 15.0 1.00000000 30.0000000 True
amx 0.75000740 0.12666860 (16.89%) 1.0 0.75000000 3.00000000 True
" + ], + "text/plain": [ + "Parameters([('dmn',\n", + " ),\n", + " ('T',\n", + " ),\n", + " ('bcmf',\n", + " ),\n", + " ('smx',\n", + " ),\n", + " ('amx',\n", + " )])" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "result.params" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 25, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "10.54369581728179" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "result.params['smx'].value" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0.18881790619869915" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "sum(abs(result.residual))/count" + "sum(abs(result.residual))/130" ] } ], From 8caa30e9c1294c98862e56ea079fed7f1599a57e Mon Sep 17 00:00:00 2001 From: Raunak laptop Date: Wed, 13 May 2020 15:37:30 +0530 Subject: [PATCH 3/3] Extract multiscenario idm features, update lmfit notebook to do multiscenario --- .gitignore | 6 +- scripts/experiments.jl | 20 +- scripts/helpers.jl | 44 +++- scripts/pythonscripts/idm_lmfit.ipynb | 344 +++++++++++++++++++------- 4 files changed, 321 insertions(+), 93 deletions(-) diff --git a/.gitignore b/.gitignore index eb6d1d3..a4604b7 100644 --- a/.gitignore +++ b/.gitignore @@ -10,10 +10,12 @@ test/media/ # media files generated by scripts scripts/media/ -# text and ipynb checkpoints in pythonscripts -scripts/pythonscripts/*.txt +# ipynb checkpoints in pythonscripts scripts/pythonscripts/.ipynb_checkpoints +# idm acc trace files stored in lmfit +scripts/lmfit/ + # Files generated by invoking Julia with --code-coverage *.jl.cov *.jl.*.cov diff --git a/scripts/experiments.jl b/scripts/experiments.jl index 045cc85..2fdd078 100644 --- a/scripts/experiments.jl +++ b/scripts/experiments.jl @@ -86,6 +86,24 @@ rmse_plots_modelscompare(rmse_pos_list,filename = "media/rmse_pos_upper.svg"); coll_mat_list = [coll_mat_idm,coll_mat_cidm,coll_mat_lmidm,coll_mat_pf]; coll_barchart(coll_mat_list,filename = "media/coll_barchart_upper.svg"); +#*****************Generate idm params for lmfit************ +s = scenarios_upper +f = FilteringEnvironment() +name = "upper" + +for i in 1:length(s) + veh_id_list = s[i][1] + ts = s[i][2][1] + te = s[i][2][2] + filename = "media/upper_$i.jld"; + id_list,ts,te = JLD.load(filename,"veh_id_list","ts","te"); + scene_list_true = replay_scenelist(f,id_list=id_list,ts=ts,te=te); + + dirname = "$(name)_$(i)" + mkdir("lmfit/$(dirname)") + feat_dict = scenelist2idmfeatures(f,scene_list_true,id_list=id_list, + scenario_name=name,scenario_number=i); +end #********************Train upper test lower****************** # We need to show a variability in the generated scenarios @@ -113,4 +131,4 @@ coll_barchart(coll_mat_list,filename = "media/coll_barchart_upper.svg"); # push!(test_tdata,Dict("t"=>t)) # end -# curve_fit(test_lmfit,test_tdata,ydata,[0.5,0.5]) \ No newline at end of file +# curve_fit(test_lmfit,test_tdata,ydata,[0.5,0.5]) diff --git a/scripts/helpers.jl b/scripts/helpers.jl index cec39c2..ab8c368 100644 --- a/scripts/helpers.jl +++ b/scripts/helpers.jl @@ -93,6 +93,40 @@ modelmaker=make_cidm_models) return rmse_pos,rmse_vel,c_array end +""" +function make_lmfit_idm_models(f::FilteringEnvironment,scene) + +- Read lmfit params stored in txt file and create associated driver model + +# Examples +cd("scripts") +include("helpers.jl") +f = FilteringEnvironment() +ts = 310 # Corresponds to scenario 4 +scene = deepcopy(f.traj[ts]) +models = make_lmfit_idm_models(f,scene,scenario_name="upper",scenario_number=4) +""" +function make_lmfit_idm_models(f::FilteringEnvironment,scene;scenario_name="upper", + scenario_number=1) + print("Models assigned based on lmfit parameters\n") + models = Dict{Int64,DriverModel}() + for veh in scene + veh_id = veh.id + if isfile("pythonscripts/$(scenario_name)_$(scenario_number)/$(veh_id)_lmfit_params.txt") + lmparams = readdlm("pythonscripts/upper_4/$(veh_id)_lmfit_params.txt") + v_des = lmparams[1] + T = lmparams[2] + dmin = lmparams[3] + models[veh.id] = IntelligentDriverModel(v_des=v_des,T=T,s_min=dmin) + else + # Default to Jeremy's parameters + models[veh.id] = IntelligentDriverModel(v_des=17.837,s_min=5.249, + T=0.918,a_max=0.758,d_cmf=3.811) + end + end + return models +end + """ - Run multiple scenarios for the idm based models @@ -454,6 +488,7 @@ function scenelist2idmfeatures(f,scene_list;id_list=[]) - Returns dict with vehicle id as key and array of idm features as value - Array has each row a different timestep. Col 1 is v_ego, col2 is delta_v, 3 is headway - Col4 is the true acceleration +- Writes the features and accleration trace to .txt files # Example ```julia @@ -469,7 +504,8 @@ scene_list_true = replay_scenelist(f,id_list=id_list,ts=ts,te=te); feat_dict = scenelist2idmfeatures(f,scene_list_true,id_list=id_list); ``` """ -function scenelist2idmfeatures(f,scene_list;id_list=[]) +function scenelist2idmfeatures(f,scene_list;id_list=[], + scenario_name="upper",scenario_number=4) numscenes=length(scene_list) idmfeat_dict = Dict() for vehid in id_list @@ -486,8 +522,10 @@ function scenelist2idmfeatures(f,scene_list;id_list=[]) idm_feats = temp[2:end,1:3] acc_trace = temp[2:end,4] - writedlm("pythonscripts/$(vehid)_idmfeats.txt",idm_feats) - writedlm("pythonscripts/$(vehid)_trueacc.txt",acc_trace) + dirname = "$(scenario_name)_$(scenario_number)" + + writedlm("lmfit/$(dirname)/$(vehid)_idmfeats.txt",idm_feats) + writedlm("lmfit/$(dirname)/$(vehid)_trueacc.txt",acc_trace) idmfeat_dict[vehid] = temp end diff --git a/scripts/pythonscripts/idm_lmfit.ipynb b/scripts/pythonscripts/idm_lmfit.ipynb index 2c1a19e..edfd049 100644 --- a/scripts/pythonscripts/idm_lmfit.ipynb +++ b/scripts/pythonscripts/idm_lmfit.ipynb @@ -11,24 +11,32 @@ "- Should think about integrating python part i.e. this file directly using PyCall but leave for later\n", "\n", "- How this works\n", - " - Reads in idm feature values from \"66_idmfeats.txt\" and true acceleration from \"66_trueacc.txt\"\n", - " - Calls lmfit.minimize to run the Leveberg-Marquardt algorithm for fitting" + " - Reads in idm feature values from `../lmfit/scenario_name/vehid_idmfeats.txt`\n", + " and true acceleration from `../lmfit/scenario_name/vehid_acctrace.txt`\n", + " - Calls lmfit.minimize to run the Leveberg-Marquardt algorithm for fitting\n", + " - Writes resulting parameters in the order `v_des`,`T`,`dmin` in `../lmfit/scenario_name/vehid_lmfit_params.txt`" ] }, { "cell_type": "code", - "execution_count": 1, - "metadata": {}, + "execution_count": 11, + "metadata": { + "code_folding": [ + 0 + ] + }, "outputs": [], "source": [ + "# imports\n", "from lmfit import minimize, Parameters, Parameter, report_fit\n", "import math\n", - "import numpy as np" + "import numpy as np\n", + "import os" ] }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 7, "metadata": { "code_folding": [ 0 @@ -56,7 +64,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 8, "metadata": { "code_folding": [ 0 @@ -64,8 +72,7 @@ }, "outputs": [], "source": [ - "# Define parameters; outcome is not particularly sensitive to dmn or smx values so boundaries\n", - "# are (admittedly) a little arbitrary\n", + "# Initialize parameter values\n", "params = Parameters()\n", "params.add('dmn', value = 6., min = 0.1, max = 5.25) \n", "params.add('T', value = 1., min = 0.1, max = 5.0)\n", @@ -76,7 +83,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 9, "metadata": { "code_folding": [ 0 @@ -84,96 +91,259 @@ }, "outputs": [], "source": [ - "# Read in the x and the acceleration provided a vehicle id\n", - "vehid = 66\n", - "x = np.loadtxt(\"{}_idmfeats.txt\".format(vehid))\n", - "acc = np.loadtxt(\"{}_trueacc.txt\".format(vehid))" + "# Upper scenarios: Specify the list of scenarios\n", + "scenarios_upper = [\n", + " [6,8,13,19,28,29],\n", + " [34,37,40,42,43,49,50],\n", + " [59,60,62,66,67,71,73],\n", + " [66,67,71,73,76,82,88,92,95],\n", + " [187,191,193,194],\n", + "\n", + " [211,213,215,217,221],\n", + " [248,249,250,256],\n", + " [272,275,276,277,279],\n", + " [294,295,300,307,308,311],\n", + " [324,327,329,333,335,339,341,346,354,357]\n", + "]" ] }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 23, "metadata": { "code_folding": [ 0 - ] + ], + "collapsed": true }, - "outputs": [], - "source": [ - "# Perform the LM fit\n", - "result = minimize(IDM, params, args=(range(len(acc)), acc))" - ] - }, - { - "cell_type": "code", - "execution_count": 24, - "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
name value standard error relative error initial value min max vary
dmn 3.35795599 0.25548101 (7.61%) 5.25 0.10000000 5.25000000 True
T 0.10000987 0.02930858 (29.31%) 1.0 0.10000000 5.00000000 True
bcmf 5.00000000 0.49746135 (9.95%) 1.0 0.50000000 5.00000000 True
smx 10.5436958 0.35315817 (3.35%) 15.0 1.00000000 30.0000000 True
amx 0.75000740 0.12666860 (16.89%) 1.0 0.75000000 3.00000000 True
" - ], - "text/plain": [ - "Parameters([('dmn',\n", - " ),\n", - " ('T',\n", - " ),\n", - " ('bcmf',\n", - " ),\n", - " ('smx',\n", - " ),\n", - " ('amx',\n", - " )])" - ] - }, - "execution_count": 24, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "result.params" - ] - }, - { - "cell_type": "code", - "execution_count": 25, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "10.54369581728179" - ] - }, - "execution_count": 25, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "result.params['smx'].value" - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": {}, "outputs": [ { - "data": { - "text/plain": [ - "0.18881790619869915" - ] - }, - "execution_count": 20, - "metadata": {}, - "output_type": "execute_result" + "name": "stdout", + "output_type": "stream", + "text": [ + "name = upper_1\n", + "id_list = [6, 8, 13, 19, 28, 29]\n", + "vehicle id = 6\n", + "\n", + "Features file is not empty. Filename = ../lmfit/upper_1/6_idmfeats.txt\n", + "vehicle id = 8\n", + "\n", + "Features file is not empty. Filename = ../lmfit/upper_1/8_idmfeats.txt\n", + "vehicle id = 13\n", + "\n", + "vehicle id = 19\n", + "\n", + "Features file is not empty. Filename = ../lmfit/upper_1/19_idmfeats.txt\n", + "vehicle id = 28\n", + "\n", + "Features file is not empty. Filename = ../lmfit/upper_1/28_idmfeats.txt\n", + "vehicle id = 29\n", + "\n", + "Features file is not empty. Filename = ../lmfit/upper_1/29_idmfeats.txt\n", + "name = upper_2\n", + "id_list = [34, 37, 40, 42, 43, 49, 50]\n", + "vehicle id = 34\n", + "\n", + "Features file is not empty. Filename = ../lmfit/upper_2/34_idmfeats.txt\n", + "vehicle id = 37\n", + "\n", + "Features file is not empty. Filename = ../lmfit/upper_2/37_idmfeats.txt\n", + "vehicle id = 40\n", + "\n", + "vehicle id = 42\n", + "\n", + "Features file is not empty. Filename = ../lmfit/upper_2/42_idmfeats.txt\n", + "vehicle id = 43\n", + "\n", + "Features file is not empty. Filename = ../lmfit/upper_2/43_idmfeats.txt\n", + "vehicle id = 49\n", + "\n", + "Features file is not empty. Filename = ../lmfit/upper_2/49_idmfeats.txt\n", + "vehicle id = 50\n", + "\n", + "Features file is not empty. Filename = ../lmfit/upper_2/50_idmfeats.txt\n", + "name = upper_3\n", + "id_list = [59, 60, 62, 66, 67, 71, 73]\n", + "vehicle id = 59\n", + "\n", + "vehicle id = 60\n", + "\n", + "Features file is not empty. Filename = ../lmfit/upper_3/60_idmfeats.txt\n", + "vehicle id = 62\n", + "\n", + "Features file is not empty. Filename = ../lmfit/upper_3/62_idmfeats.txt\n", + "vehicle id = 66\n", + "\n", + "Features file is not empty. Filename = ../lmfit/upper_3/66_idmfeats.txt\n", + "vehicle id = 67\n", + "\n", + "Features file is not empty. Filename = ../lmfit/upper_3/67_idmfeats.txt\n", + "vehicle id = 71\n", + "\n", + "Features file is not empty. Filename = ../lmfit/upper_3/71_idmfeats.txt\n", + "vehicle id = 73\n", + "\n", + "Features file is not empty. Filename = ../lmfit/upper_3/73_idmfeats.txt\n", + "name = upper_4\n", + "id_list = [66, 67, 71, 73, 76, 82, 88, 92, 95]\n", + "vehicle id = 66\n", + "\n", + "Features file is not empty. Filename = ../lmfit/upper_4/66_idmfeats.txt\n", + "vehicle id = 67\n", + "\n", + "vehicle id = 71\n", + "\n", + "Features file is not empty. Filename = ../lmfit/upper_4/71_idmfeats.txt\n", + "vehicle id = 73\n", + "\n", + "Features file is not empty. Filename = ../lmfit/upper_4/73_idmfeats.txt\n", + "vehicle id = 76\n", + "\n", + "Features file is not empty. Filename = ../lmfit/upper_4/76_idmfeats.txt\n", + "vehicle id = 82\n", + "\n", + "Features file is not empty. Filename = ../lmfit/upper_4/82_idmfeats.txt\n", + "vehicle id = 88\n", + "\n", + "Features file is not empty. Filename = ../lmfit/upper_4/88_idmfeats.txt\n", + "vehicle id = 92\n", + "\n", + "Features file is not empty. Filename = ../lmfit/upper_4/92_idmfeats.txt\n", + "vehicle id = 95\n", + "\n", + "Features file is not empty. Filename = ../lmfit/upper_4/95_idmfeats.txt\n", + "name = upper_5\n", + "id_list = [187, 191, 193, 194]\n", + "vehicle id = 187\n", + "\n", + "vehicle id = 191\n", + "\n", + "Features file is not empty. Filename = ../lmfit/upper_5/191_idmfeats.txt\n", + "vehicle id = 193\n", + "\n", + "Features file is not empty. Filename = ../lmfit/upper_5/193_idmfeats.txt\n", + "vehicle id = 194\n", + "\n", + "Features file is not empty. Filename = ../lmfit/upper_5/194_idmfeats.txt\n", + "name = upper_6\n", + "id_list = [211, 213, 215, 217, 221]\n", + "vehicle id = 211\n", + "\n", + "Features file is not empty. Filename = ../lmfit/upper_6/211_idmfeats.txt\n", + "vehicle id = 213\n", + "\n", + "vehicle id = 215\n", + "\n", + "Features file is not empty. Filename = ../lmfit/upper_6/215_idmfeats.txt\n", + "vehicle id = 217\n", + "\n", + "Features file is not empty. Filename = ../lmfit/upper_6/217_idmfeats.txt\n", + "vehicle id = 221\n", + "\n", + "Features file is not empty. Filename = ../lmfit/upper_6/221_idmfeats.txt\n", + "name = upper_7\n", + "id_list = [248, 249, 250, 256]\n", + "vehicle id = 248\n", + "\n", + "vehicle id = 249\n", + "\n", + "Features file is not empty. Filename = ../lmfit/upper_7/249_idmfeats.txt\n", + "vehicle id = 250\n", + "\n", + "Features file is not empty. Filename = ../lmfit/upper_7/250_idmfeats.txt\n", + "vehicle id = 256\n", + "\n", + "Features file is not empty. Filename = ../lmfit/upper_7/256_idmfeats.txt\n", + "name = upper_8\n", + "id_list = [272, 275, 276, 277, 279]\n", + "vehicle id = 272\n", + "\n", + "vehicle id = 275\n", + "\n", + "Features file is not empty. Filename = ../lmfit/upper_8/275_idmfeats.txt\n", + "vehicle id = 276\n", + "\n", + "Features file is not empty. Filename = ../lmfit/upper_8/276_idmfeats.txt\n", + "vehicle id = 277\n", + "\n", + "Features file is not empty. Filename = ../lmfit/upper_8/277_idmfeats.txt\n", + "vehicle id = 279\n", + "\n", + "Features file is not empty. Filename = ../lmfit/upper_8/279_idmfeats.txt\n", + "name = upper_9\n", + "id_list = [294, 295, 300, 307, 308, 311]\n", + "vehicle id = 294\n", + "\n", + "vehicle id = 295\n", + "\n", + "Features file is not empty. Filename = ../lmfit/upper_9/295_idmfeats.txt\n", + "vehicle id = 300\n", + "\n", + "Features file is not empty. Filename = ../lmfit/upper_9/300_idmfeats.txt\n", + "vehicle id = 307\n", + "\n", + "Features file is not empty. Filename = ../lmfit/upper_9/307_idmfeats.txt\n", + "vehicle id = 308\n", + "\n", + "Features file is not empty. Filename = ../lmfit/upper_9/308_idmfeats.txt\n", + "vehicle id = 311\n", + "\n", + "Features file is not empty. Filename = ../lmfit/upper_9/311_idmfeats.txt\n", + "name = upper_10\n", + "id_list = [324, 327, 329, 333, 335, 339, 341, 346, 354, 357]\n", + "vehicle id = 324\n", + "\n", + "vehicle id = 327\n", + "\n", + "Features file is not empty. Filename = ../lmfit/upper_10/327_idmfeats.txt\n", + "vehicle id = 329\n", + "\n", + "Features file is not empty. Filename = ../lmfit/upper_10/329_idmfeats.txt\n", + "vehicle id = 333\n", + "\n", + "Features file is not empty. Filename = ../lmfit/upper_10/333_idmfeats.txt\n", + "vehicle id = 335\n", + "\n", + "Features file is not empty. Filename = ../lmfit/upper_10/335_idmfeats.txt\n", + "vehicle id = 339\n", + "\n", + "Features file is not empty. Filename = ../lmfit/upper_10/339_idmfeats.txt\n", + "vehicle id = 341\n", + "\n", + "Features file is not empty. Filename = ../lmfit/upper_10/341_idmfeats.txt\n", + "vehicle id = 346\n", + "\n", + "Features file is not empty. Filename = ../lmfit/upper_10/346_idmfeats.txt\n", + "vehicle id = 354\n", + "\n", + "Features file is not empty. Filename = ../lmfit/upper_10/354_idmfeats.txt\n", + "vehicle id = 357\n", + "\n", + "Features file is not empty. Filename = ../lmfit/upper_10/357_idmfeats.txt\n" + ] } ], "source": [ - "sum(abs(result.residual))/130" + "# Loop over the scenarios. Save fit params in eg: `lmfit/upper_1/veh_id_lmfit_params.txt\n", + "for j in range(0,len(scenarios_upper)):\n", + " id_list = scenarios_upper[j]\n", + " i = j+1 # because used to read files so can't start from 0\n", + " scenario_name=\"upper_{}\".format(i)\n", + " print(\"name = {}\".format(scenario_name))\n", + " print(\"id_list = {}\".format(id_list))\n", + " \n", + " for vehid in id_list:\n", + " print(\"vehicle id = {}\\n\".format(vehid))\n", + " idm_feat_filename = \"../lmfit/{}/{}_idmfeats.txt\".format(scenario_name,vehid)\n", + " if os.stat(idm_feat_filename).st_size != 0:\n", + " print(\"Features file is not empty. Filename = {}\".format(idm_feat_filename))\n", + " x = np.loadtxt(idm_feat_filename)\n", + " acc = np.loadtxt(\"../lmfit/{}/{}_trueacc.txt\".format(scenario_name,vehid))\n", + " # Perform the fit\n", + " result = minimize(IDM,params,args=(range(len(acc)),acc))\n", + " paramvals = np.array([result.params['smx'].value,\n", + " result.params['T'].value,result.params['dmn'].value])\n", + " np.savetxt(\"../lmfit/{}/{}_lmfit_params.txt\".format(scenario_name,vehid),paramvals)" ] } ],