Skip to content

Commit

Permalink
Add tot flux measurements.
Browse files Browse the repository at this point in the history
Debug tot flux measurements.

Add None to docs.

Fix name of input column for Chi2.
  • Loading branch information
morriscb committed Oct 11, 2019
1 parent 4148b2c commit 8d2d6b7
Show file tree
Hide file tree
Showing 2 changed files with 222 additions and 16 deletions.
161 changes: 147 additions & 14 deletions python/lsst/ap/association/diaCalculationPlugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@
"MaxSlopeDiaPsFlux", "MaxSlopeDiaPsFluxConfig",
"ErrMeanDiaPsFlux", "ErrMeanDiaPsFluxConfig",
"LinearFitDiaPsFlux", "LinearFitDiaPsFluxConfig",
"StetsonJDiaPsFlux", "StetsonJDiaPsFluxConfig")
"StetsonJDiaPsFlux", "StetsonJDiaPsFluxConfig",
"WeightedMeanDiaTotFlux", "WeightedMeanDiaTotFluxConfig",
"SigmaDiaTotFlux", "SigmaDiaTotFluxConfig")


class MeanDiaPositionConfig(DiaObjectCalculationPluginConfig):
Expand Down Expand Up @@ -90,7 +92,7 @@ def fail(self, diaObject, error=None):
----------
diaObject : `dict`
Summary object to store values in.
error : `BaseException`
error : `BaseException` or `None`
Error to pass. Kept for consistency with CatologCalculationPlugin.
Unused.
"""
Expand Down Expand Up @@ -165,7 +167,7 @@ def fail(self, diaObject, filterName, error=None):
Summary object to store values in.
filterName : `str`
Simple name of the filter for the flux being calculated.
error : `BaseException`
error : `BaseException` or `None`
Error to pass.
"""
diaObject["{}PSFluxMean".format(filterName)] = np.nan
Expand Down Expand Up @@ -244,7 +246,7 @@ def fail(self, diaObject, filterName, error=None):
Summary object to store values in.
filterName : `str`
Simple name of the filter for the flux being calculated.
error : `BaseException`
error : `BaseException` or `None`
Error to pass.
"""
for pTile in self.config.percentiles:
Expand Down Expand Up @@ -305,7 +307,7 @@ def fail(self, diaObject, filterName, error=None):
Summary object to store values in.
filterName : `str`
Simple name of the filter for the flux being calculated.
error : `BaseException`
error : `BaseException` or `None`
Error to pass.
"""
diaObject["{}PSFluxSigma".format(filterName)] = np.nan
Expand All @@ -323,7 +325,7 @@ class Chi2DiaPsFlux(DiaObjectCalculationPlugin):
ConfigClass = Chi2DiaPsFluxConfig

# Required input Cols
inputCols = ["PSFlux"]
inputCols = ["PSFluxMean"]
# Output columns are created upon instantiation of the class.
outputCols = ["PSFluxChi2"]

Expand Down Expand Up @@ -369,7 +371,7 @@ def fail(self, diaObject, filterName, error=None):
Summary object to store values in.
filterName : `str`
Simple name of the filter for the flux being calculated.
error : `BaseException`
error : `BaseException` or `None`
Error to pass.
"""
diaObject["{}PSFluxChi2".format(filterName)] = np.nan
Expand Down Expand Up @@ -432,7 +434,7 @@ def fail(self, diaObject, filterName, error=None):
Summary object to store values in.
filterName : `str`
Simple name of the filter for the flux being calculated.
error : `BaseException`
error : `BaseException` or `None`
Error to pass.
"""
diaObject["{}PSFluxMAD".format(filterName)] = np.nan
Expand Down Expand Up @@ -495,7 +497,7 @@ def fail(self, diaObject, filterName, error=None):
Summary object to store values in.
filterName : `str`
Simple name of the filter for the flux being calculated.
error : `BaseException`
error : `BaseException` or `None`
Error to pass.
"""
diaObject["{}PSFluxSkew".format(filterName)] = np.nan
Expand Down Expand Up @@ -557,7 +559,7 @@ def fail(self, diaObject, filterName, error=None):
Summary object to store values in.
filterName : `str`
Simple name of the filter for the flux being calculated.
error : `BaseException`
error : `BaseException` or `None`
Error to pass.
"""
diaObject["{}PSFluxMin".format(filterName)] = np.nan
Expand Down Expand Up @@ -622,7 +624,7 @@ def fail(self, diaObject, filterName, error=None):
Summary object to store values in.
filterName : `str`
Simple name of the filter for the flux being calculated.
error : `BaseException`
error : `BaseException` or `None`
Error to pass.
"""
diaObject["{}PSFluxMaxSlope".format(filterName)] = np.nan
Expand Down Expand Up @@ -683,7 +685,7 @@ def fail(self, diaObject, filterName, error=None):
Summary object to store values in.
filterName : `str`
Simple name of the filter for the flux being calculated.
error : `BaseException`
error : `BaseException` or `None`
Error to pass.
"""
diaObject["{}PSFluxErrMean".format(filterName)] = np.nan
Expand Down Expand Up @@ -752,7 +754,7 @@ def fail(self, diaObject, filterName, error=None):
Summary object to store values in.
filterName : `str`
Simple name of the filter for the flux being calculated.
error : `BaseException`
error : `BaseException` or `None`
Error to pass.
"""
diaObject["{}PSFluxLinearSlope".format(filterName)] = np.nan
Expand Down Expand Up @@ -915,7 +917,138 @@ def fail(self, diaObject, filterName, error=None):
Summary object to store values in.
filterName : `str`
Simple name of the filter for the flux being calculated.
error : `BaseException`
error : `BaseException` or `None`
Error to pass.
"""
diaObject["{}PSFluxStetsonJ".format(filterName)] = np.nan


