Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #20

Merged
merged 25 commits into from
Jun 7, 2024
Merged

Dev #20

Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
a39cc33
new SumSquareReference class
bmeyers Apr 19, 2023
1e44865
this is a test commit for educational purposes
bmeyers Apr 19, 2023
c0e4afa
undoing last commit
bmeyers Apr 19, 2023
d2fdd8e
duck typing input
bmeyers May 1, 2023
002f875
q and r should no longer be hard-coded to zero
bmeyers May 1, 2023
f222364
work on the SumSquareReference class
bmeyers May 1, 2023
dadc75c
aggregate class needs to check for q and r attributes and handles the…
bmeyers May 1, 2023
011ba4f
update default values for q and r in base class
bmeyers May 1, 2023
eed5032
Merge branch 'main' of github.com:cvxgrp/optimal-signal-decomposition…
bmeyers May 1, 2023
819acaf
fourier basis class
bmeyers May 8, 2023
94af0b3
new class for setting values at certain indices equal to a number
bmeyers May 16, 2024
e0d14cc
Merge pull request #19 from cvxgrp/dev2
bmeyers May 16, 2024
9db2b76
Merge pull request #15 from cvxgrp/feature
bmeyers May 16, 2024
bf736e3
work on Fourier basis class
bmeyers May 17, 2024
059c000
Set 'r' to be zero always. Allows weighting matrix inside sum_abs class
bmeyers Jun 1, 2024
8e661e6
update package and workflows
pluflou Jun 6, 2024
09d5ab9
fix tests dependencies
pluflou Jun 6, 2024
b824c8d
fix tests dependencies
pluflou Jun 6, 2024
3474348
fix tests dependencies, might need to clean later
pluflou Jun 6, 2024
ac101bd
rm dead link and add __version__ attr
pluflou Jun 6, 2024
4c81ce9
add _version module
pluflou Jun 7, 2024
c1f47bb
adjust triggers for test wf
pluflou Jun 7, 2024
332b588
Update build.yml
pluflou Jun 7, 2024
b89a791
Update test.yml
pluflou Jun 7, 2024
dc52dc6
Merge pull request #21 from pluflou/fix-deploy
bmeyers Jun 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions gfosd/components/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
SumAbs,
SumHuber,
SumQuantile,
SumCard)
SumCard,
SumSquareReference)
from gfosd.components.inequality_constraints import Inequality
from gfosd.components.basis_constraints import Basis, Periodic
from gfosd.components.basis_constraints import Basis, Periodic, Fourier
from gfosd.components.finite_set import FiniteSet, Boolean
from gfosd.components.aggregate import Aggregate
from gfosd.components.equality_constraints import (FirstValEqual,
Expand Down
8 changes: 7 additions & 1 deletion gfosd/components/aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ def prepare_attributes(self, T, p=1):
self._Pz = sp.block_diag([
c._Pz for c in self._gf_list
])
self._make_q()
# same logic as above but for q
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add comment: this is related to "close to input vector" class, which is incomplete and ready for use. q is still set to be zero for all implemented classes.

qx = self._gf_list[g_ix]._q[:self._gf_list[g_ix].x_size]
qz = np.concatenate([c._q[self._gf_list[g_ix].x_size:] for c in self._gf_list])
self._q = np.concatenate([qx, qz])
self._make_r()
self._gx = self._make_gx()
self._gz = self._make_gz()
Expand All @@ -53,6 +56,9 @@ def _make_P(self):
c._Pz for c in self._gf_list
])

def _make_r(self):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment: still always set to zero

self._r = np.sum([c._r for c in self._gf_list])

def _make_gx(self):
gx = []
for ix, component in enumerate(self._gf_list):
Expand Down
4 changes: 2 additions & 2 deletions gfosd/components/base_graph_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ def _make_P(self, size):
return sp.dok_matrix(2 * (size,))

def _make_q(self):
self._q = None
self._q = np.zeros(self.x_size + self.z_size)

def _make_r(self):
self._r = None
self._r = 0

def _make_g(self, size):
return []
Expand Down
31 changes: 28 additions & 3 deletions gfosd/components/basis_constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@

