next up previous index
Next: Expression Names and Aliasing Up: Opus Variables Previous: Versioning   Index


Using Arguments in Variable Names

Opus offers the possibility of including a number or a character string into variable name which is then passed to the variable constructor as an argument. The module/class name of such variable corresponds to a pattern in which a number is replaced by `DDD' and a string is replaced by `SSS'. For example, a similar variable to is_in_wetland in Section 23.3.1 can be implemented in a way that the threshold is directly included in the variable name, such as
is_in_wetland_if_threshold_is_80. Here, we give an example of variable of the type is_near_location_if_threshold_is_number. As location we can use e.g. `highway', `arterial', or `cbd'. The class name is is_near_SSS_if_threshold_is_DDD and must have a constructor implemented which takes a string and a number as arguments:

from opus_core.variables.variable import Variable

class is_near_SSS_if_threshold_is_DDD(Variable):
    def __init__(self, location, number):
        self.location = location
        self.number = number
        Variable.__init__(self)

    def dependencies(self):
        return ["gridcell.distance_to_" + self.location]

    def compute(self, dataset_pool):
        distance_to_location = self.get_dataset().get_attribute(
                                        "distance_to_" + self.location)
        return distance_to_location < self.number
We can then invoke the computation for the `cbd' location and different thresholds by changing the variable name:
>>> res = locations.compute_variables(
      map(lambda threshold:
       "urbansim.gridcell.is_near_cbd_if_threshold_is_%s" % threshold, [2,4,7]))
urbansim.gridcell.is_near_SSS_if_threshold_is_DDD...................0.0 sec
urbansim.gridcell.is_near_SSS_if_threshold_is_DDD...................0.0 sec
urbansim.gridcell.is_near_SSS_if_threshold_is_DDD...................0.0 sec

>>> locations.get_attribute("is_near_cbd_if_threshold_is_2")
array([False, False, False,  True, False,  True, False, False, False], dtype=bool)
>>> locations.get_attribute("is_near_cbd_if_threshold_is_4")
array([False, False, False,  True, False,  True, False, False,  True], dtype=bool)
>>> locations.get_attribute("is_near_cbd_if_threshold_is_7")
array([ True, False,  True,  True, False,  True, False, False,  True], dtype=bool)

If the dataset locations would have an attribute ``distance_to_highway'', we could use the same variable implementation for variables is_near_highway_if_threshold_is_....

The arguments of the constructor are passed in the same number and order as they appear in the variable name. For example, if the name would contain only one pattern, say DDD, the constructor would expect only one argument, namely an integer. The method dependencies() is called from Variable.__init__(), therefore any class attributes used in dependencies() (such as self.location in this case) must be set before the call of
Variable.__init__().

Note: Arguments for variable names will probably be replaced with a more standard syntax in the future, for example is_near(feature='highway', threshhold=2). However, we don't have a specific date yet for this change. We will try to preserve backward compatibility.


next up previous index
Next: Expression Names and Aliasing Up: Opus Variables Previous: Versioning   Index
info (at) urbansim.org