예제 #1
0
def run_setup(input_network_dir,
              input_demand_dir,
              input_weights,
              run_config,
              iters,
              output_dir,
              pathfinding_type='stochastic',
              input_functions=None,
              output_folder=None,
              trace_only=False,
              skim_config_file=None,
              **kwargs):
    """
    Reads run configuration files from network and demand input directories.
    If additional parameters are input here, they will override the run configuration files.

    Named Keyword arguments:
        input_network_dir -- the input directory with GTFS-PLUS networks (required)
        input_demand_dir -- the input directory with the dyno-demand files (required)
        input_weights -- the file with the pathweight parameters (required)
        run_config -- the file with further run configs (required)
        iters -- number of global iterations, integer (required)
        output_dir -- where to put the output folder (required). Will be created if it doesn't already exist.

        pathfinding_type -- one of ['stochastic','deterministic','file'] (default: stochastic)
        input_functions -- the file with user functions
        output_folder -- where to put the outputs.  Will be created if it doesn't already exist.
        trace_only -- will only run demand for the trace.  Will not overwrite other output (default: False)


    Unnamed keyword arguments:
        num_trips -- if specified, will process this number of trips and ignore the rest
        pf_iters -- Integer. If specified, will set the maximum number of pathfinding iterations(default: 10)
        dispersion -- theta parameter; essentially the nesting parameter. Good value is between 0.5-1. (default: 1.0)
        max_stop_process_count = maximum number of times you will re-processe a node (default: 20)
        capacity -- Boolean to activate capacity constraints (default: False)

        overlap_variable -- One of ['None','count','distance','time']. Variable to use for overlap penalty calculation (default: 'count')
        overlap_split_transit -- Boolean.Split transit for path overlap penalty calculation (default: False)

        time_window = Integer. The time window in minutes where a passenger searches for a eminating transit route at each node.
        transfer_fare_ignore_pathfinding = Boolean. In path-finding, suppress trying to adjust fares using transfer rules.  For performance.
        transfer_fare_ignore_pathenum = Boolean. In path-enumeration, suppress trying to adjust fares using transfer rules.  For performance.
        number_of_processes = Integer. Number of processes to run at once (default: 1)
        output_pathset_per_sim_iter = Boolean. Output pathsets per simulation iteration?  (default: false)

        debug_output_columnns -- boolean to activate extra columns for debugging (default: False)
    """
    print(kwargs)

    if not input_network_dir:
        msg = "Must specify where to find input networks"
        FastTripsLogger.fatal(msg)
        raise fasttrips.ConfigurationError("external input", msg)

    if not input_demand_dir:
        msg = "Must specify where to find input demand"
        FastTripsLogger.fatal(msg)
        raise fasttrips.ConfigurationError("external input", msg)

    if not input_weights:
        msg = "Must specify where pathweight parameters are"
        FastTripsLogger.fatal(msg)
        raise fasttrips.ConfigurationError("external input", msg)

    if not run_config:
        msg = "Must specify file with run configurations"
        FastTripsLogger.fatal(msg)
        raise fasttrips.ConfigurationError("external input", msg)

    if pathfinding_type not in ['deterministic', 'stochastic', 'file']:
        msg = "pathfinding.type [%s] not defined. Expected values: %s" % (
            pathfinding_type, ['deterministic', 'stochastic', 'file'])
        FastTripsLogger.fatal(msg)
        raise fasttrips.ConfigurationError("external override", msg)

    # Setup Output Directory
    if not output_folder:
        output_folder = "output_%s_iter%d_%s" % (
            pathfinding_type, iters, "cap" if
            ("capacity" in kwargs and kwargs["capacity"] == True) else "nocap")

    # don't override full run results
    if trace_only:
        output_folder = "%s_trace" % output_folder

    if not output_dir:
        output_dir = os.path.basename(input_demand_dir)

    # create folder if it doesn't already exist
    full_output_dir = os.path.join(output_dir, output_folder)

    if not os.path.exists(output_dir):
        os.mkdir(output_dir)

    if not os.path.exists(full_output_dir):
        print("Creating full output dir [%s]" % full_output_dir)
        os.mkdir(full_output_dir)

    # Create fast-trips instance
    ft = fasttrips.FastTrips(input_network_dir,
                             input_demand_dir,
                             input_weights,
                             run_config,
                             full_output_dir,
                             input_functions=input_functions,
                             skim_config_file=skim_config_file)

    # Read the configuration file and overwrite with any options called with the function call
    ft.read_configuration()

    if iters > 0:
        fasttrips.Assignment.MAX_ITERATIONS = int(iters)

    if "pf_iters" in kwargs:
        fasttrips.Assignment.MAX_PF_ITERATIONS = kwargs["pf_iters"]

    if "number_of_processes" in kwargs:
        fasttrips.Assignment.NUMBER_OF_PROCESSES = kwargs[
            "number_of_processes"]

    if "trace_ids" in list(kwargs.keys()):
        fasttrips.Assignment.TRACE_IDS = kwargs["trace_ids"]

    if trace_only:
        if len(fasttrips.Assignment.TRACE_IDS) == 0:
            print(
                "Trace only requested but no trace IDs are specified in configuration."
            )
            sys.exit(2)
        fasttrips.Assignment.DEBUG_TRACE_ONLY = True
        fasttrips.Assignment.NUMBER_OF_PROCESSES = 1

    if "pathfinding_type" in list(kwargs.keys()):
        fasttrips.Assignment.PATHFINDING_TYPE = kwargs["pathfinding_type"]

    if "learning_convergence" in list(kwargs.keys()):
        fasttrips.PathSet.LEARN_ROUTES = kwargs["learning_convergence"]

    if "max_stop_process_count" in list(kwargs.keys()):

        fasttrips.Assignment.STOCH_MAX_STOP_PROCESS_COUNT = kwargs[
            "max_stop_process_count"]

    if "debug_output_columns" in list(kwargs.keys()):
        fasttrips.Assignment.DEBUG_OUTPUT_COLUMNS = kwargs[
            "debug_output_columns"]

    if "overlap_variable" in list(kwargs.keys()):
        if kwargs["overlap_variable"] not in [
                'None', 'count', 'distance', 'time'
        ]:
            msg = "pathfinding.overlap_variable [%s] not defined. Expected values: %s" % (
                kwargs["overlap_variable"],
                str(fasttrips.PathSet.OVERLAP_VARIABLE_OPTIONS))
            fasttrips.FastTripsLogger.fatal(msg)
            raise fasttrips.ConfigurationError("external override", msg)
        fasttrips.PathSet.OVERLAP_VARIABLE = kwargs["overlap_variable"]

    if "overlap_split_transit" in list(kwargs.keys()):
        fasttrips.PathSet.OVERLAP_SPLIT_TRANSIT = kwargs[
            "overlap_split_transit"]

    if "transfer_fare_ignore_pathfinding" in list(kwargs.keys()):
        fasttrips.Assignment.TRANSFER_FARE_IGNORE_PATHFINDING = kwargs[
            "transfer_fare_ignore_pathfinding"]

    if "transfer_fare_ignore_pathenum" in list(kwargs.keys()):
        fasttrips.Assignment.TRANSFER_FARE_IGNORE_PATHENUM = kwargs[
            "transfer_fare_ignore_pathenum"]

    if "time_window" in list(kwargs.keys()):
        fasttrips.Assignment.TIME_WINDOW = datetime.timedelta(
            minutes=float(kwargs["time_window"]))

    if "utils_conversion_factor" in list(kwargs.keys()):
        fasttrips.Assignment.UTILS_CONVERSION = kwargs[
            "utils_conversion_factor"]

    if "dispersion" in list(kwargs.keys()):
        fasttrips.Assignment.STOCH_DISPERSION = kwargs["dispersion"]

    if "num_trips" in list(kwargs.keys()):
        fasttrips.Assignment.DEBUG_NUM_TRIPS = kwargs["num_trips"]

    if "capacity" in list(kwargs.keys()):
        fasttrips.Assignment.CAPACITY_CONSTRAINT = kwargs["capacity"]

    if "output_pathset_per_sim_iter" in list(kwargs.keys()):
        fasttrips.Assignment.OUTPUT_PATHSET_PER_SIM_ITER = kwargs[
            "output_pathset_per_sim_iter"]

    if "user_class_function" in list(kwargs.keys()):
        fasttrips.PathSet.USER_CLASS_FUNCTION = kwargs["user_class_function"]

    return ft
예제 #2
0
    person_trips_df = person_trips_df.sort_values(['Unique_ID','route_id','agency_id','stop_dist'])
    person_trips_df = person_trips_df.groupby(['Unique_ID','route_id']).head(1)

    # rename the new columns
    person_trips_df.rename(columns={"stop_id"   :"%s_stop_id"   % location_prefix,
                                    "stop_name" :"%s_stop_name" % location_prefix,
                                    "stop_lat"  :"%s_stop_lat"  % location_prefix,
                                    "stop_lon"  :"%s_stop_lon"  % location_prefix,
                                    "stop_dist" :"%s_stop_dist" % location_prefix,
                                    "route_id"  :"%s_route_id"  % location_prefix,
                                    "agency_id" :"%s_agency_id"  % location_prefix}, inplace=True)
    return person_trips_df

if __name__ == "__main__":
    ft = fasttrips.FastTrips(input_network_dir= NETWORK_DIR,
                             input_demand_dir = None,
                             output_dir       = ".")
 
    ft.read_input_files()
    # Get the full (joined) transit vehicle trip table and add lat/lon for the stops
    full_trips_df = ft.trips.get_full_trips()
    full_trips_df = ft.stops.add_stop_lat_lon(full_trips_df, id_colname="stop_id", new_lat_colname="stop_lat", new_lon_colname="stop_lon", new_stop_name_colname="stop_name")
    # Add operator_type as the mutual field between CHTS and GTFS vehicle trips
    full_trips_df["operator_type"] = full_trips_df["agency_id"].replace(NETWORK_AGENCY_TO_OPERATOR_TYPE)

    # Read CHTS observed person trips file
    df = pd.read_csv('CHTS_ft_output.csv')
    # Add operator_type as the mutual field between CHTS and GTFS vehicle trips
    df["operator_type"] = df["transit_mode_no"].replace(CHTS_MODE_TO_OPERATOR_TYPE)
    # Add Unique_ID to be used for merging later on
    df['Unique_ID'] = df.index
        '--description',
        dest='use_description',
        action='store_true',
        help=
        "Specify this to use path description as ID.  Otherwise will use standard pathset fields (person_id, person_trip_id, iteration, pathfinding_iteration, simulation_iteration, pathnum)"
    )

    args = parser.parse_args()
    LOG_DIR = "create_tableau_path_map"

    if not os.path.exists(LOG_DIR):
        os.mkdir(LOG_DIR)

    # Use fasttrips to read the network
    ft = fasttrips.FastTrips(input_network_dir=args.input_network_dir[0],
                             input_demand_dir=None,
                             output_dir=LOG_DIR)
    ft.read_input_files()

    # and the pathset files
    (pathset_paths_df,
     pathset_links_df) = ft.passengers.read_passenger_pathsets(
         args.input_path_dir[0], ft.stops, ft.routes.modes_df)
    fasttrips.Assignment.TRACE_PERSON_IDS = pathset_paths_df["person_id"].head(
        5).tolist()
    fasttrips.FastTripsLogger.debug("Read passenger pathsets. head=\n%s" %
                                    str(pathset_links_df.head(100)))

    pathset_links_df = add_taz_coords(args.input_network_dir[0],
                                      pathset_links_df)
    pathset_links_df = add_stop_coords(ft, pathset_links_df)
