Skip to content

Commit

Permalink
Support .wdf files for 1D Raman (#466)
Browse files Browse the repository at this point in the history
* Fixed RamanBlock load_raman_spectrum error

* Make 1D .wdf files compatible with RamanBlock

* Tidy up

* Add rosettasciio dependency

* Add rosettasciio via pip

* Add example WDF file and rename load function

* Move block tests into app subdir

* Slight tidying

---------

Co-authored-by: Matthew Evans <[email protected]>
  • Loading branch information
elbee99 and ml-evs authored Oct 23, 2023
1 parent 41133b5 commit 3960aca
Show file tree
Hide file tree
Showing 8 changed files with 508 additions and 248 deletions.
1 change: 1 addition & 0 deletions pydatalab/Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ tiktoken = "*"
navani = {git = "git+https://github.com/the-grey-group/[email protected]"}
python-dateutil = "*"
pybaselines = "*"
rosettasciio = "*"

[dev-packages]
pytest = "*"
Expand Down
701 changes: 463 additions & 238 deletions pydatalab/Pipfile.lock

Large diffs are not rendered by default.

Binary file added pydatalab/example_data/raman/raman_example.wdf
Binary file not shown.
52 changes: 43 additions & 9 deletions pydatalab/pydatalab/apps/raman/blocks.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import os
from pathlib import Path
from typing import List, Tuple

import bokeh
import numpy as np
import pandas as pd
from pybaselines import Baseline
from rsciio.renishaw import file_reader
from scipy.signal import medfilt

from pydatalab.blocks.base import DataBlock
Expand All @@ -15,20 +17,23 @@
class RamanBlock(DataBlock):
blocktype = "raman"
description = "Raman spectroscopy"
accepted_file_extensions = (".txt",)
accepted_file_extensions = (".txt", ".wdf")

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

@classmethod
def load(self, location: str) -> Tuple[pd.DataFrame, List[str]]:
def load(self, location: str | Path) -> Tuple[pd.DataFrame, List[str]]:
if not isinstance(location, str):
location = str(location)
ext = os.path.splitext(location)[-1].lower()

df = pd.read_csv(location, sep=r"\s+")

df = df.rename(columns={"#Wave": "wavenumber", "#Intensity": "intensity"})
if ext == ".txt":
df = pd.read_csv(location, sep=r"\s+")
df = df.rename(columns={"#Wave": "wavenumber", "#Intensity": "intensity"})
elif ext == ".wdf":
df = self.make_wdf_df(location)

df["sqrt(intensity)"] = np.sqrt(df["intensity"])
df["log(intensity)"] = np.log10(df["intensity"])
Expand Down Expand Up @@ -81,6 +86,38 @@ def load(self, location: str) -> Tuple[pd.DataFrame, List[str]]:

return df, y_options

@classmethod
def make_wdf_df(self, location: Path | str) -> pd.DataFrame:
"""Read the .wdf file with RosettaSciIO and try to extract
1D Raman spectra.
Parameters:
location: The location of the file to read.
Returns:
A dataframe with the appropriate columns.
"""

raman_data = file_reader(location)

if len(raman_data[0]["axes"]) == 1:
pass
elif len(raman_data[0]["axes"]) == 3:
raise RuntimeError("This block does not support 2D Raman yet.")
else:
raise RuntimeError("Data is not compatible 1D or 2D Raman data.")

intensity = raman_data[0]["data"]
wavenumber_size = raman_data[0]["axes"][0]["size"]
wavenumber_offset = raman_data[0]["axes"][0]["offset"]
wavenumber_scale = raman_data[0]["axes"][0]["scale"]
wavenumbers = np.array(
[wavenumber_offset + i * wavenumber_scale for i in range(wavenumber_size)]
)
df = pd.DataFrame({"wavenumber": wavenumbers, "intensity": intensity})
return df

def generate_raman_plot(self):
file_info = None
pattern_dfs = None
Expand All @@ -97,10 +134,7 @@ def generate_raman_plot(self):
self.accepted_file_extensions,
ext,
)

pattern_dfs, y_options = self.load_raman_spectrum(
file_info["location"],
)
pattern_dfs, y_options = self.load(file_info["location"])
pattern_dfs = [pattern_dfs]

if pattern_dfs:
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion webapp/src/components/datablocks/RamanBlock.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ DataBlockBase as a prop, and save from within DataBlockBase -->
v-model="file_id"
:item_id="item_id"
:block_id="block_id"
:extensions="['.txt']"
:extensions="['.txt', '.wdf']"
updateBlockOnChange
/>

Expand Down

0 comments on commit 3960aca

Please sign in to comment.