Chris Mutel avatar Chris Mutel committed 3994d3e

Fixes to Brightway claculator (doesn't work against latest Brightway)

Comments (0)

Files changed (1)

 from brightway.core.utils.safe_hash import generate_safe_hash
 from brightway.apps.impact_assessment.models import Weighting
 from brightway.core.caching.cache_interface import CacheInterface
-from brightway.core.regionalize.amoeba import AMOEBA
+# from brightway.core.regionalize.amoeba import AMOEBA
 from brightway.core.caching.errors import CacheError
 
 from hashlib import md5 as universal_hash
 from numpy import array
-from amoeba import DjangoAMOEBA
+from amoeba import DjangoAMOEBA as AMOEBA
 from ac_base import AutocorrelationBase
 from brightway.apps.geo import WORLD
 from shapefile import ShapefileWriter
         self.process = process
         self.method = method
         self.qs = Weighting.objects.filter(process=process, method=method
-            ).exclude(geography=WORLD())
+            ).exclude(geography=WORLD()).exclude(amount__lte=0)
         if not self.qs.exists():
             raise ValueError("The selected process and method have no" + \
                 " regionalized weightings")
         self.reverse = dict([(value, key) for key, value in self.mapping.\
             iteritems()])
         self.W = self.weighting_matrix()
-        self.local = pysal.Moran_Local(self.variable.data, self.W)
+        # self.local = pysal.Moran_Local(self.variable.data, self.W)
 
-    def get_variable(self, ds):
-        return array([x.amount for x in ds])
+    # def get_variable(self, ds):
+    #     return array([x.amount for x in ds])
 
     def construct_data_source(self, ds):
         """Parse a Django GeoQuerySet of Brightway Weightings"""
         self.method = ds[0].method
         return ds
 
-    # def get_variable(self):
-    #     mapping = dict([(key, index) for index, key in enumerate( 
-    #         self.qs.values_list('geography_id', flat=True).distinct())])
-    #     variable = OneDimensionalArrayProxy(zeros(len(mapping)), mapping)
-    #     for obj in self.qs:
-    #         variable[obj.geography_id] = obj.amount
-    #     return mapping, variable
+    def get_variable(self):
+        mapping = dict([(key, index) for index, key in enumerate( 
+            self.qs.values_list('geography_id', flat=True).distinct())])
+        variable = OneDimensionalArrayProxy(zeros(len(mapping)), mapping)
+        for obj in self.qs:
+            variable[obj.geography_id] = obj.amount
+        return mapping, variable
 
-    def weighting_values(self, qs):
+    def weighting_values(self, qs=None):
         """Build a pysal spatial weights matrix."""
+        if not qs:
+            qs = self.qs
         amoeba = AMOEBA(qs).go()
         # AMOEBA produces results like: {geo1: {geo2: weight, geo3: weight}}. 
         # Need to transform to: {geo1: [geo2, geo3]}, {geo1: [weight, weight]}
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.