Skip to content

Commit

Permalink
Refs #11289. Added matplotblit canvas class.
Browse files Browse the repository at this point in the history
And fixed issues  during testing.

	new file:   MplFigureCanvas.py
  • Loading branch information
wdzhou committed Mar 16, 2015
1 parent 4547085 commit 989d786
Show file tree
Hide file tree
Showing 5 changed files with 340 additions and 49 deletions.
99 changes: 68 additions & 31 deletions Code/Mantid/scripts/HFIRPowderReduction/HfirPDReductionGUI.py
Expand Up @@ -21,27 +21,37 @@ def _fromUtf8(s):
from matplotlib.pyplot import setp

# FIXME - Remove after debugging
NOMANTID = True
NOMANTID = None

try:
import mantid.simpleapi as api
import mantid.kernel
from mantid.simpleapi import AnalysisDataService
from mantid.kernel import ConfigService
import mantid
IMPORT_MANTID = True
except ImportError as e:
if NOMANTID is False:
print "Unable to import Mantid: %s." % (str(e))
raise e
sys.path.append('/home/wzz/Mantid_Project/Mantid2/Code/release/bin')
try:
import mantid
except ImportError as e2:
if NOMANTID is False:
print "Unable to import Mantid: %s." % (str(e))
raise e
else:
print "NO MANTID IS USED FOR DEBUGGING PURPOSE."
IMPORT_MANTID = False
else:
print "NO MANTID IS USED FOR DEBUGGING PURPOSE."
IMPORT_MANTID = True
NOMANTID = True
finally:
if IMPORT_MANTID is True:
import mantid.simpleapi as api
import mantid.kernel
from mantid.simpleapi import AnalysisDataService
from mantid.kernel import ConfigService


#----- default configuration ---------------
DEFAULT_SERVER = 'http://neutron.ornl.gov/user_data'
DEFAULT_INSTRUMENT = 'hb2a'
DEFAULT_WAVELENGTH = 2.4100

#-------------------------------------------


Expand Down Expand Up @@ -170,6 +180,9 @@ def _initSetup(self):
# UI widgets setup
self.ui.comboBox_outputFormat.addItems(['Fullprof', 'GSAS', 'Fullprof+GSAS'])

# FIXME : Need to disable some widgets... consider to refactor the code
self.ui.radioButton_useServer.setChecked(True)
self.ui.radioButton_useLocal.setChecked(False)

# Set up data source
self._serverAddress = DEFAULT_SERVER
Expand All @@ -180,14 +193,15 @@ def _initSetup(self):
self._currUnit = '2theta'

# Workspaces
self._outws = None
self._myReducedPDWs = None
self._prevoutws = None

self._ptNo = None
self._detNo = None

# State machine
self._inPlotState = False
# self._inPlotState = False


return

Expand Down Expand Up @@ -284,9 +298,11 @@ def doLoadData(self):
self._logError("Bin size must be specified.")

unit = self._currUnit
wavelength = float(self.ui.lineEdit_wavelength.text())

# Reduce data
execstatus = self._reduceSpicePDData(datafilename, unit, xmin, xmax, binsize)
execstatus = self._reduceSpicePDData(datafilename, unit, xmin, xmax, binsize, wavelength)
print "[DB] reduction status = ", execstatus

# Plot data
if execstatus is True:
Expand All @@ -301,6 +317,8 @@ def doPlot2Theta(self):
# check binning parameters and target unit
change, xmin, xmax, binsize = self._uiCheckBinningParameters(self._myMinX, self._myMaxX,
self._myBinSize, self._myCurrentUnit, "2theta")

print "[DB] Rebin is required: ", change

# no change, return with a notice
if change is False:
Expand Down Expand Up @@ -476,7 +494,7 @@ def doSaveData(self):
""" Save data
"""
# check whether it is fine to save
if self._outws is None:
if self._myReducedPDWs is None:
self._logError("No reduced diffraction data to save.")
#return

Expand Down Expand Up @@ -570,7 +588,6 @@ def _uiLoadDataFile(self, exp, scan):
if status is False:
self._logError(errmsg)
self._srcFileName = None
return

elif self._srcAtLocal is True:
# Data from local
Expand All @@ -582,12 +599,12 @@ def _uiLoadDataFile(self, exp, scan):
return self._srcFileName


def _reduceSpicePDData(self, datafilename, unit, xmin, xmax, binsize):
def _reduceSpicePDData(self, datafilename, unit, xmin, xmax, binsize, wavelength):
""" Reduce SPICE powder diffraction data.
"""
# cache the previous one
self._prevoutws = self._outws
self._outws = None
self._prevoutws = self._myReducedPDWs
self._myReducedPDWs = None

