Skip to content

Commit

Permalink
CompositeIndicator now also handles functions defined with arrays as …
Browse files Browse the repository at this point in the history
…input. This allows the user to pass a prexisting numpy function such as average
  • Loading branch information
crisjf committed May 15, 2020
1 parent 1e1621c commit 04f1450
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ Let's create an indicator that averages Innovation Potential, Mobility Inmpact,

```
from toolbox import Handler, CompositeIndicator
from random_indicator import RandomIndicator
from examples import RandomIndicator
def innovation_average(indicator_values):
avg = (indicator_values['Innovation Potential']+indicator_values['Mobility Impact']+indicator_values['Economic Impact'])/3
Expand All @@ -175,14 +175,27 @@ avg_I = CompositeIndicator(innovation_average,name='Composite')
H.add_indicators([R,avg_I])
```

You can also pass it a pre-existing function, such as `np.mean`.
```
from toolbox import Handler, CompositeIndicator
from examples import RandomIndicator
import numpy as np
H = Handler('corktown')
R = RandomIndicator()
avg_I = CompositeIndicator(np.mean,selected_indicators=['Innovation Potential','Mobility Impact','Economic Impact'],name='Composite')
H.add_indicators([R,avg_I])
```


## Custom Composite indicator

Let's create an indicator that averages Innovation Potential, Mobility Inmpact, and Economic Impact.
First, we load the RandomIndicator and pass it to a Handler.

```
from toolbox import Handler, CompositeIndicator
from random_indicator import RandomIndicator
from examples import RandomIndicator
H = Handler('corktown')
R = RandomIndicator()
Expand Down
3 changes: 1 addition & 2 deletions examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ def return_indicator(self, geogrid_data):


from numpy import mean
from numpy.random import random
class Noise(Indicator):
'''
Example of Noise heatmap indicator for points centered in each grid cell.
Expand All @@ -84,7 +83,7 @@ def return_indicator(self, geogrid_data):
lat,lon = zip(*cell['geometry']['coordinates'][0])
lat,lon = mean(lat),mean(lon)
feature['geometry'] = {'coordinates': [lat,lon],'type': 'Point'}
feature['properties'] = {self.name:random()}
feature['properties'] = {self.name:random.random()}
features.append(feature)
out = {'type':'FeatureCollection','features':features}
return out
Expand Down
23 changes: 16 additions & 7 deletions toolbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import json
import Geohash
import joblib
import numpy as np
from warnings import warn
from time import sleep
from collections import defaultdict
Expand Down Expand Up @@ -693,10 +694,18 @@ def load_module(self):


class CompositeIndicator(Indicator):
def setup(self,compose_function,*args,**kwargs):
self.compose_function = compose_function
self.is_composite = True

def return_indicator(self, indicator_values):
value = self.compose_function(indicator_values)
return [{'name': self.name, 'value': value, 'viz_type': self.viz_type}]
def setup(self,compose_function,selected_indicators=[],*args,**kwargs):
self.compose_function = compose_function
self.is_composite = True
self.selected_indicators = selected_indicators

def return_indicator(self, indicator_values):
if len(self.selected_indicators)!=0:
indicator_values = {k:indicator_values[k] for k in indicator_values if k in self.selected_indicators}
try:
value = self.compose_function(indicator_values)
except:
indicator_values = np.array([v for v in indicator_values.values()])
value = self.compose_function(indicator_values)
return [{'name': self.name, 'value': value, 'viz_type': self.viz_type}]

0 comments on commit 04f1450

Please sign in to comment.