diff --git a/Stoner/plot/core.py b/Stoner/plot/core.py index 8367a6782..c754f4d11 100755 --- a/Stoner/plot/core.py +++ b/Stoner/plot/core.py @@ -1093,10 +1093,10 @@ def plot(self, *args, **kargs): and whether to plot error bars (for an x,y plot). All keyword argume nts are passed through to the selected plotting routine. """ + _ = self._col_args(**kargs) if len(args) != 0: axes = len(args) else: - _ = self._col_args(**kargs) axes = _.axes if "template" in kargs: self.template = kargs.pop("template") diff --git a/scripts/PCAR-New.ini b/scripts/PCAR-New.ini index 7c09eaef4..7a23b7c6a 100755 --- a/scripts/PCAR-New.ini +++ b/scripts/PCAR-New.ini @@ -20,6 +20,8 @@ rescale_v: False # Tries to find and remove an offset in V by looking for peaks within 5 Delta # Warning, this may go badly wrong with certain DC waveforms remove_offset: False +# Decompose the data into symmetric and anti-symmetric with respect to bias and then fit the symmetric part only. +make_symmetric: True # Be clever about annotating the result on the plots fancy_result: True # diff --git a/scripts/PCAR-New.py b/scripts/PCAR-New.py index d923cf6d6..fabe234c2 100755 --- a/scripts/PCAR-New.py +++ b/scripts/PCAR-New.py @@ -7,10 +7,15 @@ import pathlib import numpy as np -from Stoner import Data +from Stoner import Data, __version_info__ from Stoner.analysis.fitting.models import cfg_data_from_ini, cfg_model_from_ini from Stoner.analysis.fitting.models.generic import quadratic +if __version_info__[0] == 0 and __version_info__[1] < 11: + raise ImportError( + "The version of the Stoner package is too old. This version of the script needs v. 0.11" + ) + class working(Data): @@ -24,8 +29,8 @@ def __init__(self, *args, **kargs): raise RuntimeError( f"Could not find the fitting ini file {inifile}!" ) - - tmp = cfg_data_from_ini(inifile, filename=False) + filename = kargs.get("filename", False) + tmp = cfg_data_from_ini(inifile, filename) self._setas = tmp.setas.clone self.column_headers = tmp.column_headers self.metadata = tmp.metadata @@ -47,6 +52,9 @@ def __init__(self, *args, **kargs): self.fancyresults = config.has_option( "Options", "fancy_result" ) and config.getboolean("Options", "fancy_result") + self.make_symmetric = config.has_option( + "Options", "make_symmetric" + ) and config.getboolean("Options", "make_symmetric") self.method = config.get("Options", "method") self.model = model self.p0 = p0 @@ -114,26 +122,40 @@ def offset_correct(self): "Options", "remove_offset" ) and self.config.getboolean("Options", "remove_offset"): print("Doing offset correction") - peaks = self.peaks( - ycol=self.gcol, - width=len(self) / 20, - xcol=self.vcol, - poly=4, - peaks=True, - troughs=True, - ) - peaks = filter(lambda x: abs(x) < 4 * self.delta["value"], peaks) - offset = np.mean(np.array(peaks)) - print("Mean offset =" + str(offset)) - self.apply(lambda x: x[self.vcol] - offset, self.vcol) + if self.config.has_option( + "Options", "simple_offset" + ) and self.config.getboolean("Options", "simple_offset"): + self.x -= 0.5 * (self.x.min() + self.x.max()) + else: + peaks = self.peaks( + ycol=self.gcol, + width=len(self) / 20, + xcol=self.vcol, + poly=4, + peaks=True, + troughs=True, + ) + peaks = filter( + lambda x: abs(x) < 4 * self.delta["value"], peaks + ) + offset = np.mean(np.array(peaks)) + print("Mean offset =" + str(offset)) + self.apply(lambda x: x[self.vcol] - offset, self.vcol) + return self + + def symmetrise(self): + """Decompose the data into symmetric and antisymmetric parts.""" + if self.make_symmetric: + self.decompose() + self.setas(x=self.setas.x, y="Symmetric") return self def plot_results(self): """Do the plotting of the data and the results.""" self.figure() # Make a new figure and show the results self.plot_xy( - self.vcol, - [self.gcol, "Fit"], + self.setas.x, + self.setas.y + ["Fit"], fmt=["ro", "b-"], label=["Data", "Fit"], ) @@ -151,7 +173,7 @@ def plot_results(self): def Fit(self): """Run the fitting code.""" - self.Discard().Normalise().offset_correct() + self.Discard().offset_correct().symmetrise().Normalise() chi2 = self.p0.shape[0] > 1 method = getattr(self, self.method)