Changeset 3344

Show
Ignore:
Timestamp:
05/14/08 12:54:05 (2 months ago)
Author:
lmwang
Message:

(lmwang)created abstract logsum variable

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/psrc_parcel/person_x_job/logsum_hbw_am_from_home_to_work.py

    r3288 r3344  
    1313# 
    1414 
    15 from psrc.abstract_variables.abstract_travel_time_variable import abstract_travel_time_variable 
     15from psrc.abstract_variables.abstract_logsum_variable import abstract_logsum_variable 
    1616from opus_core.misc import unique_values 
    1717from numpy import where, repeat, ones, float32, resize, array 
     
    1919from opus_core.logger import logger 
    2020 
    21 class logsum_hbw_am_from_home_to_work(abstract_travel_time_variable): 
     21class logsum_hbw_am_from_home_to_work(abstract_logsum_variable): 
    2222    """logsum_hbw_am_from_home_to_work 
    2323       logsum breaks by income: 
     
    2727           More than $75K. 
    2828    """ 
    29     default_value = -1 
     29     
     30    default_value = -9 
    3031    agent_zone_id = "zone_id = person.disaggregate(urbansim_parcel.household.zone_id)" 
     32    agent_category_attribute = "logsum_income_break =( person.disaggregate(psrc.household.logsum_income_break)).astype(int32)" 
    3133    location_zone_id = "urbansim_parcel.job.zone_id" 
     34    travel_data_attributes = {1: "travel_data.logsum_hbw_am_income_1",  
     35                              2: "travel_data.logsum_hbw_am_income_2",  
     36                              3: "travel_data.logsum_hbw_am_income_3",  
     37                              4: "travel_data.logsum_hbw_am_income_4" } 
    3238    direction_from_home = True 
    33      
    34     def dependencies(self): 
    35         return [ self.agent_zone_id, self.location_zone_id, 
    36                  "income_breaks = 1 * (household.income < 25000) +" + \ 
    37                                 " 2 * numpy.logical_and(household.income >= 25000, household.income < 45000) +" + \ 
    38                                 " 3 * numpy.logical_and(household.income >= 45000, household.income < 75000) +" + \ 
    39                                 " 4 * (household.income >= 75000)", 
    40                  "income_breaks = ( person.disaggregate(household.income_breaks) ).astype(int32)"] 
    41  
    42     def compute(self, dataset_pool): 
    43         interaction_dataset = self.get_dataset() 
    44         income_breaks = interaction_dataset.get_dataset(1).get_attribute_by_index("income_breaks", 
    45                                                                                interaction_dataset.get_2d_index_of_dataset1()) 
    46         unique_income_breaks = unique_values(income_breaks.ravel()) 
    47         self.add_dependencies(["travel_data.logsum_hbw_am_income_" + str(i) for i in unique_income_breaks]) 
    48          
    49         travel_data = dataset_pool.get_dataset('travel_data') 
    50         var1 = interaction_dataset.get_dataset(1).get_attribute_by_index(self.agent_zone_id, 
    51                                                                          interaction_dataset.get_2d_index_of_dataset1()) 
    52         var2 = interaction_dataset.get_2d_dataset_attribute(self.location_zone_id) 
    53         if self.direction_from_home: 
    54             home_zone = var1 
    55             work_zone = var2 
    56         else: 
    57             home_zone = var2 
    58             work_zone = var1 
    59         times = resize(array([self.default_value], dtype=float32), home_zone.shape) 
    60         positions = ones(home_zone.shape, dtype="int32") 
    61         #create indices for 2d array of (origin, destination) 
    62         ij = map(lambda x, y: (x, y), where(positions)[0], where(positions)[1]) 
    63         for a in ij: 
    64             i, j = a 
    65             try: 
    66                 times[i,j] = travel_data.get_attribute_by_id("logsum_hbw_am_income_" + str(income_breaks[i, j]) , (home_zone[i,j], work_zone[i,j])) 
    67             except: 
    68                 logger.log_warning("zone pairs (%s, %s) is not in zoneset; value set to %s." % (home_zone[i,j], work_zone[i,j], self.default_value)) 
    69  
    70         return times 
    71      
    7239 
    7340from opus_core.tests import opus_unittest