Commit 3b31c7e2 authored by William Clarke's avatar William Clarke
Browse files

Slight reworking of rescaling. Makes fitting more robust.

parent a9fa0d04
...@@ -246,6 +246,23 @@ class MRS(object): ...@@ -246,6 +246,23 @@ class MRS(object):
# Helper functions # Helper functions
def processForFitting(self,ppmlim=(.2,4.2),):
""" Apply rescaling and run the conjugation checks"""
self.check_FID(ppmlim=ppmlim,repair=True)
self.check_Basis(ppmlim=ppmlim,repair=True)
self.rescaleForFitting()
def rescaleForFitting(self,scale=100):
""" Apply rescaling across data, basis and H20"""
scaledFID,scaling = misc.rescale_FID(self.FID,scale=scale)
self.set_FID(scaledFID)
if self.H2O is not None:
self.H2O *= scaling
if self.basis is not None:
self.basis,_ = misc.rescale_FID(self.basis,scale=scale)
def check_FID(self,ppmlim=(.2,4.2),repair=False): def check_FID(self,ppmlim=(.2,4.2),repair=False):
""" """
Check if FID needs to be conjugated Check if FID needs to be conjugated
......
...@@ -159,17 +159,9 @@ def main(): ...@@ -159,17 +159,9 @@ def main():
FID,dataheader = mrs_io.read_FID(args.data) FID,dataheader = mrs_io.read_FID(args.data)
basis, names, basisheader = mrs_io.read_basis(args.basis) basis, names, basisheader = mrs_io.read_basis(args.basis)
# Rescale FID and basis to have nice range
if not args.no_rescale:
FID = misc.rescale_FID(FID,scale=100)
basis = misc.rescale_FID(basis,scale=100)
if args.h2o is not None: if args.h2o is not None:
H2O,_ = mrs_io.read_FID(args.h2o) H2O,_ = mrs_io.read_FID(args.h2o)
if not args.no_rescale:
H2O = misc.rescale_FID(H2O,scale=100)
else: else:
H2O = None H2O = None
...@@ -220,6 +212,10 @@ def main(): ...@@ -220,6 +212,10 @@ def main():
if args.verbose: if args.verbose:
if conjugated == 1: if conjugated == 1:
warnings.warn('Basis has been checked and conjugated. Please check!',UserWarning) warnings.warn('Basis has been checked and conjugated. Please check!',UserWarning)
# Rescale FID, H2O and basis to have nice range
if not args.no_rescale:
mrs.rescaleForFitting()
# Do phase correction # Do phase correction
if args.phase_correct: if args.phase_correct:
......
...@@ -398,7 +398,8 @@ def fit_FSLModel(mrs, ...@@ -398,7 +398,8 @@ def fit_FSLModel(mrs,
verbose=False, verbose=False,
monitor=True, monitor=True,
args=args) args=args)
results.optim_out = res_vb
# de-log # de-log
if model.lower()=='lorentzian': if model.lower()=='lorentzian':
logcon,loggamma,eps,phi0,phi1,b = x2p(res_vb.x,mrs.numBasis,g) logcon,loggamma,eps,phi0,phi1,b = x2p(res_vb.x,mrs.numBasis,g)
......
...@@ -434,7 +434,7 @@ def rescale_FID(x,scale=100): ...@@ -434,7 +434,7 @@ def rescale_FID(x,scale=100):
else: else:
factor = np.linalg.norm(np.mean(y,axis=1),axis=0) factor = np.linalg.norm(np.mean(y,axis=1),axis=0)
y = y / factor * scale y = y / factor * scale
return y return y,1/factor * scale
def create_peak(mrs,ppm,gamma=0,sigma=0): def create_peak(mrs,ppm,gamma=0,sigma=0):
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment