From 03d8d2eb6058dbc9c2bcbf1d4c3833db1cf4529c Mon Sep 17 00:00:00 2001 From: kvyh Date: Mon, 8 Aug 2016 17:16:38 -0700 Subject: [PATCH 1/8] improved docstring and generalised _rescale_minmax --- astroplan/constraints.py | 51 ++++++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 20 deletions(-) diff --git a/astroplan/constraints.py b/astroplan/constraints.py index 364008dc..5cfc73de 100644 --- a/astroplan/constraints.py +++ b/astroplan/constraints.py @@ -345,7 +345,7 @@ def compute_constraint(self, times, observer, targets): mi = 1 if self.min is None else self.min # we reverse order so that airmass close to 1/min is good - return _rescale_airmass(secz, mi, mx) + return _rescale_minmax(secz, mi, mx, better_than=0, worse_than=0) class AtNightConstraint(Constraint): @@ -997,24 +997,35 @@ def observability_table(constraints, observer, targets, times=None, return tab -def _rescale_minmax(vals, min_val, max_val): - """ Rescale altitude into an observability score.""" - rescaled = (vals - min_val) / (max_val - min_val) - below = rescaled < 0 - above = rescaled > 1 - rescaled[below] = 0 - rescaled[above] = 1 - - return rescaled - +def _rescale_minmax(vals, worst_val, best_val, better_than=1, worse_than=0): + """ + rescales the input ``vals`` between 0 and 1 + Parameters + ---------- + vals : array of values + worst_val : value + worst acceptable value (rescales to 0) + best_val : value + best value cared about (rescales to 1) + better_than : 0 or 1 + What is returned for ``vals`` beyond than ``best_val`` + worse_than : 0 or 1 + What is returned for ``vals`` beyond than ``worst_val`` -def _rescale_airmass(vals, min_val, max_val): - """ Rescale airmass into an observability score.""" - rescaled = (vals - min_val) / (max_val - min_val) - below = rescaled < 0 - above = rescaled > 1 - # In both cases, we want out-of-range airmasses to return a 0 score - rescaled[below] = 1 - rescaled[above] = 1 + Returns + ------- + array of floats between 0 and 1 inclusive rescaled so that + ``vals`` equal to ``worst_val`` equal 0 and those equal to + ``best_val`` equal 1 + """ + rescaled = (vals - worst_val) / (best_val - worst_val) + if best_val - worst_val > 0: + worse = vals < worst_val + better = vals > best_val + else: + worse = vals < best_val + better = vals > worst_val + rescaled[worse] = worse_than + rescaled[better] = better_than - return 1 - rescaled + return rescaled From 0ddf6811a6ab29340c11ceb4d1bf47183f8c9c76 Mon Sep 17 00:00:00 2001 From: kvyh Date: Mon, 8 Aug 2016 17:28:17 -0700 Subject: [PATCH 2/8] fixed a few typos and formatting issues --- astroplan/constraints.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/astroplan/constraints.py b/astroplan/constraints.py index 5cfc73de..4385305c 100644 --- a/astroplan/constraints.py +++ b/astroplan/constraints.py @@ -345,7 +345,7 @@ def compute_constraint(self, times, observer, targets): mi = 1 if self.min is None else self.min # we reverse order so that airmass close to 1/min is good - return _rescale_minmax(secz, mi, mx, better_than=0, worse_than=0) + return _rescale_minmax(secz, mi, mx, better_than=0) class AtNightConstraint(Constraint): @@ -1000,17 +1000,19 @@ def observability_table(constraints, observer, targets, times=None, def _rescale_minmax(vals, worst_val, best_val, better_than=1, worse_than=0): """ rescales the input ``vals`` between 0 and 1 + Parameters ---------- vals : array of values + the values that need to be rescaled to be between 0 and 1 worst_val : value worst acceptable value (rescales to 0) best_val : value best value cared about (rescales to 1) better_than : 0 or 1 - What is returned for ``vals`` beyond than ``best_val`` + what is returned for ``vals`` beyond the ``best_val`` worse_than : 0 or 1 - What is returned for ``vals`` beyond than ``worst_val`` + what is returned for ``vals`` beyond the ``worst_val`` Returns ------- @@ -1029,3 +1031,5 @@ def _rescale_minmax(vals, worst_val, best_val, better_than=1, worse_than=0): rescaled[better] = better_than return rescaled + +_rescale_minmax() \ No newline at end of file From 1bee05bcbcd58a0fb0e0c21777be4d4020164283 Mon Sep 17 00:00:00 2001 From: kvyh Date: Mon, 8 Aug 2016 19:04:42 -0700 Subject: [PATCH 3/8] removed an extra line --- astroplan/constraints.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/astroplan/constraints.py b/astroplan/constraints.py index 4385305c..0dd75237 100644 --- a/astroplan/constraints.py +++ b/astroplan/constraints.py @@ -1031,5 +1031,3 @@ def _rescale_minmax(vals, worst_val, best_val, better_than=1, worse_than=0): rescaled[better] = better_than return rescaled - -_rescale_minmax() \ No newline at end of file From 0af7c43eb45ebb01fcf447d10648ebfebfc70a4b Mon Sep 17 00:00:00 2001 From: kvyh Date: Tue, 9 Aug 2016 11:25:41 -0700 Subject: [PATCH 4/8] added test and examples --- astroplan/constraints.py | 26 ++++++++++++++++++++++++-- astroplan/tests/test_constraints.py | 13 ++++++++++++- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/astroplan/constraints.py b/astroplan/constraints.py index 0dd75237..8bbdb5a1 100644 --- a/astroplan/constraints.py +++ b/astroplan/constraints.py @@ -1019,14 +1019,36 @@ def _rescale_minmax(vals, worst_val, best_val, better_than=1, worse_than=0): array of floats between 0 and 1 inclusive rescaled so that ``vals`` equal to ``worst_val`` equal 0 and those equal to ``best_val`` equal 1 + + Examples + -------- + rescale altitude = [20, 30, 40, 45, 55, 70] to between + 0 and 1, with the best at 60->1 and worst at 35->0. 0 + below 35 and 1 above 60. + >>> from astroplan.constraints import _rescale_minmax + >>> import numpy as np + >>> airmasses = np.array([20, 30, 40, 45, 55, 70]) + >>> _rescale_minmax(airmasses, 35, 60) + array([ 0. , 0. , 0.2, 0.4, 0.8, 1. ]) + + rescale airmasses = [1,1,1.5,2,2,2,3,3,3,2,2,1] to between + 0 and 1, with the best at 1->1 and worst at 2.25->0. 0 beyond + 1 and 2.25. + >>> from astroplan.constraints import _rescale_minmax + >>> import numpy as np + >>> airmasses = np.array([1,1,1.5,2,2,2,3,3,3,2,2,1,0]) + >>> _rescale_minmax(airmasses, 2.25, 1, better_than = 0) + array([ 1. , 1. , 0.6, 0.2, 0.2, 0.2, 0. , 0. , 0. , + 0.2, 0.2, 1. , 0. ]) + """ rescaled = (vals - worst_val) / (best_val - worst_val) if best_val - worst_val > 0: worse = vals < worst_val better = vals > best_val else: - worse = vals < best_val - better = vals > worst_val + worse = vals > worst_val + better = vals < best_val rescaled[worse] = worse_than rescaled[better] = better_than diff --git a/astroplan/tests/test_constraints.py b/astroplan/tests/test_constraints.py index 6c26145b..24c9dec1 100644 --- a/astroplan/tests/test_constraints.py +++ b/astroplan/tests/test_constraints.py @@ -15,7 +15,8 @@ is_observable, is_always_observable, observability_table, time_grid_from_range, SunSeparationConstraint, MoonSeparationConstraint, MoonIlluminationConstraint, - TimeConstraint, LocalTimeConstraint, months_observable) + TimeConstraint, LocalTimeConstraint, months_observable, + _rescale_minmax) APY_LT104 = not minversion('astropy','1.0.4') @@ -369,6 +370,16 @@ def test_months_observable(): assert months == should_be +def test_rescale_minmax(): + a = np.array([2]) + rescaled = np.zeros(5) + rescaled[0] = (_rescale_minmax(a, 6, 1))[0] + rescaled[1] = (_rescale_minmax(a, 1, 6))[0] + rescaled[2] = (_rescale_minmax(a, 0, 1))[0] + rescaled[3] = (_rescale_minmax(a, 1, 0))[0] + rescaled[4] = (_rescale_minmax(a, 0, 1, better_than=0))[0] + assert all(np.array([0.8, 0.2, 1, 0, 0]) == rescaled) + constraint_tests = [ AltitudeConstraint(), AirmassConstraint(2), From 5aa82ed97b95d22fe4f09e297ba639d8a594a295 Mon Sep 17 00:00:00 2001 From: kvyh Date: Tue, 9 Aug 2016 12:42:04 -0700 Subject: [PATCH 5/8] reworded and simplified examples --- astroplan/constraints.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/astroplan/constraints.py b/astroplan/constraints.py index 8bbdb5a1..ea81f0a6 100644 --- a/astroplan/constraints.py +++ b/astroplan/constraints.py @@ -1022,24 +1022,24 @@ def _rescale_minmax(vals, worst_val, best_val, better_than=1, worse_than=0): Examples -------- - rescale altitude = [20, 30, 40, 45, 55, 70] to between - 0 and 1, with the best at 60->1 and worst at 35->0. 0 - below 35 and 1 above 60. + rescale an array of altitudes to be between 0 and 1, + with the best (60) going to 1 and worst (35) going to + 0. For values outside the range, the rescale should + return 0 below 35 and 1 above 60. >>> from astroplan.constraints import _rescale_minmax >>> import numpy as np - >>> airmasses = np.array([20, 30, 40, 45, 55, 70]) - >>> _rescale_minmax(airmasses, 35, 60) + >>> altitudes = np.array([20, 30, 40, 45, 55, 70]) + >>> _rescale_minmax(altitudes, 35, 60) array([ 0. , 0. , 0.2, 0.4, 0.8, 1. ]) - rescale airmasses = [1,1,1.5,2,2,2,3,3,3,2,2,1] to between - 0 and 1, with the best at 1->1 and worst at 2.25->0. 0 beyond - 1 and 2.25. + rescale airmasses to between 0 and 1, with the best (1) + and worst (2.25). All values outside the range should + return 0. >>> from astroplan.constraints import _rescale_minmax >>> import numpy as np - >>> airmasses = np.array([1,1,1.5,2,2,2,3,3,3,2,2,1,0]) + >>> airmasses = np.array([1, 1.5, 2, 3, 0]) >>> _rescale_minmax(airmasses, 2.25, 1, better_than = 0) - array([ 1. , 1. , 0.6, 0.2, 0.2, 0.2, 0. , 0. , 0. , - 0.2, 0.2, 1. , 0. ]) + array([ 1. , 0.6, 0.2, 0. , 0. ]) """ rescaled = (vals - worst_val) / (best_val - worst_val) From 23ed9f6fc34679ae1e07a758ef67f49e76521ce9 Mon Sep 17 00:00:00 2001 From: kvyh Date: Fri, 12 Aug 2016 15:59:36 -0700 Subject: [PATCH 6/8] changing rescale_minmax to not be private --- astroplan/constraints.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/astroplan/constraints.py b/astroplan/constraints.py index ea81f0a6..4242bae3 100644 --- a/astroplan/constraints.py +++ b/astroplan/constraints.py @@ -28,7 +28,8 @@ "is_observable", "is_always_observable", "time_grid_from_range", "SunSeparationConstraint", "MoonSeparationConstraint", "MoonIlluminationConstraint", "LocalTimeConstraint", "Constraint", - "TimeConstraint", "observability_table", "months_observable"] + "TimeConstraint", "observability_table", "months_observable", + "rescale_minmax"] def _get_altaz(times, observer, targets, @@ -286,7 +287,7 @@ def compute_constraint(self, times, observer, targets): uppermask = alt <= self.max return lowermask & uppermask else: - return _rescale_minmax(alt, self.min, self.max) + return rescale_minmax(alt, self.min, self.max) class AirmassConstraint(AltitudeConstraint): @@ -345,7 +346,7 @@ def compute_constraint(self, times, observer, targets): mi = 1 if self.min is None else self.min # we reverse order so that airmass close to 1/min is good - return _rescale_minmax(secz, mi, mx, better_than=0) + return rescale_minmax(secz, mi, mx, better_than=0) class AtNightConstraint(Constraint): @@ -997,9 +998,9 @@ def observability_table(constraints, observer, targets, times=None, return tab -def _rescale_minmax(vals, worst_val, best_val, better_than=1, worse_than=0): +def rescale_minmax(vals, worst_val, best_val, better_than=1, worse_than=0): """ - rescales the input ``vals`` between 0 and 1 + rescales an input array ``vals`` between zero and one Parameters ---------- @@ -1026,19 +1027,19 @@ def _rescale_minmax(vals, worst_val, best_val, better_than=1, worse_than=0): with the best (60) going to 1 and worst (35) going to 0. For values outside the range, the rescale should return 0 below 35 and 1 above 60. - >>> from astroplan.constraints import _rescale_minmax + >>> from astroplan.constraints import rescale_minmax >>> import numpy as np >>> altitudes = np.array([20, 30, 40, 45, 55, 70]) - >>> _rescale_minmax(altitudes, 35, 60) + >>> rescale_minmax(altitudes, 35, 60) array([ 0. , 0. , 0.2, 0.4, 0.8, 1. ]) rescale airmasses to between 0 and 1, with the best (1) and worst (2.25). All values outside the range should return 0. - >>> from astroplan.constraints import _rescale_minmax + >>> from astroplan.constraints import rescale_minmax >>> import numpy as np >>> airmasses = np.array([1, 1.5, 2, 3, 0]) - >>> _rescale_minmax(airmasses, 2.25, 1, better_than = 0) + >>> rescale_minmax(airmasses, 2.25, 1, better_than = 0) array([ 1. , 0.6, 0.2, 0. , 0. ]) """ From c2ec0ce61315840dcfbace23377b16f1700ac5aa Mon Sep 17 00:00:00 2001 From: kvyh Date: Sat, 13 Aug 2016 08:12:03 -0700 Subject: [PATCH 7/8] updated the test to the new function name --- astroplan/tests/test_constraints.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/astroplan/tests/test_constraints.py b/astroplan/tests/test_constraints.py index 24c9dec1..184aa9c4 100644 --- a/astroplan/tests/test_constraints.py +++ b/astroplan/tests/test_constraints.py @@ -16,7 +16,7 @@ time_grid_from_range, SunSeparationConstraint, MoonSeparationConstraint, MoonIlluminationConstraint, TimeConstraint, LocalTimeConstraint, months_observable, - _rescale_minmax) + rescale_minmax) APY_LT104 = not minversion('astropy','1.0.4') @@ -373,11 +373,11 @@ def test_months_observable(): def test_rescale_minmax(): a = np.array([2]) rescaled = np.zeros(5) - rescaled[0] = (_rescale_minmax(a, 6, 1))[0] - rescaled[1] = (_rescale_minmax(a, 1, 6))[0] - rescaled[2] = (_rescale_minmax(a, 0, 1))[0] - rescaled[3] = (_rescale_minmax(a, 1, 0))[0] - rescaled[4] = (_rescale_minmax(a, 0, 1, better_than=0))[0] + rescaled[0] = (rescale_minmax(a, 6, 1))[0] + rescaled[1] = (rescale_minmax(a, 1, 6))[0] + rescaled[2] = (rescale_minmax(a, 0, 1))[0] + rescaled[3] = (rescale_minmax(a, 1, 0))[0] + rescaled[4] = (rescale_minmax(a, 0, 1, better_than=0))[0] assert all(np.array([0.8, 0.2, 1, 0, 0]) == rescaled) constraint_tests = [ From a4653b44dfb8ed327b5c55ee60d26d76ca21a225 Mon Sep 17 00:00:00 2001 From: kvyh Date: Mon, 15 Aug 2016 12:53:34 -0700 Subject: [PATCH 8/8] removed empty line --- astroplan/constraints.py | 1 - 1 file changed, 1 deletion(-) diff --git a/astroplan/constraints.py b/astroplan/constraints.py index 4242bae3..9aba194a 100644 --- a/astroplan/constraints.py +++ b/astroplan/constraints.py @@ -1041,7 +1041,6 @@ def rescale_minmax(vals, worst_val, best_val, better_than=1, worse_than=0): >>> airmasses = np.array([1, 1.5, 2, 3, 0]) >>> rescale_minmax(airmasses, 2.25, 1, better_than = 0) array([ 1. , 0.6, 0.2, 0. , 0. ]) - """ rescaled = (vals - worst_val) / (best_val - worst_val) if best_val - worst_val > 0: