Skip to content

Commit 2d68f9a

Browse files
committed
Fix warnings, add warnings
1 parent 259d3fd commit 2d68f9a

File tree

9 files changed

+44
-22
lines changed

9 files changed

+44
-22
lines changed

news/docs_patch.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
**Added:**
22

33
* New documentation build.
4+
* Quickstart tutorial for parsers.
45

56
**Changed:**
67

7-
* <news item>
8+
* Theme changed from `sphinx_py3doc_enhanced_theme` to `sphinx_rtd_theme`.
9+
* User now warned when data_table data overwrites hdata (header) data.
810

911
**Deprecated:**
1012

rever.xsh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
$PROJECT = 'diffpy.utils'
22
$ACTIVITIES = [
3-
'changelog', # Create a CHANGELOG.rst using news directory
43
'tag', # Creates a tag for the new version number
54
'push_tag', # Pushes the tag up to the $TAG_REMOTE
5+
'changelog', # Create a CHANGELOG.rst using news directory
66
'ghrelease', # Creates a Github release entry for the new tag
77
'pypi', # Sends the package to pypi
88
]

src/diffpy/utils/parsers/loaddata.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def countcolumnsvalues(line):
8888
if usecols is not None:
8989
nv = len([float(words[i]) for i in usecols])
9090
else:
91-
nv = len([float(w) for w in words])
91+
nv = len([ float(w) for w in words])
9292
except (IndexError, ValueError):
9393
nc = nv = 0
9494
return nc, nv

src/diffpy/utils/parsers/resample.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def wsinterp(x, xp, fp, left=None, right=None):
2626
This uses the Whittaker-Shannon interpolation formula to interpolate the value of fp (array), which is defined over
2727
xp (array), at x (array or float).
2828
29-
Paramaters
29+
Parameters
3030
----------
3131
x: ndarray
3232
Desired range for interpolation.
@@ -72,7 +72,7 @@ def wsinterp(x, xp, fp, left=None, right=None):
7272

7373
# Return a float if we got a float
7474
if scalar:
75-
return float(fp_at_x)
75+
return float(fp_at_x[0])
7676

7777
return fp_at_x
7878

@@ -97,7 +97,7 @@ def resample(r, s, dr):
9797
Returns resampled (r, s).
9898
"""
9999

100-
dr0 = r[1] - r[0]
100+
dr0 = r[1] - r[0] # Constant timestep
101101

102102
if dr0 < dr:
103103
rnew = numpy.arange(r[0], r[-1]+0.5*dr, dr)

src/diffpy/utils/parsers/serialization.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import json
1818

1919
from .custom_exceptions import UnsupportedTypeError, ImproperSizeError
20+
import warnings
2021

2122
# FIXME: add support for yaml, xml
2223
supported_formats = ['.json']
@@ -64,7 +65,11 @@ def serialize_data(filename, hdata: dict, data_table: list, show_path=True, dt_c
6465
# title the entry with name of file (taken from end of path)
6566
title = abs_path.name
6667

67-
# first add named columns in dt_cols
68+
# first add data in hddata dict
69+
data.update(hdata)
70+
71+
# second add named columns in dt_cols
72+
# performed second to prioritize overwriting hdata entries with data_table column entries
6873
named_columns = 0 # initial value
6974
max_columns = 1 # higher than named_columns to trigger 'data table' entry
7075
if dt_colnames is not None:
@@ -77,21 +82,18 @@ def serialize_data(filename, hdata: dict, data_table: list, show_path=True, dt_c
7782
for idx in range(num_col_names):
7883
colname = dt_colnames[idx]
7984
if colname is not None:
85+
if colname in hdata.keys():
86+
warnings.warn(f'Entry \'{colname}\' in hdata has been overwritten by a data_table entry.',
87+
RuntimeWarning)
8088
data.update({colname: list(data_table[:, idx])})
8189
named_columns += 1
8290

83-
# second add data in hddata dict
84-
data.update(hdata)
85-
8691
# finally add data_table as an entry named 'data table' if not all columns were parsed
8792
if named_columns < max_columns:
88-
if 'data table' not in data.keys():
89-
data.update({'data table': data_table})
90-
else: # if 'data table' is already a key, keep adding primes to the end
91-
dt_name = 'data table'
92-
while dt_name in data.keys():
93-
dt_name += " prime"
94-
data.update({dt_name: data_table})
93+
if 'data table' in data.keys():
94+
warnings.warn('Entry \'data table\' in hdata has been overwritten by data_table.',
95+
RuntimeWarning)
96+
data.update({'data table': data_table})
9597

9698
# parse name using pathlib and generate dictionary entry
9799
entry = {title: data}

src/diffpy/utils/tests/test_resample.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def test_wsinterp():
1212
# Sampling rate
1313
ssr = 44100**-1 # Standard sampling rate for human-hearable frequencies
1414
t = ssr
15-
n = 5 # Do 6 samples
15+
n = 5
1616
xp = np.array([i*t for i in range(-n, n+1, 1)])
1717
x = np.array([i*t for i in range(-n-1, n+2, 1)])
1818

src/diffpy/utils/tests/test_serialization.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
schemaname = datafile('strumining.json')
1515
wrongtype = datafile('wrong.type')
1616
loadfile = datafile('loadfile.txt')
17+
warningfile = datafile('generatewarnings.txt')
1718
nodt = datafile('loaddatawithheaders.txt')
1819

1920

@@ -69,9 +70,7 @@ def test_exceptions():
6970
# check equivalence
7071
assert missing_parameter == empty_parameter
7172
assert missing_parameter == none_entry_parameter
72-
print(data_table)
73-
print(missing_parameter[data_name]['data table prime'])
74-
assert numpy.allclose(missing_parameter[data_name]['data table prime'], data_table)
73+
assert numpy.allclose(missing_parameter[data_name]['data table'], data_table)
7574
# extract a single column
7675
r_extract = serialize_data(loadfile, hdata, data_table, show_path=False, dt_colnames=['r'])
7776
gr_extract = serialize_data(loadfile, hdata, data_table, show_path=False, dt_colnames=[None, 'gr'])
@@ -89,3 +88,12 @@ def test_exceptions():
8988
no_dt = serialize_data(nodt, nodt_hdata, nodt_dt, show_path=False)
9089
nodt_data_name = list(no_dt.keys())[0]
9190
assert numpy.allclose(no_dt[nodt_data_name]['data table'], nodt_dt)
91+
92+
# ensure user is warned when columns are overwritten
93+
hdata = loadData(warningfile, headers=True)
94+
data_table = loadData(warningfile)
95+
with pytest.warns(RuntimeWarning) as record:
96+
data = serialize_data(warningfile, hdata, data_table, show_path=False, dt_colnames=['c1', 'c2', 'c3'])
97+
assert len(record) == 4
98+
for msg in record:
99+
assert 'overwritten' in msg.message.args[0]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
data table = generate data table
2+
c1 = time
3+
c2 = position
4+
c3 = velocity
5+
6+
# label: [c1] [c2] [c3]
7+
0.0 0.0 0.0 1.0
8+
1.0 0.5 1.0 1.0
9+
2.0 2.0 2.0 1.0
10+
3.0 4.5 3.0 1.0
11+
4.0 8.0 4.0 1.0

src/diffpy/utils/tests/testdata/loadfile.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ qbroad = 0.2
1515
qdamp = 0.0001
1616
stype = Neutron
1717
stru_str = baddata
18-
data table = no_table
1918

2019
0 0
2120
1 0.00001

0 commit comments

Comments
 (0)