LSST Applications g034a557a3c+dd8dd8f11d,g0afe43252f+b86e4b8053,g11f7dcd041+017865fdd3,g1cd03abf6b+8446defddb,g1ce3e0751c+f991eae79d,g28da252d5a+ca8a1a9fb3,g2bbee38e9b+b6588ad223,g2bc492864f+b6588ad223,g2cdde0e794+8523d0dbb4,g347aa1857d+b6588ad223,g35bb328faa+b86e4b8053,g3a166c0a6a+b6588ad223,g461a3dce89+b86e4b8053,g52b1c1532d+b86e4b8053,g7f3b0d46df+ad13c1b82d,g80478fca09+f29c5d6c70,g858d7b2824+293f439f82,g8cd86fa7b1+af721d2595,g965a9036f2+293f439f82,g979bb04a14+51ed57f74c,g9ddcbc5298+f24b38b85a,gae0086650b+b86e4b8053,gbb886bcc26+b97e247655,gc28159a63d+b6588ad223,gc30aee3386+a2f0f6cab9,gcaf7e4fdec+293f439f82,gcd45df26be+293f439f82,gcdd4ae20e8+70b5def7e6,gce08ada175+da9c58a417,gcf0d15dbbd+70b5def7e6,gdaeeff99f8+006e14e809,gdbce86181e+6a170ce272,ge3d4d395c2+224150c836,ge5f7162a3a+bb2241c923,ge6cb8fbbf7+d119aed356,ge79ae78c31+b6588ad223,gf048a9a2f4+40ffced2b8,gf0baf85859+b4cca3d10f,w.2024.30
LSST Data Management Base Package
Loading...
Searching...
No Matches
Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | Static Protected Attributes | List of all members
lsst.pipe.tasks.functors.Functor Class Reference
Inheritance diagram for lsst.pipe.tasks.functors.Functor:
lsst.pipe.tasks.functors.Color lsst.pipe.tasks.functors.Column lsst.pipe.tasks.functors.CompositeFunctor lsst.pipe.tasks.functors.CustomFunctor lsst.pipe.tasks.functors.DeconvolvedMoments lsst.pipe.tasks.functors.E1 lsst.pipe.tasks.functors.E2 lsst.pipe.tasks.functors.Ebv lsst.pipe.tasks.functors.HsmFwhm lsst.pipe.tasks.functors.HsmTraceSize lsst.pipe.tasks.functors.HtmIndex20 lsst.pipe.tasks.functors.Index lsst.pipe.tasks.functors.LocalPhotometry lsst.pipe.tasks.functors.LocalWcs lsst.pipe.tasks.functors.Mag lsst.pipe.tasks.functors.MagDiff lsst.pipe.tasks.functors.Photometry lsst.pipe.tasks.functors.PsfHsmTraceSizeDiff lsst.pipe.tasks.functors.PsfSdssTraceSizeDiff lsst.pipe.tasks.functors.RadiusFromQuadrupole lsst.pipe.tasks.functors.ReferenceBand lsst.pipe.tasks.functors.SdssTraceSize

Public Member Functions

 __init__ (self, filt=None, dataset=None, noDup=None)
 
 noDup (self)
 
 columns (self)
 
 multilevelColumns (self, data, columnIndex=None, returnTuple=False)
 
 __call__ (self, data, dropna=False)
 
 difference (self, data1, data2, **kwargs)
 
 fail (self, df)
 
 name (self)
 
 shortname (self)
 

Public Attributes

 filt
 
 dataset
 
 log
 
 name
 

Protected Member Functions

 _get_data_columnLevels (self, data, columnIndex=None)
 
 _get_data_columnLevelNames (self, data, columnIndex=None)
 
 _colsFromDict (self, colDict, columnIndex=None)
 
 _func (self, df, dropna=True)
 
 _get_columnIndex (self, data)
 
 _get_data (self, data)
 
 _setLevels (self, df)
 
 _dropna (self, vals)
 

Protected Attributes

 _noDup
 

Static Protected Attributes

str _defaultDataset = 'ref'
 
tuple _dfLevels = ('column',)
 
bool _defaultNoDup = False
 

Detailed Description

Define and execute a calculation on a DataFrame or Handle holding a
DataFrame.

The `__call__` method accepts either a `~pandas.DataFrame` object or a
`~lsst.daf.butler.DeferredDatasetHandle` or
`~lsst.pipe.base.InMemoryDatasetHandle`, and returns the
result of the calculation as a single column.
Each functor defines what columns are needed for the calculation, and only
these columns are read from the dataset handle.

