Skip to content

Commit ac4d2ff

Browse files
author
Andrew Yang
committed
Edit functionality, use tmp_path fixture for tests
1 parent 6ab58e4 commit ac4d2ff

File tree

6 files changed

+109
-45
lines changed

6 files changed

+109
-45
lines changed

conda-recipe/run_test.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
#!/usr/bin/env python
22

3+
import sys
4+
import pathlib
5+
sys.path.append((pathlib.Path.cwd().parent.absolute() / "src").as_posix())
6+
37
import diffpy.utils.tests
48
assert diffpy.utils.tests.test().wasSuccessful()

src/diffpy/utils/parsers/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"""
1818

1919
from .loaddata import loadData
20-
from .loadmetafile import load_PDF_into_db, markup_PDF, apply_schema
20+
from .loadmetafile import load_PDF_into_db, markup_PDF, apply_schema_to_file, markup_oneline
2121
from .resample import resample
2222

2323
# silence the pyflakes syntax checker

src/diffpy/utils/parsers/loadmetafile.py

Lines changed: 69 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,22 @@
1616
import pathlib
1717
import json
1818

19-
from diffpy.utils.parsers import loadData
20-
2119
# FIXME: add support for yaml, xml
2220
supported_formats = ['.json']
2321

2422

25-
def load_PDF_into_db(dbname, pdfname, hddata: dict, rv: list, oneline=True, show_path=True):
26-
"""Load PDF header and base data into a database file.
23+
def load_PDF_into_db(dbname, pdfname, hddata: dict, rv: list, show_path=True):
24+
"""Load an entry consisting of PDF header and base data into a database file.
2725
2826
Requires hdata and rv generated from loadData.
2927
30-
dbname -- name of the database file to load into.
31-
pdfname -- name of the PDF file.
32-
hddata -- Dictionary of PDF metadata generated by loadData.
33-
rv -- List of PDF (r, gr) pairs generated by loadData.
34-
oneline -- store r and gr arrays in a single line for compactness (default True).
35-
show_path -- include a PDF_path element in the database entry (default True).
28+
dbname -- name of the database file to load an entry into.
29+
pdfname -- name of the PDF file.
30+
hddata -- Dictionary of PDF metadata generated by loadData.
31+
rv -- List of PDF (r, gr) pairs generated by loadData.
32+
show_path -- include a PDF_path element in the database entry (default True).
33+
34+
Returns the dictionary loaded from/into the updated database file.
3635
"""
3736
# new file or update
3837
existing = False
@@ -49,10 +48,7 @@ def load_PDF_into_db(dbname, pdfname, hddata: dict, rv: list, oneline=True, show
4948
data.update({'PDF_path': grpath})
5049

5150
# add r, gr, and header metadata
52-
if oneline:
53-
data.update({'r': str(list(rv[:, 0])), 'gr': str(list(rv[:, 1]))})
54-
else:
55-
data.update({'r': list(rv[:, 0]), 'gr': list(rv[:, 1])})
51+
data.update({'r': list(rv[:, 0]), 'gr': list(rv[:, 1])})
5652
data.update(hddata)
5753

5854
# parse name using pathlib and generate json entry
@@ -69,7 +65,8 @@ def load_PDF_into_db(dbname, pdfname, hddata: dict, rv: list, oneline=True, show
6965
# dump if non-existing
7066
if not existing:
7167
with open(dbname, 'w') as jsonfile:
72-
jsonfile.write(json.dumps(entry, indent=2))
68+
pdfs = entry # for return
69+
json.dump(pdfs, jsonfile, indent=2)
7370

7471
# update if existing
7572
else:
@@ -80,20 +77,30 @@ def load_PDF_into_db(dbname, pdfname, hddata: dict, rv: list, oneline=True, show
8077
# dump to string first for formatting
8178
json.dump(pdfs, json_write, indent=2)
8279

80+
return pdfs
8381

84-
def markup_PDF(muname, hddata: dict, rv: list):
85-
# FIXME: for REST API, remove if better ways to implement
86-
"""Put PDF file information in a markup language file.
8782

88-
mumane -- name of markup file to put data into.
83+
def markup_PDF(hddata: dict, rv: list, muname=None):
84+
# FIXME: may be better suited for REST API package, not diffpy.utils
85+
"""Put PDF file information into a dictionary.
86+
8987
hddata -- Dictionary of metadata.
9088
rv -- List of (r, gr) pairs.
89+
muname -- file to save into (default None, no saving occurs).
90+
91+
Returns the dictionary loaded from/into markup file.
9192
"""
9293

9394
# gather data
9495
data = {}
95-
data.update({'r': str(list(rv[:, 0])), 'gr': str(list(rv[:, 1]))})
96+
data.update({'r': list(rv[:, 0]), 'gr': list(rv[:, 1])})
9697
data.update(hddata)
98+
99+
# return directly
100+
if muname is None:
101+
return data
102+
103+
# save to disk when enabled
97104
extension = pathlib.Path(muname).suffix
98105
if extension not in supported_formats:
99106
raise Exception(f"Format of {muname} is not supported.")
@@ -103,15 +110,54 @@ def markup_PDF(muname, hddata: dict, rv: list):
103110
with open(muname, 'w') as json_write:
104111
json.dump(data, json_write, indent=2)
105112

113+
return data
114+
115+
116+
def markup_oneline(filename):
117+
"""Reformat lists in markup languages to take up only one line.
106118
107-
def apply_schema(filename, schemaname, multiple_entries=False):
119+
Works well when only lists are surrounded by square brackets and no other data is comma and newline separated.
120+
121+
filename -- name of markup file to reformat.
122+
"""
123+
124+
# check file type
125+
extension = pathlib.Path(filename).suffix
126+
if extension not in supported_formats:
127+
raise Exception(f"Format of {filename} is not supported.")
128+
129+
if extension == '.json':
130+
# cannot easily do regex substitution since lists are of floats
131+
with open(filename, 'r+') as json_file:
132+
lines = json_file.readlines()
133+
json_file.seek(0)
134+
json_file.truncate()
135+
136+
s_flag = False
137+
for line in lines:
138+
if "\"r\": [" in line or "\"gr\": [" in line:
139+
s_flag = True
140+
updated_line = line[:-1]
141+
elif "]," in line:
142+
s_flag = False
143+
updated_line = f"{updated_line[:-1]}{line.strip()}\n"
144+
json_file.write(updated_line)
145+
elif s_flag:
146+
updated_line += f"{line[:-1].strip()} "
147+
else:
148+
json_file.write(line)
149+
150+
151+
def apply_schema_to_file(filename, schemaname, multiple_entries=False):
108152
""" Reformat a file so relevant entries match the same order as a schema file.
109153
Other entries are put at the end in the same order.
110154
111155
filename -- name of file to apply the schema to.
112156
schemaname -- name of schema to apply.
113157
multiple_entries -- True if database file (i.e. those generated by load_PDF_into_db).
114158
False if data from a single file (i.e. those generated by markup_PDF).
159+
160+
Returns the dictionary loaded from/into the reformatted file.
115161
"""
116162

117163
# ensure proper extension
@@ -162,3 +208,5 @@ def apply_schema(filename, schemaname, multiple_entries=False):
162208
reformatted_dict.update(data_dict)
163209
with open(filename, 'w') as json_write:
164210
json.dump(reformatted_dict, json_write, indent=2)
211+
212+
return reformatted_dict

src/diffpy/utils/tests/test_loadmetafile.py

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from diffpy.utils.parsers import load_PDF_into_db, markup_PDF, apply_schema
1+
from diffpy.utils.parsers import load_PDF_into_db, markup_PDF, apply_schema_to_file, markup_oneline
22
from diffpy.utils.parsers import loadData
33
from diffpy.utils.tests.testhelpers import datafile
44

@@ -7,40 +7,52 @@
77

88
tests_dir = os.path.dirname(os.path.abspath(locals().get('__file__', 'file.py')))
99

10-
generatedjson = datafile('tljson.json')
1110
targetjson = datafile('targetdb.json')
1211

1312
schemaname = datafile('strumining.json')
1413
muload = datafile('loadmu.txt')
15-
generatedmu = datafile('tmujson.json')
1614
targetmu = datafile('targetmu.json')
1715

1816

19-
def test_load_gr():
17+
def test_load_gr(tmp_path):
2018
# generate json and apply schema
19+
generatedjson = tmp_path / "generated_db.json"
2120
tddbload_list = os.listdir(os.path.join(tests_dir, "testdata", "dbload"))
2221
tddbload_list.sort()
23-
print(tddbload_list)
2422
for headerfile in tddbload_list:
2523
headerfile = os.path.join(tests_dir, "testdata", "dbload", headerfile)
26-
hdata, rv = loadData(headerfile, headers=True)
27-
load_PDF_into_db(generatedjson, headerfile, hdata, rv, show_path=False)
28-
apply_schema(generatedjson, schemaname, multiple_entries=True)
24+
hdata = loadData(headerfile, headers=True)
25+
rv = loadData(headerfile)
26+
db_data = load_PDF_into_db(generatedjson, headerfile, hdata, rv, show_path=False)
27+
apply_schema_to_file(generatedjson, schemaname, multiple_entries=True)
28+
markup_oneline(generatedjson)
2929

3030
# compare to target
31+
# first compare if base data is same
32+
import json
33+
with open(targetjson, 'r') as target:
34+
target_db_data = json.load(target)
35+
assert target_db_data == db_data
36+
# then compare file structure/organization
3137
assert filecmp.cmp(generatedjson, targetjson)
3238

33-
# cleanup
34-
os.remove(generatedjson)
35-
3639

37-
def test_markup_gr():
40+
def test_markup_gr(tmp_path):
3841
# put into json and apply schema
39-
hdata, rv = loadData(muload, headers=True)
40-
markup_PDF(generatedmu, hdata, rv)
41-
apply_schema(generatedmu, schemaname)
42+
generatedmu = tmp_path / "generated_markup.json"
43+
hdata = loadData(muload, headers=True)
44+
rv = loadData(muload)
45+
data = markup_PDF(hdata, rv, generatedmu)
46+
apply_schema_to_file(generatedmu, schemaname)
47+
markup_oneline(generatedmu)
4248

4349
# check against target
50+
# first compare data is same
51+
import json
52+
with open(targetmu, 'r') as target:
53+
target_data = json.load(target)
54+
assert target_data == data
55+
# then compare structure
4456
assert filecmp.cmp(generatedmu, targetmu)
4557

4658
# cleanup

src/diffpy/utils/tests/testdata/targetdb.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
{
22
"e1.gr": {
3-
"r": "[0.0, 1.0, 2.0, 3.0]",
4-
"gr": "[0.0, 0.0, 0.0, 0.0]",
3+
"r": [0.0, 1.0, 2.0, 3.0],
4+
"gr": [0.0, 0.0, 0.0, 0.0],
55
"qmax": 10.0,
66
"qmin": 0.0,
77
"rmax": 10.0,
88
"rmin": 0.0
99
},
1010
"e2.gr": {
11-
"r": "[0.0, 1.0, 2.0, 3.0]",
12-
"gr": "[1.0, 2.0, 3.0, 4.0]",
11+
"r": [0.0, 1.0, 2.0, 3.0],
12+
"gr": [1.0, 2.0, 3.0, 4.0],
1313
"qmax": 11.0,
1414
"qmin": 1.0,
1515
"rmax": 11.0,
1616
"rmin": 1.0
1717
},
1818
"e3.gr": {
19-
"r": "[0.0, 1.0, 2.0, 3.0]",
20-
"gr": "[0.0, 5.0, 4.0, 3.0]",
19+
"r": [0.0, 1.0, 2.0, 3.0],
20+
"gr": [0.0, 5.0, 4.0, 3.0],
2121
"qmax": 12.0,
2222
"qmin": 2.0,
2323
"rmax": 12.0,

src/diffpy/utils/tests/testdata/targetmu.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"r": "[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0]",
3-
"gr": "[0.0, 1e-05, 3e-05, 5e-05, 1e-05, -3e-05, -6e-05]",
2+
"r": [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0],
3+
"gr": [0.0, 1e-05, 3e-05, 5e-05, 1e-05, -3e-05, -6e-05],
44
"stru_str": "baddata",
55
"stype": "Neutron",
66
"qdamp": 0.0001,

0 commit comments

Comments
 (0)