# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2016-2017 European Synchrotron Radiation Facility
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# ###########################################################################*/
__authors__ = ["J. Garriga"]
__license__ = "MIT"
__date__ = "06/04/2020"
import logging
import numpy
from .base import Base
_logger = logging.getLogger(__file__)
[docs]class NMF(Base):
"""
Non-Negative Matrix Factorization.
Find two non-negative matrices whose product approximates the non-negative
matrix data.
"""
def _init_w(self):
if self.W is None:
Base._init_w(self)
def _init_h(self):
if self.H is None:
Base._init_h(self)
def _update_h(self):
_logger.info("Updating H")
H2 = numpy.empty(self.H.shape[0])
h = numpy.empty(self.H.shape[1])
for row in range(self.W.shape[1]):
H2 = numpy.matmul(numpy.matmul(self.W.T[row], self.W), self.H) + 10**-9
for column in range(0, self.H.shape[1], self._hstep):
if self.indices is None:
h[column:column + self._hstep] = numpy.matmul(
self.W.T[row], self.data[:, column:column + self._hstep])
else:
h[column:column + self._hstep] = numpy.matmul(
self.W.T[row], self.data[self.indices, column:column + self._hstep])
self.H[row] *= h
self.H[row] /= H2
def _update_w(self):
_logger.info("Updating W")
W2 = numpy.empty(self.W.shape[0])
if self.indices is None:
for row in range(0, len(self.W), self._vstep):
W2 = numpy.matmul(numpy.matmul(self.W[row:row + self._vstep], self.H), self.H.T) + 10**-9
numpy.matmul(self.data[row:row + self._vstep], self.H.T)
self.W[row:row + self._vstep] *= numpy.matmul(self.data[row:row + self._vstep], self.H.T)
self.W[row:row + self._vstep] /= W2
else:
for row in range(0, len(self.indices), self._vstep):
indx = self.indices[row:row + self._vstep]
W2 = numpy.matmul(numpy.matmul(self.W[row:row + self._vstep], self.H), self.H.T) + 10**-9
numpy.matmul(self.data[indx], self.H.T)
self.W[row:row + self._vstep] *= numpy.matmul(self.data[indx], self.H.T)
self.W[row:row + self._vstep] /= W2
self.W /= numpy.sqrt(numpy.sum(self.W**2.0, axis=0))