Skip to content

Commit

Permalink
Ignore many numpy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
taranu committed Oct 12, 2023
1 parent 9cc89a0 commit b58d41e
Showing 1 changed file with 48 additions and 31 deletions.
79 changes: 48 additions & 31 deletions python/lsst/analysis/tools/interfaces/_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

__all__ = ("AnalysisBaseConnections", "AnalysisBaseConfig", "AnalysisPipelineTask")

import warnings
import weakref
from collections.abc import Iterable
from copy import deepcopy
Expand Down Expand Up @@ -276,38 +277,54 @@ class AnalysisPipelineTask(PipelineTask):
config: AnalysisBaseConfig
ConfigClass = AnalysisBaseConfig

warnings_ignore = (
"divide by zero encountered in divide",
"invalid value encountered in arcsin",
"invalid value encountered in cos",
"invalid value encountered in divide",
"invalid value encountered in log10",
"invalid value encountered in scalar divide",
"invalid value encountered in sin",
"invalid value encountered in sqrt",
"invalid value encountered in true_divide",
"Mean of empty slice",
)

def _runTools(self, data: KeyedData, **kwargs) -> Struct:
results = Struct()
results.metrics = MetricMeasurementBundle(
dataset_identifier=self.config.dataset_identifier,
reference_package=self.config.reference_package,
timestamp_version=self.config.timestamp_version,
)
# copy plot info to be sure each action sees its own copy
plotInfo = kwargs.get("plotInfo")
plotKey = f"{self.config.connections.outputName}_{{name}}"
weakrefArgs = []
for name, action in self.config.atools.items():
kwargs["plotInfo"] = deepcopy(plotInfo)
actionResult = action(data, **kwargs)
metricAccumulate = []
for resultName, value in actionResult.items():
match value:
case PlotTypes():
setattr(results, plotKey.format(name=resultName), value)
weakrefArgs.append(value)
case Measurement():
metricAccumulate.append(value)
# only add the metrics if there are some
if metricAccumulate:
results.metrics[name] = metricAccumulate
# Wrap the return struct in a finalizer so that when results is
# garbage collected the plots will be closed.
# TODO: This finalize step closes all open plots at the conclusion of
# a task. When DM-39114 is implemented, this step should no longer be
# required and may be removed.
weakref.finalize(results, _plotCloser, *weakrefArgs)
return results
with warnings.catch_warnings():
for warning in self.warnings_ignore:
warnings.filterwarnings("ignore", warning, RuntimeWarning)
results = Struct()
results.metrics = MetricMeasurementBundle(
dataset_identifier=self.config.dataset_identifier,
reference_package=self.config.reference_package,
timestamp_version=self.config.timestamp_version,
)
# copy plot info to be sure each action sees its own copy
plotInfo = kwargs.get("plotInfo")
plotKey = f"{self.config.connections.outputName}_{{name}}"
weakrefArgs = []
for name, action in self.config.atools.items():
kwargs["plotInfo"] = deepcopy(plotInfo)
actionResult = action(data, **kwargs)
metricAccumulate = []
for resultName, value in actionResult.items():
match value:
case PlotTypes():
setattr(results, plotKey.format(name=resultName), value)
weakrefArgs.append(value)
case Measurement():
metricAccumulate.append(value)
# only add the metrics if there are some
if metricAccumulate:
results.metrics[name] = metricAccumulate
# Wrap the return struct in a finalizer so that when results is
# garbage collected the plots will be closed.
# TODO: This finalize step closes all open plots at the conclusion
# of a task. When DM-39114 is implemented, this step should not
# be required and may be removed.
weakref.finalize(results, _plotCloser, *weakrefArgs)
return results

def run(self, *, data: KeyedData | None = None, **kwargs) -> Struct:
"""Produce the outputs associated with this `PipelineTask`.
Expand Down

0 comments on commit b58d41e

Please sign in to comment.