1. Chris Mutel
  2. brightway2-calc

Commits

Chris Mutel  committed d0419da

Use Database.filename to load processed arrays. Compatible with bw2data 0.16

  • Participants
  • Parent commits 4a7e2ca
  • Branches default

Comments (0)

Files changed (5)

File CHANGES.txt

View file
  • Ignore whitespace
 Changelog
 *********
 
+0.14 (2014-XX-XX)
+=================
+
+- BREAKING CHANGE: Use ``Database.filename`` for processed data. Requires update to bw2data version 0.16 or greater.
+
 0.13 (2014-04-16)
 =================
 

File bw2calc/tests/matrices.py

View file
  • Ignore whitespace
 from .. import *
-from bw2data import config
+from bw2data import config, Database
 from bw2data.tests import BW2DataTest
 from bw2data.utils import MAX_INT_32
 import numpy as np
-import os
 try:
     import cPickle as pickle
 except ImportError:
 
 class MatrixBuilderTestCase(BW2DataTest):
     def test_load(self):
+        a_db = Database("a")
+        a_db.register()
+        b_db = Database("b")
+        b_db.register()
         a = np.ones(10)
         b = np.arange(10)
         c = np.hstack((a, b))
-        with open(os.path.join(config.dir, "processed", "a.pickle"), "wb") as f:
+        with open(a_db.filepath_processed(), "wb") as f:
             pickle.dump(a, f, protocol=pickle.HIGHEST_PROTOCOL)
-        with open(os.path.join(config.dir, "processed", "b.pickle"), "wb") as f:
+        with open(b_db.filepath_processed(), "wb") as f:
             pickle.dump(b, f, protocol=pickle.HIGHEST_PROTOCOL)
         self.assertTrue(np.allclose(
             a,
         ))
 
     def test_build_one_d(self):
+        database = Database("sour")
+        database.register()
         dtype = [
             ('a', np.uint32),
             ('row', np.uint32),
             ], dtype=dtype
         )
         row_dict = {1: 0, 2: 1}
-        with open(os.path.join(
-                config.dir,
-                "processed",
-                "sour.pickle"), "wb") as f:
+        with open(database.filepath_processed(), "wb") as f:
             pickle.dump(array, f, protocol=pickle.HIGHEST_PROTOCOL)
         matrix = MatrixBuilder.build(config.dir, ["sour"], "values", "a",
             "row", row_dict=row_dict, one_d=True)[3]
         ))
 
     def test_build_one_d_drop_missing(self):
+        database = Database("ghost")
+        database.register()
         dtype = [
             ('a', np.uint32),
             ('row', np.uint32),
             ], dtype=dtype
         )
         row_dict = {1: 0, 2: 1}
-        with open(os.path.join(
-                config.dir,
-                "processed",
-                "ghost.pickle"), "wb") as f:
+        with open(database.filepath_processed(), "wb") as f:
             pickle.dump(array, f, protocol=pickle.HIGHEST_PROTOCOL)
         values = MatrixBuilder.build(config.dir, ["ghost"], "values", "a",
             "row", row_dict=row_dict, one_d=True)[0]
         self.assertEqual(values.shape, (2,))
 
     def test_one_d_missing_in_row_dict_raise_valueerror(self):
+        database = Database("ghost")
+        database.register()
         dtype = [
             ('a', np.uint32),
             ('row', np.uint32),
             ], dtype=dtype
         )
         row_dict = {1: 0}
-        with open(os.path.join(
-                config.dir,
-                "processed",
-                "ghost.pickle"), "wb") as f:
+        with open(database.filepath_processed(), "wb") as f:
             pickle.dump(array, f, protocol=pickle.HIGHEST_PROTOCOL)
         with self.assertRaises(ValueError):
             MatrixBuilder.build(config.dir, ["ghost"], "values", "a",
                 "row", row_dict=row_dict, one_d=True, drop_missing=False)
 
     def test_build_drop_missing(self):
+        database = Database("boo")
+        database.register()
         dtype = [
             ('a', np.uint32),
             ('b', np.uint32),
         )
         row_dict = {1: 0, 3: 1}
         col_dict = {2: 0, 6: 1}
-        with open(os.path.join(
-                config.dir,
-                "processed",
-                "boo.pickle"), "wb") as f:
+        with open(database.filepath_processed(), "wb") as f:
             pickle.dump(array, f, protocol=pickle.HIGHEST_PROTOCOL)
         values = MatrixBuilder.build(config.dir, ["boo"], "values", "a", "row",
             "b", "col", row_dict, col_dict)[0]
         self.assertEqual(values.shape, (2,))
 
     def test_missing_in_row_dict_raise_valueerror(self):
+        database = Database("whoah")
+        database.register()
         dtype = [
             ('a', np.uint32),
             ('b', np.uint32),
         )
         row_dict = {1: 0}
         col_dict = {2: 0}
-        with open(os.path.join(
-                config.dir,
-                "processed",
-                "whoah.pickle"), "wb") as f:
+        with open(database.filepath_processed(), "wb") as f:
             pickle.dump(array, f, protocol=pickle.HIGHEST_PROTOCOL)
         with self.assertRaises(ValueError):
             MatrixBuilder.build(config.dir, ["whoah"], "values", "a",

File bw2calc/utils.py

View file
  • Ignore whitespace
 # -*- coding: utf-8 -*
+from bw2data.database import Database
 import numpy as np
 import os
 try:
 
 def load_arrays(dirpath, names):
     return np.hstack([pickle.load(open(os.path.join(
-        dirpath, "processed", "%s.pickle" % name), "rb")
+        dirpath, u"processed", u"%s.pickle" % Database(name).filename), "rb")
     ) for name in names])
 
 

File requirements.txt

View file
  • Ignore whitespace
 bw2analyzer
-bw2data
+bw2data>=0.16
 nose
 numpy>=1.6
 progressbar

File setup.py

View file
  • Ignore whitespace
     author_email="cmutel@gmail.com",
     license=open('LICENSE.txt').read(),
     url="https://bitbucket.org/cmutel/brightway2-calc",
-    install_requires=["brightway2"],
+    install_requires=["brightway2", "bw2data>=0.16"],
     long_description=open('README.rst').read(),
     classifiers=[
         'Development Status :: 5 - Production/Stable',