Changeset 3330

Show
Ignore:
Timestamp:
05/12/08 12:31:32 (2 months ago)
Author:
hana
Message:

(hana) load specification from dictionary updated for the case when there are equations defined

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/opus_core/equation_specification.py

    r2867 r3330  
    419419        for var_coef in submodel_spec: 
    420420            if isinstance(var_coef, str): 
    421                 #var_coef is actually just variables long names or alias 
     421                #var_coef is just variables long names or alias 
    422422                if ("variables" in definition.keys()) and (var_coef in definition["alias"]): 
    423423                    i = definition["alias"].index(var_coef) 
     
    458458        for var, coef in submodel_spec.items(): 
    459459            if not equation_ids: 
    460                 variables.append(var) 
    461                 coefficients.append(coef) 
     460                if ("variables" in definition.keys()) and (var in definition["alias"]): 
     461                    i = definition["alias"].index(var) 
     462                    variables.append(definition["variables"][i]) 
     463                    coefficients.append(definition["coefficients"][i]) 
     464                    fixed_values.append(definition["fixed_values"][i]) 
     465                else: 
     466                    variables.append(var) 
     467                    coefficients.append(coef) 
     468                    fixed_values.append(0) 
    462469                submodels.append(sub_model) 
    463                 fixed_values.append(0) 
    464470            elif type(coef) is list or type(coef) is tuple: 
    465471                for i in range(len(coef)): 
    466472                    if coef[i] != 0: 
    467                         variables.append(var) 
     473                        if ("variables" in definition.keys()) and (var in definition["alias"]): 
     474                            j = definition["alias"].index(var) 
     475                            variables.append(definition["variables"][j]) 
     476                            fixed_values.append(definition["fixed_values"][j]) 
     477                        else: 
     478                            variables.append(var) 
     479                            fixed_values.append(0) 
    468480                        coefficients.append(coef[i]) 
    469481                        equations.append(equation_ids[i]) 
    470482                        submodels.append(sub_model) 
    471                         fixed_values.append(0) 
     483                         
    472484            else: 
    473485                logger.log_error("Wrong specification format for submodel %s variable %s; \nwith equation_ids provided, coefficients must be a list or tuple of the same length of equation_ids" % sub_model, var) 
     
    522534        specification.replace_variables(new_variables) 
    523535        result = specification.get_long_variable_names() 
    524         self.assert_(ma.allequal(result, array([variables[0], 'xxx.my_new_variable', 'y.replaces_y'])), 
     536        self.assert_(alltrue(result == array([variables[0], 'xxx.my_new_variable', 'y.replaces_y'])), 
    525537                     "Error in replace_varibles") 
    526538         
     
    543555        subm = result.get_submodels() 
    544556        fixedval = result.get_fixed_values() 
    545         self.assert_(ma.allequal(coefs, array(["BPOP", "BINC", "BART", "BHWY", "BAGE"])), 
     557        self.assert_(alltrue(coefs == array(["BPOP", "BINC", "BART", "BHWY", "BAGE"])), 
    546558                     msg = "Error in test_load_specification (coefficients)") 
    547         self.assert_(ma.allequal(vars, 
     559        self.assert_(alltrue(vars == 
    548560                                 array(["population", "average_income", "is_near_arterial", "is_near_highway", "lage"])), 
    549561                     msg = "Error in test_load_specification (variables)") 
     
    582594        subm = result.get_submodels() 
    583595        fixedval = result.get_fixed_values() 
    584         self.assert_(ma.allequal(coefs, array(["BPOP", "BINC", "BAGE", "BART", "C", "BHWY"])), 
     596        self.assert_(alltrue(coefs == array(["BPOP", "BINC", "BAGE", "BART", "C", "BHWY"])), 
    585597                     msg = "Error in test_load_specification_with_definition (coefficients)") 
    586         self.assert_(ma.allequal(vars, 
     598        self.assert_(alltrue(vars == 
    587599                                 array(["population", "average_income", "lage", "is_near_arterial", "constant",  
    588600                                        "is_near_highway"])), 
     
    614626        coefs = result.get_coefficient_names() 
    615627        subm = result.get_submodels() 
    616         self.assert_(ma.allequal(coefs, array(["population", "average_income", "lage", "is_near_arterial", "BHWY"])), 
    617                      msg = "Error in test_load_specification_with_definition (coefficients)") 
    618         self.assert_(ma.allequal(vars, 
     628        self.assert_(alltrue(coefs == array(["population", "average_income", "lage", "is_near_arterial", "BHWY"])), 
     629                     msg = "Error in test_load_specification_with_definition_with_implicit_coefficients (coefficients)") 
     630        self.assert_(alltrue(vars == 
    619631                                 array(["population", "average_income", "lage", "is_near_arterial", "is_near_highway"])), 
    620                      msg = "Error in test_load_specification_with_definition (variables)") 
     632                     msg = "Error in test_load_specification_with_definition_with_implicit_coefficients (variables)") 
    621633        self.assert_(ma.allequal(subm, array([1, 1, 1, 2, 2])), 
    622                      msg = "Error in test_load_specification_with_definition (submodels)") 
     634                     msg = "Error in test_load_specification_with_definition_with_implicit_coefficients (submodels)") 
    623635        # test data type 
    624636        self.assert_(subm.dtype.name == "int16", 
    625637                     msg = "Error in data type of submodels.") 
     638         
     639    def test_load_specification_with_definition_with_equations(self): 
     640        specification = { 
     641             "_definition_": [ 
     642                 "pop = urbansim.gridcell.population", 
     643                 "inc = urbansim.gridcell.average_income", 
     644                 "art = urbansim.gridcell.is_near_arterial", 
     645                 ], 
     646              -2: { 
     647                "equation_ids": (1,2), 
     648                 "pop": ("bpop",0),  
     649                 "inc": (0, "binc"),  
     650                 "art": ("bart", 0),  
     651                 "constant": ("asc", 0) 
     652                             } 
     653              } 
     654        result = load_specification_from_dictionary(specification) 
     655        vars = result.get_variable_names() 
     656        coefs = result.get_coefficient_names() 
     657        eqs = result.get_equations() 
     658        lvars = result.get_long_variable_names() 
     659        self.assert_(alltrue(coefs == array(["asc", "bart", "bpop", "binc"])), 
     660                     msg = "Error in test_load_specification_with_definition_with_equations (coefficients)") 
     661        self.assert_(alltrue(vars == array(["constant",  "art", "pop", "inc"])), 
     662                     msg = "Error in test_load_specification_with_definition_with_equations (variables)") 
     663        self.assert_(ma.allequal(eqs, array([1,1,1,2])), 
     664                     msg = "Error in test_load_specification_with_definition_with_equations (equations)") 
     665        self.assert_(alltrue(lvars == array(["constant",   
     666                                             "art = urbansim.gridcell.is_near_arterial",  
     667                                            "pop = urbansim.gridcell.population",  
     668                                            "inc = urbansim.gridcell.average_income"])), 
     669                     msg = "Error in test_load_specification_with_definition_with_equations (long names of variables)") 
     670 
    626671 
    627672if __name__=='__main__':