The action of `__call__` consists of two steps: first, loading the
necessary columns from disk into memory as a `~pandas.DataFrame` object;
and second, performing the computation on this DataFrame and returning the
result.

To define a new `Functor`, a subclass must define a `_func` method,
that takes a `~pandas.DataFrame` and returns result in a `~pandas.Series`.
In addition, it must define the following attributes:

* `_columns`: The columns necessary to perform the calculation
* `name`: A name appropriate for a figure axis label
* `shortname`: A name appropriate for use as a dictionary key

On initialization, a `Functor` should declare what band (``filt`` kwarg)
and dataset (e.g. ``'ref'``, ``'meas'``, ``'forced_src'``) it is intended
to be applied to.
This enables the `_get_data` method to extract the proper columns from the
underlying data.
If not specified, the dataset will fall back on the `_defaultDataset`
attribute.
If band is not specified and ``dataset`` is anything other than ``'ref'``,
then an error will be raised when trying to perform the calculation.

Originally, `Functor` was set up to expect datasets formatted like the
``deepCoadd_obj`` dataset; that is, a DataFrame with a multi-level column
index, with the levels of the column index being ``band``, ``dataset``, and
``column``.
It has since been generalized to apply to DataFrames without multi-level
indices and multi-level indices with just ``dataset`` and ``column``
levels.
In addition, the `_get_data` method that reads the columns from the
underlying data will return a DataFrame with column index levels defined by
the `_dfLevels` attribute; by default, this is ``column``.

The `_dfLevels` attributes should generally not need to be changed, unless
`_func` needs columns from multiple filters or datasets to do the
calculation.
An example of this is the `~lsst.pipe.tasks.functors.Color` functor, for
which `_dfLevels = ('band', 'column')`, and `_func` expects the DataFrame
it gets to have those levels in the column index.

Parameters
----------
filt : str
    Band upon which to do the calculation.

dataset : str
    Dataset upon which to do the calculation (e.g., 'ref', 'meas',
    'forced_src').

Definition at line 97 of file functors.py.

Constructor & Destructor Documentation

◆ __init__()

lsst.pipe.tasks.functors.Functor.__init__ ( self,
filt = None,
dataset = None,
noDup = None )

Member Function Documentation

◆ __call__()

lsst.pipe.tasks.functors.Functor.__call__ ( self,
data,
dropna = False )

Reimplemented in lsst.pipe.tasks.functors.RAColumn, lsst.pipe.tasks.functors.DecColumn, and lsst.pipe.tasks.functors.CompositeFunctor.

Definition at line 348 of file functors.py.

348 def __call__(self, data, dropna=False):
349 df = self._get_data(data)
350 try:
351 vals = self._func(df)
352 except Exception as e:
353 self.log.error("Exception in %s call: %s: %s", self.name, type(e).__name__, e)
354 vals = self.fail(df)
355 if dropna:
356 vals = self._dropna(vals)
357
358 return vals
359

◆ _colsFromDict()

lsst.pipe.tasks.functors.Functor._colsFromDict ( self,
colDict,
columnIndex = None )
protected
Converts dictionary column specficiation to a list of columns.

Definition at line 218 of file functors.py.

218 def _colsFromDict(self, colDict, columnIndex=None):
219 """Converts dictionary column specficiation to a list of columns."""
220 new_colDict = {}
221 columnLevels = self._get_data_columnLevels(None, columnIndex=columnIndex)
222
223 for i, lev in enumerate(columnLevels):
224 if lev in colDict:
225 if isinstance(colDict[lev], str):
226 new_colDict[lev] = [colDict[lev]]
227 else:
228 new_colDict[lev] = colDict[lev]
229 else:
230 new_colDict[lev] = columnIndex.levels[i]
231
232 levelCols = [new_colDict[lev] for lev in columnLevels]
233 cols = list(product(*levelCols))
234 colsAvailable = [col for col in cols if col in columnIndex]
235 return colsAvailable
236

◆ _dropna()

lsst.pipe.tasks.functors.Functor._dropna ( self,
vals )
protected

Definition at line 345 of file functors.py.

345 def _dropna(self, vals):
346 return vals.dropna()
347

◆ _func()

lsst.pipe.tasks.functors.Functor._func ( self,
df,
dropna = True )
protected

