Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update github main #32

Merged
merged 15 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 48 additions & 48 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ authors:
# Test containers scripts are up to date
containers:
stage: initial
image: containers.ligo.org/lscsoft/bilby/v2-bilby-python310
image: containers.ligo.org/lscsoft/bilby/v2-bilby-python311
script:
- cd containers
- python write_dockerfiles.py #HACK
# Fail if differences exist. If this fails, you may need to run
# write_dockerfiles.py and commit the changes.
- git diff --exit-code
- cp env-template.yml env.yml
- echo " - python=3.10" >> env.yml
- echo " - python=3.11" >> env.yml
- mamba env create -f env.yml -n test --dry-run

.test-python: &test-python
Expand All @@ -70,10 +70,6 @@ containers:
${script} --help;
done

basic-3.9:
<<: *test-python
image: python:3.9

basic-3.10:
<<: *test-python
image: python:3.10
Expand All @@ -82,17 +78,17 @@ basic-3.11:
<<: *test-python
image: python:3.11

basic-3.12:
<<: *test-python
image: python:3.12

.test-samplers-import: &test-samplers-import
stage: initial
script:
- python -m pip install .
- *list-env
- pytest test/test_samplers_import.py -v

import-samplers-3.9:
<<: *test-samplers-import
image: containers.ligo.org/lscsoft/bilby/v2-bilby-python39

import-samplers-3.10:
<<: *test-samplers-import
image: containers.ligo.org/lscsoft/bilby/v2-bilby-python310
Expand All @@ -101,6 +97,10 @@ import-samplers-3.11:
<<: *test-samplers-import
image: containers.ligo.org/lscsoft/bilby/v2-bilby-python311

import-samplers-3.12:
<<: *test-samplers-import
image: containers.ligo.org/lscsoft/bilby/v2-bilby-python312

.precommits: &precommits
stage: initial
script:
Expand All @@ -112,19 +112,19 @@ import-samplers-3.11:
# Run precommits (flake8, spellcheck, isort, no merge conflicts, etc)
- pre-commit run --all-files --verbose --show-diff-on-failure

precommits-py3.10:
precommits-py3.11:
<<: *precommits
image: containers.ligo.org/lscsoft/bilby/v2-bilby-python310
image: containers.ligo.org/lscsoft/bilby/v2-bilby-python311
variables:
CACHE_DIR: ".pip310"
PYVERSION: "python310"
CACHE_DIR: ".pip311"
PYVERSION: "python311"

install:
stage: initial
parallel:
matrix:
- EXTRA: [gw, mcmc, all]
image: containers.ligo.org/lscsoft/bilby/v2-bilby-python310
image: containers.ligo.org/lscsoft/bilby/v2-bilby-python311
script:
- pip install .[$EXTRA]

Expand All @@ -138,15 +138,15 @@ install:

- pytest --cov=bilby --durations 10

python-3.9:
<<: *unit-test
needs: ["basic-3.9"]
image: containers.ligo.org/lscsoft/bilby/v2-bilby-python39

python-3.10:
<<: *unit-test
needs: ["basic-3.10", "precommits-py3.10"]
needs: ["basic-3.10"]
image: containers.ligo.org/lscsoft/bilby/v2-bilby-python310

python-3.11:
<<: *unit-test
needs: ["basic-3.11", "precommits-py3.11"]
image: containers.ligo.org/lscsoft/bilby/v2-bilby-python311
after_script:
- coverage html
- coverage xml
Expand All @@ -160,10 +160,10 @@ python-3.10:
- htmlcov/
expire_in: 30 days

python-3.11:
python-3.12:
<<: *unit-test
needs: ["basic-3.11"]
image: containers.ligo.org/lscsoft/bilby/v2-bilby-python311
needs: ["basic-3.12"]
image: containers.ligo.org/lscsoft/bilby/v2-bilby-python312

.test-sampler: &test-sampler
stage: test
Expand All @@ -172,25 +172,25 @@ python-3.11:
- *list-env
- pytest test/integration/sampler_run_test.py --durations 10 -v

python-3.9-samplers:
<<: *test-sampler
needs: ["basic-3.9"]
image: containers.ligo.org/lscsoft/bilby/v2-bilby-python39

python-3.10-samplers:
<<: *test-sampler
needs: ["basic-3.10", "precommits-py3.10"]
needs: ["basic-3.10"]
image: containers.ligo.org/lscsoft/bilby/v2-bilby-python310

