Skip to content

Commit 6ab58e4

Browse files
author
Andrew Yang
committed
Merge branch 'loaddata_headers' into loadmetadata
2 parents a2171a0 + 699e604 commit 6ab58e4

File tree

3 files changed

+55
-30
lines changed

3 files changed

+55
-30
lines changed

CHANGELOG.md

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,32 @@
11
# Release notes
22

3-
## Version 3.1.0 – 2022-12-09
3+
## Version 3.2.0 – 2023-8-**
44

55
### Added
66

7-
- Compatibility with Python 3.10, 3.9, 3.8
7+
- CI Coverage.
8+
- New tests for loadData function.
89

910
### Changed
1011

11-
### Deprecated
12+
- loadData function now toggleable. Can return either (a) data read from data blocks or (b) header
13+
information stored above the data block.
14+
- Exclude wx from tests.
1215

1316
### Removed
1417

15-
- Remove the support for Python 3.5, 3.6.
18+
- Remove use of pkg_resources (deprecated).
19+
- No longer use Travis.
1620

17-
### Fixed
21+
## Version 3.1.0 – 2022-12-09
22+
23+
### Added
24+
25+
- Compatibility with Python 3.10, 3.9, 3.8
26+
27+
### Removed
28+
29+
- Remove the support for Python 3.5, 3.6.
1830

1931
## Version 3.0.0 -- 2019-03-12
2032

src/diffpy/utils/parsers/loaddata.py

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,40 @@
1919
def loadData(filename, minrows=10, headers=False, hdel='=', hignore=None, **kwargs):
2020
"""Find and load data from a text file.
2121
22-
The data reading starts at the first matrix block of at least minrows rows
23-
and constant number of columns. This seems to work for most of the
24-
datafiles including those generated by PDFGetX2.
22+
The data block is identified as the first matrix block of at least minrows rows
23+
and constant number of columns. This seems to work for most of the datafiles including
24+
those generated by PDFGetX2.
2525
26-
filename -- name of the file we want to load data from.
27-
minrows -- minimum number of rows in the first data block.
28-
All rows must have the same number of floating point values.
29-
headers -- return also a dictionary of parameters specified in header
30-
hdel -- delimiter for parsing header information
31-
hignore -- ignore header rows beginning with any elements in the hignore list
32-
usecols -- zero-based index of columns to be loaded, by default use
33-
all detected columns. The reading skips data blocks that
34-
do not have the usecols-specified columns.
35-
unpack -- return data as a sequence of columns that allows tuple
36-
unpacking such as x, y = loadData(FILENAME, unpack=True).
37-
Note transposing the loaded array as loadData(FILENAME).T
38-
has the same effect.
39-
kwargs -- keyword arguments that are passed to numpy.loadtxt
26+
filename -- name of the file we want to load data from.
27+
minrows -- minimum number of rows in the first data block.
28+
All rows must have the same number of floating point values.
29+
headers -- when False (defualt), the function returns a numpy array of the
30+
data in the data block. When True, the function instead returns a
31+
dictionary of parameters and their corresponding values parsed from
32+
header (information prior the data block). See hdel and hignore for
33+
options to help with parsing header information.
34+
hdel -- (only used when headers enabled) delimiter for parsing header
35+
information (default '='). e.g. using default hdel, the line
36+
'parameter = p_value' is put into the dictionary as
37+
{parameter: p_value}.
38+
hignore -- (only used when headers enabled) ignore header rows beginning
39+
with any elements in the hignore list. e.g. hignore=['# ', '[']
40+
means the following lines are skipped: '# qmax=10', '[defaults]'.
41+
kwargs -- keyword arguments that are passed to numpy.loadtxt including
42+
the following arguments below. (See also numpy.loadtxt for more
43+
details.)
44+
delimiter -- delimiter for the data in the block (default use whitespace).
45+
For comma-separated data blocks, set delimiter to ','.
46+
usecols -- zero-based index of columns to be loaded, by default use
47+
all detected columns. The reading skips data blocks that
48+
do not have the usecols-specified columns.
49+
unpack -- return data as a sequence of columns that allows tuple
50+
unpacking such as x, y = loadData(FILENAME, unpack=True).
51+
Note transposing the loaded array as loadData(FILENAME).T
52+
has the same effect.
4053
41-
Return a numpy array of the data.
42-
See also numpy.loadtxt for more details.
54+
Return a numpy array of the data. If headers enabled, instead returns a
55+
dictionary of parameters read from the header.
4356
"""
4457
from numpy import array, loadtxt
4558
# for storing header data
@@ -124,6 +137,11 @@ def countcolumnsvalues(line):
124137
# block was found here!
125138
if nrows >= minrows:
126139
break
140+
141+
# Return header data if requested
142+
if headers:
143+
return hdata # Return, so do not proceed to reading datablock
144+
127145
# Return an empty array when no data found.
128146
# loadtxt would otherwise raise an exception on loading from EOF.
129147
if start is None:
@@ -134,11 +152,6 @@ def countcolumnsvalues(line):
134152
# in case of trailing delimiters.
135153
kwargs.setdefault('usecols', list(range(ncvblock[0])))
136154
rv = loadtxt(fid, **kwargs)
137-
138-
# return headers if requested
139-
if headers:
140-
return hdata, rv
141-
# otherwise do not
142155
return rv
143156

144157

src/diffpy/utils/tests/test_loaddata.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def test_loadData_headers(self):
5151
"""
5252
hignore = ["# ", "// ", "["] # ignore lines beginning with these strings
5353
delimiter = ": " # what our data should be separated by
54-
hdata, rv = loadData(loaddatawithheaders, headers=True, hdel=delimiter, hignore=hignore)
54+
hdata = loadData(loaddatawithheaders, headers=True, hdel=delimiter, hignore=hignore)
5555
# only fourteen lines of data are formatted properly
5656
assert len(hdata) == 14
5757
# check the following are floats

0 commit comments

Comments
 (0)