# Rebin
if xmin is None or xmax is None:
Expand All @@ -596,6 +613,7 @@ def _reduceSpicePDData(self, datafilename, unit, xmin, xmax, binsize):
binpar = "%.7f, %.7f, %.7f" % (xmin, binsize, xmax)

# base workspace name
print "base workspace name: ", datafilename
basewsname = os.path.basename(datafilename).split(".")[0]

# load SPICE
Expand All @@ -610,7 +628,7 @@ def _reduceSpicePDData(self, datafilename, unit, xmin, xmax, binsize):
api.ConvertSpiceDataToRealSpace(InputWorkspace=tablewsname,
RunInfoWorkspace=infowsname,
OutputWorkspace=datamdwsname,
OutputMontiorWorkspace=monitorwsname)
OutputMonitorWorkspace=monitorwsname)

self._datamdws = AnalysisDataService.retrieve(datamdwsname)
self._monitormdws = AnalysisDataService.retrieve(monitorwsname)
Expand All @@ -629,21 +647,37 @@ def _reduceSpicePDData(self, datafilename, unit, xmin, xmax, binsize):
UnitOutput = unit,
NeutronWaveLength=wavelength)

self._outws = AnalysisDataService.retrieve(outwsname)
print "[DB] Reduction is finished. Data is in workspace %s. " % (datamdwsname)

# Set up class variable for min/max and
outws = AnalysisDataService.retrieve(outwsname)
if outws is not None:
self._myReducedPDWs = outws

return
self._myMinX = self._myReducedPDWs.readX(0)[0]
self._myMaxX = self._myReducedPDWs.readX(0)[-1]
self._myBinSize = binsize
self._myCurrentUnit = unit

return True


def _plotReducedData(self, targetunit, spectrum):
""" Plot reduced data stored in self._reducedWS to
"""
# print "[DB] Plot reduced data!", "_inPlotState = ", str(self._inPlotState)

# whether the data is load?
if self._inPlotState is False or self._myReducedPDWs is None:
if self._myReducedPDWs is None:
self._logWarning("No data to plot!")
return

# plot
self.ui.graphicsView_reducedData.plot(self._myReducedPDWs.readX(spectrum),
# FIXME - Should not modify the original workspace
wsname = str(self._myReducedPDWs)
api.ConvertToPointData(InputWorkspace=self._myReducedPDWs, OutputWorkspace=wsname)
self._myReducedPDWs = AnalysisDataService.retrieve(wsname)
self.ui.graphicsView_reducedData.addPlot(self._myReducedPDWs.readX(spectrum),
self._myReducedPDWs.readY(spectrum))

return
Expand All @@ -663,23 +697,25 @@ def _uiCheckBinningParameters(self, curxmin=None, curxmax=None, curbinsize=None,
# check x-min
if len(xmin) > 0:
xmin = float(xmin)
if ( (curmin is None) or (curmin is not None and abs(xmin-curxmin) > 1.0E-5) ):
if ( (self._myMinX is None) or (self._myMinX is not None and abs(xmin-self._myMinX) > 1.0E-5) ):
change = True
else:
xmin = None

# check x-max
if len(xmax) > 0:
xmax = float(xmax)
if ( (curmax is None) or (curmax is not None and abs(xmax-curxmax) > 1.0E-5) ):
if ( (self._myMaxX is None) or (self._myMaxX is not None and
abs(xmax-self._myMaxX) > 1.0E-5) ):
change = True
else:
xmax = None

# check binsize
if len(binsize) > 0:
binsize = float(binsize)
if ( (curbinsize is None) or (curbinsize is not None and abs(binsize-curbinsize) > 1.0E-5) ):
if ( (self._myBinSize is None) or (self._myBinSize is not None and
abs(binsize-self._myBinSize) > 1.0E-5) ):
change = True
else:
binsize = None
Expand All @@ -691,8 +727,6 @@ def _uiCheckBinningParameters(self, curxmin=None, curxmax=None, curbinsize=None,
return (change, xmin, xmax, binsize)




def _rebin(self, unit, xmin, binsize, xmax):
"""
"""
Expand Down Expand Up @@ -727,8 +761,6 @@ def _excludePt(self, pts):





def _logDebug(self, dbinfo):
""" Log debug information
"""
Expand All @@ -738,8 +770,14 @@ def _logDebug(self, dbinfo):
def _logError(self, errinfo):
""" Log error information
"""
print "Log(Error): %s" % (errinfo)


def _logWarning(self, errinfo):
""" Log error information
"""
print "Log(Warning): %s" % (errinfo)


def _downloadFile(self, url, localfilepath):
"""
Expand All @@ -752,7 +790,6 @@ def _downloadFile(self, url, localfilepath):
if wbuf.count('not found') > 0:
return (False, "File cannot be found at %s." % (url))


ofile = open(localfilepath, 'w')
ofile.write(wbuf)
ofile.close()
Expand Down

0 comments on commit 989d786

Please sign in to comment.