Skip to content

Commit

Permalink
Tweak PCAR script
Browse files Browse the repository at this point in the history
  • Loading branch information
gb119 committed Dec 25, 2023
1 parent 55d2eeb commit 289da44
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Stoner/plot/core.py
Expand Up @@ -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")
Expand Down
2 changes: 2 additions & 0 deletions scripts/PCAR-New.ini
Expand Up @@ -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
#
Expand Down
58 changes: 40 additions & 18 deletions scripts/PCAR-New.py
Expand Up @@ -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):

Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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"],
)
Expand All @@ -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)
Expand Down

0 comments on commit 289da44

Please sign in to comment.