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

Guess binary model & add conversion script #1695

Merged
merged 24 commits into from
Apr 26, 2024

Conversation

vhaasteren
Copy link
Member

@vhaasteren vhaasteren commented Dec 15, 2023

When a binary model is used that PINT does not know about (e.g. T2), an error is thrown. Sometimes the underlying binary model that the user wants to use is easy to guess. We can provide that guess to help the user

This PR provides a utility function that can guess the binary model from a parfile dictionary, and the UnknownBinaryModel error is modified to include a hint for the user as to which binary model they may want to try, such as this one:

UnknownBinaryModel: Pulsar system/Binary model component T2 is not provided. Perhaps use DDK?

An error is still thrown, and no attempt is made to convert parameter values. A conversion script is also available, called t2binary2pint. This script just uses the ModelBuilder class with the option allow_T2=True. That will decide on the best binary model, and in the case of DDK it will convert the KOM/KIN parameters

TODO

  • When no guess can be made, the error message should not be modified
  • Linting
  • Unit tests
  • Add unit test that checks the two types of messages of UnknownBinaryModel
  • Script to make the conversion (just like TCB -> TDB)
  • Refactor choose_model
  • Test whether converted models are correct
  • Test the t2binary2pint script (would solve all uncovered lines)

@vhaasteren vhaasteren marked this pull request as draft December 15, 2023 18:52
Copy link

codecov bot commented Dec 15, 2023

Codecov Report

Attention: Patch coverage is 45.00000% with 55 lines in your changes are missing coverage. Please review.

Project coverage is 68.97%. Comparing base (683d162) to head (8c01047).
Report is 30 commits behind head on master.

❗ Current head 8c01047 differs from pull request most recent head 8f4a185. Consider uploading reports for the commit 8f4a185 to get more accurate results

Files Patch % Lines
src/pint/models/model_builder.py 50.66% 34 Missing and 3 partials ⚠️
src/pint/scripts/t2binary2pint.py 0.00% 18 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1695      +/-   ##
==========================================
- Coverage   69.11%   68.97%   -0.15%     
==========================================
  Files         105      106       +1     
  Lines       24749    24807      +58     
  Branches     4416     4424       +8     
==========================================
+ Hits        17106    17110       +4     
- Misses       6534     6586      +52     
- Partials     1109     1111       +2     

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

@dlakaplan
Copy link
Contributor

Very nice. Would it make sense to further build a function to make this change (sort of like the TCB->TDB function) based on the best-guess? A scripts too?

@vhaasteren
Copy link
Member Author

vhaasteren commented Dec 16, 2023

I think such a script would be convenient to have, and it's in line with not supporting TCB but still providing a script. Same philosophy. Sure, I can add that. As far as I understood from the timers, the only transformation I would need to do are for the DDK parameters KOM/KIN, which have a different definition in T2 compared to DDK. That's your expertise, @dlakaplan, is that the only transformation that is necessary?

Also, although what I wrote works, I now feel that it is not elegant enough, and it adds complexity to the logic. My thought is be to re-factor choose_model into smaller components with singular tasks. Then I'd also expand the UnknownBinaryModel class to include potential suggestions. I'm open to suggestions though.

Something that I need feedback on is the binary_model_priority I define in the function guess_binary_model. This ranking is necessary when there are multiple viable binary models (like BT being a subset of others). In that case, the simplest one should have a higher priority in that list. The ranking I provided is not a very well-informed ranking.

@vhaasteren vhaasteren linked an issue Dec 16, 2023 that may be closed by this pull request
@dlakaplan
Copy link
Contributor

I think such a script would be convenient to have, and it's in line with not supporting TCB but still providing a script. Same philosophy. Sure, I can add that. As far as I understood from the timers, the only transformation I would need to do are for the DDK parameters KOM/KIN, which have a different definition in T2 compared to DDK. That's your expertise, @dlakaplan, is that the only transformation that is necessary?

I think so. There are routines in models/binary_ddk.py to do the conversion.

@vhaasteren
Copy link
Member Author

vhaasteren commented Dec 18, 2023

I think so. There are routines in models/binary_ddk.py to do the conversion.

Right, I see. But... I also see that upon reading, the KOM/KIN parameters are converted in the BinaryDDK to the AU convention, and SINI is removed. I am now a bit puzzled by that logic. SINI is only there if we are reading in a Tempo2 parfile, which would have KOM/KIN in the IAU convention to begin with. But BinaryDDK needs them in the DT96 convention. Isn't that a bit of a weird state for the parfile to be in?

My current conversion function will remove SINI and convert KOM/KIN.

Anyway, a somewhat important question: it looks like the model_builder actually allows TCB if you set the option for it. By default PINT throws an error for tcb, but you can force it to read a model with TCB. Reason this is important is that the neatest design for the script would be for the model_builder to read/convert/write the parfile. But in order to convert with the model builder, it needs to be able to read/convert them with options. This is (close to) supporting T2 models. Perhaps this is a good topic for a telecon

@vhaasteren
Copy link
Member Author

@dlakaplan, I've just put together an initial t2binary2pint script, and I'm realizing that such a parfile will always be TCB, so it's kind of superseding functionality of tcb2tdb. In fact, I've just implemented it in the ModelBuilder class in the same way.

A single tool that converts tempo2 -> PINT would be more general though, and perhaps not something we'd want to support. Shall I just leave it as a separate?

@vhaasteren vhaasteren changed the title Guess binary model to help the user Guess binary model to help the user & add conversion script Mar 29, 2024
@vhaasteren vhaasteren changed the title Guess binary model to help the user & add conversion script Guess binary model & add conversion script Mar 29, 2024
@vhaasteren vhaasteren added enhancement ready for new iteration review The PR is ready for a new iteration of review labels Mar 29, 2024
@vhaasteren
Copy link
Member Author

vhaasteren commented Mar 29, 2024

Added iteration review tag, because the converted binary model is likely not correct. For DDK I'm using the _convert_kom and _convert_kin functions, so perhaps I'm missing something important?

Specific question

Use the tool t2binary2pint from this PR on, say, the PPTA J1713 parfile: t2binary2pint J1713-ppta.par J1713-converted.par. The resulting parfile should give a TDB DDK parfile. I don't think it's correctly converted.

@dlakaplan
Copy link
Contributor

Added iteration review tag, because the converted binary model is likely not correct. For DDK I'm using the _convert_kom and _convert_kin functions, so perhaps I'm missing something important?

Specific question

Use the tool t2binary2pint from this PR on, say, the PPTA J1713 parfile: t2binary2pint J1713-ppta.par J1713-converted.par. The resulting parfile should give a TDB DDK parfile. I don't think it's correctly converted.

What do you think is wrong?

@vhaasteren
Copy link
Member Author

Added iteration review tag, because the converted binary model is likely not correct. For DDK I'm using the _convert_kom and _convert_kin functions, so perhaps I'm missing something important?
Specific question
Use the tool t2binary2pint from this PR on, say, the PPTA J1713 parfile: t2binary2pint J1713-ppta.par J1713-converted.par. The resulting parfile should give a TDB DDK parfile. I don't think it's correctly converted.

What do you think is wrong?

Oh... I don't know why I thought it was wrong actually. The residuals look the same as they are with libstempo/tempo2. And the original parfile has:

BINARY         T2
SINI            KIN
#SINI		0.95093080767197021955      
KOM            92.824444242804586412     1  0.95050573178270669761   
KIN            70.946081736425649543     1  0.40200796477828915076   

The new one has:

BINARY                                DDK
KIN                    109.05391826357435 1 0.40200796477828915
KOM                   -2.8244442428045886 1 0.9505057317827067
# KINIAU                  70.94608173642565
# KOMIAU                  92.82444424280459

I looked at the KOMIAU/KINIAU parameters, and somehow decided they were different. Ok, fine, I'll add some tests for the conversion script and mark this PR as completed. It seems fully functional now.

@vhaasteren vhaasteren marked this pull request as ready for review April 12, 2024 17:16
@vhaasteren vhaasteren added awaiting review This PR needs someone to review it so it can be merged and removed ready for new iteration review The PR is ready for a new iteration of review labels Apr 12, 2024
@vhaasteren
Copy link
Member Author

Should be ready now

@dlakaplan
Copy link
Contributor

Can you just paste a few examples in here?

@vhaasteren
Copy link
Member Author

vhaasteren commented Apr 15, 2024

