### Set Up Virtual Environment and Install Source: https://github.com/barahona-research-group/ramanspy/blob/main/CONTRIBUTING.rst Create a virtual environment for the project to manage dependencies separately and install the project. This ensures a clean and isolated development environment. ```bash python -m venv venv source venv/bin/activate # On Windows, use: venv\Scripts\activate pip install . ``` -------------------------------- ### Load and prepare example data Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/_downloads/1404a83df5094668a673349fef8ec679/plot_vi_customisation.ipynb Loads the 'Volumetric cell data' from RamanSPy, crops it, and extracts specific layers and spectral slices for plotting examples. ```python dir_ = r'../../../../data/kallepitis_data' volumes = ramanspy.datasets.volumetric_cells(cell_type='THP-1', folder=dir_) cell_volume = volumes[0] # crop the data cropper = ramanspy.preprocessing.misc.Cropper(region=(300, None)) cell_volume = cropper.apply(cell_volume) # get the fourth layer of the volume as an example spectral image cell_layer = cell_volume.layer(4) # define example volume and image slices and spectra cell_volume_slices = [cell_volume.band(1600), cell_volume.band(2930), cell_volume.band(3300)] cell_layer_slices = [cell_layer.band(1600), cell_layer.band(2930), cell_layer.band(3300)] spectra = [cell_layer[20, 30], cell_layer[30, 20], cell_layer[10, 20]] ``` -------------------------------- ### Load and prepare example data Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/_sources/auto_tutorials/iv-viz/plot_vi_customisation.rst.txt Loads volumetric cell data and preprocesses it by cropping and selecting a specific layer for analysis. ```python dir_ = r'../../../../data/kallepitis_data' volumes = ramanspy.datasets.volumetric_cells(cell_type='THP-1', folder=dir_) ``` ```python cell_volume = volumes[0] # crop the data cropper = ramanspy.preprocessing.misc.Cropper(region=(300, None)) cell_volume = cropper.apply(cell_volume) # get the fourth layer of the volume as an example spectral image cell_layer = cell_volume.layer(4) # define example volume and image slices and spectra cell_volume_slices = [cell_volume.band(1600), cell_volume.band(2930), cell_volume.band(3300)] cell_layer_slices = [cell_layer.band(1600), cell_layer.band(2930), cell_layer.band(3300)] spectra = [cell_layer[20, 30], cell_layer[30, 20], cell_layer[10, 20]] ``` -------------------------------- ### Bergholt 2016 Preprocessing Protocol Example Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/generated/prepprocessing/protocols/ramanspy.preprocessing.protocols.bergholt2016.html This example demonstrates how to apply the Bergholt 2016 preprocessing protocol to Raman data. It first creates the pipeline and then applies it to the input data. ```python pipeline = preprocessing.protocols.ARTICULAR_CARTILAGE() preprocessed_data = pipeline.apply(data) ``` -------------------------------- ### Initialize SpectralContainer Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/_modules/ramanspy/core.html Demonstrates how to initialize a SpectralContainer with spectral data and axis. Includes an example of converting wavelength units to wavenumber units using utils.wavelength_to_wavenumber. ```python import numpy as np import ramanspy as rp spectral_data = np.random.rand(20, 1500) spectral_axis = np.linspace(100, 3600, 1500) # if the spectral axis is in wavelength units (nm) and needs converting spectral_axis = rp.utils.wavelength_to_wavenumber(spectral_axis) raman_object = rp.SpectralContainer(spectral_data, spectral_axis) ``` -------------------------------- ### Install RamanSPy Source: https://github.com/barahona-research-group/ramanspy/blob/main/README.md Install the RamanSPy package using pip. This command fetches the latest version from PyPI. ```console pip install ramanspy ``` -------------------------------- ### Initialize a Preprocessing Pipeline Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/_modules/ramanspy/preprocessing/Pipeline.html Create a new preprocessing pipeline by providing a list of preprocessing steps. This example shows how to include custom functions and built-in steps like SavGol filtering and Vector normalization. ```python from ramanspy import preprocessing preprocessing_pipeline = preprocessing.Pipeline([ preprocessing.PreprocessingStep(some_custom_preprocessing_func, *args, **kwargs), preprocessing.denoise.SavGol(window_length=7, polyorder=3), preprocessing.normalise.Vector() ]) ``` -------------------------------- ### Load, Preprocess, Analyze, and Visualize Raman Data Source: https://github.com/barahona-research-group/ramanspy/blob/main/README.md Example demonstrating a typical workflow in RamanSPy: loading data from a Witec instrument, applying a preprocessing pipeline, performing spectral unmixing, and visualizing the results. Ensure you replace '' with the actual file path. ```python import ramanspy as rp # load data image_data = rp.load.witec("") # apply a preprocessing pipeline pipeline = rp.preprocessing.Pipeline([ rp.preprocessing.misc.Cropper(region=(700, 1800)), rp.preprocessing.despike.WhitakerHayes(), rp.preprocessing.denoise.SavGol(window_length=9, polyorder=3), rp.preprocessing.baseline.ASPLS(), rp.preprocessing.normalise.MinMax() ]) data = pipeline.apply(image_data) # perform spectral unmixing nfindr = rp.analysis.unmix.NFINDR(n_endmembers=5) amaps, endmembers = nfindr.apply(data) # plot results rp.plot.spectra(endmembers) rp.plot.image(amaps) rp.plot.show() ``` -------------------------------- ### Create a Raman Spectrum instance Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/_downloads/9da78011b06d1bdb69867f4b9ae05b9e/plot_ii_spectrum_container.ipynb Initialize a Spectrum object by providing 1D intensity data and the corresponding wavenumber axis. This example creates a spectrum with 1500 data points. ```python spectral_axis = np.linspace(100, 3600, 1500) spectral_data = np.sin(spectral_axis/120) raman_spectrum = ramanspy.Spectrum(spectral_data, spectral_axis) ``` -------------------------------- ### Importing Libraries and Loading Data Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/auto_tutorials/iv-viz/plot_vi_customisation.html Imports necessary libraries (matplotlib.pyplot and ramanspy) and loads example volumetric cell data for customization demonstrations. ```python from matplotlib import pyplot as plt import ramanspy dir_ = r'../../../../data/kallepitis_data' volumes = ramanspy.datasets.volumetric_cells(cell_type='THP-1', folder=dir_) ``` -------------------------------- ### Load Pre-trained ResUNet Model Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/_downloads/d82e73c01b6c50095b1b356c6c47e8a4/plot_ii_dl_denoising.ipynb Loads a pre-trained ResUNet model state dictionary. Ensure the model architecture matches the state dictionary. This example uses PyTorch and loads the model to the CPU. ```python net = ResUNet(3, False).float() net.load_state_dict(torch.load(r"ResUNet.pt", map_location=torch.device('cpu'))) ``` -------------------------------- ### Load Data and Prepare for Visualization Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/_sources/auto_tutorials/iv-viz/plot_v_peak_dist.rst.txt Imports necessary libraries and loads the bacteria dataset. The data is then preprocessed into a list of samples, each containing 2000 spectra, and labels are generated for these samples. ```python import numpy as np from matplotlib import pyplot as plt import ramanspy dir_ = r"../../../../data/bacteria_data" X_train, y_train = ramanspy.datasets.bacteria("train", folder=dir_) bacteria_lists = [[X_train[i:i+2000, :]] for i in range(0, X_train.shape[0], 2000)] bacteria_sample = bacteria_lists[:5] bacteria_sample_labels = [f"Species {int(y_train[i*2000])}" for i in range(0, 5)] ``` -------------------------------- ### Upgrade RamanSPy Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/_sources/installation.rst.txt Upgrade an existing RamanSPy installation to the latest version using pip. ```console pip install ramanspy --upgrade ``` -------------------------------- ### Access Spectral Axis Length Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/_downloads/cd1b15cae80fcdf13744ad6dc2110d7b/plot_i_generic_container.ipynb Gets the number of data points along the spectral axis. ```python # access to the length of the spectral axis raman_hypervolume.spectral_length ``` -------------------------------- ### Access Non-Spectral Data Shape Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/_downloads/cd1b15cae80fcdf13744ad6dc2110d7b/plot_i_generic_container.ipynb Gets the shape of the data excluding the spectral dimension, representing spatial dimensions. ```python # access to the non-spectral (i.e. spatial) shape of the data encapsulated within the instance raman_hypervolume.shape ``` -------------------------------- ### Initialize SpectralContainer with Various Dimensions Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/_downloads/cd1b15cae80fcdf13744ad6dc2110d7b/plot_i_generic_container.ipynb Demonstrates creating SpectralContainer instances for different data dimensions, from a single spectrum to a hypervolume. ```python spectral_data = np.random.rand(1500) raman_spectrum = ramanspy.SpectralContainer(spectral_data, spectral_axis) spectral_data = np.random.rand(20, 20, 1500) raman_image = ramanspy.SpectralContainer(spectral_data, spectral_axis) spectral_data = np.random.rand(20, 20, 20, 1500) raman_volume = ramanspy.SpectralContainer(spectral_data, spectral_axis) spectral_data = np.random.rand(20, 20, 20, 20, 1500) raman_hypervolume = ramanspy.SpectralContainer(spectral_data, spectral_axis) ``` -------------------------------- ### Import necessary libraries Source: https://github.com/barahona-research-group/ramanspy/blob/main/paper_reproducibility/fig5_bacteria_classification.ipynb Imports required libraries for machine learning, data manipulation, and plotting. Ensure these are installed before running. ```python from lazypredict.Supervised import LazyClassifier from sklearn.utils import shuffle from sklearn.metrics import accuracy_score, confusion_matrix import matplotlib.pyplot as plt import seaborn as sns import numpy as np ``` ```python import ramanspy ``` ```python plt.rcdefaults() ``` -------------------------------- ### Applying a Preprocessing Pipeline Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/_modules/ramanspy/preprocessing/Pipeline.html Demonstrates how to apply an initialized preprocessing pipeline to various Raman data structures. The pipeline is applied sequentially. ```python # once a preprocessing pipeline is initialised, it can be applied to different Raman data just as single PreprocessingStep instances preprocessed_data = preprocessing_pipeline.apply(raman_object) preprocessed_data = preprocessing_method.apply([raman_object, raman_spectrum, raman_image]) preprocessed_data = preprocessing_method.apply([raman_object, raman_spectrum], raman_object, [raman_spectrum, raman_image]) ``` -------------------------------- ### Import Libraries and Initialize SpectralVolume Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/_sources/auto_tutorials/i-classes/plot_iv_volume_container.rst.txt Import necessary libraries and initialize a SpectralVolume instance with a 4D intensity data array and a spectral axis. This sets up the volumetric data structure for analysis. ```python import numpy as np import ramanspy ``` ```python spectral_data = np.random.rand(50, 50, 10, 1500) spectral_axis = np.linspace(100, 3600, 1500) raman_volume = ramanspy.SpectralVolume(spectral_data, spectral_axis) ``` -------------------------------- ### Define Bands and Get Colors Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/_downloads/ff8c4da9032d200d102e1da05a80bd8e/plot_v_peak_dist.ipynb Defines the Raman bands of interest and retrieves corresponding colors from the default matplotlib colormap. ```python # defining some bands we are interested in bands = [400, 800, 1200, 1600] # getting the corresponding colors using the default colormap colors = list(plt.cm.get_cmap()(np.linspace(0, 1, len(bands)))) ``` -------------------------------- ### Get Default Preprocessing Pipeline Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/_downloads/fdf9172f6e1f349712607758dbc3ca35/plot_iv_predefined_pipeline.ipynb Retrieves the default fingerprint preprocessing pipeline from ramanspy.preprocessing.protocols. This pipeline can be used for standard preprocessing tasks. ```python preprocessing_pipeline = ramanspy.preprocessing.protocols.default_fingerprint() ``` -------------------------------- ### Generate and Plot Synthetic Spectra Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/_downloads/208ea2ce3d891e0316a8fb17d9fe77f2/plot_i_endmembers.ipynb Generates synthetic Raman spectra with realistic parameters and displays them in a stacked plot. Ensure RamanSPy is installed. ```python import ramanspy as rp # Generate synthetic spectra spectra = rp.synth.generate_spectra(5, 1000, realistic=True) rp.plot.spectra(spectra, plot_type='single stacked') rp.plot.show() ``` -------------------------------- ### BackgroundSubtractor.__init__ Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/generated/prepprocessing/misc/ramanspy.preprocessing.misc.BackgroundSubtractor.html Initializes the BackgroundSubtractor object. This is the constructor for the class. ```APIDOC ## BackgroundSubtractor.__init__() ### Description Initializes the BackgroundSubtractor object. ### Method __init__ ### Parameters This method does not take any explicit parameters beyond `self`. ``` -------------------------------- ### PCA.__init__() Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/generated/analysis/decompose/ramanspy.analysis.decompose.PCA.html Initializes the PCA object. This is the main entry point for using PCA for spectral decomposition. ```APIDOC ## PCA.__init__() ### Description Initializes the PCA object. This is the main entry point for using PCA for spectral decomposition. ### Method __init__ ### Parameters This method does not explicitly list parameters in the provided documentation. Refer to the class definition for potential arguments. ### Request Example ```python from ramanspy.analysis.decompose import PCA # Example of initializing PCA (specific arguments depend on the class implementation) # pca_model = PCA(...) ``` ### Response #### Success Response Initializes a PCA object. #### Response Example ```python # No direct response example available, as this is a constructor. ``` ``` -------------------------------- ### ResUNet Model Definition Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/source/auto_examples/plot_ii_dl_denoising.ipynb Defines the ResUNet architecture for denoising. This includes helper classes for convolutional blocks and linear transformations. It requires PyTorch to be installed. ```python """MIT License Copyright (c) 2020 conor-horgan Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ import torch from torch import nn class BasicConv(nn.Module): def __init__(self, channels_in, channels_out, batch_norm): super(BasicConv, self).__init__() basic_conv = [nn.Conv1d(channels_in, channels_out, kernel_size=3, stride=1, padding=1, bias=True)] basic_conv.append(nn.PReLU()) if batch_norm: basic_conv.append(nn.BatchNorm1d(channels_out)) self.body = nn.Sequential(*basic_conv) def forward(self, x): return self.body(x) class ResUNetConv(nn.Module): def __init__(self, num_convs, channels, batch_norm): super(ResUNetConv, self).__init__() unet_conv = [] for _ in range(num_convs): unet_conv.append(nn.Conv1d(channels, channels, kernel_size=3, stride=1, padding=1, bias=True)) unet_conv.append(nn.PReLU()) if batch_norm: unet_conv.append(nn.BatchNorm1d(channels)) self.body = nn.Sequential(*unet_conv) def forward(self, x): res = self.body(x) res += x return res class UNetLinear(nn.Module): def __init__(self, repeats, channels_in, channels_out): super().__init__() modules = [] for i in range(repeats): modules.append(nn.Linear(channels_in, channels_out)) modules.append(nn.PReLU()) self.body = nn.Sequential(*modules) def forward(self, x): x = self.body(x) return x class ResUNet(nn.Module): def __init__(self, num_convs, batch_norm): super(ResUNet, self).__init__() res_conv1 = [BasicConv(1, 64, batch_norm)] res_conv1.append(ResUNetConv(num_convs, 64, batch_norm)) self.conv1 = nn.Sequential(*res_conv1) self.pool1 = nn.MaxPool1d(2) res_conv2 = [BasicConv(64, 128, batch_norm)] res_conv2.append(ResUNetConv(num_convs, 128, batch_norm)) self.conv2 = nn.Sequential(*res_conv2) self.pool2 = nn.MaxPool1d(2) res_conv3 = [BasicConv(128, 256, batch_norm)] res_conv3.append(ResUNetConv(num_convs, 256, batch_norm)) res_conv3.append(BasicConv(256, 128, batch_norm)) self.conv3 = nn.Sequential(*res_conv3) self.up3 = nn.Upsample(scale_factor=2) res_conv4 = [BasicConv(256, 128, batch_norm)] res_conv4.append(ResUNetConv(num_convs, 128, batch_norm)) res_conv4.append(BasicConv(128, 64, batch_norm)) self.conv4 = nn.Sequential(*res_conv4) self.up4 = nn.Upsample(scale_factor=2) res_conv5 = [BasicConv(128, 64, batch_norm)] res_conv5.append(ResUNetConv(num_convs, 64, batch_norm)) self.conv5 = nn.Sequential(*res_conv5) res_conv6 = [BasicConv(64, 1, batch_norm)] self.conv6 = nn.Sequential(*res_conv6) self.linear7 = UNetLinear(3, 500, 500) def forward(self, x): x = self.conv1(x) x1 = self.pool1(x) x2 = self.conv2(x1) x3 = self.pool1(x2) x3 = self.conv3(x3) x3 = self.up3(x3) x4 = torch.cat((x2, x3), dim=1) x4 = self.conv4(x4) x5 = self.up4(x4) x6 = torch.cat((x, x5), dim=1) x6 = self.conv5(x6) x7 = self.conv6(x6) out = self.linear7(x7) return out ``` -------------------------------- ### CornerCutting.__init__() Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/generated/prepprocessing/baseline/ramanspy.preprocessing.baseline.CornerCutting.html Initializes the CornerCutting baseline correction object. ```APIDOC ## CornerCutting.__init__() ### Description Initializes the CornerCutting baseline correction object. ### Method __init__ ### Parameters This method does not explicitly list parameters in the provided documentation. ``` -------------------------------- ### Initialize MinMax Normalization Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/_downloads/d82e73c01b6c50095b1b356c6c47e8a4/plot_ii_dl_denoising.ipynb Sets up a MinMax normalizer object to scale spectral data to the range [0, 1]. This is useful for consistent input to models and metrics. ```python minmax = ramanspy.preprocessing.normalise.MinMax() ``` -------------------------------- ### Initialize SpectralImage Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/auto_tutorials/i-classes/plot_iii_image_container.html Instantiate a SpectralImage object using the prepared spectral data and axis. ```python raman_image = ramanspy.SpectralImage(spectral_data, spectral_axis) ``` -------------------------------- ### Get the shape of the SpectralVolume Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/_downloads/b5691425122ad036bc6502e2dc989186/plot_iv_volume_container.ipynb Retrieves the shape of the initialized SpectralVolume object, indicating its dimensions (e.g., spatial x, spatial y, depth, spectral points). ```python raman_volume.shape ``` -------------------------------- ### Load Ocean Insight .txt files Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/_modules/ramanspy/load.html Loads Raman spectra from Ocean Insight's OceanView software .txt files. Supports optional preprocessing and specification of laser excitation wavelength. ```python import ramanspy as rp # Loading a single spectrum raman_spectrum = rp.load.ocean_insight("path/to/file/ocean_insight_spectrum.txt") ``` -------------------------------- ### Load Training and Testing Datasets Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/auto_tutorials/iii-datasets/plot_i_bacteria.html Load the training and testing splits of the bacteria dataset. This is a common starting point for building and evaluating classification models. ```python import ramanspy as rp # Load training and testing datasets X_train, y_train = rp.datasets.bacteria("train", path_to_data="path/to/data") X_test, y_test = rp.datasets.bacteria("test", path_to_data="path/to/data")) ``` -------------------------------- ### Internal Get Indices to Leave Function Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/_modules/ramanspy/preprocessing/misc.html Calculates indices for spectral data within a specified region, handling None values and swapped start/end points. ```python def _get_indices_to_leave(spectral_axis, region): start = spectral_axis[0] if region[0] is None else region[0] end = spectral_axis[-1] if region[1] is None else region[1] if start > end: # swap start, end = end, start indices_to_leave = np.logical_and( start <= spectral_axis, spectral_axis <= end) return indices_to_leave ``` -------------------------------- ### Pipeline Initialization Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/_modules/ramanspy/preprocessing/Pipeline.html Initializes a preprocessing pipeline with a list of preprocessing steps. ```APIDOC ## Pipeline ### Description Defines a preprocessing pipeline consisting of multiple preprocessing procedures. ### Parameters * **pipeline** (list[:class:`PreprocessingStep`]) - The preprocessing procedures defining the pipeline. ### Example ```python from ramanspy import preprocessing preprocessing_pipeline = preprocessing.Pipeline([ preprocessing.PreprocessingStep(some_custom_preprocessing_func, *args, **kwargs), preprocessing.denoise.SavGol(window_length=7, polyorder=3), preprocessing.normalise.Vector() ]) ``` ``` -------------------------------- ### Load and Plot Original Raman Spectra Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/auto_tutorials/v-preprocessing/plot_i_predefined_methods.html Loads volumetric cell data and selects a random spectrum for visualization. This is a starting point before applying preprocessing steps. ```python volumes = ramanspy.datasets.volumetric_cells(cell_type='THP-1', folder=dir_) # We will use the first volume cell_volume = volumes[0] # selecting a random spectrum for visualisation purposes random_spectrum = cell_volume[25, 25, 5] random_spectrum.plot(title='Original Raman spectra') ``` -------------------------------- ### Cropper.__init__() Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/generated/prepprocessing/misc/ramanspy.preprocessing.misc.Cropper.html Initializes the Cropper object. This is the constructor for the Cropper class, used to set up the cropping parameters. ```APIDOC ## Cropper.__init__() ### Description Initializes the Cropper object. This is the constructor for the Cropper class, used to set up the cropping parameters. ### Method __init__ ### Parameters This method does not explicitly list parameters in the provided documentation. ``` -------------------------------- ### Import necessary libraries for AI-based denoising Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/auto_examples/plot_ii_dl_denoising.html Imports required libraries for data manipulation, deep learning model loading, and plotting. Ensure these libraries are installed before running the code. ```python import ramanspy import matplotlib.pyplot as plt import numpy as np from ramanspy.preprocessing.denoise import DL_denoise from ramanspy.datasets import load_dataset from ramanspy.utils.plot import plot_spectra ``` -------------------------------- ### Initialize SpectralContainer with Different Dimensions Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/auto_tutorials/i-classes/plot_i_generic_container.html Demonstrates initializing SpectralContainer with various data shapes, including single spectra, images, volumes, and hypervolumes. Ensure the last dimension always corresponds to the spectral axis. ```python spectral_data = np.random.rand(1500) raman_spectrum = ramanspy.SpectralContainer(spectral_data, spectral_axis) ``` ```python spectral_data = np.random.rand(20, 20, 1500) raman_image = ramanspy.SpectralContainer(spectral_data, spectral_axis) ``` ```python spectral_data = np.random.rand(20, 20, 20, 1500) raman_volume = ramanspy.SpectralContainer(spectral_data, spectral_axis) ``` ```python spectral_data = np.random.rand(20, 20, 20, 20, 1500) raman_hypervolume = ramanspy.SpectralContainer(spectral_data, spectral_axis) ``` -------------------------------- ### Applying Preprocessing to Raman Data Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/_modules/ramanspy/preprocessing/Step.html Demonstrates how to apply an initialized preprocessing method to single Raman objects, lists of objects, or a mix of objects and spectra/images. ```python preprocessed_data = preprocessing_method.apply(raman_object) preprocessed_data = preprocessing_method.apply([raman_object, raman_spectrum, raman_image]) preprocessed_data = preprocessing_method.apply([raman_object, raman_spectrum], raman_object, [raman_spectrum, raman_image]) ``` -------------------------------- ### Denoising Results on Example Spectrum (Transfer Dataset) Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/_downloads/d82e73c01b6c50095b1b356c6c47e8a4/plot_ii_dl_denoising.ipynb Applies a neural network denoiser and a Savitzky-Golay filter to a noisy spectrum from the THP-1 dataset and plots the results alongside the authentic data. ```python np.random.seed(SEED) selected_index = np.random.randint(0, thp_slice.shape[0]) selected_target = thp_slice[selected_index] selected_input = add_normal_noise(selected_target) nn_results = get_results(selected_input, selected_target, nn_denoiser)[0] baseline_results = get_results(selected_input, selected_target, baseliners['SG (3, 9)'])[0] results = minmax.apply([selected_input, baseline_results, selected_target, nn_results]) labels = ['Input (data with noise)', 'Savitzky-Golay (3, 9)', 'Target (authentic data)', 'Neural network'] plt.figure(figsize=(10, 4), tight_layout=True) ax = ramanspy.plot.spectra(results, plot_type='single', ylabel='Normalised intensity', title='Transfer dataset', color=colors) ax.legend(labels) plt.show() ``` -------------------------------- ### Prepare Data Samples and Labels Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/_downloads/ff8c4da9032d200d102e1da05a80bd8e/plot_v_peak_dist.ipynb Prepares the loaded data into samples and corresponding labels for plotting. Samples are grouped into chunks, and labels are generated based on the species. ```python bacteria_lists = [[X_train[i:i+2000, :]] for i in range(0, X_train.shape[0], 2000)] bacteria_sample = bacteria_lists[:5] bacteria_sample_labels = [f"Species {int(y_train[i*2000])}" for i in range(0, 5)] ``` -------------------------------- ### Initialize Spectral Cropper Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/_downloads/52073867785117c31fe34b88eba3f264/plot_i_predefined_methods.ipynb Initializes the spectral cropper preprocessing step from ramanspy.preprocessing.misc. Specify the desired region for cropping. Check the method's documentation for available parameters. ```python cropper = ramanspy.preprocessing.misc.Cropper(region=(300, None)) ``` -------------------------------- ### Poly Baseline Correction Initialization with Regions Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/_modules/ramanspy/preprocessing/baseline.html Initializes the polynomial baseline correction method. It supports specifying regions for selective masking, where each region is a tuple of start and end points (inclusive). ```python def __init__(self, *, poly_order=2, regions: List[Tuple[Number or None, Number or None]] = None): if regions is not None: for region in regions: if len(region) != 2: raise ValueError("Region must be a list of tuples of two elements") super().__init__(pybaselines.polynomial.poly, poly_order=poly_order, weights=regions) ``` -------------------------------- ### MinMax.__init__ Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/generated/prepprocessing/norm/ramanspy.preprocessing.normalise.MinMax.html Initializes the MinMax normalisation object. This method sets up the parameters for the MinMax scaling operation. ```APIDOC ## MinMax.__init__() ### Description Initializes the MinMax normalisation object. ### Method __init__ ### Parameters This method does not take any explicit parameters beyond `self`. ``` -------------------------------- ### Setup Plotting Parameters Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/_downloads/d82e73c01b6c50095b1b356c6c47e8a4/plot_ii_dl_denoising.ipynb Configures Matplotlib's runtime parameters for consistent plot aesthetics, including font sizes for various plot elements and defining metrics and color schemes for visualizations. ```python SEED = 19 matplotlib.rc_file_defaults() plt.rc('font', size=16) # controls default text sizes plt.rc('axes', titlesize=24) # fontsize of the axes title plt.rc('xtick', labelsize=16) # fontsize of the tick labels plt.rc('ytick', labelsize=16) # fontsize of the tick labels plt.rc('legend', fontsize=16) # legend fontsize plt.rc('figure', titlesize=24) # fontsize of the figure title METRICS = ['MSE', 'SAD', 'SID'] colors = list(plt.cm.get_cmap()(np.linspace(0, 1, 4))) ``` -------------------------------- ### Initialize SpectralVolume from Data Array Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/auto_tutorials/i-classes/plot_iv_volume_container.html Create a SpectralVolume instance by providing a 4D intensity data array and its corresponding Raman wavenumber axis. This is suitable for direct data input. ```python import numpy as np import ramanspy spectral_data = np.random.rand(50, 50, 10, 1500) spectral_axis = np.linspace(100, 3600, 1500) raman_volume = ramanspy.SpectralVolume(spectral_data, spectral_axis) ``` -------------------------------- ### Denoising Results on Example Spectrum Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/auto_examples/plot_ii_dl_denoising.html Applies noise to a selected spectrum, denoises it using a neural network and Savitzky-Golay filter, and visualizes the results. Requires pre-defined `SEED`, `get_results`, `nn_denoiser`, `baseliners`, `minmax`, `colors`. ```python np.random.seed(SEED) selected_index = np.random.randint(0, thp_slice.shape[0]) selected_target = thp_slice[selected_index] selected_input = add_normal_noise(selected_target) nn_results = get_results(selected_input, selected_target, nn_denoiser)[0] baseline_results = get_results(selected_input, selected_target, baseliners['SG (3, 9)']) results = minmax.apply([selected_input, baseline_results, selected_target, nn_results]) labels = ['Input (data with noise)', 'Savitzky-Golay (3, 9)', 'Target (authentic data)', 'Neural network'] plt.figure(figsize=(10, 4), tight_layout=True) ax = ramanspy.plot.spectra(results, plot_type='single', ylabel='Normalised intensity', title='Transfer dataset', color=colors) ax.legend(labels) plt.show() ``` -------------------------------- ### Import RamanSPy Library Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/_downloads/1b3a8cd44cecaae6ba900a04bff6b2e7/iv_other.ipynb Import the RamanSPy library to begin using its functionalities. ```python import ramanspy ``` -------------------------------- ### Denoising Example Spectrum Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/_sources/auto_examples/plot_ii_dl_denoising.rst.txt Applies and visualizes denoising on a single spectrum from the MDA-MB-231 dataset. Compares the original low SNR input, the target high SNR spectrum, and the outputs from a Savitzky-Golay filter and a neural network denoiser. ```default np.random.seed(SEED) selected_index = np.random.randint(0, MDA_MB_231_X_test.shape[0]) selected_input, selected_target = MDA_MB_231_X_test[selected_index], MDA_MB_231_Y_test[selected_index] nn_results = get_results(selected_input, selected_target, nn_denoiser)[0] baseline_results = get_results(selected_input, selected_target, baseliners['SG (3, 9)']) results = minmax.apply([selected_input, baseline_results, selected_target, nn_results]) labels = ['Low SNR Input', 'Savitzky-Golay (3, 9)', 'High SNR Target', 'Neural network'] plt.figure(figsize=(10, 4), tight_layout=True) ax = ramanspy.plot.spectra(results, plot_type='single', ylabel='Normalised intensity', title='Original dataset', color=colors) ax.legend(labels) plt.show() ``` -------------------------------- ### Denoising Example Spectrum Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/_downloads/d82e73c01b6c50095b1b356c6c47e8a4/plot_ii_dl_denoising.ipynb Visualizes denoising results on a single spectrum from the MDA-MB-231 dataset. It compares the original low SNR input, Savitzky-Golay filtered output, and the neural network denoiser output against the high SNR target. ```python np.random.seed(SEED) selected_index = np.random.randint(0, MDA_MB_231_X_test.shape[0]) selected_input, selected_target = MDA_MB_231_X_test[selected_index], MDA_MB_231_Y_test[selected_index] nn_results = get_results(selected_input, selected_target, nn_denoiser)[0] baseline_results = get_results(selected_input, selected_target, baseliners['SG (3, 9)'])[0] results = minmax.apply([selected_input, baseline_results, selected_target, nn_results]) labels = ['Low SNR Input', 'Savitzky-Golay (3, 9)', 'High SNR Target', 'Neural network'] plt.figure(figsize=(10, 4), tight_layout=True) ax = ramanspy.plot.spectra(results, plot_type='single', ylabel='Normalised intensity', title='Original dataset', color=colors) ax.legend(labels) plt.show() ``` -------------------------------- ### Load and Use Pretrained ResUNet Model Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/source/auto_examples/plot_ii_dl_denoising.rst Loads a pre-trained ResUNet model and wraps it into a preprocessing step for denoising spectral data. Ensure the model file 'ResUNet.pt' is available. ```python net = ResUNet(3, False).float() net.load_state_dict(torch.load(r"ResUNet.pt", map_location=torch.device('cpu'))) ``` ```python def nn_preprocesing(spectral_data, wavenumber_axis): flat_spectral_data = spectral_data.reshape(-1, spectral_data.shape[-1]) output = net(torch.Tensor(flat_spectral_data).unsqueeze(1)).cpu().detach().numpy() output = np.squeeze(output) output = output.reshape(spectral_data.shape) return output, wavenumber_axis nn_denoiser = ramanspy.preprocessing.PreprocessingStep(nn_preprocesing) ``` -------------------------------- ### Apply Preprocessing Method to Raman Objects Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/preprocessing.html Demonstrates how to apply an initialized preprocessing method to various Raman spectroscopic objects. The method can be applied to a single object, a list of objects, or a mix of objects and lists. ```python # once a preprocessing method is initialised, it can be applied to different Raman data preprocessed_data = preprocessing_method.apply(raman_object) preprocessed_data = preprocessing_method.apply([raman_object, raman_spectrum, raman_image]) preprocessed_data = preprocessing_method.apply([raman_object, raman_spectrum], raman_object, [raman_spectrum, raman_image]) ``` -------------------------------- ### Denoise and Plot Example Spectrum Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/auto_examples/plot_ii_dl_denoising.html Selects a random spectrum from the test set, denoises it using both a neural network and a Savitzky-Golay filter, and plots the original low-SNR input, denoised outputs, and the high-SNR target spectrum for visual comparison. ```python np.random.seed(SEED) selected_index = np.random.randint(0, MDA_MB_231_X_test.shape[0]) selected_input, selected_target = MDA_MB_231_X_test[selected_index], MDA_MB_231_Y_test[selected_index] nn_results = get_results(selected_input, selected_target, nn_denoiser)[0] baseline_results = get_results(selected_input, selected_target, baseliners['SG (3, 9)'] )[0] results = minmax.apply([selected_input, baseline_results, selected_target, nn_results]) labels = ['Low SNR Input', 'Savitzky-Golay (3, 9)', 'High SNR Target', 'Neural network'] plt.figure(figsize=(10, 4), tight_layout=True) ax = ramanspy.plot.spectra(results, plot_type='single', ylabel='Normalised intensity', title='Original dataset', color=colors) ax.legend(labels) plt.show() ``` -------------------------------- ### Initialize and apply KMeans clustering Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/_sources/auto_tutorials/vi-analysis/plot_ii_kmeans.rst.txt Initializes the KMeans clustering algorithm with a specified number of clusters (4) and applies it to the preprocessed spectral image to obtain cluster assignments and cluster centers. ```python kmeans = ramanspy.analysis.cluster.KMeans(n_clusters=4) clusters, cluster_centres = kmeans.apply(preprocessed_cell_layer) ``` -------------------------------- ### Whittaker.__init__() Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/generated/prepprocessing/denoising/ramanspy.preprocessing.denoise.Whittaker.html Initializes the Whittaker class. This is the constructor for the Whittaker denoiser. ```APIDOC ## Whittaker.__init__() ### Description Initializes the Whittaker class. This is the constructor for the Whittaker denoiser. ### Method __init__ ### Parameters This method does not explicitly list parameters in the provided documentation. ``` -------------------------------- ### Gaussian Class Initialization Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/_sources/generated/prepprocessing/denoising/ramanspy.preprocessing.denoise.Gaussian.rst.txt Initializes the Gaussian denoiser with a specified window size. ```APIDOC ## Gaussian.__init__ ### Description Initializes the Gaussian denoiser. ### Parameters * **window_size** (int) - The size of the Gaussian smoothing window. Defaults to 5. ``` -------------------------------- ### MaxIntensity.__init__() Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/generated/prepprocessing/norm/ramanspy.preprocessing.normalise.MaxIntensity.html Initializes the MaxIntensity normalizer. This class does not take any arguments during initialization. ```APIDOC ## MaxIntensity.__init__() ### Description Initializes the MaxIntensity normalizer. This class does not take any arguments during initialization. ### Method __init__ ### Parameters This method does not take any parameters. ### Returns None ``` -------------------------------- ### Showcase Preprocessing Steps Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/_downloads/52073867785117c31fe34b88eba3f264/plot_i_predefined_methods.ipynb Plots multiple spectra (original, cropped, denoised, baseline corrected, and normalized) in a stacked format to visually compare the effects of the preprocessing steps. Labels and a title are provided for clarity. ```python ramanspy.plot.spectra( [random_spectrum, cropped_random_spectrum, denoised_random_spectrum, baselined_random_spectrum, normalised_random_spectrum], plot_type='stacked', label=['Original', 'Cropped', 'Smoothened', 'Baseline corrected', 'Normalised'], title='Preprocessing showcase') ``` -------------------------------- ### Initialize Vector Normalization Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/_downloads/52073867785117c31fe34b88eba3f264/plot_i_predefined_methods.ipynb Initializes the Vector normalization method from ramanspy.preprocessing.normalise. This scales spectral intensities to have a unit vector norm. ```python vector_normaliser = ramanspy.preprocessing.normalise.Vector() normalised_random_spectrum = vector_normaliser.apply(baselined_random_spectrum) normalised_random_spectrum.plot(title='Normalised Raman spectra', ylabel="Normalised intensity (a.u.)") ``` -------------------------------- ### Create Dummy Spectra Data Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/_downloads/52676b36d1bcfb10b6d5233b0e3f4740/plot_ii_metrics.ipynb Generate sample spectral data, including sine and cosine waves, to use for metric calculations. This involves creating a spectral axis and corresponding intensity data. ```python spectral_axis = np.linspace(100, 3600, 1500) sine_data = np.sin(spectral_axis/120) cosine_data = np.cos(spectral_axis/120) sine_spectrum = ramanspy.Spectrum(sine_data, spectral_axis) cosine_spectrum = ramanspy.Spectrum(cosine_data, spectral_axis) ``` -------------------------------- ### Create SpectralContainer by Stacking Spectra Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/_downloads/cd1b15cae80fcdf13744ad6dc2110d7b/plot_i_generic_container.ipynb Initializes a 2D SpectralContainer by stacking a list of individual Spectrum objects. ```python raman_spectra = [ramanspy.Spectrum(np.random.rand(1500), spectral_axis) for _ in range(5)] raman_spectra_list = ramanspy.SpectralContainer.from_stack(raman_spectra) raman_spectra_list.shape ``` -------------------------------- ### Saving and Loading SpectralContainer Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/_sources/auto_tutorials/i-classes/plot_i_generic_container.rst.txt Demonstrates how to save a SpectralContainer to a pickle file and load it back. This is useful for persisting and sharing data. ```python # save raman_image.save("my_raman_image") # load raman_image_ = ramanspy.SpectralContainer.load("my_raman_image") raman_image_.shape ``` ```none (20, 20) ``` -------------------------------- ### Load Pretrained ResUNet Model Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/_sources/auto_examples/plot_ii_dl_denoising.rst.txt Loads a pretrained ResUNet model for denoising. Ensure the 'ResUNet.pt' file is available in the specified path. The model is loaded onto the CPU. ```python net = ResUNet(3, False).float() net.load_state_dict(torch.load(r"ResUNet.pt", map_location=torch.device('cpu'))) ``` -------------------------------- ### Stack Spectral Objects Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/_modules/ramanspy/core.html Illustrates how to combine a list of individual Spectrum objects into a single SpectralContainer. Requires that all spectral objects have matching spectral axes. ```python stacked_raman_object = rp.SpectralContainer.from_stack(stack=[spectrum1, spectrum2, spectrum3]) ``` -------------------------------- ### Load and Apply Pretrained ResUNet Model Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/auto_examples/plot_ii_dl_denoising.html Loads a pretrained ResUNet model for denoising Raman spectra and defines a preprocessing function to apply it. The model is loaded from a specified path and configured to run on the CPU. ```python net = ResUNet(3, False).float() net.load_state_dict(torch.load(r"ResUNet.pt", map_location=torch.device('cpu'))) def nn_preprocesing(spectral_data, wavenumber_axis): flat_spectral_data = spectral_data.reshape(-1, spectral_data.shape[-1]) output = net(torch.Tensor(flat_spectral_data).unsqueeze(1)).cpu().detach().numpy() output = np.squeeze(output) output = output.reshape(spectral_data.shape) return output, wavenumber_axis nn_denoiser = ramanspy.preprocessing.PreprocessingStep(nn_preprocesing) ``` -------------------------------- ### Initialize SpectralContainer with 2D Data Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/auto_tutorials/i-classes/plot_i_generic_container.html Create a SpectralContainer for multi-spectrum data by providing intensity data and a spectral axis. This is suitable for datasets with multiple spectra, such as those from Raman mapping experiments. ```python import numpy as np import ramanspy # an evenly spaced Raman wavenumber axis between 100 and 3000 cm^-1, consisting of 1500 elements. spectral_axis = np.linspace(100, 3600, 1500) # randomly generating intensity data array of shape (20, 1500) spectral_data = np.random.rand(20, 1500) # wrapping the data into a SpectralContainer instance raman_object = ramanspy.SpectralContainer(spectral_data, spectral_axis) ``` -------------------------------- ### Load SpectralContainer from Pickle File Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/_downloads/cd1b15cae80fcdf13744ad6dc2110d7b/plot_i_generic_container.ipynb Loads a SpectralContainer instance from a previously saved pickle file. ```python # load raman_image_ = ramanspy.SpectralContainer.load("my_raman_image") raman_image_.shape ``` -------------------------------- ### FastICA Initialization Source: https://github.com/barahona-research-group/ramanspy/blob/main/docs/build/html/_modules/ramanspy/analysis/decompose.html Initializes the FastICA decomposition step. It wraps the scikit-learn FastICA implementation. ```python class FastICA(AnalysisStep): """ Fast Independent Component Analysis (FastICA). Parameters ---------- n_components : int The number of components. **kwargs : Check original `implementation `_ for additional parameters. .. note :: Implementation and documentation based on`scikit-learn `_. """ def __init__(self, *, n_components, **kwargs): super().__init__(scikit_learn_wrapper(decomp.FastICA), n_components, **kwargs) ```