Skip to content

Commit c101622

Browse files
committed
numpy format
1 parent 284495a commit c101622

File tree

7 files changed

+182
-104
lines changed

7 files changed

+182
-104
lines changed

src/diffpy/utils/__init__.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,7 @@
1313
#
1414
##############################################################################
1515

16-
"""Smalled shared functions for use by other diffpy packages.
17-
18-
Subpackages:
19-
20-
parsers -- various utilities related to data parsing and manipulation.
16+
"""Smaller shared functions for use by other diffpy packages.
2117
"""
2218

2319
# package version

src/diffpy/utils/parsers/custom_exceptions.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,14 @@
1616
class UnsupportedTypeError(Exception):
1717
"""For file types not supported by our parsers.
1818
19-
supported_types -- List of supported types.
20-
file -- file triggering the error.
21-
message -- for writing a custom message.
19+
Parameters
20+
----------
21+
file
22+
Name of file triggering the error.
23+
supported_types: list
24+
Supported file types.
25+
message: str
26+
Overwrites default message.
2227
"""
2328

2429
def __init__(self, file, supported_types=None, message=None):
@@ -35,8 +40,12 @@ def __init__(self, file, supported_types=None, message=None):
3540
class ImproperSizeError(Exception):
3641
"""When the size of an object does not match expectations.
3742
38-
bad_object -- Object with improper size.
39-
message -- for writing a custom message.
43+
Parameters
44+
----------
45+
bad_object
46+
Object with improper size.
47+
message: str
48+
Overwrites default message.
4049
"""
4150

4251
def __init__(self, bad_object, message=None):

src/diffpy/utils/parsers/loaddata.py

Lines changed: 64 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -19,40 +19,49 @@
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 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 diffpy programs.
22+
The data block is identified as the first matrix block of at least minrows rows and constant number of columns.
23+
This seems to work for most of the datafiles including those generated by diffpy programs.
2524
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.
25+
Parameters
26+
----------
27+
filename
28+
Name of the file we want to load data from.
29+
minrows: int
30+
Minimum number of rows in the first data block. All rows must have the same number of floating point values.
31+
headers: bool
32+
when False (defualt), the function returns a numpy array of the data in the data block.
33+
When True, the function instead returns a dictionary of parameters and their corresponding values parsed from
34+
header (information prior the data block). See hdel and hignore for options to help with parsing header
35+
information.
36+
hdel: str
37+
(Only used when headers enabled.) Delimiter for parsing header information (default '='). e.g. using
38+
default hdel, the line 'parameter = p_value' is put into the dictionary as {parameter: p_value}.
39+
hignore: list
40+
(Only used when headers enabled.) Ignore header rows beginning with any elements in hignore.
41+
e.g. hignore=['# ', '['] causes the following lines to be skipped: '# qmax=10', '[defaults]'.
42+
kwargs:
43+
Keyword arguments that are passed to numpy.loadtxt including the following arguments below. (See
44+
numpy.loadtxt for more details.) Only pass kwargs used by numpy.loadtxt.
5345
54-
Return a numpy array of the data (data_block). If headers enabled, instead returns a
55-
dictionary of parameters read from the header (hddata).
46+
Useful kwargs
47+
=============
48+
delimiter: str
49+
Delimiter for the data in the block (default use whitespace). For comma-separated data blocks,
50+
set delimiter to ','.
51+
usecols:
52+
Zero-based index of columns to be loaded, by default use all detected columns. The reading skips
53+
data blocks that do not have the usecols-specified columns.
54+
unpack: bool
55+
Return data as a sequence of columns that allows tuple unpacking such as x, y =
56+
loadData(FILENAME, unpack=True). Note transposing the loaded array as loadData(FILENAME).T has the same
57+
effect.
58+
59+
Returns
60+
-------
61+
data_block: ndarray
62+
A numpy array containing the found data block. (This is not returned if headers is enabled.)
63+
hdata: dict
64+
If headers are enabled, return a dictionary of parameters read from the header.
5665
"""
5766
from numpy import array, loadtxt
5867
# for storing header data
@@ -156,18 +165,24 @@ def countcolumnsvalues(line):
156165

157166

158167
class TextDataLoader(object):
159-
'''Smart loading of a text data with possibly multiple datasets.
160-
'''
168+
"""Smart loading of a text data with possibly multiple datasets.
161169
162-
minrows = 10
163-
usecols = None
164-
skiprows = None
170+
Parameters
171+
----------
172+
minrows: int
173+
Minimum number of rows in the first data block. (Default 10.)
174+
usecols: tuple
175+
Which columns in our dataset to use. Ignores all other columns. If None (default), use all columns.
176+
skiprows
177+
Rows in dataset to skip. (Currently not functional.)
178+
"""
165179

166-
def __init__(self, minrows=None, usecols=None, skiprows=None):
180+
def __init__(self, minrows=10, usecols=None, skiprows=None):
167181
if minrows is not None:
168182
self.minrows = minrows
169183
if usecols is not None:
170184
self.usecols = tuple(usecols)
185+
# FIXME: implement usage in _findDataBlocks
171186
if skiprows is not None:
172187
self.skiprows = skiprows
173188
# data items
@@ -194,12 +209,23 @@ def _resetvars(self):
194209

195210

196211
def read(self, filename):
212+
"""Open a file and run readfp.
213+
214+
Use if file is not already open for read byte.
215+
"""
197216
with open(filename, 'rb') as fp:
198217
self.readfp(fp)
199218
return
200219

201220

202221
def readfp(self, fp, append=False):
222+
"""Get file details.
223+
224+
File details include:
225+
* File name.
226+
* All data blocks findable by loadData.
227+
* Headers (if present) for each data block. (Generally the headers contain column name information).
228+
"""
203229
self._reset()
204230
# try to read lines from fp first
205231
self._lines = fp.readlines()

src/diffpy/utils/parsers/resample.py

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,34 @@
1717

1818
import numpy
1919

20+
2021
# NOTE - this should be faster than resample below and conforms more closely to
2122
# numpy.interp. I'm keeping resample for legacy reasons.
22-
def wsinterp(x, xp, fp, left = None, right = None):
23+
def wsinterp(x, xp, fp, left=None, right=None):
2324
"""One-dimensional Whittaker-Shannon interpolation.
2425
25-
This uses the Whittaker-Shannon interpolation formula to interpolate the
26-
value of fp (array), which is defined over xp (array), at x (array or
27-
float).
28-
29-
Returns the interpolated array with dimensions of x.
30-
26+
This uses the Whittaker-Shannon interpolation formula to interpolate the value of fp (array), which is defined over
27+
xp (array), at x (array or float).
28+
29+
Paramaters
30+
----------
31+
x: ndarray
32+
Desired range for interpolation.
33+
xp: ndarray
34+
Defined range for fp.
35+
fp: ndarray
36+
Function to be interpolated.
37+
left: float
38+
If given, set fp for x < xp[0] to left. Otherwise, if left is None (default) or not given, set fp for x < xp[0]
39+
to fp evaluated at xp[-1].
40+
right: float
41+
If given, set fp for x > xp[-1] to right. Otherwise, if right is None (default) or not given, set fp for
42+
x > xp[-1] to fp evaluated at xp[-1].
43+
44+
Returns
45+
-------
46+
ndarray:
47+
Returns the interpolated array with dimensions of x.
3148
"""
3249
scalar = numpy.isscalar(x)
3350
if scalar:
@@ -50,23 +67,30 @@ def wsinterp(x, xp, fp, left = None, right = None):
5067
fp_at_x[x > xp[-1]] = right
5168

5269
# Return a float if we got a float
53-
if scalar: return float(fp_at_x)
70+
if scalar:
71+
return float(fp_at_x)
5472

5573
return fp_at_x
5674

