Detect overflow in Differential, add auto strat

This commit is contained in:
Florent Guiotte 2018-05-11 17:26:08 +02:00
parent 1e04ac54cb
commit 19d9b99091
6 changed files with 183 additions and 7 deletions

View File

@ -0,0 +1,153 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Detect overflow with two rasters"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"im1 = np.arange(16*9*1).astype(np.uint8).reshape(16,9,1)\n",
"\n",
"im2 = im1.copy()\n",
"f = np.random.random(im2.shape) < .2\n",
"im2[f] += 1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Detection"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(im2 > im1).any()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"old_settings = np.seterr(all='ignore')\n",
"np.seterr(all='print')\n",
"np.unique(im1 - im2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Detect overflow in a Stack"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"stack = np.arange(16*9*4).astype(np.uint8).reshape(16,9,4)\n",
"stack.shape"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(stack[:,:,1:] > stack[:,:,:-1]).any()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"np.subtract(stack[:,:,:-1], stack[:,:,1:], dtype=stack.dtype)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"stack.dtype.str\n",
"np.dtype('|i1')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"t = str(stack.dtype)\n",
"np.dtype('int8')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"t.replace('u', '')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"t.lstrip('u')"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

View File

@ -52,10 +52,10 @@
"t = ld2dap.Treshold(1e4)\n",
"a = ld2dap.SelfDualAttributeProfiles(area = [1e3, 1e6], sd=[.4,.6,.8], moi=[.5,.9])\n",
"f = ld2dap.Differential()\n",
"d = ld2dap.ShowFig(stack_id='all', symb=True)\n",
"d = ld2dap.ShowFig(stack_id='all', symb=False)\n",
"o = ld2dap.RawOutput()\n",
"\n",
"o.input = f\n",
"o.input = a\n",
"d.input = f\n",
"f.input = a\n",
"a.input = t\n",
@ -64,6 +64,15 @@
"d.run()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(o.data[:,:,0] == o.data[:,:,13]).all()"
]
},
{
"cell_type": "code",
"execution_count": null,

View File

@ -12,8 +12,9 @@ from ld2dap.core import Filter, Stack
import numpy as np
class Differential(Filter):
def __init__(self):
def __init__(self, overflow='auto'):
super().__init__(self.__class__.__name__)
self.overflow = overflow
def _process(self, data, metadata):
self.logger.info('Run differential on stack of size {}'.format(data.shape))
@ -23,8 +24,20 @@ class Differential(Filter):
offset = 0
for stack in metadata:
self.logger.debug('{}'.format(stack))
raster_list.append(data[:,:,stack.begin+1:stack.end] -
data[:,:,stack.begin:stack.end-1])
overflow = (data[:, :, stack.begin:stack.end-1] >
data[:, :, stack.begin+1:stack.end]).any() and \
str(data.dtype).startswith('u')
dtype = data.dtype
if overflow:
self.logger.warn('Overflow detected')
if self.overflow == 'auto':
dtype = np.dtype(str(data.dtype).lstrip('u'))
self.logger.info('Changing dtype from {} to {}'.format(
data.dtype, dtype))
raster_list.append(np.subtract(data[:, :, stack.begin+1:stack.end],
data[:, :, stack.begin:stack.end-1], dtype=dtype))
size_new = stack.end - stack.begin - 1
stack_new = Stack(offset, size_new)

View File

@ -80,7 +80,7 @@ class SelfDualAttributeProfiles(Filter):
# Data
# Duplicate origin in data to respect Stack structure
# Compute insert index in where
where = np.array(list(att_len.values()))
where = np.array(list(att_len.values()))
where = where[where != 0] - 1
where[0] += 1
count = sum(where)

View File

@ -45,6 +45,7 @@ class ShowFig(Output):
plt.rc('text', usetex=True)
f1.set_title('${}$'.format(''.join(filter(None, stack.symb[i]))))
else:
plt.rc('text', usetex=False)
f1.set_title(' > '.join(filter(None, stack.desc[i])))
if self.fname is not None:

View File

@ -22,7 +22,7 @@ def main():
t = Treshold(1e4)
ap = SDAPs([100,1e3], [.5,.7,.9], [.1,.3])
o = SaveFig('Res/test.png')
s = ShowFig(0, True)
s = ShowFig(0, False)
d = Differential()
t.input = i