|
3 | 3 | Local NMF Analysis of PDFs for PDFitc.
|
4 | 4 | """
|
5 | 5 |
|
| 6 | +import re |
| 7 | +import warnings |
6 | 8 | from pathlib import Path
|
7 | 9 |
|
8 | 10 | import matplotlib.pyplot as plt
|
9 | 11 | import numpy as np
|
10 | 12 | 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 |
19 | 14 | from diffpy.utils.parsers.loaddata import loadData
|
20 | 15 | from scipy import interpolate
|
21 | 16 | from sklearn.decomposition import NMF, PCA
|
22 | 17 | from sklearn.exceptions import ConvergenceWarning
|
23 | 18 |
|
| 19 | +plt.style.use(all_styles["bg_style"]) |
24 | 20 | warnings.filterwarnings("ignore", category=FutureWarning)
|
25 | 21 | warnings.filterwarnings("ignore", category=ConvergenceWarning)
|
26 | 22 |
|
@@ -74,8 +70,8 @@ def load_data(dir, xrd=False):
|
74 | 70 | raise FileNotFoundError("No gr or dat files found")
|
75 | 71 | n = len(data_list)
|
76 | 72 | 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)) |
79 | 75 |
|
80 | 76 | # if not on the same x grid, interpolate, use first data set as standard
|
81 | 77 | x_set = loadData(data_list[0])[:, 0]
|
@@ -158,7 +154,8 @@ def NMF_decomposition(
|
158 | 154 |
|
159 | 155 | nmf_loss = []
|
160 | 156 | 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 |
162 | 159 | if thresh is None:
|
163 | 160 | if len(x_vs_y_df.columns) < 10:
|
164 | 161 | max_comp = len(x_vs_y_df.columns)
|
@@ -193,7 +190,8 @@ def NMF_decomposition(
|
193 | 190 |
|
194 | 191 | if additional_comp:
|
195 | 192 | 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 |
197 | 195 | if len(x_vs_y_df.columns) < thresh:
|
198 | 196 | n_comp = len(x_vs_y_df.columns)
|
199 | 197 | else:
|
@@ -235,10 +233,7 @@ def component_plot(df_components, xrd=False, x_units=None, show=True):
|
235 | 233 | figure on absolute scale
|
236 | 234 |
|
237 | 235 | """
|
238 |
| - try: |
239 |
| - plt.style.use(bg_mpl_style) |
240 |
| - except: |
241 |
| - pass |
| 236 | + |
242 | 237 | df = df_components.copy()
|
243 | 238 | data_list = df.columns
|
244 | 239 |
|
@@ -287,13 +282,9 @@ def component_ratio_plot(df_component_weight_timeseries, show=True):
|
287 | 282 | figure on absolute scale
|
288 | 283 |
|
289 | 284 | """
|
290 |
| - try: |
291 |
| - plt.style.use(bg_mpl_style) |
292 |
| - except: |
293 |
| - pass |
| 285 | + |
294 | 286 | df = df_component_weight_timeseries.copy()
|
295 | 287 | component_list = df.index
|
296 |
| - |
297 | 288 | fig, ax = plt.subplots(figsize=(6, 8))
|
298 | 289 | # seq to align with input phase
|
299 | 290 | for component in component_list:
|
@@ -328,10 +319,7 @@ def reconstruction_error_plot(df_reconstruction_error, show=True):
|
328 | 319 | figure on absolute scale with removed files
|
329 | 320 |
|
330 | 321 | """
|
331 |
| - try: |
332 |
| - plt.style.use(bg_mpl_style) |
333 |
| - except: |
334 |
| - pass |
| 322 | + |
335 | 323 | df = df_reconstruction_error.copy()
|
336 | 324 |
|
337 | 325 | fig, ax = plt.subplots(figsize=(6, 8))
|
@@ -370,10 +358,7 @@ def explained_variance_plot(df_explained_var_ratio, show=True):
|
370 | 358 | figure on absolute scale with removed files
|
371 | 359 |
|
372 | 360 | """
|
373 |
| - try: |
374 |
| - plt.style.use(bg_mpl_style) |
375 |
| - except: |
376 |
| - pass |
| 361 | + |
377 | 362 | df = df_explained_var_ratio.copy()
|
378 | 363 |
|
379 | 364 | fig, ax = plt.subplots(figsize=(6, 8))
|
|
0 commit comments