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

DM-14253: Modernize python in meas_base and meas_algorithms #119

Merged
merged 9 commits into from May 3, 2018

Conversation

r-owen
Copy link
Contributor

@r-owen r-owen commented May 1, 2018

No description provided.

Remove `from __future__ import...`
Remove use of future module, including `with_metaclass`
Copy link
Member

@timj timj left a comment

Choose a reason for hiding this comment

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

Fro the try blocks catching Exception do we know anything more specific or is it really anything is as bad as anything else?

from .coaddPsf import CoaddPsfControl
from lsst.pex.config import makeConfigClass

makeConfigClass(CoaddPsfControl, "CoaddPsfConfig")
CoaddPsfConfig = makeConfigClass(CoaddPsfControl, "CoaddPsfConfig")
Copy link
Member

Choose a reason for hiding this comment

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

How did this work before?

Copy link
Contributor Author

@r-owen r-owen May 3, 2018

Choose a reason for hiding this comment

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

I'm not sure it did work before. I suspect it was a no-op.

@r-owen
Copy link
Contributor Author

r-owen commented May 3, 2018

In most cases the bare exceptions were in plotting code, where it seemed that a "best effort" was intended, instead of expecting some particular exception. In cases where that was not true I tried to be more careful.

@r-owen
Copy link
Contributor Author

r-owen commented May 3, 2018

That said, changing except: to except Exception: is sleazy and could be masking lots of bugs. The best I can say is this change didn't make the code any worse. It's not easily testable. I think doing better is out of scope for this ticket. I can file a ticket to have a closer look if you like, but I fear it may never get worked on.

@timj
Copy link
Member

timj commented May 3, 2018

Let it go. It's easy to find them if we ever feel the need to look again.

Copy link
Contributor

@parejkoj parejkoj left a comment

Choose a reason for hiding this comment

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

I recall when I did this that there were a handful of builtins imports hidden below other imports: did you do an all file search for future and builtins?


from builtins import zip

Copy link
Contributor

Choose a reason for hiding this comment

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

Did this leave 3 empty lines here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not that I can see in my copy. I wonder why this got hidden as outdated? I have not pushed anything since May 1

@@ -39,7 +39,7 @@
from .spatialModelPsf import createKernelFromPsfCandidates, countPsfCandidates, \
fitSpatialKernelFromPsfCandidates, fitKernelParamsToImage
from .pcaPsf import PcaPsf
from . import utils as maUtils
from . import utils as mautils
Copy link
Contributor

Choose a reason for hiding this comment

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

Why change this name to non-camelCase?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because flake8 complained

Copy link
Contributor

Choose a reason for hiding this comment

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

Complained about what? maUtils is follows our other camelCase renamings better.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

warning flake8: N812 lowercase 'maUtils' imported as non lowercase

Copy link
Contributor

Choose a reason for hiding this comment

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

Can you instead just do from . import utils instead of renaming it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK

normalize=normalizeResiduals,
showBadCandidates=showBadCandidates,
variance=True)
except:
except Exception:
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you look at what showPsfCandidates could raise, so as to make this exception more specific?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think that is reasonable. It's 200 lines of code, with no documentation as to what exceptions it might raise. I admit that many of those lines are contained in their own try/except Exception blocks, but it's still a big job to try to figure out all the possible errors it might raise.

Copy link
Contributor

@parejkoj parejkoj left a comment

Choose a reason for hiding this comment

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

I marked the except Exception blocks that I think could use a little more investigation (they only call one or two things). Give them each a minute or two of looking at the try-ed code to see if you can narrow down the list.

I don't like the others, but I've filed DM-14310 to deal with it more broadly.

@@ -224,7 +224,7 @@ def showPsfCandidates(exposure, psfCellSet, psf=None, frame=None, normalize=True
# residuals using spatial model
try:
subtractPsf(psf, im, xc, yc)
except:
except Exception:
Copy link
Contributor

Choose a reason for hiding this comment

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

Please check if there's an obvious exception(s) raised by subtractPSf here.

Copy link
Contributor Author

@r-owen r-owen May 3, 2018

Choose a reason for hiding this comment

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

Here is the full documentation for subtractPsf:

/**
 * Subtract a PSF from an image at a given position
 */
template<typename MaskedImageT>
double subtractPsf(afwDetection::Psf const& psf,      ///< the PSF to subtract
                   MaskedImageT *data,                ///< Image to subtract PSF from
                   double x,                          ///< column position
                   double y,                          ///< row position
                   double psfFlux                     ///< object's PSF flux (if not NaN)
                  )

I think this is a clear case where except Exception: is actually reasonable. This means "make a best effort to subtract the PSF and continue if it fails". Fortunately this is plotting code.

@@ -246,7 +246,7 @@ def showPsfCandidates(exposure, psfCellSet, psf=None, frame=None, normalize=True

try:
noSpatialKernel = psf.getKernel()
except:
except Exception:
Copy link
Contributor

Choose a reason for hiding this comment

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

Please check if there's an obvious exception(s) raised by psf.getKernel() here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is C++ code, with different versions for different PSF classes. And none of it documents any exceptions that I can find.

I = I0*psfVal(ix, iy, x + dx, y + dy, sigma1, sigma2, b)
Isample = rand.poisson(I) if addNoise else I
Ival = I0*psfVal(ix, iy, x + dx, y + dy, sigma1, sigma2, b)
Isample = rand.poisson(Ival) if addNoise else Ival
Copy link
Contributor

Choose a reason for hiding this comment

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

Is intensity a better name here?

@@ -258,7 +258,7 @@ def subtractStars(self, exposure, catalog, chi_lim=-1):
if bbox.contains(afwGeom.PointI(int(xc), int(yc))):
try:
measAlg.subtractPsf(psf, subtracted, xc, yc)
except:
except Exception:
Copy link
Contributor

Choose a reason for hiding this comment

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

Please check if there's an obvious exception(s) raised by subtractPSf here (as above).

Copy link
Contributor Author

@r-owen r-owen May 3, 2018

Choose a reason for hiding this comment

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

I agree that a unit test is a very nasty spot to ignore errors. I tried leaving out the try/except to see what got raised in the and nothing did, so I removed it.

@r-owen
Copy link
Contributor Author

r-owen commented May 3, 2018

I did indeed search for "future" and "builtins" and removed all associated imports.

Copy link
Contributor

@parejkoj parejkoj left a comment

Choose a reason for hiding this comment

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

Please check that the doxygen builds without errors/warnings after this change.

Copy link
Contributor

@parejkoj parejkoj left a comment

Choose a reason for hiding this comment

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

A few comments warranting further investigation. I'll trust your judgment on those.

@parejkoj
Copy link
Contributor

parejkoj commented May 3, 2018

Further thought about except Exception as e: pass blocks in plotting code: at bare minimum, add log.warn("Raised %s: %s"%(type(ex).__name__, e) before the pass, so we at least have a log of what was raised.

@r-owen
Copy link
Contributor Author

r-owen commented May 3, 2018

I fixed one try/except Exception in a unit test and am declaring that enough. The plotting code is riddled with them and I think it will take some care and exercising the code to understand where exceptions should be logged or their scope reduced.

@r-owen r-owen merged commit 4b67208 into master May 3, 2018
@ktlim ktlim deleted the tickets/DM-14253 branch August 25, 2018 06:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants