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

Integration of pygrb aligntotalspin functionality into pycbc/transforms.py as a class #3756

Merged
merged 24 commits into from
Dec 17, 2021

Conversation

SamuelH-97
Copy link
Contributor

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.

@SamuelH-97
Copy link
Contributor Author

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

@spxiwh
Copy link
Contributor

spxiwh commented Jul 28, 2021

@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.

@@ -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 ################################################################################################################################################
Copy link
Contributor

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.

@@ -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
Copy link
Contributor

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.


######################################################################################################### TEST ################################################################################################################################################
import numpy as np
from pycbc.waveform.waveform_modes import jframe_to_l0frame
Copy link
Contributor

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 ...

from pycbc.waveform.waveform_modes import jframe_to_l0frame

class alignTotalSpin(BaseTransform):

Copy link
Contributor

Choose a reason for hiding this comment

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

Remove whitespace here

class alignTotalSpin(BaseTransform):

"""Parameters
----------
Copy link
Contributor

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?

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??????
Copy link
Contributor

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!)

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]) / \
Copy link
Contributor

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.

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])
Copy link
Contributor

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

news1 = np.zeros(3)
news2 = np.zeros(3)

output = \
Copy link
Contributor

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.

theta1, theta2,
phi12)

newIota = output['inclination'] #not elegant, just quickly done for testing
Copy link
Contributor

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).

news2[1] = output['spin2y']
news2[2] = output['spin2z']

#print(output['spin1x']) ### WIP!!!!
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove

out = {self.newIota: iota, self.news1: s1, self.news2: s2}
return self.format_output(maps, out)

######################################################################################################### TEST ################################################################################################################################################
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove this as well

Copy link
Contributor Author

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.

@spxiwh spxiwh requested a review from cdcapano July 28, 2021 10:32
Copy link
Contributor

@cdcapano cdcapano left a 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.

@@ -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
Copy link
Contributor

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).

Copy link
Contributor Author

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

Copy link
Contributor

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.

@SamuelH-97
Copy link
Contributor Author

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?

@spxiwh
Copy link
Contributor

spxiwh commented Aug 2, 2021

@SamuelH-97 Thanks for this. In some cases here I think black should have been able to keep the 79 character line limit. I think you probably ran it without specifying this. I quote from the black documentation:

If you’re paid by the line of code you write, you can pass --line-length with a lower number. Black will try to respect that. However, sometimes it won’t be able to without breaking other rules. In those rare cases, auto-formatted code will exceed your allotted limit.

In some cases here it probably will keep > 79 character lines if you use line-length, but please do try running black with line-length 79 first. Then I think this is good to go.

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 Show resolved Hide resolved
pycbc/transforms.py Outdated Show resolved Hide resolved


name = "align_total_spin"

def __init__(self, iota, s1, s2, m1, m2, f_lower, phi_ref, newIota, news1, news2):
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 we should be passing in & out the x/y/z components of the spin, rather than packaging them into vectors.

@a-r-williamson
Copy link
Contributor

The changes up until now have been tested and work if the suggested bugfix in #3760 is included.

@a-r-williamson
Copy link
Contributor

a-r-williamson commented Oct 12, 2021

This is the first significant PR aiming to address the issue #3468

@SamuelH-97
Copy link
Contributor Author

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.

@a-r-williamson
Copy link
Contributor

a-r-williamson commented Oct 26, 2021

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.

@SamuelH-97
Copy link
Contributor Author

With these changes and fixes now pushed, would it be possible for you to take another look @cdcapano @spxiwh and make sure you're happy with them?

@ahnitz
Copy link
Member

ahnitz commented Nov 29, 2021

@a-r-williamson @SamuelH-97 Can you rebase so we can verify the unittests / CC pass?

@ahnitz
Copy link
Member

ahnitz commented Nov 29, 2021

@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.

@SamuelH-97
Copy link
Contributor Author

@a-r-williamson @SamuelH-97 Can you rebase so we can verify the unittests / CC pass?

Rebase is done

@SamuelH-97
Copy link
Contributor Author

@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.

Will get that fixed asap

pycbc/pnutils.py Outdated
Comment on lines 969 to 971
{mass1}
{mass2}
{f_ref}
Copy link
Contributor

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:

Suggested change
{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

@titodalcanton
Copy link
Contributor

Please rebase on master again, as we recently did some documentation fixes that may affect this. Sorry!

SamuelH-97 and others added 21 commits December 16, 2021 07:51
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
@a-r-williamson
Copy link
Contributor

The previous check failures are now resolved.

@a-r-williamson
Copy link
Contributor

@ahnitz Given the current state of this, can it be merged? It's been tested and the builds are passing.

@spxiwh spxiwh dismissed cdcapano’s stale review December 17, 2021 16:15

Collin not responding

@spxiwh spxiwh removed the request for review from cdcapano December 17, 2021 16:15
@a-r-williamson a-r-williamson merged commit 0bb2754 into gwastro:master Dec 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PyGRB PyGRB development
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

6 participants