Skip to content

MAINT: update file extension logic#962

Merged
ColmTalbot merged 14 commits into
bilby-dev:mainfrom
mj-will:fix-extensions
Sep 4, 2025
Merged

MAINT: update file extension logic#962
ColmTalbot merged 14 commits into
bilby-dev:mainfrom
mj-will:fix-extensions

Conversation

@mj-will

@mj-will mj-will commented Jun 10, 2025

Copy link
Copy Markdown
Collaborator

Update the file extension logic in Result.save_to_file to match what is described in the doc-string.

The main change is to no longer replace or add an extension when filename is specified. For example filename=test, extension='pkl' will just save to test rather than test.pkl.

I also update read_in_result so that extension isn't overridden.

This consistent with versions <2.4.0 but differs to versions since.

I also added explicit support for h5 and pickle.

Changes to read_in_result

  • Previously, the final if/else statement couldn't actually handle None
  • The extension was always overridden by the value inferred from the file. Now there is only a default (.json) if the filename is not provided. This means the behaviour when filename=None is identical. When filename is specified it is the behaviour is also the same but the error that is raised when extension=None and the filename is missing in extension is slightly different.

@ColmTalbot ColmTalbot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This generally looks good. On testing, it would be good to add some tests of what happens if you attempt to read a file saved with one format with the wrong read_in_result extension.

Comment thread bilby/core/result.py
)
logger.warning(message)

if extension is None:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're adding logging, it would be good to add an info like "No file extension specified, defaulting to JSON."

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I've added a couple more statements.

@ColmTalbot

Copy link
Copy Markdown
Collaborator

Also, I've seen the precommit failure recently, I'm not really sure where it comes from and why main isn't failing.

@mj-will

mj-will commented Jun 11, 2025

Copy link
Copy Markdown
Collaborator Author

This generally looks good. On testing, it would be good to add some tests of what happens if you attempt to read a file saved with one format with the wrong read_in_result extension.

Good idea. Whilst adding these tests I realised it would probably be insightful to actually raise a helpful error, so I've slightly reworked the error handling in read_in_result. Let me know what you think.

@mj-will

mj-will commented Jun 13, 2025

Copy link
Copy Markdown
Collaborator Author

Also, I've seen the precommit failure recently, I'm not really sure where it comes from and why main isn't failing.

I see you've fixed that in #965, hopefully we can this in once that's merged.

@mj-will mj-will requested review from GregoryAshton and asb5468 June 20, 2025 08:51
@mj-will mj-will added the to discuss To be discussed on an upcoming call label Jun 24, 2025
@ColmTalbot ColmTalbot added this to the 2.7.0 milestone Aug 27, 2025
Comment thread test/core/result_test.py Outdated
@ColmTalbot ColmTalbot enabled auto-merge September 4, 2025 14:18
@ColmTalbot ColmTalbot added this pull request to the merge queue Sep 4, 2025
Merged via the queue into bilby-dev:main with commit 72f90f2 Sep 4, 2025
12 checks passed
mj-will added a commit to mj-will/bilby that referenced this pull request Oct 16, 2025
* MAINT: rework file extension logic

* TST: update result saving tests

* BUG: fix extension precedence in `read_in_result`

* TST: assert files can be read in

* MAINT: tweak extension logic in `read_in_result`

* TST: update extensions in tests

* BUG: fix extension default

* BUG: fix logic in `read_in_result`

* TST: improve result save/read tests

* ENH: improve error handling in `read_in_result`

* TST: add tests for file loading with incorrect extensions

* MAINT: tweak IOError message

* TST: fix logging check in caching test

---------

Co-authored-by: Colm Talbot <talbotcolm@gmail.com>
mj-will added a commit to mj-will/bilby that referenced this pull request Feb 19, 2026
* MAINT: rework file extension logic

* TST: update result saving tests

* BUG: fix extension precedence in `read_in_result`

* TST: assert files can be read in

* MAINT: tweak extension logic in `read_in_result`

* TST: update extensions in tests

* BUG: fix extension default

* BUG: fix logic in `read_in_result`

* TST: improve result save/read tests

* ENH: improve error handling in `read_in_result`

* TST: add tests for file loading with incorrect extensions

* MAINT: tweak IOError message

* TST: fix logging check in caching test

---------

Co-authored-by: Colm Talbot <talbotcolm@gmail.com>
isaaclegred pushed a commit to isaaclegred/bilby that referenced this pull request Mar 7, 2026
* MAINT: rework file extension logic

* TST: update result saving tests

* BUG: fix extension precedence in `read_in_result`

* TST: assert files can be read in

* MAINT: tweak extension logic in `read_in_result`

* TST: update extensions in tests

* BUG: fix extension default

* BUG: fix logic in `read_in_result`

* TST: improve result save/read tests

* ENH: improve error handling in `read_in_result`

* TST: add tests for file loading with incorrect extensions

* MAINT: tweak IOError message

* TST: fix logging check in caching test

---------

Co-authored-by: Colm Talbot <talbotcolm@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

result/output to discuss To be discussed on an upcoming call

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants