desihub / desitarget

DESI Targeting
BSD 3-Clause "New" or "Revised" License
18 stars 23 forks source link

Add the final QSO decision logic to the full Main Survey MTL loop. #751

Closed geordie666 closed 3 years ago

geordie666 commented 3 years ago

This PR should be the final piece needed for the full MTL loop for the Main Survey. It adds the logic for deciding which quasars are observed at high priority on repeat passes. To try to succinctly describe the logic:

Then:

Additionally:

I'll use this branch to make MTL ledgers for review once the zqso catalogs are ready at NERSC. I'll subsequently merge this PR if those MTL ledgers look reasonable to the Operations team and some select quasologists.

geordie666 commented 3 years ago

The basic/main QSO cases (tracer/secondary-mid-z-QSO/LyA-QSO) are already in the unit tests. There are possibly one or two that I've missed, which I can add relatively quickly.

Adding "exhaustive" tests will require significant new functionality that we have never previously included. First, we would also need to check decisions on passes 2, 3 and 4 of the overlapping observations, not just on passes 0 and 1. Second, we would also need to include secondary targets to be truly exhaustive.

There are potentially dozens of individual cases as a few of the secondary targets request additional observations and could, in theory, merge with a primary QSO that takes precedence in dark time but not in bright time. Checking many of these cases using real data took me several days of work for this PR. Writing such tests with ersatz data will potentially take a great deal of time and delay merging this PR (and the creation of the subsequent MTL catalogs). I doubt I can add all of the cases by Monday.

We also don't have tests that check the full MTL loop, and never have. To some extent, I see checking pass-2/3/4 decisions and looking for corner cases as the purview of mocks rather than of unit tests. We have never unit-tested beyond passes 0 and 1.

geordie666 commented 3 years ago

I added unit tests to:

If I find some time over the weekend I'll add a few more cases.

geordie666 commented 3 years ago

I extended the "locked in" test to an additional pass, and added a new unit test, to check that:

geordie666 commented 3 years ago

I extended the "locked in" test further, to check that:

sbailey commented 3 years ago

Thanks for adding the extra tests. Can you confirm that the LyA followup choices purposefully do not use ZWARN or DELTACHI2? I don't see that in the description, which surprises me, especially for more dramatic ZWARN bits like NODATA, Z_FITLIMIT, or BAD_MINFIT. Or is that caught earlier before a target is even considered for LyA followup?

Update: I now see the docstring comment about "Sources in the zcat with ZWARN of NODATA are always ignored". Please confirm if any other ZWARN bits or DELTACHI2 are used (or purposefully ignored).

sbailey commented 3 years ago

Running make_mtl alters the input zcat table by adding a NUMOBS_MORE column. Ideally make_mtl should not alter the inputs, or otherwise this side effect should be documented (including why). If you are only adding a column for bookkeeping convenience but not otherwise modifying the rows of the pre-existing columns, you could start with

zcat = zcat.copy(copy_data=False)
sbailey commented 3 years ago

@geordie666 are your criteria applied in order and the first matching one determines the state? e.g.

Z=0.5, QN_Z=2.5, IS_QSO_QN==1 matches your first criterion as a LyA quasar but matches your second criterion as a tracer quasar. Does the first criterion win?

[apologies to others getting N>>1 emails via this ticket from me; more are likely coming...]

sbailey commented 3 years ago

minor: make_mtl docstring says that targets requires columns TARGETID, DESI_TARGET, NUMOBS_INIT, PRIORITY_INIT; experimentally it also requires BGS_TARGET, MWS_TARGET, and if zcat is not None then it also requires PRIORITY (i.e. if zcat is not None then targets needs to be a previous mtl, not an original targets table, even if this is the first iteration). That matches how targets -> mtl ledger -> obs -> mtl updates happens in practice, but is somewhat inconsistent with the docstring.

sbailey commented 3 years ago

@geordie666 FWIW I independently tested all the initial cases of RR/QN for the first MTL update. Initially I found a bunch of errors, then realized that I had the master branch checked out; switching to this branch fixed all the failing cases. Good! Attached is a gzipped python script I used (GitHub doesn't allow direct .py attachments) in case it covers any cases that you don't think you have already covered in unit tests.

check_lya_mtl.py.gz

I have not independently tested the "locked in" and "promoted" cases, though it sounds like you have those included in your unit tests.

Summarizing my tests/questions for the night:

geordie666 commented 3 years ago

@sbailey: In answer to your comments:

numobs_more_blame

So, I'm not sure what your request is, here? If the issue is that the number of rows in the zcat can potentially change, that's been possible since you updated the code back in late 2018 (specifically, your zcat = zcat[ok] line can alter the number of rows in the zcat):

alter zcat

My only change in this initial zcat-altering-behavior was to log that this alteration was a possibility. Essentially, I don't think this specific PR changes existing allowed behavior in any way, it merely inherits behavior from earlier PRs?

geordie666 commented 3 years ago

Sorry, I misunderstood your 2nd point, above, by conflating a discussion of the MTL table with a discussion of the zcat. I think I see what you mean, now.

Although the zcat has been modified-in-place by make_mtl() for as long as I can remember, nothing about processing the "real" data requires that modification, so I've always assumed it was a necessary feature for the mocks. Can you confirm that the mocks will be just fine if I force the output zcat from make_mtl() to always be identical to the input zcat?

sbailey commented 3 years ago

Thanks for the additional info @geordie666 . I'm apparently 5 years late in noticing that make_mtl modifies zcat by adding a new NUMOBS_MORE column. I suspect that is an unintended side-effect of the bookkeeping rather than something actually used for mocks, but in the interest of not breaking things at the last moment let's just document this behavior while we're noticing it, but not change it right now. i.e. this is a technical debt/maintenance issue to document whenever a function modifies its inputs (as opposed to just using them to derive outputs).

Additionally, if you know of any other inputs that are modified, or know whether make_mtl would use a pre-existing NUMOBS_MORE column, that should be documented too.

Otherwise I'm satisfied with the functionality of this PR. Thanks.

geordie666 commented 3 years ago

I expect that this is ready to merge, now, but I'm still going to run a few more tests through the rest of the evening. I'll likely self-merge this early tomorrow.

coveralls commented 3 years ago

Coverage Status

Coverage decreased (-0.005%) to 58.761% when pulling dceb6d4434e9e8dc01ec380d11e976d7277e40a5 on ADM-LyA-logic into cb8812282dd57b530b81d5e7556e683bcd66a84d on master.