Skip to content

Commit

Permalink
Merge pull request #136 from mantidproject/update-inputs-file
Browse files Browse the repository at this point in the history
Clean up of user inputs file and restructure of files
  • Loading branch information
GuiMacielPereira authored Nov 14, 2024
2 parents f7b610e + 2b039a6 commit d84e4ae
Show file tree
Hide file tree
Showing 33 changed files with 819 additions and 1,119 deletions.
135 changes: 135 additions & 0 deletions legacy/original_analysis/process_inputs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
from mvesuvio.util import handle_config

def completeBootIC(bootIC, bckwdIC, fwdIC, yFitIC):
if not (bootIC.runBootstrap):
return

try: # Assume it is not running a test if atribute is not found
bootIC.runningTest
except AttributeError:
bootIC.runningTest = False

setBootstrapDirs(bckwdIC, fwdIC, bootIC, yFitIC)
return


def setBootstrapDirs(bckwdIC, fwdIC, bootIC, yFitIC):
"""Form bootstrap output data paths"""
experimentPath = _get_expr_path()
scriptName = handle_config.get_script_name()

# Select script name and experiments path
sampleName = bckwdIC.scriptName # Name of sample currently running

# Used to store running times required to estimate Bootstrap total run time.
bootIC.runTimesPath = experimentPath / "running_times.txt"

# Make bootstrap and jackknife data directories
if bootIC.bootstrapType == "JACKKNIFE":
bootPath = experimentPath / "jackknife_data"
else:
bootPath = experimentPath / "bootstrap_data"
bootPath.mkdir(exist_ok=True)

# Folders for skipped and unskipped MS
if bootIC.skipMSIterations:
dataPath = bootPath / "skip_MS_corrections"
else:
dataPath = bootPath / "with_MS_corrections"
dataPath.mkdir(exist_ok=True)

# Create text file for logs
logFilePath = dataPath / "data_files_log.txt"
if not (logFilePath.is_file()):
with open(logFilePath, "w") as txtFile:
txtFile.write(header_string())

for IC in [bckwdIC, fwdIC]: # Make save paths for .npz files
bootName, bootNameYFit = genBootFilesName(IC, bootIC)

IC.bootSavePath = (
dataPath / bootName
) # works because modeRunning has same strings as procedure
IC.bootYFitSavePath = dataPath / bootNameYFit

IC.logFilePath = logFilePath
IC.bootSavePathLog = logString(bootName, IC, yFitIC, bootIC, isYFit=False)
IC.bootYFitSavePathLog = logString(
bootNameYFit, IC, yFitIC, bootIC, isYFit=True
)
return


def genBootFilesName(IC, bootIC):
"""Generates save file name for either BACKWARD or FORWARD class"""

nSamples = bootIC.nSamples
if bootIC.bootstrapType == "JACKKNIFE":
nSamples = 3 if bootIC.runningTest else noOfHistsFromTOFBinning(IC)

# Build Filename based on ic
corr = ""
if IC.MSCorrectionFlag & (IC.noOfMSIterations > 0):
corr += "_MS"
if IC.GammaCorrectionFlag & (IC.noOfMSIterations > 0):
corr += "_GC"

fileName = f"spec_{IC.firstSpec}-{IC.lastSpec}_iter_{IC.noOfMSIterations}{corr}"
bootName = fileName + f"_nsampl_{nSamples}" + ".npz"
bootNameYFit = fileName + "_ySpaceFit" + f"_nsampl_{nSamples}" + ".npz"
return bootName, bootNameYFit


def header_string():
return """
This file contains some information about each data file in the folder.
ncp data file: boot type | procedure | tof binning | masked tof range.
yspace fit data file: boot type | procedure | symmetrisation | rebin pars | fit model | mask type
"""


def logString(bootDataName, IC, yFitIC, bootIC, isYFit):
if isYFit:
log = (
bootDataName
+ " : "
+ bootIC.bootstrapType
+ " | "
+ str(bootIC.fitInYSpace)
+ " | "
+ str(yFitIC.symmetrisationFlag)
+ " | "
+ yFitIC.rebinParametersForYSpaceFit
+ " | "
+ yFitIC.fitModel
+ " | "
+ str(yFitIC.maskTypeProcedure)
)
else:
log = (
bootDataName
+ " : "
+ bootIC.bootstrapType
+ " | "
+ str(bootIC.procedure)
+ " | "
+ IC.tofBinning
+ " | "
+ str(IC.maskTOFRange)
)
return log


