ld2daps/ld2dap/Normalize.py

50 lines
1.3 KiB
Python

#!/usr/bin/python
# -*- coding: utf-8 -*-
# \file Normalize.py
# \brief TODO
# \author Florent Guiotte <florent.guiotte@gmail.com>
# \version 0.1
# \date 12 sept. 2018
#
# TODO details
from ld2dap.core import Filter
import numpy as np
class Normalize(Filter):
"""Normalize stream values
This node will normalize values between min and max, unless dtype is
provided.
If dtype is not None, min and max will be set to dtype extremum.
"""
def __init__(self, min=0., max=1., dtype=None):
super().__init__(self.__class__.__name__)
if dtype is not None:
self.min = np.iinfo(dtype).min
self.max = np.iinfo(dtype).max
else:
self.min = min
self.max = max
def _process(self, data, metadata):
self.logger.info('Filtering')
# TODO: see TODO from Treshold _process
## Channel independant scale
for i in range(data.shape[2]):
data[:,:,i] -= data[:,:,i].min() - self.min
data[:,:,i] *= (self.max - self.min) / (data[:,:,i].max() - data[:,:,i].min())
for stack in metadata:
for d, s in zip(stack.desc, stack.symb):
d.append('normalize [{}, {}]'.format(self.min, self.max))
# s.append('T_{{{}}}'.format(self.treshold))
return data, metadata