funcnodes-span 0.1.3__tar.gz → 0.1.5__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: funcnodes-span
3
- Version: 0.1.3
3
+ Version: 0.1.5
4
4
  Summary:
5
5
  Author: Kourosh Rezaei
6
6
  Author-email: kouroshrezaei90@gmail.com
@@ -2,9 +2,9 @@ import funcnodes as fn
2
2
 
3
3
  from .normalization import NORM_NODE_SHELF as NORM
4
4
  from .smoothing import SMOOTH_NODE_SHELF as SMOOTH
5
+ from .peak_analysis import PEAKS_NODE_SHELF as PEAK
5
6
 
6
-
7
- __version__ = "0.1.3"
7
+ __version__ = "0.1.5"
8
8
 
9
9
  NODE_SHELF = fn.Shelf(
10
10
  name="Spectral Analysis",
@@ -13,5 +13,6 @@ NODE_SHELF = fn.Shelf(
13
13
  subshelves=[
14
14
  NORM,
15
15
  SMOOTH,
16
+ PEAK
16
17
  ],
17
18
  )
@@ -12,6 +12,7 @@ import plotly.graph_objs as go
12
12
  from plotly.subplots import make_subplots
13
13
  import re
14
14
 
15
+
15
16
  class PeakProperties(TypedDict):
16
17
  id: str
17
18
  i_index: int
@@ -286,11 +287,11 @@ def peak_finder(
286
287
  return peak_properties_list
287
288
 
288
289
 
289
- # ['Constant', 'Complex Constant', 'Linear', 'Quadratic', 'Polynomial',
290
- # 'Spline', 'Gaussian', 'Gaussian-2D', 'Lorentzian', 'Split-Lorentzian', 'Voigt',
291
- # 'PseudoVoigt', 'Moffat', 'Pearson4', 'Pearson7', 'StudentsT', 'Breit-Wigner', 'Log-Normal',
292
- # 'Damped Oscillator', 'Damped Harmonic Oscillator', 'Exponential Gaussian', 'Skewed Gaussian',
293
- # 'Skewed Voigt', 'Thermal Distribution', 'Doniach', 'Power Law', 'Exponential', 'Step',
290
+ # ['Constant', 'Complex Constant', 'Linear', 'Quadratic', 'Polynomial',
291
+ # 'Spline', 'Gaussian', 'Gaussian-2D', 'Lorentzian', 'Split-Lorentzian', 'Voigt',
292
+ # 'PseudoVoigt', 'Moffat', 'Pearson4', 'Pearson7', 'StudentsT', 'Breit-Wigner', 'Log-Normal',
293
+ # 'Damped Oscillator', 'Damped Harmonic Oscillator', 'Exponential Gaussian', 'Skewed Gaussian',
294
+ # 'Skewed Voigt', 'Thermal Distribution', 'Doniach', 'Power Law', 'Exponential', 'Step',
294
295
  # 'Rectangle', 'Expression']
295
296
 
296
297
 
@@ -411,10 +412,7 @@ def fit_1D(
411
412
  )
412
413
  pars[f"peak{index+1}_amplitude"].set(value=y[peak["index"]], min=0)
413
414
 
414
- if (
415
- model == "Exponential Gaussian"
416
- or model == "Skewed Gaussian"
417
- ):
415
+ if model == "Exponential Gaussian" or model == "Skewed Gaussian":
418
416
  pars[f"peak{index+1}_gamma"].set(value=1)
419
417
 
420
418
  f += model
@@ -438,10 +436,7 @@ def fit_1D(
438
436
  value=out.__dict__["best_values"][f"peak{index+1}_amplitude"], min=0
439
437
  )
440
438
 
441
- if (
442
- model == "Exponential Gaussian"
443
- or model == "Skewed Gaussian"
444
- ):
439
+ if model == "Exponential Gaussian" or model == "Skewed Gaussian":
445
440
  pars[f"peak{index+1}_gamma"].set(
446
441
  value=out.__dict__["best_values"][f"peak{index+1}_gamma"]
447
442
  )
@@ -474,8 +469,6 @@ def fit_1D(
474
469
  return peak_properties_list
475
470
 
476
471
 
477
-
478
-
479
472
  # Define a mapping from "C0", "C1", etc., to CSS color names
480
473
  color_map = {
481
474
  "C0": "blue",
@@ -490,9 +483,11 @@ color_map = {
490
483
  "C9": "cyan",
491
484
  }
492
485
 
493
- def plot_peaks(peaks):
486
+
487
+ @NodeDecorator(id="span.basics.fit.plot", name="Plot fit 1D")
488
+ def plot_fitted_peaks(peaks: List[PeakProperties]) -> go.Figure:
494
489
  peak = peaks[0]
495
- x =peak['fitting_info']['userkws']['x']
490
+ x = peak["fitting_info"]["userkws"]["x"]
496
491
  # Extract data from peaks
497
492
  y = peak["fitting_info"]["data"]
498
493
  best_fit = peak["fitting_info"]["best_fit"]
@@ -502,13 +497,21 @@ def plot_peaks(peaks):
502
497
 
503
498
  # Add the original data trace
504
499
  fig.add_trace(
505
- go.Scatter(x=x, y=y, mode="lines", name="original", line=dict(color=color_map["C0"])),
500
+ go.Scatter(
501
+ x=x, y=y, mode="lines", name="original", line=dict(color=color_map["C0"])
502
+ ),
506
503
  secondary_y=False,
507
504
  )
508
505
 
509
506
  # Add the best fit trace
510
507
  fig.add_trace(
511
- go.Scatter(x=x, y=best_fit, mode="lines", name="best_fit", line=dict(dash="dash", color=color_map["C1"])),
508
+ go.Scatter(
509
+ x=x,
510
+ y=best_fit,
511
+ mode="lines",
512
+ name="best_fit",
513
+ line=dict(dash="dash", color=color_map["C1"]),
514
+ ),
512
515
  secondary_y=False,
513
516
  )
514
517
 
@@ -518,9 +521,17 @@ def plot_peaks(peaks):
518
521
  color = color_map["C2"]
519
522
  else:
520
523
  peak_number = int(re.search(r"\d+", key).group())
521
- color = color_map.get(f"C{peak_number + 2}", "black") # Default to black if not found
522
-
523
- trace = go.Scatter(x=x, y=peak["fitting_data"][key], mode="lines", name=key, line=dict(color=color))
524
+ color = color_map.get(
525
+ f"C{peak_number + 2}", "black"
526
+ ) # Default to black if not found
527
+
528
+ trace = go.Scatter(
529
+ x=x,
530
+ y=peak["fitting_data"][key],
531
+ mode="lines",
532
+ name=key,
533
+ line=dict(color=color),
534
+ )
524
535
  fig.add_trace(trace, secondary_y=(key != "baseline"))
525
536
 
526
537
  # Update axes labels and legend
@@ -528,18 +539,17 @@ def plot_peaks(peaks):
528
539
  fig.update_yaxes(title_text="Baseline corrected", secondary_y=True)
529
540
  fig.update_layout(
530
541
  title={
531
- 'text': f"{peak['fitting_info']['model_name']} model with fitting score = {np.round(peak['fitting_info']['rsquared'], 4)}",
532
- 'x': 0.5, # Center the title
533
- 'xanchor': 'center'
534
- },
542
+ "text": f"{peak['fitting_info']['model_name']} model with fitting score = {np.round(peak['fitting_info']['rsquared'], 4)}",
543
+ "x": 0.5, # Center the title
544
+ "xanchor": "center",
545
+ },
535
546
  )
536
547
 
537
548
  return fig
538
549
 
539
550
 
540
-
541
551
  PEAKS_NODE_SHELF = Shelf(
542
- nodes=[peak_finder, fit_1D],
552
+ nodes=[peak_finder, fit_1D, plot_fitted_peaks],
543
553
  subshelves=[],
544
554
  name="Peak analysis",
545
555
  description="Tools for the peak analysis of the spectra",
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "funcnodes-span"
3
- version = "0.1.3"
3
+ version = "0.1.5"
4
4
  description = ""
5
5
  authors = ["Kourosh Rezaei <kouroshrezaei90@gmail.com>"]
6
6
  readme = "README.md"
File without changes