75+
5776
def resample(r, s, dr):
5877
"""Resample a PDF on a new grid.
5978
60-
This uses the Whittaker-Shannon interpolation formula to put s1 on a new
61-
grid if dr is less than the sampling interval of r1, or linear
62-
interpolation if dr is greater than the sampling interval of r1.
63-
64-
r -- The r-grid used for s1
65-
s -- The signal to be resampled
66-
dr -- The new sampling interval
67-
68-
Returns resampled (r, s)
69-
79+
This uses the Whittaker-Shannon interpolation formula to put s1 on a new grid if dr is less than the sampling
80+
interval of r1, or linear interpolation if dr is greater than the sampling interval of r1.
81+
82+
Parameters
83+
----------
84+
r
85+
The r-grid used for s1.
86+
s
87+
The signal to be resampled.
88+
dr
89+
The new sampling interval.
90+
91+
Returns
92+
-------
93+
Returns resampled (r, s).
7094
"""
7195

7296
dr0 = r[1] - r[0]

src/diffpy/utils/parsers/serialization.py

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,34 @@
2323

2424

2525
def serialize_data(filename, hdata: dict, data_table: list, show_path=True, dt_colnames=None, serial_file=None):
26-
"""Serialize file data into a dictionary. Can also save dictionary into a serial language file.
27-
Dictionary is formatted as {filename: data}.
28-
29-
Requires hdata and data_table generated from loadData.
30-
31-
filename -- name of the file whose data is being serialized.
32-
hdata -- Dictionary of PDF metadata generated by loadData.
33-
data_table -- List storing parsed by loadData.
34-
dt_colnames -- List containing names of each column in data_table. Every name in
35-
data_table_cols will be put into the Dictionary as a key with a value
36-
of that column in data_table (stored as a List). Put None for
37-
columns without names. If dt_cols has less non-None entries
38-
than columns in data_table, the pair {'data table': data_table} will be put
39-
in the dictionary. (Default None: only entry {'data table': data_table}
40-
will be added to dictionary.)
41-
show_path -- include a path element in the database entry (default True).
42-
If 'path' is not included in hddata, extract path from filename.
43-
serial_file -- serial language file to dump dictionary into.
44-
45-
Returns the dictionary loaded from/into the updated database file.
26+
"""Serialize file data into a dictionary. Can also save dictionary into a serial language file. Dictionary is
27+
formatted as {filename: data}.
28+
29+
Requires hdata and data_table (can be generated by loadData).
30+
31+
Parameters
32+
----------
33+
filename
34+
Name of the file whose data is being serialized.
35+
hdata: dict
36+
File metadata (generally related to data table).
37+
data_table: list
38+
Data table.
39+
dt_colnames: list
40+
Names of each column in data_table. Every name in data_table_cols will be put into the Dictionary as a key with
41+
a value of that column in data_table (stored as a List). Put None for columns without names. If dt_cols has less
42+
non-None entries than columns in data_table, the pair {'data table': data_table} will be put in the dictionary.
43+
(Default None: only entry {'data table': data_table} will be added to dictionary.)
44+
show_path: bool
45+
include a path element in the database entry (default True). If 'path' is not included in hddata, extract path
46+
from filename.
47+
serial_file
48+
Serial language file to dump dictionary into. If None (defualt), no dumping will occur.
49+
50+
Returns
51+
-------
52+
dict:
53+
Returns the dictionary loaded from/into the updated database file.
4654
"""
4755

4856
# compile data_table and hddata together
@@ -131,9 +139,15 @@ def serialize_data(filename, hdata: dict, data_table: list, show_path=True, dt_c
131139
def deserialize_data(filename):
132140
"""Load a dictionary from a serial file.
133141
134-
filename -- database file to load from.
142+
Parameters
143+
----------
144+
filename
145+
Serial file to load from.
135146
136-
Returns a dictionary of database information.
147+
Returns
148+
-------
149+
dict
150+
A dictionary read from a serial file.
137151
"""
138152

139153
# check if supported type

src/diffpy/utils/wx/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
##############################################################################
1515

1616
"""Utilities related wx Python GUIs.
17-
18-
gridutils -- selection management in wx.grid.Grid
1917
"""
2018

2119
# End of file

0 commit comments

Comments
 (0)