2.9 MtkRegression

class MtkRegression
Contains Regression Functionality.

>>> regr = MtkRegression()

downsample( )
Downsamples data by averaging pixels.

>>> r = MtkRegion(37, 50, 60)
>>> m = MtkFile('../Mtk_testdata/in/MISR_AM1_GRP_ELLIPSOID_GM_P037_O029058_AA_F03_0024.hdf')
>>> g = m.grid('BlueBand')
>>> f = m.grid('BlueBand').field('Blue Radiance')
>>> srcdata = f.read(r).data()
>>> srcmask = numpy.ones((srcdata.shape[0],srcdata.shape[1]), dtype= numpy.uint8)
>>> sizefactor = 2
>>> rsmpdata, rsmpmask = regr.downsample(srcdata,srcmask,sizefactor)
>>> print rsmpdata[0][90]
315.341

linear_regression_calc( )
Uses linear regression to fit data.

>>> x = numpy.linspace(2,12,num = 5)
>>> y = numpy.linspace(4,24,num = 5)
>>> ysigma = numpy.linspace(0.1,0.6,num = 5)
>>> regr.linear_regression_calc(x,y,ysigma)
(array([  0.00000000e+000,  -2.32035723e+077,   2.15126649e-314,
         1.06717989e-287,   3.38503263e+125]), 
array([  2.00000000e+000,  -2.68679011e+154,   2.15168741e-314,
         2.15170772e-314,   2.32035723e+077]), 
array([  1.00000000e+000,   3.11109040e+231,   2.15175217e-314,
         6.94154000e-310,  -3.11109040e+231]))

smooth_data( )
Smooths the given array with a boxcar average.

>>> r = MtkRegion(37, 50, 60)
>>> m = MtkFile('../Mtk_testdata/in/MISR_AM1_GRP_ELLIPSOID_GM_P037_O029058_AA_F03_0024.hdf')
>>> g = m.grid('BlueBand')
>>> f = m.grid('BlueBand').field('Blue Radiance')
>>> srcdata = f.read(r).data()
>>> srcmask = numpy.ones((srcdata.shape[0],srcdata.shape[1]), dtype= numpy.uint8)
>>> line_width = 3
>>> sample_width = 3
>>> regr.smooth_data(srcdata,srcmask,line_width,sample_width)
array([[ 0.,  0.,  0., ...,  0.,  0.,  0.],
       [ 0.,  0.,  0., ...,  0.,  0.,  0.],
       [ 0.,  0.,  0., ...,  0.,  0.,  0.],
       ...,
       [ 0.,  0.,  0., ...,  0.,  0.,  0.],
       [ 0.,  0.,  0., ...,  0.,  0.,  0.],
       [ 0.,  0.,  0., ...,  0.,  0.,  0.]], dtype=float32)

upsample_mask( )
Upsamples a mask by nearest neighbor sampling.

>>> srcmask = numpy.ones((1408,624), dtype= numpy.uint8)
>>> size_factor = 2
>>> regr.upsample_mask(srcmask,size_factor)
array([[1, 1, 1, ..., 1, 1, 1],
       [1, 1, 1, ..., 1, 1, 1],
       [0, 0, 0, ..., 0, 0, 0],
       ...,
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0]], dtype=uint8)

coeff_calc( )
Calculates linear regression coefficients for translating values.

>>> r = MtkRegion(37, 50, 60)
>>> m = MtkFile('../Mtk_testdata/in/MISR_AM1_GRP_ELLIPSOID_GM_P037_O029058_AA_F03_0024.hdf')
>>> g1 = m.grid('BlueBand')
>>> f1 = g1.field('Blue Radiance')
>>> data1 = f1.read(r).data()
>>> mask1 = numpy.ones((data1.shape[0],data1.shape[1]), dtype= numpy.uint8)
>>> g2 = m.grid('GreenBand')
>>> f2 = g2.field('Green Radiance')
>>> data2 = f2.read(r).data()
>>> sigma2 = numpy.tile((numpy.linspace(0.1,0.6,data2.shape[0])), (data2.shape[1],1)).transpose()
>>> sigma2 = sigma2.astype(numpy.float32)
>>> mask2 = numpy.ones((data2.shape[0],data2.shape[1]), dtype= numpy.uint8)
>>> mapinfo = f2.read(r).mapinfo()
>>> sizefactor = 2
>>> regr.coeff_calc(data1, mask1, data2, sigma2, mask2, sizefactor, mapinfo)
(<MisrToolkit.MtkRegCoeff object at 0x102e99930>, <MisrToolkit.MtkMapInfo object at 0x7fde19304400>)

