-
Notifications
You must be signed in to change notification settings - Fork 60
Description
I'm not using the GUI and want to understand the use_limits, xmin and xmax behavior for batch fitting McaAdvancedFitBatch, fast fitting FastXRFLinearFit and single spectrum fitting ClassMcaTheory.
Sorry but my blood boils every time I fall over this. I should calm down and actually understand the logic here.
So the single spectrum fitting ClassMcaTheory parses configuration (use_limits, xmin and xmax) and setData function parameters (xmin and xmax) like this in the setData function
pymca/src/PyMca5/PyMcaPhysics/xrf/ClassMcaTheory.py
Lines 1117 to 1139 in a072333
| xmin = self.config['fit']['xmin'] | |
| if not self.config['fit']['use_limit']: | |
| if 'xmin' in kw: | |
| xmin=kw['xmin'] | |
| if xmin is not None: | |
| self.config['fit']['xmin'] = xmin | |
| else: | |
| xmin=min(self.xdata) | |
| elif len(self.xdata): | |
| xmin=min(self.xdata) | |
| xmax = self.config['fit']['xmax'] | |
| if not self.config['fit']['use_limit']: | |
| if 'xmax' in kw: | |
| xmax=kw['xmax'] | |
| if xmax is not None: | |
| self.config['fit']['xmax'] = xmax | |
| else: | |
| xmax=max(self.xdata) | |
| elif len(self.xdata): | |
| xmax=max(self.xdata) | |
| self.lastxmin = xmin | |
| self.lastxmax = xmax |
and apply it like this
pymca/src/PyMca5/PyMcaPhysics/xrf/ClassMcaTheory.py
Lines 1148 to 1155 in a072333
| #take the data between limits | |
| i1=numpy.nonzero((self.xdata >=xmin) & (self.xdata<=xmax))[0] | |
| self.xdata=numpy.take(self.xdata,i1) | |
| n=len(self.xdata) | |
| #Calculate the background just of the regions gives better results | |
| #self.zz=SpecfitFuns.subac(self.ydata,1.000,20000) | |
| #self.zz =numpy.take(self.zz,i1) | |
| self.ydata=numpy.take(self.ydata,i1) |
So the behavior as I think I understand it:
- the configuration has
xmin,xmaxanduse_limits. - when
use_limitsisTruewe use thexminandxmaxvalues from the configuration for fitting and ignore the function arguments. - when
use_limitsisFalsewe usexminandxmaxfrom the function arguments for fitting and ignore the values from the configuration.
So configuration has priority when use_limits=True and function arguments has priority when use_limits=False.
This is confusing when I just look at the API of ClassMcaTheory.setData. This behavior exists for the GUI (see questions later).
McaAdvancedFitBatch behavior
Both in the new McaAdvancedFitBatch (with HDF5 output support) and LegacyMcaAdvancedFitBatch (no HDF5 output support) we force use_limit and completely ignore what was in the configuration:
| self.mcafit.config['fit']['use_limit'] = 1 # TODO: why??? |
| self.mcafit.config['fit']['use_limit'] = 1 |
When using the McaAdvancedFitBatch I don't think you can pass xmin and xmax as arguments. So the result is: we always use xmin and xmax from the configuration no matter what.
ℹ️ I can solve the problem by patching the configuration
batch = McaAdvancedFitBatch(...) if not configuration["fit"]["use_limit"]: # WARNING: McaAdvancedFitBatch ignores `use_limit=False` configuration["fit"]["xmin"] = 0 configuration["fit"]["xmax"] = xrf_spectra.shape[1] batch.mcafit.configure(configuration)
FastXRFLinearFit behavior
We do not do such a thing in FastXRFLinearFit and LegacyFastXRFLinearFit but we have this
pymca/src/PyMca5/PyMcaPhysics/xrf/FastXRFLinearFit.py
Lines 133 to 138 in a072333
| if xmin is None: | |
| xmin = config['fit']['xmin'] | |
| if xmax is None: | |
| xmax = config['fit']['xmax'] | |
| dtypeCalculcation = self._fitDtypeCalculation(data) | |
| self._mcaTheory.setData(x=x, y=yref, xmin=xmin, xmax=xmax) |
pymca/src/PyMca5/PyMcaPhysics/xrf/LegacyFastXRFLinearFit.py
Lines 117 to 121 in a072333
| if xmin is None: | |
| xmin = config['fit']['xmin'] | |
| if xmax is None: | |
| xmax = config['fit']['xmax'] | |
| toReconfigure = False |
| self._mcaTheory.setData(x=x, y=firstSpectrum, xmin=xmin, xmax=xmax) |
So when the xmin and xmax arguments are not provided to FastXRFLinearFit.fitMultipleSpectra we replace them by the configuration values and pass it to ClassMcaTheory.setData. So the result is: we always use xmin and xmax from the configuration (regardless of the value of use_limits) unless we provided them as function arguments to FastXRFLinearFit.fitMultipleSpectra.
ℹ️ I can solve the problem by calling
fastFit.fitMultipleSpectrain this wayfastFit = FastXRFLinearFit() if configuration["fit"]["use_limit"]: xmin = None xmax = None else: # WARNING: FastXRFLinearFit ignores `use_limit=False` xmin = 0 xmax = xrf_spectra.shape[1] outbuffer = fastFit.fitMultipleSpectra(..., xmin=xmin, xmax=xmax)
Questions
- So
McaAdvancedFitBatchandFastXRFLinearFitignoresuse_limits=False(they achieve this in a different way). I believe this was done becauseuse_limitsis only supposed to affect the GUI (see next question). Is that correct? - The priority of xmin and xmax configuration values vs.
ClassMcaTheory.setDatafunction arguments changes depending on whetheruse_limitsisTrueorFalse. This had something to do with the GUI. Whenuse_limits=Truewe ignore the zoom level of the spectrum plot and whenuse_limits=Falsethe x limits are based on the zoom level of the spectrum plot. Something like that?
Once I understand this I'd like to have this documented somewhere.