brightway2-data / bw2data / tests / geo.py

# -*- coding: utf-8 -*-
from . import BW2DataTest
from .. import Database, databases, geomapping, reset_meta, config, Method
from fixtures import food, biosphere
import copy
import numpy as np
import os
import pickle


class GeoTest(BW2DataTest):
    def extra_setup(self):
        geomapping.data = {}
        geomapping.flush()
        geomapping.__init__()

    def add_biosphere(self):
        d = Database("biosphere")
        d.register("biosphere", [], len(biosphere))
        d.write(biosphere)

    def add_method(self, simple=True):
        self.add_biosphere()
        method = Method(("test method",))
        method.register("kg")
        if simple:
            method.write({
                ("biosphere", 1): 6,
                ("biosphere", 2): 5
                })
        else:
            method.write({
                ("biosphere", 1): (6, "foo"),
                ("biosphere", 2): (5, "bar")
                })
        return method

    def test_geomapping_retrieval(self):
        geomapping.add(["foobar"])
        self.assertTrue("foobar" in geomapping)
        geomapping.__init__()
        self.assertTrue("foobar" in geomapping)

    def test_glo_always_present(self):
        print geomapping.data
        self.assertTrue("GLO" in geomapping)

    def test_method_adds_default_geo(self):
        method = self.add_method()
        method.process()
        pickled = pickle.load(open(os.path.join(config.dir, "processed",
            method.get_abbreviation() + ".pickle"), "rb"))
        global_index = geomapping["GLO"]
        self.assertEqual(global_index, int(pickled[0]["geo"]))
        self.assertEqual(global_index, int(pickled[1]["geo"]))
        self.assertEquals(pickled.shape, (2,))

    def test_method_adds_correct_geo(self):
        method = self.add_method(False)
        method.process()
        pickled = pickle.load(open(os.path.join(config.dir, "processed",
            method.get_abbreviation() + ".pickle"), "rb"))
        self.assertEqual(geomapping["foo"], int(pickled[0]["geo"]))
        self.assertEqual(geomapping["bar"], int(pickled[1]["geo"]))
        self.assertEquals(pickled.shape, (2,))

    def test_database_adds_correct_geo(self):
        self.add_biosphere()
        database = Database("food")
        database.register("food", ["biosphere"], len(food))
        database.write(food)
        database.process()
        pickled = pickle.load(open(os.path.join(config.dir, "processed",
            database.database + ".pickle"), "rb"))
        self.assertTrue(geomapping["CA"] in pickled["geo"].tolist())
        self.assertTrue(geomapping["CH"] in pickled["geo"].tolist())

    def test_database_adds_default_geo(self):
        self.add_biosphere()
        database = Database("food")
        database.register("food", ["biosphere"], len(food))
        new_food = copy.deepcopy(food)
        for v in new_food.values():
            del v["location"]
        database.write(new_food)
        database.process()
        pickled = pickle.load(open(os.path.join(config.dir, "processed",
            database.database + ".pickle"), "rb"))
        self.assertTrue(np.allclose(pickled["geo"],
            geomapping["GLO"] * np.ones(pickled.shape)))

    def test_method_write_adds_to_geomapping(self):
        self.add_method(False)
        self.assertTrue("foo" in geomapping)
        self.assertTrue("bar" in geomapping)

    def test_database_write_adds_to_geomapping(self):
        self.add_biosphere()
        d = Database("food")
        d.register("Tests", ["biosphere"], len(food))
        d.write(food)
        self.assertTrue("CA" in geomapping)
        self.assertTrue("CH" in geomapping)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.