PPTA DR3 J1713 (skipping JUMP parameters):

PSRJ           J1713+0747
ELONG          256.6686963862985921269732614 1  0.00000000090876673235   
ELAT           30.7003598118515997148630925 1  0.00000000167397472599   
F0             218.81184039471733346     1  7.691921346252546715e-14 
F1             -4.0839252783054717257e-16 1  5.4594314711576426696e-22
PEPOCH         55636                       
POSEPOCH       55636                       
DMEPOCH        58983                       
DM             15.988527064339802541     1  0.00025307207531452086   
DM1            -6.5890534863252850064e-05 1  0.00000605342119520716   
DM2            -5.4543887888466437085e-06 1  0.00000101281051729864   
PMELONG        5.2676082172334073583     1  0.00055800795406477962   
PMELAT         -3.4414946315630252599    1  0.00115897614003722393   
PX             0.89811696943912794388    1  0.01256210980106707044   
SINI            KIN
#SINI		0.95093080767197021955      
BINARY         T2
PB             67.825136364466631286     1  0.00000219567958252830   
T0             54303.634515361629319     1  0.00018447137504573347   
A1             32.342422610088938466     1  0.00000008013425585979   
OM             176.19952646143242884     1  0.00097913197491892516   
ECC            7.4940884937095751794e-05 1  0.00000000034213864828   
PBDOT          1.9589808646702856654e-14 1  9.4538916445564820213e-14
OMDOT          0.00015396242225124112747 1  0.00006275870760582978   
M2             0.30165635446915292143    1  0.00768779658427406771   
START          53040.943687135087238       
FINISH         59303.777495481269703       
TZRMJD         56172.283864994755          
TZRFRQ         709.57799999999997453    
TZRSITE        pks   
FD1            -1.4425242329551252494e-05 1  0.00000207839278552605   
FD2            3.1872195202268463094e-06 1  0.00000171340474719996   
FD3            1.3563215055025152626e-06 1  0.00000054795329246384   
KOM            92.824444242804586412     1  0.95050573178270669761   
KIN            70.946081736425649543     1  0.40200796477828915076   
TRES           0.248        
EPHVER         5                           
CLK            TT(BIPM2020)
MODE 1
TIMEEPH        IF99
DILATEFREQ     Y
PLANET_SHAPIRO -1
T2CMETHOD      IAU2000B
NE_SW          0.000
CORRECT_TROPOSPHERE  Y
EPHEM          DE436
NTOA           5141
CHI2R          2.1010 5094

becomes:

# Created: 2024-04-15T18:07:40.081257
# PINT_version: 0.9.8+282.ge23026b2
# User: vhaasteren
# Host: Rutgers-M1-MBPro.local
# OS: macOS-14.4.1-x86_64-i386-64bit
# Python: 3.10.12 | packaged by conda-forge | (main, Jun 23 2023, 22:39:40) [Clang 15.0.7 ]
# Format: pint
PSRJ                           J1713+0747
EPHEM                               DE436
CLK                          TT(BIPM2020)
UNITS                                 TDB
START              53040.9436871350872380
FINISH             59303.7774954812697030
TIMEEPH                              FB90
T2CMETHOD                        IAU2000B
DILATEFREQ                              N
DMDATA                                  N
NTOA                                 5141
CHI2R                               2.101 0 5094.0
TRES                                0.248
ELONG                 256.668696386298620 1 0.00000000090876673235
ELAT                   30.700359811851598 1 0.00000000167397472599
PMELONG                 5.267608217233407 1 0.0005580079540647797
PMELAT                 -3.441494631563025 1 0.001158976140037224
PX                      0.898116969439128 1 0.01256210980106707
ECL                              IERS2010
POSEPOCH           55636.0000000000000000
F0                  218.81184039471733346 1 7.691921346252546715e-14
F1              -4.0839252783054717257e-16 1 5.4594314711576426696e-22
PEPOCH             55636.0000000000000000
CORRECT_TROPOSPHERE                         Y
PLANET_SHAPIRO                          Y
NE_SW                                 0.0
SWM                                   0.0
DM                  15.988527064339802541 1 0.00025307207531452086
DM1             -6.5890534863252850064e-05 1 6.05342119520716e-06
DM2             -5.4543887888466437085e-06 1 1.01281051729864e-06
DMEPOCH            58983.0000000000000000
BINARY                                DDK
PB                  67.825136364466631286 1 2.1956795825283e-06
PBDOT              1.9589808646702858e-14 1 9.453891644556482e-14
A1                      32.34242261008894 1 8.013425585979e-08
A1DOT                                 0.0
ECC                 7.494088493709575e-05 1 3.4213864828e-10
EDOT                                  0.0
T0                 54303.6345153616293190 1 0.00018447137504573347
OM                  176.19952646143242884 1 0.00097913197491892516
OMDOT           0.00015396242225124112747 1 6.275870760582978e-05
M2                     0.3016563544691529 1 0.007687796584274068
A0                                    0.0
B0                                    0.0
GAMMA                                 0.0
DR                                    0.0
DTH                                   0.0
KIN                    109.05391826357435 1 0.40200796477828915
KOM                   -2.8244442428045886 1 0.9505057317827067
# KINIAU                  70.94608173642565
# KOMIAU                  92.82444424280459
# SINI                   0.9452117806686158
FD1               -1.4425242329551252e-05 1 2.07839278552605e-06
FD2                3.1872195202268463e-06 1 1.71340474719996e-06
FD3                1.3563215055025152e-06 1 5.4795329246384e-07
TZRMJD             56172.2838649947550000
TZRSITE                               pks
TZRFRQ                            709.578

And PPTA DR3 J0613:

PSRJ           J0613-0200
ELONG          93.7990080578252241119696642 1  0.00000000527342482862   
ELAT           -25.4071385368251683716661615 1  0.00000001081494991767   
F0             326.60056196727986788     1  5.5168645420374461129e-13
F1             -1.0230273661253295295e-15 1  3.9055869190811176866e-21
PEPOCH         55636                       
POSEPOCH       55636                       
DMEPOCH        58991                       
DM             38.773619269948962945     1  0.00075685011491238711   
DM1            -9.7690099037869001671e-05 1  0.00001151008531354333   
DM2            -1.5085519903698426756e-05 1  0.00000155975486599981   
PMELONG        2.117609094803266377      1  0.00232499275314834832   
PMELAT         -10.305261236969457742    1  0.00541024949649476696   
PX             0.85425650360254235313    1  0.06272691625352466716   
BINARY         T2
PB             1.1985125750770358367     1  0.00000000002467310927   
A1             1.0914441628309673835     1  0.00000002980102471189   
PBDOT          3.679353219966319937e-14  1  3.923498139518176671e-15 
TASC           50315.269491312552546     1  0.00000005893419374513   
EPS1           3.9497878993773303543e-06 1  0.00000005213069600587   
EPS2           3.6092533823433831367e-06 1  0.00000005525232749017   
START          53044.545290457821217       
FINISH         59645.34970053811595        
TZRMJD         56352.38403350918524        
TZRFRQ         1281.7480000000000473    
TZRSITE        pks   
FD1            -2.9824988539258083678e-05 1  0.00000649081817900048   
FD2            2.3749491702132622238e-05 1  0.00000623740134743948   
FD3            -6.3327550387763506748e-06 1  0.00000240603869934920   
TRES           1.406        
EPHVER         5                           
H3             1.5395869138306373323e-07 1  0.00000002204402957066   
H4             2.5457245374629321327e-07 1  0.00000002937911765009   
CLK            TT(BIPM2020)
MODE 1
TIMEEPH        IF99
DILATEFREQ     Y
PLANET_SHAPIRO -1
T2CMETHOD      IAU2000B
NE_SW          0.000
CORRECT_TROPOSPHERE  Y
EPHEM          DE436
NTOA           4927
CHI2R          2.0104 4882

becomes

