Siva,
You used a wrong expression for doing 'logical and' on arrays (use
logical_and instead of just 'and'). Furthermore, your unit tests refer
to different variables. I'm attaching versions of the two files that
work (at least the unit tests). Make sure, the first variable lives in
the subdirectory 'zone', the other one in the subdirectory 'household'.
I'm not sure if you're referring to failing when running a simulation or
when running a unit test. But in case the zone_id error comes during a
simulation, make sure that both the zones table and households table
have the attribute 'zone_id' in lowercase (check also your cache, if
it's the case).
BTW, if you are using an Opus version that supports our expression
language, you could specify this variable as an expression directly in
your model specification (without having to go through python modules)
as follows:
'has_3_workers_and_children =
zone.aggregate(numpy.logical_and(household.workers == 3,
household.children>0))'
Hana
sivakarthik wrote:
> Hi All,
>
> I was trying to create an indicator which gives me the
> number of households in a zone with one worker and at least one child. I
> am attaching 2 files which is the best we could do.
>
>
>
> has_DDD_workers_x_has_children.py file should return '1' if the
> household has children and DDD workers, and zero otherwise but this file
> is giving the same results even after the DDD value has been changed.
>
>
>
> number_of_DDD_workers_households_has_children.py file should sum the
> results of the first file over each zone but it is not working and
> giving an error saying it cannot find zone_ID but other similar files
> are able to find Zone_ID.
>
>
>
> Can anyone help me with this issue
>
>
> Thanks & Regards,
>
> Siva
>
>
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Users mailing list
> Users_at_urbansim.org
> http://www.urbansim.org/mailman/listinfo/users
#
# 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 urbansim.functions import attribute_label
class number_of_DDD_workers_households_has_children(Variable):
"""Number of households in a zone with given number of workers and has children"""
_return_type="int32"
def __init__(self,number):
Variable.__init__(self)
self.has_workers = "has_%s_workers_x_has_children" % number
def dependencies(self):
return [attribute_label("household", self.has_workers),
attribute_label("household", "zone_id")]
def compute(self, dataset_pool):
households = dataset_pool.get_dataset('household')
return self.get_dataset().sum_dataset_over_ids(households, self.has_workers)
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.zone.number_of_3_workers_households_has_children"
def test_full_tree(self):
zone_id = array([1, 2, 3, 4])
hh_zone_id = array([1, 2, 3, 4, 2, 2])
children = array([2, 1, 0, 4, 2, 3])
workers = array([3, 2, 2, 1, 3, 2])
values = VariableTestToolbox().compute_variable(self.variable_name,
{"zone":{
"zone_id": zone_id},
"household":{
"zone_id":hh_zone_id,
"children":children,
"workers": workers} },
dataset = "zone")
should_be = array([1,1,0,0])
self.assertEqual(ma.allclose(values, should_be, rtol=1e-20), True, msg = "Error in " + self.variable_name)
if __name__=='__main__':
opus_unittest.main()
#
# 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 numpy import logical_and
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 logical_and(self.get_dataset().get_attribute(self.workers)==self.tnumber, 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_2_workers_x_has_children"
def test_my_inputs( self ):
children = array([0, 1, 2, 1,0])
workers = array([1, 0, 2, 2, 2])
values = VariableTestToolbox().compute_variable( self.variable_name,
{"household":{
"children":children,
"workers":workers}},
dataset = "household" )
should_be = array( [0,0,1,1, 0] )
self.assertEqual( ma.allclose( values, should_be, rtol=1e-7 ), True, msg = "Error in " + self.variable_name )
if __name__=='__main__':
opus_unittest.main()
Received on Fri May 02 2008 - 10:43:19 PDT
This archive was generated by hypermail 2.2.0 : Fri May 02 2008 - 10:43:20 PDT