Repository navigation
[1/6] Parallel frame generation in generate_observation_sequence - #300
alexisyslau wants to merge 18 commits into
Conversation
f9e9731 to
b35b813
Compare
b35b813 to
89b6925
Compare
sfr348
left a comment
There was a problem hiding this comment.
Logic looks good, small nomenclature change request.
| """ | ||
| Split an observation sequence into segments, in the order the frames are taken. | ||
|
|
||
| A segment is a consecutive block of frames in one visit that share the same optics |
There was a problem hiding this comment.
same back-end optics or front end optics or both?
| - `zindex is None`: the frames of a segment share its optics state. The PSFs are | ||
| computed first, one `_compute_psf` task per segment through `frame_runner` (so the | ||
| segments' PSFs run in parallel). Every frame task is then only the detector step | ||
| (`_apply_detector`). | ||
| - `zindex is not None`: every frame has its own optics state, so every frame task is | ||
| one PROPER run plus the detector step (`_generate_one_frame`). The rows of `zval_m_i` | ||
| follow the frames in sequence order, across all segments. |
There was a problem hiding this comment.
small thing but thoughout can you clarify when 'optics state' refers to the backend optics, the front-end optics, or the optics variable that gets passed to proper?
There was a problem hiding this comment.
perhaps if you want to clarify that is CorgiOptics, you can just called it that: CorgiOptics
| n_frames_total = sum(segment['n_frames'] for segment in segments) | ||
| if zval_m_i is None or len(zval_m_i) != n_frames_total: | ||
| raise ValueError(f'zindex is given, so zval_m_i needs one row per frame of the sequence ({n_frames_total} rows, ' | ||
| 'in the order the frames are taken: satellite spot frames, if any, then science frames)') | ||
|
|
||
| # Each frame has its own optics state (zval_m_i[i]), so each frame is its own PROPER run | ||
| args_list = [] | ||
| for segment in segments: | ||
| for i in range(segment['n_frames']): | ||
| keyword_updates = {'zindex': zindex, 'zval_m': zval_m_i[len(args_list)]} | ||
| args_list.append((scene, segment['optics'], segment['detector'], segment['exp_time'], full_frame, loc_x, loc_y, keyword_updates, seeds[len(args_list)])) | ||
| return _generate_one_frame, args_list |
There was a problem hiding this comment.
note for self to come back to
There was a problem hiding this comment.
This might be in the nested PRs but where do the Zvals come from? If it is from OS11 is there a way to interpolate based on the actual timing between frames? (this doesn't need to be in this PR, just curious of the plan.
There was a problem hiding this comment.
This is linked #246 (built by Alexis Bidot and I am going to finish this afterwards). Essentially we are taking the zernike coefficient from OS11, interpolate between frames, and try to introduce speckle variations under certain conditions. Hence zindex and zval_m in here, since we have a rough idea on how to introduce speckle variation.
| min_step = timedelta(seconds=0.1) | ||
| frame_start = datetime.fromisoformat(simulatedImage_list[0].image_on_detector[1].header['FTIMEUTC']) |
maxwellmb
left a comment
There was a problem hiding this comment.
Please see comments for some requested changes.
Also, how do we approach tests in the stacked PR? I'm surprised not to see any new tests here...
| Generate emccd frame on already defined simulated scene. | ||
|
|
||
| Args: | ||
| sim_scene: output of optics.get_host_star_psf() or inject_point_sources(). |
There was a problem hiding this comment.
Specifically a SimulatedImage object, right?
| satspots_are_present = False, satspots_detector = None, satspots_exptime = None, satspots_number_of_frames = None, satspot_keywords = None, | ||
| vistype = 'CGIVST_TDD_OBS', visit_id= '0200001001001001001', save_as_fits= False, output_dir=None, full_frame= False, loc_x=None, loc_y=None): | ||
|
|
||
| def _apply_detector(sim_scene, detector, exp_time, full_frame, loc_x, loc_y, seed=None): |
There was a problem hiding this comment.
take away _ everywhere please, not needed.
Do we want to make full_frame, and loc_x and loc_y kwargs?
| optics = copy.copy(optics) | ||
| optics.optics_keywords = {**optics.optics_keywords, **(keyword_updates or {})} | ||
| for name, value in (attr_updates or {}).items(): | ||
| setattr(optics, name, value) |
There was a problem hiding this comment.
the current init function does a LOT and it makes me worried that just changing an attribute like this might miss something. What you checked to see if we can do this always?
| return optics | ||
|
|
||
|
|
||
| def _compute_psf(scene, optics): |
There was a problem hiding this comment.
this name is misleading since it also does the point sources.
| - `zindex is None`: the frames of a segment share its optics state. The PSFs are | ||
| computed first, one `_compute_psf` task per segment through `frame_runner` (so the | ||
| segments' PSFs run in parallel). Every frame task is then only the detector step | ||
| (`_apply_detector`). | ||
| - `zindex is not None`: every frame has its own optics state, so every frame task is | ||
| one PROPER run plus the detector step (`_generate_one_frame`). The rows of `zval_m_i` | ||
| follow the frames in sequence order, across all segments. |
There was a problem hiding this comment.
perhaps if you want to clarify that is CorgiOptics, you can just called it that: CorgiOptics
| exp_time (float): The exposure time for each individual frame in seconds. | ||
| n_frames (int): The number of frames to generate in this observation sequence, satellite spots frames not included. | ||
| satspots_present (bool): If yes, the sequence will generate 3xsatspots_number_of_frames satellite spots frames | ||
| satspots_are_present (bool): If True, the sequence also has 3*satspots_number_of_frames satellite spot frames. |
There was a problem hiding this comment.
why change this keyword argument? It might break old scripts.
There was a problem hiding this comment.
satspots_present was changed on main in the def ... but not the docstring for some reasons. Here just changing the docstring to reflect the changes.
corgisim/corgisim/observation.py
Lines 10 to 32 in 5cdb73b
| # satspot and science frames can have different exposure times | ||
| frame_exp_times = [segment['exp_time'] for segment in segments for i in range(segment['n_frames'])] | ||
|
|
||
| if save_as_fits: |
There was a problem hiding this comment.
Is there a reason we don't want frame saving done by the runner?
a2292d9 to
26fa18a
Compare
…th the time seires thing (that trigger get_host_star_psf at each frame) and when we just have a fixed optic keywords
…and ensure reprodcuibility
…update and time series option
…s because the job might hang because of silent dead process
…he server and locally
26fa18a to
fc6c258
Compare
Describe your changes and reference any relevant issues (don't forget the #)
Part of #256. Part 1/6 of a stack
generate_observation_sequencecan run a visit's PSFs and frames in parallel through aframe_runner, with reproducible per-frame seedsmpi_runtime.py: an MPIframe_runner(manager/worker task queue)_simulate_diskand added to every satellite spot and science frame (_add_disk)-n-n; using it in your own scriptMerge bottom-up: #300, #301, #318, #319, #302, #311. Reading the docstring in the example scripts (#302) and #311 docs should help reviewing this PR. Output of the runs in various modes can be found in #302 .
Terms
generate_observation_scenario_from_cpgs). Visits run one after another.visit_id; onegenerate_observation_sequencecall.SATSPOTSflag) and the same detector setup. Up to 4 per visit: satellite spot background, positive, negative, science (which is not the case for time series)What changes
generate_observation_sequence(andgenerate_observation_scenario_from_cpgs, which calls it per visit) get two optional arguments:frame_runner(func, args_list): runs a list of independent tasks and returns their results in order. DefaultNone= serial, in this process (same asmain).functools.partial(run_parallel, n_jobs=n)uses local worker processes; [2/6] MPI frame_runner setups #301 adds an MPI one.seed: base seed for the detector noise (see below). DefaultNone= as onmain.A visit now runs in two rounds through
frame_runner:sequence_segmentsgives each segment its own copy of the optics (the caller's optics is no longer changed byadd_satspot/remove_satspot, which gets from the latest main), andprepare_frame_tasksruns one_compute_psftask per segment, all at once._apply_detectortask (segment PSF + noise), all at once.With
zindex/zval_m_i(speckle variation, one optics state per frame) there is one round: every frame is a_generate_one_frametask (its own PROPER run + detector).zval_m_ihas one row per frame of the whole visit, satellite spot frames first. The time series itself still has to be supplied by the caller. This is not tested as it requires #164 time-series branchesNote: Polarimetry is not parallelised further: the 4 PROPER calls of a POL PSF stay inside
get_host_star_psf.corgisim/parallel_executor.py(new, partly adapted from corgihowfsc):run_parallelwith aspawnpool whose workers may start their own processes (PROPER opens its own pool per PSF).spawn, notfork, so workers don't inherit the parent's RNG state.While the tasks run, it logs progress and memory use (main process, subprocesses, available memory) every minute, and raises a
WorkerDiedErrorwith a memory report if a worker dies (e.g. killed for running out of memory, exit code -9), instead of multiprocessing.Pool hanging forever. An optionaltimeoutraises aTimeoutError. It does not check memory before the run (see #311 for sizing).Seeds
Each frame gets its own seed, derived from the base seed and the frame's position in the visit (
frame_seeds,numpy.random.SeedSequence), so the frames are the same with serial, any number of workers, or MPI, and on every rerun. The CPGS function gives each visit its own sub-seed (SeedSequence(seed).spawn)._apply_detectorseeds numpy's RNG just for that frame and restores the caller's state afterwards. Withseed=Nonenothing is seeded: serial runs behave as onmain; parallel runs are statistically correct but not having the same noise pattern across runs.Behaviour changes compared with
main(please check these - and make sure they are what we want)opticsis not modified by the satellite spot sequence any more (each segment works on a copy); onlyoptics.visit_idandoptics.visit_typeare still set on it.FTIMEUTCis the start of its exposure (t0 + the exposure times of the frames before it) instead of the wall clock, so L1 filenames differ frommain. Frames shorter than 0.1 s (e.g. 0 s satellite spot frames in some CPGS files) are spaced by 0.1 s so filenames stay unique.VISITIDis written to the primary header.satspot_keywordsis now required whensatspots_are_present(aValueErrorotherwise), since we need satspots in thegenerate_observing_sequencefunctionQuestions
mpi4pyChecklist before requesting a review
Checklist before merging
Stack created with GitHub Stacks CLI • Give Feedback 💬
Stack created with GitHub Stacks CLI • Give Feedback 💬