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-16957: Add pickle support to DataId class #112

Merged
merged 1 commit into from
Dec 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions python/lsst/daf/butler/core/dimensions/dataId.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,3 +339,23 @@ def fields(self, element, region=True, metadata=True):
return entries
return {k: v for k, v in entries.items()
if (metadata or k in self.keys()) and (region or k != "region")}

def __getnewargs_ex__(self):
"""Support special pickling for DataId.

Default unpickling code calls `__new__` without arguments which does
not work for this class, need to provide minimal set of arguments to
to support logic in `__new__` and to run it without error. Pickle
still executes its regular logic to save instance attributes and
restore their state (after creating new instance with `__new__`).

Returns
-------
args : `tuple`
Positional arguments for `__new__`.
kwargs : `dict`
Keyword arguments for `__new__`.
"""
args = (None,)
kwargs = dict(dimensions=self.dimensions)
return (args, kwargs)
73 changes: 73 additions & 0 deletions tests/test_dataid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# This file is part of daf_butler.
#
# Developed for the LSST Data Management System.
# This product includes software developed by the LSST Project
# (http://www.lsst.org).
# See the COPYRIGHT file at the top-level directory of this distribution
# for details of code ownership.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import unittest
import pickle

import lsst.utils
import lsst.utils.tests

from lsst.daf.butler import DataId, DimensionGraph


class DataIdTestCase(lsst.utils.tests.TestCase):
"""Tests for DataId.

All tests here rely on the content of ``config/dimensions.yaml``, either
to test that the definitions there are read in properly or just as generic
data for testing various operations.
"""

def setUp(self):
self.universe = DimensionGraph.fromConfig()

#
# TODO: more tests would be useful
#

def testConstructor(self):
"""Test for making new instances.
"""

dataId = DataId(dict(instrument="DummyInstrument",
detector=1,
visit=2,
physical_filter="i"),
universe=self.universe)
self.assertEqual(len(dataId), 4)
self.assertCountEqual(dataId.keys(),
("instrument", "detector", "visit", "physical_filter"))

def testPickle(self):
"""Test pickle support.
"""
dataId = DataId(dict(instrument="DummyInstrument",
detector=1,
visit=2,
physical_filter="i"),
universe=self.universe)
dataIdOut = pickle.loads(pickle.dumps(dataId))
self.assertIsInstance(dataIdOut, DataId)
self.assertEqual(dataId, dataIdOut)


if __name__ == "__main__":
unittest.main()