def resolution(x, zero=True): """ Compute the resolution of a data vector Resolution is smallest non-zero distance between adjacent values Parameters ---------- x : 1D array-like zero : Boolean Whether to include zero values in the computation Result ------ res : resolution of x If x is an integer array, then the resolution is 1 """ x = np.asarray(x) # (unsigned) integers or an effective range of zero _x = x[~pd.isna(x)] _x = (x.min(), x.max()) if x.dtype.kind in ('i', 'u') or zero_range(_x): return 1 x = np.unique(x) if zero: x = np.unique(np.hstack([0, x])) return np.min(np.diff(np.sort(x)))
def resolution(x, zero=True): """ Compute the resolution of a data vector Resolution is smallest non-zero distance between adjacent values Parameters ---------- x : 1D array-like zero : Boolean Whether to include zero values in the computation Result ------ res : resolution of x If x is an integer array, then the resolution is 1 """ x = np.asarray(x) # (unsigned) integers or an effective range of zero _x = x[~np.isnan(x)] _x = (x.min(), x.max()) if x.dtype.kind in ('i', 'u') or zero_range(_x): return 1 x = np.unique(x) if zero: x = np.unique(np.hstack([0, x])) return np.min(np.diff(np.sort(x)))
def get_breaks(self, limits=None, strict=False): """ Generate breaks for the axis or legend Parameters ---------- limits : list-like | None If None the self.limits are used They are expected to be in transformed space. strict : bool If True then the breaks gauranteed to fall within the limits. e.g. when the legend uses this method. Returns ------- out : array-like Notes ----- Breaks are calculated in data space and returned in transformed space since all data is plotted in transformed space. """ if limits is None: limits = self.limits # To data space _limits = self.inverse(limits) if self.is_empty(): breaks = [] elif self.breaks is None or self.breaks is False: breaks = [] elif zero_range(_limits): breaks = [_limits[0]] elif is_waive(self.breaks): breaks = self.trans.breaks(_limits) elif callable(self.breaks): breaks = self.breaks(_limits) else: breaks = self.breaks breaks = self.transform(breaks) breaks = np.asarray(breaks) # At this point, any breaks beyond the limits # are kept since they may be used to calculate # minor breaks if strict: cond = (breaks >= limits[0]) & (limits[1] >= breaks) breaks = np.compress(cond, breaks) return breaks
def test_zero_range(): c = np.array eps = np.finfo(float).eps assert zero_range(c((1, 1 + eps))) assert zero_range(c((1, 1 + 99 * eps))) # Crossed the tol threshold # assert zero_range(c((1, 1 + 101 * eps))) is False assert (not zero_range(c((1, 1 + 101 * eps)))) # Changed tol assert (not zero_range(c((1, 1 + 2 * eps)), tol=eps)) # Scaling up or down all the values has no effect since # the values are rescaled to 1 before checking against # the tolerance assert zero_range(100000 * c((1, 1 + eps))) assert (not zero_range(100000 * c((1, 1 + 200 * eps)))) assert zero_range(.00001 * c((1, 1 + eps))) assert (not zero_range(.00001 * c((1, 1 + 200 * eps)))) # NA values assert zero_range((1, np.nan)) # Infinite values assert (not zero_range((1, np.inf))) assert (not zero_range((-np.inf, np.inf))) assert zero_range((np.inf, np.inf)) # Single value assert zero_range(1) assert zero_range([1]) # length greater than 2 with pytest.raises(ValueError): zero_range([1, 2, 3]) # datetime - pandas, cpython x = datetime(2010, 1, 1), datetime(2010, 1, 1) x2 = datetime(2010, 1, 1), datetime(2020, 1, 1) x3 = (pd.Timestamp('2010-01-01', tz='US/Eastern'), pd.Timestamp('2010-01-01', tz='US/Central')) assert (zero_range(x)) assert (not zero_range(x2)) assert (not zero_range(x3)) # datetime - numpy x = np.datetime64(7, 'D'), np.datetime64(7, 'D') x2 = np.datetime64(7, 'D'), np.datetime64(1, 'W') x3 = np.datetime64(7, 'D'), np.datetime64(1, 'D') assert (zero_range(x)) assert (zero_range(x2)) assert (not zero_range(x3)) # timedelta - pandas, cpython x = timedelta(seconds=2010), timedelta(seconds=2010) x2 = (timedelta(seconds=2010, microseconds=90), timedelta(seconds=2010, microseconds=34)) x3 = pd.Timedelta(200, 'D'), pd.Timedelta(203, 'D') assert (zero_range(x)) assert (not zero_range(x2)) assert (not zero_range(x3)) # timedelta - numpy x = np.timedelta64(7, 'D'), np.timedelta64(7, 'D') x2 = np.timedelta64(7, 'D'), np.timedelta64(1, 'W') x3 = np.timedelta64(7, 'D'), np.timedelta64(2, 'D') assert (zero_range(x)) assert (zero_range(x2)) assert (not zero_range(x3)) # branches # assert str(zero_range([4, float('nan')])) == 'nan' assert (not zero_range([4, float('inf')])) with pytest.raises(TypeError): zero_range(['a', 'b'])
def test_zero_range(): c = np.array eps = np.finfo(float).eps assert zero_range(c((1, 1 + eps))) assert zero_range(c((1, 1 + 99 * eps))) # Crossed the tol threshold # assert zero_range(c((1, 1 + 101 * eps))) is False assert(not zero_range(c((1, 1 + 101 * eps)))) # Changed tol assert(not zero_range(c((1, 1 + 2 * eps)), tol=eps)) # Scaling up or down all the values has no effect since # the values are rescaled to 1 before checking against # the tolerance assert zero_range(100000 * c((1, 1 + eps))) assert(not zero_range(100000 * c((1, 1 + 200 * eps)))) assert zero_range(.00001 * c((1, 1 + eps))) assert(not zero_range(.00001 * c((1, 1 + 200 * eps)))) # NA values assert zero_range((1, np.nan)) # Infinite values assert(not zero_range((1, np.inf))) assert(not zero_range((-np.inf, np.inf))) assert zero_range((np.inf, np.inf)) # Single value assert zero_range(1) assert zero_range([1]) # length greater than 2 with pytest.raises(ValueError): zero_range([1, 2, 3]) # datetime - pandas, cpython x = datetime(2010, 1, 1), datetime(2010, 1, 1) x2 = datetime(2010, 1, 1), datetime(2020, 1, 1) x3 = (pd.Timestamp('2010-01-01', tz='US/Eastern'), pd.Timestamp('2010-01-01', tz='US/Central')) assert(zero_range(x)) assert(not zero_range(x2)) assert(not zero_range(x3)) # datetime - numpy x = np.datetime64(7, 'D'), np.datetime64(7, 'D') x2 = np.datetime64(7, 'D'), np.datetime64(1, 'W') x3 = np.datetime64(7, 'D'), np.datetime64(1, 'D') assert(zero_range(x)) assert(zero_range(x2)) assert(not zero_range(x3)) # timedelta - pandas, cpython x = timedelta(seconds=2010), timedelta(seconds=2010) x2 = (timedelta(seconds=2010, microseconds=90), timedelta(seconds=2010, microseconds=34)) x3 = pd.Timedelta(200, 'D'), pd.Timedelta(203, 'D') assert(zero_range(x)) assert(not zero_range(x2)) assert(not zero_range(x3)) # timedelta - numpy x = np.timedelta64(7, 'D'), np.timedelta64(7, 'D') x2 = np.timedelta64(7, 'D'), np.timedelta64(1, 'W') x3 = np.timedelta64(7, 'D'), np.timedelta64(2, 'D') assert(zero_range(x)) assert(zero_range(x2)) assert(not zero_range(x3)) # branches # assert str(zero_range([4, float('nan')])) == 'nan' assert(not zero_range([4, float('inf')])) with pytest.raises(TypeError): zero_range(['a', 'b'])