Skip to content

Commit 2c550cc

Browse files
Take out instr pars file out of initialization
And make classes dataclasses
1 parent f7b610e commit 2c550cc

File tree

5 files changed

+42
-63
lines changed

5 files changed

+42
-63
lines changed

src/mvesuvio/analysis_fitting.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import jacobi
1010
import time
1111

12+
from mvesuvio.util import handle_config
13+
1214
repoPath = Path(__file__).absolute().parent # Path to the repository
1315

1416

@@ -1507,7 +1509,8 @@ def extractData(ws, wsRes, ic):
15071509

15081510

15091511
def loadInstrParsFileIntoArray(ic):
1510-
data = np.loadtxt(ic.InstrParsPath, dtype=str)[1:].astype(float)
1512+
ipFilesPath = Path(handle_config.read_config_var("caching.ipfolder"))
1513+
data = np.loadtxt(str(ipFilesPath / ic.instrParsFile), dtype=str)[1:].astype(float)
15111514
spectra = data[:, 0]
15121515
select_rows = np.where((spectra >= ic.firstSpec) & (spectra <= ic.lastSpec))
15131516
instrPars = data[select_rows]

src/mvesuvio/analysis_routines.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
loadRawAndEmptyWsFromUserPath, cropAndMaskWorkspace, \
1010
NeutronComptonProfile, calculate_h_ratio
1111
from mvesuvio.analysis_reduction import VesuvioAnalysisRoutine
12+
from mvesuvio.util import handle_config
13+
14+
from pathlib import Path
1215

