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

Rewrite code which tags complex objects #223

Merged
merged 5 commits into from
Oct 24, 2016
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
1.1.0 (Unreleased)
1.1.1(Unreleased)
-----------------

- Added Tabular model. [#214]

1.0.5 (2016-06-28)
------------------

- No changes yet.
- Fixed a memory leak when reading wcs that grew memory to over 10 Gb. [#200]

1.0.4 (2016-05-25)
------------------
Expand Down
2 changes: 1 addition & 1 deletion asdf/asdftypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ def to_tree_tagged(cls, node, ctx):
`to_tree`, allows types to customize how the result is tagged.
"""
obj = cls.to_tree(node, ctx)
return tagged.tag_object(cls.yaml_tag, obj)
return tagged.tag_object(cls.yaml_tag, obj, ctx=ctx)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't obj already containing ctx? Does it need to be passed separately and wouldn't this increase memory usage?


@classmethod
def from_tree(cls, tree, ctx):
Expand Down
31 changes: 12 additions & 19 deletions asdf/tagged.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@

import six

from astropy import time
from .compat import UserDict, UserList, UserString

__all__ = ['tag_object', 'get_tag']
Expand Down Expand Up @@ -98,35 +97,29 @@ def __eq__(self, other):
self._tag == other._tag)


class TaggedTime(Tagged, time.Time):
"""
An Astropy time object with a tag attached.
"""
def __new__(cls, instance, tag):
self = time.Time.__new__(type(instance), instance)
self._tag = tag
return self

def tag_object(tag, instance):
def tag_object(tag, instance, ctx=None):
"""
Tag an object by wrapping it in a ``Tagged`` instance.
"""
if isinstance(instance, Tagged):
instance._tag = tag
return instance
elif isinstance(instance, dict):
return TaggedDict(instance, tag)
instance = TaggedDict(instance, tag)
elif isinstance(instance, list):
return TaggedList(instance, tag)
elif isinstance(instance, time.Time):
return TaggedTime(instance, tag)
instance = TaggedList(instance, tag)
elif isinstance(instance, six.string_types):
instance = TaggedString(instance)
instance._tag = tag
return instance
else:
raise TypeError(
"Don't know how to tag a {0}".format(type(instance)))
from . import AsdfFile, yamlutil
if ctx is None:
ctx = AsdfFile()
try:
instance = yamlutil.custom_tree_to_tagged_tree(instance, ctx)
except TypeError:
raise TypeError("Don't know how to tag a {0}".format(type(instance)))
instance._tag = tag
return instance


def get_tag(instance):
Expand Down
3 changes: 2 additions & 1 deletion asdf/tags/transform/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ def _to_tree_base_transform_members(cls, model, node, ctx):
# domains, get this from somewhere else.
domain = model.meta.get('domain')
if domain:
domain = [tagged.tag_object(DomainType.yaml_tag, x) for x in domain]
domain = [tagged.tag_object(DomainType.yaml_tag, x, ctx=ctx)
for x in domain]
node['domain'] = domain

@classmethod
Expand Down
2 changes: 1 addition & 1 deletion asdf/tags/transform/compound.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def _to_tree_from_model_tree(cls, tree, ctx):
except KeyError:
raise ValueError("Unknown operator '{0}'".format(tree.value))

node = tagged.tag_object(cls.make_yaml_tag(tag_name), node)
node = tagged.tag_object(cls.make_yaml_tag(tag_name), node, ctx=ctx)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm. The CompoundType represents a compound model of type astropy.modeling.core.CompoundModel. In my experience it never fails serialization. Why does it need to be wrapped? I may be wrong but my understanding is that the intention was to tag only python primitive types.
It would really help if there's a failing example which illustrates the problem.

return node

@classmethod
Expand Down
2 changes: 1 addition & 1 deletion asdf/tests/test_generic_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ def get_read_fd():
if len(tree) == 4:
assert connection[0]._nreads == 0
else:
assert connection[0]._nreads == 5
assert connection[0]._nreads == 6

assert len(list(ff.blocks.internal_blocks)) == 2
assert isinstance(next(ff.blocks.internal_blocks)._data, np.core.memmap)
Expand Down
120 changes: 120 additions & 0 deletions asdf/tests/test_tagged.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import io
import six
import datetime
from astropy.time import Time
from collections import OrderedDict

from .. import tagged
from .. import yamlutil
from .. import AsdfFile
from .. import schema as asdf_schema
from jsonschema import ValidationError

def walk_schema(schema, callback, ctx={}):
def recurse(schema, path, combiner, ctx):
if callback(schema, path, combiner, ctx, recurse):
return

for c in ['allOf', 'not']:
for sub in schema.get(c, []):
recurse(sub, path, c, ctx)

for c in ['anyOf', 'oneOf']:
for i, sub in enumerate(schema.get(c, [])):
recurse(sub, path + [i], c, ctx)

if schema.get('type') == 'object':
for key, val in six.iteritems(schema.get('properties', {})):
recurse(val, path + [key], combiner, ctx)

if schema.get('type') == 'array':
items = schema.get('items', {})
if isinstance(items, list):
for i, item in enumerate(items):
recurse(item, path + [i], combiner, ctx)
elif len(items):
recurse(items, path + ['items'], combiner, ctx)

recurse(schema, [], None, ctx)


def flatten_combiners(schema):
newschema = OrderedDict()

def add_entry(path, schema, combiner):
# TODO: Simplify?
cursor = newschema
for i in range(len(path)):
part = path[i]
if isinstance(part, int):
cursor = cursor.setdefault('items', [])
while len(cursor) <= part:
cursor.append({})
cursor = cursor[part]
elif part == 'items':
cursor = cursor.setdefault('items', OrderedDict())
else:
cursor = cursor.setdefault('properties', OrderedDict())
if i < len(path) - 1 and isinstance(path[i+1], int):
cursor = cursor.setdefault(part, [])
else:
cursor = cursor.setdefault(part, OrderedDict())

cursor.update(schema)

def callback(schema, path, combiner, ctx, recurse):
type = schema.get('type')
schema = OrderedDict(schema)
if type == 'object':
del schema['properties']
elif type == 'array':
del schema['items']
if 'allOf' in schema:
del schema['allOf']
if 'anyOf' in schema:
del schema['anyOf']

add_entry(path, schema, combiner)

walk_schema(schema, callback)

return newschema


def test_time_tag():
schema = asdf_schema.load_schema('http://stsci.edu/schemas/asdf/time/time-1.0.0',
resolve_references=True)
schema = flatten_combiners(schema)

date = Time(datetime.datetime.now())
tree = {'date': date}
asdf = AsdfFile(tree=tree)
instance = yamlutil.custom_tree_to_tagged_tree(tree, asdf)

try:
asdf_schema.validate(instance, schema=schema)
except ValidationError:
fail = True
else:
fail = False

assert fail, True

tag = 'tag:stsci.edu:asdf/time/time-1.0.0'
date = tagged.tag_object(tag, date)
tree = {'date': date}
instance = yamlutil.custom_tree_to_tagged_tree(tree, asdf)

try:
asdf_schema.validate(instance, schema=schema)
except ValidationError:
fail = True
else:
fail = False

assert fail, False

if __name__ == "__main__":
import pdb
pdb.set_trace()
test_time_tag()