Skip to content

Commit

Permalink
Merge pull request #56 from singhish/minor_fixes
Browse files Browse the repository at this point in the history
Minor fixes
  • Loading branch information
shankari authored Apr 7, 2021
2 parents 34197e8 + dafc322 commit 40fb048
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 82 deletions.
18 changes: 15 additions & 3 deletions emeval/input/spec_details.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ def retrieve_data(self, user, key_list, start_ts, end_ts):
pass

def retrieve_all_data(self, user, key_list):
# sys.maxsize is used for end_ts as opposed to arrow.get().timestamp (the current timestamp)
# as it is a constant. This is to ensure that a FileSpecDetails instance can find data with
# the largest specified time range.
return self.retrieve_data(user, key_list, 0, sys.maxsize)

def get_current_spec(self):
Expand Down Expand Up @@ -84,21 +87,30 @@ def get_concat_trajectories(trip):
return gj.Feature(geometry=gj.LineString(coords_list),
properties={"modes": modes_list})

def get_ground_truth_for_leg(self, trip_id, leg_id):
def get_ground_truth_for_leg(self, trip_id, leg_id, start_ts, end_ts):
for t in self.curr_spec_entry["data"]["label"]["evaluation_trips"]:
if t["id"] == trip_id:
ll = [l for l in t["legs"] if l["id"] == leg_id]
# print(leg_id, len(ll), [l["id"] for l in ll])
if len(ll) == 1:
return ll[0]
ll = ll[0]
for key in ["loc", "start_loc", "end_loc", "route_coords"]:
if key in ll and isinstance(ll[key], list):
within_ts = [x for x in ll[key]
if start_ts >= x["properties"]["valid_start_ts"]
and end_ts <= x["properties"]["valid_end_ts"]]
assert len(within_ts) == 1, f"Invalid amount of {key} info for {leg['id']} between timestamps {start_ts} -> {end_ts}"
ll[key] = within_ts[0]
return ll


