# 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#fit-pca-from-rois # region: setup import os from pathlib import Path import joblib import numpy as np 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 from sklearn.decomposition import PCA 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") ROI_PCA_MODEL_PATH = Path(os.environ.get("HSI_EXAMPLE_ROI_PCA_MODEL", "roi_pca.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 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)) # end region # region: example reflectance = open_reflectance_cube() ann_file = load_annotations() rng = np.random.default_rng(42) max_pixels_per_roi = 1000 roi_pixels_list = [] for annot in ann_file.annotations: selected = reflectance.select_mask_from_descriptor(annot.descriptor) spectra = selected.to_numpy_with_interleave(hs.bip)[:, 0, :] if spectra.shape[0] > max_pixels_per_roi: selected = rng.choice(spectra.shape[0], size=max_pixels_per_roi, replace=False) spectra = spectra[selected] roi_pixels_list.append(spectra) roi_pixels = np.concatenate(roi_pixels_list) roi_pca = PCA(n_components=3, random_state=42) roi_pca.fit(roi_pixels) joblib.dump(roi_pca, ROI_PCA_MODEL_PATH) print(f"ROI PCA explained variance: {roi_pca.explained_variance_ratio_}") print(f"Saved ROI PCA model to {ROI_PCA_MODEL_PATH}") # end region