Reimplemented in lsst.pipe.tasks.functors.CustomFunctor, lsst.pipe.tasks.functors.Column, lsst.pipe.tasks.functors.Index, lsst.pipe.tasks.functors.CoordColumn, lsst.pipe.tasks.functors.RADecCovColumn, lsst.pipe.tasks.functors.HtmIndex20, lsst.pipe.tasks.functors.Mag, lsst.pipe.tasks.functors.MagErr, lsst.pipe.tasks.functors.MagDiff, lsst.pipe.tasks.functors.Color, lsst.pipe.tasks.functors.DeconvolvedMoments, lsst.pipe.tasks.functors.SdssTraceSize, lsst.pipe.tasks.functors.PsfSdssTraceSizeDiff, lsst.pipe.tasks.functors.HsmTraceSize, lsst.pipe.tasks.functors.PsfHsmTraceSizeDiff, lsst.pipe.tasks.functors.HsmFwhm, lsst.pipe.tasks.functors.E1, lsst.pipe.tasks.functors.E2, lsst.pipe.tasks.functors.RadiusFromQuadrupole, lsst.pipe.tasks.functors.ComputePixelScale, lsst.pipe.tasks.functors.ConvertPixelToArcseconds, lsst.pipe.tasks.functors.ConvertPixelSqToArcsecondsSq, lsst.pipe.tasks.functors.ConvertDetectorAngleToPositionAngle, lsst.pipe.tasks.functors.NanoJansky, lsst.pipe.tasks.functors.NanoJanskyErr, lsst.pipe.tasks.functors.LocalNanojansky, lsst.pipe.tasks.functors.LocalNanojanskyErr, lsst.pipe.tasks.functors.LocalDipoleMeanFlux, lsst.pipe.tasks.functors.LocalDipoleMeanFluxErr, lsst.pipe.tasks.functors.LocalDipoleDiffFlux, lsst.pipe.tasks.functors.LocalDipoleDiffFluxErr, lsst.pipe.tasks.functors.Ebv, and lsst.pipe.tasks.functors.ReferenceBand.

Definition at line 291 of file functors.py.

291 def _func(self, df, dropna=True):
292 raise NotImplementedError('Must define calculation on DataFrame')
293

◆ _get_columnIndex()

lsst.pipe.tasks.functors.Functor._get_columnIndex ( self,
data )
protected
Return columnIndex.

Definition at line 294 of file functors.py.

294 def _get_columnIndex(self, data):
295 """Return columnIndex."""
296
297 if isinstance(data, (DeferredDatasetHandle, InMemoryDatasetHandle)):
298 return data.get(component="columns")
299 else:
300 return None
301

◆ _get_data()

lsst.pipe.tasks.functors.Functor._get_data ( self,
data )
protected
Retrieve DataFrame necessary for calculation.

The data argument can be a `~pandas.DataFrame`, a
`~lsst.daf.butler.DeferredDatasetHandle`, or
an `~lsst.pipe.base.InMemoryDatasetHandle`.

Returns a DataFrame upon which `self._func` can act.

Definition at line 302 of file functors.py.

302 def _get_data(self, data):
303 """Retrieve DataFrame necessary for calculation.
304
305 The data argument can be a `~pandas.DataFrame`, a
306 `~lsst.daf.butler.DeferredDatasetHandle`, or
307 an `~lsst.pipe.base.InMemoryDatasetHandle`.
308
309 Returns a DataFrame upon which `self._func` can act.
310 """
311 # We wrap a DataFrame in a handle here to take advantage of the
312 # DataFrame delegate DataFrame column wrangling abilities.
313 if isinstance(data, pd.DataFrame):
314 _data = InMemoryDatasetHandle(data, storageClass="DataFrame")
315 elif isinstance(data, (DeferredDatasetHandle, InMemoryDatasetHandle)):
316 _data = data
317 else:
318 raise RuntimeError(f"Unexpected type provided for data. Got {get_full_type_name(data)}.")
319
320 # First thing to do: check to see if the data source has a multilevel
321 # column index or not.
322 columnIndex = self._get_columnIndex(_data)
323 is_multiLevel = isinstance(columnIndex, pd.MultiIndex)
324
325 # Get proper columns specification for this functor.
326 if is_multiLevel:
327 columns = self.multilevelColumns(_data, columnIndex=columnIndex)
328 else:
329 columns = self.columns
330
331 # Load in-memory DataFrame with appropriate columns the gen3 way.
332 df = _data.get(parameters={"columns": columns})
333
334 # Drop unnecessary column levels.
335 if is_multiLevel:
336 df = self._setLevels(df)
337
338 return df
339

