Skip to content

Commit

Permalink
Bug/Require OrderedDict for Scans input (#99)
Browse files Browse the repository at this point in the history
* check that a multiscan input is an ordered dictionary
* updated test to check for `dict` type checking error
  • Loading branch information
granrothge authored and David M Fobes committed Feb 16, 2017
1 parent 01ec23d commit bf0ae96
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
9 changes: 7 additions & 2 deletions neutronpy/data/scans.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
import matplotlib.pyplot as plt
import matplotlib.colors as mpl_c
import numpy as np
import collections


class Scans(object):
r"""A class for a collection of scans
Attributes
----------
scans : dict
scans : Ordered dict
A dictionary of scan objects
num_scans : int
The number of scans
Expand All @@ -25,9 +26,11 @@ class Scans(object):
"""

def __init__(self, scans_dict=None):
self.scans = scans_dict
if scans_dict is not None:
if not isinstance(scans_dict,collections.OrderedDict):
raise RuntimeError("the input dictionary must be of type OrderedDict")
self.num_scans = len(scans_dict)
self.scans = scans_dict

def update(self, scans_dict):
r"""Update the scans_dict to include the dictionary scans_dict
Expand All @@ -40,6 +43,8 @@ def update(self, scans_dict):
A dictionary of multiple scans to add to the collection.
"""
if not isinstance(scans_dict,collections.OrderedDict):
raise RuntimeError("the input dictionary must be of type OrderedDict")
self.scans.update(scans_dict)
self.num_scans = len(scans_dict)

Expand Down
18 changes: 15 additions & 3 deletions tests/test_scans.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import neutronpy.fileio as npyio
import os
import pytest
import collections
from mock import patch


Expand All @@ -16,7 +17,7 @@ def load_scans(start,stop):
"""
Creates a dictionary of scans for use in other test functions
"""
scansin={}
scansin=collections.OrderedDict()
try:
for idx in range(start,stop+1):
scansin[idx]=npyio.load_data(os.path.join(os.path.dirname(__file__),'filetypes/HB1A/HB1A_exp0718_scan0%d.dat'%idx))
Expand All @@ -26,12 +27,23 @@ def load_scans(start,stop):


def test_scans_init():
#load file
scansin=load_scans(222,243)

# check expecteed execution
try:
npysc.Scans(scans_dict=scansin)
tst_obj=npysc.Scans(scans_dict=scansin)
except:
pytest.failed('could not build scan collection')
# check if appropriate error is raise if not an ordered dict.
dict_scans=dict(scansin)
with pytest.raises(RuntimeError) as excinfo:
npysc.Scans(scans_dict=dict_scans)
assert 'type OrderedDict' in str(excinfo.value)
# check that update raises the proper error
with pytest.raises(RuntimeError) as excinfo:
tst_obj.update(dict_scans)
assert 'type OrderedDict' in str(excinfo.value)


@patch('matplotlib.pyplot.show')
def test_pcolor(mock_show):
Expand Down

0 comments on commit bf0ae96

Please sign in to comment.