|
| 1 | +''' |
| 2 | +Unit test for Phass unwrapper |
| 3 | +''' |
| 4 | + |
| 5 | +import os |
| 6 | + |
| 7 | +import numpy as np |
| 8 | +import numpy.testing as npt |
| 9 | +import pybind_isce3 as isce3 |
| 10 | +from osgeo import gdal, gdal_array |
| 11 | + |
| 12 | +width = 256 |
| 13 | +length = 1100 |
| 14 | + |
| 15 | + |
| 16 | +def to_gdal_dataset(outpath, array): |
| 17 | + driver = gdal.GetDriverByName("Gtiff") |
| 18 | + dtype = gdal_array.NumericTypeCodeToGDALTypeCode(array.dtype) |
| 19 | + length, width = array.shape |
| 20 | + dset = driver.Create(outpath, xsize=width, ysize=length, bands=1, |
| 21 | + eType=dtype) |
| 22 | + dset.GetRasterBand(1).WriteArray(array) |
| 23 | + |
| 24 | + |
| 25 | +def create_datasets(): |
| 26 | + # Generate interferogram |
| 27 | + xx = np.linspace(0.0, 50.0, width) |
| 28 | + yy = np.linspace(0.0, 50.0, length) |
| 29 | + |
| 30 | + x, y = np.meshgrid(xx, yy) |
| 31 | + igram = np.exp(1j * (x + y)) |
| 32 | + phase = np.angle(igram) |
| 33 | + |
| 34 | + to_gdal_dataset('phase.tif', phase) |
| 35 | + |
| 36 | + # Generate coherence |
| 37 | + corr = np.zeros((length, width), dtype=np.float32) |
| 38 | + corr[100:900, 50:100] = 1.0 |
| 39 | + corr[100:900, 150:200] = 1.0 |
| 40 | + corr[900:950, 50:200] = 1.0 |
| 41 | + corr[1000:1050, 50:200] = 1.0 |
| 42 | + |
| 43 | + to_gdal_dataset('coherence.tif', corr) |
| 44 | + |
| 45 | + |
| 46 | +def read_raster(infile): |
| 47 | + ds = gdal.Open(infile, gdal.GA_ReadOnly) |
| 48 | + array = ds.GetRasterBand(1).ReadAsArray() |
| 49 | + ds = None |
| 50 | + return array |
| 51 | + |
| 52 | + |
| 53 | +def test_getter_setter(): |
| 54 | + phass = isce3.unwrap.Phass() |
| 55 | + |
| 56 | + phass.correlation_threshold = 0.5 |
| 57 | + npt.assert_equal(phass.correlation_threshold, 0.5) |
| 58 | + |
| 59 | + phass.good_correlation = 0.6 |
| 60 | + npt.assert_equal(phass.good_correlation, 0.6) |
| 61 | + |
| 62 | + phass.min_pixels_region = 100 |
| 63 | + npt.assert_equal(phass.min_pixels_region, 100) |
| 64 | + |
| 65 | + |
| 66 | +def test_run_phass(): |
| 67 | + # Create interferogram and coherence |
| 68 | + create_datasets() |
| 69 | + |
| 70 | + # Open created datasets as ISCE3 rasters |
| 71 | + phase = isce3.io.Raster('phase.tif') |
| 72 | + corr = isce3.io.Raster('coherence.tif') |
| 73 | + |
| 74 | + # Generate output rasters |
| 75 | + unwRaster = isce3.io.Raster('unw.f4', phase.width, |
| 76 | + phase.length, 1, gdal.GDT_Float32, "ENVI") |
| 77 | + labelRaster = isce3.io.Raster('label.u1', phase.width, |
| 78 | + phase.length, 1, gdal.GDT_Byte, "ENVI") |
| 79 | + |
| 80 | + # Configure and run Phass |
| 81 | + phass = isce3.unwrap.Phass() |
| 82 | + phass.unwrap(phase, corr, unwRaster, labelRaster) |
| 83 | + |
| 84 | + |
| 85 | +def test_check_unwrapped_phase(): |
| 86 | + # Read interferogram and connected components |
| 87 | + label = read_raster('label.u1') |
| 88 | + unw = read_raster('unw.f4') |
| 89 | + |
| 90 | + # Generate reference interferogram |
| 91 | + xx = np.linspace(0.0, 50.0, width) |
| 92 | + yy = np.linspace(0.0, 50.0, length) |
| 93 | + |
| 94 | + x, y = np.meshgrid(xx, yy) |
| 95 | + ref_unw = x + y |
| 96 | + |
| 97 | + # Reference to each label differently |
| 98 | + labels = np.unique(label) |
| 99 | + diff = (ref_unw[np.where(label == labels[1])] - ref_unw[102, 52]) - \ |
| 100 | + (unw[np.where(label == labels[1])] - unw[102, 52]) |
| 101 | + npt.assert_array_less(np.abs(diff).max(), 1e-5) |
| 102 | + |
| 103 | + diff = (ref_unw[np.where(label == labels[2])] - ref_unw[1002, 52]) - \ |
| 104 | + (unw[np.where(label == labels[2])] - unw[1002, 52]) |
| 105 | + npt.assert_array_less(np.abs(diff).max(), 1e-5) |
| 106 | + |
| 107 | + |
| 108 | +def test_check_labels(): |
| 109 | + # Open labels |
| 110 | + label = read_raster('label.u1') |
| 111 | + l, w = label.shape |
| 112 | + |
| 113 | + npt.assert_equal(w, width) |
| 114 | + npt.assert_equal(l, length) |
| 115 | + |
| 116 | + # Check all pixels within the U |
| 117 | + # have the same label |
| 118 | + |
| 119 | + npt.assert_equal(np.all(label[100:900, 50:100] == label[100, 50]), True) |
| 120 | + npt.assert_equal(np.all(label[100:900, 150:200] == label[100, 50]), True) |
| 121 | + npt.assert_equal(np.all(label[900:950, 50:200] == label[900, 50]), True) |
| 122 | + npt.assert_equal(np.all(label[1000:1050, 50:200] == label[1000, 50]), True) |
| 123 | + |
| 124 | + # Check different connected components |
| 125 | + # have different labels |
| 126 | + npt.assert_raises(AssertionError, npt.assert_array_equal, label[100, 50], |
| 127 | + label[1000, 50]) |
| 128 | + npt.assert_raises(AssertionError, npt.assert_array_equal, label[900, 50], |
| 129 | + label[1000, 50]) |
| 130 | + |
| 131 | + |
| 132 | +if __name__ == '__main__': |
| 133 | + test_getter_setter() |
| 134 | + test_run_phass() |
| 135 | + test_check_unwrapped_phase() |
| 136 | + test_check_labels() |
| 137 | + |
0 commit comments