forked from diffpy/diffpy.morph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmorph_io.py
More file actions
353 lines (304 loc) · 11.2 KB
/
morph_io.py
File metadata and controls
353 lines (304 loc) · 11.2 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
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
#!/usr/bin/env python
##############################################################################
#
# diffpy.morph by DANSE Diffraction group
# Simon J. L. Billinge
# (c) 2010 Trustees of the Columbia University
# in the City of New York. All rights reserved.
#
# File coded by:
#
# See AUTHORS.txt for a list of people who contributed.
# See LICENSE.txt for license information.
#
##############################################################################
from __future__ import print_function
import sys
from pathlib import Path
import numpy
import diffpy.morph.tools as tools
from diffpy.morph import __save_morph_as__
def single_morph_output(
morph_inputs,
morph_results,
save_file=None,
morph_file=None,
xy_out=None,
verbose=False,
stdout_flag=False,
):
"""Helper function for printing details about a single morph. Handles both
printing to terminal and printing to a file.
Parameters
----------
morph_inputs: dict
Parameters given by the user.
morph_results: dict
Resulting data after morphing.
save_file
Name of file to print to. If None (default) print to terminal.
morph_file
Name of the morphed PDF file. Required when printing to a
non-terminal file.
param xy_out: list
List of the form [x_morph_out, y_morph_out]. x_morph_out is a List of
r values and y_morph_out is a List of gr values.
verbose: bool
Print additional details about the morph when True (default False).
stdout_flag: bool
Print to terminal when True (default False).
"""
# Input and output parameters
morphs_in = "\n# Input morphing parameters:\n"
morphs_in += (
"\n".join(
f"# {key} = {morph_inputs[key]}" for key in morph_inputs.keys()
)
+ "\n"
)
morphs_out = "# Optimized morphing parameters:\n"
morphs_out += "\n".join(
f"# {key} = {morph_results[key]:.6f}" for key in morph_results.keys()
)
# Printing to terminal
if stdout_flag:
print(f"{morphs_in}\n{morphs_out}\n")
# Saving to file
if save_file is not None:
path_name = str(Path(morph_file).resolve())
header = "# PDF created by diffpy.morph\n"
header += f"# from {path_name}"
header_verbose = f"{morphs_in}\n{morphs_out}"
if save_file != "-":
with open(save_file, "w") as outfile:
# Print out a header (more if verbose)
print(header, file=outfile)
if verbose:
print(header_verbose, file=outfile)
# Print table with label
print("\n# Labels: [r] [gr]", file=outfile)
numpy.savetxt(outfile, numpy.transpose(xy_out))
if stdout_flag:
# Indicate successful save
save_message = f"# Morph saved to {save_file}\n"
print(save_message)
else:
# Just print table with label if save is to stdout
print("# Labels: [r] [gr]")
numpy.savetxt(sys.stdout, numpy.transpose(xy_out))
def create_morphs_directory(save_directory):
"""Create a directory for saving multiple morphed PDFs.
Takes in a user-given path to a directory save_directory and create a
subdirectory named Morphs. diffpy.morph will save all morphs into the
Morphs subdirectory while metadata about the morphs will be stored in
save_directory outside Morphs.
Parameters
----------
save_directory
Path to a directory. diffpy.morph will save all generated files within
this directory.
Returns
-------
str
The absolute path to the Morph subdirectory.
"""
# Make directory to save files in if it does not already exist
Path(save_directory).mkdir(parents=True, exist_ok=True)
# Morphs will be saved in the subdirectory "Morphs"
morphs_subdirectory = Path(save_directory).joinpath("Morphs")
morphs_subdirectory.mkdir(exist_ok=True)
return str(morphs_subdirectory.resolve())
def get_multisave_names(target_list: list, save_names_file=None, mm=False):
"""Create or import a dictionary that specifies names to save morphs as.
First attempt to import names from a specified file. If names for certain
morphs not found, use default naming scheme: 'Morph_with_Target_<target
file name>.cgr'.
Used when saving multiple morphs.
Parameters
----------
target_list: list
Target (or Morph if mm enabled) PDFs used for each morph.
save_names_file
Name of file to import save names dictionary from (default None).
mm: bool
Rather than multiple targets, multiple morphs are being done.
Returns
-------
dict
The names to save each morph as. Keys are the target PDF file names
used to produce that morph.
"""
# Dictionary storing save file names
save_names = {}
# Import names from a serial file
if save_names_file is not None:
# Names should be stored properly in save_names_file
save_names = tools.deserialize(save_names_file)
# Apply default naming scheme to missing targets
for target_file in target_list:
if target_file.name not in save_names.keys():
if not mm:
save_names.update(
{
target_file.name: {
__save_morph_as__: (
f"Morph_with_Target_{target_file.stem}.cgr"
)
}
}
)
else:
save_names.update(
{
target_file.name: {
__save_morph_as__: (
f"Morph_of_{target_file.stem}.cgr"
)
}
}
)
return save_names
def multiple_morph_output(
morph_inputs,
morph_results,
target_files,
field=None,
field_list=None,
save_directory=None,
morph_file=None,
target_directory=None,
verbose=False,
stdout_flag=False,
mm=False,
):
"""Helper function for printing details about a series of multiple morphs.
Handles both printing to terminal and printing to a file.
Parameters
----------
morph_inputs: dict
Input parameters given by the user.
morph_results: dict
Resulting data after morphing.
target_files: list
PDF files that acted as targets to morphs.
save_directory
Name of directory to save morphs in.
field
Name of field if data was sorted by a particular field.
Otherwise, leave blank.
field_list: list
List of field values for each target PDF.
Generated by diffpy.morph.tools.field_sort().
morph_file
Name of the morphed PDF file.
Required to give summary data after saving to a directory.
target_directory
Name of the directory containing the target PDF files.
Required to give summary data after saving to a directory.
verbose: bool
Print additional summary details when True (default False).
stdout_flag: bool
Print to terminal when True (default False).
mm: bool
Multiple morphs done with a single target rather than multiple
targets for a single morphed file. Swaps morph and target in the code.
"""
# Input parameters used for every morph
inputs = "\n# Input morphing parameters:\n"
inputs += "\n".join(
f"# {key} = {morph_inputs[key]}" for key in morph_inputs.keys()
)
# Verbose to get output for every morph
verbose_outputs = ""
if verbose:
# Output for every morph
# (information repeated in a succinct table below)
for target in morph_results.keys():
if not mm:
output = f"\n# Target: {target}\n"
else:
output = f"\n# Morph: {target}\n"
output += "# Optimized morphing parameters:\n"
output += "\n".join(
f"# {param} = {morph_results[target][param]:.6f}"
for param in morph_results[target]
)
verbose_outputs += f"{output}\n"
# Get items we want to put in table
tabulated_results = tabulate_results(morph_results)
# Table labels
if not mm:
labels = "\n# Labels: [Target]"
else:
labels = "\n# Labels: [Morph]"
if field is not None:
labels += f" [{field}]"
for param in tabulated_results.keys():
if len(tabulated_results[param]) > 0:
labels += f" [{param}]"
# Corresponding table
table = f"{labels}\n"
for idx in range(len(target_files)):
row = f"{target_files[idx]}"
if field_list is not None:
row += f" {field_list[idx]}"
for param in tabulated_results.keys():
if len(tabulated_results[param]) > idx:
row += f" {tabulated_results[param][idx]:0.6f}"
table += f"{row}\n"
table = table[:-1] # Remove extra indent
# Printing summary to terminal
if stdout_flag:
print(f"{inputs}\n{verbose_outputs}{table}\n")
# Saving summary as a file
if save_directory is not None:
morph_path_name = str(Path(morph_file).resolve())
target_path_name = str(Path(target_directory).resolve())
header = "# Data generated by diffpy.morph\n"
if not mm:
header += f"# from morphing {morph_path_name}\n"
header += f"# with target directory {target_path_name}"
else:
header += f"# from morphing directory {target_path_name}\n"
header += f"# with target {morph_path_name}"
reference_table = Path(save_directory).joinpath(
"Morph_Reference_Table.txt"
)
with open(reference_table, "w") as reference:
print(
f"{header}\n{inputs}\n{verbose_outputs}{table}", file=reference
)
if stdout_flag:
# Indicate successful save
save_message = (
f"# Morphs saved in the directory {save_directory}\n"
)
print(save_message)
def tabulate_results(multiple_morph_results):
"""Helper function to make a data table summarizing details about the
results of multiple morphs.
Parameters
----------
multiple_morph_results
A collection of Dictionaries. Each Dictionary summarizes the
resultsof a single morph.
Returns
-------
tabulated_results: dict
Keys in tabulated_results are the table's column names and each
corresponding value is a list of data for that column.
"""
# We only care about the following parameters in our data tables
relevant_parameters = ["Scale", "Smear", "Stretch", "Pearson", "Rw"]
# Keys in this table represent column names and the value will be a list
# of column data
tabulated_results = {}
for param in relevant_parameters:
tabulated_results.update(
{
param: tools.get_values_from_dictionary_collection(
multiple_morph_results, param
)
}
)
return tabulated_results