-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloadCube.py
More file actions
77 lines (65 loc) · 2.87 KB
/
loadCube.py
File metadata and controls
77 lines (65 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""
HSI Classifier
Copyright (C) 2021 Josef Brandt, University of Gothenburg <josef.brandt@gu.se>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program, see COPYING.
If not, see <https://www.gnu.org/licenses/>.
"""
import os
import sys
from typing import Tuple
import numpy as np
from readConfig import snapScanFolder
snapscanBinaries = os.path.join(snapScanFolder, "bin")
snapscanAPI = os.path.join(snapScanFolder, "python")
os.environ['PATH'] = snapscanBinaries + os.pathsep + os.environ['PATH']
sys.path.append(snapscanAPI)
try:
import hsi_snapscan as HSI
snapscanEnabled: bool = True
except ImportError:
print("Failed to load HSI Snapscan API")
snapscanEnabled: bool = False
except OSError:
print("Failed to load HSI Snapscan API")
snapscanEnabled: bool = False
def loadCube(fname: str, errorPixelThreshold: float = 1000) -> Tuple[np.ndarray, np.ndarray]:
"""
Loads a numpy cube and sets "defect" pixels to zero. They usually contain very large values (1e34) and thereby hamper
further evaluation.
:param fname: Path to .npy file
:param errorPixelThreshold: The threshold used to detet erroneous pixels.
:return: Tuple[(NxM) array of N spectra with M wavelenghts, array of M wavelengths]
"""
if not os.path.exists(fname) and fname.endswith(".npy"):
fname = fname.replace("npy", "hdr")
assert os.path.exists(fname), f"File {fname} not found for loading the cube."
if fname.endswith(".npy"):
cube: np.ndarray = np.load(fname)
wavelengths: np.ndarray = np.arange(cube.shape[0])
elif fname.endswith(".hdr"):
assert snapscanEnabled, f'Cannot load {fname}, snapscan API could not be loaded. Check the "loadCube.npy" for details.'
cube, wavelengths = loadHDRCube(fname)
else:
raise TypeError("The specified file is not supported (only .npy or .hdr)")
cube[cube > errorPixelThreshold] = 0
return cube, wavelengths
def loadHDRCube(fname: str) -> Tuple[np.ndarray, np.ndarray]:
"""
Loads a .hdr file, converts to numpy array and returns it together with the wavelengths.
:param fname: Absolute path to .hdr file
:return: Tuple[(NxM) array of N spectra with M wavelenghts, array of M wavelengths]
"""
img = HSI.LoadCube(fname)
format = img.format.as_dict()
bands_nm: np.ndarray = format["bands_nm"]
cube:np.ndarray = HSI.CubeAsArray(img)
return cube, bands_nm