Esempio n. 1
0
def fit_refs_to_image(imagetoprocess, outputdirectory, signaturecollection, startDOY,
                               doyinterval, bestguess, threshold=None, ndvalue=-3000, fitmethod=None, subset=None,
                               meantype=None, workers=4, timebounds=None, xbounds=None, ybounds=None):
    """
    imagepath = "/Users/phoetrymaster/Documents/School/Geography/Thesis/Data/ARC_Testing/ClipTesting/ENVI_1/test_clip_envi_3.dat"
    outdir = "/Users/phoetrymaster/Documents/School/Geography/Thesis/Data/OutImages/"

    newfoldername = "Testing"

    drivercode1 = 'ENVI'
    #ndvalue = -3000

    startDOY = 1
    interval = 16
    threshold = 500
    bestguess = 0
    fitmthd = 'SLSQP'
    mean = geometric  # Acceptable values are geometric (geometric mean) and arithmetic (arithmetic mean). It is an optional argument for the classifier.


    refs = {
        'soy': {1: 174.5, 97: 1252.25, 65: 1139.5, 209: 7659.0, 273: 4606.75, 337: 1371.75, 17: 1055.5, 33: 1098.0,
                49: 1355.25,
                129: 1784.75, 257: 6418.0, 321: 1644.5, 305: 1472.75, 193: 5119.75, 289: 1878.75, 177: 3439.5, 241: 7565.75,
                81: 1205.5, 225: 7729.75, 145: 1736.25, 161: 1708.25, 353: 1358.25, 113: 1340.0},
        'corn': {1: 392.25, 97: 1433.25, 65: 1258.5, 209: 6530.0, 273: 1982.5, 337: 1658.5, 17: 1179.25, 33: 1196.75,
                 49: 1441.25, 129: 1885.25, 257: 2490.25, 321: 1665.75, 305: 1439.0, 193: 6728.25, 289: 1634.5,
                 177: 6356.75,
                 241: 4827.25, 81: 1355.75, 225: 5547.5, 145: 2196.5, 161: 3143.25, 353: 1704.75, 113: 1716.5},
        'wheat': {1: 719.75, 97: 6594.75, 65: 1935.25, 209: 2013.5, 273: 1493.5, 337: 1498.25, 17: 1816.5, 33: 1815.0,
                  49: 1985.25, 129: 6758.0, 257: 1685.75, 321: 1582.5, 305: 1163.25, 193: 2186.25, 289: 1264.5, 177: 2222.5,
                  241: 2301.0, 81: 4070.5, 225: 1858.0, 145: 6228.5, 161: 3296.5, 353: 1372.5, 113: 7035.25}
    }

    sys.exit(fit_refs_to_image(imagepath, outdir, newfoldername, refs, startDOY, interval, threshold, bestguess, fitmthd, meantype=mean))
    """
    #TODO docstrings

    start = dt.now()
    print(start)
    try:
        print("\nProcessing {0}...".format(imagetoprocess))
        print("Outputting files to {0}\n".format(outputdirectory))

        #Open multi-date image to analyze
        image = openImage(imagetoprocess)
        imageproperties = gdalProperties(image)

        print("Input image dimensions are {0} columns by {1} rows and contains {2} bands.".format(imageproperties.cols,
                                                                                                  imageproperties.rows,
                                                                                                  imageproperties.bands))

        array = read_image_into_array(image)  # Read all bands into a 3d array representing the image stack (x, y, time orientation)
        image = ""

        if timebounds:
            timebounds = (bestguess + timebounds[0], bestguess + timebounds[1])
        else:
            timebounds = (bestguess - 10, bestguess + 10)

        if not xbounds:
            xbounds = (0.6, 1.4)

        if not ybounds:
            ybounds = (0.6, 1.4)

        bounds = (xbounds, ybounds, timebounds)
        print(bounds)

        if subset:
            subset = get_px_coords_from_shapefile(imagetoprocess, subset)

        processes = []
        for signum, signature in enumerate(signaturecollection.signatures, start=1):
            p = multiprocessing.Process(target=process_reference,
                                        args=(outputdirectory, signature, array, imageproperties, startDOY, doyinterval,
                                              bestguess, ndvalue),
                                        kwargs={"subset": subset, "fitmthd": fitmethod, "meantype": meantype,
                                                "thresh": threshold, "bounds": bounds})

            #TODO: Problem with joining/starting processes--original thread closes before others are completed -- believe this is now fixed.

            p.start()
            processes.append(p)

            if len(processes) == workers:
                for p in processes:
                    p.join()
                    processes.remove(p)

        for p in processes:
                    p.join()

    except Exception as e:
        import traceback
        exc_type, exc_value, exc_traceback = sys.exc_info()
        print(e)
        traceback.print_exception(exc_type, exc_value, exc_traceback, limit=2, file=sys.stdout)

    finally:
        print(dt.now() - start)
