Skip to content

Commit

Permalink
Merge pull request #81 from GeoDaCenter/update_normalization
Browse files Browse the repository at this point in the history
added normalize type
  • Loading branch information
Logan Noel authored Jun 9, 2019
2 parents e72e3b3 + 029b0c0 commit dee2f0d
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 40 deletions.
12 changes: 9 additions & 3 deletions spatial_access/Models.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,14 +542,14 @@ def _log_category_weight_dict(self, category_weight_dict):
self.logger.info("Using weights: {}".format(presented_weight_dict))

def calculate(self, upper_threshold, category_weight_dict=None,
normalize=False, normalize_type='z_score'):
normalize=False, normalize_type='minmax'):
"""
Args:
category_weight_dict: category_weight_dict: dictionary of {category : [numeric weights]} or None
upper_threshold: time in seconds.
normalize: boolean. If true, results will be normalized
from 0 to 100.
normalize_type: 'z_score'.
normalize_type: 'z_score', 'minmax'
Returns: DataFrame.
Raises:
UnexpectedNormalizeColumnsException
Expand Down Expand Up @@ -630,7 +630,7 @@ def _normalize(self, column, normalize_type):
Normalize results.
Args:
column: which column to normalize.
normalize_type: 'z-score'
normalize_type: 'z-score', 'minmax'
Raises:
UnexpectedEmptyColumnException
Expand All @@ -642,6 +642,12 @@ def _normalize(self, column, normalize_type):
- self.model_results[column].mean()) / self.model_results[column].std()
except ZeroDivisionError:
raise UnexpectedEmptyColumnException(column)
elif normalize_type == 'minmax':
try:
normalize_factor = self.model_results[column].max() - self.model_results[column].min()
except ZeroDivisionError:
raise RuntimeError("column max == column min ({})".format(self.model_results[column].max()))
self.model_results[column] = (self.model_results[column] - self.model_results[column].min()) / normalize_factor
else:
raise UnexpectedNormalizeTypeException(normalize_type)

2 changes: 1 addition & 1 deletion spatial_access/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '0.1.18'
__version__ = '1.0.0'
__author__ = 'Logan Noel (lmnoel)'
__url__ = 'https://github.com/GeoDaCenter/spatial_access'
__license__ = 'GPL'
13 changes: 0 additions & 13 deletions spatial_access/src/CMakeLists.txt

This file was deleted.

21 changes: 0 additions & 21 deletions spatial_access/src/Matrix.cpp

This file was deleted.

14 changes: 12 additions & 2 deletions tests/test_Models.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,8 @@ def test_16(self):
'D': [4, 3, 1],
'C': [1]}
coverage_model.transit_matrix = self.mock_transit_matrix_values(coverage_model.transit_matrix)
coverage_model.calculate(category_weight_dict=category_weight_dict, upper_threshold=700, normalize=True)
coverage_model.calculate(category_weight_dict=category_weight_dict, upper_threshold=700, normalize=True,
normalize_type='z_score')

assert almost_equal(coverage_model.model_results['all_categories_score'].max(), 1.336)
assert almost_equal(coverage_model.model_results['all_categories_score'].min(), -1.336)
Expand Down Expand Up @@ -441,11 +442,20 @@ def test_18(self):
category_weight_dict = {'A': [5, 4, 3, 2, 1],
'D': [4, 3, 1]}
coverage_model.transit_matrix = self.mock_transit_matrix_values(coverage_model.transit_matrix)
coverage_model.calculate(category_weight_dict=category_weight_dict, upper_threshold=200, normalize=['A'])
coverage_model.calculate(category_weight_dict=category_weight_dict, upper_threshold=200, normalize=['A'],
normalize_type='z_score')

assert almost_equal(coverage_model.model_results['A_score'].max(), 2.041)
assert almost_equal(coverage_model.model_results['A_score'].min(), -0.408)

coverage_model.calculate(category_weight_dict=category_weight_dict, upper_threshold=200, normalize=True,
normalize_type='minmax')
assert almost_equal(coverage_model.model_results['A_score'].max(), 1.0)
assert almost_equal(coverage_model.model_results['A_score'].min(), 0.0)
assert almost_equal(coverage_model.model_results['D_score'].max(), 1.0)
assert almost_equal(coverage_model.model_results['D_score'].min(), 0.0)


def test_19(self):
"""
Test the DestFloatingCatchmentAreaModel aggregation.
Expand Down

0 comments on commit dee2f0d

Please sign in to comment.