Skip to content

Commit

Permalink
Simulator Bug Fixes (#143)
Browse files Browse the repository at this point in the history
* Convert assertion to exception and add corresponding test

* Adds function to fetch all channels
  • Loading branch information
usmanwardag authored and matteobachetti committed Aug 20, 2016
1 parent dec3a37 commit d959c45
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
14 changes: 12 additions & 2 deletions stingray/simulator/simulator.py
Expand Up @@ -170,8 +170,11 @@ def simulate_channel(self, channel, *args):
"""

# Check that channel name does not already exist.
assert channel not in [lc[0] for lc in self.channels]
self.channels.append((channel, self.simulate(*args)))
if channel not in [lc[0] for lc in self.channels]:
self.channels.append((channel, self.simulate(*args)))

else:
raise KeyError('A channel with this name already exists.')

def get_channel(self, channel):
"""
Expand All @@ -187,6 +190,13 @@ def get_channels(self, channels):

return [lc[1] for lc in self.channels if lc[0] in channels]

def get_all_channels(self):
"""
Get lightcurves belonging to all channels.
"""

return [lc[1] for lc in self.channels]

def delete_channel(self, channel):
"""
Delete an energy channel.
Expand Down
15 changes: 15 additions & 0 deletions stingray/simulator/tests/test_simulator.py
Expand Up @@ -53,6 +53,13 @@ def test_simulate_channel(self):
self.simulator.simulate_channel('3.5-4.5', 'lorenzian', [1, 2, 3, 4])
self.simulator.delete_channel('3.5-4.5')

def test_incorrect_simulate_channel(self):
"""Test simulating a channel that already exists."""
self.simulator.simulate_channel('3.5-4.5', 2)
with pytest.raises(KeyError):
self.simulator.simulate_channel('3.5-4.5', 2)
self.simulator.delete_channel('3.5-4.5')

def test_get_channel(self):
"""
Retrieve an energy channel after it has been simulated.
Expand All @@ -71,6 +78,14 @@ def test_get_channels(self):

self.simulator.delete_channels(['3.5-4.5', '4.5-5.5'])

def test_get_all_channels(self):
""" Retrieve all energy channels. """
self.simulator.simulate_channel('3.5-4.5', 2)
self.simulator.simulate_channel('4.5-5.5', 1)
lc = self.simulator.get_all_channels()

self.simulator.delete_channels(['3.5-4.5', '4.5-5.5'])

def test_count_channels(self):
"""
Count energy channels after they have been simulated.
Expand Down

0 comments on commit d959c45

Please sign in to comment.