Esempio n. 2
0
def classify_and_assess_accuracy(outputdir, cropimgpath, searchstringsvals, filevalist, nodata, thresholdsandlength,
                                 classifiedimagename=None, numberofprocesses=4):
    """
    """
    #TODO Docstring

    #from multiprocessing import Pool
    #pool = Pool(numberofprocesses)

    if classifiedimagename is None:
        today = dt.now()
        classifiedimagename = today.strftime("%Y-%m-%d_%H%M_") + os.path.splitext(os.path.basename(cropimgpath))[0]

    classificationimage = os.path.join(outputdir, classifiedimagename + ".tif")
    accuracyimage = os.path.join(outputdir, classifiedimagename + "_accuracy.tif")
    accuracyreport = os.path.join(outputdir, classifiedimagename + ".txt")

    #np.set_printoptions(threshold=np.nan)  # For debug: Makes numpy print whole contents of an array.
    #Crop image is constant for all iterations
    cropimg = openImage(cropimgpath)
    cropimgproperties = gdalProperties(cropimg)
    croparray = read_image_into_array(cropimg)
    cropimg = None

    arraylist = [(read_image_into_array(openImage(f[0])), f[1]) for f in filevalist]  # fit images with crop vals

    writestring = "The fit rasters and truth values used for this classification process are:\n"
    for f in filevalist:
        writestring = writestring + "\t{0}\t{1}\n".format(f[0], f[1])

    writestring = writestring + "\n"

    bestacc = 0
    bestthresh = None

    thresholdlist, lengthofthresholdlist = thresholdsandlength

    try:
        if lengthofthresholdlist == 1:
            writestring = writestring + "\n\n**Only using a single threshold value--not iterating.**\n\n"
            bestthresh = thresholdlist[0]
        else:

            #TODO: Refactor to allow use of multiprocessing.Pool.map -- need to reason about the output/logging
            for thresh in thresholdlist:
                start = dt.now()
                accuracy, classification, outstring = classify_with_threshold(croparray, arraylist, searchstringsvals,
                                                                              thresh, nodata, cropimgproperties.nodata)
                writestring = writestring + outstring

                if accuracy > bestacc:
                    bestacc = accuracy
                    bestthresh = thresh

                elapsed = dt.now() - start
                toprint = [thresh, "{}:{}".format(elapsed.seconds, str(elapsed.microseconds).zfill(6)), accuracy, bestacc, bestthresh]
                width = (6 * len(arraylist))
                sys.stdout.write("Thresh: {: <{width}}   Time: {}   Acc: {: <14}   Best: {: <14} at {}\r".format(*toprint, width=width))
                sys.stdout.flush()

    except Exception as e:
        import traceback
        exc_type, exc_value, exc_traceback = sys.exc_info()
        print e
        traceback.print_exception(exc_type, exc_value, exc_traceback, limit=2, file=sys.stdout)

    finally:
        accuracy, classificationarray, outstring = classify_with_threshold(croparray, arraylist, searchstringsvals,
                                                                           bestthresh, nodata, cropimgproperties.nodata)

        writestring = writestring + outstring

        accuracyarray = find_correct_incorrect_array(croparray, classificationarray, ndvalue=nodata,
                                                     truthndvalue=cropimgproperties.nodata)

        with open(accuracyreport, 'w') as text:
            text.write("Classification using fit images from {0}\n\n".format(os.path.dirname(filevalist[0][0])))
            text.write("{0}\nBest:\n{1} {2}".format(writestring, bestthresh, accuracy))

        print("\n{0}, {1}".format(bestthresh, accuracy))

        driver = gdal.GetDriverByName("ENVI")
        driver.Register()

        write_output_image(cropimgproperties, classificationimage, classificationarray, nodata)
        write_output_image(cropimgproperties, accuracyimage, accuracyarray, nodata)

        print("outputted")

        return 0