Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
tfields
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Daniel Boeckenhoff
tfields
Commits
e3d73b70
Commit
e3d73b70
authored
3 years ago
by
dboe
Browse files
Options
Downloads
Patches
Plain Diff
new remap method
parent
3ed3ff89
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
tests/test_util_sets.py
+69
-0
69 additions, 0 deletions
tests/test_util_sets.py
tfields/lib/sets.py
+36
-2
36 additions, 2 deletions
tfields/lib/sets.py
tfields/lib/util.py
+1
-0
1 addition, 0 deletions
tfields/lib/util.py
with
106 additions
and
2 deletions
tests/test_util_sets.py
0 → 100644
+
69
−
0
View file @
e3d73b70
import
unittest
import
numpy
as
np
import
tfields
class
RemapTest
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
method
=
tfields
.
lib
.
sets
.
remap
def
test_remap_1d
(
self
):
array
=
np
.
array
([
1
,
2
,
2
,
1
])
keys
=
np
.
array
([
1
,
2
])
values
=
np
.
array
([
0
,
10
])
solution
=
np
.
array
([
0
,
10
,
10
,
0
])
result
=
self
.
method
(
array
,
keys
,
values
)
comparison
=
result
==
solution
self
.
assertTrue
(
comparison
.
all
())
self
.
assertIsNot
(
array
,
result
)
def
test_remap_inplace
(
self
):
array
=
np
.
array
([
1
,
2
,
3
,
1
])
keys
=
np
.
array
([
1
,
2
])
values
=
np
.
array
([
0
,
10
])
solution
=
np
.
array
([
0
,
10
,
3
,
0
])
# inplace
result
=
self
.
method
(
array
,
keys
,
values
,
inplace
=
True
)
comparison
=
result
==
solution
self
.
assertTrue
(
comparison
.
all
())
self
.
assertIs
(
array
,
result
)
def
test_remap
(
self
):
array
=
np
.
array
([
1
,
3
,
2
,
1
]).
reshape
(
2
,
2
)
keys
=
np
.
array
([
1
,
2
])
values
=
np
.
array
([
0
,
10
])
solution
=
np
.
array
([[
0
,
3
],
[
10
,
0
]])
result
=
self
.
method
(
array
,
keys
,
values
)
comparison
=
result
==
solution
self
.
assertTrue
(
comparison
.
all
())
def
test_remap_large
(
self
):
shape
=
(
20
,
200
)
num
=
np
.
prod
(
shape
)
array
=
np
.
arange
(
num
).
reshape
(
shape
)
keys
=
np
.
arange
(
num
)
values
=
np
.
flip
(
np
.
arange
(
num
))
solution
=
np
.
flip
(
np
.
arange
(
num
)).
reshape
(
shape
)
result
=
self
.
method
(
array
,
keys
,
values
)
comparison
=
result
==
solution
self
.
assertTrue
(
comparison
.
all
())
def
test_remap_incomplete
(
self
):
shape
=
(
4
,
4
)
num
=
np
.
prod
(
shape
)
array
=
np
.
arange
(
num
).
reshape
(
shape
)
keys
=
np
.
arange
(
num
)
values
=
np
.
arange
(
num
-
1
,
-
1
,
-
1
)
keys
=
keys
[
1
:
-
1
]
values
=
values
[
1
:
-
1
]
solution
=
np
.
flip
(
np
.
arange
(
num
)).
reshape
(
shape
)
solution
[
0
,
0
]
=
0
# skipped first value in remap keys
solution
[
-
1
,
-
1
]
=
num
-
1
# skipped first value in remap keys
result
=
self
.
method
(
array
,
keys
,
values
)
comparison
=
result
==
solution
self
.
assertTrue
(
comparison
.
all
())
This diff is collapsed.
Click to expand it.
tfields/lib/sets.py
+
36
−
2
View file @
e3d73b70
...
...
@@ -20,6 +20,7 @@ class UnionFind(object):
ufset.find(obja) != ufset.find(objb)
ufset.union(obja, objb)
"""
def
__init__
(
self
):
"""
Create an empty union find data structure.
...
...
@@ -93,7 +94,7 @@ class UnionFind(object):
for
i
in
sets
.
itervalues
():
if
i
:
out
.
append
(
repr
(
i
))
return
'
,
'
.
join
(
out
)
return
"
,
"
.
join
(
out
)
def
__call__
(
self
,
iterator
):
"""
...
...
@@ -177,6 +178,39 @@ def disjoint_group_indices(iterator):
return
uf
.
group_indices
(
iterator
)
if
__name__
==
'
__main__
'
:
def
remap
(
arr
:
np
.
ndarray
,
keys
:
np
.
ndarray
,
values
:
np
.
ndarray
,
inplace
=
False
)
->
np
.
ndarray
:
"""
Given an input array, remap its entries corresponding to
'
keys
'
to
'
values
'
Args:
input: array to remap
keys: values to be replaced
values : values to replace
'
keys
'
with
Returns:
output:
like
'
input
'
, but with elements remapped according to the mapping
defined by
'
keys
'
and
'
values
'
"""
assert
arr
.
dtype
==
int
assert
arr
.
min
()
>=
0
"""
Assuming the values are between 0 and some maximum integer,
one could implement a fast replace by using the numpy-array as int->int dict, like below
"""
if
not
inplace
:
arr
=
arr
.
copy
()
mp
=
np
.
arange
(
0
,
arr
.
max
()
+
1
)
mp
[
keys
]
=
values
arr
.
ravel
()[:]
=
mp
[
arr
.
ravel
()]
return
arr
if
__name__
==
"
__main__
"
:
import
doctest
doctest
.
testmod
()
This diff is collapsed.
Click to expand it.
tfields/lib/util.py
+
1
−
0
View file @
e3d73b70
...
...
@@ -194,6 +194,7 @@ def index(arr, entry, rtol=0, atol=0, equal_nan=False, axis=None):
"""
Examples:
>>>
import
tfields
>>>
import
numpy
as
np
>>>
a
=
np
.
array
([[
1
,
0
,
0
],
[
1
,
0
,
0
],
[
2
,
3
,
4
]])
>>>
tfields
.
lib
.
util
.
index
(
a
,
[
2
,
3
,
4
],
axis
=
0
)
2
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment