{ "cells": [ { "cell_type": "markdown", "id": "48a40c38-c1e8-458d-aa29-f14429af2964", "metadata": {}, "source": [ "# Notebook showing all the spectral extraction methods implemented in pyFRESCO" ] }, { "cell_type": "code", "execution_count": 2, "id": "64fa6b08-95d6-4089-914a-62a29f99dfde", "metadata": {}, "outputs": [], "source": [ "import pyfresco" ] }, { "cell_type": "markdown", "id": "4b61083f-0ce3-4983-b702-8c228640b33f", "metadata": {}, "source": [ "## Data import\n", "\n", "In the following code cell we define the paths pointing to the img and header files of the proper hyperspectral datacube and of the summary parameters datacube.\n", "These two datacubes are the ones needed by pyFRESCO to work properly." ] }, { "cell_type": "code", "execution_count": 3, "id": "fade6b0e-6d27-41ad-a2a7-d3e547802a00", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['R770', 'RBR', 'BD530_2', 'SH600_2', 'SH770', 'BD640_2', 'BD860_2', 'BD920_2', 'RPEAK1', 'BDI1000VIS', 'R440', 'IRR1', 'BDI1000IR', 'OLINDEX3', 'R1330', 'BD1300', 'LCPINDEX2', 'HCPINDEX2', 'VAR', 'ISLOPE1', 'BD1400', 'BD1435', 'BD1500_2', 'ICER1_2', 'BD1750_2', 'BD1900_2', 'BD1900R2', 'BDI2000', 'BD2100_2', 'BD2165', 'BD2190', 'MIN2200', 'BD2210_2', 'D2200', 'BD2230', 'BD2250', 'MIN2250', 'BD2265', 'BD2290', 'D2300', 'BD2355', 'SINDEX2', 'ICER2_2', 'MIN2295_2480', 'MIN2345_2537', 'BD2500_2', 'BD3000', 'BD3100', 'BD3200', 'BD3400_2', 'CINDEX2', 'BD2600', 'IRR2', 'IRR3', 'R530', 'R600', 'R1080', 'R1506', 'R2529', 'R3920']\n" ] } ], "source": [ "path_if_mtrdr = \"FRT00009b5a/frt00009b5a_07_if165j_mtr3.img\" # Path to the proper hyperspectral datacube\n", "head_if_mtrdr = \"FRT00009b5a/frt00009b5a_07_if165j_mtr3.hdr\" # Path to the header of the proper hyperspectral datacube\n", "\n", "path_sr_mtrdr = \"FRT00009b5a/frt00009b5a_07_sr165j_mtr3.img\" # Path to the summary parameters datacube\n", "head_sr_mtrdr = \"FRT00009b5a/frt00009b5a_07_sr165j_mtr3.hdr\" # Path to the header of the summary parameters datacube\n", "\n", "img , img_sr , wavelength , sr_names = pyfresco.open_raw(path_if_mtrdr , head_if_mtrdr , path_sr_mtrdr , head_sr_mtrdr) # Open the two datacubes\n", "\n", "Nbands = len(wavelength)\n", "print(sr_names)" ] }, { "cell_type": "markdown", "id": "97ab8592-fc69-4578-ab3a-a9293f46f619", "metadata": {}, "source": [ "## Uploading the RGB map\n", "\n", "In the following cells we upload the RGB map used as visual guide for the selections.\n", "The map is the one saved in the first tutorial notebook." ] }, { "cell_type": "code", "execution_count": 4, "id": "0ca73990-0e80-49a4-9d09-3015eab0f5a0", "metadata": {}, "outputs": [], "source": [ "SE = pyfresco.SpectraExtract(img , Nbands , wavelength , 750 , 2600) # Definition of the object for spectral extraction\n", "\n", "RGB = SE.upload_map('PHY' , folder = 'FRT00009b5a') # Upload an RGB map for the ROI selection" ] }, { "cell_type": "markdown", "id": "16996464-7743-4f28-9415-80d0c9468704", "metadata": {}, "source": [ "## Point extraction\n", "\n", "In this extraction method we manually select single pixels on the RGB map.\n", "The extracted spectra correspond to the clicked points and can be plotted one by one before computing the final spectrum." ] }, { "cell_type": "code", "execution_count": 5, "id": "35b8b9b7-b371-428e-b169-d7e62a651eeb", "metadata": {}, "outputs": [], "source": [ "point_spectra = SE.point_spectra(N = 5) # Select 5 points on the RGB map\n", "\n", "point_spectrum , point_error = SE.final_spectra(mean = True , c = 'blue') # Mean and standard deviation spectrum" ] }, { "cell_type": "markdown", "id": "image-cell-point-output", "metadata": {}, "source": [ "The output of the above cell should look like this (the selected pixels are the ones with the red cross over it, remember that the last selected pixel will not have the cross!): \n", "![Point selection](notebook2_images/point_selection.png)\n", "![Point mean spectra](notebook2_images/point_mean.png)" ] }, { "cell_type": "markdown", "id": "3db60ab9-afd8-491e-98d8-36cacaa5817b", "metadata": {}, "source": [ "## Square extraction\n", "\n", "In this extraction method we select the center of a square ROI.\n", "The value of `N` is the side length of the square, and it must be an odd number." ] }, { "cell_type": "code", "execution_count": 6, "id": "2a008838-9afc-45f4-affa-59f3198e49ae", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Square center = 301 247\n" ] } ], "source": [ "square_spectra , x_square , y_square = SE.square_spectra(N = 11 , show = True) # Select the center of an 11 x 11 pixels square\n", "\n", "square_spectrum , square_error = SE.final_spectra(mean = True , c = 'green') # Mean and standard deviation spectrum\n", "\n", "print('Square center = ' , x_square , y_square)" ] }, { "cell_type": "markdown", "id": "image-cell-square-output", "metadata": {}, "source": [ "The output of the above cell should look like this (remember that the square is selected in another image by clicking the right mouse button!): \n", "![square selection](notebook2_images/square_selection.png)\n", "![square mean spectra](notebook2_images/square_mean.png)\n" ] }, { "cell_type": "markdown", "id": "cb0bea13-743f-4298-8362-bdacf61e7da4", "metadata": {}, "source": [ "## Polygon extraction\n", "\n", "In this extraction method we draw a polygonal ROI on the RGB map.\n", "The spectra are extracted from all the pixels inside the drawn polygon." ] }, { "cell_type": "code", "execution_count": 7, "id": "b87cf6f7-aed0-4e08-ae4e-26220a71eaae", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Number of points inside the drewn polygon: 796\n", "Number of polygon pixels = 796\n" ] } ], "source": [ "polygon_spectra , L , mask = SE.polygon_spectra(save_pixel = True , # Select polygonal ROI on the RGB map\n", " folder = 'FRT00009b5a' ,\n", " name = 'polygon_ROI_coordinates')\n", "\n", "polygon_spectrum , polygon_error = SE.final_spectra(mean = True , c = 'red') # Mean and standard deviation spectrum\n", "\n", "print('Number of polygon pixels = ' , L)" ] }, { "cell_type": "markdown", "id": "image-cell-polygon-output", "metadata": {}, "source": [ "The output of the above cell should look like this: \n", "![Polygon selection](notebook2_images/polygon_selection.png)\n", "![polygon mean spectra](notebook2_images/polygon_mean.png)\n" ] }, { "cell_type": "markdown", "id": "8ee27b08-befe-4808-b930-304985664aec", "metadata": {}, "source": [ "## Median spectrum and MAD\n", "\n", "The same final extraction step can be repeated using the median spectrum and the Median Absolute Deviation instead of the mean spectrum and standard deviation.\n", "This is useful when the selected ROI contains outliers." ] }, { "cell_type": "code", "execution_count": 8, "id": "4f81c7ab-8d58-4807-ad82-15dd7228e820", "metadata": {}, "outputs": [], "source": [ "polygon_median , polygon_mad = SE.final_spectra(mean = False , c = 'black') # Median and MAD spectrum" ] }, { "cell_type": "markdown", "id": "image-cell-point-output-median", "metadata": {}, "source": [ "The output of the above cell should look like this: \n", "![median](notebook2_images/polygon_median.png)\n" ] }, { "cell_type": "markdown", "id": "e3911e36-3ffd-4760-a39e-1527a773517f", "metadata": {}, "source": [ "## Saving the extracted spectra\n", "\n", "The selected spectra can be saved and then reused for the other modules." ] }, { "cell_type": "code", "execution_count": 9, "id": "a6cf0b89-beda-4fde-a9d4-c0b6484edb1b", "metadata": {}, "outputs": [], "source": [ "SE.save_spectra(name = 'polygon_ROI_spectra' ,\n", " folder = 'FRT00009b5a/' ,\n", " method = 'polygon') # Save the extracted spectra and the wavelength range\n", "\n", "SE.save_spectrum(name = 'polygon_ROI_mean_spectra' ,\n", " folder = 'FRT00009b5a/' ,\n", " method = 'polygon') # Save the extracted spectra and the wavelength range" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.20" }, "nbsphinx": { "execute": "never" } }, "nbformat": 4, "nbformat_minor": 5 }