"""


import numpy as np
import scipy.sparse as sp
from gfosd.components.base_graph_class import GraphComponent
from gfosd.components.utilities import make_basis_matrix, make_regularization_matrix


class Basis(GraphComponent):
def __init__(self, basis, penalty=None, *args, **kwargs):
Expand Down Expand Up @@ -51,8 +52,8 @@ def _make_g(self, size):
else:
# typically 'abs', 'huber', or 'quantile'
g = [{'g': self._penalty,
'args': {'weight': self.weight},
'range': (0, size)}]
'args': {'weight': self.weight},
'range': (0, size)}]
return g

def _make_P(self, size):
Expand All @@ -65,8 +66,32 @@ def _make_P(self, size):
P = sp.dok_matrix(2 * (size,))
return P


class Periodic(Basis):
def __init__(self, period, *args, **kwargs):
self._period = period
M = sp.eye(period)
super().__init__(M, *args, **kwargs)


class Fourier(Basis):
def __init__(self, num_harmonics, length, periods, standing_wave=False, trend=False, max_cross_k=None,
custom_basis=None, weight=1, **kwargs):
_B = make_basis_matrix(
num_harmonics,
length,
periods,
standing_wave,
trend,
max_cross_k,
custom_basis)
_D = make_regularization_matrix(
num_harmonics,
weight,
periods,
standing_wave,
trend,
max_cross_k,
custom_basis
)
super().__init__(basis=_B, penalty=_D, weight=weight, **kwargs)
28 changes: 28 additions & 0 deletions gfosd/components/equality_constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,34 @@ def _make_B(self):
def _make_c(self):
self._c = np.concatenate([np.atleast_1d(self._c[-1]),
[self._last_val]])


class ValsEqual(GraphComponent):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

incomplete and no intention to finish at this time

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add not implemented warning

def __init__(self, indices=0, value=0, *args, **kwargs):
self.indices = np.atleast_1d(indices)
self._val = value
super().__init__(*args, **kwargs)
# always retain helper variable
self._has_helpers = True

def _make_A(self):
super()._make_A()
super()._make_B()
super()._make_c()
self._A = sp.bmat([
[self._A.tocsr()[-1]],
[sp.dok_matrix((len(self.indices), self._A.shape[1]))]
])

def _make_B(self):
self._B = sp.bmat([
[self._B.tocsr()[-1]], #XXXXX got to here! need to update this matrix generating function to identify all indices that are to be set equal
[sp.coo_matrix(([1], ([0], [self._B.shape[1]-1])), shape=(len(self.indices), self._B.shape[1]))]
])

def _make_c(self):
self._c = np.concatenate([np.atleast_1d(self._c[-1]),
[self.val] * len(self.indices)])

class AverageEqual(GraphComponent):
def __init__(self, value=0, period=None, *args, **kwargs):
Expand Down
54 changes: 53 additions & 1 deletion gfosd/components/sums.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import numpy as np
import scipy.sparse as sp
from gfosd.components.base_graph_class import GraphComponent


class SumSquare(GraphComponent):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Expand All @@ -9,17 +11,65 @@ def __init__(self, *args, **kwargs):
def _make_P(self, size):
return self.weight * 2 * sp.eye(size) # note the (1/2) in canonical form!


class SumSquareReference(GraphComponent):
"""
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add not implemented warning?

phi(x;v) = || x - v ||_2^2 = ||x||_2^2 -2v^Tx + ||v||_2^2
"""

def __init__(self, vec, mask=None, *args, **kwargs):
self._vec = vec
self._mask = mask
super().__init__(*args, **kwargs)
if self._mask is not None:
self._has_helpers = True

def _set_z_size(self):
if self._mask is None:
self._z_size = (self._T - self._diff) * self._p
else:
self._z_size = self._mask.shape[0]

def _make_P(self, size):
return self.weight * 2 * sp.eye(size) # note the (1/2) in canonical form!

def _make_q(self):
if self._mask is None:
qx = self.weight * (-2) * self._vec
self._q = np.r_[qx, np.zeros(self.z_size)]
else:
qz = (-2) * self.weight * self._mask @ self._vec
self._q = np.r_[np.zeros(self.x_size), qz]

def _make_r(self):
if self._mask is None:
self._r = self.weight * np.sum(np.square(self._vec))
else:
self._r = self.weight * np.sum(np.square(self._mask @ self._vec))

def _make_A(self):
self._A = self._mask


class SumAbs(GraphComponent):
def __init__(self, *args, **kwargs):
def __init__(self, weighting_mat=None, *args, **kwargs):
self.weighting_mat = weighting_mat
super().__init__(*args, **kwargs)
return

def _make_B(self):
if self.weighting_mat is None:
self._B = sp.eye(self._A.shape[0], self.z_size) * -1
else:
self._B = self.weighting_mat

def _make_g(self, size):
g = [{'g': 'abs',
'args': {'weight': self.weight},
'range': (0, size)}]
return g


class SumHuber(GraphComponent):
def __init__(self, M=1, *args, **kwargs):
self._M = M
Expand All @@ -32,6 +82,7 @@ def _make_g(self, size):
'range': (0, size)}]
return g


class SumQuantile(GraphComponent):
def __init__(self, tau, *args, **kwargs):
self.tau = tau
Expand All @@ -44,6 +95,7 @@ def _make_g(self, size):
'range': (0, size)}]
return g


class SumCard(GraphComponent):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Expand Down
Loading
Loading