Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
csst-pipeline
csst_proto
Commits
48da47e8
Commit
48da47e8
authored
Oct 10, 2022
by
BO ZHANG
🏀
Browse files
added parallel mode in example interface
parent
acd0f3cb
Pipeline
#217
passed with stages
in 13 seconds
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
doc/source/csst_common/csst_common.rst
View file @
48da47e8
...
...
@@ -178,7 +178,7 @@ Source code
.. literalinclude:: example_interface.py
:caption: ``example_interface.py``
:emphasize-lines: 7-1
0
,36-4
0
,8
4
,8
6
-8
7
,9
0
-9
1
,9
4
-9
5
,99
,
10
2
,148,15
2
-1
53,156-157,160-161,164-165
:emphasize-lines: 7-1
1
,36-4
1
,8
5
,8
7
-8
8
,9
1
-9
2
,9
5
-9
6
,99
-
10
1,111-116
,148,15
5
-1
65,167-173,178-179,182-183,186-187,190-191
:linenos:
:language: python
...
...
doc/source/csst_common/example_interface.py
View file @
48da47e8
...
...
@@ -4,6 +4,7 @@ from typing import Union
import
numpy
as
np
from
astropy.io
import
fits
import
joblib
from
csst_common.data_manager
import
CsstMsDataManager
from
csst_common.file_recorder
import
FileRecorder
from
csst_common.logger
import
get_logger
...
...
@@ -32,7 +33,7 @@ def check_results(dm: CsstMsDataManager, logger: logging.Logger) -> bool:
return
False
# process a single image
# process a single image
(NOT RECOMMENDED!)
def
process_single_image
(
filepath_input
:
str
,
filepath_output
:
str
,
...
...
@@ -95,15 +96,28 @@ def process_single_image(
return
CsstStatus
.
ERROR
,
fr
# process an exposure (MBI or SLS)
# process multiple images in an exposure (RECOMMENDED, at least for MBI or SLS)
# define a single job
def
one_job
(
dm
,
detector
):
""" Process a single image, defined for parallel processing. """
filepath_input
=
dm
.
l0_detector
(
detector
=
detector
)
filepath_output
=
dm
.
l1_detector
(
detector
=
detector
,
post
=
"L1_processed.fits"
)
data
=
read_image
(
filepath_input
)
data_processed
=
process_data
(
data
)
np
.
save
(
filepath_output
,
data_processed
)
return
# parallel processing jobs
def
process_multiple_images
(
dm
:
CsstMsDataManager
,
logger
:
Union
[
None
,
logging
.
Logger
]
=
None
logger
:
Union
[
None
,
logging
.
Logger
]
=
None
,
n_jobs
:
int
=
-
1
,
)
->
tuple
[
CsstStatus
,
FileRecorder
]:
"""
Flip all images.
Flip all images in an exposure in a for-loop (
not in
parallel).
Flip all images in an exposure in a for-loop (
serial and
parallel).
Parameters
----------
...
...
@@ -111,6 +125,8 @@ def process_multiple_images(
The data manager of the specified exposure.
logger : {None, logging.Logger}
The logger. If None, use the default logger.
n_jobs : int
The number of prcesses.
Returns
-------
...
...
@@ -134,8 +150,9 @@ def process_multiple_images(
# process data
try
:
# dm.target_detectors is a list of detector number that should be processed
# start processing
# start processing (dm.target_detectors is a list of detector number that should be processed)
# [1/2] single-thread mode
for
detector
in
dm
.
target_detectors
:
# this will NOT be written into the log file
logger
.
debug
(
"Processing for detector {}"
.
format
(
detector
))
...
...
@@ -146,6 +163,15 @@ def process_multiple_images(
np
.
save
(
filepath_output
,
data_processed
)
# record file!
fr
.
add_record
(
filepath
=
filepath_output
,
db
=
True
,
comment
=
"processed file for Detector {}"
.
format
(
detector
))
# [2/2] multi-processing mode
joblib
.
Parallel
(
n_jobs
=
n_jobs
,
backend
=
"multiprocessing"
)(
joblib
.
delayed
(
one_job
)(
dm
,
detector
)
for
detector
in
dm
.
target_detectors
)
for
detector
in
dm
.
target_detectors
:
filepath_output
=
dm
.
l1_detector
(
detector
=
detector
,
post
=
"L1_processed.fits"
)
fr
.
add_record
(
filepath
=
filepath_output
,
db
=
True
,
comment
=
"processed file for Detector {}"
.
format
(
detector
))
# check results
if
check_results
(
dm
=
dm
,
logger
=
logger
):
# this will be written into the log file
...
...
Write
Preview
Supports
Markdown
0%
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!
Cancel
Please
register
or
sign in
to comment