Skip to content
Snippets Groups Projects
Commit 913d98ab authored by Savici, Andrei T.'s avatar Savici, Andrei T. Committed by GitHub
Browse files

Merge pull request #20809 from mantidproject/20802_Python3DocTestLogToMaskPeaks

Python3 doctest compatibility: Logarithm to MaskPeaksWorkspace
parents a53ac0f3 1d141a10
No related merge requests found
...@@ -33,19 +33,20 @@ Usage ...@@ -33,19 +33,20 @@ Usage
.. testcode:: ExLogarithm .. testcode:: ExLogarithm
dataX = range(0,10)*10 # define 10 x-spectra dataX = list(range(0,10))*10 # define 10 x-spectra
dataY =([1]*10)*10 # with values 1 dataY =([1]*10)*10 # with values 1
dataY[0]=-10 # make first value not suitable for logarithm dataY[0]=-10 # make first value not suitable for logarithm
dataY[1]=10 # make second value different dataY[1]=10 # make second value different
dataE =([1]*10)*10 # define 10 error spectra with value 1 dataE =([1]*10)*10 # define 10 error spectra with value 1
# create test workspace # create test workspace
ws = CreateWorkspace(dataX, dataY,dataE,NSpec=10) ws = CreateWorkspace(dataX, dataY, dataE, NSpec=10)
# caluclate log10 # Calculate log10
ws = Logarithm(ws,Filler=-1,Natural='0') ws = Logarithm(ws,Filler=-1,Natural='0')
# #
# check results: # check results:
print 'Log10 for spectra 0: ',ws.readY(0)
print 'Log10 for Err spectra 0: ',ws.readE(0)[0:4] print('Log10 for spectra 0: {}'.format(ws.readY(0)))
print('Log10 for Err spectra 0: {}'.format(ws.readE(0)[0:4]))
.. testcleanup:: ExLogarithm .. testcleanup:: ExLogarithm
......
...@@ -33,9 +33,9 @@ Usage ...@@ -33,9 +33,9 @@ Usage
y = corrected.readY(2) y = corrected.readY(2)
e = corrected.readE(2) e = corrected.readE(2)
# print first corrected yvalues # print first corrected yvalues
print y[1:5] print(y[1:5])
# print first corrected evalues # print first corrected evalues
print e[1:5] print(e[1:5])
Output: Output:
......
...@@ -35,8 +35,8 @@ Usage ...@@ -35,8 +35,8 @@ Usage
output = MDHistoToWorkspace2D(InputWorkspace='input') output = MDHistoToWorkspace2D(InputWorkspace='input')
# print 6th group of 4 bins in both input and output workspaces # print 6th group of 4 bins in both input and output workspaces
print "part of MD workspace",input.getSignalArray()[1,1] print("part of MD workspace {}".format(input.getSignalArray()[1,1]))
print "corresponding part of 2D workspace",output.dataY(5) print("corresponding part of 2D workspace {}".format(output.dataY(5) ))
Output: Output:
......
...@@ -22,17 +22,17 @@ Usage ...@@ -22,17 +22,17 @@ Usage
.. testcode:: MaskAngle .. testcode:: MaskAngle
#Load a workspace #Load a workspace
ws=Load("CNCS_7860") ws = Load("CNCS_7860")
#Do the masking for direct beam #Do the masking for direct beam
mask=MaskAngle(ws,MinAngle=0,MaxAngle=10) mask = MaskAngle(ws, MinAngle=0, MaxAngle=10)
print "The algorithm has masked ",mask.size," detectors" print("The algorithm has masked {} detectors".format(mask.size))
#to test check a couple of detectors #to test check a couple of detectors
inst=ws.getInstrument() inst = ws.getInstrument()
print "Is the minimum element in the mask list (detector ",mask.min(),") masked? ",inst.getDetector(int(mask.min())).isMasked() print("Is the minimum element in the mask list (detector {}) masked? {}".format(mask.min(), inst.getDetector(int(mask.min())).isMasked()))
print "Is the maximum element in the mask list (detector ",mask.max(),") masked? ",inst.getDetector(int(mask.max())).isMasked() print("Is the maximum element in the mask list (detector {}) masked? {}".format(mask.max(), inst.getDetector(int(mask.max())).isMasked()))
print "Is a detector outside the list masked (for example detector 100)? ", inst.getDetector(100).isMasked() print("Is a detector outside the list masked (for example detector 100)? {}".format(inst.getDetector(100).isMasked() ))
.. testcleanup:: MaskAngle .. testcleanup:: MaskAngle
...@@ -42,9 +42,9 @@ Output: ...@@ -42,9 +42,9 @@ Output:
.. testoutput:: MaskAngle .. testoutput:: MaskAngle
The algorithm has masked 1516 detectors The algorithm has masked 1516 detectors
Is the minimum element in the mask list (detector 35126 ) masked? True Is the minimum element in the mask list (detector 35126) masked? True
Is the maximum element in the mask list (detector 38601 ) masked? True Is the maximum element in the mask list (detector 38601) masked? True
Is a detector outside the list masked (for example detector 100)? False Is a detector outside the list masked (for example detector 100)? False
......
...@@ -41,19 +41,19 @@ Usage ...@@ -41,19 +41,19 @@ Usage
a=MaskBTP(Instrument='CNCS',Tube='1-3') a=MaskBTP(Instrument='CNCS',Tube='1-3')
b=MaskBTP(Workspace='CNCSMaskBTP',Instrument='CNCS',Bank='40-50',Pixel='1-10') b=MaskBTP(Workspace='CNCSMaskBTP',Instrument='CNCS',Bank='40-50',Pixel='1-10')
c=MaskBTP(Workspace='CNCSMaskBTP',Instrument='CNCS',Bank='42') c=MaskBTP(Workspace='CNCSMaskBTP',Instrument='CNCS',Bank='42')
print "Detectors masked" print("Detectors masked")
print "Step 1: 50 * 3 * 128 =", a.size print("Step 1: 50 * 3 * 128 = {0.size}".format(a))
print "Step 2: 11 * 8 * 10 =", b.size print("Step 2: 11 * 8 * 10 = {0.size}".format(b))
print "Step 3: 1 * 8 * 128 =", c.size print("Step 3: 1 * 8 * 128 = {0.size}".format(c))
print "Here are some of the masked detectors:" print("Here are some of the masked detectors:")
instrument=mtd['CNCSMaskBTP'].getInstrument() instrument=mtd['CNCSMaskBTP'].getInstrument()
print "A pixel Bank 42, detector 42700: ",instrument.getDetector(42700).isMasked() print("A pixel Bank 42, detector 42700: {}".format(instrument.getDetector(42700).isMasked()))
print "Pixel 1 in Tube 4 in Bank 45, detector 45440: ",instrument.getDetector(45440).isMasked() print("Pixel 1 in Tube 4 in Bank 45, detector 45440: {}".format(instrument.getDetector(45440).isMasked()))
print "Pixel 100 in Tube 2 in Bank 45, detector 45283: ",instrument.getDetector(45283).isMasked() print("Pixel 100 in Tube 2 in Bank 45, detector 45283: {}".format(instrument.getDetector(45283).isMasked()))
print print("")
print "And one that should not be masked" print("And one that should not be masked")
print "Pixel 128 in Tube 8 in Bank 50, detector 51199: ",instrument.getDetector(51199).isMasked() print("Pixel 128 in Tube 8 in Bank 50, detector 51199: {}".format(instrument.getDetector(51199).isMasked()))
.. testcleanup:: MaskBTP .. testcleanup:: MaskBTP
......
...@@ -38,8 +38,8 @@ Usage ...@@ -38,8 +38,8 @@ Usage
wsMasked = MaskBins(ws,XMin=16,XMax=32) wsMasked = MaskBins(ws,XMin=16,XMax=32)
# Show Y values in workspaces # Show Y values in workspaces
print "Before masking:", ws.readY(0) print("Before masking: {}".format(ws.readY(0)))
print "After masking:",wsMasked.readY(0) print("After masking: {}".format(wsMasked.readY(0)))
Output: Output:
......
...@@ -41,8 +41,8 @@ Usage ...@@ -41,8 +41,8 @@ Usage
wsMasked = MaskBinsFromTable(ws,MaskingInformation=mask_info) wsMasked = MaskBinsFromTable(ws,MaskingInformation=mask_info)
# Show Y values in workspaces # Show Y values in workspaces
print "Before masking:", ws.readY(0) print("Before masking: {}".format(ws.readY(0)))
print "After masking:",wsMasked.readY(0) print("After masking: {}".format(wsMasked.readY(0)))
Output: Output:
......
...@@ -22,14 +22,14 @@ always returns first spectra present in the workspace. ...@@ -22,14 +22,14 @@ always returns first spectra present in the workspace.
The *Spectra Number* or *spectra ID* mean the number, assigned to a spectrum. This number is often equal to *WorkspaceIndex+1*, e.g. :: The *Spectra Number* or *spectra ID* mean the number, assigned to a spectrum. This number is often equal to *WorkspaceIndex+1*, e.g. ::
print sp.getSpectrumNo() print(sp.getSpectrumNo())
from the sample above will often print 1 but not always. The simplest case when this from the sample above will often print 1 but not always. The simplest case when this
number is different is when you load a second half of a workspace, when the first spectrum number still is **NumTotalSpectraInWorkspace/2+1**, number is different is when you load a second half of a workspace, when the first spectrum number still is **NumTotalSpectraInWorkspace/2+1**,
while *WorkspaceIndex* of this spectra becomes 0, i.e.: :: while *WorkspaceIndex* of this spectra becomes 0, i.e.: ::
sp = ws.getSpectrum(0) sp = ws.getSpectrum(0)
print sp.getSpectrumNo() print(sp.getSpectrumNo())
prints number equal to **NumTotalSpectraInWorkspace/2+1**. There are other ways to assign a random number to a spectrum. prints number equal to **NumTotalSpectraInWorkspace/2+1**. There are other ways to assign a random number to a spectrum.
...@@ -38,8 +38,8 @@ a first detector corresponds to the first spectra of a workspace, but it is not ...@@ -38,8 +38,8 @@ a first detector corresponds to the first spectra of a workspace, but it is not
the code: :: the code: ::
ws = CreateSimulationWorkspace('MARI','-0.5,0.5,0.5') ws = CreateSimulationWorkspace('MARI','-0.5,0.5,0.5')
sp=ws.getSpectrum(1) sp = ws.getSpectrum(1)
print sp.getSpectrumNo(), sp.getDetectorIDs() print("{} {}".format(sp.getSpectrumNo(), sp.getDetectorIDs()))
Will print: :: Will print: ::
...@@ -190,34 +190,34 @@ Example 1: specifying spectrum numbers ...@@ -190,34 +190,34 @@ Example 1: specifying spectrum numbers
# Get the 1st spectrum in the workspace # Get the 1st spectrum in the workspace
spec = ws.getSpectrum(0) spec = ws.getSpectrum(0)
detid = spec.getDetectorIDs()[0] detid = spec.getDetectorIDs()[0]
print 'Spectrum number is',spec.getSpectrumNo() print('Spectrum number is {}'.format(spec.getSpectrumNo()))
print 'Detector of this spectrum is masked:',ws.getInstrument().getDetector(detid).isMasked() print('Detector of this spectrum is masked: {}'.format(ws.getInstrument().getDetector(detid).isMasked()))
y = ws.readY(0) y = ws.readY(0)
print 'All counts in the spectrum are 0: ',np.all( y == 0.0 ) print('All counts in the spectrum are 0: {}'.format(np.all( y == 0.0 )))
# Get the 2nd spectrum in the workspace # Get the 2nd spectrum in the workspace
spec = ws.getSpectrum(1) spec = ws.getSpectrum(1)
detid = spec.getDetectorIDs()[0] detid = spec.getDetectorIDs()[0]
print 'Spectrum number is',spec.getSpectrumNo() print('Spectrum number is {}'.format(spec.getSpectrumNo()))
print 'Detector of this spectrum is masked:',ws.getInstrument().getDetector(detid).isMasked() print('Detector of this spectrum is masked: {}'.format(ws.getInstrument().getDetector(detid).isMasked()))
y = ws.readY(1) y = ws.readY(1)
print 'All counts in the spectrum are 0: ',np.all( y == 0.0 ) print('All counts in the spectrum are 0: {}'.format(np.all( y == 0.0 )))
# Get the 3rd spectrum in the workspace # Get the 3rd spectrum in the workspace
spec = ws.getSpectrum(2) spec = ws.getSpectrum(2)
detid = spec.getDetectorIDs()[0] detid = spec.getDetectorIDs()[0]
print 'Spectrum number is',spec.getSpectrumNo() print('Spectrum number is {}'.format(spec.getSpectrumNo()))
print 'Detector of this spectrum is masked:',ws.getInstrument().getDetector(detid).isMasked() print('Detector of this spectrum is masked: {}'.format(ws.getInstrument().getDetector(detid).isMasked()))
y = ws.readY(2) y = ws.readY(2)
print 'All counts in the spectrum are 0: ',np.all( y == 0.0 ) print('All counts in the spectrum are 0: {}'.format(np.all( y == 0.0 )))
# Get the 4th spectrum in the workspace # Get the 4th spectrum in the workspace
spec = ws.getSpectrum(3) spec = ws.getSpectrum(3)
detid = spec.getDetectorIDs()[0] detid = spec.getDetectorIDs()[0]
print 'Spectrum number is',spec.getSpectrumNo() print('Spectrum number is {}'.format(spec.getSpectrumNo()))
print 'Detector of this spectrum is masked:',ws.getInstrument().getDetector(detid).isMasked() print('Detector of this spectrum is masked: {}'.format(ws.getInstrument().getDetector(detid).isMasked()))
y = ws.readY(3) y = ws.readY(3)
print 'All counts in the spectrum are 0: ',np.all( y == 0.0 ) print('All counts in the spectrum are 0: {}'.format(np.all( y == 0.0 )))
Output Output
^^^^^^ ^^^^^^
...@@ -252,19 +252,19 @@ Example 2: specifying detector IDs ...@@ -252,19 +252,19 @@ Example 2: specifying detector IDs
# Check the 1st detector # Check the 1st detector
det = ws.getInstrument().getDetector(101) det = ws.getInstrument().getDetector(101)
print 'Detector ',det.getID(),' is masked:',det.isMasked() print('Detector {} is masked: {}'.format(det.getID(), det.isMasked()))
# Check the 2nd detector # Check the 2nd detector
det = ws.getInstrument().getDetector(103) det = ws.getInstrument().getDetector(103)
print 'Detector ',det.getID(),' is masked:',det.isMasked() print('Detector {} is masked: {}'.format(det.getID(), det.isMasked()))
# Check some other detectors # Check some other detectors
det = ws.getInstrument().getDetector(100) det = ws.getInstrument().getDetector(100)
print 'Detector ',det.getID(),' is masked:',det.isMasked() print('Detector {} is masked: {}'.format(det.getID(), det.isMasked()))
det = ws.getInstrument().getDetector(102) det = ws.getInstrument().getDetector(102)
print 'Detector ',det.getID(),' is masked:',det.isMasked() print('Detector {} is masked: {}'.format(det.getID(), det.isMasked()))
det = ws.getInstrument().getDetector(105) det = ws.getInstrument().getDetector(105)
print 'Detector ',det.getID(),' is masked:',det.isMasked() print('Detector {} is masked: {}'.format(det.getID(), det.isMasked()))
Output Output
^^^^^^ ^^^^^^
...@@ -293,23 +293,23 @@ Example 3: specifying workspace indices ...@@ -293,23 +293,23 @@ Example 3: specifying workspace indices
# Check the 1st spectrum # Check the 1st spectrum
workspaceIndex = 0 workspaceIndex = 0
det = ws.getDetector( workspaceIndex ) det = ws.getDetector( workspaceIndex )
print 'Detector in spectrum with workspace index ',workspaceIndex,' is masked:',det.isMasked() print('Detector in spectrum with workspace index {} is masked: {}'.format(workspaceIndex, det.isMasked()))
# Check the 2nd spectrum # Check the 2nd spectrum
workspaceIndex = 2 workspaceIndex = 2
det = ws.getDetector( workspaceIndex ) det = ws.getDetector( workspaceIndex )
print 'Detector in spectrum with workspace index ',workspaceIndex,' is masked:',det.isMasked() print('Detector in spectrum with workspace index {} is masked: {}'.format(workspaceIndex, det.isMasked()))
# Check some other spectra # Check some other spectra
workspaceIndex = 1 workspaceIndex = 1
det = ws.getDetector( workspaceIndex ) det = ws.getDetector( workspaceIndex )
print 'Detector in spectrum with workspace index ',workspaceIndex,' is masked:',det.isMasked() print('Detector in spectrum with workspace index {} is masked: {}'.format(workspaceIndex, det.isMasked()))
workspaceIndex = 3 workspaceIndex = 3
det = ws.getDetector( workspaceIndex ) det = ws.getDetector( workspaceIndex )
print 'Detector in spectrum with workspace index ',workspaceIndex,' is masked:',det.isMasked() print('Detector in spectrum with workspace index {} is masked: {}'.format(workspaceIndex, det.isMasked()))
workspaceIndex = 4 workspaceIndex = 4
det = ws.getDetector( workspaceIndex ) det = ws.getDetector( workspaceIndex )
print 'Detector in spectrum with workspace index ',workspaceIndex,' is masked:',det.isMasked() print('Detector in spectrum with workspace index {} is masked: {}'.format(workspaceIndex, det.isMasked()))
Output Output
^^^^^^ ^^^^^^
...@@ -375,13 +375,13 @@ Example 5: specifying a masking workspace ...@@ -375,13 +375,13 @@ Example 5: specifying a masking workspace
# Extract created mask into specialised masking workspace # Extract created mask into specialised masking workspace
masking_ws,dummy = ExtractMask( tmp ) masking_ws,dummy = ExtractMask( tmp )
print 'A masking workspace has',masking_ws.blocksize(),'spectrum' print('A masking workspace has {} spectrum'.format(masking_ws.blocksize()))
print 'Unmasked spectrum, value=',masking_ws.readY(0)[0] print('Unmasked spectrum, value = {}'.format(masking_ws.readY(0)[0]))
print 'Masked spectrum, value=',masking_ws.readY(1)[0] print('Masked spectrum, value = {}'.format(masking_ws.readY(1)[0]))
print 'Unmasked spectrum, value=',masking_ws.readY(2)[0] print('Unmasked spectrum, value = {}'.format(masking_ws.readY(2)[0]))
print 'Masked spectrum, value=',masking_ws.readY(3)[0] print('Masked spectrum, value = {}'.format(masking_ws.readY(3)[0]))
print 'Unmasked spectrum, value=',masking_ws.readY(4)[0] print('Unmasked spectrum, value = {}'.format(masking_ws.readY(4)[0]))
print print('')
# Create a data workspace # Create a data workspace
ws = CreateSampleWorkspace() ws = CreateSampleWorkspace()
...@@ -390,15 +390,15 @@ Example 5: specifying a masking workspace ...@@ -390,15 +390,15 @@ Example 5: specifying a masking workspace
# Check masking of first 5 detectors # Check masking of first 5 detectors
det = ws.getDetector(0) det = ws.getDetector(0)
print 'Detector',det.getID(),'is masked:',det.isMasked() print('Detector {} is masked: {}'.format(det.getID(), det.isMasked()))
det = ws.getDetector(1) det = ws.getDetector(1)
print 'Detector',det.getID(),'is masked:',det.isMasked() print('Detector {} is masked: {}'.format(det.getID(), det.isMasked()))
det = ws.getDetector(2) det = ws.getDetector(2)
print 'Detector',det.getID(),'is masked:',det.isMasked() print('Detector {} is masked: {}'.format(det.getID(), det.isMasked()))
det = ws.getDetector(3) det = ws.getDetector(3)
print 'Detector',det.getID(),'is masked:',det.isMasked() print('Detector {} is masked: {}'.format(det.getID(), det.isMasked()))
det = ws.getDetector(4) det = ws.getDetector(4)
print 'Detector',det.getID(),'is masked:',det.isMasked() print('Detector {} is masked: {}'.format(det.getID(), det.isMasked()))
Output Output
...@@ -407,11 +407,11 @@ Output ...@@ -407,11 +407,11 @@ Output
.. testoutput:: ExMaskMask .. testoutput:: ExMaskMask
A masking workspace has 1 spectrum A masking workspace has 1 spectrum
Unmasked spectrum, value= 0.0 Unmasked spectrum, value = 0.0
Masked spectrum, value= 1.0 Masked spectrum, value = 1.0
Unmasked spectrum, value= 0.0 Unmasked spectrum, value = 0.0
Masked spectrum, value= 1.0 Masked spectrum, value = 1.0
Unmasked spectrum, value= 0.0 Unmasked spectrum, value = 0.0
Detector 100 is masked: False Detector 100 is masked: False
Detector 101 is masked: True Detector 101 is masked: True
...@@ -430,9 +430,9 @@ Example 6: specifying a masking range ...@@ -430,9 +430,9 @@ Example 6: specifying a masking range
MaskDetectors(ws, StartWorkspaceIndex=2, EndWorkspaceIndex=4) MaskDetectors(ws, StartWorkspaceIndex=2, EndWorkspaceIndex=4)
# Check masking of first 6 detectors # Check masking of first 6 detectors
for ind in xrange(0,6): for ind in range(0,6):
det = ws.getDetector(ind) det = ws.getDetector(ind)
print 'Detector',det.getID(),'is masked:',det.isMasked() print('Detector {} is masked: {}'.format(det.getID(), det.isMasked()))
Output Output
...@@ -461,13 +461,13 @@ Example 7: constraining the masking range ...@@ -461,13 +461,13 @@ Example 7: constraining the masking range
# Extract created mask into specialised masking workspace # Extract created mask into specialised masking workspace
masking_ws,_ = ExtractMask( tmp ) masking_ws,_ = ExtractMask( tmp )
for ind in xrange(0,7): for ind in range(0,7):
val = masking_ws.readY(ind)[0] val = masking_ws.readY(ind)[0]
if val>0: if val>0:
print 'Unmasked spectrum, value=',val print('Unmasked spectrum, value= {}'.format(val))
else: else:
print 'Masked spectrum, value=',val print('Masked spectrum, value= {}'.format(val))
print print('')
# Create a data workspace # Create a data workspace
ws = CreateSampleWorkspace() ws = CreateSampleWorkspace()
...@@ -475,10 +475,10 @@ Example 7: constraining the masking range ...@@ -475,10 +475,10 @@ Example 7: constraining the masking range
MaskDetectors(ws, MaskedWorkspace=masking_ws,StartWorkspaceIndex=4, EndWorkspaceIndex=5) MaskDetectors(ws, MaskedWorkspace=masking_ws,StartWorkspaceIndex=4, EndWorkspaceIndex=5)
# Check masking of first 7 detectors # Check masking of first 7 detectors
for ind in xrange(0,7): for ind in range(0,7):
det = ws.getDetector(ind) det = ws.getDetector(ind)
print 'Detector',det.getID(),'is masked:',det.isMasked() print('Detector {} is masked: {}'.format(det.getID(), det.isMasked()))
Output Output
^^^^^^ ^^^^^^
......
...@@ -66,11 +66,11 @@ Usage ...@@ -66,11 +66,11 @@ Usage
inside_dets.append( det.getID() ) inside_dets.append( det.getID() )
# Print out the IDs # Print out the IDs
print masked_dets print(masked_dets)
print inside_dets print(inside_dets)
# Check that the two arrays are equal # Check that the two arrays are equal
print masked_dets == inside_dets print(masked_dets == inside_dets)
Output Output
###### ######
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment