easyxrd 0.1.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
easyxrd/__init__.py ADDED
@@ -0,0 +1,245 @@
1
+ import sys
2
+ import os
3
+ import subprocess
4
+
5
+ import importlib
6
+ from importlib.metadata import version
7
+
8
+ import shutil
9
+ import numpy as np
10
+ import urllib.request
11
+
12
+ import tarfile
13
+ import time
14
+
15
+ import platform
16
+
17
+
18
+ class HiddenPrints:
19
+ """
20
+ This class hides print outputs from functions. It is useful for processes like refinement which produce a lot of text prints.
21
+ """
22
+
23
+ def __enter__(self):
24
+ self._original_stdout = sys.stdout
25
+ sys.stdout = open(os.devnull, "w")
26
+
27
+ def __exit__(self, exc_type, exc_val, exc_tb):
28
+ sys.stdout.close()
29
+ sys.stdout = self._original_stdout
30
+
31
+
32
+ print("\n\nPython=%d.%d.%d | Platform=%s"%(sys.version_info.major,sys.version_info.minor,sys.version_info.micro,platform.platform()))
33
+ print("Checking required packages:\n")
34
+ # These are big python libraries that we will need in pySULI.
35
+ # If the required library doesn't exist, we can it via pip
36
+
37
+ required_packages = {
38
+ "numpy",
39
+ "scipy",
40
+ "xarray",
41
+ "ipympl",
42
+ "pymatgen",
43
+ "pyFAI",
44
+ "fabio",
45
+ "pybaselines",
46
+ "mp_api",
47
+ }
48
+
49
+ failed_packages = []
50
+ for rp in required_packages:
51
+ try:
52
+ globals()[rp] = importlib.import_module(rp)
53
+ print(
54
+ "---%s package with version %s is available and can be imported "
55
+ % (rp, version(rp))
56
+ )
57
+ except:
58
+ failed_packages.append(rp)
59
+
60
+
61
+ for fp in failed_packages:
62
+ print("\n\n----Failed to import %s" % fp)
63
+ print('Try installing it via `pip install %s`\n'% fp.replace('_','-'))
64
+
65
+
66
+
67
+ # defaults
68
+ easyxrd_defaults = dict()
69
+ user_home = os.path.expanduser("~")
70
+
71
+
72
+ # Setting up easyxrd_scratch folder
73
+ if not os.path.isdir(os.path.join(user_home, ".easyxrd_scratch")):
74
+ os.mkdir(os.path.join(user_home, ".easyxrd_scratch"))
75
+ easyxrd_defaults["easyxrd_scratch_path"] = os.path.join(user_home, ".easyxrd_scratch")
76
+
77
+
78
+ # Get GSAS-II and binaries from GitHub
79
+
80
+ gsas2_path_in_easyxrd_scratch = os.path.join(user_home, ".easyxrd_scratch", "GSAS-II")
81
+ sys.path += [os.path.join(gsas2_path_in_easyxrd_scratch, "GSASII")]
82
+
83
+ try:
84
+ with HiddenPrints():
85
+ import GSASIIscriptable as G2sc
86
+ print(
87
+ "\nFound usable GSAS-II lib path @ %s"
88
+ % os.path.join(gsas2_path_in_easyxrd_scratch, "GSASII")
89
+ )
90
+ easyxrd_defaults["gsasii_lib_path"] = os.path.join(
91
+ gsas2_path_in_easyxrd_scratch, "GSASII"
92
+ )
93
+ except Exception as exc:
94
+ # print(exc)
95
+ if os.path.isdir(gsas2_path_in_easyxrd_scratch):
96
+ shutil.rmtree(gsas2_path_in_easyxrd_scratch)
97
+
98
+ # print("\nClonning GSAS-II package from GitHub")
99
+ # import git
100
+ # git.Repo.clone_from(
101
+ # "https://github.com/AdvancedPhotonSource/GSAS-II",
102
+ # to_path=gsas2_path_in_easyxrd_scratch,
103
+ # multi_options=["--depth 1"],
104
+ # )
105
+
106
+ print("\nDownloading GSAS-II (version 5805) from GitHub")
107
+ urllib.request.urlretrieve(
108
+ "https://github.com/AdvancedPhotonSource/GSAS-II/archive/refs/tags/5805.tar.gz",
109
+ os.path.join(easyxrd_defaults["easyxrd_scratch_path"], "GSAS-5805.tar.gz"),
110
+ ),
111
+
112
+ with tarfile.open(
113
+ os.path.join(easyxrd_defaults["easyxrd_scratch_path"], "GSAS-5805.tar.gz"),
114
+ "r:gz",
115
+ ) as tar:
116
+ tar.extractall(os.path.join(easyxrd_defaults["easyxrd_scratch_path"]))
117
+ os.rename(
118
+ os.path.join(easyxrd_defaults["easyxrd_scratch_path"], "GSAS-II-5805"),
119
+ os.path.join(easyxrd_defaults["easyxrd_scratch_path"], "GSAS-II"),
120
+ )
121
+
122
+
123
+ try:
124
+ gsas_file_to_fix = os.path.join(easyxrd_defaults["easyxrd_scratch_path"], "GSAS-II", "GSASII", "NIST_profile", "profile_functions_class.py")
125
+
126
+ if os.path.isfile(gsas_file_to_fix):
127
+ with open(gsas_file_to_fix, 'r') as pfile:
128
+ pcontent = pfile.read()
129
+ modified_pcontent = pcontent.replace("xrange", "range")
130
+ with open(gsas_file_to_fix, 'w') as pmfile:
131
+ pmfile.write(modified_pcontent)
132
+
133
+ except:
134
+ pass
135
+
136
+
137
+
138
+
139
+ os.makedirs(
140
+ os.path.join(gsas2_path_in_easyxrd_scratch, "GSASII-bin"), exist_ok=True
141
+ )
142
+
143
+ if (platform.system() == "Linux") and (platform.machine() == 'x86_64'):
144
+ os_str = "linux_64"
145
+ elif (platform.system() == "Windows") and (platform.machine() == 'AMD64'):
146
+ os_str = "win_64"
147
+ elif (platform.system() == "Darvin") and (platform.machine() == 'x86_64'):
148
+ os_str = "mac_64"
149
+ else:
150
+ os_str = "mac_arm"
151
+
152
+
153
+ py_str = "p%d.%d" % (sys.version_info.major, sys.version_info.minor)
154
+ np_str = "n%s.%s" % (
155
+ np.version.version.split(".")[0],
156
+ np.version.version.split(".")[1],
157
+ )
158
+ gsas2_bin_tgz = (
159
+ "https://github.com/AdvancedPhotonSource/GSAS-II-buildtools/releases/download/v1.0.1/%s_%s_%s.tgz"
160
+ % (os_str, py_str, np_str)
161
+ )
162
+ print(gsas2_bin_tgz)
163
+
164
+ os.makedirs(
165
+ os.path.join(
166
+ gsas2_path_in_easyxrd_scratch,
167
+ "GSASII-bin",
168
+ gsas2_bin_tgz.split("/")[-1][:-4],
169
+ ),
170
+ exist_ok=True,
171
+ )
172
+
173
+ try:
174
+ print("Downloading %s from GitHub" % gsas2_bin_tgz)
175
+ urllib.request.urlretrieve(
176
+ gsas2_bin_tgz,
177
+ os.path.join(
178
+ gsas2_path_in_easyxrd_scratch,
179
+ "GSASII-bin",
180
+ gsas2_bin_tgz.split("/")[-1][:-4],
181
+ "bin.tgz",
182
+ ),
183
+ )
184
+ with tarfile.open(
185
+ os.path.join(
186
+ gsas2_path_in_easyxrd_scratch,
187
+ "GSASII-bin",
188
+ gsas2_bin_tgz.split("/")[-1][:-4],
189
+ "bin.tgz",
190
+ ),
191
+ "r:gz",
192
+ ) as tar:
193
+ tar.extractall(
194
+ path=os.path.join(
195
+ gsas2_path_in_easyxrd_scratch,
196
+ "GSASII-bin",
197
+ gsas2_bin_tgz.split("/")[-1][:-4],
198
+ )
199
+ )
200
+ easyxrd_defaults["gsasii_lib_path"] = os.path.join(
201
+ gsas2_path_in_easyxrd_scratch, "GSASII"
202
+ )
203
+
204
+ print(
205
+ "\n!!!! Please re-run this cell (after kernel restart) for the GSAS-II installation to take effect !!!!!"
206
+ )
207
+ time.sleep(2)
208
+ os._exit(1)
209
+ except Exception as exc:
210
+ print(exc)
211
+ easyxrd_defaults["gsasii_lib_path"] = "not found"
212
+
213
+
214
+ # check Materials Project API key in easyxrd_scratch folder
215
+ if os.path.isfile(os.path.join(user_home, ".easyxrd_scratch", "mp_api_key.dat")):
216
+ with open(
217
+ os.path.join(user_home, ".easyxrd_scratch", "mp_api_key.dat"), "r"
218
+ ) as api_key_file:
219
+ api_key_file_content = api_key_file.read().split()[-1]
220
+ if len(api_key_file_content) == 32:
221
+ mp_api_key = api_key_file_content
222
+ else:
223
+ mp_api_key = "invalid"
224
+ else:
225
+ mp_api_key = "not found"
226
+ easyxrd_defaults["mp_api_key"] = mp_api_key
227
+
228
+
229
+ def set_defaults(name, val):
230
+ """set a global variable."""
231
+ global easyxrd_defaults
232
+ easyxrd_defaults[name] = val
233
+
234
+
235
+ def print_defaults():
236
+ for key, val in easyxrd_defaults.items():
237
+
238
+ if key != "mp_api_key":
239
+ print("%s : %s" % (key, val))
240
+ else:
241
+ print("%s : %s.........." % (key, val[:9]))
242
+
243
+
244
+ print("\n\nImported easyxrd with the following configuration:\n")
245
+ print_defaults()