Skip to content

Conversation

@ahmadtourei
Copy link
Collaborator

Description

This PR adds notch filtering functionality for Patch as discussed in #443.

Checklist

I have (if applicable):

  • referenced the GitHub issue this PR closes.
  • documented the new feature with docstrings or appropriate doc page.
  • included a test. See testing guidelines.
  • your name has been added to the contributors page (docs/contributors.md).
  • added the "ready_for_review" tag once the PR is ready to be reviewed.

@ahmadtourei
Copy link
Collaborator Author

ahmadtourei commented Oct 16, 2024

Hey @d-chambers, please review and let me know what you think at your convenience. Also, I wonder how I can adjust the _create_size_fw_and_axes(patch, kwargs) function so it can accept specified units as well (please see two commented tests in tests/test_proc/test_filter.py) Thanks!

@codecov
Copy link

codecov bot commented Oct 16, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.84%. Comparing base (e6ddf30) to head (a82f18f).
Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #448   +/-   ##
=======================================
  Coverage   99.84%   99.84%           
=======================================
  Files         114      114           
  Lines        9184     9203   +19     
=======================================
+ Hits         9170     9189   +19     
  Misses         14       14           
Flag Coverage Δ
unittests 99.84% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@d-chambers d-chambers left a comment

Choose a reason for hiding this comment

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

Looks ok, just a few things to tweak on the docstring stuff.

patch
The patch to filter
samples
{sample_explination}
Copy link
Contributor

Choose a reason for hiding this comment

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

Where is sample_explination coming from? You would have to use the compose_docstring in utils.docs to make this work. Also, I don't really see how samples is to be used in this function. Could you maybe add an example?

Copy link
Collaborator Author

@ahmadtourei ahmadtourei Oct 18, 2024

Choose a reason for hiding this comment

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

Since this is a filter, we actually do not need the samples argument. So, I deleted this from docs.

@d-chambers
Copy link
Contributor

d-chambers commented Oct 17, 2024

Also, I wonder how I can adjust the _create_size_fw_and_axes(patch, kwargs) function so it can accept specified units as well (please see two commented tests in tests/test_proc/test_filter.py) Thanks!

Probably the easiest thing to do for the units is to pull get_inverted_quant from being a nested function to being on the dascore.units level. Then you can do something like this (but I haven't tested it yet):

from dascore.units import get_inverted_quant
from dascore.utils.time import to_float

size, axes = _create_size_fw_and_axes(patch, kwargs)
data = patch.data
for coord_name, info in get_multiple_dim_value_from_kwargs(patch, kwargs).items():
    dim, ax, value = info['dim'], info['axis'], info['value']
    coord = patch.get_coord(coord_name, required_evenly_sampled=True)  # will also raise if not evenly sampled.

    # Invert units if needed
    inverted_units = False
    if isinstance(value, dc.units.Quantity) and coord.units is not None:
        value, inverted_units = get_inverted_quant(value, coord.units)
    
    # Check valid parameters
    sr, value = to_float(coord), to_float(value)
    nyquist = 0.5 * sr 
    if w0 > nyquist:
        msg = f"possible filter values are in [0, {nyquist}] you passed {w0}"
        raise FilterValueError(msg)

    b, a = iirnotch(value, Q=q, fs=sr)
    data = filtfilt(b, a, data, axis=ax)

note: the imports should be at the top of the module, just put them here for reference.

@ahmadtourei
Copy link
Collaborator Author

        value, inverted_units = get_inverted_quant(value, coord.units)

Thanks for this explanation, Derrick! I'm unsure why the inverted_units is being reported False when the get_inverted_quant actually has inverted the unit! Do we need to fix this inside the get_inverted_quant function?

@d-chambers
Copy link
Contributor

Do we need to fix this inside the get_inverted_quant function?

So the function returns the correctly inverted quantity but just the boolean is wrong? Yes, I suppose we should fix it, hopefully it hasn't caused issues elsewhere.

Sorry about the linter failing. It has to do with newer versions of to pip I think. I will push a fix in a few minutes.

@d-chambers
Copy link
Contributor

So the function returns the correctly inverted quantity but just the boolean is wrong? Yes, I suppose we should fix it, hopefully it hasn't caused issues elsewhere.

Actually, looking closer I think it is correct, just a bit confusing. With filtering we do expect the dimensionality of inputs to the be inverse of the coordinate units. For example, for the time dimension (seconds) we expect the filter parameters to be the multiplicative inverse dimensionality (1/seconds, Hz). So the second parameter returned from this function just indicates if the dimensionality of the filter argument were already the inverse dimensionality. Its probably safe to just ignore the second output parameter.

Copy link
Contributor

@d-chambers d-chambers left a comment

Choose a reason for hiding this comment

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

Looks good @ahmadtourei, just double check the unit inversion stuff is right then merge when you are happy with it.

Comment on lines 311 to 315
# Check valid parameters
if inverted_units:
w0 = to_float(value)
else:
w0 = to_float(1 / value)
Copy link
Contributor

Choose a reason for hiding this comment

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

hmmm... its a bit odd this is required. Doesn't get_inverted_quant already do this?

Copy link
Contributor

Choose a reason for hiding this comment

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

Specifically here.

Copy link
Collaborator Author

@ahmadtourei ahmadtourei Oct 19, 2024

Choose a reason for hiding this comment

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

Thanks for looking into this more closely. I did not initially understand the behavior of the get_inverted_quant function well. We actually don't need this. Please see my last commit (changed examples and a test) for further details.

@ahmadtourei ahmadtourei merged commit 7649ac4 into master Oct 28, 2024
@ahmadtourei
Copy link
Collaborator Author

closes #443.

@ahmadtourei ahmadtourei deleted the notch_func branch October 28, 2024 23:04
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.

3 participants