Skip to content

Commit

Permalink
new API: filter banks behave like containers and matrices (experimental)
Browse files Browse the repository at this point in the history
not documented and tested yet as we want to see how it works in practice
before recommending its usage
  • Loading branch information
mdeff committed Dec 14, 2018
1 parent f44d31d commit dfc428d
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions pygsp/filters/filter.py
Expand Up @@ -5,6 +5,7 @@
from pygsp import utils
# prevent circular import in Python < 3.5
from . import approximations
from ..graphs import Graph


_logger = utils.build_logger(__name__)
Expand Down Expand Up @@ -63,6 +64,7 @@ def __init__(self, G, kernels):

# Only used by subclasses to instantiate a single filterbank.
self.n_features_in, self.n_features_out = (1, len(kernels))
self.shape = (self.n_features_out, self.n_features_in)
self.n_filters = self.n_features_in * self.n_features_out
self.Nf = self.n_filters # TODO: kept for backward compatibility only.

Expand All @@ -78,6 +80,28 @@ def __repr__(self):
s += '{}={}, '.format(key, value)
return '{}({})'.format(self.__class__.__name__, s[:-2])

def __len__(self):
# Numpy returns shape[0].
return self.n_filters

def __getitem__(self, key):
return Filter(self.G, self._kernels[key])

def __add__(self, other):
"""Concatenation of filterbanks."""
if not isinstance(other, Filter):
return NotImplemented
return Filter(self.G, self._kernels + other._kernels)

def __call__(self, x):
if isinstance(x, Graph):
return Filter(x, self._kernels)
else:
return self.evaluate(x)

def __matmul__(self, other):
return self.filter(other)

def evaluate(self, x):
r"""Evaluate the kernels at given frequencies.
Expand Down

0 comments on commit dfc428d

Please sign in to comment.