Ejemplo n.º 1
0
def cluster(targetLocationPath, probabilityPath, iterationCountPerBurst, maximumGeoDiameter, minimumGeoDiameter):
    # Initialize
    probabilityInformation = probability_store.Information(probabilityPath)
    imagePath = probabilityInformation.getImagePath()
    imageInformation = image_store.Information(imagePath)
    multispectralImage = imageInformation.getMultispectralImage()
    multispectralGeoTransform = multispectralImage.getGeoTransform()
    spatialReference = multispectralImage.getSpatialReference()
    # Convert maximumGeoDiameter into maximumPixelDiameter
    maximumPixelDiameter = max(
        image_store.convertGeoDimensionsToPixelDimensions(
            maximumGeoDiameter, maximumGeoDiameter, multispectralGeoTransform
        )
    )
    # Convert minimumGeoDiameter into minimumPixelDiameter
    minimumPixelDiameter = max(
        image_store.convertGeoDimensionsToPixelDimensions(
            minimumGeoDiameter, minimumGeoDiameter, multispectralGeoTransform
        )
    )
    # Load pixelDimensions
    windowGeoLength = probabilityInformation.getWindowLengthInMeters()
    pixelWidth, pixelHeight = image_store.convertGeoDimensionsToPixelDimensions(
        windowGeoLength, windowGeoLength, multispectralGeoTransform
    )
    # Cluster
    probabilityPacks = probability_store.load(probabilityPath)
    vectors = [(x, y, probability) for x, y, label, probability in probabilityPacks if label == 1]
    windowLocations = grapeCluster(vectors, iterationCountPerBurst, maximumPixelDiameter, minimumPixelDiameter)
    windowCenters = [(xy[0] + pixelWidth / 2, xy[1] + pixelHeight / 2) for xy in windowLocations]
    geoCenters = image_store.convertPixelLocationsToGeoLocations(windowCenters, multispectralGeoTransform)
    # Save
    point_store.save(targetLocationPath, geoCenters, spatialReference)
def evaluateWindows(probabilityPath, actualLocationPath, multispectralImagePath, windowGeoLength):
    # Initialize
    print 'Evaluating windows...'
    multispectralImage = image_store.load(multispectralImagePath)
    # Load
    probabilityPacks = probability_store.load(probabilityPath)
    windowLocations = [(x[0], x[1]) for x in probabilityPacks]
    windowPixelWidth, windowPixelHeight = multispectralImage.convertGeoDimensionsToPixelDimensions(windowGeoLength, windowGeoLength)
    windowCount = len(windowLocations)
    # Load predicted
    predictedWindowLocations = set((x[0], x[1]) for x in probabilityPacks if x[2] == 1)
    # Load actual
    actualGeoLocations = point_store.load(actualLocationPath)[0]
    actualPixelLocations = multispectralImage.convertGeoLocationsToPixelLocations(actualGeoLocations)
    actualPixelPointMachine = point_process.PointMachine(actualPixelLocations, 'INTEGER', windowPixelWidth, windowPixelHeight)
    actualWindowLocations = set(filter(lambda x: actualPixelPointMachine.getPointsInsideWindow(x), windowLocations))
    # Get
    predictedNotActualWindowLocations = predictedWindowLocations - actualWindowLocations
    actualNotPredictedWindowLocations = actualWindowLocations - predictedWindowLocations
    wrongWindowLocations = predictedNotActualWindowLocations.union(actualNotPredictedWindowLocations)
    wrongPixelCenters = [image_store.getWindowCenter(x, windowPixelWidth, windowPixelHeight) for x in wrongWindowLocations]
    # Compute
    actualTrue_predictedFalse = len(actualNotPredictedWindowLocations)
    actualFalse_predictedTrue = len(predictedNotActualWindowLocations)
    actualTrue = len(actualWindowLocations)
    actualFalse = windowCount - actualTrue
    predictedTrue = len(predictedWindowLocations)
    predictedFalse = windowCount - predictedTrue
    percentError, falsePositiveError, falseNegativeError = computePercentages(actualTrue_predictedFalse, actualFalse_predictedTrue, actualTrue, actualFalse)
    # Save
    windowPerformance = {
        'percent error': percentError, 
        'false positive error': falsePositiveError,
        'false negative error': falseNegativeError,
        'actual true': actualTrue,
        'actual false': actualFalse,
        'predicted true': predictedTrue,
        'predicted false': predictedFalse,
        'actual true predicted true': actualTrue - actualTrue_predictedFalse,
        'actual true predicted false': actualTrue_predictedFalse,
        'actual false predicted true': actualFalse_predictedTrue,
        'actual false predicted false': actualFalse - actualFalse_predictedTrue,
        'window count': windowCount,
    }
    return windowPerformance, wrongPixelCenters, actualPixelPointMachine