class WeightedMeanDiaTotFluxConfig(DiaObjectCalculationPluginConfig):
pass


@register("ap_meanTotFlux")
class WeightedMeanDiaTotFlux(DiaObjectCalculationPlugin):
"""Compute the weighted mean and mean error on the point source fluxes
forced photometered at the DiaSource location in the calibrated image.
Additionally store number of usable data points.
"""

ConfigClass = WeightedMeanDiaPsFluxConfig
outputCols = ["TOTFluxMean", "TOTFluxMeanErr"]

@classmethod
def getExecutionOrder(cls):
return cls.DEFAULT_CATALOGCALCULATION

def calculate(self,
diaObject,
diaSources,
filterDiaFluxes,
filterName,
**kwargs):
"""Compute the weighted mean and mean error of the point source flux.
Parameters
----------
diaObject : `dict`
Summary object to store values in.
diaSources : `pandas.DataFrame`
DataFrame representing all diaSources associated with this
diaObject.
filterDiaFluxes : `pandas.DataFrame`
DataFrame representing diaSources associated with this
diaObject that are observed in the band pass ``filterName``.
filterName : `str`
Simple, string name of the filter for the flux being calculated.
"""
if len(filterDiaFluxes) > 0:
tot_weight = np.nansum(1 / filterDiaFluxes["totFluxErr"] ** 2)
fluxMean = np.nansum(filterDiaFluxes["totFlux"] /
filterDiaFluxes["totFluxErr"] ** 2)
fluxMean /= tot_weight
fluxMeanErr = np.sqrt(1 / tot_weight)
else:
fluxMean = np.nan
fluxMeanErr = np.nan
if np.isfinite(fluxMean) and np.isfinite(fluxMeanErr):
diaObject["{}TOTFluxMean".format(filterName)] = fluxMean
diaObject["{}TOTFluxMeanErr".format(filterName)] = fluxMeanErr
else:
self.fail(diaObject, filterName)

def fail(self, diaObject, filterName, error=None):
"""Set diaObject position values to nan.
Parameters
----------
diaObject : `dict`
Summary object to store values in.
filterName : `str`
Simple name of the filter for the flux being calculated.
error : `BaseException` or `None`
Error to pass.
"""
diaObject["{}TOTFluxMean".format(filterName)] = np.nan
diaObject["{}TOTFluxMeanErr".format(filterName)] = np.nan


class SigmaDiaTotFluxConfig(DiaObjectCalculationPluginConfig):
pass


@register("ap_sigmaTotFlux")
class SigmaDiaTotFlux(DiaObjectCalculationPlugin):
"""Compute scatter of diaSource fluxes.
"""

ConfigClass = SigmaDiaPsFluxConfig
# Output columns are created upon instantiation of the class.
outputCols = ["TOTFluxSigma"]

@classmethod
def getExecutionOrder(cls):
return cls.DEFAULT_CATALOGCALCULATION

def calculate(self,
diaObject,
diaSources,
filterDiaFluxes,
filterName,
**kwargs):
"""Compute the sigma fluxes of the point source flux measured on the
calibrated image.
Parameters
----------
diaObject : `dict`
Summary object to store values in.
diaSources : `pandas.DataFrame`
DataFrame representing all diaSources associated with this
diaObject.
filterDiaFluxes : `pandas.DataFrame`
DataFrame representing diaSources associated with this
diaObject that are observed in the band pass ``filterName``.
filterName : `str`
Simple, string name of the filter for the flux being calculated.
"""
if len(filterDiaFluxes) > 1:
diaObject["{}TOTFluxSigma".format(filterName)] = np.nanstd(
filterDiaFluxes["totFlux"])
else:
self.fail(diaObject, filterName)

