Skip to content

Commit

Permalink
Issue413 sampletau (blue-yonder#500)
Browse files Browse the repository at this point in the history
* refactoring drifbif_simulation.py
* remove test_tsfresh_baseline_dataset
* update threshold in test_ratio to 0.0006, refactoring
  • Loading branch information
MaxBenChrist authored Feb 25, 2019
1 parent 7d52283 commit 5e887bb
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 80 deletions.
38 changes: 18 additions & 20 deletions tests/integrations/examples/test_driftbif_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,45 +14,44 @@ def test_intrinsic_velocity_at_default_bifurcation_point(self):
"""
The intrinsic velocity of a dissipative soliton at the Drift bifurcation point is zero.
"""
ds = velocity(tau=1.0/0.3)
ds = velocity(tau=1.0 / 0.3)
self.assertEqual(ds.deterministic, 0.0)

def test_relaxation_dynamics(self):
"""
Test accuracy of integrating the deterministic dynamics [6, p. 116]
"""
ds = velocity(tau=1.01/0.3, R=0)
ds = velocity(tau=1.01 / 0.3, R=0)
v0 = 1.01 * ds.deterministic

Nt = 100 # Number of time steps
Nt = 100 # Number of time steps
v = ds.simulate(Nt, v0=np.array([v0, 0.]))

k3t = ds.kappa_3 * ds.tau
k3st = ds.kappa_3**2 * ds.tau
k3st = ds.kappa_3 ** 2 * ds.tau
a0 = v0 / ds.kappa_3
acceleration = lambda t: ds.kappa_3 * (a0 * np.sqrt(k3t - 1) * np.exp(k3st * t) /
np.sqrt(np.exp(2.0 * k3st * t) * ds.Q * a0**2 +
np.exp(2.0 * ds.kappa_3 * t) * (k3t - 1 - ds.Q * a0**2)))
np.sqrt(np.exp(2.0 * k3st * t) * ds.Q * a0 ** 2 +
np.exp(2.0 * ds.kappa_3 * t) * (k3t - 1 - ds.Q * a0 ** 2)))
t = ds.delta_t * np.arange(Nt)
return np.testing.assert_array_almost_equal(v[:,0], np.vectorize(acceleration)(t),
return np.testing.assert_array_almost_equal(v[:, 0], np.vectorize(acceleration)(t),
decimal=8)

def test_equlibrium_velocity(self):
"""
Test accuracy of integrating the deterministic dynamics for equilibrium velocity [6, p. 116]
"""
ds = velocity(tau=1.01/0.3, R=0)
ds = velocity(tau=1.01 / 0.3, R=0)
v0 = ds.deterministic

Nt = 100 # Number of time steps
Nt = 100 # Number of time steps
v = ds.simulate(Nt, v0=np.array([v0, 0.]))

return np.testing.assert_array_almost_equal(v[:,0]-v0, np.zeros(Nt),
return np.testing.assert_array_almost_equal(v[:, 0] - v0, np.zeros(Nt),
decimal=8)


def test_dimensionality(self):
ds = velocity(tau=1.0/0.3)
ds = velocity(tau=1.0 / 0.3)
Nt = 10
v = ds.simulate(Nt)
self.assertEqual(v.shape, (Nt, 2),
Expand All @@ -62,6 +61,7 @@ def test_dimensionality(self):
self.assertEqual(v.shape, (Nt, 3),
'The returned vector should reflect the dimension of the initial condition.')


class SampleTauTestCase(unittest.TestCase):
def test_range(self):
tau = sample_tau(100)
Expand All @@ -71,14 +71,15 @@ def test_range(self):
def test_ratio(self):
tau = sample_tau(100000, ratio=0.4)
sample = np.array(tau)
before = np.sum(sample <= 1/0.3)
beyond = np.sum(sample > 1/0.3)
self.assertTrue(abs(0.4 - float(before)/(before+beyond))<0.002)

before = np.sum(sample <= 1 / 0.3)
beyond = np.sum(sample > 1 / 0.3)
self.assertTrue(abs(0.4 - float(before) / (before + beyond)) < 0.006)


class LoadDriftBifTestCase(unittest.TestCase):
def test_classification_labels(self):
X, y = load_driftbif(10, 100)
self.assertEqual(set(y), set([0,1]))
self.assertEqual(set(y), set([0, 1]))

def test_regression_labels(self):
Nsamples = 10
Expand All @@ -97,6 +98,3 @@ def test_configured_dimensionality(self):
Nt = 100
X, y = load_driftbif(Nsamples, Nt, m=3)
self.assertEqual(X.shape, (3 * Nt * Nsamples, 4))

if __name__ == '__main__':
unittest.main()
17 changes: 9 additions & 8 deletions tsfresh/examples/driftbif_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def __init__(self, tau=3.8, kappa_3=0.3, Q=1950.0, R=3e-4, delta_t=0.05, seed=No

if not seed is None:
np.random.seed(seed)

if tau <= 1.0 / kappa_3:
self.deterministic = 0.0
else:
Expand Down Expand Up @@ -91,9 +91,9 @@ def simulate(self, N, v0=np.zeros(2)):
:rtype: ndarray
"""

v = [v0] # first value is initial condition
n = N - 1 # Because we are returning the initial condition,
# only (N-1) time steps are computed
v = [v0] # first value is initial condition
n = N - 1 # Because we are returning the initial condition,
# only (N-1) time steps are computed
gamma = np.random.randn(n, v0.size)
for i in range(n):
next_v = self.__call__(v[i]) + self.c * gamma[i]
Expand All @@ -120,12 +120,13 @@ def sample_tau(n=10, kappa_3=0.3, ratio=0.5, rel_increase=0.15):
assert ratio > 0 and ratio <= 1
assert kappa_3 > 0
assert rel_increase > 0 and rel_increase <= 1
tau_c = 1.0/kappa_3
tau_c = 1.0 / kappa_3

tau_max = tau_c * (1.0 + rel_increase)
tau = tau_c + (tau_max - tau_c) * (np.random.rand(n)-ratio)
tau = tau_c + (tau_max - tau_c) * (np.random.rand(n) - ratio)
return tau.tolist()


def load_driftbif(n, l, m=2, classification=True, kappa_3=0.3, seed=False):
"""
Simulates n time-series with l time steps each for the m-dimensional velocity of a dissipative soliton
Expand Down Expand Up @@ -180,5 +181,5 @@ def load_driftbif(n, l, m=2, classification=True, kappa_3=0.3, seed=False):
df = pd.DataFrame({'id': id, "time": time, "value": np.stack(values).flatten(), "dimension": dimensions})
y = pd.Series(labels)
y.index = range(n)

return df, y
52 changes: 0 additions & 52 deletions tsfresh/examples/test_tsfresh_baseline_dataset.py

This file was deleted.

0 comments on commit 5e887bb

Please sign in to comment.