Skip to content

Commit

Permalink
more tests on log cdf
Browse files Browse the repository at this point in the history
  • Loading branch information
grantbuster committed Jan 7, 2024
1 parent 5fec663 commit 8f7cb01
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
4 changes: 2 additions & 2 deletions tests/test_sam_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,13 +586,13 @@ def test_bias_correct_wind_qdm():
assert np.allclose(bc_ws, base_ws)

res = WindResource.preload_SAM(h5, sites, hub_heights)
params = list(0.1*np.ones(10))
params = sorted(np.random.uniform(10, 20, 10))
bc['params_oh'] = json.dumps(params)
bc.loc[0, 'params_oh'] = json.dumps(base_params)
res.bias_correct(bc)
bc_ws = res._res_arrays['windspeed']
assert np.allclose(bc_ws[:, 0], base_ws[:, 0])
assert ((bc_ws[:, 1:] > base_ws[:, 1:]).sum() / bc_ws[:, 1:].size) < 1e-4
assert ((bc_ws[:, 1:] != base_ws[:, 1:]).sum() / bc_ws[:, 1:].size) > 0.99


def test_bias_correct_irrad_qdm():
Expand Down
31 changes: 28 additions & 3 deletions tests/test_temporal_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,13 +281,38 @@ def test_weighted_circular_means(weights):
assert np.allclose(truth, test_stats[name].values, rtol=0, atol=0.01), msg


def test_cdf(n=21):
@pytest.mark.parametrize("n_samples", [21, 50])
def test_cdf_linear(n_samples):
"""Test the CDF function which gets evenly spaced values (in quantile
space) defining the empirical CDF of a dataset"""
data = np.random.normal(0, 1, (1000,))
x = cdf(data, n=n, decimals=None)
x = cdf(data, n_samples=n_samples, decimals=None)

quantiles = np.linspace(0, 1, n)
assert x[0] == data.min()
assert x[-1] == data.max()

quantiles = np.linspace(0, 1, n_samples)
assert quantiles[0] == 0
assert quantiles[-1] == 1

for i, q in enumerate(quantiles):
assert np.allclose(np.percentile(data, 100 * q), x[i])


@pytest.mark.parametrize(("n_samples", "log_base"), [(21, 10), (50, 2)])
def test_cdf_invlog(n_samples, log_base):
"""Test the CDF function which gets inverse-log-spaced spaced values
(in quantile space) defining the empirical CDF of a dataset"""
data = np.random.normal(0, 1, (10000,))
x = cdf(data, n_samples=n_samples, decimals=None, sampling='invlog',
log_base=log_base)

assert x[0] == data.min()
assert x[-1] == data.max()

quantiles = np.logspace(0, 1, n_samples, base=log_base)
quantiles = (quantiles - 1) / (log_base - 1)
quantiles = np.array(sorted(1 - quantiles))
assert quantiles[0] == 0
assert quantiles[-1] == 1

Expand Down

0 comments on commit 8f7cb01

Please sign in to comment.