# Created: 2024-04-15T18:08:15.692842
# PINT_version: 0.9.8+282.ge23026b2
# User: vhaasteren
# Host: Rutgers-M1-MBPro.local
# OS: macOS-14.4.1-x86_64-i386-64bit
# Python: 3.10.12 | packaged by conda-forge | (main, Jun 23 2023, 22:39:40) [Clang 15.0.7 ]
# Format: pint
PSRJ                           J0613-0200
EPHEM                               DE436
CLK                          TT(BIPM2020)
UNITS                                 TDB
START              53044.5452904578212170
FINISH             59645.3497005381159500
TIMEEPH                              FB90
T2CMETHOD                        IAU2000B
DILATEFREQ                              N
DMDATA                                  N
NTOA                                 4927
CHI2R                              2.0104 0 4882.0
TRES                                1.406
ELONG                  93.799008057825219 1 0.00000000527342482862
ELAT                  -25.407138536825169 1 0.00000001081494991767
PMELONG                2.1176090948032664 1 0.0023249927531483485
PMELAT                -10.305261236969457 1 0.005410249496494767
PX                     0.8542565036025423 1 0.06272691625352467
ECL                              IERS2010
POSEPOCH           55636.0000000000000000
F0                  326.60056196727986788 1 5.516864542037446113e-13
F1              -1.0230273661253295295e-15 1 3.9055869190811176866e-21
PEPOCH             55636.0000000000000000
CORRECT_TROPOSPHERE                         Y
PLANET_SHAPIRO                          Y
NE_SW                                 0.0
SWM                                   0.0
DM                  38.773619269948962945 1 0.00075685011491238711
DM1             -9.769009903786900167e-05 1 1.151008531354333e-05
DM2             -1.5085519903698426756e-05 1 1.55975486599981e-06
DMEPOCH            58991.0000000000000000
BINARY                              ELL1H
PB                  1.1985125750770358367 1 2.467310927e-11
PBDOT                3.67935321996632e-14 1 3.923498139518177e-15
A1                     1.0914441628309675 1 2.980102471189e-08
TASC               50315.2694913125525460 1 5.893419374513e-08
EPS1            3.9497878993773303543e-06 1 5.213069600587e-08
EPS2            3.6092533823433831367e-06 1 5.525232749017e-08
# ECC             5.3504704866044009753e-06
# OM                  47.579428609186540298
H3              1.5395869138306373323e-07 1 2.204402957066e-08
H4              2.5457245374629321327e-07 1 2.937911765009e-08
NHARMS                                  7
FD1               -2.9824988539258085e-05 1 6.49081817900048e-06
FD2                2.3749491702132622e-05 1 6.23740134743948e-06
FD3                -6.332755038776351e-06 1 2.4060386993492e-06
TZRMJD             56352.3840335091852431
TZRSITE                               pks
TZRFRQ                           1281.748

Edit: I see now that the PPTA release doesn't even declare 'UNITS' in the par file. However, I suppose that should be irrelevant for checking the functionality on display here. All above was run with t2binary2pint oldpar.par newpar.par

src/pint/models/model_builder.py Outdated Show resolved Hide resolved
src/pint/models/model_builder.py Outdated Show resolved Hide resolved
src/pint/models/model_builder.py Outdated Show resolved Hide resolved
src/pint/models/model_builder.py Outdated Show resolved Hide resolved
src/pint/models/model_builder.py Outdated Show resolved Hide resolved
src/pint/models/model_builder.py Outdated Show resolved Hide resolved
src/pint/models/model_builder.py Show resolved Hide resolved
src/pint/models/model_builder.py Outdated Show resolved Hide resolved
src/pint/models/model_builder.py Outdated Show resolved Hide resolved
src/pint/models/timing_model.py Outdated Show resolved Hide resolved
@abhisrkckl
Copy link
Contributor

Please add an entry in CHANGELOG-unrelased.md.

@abhisrkckl
Copy link
Contributor

Also, please add the allow_T2 and force_binary_model options in the get_model and get_model_and_toas functions. Most people use those instead of ModelBuilder directly.

@vhaasteren
Copy link
Member Author

Done

@abhisrkckl
Copy link
Contributor

Can you please merge nanograv:master? It fixes the macos-latest issue.

I think this is pretty much ready to go otherwise. I'll merge this once all the tests pass.

@vhaasteren
Copy link
Member Author

Can you please merge nanograv:master? It fixes the macos-latest issue.

I think this is pretty much ready to go otherwise. I'll merge this once all the tests pass.

Done. I also added one more unit test that checks for an Exception when the binary model cannot be determined

@abhisrkckl abhisrkckl merged commit 0d66e54 into nanograv:master Apr 26, 2024
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting review This PR needs someone to review it so it can be merged enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Generic binary model?
3 participants