Ejemplo n.º 1
0
    def pandas_to_matrix(series_or_dataframe):
        """Converts a Series or DataFrame back to a MatrixData object

        Args:
            series_or_dataframe: Series or DataFrame

        Returns:
            MatrixData object
        """
        data_values = series_or_dataframe.to_numpy(
        ) if _USE_PD_TO_NUMPY else series_or_dataframe.values
        if isinstance(series_or_dataframe, pd.Series):
            indices = series_or_dataframe.index.tolist()
            md = MatrixData(indices)
            md.from_numpy(data_values)
            return md
        elif isinstance(series_or_dataframe, pd.DataFrame):
            indices = series_or_dataframe.index.tolist(
            ), series_or_dataframe.columns.tolist()
            md = MatrixData(indices)
            md.from_numpy(data_values)
            return md
        else:
            raise TypeError("Expected a Series or DataFrame, got %s" %
                            type(series_or_dataframe))
Ejemplo n.º 2
0
    def _Execute(self):
        with _m.logbook_trace(name="%s v%s" %(self.__class__.__name__, self.version), \
                              attributes= self._GetAtts()):
            matrix = _util.initializeMatrix(self.MatrixId)
            if self.MatrixDescription:
                matrix.description = self.MatrixDescription

            data = _MatrixData.load(self.ImportFile)

            origins, destinations = data.indices
            origins = set(origins)
            destinations = set(destinations)
            if origins ^ destinations:
                raise Exception(
                    "Asymmetrical matrix detected. Matrix must be square.")

            if _util.databankHasDifferentZones(_bank):

                zones = set(self.Scenario.zone_numbers)
                if zones ^ origins:

                    with _m.logbook_trace(
                            "Zones in matrix file but not in scenario"):
                        for index in origins - zones:
                            _m.logbook_write(index)
                    with _m.logbook_trace("Zones in scenario but not in file"):
                        for index in zones - origins:
                            _m.logbook_write(index)

                    raise Exception(
                        "Matrix zones not compatible with scenario %s. Check logbook for details."
                        % self.Scenario)

                matrix.set_data(data, scenario_id=self.Scenario.id)
            else:
                sc = _bank.scenarios()[0]
                zones = set(sc.zone_numbers)
                if zones ^ origins:

                    with _m.logbook_trace(
                            "Zones in matrix file but not in scenario"):
                        for index in origins - zones:
                            _m.logbook_write(index)
                    with _m.logbook_trace("Zones in scenario but not in file"):
                        for index in zones - origins:
                            _m.logbook_write(index)

                    raise Exception(
                        "Matrix zones not compatible with emmebank zone system. Check Logbook for details."
                    )

                matrix.set_data(data)

            self.TRACKER.completeTask()
Ejemplo n.º 3
0
 def _Execute(self):
     with _m.logbook_trace(name="%s v%s" %(self.__class__.__name__, self.version), \
                           attributes= self._GetAtts()):
         matrix = _util.initializeMatrix(self.MatrixId)
         if self.MatrixDescription:
             matrix.description = self.MatrixDescription
         
         data = _MatrixData.load(self.ImportFile)
         
         origins, destinations = data.indices
         origins = set(origins)
         destinations = set(destinations)
         if origins ^ destinations:
             raise Exception("Asymmetrical matrix detected. Matrix must be square.")
         
         if _util.databankHasDifferentZones(_bank):
             
             zones = set(self.Scenario.zone_numbers)
             if zones ^ origins:
                 
                 with _m.logbook_trace("Zones in matrix file but not in scenario"):
                     for index in origins - zones: _m.logbook_write(index)
                 with _m.logbook_trace("Zones in scenario but not in file"):
                     for index in zones - origins: _m.logbook_write(index)
                 
                 raise Exception("Matrix zones not compatible with scenario %s. Check logbook for details." %self.Scenario)
             
             matrix.set_data(data, scenario_id= self.Scenario.id)
         else:
             sc = _bank.scenarios()[0]
             zones = set(sc.zone_numbers)
             if zones ^ origins:
                 
                 with _m.logbook_trace("Zones in matrix file but not in scenario"):
                     for index in origins - zones: _m.logbook_write(index)
                 with _m.logbook_trace("Zones in scenario but not in file"):
                     for index in zones - origins: _m.logbook_write(index)
                 
                 raise Exception("Matrix zones not compatible with emmebank zone system. Check Logbook for details.")
             
             matrix.set_data(data)
         
         self.TRACKER.completeTask()
Ejemplo n.º 4
0
    def pandas_to_matrix(series_or_dataframe):
        '''
        Converts a Series or DataFrame back to a MatrixData object

        Args:
            series_or_dataframe: Series or DataFrame

        Returns: MatrixData object.

        '''
        if isinstance(series_or_dataframe, pd.Series):
            indices = list(series_or_dataframe.index.values)
            md = MatrixData(indices)
            md.from_numpy(series_or_dataframe.values)
            return md
        elif isinstance(series_or_dataframe, pd.DataFrame):
            indices = list(series_or_dataframe.index.values), list(
                series_or_dataframe.columns.values)
            md = MatrixData(indices)
            md.from_numpy(series_or_dataframe.values)
            return md
        else:
            raise TypeError("Expected a Series or DataFrame, got %s" %
                            type(series_or_dataframe))
