Misc. Utilities¶
API¶
Utilities used within urbansim that don’t yet have a better home.
-
urbansim.utils.misc.
charts_dir
()[source]¶ Return the directory for the chart configuration files (used by the website).
-
urbansim.utils.misc.
column_list
(tables, columns)[source]¶ Take a list of tables and a list of column names and return the columns that are present in the tables.
- Parameters
- tablessequence of _DataFrameWrapper or _TableFuncWrapper
Could also be sequence of modified pandas.DataFrames, the important thing is that they have
.name
and.columns
attributes.- columnssequence of str
The column names of interest.
- Returns
- colslist
Lists of column names available in the tables.
-
urbansim.utils.misc.
column_map
(tables, columns)[source]¶ Take a list of tables and a list of column names and resolve which columns come from which table.
- Parameters
- tablessequence of _DataFrameWrapper or _TableFuncWrapper
Could also be sequence of modified pandas.DataFrames, the important thing is that they have
.name
and.columns
attributes.- columnssequence of str
The column names of interest.
- Returns
- col_mapdict
Maps table names to lists of column names.
-
urbansim.utils.misc.
compute_range
(travel_data, attr, travel_time_attr, dist, agg=<function sum at 0x7ff348e965e0>)[source]¶ Compute a zone-based accessibility query using the urbansim format travel data dataframe.
- Parameters
- travel_datadataframe
The dataframe of urbansim format travel data. Has from_zone_id as first index, to_zone_id as second index, and different impedances between zones as columns.
- attrseries
The attr to aggregate. Should be indexed by zone_id and the values will be aggregated.
- travel_time_attrstring
The column name in travel_data to use as the impedance.
- distfloat
The max distance to aggregate up to
- aggfunction, optional, np.sum by default
The numpy function to use for aggregation
-
urbansim.utils.misc.
config
(fname)[source]¶ Return the config path for the file with the given filename.
-
urbansim.utils.misc.
df64bitto32bit
(tbl)[source]¶ Convert a Pandas dataframe from 64 bit types to 32 bit types to save memory or disk space.
- Parameters
- tblThe dataframe to convert
- Returns
- The converted dataframe
-
urbansim.utils.misc.
edits_dir
()[source]¶ Return the directory for the editable files (used by the website).
-
urbansim.utils.misc.
fidx
(right, left, left_fk=None)[source]¶ Re-indexes a series or data frame (right) to align with another (left) series or data frame via foreign key relationship. The index of the right must be unique.
This is similar to misc.reindex, but allows for data frame re-indexes and supports re-indexing data frames or series with a multi-index.
-
urbansim.utils.misc.
get_run_number
()[source]¶ Get a run number for this execution of the model system, for identifying the output hdf5 files).
- Returns
- The integer number for this run of the model system.
-
urbansim.utils.misc.
maps_dir
()[source]¶ Return the directory for the map configuration files (used by the website).
-
urbansim.utils.misc.
models_dir
()[source]¶ Return the directory for the model configuration files (used by the website).
-
urbansim.utils.misc.
naicsname
(val)[source]¶ This function maps NAICS (job codes) from number to name.
-
urbansim.utils.misc.
numpymat2df
(mat)[source]¶ Sometimes (though not very often) it is useful to convert a numpy matrix which has no column names to a Pandas dataframe for use of the Pandas functions. This method converts a 2D numpy matrix to Pandas dataframe with default column headers.
- Parameters
- matThe numpy matrix
- Returns
- A pandas dataframe with the same data as the input matrix but with columns
- named x0, x1, … x[n-1] for the number of columns.
-
urbansim.utils.misc.
pandasdfsummarytojson
(df, ndigits=3)[source]¶ Convert the result of a
- Parameters
- dfThe result of a Pandas describe operation.
- ndigitsint, optional - The number of significant digits to round to.
- Returns
- A json object which captures the describe. Keys are field names and
- values are dictionaries with all of the indexes returned by the Pandas
- describe.
-
urbansim.utils.misc.
reindex
(series1, series2)[source]¶ This reindexes the first series by the second series. This is an extremely common operation that does not appear to be in Pandas at this time. If anyone knows of an easier way to do this in Pandas, please inform the UrbanSim developers.
The canonical example would be a parcel series which has an index which is parcel_ids and a value which you want to fetch, let’s say it’s land_area. Another dataset, let’s say of buildings has a series which indicate the parcel_ids that the buildings are located on, but which does not have land_area. If you pass parcels.land_area as the first series and buildings.parcel_id as the second series, this function returns a series which is indexed by buildings and has land_area as values and can be added to the buildings dataset.
In short, this is a join on to a different table using a foreign key stored in the current table, but with only one attribute rather than for a full dataset.
This is very similar to the pandas “loc” function or “reindex” function, but neither of those functions return the series indexed on the current table. In both of those cases, the series would be indexed on the foreign table and would require a second step to change the index.
-
urbansim.utils.misc.
reports_dir
()[source]¶ Return the directory for the report configuration files (used by the website).
-
urbansim.utils.misc.
series64bitto32bit
(s)[source]¶ Convert a Pandas series from 64 bit types to 32 bit types to save memory or disk space.
- Parameters
- sThe series to convert
- Returns
- The converted series