apply_regression( )
Applies regression to given data.

>>> r = MtkRegion(37, 50, 60)
>>> m = MtkFile('../Mtk_testdata/in/MISR_AM1_GRP_ELLIPSOID_GM_P037_O029058_AA_F03_0024.hdf')
>>> g1 = m.grid('BlueBand')
>>> f1 = g1.field('Blue Radiance')
>>> data1 = f1.read(r).data()
>>> mask1 = numpy.ones((data1.shape[0],data1.shape[1]), dtype= numpy.uint8)
>>> g2 = m.grid('GreenBand')
>>> f2 = g2.field('Green Radiance')
>>> data2 = f2.read(r).data()
>>> sigma2 = numpy.tile((numpy.linspace(0.1,0.6,data2.shape[0])), (data2.shape[1],1)).transpose()
>>> sigma2 = sigma2.astype(numpy.float32)
>>> mask2 = numpy.ones((data2.shape[0],data2.shape[1]), dtype= numpy.uint8)
>>> mapinfo = f2.read(r).mapinfo()
>>> sizefactor = 2
>>> regr_coeff, regr_mapinfo = regr.coeff_calc(data1, mask1, data2, sigma2, mask2, sizefactor, mapinfo)
>>> regr.apply_regression(data1, mask1, mapinfo, regr_coeff, regr_mapinfo)
(array([[ 0.,  0.,  0., ...,  0.,  0.,  0.],
       [ 0.,  0.,  0., ...,  0.,  0.,  0.],
       [ 0.,  0.,  0., ...,  0.,  0.,  0.],
       ...,
       [ 0.,  0.,  0., ...,  0.,  0.,  0.],
       [ 0.,  0.,  0., ...,  0.,  0.,  0.],
       [ 0.,  0.,  0., ...,  0.,  0.,  0.]], dtype=float32), 
array([[0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       ...,
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0]], dtype=uint8))

resample_reg_coeff( )
Resamples regression coefficients at each pixel.

>>> r = MtkRegion(37, 50, 60)
>>> m = MtkFile('../Mtk_testdata/in/MISR_AM1_GRP_ELLIPSOID_GM_P037_O029058_AA_F03_0024.hdf')
>>> g1 = m.grid('BlueBand')
>>> f1 = g1.field('Blue Radiance')
>>> data1 = f1.read(r).data()
>>> mask1 = numpy.ones((data1.shape[0],data1.shape[1]), dtype= numpy.uint8)
>>> g2 = m.grid('GreenBand')
>>> f2 = g2.field('Green Radiance')
>>> data2 = f2.read(r).data()
>>> sigma2 = numpy.tile((numpy.linspace(0.1,0.6,data2.shape[0])), (data2.shape[1],1)).transpose()
>>> sigma2 = sigma2.astype(numpy.float32)
>>> mask2 = numpy.ones((data2.shape[0],data2.shape[1]), dtype= numpy.uint8)
>>> mapinfo = f2.read(r).mapinfo()
>>> sizefactor = 2
>>> regr_coeff, regr_mapinfo = regr.coeff_calc(data1, mask1, data2, sigma2, mask2, sizefactor, mapinfo)
>>> target_map_info = MtkRegion(37, 50, 60).snap_to_grid(37, 1100)
>>> regr.resample_reg_coeff(regr_coeff, regr_mapinfo, target_map_info)
<MisrToolkit.MtkRegCoeff object at 0x10a623a30>