1316
def _create_analysis_object_from_current_interface(IC, running_tests=False):
1417
ws = loadRawAndEmptyWsFromUserPath(
@@ -40,10 +43,12 @@ def _create_analysis_object_from_current_interface(IC, running_tests=False):
4043

4144
profiles_table = create_profiles_table(cropedWs.name()+"_initial_parameters", profiles)
4245

46+
ipFilesPath = Path(handle_config.read_config_var("caching.ipfolder"))
47+
4348
kwargs = {
4449
"InputWorkspace": cropedWs.name(),
4550
"InputProfiles": profiles_table.name(),
46-
"InstrumentParametersFile": str(IC.InstrParsPath),
51+
"InstrumentParametersFile": str(ipFilesPath / IC.instrParsFile),
4752
"HRatioToLowestMass": IC.HToMassIdxRatio if hasattr(IC, 'HRatioToLowestMass') else 0,
4853
"NumberOfIterations": int(IC.noOfMSIterations),
4954
"InvalidDetectors": IC.maskedSpecAllNo.astype(int).tolist(),

src/mvesuvio/config/analysis_inputs.py

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import numpy as np
2+
from dataclasses import dataclass
23

34

5+
@dataclass
46
class LoadVesuvioBackParameters:
5-
def __init__(self, ipFilesPath):
6-
self.ipfile = ipFilesPath / "ip2019.par"
7+
ipfile = "ip2019.par"
78

89
runs = "43066-43076" # 77K # The numbers of the runs to be analysed
910
empty_runs = (
@@ -16,9 +17,9 @@ def __init__(self, ipFilesPath):
1617
scaleRaw = 1
1718

1819

20+
@dataclass
1921
class LoadVesuvioFrontParameters:
20-
def __init__(self, ipFilesPath):
21-
self.ipfile = ipFilesPath / "ip2018_3.par"
22+
ipfile = "ip2018_3.par"
2223

2324
runs = "43066-43076" # 100K # The numbers of the runs to be analysed
2425
empty_runs = (
@@ -31,34 +32,34 @@ def __init__(self, ipFilesPath):
3132
scaleRaw = 1
3233

3334

35+
@dataclass
3436
class GeneralInitialConditions:
3537
"""Used to define initial conditions shared by both Back and Forward scattering"""
3638

3739
# Sample slab parameters
3840
vertical_width, horizontal_width, thickness = 0.1, 0.1, 0.001 # Expressed in meters
3941

4042

43+
@dataclass
4144
class BackwardInitialConditions(GeneralInitialConditions):
42-
def __init__(self, ipFilesPath):
43-
self.InstrParsPath = ipFilesPath / "ip2018_3.par"
45+
instrParsFile = "ip2018_3.par"
4446

4547
HToMassIdxRatio = 19.0620008206 # Set to zero or None when H is not present
4648

4749
# Masses, instrument parameters and initial fitting parameters
4850
masses = np.array([12, 16, 27])
49-
# noOfMasses = len(masses)
5051

5152
# Intensities, NCP widths, NCP centers
5253
initPars = np.array([1, 12, 0.0, 1, 12, 0.0, 1, 12.5, 0.0])
5354
bounds = np.array(
5455
[
55-
[0, np.nan],
56+
[0, None],
5657
[8, 16],
5758
[-3, 1],
58-
[0, np.nan],
59+
[0, None],
5960
[8, 16],
6061
[-3, 1],
61-
[0, np.nan],
62+
[0, None],
6263
[11, 14],
6364
[-3, 1],
6465
]
@@ -83,31 +84,28 @@ def __init__(self, ipFilesPath):
8384
multiple_scattering_order = 2
8485
number_of_events = 1.0e5
8586

86-
# Original data uses histogram data instead of point data
87-
runHistData = True
88-
normVoigt = False
89-
9087

88+
@dataclass
9189
class ForwardInitialConditions(GeneralInitialConditions):
92-
def __init__(self, ipFilesPath):
93-
self.InstrParsPath = ipFilesPath / "ip2018_3.par"
90+
91+
instrParsFile = "ip2018_3.par"
9492

9593
masses = np.array([1.0079, 12, 16, 27])
9694

9795
# Intensities, NCP widths, NCP centers
9896
initPars = np.array([1, 4.7, 0, 1, 12.71, 0.0, 1, 8.76, 0.0, 1, 13.897, 0.0])
9997
bounds = np.array(
10098
[
101-
[0, np.nan],
99+
[0, None],
102100
[3, 6],
103101
[-3, 1],
104-
[0, np.nan],
102+
[0, None],
105103
[12.71, 12.71],
106104
[-3, 1],
107-
[0, np.nan],
105+
[0, None],
108106
[8.76, 8.76],
109107
[-3, 1],
110-
[0, np.nan],
108+
[0, None],
111109
[13.897, 13.897],
112110
[-3, 1],
113111
]
@@ -131,11 +129,8 @@ def __init__(self, ipFilesPath):
131129
multiple_scattering_order = 2
132130
number_of_events = 1.0e5
133131

134-
# Original data uses histogram data instead of point data
135-
runHistData = True
136-
normVoigt = False
137-
138132

133+
@dataclass
139134
class YSpaceFitInitialConditions:
140135
showPlots = True
141136
symmetrisationFlag = True
@@ -147,6 +142,7 @@ class YSpaceFitInitialConditions:
147142
maskTypeProcedure = "NAN" # Options: 'NCP', 'NAN', None
148143

149144

145+
@dataclass
150146
class UserScriptControls:
151147
runRoutine = True
152148

src/mvesuvio/main/analysis_runner.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@
88

99
def run(yes_to_all=False):
1010
inputs_path = Path(handle_config.read_config_var("caching.inputs"))
11-
ipFilesPath = Path(handle_config.read_config_var("caching.ipfolder"))
1211

1312
ai = import_from_path(inputs_path, "analysis_inputs")
1413

1514
start_time = time.time()
1615

17-
wsBackIC = ai.LoadVesuvioBackParameters(ipFilesPath)
18-
wsFrontIC = ai.LoadVesuvioFrontParameters(ipFilesPath)
19-
bckwdIC = ai.BackwardInitialConditions(ipFilesPath)
16+
wsBackIC = ai.LoadVesuvioBackParameters
17+
wsFrontIC = ai.LoadVesuvioFrontParameters
18+
bckwdIC = ai.BackwardInitialConditions
2019
fwdIC = ai.ForwardInitialConditions
2120
yFitIC = ai.YSpaceFitInitialConditions
2221
userCtr = ai.UserScriptControls

src/mvesuvio/util/process_inputs.py

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,6 @@ def completeICFromInputs(IC, wsIC):
4747
IC.scaleEmpty = wsIC.scaleEmpty
4848
IC.scaleRaw = wsIC.scaleRaw
4949

50-
# When attribute InstrParsPath is not present, set it equal to path from wsIC
51-
try:
52-
IC.InstrParsPath # If present, leave it unaltered
53-
except AttributeError:
54-
IC.InstrParsPath = wsIC.ipfile
55-
5650
# Sort out input and output paths
5751
rawPath, emptyPath = setInputWSForSample(wsIC)
5852

@@ -70,26 +64,6 @@ def completeICFromInputs(IC, wsIC):
7064
# Default not running preliminary procedure to estimate HToMass0Ratio
7165
IC.runningPreliminary = False
7266

73-
# Create default of not running original version with histogram data
74-
try:
75-
IC.runHistData
76-
except AttributeError:
77-
IC.runHistData = False
78-
79-
# Norm voigt except when comparing with tests
80-
try:
81-
IC.normVoigt
82-
except AttributeError:
83-
IC.normVoigt = True
84-
85-
#Create default for H ratio
86-
# Only for completeness' sake, will be removed anyway
87-
# when transition to new interface is complete
88-
try:
89-
IC.HToMassIdxRatio
90-
except AttributeError:
91-
IC.HToMassIdxRatio = None
92-
9367
return
9468

9569

@@ -108,23 +82,25 @@ def setInputWSForSample(wsIC):
10882
rawPath = inputWSPath / rawWSName
10983
emptyPath = inputWSPath / emptyWSName
11084

85+
ipFilesPath = Path(handle_config.read_config_var("caching.ipfolder"))
86+
11187
if not wsHistoryMatchesInputs(wsIC.runs, wsIC.mode, wsIC.ipfile, rawPath):
112-
saveWSFromLoadVesuvio(wsIC.runs, wsIC.mode, wsIC.ipfile, rawPath)
88+
saveWSFromLoadVesuvio(wsIC.runs, wsIC.mode, str(ipFilesPath/wsIC.ipfile), rawPath)
11389

11490
if not wsHistoryMatchesInputs(wsIC.empty_runs, wsIC.mode, wsIC.ipfile, emptyPath):
115-
saveWSFromLoadVesuvio(wsIC.empty_runs, wsIC.mode, wsIC.ipfile, emptyPath)
91+
saveWSFromLoadVesuvio(wsIC.empty_runs, wsIC.mode, str(ipFilesPath/wsIC.ipfile), emptyPath)
11692

11793
return rawPath, emptyPath
11894

11995

12096
def getRunningMode(wsIC):
121-
if wsIC.__class__.__name__ == "LoadVesuvioBackParameters":
97+
if wsIC.__name__ == "LoadVesuvioBackParameters":
12298
runningMode = "backward"
123-
elif wsIC.__class__.__name__ == "LoadVesuvioFrontParameters":
99+
elif wsIC.__name__ == "LoadVesuvioFrontParameters":
124100
runningMode = "forward"
125101
else:
126102
raise ValueError(
127-
f"Input class for loading workspace not valid: {wsIC.__class__.__name__}"
103+
f"Input class for loading workspace not valid: {wsIC.__name__}"
128104
)
129105
return runningMode
130106

@@ -177,9 +153,9 @@ def wsHistoryMatchesInputs(runs, mode, ipfile, localPath):
177153
return False
178154

179155
saved_ipfile_name = ntpath.basename(metadata.getPropertyValue("InstrumentParFile"))
180-
if saved_ipfile_name != ipfile.name:
156+
if saved_ipfile_name != ipfile:
181157
logger.notice(
182-
f"IP files in saved workspace did not match: {saved_ipfile_name} and {ipfile.name}"
158+
f"IP files in saved workspace did not match: {saved_ipfile_name} and {ipfilename}"
183159
)
184160
return False
185161

0 commit comments

Comments
 (0)