예제 #4
0
    else:
        test_dir = "%s%s_iter%d_%s" % (
            "" if args.input_network_dir == args.input_demand_dir else "%s_" %
            os.path.basename(args.input_demand_dir), args.pathfinding_type,
            args.iters, "cap" if args.capacity else "nocap")

        # don't override full run results
        if args.trace_only:
            test_dir = "%s_trace" % test_dir

    full_output_dir = os.path.join(args.output_loc, test_dir)
    if not os.path.exists(full_output_dir):
        print "Creating full output dir [%s]" % full_output_dir
        os.mkdir(full_output_dir)

    ft = fasttrips.FastTrips(args.input_network_dir, args.input_demand_dir,
                             full_output_dir)

    # Read the configuration here so we can overwrite options below
    ft.read_configuration()

    fasttrips.Assignment.PATHFINDING_TYPE = args.pathfinding_type
    fasttrips.Assignment.ITERATION_FLAG = int(args.iters)

    if args.max_stop_process_count:
        fasttrips.Assignment.STOCH_MAX_STOP_PROCESS_COUNT = args.max_stop_process_count

    if args.overlap_variable:
        fasttrips.PathSet.OVERLAP_VARIABLE = args.overlap_variable

    if args.overlap_split_transit:
        fasttrips.PathSet.OVERLAP_SPLIT_TRANSIT = args.overlap_split_transit
예제 #5
0
                rec['agency_id'] = ''
            done_queue.put(rec)
        except:
            FastTripsLogger.exception("Exception")
            # call it a day
            done_queue.put((worker_num, "EXCEPTION", str(sys.exc_info())))
            break


if __name__ == "__main__":
    start_time = datetime.datetime.now()

    ## Read FT network input files and get transit trips information
    ft = fasttrips.FastTrips(input_network_dir=NETWORK_DIR,
                             input_demand_dir=None,
                             input_weights=None,
                             run_config=None,
                             output_dir=".")
    fasttrips.FastTripsLogger.info("Reading network from [%s]" % NETWORK_DIR)

    ft.read_input_files()
    # Get the full (joined) transit vehicle trip table and add lat/lon for the stops
    full_trips_df = ft.trips.get_full_trips()
    full_trips_df = ft.stops.add_stop_lat_lon(
        full_trips_df,
        id_colname="stop_id",
        new_lat_colname="stop_lat",
        new_lon_colname="stop_lon",
        new_stop_name_colname="stop_name")
    #     full_trips_df.to_csv('full_trips.csv', index=False)
    #     sys.exit()
예제 #6
0
    pd.set_option('display.width', 1000)
    pd.set_option('display.max_rows', 1000)
    pd.set_option('display.max_columns', 100)

    parser = argparse.ArgumentParser()
    parser.add_argument("network_dir",
                        type=str,
                        help="Location of GTFS-Plus network")
    args = parser.parse_args(sys.argv[1:])

    # Initialize fasttrips; we'll use it to read the network
    fasttrips.FastTripsLogger.info("Reading network from [%s]" %
                                   args.network_dir)
    ft = fasttrips.FastTrips(input_network_dir=args.network_dir,
                             input_demand_dir=None,
                             output_dir=".")

    # Read the observed person trips file
    df = pd.read_csv('survey_wSFtaz.csv',
                     dtype={
                         "onoff_enter_station": object,
                         "onoff_exit_station": object,
                         "persons": object
                     },
                     na_values=["None"])
    # Keep only the columns we're interested in
    df = df[[
        'Unique_ID',
        'operator',
        'transfer_from',  # operator