python-3.11-samplers:
<<: *test-sampler
needs: ["basic-3.11"]
needs: ["basic-3.11", "precommits-py3.11"]
image: containers.ligo.org/lscsoft/bilby/v2-bilby-python311

integration-tests-python-3.10:
python-3.12-samplers:
<<: *test-sampler
needs: ["basic-3.12"]
image: containers.ligo.org/lscsoft/bilby/v2-bilby-python312

integration-tests-python-3.11:
stage: test
image: containers.ligo.org/lscsoft/bilby/v2-bilby-python310
needs: ["basic-3.10", "precommits-py3.10"]
image: containers.ligo.org/lscsoft/bilby/v2-bilby-python311
needs: ["basic-3.11", "precommits-py3.11"]
only:
- schedules
script:
Expand All @@ -208,26 +208,26 @@ integration-tests-python-3.10:
- *list-env
- pytest test/gw/plot_test.py

plotting-python-3.9:
<<: *plotting
image: containers.ligo.org/lscsoft/bilby/v2-bilby-python39
needs: ["basic-3.9"]

plotting-python-3.10:
<<: *plotting
image: containers.ligo.org/lscsoft/bilby/v2-bilby-python310
needs: ["basic-3.10", "precommits-py3.10"]
needs: ["basic-3.10"]

plotting-python-3.11:
<<: *plotting
image: containers.ligo.org/lscsoft/bilby/v2-bilby-python311
needs: ["basic-3.11"]
needs: ["basic-3.11", "precommits-py3.11"]

plotting-python-3.12:
<<: *plotting
image: containers.ligo.org/lscsoft/bilby/v2-bilby-python312
needs: ["basic-3.12"]

# ------------------- Docs stage -------------------------------------------

docs:
stage: docs
image: containers.ligo.org/lscsoft/bilby/v2-bilby-python310
image: containers.ligo.org/lscsoft/bilby/v2-bilby-python311
before_script:
- python -m ipykernel install
script:
Expand All @@ -248,7 +248,7 @@ docs:

pages:
stage: deploy
needs: ["docs", "python-3.10"]
needs: ["docs", "python-3.11"]
script:
- mkdir public/
- mv htmlcov/ public/
Expand Down Expand Up @@ -280,11 +280,6 @@ pages:
- docker image tag v3-bilby-$PYVERSION containers.ligo.org/lscsoft/bilby/v2-bilby-$PYVERSION:latest
- docker image push containers.ligo.org/lscsoft/bilby/v2-bilby-$PYVERSION:latest

build-python39-container:
<<: *build-container
variables:
PYVERSION: "python39"

build-python310-container:
<<: *build-container
variables:
Expand All @@ -295,6 +290,11 @@ build-python311-container:
variables:
PYVERSION: "python311"

build-python312-container:
<<: *build-container
variables:
PYVERSION: "python312"

pypi-release:
stage: deploy
image: containers.ligo.org/lscsoft/bilby/v2-bilby-python310
Expand Down
1 change: 1 addition & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Bruce Edelman
Carl-Johan Haster
Cecilio Garcia-Quiros
Charlie Hoy
Chentao Yang
Christopher Philip Luke Berry
Christos Karathanasis
Colm Talbot
Expand Down
4 changes: 3 additions & 1 deletion bilby/bilby_mcmc/sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,9 @@ def read_current_state(self):
If true, resume file was successfully loaded, otherwise false

"""
if os.path.isfile(self.resume_file) is False:
if os.path.isfile(self.resume_file) is False or not os.path.getsize(
self.resume_file
):
return False
import dill

Expand Down
2 changes: 1 addition & 1 deletion bilby/core/sampler/base_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ def _time_likelihood(self, n_evaluations=100):
logger.info("Unable to measure single likelihood time")
else:
logger.info(
f"Single likelihood evaluation took {self._log_likelihood_eval_time:.3e} s"
f"Single likelihood evaluation took {log_likelihood_eval_time:.3e} s"
)
return log_likelihood_eval_time

Expand Down
19 changes: 16 additions & 3 deletions bilby/core/sampler/dynesty.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import sys
import time
import warnings

import numpy as np
from pandas import DataFrame
Expand Down Expand Up @@ -677,12 +678,21 @@ def _run_external_sampler_with_checkpointing(self):
chain of nested samples within dynesty and have to be removed before
restarting the sampler.
"""

logger.debug("Running sampler with checkpointing")

