Skip to content

Commit

Permalink
Linting and tidying
Browse files Browse the repository at this point in the history
  • Loading branch information
ml-evs committed Aug 15, 2023
1 parent 046ce97 commit 1683cf6
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 250 deletions.
80 changes: 28 additions & 52 deletions pydatalab/pydatalab/apps/raman/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,87 +4,65 @@
import bokeh
import numpy as np
import pandas as pd
from scipy.signal import medfilt
from pybaselines import Baseline
from scipy.signal import medfilt

from pydatalab.blocks.blocks import DataBlock
from pydatalab.bokeh_plots import mytheme, selectable_axes_plot
from pydatalab.file_utils import get_file_info_by_id
from pydatalab.logger import LOGGER
from pydatalab.mongo import flask_mongo

# from .utils import parse_xrdml


class RamanBlock(DataBlock):
blocktype = "raman"
description = "Raman spectroscopy"
accepted_file_extensions = (".txt")

# don't think this is needed for Raman
# potentially the laser wavelength used but don't think it matters
# defaults = {"wavelength": 1.54060}
accepted_file_extensions = (".txt",)

@property
def plot_functions(self):
return (self.generate_raman_plot,)

@classmethod
def load_pattern(
self, location: str
) -> Tuple[pd.DataFrame, List[str]]:

def load_raman_spectrum(self, location: str) -> Tuple[pd.DataFrame, List[str]]:
if not isinstance(location, str):
location = str(location)

ext = os.path.splitext(location.split("/")[-1])[-1].lower()

if ext == ".txt":
df = pd.read_csv(location, sep=r"\s+")

# elif ext == ".xy":
# df = pd.read_csv(location, sep=r"\s+", names=["twotheta", "intensity"])
# leaving here for other file format e.g. .spc
else:
df = pd.read_csv(location, sep=r"\s+", names=["twotheta", "intensity", "error"])
df = pd.read_csv(location, sep=r"\s+")

df = df.rename(columns={"#Wave": "Wavenumber", "#Intensity": "intensity"})

# unnecessary for Raman
# if no wavelength (or invalid wavelength) is passed, don't convert to Q and d
# if wavelength:
# try:
# df["Q (Å⁻¹)"] = 4 * np.pi / wavelength * np.sin(np.deg2rad(df["2θ (°)"]) / 2)
# df["d (Å)"] = 2 * np.pi / df["Q (Å⁻¹)"]
# except (ValueError, ZeroDivisionError):
# pass
df = df.rename(columns={"#Wave": "wavenumber", "#Intensity": "intensity"})

df["sqrt(intensity)"] = np.sqrt(df["intensity"])
df["log(intensity)"] = np.log10(df["intensity"])
df["normalized intensity"] = df["intensity"] / np.max(df["intensity"])
polyfit_deg = 15
polyfit_baseline = np.poly1d(
np.polyfit(df["Wavenumber"], df["normalized intensity"], deg=polyfit_deg)
)(df["Wavenumber"])
np.polyfit(df["wavenumber"], df["normalized intensity"], deg=polyfit_deg)
)(df["wavenumber"])
df["intensity - polyfit baseline"] = df["normalized intensity"] - polyfit_baseline
df["intensity - polyfit baseline"] /= np.max(df["intensity - polyfit baseline"])
df[f"baseline (`numpy.polyfit`, deg={polyfit_deg})"] = polyfit_baseline / np.max(
df[f"baseline (`numpy.polyfit`, {polyfit_deg=})"] = polyfit_baseline / np.max(
df["intensity - polyfit baseline"]
)
df["intensity - polyfit baseline"] /= np.max(df["intensity - polyfit baseline"])

kernel_size = 101
median_baseline = medfilt(df["normalized intensity"], kernel_size=kernel_size)
df["intensity - median baseline"] = df["normalized intensity"] - median_baseline
df[f"baseline (`scipy.signal.medfilt`, {kernel_size=})"] = median_baseline / np.max(
df["intensity - median baseline"]
)
df["intensity - median baseline"] /= np.max(df["intensity - median baseline"])
df[
f"baseline (`scipy.signal.medfilt`, kernel_size={kernel_size})"
] = median_baseline / np.max(df["intensity - median baseline"])

# baseline calculation I used in my data
# baseline calculation I used in my data
half_window = 30
baseline_fitter = Baseline(x_data = df["Wavenumber"])
morphological_baseline = baseline_fitter.mor(df["normalized intensity"], half_window=half_window)[0]
df["intensity - morphological baseline"] = df["normalized intensity"] - morphological_baseline
baseline_fitter = Baseline(x_data=df["wavenumber"])
morphological_baseline = baseline_fitter.mor(
df["normalized intensity"], half_window=half_window
)[0]
df["intensity - morphological baseline"] = (
df["normalized intensity"] - morphological_baseline
)
df[
f"baseline (`pybaselines morphological ({half_window=})"
] = morphological_baseline / np.max(df["intensity - morphological baseline"])
df["intensity - morphological baseline"] /= np.max(df["intensity - morphological baseline"])
df.index.name = location.split("/")[-1]

Expand All @@ -94,18 +72,17 @@ def load_pattern(
"sqrt(intensity)",
"log(intensity)",
"intensity - median baseline",
f"baseline (`scipy.signal.medfilt`, kernel_size={kernel_size})",
f"baseline (`scipy.signal.medfilt`, {kernel_size=})",
"intensity - polyfit baseline",
f"baseline (`numpy.polyfit`, deg={polyfit_deg})",
f"baseline (`numpy.polyfit`, {polyfit_deg=})",
"intensity - morphological baseline",
f"baseline (`pybaselines.Baseline.mor`, half_window={half_window})"
f"baseline (`pybaselines.Baseline.mor`, {half_window=})",
]

return df, y_options

def generate_raman_plot(self):
file_info = None
all_files = None
pattern_dfs = None

if "file_id" not in self.data:
Expand All @@ -121,16 +98,15 @@ def generate_raman_plot(self):
ext,
)

pattern_dfs, y_options = self.load_pattern(
pattern_dfs, y_options = self.load_raman_spectrum(
file_info["location"],
# wavelength=float(self.data.get("wavelength", self.defaults["wavelength"])),
)
pattern_dfs = [pattern_dfs]

if pattern_dfs:
p = selectable_axes_plot(
pattern_dfs,
x_options=["Wavenumber"],
x_options=["wavenumber"],
y_options=y_options,
plot_line=True,
plot_points=True,
Expand Down
49 changes: 0 additions & 49 deletions pydatalab/pydatalab/apps/raman/models.py

This file was deleted.

148 changes: 0 additions & 148 deletions pydatalab/pydatalab/apps/raman/utils.py

This file was deleted.

2 changes: 1 addition & 1 deletion webapp/src/resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const blockTypes = {
comment: { description: "Comment", component: DataBlockBase },
media: { description: "Media", component: MediaBlock },
xrd: { description: "Powder XRD", component: XRDBlock },
raman: { description: "Raman", component: RamanBlock},
raman: { description: "Raman", component: RamanBlock },
cycle: { description: "Electrochemistry", component: CycleBlock },
eis: { description: "EIS", component: EISBlock },
nmr: { description: "NMR", component: NMRBlock },
Expand Down

0 comments on commit 1683cf6

Please sign in to comment.