Exemplo n.º 1
0
from __future__ import print_function
import os, os.path
import matplotlib.pyplot as plt
from SdalReader import SdalReader
from OverlapHandler import OverlapHandler

if __name__ == "__main__":
    print("Testing the OverlapHandler class")

    testdir = "/home/prabu/mycode/sdal/tests_data/test_OverlapHandler"

    signames = ["gr082014_000.sig", "gr082014_001.sig"]
    sigfiles = [os.path.join(testdir, "input", f) for f in signames]
    sigspec = [SdalReader().read_spectrum(f) for f in sigfiles]

    oh = OverlapHandler()
    ohdspec = [oh.process_overlap(s) for s in sigspec]

    #plot it to see if it looks correct
    fig1, axes1 = plt.subplots(nrows=2, ncols=1)
    for i in range(len(ohdspec)):
        axes1[i].set_xlim([300.0, 2600.0])
        axes1[i].set_ylim([0.0, 1.0])
        axes1[i].plot(ohdspec[i].wavelengths,
                      ohdspec[i].reflectances + 0.1,
                      color='g',
                      alpha=0.6,
                      label="After overlap handling")
        axes1[i].plot(sigspec[i].wavelengths,
                      sigspec[i].reflectances,
                      color='r',
def process_directory(params):
    
    #parameters for processing
    input_dir = params["input_directory"]
    ws = params["subset_wavelength_range"][0]
    we = params["subset_wavelength_range"][1]
    pattern = params["grouping_pattern"]
    patt_name = params['grouping_output_name']
    
    #read the spectrums    
    extension = 'sig'
    spec_files = [os.path.join(input_dir, f) for f in os.listdir(input_dir) 
                                             if get_extension(f) == extension]
#    for f in spec_files:
#        print(f)
    raw_specs = [SdalReader().read_spectrum(f) for f in spec_files]
    if len(raw_specs) == 0: 
        return    
#    print("Data directory: {}".format(input_dir))
#    print("  Read {} {} files".format(len(raw_specs), extension))
#    for s in raw_specs:
#        print(s.idstr)
    
        
    #uniquify wavelengths and interpolate to 1nm
    ohr = OverlapHandler(rstype = 'linear')
    rs_specs = []
    for s in raw_specs:
        if ohr.is_1nm(s):
            rs_specs.append(s)
        else:
            rs_specs.append(ohr.process_overlap(s))
    print("  Uniquified and monotoned spectrums")
    
#    for w in rs_specs[0].wavelengths:
#        print(w)
    
    #separate into green vegetation and non-green vegetation spectra
    gvd = GreenVegDetector()
    raw_white_specs, raw_target_specs = [], []
    rs_white_specs, rs_target_specs = [], []
    ndvis, reflectance_ranges = [], []
    for (raw, rs) in zip(raw_specs, rs_specs):
        (status, ndvi, reflectance_range) = gvd.is_green_vegetation(rs)
        ndvis.append(ndvi)
        reflectance_ranges.append(reflectance_range)
        if status:
            raw_target_specs.append(raw)
            rs_target_specs.append(rs)
        else:
            raw_white_specs.append(raw)
            rs_white_specs.append(rs)
    print("   # white spectra: {}".format(len(rs_white_specs)))
    print("   # target spectra: {}".format(len(rs_target_specs)))

    
    #subset to desired wavelength range
    #do this for the jc_target_specs only
    proc_target_specs = [s.wavelength_subset(ws, we) for s in rs_target_specs]
    print("   Subsetted spectra to range: {}, {}".format(ws, we))

        
    #identify groups using SpectrumRegex and create SpectrumGroup objects
    grps_dict = SpectrumRegex().make_groups(proc_target_specs, pattern)
#    for k in grps_dict:
#        print(k, len(grps_dict[k]))
#        SpectrumGroup().group(grps_dict[k], k)
    patt_groups = [SpectrumGroup().group(grps_dict[k], k) for k in grps_dict]
    print("   Grouped spectrums: {} groups".format(len(patt_groups)))    
    
    #create output directories and save data    
    save_data(input_dir, 
              raw_white_specs, 
              raw_target_specs, 
              proc_target_specs,
              patt_name,
              patt_groups)
def process_directory(params):

    #parameters for processing
    input_dir = params["input_directory"]
    ws = params["subset_wavelength_range"][0]
    we = params["subset_wavelength_range"][1]
    pattern = params["grouping_pattern"]
    patt_name = params['grouping_output_name']

    #read the spectrums
    extension = 'sed'
    spec_files = [
        os.path.join(input_dir, f) for f in os.listdir(input_dir)
        if get_extension(f) == extension
    ]
    raw_specs = [SdalReader().read_spectrum(f) for f in spec_files]
    if len(raw_specs) == 0: return
    print("Data directory: {}".format(input_dir))
    print("  Read {} {} files".format(len(raw_specs), extension))

    #uniquify wavelengths, monotone them and interpolate to 1nm
    ohr = OverlapHandler(rstype='cubic')
    rs_specs = []
    for s in raw_specs:
        if ohr.is_1nm(s):
            rs_specs.append(s)
        else:
            rs_specs.append(ohr.process_overlap(s))
    print("  Uniquified and monotoned spectrums")

    #separate into green vegetation and non-green vegetation spectra
    gvd = GreenVegDetector()
    raw_white_specs, raw_target_specs = [], []
    rs_white_specs, rs_target_specs = [], []
    ndvis, reflectance_ranges = [], []
    for (raw, rs) in zip(raw_specs, rs_specs):
        (status, ndvi, reflectance_range) = gvd.is_green_vegetation(rs)
        ndvis.append(ndvi)
        reflectance_ranges.append(reflectance_range)
        if status:
            raw_target_specs.append(raw)
            rs_target_specs.append(rs)
        else:
            raw_white_specs.append(raw)
            rs_white_specs.append(rs)
    print("   # white spectra: {}".format(len(rs_white_specs)))
    print("   # target spectra: {}".format(len(rs_target_specs)))

    #subset to desired wavelength range
    #do this for the jc_target_specs only
    proc_target_specs = [s.wavelength_subset(ws, we) for s in rs_target_specs]
    print("   Subsetted spectra to range: {}, {}".format(ws, we))

    #identify groups using SpectrumRegex and create SpectrumGroup objects
    grps_dict = SpectrumRegex().make_groups(proc_target_specs, pattern)
    patt_groups = [SpectrumGroup().group(grps_dict[k], k) for k in grps_dict]
    print("   Grouped spectrums: {} groups".format(len(patt_groups)))

    #create output directories and save data
    save_data(input_dir, raw_white_specs, raw_target_specs, proc_target_specs,
              patt_name, patt_groups)
Exemplo n.º 4
0
import os, os.path
import matplotlib.pyplot as plt
from SdalReader import SdalReader
from OverlapHandler import OverlapHandler

if __name__ == "__main__":
    print("Testing the OverlapHandler class")
    
    testdir = "/home/prabu/mycode/sdal/tests_data/test_OverlapHandler"
    
    
    signames = ["gr082014_000.sig", "gr082014_001.sig"]
    sigfiles = [os.path.join(testdir, "input", f) for f in signames]
    sigspec = [SdalReader().read_spectrum(f) for f in sigfiles]
    
    oh = OverlapHandler()
    ohdspec = [oh.process_overlap(s) for s in sigspec]
    
    #plot it to see if it looks correct
    fig1, axes1 = plt.subplots(nrows = 2, ncols = 1)
    for i in range(len(ohdspec)):
        axes1[i].set_xlim([300.0, 2600.0])
        axes1[i].set_ylim([0.0, 1.0])
        axes1[i].plot(ohdspec[i].wavelengths, 
                      ohdspec[i].reflectances + 0.1, 
                      color = 'g',
                      alpha = 0.6,
                      label = "After overlap handling")
        axes1[i].plot(sigspec[i].wavelengths, 
                      sigspec[i].reflectances, 
                      color = 'r',