old_ncall = self.sampler.ncall
sampler_kwargs = self.sampler_function_kwargs.copy()
warnings.filterwarnings(
"ignore",
message="The sampling was stopped short due to maxiter/maxcall limit*",
category=UserWarning,
module="dynesty.sampler",
)
while True:
self.finalize_sampler_kwargs(sampler_kwargs)
if getattr(self.sampler, "added_live", False):
self.sampler._remove_live_points()
self.sampler.run_nested(**sampler_kwargs)
if self.sampler.ncall == old_ncall:
break
Expand All @@ -697,8 +707,8 @@ def _run_external_sampler_with_checkpointing(self):
if last_checkpoint_s > self.check_point_delta_t:
self.write_current_state()
self.plot_current_state()
if getattr(self.sampler, "added_live", False):
self.sampler._remove_live_points()
if getattr(self.sampler, "added_live", False):
self.sampler._remove_live_points()

self.sampler.run_nested(**sampler_kwargs)
self.write_current_state()
Expand Down Expand Up @@ -736,7 +746,10 @@ def read_saved_state(self, continuing=False):
if os.path.isfile(self.resume_file):
logger.info(f"Reading resume file {self.resume_file}")
with open(self.resume_file, "rb") as file:
sampler = dill.load(file)
try:
sampler = dill.load(file)
except EOFError:
sampler = None

if not hasattr(sampler, "versions"):
logger.warning(
Expand Down
22 changes: 14 additions & 8 deletions bilby/core/sampler/emcee.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import os
import shutil
from collections import namedtuple
from shutil import copyfile

import numpy as np
from packaging import version
Expand Down Expand Up @@ -311,7 +310,11 @@ def sampler(self):
"""
if hasattr(self, "_sampler"):
pass
elif self.resume and os.path.isfile(self.checkpoint_info.sampler_file):
elif (
self.resume
and os.path.isfile(self.checkpoint_info.sampler_file)
and os.path.getsize(self.checkpoint_info.sampler_file)
):
import dill

logger.info(
Expand All @@ -329,16 +332,19 @@ def sampler(self):
def write_chains_to_file(self, sample):
chain_file = self.checkpoint_info.chain_file
temp_chain_file = chain_file + ".temp"
if os.path.isfile(chain_file):
copyfile(chain_file, temp_chain_file)
if self.prerelease:
points = np.hstack([sample.coords, sample.blobs])
else:
points = np.hstack([sample[0], np.array(sample[3])])
with open(temp_chain_file, "a") as ff:
for ii, point in enumerate(points):
ff.write(self.checkpoint_info.chain_template.format(ii, *point))
shutil.move(temp_chain_file, chain_file)
data_to_write = "\n".join(
self.checkpoint_info.chain_template.format(ii, *point)
for ii, point in enumerate(points)
)
with open(temp_chain_file, "w") as ff:
ff.write(data_to_write)
with open(temp_chain_file, "rb") as ftemp, open(chain_file, "ab") as fchain:
shutil.copyfileobj(ftemp, fchain)
os.remove(temp_chain_file)

@property
def _previous_iterations(self):
Expand Down
6 changes: 5 additions & 1 deletion bilby/core/sampler/kombine.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,11 @@ def sampler_chain(self):
return self.sampler.chain[:nsteps, :, :]

def check_resume(self):
return self.resume and os.path.isfile(self.checkpoint_info.sampler_file)
return (
self.resume
and os.path.isfile(self.checkpoint_info.sampler_file)
and os.path.getsize(self.checkpoint_info.sampler_file) > 0
)

@signal_wrapper
def run_sampler(self):
Expand Down
8 changes: 6 additions & 2 deletions bilby/core/sampler/ptemcee.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,11 @@ def setup_sampler(self):
# This is a very ugly hack to support numpy>=1.24
ptemcee.sampler.np.float = float

if os.path.isfile(self.resume_file) and self.resume is True:
if (
os.path.isfile(self.resume_file)
and os.path.getsize(self.resume_file)
and self.resume is True
):
import dill

logger.info(f"Resume data {self.resume_file} found")
Expand Down Expand Up @@ -513,7 +517,7 @@ def run_sampler(self):
logger.info("Starting to sample")

while True:
for (pos0, log_posterior, log_likelihood) in sampler.sample(
for pos0, log_posterior, log_likelihood in sampler.sample(
self.pos0,
storechain=False,
iterations=self.convergence_inputs.niterations_per_check,
Expand Down
Loading
Loading