Commits

Chris Mutel committed 6ecc463

PEP-8 fixes

Comments (0)

Files changed (6)

bw2calc/graph_traversal.py

 
         # Create matrix of LCIA CFs times biosphere flows, as these don't
         # change. This is also the unit score of each activity.
-        characterized_biosphere = np.array(
-            (lca.characterization_matrix.data * \
+        characterized_biosphere = np.array((
+            lca.characterization_matrix.data *
             lca.biosphere_matrix.data).sum(axis=0)).ravel()
 
-        heap, nodes, edges = self.initialize_heap(demand, lca, supply,
-            characterized_biosphere)
-        nodes, edges, counter = self.traverse(heap, nodes, edges, counter,
-            max_calc, cutoff, score, supply, characterized_biosphere, lca)
+        heap, nodes, edges = self.initialize_heap(
+            demand, lca, supply, characterized_biosphere)
+        nodes, edges, counter = self.traverse(
+            heap, nodes, edges, counter, max_calc, cutoff, score, supply,
+            characterized_biosphere, lca)
         nodes = self.add_metadata(nodes, lca)
 
         return {
             'edges': edges,
             'lca': lca,
             'counter': counter,
-            }
+        }
 
     def initialize_heap(self, demand, lca, supply, characterized_biosphere):
         heap, nodes, edges = [], {}, []
             heappush(heap, (1, index))
             nodes[index] = {
                 "amount": supply[index],
-                "cum": self.cumulative_score(index, supply,
-                    characterized_biosphere, lca),
+                "cum": self.cumulative_score(
+                    index, supply, characterized_biosphere, lca),
                 "ind": self.unit_score(index, supply, characterized_biosphere)
-                }
+            }
             # -1 is a special index for total demand, which can be
             # composite. Initial edges are inputs to the
             # functional unit.
                 "from": index,
                 "amount": value,
                 "impact": lca.score,
-                })
+            })
         return heap, nodes, edges
 
     def build_lca(self, demand, method):
         return float(characterized_biosphere[index] * supply[index])
 
     def traverse(self, heap, nodes, edges, counter, max_calc, cutoff,
-            total_score, supply, characterized_biosphere, lca):
+                 total_score, supply, characterized_biosphere, lca):
         """
 Build a directed graph of the supply chain.
 
                 if activity == parent_index or amount <= 0:
                     continue
                 counter += 1
-                cumulative_score = self.cumulative_score(activity, supply,
-                    characterized_biosphere, lca)
+                cumulative_score = self.cumulative_score(
+                    activity, supply, characterized_biosphere, lca)
                 if abs(cumulative_score) < abs(total_score * cutoff):
                     continue
                 # Edge format is (to, from, mass amount, cumulative impact)
                     # Amount of this link * amount of parent demanding link
                     "amount": amount * nodes[parent_index]["amount"],
                     # Amount of this input
-                    "impact": amount * nodes[parent_index]["amount"] \
+                    "impact": amount * nodes[parent_index]["amount"]
                     # times impact per unit of this input
-                        * cumulative_score / supply[activity]
-                    })
+                    * cumulative_score / supply[activity]
+                })
                 # Want multiple incoming edges, but don't add existing node
                 if activity in nodes:
                     continue
                     # Individual score attributable to environmental flows
                     # coming directory from or to this activity
                     "ind": self.unit_score(activity, supply,
-                        characterized_biosphere)
-                    }
+                                           characterized_biosphere)
+                }
                 heappush(heap, (1 / cumulative_score, activity))
 
         return nodes, edges, counter
             "amount": 1,
             "name": "Functional unit",
             "cat": "Functional unit"
-            })]
+        })]
         for key, value in nodes.iteritems():
             if key == -1:
                 continue
                 "code": code,
                 "name": db_data[code]["name"],
                 "cat": db_data[code]["categories"][0],
-                })
+            })
             new_nodes.append((key, value))
         return dict(new_nodes)
 
     """
 This class takes nodes and edges, and removes nodes to edges with low individual scores and reroutes the edges.
     """
-    nodes_to_delete = [key for key, value in nodes.iteritems() if \
-        value["ind"] < (total * limit) and key != -1]
+    nodes_to_delete = [key for key, value in nodes.iteritems() if
+                       value["ind"] < (total * limit) and key != -1]
     size = len(nodes)
     arr = np.zeros((size, size), dtype=np.float32)
     arr_map = dict([(key, index) for index, key in enumerate(sorted(nodes.keys()))])
             "source": lookup[e["to"]],
             "target": lookup[e["from"]],
             "amount": e["impact"]
-            } for e in edges]
+        } for e in edges]
         return {"edges": new_edges, "nodes": new_nodes,
-            "total": total}
+                "total": total}
 
     def get_databases(self, demand):
         """Get list of databases for functional unit"""
-        return set.union(*[set(databases[key[0]]["depends"] + [key[0]]
-            ) for key in demand])
+        return set.union(
+            *[set(databases[key[0]]["depends"] + [key[0]]) for key in demand])
 
     def build_demand_array(self, demand=None):
         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]]] = demand[key]
+            self.demand_array[self.technosphere_dict[mapping[key]]] = \
+                demand[key]
 
     #########################
     ### Data manipulation ###
 
         """
         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.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()}
 
     def reverse_dict(self):
         """Construct reverse dicts from row and col indices to processes"""
             builder.build(self.dirpath, self.databases)
 
     def load_lcia_data(self, builder=MatrixBuilder):
