Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/feature/9342_indirect_py_alg_tes…
Browse files Browse the repository at this point in the history
…ting'

Re #9342.

Conflicts:
	Code/Mantid/Framework/PythonInterface/test/python/plugins/algorithms/CMakeLists.txt
  • Loading branch information
peterfpeterson committed Aug 28, 2014
2 parents 0269bfb + 870980a commit ab23408
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from mantid.api import *
from mantid.simpleapi import *

import scipy.constants
import numpy as np
import re
import os.path
Expand Down Expand Up @@ -615,5 +614,8 @@ def _parse_castep_file(self, file_name):

return frequencies, ir_intensities, raman_intensities, warray

# Register algorithm with Mantid
AlgorithmFactory.subscribe(DensityOfStates)
try:
import scipy.constants
AlgorithmFactory.subscribe(DensityOfStates)
except:
logger.debug('Failed to subscribe algorithm DensityOfStates; The python package scipy may be missing.')
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ set ( TEST_PY_FILES
CreateLeBailFitInputTest.py
CreateWorkspaceTest.py
DakotaChiSquaredTest.py
DensityOfStatesTest.py
DSFinterpTest.py
FilterLogByTimeTest.py
FindReflectometryLinesTest.py
GetEiT0atSNSTest.py
IndirectILLReductionTest.py
IndirectTransmissionTest.py
LoadFullprofFileTest.py
LoadLiveDataTest.py
LoadLogPropertyTableTest.py
Expand All @@ -28,6 +30,7 @@ set ( TEST_PY_FILES
SANSWideAngleCorrectionTest.py
SavePlot1DTest.py
SortByQVectorsTest.py
SofQWMomentsTest.py
SortDetectorsTest.py
SortXAxisTest.py
Stitch1DManyTest.py
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import unittest
from mantid import logger
from mantid.simpleapi import DensityOfStates, CheckWorkspacesMatch, Scale

def scipy_not_available():
''' Check whether scipy is available on this platform'''
try:
import scipy
return False
except:
logger.warning("Skipping DensityOfStatesTest because scipy is unavailable.")
return True

def skip_if(skipping_criteria):
'''
Skip all tests if the supplied functon returns true.
Python unittest.skipIf is not available in 2.6 (RHEL6) so we'll roll our own.
'''
def decorate(cls):
if skipping_criteria():
for attr in cls.__dict__.keys():
if callable(getattr(cls, attr)) and 'test' in attr:
delattr(cls, attr)
return cls
return decorate

@skip_if(scipy_not_available)
class DensityOfStatesTest(unittest.TestCase):

def setUp(self):
self._file_name = 'squaricn.phonon'

def test_phonon_load(self):
ws = DensityOfStates(File=self._file_name)
self.assertEquals(ws.getNumberHistograms(), 1)

def test_castep_load(self):
ws = DensityOfStates(File='squaricn.castep')
self.assertEquals(ws.getNumberHistograms(), 1)

def test_raman_active(self):
spec_type = 'Raman_Active'
ws = DensityOfStates(File=self._file_name, SpectrumType=spec_type)
self.assertEquals(ws.getNumberHistograms(), 1)

def test_ir_active(self):
spec_type = 'IR_Active'
ws = DensityOfStates(File=self._file_name, SpectrumType=spec_type)
self.assertEquals(ws.getNumberHistograms(), 1)

def test_lorentzian_function(self):
ws = DensityOfStates(File=self._file_name, Function='Lorentzian')
self.assertEquals(ws.getNumberHistograms(), 1)

def test_peak_width(self):
ws = DensityOfStates(File=self._file_name, PeakWidth=0.3)
self.assertEquals(ws.getNumberHistograms(), 1)

def test_temperature(self):
ws = DensityOfStates(File=self._file_name, Temperature=50)
self.assertEquals(ws.getNumberHistograms(), 1)

def test_scale(self):
ws = DensityOfStates(File=self._file_name, Scale=10)
ref = DensityOfStates(File=self._file_name)
ref = Scale(ref, Factor=10)

CheckWorkspacesMatch(ws, ref)

def test_bin_width(self):
import math

ref = DensityOfStates(File=self._file_name)
ws = DensityOfStates(File=self._file_name, BinWidth=2)

size = ws.blocksize()
ref_size = ref.blocksize()

self.assertEquals(size, math.ceil(ref_size/2.0))

def test_zero_threshold(self):
import numpy as np

ws = DensityOfStates(File=self._file_name, ZeroThreshold=20)

x = ws.readX(0)
y = ws.readY(0)

mask = np.where(x < 20)
self.assertEquals(sum(y[mask]), 0)

def test_partial(self):
spec_type = 'DOS'
ws = DensityOfStates(File=self._file_name, SpectrumType=spec_type, Ions="H,C,O")

workspaces = ws.getNames()
self.assertEquals(len(workspaces), 3)

def test_sum_partial_contributions(self):
spec_type = 'DOS'
tolerance = 1e-10

summed = DensityOfStates(File=self._file_name, SpectrumType=spec_type, Ions="H,C,O", SumContributions=True)
total = DensityOfStates(File=self._file_name, SpectrumType=spec_type)

CheckWorkspacesMatch(summed, total, tolerance)

if __name__=="__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import unittest
import numpy as np
import mantid.simpleapi
from mantid.simpleapi import IndirectTransmission

class IndirectTransmissionTest(unittest.TestCase):

def test_indirect_transmission(self):
instrument = "IRIS"
analyser = "graphite"
reflection = "002"

#using water sample
formula = "H2-O"
density = 0.1
thickness = 0.1

ws = IndirectTransmission(Instrument=instrument, Analyser=analyser, Reflection=reflection,
ChemicalFormula=formula, NumberDensity=density, Thickness=thickness)

#expected values from table
ref_result = [6.658, 0.821223, 2.58187, 53.5069, 56.0888, 0.1, 0.1, 0.566035, 0.429298]
values = ws.column(1)
np.testing.assert_array_almost_equal(values, ref_result, decimal=4)

if __name__=="__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import unittest
from mantid.simpleapi import SofQWMoments, CreateSampleWorkspace, ScaleX, RenameWorkspace, LoadInstrument, SofQW
from mantid import mtd

class SofQWMomentsTest(unittest.TestCase):

def createSampleWorkspace(self):
""" Create a dummy workspace that looks like a sample run"""
#create a dummy workspace
function = "name=Lorentzian,Amplitude=1,PeakCentre=5,FWHM=1"
ws = CreateSampleWorkspace("Histogram", Function="User Defined", UserDefinedFunction=function, XMin=0, XMax=10, BinWidth=0.01, XUnit="DeltaE")
ws = ScaleX(ws, -5, "Add") #shift to center on 0
ws = ScaleX(ws, 0.1) #scale to size
LoadInstrument(ws, InstrumentName='IRIS')
return ws

def test_sofqwmoments(self):
ws = self.createSampleWorkspace()

#Run SofQW and then SofQWMoments
ws = SofQW(ws, '0.4, 0.1, 1.8', EMode='Indirect', EFixed='1.845')
ws = SofQWMoments(ws)

workspaces = ws.getNames()
self.assertEquals(len(workspaces), 4)

for wsname in workspaces:
ws = mtd[wsname]
self.assertEquals(ws.getNumberHistograms(), 1)
self.assertEquals(ws.blocksize(), 14)

if __name__=="__main__":
unittest.main()

0 comments on commit ab23408

Please sign in to comment.