forked from BAMresearch/McSAS3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmcsas3_cli_histogrammer.py
137 lines (112 loc) · 4.28 KB
/
mcsas3_cli_histogrammer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
from attrs import define, validators, field
from pathlib import Path
from numpy import save
from mcsas3 import McHat
from mcsas3 import McPlot
from mcsas3 import McData1D # , McData2D
import pandas as pd
from mcsas3.mcmodelhistogrammer import McModelHistogrammer
from mcsas3.mcanalysis import McAnalysis
import yaml
import argparse
# import logging
import multiprocessing
import sys # , os
from sys import platform
@define
class McSAS3_cli_hist:
"""Runs the McSAS histogrammer from the command line arguments"""
def checkConfig(self, attribute, value):
assert value.exists(), f"configuration file {value} must exist"
assert (
value.suffix == ".yaml"
), "configuration file must be a yaml file (and end in .yaml)"
resultFile: Path = field(kw_only=True, validator=validators.instance_of(Path))
histConfigFile: Path = field(
kw_only=True, validator=[validators.instance_of(Path), checkConfig]
)
resultIndex: int = field(
kw_only=True, validator=[validators.instance_of(int)]
)
def run(self):
# read the configuration file
# load the data
mds = McData1D.McData1D(loadFromFile=self.resultFile, resultIndex=self.resultIndex)
# read the configuration file
with open(self.histConfigFile, "r") as f:
histRanges = pd.DataFrame(list(yaml.safe_load_all(f)))
# run the Monte Carlo method
md = mds.measData.copy()
mcres = McAnalysis(self.resultFile, md, histRanges, store=True, resultIndex=self.resultIndex)
# plotting:
# plot the histogram result
mp = McPlot.McPlot()
# output file for plot:
saveHistFile = self.resultFile.with_suffix(".pdf")
if saveHistFile.is_file():
saveHistFile.unlink()
mp.resultCard(mcres, saveHistFile=saveHistFile)
# adapted from: https://stackoverflow.com/questions/8220108/how-do-i-check-the-operating-system-in-python
def isLinux():
return platform == "linux" or platform == "linux2"
def isMac():
return platform == "darwin"
def isWindows():
return platform == "win32"
if __name__ == "__main__":
multiprocessing.freeze_support()
# manager=pyplot.get_current_fig_manager()
# print manager
# process input arguments
parser = argparse.ArgumentParser(
description="""
Runs a McSAS histogramming from the command line.
For this to work, you need to have YAML-formatted configuration files ready,
both for the input file read parameters, as well as for the optimization set-up.
The histogrammer furthermore needs a result file from the McSAS optimization.
If you do not have that result file, please run the McSAS optimization first before
attempting to histogram the results.
Examples of these configuration files are provided in the example_configurations subdirectory.
Released under a GPLv3+ license.
"""
)
parser.add_argument(
"-r",
"--resultFile",
type=lambda p: Path(p).absolute(),
default=Path(__file__).absolute().parent / "test.nxs",
help="Path to the file with the McSAS3 optimization result",
required=True,
)
parser.add_argument(
"-H",
"--histConfigFile",
type=lambda p: Path(p).absolute(),
default=Path("./example_configurations/hist_config_dual.yaml"),
help="Path to the filename with the histogramming configuration",
# required=True,
)
parser.add_argument(
"-i",
"--resultIndex",
type=int,
default=1,
help="The result index to work on, in case you want multiple McSAS runs on the same data",
# required=True,
)
if isMac():
# on OSX remove automatically provided PID,
# otherwise argparse exits and the bundle start fails silently
for i in range(len(sys.argv)):
if sys.argv[i].startswith("-psn"): # PID provided by osx
del sys.argv[i]
try:
args = parser.parse_args()
except SystemExit:
raise
# initiate logging (to console stderr for now)
# replaceStdOutErr() # replace all text output with our sinks
# testing:
adict = vars(args)
m = McSAS3_cli_hist(**adict)
m.run()