◆ _get_data_columnLevelNames()

lsst.pipe.tasks.functors.Functor._get_data_columnLevelNames ( self,
data,
columnIndex = None )
protected
Gets the content of each of the column levels for a multilevel
table.

Definition at line 204 of file functors.py.

204 def _get_data_columnLevelNames(self, data, columnIndex=None):
205 """Gets the content of each of the column levels for a multilevel
206 table.
207 """
208 if columnIndex is None:
209 columnIndex = data.get(component="columns")
210
211 columnLevels = columnIndex.names
212 columnLevelNames = {
213 level: list(np.unique(np.array([c for c in columnIndex])[:, i]))
214 for i, level in enumerate(columnLevels)
215 }
216 return columnLevelNames
217

◆ _get_data_columnLevels()

lsst.pipe.tasks.functors.Functor._get_data_columnLevels ( self,
data,
columnIndex = None )
protected
Gets the names of the column index levels.

This should only be called in the context of a multilevel table.

Parameters
----------
data : various
    The data to be read, can be a
    `~lsst.daf.butler.DeferredDatasetHandle` or
    `~lsst.pipe.base.InMemoryDatasetHandle`.
columnIndex (optional): pandas `~pandas.Index` object
    If not passed, then it is read from the
    `~lsst.daf.butler.DeferredDatasetHandle`
    for `~lsst.pipe.base.InMemoryDatasetHandle`.

Definition at line 184 of file functors.py.

184 def _get_data_columnLevels(self, data, columnIndex=None):
185 """Gets the names of the column index levels.
186
187 This should only be called in the context of a multilevel table.
188
189 Parameters
190 ----------
191 data : various
192 The data to be read, can be a
193 `~lsst.daf.butler.DeferredDatasetHandle` or
194 `~lsst.pipe.base.InMemoryDatasetHandle`.
195 columnIndex (optional): pandas `~pandas.Index` object
196 If not passed, then it is read from the
197 `~lsst.daf.butler.DeferredDatasetHandle`
198 for `~lsst.pipe.base.InMemoryDatasetHandle`.
199 """
200 if columnIndex is None:
201 columnIndex = data.get(component="columns")
202 return columnIndex.names
203

◆ _setLevels()

lsst.pipe.tasks.functors.Functor._setLevels ( self,
df )
protected

Definition at line 340 of file functors.py.

340 def _setLevels(self, df):
341 levelsToDrop = [n for n in df.columns.names if n not in self._dfLevels]
342 df.columns = df.columns.droplevel(levelsToDrop)
343 return df
344

◆ columns()

lsst.pipe.tasks.functors.Functor.columns ( self)

◆ difference()

lsst.pipe.tasks.functors.Functor.difference ( self,
data1,
data2,
** kwargs )
Computes difference between functor called on two different
DataFrame/Handle objects.

Definition at line 360 of file functors.py.

360 def difference(self, data1, data2, **kwargs):
361 """Computes difference between functor called on two different
362 DataFrame/Handle objects.
363 """
364 return self(data1, **kwargs) - self(data2, **kwargs)
365

◆ fail()

lsst.pipe.tasks.functors.Functor.fail ( self,
df )

Definition at line 366 of file functors.py.

366 def fail(self, df):
367 return pd.Series(np.full(len(df), np.nan), index=df.index)
368

◆ multilevelColumns()

lsst.pipe.tasks.functors.Functor.multilevelColumns ( self,
data,
columnIndex = None,
returnTuple = False )
Returns columns needed by functor from multilevel dataset.

To access tables with multilevel column structure, the
`~lsst.daf.butler.DeferredDatasetHandle` or
`~lsst.pipe.base.InMemoryDatasetHandle` needs to be passed
either a list of tuples or a dictionary.

Parameters
----------
data : various
    The data as either `~lsst.daf.butler.DeferredDatasetHandle`, or
    `~lsst.pipe.base.InMemoryDatasetHandle`.
columnIndex (optional): pandas `~pandas.Index` object
    Either passed or read in from
    `~lsst.daf.butler.DeferredDatasetHandle`.
`returnTuple` : `bool`
    If true, then return a list of tuples rather than the column
    dictionary specification.
    This is set to `True` by `CompositeFunctor` in order to be able to
    combine columns from the various component functors.

Reimplemented in lsst.pipe.tasks.functors.CompositeFunctor, and lsst.pipe.tasks.functors.Color.

Definition at line 237 of file functors.py.

237 def multilevelColumns(self, data, columnIndex=None, returnTuple=False):
238 """Returns columns needed by functor from multilevel dataset.
239
240 To access tables with multilevel column structure, the
241 `~lsst.daf.butler.DeferredDatasetHandle` or
242 `~lsst.pipe.base.InMemoryDatasetHandle` needs to be passed
243 either a list of tuples or a dictionary.
244
245 Parameters
246 ----------
247 data : various
248 The data as either `~lsst.daf.butler.DeferredDatasetHandle`, or
249 `~lsst.pipe.base.InMemoryDatasetHandle`.
250 columnIndex (optional): pandas `~pandas.Index` object
251 Either passed or read in from
252 `~lsst.daf.butler.DeferredDatasetHandle`.
253 `returnTuple` : `bool`
254 If true, then return a list of tuples rather than the column
255 dictionary specification.
256 This is set to `True` by `CompositeFunctor` in order to be able to
257 combine columns from the various component functors.
258
259 """
260 if not isinstance(data, (DeferredDatasetHandle, InMemoryDatasetHandle)):
261 raise RuntimeError(f"Unexpected data type. Got {get_full_type_name(data)}.")
262
263 if columnIndex is None:
264 columnIndex = data.get(component="columns")
265
266 # Confirm that the dataset has the column levels the functor is
267 # expecting it to have.
268 columnLevels = self._get_data_columnLevels(data, columnIndex)
269
270 columnDict = {'column': self.columns,
271 'dataset': self.dataset}
272 if self.filt is None:
273 columnLevelNames = self._get_data_columnLevelNames(data, columnIndex)
274 if "band" in columnLevels:
275 if self.dataset == "ref":
276 columnDict["band"] = columnLevelNames["band"][0]
277 else:
278 raise ValueError(f"'filt' not set for functor {self.name}"
279 f"(dataset {self.dataset}) "
280 "and DataFrame "
281 "contains multiple filters in column index. "
282 "Set 'filt' or set 'dataset' to 'ref'.")
283 else:
284 columnDict['band'] = self.filt
285
286 if returnTuple:
287 return self._colsFromDict(columnDict, columnIndex=columnIndex)
288 else:
289 return columnDict
290

◆ name()

lsst.pipe.tasks.functors.Functor.name ( self)

◆ noDup()

lsst.pipe.tasks.functors.Functor.noDup ( self)
Do not explode by band if used on object table.

Definition at line 170 of file functors.py.

170 def noDup(self):
171 """Do not explode by band if used on object table."""
172 if self._noDup is not None:
173 return self._noDup
174 else:
175 return self._defaultNoDup
176

◆ shortname()

lsst.pipe.tasks.functors.Functor.shortname ( self)
Short name of functor (suitable for column name/dict key).

Reimplemented in lsst.pipe.tasks.functors.MagDiff, and lsst.pipe.tasks.functors.Color.

Definition at line 375 of file functors.py.

375 def shortname(self):
376 """Short name of functor (suitable for column name/dict key)."""
377 return self.name
378
379

Member Data Documentation

◆ _defaultDataset

str lsst.pipe.tasks.functors.Functor._defaultDataset = 'ref'
staticprotected

Definition at line 159 of file functors.py.

◆ _defaultNoDup

bool lsst.pipe.tasks.functors.Functor._defaultNoDup = False
staticprotected

Definition at line 161 of file functors.py.

◆ _dfLevels

tuple lsst.pipe.tasks.functors.Functor._dfLevels = ('column',)
staticprotected

Definition at line 160 of file functors.py.

◆ _noDup

lsst.pipe.tasks.functors.Functor._noDup
protected

Definition at line 166 of file functors.py.

◆ dataset

lsst.pipe.tasks.functors.Functor.dataset

Definition at line 165 of file functors.py.

◆ filt

lsst.pipe.tasks.functors.Functor.filt

◆ log

lsst.pipe.tasks.functors.Functor.log

Definition at line 167 of file functors.py.

◆ name

lsst.pipe.tasks.functors.Functor.name

The documentation for this class was generated from the following file: