forked from diffpy/diffpy.srfit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinpututils.py
More file actions
116 lines (96 loc) · 3.34 KB
/
inpututils.py
File metadata and controls
116 lines (96 loc) · 3.34 KB
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
#!/usr/bin/env python
##############################################################################
#
# diffpy.srfit by DANSE Diffraction group
# Simon J. L. Billinge
# (c) 2010 The Trustees of Columbia University
# in the City of New York. All rights reserved.
#
# File coded by: Chris Farrow
#
# See AUTHORS.txt for a list of people who contributed.
# See LICENSE_DANSE.txt for license information.
#
##############################################################################
"""Input utilities."""
__all__ = ["inputToString"]
import os.path
from pathlib import Path
def inputToString(input):
"""Convert input from various modes to a string.
This is useful when you want a method to accept a string, open file object
or file name.
Attributes
----------
input
An open file-like object, name of a file
or a string containing the input.
Returns the input in a string
Raises IOError if the input is supected to be a file name, but the file
cannot be found.
"""
# Get the input into a string
inptstr = ""
if hasattr(input, "read"):
inptstr = input.read()
# TODO remove handling of string input accept only file or filename
# FIXME check for typos in the file name
elif os.path.exists(input) or (len(input) < 80 and input.count("\n") == 0):
with open(input, "r") as infile:
inptstr = infile.read()
else:
inptstr = input
return inptstr
def get_dict_from_results_file(
results_filepath: Path | str,
) -> dict[str, float]:
"""Get a dictionary of parameter names and values from a results
file.
The file should have lines in the format:
"parameter_name value +/- uncertainty". Lines that do not match this
format will be ignored.
Parameters
----------
results_filepath : pathlib.Path or str
The path to the results file.
Returns
-------
parsed_results_dict : dict
The dictionary where keys are parameter names and values are the
corresponding parameter values as floats.
"""
with open(results_filepath, "r") as f:
results_string = f.read()
parsed_results_dict = {}
for raw_line in results_string.splitlines():
line = raw_line.strip()
# skip blank lines and lines that are just dashes
if not line or set(line) == {"-"}:
continue
line_items = line.split()
if len(line_items) < 2:
continue
if len(line_items) >= 4 and line_items[2] == "+/-":
try:
parsed_results_dict[line_items[0]] = float(line_items[1])
except ValueError:
pass
return parsed_results_dict
def get_dict_from_results_object(results_object) -> dict[str, float]:
"""Get a dictionary of parameter names and values from a FitResults
object.
Parameters
----------
results_object : FitResults
The FitResults object containing the parameter names and values.
Returns
-------
params_dict : dict
The dictionary where keys are parameter names and values are the
corresponding parameter values as floats.
"""
param_names = results_object.varnames
param_vals = results_object.varvals
params_dict = dict(zip(param_names, param_vals))
return params_dict
# End of file