ImageMath: Very specific problem with scipy mode function on Linux

Issue #476 resolved
Fabian Thiel created an issue

I get an error using the stats.mode function in imageMath on Linux (on our jupyterhub VM). Works on Windows!

We are running Python 3.8 on the VM.

The code executes fine, the error only occurs when the count variable is mapped to an output. Writing mode to disk is also no problem (see code below).

Maybe you can spot the issue. Why would it work on Windows…. gdal versions seem to be the same…

ImageMath code:

import numpy as np
import scipy

stack = np.stack((c1,c2,c3), axis=0)
mode, count = scipy.stats.mode(stack, axis=0)

Error:

Traceback (most recent call last):
File "/home/openshift/.local/share/QGIS/QGIS3/profiles/default/python/plugins/enmapboxplugin/enmapbox/coreapps/enmapboxapplications/imagemathapp/core.py", line 910, in execute
calculator.applyCode(code=code, options=options, overlap=self.uiBlockOverlap.value(), outputKeys=outputKeys)
File "/home/openshift/.local/share/QGIS/QGIS3/profiles/default/python/plugins/enmapboxplugin/enmapbox/coreapps/enmapboxapplications/imagemathapp/calculator.py", line 16, in applyCode
self.apply(operatorType=CalculatorOperator, description='Calculator', overwrite=overwrite,
File "/home/openshift/.local/share/QGIS/QGIS3/profiles/default/python/plugins/enmapboxplugin/hubdc/applier.py", line 975, in apply
results = self._runProcessSubgrids()
File "/home/openshift/.local/share/QGIS/QGIS3/profiles/default/python/plugins/enmapboxplugin/hubdc/applier.py", line 1039, in _runProcessSubgrids
blockResults.append(_Worker.processSubgrid(**kwargs))
File "/home/openshift/.local/share/QGIS/QGIS3/profiles/default/python/plugins/enmapboxplugin/hubdc/applier.py", line 1121, in processSubgrid
return cls.operator._apply(workingGrid=workingGrid, iblock=i, nblock=n, yblock=iy, xblock=ix, nyblock=ny,
File "/home/openshift/.local/share/QGIS/QGIS3/profiles/default/python/plugins/enmapboxplugin/hubdc/applier.py", line 1268, in _apply
blockResult = self.ufunc(*self._ufuncArgs, **self._ufuncKwargs)
File "/home/openshift/.local/share/QGIS/QGIS3/profiles/default/python/plugins/enmapboxplugin/enmapbox/coreapps/enmapboxapplications/imagemathapp/calculator.py", line 92, in ufunc
raster.setArray(array=array, overlap=overlap)
File "/home/openshift/.local/share/QGIS/QGIS3/profiles/default/python/plugins/enmapboxplugin/hubdc/applier.py", line 417, in setArray
self._writerQueue.put(
File "/home/openshift/.local/share/QGIS/QGIS3/profiles/default/python/plugins/enmapboxplugin/hubdc/writer.py", line 108, in put
Writer.handleTask(task=task, args=args, outputRasters=self.outputRasters)
File "/home/openshift/.local/share/QGIS/QGIS3/profiles/default/python/plugins/enmapboxplugin/hubdc/writer.py", line 19, in handleTask
cls.writeArray(outputRasters, *args)
File "/home/openshift/.local/share/QGIS/QGIS3/profiles/default/python/plugins/enmapboxplugin/hubdc/writer.py", line 55, in writeArray
Writer.createRaster(outputRasters=outputRasters, filename=filename, bands=len(array), dtype=array.dtype, grid=maingrid, driver=driver, creationOptions=creationOptions)
File "/home/openshift/.local/share/QGIS/QGIS3/profiles/default/python/plugins/enmapboxplugin/hubdc/writer.py", line 38, in createRaster
outputRasters[filename] = driver.create(grid=grid, bands=bands,
File "/home/openshift/.local/share/QGIS/QGIS3/profiles/default/python/plugins/enmapboxplugin/hubdc/core.py", line 123, in create
gdalDataset = self.gdalDriver().Create(filename, grid.size().x(), grid.size().y(), bands, gdalType, options)
File "/usr/lib/python3/dist-packages/osgeo/gdal.py", line 1676, in Create
return _gdal.Driver_Create(self, *args, **kwargs)
TypeError: in method 'Driver_Create', argument 6 of type 'GDALDataType'

Comments (3)

  1. Andreas Janz

    Looks that under Linux the numpy dtype of the count array can not be translated into a GDAL dtype.

    You can check the dtype of count using print(count.dtype)

    To fix this try count = count.astype(int)

  2. Fabian Thiel reporter

    Thanks! 🙂
    Indeed, the count array was int64. Setting it explicitly to int16 fixed the issue.

  3. Log in to comment