# Path: odelab/store.py
# class SimpleStore(object):
# class PyTableStore(SimpleStore):
# 	class AlreadyInitialized(Exception):
# 	class NotInitialized(Exception):
# 	def __init__(self, path=None):
# 	def __getitem__(self, key):
# 	def __setitem__(self, key, value):
# 	def initialize(self, event0, name):
# 	def __len__(self):
# 	def append(self, event):
# 	def open(self, name=None, write=False):
# 	def get_nb_stage(self, events):
# 	def __init__(self, path=None):
# 	def append(self, event):
# 	def getitem(self, events, key):
# 	def __getitem__(self, key):
# 	def setitem(self, events, key, value):
# 	def __setitem__(self, key, value):
# 	def initialize(self, event0, name):
# 	def __len__(self):
# 	def get_nb_stage(self, events):
# 	def open(self, write=False):


from odelab.scheme.rungekutta import *
from odelab.scheme.generallinear import *
from odelab.scheme import *
from odelab.scheme.classic import *
from odelab.scheme.exponential import *
from odelab.store import Store, PyTableStore, SimpleStore
from odelab.system.classic import *
from odelab.system.exponential import *
from odelab import *
import tempfile
import os
import numpy as np
import numpy.testing as npt
import unittest
import pytest
import matplotlib.pyplot as plt

class Test_Access(unittest.TestCase):
	"""
	Test the Solver.get_events method.
	"""
	def setUp(self):
		self.s = Solver(ExplicitEuler(.1), System(f))
		self.time = 30
		self.s.initialize(u0=np.array([0.]))

	def test_access(self):
		self.s.run(self.time)
		sampling_rate = .5
		time = 15
		evts = self.s.get_events(t0=0, time=time, sampling_rate=sampling_rate)
		npt.assert_almost_equal(len(evts.T), len(self.s)*sampling_rate/2, -1) # approx 1/4 of total nb of events
		## self.assertEqual(len(evts.T), 250)

	def test_notrun(self):
		with self.assertRaises(Solver.NotRun):
			self.s.get_events()



class TestSolver(unittest.TestCase):
	def setUp(self):
		self.setup_solver()

	def setup_solver(self):
		self.solver = Solver(ExplicitEuler(h=.1), System(f))

# Path: odelab/store.py
# class SimpleStore(object):
# class PyTableStore(SimpleStore):
# 	class AlreadyInitialized(Exception):
# 	class NotInitialized(Exception):
# 	def __init__(self, path=None):
# 	def __getitem__(self, key):
# 	def __setitem__(self, key, value):
# 	def initialize(self, event0, name):
# 	def __len__(self):
# 	def append(self, event):
# 	def open(self, name=None, write=False):
# 	def get_nb_stage(self, events):
# 	def __init__(self, path=None):
# 	def append(self, event):
# 	def getitem(self, events, key):
# 	def __getitem__(self, key):
# 	def setitem(self, events, key, value):
# 	def __setitem__(self, key, value):
# 	def initialize(self, event0, name):
# 	def __len__(self):
# 	def get_nb_stage(self, events):
# 	def open(self, write=False):


# Path: tests/test_solver.py
from odelab.scheme.rungekutta import *
from odelab.scheme.generallinear import *
from odelab.scheme import *
from odelab.scheme.classic import *
from odelab.scheme.exponential import *
from odelab.store import Store, PyTableStore, SimpleStore
from odelab.system.classic import *
from odelab.system.exponential import *
from odelab import *
import tempfile
import os
import numpy as np
import numpy.testing as npt
import unittest
import pytest
import matplotlib.pyplot as plt


class Test_Access(unittest.TestCase):
	"""
	Test the Solver.get_events method.
	"""
	def setUp(self):
		self.s = Solver(ExplicitEuler(.1), System(f))
		self.time = 30
		self.s.initialize(u0=np.array([0.]))

	def test_access(self):
		self.s.run(self.time)
		sampling_rate = .5
		time = 15
		evts = self.s.get_events(t0=0, time=time, sampling_rate=sampling_rate)
		npt.assert_almost_equal(len(evts.T), len(self.s)*sampling_rate/2, -1) # approx 1/4 of total nb of events
		## self.assertEqual(len(evts.T), 250)

	def test_notrun(self):
		with self.assertRaises(Solver.NotRun):
			self.s.get_events()



class TestSolver(unittest.TestCase):
	def setUp(self):
		self.setup_solver()

	def setup_solver(self):
		self.solver = Solver(ExplicitEuler(h=.1), System(f))

		self.solver.store = Store(None)