From 31182740d703a10dae96165dc0467f419c31af2c Mon Sep 17 00:00:00 2001 From: Sangjoon Bob Lee Date: Thu, 8 Aug 2024 08:39:18 -0400 Subject: [PATCH 01/11] Fix F401 - remove imported but not used --- diffpy/nmf_mapping/__init__.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/diffpy/nmf_mapping/__init__.py b/diffpy/nmf_mapping/__init__.py index ff7b2cc..79d7ef3 100644 --- a/diffpy/nmf_mapping/__init__.py +++ b/diffpy/nmf_mapping/__init__.py @@ -21,7 +21,4 @@ # obtain version information __version__ = "1.0.0" -# top-level import -from diffpy.nmf_mapping.nmf_mapping import nmf_mapping_code as nmf - # End of file From 5d900e4a0837035301ddbc5eeb4eb47db22317a3 Mon Sep 17 00:00:00 2001 From: Sangjoon Bob Lee Date: Thu, 8 Aug 2024 08:40:18 -0400 Subject: [PATCH 02/11] Fix E501 fix line too long problems --- diffpy/nmf_mapping/nmf_mapping/nmf_mapping_code.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/diffpy/nmf_mapping/nmf_mapping/nmf_mapping_code.py b/diffpy/nmf_mapping/nmf_mapping/nmf_mapping_code.py index b7e96d2..ec044c5 100644 --- a/diffpy/nmf_mapping/nmf_mapping/nmf_mapping_code.py +++ b/diffpy/nmf_mapping/nmf_mapping/nmf_mapping_code.py @@ -158,7 +158,8 @@ def NMF_decomposition( nmf_loss = [] pca_explained_variance = [] - # Assuming that the algorithm won't be able to decompose a timeseries of less than x scans into x or more components + # Assuming that the algorithm won't be able to decompose a timeseries of less than x scans into + # x or more components if thresh is None: if len(x_vs_y_df.columns) < 10: max_comp = len(x_vs_y_df.columns) @@ -193,7 +194,8 @@ def NMF_decomposition( if additional_comp: thresh += 1 - # Assuming that the algorithm won't be able to decompose a timeseries of less than x scans into x or more components + # Assuming that the algorithm won't be able to decompose a timeseries of less than x scans into + # x or more components if len(x_vs_y_df.columns) < thresh: n_comp = len(x_vs_y_df.columns) else: From 29893e1b05fdb595df78bcfe61c18dfb66bc3f12 Mon Sep 17 00:00:00 2001 From: Sangjoon Bob Lee Date: Thu, 8 Aug 2024 08:40:55 -0400 Subject: [PATCH 03/11] Fix F821, import setuptools --- setup.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/setup.py b/setup.py index a9a4fab..ac5284c 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,8 @@ Packages: diffpy.nmf_mapping """ -from setuptools import find_packages, setup +import setuptools +from setuptools import find_packages with open("README.md", "r") as fh: long_description = fh.read() From dd0fdfa76679f2d858a8871195277e53a0487e55 Mon Sep 17 00:00:00 2001 From: Sangjoon Bob Lee Date: Thu, 8 Aug 2024 08:42:03 -0400 Subject: [PATCH 04/11] Fix E741 - clarify variable name --- diffpy/nmf_mapping/nmf_mapping/nmf_mapping_code.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/diffpy/nmf_mapping/nmf_mapping/nmf_mapping_code.py b/diffpy/nmf_mapping/nmf_mapping/nmf_mapping_code.py index ec044c5..55c5d8d 100644 --- a/diffpy/nmf_mapping/nmf_mapping/nmf_mapping_code.py +++ b/diffpy/nmf_mapping/nmf_mapping/nmf_mapping_code.py @@ -74,8 +74,8 @@ def load_data(dir, xrd=False): raise FileNotFoundError("No gr or dat files found") n = len(data_list) data_list.sort(key=natural_keys_file_name) - l = loadData(data_list[0]).shape[0] - data_arr = np.zeros((n, l, 2)) + data_length = loadData(data_list[0]).shape[0] + data_arr = np.zeros((n, data_length, 2)) # if not on the same x grid, interpolate, use first data set as standard x_set = loadData(data_list[0])[:, 0] From de9d8e193c7e6013f1153ed28ac5a8a481cde426 Mon Sep 17 00:00:00 2001 From: Sangjoon Bob Lee Date: Thu, 8 Aug 2024 08:44:12 -0400 Subject: [PATCH 05/11] Fix E722 - do not use bare 'except' --- diffpy/nmf_mapping/nmf_mapping/main.py | 2 +- diffpy/nmf_mapping/nmf_mapping/nmf_mapping_code.py | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/diffpy/nmf_mapping/nmf_mapping/main.py b/diffpy/nmf_mapping/nmf_mapping/main.py index a81a7d4..38a3073 100644 --- a/diffpy/nmf_mapping/nmf_mapping/main.py +++ b/diffpy/nmf_mapping/nmf_mapping/main.py @@ -37,7 +37,7 @@ def tup(s): try: l, h = map(int, s.split(",")) return l, h - except: + except Exception: raise TypeError("r range must be low, high") # args diff --git a/diffpy/nmf_mapping/nmf_mapping/nmf_mapping_code.py b/diffpy/nmf_mapping/nmf_mapping/nmf_mapping_code.py index 55c5d8d..afe6766 100644 --- a/diffpy/nmf_mapping/nmf_mapping/nmf_mapping_code.py +++ b/diffpy/nmf_mapping/nmf_mapping/nmf_mapping_code.py @@ -239,7 +239,7 @@ def component_plot(df_components, xrd=False, x_units=None, show=True): """ try: plt.style.use(bg_mpl_style) - except: + except Exception: pass df = df_components.copy() data_list = df.columns @@ -291,7 +291,7 @@ def component_ratio_plot(df_component_weight_timeseries, show=True): """ try: plt.style.use(bg_mpl_style) - except: + except Exception: pass df = df_component_weight_timeseries.copy() component_list = df.index @@ -332,7 +332,7 @@ def reconstruction_error_plot(df_reconstruction_error, show=True): """ try: plt.style.use(bg_mpl_style) - except: + except Exception: pass df = df_reconstruction_error.copy() @@ -374,7 +374,7 @@ def explained_variance_plot(df_explained_var_ratio, show=True): """ try: plt.style.use(bg_mpl_style) - except: + except Exception: pass df = df_explained_var_ratio.copy() From 09b48215a347e2013d4e8ca404a126531afa01d9 Mon Sep 17 00:00:00 2001 From: Sangjoon Bob Lee Date: Thu, 8 Aug 2024 10:09:19 -0400 Subject: [PATCH 06/11] Import nmf and use __all__ --- diffpy/nmf_mapping/__init__.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/diffpy/nmf_mapping/__init__.py b/diffpy/nmf_mapping/__init__.py index 79d7ef3..6fc19a1 100644 --- a/diffpy/nmf_mapping/__init__.py +++ b/diffpy/nmf_mapping/__init__.py @@ -21,4 +21,9 @@ # obtain version information __version__ = "1.0.0" +# top-level import +from diffpy.nmf_mapping.nmf_mapping import nmf_mapping_code as nmf + +__all__ = ["nmf"] + # End of file From c4aec29e0b4860c104ccbe123fc424c835e3ad14 Mon Sep 17 00:00:00 2001 From: Sangjoon Bob Lee Date: Thu, 8 Aug 2024 15:29:03 -0400 Subject: [PATCH 07/11] Import functions directly --- setup.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/setup.py b/setup.py index ac5284c..05f92d9 100644 --- a/setup.py +++ b/setup.py @@ -6,14 +6,13 @@ Packages: diffpy.nmf_mapping """ -import setuptools -from setuptools import find_packages +from setuptools import find_packages, setup with open("README.md", "r") as fh: long_description = fh.read() -setuptools.setup( +setup( name="diffpy.nmf_mapping", version="1.0.0", author="Simon J.L. Billinge", From 6a4e6138296e0f9c40e2bcbc0b4c92680afe842e Mon Sep 17 00:00:00 2001 From: Sangjoon Bob Lee Date: Thu, 8 Aug 2024 15:56:06 -0400 Subject: [PATCH 08/11] Handle exception with specific error types --- diffpy/nmf_mapping/nmf_mapping/main.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/diffpy/nmf_mapping/nmf_mapping/main.py b/diffpy/nmf_mapping/nmf_mapping/main.py index 38a3073..f833902 100644 --- a/diffpy/nmf_mapping/nmf_mapping/main.py +++ b/diffpy/nmf_mapping/nmf_mapping/main.py @@ -34,11 +34,14 @@ def main(args=None): parser = ArgumentParser(prog="nmf_mapping", description=_BANNER, formatter_class=RawTextHelpFormatter) def tup(s): + if not isinstance(s, str): + raise TypeError("Input must be a string of two integers separated by a comma.") + try: l, h = map(int, s.split(",")) return l, h - except Exception: - raise TypeError("r range must be low, high") + except ValueError: + raise ValueError("Input must be two integers separated by a comma (e.g., '1,5')") # args parser.add_argument( From 7a410f43e158850244ddb3e797e559b02d04c56e Mon Sep 17 00:00:00 2001 From: Sangjoon Bob Lee Date: Thu, 8 Aug 2024 16:05:18 -0400 Subject: [PATCH 09/11] Add bg_mpl_stylesheet requirement and remove try/except block --- .../nmf_mapping/nmf_mapping_code.py | 33 +++++-------------- requirements/run.txt | 1 + 2 files changed, 9 insertions(+), 25 deletions(-) diff --git a/diffpy/nmf_mapping/nmf_mapping/nmf_mapping_code.py b/diffpy/nmf_mapping/nmf_mapping/nmf_mapping_code.py index afe6766..f04ed59 100644 --- a/diffpy/nmf_mapping/nmf_mapping/nmf_mapping_code.py +++ b/diffpy/nmf_mapping/nmf_mapping/nmf_mapping_code.py @@ -3,24 +3,20 @@ Local NMF Analysis of PDFs for PDFitc. """ +import re +import warnings from pathlib import Path import matplotlib.pyplot as plt import numpy as np import pandas as pd - -try: - from bg_mpl_stylesheet.bg_mpl_stylesheet import bg_mpl_style -except ImportError: - print("bg_mpl_style not found. Using generic matplotlib style.") -import re -import warnings - +from bg_mpl_stylesheet.bg_mpl_stylesheet import bg_mpl_style from diffpy.utils.parsers.loaddata import loadData from scipy import interpolate from sklearn.decomposition import NMF, PCA from sklearn.exceptions import ConvergenceWarning +plt.style.use(bg_mpl_style) warnings.filterwarnings("ignore", category=FutureWarning) warnings.filterwarnings("ignore", category=ConvergenceWarning) @@ -237,10 +233,7 @@ def component_plot(df_components, xrd=False, x_units=None, show=True): figure on absolute scale """ - try: - plt.style.use(bg_mpl_style) - except Exception: - pass + df = df_components.copy() data_list = df.columns @@ -289,13 +282,9 @@ def component_ratio_plot(df_component_weight_timeseries, show=True): figure on absolute scale """ - try: - plt.style.use(bg_mpl_style) - except Exception: - pass + df = df_component_weight_timeseries.copy() component_list = df.index - fig, ax = plt.subplots(figsize=(6, 8)) # seq to align with input phase for component in component_list: @@ -330,10 +319,7 @@ def reconstruction_error_plot(df_reconstruction_error, show=True): figure on absolute scale with removed files """ - try: - plt.style.use(bg_mpl_style) - except Exception: - pass + df = df_reconstruction_error.copy() fig, ax = plt.subplots(figsize=(6, 8)) @@ -372,10 +358,7 @@ def explained_variance_plot(df_explained_var_ratio, show=True): figure on absolute scale with removed files """ - try: - plt.style.use(bg_mpl_style) - except Exception: - pass + df = df_explained_var_ratio.copy() fig, ax = plt.subplots(figsize=(6, 8)) diff --git a/requirements/run.txt b/requirements/run.txt index 794072e..95c31fb 100644 --- a/requirements/run.txt +++ b/requirements/run.txt @@ -4,3 +4,4 @@ scipy diffpy.utils pandas matplotlib +bg_mpl_stylesheet From 14fa2b2bce35286fb00a29278e0a50265f5ca118 Mon Sep 17 00:00:00 2001 From: Sangjoon Bob Lee Date: Thu, 8 Aug 2024 16:59:01 -0400 Subject: [PATCH 10/11] Fix bg_mpl_stlyesheets module name and usage --- diffpy/nmf_mapping/nmf_mapping/nmf_mapping_code.py | 4 ++-- requirements/run.txt | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/diffpy/nmf_mapping/nmf_mapping/nmf_mapping_code.py b/diffpy/nmf_mapping/nmf_mapping/nmf_mapping_code.py index f04ed59..58827f5 100644 --- a/diffpy/nmf_mapping/nmf_mapping/nmf_mapping_code.py +++ b/diffpy/nmf_mapping/nmf_mapping/nmf_mapping_code.py @@ -10,13 +10,13 @@ import matplotlib.pyplot as plt import numpy as np import pandas as pd -from bg_mpl_stylesheet.bg_mpl_stylesheet import bg_mpl_style +from bg_mpl_stylesheets.styles import all_styles from diffpy.utils.parsers.loaddata import loadData from scipy import interpolate from sklearn.decomposition import NMF, PCA from sklearn.exceptions import ConvergenceWarning -plt.style.use(bg_mpl_style) +plt.style.use(all_styles["bg-style"]) warnings.filterwarnings("ignore", category=FutureWarning) warnings.filterwarnings("ignore", category=ConvergenceWarning) diff --git a/requirements/run.txt b/requirements/run.txt index 95c31fb..862eca2 100644 --- a/requirements/run.txt +++ b/requirements/run.txt @@ -4,4 +4,4 @@ scipy diffpy.utils pandas matplotlib -bg_mpl_stylesheet +bg-mpl-stylesheets From bd81335cb4aeebe8763fe6303a0cfc387f336017 Mon Sep 17 00:00:00 2001 From: Sangjoon Bob Lee Date: Thu, 8 Aug 2024 18:41:06 -0400 Subject: [PATCH 11/11] Fix key name in bg_mpl_stylesheets --- diffpy/nmf_mapping/nmf_mapping/nmf_mapping_code.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/diffpy/nmf_mapping/nmf_mapping/nmf_mapping_code.py b/diffpy/nmf_mapping/nmf_mapping/nmf_mapping_code.py index 58827f5..4ad293a 100644 --- a/diffpy/nmf_mapping/nmf_mapping/nmf_mapping_code.py +++ b/diffpy/nmf_mapping/nmf_mapping/nmf_mapping_code.py @@ -16,7 +16,7 @@ from sklearn.decomposition import NMF, PCA from sklearn.exceptions import ConvergenceWarning -plt.style.use(all_styles["bg-style"]) +plt.style.use(all_styles["bg_style"]) warnings.filterwarnings("ignore", category=FutureWarning) warnings.filterwarnings("ignore", category=ConvergenceWarning)