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

FEAT: add argument to specify calibration correction direction #47

Merged
merged 11 commits into from
Oct 25, 2024

Conversation

ColmTalbot
Copy link
Collaborator

This PR aims to add sufficient flexibility in reading calibration data products to make sure that we correctly map these to the templates in the likelihood. There are probably additional documentation changes that we will want.

@ColmTalbot ColmTalbot marked this pull request as draft October 2, 2024 18:17
@ColmTalbot ColmTalbot requested a review from mj-will October 2, 2024 18:17
@mj-will
Copy link
Collaborator

mj-will commented Oct 7, 2024

We had some discussion about this on the last dev call but not many of us attended. So I'd encourage people to comment.

@asb5468 could you add a comment with what you said on the call?

@ColmTalbot ColmTalbot force-pushed the calibration-reading-fix branch from 73d0e80 to e342d79 Compare October 8, 2024 16:14
@ColmTalbot ColmTalbot marked this pull request as ready for review October 8, 2024 16:25
@farr
Copy link

farr commented Oct 10, 2024

@ColmTalbot FWIW, I like this change, particularly the description and choices in the keyword argument correction='data' or correction='template'. One thing I noticed which is subtle (but, as written, correct!) is in the reading of the calibration uncertainties in phase and amplitude: currently the code assumes the "ordering" of the quantiles given in the calibration file and performs subtraction to determine the sigma parameters in the calibration envelope (ordering reversed for data calibration where you are taking the inverse):

        if correction.lower() == "data":
            amplitude_median = 1 / calibration_data[1] - 1
            phase_median = -calibration_data[2]
            amplitude_sigma = (1 / calibration_data[3] - 1 / calibration_data[5]) / 2
            phase_sigma = (calibration_data[6] - calibration_data[4]) / 2
        else:
            amplitude_median = calibration_data[1] - 1
            phase_median = calibration_data[2]
            amplitude_sigma = (calibration_data[5] - calibration_data[3]) / 2
            phase_sigma = (calibration_data[6] - calibration_data[4]) / 2

I might throw an np.abs(...) around those, just to be robust against changing the ordering (ultimately, it would be nice to have the columns named, instead of indexed, so you can just do (calibration_data['amplitude_q84'] - calibration_data['amplitude_q16'])/2 and don't have to rely on opaque column numbers).

@transientlunatic
Copy link
Collaborator

This lgtm.

def read_calibration_file(filename, frequency_array, number_of_response_curves, starting_index=0):
"""
Function to read the hdf5 files from the calibration group containing the physical calibration response curves.
def read_calibration_file(filename, frequency_array, number_of_response_curves, starting_index=0, correction=None):
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is a minor point, but can the new parameter be called correction_type rather than correction? The latter to me seems like an optional boolean variable, like whether to implement a higher-order correction.

@@ -27,6 +33,9 @@ def read_calibration_file(filename, frequency_array, number_of_response_curves,
starting_index: int
Index of the first curve to use within the array. This allows for segmenting the calibration curve array
into smaller pieces.
correction: str
How the correction is defined, either to the data (default) or
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd suggest either (i) we make correction='data' instead of None, or (ii) change the text here to say something like traditional or usual rather than default. I suspect (ii) is better, as we want to avoid cases where people don't know which one they are doing.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Is the issue here that None is mapped internally to data ad so data technically isn't the default value? I'm not really concerned about that as practically the default is data.

@@ -82,10 +110,33 @@ def write_calibration_file(filename, frequency_array, calibration_draws, calibra
Shape is (number_of_response_curves x len(frequency_array))
calibration_parameter_draws: data_frame
Parameters used to generate the random draws of the calibration response curves
correction: str
How the correction is defined, either to the data (default) or
Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy link
Collaborator

@adivijaykumar adivijaykumar left a comment

Choose a reason for hiding this comment

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

Broadly LGTM! I have left a few comments; I also agree with suggestions raised by Sylvia and Will.

@@ -1138,7 +1138,7 @@ def to_file(self, outdir, label):
@staticmethod
def from_envelope_file(envelope_file, minimum_frequency,
maximum_frequency, n_nodes, label,
boundary="reflective"):
boundary="reflective", correction=None):
Copy link
Contributor

@jacobgolomb jacobgolomb Oct 10, 2024

Choose a reason for hiding this comment

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

Agree with correction -> correction_type, but also I think the default in the signature should be correction_type='data' and there should just always be a logger message saying saying what it is doing.

@mj-will
Copy link
Collaborator

mj-will commented Oct 10, 2024

There are a few comments about whether the default should be None or 'data'. FWIW, I'm in favour of keeping it as None for now with a warning, as it makes it very clear that you should be setting it.

In future, I'd be in favour of going a step further and either making it a keyword argument without a default, meaning the function cannot be called without specifying the type of correction being used. This would break any codes which do not set it and avoid potential issues due to assumed behaviour/ignoring warnings.

@ColmTalbot
Copy link
Collaborator Author

(ultimately, it would be nice to have the columns named, instead of indexed, so you can just do (calibration_data['amplitude_q84'] - calibration_data['amplitude_q16'])/2 and don't have to rely on opaque column numbers).

@farr I agree in principle, in practice, parsing the header of these files is sufficiently difficult that I'd like to at least leave that to a follow up.

@ColmTalbot
Copy link
Collaborator Author

Thanks everyone for the comments! I think I've addressed most of the questions here. The major outstanding point is essentially, should the code always emit a message when reading in the calibration envelopes? Currently, I've set this as a debug statement, so people can see it if they like, but it won't always be printed by default. My main argument for this is that if people have specified the method, then telling them which method is being used is just noise.

As a reminder, if users don't specify an option they will get a warning, and probably an error in future versions.

Can people react thumbs up for keeping as debug or thumbs down for making this a logger.info?

@ColmTalbot
Copy link
Collaborator Author

ColmTalbot commented Oct 14, 2024

@hoyc1 @SimonStevenson please can you look at this PR if you have a chance?

ColmTalbot added a commit to ColmTalbot/bilby that referenced this pull request Oct 14, 2024
@ColmTalbot ColmTalbot force-pushed the calibration-reading-fix branch from 319d0d7 to 9204055 Compare October 24, 2024 23:36
@ColmTalbot ColmTalbot merged commit 1a1868a into bilby-dev:main Oct 25, 2024
10 checks passed
@ColmTalbot
Copy link
Collaborator Author

This is now merged, thanks everyone for the feedback!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants