Commits

Chris Mutel committed 2f496eb

Use _mapped_dict to determine if fix_dictionaries has been run. Fix build_demand_array bug

Comments (0)

Files changed (3)

 Changelog
 *********
 
+0.13 (2014-XX-XX)
+=================
+
+- BREAKING CHANGE: LCA.fix_dictionaries now sets/uses ``_mapped_dict`` to determine if ``fix_dictionaries`` has been called.
+- BUGFIX: LCA.build_demand_array doesn't break if ``fix_dictionaries`` has been called.
+
 0.12 (2014-02-13)
 =================
 
             A new LCA object
 
         """
+        self._mapped_dict = True
         self.dirpath = (config or base_config).dir
         if isinstance(demand, (basestring, tuple, list)):
             raise ValueError("Demand must be a dictionary")
         demand = demand or self.demand
         self.demand_array = np.zeros(len(self.technosphere_dict))
         for key in demand:
-            self.demand_array[self.technosphere_dict[mapping[key]]] = \
+            if self._mapped_dict:
+                self.demand_array[self.technosphere_dict[mapping[key]]] = \
                 demand[key]
+            else:
+                self.demand_array[self.technosphere_dict[key]] = demand[key]
+
 
     #########################
     ### Data manipulation ###
 Doesn't require any arguments or return anything, but changes ``self.technosphere_dict`` and ``self.biosphere_dict``.
 
         """
-        if not isinstance(self.technosphere_dict.keys()[0], int):
+        if not self._mapped_dict:
             # Already reversed - should be idempotent
-            return
+            return False
         rev_mapping = {v: k for k, v in mapping.iteritems()}
         self.technosphere_dict = {
             rev_mapping[k]: v for k, v in self.technosphere_dict.iteritems()}
         self.biosphere_dict = {
             rev_mapping[k]: v for k, v in self.biosphere_dict.iteritems()}
+        self._mapped_dict = False
+        return True
 
     def reverse_dict(self):
         """Construct reverse dicts from technosphere and biosphere row and col indices to technosphere_dict/biosphere_dict keys.

bw2calc/tests/lca.py

         # Indirect test because no easy way to test a function is called
         lca.technosphere_matrix = None
         self.assertEqual(float(lca.solve_linear_system().sum()), 1.5)
+
+    def test_fix_dictionaries(self):
+        test_data = {
+            ("t", 1): {
+                'exchanges': [{
+                    'amount': 0.5,
+                    'input': ('t', 2),
+                    'type': 'technosphere',
+                    'uncertainty type': 0},
+                    {'amount': 1,
+                    'input': ('biosphere', 1),
+                    'type': 'biosphere',
+                    'uncertainty type': 0}],
+                'type': 'process',
+                'unit': 'kg'
+                },
+            ("t", 2): {
+                'exchanges': [],
+                'type': 'process',
+                'unit': 'kg'
+                },
+            }
+        self.add_basic_biosphere()
+        test_db = Database("t")
+        test_db.register(depends=["biosphere"])
+        test_db.write(test_data)
+        test_db.process()
+        lca = LCA({("t", 1): 1})
+        lca.lci()
+
+        supply = lca.supply_array.sum()
+
+        self.assertTrue(lca._mapped_dict)
+        self.assertTrue(lca.fix_dictionaries())
+        self.assertFalse(lca._mapped_dict)
+        # Second time doesn't do anything
+        self.assertFalse(lca.fix_dictionaries())
+        self.assertFalse(lca._mapped_dict)
+        lca.redo_lci({("t", 1): 2})
+        self.assertEqual(lca.supply_array.sum(), supply * 2)