def fail(self, diaObject, filterName, error=None):
"""Set diaObject values to nan.
Parameters
----------
diaObject : `dict`
Summary object to store values in.
filterName : `str`
Simple name of the filter for the flux being calculated.
error : `BaseException` or `None`
Error to pass.
"""
diaObject["{}TOTFluxSigma".format(filterName)] = np.nan
77 changes: 75 additions & 2 deletions tests/test_dia_calculation_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@
MaxSlopeDiaPsFlux, MaxSlopeDiaPsFluxConfig,
ErrMeanDiaPsFlux, ErrMeanDiaPsFluxConfig,
LinearFitDiaPsFlux, LinearFitDiaPsFluxConfig,
StetsonJDiaPsFlux, StetsonJDiaPsFlux)
StetsonJDiaPsFlux, StetsonJDiaPsFluxConfig,
WeightedMeanDiaTotFlux, WeightedMeanDiaTotFluxConfig,
SigmaDiaTotFlux, SigmaDiaTotFluxConfig)
import lsst.utils.tests


Expand Down Expand Up @@ -472,7 +474,7 @@ def testCalculate(self):
"psFluxErr": errors,
"midPointTai": times})

plug = StetsonJDiaPsFlux(LinearFitDiaPsFluxConfig(),
plug = StetsonJDiaPsFlux(StetsonJDiaPsFluxConfig(),
"ap_LinearFit",
None)
plug.calculate(diaObject, diaSources, diaSources, "u")
Expand Down Expand Up @@ -508,6 +510,77 @@ def testCalculate(self):
-0.5412797916187173)


class TestWeightedMeanDiaTotFlux(unittest.TestCase):

def testCalculate(self):
"""Test mean value calculation.
"""
n_sources = 10
diaObject = dict()
diaSources = pd.DataFrame(data={"totFlux": np.linspace(-1, 1, n_sources),
"totFluxErr": np.ones(n_sources)})

plug = WeightedMeanDiaTotFlux(WeightedMeanDiaTotFluxConfig(),
"ap_meanTotFlux",
None)
plug.calculate(diaObject, diaSources, diaSources, "u")

self.assertAlmostEqual(diaObject["uTOTFluxMean"], 0.0)
self.assertAlmostEqual(diaObject["uTOTFluxMeanErr"], np.sqrt(1 / n_sources))

diaObject = dict()
plug.calculate(diaObject, [], [], "g")

self.assertTrue(np.isnan(diaObject["gTOTFluxMean"]))
self.assertTrue(np.isnan(diaObject["gTOTFluxMeanErr"]))

diaObject = dict()
diaSources.loc[4, "totFlux"] = np.nan
plug.calculate(diaObject, diaSources, diaSources, "r")

self.assertTrue(~np.isnan(diaObject["rTOTFluxMean"]))
self.assertTrue(~np.isnan(diaObject["rTOTFluxMeanErr"]))


class TestSigmaDiaTotFlux(unittest.TestCase):

def testCalculate(self):
"""Test flux scatter calculation.
"""
n_sources = 10
diaObject = dict()
fluxes = np.linspace(-1, 1, n_sources)
diaSources = pd.DataFrame(data={"totFlux": fluxes,
"totFluxErr": np.ones(n_sources)})

plug = SigmaDiaTotFlux(SigmaDiaTotFluxConfig(),
"ap_sigmaTotFlux",
None)
plug.calculate(diaObject, diaSources, diaSources, "u")
self.assertAlmostEqual(diaObject["uTOTFluxSigma"],
np.nanstd(fluxes))

# test no inputs
diaObject = dict()
plug.calculate(diaObject, [], [], "g")
self.assertTrue(np.isnan(diaObject["gTOTFluxSigma"]))

# test one input
diaObject = dict()
diaSources = pd.DataFrame(data={"totFlux": [fluxes[0]],
"totFluxErr": [np.ones(n_sources)[0]]})
plug.calculate(diaObject, diaSources, diaSources, "g")
self.assertTrue(np.isnan(diaObject["gTOTFluxSigma"]))

diaObject = dict()
fluxes[4] = np.nan
diaSources = pd.DataFrame(data={"totFlux": fluxes,
"totFluxErr": np.ones(n_sources)})
plug.calculate(diaObject, diaSources, diaSources, "r")
self.assertAlmostEqual(diaObject["rTOTFluxSigma"],
np.nanstd(fluxes))


class MemoryTester(lsst.utils.tests.MemoryTestCase):
pass

Expand Down

0 comments on commit 8d2d6b7

Please sign in to comment.