Skip to content

Commit 90dd060

Browse files
committed
Fix all flake8 issues
1 parent 5439088 commit 90dd060

12 files changed

+154
-146
lines changed

Diff for: diffpy/snmf/containers.py

+12-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import numpy as np
21
import numdifftools
2+
import numpy as np
33

44

55
class ComponentSignal:
@@ -8,7 +8,7 @@ class ComponentSignal:
88
----------
99
grid: 1d array of floats
1010
The vector containing the grid points of the component.
11-
iq: 1d array of floats
11+
1212
The intensity/g(r) values of the component.
1313
weights: 1d array of floats
1414
The vector containing the weight of the component signal for each signal.
@@ -36,13 +36,18 @@ def apply_stretch(self, m):
3636
Returns
3737
-------
3838
tuple of 1d arrays
39-
The tuple of vectors where one vector is the stretched component, one vector is the 1st derivative of the
40-
stretching operation, and one vector is the second derivative of the stretching operation.
39+
The tuple of vectors where one vector is the stretched component, one vector is the 1st derivative
40+
of the stretching operation, and one vector is the second derivative of the stretching operation.
4141
"""
4242
normalized_grid = np.arange(len(self.grid))
43-
func = lambda stretching_factor: np.interp(
44-
normalized_grid / stretching_factor, normalized_grid, self.iq, left=0, right=0
45-
)
43+
# func = lambda stretching_factor: np.interp(
44+
# normalized_grid / stretching_factor, normalized_grid, self.iq, left=0, right=0
45+
# )
46+
47+
# E731 do not assign a lambda expression, use a def
48+
def func(stretching_factor):
49+
return np.interp(normalized_grid / stretching_factor, normalized_grid, self.iq, left=0, right=0)
50+
4651
derivative_func = numdifftools.Derivative(func)
4752
second_derivative_func = numdifftools.Derivative(derivative_func)
4853

Diff for: diffpy/snmf/factorizers.py

+8-9
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,26 @@
55
def lsqnonneg(stretched_component_matrix, target_signal):
66
"""Finds the weights of stretched component signals under one-sided constraint.
77
8-
Solves ``argmin_x || Ax - b ||_2`` for ``x>=0`` where A is the stretched_component_matrix and b is the target_signal
9-
vector. Finds the weights of component signals given undecomposed signal data and stretched components under a
10-
one-sided constraint on the weights.
8+
Solves ``argmin_x || Ax - b ||_2`` for ``x>=0`` where A is the stretched_component_matrix and b is the
9+
target_signal vector. Finds the weights of component signals given undecomposed signal data and stretched
10+
components under a one-sided constraint on the weights.
1111
1212
Parameters
1313
----------
1414
stretched_component_matrix: 2d array like
1515
The component matrix where each column contains a stretched component signal. Has dimensions R x C where R is
16-
the length of the signal and C is the number of components. Does not need to be nonnegative. Corresponds with 'A'
17-
from the objective function.
16+
the length of the signal and C is the number of components. Does not need to be nonnegative. Corresponds with
17+
'A' from the objective function.
1818
1919
target_signal: 1d array like
20-
The signal that is used as reference against which weight factors will be determined. Any column from the matrix
21-
of the entire, unfactorized input data could be used. Has length R. Does not need to be nonnegative. Corresponds
22-
with 'b' from the objective function.
20+
The signal that is used as reference against which weight factors will be determined. Any column from the
21+
matrix of the entire, unfactorized input data could be used. Has length R. Does not need to be nonnegative.
22+
Corresponds with 'b' from the objective function.
2323
2424
Returns
2525
-------
2626
1d array like
2727
The vector containing component signal weights at a moment. Has length C.
28-
2928
"""
3029
stretched_component_matrix = np.asarray(stretched_component_matrix)
3130
target_signal = np.asarray(target_signal)

Diff for: diffpy/snmf/io.py

+24-22
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
from pathlib import Path
2+
13
import numpy as np
24
import scipy.sparse
3-
from pathlib import Path
5+
46
from diffpy.utils.parsers.loaddata import loadData
57

68

@@ -10,8 +12,8 @@ def initialize_variables(data_input, number_of_components, data_type, sparsity=1
1012
Parameters
1113
----------
1214
data_input: 2d array like
13-
The observed or simulated PDF or XRD data provided by the user. Has dimensions R x N where R is the signal length
14-
and N is the number of PDF/XRD signals.
15+
The observed or simulated PDF or XRD data provided by the user. Has dimensions R x N where R is the signa
16+
length and N is the number of PDF/XRD signals.
1517
1618
number_of_components: int
1719
The number of component signals the user would like to decompose 'data_input' into.
@@ -20,23 +22,23 @@ def initialize_variables(data_input, number_of_components, data_type, sparsity=1
2022
The type of data the user has passed into the program. Can assume the value of 'PDF' or 'XRD.'
2123
2224
sparsity: float, optional
23-
The regularization parameter that behaves as the coefficient of a "sparseness" regularization term that enhances
24-
the ability to decompose signals in the case of sparse data e.g. X-ray Diffraction data. A non-zero value
25-
indicates sparsity in the data; greater magnitudes indicate greater amounts of sparsity.
25+
The regularization parameter that behaves as the coefficient of a "sparseness" regularization term that
26+
enhances the ability to decompose signals in the case of sparse data e.g. X-ray Diffraction data.
27+
A non-zero value indicates sparsity in the data; greater magnitudes indicate greater amounts of sparsity.
2628
2729
smoothness: float, optional
28-
The regularization parameter that behaves as the coefficient of a "smoothness" term that ensures that component
29-
signal weightings change smoothly with time. Assumes a default value of 1e18.
30+
The regularization parameter that behaves as the coefficient of a "smoothness" term that ensures that
31+
component signal weightings change smoothly with time. Assumes a default value of 1e18.
3032
3133
Returns
3234
-------
3335
dictionary
34-
The collection of the names and values of the constants used in the algorithm. Contains the number of observed PDF
35-
/XRD patterns, the length of each pattern, the type of the data, the number of components the user would like to
36-
decompose the data into, an initial guess for the component matrix, and initial guess for the weight factor matrix
37-
,an initial guess for the stretching factor matrix, a parameter controlling smoothness of the solution, a
38-
parameter controlling sparseness of the solution, the matrix representing the smoothness term, and a matrix used
39-
to construct a hessian matrix.
36+
The collection of the names and values of the constants used in the algorithm. Contains the number of
37+
observed PDF/XRD patterns, the length of each pattern, the type of the data, the number of components
38+
the user would like to decompose the data into, an initial guess for the component matrix, and initial
39+
guess for the weight factor matrix, an initial guess for the stretching factor matrix, a parameter
40+
controlling smoothness of the solution, a parameter controlling sparseness of the solution, the matrix
41+
representing the smoothness term, and a matrix used to construct a hessian matrix.
4042
4143
"""
4244
signal_length = data_input.shape[0]
@@ -74,22 +76,22 @@ def initialize_variables(data_input, number_of_components, data_type, sparsity=1
7476
def load_input_signals(file_path=None):
7577
"""Processes a directory of a series of PDF/XRD patterns into a usable format.
7678
77-
Constructs a 2d array out of a directory of PDF/XRD patterns containing each files dependent variable column in a
78-
new column. Constructs a 1d array containing the grid values.
79+
Constructs a 2d array out of a directory of PDF/XRD patterns containing each files dependent variable
80+
column in a new column. Constructs a 1d array containing the grid values.
7981
8082
Parameters
8183
----------
8284
file_path: str or Path object, optional
83-
The path to the directory containing the input XRD/PDF data. If no path is specified, defaults to the current
84-
working directory. Accepts a string or a pathlib.Path object. Input data not on the same grid as the first file
85-
read will be ignored.
85+
The path to the directory containing the input XRD/PDF data. If no path is specified, defaults to the
86+
current working directory. Accepts a string or a pathlib.Path object. Input data not on the same grid
87+
as the first file read will be ignored.
8688
8789
Returns
8890
-------
8991
tuple
90-
The tuple whose first element is an R x M 2d array made of PDF/XRD patterns as each column; R is the length of the
91-
signal and M is the number of patterns. The tuple contains a 1d array containing the values of the grid points as
92-
its second element; Has length R.
92+
The tuple whose first element is an R x M 2d array made of PDF/XRD patterns as each column; R is the
93+
length of the signal and M is the number of patterns. The tuple contains a 1d array containing the values
94+
of the grid points as its second element; Has length R.
9395
9496
"""
9597

Diff for: diffpy/snmf/optimizers.py

+15-13
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,39 @@
1-
import numpy as np
21
import cvxpy
2+
import numpy as np
33

44

55
def get_weights(stretched_component_gram_matrix, linear_coefficient, lower_bound, upper_bound):
66
"""Finds the weights of stretched component signals under a two-sided constraint
77
8-
Solves min J(y) = (linear_coefficient)' * y + (1/2) * y' * (quadratic coefficient) * y where lower_bound <= y <=
9-
upper_bound and stretched_component_gram_matrix is symmetric positive definite. Finds the weightings of stretched
10-
component signals under a two-sided constraint.
8+
Solves min J(y) = (linear_coefficient)' * y + (1/2) * y' * (quadratic coefficient) * y where
9+
lower_bound <= y <= upper_bound and stretched_component_gram_matrix is symmetric positive definite.
10+
Finds the weightings of stretched component signals under a two-sided constraint.
1111
1212
Parameters
1313
----------
1414
stretched_component_gram_matrix: 2d array like
15-
The Gram matrix constructed from the stretched component matrix. It is a square positive definite matrix. It has
16-
dimensions C x C where C is the number of component signals. Must be symmetric positive definite.
15+
The Gram matrix constructed from the stretched component matrix. It is a square positive definite matrix.
16+
It has dimensions C x C where C is the number of component signals. Must be symmetric positive definite.
1717
1818
linear_coefficient: 1d array like
19-
The vector containing the product of the stretched component matrix and the transpose of the observed data matrix.
20-
Has length C.
19+
The vector containing the product of the stretched component matrix and the transpose of the observed
20+
data matrix. Has length C.
2121
2222
lower_bound: 1d array like
2323
The lower bound on the values of the output weights. Has the same dimensions of the function output. Each
24-
element in 'lower_bound' determines the minimum value the corresponding element in the function output may take.
24+
element in 'lower_bound' determines the minimum value the corresponding element in the function output may
25+
take.
2526
2627
upper_bound: 1d array like
27-
The upper bound on the values of the output weights. Has the same dimensions of the function output. Each element
28-
in 'upper_bound' determines the maximum value the corresponding element in the function output may take.
28+
The upper bound on the values of the output weights. Has the same dimensions of the function output. Each
29+
element in 'upper_bound' determines the maximum value the corresponding element in the function output may
30+
take.
2931
3032
Returns
3133
-------
3234
1d array like
33-
The vector containing the weightings of the components needed to reconstruct a given input signal from the input
34-
set. Has length C
35+
The vector containing the weightings of the components needed to reconstruct a given input signal from the
36+
input set. Has length C
3537
3638
"""
3739
stretched_component_gram_matrix = np.asarray(stretched_component_gram_matrix)

Diff for: diffpy/snmf/polynomials.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
def rooth(linear_coefficient, constant_term):
55
"""
6-
Returns the largest real root of x^3+(linear_coefficient) * x + constant_term. If there are no real roots return 0.
6+
Returns the largest real root of x^3+(linear_coefficient) * x + constant_term. If there are no real roots
7+
return 0.
78
89
Parameters
910
----------
@@ -15,7 +16,8 @@ def rooth(linear_coefficient, constant_term):
1516
Returns
1617
-------
1718
ndarray of floats
18-
The largest real root of x^3+(linear_coefficient) * x + constant_term if roots are real, else return 0 array
19+
The largest real root of x^3+(linear_coefficient) * x + constant_term if roots are real, else
20+
return 0 array
1921
2022
2123
"""

Diff for: diffpy/snmf/stretchednmfapp.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import numpy as np
21
import argparse
32
from pathlib import Path
4-
from diffpy.snmf.subroutines import lift_data, initialize_components
5-
from diffpy.snmf.containers import ComponentSignal
6-
from diffpy.snmf.io import load_input_signals, initialize_variables
3+
4+
from diffpy.snmf.io import initialize_variables, load_input_signals
5+
from diffpy.snmf.subroutines import initialize_components, lift_data
76

87
ALLOWED_DATA_TYPES = ["powder_diffraction", "pd", "pair_distribution_function", "pdf"]
98

@@ -38,7 +37,7 @@ def create_parser():
3837
"--lift-factor",
3938
type=float,
4039
default=1,
41-
help="The lifting factor. Data will be lifted by lifted_data = data + abs(min(data) * lift). Default is 1.",
40+
help="The lifting factor. Data will be lifted by lifted_data = data + abs(min(data) * lift). Default 1.",
4241
)
4342
parser.add_argument(
4443
"number-of-components",

0 commit comments

Comments
 (0)