next up previous index
Next: Number of Agents Up: Aggregation and Disaggregation Previous: Aggregation   Index

Disaggregation

Disaggregation is done analogously. The disaggregate method takes information from a coarse set of entities and allocates it to a finer set of entities, in the manner of a one-to-many relationship. By default, the function for allocating data is to simply replicate the data on the parent entity for each inheriting entity. The method takes one required argument, an attribute/variable name, and one optional argument, a list of dataset names. Here we add an attribute ``is_cbd'' to the neighborhood set and distribute it across gridcells:

>>> neighborhoods.add_primary_attribute(name="is_cbd", data=[0,0,1])
>>> disaggr_var = \
       "is_cbd = gridcell.disaggregate(neighborhood.is_cbd, intermediates=[zone])"
>>> locations.compute_variables(disaggr_var, dataset_pool=dataset_pool)
is_cbd = gridcell.disaggregate(neighborhood.is_cbd, intermediates=[zone])
    zone.disaggregate(neighborhood.is_cbd).......................0.0 sec
is_cbd = gridcell.disaggregate(neighborhood.is_cbd, intermediates=[zone]):
                                                           completed...0.0 sec
array([0, 0, 1, 1, 1, 1, 0, 0, 0])

Note that since we used the dataset-qualified name for ``is_cbd'' in the disaggregate method, the attribute must be a primary attribute of neighborhoods. The aggregate and disaggregate methods both must have the dataset name of the dataset for which they are computed before the method name, e.g. gridcell.disaggregate.

To aggregate over all members of one dataset, one can use the built-in method aggregate_all. It must be used with a dataset that has one element which is the case of the opus_core dataset AlldataDataset implemented in the directory datasets. For example, the total capacity for all gridcells can be determined by:

>>> from opus_core.datasets.alldata_dataset import AlldataDataset
>>> alldata = AlldataDataset()
>>> alldata.compute_variables(
        "total_capacity = alldata.aggregate_all(gridcell.capacity, function=sum)",
        dataset_pool=dataset_pool)
total_capacity = alldata.aggregate_all(gridcell.capacity, function=sum)....0.0 sec
array(15.0)
In addition to sum, the aggregate_all class accepts all functions that are accepted by the aggregate class; the default is sum.

If the attribute being aggregated or disaggregated is a simple variable, it should be either dataset-qualified or fully-qualified, i.e. always including the dataset name and optionally including the package name. The attribute being aggregated can also be an expression. (In this case, behind the scenes the system generates a new variable for that expression, and then uses the new variable in the aggregation or disaggregation operations. However, this isn't visible to the user.) The result of an aggregation or disaggregation can also be used in more complex expressions, e.g. ln(2*aggregate(gridcell.population)).


next up previous index
Next: Number of Agents Up: Aggregation and Disaggregation Previous: Aggregation   Index
info (at) urbansim.org