Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
nam
ProxPython
Commits
75209682
Commit
75209682
authored
Mar 26, 2020
by
Matthijs
Browse files
compatibility of sparsity with non-2d arrays
parent
df972ad2
Changes
1
Show whitespace changes
Inline
Side-by-side
proxtoolbox/ProxOperators/P_Sparsity.py
View file @
75209682
from
numpy
import
zeros_like
from
numpy
import
zeros_like
,
unravel_index
import
numpy
as
np
from
.proxoperators
import
ProxOperator
...
...
@@ -29,14 +29,14 @@ class P_Sparsity(ProxOperator):
if
self
.
sparsity_parameter
>
30
:
def
value_selection
(
original
,
indices
,
sparsity_parameter
):
idx_for_threshold
=
divmod
(
indices
[
-
sparsity_parameter
],
self
.
ny
)
idx_for_threshold
=
unravel_index
(
indices
[
-
sparsity_parameter
],
original
.
shape
)
threshold_val
=
abs
(
original
[
idx_for_threshold
].
get
())
return
(
abs
(
original
)
>=
threshold_val
)
*
original
else
:
def
value_selection
(
original
,
indices
,
sparsity_parameter
):
out
=
zeros_like
(
original
)
hits
=
indices
[
-
sparsity_parameter
:].
get
()
hit_idx
=
[
divmod
(
hit
,
self
.
ny
)
for
hit
in
hits
]
hit_idx
=
[
unravel_index
(
hit
,
original
.
shape
)
for
hit
in
hits
]
for
_idx
in
hit_idx
:
out
[
_idx
[
0
],
_idx
[
1
]]
=
original
[
_idx
[
0
],
_idx
[
1
]]
return
out
...
...
@@ -56,7 +56,7 @@ class P_Sparsity(ProxOperator):
u
*=
self
.
support
# apply support
p_Sparsity
=
0
*
u
sorting
=
np
.
argsort
(
abs
(
u
),
axis
=
None
)
# gives indices of sorted array in ascending order
indices
=
np
.
asarray
([
divmod
(
sorting
[
i
],
self
.
ny
)
for
i
in
range
(
-
1
*
self
.
sparsity_parameter
,
0
)])
indices
=
np
.
asarray
([
unravel_index
(
sorting
[
i
],
u
)
for
i
in
range
(
-
1
*
self
.
sparsity_parameter
,
0
)])
p_Sparsity
[
indices
[:,
0
],
indices
[:,
1
]]
=
u
[
indices
[:,
0
],
indices
[:,
1
]]
return
p_Sparsity
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment