/
svm_class_mapper.py
87 lines (70 loc) · 2.71 KB
/
svm_class_mapper.py
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
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python
import sys
import json
import numpy
import binaryhadoop
import utilities
def calcNoise(imageArray,mask):
ny,nx = numpy.nonzero(mask)
rotatedMaskRows = []
for y in list(set(list(ny)))[10:-10]:
rotatedMaskRows.append(numpy.where(ny==y)[0])
noiseArray = []
for band in imageArray:
noise = []
for row in rotatedMaskRows:
noise += list(band[row[1:]] - band[row[:-1]])
noiseArray.append(numpy.array(noise))
return noiseArray
def main(imageArray):
iDot = []
iPartial = []
for imageRow in imageArray:
iPartial.append(imageRow.sum())
iRow = []
for imageCol in imageArray:
iRow.append(numpy.dot(imageRow,imageCol))
iDot.append(iRow)
return iDot, iPartial
if __name__ == "__main__":
imageData = {}
imageData["metadata"] = None
parameterFile = open("analyticconfig","r")
for line in parameterFile.readlines():
line = line.rstrip().split("\t")
if line[0]=="contourclusters.noiseFlag":
noiseFlag = line[1]
for key, value in binaryhadoop.mapperInput(sys.stdin):
if key == "metadata":
imageData["metadata"] = value
bands = {}
elif key == "mask":
mask = utilities.rollMask(value > 0)
imageData["numPixels"] = numpy.nonzero(mask)[0].size
else:
bands[key] = numpy.array(value[mask],dtype=numpy.float64)
if imageData["metadata"] is not None:
if 'HSI' in imageData["metadata"].keys():
wavelengths = {}
multipliers = {}
for w,wave in enumerate(imageData["metadata"][unicode("HSI")][unicode("wavelength")]):
wavelengths["B" + "%03d" % w] = float(wave)
multipliers["B" + "%03d" % w] = 1
else:
wavelengths = imageData["metadata"]["bandWavelength"]
multipliers = imageData["metadata"]["bandMultiplier"]
imageList = utilities.preprocessImage(bands, multipliers, wavelengths, imageData)
sys.stderr.write("This is the number of bands: %r\n" % len(imageList))
iDot,iPartial = main(imageList)
imageData["imageDot"] = iDot
imageData["imagePartial"] = iPartial
if noiseFlag.upper() == "TRUE":
noiseList = calcNoise(imageList,mask)
nDot,nPartial = main(noiseList)
imageData["noiseDot"] = nDot
imageData["noisePartial"] = nPartial
try:
regionKey = imageData["metadata"]["originalDirName"]
except KeyError:
regionKey = imageData["metadata"]["outputFile"]
binaryhadoop.emit(sys.stdout,regionKey,imageData,encoding=binaryhadoop.TYPEDBYTES_JSON)