diff --git a/src/SpectralUnmixing.jl b/src/SpectralUnmixing.jl index cef26b4..22d656b 100644 --- a/src/SpectralUnmixing.jl +++ b/src/SpectralUnmixing.jl @@ -34,6 +34,7 @@ include("Plotting.jl") # Endmember Library Functions export SpectralLibrary, load_data!, filter_by_class!, read_envi_wavelengths, interpolate_library_to_new_wavelengths!, remove_wavelength_region_inplace!, scale_library! export reduce_endmembers_nmf!, reduce_endmembers_kmeans!, reduce_endmembers_pca!, brightness_normalize!, split_library +export prepare_combinations, prepare_options, scale_data # Plotting Functions export plot_mean_endmembers, plot_endmembers, plot_endmembers_individually @@ -240,6 +241,36 @@ function unmix_pixel(library::SpectralLibrary, img_dat_input::Array{Float64}, un end +function prepare_combinations(library::SpectralLibrary, combination_type::String) + class_idx = [] + if combination_type == "class-even" + for uc in library.class_valid_keys + push!(class_idx, (1:size(library.classes)[1])[library.classes .== uc]) + end + end + return class_idx +end + +function prepare_options(library::SpectralLibrary, combination_type::String, num_endmembers::Vector{Int64}, class_idx) + + # Prepare combinations if relevant + options = [] + if combination_type == "class-even" + options = collect(Iterators.product(class_idx...))[:] + elseif combination_type == "all" + for num in num_endmembers + combo = [c for c in combinations(1:length(library.classes), num)] + push!(options,combo...) + end + else + error("Invalid combiation string") + end + + return options +end + + + function unmix_line(line::Int64, reflectance_file::String, mode::String, refl_nodata::Float64, refl_scale::Float64, normalization::String, library::SpectralLibrary, reflectance_uncertainty_file::String = "", n_mc::Int64 = 1, @@ -268,26 +299,10 @@ function unmix_line(line::Int64, reflectance_file::String, mode::String, refl_no scale_data(img_dat, library.wavelengths[library.good_bands], normalization) img_dat = img_dat ./ refl_scale - class_idx = [] - if combination_type == "class-even" - for uc in library.class_valid_keys - push!(class_idx, (1:size(library.classes)[1])[library.classes .== uc]) - end - end - + class_idx = prepare_combinations(library, combination_type) # Prepare combinations if relevant - options = [] if mode == "mesma" || mode == "mesma-best" - if combination_type == "class-even" - options = collect(Iterators.product(class_idx...))[:] - elseif combination_type == "all" - for num in num_endmembers - combo = [c for c in combinations(1:length(library.classes), num)] - push!(options,combo...) - end - else - error("Invalid combiation string") - end + options = prepare_options(library, combination_type, num_endmembers, class_idx) end start_time = time() @@ -369,12 +384,7 @@ function simulate_pixel(library::SpectralLibrary, max_components::Int64, combina output_mixture = zeros(size(library.spectra)[2]) output_mixture[:] .= NaN - class_idx = [] - if combination_type == "class-even" - for uc in library.class_valid_keys - push!(class_idx, (1:size(library.classes)[1])[library.classes .== uc]) - end - end + class_idx = prepare_combinations(library, combination_type) perm = get_sma_permutation(class_idx, [max_components], combination_type, size(library.spectra)[1]) G = library.spectra[perm,:]