Esempio n. 1
0
def test_export(session_with_pywr_network, root_user_id):
    pywr_network_id, pywr_json_filename = session_with_pywr_network

    # Fetch the network
    network = hydra_base.get_network(pywr_network_id,
                                     user_id=root_user_id,
                                     include_data='Y')
    # Fetch all the attributes
    attributes = hydra_base.get_attributes()
    # TODO not sure why this returns SQLAlchemy object?
    # TODO rename this to map/lookup
    attributes = {attr.id: JSONObject(attr) for attr in attributes}

    # We also need the template to get the node types
    template = JSONObject(
        hydra_base.get_template_by_name(pywr_template_name('Full')))

    exporter = PywrHydraExporter(network, attributes, template)

    pywr_data_exported = exporter.get_pywr_data()

    # Check transformed data is about right
    with open(pywr_json_filename) as fh:
        pywr_data = json.load(fh)

    assert_identical_pywr_data(pywr_data, pywr_data_exported)
Esempio n. 2
0
def test_add_network(pywr_json_filename, session_with_pywr_template,
                     projectmaker, root_user_id):
    project = projectmaker.create()

    template = JSONObject(
        hydra_base.get_template_by_name(pywr_template_name('Full')))

    importer = PywrHydraImporter(pywr_json_filename, template)

    # First the attributes must be added.
    attributes = [
        JSONObject(a) for a in importer.add_attributes_request_data()
    ]

    # The response attributes have ids now.
    response_attributes = hydra_base.add_attributes(attributes)

    # Convert to a simple dict for local processing.
    # TODO change this variable name to map or lookup
    attribute_ids = {a.name: a.id for a in response_attributes}

    # Now we try to create the network
    network = importer.add_network_request_data(attribute_ids, project.id)

    # Check transformed data is about right
    with open(pywr_json_filename) as fh:
        pywr_data = json.load(fh)

    assert_hydra_pywr(network, pywr_data)
    hydra_network = hydra_base.add_network(JSONObject(network),
                                           user_id=root_user_id)
Esempio n. 3
0
def session_with_pywr_network(pywr_json_filename, session_with_pywr_template,
                              projectmaker, root_user_id):
    project = projectmaker.create()

    template = JSONObject(
        hydra_base.get_template_by_name(pywr_template_name('Full')))

    importer = PywrHydraImporter(pywr_json_filename, template)

    # First the attributes must be added.
    attributes = [
        JSONObject(a) for a in importer.add_attributes_request_data()
    ]

    # The response attributes have ids now.
    response_attributes = hydra_base.add_attributes(attributes)

    # Convert to a simple dict for local processing.
    attribute_ids = {a.name: a.id for a in response_attributes}

    # Now we try to create the network
    network = importer.add_network_request_data(attribute_ids, project.id)
    hydra_network = hydra_base.add_network(JSONObject(network),
                                           user_id=root_user_id)

    return hydra_network.id, pywr_json_filename
Esempio n. 4
0
def test_add_network(pywr_json_filename, session_with_pywr_template, projectmaker, root_user_id):
    project = projectmaker.create()

    template = JSONObject(hydra_base.get_template_by_name(pywr_template_name('Full')))

    importer = PywrHydraImporter(pywr_json_filename, template)

    # First the attributes must be added.
    attributes = [JSONObject(a) for a in importer.add_attributes_request_data()]

    # The response attributes have ids now.
    response_attributes = hydra_base.add_attributes(attributes)

    # Convert to a simple dict for local processing.
    # TODO change this variable name to map or lookup
    attribute_ids = {a.name: a.id for a in response_attributes}

    # Now we try to create the network
    network = importer.add_network_request_data(attribute_ids, project.id)

    # Check transformed data is about right
    with open(pywr_json_filename) as fh:
        pywr_data = json.load(fh)

    assert_hydra_pywr(network, pywr_data)
    hydra_network = hydra_base.add_network(JSONObject(network), user_id=root_user_id)
Esempio n. 5
0
def test_create_empty_network(db_with_template, projectmaker,
                              logged_in_client):
    client = logged_in_client

    project = projectmaker.create()
    template = client.get_template_by_name(pywr_template_name('Full'))

    # Find the network type in this template.
    # There is only one of these in the template.
    for template_type in template['templatetypes']:
        if template_type['resource_type'] == 'NETWORK':
            template_type_id = template_type['id']
            break
    else:
        raise ValueError('No network type found in this template!')

    # This is a minimal network with no data and a scenario
    network_data = {
        'name':
        'empty',
        'description':
        'empty network',
        'project_id':
        project.id,
        'types': [{
            'id': template_type_id
        }],
        'scenarios': [{
            "name": "Baseline",
            "description": "",
            "resourcescenarios": []
        }]
    }

    hydra_network = client.add_network(network_data)

    scenario_id = hydra_network['scenarios'][0]['id']

    exporter = PywrHydraExporter.from_network_id(client, hydra_network.id,
                                                 scenario_id)
    pywr_data_exported = exporter.get_pywr_data()

    assert 'timestepper' in pywr_data_exported

    for key in PYWR_TIMESTEPPER_ATTRIBUTES:
        assert key in pywr_data_exported['timestepper']

    assert 'metadata' in pywr_data_exported
    assert 'title' in pywr_data_exported['metadata']
    assert 'description' in pywr_data_exported['metadata']

    assert 'nodes' in pywr_data_exported
    assert 'edges' in pywr_data_exported
Esempio n. 6
0
def test_add_network(pywr_json_filename, db_with_template, projectmaker,
                     logged_in_client):
    client = logged_in_client

    project = projectmaker.create()

    config = load_template_config('full')
    template = client.get_template_by_name(pywr_template_name(config['name']))

    importer = PywrHydraImporter.from_client(client, pywr_json_filename,
                                             template['id'])
    importer.import_data(client, project.id)
Esempio n. 7
0
def test_create_empty_network(db_with_template, projectmaker, logged_in_client):
    client = logged_in_client

    project = projectmaker.create()
    template = client.get_template_by_name(pywr_template_name('Full'))

    # Find the network type in this template.
    # There is only one of these in the template.
    for template_type in template['templatetypes']:
        if template_type['resource_type'] == 'NETWORK':
            template_type_id = template_type['id']
            break
    else:
        raise ValueError('No network type found in this template!')

    # This is a minimal network with no data and a scenario
    network_data = {
        'name': 'empty', 'description': 'empty network', 'project_id': project.id, 'types': [{'id': template_type_id}],
        'scenarios': [{
            "name": "Baseline",
            "description": "",
            "resourcescenarios": []
        }]
    }

    hydra_network = client.add_network(network_data)

    scenario_id = hydra_network['scenarios'][0]['id']

    exporter = PywrHydraExporter.from_network_id(client, hydra_network.id, scenario_id)
    pywr_data_exported = exporter.get_pywr_data()

    assert 'timestepper' in pywr_data_exported

    for key in PYWR_TIMESTEPPER_ATTRIBUTES:
        assert key in pywr_data_exported['timestepper']

    assert 'metadata' in pywr_data_exported
    assert 'title' in pywr_data_exported['metadata']
    assert 'description' in pywr_data_exported['metadata']

    assert 'nodes' in pywr_data_exported
    assert 'edges' in pywr_data_exported
Esempio n. 8
0
def session_with_pywr_network(pywr_json_filename, session_with_pywr_template, projectmaker, root_user_id):
    project = projectmaker.create()

    template = JSONObject(hydra_base.get_template_by_name(pywr_template_name('Full')))

    importer = PywrHydraImporter(pywr_json_filename, template)

    # First the attributes must be added.
    attributes = [JSONObject(a) for a in importer.add_attributes_request_data()]

    # The response attributes have ids now.
    response_attributes = hydra_base.add_attributes(attributes)

    # Convert to a simple dict for local processing.
    attribute_ids = {a.name: a.id for a in response_attributes}

    # Now we try to create the network
    network = importer.add_network_request_data(attribute_ids, project.id)
    hydra_network = hydra_base.add_network(JSONObject(network), user_id=root_user_id)

    return hydra_network.id, pywr_json_filename
Esempio n. 9
0
def pywr_with_demand_pattern(model_directory, db_with_template, projectmaker,
                             logged_in_client):
    client = logged_in_client

    # Create the basic pywr model
    project = projectmaker.create()
    pywr_json_filename = os.path.join(model_directory, 'simple1.json')
    template = client.get_template_by_name(pywr_template_name('Full'))

    importer = PywrHydraImporter.from_client(client, pywr_json_filename,
                                             template['id'])
    network_id, scenario_id = importer.import_data(client, project.id)

    # Create the demand pattern
    pattern_attr = client.add_attribute({'name': 'demand_pattern'})
    ra = client.add_resource_attribute('NETWORK', network_id,
                                       pattern_attr['id'], 'N')

    with open(os.path.join(model_directory,
                           'simple_demand_pattern.json')) as fh:
        pattern_str = fh.read()

    pattern_data = Dataset({
        'name': 'demand_pattern',
        'value': pattern_str,
        "hidden": "N",
        "type": 'PYWR_PARAMETER_PATTERN',
    })

    client.add_data_to_attribute(scenario_id, ra['id'], pattern_data)

    # Assign the pattern to one of the nodes
    node = client.get_node_by_name(network_id, 'demand1')
    pattern_ref_attr = client.add_attribute({'name': 'demand'})
    ra = client.add_resource_attribute('NODE', node['id'],
                                       pattern_ref_attr['id'], 'N')

    pattern_ref_data = Dataset({
        'name': 'demand',
        'value': 'demand_pattern',
        'hidden': 'N',
        'type': 'PYWR_PARAMETER_PATTERN_REF'
    })

    client.add_data_to_attribute(scenario_id, ra['id'], pattern_ref_data)

    #
    population_attr = client.add_attribute({'name': 'population'})
    ra = client.add_resource_attribute('NODE', node['id'],
                                       population_attr['id'], 'N')

    population_data = Dataset({
        'name': 'population',
        'value': 3.14,
        'hidden': 'N',
        'type': 'SCALAR'
    })

    client.add_data_to_attribute(scenario_id, ra['id'], population_data)

    return network_id, scenario_id