Ejemplo n.º 5
0
    def pandas_to_matrix(series_or_dataframe):
        '''
        Converts a Series or DataFrame back to a MatrixData object

        Args:
            series_or_dataframe: Series or DataFrame

        Returns: MatrixData object.

        '''
        if isinstance(series_or_dataframe, pd.Series):
            indices = list(series_or_dataframe.index.values)
            md = MatrixData(indices)
            md.from_numpy(series_or_dataframe.values)
            return md
        elif isinstance(series_or_dataframe, pd.DataFrame):
            indices = list(series_or_dataframe.index.values), list(series_or_dataframe.columns.values)
            md = MatrixData(indices)
            md.from_numpy(series_or_dataframe.values)
            return md
        else: raise TypeError("Expected a Series or DataFrame, got %s" %type(series_or_dataframe))
Ejemplo n.º 6
0
def pandas_to_matrix(series_or_dataframe, mtx_out=None, scenario_id=None):
    """
    Converts a Series or DataFrame to an Emme Matrix; either as a MatrixData instance or saved to a Matrix instance.

    Args:
        series_or_dataframe: The Series or DataFrame to convert.
        mtx_out: A Matrix instance (usually retrieved from Emmebank.matrix()) or None. If provided, the resulting matrix
            data will be saved to the Emmebank
        scenario_id: Optional scenario ID to pass to Matrix.set_data()

    Returns:
        MatrixData if `mtx_out` is None; None otherwise.

    """
    if isinstance(series_or_dataframe, pd.Series):
        indices = list(series_or_dataframe.index.values)
        md = MatrixData(indices)

        array = series_or_dataframe.values
        if array.flags.f_contiguous:
            array = np.ascontiguousarray(array)

        md.from_numpy(array)
    elif isinstance(series_or_dataframe, pd.DataFrame):
        indices = list(series_or_dataframe.index.values), list(series_or_dataframe.columns.values)
        md = MatrixData(indices)

        array = series_or_dataframe.values
        if array.flags.f_contiguous:
            array = np.ascontiguousarray(array)

        md.from_numpy(array)
    else: raise TypeError("Expected a Series or DataFrame, got %s" % type(series_or_dataframe))

    if mtx_out is not None:
        mtx_out.set_data(md, scenario_id=scenario_id)
        return
    return md
Ejemplo n.º 7
0
    def _Execute(self):
        with _m.logbook_trace(name="%s v%s" %(self.__class__.__name__, self.version), \
                              attributes= self._GetAtts()):

            if self.MatrixId is None:
                matrix = _util.initializeMatrix(
                    id=self.NewMatrixID,
                    name=self.NewMatrixName,
                    description=self.NewMatrixDescription,
                    matrix_type=self.NewMatrixType)
            else:
                matrix = _util.initializeMatrix(self.MatrixId)
                if self.MatrixDescription:
                    matrix.description = self.MatrixDescription

            if str(self.ImportFile)[-2:] == "gz":
                new_file = 'matrix.mtx'
                with gzip.open(self.ImportFile,
                               'rb') as zip_file, open(new_file,
                                                       'wb') as non_zip_file:
                    shutil.copyfileobj(zip_file, non_zip_file)
                data = _MatrixData.load(new_file)
                os.remove(new_file)
            else:
                data = _MatrixData.load(self.ImportFile)

            self.MatrixType = matrix.type
            # 2D matrix
            if self.MatrixType == "mf":
                origins, destinations = data.indices
                origins = set(origins)
                destinations = set(destinations)
                if origins ^ destinations:
                    raise Exception(
                        "Asymmetrical matrix detected. Matrix must be square.")
            # 1D matrix
            else:
                origins = data.indices[0]
                origins = set(origins)

            if _util.databankHasDifferentZones(_bank):

                zones = set(self.Scenario.zone_numbers)
                if zones ^ origins:

                    with _m.logbook_trace(
                            "Zones in matrix file but not in scenario"):
                        for index in origins - zones:
                            _m.logbook_write(index)
                    with _m.logbook_trace("Zones in scenario but not in file"):
                        for index in zones - origins:
                            _m.logbook_write(index)

                    raise Exception(
                        "Matrix zones not compatible with scenario %s. Check logbook for details."
                        % self.Scenario)

                matrix.set_data(data, scenario_id=self.Scenario.id)
            else:
                sc = _bank.scenarios()[0]
                zones = set(sc.zone_numbers)
                if zones ^ origins:

                    with _m.logbook_trace(
                            "Zones in matrix file but not in scenario"):
                        for index in origins - zones:
                            _m.logbook_write(index)
                    with _m.logbook_trace("Zones in scenario but not in file"):
                        for index in zones - origins:
                            _m.logbook_write(index)

                    raise Exception(
                        "Matrix zones not compatible with emmebank zone system. Check Logbook for details."
                    )

                matrix.set_data(data)

            self.TRACKER.completeTask()