Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
alibylite
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor 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
Swain Lab
aliby
alibylite
Commits
73b8b97f
Commit
73b8b97f
authored
2 years ago
by
Alán Muñoz
Browse files
Options
Downloads
Patches
Plain Diff
feat(agora.utils): add lineage grouping function
parent
245066eb
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/agora/utils/lineage.py
+52
-0
52 additions, 0 deletions
src/agora/utils/lineage.py
with
52 additions
and
0 deletions
src/agora/utils/lineage.py
+
52
−
0
View file @
73b8b97f
#!/usr/bin/env python3
import
re
import
typing
as
t
import
numpy
as
np
import
pandas
as
pd
from
agora.io.bridge
import
groupsort
from
itertools
import
groupby
def
mb_array_to_dict
(
mb_array
:
np
.
ndarray
):
...
...
@@ -25,3 +29,51 @@ def mb_array_to_indices(mb_array: np.ndarray):
return
pd
.
MultiIndex
.
from_arrays
(
mb_array
[:,
:
2
].
T
).
union
(
pd
.
MultiIndex
.
from_arrays
(
mb_array
[:,
[
0
,
2
]].
T
)
)
def
group_matrix
(
matrix
:
np
.
ndarray
,
n_keys
:
int
=
2
,
)
->
t
.
Dict
[
t
.
Tuple
[
int
],
t
.
List
[
int
]]:
"""
Group a matrix of integers by grouping the first two columns
and setting the third one in a list.
Parameters
----------
matrix : np.ndarray
id_matrix, generally its columns are three integers indicating trap,
mother and daughter.
n_keys : int
number of keys to use to determine groups.
Returns
-------
t.Dict[t.Tuple[int], t.Collection[int, ...]]
The column(s) not used for generaeting keys are grouped as values.
Examples
--------
FIXME: Add docs.
"""
lineage_dict
=
{}
if
len
(
matrix
):
daughter
=
matrix
[:,
n_keys
]
mother_global_id
=
matrix
[:,
:
n_keys
]
iterator
=
groupby
(
zip
(
mother_global_id
,
daughter
),
lambda
x
:
str
(
x
[
0
])
)
lineage_dict
=
{
key
:
[
x
[
1
]
for
x
in
group
]
for
key
,
group
in
iterator
}
def
str_to_tuple
(
k
:
str
)
->
t
.
Tuple
[
int
,
...]:
return
tuple
([
int
(
x
)
for
x
in
re
.
findall
(
"
[0-9]+
"
,
k
)])
# Convert keys from str to tuple
lineage_dict
=
{
str_to_tuple
(
k
):
sorted
(
v
)
for
k
,
v
in
lineage_dict
.
items
()
}
return
lineage_dict
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