# # UrbanSim software. Copyright (C) 1998-2007 University of Washington # # You can redistribute this program and/or modify it under the terms of the # GNU General Public License as published by the Free Software Foundation # (http://www.gnu.org/copyleft/gpl.html). # # This program is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or # FITNESS FOR A PARTICULAR PURPOSE. See the file LICENSE.html for copyright # and licensing information, and the file ACKNOWLEDGMENTS.html for funding and # other acknowledgments. # from opus_core.variables.variable import Variable from variable_functions import my_attribute_label class has_DDD_workers_x_has_children(Variable): """Does this household have DDD workers and children.""" _return_type="bool8" children = "children" workers = "workers" def __init__(self, number): Variable.__init__(self) self.tnumber = number def dependencies(self): return [my_attribute_label(self.children), my_attribute_label(self.workers)] """For each household...""" def compute(self, dataset_pool): return self.get_dataset().get_attribute(self.workers)==self.tnumber and self.get_dataset().get_attribute(self.children) > 0 from opus_core.tests import opus_unittest from urbansim.variable_test_toolbox import VariableTestToolbox from numpy import array from numpy import ma class Tests(opus_unittest.OpusTestCase): variable_name = "urbansim.household.has_less_cars_than_workers" def test_my_inputs( self ): cars = array([0, 1, 2, 1]) workers = array([1, 0, 2, 2]) values = VariableTestToolbox().compute_variable( self.variable_name, {"household":{ "cars":cars, "workers":workers}}, dataset = "household" ) should_be = array( [1,0,0,1] ) self.assertEqual( ma.allclose( values, should_be, rtol=1e-7 ), True, msg = "Error in " + self.variable_name ) if __name__=='__main__': opus_unittest.main()