-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/feature/9342_indirect_py_alg_tes…
…ting' Re #9342. Conflicts: Code/Mantid/Framework/PythonInterface/test/python/plugins/algorithms/CMakeLists.txt
- Loading branch information
Showing
5 changed files
with
177 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
Code/Mantid/Framework/PythonInterface/test/python/plugins/algorithms/DensityOfStatesTest.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
27 changes: 27 additions & 0 deletions
27
...ntid/Framework/PythonInterface/test/python/plugins/algorithms/IndirectTransmissionTest.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
33 changes: 33 additions & 0 deletions
33
Code/Mantid/Framework/PythonInterface/test/python/plugins/algorithms/SofQWMomentsTest.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |