Пример #1
0
def import_skims():
    # Import districts
    my_project.initialize_zone_partition('ga')
    my_project.process_zone_partition('inputs/trucks/' + districts_file)
    # Import truck operating costs
    my_project.import_matrices('inputs/trucks/truck_operating_costs.in')
    
    # Open GC skims from H5 container, average am/pm, import to emme:
    np_gc_skims = {}
    np_bidir_gc_skims = {}
    for tod in truck_generalized_cost_tod.keys():
        hdf_file = h5py.File('inputs/' + tod + '.h5', "r")
        for item in input_skims.values():
            #gc
            skim_name = item['gc_name']
            h5_skim = hdf_file['Skims'][skim_name]
            np_skim = np.matrix(h5_skim)
            np_gc_skims[skim_name + '_' + truck_generalized_cost_tod[tod]] = np_skim
        
            #distance
            skim_name = item['dist_name']
            h5_skim = hdf_file['Skims'][skim_name]
            np_skim = np.matrix(h5_skim)
            np_gc_skims[skim_name + '_' + truck_generalized_cost_tod[tod]] = np_skim

    zones = my_project.current_scenario.zone_numbers
    zonesDim = len(my_project.current_scenario.zone_numbers)

    for truck_type in input_skims.values():
        #gc:
        am_skim_name = truck_type['gc_name'] + '_am'
        pm_skim_name = truck_type['gc_name'] + '_pm'
        bidir_skim_name = truck_type['gc_bidir_name']
        bi_dir_skim = np_gc_skims[am_skim_name] + np_gc_skims[pm_skim_name]
        bi_dir_skim = np.asarray(bi_dir_skim)
        #have sum, now get average
        bi_dir_skim *= .5
        bi_dir_skim = bi_dir_skim[0:zonesDim, 0:zonesDim]
        np_bidir_gc_skims[bidir_skim_name] = bi_dir_skim
   
        #distance
        am_skim_name = truck_type['dist_name'] + '_am'
        pm_skim_name = truck_type['dist_name'] + '_pm'
        bidir_skim_name = truck_type['dist_bidir_name']
        #distance skims are multiplied by 100 when exported by SkimsAndPaths, so we devide by 100
        bi_dir_skim = (np_gc_skims[am_skim_name] + np_gc_skims[pm_skim_name])/100.0
        bi_dir_skim = np.asarray(bi_dir_skim)
        #have sum, now get average
        bi_dir_skim *= .5
        bi_dir_skim = bi_dir_skim[0:zonesDim, 0:zonesDim]
        np_bidir_gc_skims[bidir_skim_name] = bi_dir_skim

    #import bi-directional skims to emmebank
    for mat_name, matrix in np_bidir_gc_skims.iteritems():
        matrix_id = my_project.bank.matrix(str(mat_name)).id
        emme_matrix = ematrix.MatrixData(indices=[zones,zones],type='f')
        emme_matrix.raw_data=[_array.array('f',row) for row in matrix]
        my_project.bank.matrix(matrix_id).set_data(emme_matrix,my_project.current_scenario)
