def test_mask(self): for is_3d in [True, False]: mask = '/tmp/mask.tif' processing.image_mask_from_vector(vector, raster, mask) mask_src = gdal.Open(mask) raster_src = gdal.Open(raster) mask_proj = osr.SpatialReference(wkt=mask_src.GetProjection()) raster_proj = osr.SpatialReference(wkt=raster_src.GetProjection()) assert (raster_proj.GetAttrValue('projcs') == mask_proj.GetAttrValue('projcs')) assert (mask_src.RasterCount == 1) assert (mask_src.RasterXSize == raster_src.RasterXSize) assert (mask_src.RasterYSize == raster_src.RasterYSize) rM_band = processing.RasterMath(raster, return_3d=is_3d) for idx, band in enumerate(rM_band.read_band_per_band()): pass rM_band.add_function(np.mean, axis=is_3d + 1, out_image='/tmp/mean.tif') rM_band.run() self.assertRaises(MemoryError, rM_band.run, '1K') assert (idx + 1 == rM_band.n_bands) x = rM_band.get_random_block() assert (x.ndim == is_3d + 2) os.remove('/tmp/mean.tif')
def test_mask(self) : for is_3d in [True, False]: mask = '/tmp/mask.tif' processing.image_mask_from_vector(vector,raster,mask) mask_src = gdal.Open(mask) raster_src = gdal.Open(raster) assert(mask_src.GetProjection() == raster_src.GetProjection()) assert(mask_src.RasterCount == 1) assert(mask_src.RasterXSize == raster_src.RasterXSize) assert(mask_src.RasterYSize == raster_src.RasterYSize) rM_band = processing.RasterMath(raster,return_3d=is_3d) for idx,band in enumerate(rM_band.read_band_per_band()): pass rM_band.add_function(np.mean,axis=1,out_image='/tmp/mean.tif') rM_band.run() assert(idx+1 == rM_band.n_bands) x = rM_band.get_random_block() assert(x.ndim == is_3d+2) os.remove('/tmp/mean.tif')
from museotoolbox.processing import image_mask_from_vector from osgeo import gdal import os import tempfile tempdir = tempfile.mkdtemp() import shutil from sklearn.ensemble import RandomForestClassifier raster, vector = load_historical_data(low_res=True) X, y, g = load_historical_data(return_X_y_g=True, low_res=True) param_grid = dict(n_estimators=[1, 10]) classifier = RandomForestClassifier() image_mask_from_vector(vector, raster, os.path.join(tempdir, 'mask.tif')) class TestStats(unittest.TestCase): def test_superLearner(self): n_cv = 2 for tf in [True, False]: verbose = tf + 1 model = ai.SuperLearner(classifier, param_grid=param_grid, n_jobs=1, verbose=verbose) model.fit(X, y, group=g, standardize=tf, cv=n_cv) assert (model.predict_array(X).shape == y.shape) len(model.CV) == n_cv
from matplotlib import pyplot as plt import numpy as np ############################################################################## # Load HistoricalMap dataset # ------------------------------------------- raster, vector = datasets.load_historical_data() ############################################################################## # Initialize rasterMath with raster # ------------------------------------ # Set return_3d to True to have full block size (not one pixel per row) # Create raster mask to only keep pixel inside polygons. image_mask_from_vector(vector, raster, '/tmp/mask.tif', invert=False) rM = RasterMath(raster, in_image_mask='/tmp/mask.tif', return_3d=True) #rM.addInputRaster('/tmp/mask.tif') print(rM.get_random_block().shape) ####################### # Plot blocks x = rM.get_random_block() rM.add_function(np.mean, '/tmp/mean.tif', axis=2, out_np_dt=np.int16) rM.run() from osgeo import gdal dst = gdal.Open('/tmp/mean.tif')
# -*- coding: utf-8 -*- import unittest from shutil import copyfile import numpy as np from museotoolbox import processing from museotoolbox.datasets import load_historical_data from osgeo import gdal import os raster,vector = load_historical_data() rM = processing.RasterMath(raster) mask = processing.image_mask_from_vector(vector,raster,'/tmp/mask.tif') class TestRaster(unittest.TestCase): def test_convert_datatype(self): self._assert_np_gdt(np.dtype('uint8').name,gdal.GDT_Byte) self._assert_np_gdt(np.dtype('int16').name,gdal.GDT_Int16) self._assert_np_gdt(np.dtype('uint16').name,gdal.GDT_UInt16) self._assert_np_gdt(np.dtype('int32').name,gdal.GDT_Int32) self._assert_np_gdt(np.dtype('uint32').name,gdal.GDT_UInt32) self._assert_np_gdt(np.dtype('int64').name,gdal.GDT_Int32) self._assert_np_gdt(np.dtype('uint64').name,gdal.GDT_Int32) self._assert_np_gdt(np.dtype('uint16').name,gdal.GDT_UInt16) self._assert_np_gdt(np.dtype('float32').name,gdal.GDT_Float32) self._assert_np_gdt(np.dtype('float64').name,gdal.GDT_Float64) self._assert_np_gdt(gdal.GDT_Byte,np.uint8)
import numpy as np from museotoolbox import ai from museotoolbox.datasets import load_historical_data from museotoolbox.processing import image_mask_from_vector from osgeo import gdal import os import shutil from sklearn.ensemble import RandomForestClassifier raster,vector = load_historical_data(low_res=True) X,y,g = load_historical_data(return_X_y_g=True,low_res=True) param_grid = dict(n_estimators=[1,10]) classifier = RandomForestClassifier() image_mask_from_vector(vector,raster,'/tmp/mask.tif') class TestStats(unittest.TestCase): def test_superLearner(self): n_cv = 2 for tf in [True,False]: verbose = tf+1 model = ai.SuperLearner(classifier,param_grid=param_grid,n_jobs=1,verbose=verbose) model.fit(X,y,group=g,standardize=tf,cv=n_cv) assert(model.predict_array(X).shape == y.shape) len(model.CV) == n_cv assert(np.all(model.group == g)) model.predict_image(raster,'/tmp/SuperLearner/class.tif',confidence_per_class='/tmp/SuperLearner/confclass.tif',higher_confidence='/tmp/SuperLearner/higherconf.tif') assert(model._is_standardized == tf)