Skip to content

Commit

Permalink
add parallel tqdm
Browse files Browse the repository at this point in the history
  • Loading branch information
mcoughlin committed May 30, 2024
1 parent 60a74e0 commit 99b715c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
3 changes: 2 additions & 1 deletion gwemopt/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ def get_order_heuristic(
being the exposure time.
Returns a list of tile indices in the order of observation.
"""

keys = tile_struct.keys()

exposureids_tiles = {}
Expand Down Expand Up @@ -205,7 +206,7 @@ def get_order_heuristic(
idxs = -1 * np.ones((len(exposureids_tiles.keys()),))
filts = ["n"] * len(exposureids_tiles.keys())

if nexps == 0:
if (nexps == 0) or (len(exposurelist) == 0):
return idxs, filts

if params["scheduleType"] == "airmass_weighted":
Expand Down
3 changes: 3 additions & 0 deletions gwemopt/segments.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ def get_telescope_segments(params):

def get_moon_radecs(segmentlist, observer):
dt = 1.0 / 24.0
if len(segmentlist) == 0:
return []

tt = np.arange(segmentlist[0][0], segmentlist[-1][1] + dt, dt)
tt_DJD = tt - MJD_TO_DJD

Expand Down
22 changes: 22 additions & 0 deletions gwemopt/utils/parallel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import contextlib

import joblib
from tqdm import tqdm


@contextlib.contextmanager
def tqdm_joblib(tqdm_object):
"""Context manager to patch joblib to report into tqdm progress bar given as argument"""

class TqdmBatchCompletionCallback(joblib.parallel.BatchCompletionCallBack):
def __call__(self, *args, **kwargs):
tqdm_object.update(n=self.batch_size)
return super().__call__(*args, **kwargs)

old_batch_callback = joblib.parallel.BatchCompletionCallBack
joblib.parallel.BatchCompletionCallBack = TqdmBatchCompletionCallback
try:
yield tqdm_object
finally:
joblib.parallel.BatchCompletionCallBack = old_batch_callback
tqdm_object.close()

0 comments on commit 99b715c

Please sign in to comment.