<jupyter_start><jupyter_text>Supervised Spectral Unmixing with Landsat 8This code will walk through the workflow process of arthur-e's github repo example: https://github.com/arthur-e/unmixing/blob/master/docs/Example_Spatially_Adaptive_Spectral_Mixture_Analysis_SASMA.ipynbNOTE: This workflow requires cloning the above repo, activating your environment in the repo and `pip install -e`.  This is a very large file, so be prepared! The remainder of this notebook will also require a stacked Landsat 8 scene (see Landsat8-cropped-and-stacked.ipynb) and pre-determined spectral signatures.  In this example, I am using spectral signatures derived from an unsupervised classification using kmeans (see Kmean-Unsupervised-Classification.ipynb).<jupyter_code># Import Packages
import os
from glob import glob

import geopandas as gpd
from shapely.geometry import box
import numpy as np
from matplotlib import pyplot as plt, cm
from matplotlib.colors import ListedColormap
import rasterio as rio
from rasterio.mask import mask
from rasterio.plot import plotting_extent

import earthpy as et
import earthpy.spatial as es
import earthpy.plot as ep

from unmixing.utils import as_array
from unmixing.utils import binary_mask
from unmixing.utils import subarray
from unmixing.lsma import FCLSAbundanceMapper
from unmixing.sasma import concat_endmember_arrays
from unmixing.transform import mnf_rotation
from unmixing.visualize import FeatureSpace

%matplotlib inline

# #######################NOT WORKING: INSISTING IT IS A .PY FILES###############
# # Make ONAQ site info retrievable
# %run ./kessb-NEON-scripts/data_grabber.ipynb
# Set working directory and other key paths
os.chdir(os.path.join(et.io.HOME, 'earth-analytics'))

output_dir = os.path.join("data", "Landsat", "outputs")
if os.path.isdir(output_dir) == False:
    os.mkdir(output_dir)
    
stacked_image_path = os.path.join(output_dir, 'stacked_aoi.tif')
unclassified_image_path = os.path.join(output_dir, 'classified_aoi.tif')<jupyter_output><empty_output><jupyter_text>Preparing unsupervised classification raster data<jupyter_code># Import stacked aoi and plot NIR band
stacked_arr, gt, wkt = as_array(stacked_image_path)

preview_nir = stacked_arr[3,...]
preview_nir[preview_nir == -9999] = 0 # Remap any NoData values to zero

# Reconfigure the raster array so that the band axis is the third axis
plt.figure(figsize = (10, 10))
plt.imshow(preview_nir, cmap = cm.YlGnBu_r)
plt.show()
# Transform image to Minimum Noise Fraction (MNF)
#########PRIOR TO EXTRACTING ENDMEMBERS?  NEED TO DO PRE-CLASSIFICATION??????#########
mnf = mnf_rotation(lt5_detroit, nodata = -9999)

# The MNF image is returned in HSI form (the transpose of our original raster array)
plt.figure(figsize = (10, 10))
plt.imshow(mnf.T[0,...], cmap = cm.YlGnBu_r)
plt.show()
# Selecting endmembers from pre-defined spectral signatures
pifs, gt0, wkt0 = as_array(unclassified_image_path)

########################ONLY REFLECTING SINGLE CLASS#################################
# Create a color map for background (unclassified image used 7 classifiers)
color_code_map = ListedColormap(['lightgray', 
                                 'green', 
                                 'red', 
                                 'blue', 
                                 'yellow', 
                                 'orange', 
                                 'black'])
plt.figure(figsize = (5, 5))
plt.imshow(pifs[0,...], cmap = color_code_map)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11117462132037725, 'fraction_numerical': 0.008288082309231209, 'mean_word_length': 4.208333333333333, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '144130', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1161, 'n_tokens_neox': 1078, 'n_words': 331, 'path': 'notebooks/testing/Supervised-Spectral-Unmixing.ipynb', 'repo_name': 'sarahmjaffe/sagebrush-ecosystem-modeling-with-landsat8', 'size': '3499'}