skpkg: apply black line-length 115 to all files in the project directory by dabeycorn · Pull Request #35 · diffpy/diffpy.nmf_mapping · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions .github/ISSUE_TEMPLATE/release_checklist.md
18 changes: 16 additions & 2 deletions doc/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,13 @@
# (source start file, target name, title,
# author, documentclass [howto, manual, or own class]).
latex_documents = [
("index", "diffpy.nmf_mapping.tex", "diffpy.nmf_mapping Documentation", ab_authors, "manual"),
(
"index",
"diffpy.nmf_mapping.tex",
"diffpy.nmf_mapping Documentation",
ab_authors,
"manual",
),
]

# The name of an image file (relative to this directory) to place at the top of
Expand Down Expand Up @@ -249,7 +255,15 @@

# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [("index", "diffpy.nmf_mapping", "diffpy.nmf_mapping Documentation", ab_authors, 1)]
man_pages = [
(
"index",
"diffpy.nmf_mapping",
"diffpy.nmf_mapping Documentation",
ab_authors,
1,
)
]

# If true, show URL addresses after external links.
# man_show_urls = False
Expand Down
3 changes: 1 addition & 2 deletions src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
#
##############################################################################

"""nmf_mapping - tools for performing NMF on PDF and XRD data.
"""
"""nmf_mapping - tools for performing NMF on PDF and XRD data."""


