Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
LISA Instrument
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
LISA Simulation
LISA Instrument
Commits
b365b1d9
Commit
b365b1d9
authored
2 years ago
by
Jean-Baptiste Bayle
Browse files
Options
Downloads
Patches
Plain Diff
Update containers to allow for concurrency
parent
cbd12911
No related branches found
No related tags found
1 merge request
!134
Parallelize MOSA and SC quantities
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
lisainstrument/containers.py
+13
-4
13 additions, 4 deletions
lisainstrument/containers.py
with
13 additions
and
4 deletions
lisainstrument/containers.py
+
13
−
4
View file @
b365b1d9
...
...
@@ -9,6 +9,7 @@ Authors:
import
abc
import
logging
from
concurrent.futures
import
ThreadPoolExecutor
import
h5py
import
numpy
as
np
import
matplotlib.pyplot
as
plt
...
...
@@ -19,16 +20,23 @@ logger = logging.getLogger(__name__)
class
ForEachObject
(
abc
.
ABC
):
"""
Abstract class which represents a dictionary holding a value for each object.
"""
def
__init__
(
self
,
values
):
def
__init__
(
self
,
values
,
concurrent
=
False
):
"""
Initialize an object with a value or a function of the mosa index.
Args:
values: a value, a dictionary of values, or a function (mosa -> value)
concurrent (bool): whether to parallelize using a thread pool
"""
if
isinstance
(
values
,
dict
):
self
.
dict
=
{
mosa
:
values
[
mosa
]
for
mosa
in
self
.
indices
()}
elif
callable
(
values
):
self
.
dict
=
{
mosa
:
values
(
mosa
)
for
mosa
in
self
.
indices
()}
if
concurrent
:
with
ThreadPoolExecutor
()
as
executor
:
indices
=
self
.
indices
()
computed_values
=
executor
.
map
(
values
,
indices
)
self
.
dict
=
dict
(
zip
(
indices
,
computed_values
))
else
:
self
.
dict
=
{
mosa
:
values
(
mosa
)
for
mosa
in
self
.
indices
()}
elif
isinstance
(
values
,
h5py
.
Dataset
):
self
.
dict
=
{
mosa
:
values
[
mosa
]
for
mosa
in
self
.
indices
()}
else
:
...
...
@@ -40,13 +48,14 @@ class ForEachObject(abc.ABC):
"""
Return list of object indices.
"""
raise
NotImplementedError
def
transformed
(
self
,
transformation
):
def
transformed
(
self
,
transformation
,
concurrent
=
False
):
"""
Return a new dictionary from transformed objects.
Args:
transformation: function (mosa, value -> new_value)
concurrent (bool): whether to parallelize using a thread pool
"""
return
self
.
__class__
(
lambda
mosa
:
transformation
(
mosa
,
self
[
mosa
]))
return
self
.
__class__
(
lambda
mosa
:
transformation
(
mosa
,
self
[
mosa
])
,
concurrent
)
def
collapsed
(
self
):
"""
Turn a numpy arrays containing identical elements into a scalar.
...
...
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