Пример #2
0
def hdf5_to_emme(my_project):

    start_import_hdf5 = time.time()

    #Determine the Path and Scenario File and Zone indicies that go with it
    current_scenario = my_project.desktop.data_explorer(
    ).primary_scenario.core_scenario.ref
    my_bank = current_scenario.emmebank
    zones = current_scenario.zone_numbers

    #Load in the necessary Dictionaries
    my_user_classes = load_dictionary(my_project, "user_classes")

    #Open the HDF5 Container in read only mode using "r"
    hdf_filename = os.path.join(os.path.dirname(my_bank.path),
                                'Skims/Emme_Skims.hdf5').replace("\\", "/")
    hdf_file = h5py.File(hdf_filename, "r")

    #Delimiter of a Demand Matrix in Emme - this could be part of a control file but hardcoded for now
    matrix_designation = 'v'

    for x in range(0, len(my_user_classes["Vehicle User Class"])):
        matrix_name = my_user_classes["Vehicle User Class"][x][
            "Name"] + matrix_designation
        matrix_id = my_bank.matrix(matrix_name).id

        try:
            hdf_matrix = hdf_file['Vehicle Class Demand'][matrix_name]
            np_matrix = np.matrix(hdf_matrix)
            np_matrix = np_matrix.astype(float)
            np_array = np.squeeze(np.asarray(np_matrix))
            emme_matrix = ematrix.MatrixData(indices=[zones, zones], type='f')
            emme_matrix.raw_data = [_array.array('f', row) for row in np_array]
            my_bank.matrix(matrix_id).set_data(emme_matrix, current_scenario)

        #If the HDF5 File does not have a matirx of that name
        except KeyError:

            print matrix_id + ' does not exist in the HDF5 container - no matrix was imported'

    hdf_file.close()
    end_import_hdf5 = time.time()

    print 'It took', round((end_import_hdf5 - start_import_hdf5) / 60,
                           2), 'minutes to import matrices to Emme.'
Пример #3
0
def avgMf(bank, matName1, matName2, scenarioNum):
    ''' Calculate the average OD values from two full matrices, and return them
        in the same "raw_data" format returned by getMf(). '''

    # Get matrices as numpy arrays
    mat1 = bank.matrix(matName1).get_data(scenarioNum).to_numpy()
    mat2 = bank.matrix(matName2).get_data(scenarioNum).to_numpy()

    # Calculate average cell values
    avg = np.mean(np.array([mat1, mat2]), axis=0)

    # Load averages into a new MatrixData object (NOT saved to emmebank)
    avgmat = _matrix.MatrixData([bank.scenario(scenarioNum).zone_numbers] * 2)
    avgmat.from_numpy(avg)
    mat = avgmat.raw_data

    # Get centroid numbers for scenario for matrix trailing zeros
    centroids = bank.scenario(scenarioNum).get_network().centroids()
    names = [i.id for i in centroids]

    # Return matrix data and zone names
    return (mat, names)
Пример #4
0
    def _ApplyODProbabilities(self, inputProbs, outputMatrix, probType):
        zoneList = self.Scenario.zone_numbers
        probMatrix = np.zeros((len(zoneList), len(zoneList)))
        if probType == 'ORIGIN':
            for key, probs in inputProbs.iteritems(
            ):  #iterate through the probability dictionary
                location = zoneList.index(
                    key
                )  #index the current station parking node in the zone list
                probMatrix[:, location] = probs[
                    0]  #insert a column (with the line group probability) at the station parking node location in the matrix
        elif probType == 'DESTINATION':
            for key, probs in inputProbs.iteritems(
            ):  #iterate through the probability dictionary
                location = zoneList.index(
                    key
                )  #index the current station parking node in the zone list
                probMatrix[location, :] = probs[
                    1]  #insert a row (with the line group probability) at the station parking node location in the matrix
        else:
            raise Exception("Need to specify either ORIGIN or DESTINATION")

        #if EMME_VERSION >= (4,2):
        #    print outputMatrix.id
        #    outputMatrix.set_numpy_data(probMatrix, scenario_id=self.Scenario.id) # set the probability matrix data to the matrix we created earlier
        if EMME_VERSION >= (4, 1, 2):
            zoneSystem = [self.Scenario.zone_numbers] * 2
            matrix_data = _matrix.MatrixData(zoneSystem, type='f')
            matrix_data.from_numpy(
                probMatrix)  #pull the data from the probability Matrix
            outputMatrix.set_data(
                matrix_data,
                self.Scenario.id)  #and set it to the matrix we created earlier
        else:
            raise Exception(
                "Please upgrade to at least Emme 4.1.2 to use this tool")
def hdf5_trips_to_Emme(my_project, hdf_filename):

    start_time = time.time()

    #Determine the Path and Scenario File and Zone indicies that go with it
    current_scenario = my_project.desktop.data_explorer(
    ).primary_scenario.core_scenario.ref
    my_bank = current_scenario.emmebank
    zonesDim = len(current_scenario.zone_numbers)
    zones = current_scenario.zone_numbers
    bank_name = my_project.emmebank.title

    #create an index of trips for this TOD. This prevents iterating over the entire array (all trips).
    tod_index = create_trip_tod_indices(bank_name)

    #Create the HDF5 Container if needed and open it in read/write mode using "r+"

    my_store = h5py.File(hdf_filename, "r+")

    #Read the Matrix File from the Dictionary File and Set Unique Matrix Names
    matrix_dict = text_to_dictionary('demand_matrix_dictionary')
    uniqueMatrices = set(matrix_dict.values())

    #Read the Time of Day File from the Dictionary File and Set Unique TOD List
    #tod_dict = text_to_dictionary('time_of_day')
    #uniqueTOD = set(tod_dict.values())
    #uniqueTOD = list(uniqueTOD)

    #Read the Mode File from the Dictionary File
    mode_dict = text_to_dictionary('modes')

    #Stores in the HDF5 Container to read or write to
    daysim_set = my_store["Daysim"]["Trip"]

    #Store arrays from Daysim/Trips Group into numpy arrays, indexed by TOD.
    #This means that only trip info for the current Time Period will be included in each array.
    otaz = np.asarray(daysim_set["otaz"])
    otaz = otaz[tod_index]

    dtaz = np.asarray(daysim_set["dtaz"])
    dtaz = dtaz[tod_index]

    mode = np.asarray(daysim_set["mode"])
    mode = mode[tod_index]

    vot = np.asarray(daysim_set["vot"])
    vot = vot[tod_index]

    deptm = np.asarray(daysim_set["deptm"])
    deptm = deptm[tod_index]

    dorp = np.asarray(daysim_set["dorp"])
    dorp = dorp[tod_index]

    toll_path = np.asarray(daysim_set["pathtype"])
    toll_path = toll_path[tod_index]

    my_store.close

    #create & store in-memory numpy matrices in a dictionary. Key is matrix name, value is the matrix
    demand_matrices = {}
    for matrices in uniqueMatrices:
        demand_matrices.update(
            {matrices: np.zeros((zonesDim, zonesDim), np.uint16)})

    #Start going through each trip & assign it to the correct Matrix. Using Otaz, but array length should be same for all
    #The correct matrix is determined using a tuple that consists of (mode, vot, toll path). This tuple is the key in matrix_dict.

    for x in range(0, len(otaz)):
        #Start building the tuple key, 3 VOT of categories...
        if vot[x] < 2.50: vot[x] = 1
        elif vot[x] < 8.00: vot[x] = 2
        else: vot[x] = 3

        #get the matrix name from matrix_dict
        mat_name = matrix_dict[(int(mode[x]), int(vot[x]), int(toll_path[x]))]

        #Only want drivers, transit trips.
        if dorp[x] <= 1:

            #account for zero based numpy matrices
            myOtaz = otaz[x] - 1
            myDtaz = dtaz[x] - 1

            #add the trip
            demand_matrices[mat_name][
                int(myOtaz),
                int(myDtaz)] = demand_matrices[mat_name][int(myOtaz),
                                                         int(myDtaz)] + 1

        #all in-memory numpy matrices populated, now write out to emme
    for mat_name in uniqueMatrices:
        print mat_name
        matrix_id = my_bank.matrix(str(mat_name)).id
        np_array = demand_matrices[mat_name]
        emme_matrix = ematrix.MatrixData(indices=[zones, zones], type='f')
        emme_matrix.raw_data = [_array.array('f', row) for row in np_array]
        my_bank.matrix(matrix_id).set_data(emme_matrix, current_scenario)

    end_time = time.time()

    print 'It took', round((end_time - start_time) / 60,
                           2), 'minutes to export all skims to the HDF5 File.'