Skip to content

Commit

Permalink
clamp season to years with data
Browse files Browse the repository at this point in the history
If there is no data for the given season, use data from the closest
season instead.
  • Loading branch information
undefx committed Mar 22, 2018
1 parent 5a0f29d commit 9e339ef
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/geo/populations.py
Expand Up @@ -224,8 +224,10 @@
},
}

first_season, last_season = min(population_weights), max(population_weights)

def get_population_weight(location, season=None):

def get_population_weight(location, season=last_season):
"""
Return the population weight of the given location, relative to the US
nationally.
Expand All @@ -243,12 +245,11 @@ def get_population_weight(location, season=None):
output:
- the fraction of the US population contained within the given location
"""
if not season:
season = max(population_weights)
season = max(min(season, last_season), first_season)
return population_weights[season][location]


def get_population(location, season=None):
def get_population(location, season=last_season):
"""
Return the approximate population of the given location. The returned value
is rounded to the nearest integer and is based on the assumption that the US
Expand Down
14 changes: 14 additions & 0 deletions tests/geo/test_populations.py
Expand Up @@ -38,6 +38,20 @@ def test_season_defaults_to_latest(self):
val2 = get_population_weight(location)
self.assertEqual(val1, val2)

def test_season_clamped_to_first(self):
location = 'nc'
season = min(population_weights)
val1 = get_population_weight(location, season)
val2 = get_population_weight(location, season - 1)
self.assertEqual(val1, val2)

def test_season_clamped_to_last(self):
location = 'sc'
season = max(population_weights)
val1 = get_population_weight(location, season)
val2 = get_population_weight(location, season + 1)
self.assertEqual(val1, val2)

def test_population_is_integer(self):
pop = get_population('vi')
self.assertTrue(isinstance(pop, int))

0 comments on commit 9e339ef

Please sign in to comment.