Skip to content

Commit

Permalink
Styleguide compliant changes, update pyproject.toml
Browse files Browse the repository at this point in the history
  • Loading branch information
torogi94 committed Dec 29, 2022
1 parent 0fef5ff commit f4388ca
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 27 deletions.
8 changes: 6 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,19 @@ maintainers = [
]
keywords = ["RDM", "SAS", "SAXS"]
classifiers = [
"Development Status :: 4 - Beta",
"Development Status :: 5 - Production/Stable",
"Programming Language :: Python :: 3",
"Topic :: Scientific/Engineering :: Chemistry"
]
dependencies = [
"lmfit",
"lxml",
"matplotlib",
"numpy",
"pandas",
"pyaniml"
"pyaniml",
"seaborn",
"scipy",
]

[project.urls]
Expand Down
62 changes: 37 additions & 25 deletions sastools/analyzer/curvefitting.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
"""Implementation of curve fitting for SAS experiments using Gaussian,
Lorentzian, and Voigt models.
"""

import json
import random

from pathlib import Path

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
Expand All @@ -9,27 +15,27 @@
from lmfit import models
from lmfit.model import save_modelresult, load_modelresult
from scipy import signal
from pathlib import Path


class CurveFitting:
"""tool for curve fitting"""
"""Tool for curve fitting"""

def __init__(
self,
experimental_data: pd.DataFrame,
file_name: str,
path_plots: Path,
path_fitting_data: Path,
):
"""Initialize parameters for the curve fitting class passing
experimental data
) -> None:
"""Initialize parameters for the curve fitting class by passing
experimental data whose curve is to be fitted, the file name,
and the paths to where plots fitting data are to be saved.
Args:
experimental_data (pd.DataFrame): _description_
file_name (str): _description_
path_plots (Path): _description_
path_fitting_data (Path): _description_
experimental_data (pd.DataFrame): DataFrame of experimental data whose curve is to be fitted.
file_name (str): Desired name for the files that are being saved.
path_plots (Path): Path to where plots are to be saved.
path_fitting_data (Path): Path to where fitting data is to be saved.
"""
self.exp_data = experimental_data
self.x = self.exp_data.iloc[:, 0].values.tolist()
Expand All @@ -43,7 +49,7 @@ def _pack_data_into_dict(self) -> dict:
data_dict = {"data": {"x": self.x, "y": self.y}}
return data_dict

def plot_raw_data(self):
def plot_raw_data(self) -> None:
"""Plot the data and save the plot"""
exp_data_plot = sns.lineplot(
x="scattering_vector", y="counts_per_area", data=self.exp_data
Expand All @@ -58,7 +64,7 @@ def plot_raw_data(self):

def find_peaks_cwt(
self, peak_widths: tuple = (20,), cutoff_amplitude: float = None
):
) -> None:
"""Find peaks using the `find_peaks_cwt` method from signal.
Prints number of found peaks. Figure with positions of found
peaks is plotted and saved.
Expand Down Expand Up @@ -88,7 +94,7 @@ def find_peaks_cwt(
print("peak number:", j, "x:", key, "y:", value)
j = j + 1

def plot_found_peak(self):
def plot_found_peak(self) -> None:
"""Plot peaks found and save the plot"""
peak_fig, ax = plt.subplots()
ax.plot(self.x, self.y)
Expand All @@ -103,16 +109,16 @@ def plot_found_peak(self):

def set_specifications_manually(
self, number_of_models: int, model_specifications: dict
):
"""Manually sets the specifications for the individual model used for the fitting procedure. Stores the generated
) -> None:
"""Manually sets the specifications for the individual model
used for the fitting procedure. Stores the generated
specifications in a json file.
Args:
number_of_models (int): Number of models to use for the fitting procedure
model_specifications (dict): Fitting parameters for the models, each consisting of the initial values 'type', 'center',
'height' and 'sigma' of the individual models as well as corresponding 'help' parameter which confines the position of
the model center during the fitting procedure.
"""
self.n_models = number_of_models
self.models = model_specifications
Expand All @@ -138,8 +144,9 @@ def set_specifications_manually(

def set_specifications_automatically(
self, tolerance: float, model_type: str
):
"""Automatically sets the specifications for the individual model used for the fitting procedure. Stores the generated
) -> None:
"""Automatically sets the specifications for the individual
model used for the fitting procedure. Stores the generated
specifications in a json file.
Args:
Expand Down Expand Up @@ -169,8 +176,9 @@ def set_specifications_automatically(
) as outfile:
outfile.write(json_models_dict)

def generate_model(self, speci: dict):
"""Generates a composite model and corresponding parameters based on the provided specifications using the `model` class
def generate_model(self, speci: dict) -> tuple:
"""Generates a composite model and corresponding parameters
based on the provided specifications using the `model` class
of the python library `lmfit`.
Args:
Expand Down Expand Up @@ -225,8 +233,9 @@ def generate_model(self, speci: dict):
composite_model = composite_model + model
return composite_model, params

def fit(self):
"""Executes the fitting algorithm starting from the generated model and the corresponding parameters. Saves the output as
def fit(self) -> None:
"""Executes the fitting algorithm starting from the generated
model and the corresponding parameters. Saves the output as
`model_result`, which is as a class of `lmfit`.
"""
with open(
Expand All @@ -242,7 +251,7 @@ def fit(self):
self.path_fitting_data / f"model_result_{self.file_name}.sav",
)

def save_list_of_peak_centers(self):
def save_list_of_peak_centers(self) -> None:
"""Stores the determined peak centers in a text file."""
model_result = load_modelresult(
self.path_fitting_data / f"model_result_{self.file_name}.sav"
Expand All @@ -258,9 +267,12 @@ def save_list_of_peak_centers(self):
for line in list_peak_center:
f.write(f"{line}\n")

def plot_fitting_result(self):
"""Loading the `model_result` and creating a plot using the `plot` method of the `model_result` class, which shows the fit along with the corresponding
residual values. Prints the positions of the individual fitted models along with their heights.
def plot_fitting_result(self) -> None:
"""Loading the `model_result` and creating a plot using the
`plot` method of the `model_result` class, which shows the fit
along with the corresponding residual values. Prints the
positions of the individual fitted models along with their
heights.
"""
model_result = load_modelresult(
self.path_fitting_data / f"model_result_{self.file_name}.sav"
Expand Down

0 comments on commit f4388ca

Please sign in to comment.