@staticmethod
def get_shapes_for_leg(gt_leg):
if gt_leg["type"] == "TRAVEL":
return {
"start_loc": shp.geometry.shape(gt_leg["start_loc"]["geometry"]),
"end_loc": shp.geometry.shape(gt_leg["end_loc"]["geometry"]),
"route": shp.geometry.shape(gt_leg['route_coords']['geometry'])
"route": shp.geometry.shape(gt_leg["route_coords"]["geometry"])
}
else:
return {"loc": shp.geometry.shape(gt_leg["loc"]["geometry"])}
Expand Down
66 changes: 26 additions & 40 deletions trajectory_evaluation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,6 @@
"pv_ucb = eipv.PhoneView(sd_ucb)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -145,19 +138,21 @@
"test_cases = {\n",
" \"commuter_rail_aboveground\": {\n",
" \"section\": pv_ucb.map()[\"android\"][\"ucb-sdb-android-3\"][\"evaluation_ranges\"][0][\"evaluation_trip_ranges\"][0][\"evaluation_section_ranges\"][2],\n",
" \"ground_truth\": sd_ucb.get_ground_truth_for_leg(\"mtv_to_berkeley_sf_bart\", \"commuter_rail_aboveground\")\n",
" },\n",
" \"light_rail_below_above_ground\": {\n",
" \"section\": pv_ucb.map()[\"android\"][\"ucb-sdb-android-3\"][\"evaluation_ranges\"][0][\"evaluation_trip_ranges\"][2][\"evaluation_section_ranges\"][7],\n",
" \"ground_truth\": sd_ucb.get_ground_truth_for_leg(\"berkeley_to_mtv_SF_express_bus\", \"light_rail_below_above_ground\")\n",
" },\n",
" \"express_bus\": {\n",
" \"section\": pv_ucb.map()[\"ios\"][\"ucb-sdb-ios-3\"][\"evaluation_ranges\"][1][\"evaluation_trip_ranges\"][2][\"evaluation_section_ranges\"][4],\n",
" \"ground_truth\": sd_ucb.get_ground_truth_for_leg(\"berkeley_to_mtv_SF_express_bus\", \"express_bus\")\n",
" },\n",
"}\n",
"\n",
"for t in test_cases.values():\n",
"for i, (k, t) in enumerate(test_cases.items()):\n",
" t[\"ground_truth\"] = sd_ucb.get_ground_truth_for_leg(\n",
" \"mtv_to_berkeley_sf_bart\" if i == 0 else \"berkeley_to_mtv_SF_express_bus\",\n",
" k,\n",
" t[\"section\"][\"start_ts\"],\n",
" t[\"section\"][\"end_ts\"])\n",
" t[\"gt_shapes\"] = gpd.GeoSeries(eisd.FileSpecDetails.get_shapes_for_leg(t[\"ground_truth\"]))"
]
},
Expand All @@ -179,7 +174,7 @@
"dist_checks = []\n",
"pct_checks = []\n",
"\n",
"for (k, t) in test_cases.items():\n",
"for k, t in test_cases.items():\n",
" location_gpdf = emd.filter_geo_df(emd.to_geo_df(t[\"section\"][\"location_df\"]), t[\"gt_shapes\"].filter([\"start_loc\",\"end_loc\"]))\n",
" gt_linestring = emd.filter_ground_truth_linestring(t[\"gt_shapes\"])\n",
" dc = emd.dist_using_circumference(location_gpdf, gt_linestring)\n",
Expand Down Expand Up @@ -231,7 +226,7 @@
"outputs": [],
"source": [
"# t = \"commuter_rail_aboveground\"\n",
"# gt_gj = eisd.FileSpecDetails.get_geojson_for_leg(test_cases[t][\"ground_truth\"])\n",
"# gt_gj = eisd.SpecDetails.get_geojson_for_leg(test_cases[t][\"ground_truth\"])\n",
"# print(gt_gj.features[2])\n",
"# gt_gj.features[2] = ezgj.get_geojson_for_linestring(emd.filter_ground_truth_linestring(test_cases[t][\"gt_shapes\"]))\n",
"# curr_map = ezgj.get_map_for_geojson(gt_gj)\n",
Expand Down Expand Up @@ -274,7 +269,7 @@
"outputs": [],
"source": [
"# curr_map = ezpv.display_map_detail_from_df(manual_check_points)\n",
"# curr_map.add_child(folium.GeoJson(eisd.FileSpecDetails.get_geojson_for_leg(t[\"ground_truth\"])))"
"# curr_map.add_child(folium.GeoJson(eisd.SpecDetails.get_geojson_for_leg(t[\"ground_truth\"])))"
]
},
{
Expand Down Expand Up @@ -340,9 +335,8 @@
" trip_errors = []\n",
" for (sr_idx, sr) in enumerate(tr[\"evaluation_section_ranges\"]):\n",
" # This is a Shapely LineString\n",
" \n",
" section_gt_leg = pv.spec_details.get_ground_truth_for_leg(tr[\"trip_id_base\"], sr[\"trip_id_base\"])\n",
" section_gt_shapes = gpd.GeoSeries(eisd.FileSpecDetails.get_shapes_for_leg(section_gt_leg))\n",
" section_gt_leg = pv.spec_details.get_ground_truth_for_leg(tr[\"trip_id_base\"], sr[\"trip_id_base\"], sr[\"start_ts\"], sr[\"end_ts\"])\n",
" section_gt_shapes = gpd.GeoSeries(eisd.SpecDetails.get_shapes_for_leg(section_gt_leg))\n",
" if len(section_gt_shapes) == 1:\n",
" print(\"No ground truth route for %s %s, must be polygon, skipping...\" % (tr[\"trip_id_base\"], sr[\"trip_id_base\"]))\n",
" assert section_gt_leg[\"type\"] != \"TRAVEL\", \"For %s, %s, %s, %s, %s found type %s\" % (phone_os, phone_label, r_idx, tr_idx, sr_idx, section_gt_leg[\"type\"])\n",
Expand Down Expand Up @@ -671,9 +665,9 @@
},
"outputs": [],
"source": [
"gt_leg = sd_ucb.get_ground_truth_for_leg(\"berkeley_to_mtv_SF_express_bus\", \"express_bus\"); print(gt_leg[\"id\"])\n",
"gt_leg = test_cases[\"express_bus\"][\"ground_truth\"]; print(gt_leg[\"id\"])\n",
"curr_map = ezgj.get_map_for_geojson(sd_ucb.get_geojson_for_leg(gt_leg), name=\"ground_truth\")\n",
"ezgj.get_fg_for_loc_df(emd.linestring_to_geo_df(eisd.FileSpecDetails.get_shapes_for_leg(gt_leg)[\"route\"]),\n",
"ezgj.get_fg_for_loc_df(emd.linestring_to_geo_df(eisd.SpecDetails.get_shapes_for_leg(gt_leg)[\"route\"]),\n",
" name=\"gt_points\", color=\"green\").add_to(curr_map)\n",
"\n",
"name_err_time = lambda lr: \"%d: %d, %s, %s\" % (lr[\"index\"], lr[\"df_idx\"], lr[\"error\"], sd_ucb.fmt(lr[\"ts\"], \"MM-DD HH:mm:ss\"))\n",
Expand Down Expand Up @@ -702,9 +696,9 @@
},
"outputs": [],
"source": [
"gt_leg = sd_ucb.get_ground_truth_for_leg(\"berkeley_to_mtv_SF_express_bus\", \"express_bus\"); print(gt_leg[\"id\"])\n",
"gt_leg = test_cases[\"express_bus\"][\"ground_truth\"]; print(gt_leg[\"id\"])\n",
"curr_map = ezgj.get_map_for_geojson(sd_ucb.get_geojson_for_leg(gt_leg), name=\"ground_truth\")\n",
"ezgj.get_fg_for_loc_df(emd.linestring_to_geo_df(eisd.FileSpecDetails.get_shapes_for_leg(gt_leg)[\"route\"]),\n",
"ezgj.get_fg_for_loc_df(emd.linestring_to_geo_df(eisd.SpecDetails.get_shapes_for_leg(gt_leg)[\"route\"]),\n",
" name=\"gt_points\", color=\"green\").add_to(curr_map)\n",
"\n",
"name_err_time = lambda lr: \"%d: %d, %s, %s\" % (lr[\"index\"], lr[\"df_idx\"], lr[\"error\"], sd_ucb.fmt(lr[\"ts\"], \"MM-DD HH:mm:ss\"))\n",
Expand Down Expand Up @@ -754,9 +748,9 @@
"metadata": {},
"outputs": [],
"source": [
"gt_leg = sd_ucb.get_ground_truth_for_leg(\"mtv_to_berkeley_sf_bart\", \"commuter_rail_aboveground\"); print(gt_leg[\"id\"])\n",
"gt_leg = test_cases[\"commuter_rail_aboveground\"][\"ground_truth\"]; print(gt_leg[\"id\"])\n",
"curr_map = ezgj.get_map_for_geojson(sd_ucb.get_geojson_for_leg(gt_leg), name=\"ground_truth\")\n",
"ezgj.get_fg_for_loc_df(emd.linestring_to_geo_df(eisd.FileSpecDetails.get_shapes_for_leg(gt_leg)[\"route\"]),\n",
"ezgj.get_fg_for_loc_df(emd.linestring_to_geo_df(eisd.SpecDetails.get_shapes_for_leg(gt_leg)[\"route\"]),\n",
" name=\"gt_points\", color=\"green\").add_to(curr_map)\n",
"\n",
"name_err_time = lambda lr: \"%d: %d, %s, %s\" % (lr[\"index\"], lr[\"df_idx\"], lr[\"error\"], sd_ucb.fmt(lr[\"ts\"], \"MM-DD HH:mm:ss\"))\n",
Expand Down Expand Up @@ -871,7 +865,7 @@
"source": [
"gt_leg = sd_ucb.get_ground_truth_for_leg(\"mtv_to_berkeley_sf_bart\", \"walk_to_bus\"); print(gt_leg[\"id\"])\n",
"curr_map = ezgj.get_map_for_geojson(sd_ucb.get_geojson_for_leg(gt_leg), name=\"ground_truth\")\n",
"ezgj.get_fg_for_loc_df(emd.linestring_to_geo_df(eisd.FileSpecDetails.get_shapes_for_leg(gt_leg)[\"route\"]),\n",
"ezgj.get_fg_for_loc_df(emd.linestring_to_geo_df(eisd.SpecDetails.get_shapes_for_leg(gt_leg)[\"route\"]),\n",
" name=\"gt_points\", color=\"green\").add_to(curr_map)\n",
"\n",
"name_err_time = lambda lr: \"%d: %d, %s, %s\" % (lr[\"index\"], lr[\"df_idx\"], lr[\"error\"], sd_ucb.fmt(lr[\"ts\"], \"MM-DD HH:mm:ss\"))\n",
Expand Down Expand Up @@ -929,7 +923,7 @@
"ucb_and_back = pv_ucb.map()[\"android\"][\"ucb-sdb-android-2\"][\"evaluation_ranges\"][0]; ucb_and_back[\"trip_id\"]\n",
"back_trip = ucb_and_back[\"evaluation_trip_ranges\"][2]; print(back_trip[\"trip_id\"])\n",
"lt_leg = back_trip[\"evaluation_section_ranges\"][7]; print(lt_leg[\"trip_id\"])\n",
"gt_leg = sd_ucb.get_ground_truth_for_leg(back_trip[\"trip_id_base\"], lt_leg[\"trip_id_base\"]); gt_leg[\"id\"]"
"gt_leg = sd_ucb.get_ground_truth_for_leg(back_trip[\"trip_id_base\"], lt_leg[\"trip_id_base\"], lt_leg[\"start_ts\"], lt_leg[\"end_ts\"]); gt_leg[\"id\"]"
]
},
{
Expand All @@ -947,9 +941,9 @@
"metadata": {},
"outputs": [],
"source": [
"gt_leg = sd_ucb.get_ground_truth_for_leg(\"berkeley_to_mtv_SF_express_bus\", \"light_rail_below_above_ground\"); print(gt_leg[\"id\"])\n",
"gt_leg = test_cases[\"light_rail_below_above_ground\"][\"ground_truth\"]; print(gt_leg[\"id\"])\n",
"curr_map = ezgj.get_map_for_geojson(sd_ucb.get_geojson_for_leg(gt_leg), name=\"ground_truth\")\n",
"ezgj.get_fg_for_loc_df(emd.linestring_to_geo_df(eisd.FileSpecDetails.get_shapes_for_leg(gt_leg)[\"route\"]),\n",
"ezgj.get_fg_for_loc_df(emd.linestring_to_geo_df(eisd.SpecDetails.get_shapes_for_leg(gt_leg)[\"route\"]),\n",
" name=\"gt_points\", color=\"green\").add_to(curr_map)\n",
"\n",
"name_err_time = lambda lr: \"%d: %d, %s, %s\" % (lr[\"index\"], lr[\"df_idx\"], lr[\"error\"], sd_ucb.fmt(lr[\"ts\"], \"MM-DD HH:mm:ss\"))\n",
Expand Down Expand Up @@ -982,7 +976,7 @@
"outputs": [],
"source": [
"bart_leg = pv_ucb.map()[\"android\"][\"ucb-sdb-android-3\"][\"evaluation_ranges\"][0][\"evaluation_trip_ranges\"][0][\"evaluation_section_ranges\"][5]\n",
"gt_leg = sd_ucb.get_ground_truth_for_leg(\"mtv_to_berkeley_sf_bart\", \"subway_underground\"); gt_leg[\"id\"]"
"gt_leg = sd_ucb.get_ground_truth_for_leg(\"mtv_to_berkeley_sf_bart\", \"subway_underground\", bart_leg[\"start_ts\"], bart_leg[\"end_ts\"]); gt_leg[\"id\"]"
]
},
{
Expand All @@ -991,9 +985,8 @@
"metadata": {},
"outputs": [],
"source": [
"gt_leg = sd_ucb.get_ground_truth_for_leg(\"mtv_to_berkeley_sf_bart\", \"subway_underground\"); print(gt_leg[\"id\"])\n",
"curr_map = ezgj.get_map_for_geojson(sd_ucb.get_geojson_for_leg(gt_leg), name=\"ground_truth\")\n",
"ezgj.get_fg_for_loc_df(emd.linestring_to_geo_df(eisd.FileSpecDetails.get_shapes_for_leg(gt_leg)[\"route\"]),\n",
"ezgj.get_fg_for_loc_df(emd.linestring_to_geo_df(eisd.SpecDetails.get_shapes_for_leg(gt_leg)[\"route\"]),\n",
" name=\"gt_points\", color=\"green\").add_to(curr_map)\n",
"\n",
"name_err_time = lambda lr: \"%d: %d, %s, %s\" % (lr[\"index\"], lr[\"df_idx\"], lr[\"error\"], sd_ucb.fmt(lr[\"ts\"], \"MM-DD HH:mm:ss\"))\n",
Expand All @@ -1006,7 +999,7 @@
"ezgj.get_fg_for_loc_df(error_df, name=\"sensed_points\", color=\"red\", popupfn=name_err_time, stickyfn=gt_16k).add_to(curr_map)\n",
"\n",
"folium.LayerControl().add_to(curr_map)\n",
"curr_map\n"
"curr_map"
]
},
{
Expand All @@ -1015,8 +1008,8 @@
"metadata": {},
"outputs": [],
"source": [
"gt_leg = sd_ucb.get_ground_truth_for_leg(\"mtv_to_berkeley_sf_bart\", \"subway_underground\"); gt_leg[\"id\"]\n",
"eisd.FileSpecDetails.get_shapes_for_leg(gt_leg)[\"route\"].is_simple"
"gt_leg = sd_ucb.get_ground_truth_for_leg(\"mtv_to_berkeley_sf_bart\", \"subway_underground\", bart_leg[\"start_ts\"], bart_leg[\"end_ts\"]); gt_leg[\"id\"]\n",
"eisd.SpecDetails.get_shapes_for_leg(gt_leg)[\"route\"].is_simple"
]
},
{
Expand Down Expand Up @@ -1097,13 +1090,6 @@
"source": [
"pd.concat([pd.DataFrame([{\"a\": 10, \"b\": 14}]), df, pd.DataFrame([{\"a\": 20, \"b\": 24}])], axis='index').reset_index(drop=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down
Loading

0 comments on commit 40fb048

Please sign in to comment.