File: functions.py

package info (click to toggle)
python-mcstasscript 0.0.46%2Bgit20250402111921.bfa5a26-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,440 kB
  • sloc: python: 13,421; makefile: 14
file content (367 lines) | stat: -rw-r--r-- 10,392 bytes parent folder | download
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import yaml
import os

from mcstasscript.data.data import McStasData
import mcstasscript.helper.managed_mcrun as managed_mcrun


def name_search(name, data_list):
    """"
    name_search returns McStasData instance with specific name if it is
    in the given data_list. If no match is found, a search for the data
    filename is performed. If several matches are found, a list of
    McStasData objects are returned.

    The index of certain datasets in the data_list can change if
    additional monitors are added so it is more convenient to access
    the data files using their names.

    Parameters
    ----------
    name : string
        Name of the dataset to be retrieved (component_name)

    data_list : List of McStasData instances
        List of datasets to search
    """
    if type(data_list) is not list:
        raise RuntimeError(
            "name_search function needs list of McStasData as input.")

    if len(data_list) == 0:
        raise RuntimeError("Given data list empty.")

    if not isinstance(data_list[0], McStasData):
        raise RuntimeError(
            "name_search function needs objects of type McStasData as input.")

    # Search by component name
    list_result = []
    for check in data_list:
        if check.name == name:
            list_result.append(check)

    if len(list_result) == 0:
        # Search by filename
        for check in data_list:
            if check.metadata.filename == name:
                list_result.append(check)

    if len(list_result) == 0:
        raise NameError("No dataset with name: \""
                        + name
                        + "\" found.")

    if len(list_result) == 1:
        return list_result[0]
    else:
        return list_result


def name_plot_options(name, data_list, **kwargs):
    """"
    name_plot_options passes keyword arguments to dataset with certain
    name in given data list

    Function for quickly setting plotting options on a certain dataset
    n a larger list of datasets

    Parameters
    ----------
    name : string
        Name of the dataset to be modified (component_name)

    data_list : List of McStasData instances
        List of datasets to search

    kwargs : keyword arguments
        Keyword arguments passed to set_plot_options in
        McStasPlotOptions
    """
    object_to_modify = name_search(name, data_list)
    if type(object_to_modify) is not list:
        object_to_modify.set_plot_options(**kwargs)
    else:
        for data_object in object_to_modify:
            data_object.set_plot_options(**kwargs)

def load_data(foldername):
    """
    Loads data from a McStas data folder including mccode.sim

    Parameters
    ----------
        foldername : string
            Name of the folder from which to load data
    """
    if not os.path.isdir(foldername):
        raise RuntimeError("Could not find specified foldername for"
                           + "load_data:" + str(foldername))

    return managed_mcrun.load_results(foldername)

def load_metadata(data_folder_name):
    """
    Function that loads metadata from a mcstas simulation

    Returns list of metadata objects corresponding to each monitor, all
    information is taken from mccode.sim file.

    Parameters
    ----------

    first argument : str
        path to folder from which metadata should be loaded
    """
    return managed_mcrun.load_metadata(data_folder_name)

def load_monitor(metadata, data_folder_name):
    """
    Function that loads data given metadata and name of data folder

    Loads data for single monitor and returns a McStasData object

    Parameters
    ----------

    metadata : McStasMetaData object
        McStasMetaData object corresponding to the monitor to be loaded

    data_folder_name : str
        path to folder from which metadata should be loaded
    """
    return managed_mcrun.load_monitor(metadata, data_folder_name)


class Configurator:
    """
    Class for setting the configuration file for McStasScript.

    Attributes
    ----------
    configuration_file_name : str
        absolute path of configuration file

    Methods
    -------
    set_mcstas_path(string)
        sets mcstas path

    set_mcrun_path(string)
        sets mcrun path

    set_mcxtrace_path(string)
        sets mcxtrace path

    set_mxrun_path(string)
        sets mxrun path

    set_line_length(int)
        sets maximum line length to given int

    _write_yaml(dict)
        internal method, writes a configuration yaml file with dict content

    _read_yaml()
        internal method, reads a configuration yaml file and returns a dict

    _create_new_config_file()
        internal method, creates default configuration file

    """

    def __init__(self, *args):
        """
        Initialization of configurator, checks that the configuration file
        actually exists, and if it does not, creates a default configuration
        file.

        Parameters
        ----------
        (optional) custom name : str
            Custom name for configuration file for testing purposes
        """

        if len(args) == 1:
            name = args[0]
        else:
            name = "configuration"

        # check configuration file exists
        THIS_DIR = os.path.dirname(os.path.abspath(__file__))
        conf_file = os.path.join(THIS_DIR, "..", name + ".yaml")
        self.configuration_file_name = conf_file
        if not os.path.isfile(self.configuration_file_name):
            # no config file found, write default config file
            self._create_new_config_file()

    def _write_yaml(self, dictionary):
        """
        Writes a dictionary as the new configuration file
        """
        with open(self.configuration_file_name, 'w') as yaml_file:
            yaml.dump(dictionary, yaml_file, default_flow_style=False)

    def _read_yaml(self):
        """
        Reads yaml configuration file
        """
        with open(self.configuration_file_name, 'r') as ymlfile:
            return yaml.safe_load(ymlfile)

    def _create_new_config_file(self):
        """
        Writes a default configuration file to the package root directory
        """

        run = "/usr/bin"
        mcstas = "/usr/share/mcstas/resources"

        mxrun = "/usr/bin"
        mcxtrace = "/usr/share/mcxtrace/resources"

        default_paths = {"mcrun_path": run,
                         "mcstas_path": mcstas,
                         "mxrun_path": mxrun,
                         "mcxtrace_path": mcxtrace}

        default_other = {"characters_per_line": 85}

        default_config = {"paths": default_paths, "other": default_other}

        self._write_yaml(default_config)

    def set_mcstas_path(self, path):
        """
        Sets the path to McStas

        Parameters
        ----------
        path : str
            Path to the mcstas directory containing "sources", "optics", ...
        """

        if not os.path.isdir(path):
            raise RuntimeError("Invalid path given to set_mcstas_path:"
                               + str(path))

        # read entire configuration file
        config = self._read_yaml()

        # update mcstas_path
        config["paths"]["mcstas_path"] = path

        # write new configuration file
        self._write_yaml(config)

    def set_mcrun_path(self, path):
        """
        Sets the path to mcrun

        Parameters
        ----------
        path : str
            Path to the mcrun executable
        """

        if not os.path.isdir(path):
            raise RuntimeError("Invalid path given to set_mcrun_path:"
                               + str(path))

        # read entire configuration file
        config = self._read_yaml()

        # update mcstas_path
        config["paths"]["mcrun_path"] = path

        # write new configuration file
        self._write_yaml(config)

    def set_mcxtrace_path(self, path):
        """
        Sets the path to McXtrace

        Parameters
        ----------
        path : str
            Path to the mcxtrace directory containing "sources", "optics", ...
        """

        if not os.path.isdir(path):
            raise RuntimeError("Invalid path given to set_mcxtrace_path:"
                               + str(path))

        # read entire configuration file
        config = self._read_yaml()

        # update mcxtrace_path
        config["paths"]["mcxtrace_path"] = path

        # write new configuration file
        self._write_yaml(config)

    def set_mxrun_path(self, path):
        """
        Sets the path to mxrun

        Parameters
        ----------
        path : str
            Path to the mxrun executable
        """

        if not os.path.isdir(path):
            raise RuntimeError("Invalid path given to set_mxrun_path: "
                               + str(path))

        # read entire configuration file
        config = self._read_yaml()

        # update mxrun_path
        config["paths"]["mxrun_path"] = path

        # write new configuration file
        self._write_yaml(config)

    def set_line_length(self, line_length):
        """
        Sets maximum line length for output

        Parameters
        ----------
        line_length : int
            maximum line length for output
        """

        if not isinstance(line_length, int):
            raise ValueError("Given line length in set_line_length not an "
                             + "integer.")

        if line_length < 1:
            raise ValueError("Line length specified in set_line_length must"
                             + " be positve, given length: "
                             + str(line_length))

        # read entire configuration file
        config = self._read_yaml()

        # update mcstas_path
        config["other"]["characters_per_line"] = int(line_length)

        # write new configuration file
        self._write_yaml(config)

    def __repr__(self):
        string = "Configurator:\n"
        config = self._read_yaml()
        if "paths" in config:
            string += " paths:\n"
            for key, value in config["paths"].items():
                string += "  " + str(key) + ": " + str(value) + "\n"

        if "other" in config:
            string += " other:\n"
            for key, value in config["other"].items():
                string += "  " + str(key) + ": " + str(value) + "\n"

        return string