|
15 | 15 | import sys |
16 | 16 | import numpy as np |
17 | 17 | import base64 |
| 18 | +import warnings |
18 | 19 |
|
19 | 20 | from .. import xmlutils as xml |
20 | 21 | from ..filebasedimages import SerializableImage |
21 | 22 | from ..nifti1 import data_type_codes, xform_codes, intent_codes |
| 23 | +from ..caret import CaretMetaData |
22 | 24 | from .util import (array_index_order_codes, gifti_encoding_codes, |
23 | 25 | gifti_endian_codes, KIND2FMT) |
24 | 26 | from ..deprecated import deprecate_with_version |
25 | 27 |
|
26 | 28 |
|
27 | | -class GiftiMetaData(xml.XmlSerializable): |
| 29 | +class GiftiMetaData(CaretMetaData): |
28 | 30 | """ A sequence of GiftiNVPairs containing metadata for a gifti data array |
29 | 31 | """ |
30 | 32 |
|
31 | | - def __init__(self, nvpair=None): |
32 | | - self.data = [] |
33 | | - if nvpair is not None: |
34 | | - self.data.append(nvpair) |
| 33 | + def __init__(self, *args, **kwargs): |
| 34 | + dep_init = False |
| 35 | + # Positional arg |
| 36 | + dep_init |= not kwargs and len(args) == 1 and isinstance(args[0], GiftiNVPairs) |
| 37 | + # Keyword arg |
| 38 | + dep_init |= not args and list(kwargs) == ["nvpair"] |
| 39 | + if dep_init: |
| 40 | + warnings.warn( |
| 41 | + "GiftiMetaData now has a dict-like interface. " |
| 42 | + "See ``pydoc dict`` for initialization options. " |
| 43 | + "Passing ``GiftiNVPairs()`` or using the ``nvpair`` " |
| 44 | + "keyword will fail or behave unexpectedly in NiBabel 6.0.", |
| 45 | + FutureWarning, stacklevel=2) |
| 46 | + super().__init__() |
| 47 | + pair = args[0] if args else kwargs.get("nvpair") |
| 48 | + self[pair.name] = pair.value |
| 49 | + else: |
| 50 | + super().__init__(*args, **kwargs) |
| 51 | + |
| 52 | + @property |
| 53 | + def data(self): |
| 54 | + warnings.warn( |
| 55 | + "GiftiMetaData.data will be a dict in NiBabel 6.0.", |
| 56 | + FutureWarning, stacklevel=2) |
| 57 | + return [GiftiNVPairs(k, v) for k, v in self._data.items()] |
35 | 58 |
|
36 | 59 | @classmethod |
| 60 | + @deprecate_with_version( |
| 61 | + 'from_dict class method deprecated. Use GiftiMetaData directly.', |
| 62 | + '4.0', '6.0') |
37 | 63 | def from_dict(klass, data_dict): |
38 | | - meda = klass() |
39 | | - for k, v in data_dict.items(): |
40 | | - nv = GiftiNVPairs(k, v) |
41 | | - meda.data.append(nv) |
42 | | - return meda |
| 64 | + return klass(data_dict) |
43 | 65 |
|
44 | 66 | @deprecate_with_version( |
45 | 67 | 'get_metadata method deprecated. ' |
46 | 68 | "Use the metadata property instead.", |
47 | 69 | '2.1', '4.0') |
48 | 70 | def get_metadata(self): |
49 | | - return self.metadata |
| 71 | + return dict(self) |
50 | 72 |
|
51 | 73 | @property |
| 74 | + @deprecate_with_version( |
| 75 | + 'metadata property deprecated. Use GiftiMetadata object ' |
| 76 | + 'as dict or pass to dict() for a standard dictionary.', |
| 77 | + '4.0', '6.0') |
52 | 78 | def metadata(self): |
53 | 79 | """ Returns metadata as dictionary """ |
54 | | - self.data_as_dict = {} |
55 | | - for ele in self.data: |
56 | | - self.data_as_dict[ele.name] = ele.value |
57 | | - return self.data_as_dict |
58 | | - |
59 | | - def _to_xml_element(self): |
60 | | - metadata = xml.Element('MetaData') |
61 | | - for ele in self.data: |
62 | | - md = xml.SubElement(metadata, 'MD') |
63 | | - name = xml.SubElement(md, 'Name') |
64 | | - value = xml.SubElement(md, 'Value') |
65 | | - name.text = ele.name |
66 | | - value.text = ele.value |
67 | | - return metadata |
| 80 | + return dict(self) |
68 | 81 |
|
69 | 82 | def print_summary(self): |
70 | | - print(self.metadata) |
| 83 | + print(dict(self)) |
71 | 84 |
|
72 | 85 |
|
73 | | -class GiftiNVPairs(object): |
| 86 | +class GiftiNVPairs: |
74 | 87 | """ Gifti name / value pairs |
75 | 88 |
|
76 | 89 | Attributes |
@@ -385,7 +398,7 @@ def __init__(self, |
385 | 398 | self.ind_ord = array_index_order_codes.code[ordering] |
386 | 399 | self.meta = (GiftiMetaData() if meta is None else |
387 | 400 | meta if isinstance(meta, GiftiMetaData) else |
388 | | - GiftiMetaData.from_dict(meta)) |
| 401 | + GiftiMetaData(meta)) |
389 | 402 | self.ext_fname = ext_fname |
390 | 403 | self.ext_offset = ext_offset |
391 | 404 | self.dims = [] if self.data is None else list(self.data.shape) |
@@ -544,12 +557,12 @@ def print_summary(self): |
544 | 557 | "Use the metadata property instead.", |
545 | 558 | '2.1', '4.0') |
546 | 559 | def get_metadata(self): |
547 | | - return self.meta.metadata |
| 560 | + return dict(self.meta) |
548 | 561 |
|
549 | 562 | @property |
550 | 563 | def metadata(self): |
551 | 564 | """ Returns metadata as dictionary """ |
552 | | - return self.meta.metadata |
| 565 | + return dict(self.meta) |
553 | 566 |
|
554 | 567 |
|
555 | 568 | class GiftiImage(xml.XmlSerializable, SerializableImage): |
|
0 commit comments