From edc92f943168deb907e610e63b8c22adcf877038 Mon Sep 17 00:00:00 2001 From: Matthew Evans Date: Mon, 20 Nov 2023 21:11:16 +0000 Subject: [PATCH] Minor tweaks --- pydatalab/pydatalab/apps/raman/blocks.py | 15 +++++++-------- pydatalab/tests/apps/test_raman.py | 2 +- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/pydatalab/pydatalab/apps/raman/blocks.py b/pydatalab/pydatalab/apps/raman/blocks.py index 92072522f..83d81fa41 100644 --- a/pydatalab/pydatalab/apps/raman/blocks.py +++ b/pydatalab/pydatalab/apps/raman/blocks.py @@ -1,6 +1,5 @@ import os from pathlib import Path -from typing import List, Tuple import bokeh import numpy as np @@ -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 = [] @@ -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." @@ -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: @@ -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: diff --git a/pydatalab/tests/apps/test_raman.py b/pydatalab/tests/apps/test_raman.py index 8a19ddf07..7a838a378 100644 --- a/pydatalab/tests/apps/test_raman.py +++ b/pydatalab/tests/apps/test_raman.py @@ -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)