Skip to content

Commit 57eb972

Browse files
authored
Merge pull request #3 from bobleesj/precommit
Cookiecutter pre-commit workflow (fix flake8)
2 parents 6884074 + bd81335 commit 57eb972

File tree

5 files changed

+23
-32
lines changed

5 files changed

+23
-32
lines changed

diffpy/nmf_mapping/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,6 @@
2424
# top-level import
2525
from diffpy.nmf_mapping.nmf_mapping import nmf_mapping_code as nmf
2626

27+
__all__ = ["nmf"]
28+
2729
# End of file

diffpy/nmf_mapping/nmf_mapping/main.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,14 @@ def main(args=None):
3434
parser = ArgumentParser(prog="nmf_mapping", description=_BANNER, formatter_class=RawTextHelpFormatter)
3535

3636
def tup(s):
37+
if not isinstance(s, str):
38+
raise TypeError("Input must be a string of two integers separated by a comma.")
39+
3740
try:
3841
l, h = map(int, s.split(","))
3942
return l, h
40-
except:
41-
raise TypeError("r range must be low, high")
43+
except ValueError:
44+
raise ValueError("Input must be two integers separated by a comma (e.g., '1,5')")
4245

4346
# args
4447
parser.add_argument(

diffpy/nmf_mapping/nmf_mapping/nmf_mapping_code.py

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,20 @@
33
Local NMF Analysis of PDFs for PDFitc.
44
"""
55

6+
import re
7+
import warnings
68
from pathlib import Path
79

810
import matplotlib.pyplot as plt
911
import numpy as np
1012
import pandas as pd
11-
12-
try:
13-
from bg_mpl_stylesheet.bg_mpl_stylesheet import bg_mpl_style
14-
except ImportError:
15-
print("bg_mpl_style not found. Using generic matplotlib style.")
16-
import re
17-
import warnings
18-
13+
from bg_mpl_stylesheets.styles import all_styles
1914
from diffpy.utils.parsers.loaddata import loadData
2015
from scipy import interpolate
2116
from sklearn.decomposition import NMF, PCA
2217
from sklearn.exceptions import ConvergenceWarning
2318

19+
plt.style.use(all_styles["bg_style"])
2420
warnings.filterwarnings("ignore", category=FutureWarning)
2521
warnings.filterwarnings("ignore", category=ConvergenceWarning)
2622

@@ -74,8 +70,8 @@ def load_data(dir, xrd=False):
7470
raise FileNotFoundError("No gr or dat files found")
7571
n = len(data_list)
7672
data_list.sort(key=natural_keys_file_name)
77-
l = loadData(data_list[0]).shape[0]
78-
data_arr = np.zeros((n, l, 2))
73+
data_length = loadData(data_list[0]).shape[0]
74+
data_arr = np.zeros((n, data_length, 2))
7975

8076
# if not on the same x grid, interpolate, use first data set as standard
8177
x_set = loadData(data_list[0])[:, 0]
@@ -158,7 +154,8 @@ def NMF_decomposition(
158154

159155
nmf_loss = []
160156
pca_explained_variance = []
161-
# Assuming that the algorithm won't be able to decompose a timeseries of less than x scans into x or more components
157+
# Assuming that the algorithm won't be able to decompose a timeseries of less than x scans into
158+
# x or more components
162159
if thresh is None:
163160
if len(x_vs_y_df.columns) < 10:
164161
max_comp = len(x_vs_y_df.columns)
@@ -193,7 +190,8 @@ def NMF_decomposition(
193190

194191
if additional_comp:
195192
thresh += 1
196-
# Assuming that the algorithm won't be able to decompose a timeseries of less than x scans into x or more components
193+
# Assuming that the algorithm won't be able to decompose a timeseries of less than x scans into
194+
# x or more components
197195
if len(x_vs_y_df.columns) < thresh:
198196
n_comp = len(x_vs_y_df.columns)
199197
else:
@@ -235,10 +233,7 @@ def component_plot(df_components, xrd=False, x_units=None, show=True):
235233
figure on absolute scale
236234
237235
"""
238-
try:
239-
plt.style.use(bg_mpl_style)
240-
except:
241-
pass
236+
242237
df = df_components.copy()
243238
data_list = df.columns
244239

@@ -287,13 +282,9 @@ def component_ratio_plot(df_component_weight_timeseries, show=True):
287282
figure on absolute scale
288283
289284
"""
290-
try:
291-
plt.style.use(bg_mpl_style)
292-
except:
293-
pass
285+
294286
df = df_component_weight_timeseries.copy()
295287
component_list = df.index
296-
297288
fig, ax = plt.subplots(figsize=(6, 8))
298289
# seq to align with input phase
299290
for component in component_list:
@@ -328,10 +319,7 @@ def reconstruction_error_plot(df_reconstruction_error, show=True):
328319
figure on absolute scale with removed files
329320
330321
"""
331-
try:
332-
plt.style.use(bg_mpl_style)
333-
except:
334-
pass
322+
335323
df = df_reconstruction_error.copy()
336324

337325
fig, ax = plt.subplots(figsize=(6, 8))
@@ -370,10 +358,7 @@ def explained_variance_plot(df_explained_var_ratio, show=True):
370358
figure on absolute scale with removed files
371359
372360
"""
373-
try:
374-
plt.style.use(bg_mpl_style)
375-
except:
376-
pass
361+
377362
df = df_explained_var_ratio.copy()
378363

379364
fig, ax = plt.subplots(figsize=(6, 8))

requirements/run.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ scipy
44
diffpy.utils
55
pandas
66
matplotlib
7+
bg-mpl-stylesheets

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
long_description = fh.read()
1313

1414

15-
setuptools.setup(
15+
setup(
1616
name="diffpy.nmf_mapping",
1717
version="1.0.0",
1818
author="Simon J.L. Billinge",

0 commit comments

Comments
 (0)