def noOfHistsFromTOFBinning(IC):
# Convert first to float and then to int because of decimal points
start, spacing, end = [int(float(s)) for s in IC.tofBinning.split(",")]
return int((end - start) / spacing) - 1 # To account for last column being ignored


def buildFinalWSName(procedure: str, IC):
scriptName = handle_config.get_script_name()
# Format of corrected ws from last iteration
name = scriptName + "_" + procedure + "_" + str(IC.noOfMSIterations)
return name


13 changes: 9 additions & 4 deletions src/mvesuvio/analysis_fitting.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import jacobi
import time

from mvesuvio.util import handle_config

repoPath = Path(__file__).absolute().parent # Path to the repository


Expand Down Expand Up @@ -64,7 +66,7 @@ def extractNCPFromWorkspaces(wsFinal, ic):

# Ensure shape of ncp matches data
shape = ncpForEachMass.shape
assert shape[0] == ic.noOfMasses
assert shape[0] == len(ic.masses)
assert shape[1] == wsFinal.getNumberHistograms()
# Final dimension can be missing last col or not
assert (shape[2] == wsFinal.blocksize()) | (shape[2] == wsFinal.blocksize() - 1)
Expand Down Expand Up @@ -93,7 +95,8 @@ def calculateMantidResolutionFirstMass(IC, yFitIC, ws):
else:
AppendSpectra(resName, "tmp", OutputWorkspace=resName)

MaskDetectors(resName, WorkspaceIndexList=IC.maskedDetectorIdx)
masked_idx = [ws.spectrumInfo().isMasked(i) for i in range(ws.getNumberHistograms())]
MaskDetectors(resName, WorkspaceIndexList=np.flatnonzero(masked_idx))
wsResSum = SumSpectra(InputWorkspace=resName, OutputWorkspace=resName + "_Sum")

normalise_workspace(wsResSum)
Expand Down Expand Up @@ -125,7 +128,8 @@ def subtractAllMassesExceptFirst(ic, ws, ncpForEachMass):

wsSubMass = CloneWorkspace(InputWorkspace=ws, OutputWorkspace=ws.name() + "_Mass0")
passDataIntoWS(dataX, dataY, dataE, wsSubMass)
MaskDetectors(Workspace=wsSubMass, WorkspaceIndexList=ic.maskedDetectorIdx)
wsMask, maskList = ExtractMask(ws)
MaskDetectors(Workspace=wsSubMass, MaskedWorkspace=wsMask)
SumSpectra(
InputWorkspace=wsSubMass.name(), OutputWorkspace=wsSubMass.name() + "_Sum"
)
Expand Down Expand Up @@ -1507,7 +1511,8 @@ def extractData(ws, wsRes, ic):


def loadInstrParsFileIntoArray(ic):
data = np.loadtxt(ic.InstrParsPath, dtype=str)[1:].astype(float)
ipFilesPath = Path(handle_config.read_config_var("caching.ipfolder"))
data = np.loadtxt(str(ipFilesPath / ic.ipfile), dtype=str)[1:].astype(float)
spectra = data[:, 0]
select_rows = np.where((spectra >= ic.firstSpec) & (spectra <= ic.lastSpec))
instrPars = data[select_rows]
Expand Down
3 changes: 2 additions & 1 deletion src/mvesuvio/analysis_reduction.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,8 @@ def _create_means_table(self):
self._mean_intensity_ratios,
self._std_intensity_ratios,
):
table.addRow([label, mass, mean_width, std_width, mean_intensity, std_intensity])
# Explicit conversion to float required to match profiles table
table.addRow([label, float(mass), float(mean_width), float(std_width), float(mean_intensity), float(std_intensity)])
self.log().notice(f"{label:6s} {mean_width:10.5f} \u00B1 {std_width:7.5f}" + \
f"{mean_intensity:10.5f} \u00B1 {std_intensity:7.5f}\n")

Expand Down
Loading

0 comments on commit d84e4ae

Please sign in to comment.