Skip to content

Commit

Permalink
Output path set for debugging
Browse files Browse the repository at this point in the history
Write them in the c++ extension for each process, combine them in
python. For now.
  • Loading branch information
Lisa Zorn committed Mar 8, 2016
1 parent a47b284 commit 36e3596
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
59 changes: 58 additions & 1 deletion fasttrips/FastTrips.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"""
import os
from operator import attrgetter
import pandas
import transitfeed

from .Assignment import Assignment
Expand All @@ -36,6 +37,9 @@ class FastTrips:
#: Debug log filename. Detailed output goes here, including trace information.
DEBUG_LOG = "ft_debug%s.log"

#: Pathset debug filename. Writes pathset here.
PATHSET_LOG = "ft_pathset%s.txt"

def __init__(self, input_network_dir, input_demand_dir, output_dir,
is_child_process=False, logname_append="", appendLog=False):
"""
Expand Down Expand Up @@ -93,6 +97,15 @@ def __init__(self, input_network_dir, input_demand_dir, output_dir,
os.path.join(self.output_dir, FastTrips.DEBUG_LOG % logname_append),
logToConsole=False if is_child_process else True, append=appendLog)

# clear pathset files if we're starting out -- reset them to just a header
# there will be one for the parent process, and one each for workers
pathset_filename = os.path.join(self.output_dir, FastTrips.PATHSET_LOG % logname_append)
if not appendLog:
FastTripsLogger.info("Writing %s" % pathset_filename)
pathset_file = open(pathset_filename, 'w')
pathset_file.write("iteration passenger_id_num trip_list_id_num path_cost path_probability path_board_stops path_trips path_alight_stops\n")
pathset_file.close()

# Read the configuration
Assignment.read_configuration(self.input_network_dir, self.input_demand_dir)

Expand Down Expand Up @@ -143,6 +156,50 @@ def read_input_files(self):
else:
self.passengers = None

def combine_pathset_files(self):
"""
Since the pathset files are output by worker, let's combine them into a single file.
"""
procnum = 1
pathset_init = False
pathsets_df = None
# if we don't have one, no worries

while True:
logname_append = "_worker%02d" % procnum
pathset_filename = os.path.join(self.output_dir, FastTrips.PATHSET_LOG % logname_append)

if not os.path.exists(pathset_filename):
break

# read the pathset
pathset_df = pandas.read_table(pathset_filename, sep="[ ]+")
FastTripsLogger.info("Read %d lines from %s" % (len(pathset_df), pathset_filename))
# remove it
os.remove(pathset_filename)

# append to ours
if not pathset_init:
pathsets_df = pathset_df
pathset_init = True
else:
pathsets_df = pandas.concat([pathsets_df, pathset_df], axis=0)

# see if we have more
procnum += 1

if pathset_init:
# sort it by iteration, trip_id_num
pathsets_df.sort(columns=['iteration','trip_list_id_num'], inplace=True)
# write it
pathset_filename = os.path.join(self.output_dir, FastTrips.PATHSET_LOG % "")
pathsets_df.to_csv(pathset_filename, sep=" ", index=False)
FastTripsLogger.info("Wrote %d lines to %s" % (len(pathsets_df), pathset_filename))


def run_assignment(self, output_dir):
# Do it!
Assignment.assign_paths(output_dir, self);
Assignment.assign_paths(output_dir, self)

self.combine_pathset_files()

23 changes: 23 additions & 0 deletions src/pathfinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1750,6 +1750,18 @@ namespace fasttrips {
}
if (logsum == 0) { return false; } // fail

// debug -- print pet set to file
std::ofstream pathset_file;
std::ostringstream ss;
ss << output_dir_ << kPathSeparator;
ss << "ft_pathset";
if (process_num_ > 0) {
ss << "_worker" << std::setfill('0') << std::setw(2) << process_num_;
}
ss << ".txt";
// append
pathset_file.open(ss.str().c_str(), (std::ios_base::out | std::ios_base::app));

// for integerized probability*1000000
int cum_prob = 0;
int cost_cutoff = 1;
Expand All @@ -1775,7 +1787,18 @@ namespace fasttrips {
printPathCompat(trace_file, path_spec, paths_iter->first);
trace_file << std::endl;
}
// print path to pathset file
pathset_file << path_spec.iteration_ << " "; // Iteration
pathset_file << path_spec.passenger_id_ << " "; // The passenger ID
pathset_file << path_spec.path_id_ << " "; // The path ID - uniquely identifies a passenger+path
pathset_file << std::setw(8) << std::fixed << std::setprecision(2) << paths_iter->second.cost_ << " ";
pathset_file << std::setw(8) << std::fixed << std::setprecision(6) << paths_iter->second.probability_ << " ";
printPathCompat(pathset_file, path_spec, paths_iter->first);
pathset_file << std::endl;
}

pathset_file.close();

if (cum_prob == 0) { return false; } // fail

// choose path
Expand Down

0 comments on commit 36e3596

Please sign in to comment.