| 419 | | for var_coef in submodel_spec: |
| 420 | | if isinstance(var_coef, str): |
| 421 | | #var_coef is just variables long names or alias |
| 422 | | if ("variables" in definition.keys()) and (var_coef in definition["alias"]): |
| 423 | | i = definition["alias"].index(var_coef) |
| 424 | | variables.append(definition["variables"][i]) |
| 425 | | coefficients.append(definition["coefficients"][i]) |
| 426 | | fixed_values.append(definition["fixed_values"][i]) |
| | 419 | variable, coefficient, fixed_value, error = get_variables_coefficients_equations_for_submodel_part( |
| | 420 | submodel_spec, definition) |
| | 421 | if error: |
| | 422 | logger.log_error("Error in specification of submodel %s." % sub_model) |
| | 423 | variables += variable |
| | 424 | coefficients += coefficient |
| | 425 | fixed_values += fixed_value |
| | 426 | submodels += len(variable)*[sub_model] |
| | 427 | elif isinstance(submodel_spec, dict): |
| | 428 | speckeys = submodel_spec.keys() |
| | 429 | if sum(map(lambda(x): isinstance(x, int), speckeys)) == len(speckeys): # keys are the equations |
| | 430 | for eq, spec in submodel_spec.iteritems(): |
| | 431 | variable, coefficient, fixed_value, error = get_variables_coefficients_equations_for_submodel_part( |
| | 432 | spec, definition) |
| | 433 | if error: |
| | 434 | logger.log_error("Error in specification of submodel %s equation %s" % (sub_model, eq)) |
| | 435 | variables += variable |
| | 436 | coefficients += coefficient |
| | 437 | fixed_values += fixed_value |
| | 438 | submodels += len(variable)*[sub_model] |
| | 439 | equations += len(variable)*[eq] |
| | 440 | else: |
| | 441 | if submodel_spec.has_key("equation_ids"): |
| | 442 | equation_ids = submodel_spec["equation_ids"] |
| | 443 | del submodel_spec["equation_ids"] |
| | 444 | else: |
| | 445 | equation_ids = None |
| | 446 | |
| | 447 | for var, coef in submodel_spec.items(): |
| | 448 | if not equation_ids: |
| | 449 | if ("variables" in definition.keys()) and (var in definition["alias"]): |
| | 450 | i = definition["alias"].index(var) |
| | 451 | variables.append(definition["variables"][i]) |
| | 452 | coefficients.append(definition["coefficients"][i]) |
| | 453 | fixed_values.append(definition["fixed_values"][i]) |
| | 454 | else: |
| | 455 | variables.append(var) |
| | 456 | coefficients.append(coef) |
| | 457 | fixed_values.append(0) |
| | 458 | submodels.append(sub_model) |
| | 459 | elif type(coef) is list or type(coef) is tuple: |
| | 460 | for i in range(len(coef)): |
| | 461 | if coef[i] != 0: |
| | 462 | if ("variables" in definition.keys()) and (var in definition["alias"]): |
| | 463 | j = definition["alias"].index(var) |
| | 464 | variables.append(definition["variables"][j]) |
| | 465 | fixed_values.append(definition["fixed_values"][j]) |
| | 466 | else: |
| | 467 | variables.append(var) |
| | 468 | fixed_values.append(0) |
| | 469 | coefficients.append(coef[i]) |
| | 470 | equations.append(equation_ids[i]) |
| | 471 | submodels.append(sub_model) |
| | 472 | |
| 428 | | variables.append(var_coef) |
| 429 | | coefficients.append(VariableName(var_coef).get_alias()) |
| | 474 | 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) |
| | 475 | |
| | 476 | return (variables, coefficients, equations, submodels, fixed_values) |
| | 477 | |
| | 478 | def get_variables_coefficients_equations_for_submodel_part(submodel_spec, definition={}): |
| | 479 | variables = [] |
| | 480 | coefficients = [] |
| | 481 | equations = [] |
| | 482 | submodels = [] |
| | 483 | fixed_values = [] |
| | 484 | error = False |
| | 485 | for var_coef in submodel_spec: |
| | 486 | if isinstance(var_coef, str): |
| | 487 | #var_coef is just variables long names or alias |
| | 488 | if ("variables" in definition.keys()) and (var_coef in definition["alias"]): |
| | 489 | i = definition["alias"].index(var_coef) |
| | 490 | variables.append(definition["variables"][i]) |
| | 491 | coefficients.append(definition["coefficients"][i]) |
| | 492 | fixed_values.append(definition["fixed_values"][i]) |
| | 493 | else: |
| | 494 | variables.append(var_coef) |
| | 495 | coefficients.append(VariableName(var_coef).get_alias()) |
| | 496 | fixed_values.append(0) |
| | 497 | elif isinstance(var_coef, tuple) or isinstance(var_coef, list): |
| | 498 | if len(var_coef) == 1: # coefficient name is created from variable alias |
| | 499 | variables.append(var_coef[0]) |
| | 500 | coefficients.append(VariableName(var_coef[0]).get_alias()) |
| | 501 | fixed_values.append(0) |
| | 502 | elif len(var_coef) > 1: # coefficient names explicitely given |
| | 503 | variables.append(var_coef[0]) |
| | 504 | coefficients.append(var_coef[1]) |
| | 505 | if len(var_coef) > 2: # third item is the coefficient fixed value |
| | 506 | fixed_values.append(var_coef[2]) |
| | 507 | else: |
| 431 | | elif isinstance(var_coef, tuple) or isinstance(var_coef, list): |
| 432 | | if len(var_coef) == 1: # coefficient name is created from variable alias |
| 433 | | variables.append(var_coef[0]) |
| 434 | | coefficients.append(VariableName(var_coef[0]).get_alias()) |
| 435 | | fixed_values.append(0) |
| 436 | | elif len(var_coef) > 1: # coefficient names explicitely given |
| 437 | | variables.append(var_coef[0]) |
| 438 | | coefficients.append(var_coef[1]) |
| 439 | | if len(var_coef) > 2: # third item is the coefficient fixed value |
| 440 | | fixed_values.append(var_coef[2]) |
| 441 | | else: |
| 442 | | fixed_values.append(0) |
| 443 | | else: |
| 444 | | logger.log_error("Wrong specification format for submodel %s variable %s" % sub_model, submodel_spec) |
| 445 | | elif isinstance(var_coef, dict): |
| 446 | | variables.append(var_coef.keys()[0]) |
| 447 | | coefficients.append(var_coef.values()[0]) |
| 448 | | fixed_values.append(0) |
| 450 | | logger.log_error("Wrong specification format for submodel %s variable %s" % sub_model, submodel_spec) |
| 451 | | submodels.append(sub_model) |
| 452 | | elif isinstance(submodel_spec, dict): |
| 453 | | if submodel_spec.has_key("equation_ids"): |
| 454 | | equation_ids = submodel_spec["equation_ids"] |
| 455 | | del submodel_spec["equation_ids"] |
| | 510 | logger.log_error("Wrong specification format for variable %s" % submodel_spec) |
| | 511 | error = True |
| | 512 | elif isinstance(var_coef, dict): |
| | 513 | variables.append(var_coef.keys()[0]) |
| | 514 | coefficients.append(var_coef.values()[0]) |
| | 515 | fixed_values.append(0) |
| 457 | | equation_ids = None |
| 458 | | for var, coef in submodel_spec.items(): |
| 459 | | if not equation_ids: |
| 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) |
| 469 | | submodels.append(sub_model) |
| 470 | | elif type(coef) is list or type(coef) is tuple: |
| 471 | | for i in range(len(coef)): |
| 472 | | if coef[i] != 0: |
| 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) |
| 480 | | coefficients.append(coef[i]) |
| 481 | | equations.append(equation_ids[i]) |
| 482 | | submodels.append(sub_model) |
| 483 | | |
| 484 | | else: |
| 485 | | 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) |
| 486 | | |
| 487 | | return (variables, coefficients, equations, submodels, fixed_values) |
| 488 | | |
| 489 | | |
| | 517 | logger.log_error("Wrong specification format for variable %s" % submodel_spec) |
| | 518 | error = True |
| | 519 | |
| | 520 | return (variables, coefficients, fixed_values, error) |
| | 701 | |
| | 702 | def test_load_specification_with_definition_with_equations_v2(self): |
| | 703 | specification = { |
| | 704 | "_definition_": [ |
| | 705 | ("pop = urbansim.gridcell.population", "bpop"), |
| | 706 | "inc = urbansim.gridcell.average_income", |
| | 707 | "art = urbansim.gridcell.is_near_arterial", |
| | 708 | ], |
| | 709 | -2: { |
| | 710 | 1: [ |
| | 711 | "pop", |
| | 712 | "inc", |
| | 713 | "constant" ], |
| | 714 | 2: [ "art"] |
| | 715 | } |
| | 716 | } |
| | 717 | result = load_specification_from_dictionary(specification) |
| | 718 | vars = result.get_variable_names() |
| | 719 | coefs = result.get_coefficient_names() |
| | 720 | eqs = result.get_equations() |
| | 721 | lvars = result.get_long_variable_names() |
| | 722 | self.assert_(alltrue(coefs == array(["bpop", "inc", "constant", "art",])), |
| | 723 | msg = "Error in test_load_specification_with_definition_with_equations_v2 (coefficients)") |
| | 724 | self.assert_(alltrue(vars == array(["pop", "inc", "constant", "art"])), |
| | 725 | msg = "Error in test_load_specification_with_definition_with_equations (variables)") |
| | 726 | self.assert_(ma.allequal(eqs, array([1,1,1,2])), |
| | 727 | msg = "Error in test_load_specification_with_definition_with_equations (equations)") |
| | 728 | self.assert_(alltrue(lvars == array(["pop = urbansim.gridcell.population", |
| | 729 | "inc = urbansim.gridcell.average_income", |
| | 730 | "constant", |
| | 731 | "art = urbansim.gridcell.is_near_arterial", |
| | 732 | ])), |
| | 733 | msg = "Error in test_load_specification_with_definition_with_equations (long names of variables)") |