Skip to content

Commit

Permalink
Minor tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
ml-evs committed Nov 20, 2023
1 parent cb9508c commit edc92f9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
15 changes: 7 additions & 8 deletions pydatalab/pydatalab/apps/raman/blocks.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os
from pathlib import Path
from typing import List, Tuple

import bokeh
import numpy as np
Expand All @@ -23,12 +22,14 @@ class RamanBlock(DataBlock):
def plot_functions(self):
return (self.generate_raman_plot,)

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

vendor = None
metadata: dict = {}
if ext == ".txt":
try:
header = []
Expand All @@ -47,20 +48,18 @@ def load(self, location: str | Path) -> Tuple[pd.DataFrame, List[str]]:
and metadata.get("#AxisType[1]") == "Spectr\n"
):
vendor = "labspec"
# for some reason not recognising self.data as an attribute so just putting in dummy variable for now
self.data["metadata"] = metadata
if vendor == "renishaw":
df = pd.DataFrame(np.loadtxt(location), columns=["wavenumber", "intensity"])
elif vendor == "labspec":
df = pd.DataFrame(
np.loadtxt(location, encoding="cp1252"), columns=["wavenumber", "intensity"]
)
metadata = {}
except IndexError:
pass
elif ext == ".wdf":
vendor = "renishaw"
# this metadata cannot get jsonify-ed by the update_block function, so just saving to dummy variable for now
df, dummy = self.make_wdf_df(location)
df, metadata = self.make_wdf_df(location)
if not vendor:
raise Exception(
"Could not detect Raman data vendor -- this file type is not supported by this block."
Expand Down Expand Up @@ -116,7 +115,7 @@ def load(self, location: str | Path) -> Tuple[pd.DataFrame, List[str]]:
"intensity - morphological baseline",
f"baseline (`pybaselines.Baseline.mor`, {half_window=})",
]
return df, y_options
return df, metadata, y_options

@classmethod
def make_wdf_df(self, location: Path | str) -> pd.DataFrame:
Expand Down Expand Up @@ -166,7 +165,7 @@ def generate_raman_plot(self):
self.accepted_file_extensions,
ext,
)
pattern_dfs, y_options = self.load(file_info["location"])
pattern_dfs, y_options, _ = self.load(file_info["location"])
pattern_dfs = [pattern_dfs]

if pattern_dfs:
Expand Down
2 changes: 1 addition & 1 deletion pydatalab/tests/apps/test_raman.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ def data_files():

def test_load(data_files):
for f in data_files:
df, y_options = RamanBlock.load(f)
df, metadata, y_options = RamanBlock.load(f)
assert all(y in df.columns for y in y_options)

0 comments on commit edc92f9

Please sign in to comment.