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

864 hi l1b compute coincidence type and time deltas #995

Conversation

subagonsouth
Copy link
Contributor

Change Summary

Overview

Adds the Hi L1B algorithm for computing coincidence type and delta_t values. These fields share enough commonality in computation that I decided to implement them in a single function.

Updated Files

  • imap_processing/hi/l1b/hi_l1b.py
    • Added function for computing L1B coincidence_type and delta_t variables
    • Coincidence type and delta_t variables are now added to the dataset within that function.
  • imap_processing/hi/utils.py
    • Added hi constants frozen dataclass similar to how other instruments have implemented
  • imap_processing/tests/hi/test_hi_l1b.py
    • Added fixture that generates synthetic L1A data for testing coincidence_type and delta_t algorithm
    • Added test coverage for compute_coincidence_type_and_time_deltas function

Closes: #864

@subagonsouth subagonsouth added Ins: Hi Related to the IMAP-Hi instrument Level: L1 Level 1 processing labels Oct 8, 2024
@subagonsouth subagonsouth added this to the Oct 2024 milestone Oct 8, 2024
@subagonsouth subagonsouth requested review from laspsandoval and a team October 8, 2024 20:22
@subagonsouth subagonsouth self-assigned this Oct 8, 2024
@subagonsouth subagonsouth requested review from bourque, sdhoyt and tech3371 and removed request for a team October 8, 2024 20:23
@subagonsouth subagonsouth force-pushed the 864-hi-l1b-compute-coincidence_type-and-time-deltas branch from a7c06ec to 87215f8 Compare October 8, 2024 20:26
@subagonsouth subagonsouth removed the request for review from bourque October 9, 2024 15:20
Copy link
Contributor

@sdhoyt sdhoyt left a comment

Choose a reason for hiding this comment

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

Looks great! Just hoping to get some clarification on how to read those tables in the comments

# | 2 | B | A | C1 | C2 |
# | 3 | C1 | A | B | C2 |
# Set coincidence type bitmask
out_ds.coincidence_type[a_mask | tof_1_valid_mask] |= CoincidenceBitmap.A.value
Copy link
Contributor

Choose a reason for hiding this comment

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

I think because you're using an IntEnum you can do CoincidenceBitmap.A instead of CoincidenceBitmap.A.value

imap_processing/hi/l1b/hi_l1b.py Show resolved Hide resolved
def synthetic_trigger_id_and_tof_data():
"""Create synthetic minimum dataset for testing the
coincidence_type_and_time_deltas algorithm."""
# The following coincidence type table shows possible values to consider
Copy link
Contributor

Choose a reason for hiding this comment

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

This table is really helpful! I should add something similar for Lo's coincidence types

tof2s = np.array(np.concatenate((HiConstants.TOF2_BAD_VALUES, [2])))
tof3s = np.array(np.concatenate((HiConstants.TOF3_BAD_VALUES, [3])))
var_names = ["trigger_id", "tof_1", "tof_2", "tof_3"]
data = np.meshgrid(ids, tof1s, tof2s, tof3s)
Copy link
Contributor

Choose a reason for hiding this comment

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

Great idea to more easily cover the combinations!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can't quite take credit... found it on SO.

Simplify C1 coincidence type mask logic
Remove .value from IntEnums where not needed
Copy link
Contributor

@laspsandoval laspsandoval left a comment

Choose a reason for hiding this comment

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

I think that programmatically everything looks in order. My suggestion would be to add more docstrings, but you can take it or leave it.

docs/source/conf.py Show resolved Hide resolved
imap_processing/tests/hi/test_hi_l1b.py Outdated Show resolved Hide resolved
imap_processing/hi/l1b/hi_l1b.py Outdated Show resolved Hide resolved
-out_ds.tof_1.values[b_mask & tof_1_valid_mask].astype(np.float32)
* HiConstants.TOF1_TICK_PER_NS
)
out_ds.delta_t_ab.values[c_mask & tof_1and2_valid_mask] = (
Copy link
Contributor

Choose a reason for hiding this comment

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

I think that this could be split into two separate steps for clarity:

delta_t_2 = out_ds.tof_2.values[c_mask & tof_1and2_valid_mask].astype(np.float32) * HiConstants.TOF2_TICK_PER_NS
delta_t_1 = out_ds.tof_1.values[c_mask & tof_1and2_valid_mask].astype(np.float32) * HiConstants.TOF1_TICK_PER_NS
out_ds.delta_t_ab.values[c_mask & tof_1and2_valid_mask] = delta_t_2 - delta_t_1

out_ds.tof_2.values[a_mask & tof_2_valid_mask].astype(np.float32)
* HiConstants.TOF2_TICK_PER_NS
)
out_ds.delta_t_ac1.values[b_mask & tof_1and2_valid_mask] = (
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this could be split for clarity:

delta_t_2 = out_ds.tof_2.values[b_mask & tof_1and2_valid_mask] * HiConstants.TOF2_TICK_PER_NS
delta_t_1 = out_ds.tof_1.values[b_mask & tof_1and2_valid_mask] * HiConstants.TOF1_TICK_PER_NS
out_ds.delta_t_ac1.values[b_mask & tof_1and2_valid_mask] = delta_t_2 - delta_t_1

)

# delta_t_bc1
out_ds.delta_t_bc1.values[a_mask & tof_1_valid_mask & tof_2_valid_mask] = (
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here I would split it up:
delta_t_2 = out_ds.tof_2.values[a_mask & tof_1and2_valid_mask] * HiConstants.TOF2_TICK_PER_NS
delta_t_1 = out_ds.tof_1.values[a_mask & tof_1and2_valid_mask] * HiConstants.TOF1_TICK_PER_NS
out_ds.delta_t_bc1.values[a_mask & tof_1_valid_mask & tof_2_valid_mask] = delta_t_2 - delta_t_1

# | 2 | B | A,B | B,C1 | C1,C2 |
# | 3 | C1 | A,C1 | B,C1 | C1,C2 |
# Set coincidence type bitmask
out_ds.coincidence_type[a_mask | tof_1_valid_mask] |= CoincidenceBitmap.A
Copy link
Contributor

Choose a reason for hiding this comment

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

For me personally I would really appreciate a docstring on each bitmask. Maybe it's overkill for some people, but it helps me to immediately understand the purpose.

Example:
Set the 'A' bit in the 'coincidence_type' variable where:

  • The trigger is 'A' (using a_mask), OR
  • tof_1 is valid (using tof_1_valid_mask).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My philosophy is that if you need a bunch of comments to explain your code, then the code is not written in a way that it can be clearly understood. IMO, the comments you wrote are exactly what the code says with a slightly different order. For this instance, I think the comment is redundant.

Rework some of the code to make it more readable
@subagonsouth
Copy link
Contributor Author

I think that programmatically everything looks in order. My suggestion would be to add more docstrings comments, but you can take it or leave it.

@laspsandoval, I took your feedback about needing more comments to mean that my code was not easily understandable. I made some minor tweaks that make it much more readable, IMO. I also added some comments and think that it should be very clear now.

Copy link
Contributor

@sdhoyt sdhoyt left a comment

Choose a reason for hiding this comment

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

Thanks for the walkthrough of all this. I think everything looks good!

@subagonsouth subagonsouth merged commit cbb9132 into IMAP-Science-Operations-Center:dev Oct 14, 2024
17 checks passed
@subagonsouth subagonsouth deleted the 864-hi-l1b-compute-coincidence_type-and-time-deltas branch October 14, 2024 17:58
@tech3371 tech3371 removed this from the Oct 2024 milestone Oct 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Ins: Hi Related to the IMAP-Hi instrument Level: L1 Level 1 processing
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Hi l1b - compute coincidence_type and time deltas
4 participants