-
Notifications
You must be signed in to change notification settings - Fork 358
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
Integration of pygrb aligntotalspin functionality into pycbc/transforms.py as a class #3756
Conversation
I should add this is my first time doing a pull request or contributing here so please feel free to leave comments on if I did anything wrong with my request too |
@SamuelH-97 Thanks for providing a first PR! It looks like you've set up everything correctly, and I've just approved the test code to run. I'll ask @cdcapano if he would be willing to look over the content here, as this is something he knows much better than me. (@cdcapano if you can't do this, can you suggest someone else to look this over). There are some style issues here that should be addressed first though. We do ask that everyone follows PEP-8 (for code) and the numpydoc (for documentation) styles. The automated code checker has picked up on a number of issues that should be addressed: https://codeclimate.com/github/gwastro/pycbc/pull/3756 In most cases these should be trivial to fix, but in some cases (e.g. code duplication) it might make sense to keep what you have, but you should comment on that once you've resolved everything that you can. I'll also leave a few specific comments on code style stuff in-line. |
pycbc/transforms.py
Outdated
@@ -612,7 +612,101 @@ def inverse_jacobian(self, maps): | |||
ref_mass=1.4 | |||
mchirp = maps['mchirp'] | |||
return (2.**(-1./5) * self.ref_mass / mchirp)**(5./6) | |||
|
|||
######################################################################################################### TEST ################################################################################################################################################ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove comments like this before this get merged.
pycbc/transforms.py
Outdated
@@ -612,7 +612,101 @@ def inverse_jacobian(self, maps): | |||
ref_mass=1.4 | |||
mchirp = maps['mchirp'] | |||
return (2.**(-1./5) * self.ref_mass / mchirp)**(5./6) | |||
|
|||
######################################################################################################### TEST ################################################################################################################################################ | |||
import numpy as np |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code already imports numpy, don't need to do it again.
pycbc/transforms.py
Outdated
|
||
######################################################################################################### TEST ################################################################################################################################################ | ||
import numpy as np | ||
from pycbc.waveform.waveform_modes import jframe_to_l0frame |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to check (and not part of this PR). Have you checked that this code actually works? I'm not sure if it's been used for anything important ...
pycbc/transforms.py
Outdated
from pycbc.waveform.waveform_modes import jframe_to_l0frame | ||
|
||
class alignTotalSpin(BaseTransform): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove whitespace here
pycbc/transforms.py
Outdated
class alignTotalSpin(BaseTransform): | ||
|
||
"""Parameters | ||
---------- |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't following the numpydoc formatting, and won't display correctly in the online docs. Can you update this, using other code as an example?
pycbc/transforms.py
Outdated
self.news2 = news2 | ||
self._inputs = [self.iota, self.s1, self.s2, self.m1, self.m2, self.f_lower, self.phi_ref] | ||
self._outputs = [self.newIota, self.news1, self.news2] | ||
super(alignTotalSpin, self).__init__() ############################################## what?????? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the comment here? (While we are still using python2 we do need to support old-style super statements .... python2 will be retired soon though, so feel free to update once we do that!)
pycbc/transforms.py
Outdated
l = np.array([0,0,1.]) | ||
theta1 = np.arccos(np.dot(maps[s1], l)/np.linalg.norm(maps[s1])) | ||
theta2 = np.arccos(np.dot(maps[s2], l)/np.linalg.norm(maps[s2])) | ||
phi12 = np.arccos(np.dot(maps[s1][:2], maps[s2][:2]) / np.linalg.norm(maps[s1][:2]) / \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general multi-line statements like this are hard to parse. PEP-8 does have rules on this (and CodeClimate will pick up on this). I would probably align on the brackets, and the newline symbol is not needed.
pycbc/transforms.py
Outdated
phi12 = np.arccos(np.dot(maps[s1][:2], maps[s2][:2]) / np.linalg.norm(maps[s1][:2]) / \ | ||
np.linalg.norm(maps[s2][:2])) | ||
|
||
#print(maps[s1]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove debugging statements before this gets onto the main branch
pycbc/transforms.py
Outdated
news1 = np.zeros(3) | ||
news2 = np.zeros(3) | ||
|
||
output = \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a tool called black
that you can use to align small blocks of code like this to be PEP-8 compliant .... I know this stuff sounds picky, but uniform code is much easier to parse in a long module like this one.
pycbc/transforms.py
Outdated
theta1, theta2, | ||
phi12) | ||
|
||
newIota = output['inclination'] #not elegant, just quickly done for testing |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this is a "FIXME", then put it on the line above and use # FIXME
(or fix it).
pycbc/transforms.py
Outdated
news2[1] = output['spin2y'] | ||
news2[2] = output['spin2z'] | ||
|
||
#print(output['spin1x']) ### WIP!!!! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove
pycbc/transforms.py
Outdated
out = {self.newIota: iota, self.news1: s1, self.news2: s2} | ||
return self.format_output(maps, out) | ||
|
||
######################################################################################################### TEST ################################################################################################################################################ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this as well
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, thank you for the feedback, and thanks for making me aware of issues I should have resolved before doing the PR. I'll take some time today to have a look over these and hopefully get this in a much better state.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks fine to me, although I agree with @spxiwh that you should address the PEP-8 and documentation issues (which looks like you're working on). My only additional request is you move the jframe_to_l0frame
function to avoid circular imports; see my comment below.
pycbc/transforms.py
Outdated
@@ -28,10 +28,12 @@ | |||
from pycbc.waveform import parameters | |||
from pycbc.boundaries import Bounds | |||
from pycbc import VARARGS_DELIM | |||
from pycbc.waveform.waveform_modes import jframe_to_l0frame |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might cause circular imports. It's usually a bad idea to import functions from sub modules in top-level modules. I originally put this function in waveform_modes
because that's where it was being used, but it would be useful at a higher level. Maybe move it to pycbc/pnutils.py
? Then import from there (also make sure to add an import to pycbc/waveform/waveform_modes.py
too).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the help, I'll look into this asap
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would agree with Collin that if this function becomes useful outside of waveform_modes
then it belongs somewhere else. Collin's moved this stuff about most recently, so his suggestion seems sensible.
I used black as suggested by @spxiwh to resolve many of the PEP-8 issues. The only flags I'm seeing in the codeclimate page now are for similar lines of code (which I think need to be that way), and lines that are too long. Are these still things you'd suggest I need to fix? Should I just break the long lines? |
@SamuelH-97 Thanks for this. In some cases here I think
In some cases here it probably will keep > 79 character lines if you use I agree that the code repetition point here is not a problem. It would add confusion and reduce clarity to make this a function called twice. |
pycbc/transforms.py
Outdated
|
||
|
||
name = "align_total_spin" | ||
|
||
def __init__(self, iota, s1, s2, m1, m2, f_lower, phi_ref, newIota, news1, news2): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should be passing in & out the x/y/z components of the spin, rather than packaging them into vectors.
The changes up until now have been tested and work if the suggested bugfix in #3760 is included. |
This is the first significant PR aiming to address the issue #3468 |
Hi, sorry for the extended break on this request. I've moved the jframe_to_i0frame to pnutils.py as suggested. I assume I should probably do the same with it's sibling i0frame_to_jframe. |
I think that's sensible. @cdcapano would you be happy with these functions being moved? They seem to be unused anywhere else. |
@a-r-williamson @SamuelH-97 Can you rebase so we can verify the unittests / CC pass? |
@a-r-williamson @SamuelH-97 Are you sure this branch is as you've tested? The unittests are all failing for a sensible reason. The _formatdocstr function was private to the module you move the function from and hasn't been imported over. |
Rebase is done |
Will get that fixed asap |
pycbc/pnutils.py
Outdated
{mass1} | ||
{mass2} | ||
{f_ref} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These and similar parts of the docstrings (parameters inside {} in inputs and outputs) will need to be written out explicitly as they're not going to be parsed like they would be in waveform_modes.py
. For example:
{mass1} | |
{mass2} | |
{f_ref} | |
mass1 : float | |
First component mass in solar mass units | |
mass2 : float | |
Second component mass in solar mass units | |
f_ref: float | |
Reference frequency |
Please rebase on master again, as we recently did some documentation fixes that may affect this. Sorry! |
Co-authored-by: Andrew R. Williamson <[email protected]>
Co-authored-by: Andrew R. Williamson <[email protected]>
…. May need to do the same for i0frame_to_jframe
…ns to fit expected formatting
…I0frme and it's sibling function
af82979
to
48f7d6c
Compare
The previous check failures are now resolved. |
@ahnitz Given the current state of this, can it be merged? It's been tested and the builds are passing. |
Proposing adding the aligntotalspin function from pygrb to the transforms.py code as a callable class. This does require the import of numpy and import of the jframe_to_l0frame function from pycbc.waveform.waveform_modes currently.
The class is able to rigidly rotate a binary so that the total angular momentum has the given inclination (iota) instead of the orbital angular momentum. It returns the new inclination, s1, and s2. s1 and s2 are dimensionless spin. The spins are assumed to be given in the frame defined by the orbital angular momentum.
The class has been written to fit with the style of other classes in the transforms.py code.
This is primarily used in the generation of injected binaries used to test the sensitivity of targeted GRB searches.