Example #1
0
 def test_agent_times_choice(self):
     expression = 'agent_x_choice.agent_times_choice(attr)'
     storage = StorageFactory().get_storage('dict_storage')
     storage.write_table(table_name='agents', 
         table_data={'id': array([1, 2, 3, 4, 5]), 'attr_2': array([3,   2,   4,   10, 20]), 
                                                   'attr_3': array([10, 100, 1000, 500, 0]),
                                                   'attr_4': array([100, 500, 0, 20, -30])
                     }
         )
     storage.write_table(table_name='choices', 
         table_data={'id': array([1, 2, 3, 4])}
         )
     agents = Dataset(in_storage=storage, in_table_name='agents', dataset_name='agent', id_name='id')
     choices = Dataset(in_storage=storage, in_table_name='choices', dataset_name='choice', id_name='id')
     ids = InteractionDataset(dataset1=agents, dataset2=choices, index1=array([0,1,3,4]), index2=array([1,2,3])) 
     result = ids.compute_variables(expression)
     should_be = array([[3, 10, 100], [2,100,500], [10,500, 20], [20, 0, -30]])
     self.assertEqual(ma.allequal(result, should_be), True)
     
     agents.touch_attribute('attr_2') # in order to recompute the expression
     choices.add_primary_attribute(name='name', data=array(['bus', 'car', 'tran', 'walk']))
     agents.add_primary_attribute(name='attr_tran', data=array([100, 1000, 10000, 5000,10]))
     result = ids.compute_variables(expression)
     should_be = array([[3, 100, 100], [2,1000,500], [10,5000, 20], [20, 10, -30]])
     self.assertEqual(ma.allequal(result, should_be), True)
 def test_versioning_with_disaggregate(self):
     storage = StorageFactory().get_storage('dict_storage')
     storage.write_table(table_name='zones',
         table_data={
             'id':array([1,2,3,4]),
             'id2':array([1,2,1,2])
             }
         )
     storage.write_table(table_name='faz',
         table_data={
             'my_variable':array([4,8]), 
             'id2':array([1,2])
             }
         )
     ds = Dataset(in_storage=storage, in_table_name='zones', id_name="id", dataset_name="myzone")
     ds2 = Dataset(in_storage=storage, in_table_name='faz', id_name="id2", dataset_name="myfaz")
     dataset_pool = DatasetPool()
     dataset_pool._add_dataset('myzone', ds)
     dataset_pool._add_dataset('myfaz', ds2)
     var = "my_var = myzone.disaggregate(10.0*myfaz.my_variable)"
     ds.modify_attribute("id2", array([2,1,2,1])) # should have version 1
     ds.compute_variables([var], dataset_pool=dataset_pool)
     self.assert_(ds.get_version("my_var")==0)
     ds.compute_variables([var], dataset_pool=dataset_pool)
     self.assert_(ds.get_version("my_var")==0) # version should stay the same, i.e. it should not recompute
     ds.touch_attribute("id2") # has version 2
     ds.compute_variables([var], dataset_pool=dataset_pool)
     self.assert_(ds.get_version("my_var")==1) # version should be 1, i.e. it should recompute when id changes
    def test_agent_times_choice(self):
        expression = "agent_x_choice.agent_times_choice(attr)"
        storage = StorageFactory().get_storage("dict_storage")
        storage.write_table(
            table_name="agents",
            table_data={
                "id": array([1, 2, 3, 4, 5]),
                "attr_2": array([3, 2, 4, 10, 20]),
                "attr_3": array([10, 100, 1000, 500, 0]),
                "attr_4": array([100, 500, 0, 20, -30]),
            },
        )
        storage.write_table(table_name="choices", table_data={"id": array([1, 2, 3, 4])})
        agents = Dataset(in_storage=storage, in_table_name="agents", dataset_name="agent", id_name="id")
        choices = Dataset(in_storage=storage, in_table_name="choices", dataset_name="choice", id_name="id")
        ids = InteractionDataset(dataset1=agents, dataset2=choices, index1=array([0, 1, 3, 4]), index2=array([1, 2, 3]))
        result = ids.compute_variables(expression)
        should_be = array([[3, 10, 100], [2, 100, 500], [10, 500, 20], [20, 0, -30]])
        self.assertEqual(ma.allequal(result, should_be), True)

        agents.touch_attribute("attr_2")  # in order to recompute the expression
        choices.add_primary_attribute(name="name", data=array(["bus", "car", "tran", "walk"]))
        agents.add_primary_attribute(name="attr_tran", data=array([100, 1000, 10000, 5000, 10]))
        result = ids.compute_variables(expression)
        should_be = array([[3, 100, 100], [2, 1000, 500], [10, 5000, 20], [20, 10, -30]])
        self.assertEqual(ma.allequal(result, should_be), True)
 def test_versioning_with_aggregate(self):
     storage = StorageFactory().get_storage('dict_storage')
     storage.write_table(table_name='households',
                         table_data={
                             'my_variable': array([4, 8, 2, 1, 40, 23, 78]),
                             'id0': arange(7) + 1,
                             'id1': array([1, 3, 1, 2, 3, 2, 1])
                         })
     storage.write_table(table_name='fazes',
                         table_data={
                             'id1': array([1, 2, 3]),
                             'id2': array([1, 2, 1])
                         })
     storage.write_table(table_name='fazdistr',
                         table_data={'id2': array([1, 2])})
     ds0 = Dataset(in_storage=storage,
                   in_table_name='households',
                   id_name="id0",
                   dataset_name="myhousehold")
     ds1 = Dataset(in_storage=storage,
                   in_table_name='fazes',
                   id_name="id1",
                   dataset_name="myfaz")
     ds2 = Dataset(in_storage=storage,
                   in_table_name='fazdistr',
                   id_name="id2",
                   dataset_name="myfazdistr")
     dataset_pool = DatasetPool()
     dataset_pool._add_dataset('myhousehold', ds0)
     dataset_pool._add_dataset('myfaz', ds1)
     dataset_pool._add_dataset('myfazdistr', ds2)
     ds0.modify_attribute("id1", array([1, 3, 1, 2, 3, 2,
                                        1]))  # has version 1
     variable = 'my_var = myfazdistr.aggregate(10.0*myhousehold.my_variable, intermediates=[myfaz])'
     ds2.compute_variables([variable], dataset_pool=dataset_pool)
     self.assert_(ds2.get_version("my_var") == 0)
     ds2.compute_variables([variable], dataset_pool=dataset_pool)
     self.assert_(
         ds2.get_version("my_var") ==
         0)  # version should stay the same, i.e. it should not recompute
     ds0.touch_attribute("id1")  # has version 2
     ds2.compute_variables([variable], dataset_pool=dataset_pool)
     self.assert_(
         ds2.get_version("my_var") ==
         1)  # version should be 1, i.e. it should recompute when id changes
     ds1.touch_attribute("id2")  # has version 1
     ds2.compute_variables([variable], dataset_pool=dataset_pool)
     self.assert_(
         ds2.get_version("my_var") ==
         2)  # version should be 2, i.e. it should recompute when id changes
 def test_versioning_with_aggregate(self):
     storage = StorageFactory().get_storage('dict_storage')
     storage.write_table(table_name='households',
         table_data={
             'my_variable':array([4,8,2,1,40,23,78]), 
             'id0':arange(7)+1,
             'id1':array([1,3,1,2,3,2,1])
             }
         )
     storage.write_table(table_name='fazes',
         table_data={
             'id1':array([1,2,3]), 
             'id2':array([1,2,1])
             }
         )
     storage.write_table(table_name='fazdistr',
         table_data={
             'id2':array([1,2])
             }
         )
     ds0 = Dataset(in_storage=storage, in_table_name='households', id_name="id0", dataset_name="myhousehold")
     ds1 = Dataset(in_storage=storage, in_table_name='fazes', id_name="id1", dataset_name="myfaz")             
     ds2 = Dataset(in_storage=storage, in_table_name='fazdistr', id_name="id2", dataset_name="myfazdistr")
     dataset_pool = DatasetPool()
     dataset_pool._add_dataset('myhousehold', ds0)
     dataset_pool._add_dataset('myfaz',ds1)
     dataset_pool._add_dataset('myfazdistr',ds2)
     ds0.modify_attribute("id1", array([1,3,1,2,3,2,1])) # has version 1
     variable = 'my_var = myfazdistr.aggregate(10.0*myhousehold.my_variable, intermediates=[myfaz])'
     ds2.compute_variables([variable], dataset_pool=dataset_pool)
     self.assert_(ds2.get_version("my_var")==0)
     ds2.compute_variables([variable], dataset_pool=dataset_pool)
     self.assert_(ds2.get_version("my_var")==0) # version should stay the same, i.e. it should not recompute
     ds0.touch_attribute("id1") # has version 2
     ds2.compute_variables([variable], dataset_pool=dataset_pool)
     self.assert_(ds2.get_version("my_var")==1) # version should be 1, i.e. it should recompute when id changes
     ds1.touch_attribute("id2") # has version 1
     ds2.compute_variables([variable], dataset_pool=dataset_pool)
     self.assert_(ds2.get_version("my_var")==2) # version should be 2, i.e. it should recompute when id changes
 def test_versioning_with_disaggregate(self):
     storage = StorageFactory().get_storage('dict_storage')
     storage.write_table(table_name='zones',
                         table_data={
                             'id': array([1, 2, 3, 4]),
                             'id2': array([1, 2, 1, 2])
                         })
     storage.write_table(table_name='faz',
                         table_data={
                             'my_variable': array([4, 8]),
                             'id2': array([1, 2])
                         })
     ds = Dataset(in_storage=storage,
                  in_table_name='zones',
                  id_name="id",
                  dataset_name="myzone")
     ds2 = Dataset(in_storage=storage,
                   in_table_name='faz',
                   id_name="id2",
                   dataset_name="myfaz")
     dataset_pool = DatasetPool()
     dataset_pool._add_dataset('myzone', ds)
     dataset_pool._add_dataset('myfaz', ds2)
     var = "my_var = myzone.disaggregate(10.0*myfaz.my_variable)"
     ds.modify_attribute("id2", array([2, 1, 2,
                                       1]))  # should have version 1
     ds.compute_variables([var], dataset_pool=dataset_pool)
     self.assert_(ds.get_version("my_var") == 0)
     ds.compute_variables([var], dataset_pool=dataset_pool)
     self.assert_(
         ds.get_version("my_var") ==
         0)  # version should stay the same, i.e. it should not recompute
     ds.touch_attribute("id2")  # has version 2
     ds.compute_variables([var], dataset_pool=dataset_pool)
     self.assert_(
         ds.get_version("my_var") ==
         1)  # version should be 1, i.e. it should recompute when id changes