__import__("pkg_resources").declare_namespace(__name__)
Expand Down
65 changes: 48 additions & 17 deletions src/diffpy/nmf_mapping/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ def main(args=None):
as well as the reconstruction error as a fxn of component
"""

parser = ArgumentParser(prog="nmf_mapping", description=_BANNER, formatter_class=RawTextHelpFormatter)
parser = ArgumentParser(
prog="nmf_mapping",
description=_BANNER,
formatter_class=RawTextHelpFormatter,
)

def tup(s):
if not isinstance(s, str):
Expand All @@ -45,7 +49,10 @@ def tup(s):

# args
parser.add_argument(
"directory", default=None, type=str, help="a directory of PDFs to calculate NMF decomposition"
"directory",
default=None,
type=str,
help="a directory of PDFs to calculate NMF decomposition",
)
group = parser.add_mutually_exclusive_group()
parser.add_argument(
Expand Down Expand Up @@ -105,25 +112,37 @@ def tup(s):
nargs="*",
help="the x-range over which to calculate NMF, can be multiple ranges (e.g. --xrange 5,10 12,15)",
)
parser.add_argument("--show", default=True, type=boolean_string, help="whether to show the plot")
parser.add_argument(
"--show",
default=True,
type=boolean_string,
help="whether to show the plot",
)
args0 = Namespace()
args1, _ = parser.parse_known_args(args, namespace=args0)

input_list, data_list = nmf.load_data(args1.directory, args1.xrd)
if args1.pca_thresh:
df_components, df_component_weight_timeseries, df_reconstruction_error, df_explained_var_ratio = (
nmf.NMF_decomposition(
input_list,
args1.xrange,
args1.threshold,
additional_comp=False,
improve_thresh=args1.improve_thresh,
n_iter=args1.n_iter,
pca_thresh=args1.pca_thresh,
)
(
df_components,
df_component_weight_timeseries,
df_reconstruction_error,
df_explained_var_ratio,
) = nmf.NMF_decomposition(
input_list,
args1.xrange,
args1.threshold,
additional_comp=False,
improve_thresh=args1.improve_thresh,
n_iter=args1.n_iter,
pca_thresh=args1.pca_thresh,
)
else:
df_components, df_component_weight_timeseries, df_reconstruction_error = nmf.NMF_decomposition(
(
df_components,
df_component_weight_timeseries,
df_reconstruction_error,
) = nmf.NMF_decomposition(
input_list,
args1.xrange,
args1.threshold,
Expand All @@ -149,7 +168,11 @@ def tup(s):
os.path.join(os.getcwd(), "nmf_result", "component_index_vs_pratio_col.json")
)
df_component_weight_timeseries.to_csv(
os.path.join(os.getcwd(), "nmf_result", output_fn + "component_row_pratio_col.txt"),
os.path.join(
os.getcwd(),
"nmf_result",
output_fn + "component_row_pratio_col.txt",
),
header=None,
index=False,
sep=" ",
Expand Down Expand Up @@ -196,7 +219,11 @@ def tup(s):

if args1.xrd:
np.savetxt(
os.path.join(os.getcwd(), "nmf_result", output_fn + f"_comp{i}" + ".xy"),
os.path.join(
os.getcwd(),
"nmf_result",
output_fn + f"_comp{i}" + ".xy",
),
data,
header=f"NMF Generated XRD\nSource = nmfMapping\n"
f"Date = {output_fn}\n{args1.x_units} Intensity\n",
Expand All @@ -205,7 +232,11 @@ def tup(s):
)
else:
np.savetxt(
os.path.join(os.getcwd(), "nmf_result", output_fn + f"_comp{i}" + ".cgr"),
os.path.join(
os.getcwd(),
"nmf_result",
output_fn + f"_comp{i}" + ".cgr",
),
data,
header=f"NMF Generated PDF\nSource: nmfMapping\n" f"Date: {output_fn}\nr g",
fmt="%s",
Expand Down
27 changes: 23 additions & 4 deletions src/diffpy/nmf_mapping/nmf_mapping_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,13 @@ def load_data(dir, xrd=False):

# TODO Add regularization on the frobenius norm in order to prevent creation of an excessive number of components
def NMF_decomposition(
data_arr, x_range=None, thresh=None, additional_comp=False, improve_thresh=None, n_iter=None, pca_thresh=None
data_arr,
x_range=None,
thresh=None,
additional_comp=False,
improve_thresh=None,
n_iter=None,
pca_thresh=None,
):
"""
Takes a 3D array of PDFs and returns the structurally significant
Expand Down Expand Up @@ -208,8 +214,17 @@ def NMF_decomposition(
df_component_weight_timeseries = pd.DataFrame(nmf_weight, index=range(n_comp))

if pca_thresh:
return df_components, df_component_weight_timeseries, df_reconstruction_error, df_explained_var_ratio
return df_components, df_component_weight_timeseries, df_reconstruction_error
return (
df_components,
df_component_weight_timeseries,
df_reconstruction_error,
df_explained_var_ratio,
)
return (
df_components,
df_component_weight_timeseries,
df_reconstruction_error,
)


def component_plot(df_components, xrd=False, x_units=None, show=True):
Expand Down Expand Up @@ -244,7 +259,11 @@ def component_plot(df_components, xrd=False, x_units=None, show=True):
shift = max_range
# seq to align with input phase
for i, s in enumerate(data_list):
ax.plot(df.index.to_numpy(dtype=np.single), df[s].to_numpy() + i * shift, label=s)
ax.plot(
df.index.to_numpy(dtype=np.single),
df[s].to_numpy() + i * shift,
label=s,
)
ax.legend(loc="best")
if xrd:
if x_units == "twotheta" or x_units == "ttheta":
Expand Down
3 changes: 1 addition & 2 deletions src/diffpy/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
#
##############################################################################

"""Definition of __version__ and __date__ for diffpy.nmf_mapping.
"""
"""Definition of __version__ and __date__ for diffpy.nmf_mapping."""

__id__ = "$Id$"

Expand Down
6 changes: 5 additions & 1 deletion tests/test_NMF_analysis_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
test_map = [
([data_dir, "--xrange", "5,10"], "output_1", "Number of components: 3\n"),
([data_dir], "output_2", "Number of components: 3\n"),
([data_dir, "--xrange", "5,10", "12,15"], "output_3", "Number of components: 3\n"),
(
[data_dir, "--xrange", "5,10", "12,15"],
"output_3",
"Number of components: 3\n",
),
]


Expand Down
3 changes: 1 addition & 2 deletions tests/test_version.py