# This file was extracted from the HV SDK Docusaurus examples. # It is intended as a downloadable, runnable companion to the documentation. # Set HSI_EXAMPLE_BASE_DIR and related env vars to use your own data. # Source page: /hsi/hv_sdk/examples/pca#pca-scatter-plot-with-rois # region: setup import os from pathlib import Path import joblib import matplotlib.pyplot as plt import qtec_hv_sdk as hs import qtec_hv_sdk.annotations from qtec_hv_sdk.preprocessing import make_reference from qtec_hv_sdk.preprocessing import reflectance_calibration BASE_DIR = Path(os.environ.get("HSI_EXAMPLE_BASE_DIR", "/path/to/HSI_data/nuts")) if not BASE_DIR.exists(): raise SystemExit( "Run: 'export HSI_EXAMPLE_BASE_DIR=/path/to/HSI_data/' to setup the " "folder containing the example datacubes." ) TRAIN_CUBE = os.environ.get("HSI_EXAMPLE_TRAIN_CUBE", "mix1.pam") DARK_REF = os.environ.get("HSI_EXAMPLE_DARK_REF", "dark_ref.pam") WHITE_REF = os.environ.get("HSI_EXAMPLE_WHITE_REF", "white_ref.pam") PCA_MODEL_PATH = Path(os.environ.get("HSI_EXAMPLE_PCA_MODEL", "pca_model.joblib")) def make_references(): dark = hs.open(str(BASE_DIR / DARK_REF)) white = hs.open(str(BASE_DIR / WHITE_REF)) return make_reference(dark), make_reference(white) def open_reflectance_cube(cube_name=TRAIN_CUBE): dark_ref, white_ref = make_references() img = hs.open(str(BASE_DIR / cube_name)) return reflectance_calibration(img, white_ref, dark_ref, clip=True) def annotation_value(value): return value[0] if value is not None else None def load_annotations(): annotations_path = Path(os.environ.get("HSI_EXAMPLE_ANNOTATIONS", "mix1_roi.json")) if not annotations_path.is_absolute(): annotations_path = BASE_DIR / annotations_path if not annotations_path.exists(): raise SystemExit("Set HSI_EXAMPLE_ANNOTATIONS to the annotations JSON file.") return hs.annotations.open(str(annotations_path)) def load_pca_model(): if not PCA_MODEL_PATH.exists(): raise SystemExit( f"PCA model not found at {PCA_MODEL_PATH}. " "Run 01_principal_component_analysis.py first, or set HSI_EXAMPLE_PCA_MODEL." ) return joblib.load(PCA_MODEL_PATH) # end region # region: example reflectance = open_reflectance_cube() pca = load_pca_model() ann_file = load_annotations() class_colors = {annotation_value(annot.properties["type"]): annot.color for annot in ann_file.annotations} component_x = 0 component_y = 1 plt.figure(figsize=(8, 6)) for annot in ann_file.annotations: class_name = annotation_value(annot.properties["type"]) selected = reflectance.select_mask_from_descriptor(annot.descriptor) spectra = selected.to_numpy_with_interleave(hs.bip)[:, 0, :] scores = pca.transform(spectra) plt.scatter( scores[:, component_x], scores[:, component_y], s=8, alpha=0.25, color=class_colors[class_name], edgecolors="none", label=class_name, ) handles, labels = plt.gca().get_legend_handles_labels() unique = dict(zip(labels, handles)) plt.legend(unique.values(), unique.keys()) plt.xlabel(f"PC{component_x + 1}") plt.ylabel(f"PC{component_y + 1}") plt.title("ROI pixels in PCA space") plt.grid(True, alpha=0.3) plt.show() # end region