Skip to content

Commit

Permalink
Add warning filters in context manager.
Browse files Browse the repository at this point in the history
Fix linting.

Create decorator for cathing numpy warnings.

move to python warnings.

Fix context bug.

Remove blank line
  • Loading branch information
morriscb committed Jan 9, 2020
1 parent 9dd679e commit d66c66d
Showing 1 changed file with 33 additions and 4 deletions.
37 changes: 33 additions & 4 deletions python/lsst/ap/association/diaCalculationPlugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
as defined in the schema of the Apdb both in name and units.
"""

import functools
import warnings

from astropy.stats import median_absolute_deviation
import numpy as np
import pandas as pd
Expand Down Expand Up @@ -58,6 +61,24 @@
"SigmaDiaTotFlux", "SigmaDiaTotFluxConfig")


def catchWarnings(_func=None, *, warns=[]):
"""Decorator for generically catching numpy warnings.
"""
def decoratorCatchWarnings(func):
@functools.wraps(func)
def wrapperCatchWarnings(*args, **kwargs):
with warnings.catch_warnings():
for val in warns:
warnings.filterwarnings("ignore", val)
return func(*args, **kwargs)
return wrapperCatchWarnings

if _func is None:
return decoratorCatchWarnings
else:
return decoratorCatchWarnings(_func)


class MeanDiaPositionConfig(DiaObjectCalculationPluginConfig):
pass

Expand Down Expand Up @@ -244,6 +265,8 @@ class WeightedMeanDiaPsFlux(DiaObjectCalculationPlugin):
def getExecutionOrder(cls):
return cls.DEFAULT_CATALOGCALCULATION

@catchWarnings(warns=["invalid value encountered",
"divide by zero"])
def calculate(self,
diaObjects,
diaSources,
Expand Down Expand Up @@ -327,6 +350,7 @@ def __init__(self, config, name, metadata, **kwargs):
def getExecutionOrder(cls):
return cls.DEFAULT_CATALOGCALCULATION

@catchWarnings(warns=["All-NaN slice encountered"])
def calculate(self,
diaObjects,
diaSources,
Expand Down Expand Up @@ -360,8 +384,9 @@ def calculate(self,

def _fluxPercentiles(df):
pTiles = np.nanpercentile(df["psFlux"], self.config.percentiles)
return pd.Series(dict((tileName, pTile)
for tileName, pTile in zip(pTileNames, pTiles)))
return pd.Series(
dict((tileName, pTile)
for tileName, pTile in zip(pTileNames, pTiles)))

diaObjects.loc[:, pTileNames] = filterDiaSources.apply(_fluxPercentiles)

Expand Down Expand Up @@ -434,6 +459,7 @@ class Chi2DiaPsFlux(DiaObjectCalculationPlugin):
def getExecutionOrder(cls):
return cls.FLUX_MOMENTS_CALCULATED

@catchWarnings(warns=["All-NaN slice encountered"])
def calculate(self,
diaObjects,
diaSources,
Expand Down Expand Up @@ -488,6 +514,7 @@ class MadDiaPsFlux(DiaObjectCalculationPlugin):
def getExecutionOrder(cls):
return cls.DEFAULT_CATALOGCALCULATION

@catchWarnings(warns=["All-NaN slice encountered"])
def calculate(self,
diaObjects,
diaSources,
Expand Down Expand Up @@ -966,6 +993,8 @@ class WeightedMeanDiaTotFlux(DiaObjectCalculationPlugin):
def getExecutionOrder(cls):
return cls.DEFAULT_CATALOGCALCULATION

@catchWarnings(warns=["invalid value encountered",
"divide by zero"])
def calculate(self,
diaObjects,
diaSources,
Expand Down Expand Up @@ -1008,8 +1037,8 @@ def _meanFlux(df):
return pd.Series({totMeanName: fluxMean,
totErrName: fluxMeanErr})

diaObjects.loc[:, [totMeanName, totErrName]] = filterDiaSources.apply(
_meanFlux)
diaObjects.loc[:, [totMeanName, totErrName]] = \
filterDiaSources.apply(_meanFlux)


class SigmaDiaTotFluxConfig(DiaObjectCalculationPluginConfig):
Expand Down

0 comments on commit d66c66d

Please sign in to comment.