-        self.cf_params, dummy, dummy, self.characterization_matrix = \
-            builder.build(self.dirpath, [methods[self.method]['abbreviation']
-                ], "amount", "flow", "index", row_dict=self.biosphere_dict,
-                one_d=True)
+        self.cf_params, d, d, self.characterization_matrix = builder.build(
+            self.dirpath, [methods[self.method]['abbreviation']],
+            "amount", "flow", "index", row_dict=self.biosphere_dict,
+            one_d=True)
 
     ####################
     ### Calculations ###
     #########################
 
     def rebuild_technosphere_matrix(self, vector):
-        self.technosphere_matrix = MatrixBuilder.build_matrix(self.tech_params, self.technosphere_dict, self.technosphere_dict, "row", "col", new_data=vector)
+        self.technosphere_matrix = MatrixBuilder.build_matrix(
+            self.tech_params, self.technosphere_dict, self.technosphere_dict,
+            "row", "col", new_data=vector)
 
     def rebuild_biosphere_matrix(self, vector):
-        self.biosphere_matrix = MatrixBuilder.build_matrix(self.bio_params, self.biosphere_dict, self.technosphere_dict, "row", "col", new_data=vector)
+        self.biosphere_matrix = MatrixBuilder.build_matrix(
+            self.bio_params, self.biosphere_dict, self.technosphere_dict,
+            "row", "col", new_data=vector)
 
     def rebuild_characterization_matrix(self, vector):
-        self.characterization_matrix = MatrixBuilder.build_diagonal_matrix(self.cf_params, self.biosphere_dict, "index", new_data=vector)
+        self.characterization_matrix = MatrixBuilder.build_diagonal_matrix(
+            self.cf_params, self.biosphere_dict, "index", new_data=vector)
 
     def redo_lci(self, demand):
         """Redo LCI with same databases but different demand"""

bw2calc/matrices.py

 
     @classmethod
     def build(cls, dirpath, names, data_label,
-            row_id_label, row_index_label,
-            col_id_label=None, col_index_label=None,
-            row_dict=None, col_dict=None, one_d=False):
+              row_id_label, row_index_label,
+              col_id_label=None, col_index_label=None,
+              row_dict=None, col_dict=None, one_d=False):
         """Build a sparse matrix from NumPy structured array(s).
 
         This method does the following:
         if not row_dict:
             row_dict = cls.build_dictionary(array[row_id_label])
         cls.add_matrix_indices(array[row_id_label], array[row_index_label],
-            row_dict)
+                               row_dict)
         if one_d:
             # Eliminate references to row data which isn't used;
             # Unused data remains MAX_INT_32 values because it isn't mapped
             if not col_dict:
                 col_dict = cls.build_dictionary(array[col_id_label])
             cls.add_matrix_indices(array[col_id_label],
-                array[col_index_label], col_dict)
-            matrix = cls.build_matrix(array, row_dict, col_dict,
-                row_index_label, col_index_label, data_label)
+                                   array[col_index_label], col_dict)
+            matrix = cls.build_matrix(
+                array, row_dict, col_dict, row_index_label, col_index_label,
+                data_label)
         return array, row_dict, col_dict, matrix
 
     @classmethod
     def build_matrix(cls, array, row_dict, col_dict, row_index_label,
-            col_index_label, data_label=None, new_data=None):
+                     col_index_label, data_label=None, new_data=None):
         """Build sparse matrix."""
         vector = array[data_label] if new_data is None else new_data
         assert vector.shape[0] == array.shape[0], "Incompatible data & indices"
 
     @classmethod
     def build_diagonal_matrix(cls, array, row_dict, index_label,
-            data_label=None, new_data=None):
+                              data_label=None, new_data=None):
         """Build diagonal sparse matrix."""
         return cls.build_matrix(array, row_dict, row_dict, index_label, index_label, data_label)
 
             np.hstack((
                 np.where(array['type'] == TYPE_DICTIONARY["technosphere"])[0],
                 np.where(array['type'] == TYPE_DICTIONARY["production"])[0]
-                ))
-            ]
+            ))
+        ]
         bio_array = array[np.where(array['type'] == TYPE_DICTIONARY["biosphere"])]
         tech_dict = cls.build_dictionary(np.hstack((
             tech_array['input'],
             tech_array['output'],
             bio_array['output']
-            )))
+        )))
         bio_dict = cls.build_dictionary(bio_array["input"])
         cls.add_matrix_indices(tech_array['input'], tech_array['row'],
-            tech_dict)
+                               tech_dict)
         cls.add_matrix_indices(tech_array['output'], tech_array['col'],
-            tech_dict)
+                               tech_dict)
         cls.add_matrix_indices(bio_array['input'], bio_array['row'], bio_dict)
         cls.add_matrix_indices(bio_array['output'], bio_array['col'], tech_dict)
         technosphere = cls.build_technosphere_matrix(tech_array, tech_dict)
     @classmethod
     def get_technosphere_inputs_mask(cls, array):
         """Get mask of technosphere inputs from ``array``"""
-        return np.where(array["type"] == \
-            TYPE_DICTIONARY["technosphere"])
+        return np.where(array["type"] ==
+                        TYPE_DICTIONARY["technosphere"])
 
     @classmethod
     def fix_supply_use(cls, array, vector):

bw2calc/monte_carlo.py

 # -*- coding: utf-8 -*
 from __future__ import division
 from .lca import LCA
-from .matrices import MatrixBuilder
-from .matrices import TechnosphereBiosphereMatrixBuilder as TBMBuilder
 from scipy.sparse.linalg import iterative, spsolve
 from stats_toolkit.random import MCRandomNumberGenerator
 import itertools
 import multiprocessing
-import numpy as np
 
 
 class MonteCarloLCA(LCA):
     def __init__(self, demand, method=None, iter_solver=iterative.cgs,
-            seed=None, *args, **kwargs):
+                 seed=None, *args, **kwargs):
         super(MonteCarloLCA, self).__init__(demand, method=method, *args,
-            **kwargs)
+                                            **kwargs)
         self.seed = seed
         self.iter_solver = iter_solver
         self.guess = None
         return self.score
 
     def solve_linear_system(self):
-        if not self.iter_solver or self.guess == None:
+        if not self.iter_solver or self.guess is None:
             self.guess = spsolve(
                 self.technosphere_matrix,
                 self.demand_array)
 class ComparativeMonteCarlo(LCA):
     """First draft approach at comparative LCA"""
     def __init__(self, demands, method=None, iter_solver=iterative.cgs,
-            seed=None, *args, **kwargs):
+                 seed=None, *args, **kwargs):
         self.demands = demands
         # Get all possibilities for database retrieval
         demand_all = demands[0].copy()
         return results
 
     def solve_linear_system(self):
-        if not self.iter_solver or self.guess == None:
+        if not self.iter_solver or self.guess is None:
             self.guess = spsolve(
                 self.technosphere_matrix,
                 self.demand_array)
 
 class ParallelMonteCarlo(object):
     """Split a Monte Carlo calculation into parallel jobs"""
-    def __init__(self, demand, method, iterations=1000, chunk_size=None, cpus=None):
+    def __init__(self, demand, method, iterations=1000, chunk_size=None,
+                 cpus=None):
         self.demand = demand
         self.method = method
         if chunk_size:
             self.chunk_size = (iterations // self.num_jobs) + 1
 
     def calculate(self):
-        pool = multiprocessing.Pool(processes=max(multiprocessing.cpu_count() - 1, 1))
+        pool = multiprocessing.Pool(processes=max(
+            multiprocessing.cpu_count() - 1, 1))
         results = [pool.apply_async(single_worker, (self.demand, self.method,
-            self.chunk_size)) for x in xrange(self.num_jobs)]
+                   self.chunk_size)) for x in xrange(self.num_jobs)]
         pool.close()
         pool.join()  # Blocks until calculation is finished
         return list(itertools.chain(*[x.get() for x in results]))
     def calculate(self):
         pool = multiprocessing.Pool(processes=multiprocessing.cpu_count() - 1)
         results = [pool.apply_async(multi_worker, (self.demands, self.method)
-            ) for x in xrange(self.iterations)]
+                                    ) for x in xrange(self.iterations)]
         pool.close()
         pool.join()  # Blocks until calculation is finished
         return self.merge_dictionaries(*[x.get() for x in results])

bw2calc/simple_regionalized.py

         regionalized_dict = {}
         for index in vector.shape[0]:
             regionalized_dict.setdefault(
-                int(self.cf_params["geo"][index]), []).append(
-                (int(self.cf_params["index"][index]), vector[index]))
+                int(self.cf_params["geo"][index]), []).append((
+                    int(self.cf_params["index"][index]), vector[index]))
         for key, data in regionalized_dict.iteritems():
             regionalized_dict[key] = (
-            np.array([x[0] for x in regionalized_dict]),
-            np.array([x[1] for x in regionalized_dict]))
+                np.array([x[0] for x in regionalized_dict]),
+                np.array([x[1] for x in regionalized_dict]))
         # Get location codes for technosphere processes
         tech_columns, tech_param_indices = np.unique(
             self.tech_params["col"], return_index=True)
         self.characterization_matrix = sparse.coo_matrix(
             (np.hstack(cfs), (np.hstack(rows), np.hstack(cols))),
             (len(self.biosphere_dict), len(self.technosphere_dict))
-            ).tocsr()
+        ).tocsr()
 
     def lcia_calculation(self):
         """Multiply the characterization matrix by the life cycle inventory. Uses ``.multiply`` for point-wise multiplication, as both matrices have the same dimensions."""
 def load_arrays(dirpath, names):
     return np.hstack([pickle.load(open(os.path.join(
         dirpath, "processed", "%s.pickle" % name), "rb")
-        ) for name in names])
+    ) for name in names])